xref: /onnv-gate/usr/src/cmd/fs.d/autofs/autod_nfs.c (revision 4590:1c2643f1a03c)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51676Sjpk  * Common Development and Distribution License (the "License").
61676Sjpk  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
223391Ssemery  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <stdio.h>
290Sstevel@tonic-gate #include <unistd.h>
300Sstevel@tonic-gate #include <stdlib.h>
310Sstevel@tonic-gate #include <ctype.h>
320Sstevel@tonic-gate #include <syslog.h>
330Sstevel@tonic-gate #include <string.h>
340Sstevel@tonic-gate #include <deflt.h>
350Sstevel@tonic-gate #include <kstat.h>
360Sstevel@tonic-gate #include <sys/param.h>
370Sstevel@tonic-gate #include <sys/types.h>
380Sstevel@tonic-gate #include <sys/time.h>
390Sstevel@tonic-gate #include <sys/stat.h>
400Sstevel@tonic-gate #include <sys/wait.h>
410Sstevel@tonic-gate #include <sys/socket.h>
420Sstevel@tonic-gate #include <netinet/in.h>
430Sstevel@tonic-gate #include <signal.h>
440Sstevel@tonic-gate #include <sys/signal.h>
450Sstevel@tonic-gate #include <rpc/rpc.h>
460Sstevel@tonic-gate #include <rpc/pmap_clnt.h>
470Sstevel@tonic-gate #include <sys/mount.h>
480Sstevel@tonic-gate #include <sys/mntent.h>
490Sstevel@tonic-gate #include <sys/mnttab.h>
500Sstevel@tonic-gate #include <sys/fstyp.h>
510Sstevel@tonic-gate #include <sys/fsid.h>
520Sstevel@tonic-gate #include <arpa/inet.h>
530Sstevel@tonic-gate #include <netdb.h>
540Sstevel@tonic-gate #include <netconfig.h>
550Sstevel@tonic-gate #include <netdir.h>
560Sstevel@tonic-gate #include <errno.h>
570Sstevel@tonic-gate #define	NFSCLIENT
580Sstevel@tonic-gate #include <nfs/nfs.h>
590Sstevel@tonic-gate #include <nfs/mount.h>
600Sstevel@tonic-gate #include <rpcsvc/mount.h>
610Sstevel@tonic-gate #include <rpc/nettype.h>
620Sstevel@tonic-gate #include <locale.h>
630Sstevel@tonic-gate #include <setjmp.h>
640Sstevel@tonic-gate #include <sys/socket.h>
650Sstevel@tonic-gate #include <thread.h>
660Sstevel@tonic-gate #include <limits.h>
670Sstevel@tonic-gate #include <nss_dbdefs.h>			/* for NSS_BUFLEN_HOSTS */
680Sstevel@tonic-gate #include <nfs/nfs_sec.h>
690Sstevel@tonic-gate #include <sys/sockio.h>
700Sstevel@tonic-gate #include <net/if.h>
710Sstevel@tonic-gate #include <assert.h>
720Sstevel@tonic-gate #include <nfs/nfs_clnt.h>
730Sstevel@tonic-gate #include <rpcsvc/nfs4_prot.h>
740Sstevel@tonic-gate #define	NO_RDDIR_CACHE
750Sstevel@tonic-gate #include "automount.h"
760Sstevel@tonic-gate #include "replica.h"
770Sstevel@tonic-gate #include "nfs_subr.h"
780Sstevel@tonic-gate #include "webnfs.h"
790Sstevel@tonic-gate #include <sys/sockio.h>
800Sstevel@tonic-gate #include <net/if.h>
810Sstevel@tonic-gate #include <assert.h>
820Sstevel@tonic-gate #include <rpcsvc/daemon_utils.h>
831676Sjpk #include <pwd.h>
841676Sjpk #include <strings.h>
851676Sjpk #include <tsol/label.h>
861676Sjpk #include <zone.h>
870Sstevel@tonic-gate 
880Sstevel@tonic-gate extern char *nfs_get_qop_name();
890Sstevel@tonic-gate extern AUTH *nfs_create_ah();
900Sstevel@tonic-gate extern enum snego_stat nfs_sec_nego();
910Sstevel@tonic-gate 
920Sstevel@tonic-gate #define	MAXHOSTS	512
930Sstevel@tonic-gate 
940Sstevel@tonic-gate /* number of transports to try */
950Sstevel@tonic-gate #define	MNT_PREF_LISTLEN	2
960Sstevel@tonic-gate #define	FIRST_TRY		1
970Sstevel@tonic-gate #define	SECOND_TRY		2
980Sstevel@tonic-gate 
990Sstevel@tonic-gate #define	MNTTYPE_CACHEFS "cachefs"
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate /*
1020Sstevel@tonic-gate  * host cache states
1030Sstevel@tonic-gate  */
1040Sstevel@tonic-gate #define	NOHOST		0
1050Sstevel@tonic-gate #define	GOODHOST	1
1060Sstevel@tonic-gate #define	DEADHOST	2
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate #define	NFS_ARGS_EXTB_secdata(args, secdata) \
1090Sstevel@tonic-gate 	{ (args).nfs_args_ext = NFS_ARGS_EXTB, \
1100Sstevel@tonic-gate 	(args).nfs_ext_u.nfs_extB.secdata = secdata; }
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate struct cache_entry {
1130Sstevel@tonic-gate 	struct	cache_entry *cache_next;
1140Sstevel@tonic-gate 	char	*cache_host;
1150Sstevel@tonic-gate 	time_t	cache_time;
1160Sstevel@tonic-gate 	int	cache_state;
1170Sstevel@tonic-gate 	rpcvers_t cache_reqvers;
1180Sstevel@tonic-gate 	rpcvers_t cache_outvers;
1190Sstevel@tonic-gate 	char	*cache_proto;
1200Sstevel@tonic-gate };
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate struct mfs_snego_t {
1230Sstevel@tonic-gate 	int sec_opt;
1240Sstevel@tonic-gate 	bool_t snego_done;
1250Sstevel@tonic-gate 	char *nfs_flavor;
1260Sstevel@tonic-gate 	seconfig_t nfs_sec;
1270Sstevel@tonic-gate };
1280Sstevel@tonic-gate typedef struct mfs_snego_t mfs_snego_t;
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate static struct cache_entry *cache_head = NULL;
1310Sstevel@tonic-gate rwlock_t cache_lock;	/* protect the cache chain */
1320Sstevel@tonic-gate 
1333391Ssemery static enum nfsstat nfsmount(struct mapfs *, char *, char *, int, int, uid_t,
1343391Ssemery 	action_list *);
1350Sstevel@tonic-gate static int is_nfs_port(char *);
1360Sstevel@tonic-gate 
1372170Sevanl void netbuf_free(struct netbuf *);
1382170Sevanl struct knetconfig *get_knconf(struct netconfig *);
1392170Sevanl void free_knconf(struct knetconfig *);
1400Sstevel@tonic-gate static int get_pathconf(CLIENT *, char *, char *, struct pathcnf **, int);
1410Sstevel@tonic-gate static struct mapfs *enum_servers(struct mapent *, char *);
1420Sstevel@tonic-gate static struct mapfs *get_mysubnet_servers(struct mapfs *);
1430Sstevel@tonic-gate static int subnet_test(int af, struct sioc_addrreq *);
1440Sstevel@tonic-gate static	struct	netbuf *get_addr(char *, rpcprog_t, rpcvers_t,
1450Sstevel@tonic-gate 	struct netconfig **, char *, ushort_t, struct t_info *);
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate static	struct	netbuf *get_pubfh(char *, rpcvers_t, mfs_snego_t *,
1480Sstevel@tonic-gate 	struct netconfig **, char *, ushort_t, struct t_info *, caddr_t *,
1490Sstevel@tonic-gate 	bool_t, char *);
1500Sstevel@tonic-gate 
1511676Sjpk static int create_homedir(const char *, const char *);
1521676Sjpk 
1530Sstevel@tonic-gate enum type_of_stuff {
1540Sstevel@tonic-gate 	SERVER_ADDR = 0,
1550Sstevel@tonic-gate 	SERVER_PING = 1,
1560Sstevel@tonic-gate 	SERVER_FH = 2
1570Sstevel@tonic-gate };
1580Sstevel@tonic-gate 
1592170Sevanl void *get_server_stuff(enum type_of_stuff, char *, rpcprog_t,
1600Sstevel@tonic-gate 	rpcvers_t, mfs_snego_t *, struct netconfig **, char *, ushort_t,
1610Sstevel@tonic-gate 	struct t_info *, caddr_t *, bool_t, char *, enum clnt_stat *);
1620Sstevel@tonic-gate 
1632170Sevanl void *get_the_stuff(enum type_of_stuff, char *, rpcprog_t,
1640Sstevel@tonic-gate 	rpcvers_t, mfs_snego_t *, struct netconfig *, ushort_t, struct t_info *,
1650Sstevel@tonic-gate 	caddr_t *, bool_t, char *, enum clnt_stat *);
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate struct mapfs *add_mfs(struct mapfs *, int, struct mapfs **, struct mapfs **);
1680Sstevel@tonic-gate void free_mfs(struct mapfs *);
1690Sstevel@tonic-gate static void dump_mfs(struct mapfs *, char *, int);
1700Sstevel@tonic-gate static char *dump_distance(struct mapfs *);
1710Sstevel@tonic-gate static void cache_free(struct cache_entry *);
1720Sstevel@tonic-gate static int cache_check(char *, rpcvers_t *, char *);
1730Sstevel@tonic-gate static void cache_enter(char *, rpcvers_t, rpcvers_t, char *, int);
1742170Sevanl void destroy_auth_client_handle(CLIENT *cl);
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate #ifdef CACHE_DEBUG
1770Sstevel@tonic-gate static void trace_host_cache();
1780Sstevel@tonic-gate static void trace_portmap_cache();
1790Sstevel@tonic-gate #endif /* CACHE_DEBUG */
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate static int rpc_timeout = 20;
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate #ifdef CACHE_DEBUG
1840Sstevel@tonic-gate /*
1850Sstevel@tonic-gate  * host cache counters. These variables do not need to be protected
1860Sstevel@tonic-gate  * by mutex's. They have been added to measure the utility of the
1870Sstevel@tonic-gate  * goodhost/deadhost cache in the lazy hierarchical mounting scheme.
1880Sstevel@tonic-gate  */
1890Sstevel@tonic-gate static int host_cache_accesses = 0;
1900Sstevel@tonic-gate static int host_cache_lookups = 0;
1910Sstevel@tonic-gate static int deadhost_cache_hits = 0;
1920Sstevel@tonic-gate static int goodhost_cache_hits = 0;
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate /*
1950Sstevel@tonic-gate  * portmap cache counters. These variables do not need to be protected
1960Sstevel@tonic-gate  * by mutex's. They have been added to measure the utility of the portmap
1970Sstevel@tonic-gate  * cache in the lazy hierarchical mounting scheme.
1980Sstevel@tonic-gate  */
1990Sstevel@tonic-gate static int portmap_cache_accesses = 0;
2000Sstevel@tonic-gate static int portmap_cache_lookups = 0;
2010Sstevel@tonic-gate static int portmap_cache_hits = 0;
2020Sstevel@tonic-gate #endif /* CACHE_DEBUG */
2030Sstevel@tonic-gate 
2040Sstevel@tonic-gate /*
2050Sstevel@tonic-gate  * There are the defaults (range) for the client when determining
2060Sstevel@tonic-gate  * which NFS version to use when probing the server (see above).
2070Sstevel@tonic-gate  * These will only be used when the vers mount option is not used and
2080Sstevel@tonic-gate  * these may be reset if /etc/default/nfs is configured to do so.
2090Sstevel@tonic-gate  */
2100Sstevel@tonic-gate static rpcvers_t vers_max_default = NFS_VERSMAX_DEFAULT;
2110Sstevel@tonic-gate static rpcvers_t vers_min_default = NFS_VERSMIN_DEFAULT;
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate /*
2140Sstevel@tonic-gate  * list of support services needed
2150Sstevel@tonic-gate  */
2160Sstevel@tonic-gate static char	*service_list[] = { STATD, LOCKD, NULL };
2170Sstevel@tonic-gate static char	*service_list_v4[] = { STATD, LOCKD, NFS4CBD, NFSMAPID, NULL };
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate static void read_default_nfs(void);
2200Sstevel@tonic-gate static int is_v4_mount(char *);
2210Sstevel@tonic-gate static void start_nfs4cbd(void);
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate int
2242170Sevanl mount_nfs(
2252170Sevanl 	struct mapent *me,
2262170Sevanl 	char *mntpnt,
2272170Sevanl 	char *prevhost,
2282170Sevanl 	int overlay,
2293391Ssemery 	uid_t uid,
2302170Sevanl 	action_list **alpp)
2310Sstevel@tonic-gate {
2320Sstevel@tonic-gate 	struct mapfs *mfs, *mp;
2330Sstevel@tonic-gate 	int err = -1;
2340Sstevel@tonic-gate 	int cached;
2352170Sevanl 	action_list *alp;
2362170Sevanl 
2372170Sevanl 
2382170Sevanl 	alp = *alpp;
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate 	read_default_nfs();
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate 	mfs = enum_servers(me, prevhost);
2430Sstevel@tonic-gate 	if (mfs == NULL)
2440Sstevel@tonic-gate 		return (ENOENT);
2450Sstevel@tonic-gate 
2460Sstevel@tonic-gate 	/*
2470Sstevel@tonic-gate 	 * Try loopback if we have something on localhost; if nothing
2480Sstevel@tonic-gate 	 * works, we will fall back to NFS
2490Sstevel@tonic-gate 	 */
2500Sstevel@tonic-gate 	if (is_nfs_port(me->map_mntopts)) {
2510Sstevel@tonic-gate 		for (mp = mfs; mp; mp = mp->mfs_next) {
2520Sstevel@tonic-gate 			if (self_check(mp->mfs_host)) {
2530Sstevel@tonic-gate 				err = loopbackmount(mp->mfs_dir,
254*4590Srm15945 				    mntpnt, me->map_mntopts, overlay);
2550Sstevel@tonic-gate 				if (err) {
2560Sstevel@tonic-gate 					mp->mfs_ignore = 1;
2570Sstevel@tonic-gate 				} else {
2582170Sevanl 					/*
2592170Sevanl 					 * Free action_list if there
2602170Sevanl 					 * is one as it is not needed.
2612170Sevanl 					 * Make sure to set alpp to null
2622170Sevanl 					 * so caller doesn't try to free it
2632170Sevanl 					 * again.
2642170Sevanl 					 */
2652170Sevanl 					if (*alpp) {
2662170Sevanl 						free(*alpp);
2672170Sevanl 						*alpp = NULL;
2682170Sevanl 					}
2690Sstevel@tonic-gate 					break;
2700Sstevel@tonic-gate 				}
2710Sstevel@tonic-gate 			}
2720Sstevel@tonic-gate 		}
2730Sstevel@tonic-gate 	}
2740Sstevel@tonic-gate 	if (err) {
2750Sstevel@tonic-gate 		cached = strcmp(me->map_mounter, MNTTYPE_CACHEFS) == 0;
2760Sstevel@tonic-gate 		err = nfsmount(mfs, mntpnt, me->map_mntopts,
277*4590Srm15945 		    cached, overlay, uid, alp);
2780Sstevel@tonic-gate 		if (err && trace > 1) {
2790Sstevel@tonic-gate 			trace_prt(1, "	Couldn't mount %s:%s, err=%d\n",
280*4590Srm15945 			    mfs->mfs_host, mfs->mfs_dir, err);
2810Sstevel@tonic-gate 		}
2820Sstevel@tonic-gate 	}
2830Sstevel@tonic-gate 	free_mfs(mfs);
2840Sstevel@tonic-gate 	return (err);
2850Sstevel@tonic-gate }
2860Sstevel@tonic-gate 
2870Sstevel@tonic-gate 
2880Sstevel@tonic-gate /*
2890Sstevel@tonic-gate  * Using the new ioctl SIOCTONLINK to determine if a host is on the same
2900Sstevel@tonic-gate  * subnet. Remove the old network, subnet check.
2910Sstevel@tonic-gate  */
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate static struct mapfs *
2940Sstevel@tonic-gate get_mysubnet_servers(struct mapfs *mfs_in)
2950Sstevel@tonic-gate {
2960Sstevel@tonic-gate 	int s;
2970Sstevel@tonic-gate 	struct mapfs *mfs, *p, *mfs_head = NULL, *mfs_tail = NULL;
2980Sstevel@tonic-gate 
2990Sstevel@tonic-gate 	struct netconfig *nconf;
3000Sstevel@tonic-gate 	NCONF_HANDLE *nc = NULL;
3010Sstevel@tonic-gate 	struct nd_hostserv hs;
3020Sstevel@tonic-gate 	struct nd_addrlist *retaddrs;
3030Sstevel@tonic-gate 	struct netbuf *nb;
3040Sstevel@tonic-gate 	struct sioc_addrreq areq;
3050Sstevel@tonic-gate 	int res;
3060Sstevel@tonic-gate 	int af;
3070Sstevel@tonic-gate 	int i;
3080Sstevel@tonic-gate 	int sa_size;
3090Sstevel@tonic-gate 
3100Sstevel@tonic-gate 	hs.h_serv = "rpcbind";
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate 	for (mfs = mfs_in; mfs; mfs = mfs->mfs_next) {
3130Sstevel@tonic-gate 		nc = setnetconfig();
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate 		while (nconf = getnetconfig(nc)) {
3160Sstevel@tonic-gate 
3170Sstevel@tonic-gate 			/*
3180Sstevel@tonic-gate 			 * Care about INET family only. proto_done flag
3190Sstevel@tonic-gate 			 * indicates if we have already covered this
3200Sstevel@tonic-gate 			 * protocol family. If so skip it
3210Sstevel@tonic-gate 			 */
3220Sstevel@tonic-gate 			if (((strcmp(nconf->nc_protofmly, NC_INET6) == 0) ||
323*4590Srm15945 			    (strcmp(nconf->nc_protofmly, NC_INET) == 0)) &&
324*4590Srm15945 			    (nconf->nc_semantics == NC_TPI_CLTS)) {
3250Sstevel@tonic-gate 			} else
3260Sstevel@tonic-gate 				continue;
3270Sstevel@tonic-gate 
3280Sstevel@tonic-gate 			hs.h_host = mfs->mfs_host;
3290Sstevel@tonic-gate 
3300Sstevel@tonic-gate 			if (netdir_getbyname(nconf, &hs, &retaddrs) != ND_OK)
3310Sstevel@tonic-gate 				continue;
3320Sstevel@tonic-gate 
3330Sstevel@tonic-gate 			/*
3340Sstevel@tonic-gate 			 * For each host address see if it's on our
3350Sstevel@tonic-gate 			 * local subnet.
3360Sstevel@tonic-gate 			 */
3370Sstevel@tonic-gate 
3380Sstevel@tonic-gate 			if (strcmp(nconf->nc_protofmly, NC_INET6) == 0)
3390Sstevel@tonic-gate 				af = AF_INET6;
3400Sstevel@tonic-gate 			else
3410Sstevel@tonic-gate 				af = AF_INET;
3420Sstevel@tonic-gate 			nb = retaddrs->n_addrs;
3430Sstevel@tonic-gate 			for (i = 0; i < retaddrs->n_cnt; i++, nb++) {
3440Sstevel@tonic-gate 				memset(&areq.sa_addr, 0, sizeof (areq.sa_addr));
3450Sstevel@tonic-gate 				memcpy(&areq.sa_addr, nb->buf, MIN(nb->len,
346*4590Srm15945 				    sizeof (areq.sa_addr)));
3470Sstevel@tonic-gate 				if (res = subnet_test(af, &areq)) {
3480Sstevel@tonic-gate 					p = add_mfs(mfs, DIST_MYNET,
349*4590Srm15945 					    &mfs_head, &mfs_tail);
3500Sstevel@tonic-gate 					if (!p) {
3510Sstevel@tonic-gate 						netdir_free(retaddrs,
352*4590Srm15945 						    ND_ADDRLIST);
3530Sstevel@tonic-gate 						endnetconfig(nc);
3540Sstevel@tonic-gate 						return (NULL);
3550Sstevel@tonic-gate 					}
3560Sstevel@tonic-gate 					break;
3570Sstevel@tonic-gate 				}
3580Sstevel@tonic-gate 			}  /* end of every host */
3590Sstevel@tonic-gate 			if (trace > 2) {
3600Sstevel@tonic-gate 				trace_prt(1, "get_mysubnet_servers: host=%s "
361*4590Srm15945 				    "netid=%s res=%s\n", mfs->mfs_host,
362*4590Srm15945 				    nconf->nc_netid, res == 1?"SUC":"FAIL");
3630Sstevel@tonic-gate 			}
3640Sstevel@tonic-gate 
3650Sstevel@tonic-gate 			netdir_free(retaddrs, ND_ADDRLIST);
3660Sstevel@tonic-gate 		} /* end of while */
3670Sstevel@tonic-gate 
3680Sstevel@tonic-gate 		endnetconfig(nc);
3690Sstevel@tonic-gate 
3700Sstevel@tonic-gate 	} /* end of every map */
3710Sstevel@tonic-gate 
3720Sstevel@tonic-gate 	return (mfs_head);
3730Sstevel@tonic-gate 
3740Sstevel@tonic-gate }
3750Sstevel@tonic-gate 
3760Sstevel@tonic-gate int
3770Sstevel@tonic-gate subnet_test(int af, struct sioc_addrreq *areq)
3780Sstevel@tonic-gate {
3790Sstevel@tonic-gate 	int s;
3800Sstevel@tonic-gate 
3810Sstevel@tonic-gate 	if ((s = socket(af, SOCK_DGRAM, 0)) < 0) {
3820Sstevel@tonic-gate 		return (0);
3830Sstevel@tonic-gate 	}
3840Sstevel@tonic-gate 
3850Sstevel@tonic-gate 	areq->sa_res = -1;
3860Sstevel@tonic-gate 
3870Sstevel@tonic-gate 	if (ioctl(s, SIOCTONLINK, (caddr_t)areq) < 0) {
3880Sstevel@tonic-gate 		syslog(LOG_ERR, "subnet_test:SIOCTONLINK failed");
3890Sstevel@tonic-gate 		return (0);
3900Sstevel@tonic-gate 	}
3910Sstevel@tonic-gate 	close(s);
3920Sstevel@tonic-gate 	if (areq->sa_res == 1)
3930Sstevel@tonic-gate 		return (1);
3940Sstevel@tonic-gate 	else
3950Sstevel@tonic-gate 		return (0);
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate 
3980Sstevel@tonic-gate }
3990Sstevel@tonic-gate 
4000Sstevel@tonic-gate /*
4010Sstevel@tonic-gate  * ping a bunch of hosts at once and sort by who responds first
4020Sstevel@tonic-gate  */
4030Sstevel@tonic-gate static struct mapfs *
4040Sstevel@tonic-gate sort_servers(struct mapfs *mfs_in, int timeout)
4050Sstevel@tonic-gate {
4060Sstevel@tonic-gate 	struct mapfs *m1 = NULL;
4070Sstevel@tonic-gate 	enum clnt_stat clnt_stat;
4080Sstevel@tonic-gate 
4090Sstevel@tonic-gate 	if (!mfs_in)
4100Sstevel@tonic-gate 		return (NULL);
4110Sstevel@tonic-gate 
4120Sstevel@tonic-gate 	clnt_stat = nfs_cast(mfs_in, &m1, timeout);
4130Sstevel@tonic-gate 
4140Sstevel@tonic-gate 	if (!m1) {
4150Sstevel@tonic-gate 		char buff[2048] = {'\0'};
4160Sstevel@tonic-gate 
4170Sstevel@tonic-gate 		for (m1 = mfs_in; m1; m1 = m1->mfs_next) {
4180Sstevel@tonic-gate 			(void) strcat(buff, m1->mfs_host);
4190Sstevel@tonic-gate 			if (m1->mfs_next)
4200Sstevel@tonic-gate 				(void) strcat(buff, ",");
4210Sstevel@tonic-gate 		}
4220Sstevel@tonic-gate 
4230Sstevel@tonic-gate 		syslog(LOG_ERR, "servers %s not responding: %s",
424*4590Srm15945 		    buff, clnt_sperrno(clnt_stat));
4250Sstevel@tonic-gate 	}
4260Sstevel@tonic-gate 
4270Sstevel@tonic-gate 	return (m1);
4280Sstevel@tonic-gate }
4290Sstevel@tonic-gate 
4300Sstevel@tonic-gate /*
4310Sstevel@tonic-gate  * Add a mapfs entry to the list described by *mfs_head and *mfs_tail,
4320Sstevel@tonic-gate  * provided it is not marked "ignored" and isn't a dupe of ones we've
4330Sstevel@tonic-gate  * already seen.
4340Sstevel@tonic-gate  */
4350Sstevel@tonic-gate struct mapfs *
4360Sstevel@tonic-gate add_mfs(struct mapfs *mfs, int distance, struct mapfs **mfs_head,
4370Sstevel@tonic-gate 	struct mapfs **mfs_tail)
4380Sstevel@tonic-gate {
4390Sstevel@tonic-gate 	struct mapfs *tmp, *new;
4400Sstevel@tonic-gate 
4410Sstevel@tonic-gate 	for (tmp = *mfs_head; tmp; tmp = tmp->mfs_next)
4420Sstevel@tonic-gate 		if ((strcmp(tmp->mfs_host, mfs->mfs_host) == 0 &&
4430Sstevel@tonic-gate 		    strcmp(tmp->mfs_dir, mfs->mfs_dir) == 0) ||
444*4590Srm15945 		    mfs->mfs_ignore)
4450Sstevel@tonic-gate 			return (*mfs_head);
4460Sstevel@tonic-gate 	new = (struct mapfs *)malloc(sizeof (struct mapfs));
4470Sstevel@tonic-gate 	if (!new) {
4480Sstevel@tonic-gate 		syslog(LOG_ERR, "Memory allocation failed: %m");
4490Sstevel@tonic-gate 		return (NULL);
4500Sstevel@tonic-gate 	}
4510Sstevel@tonic-gate 	bcopy(mfs, new, sizeof (struct mapfs));
4520Sstevel@tonic-gate 	new->mfs_next = NULL;
4530Sstevel@tonic-gate 	if (distance)
4540Sstevel@tonic-gate 		new->mfs_distance = distance;
4550Sstevel@tonic-gate 	if (!*mfs_head)
4560Sstevel@tonic-gate 		*mfs_tail = *mfs_head = new;
4570Sstevel@tonic-gate 	else {
4580Sstevel@tonic-gate 		(*mfs_tail)->mfs_next = new;
4590Sstevel@tonic-gate 		*mfs_tail = new;
4600Sstevel@tonic-gate 	}
4610Sstevel@tonic-gate 	return (*mfs_head);
4620Sstevel@tonic-gate }
4630Sstevel@tonic-gate 
4640Sstevel@tonic-gate static void
4650Sstevel@tonic-gate dump_mfs(struct mapfs *mfs, char *message, int level)
4660Sstevel@tonic-gate {
4670Sstevel@tonic-gate 	struct mapfs *m1;
4680Sstevel@tonic-gate 
4690Sstevel@tonic-gate 	if (trace <= level)
4700Sstevel@tonic-gate 		return;
4710Sstevel@tonic-gate 
4720Sstevel@tonic-gate 	trace_prt(1, "%s", message);
4730Sstevel@tonic-gate 	if (!mfs) {
4740Sstevel@tonic-gate 		trace_prt(0, "mfs is null\n");
4750Sstevel@tonic-gate 		return;
4760Sstevel@tonic-gate 	}
4770Sstevel@tonic-gate 	for (m1 = mfs; m1; m1 = m1->mfs_next)
4780Sstevel@tonic-gate 		trace_prt(0, "%s[%s] ", m1->mfs_host, dump_distance(m1));
4790Sstevel@tonic-gate 	trace_prt(0, "\n");
4800Sstevel@tonic-gate }
4810Sstevel@tonic-gate 
4820Sstevel@tonic-gate static char *
4830Sstevel@tonic-gate dump_distance(struct mapfs *mfs)
4840Sstevel@tonic-gate {
4850Sstevel@tonic-gate 	switch (mfs->mfs_distance) {
4860Sstevel@tonic-gate 	case 0:			return ("zero");
4870Sstevel@tonic-gate 	case DIST_SELF:		return ("self");
4880Sstevel@tonic-gate 	case DIST_MYSUB:	return ("mysub");
4890Sstevel@tonic-gate 	case DIST_MYNET:	return ("mynet");
4900Sstevel@tonic-gate 	case DIST_OTHER:	return ("other");
4910Sstevel@tonic-gate 	default:		return ("other");
4920Sstevel@tonic-gate 	}
4930Sstevel@tonic-gate }
4940Sstevel@tonic-gate 
4950Sstevel@tonic-gate /*
4960Sstevel@tonic-gate  * Walk linked list "raw", building a new list consisting of members
4970Sstevel@tonic-gate  * NOT found in list "filter", returning the result.
4980Sstevel@tonic-gate  */
4990Sstevel@tonic-gate static struct mapfs *
5000Sstevel@tonic-gate filter_mfs(struct mapfs *raw, struct mapfs *filter)
5010Sstevel@tonic-gate {
5020Sstevel@tonic-gate 	struct mapfs *mfs, *p, *mfs_head = NULL, *mfs_tail = NULL;
5030Sstevel@tonic-gate 	int skip;
5040Sstevel@tonic-gate 
5050Sstevel@tonic-gate 	if (!raw)
5060Sstevel@tonic-gate 		return (NULL);
5070Sstevel@tonic-gate 	for (mfs = raw; mfs; mfs = mfs->mfs_next) {
5080Sstevel@tonic-gate 		for (skip = 0, p = filter; p; p = p->mfs_next) {
5090Sstevel@tonic-gate 			if (strcmp(p->mfs_host, mfs->mfs_host) == 0 &&
5100Sstevel@tonic-gate 			    strcmp(p->mfs_dir, mfs->mfs_dir) == 0) {
5110Sstevel@tonic-gate 				skip = 1;
5120Sstevel@tonic-gate 				break;
5130Sstevel@tonic-gate 			}
5140Sstevel@tonic-gate 		}
5150Sstevel@tonic-gate 		if (skip)
5160Sstevel@tonic-gate 			continue;
5170Sstevel@tonic-gate 		p = add_mfs(mfs, 0, &mfs_head, &mfs_tail);
5180Sstevel@tonic-gate 		if (!p)
5190Sstevel@tonic-gate 			return (NULL);
5200Sstevel@tonic-gate 	}
5210Sstevel@tonic-gate 	return (mfs_head);
5220Sstevel@tonic-gate }
5230Sstevel@tonic-gate 
5240Sstevel@tonic-gate /*
5250Sstevel@tonic-gate  * Walk a linked list of mapfs structs, freeing each member.
5260Sstevel@tonic-gate  */
5270Sstevel@tonic-gate void
5280Sstevel@tonic-gate free_mfs(struct mapfs *mfs)
5290Sstevel@tonic-gate {
5300Sstevel@tonic-gate 	struct mapfs *tmp;
5310Sstevel@tonic-gate 
5320Sstevel@tonic-gate 	while (mfs) {
5330Sstevel@tonic-gate 		tmp = mfs->mfs_next;
5340Sstevel@tonic-gate 		free(mfs);
5350Sstevel@tonic-gate 		mfs = tmp;
5360Sstevel@tonic-gate 	}
5370Sstevel@tonic-gate }
5380Sstevel@tonic-gate 
5390Sstevel@tonic-gate /*
5400Sstevel@tonic-gate  * New code for NFS client failover: we need to carry and sort
5410Sstevel@tonic-gate  * lists of server possibilities rather than return a single
5420Sstevel@tonic-gate  * entry.  It preserves previous behaviour of sorting first by
5430Sstevel@tonic-gate  * locality (loopback-or-preferred/subnet/net/other) and then
5440Sstevel@tonic-gate  * by ping times.  We'll short-circuit this process when we
5450Sstevel@tonic-gate  * have ENOUGH or more entries.
5460Sstevel@tonic-gate  */
5470Sstevel@tonic-gate static struct mapfs *
5480Sstevel@tonic-gate enum_servers(struct mapent *me, char *preferred)
5490Sstevel@tonic-gate {
5500Sstevel@tonic-gate 	struct mapfs *p, *m1, *m2, *mfs_head = NULL, *mfs_tail = NULL;
5510Sstevel@tonic-gate 
5520Sstevel@tonic-gate 	/*
5530Sstevel@tonic-gate 	 * Short-circuit for simple cases.
5540Sstevel@tonic-gate 	 */
5550Sstevel@tonic-gate 	if (!me->map_fs->mfs_next) {
5560Sstevel@tonic-gate 		p = add_mfs(me->map_fs, DIST_OTHER, &mfs_head, &mfs_tail);
5570Sstevel@tonic-gate 		if (!p)
5580Sstevel@tonic-gate 			return (NULL);
5590Sstevel@tonic-gate 		return (mfs_head);
5600Sstevel@tonic-gate 	}
5610Sstevel@tonic-gate 
5620Sstevel@tonic-gate 	dump_mfs(me->map_fs, "	enum_servers: mapent: ", 2);
5630Sstevel@tonic-gate 
5640Sstevel@tonic-gate 	/*
5650Sstevel@tonic-gate 	 * get addresses & see if any are myself
5660Sstevel@tonic-gate 	 * or were mounted from previously in a
5670Sstevel@tonic-gate 	 * hierarchical mount.
5680Sstevel@tonic-gate 	 */
5690Sstevel@tonic-gate 	if (trace > 2)
5700Sstevel@tonic-gate 		trace_prt(1, "	enum_servers: looking for pref/self\n");
5710Sstevel@tonic-gate 	for (m1 = me->map_fs; m1; m1 = m1->mfs_next) {
5720Sstevel@tonic-gate 		if (m1->mfs_ignore)
5730Sstevel@tonic-gate 			continue;
5740Sstevel@tonic-gate 		if (self_check(m1->mfs_host) ||
5750Sstevel@tonic-gate 		    strcmp(m1->mfs_host, preferred) == 0) {
5760Sstevel@tonic-gate 			p = add_mfs(m1, DIST_SELF, &mfs_head, &mfs_tail);
5770Sstevel@tonic-gate 			if (!p)
5780Sstevel@tonic-gate 				return (NULL);
5790Sstevel@tonic-gate 		}
5800Sstevel@tonic-gate 	}
5810Sstevel@tonic-gate 	if (trace > 2 && m1)
5820Sstevel@tonic-gate 		trace_prt(1, "	enum_servers: pref/self found, %s\n",
583*4590Srm15945 		    m1->mfs_host);
5840Sstevel@tonic-gate 
5850Sstevel@tonic-gate 	/*
5860Sstevel@tonic-gate 	 * look for entries on this subnet
5870Sstevel@tonic-gate 	 */
5880Sstevel@tonic-gate 	dump_mfs(m1, "	enum_servers: input of get_mysubnet_servers: ", 2);
5890Sstevel@tonic-gate 	m1 = get_mysubnet_servers(me->map_fs);
5900Sstevel@tonic-gate 	dump_mfs(m1, "	enum_servers: output of get_mysubnet_servers: ", 3);
5910Sstevel@tonic-gate 	if (m1 && m1->mfs_next) {
5920Sstevel@tonic-gate 		m2 = sort_servers(m1, rpc_timeout / 2);
5930Sstevel@tonic-gate 		dump_mfs(m2, "	enum_servers: output of sort_servers: ", 3);
5940Sstevel@tonic-gate 		free_mfs(m1);
5950Sstevel@tonic-gate 		m1 = m2;
5960Sstevel@tonic-gate 	}
5970Sstevel@tonic-gate 
5980Sstevel@tonic-gate 	for (m2 = m1; m2; m2 = m2->mfs_next) {
5990Sstevel@tonic-gate 		p = add_mfs(m2, 0, &mfs_head, &mfs_tail);
6000Sstevel@tonic-gate 		if (!p)
6010Sstevel@tonic-gate 			return (NULL);
6020Sstevel@tonic-gate 	}
6030Sstevel@tonic-gate 	if (m1)
6040Sstevel@tonic-gate 		free_mfs(m1);
6050Sstevel@tonic-gate 
6060Sstevel@tonic-gate 	/*
6070Sstevel@tonic-gate 	 * add the rest of the entries at the end
6080Sstevel@tonic-gate 	 */
6090Sstevel@tonic-gate 	m1 = filter_mfs(me->map_fs, mfs_head);
6100Sstevel@tonic-gate 	dump_mfs(m1, "	enum_servers: etc: output of filter_mfs: ", 3);
6110Sstevel@tonic-gate 	m2 = sort_servers(m1, rpc_timeout / 2);
6120Sstevel@tonic-gate 	dump_mfs(m2, "	enum_servers: etc: output of sort_servers: ", 3);
6130Sstevel@tonic-gate 	if (m1)
6140Sstevel@tonic-gate 		free_mfs(m1);
6150Sstevel@tonic-gate 	m1 = m2;
6160Sstevel@tonic-gate 	for (m2 = m1; m2; m2 = m2->mfs_next) {
6170Sstevel@tonic-gate 		p = add_mfs(m2, DIST_OTHER, &mfs_head, &mfs_tail);
6180Sstevel@tonic-gate 		if (!p)
6190Sstevel@tonic-gate 			return (NULL);
6200Sstevel@tonic-gate 	}
6210Sstevel@tonic-gate 	if (m1)
6220Sstevel@tonic-gate 		free_mfs(m1);
6230Sstevel@tonic-gate 
6240Sstevel@tonic-gate done:
6250Sstevel@tonic-gate 	dump_mfs(mfs_head, "  enum_servers: output: ", 1);
6260Sstevel@tonic-gate 	return (mfs_head);
6270Sstevel@tonic-gate }
6280Sstevel@tonic-gate 
6290Sstevel@tonic-gate static enum nfsstat
6302170Sevanl nfsmount(
6312170Sevanl 	struct mapfs *mfs_in,
6322170Sevanl 	char *mntpnt, char *opts,
6332170Sevanl 	int cached, int overlay,
6343391Ssemery 	uid_t uid,
6352170Sevanl 	action_list *alp)
6360Sstevel@tonic-gate {
6370Sstevel@tonic-gate 	CLIENT *cl;
6380Sstevel@tonic-gate 	char remname[MAXPATHLEN], *mnttabtext = NULL;
6390Sstevel@tonic-gate 	char mopts[MAX_MNTOPT_STR];
6400Sstevel@tonic-gate 	char netname[MAXNETNAMELEN+1];
6412170Sevanl 	char	*mntopts = NULL;
6420Sstevel@tonic-gate 	int mnttabcnt = 0;
6430Sstevel@tonic-gate 	int loglevel;
6440Sstevel@tonic-gate 	struct mnttab m;
6450Sstevel@tonic-gate 	struct nfs_args *argp = NULL, *head = NULL, *tail = NULL,
646*4590Srm15945 	    *prevhead, *prevtail;
6470Sstevel@tonic-gate 	int flags;
6480Sstevel@tonic-gate 	struct fhstatus fhs;
6490Sstevel@tonic-gate 	struct timeval timeout;
6500Sstevel@tonic-gate 	enum clnt_stat rpc_stat;
6510Sstevel@tonic-gate 	enum nfsstat status;
6520Sstevel@tonic-gate 	struct stat stbuf;
6530Sstevel@tonic-gate 	struct netconfig *nconf;
6540Sstevel@tonic-gate 	rpcvers_t vers, versmin; /* used to negotiate nfs version in pingnfs */
6550Sstevel@tonic-gate 				/* and mount version with mountd */
6560Sstevel@tonic-gate 	rpcvers_t outvers;	/* final version to be used during mount() */
6570Sstevel@tonic-gate 	rpcvers_t nfsvers;	/* version in map options, 0 if not there */
6580Sstevel@tonic-gate 	rpcvers_t mountversmax;	/* tracks the max mountvers during retries */
6590Sstevel@tonic-gate 
6600Sstevel@tonic-gate 	/* used to negotiate nfs version using webnfs */
6610Sstevel@tonic-gate 	rpcvers_t pubvers, pubversmin, pubversmax;
6620Sstevel@tonic-gate 	int posix;
6630Sstevel@tonic-gate 	struct nd_addrlist *retaddrs;
6640Sstevel@tonic-gate 	struct mountres3 res3;
6650Sstevel@tonic-gate 	nfs_fh3 fh3;
6660Sstevel@tonic-gate 	char *fstype;
6670Sstevel@tonic-gate 	int count, i;
6680Sstevel@tonic-gate 	char scerror_msg[MAXMSGLEN];
6690Sstevel@tonic-gate 	int *auths;
6700Sstevel@tonic-gate 	int delay;
6710Sstevel@tonic-gate 	int retries;
6720Sstevel@tonic-gate 	char *nfs_proto = NULL;
6730Sstevel@tonic-gate 	uint_t nfs_port = 0;
6743767Sjs195444 	char *p, *host, *rhost, *dir;
6750Sstevel@tonic-gate 	struct mapfs *mfs = NULL;
6760Sstevel@tonic-gate 	int error, last_error = 0;
6770Sstevel@tonic-gate 	int replicated;
6780Sstevel@tonic-gate 	int entries = 0;
6790Sstevel@tonic-gate 	int v2cnt = 0, v3cnt = 0, v4cnt = 0;
6800Sstevel@tonic-gate 	int v2near = 0, v3near = 0, v4near = 0;
6810Sstevel@tonic-gate 	int skipentry = 0;
6820Sstevel@tonic-gate 	char *nfs_flavor;
6830Sstevel@tonic-gate 	seconfig_t nfs_sec;
6840Sstevel@tonic-gate 	int sec_opt, scerror;
6850Sstevel@tonic-gate 	struct sec_data *secdata;
6860Sstevel@tonic-gate 	int secflags;
6870Sstevel@tonic-gate 	struct netbuf *syncaddr;
6880Sstevel@tonic-gate 	bool_t	use_pubfh;
6890Sstevel@tonic-gate 	ushort_t thisport;
6900Sstevel@tonic-gate 	int got_val;
6910Sstevel@tonic-gate 	mfs_snego_t mfssnego_init, mfssnego;
6920Sstevel@tonic-gate 
6930Sstevel@tonic-gate 	dump_mfs(mfs_in, "  nfsmount: input: ", 2);
6940Sstevel@tonic-gate 	replicated = (mfs_in->mfs_next != NULL);
6950Sstevel@tonic-gate 	m.mnt_mntopts = opts;
6960Sstevel@tonic-gate 	if (replicated && hasmntopt(&m, MNTOPT_SOFT)) {
6970Sstevel@tonic-gate 		if (verbose)
6980Sstevel@tonic-gate 			syslog(LOG_WARNING,
6990Sstevel@tonic-gate 		    "mount on %s is soft and will not be replicated.", mntpnt);
7000Sstevel@tonic-gate 		replicated = 0;
7010Sstevel@tonic-gate 	}
7020Sstevel@tonic-gate 	if (replicated && !hasmntopt(&m, MNTOPT_RO)) {
7030Sstevel@tonic-gate 		if (verbose)
7040Sstevel@tonic-gate 			syslog(LOG_WARNING,
7050Sstevel@tonic-gate 		    "mount on %s is not read-only and will not be replicated.",
7060Sstevel@tonic-gate 			    mntpnt);
7070Sstevel@tonic-gate 		replicated = 0;
7080Sstevel@tonic-gate 	}
7090Sstevel@tonic-gate 	if (replicated && cached) {
7100Sstevel@tonic-gate 		if (verbose)
7110Sstevel@tonic-gate 			syslog(LOG_WARNING,
7120Sstevel@tonic-gate 		    "mount on %s is cached and will not be replicated.",
713*4590Srm15945 			    mntpnt);
7140Sstevel@tonic-gate 		replicated = 0;
7150Sstevel@tonic-gate 	}
7160Sstevel@tonic-gate 	if (replicated)
7170Sstevel@tonic-gate 		loglevel = LOG_WARNING;
7180Sstevel@tonic-gate 	else
7190Sstevel@tonic-gate 		loglevel = LOG_ERR;
7200Sstevel@tonic-gate 
7210Sstevel@tonic-gate 	if (trace > 1) {
7220Sstevel@tonic-gate 		if (replicated)
7230Sstevel@tonic-gate 			trace_prt(1, "	nfsmount: replicated mount on %s %s:\n",
724*4590Srm15945 			    mntpnt, opts);
7250Sstevel@tonic-gate 		else
7260Sstevel@tonic-gate 			trace_prt(1, "	nfsmount: standard mount on %s %s:\n",
727*4590Srm15945 			    mntpnt, opts);
7280Sstevel@tonic-gate 		for (mfs = mfs_in; mfs; mfs = mfs->mfs_next)
7290Sstevel@tonic-gate 			trace_prt(1, "	  %s:%s\n",
730*4590Srm15945 			    mfs->mfs_host, mfs->mfs_dir);
7310Sstevel@tonic-gate 	}
7320Sstevel@tonic-gate 
7330Sstevel@tonic-gate 	/*
7340Sstevel@tonic-gate 	 * Make sure mountpoint is safe to mount on
7350Sstevel@tonic-gate 	 */
7360Sstevel@tonic-gate 	if (lstat(mntpnt, &stbuf) < 0) {
7370Sstevel@tonic-gate 		syslog(LOG_ERR, "Couldn't stat %s: %m", mntpnt);
7380Sstevel@tonic-gate 		return (NFSERR_NOENT);
7390Sstevel@tonic-gate 	}
7400Sstevel@tonic-gate 
7410Sstevel@tonic-gate 	/*
7420Sstevel@tonic-gate 	 * Get protocol specified in options list, if any.
7430Sstevel@tonic-gate 	 */
7440Sstevel@tonic-gate 	if ((str_opt(&m, "proto", &nfs_proto)) == -1) {
7450Sstevel@tonic-gate 		return (NFSERR_NOENT);
7460Sstevel@tonic-gate 	}
7470Sstevel@tonic-gate 
7480Sstevel@tonic-gate 	/*
7490Sstevel@tonic-gate 	 * Get port specified in options list, if any.
7500Sstevel@tonic-gate 	 */
7510Sstevel@tonic-gate 	got_val = nopt(&m, MNTOPT_PORT, (int *)&nfs_port);
7520Sstevel@tonic-gate 	if (!got_val)
7530Sstevel@tonic-gate 		nfs_port = 0;	/* "unspecified" */
7540Sstevel@tonic-gate 	if (nfs_port > USHRT_MAX) {
7550Sstevel@tonic-gate 		syslog(LOG_ERR, "%s: invalid port number %d", mntpnt, nfs_port);
7560Sstevel@tonic-gate 		return (NFSERR_NOENT);
7570Sstevel@tonic-gate 	}
7580Sstevel@tonic-gate 
7590Sstevel@tonic-gate 	/*
7600Sstevel@tonic-gate 	 * Set mount(2) flags here, outside of the loop.
7610Sstevel@tonic-gate 	 */
7620Sstevel@tonic-gate 	flags = MS_OPTIONSTR;
7630Sstevel@tonic-gate 	flags |= (hasmntopt(&m, MNTOPT_RO) == NULL) ? 0 : MS_RDONLY;
7640Sstevel@tonic-gate 	flags |= (hasmntopt(&m, MNTOPT_NOSUID) == NULL) ? 0 : MS_NOSUID;
7650Sstevel@tonic-gate 	flags |= overlay ? MS_OVERLAY : 0;
7660Sstevel@tonic-gate 	if (mntpnt[strlen(mntpnt) - 1] != ' ')
7670Sstevel@tonic-gate 		/* direct mount point without offsets */
7680Sstevel@tonic-gate 		flags |= MS_OVERLAY;
7690Sstevel@tonic-gate 
7700Sstevel@tonic-gate 	use_pubfh = (hasmntopt(&m, MNTOPT_PUBLIC) == NULL) ? FALSE : TRUE;
7710Sstevel@tonic-gate 
7720Sstevel@tonic-gate 	(void) memset(&mfssnego_init, 0, sizeof (mfs_snego_t));
7730Sstevel@tonic-gate 	if (hasmntopt(&m, MNTOPT_SECURE) != NULL) {
7740Sstevel@tonic-gate 		if (++mfssnego_init.sec_opt > 1) {
7750Sstevel@tonic-gate 			syslog(loglevel,
7760Sstevel@tonic-gate 			    "conflicting security options");
7770Sstevel@tonic-gate 			return (NFSERR_IO);
7780Sstevel@tonic-gate 		}
7790Sstevel@tonic-gate 		if (nfs_getseconfig_byname("dh", &mfssnego_init.nfs_sec)) {
7800Sstevel@tonic-gate 			syslog(loglevel,
7810Sstevel@tonic-gate 			    "error getting dh information from %s",
7820Sstevel@tonic-gate 			    NFSSEC_CONF);
7830Sstevel@tonic-gate 			return (NFSERR_IO);
7840Sstevel@tonic-gate 		}
7850Sstevel@tonic-gate 	}
7860Sstevel@tonic-gate 
7870Sstevel@tonic-gate 	/*
7880Sstevel@tonic-gate 	 * Have to workaround the fact that hasmntopt() returns true
7890Sstevel@tonic-gate 	 * when comparing "secure" (in &m) with "sec".
7900Sstevel@tonic-gate 	 */
7910Sstevel@tonic-gate 	if (hasmntopt(&m, "sec=") != NULL) {
7920Sstevel@tonic-gate 		if ((str_opt(&m, MNTOPT_SEC,
793*4590Srm15945 		    &mfssnego_init.nfs_flavor)) == -1) {
7940Sstevel@tonic-gate 			syslog(LOG_ERR, "nfsmount: no memory");
7950Sstevel@tonic-gate 			return (NFSERR_IO);
7960Sstevel@tonic-gate 		}
7970Sstevel@tonic-gate 	}
7980Sstevel@tonic-gate 
7990Sstevel@tonic-gate 	if (mfssnego_init.nfs_flavor) {
8000Sstevel@tonic-gate 		if (++mfssnego_init.sec_opt > 1) {
8010Sstevel@tonic-gate 			syslog(loglevel,
8020Sstevel@tonic-gate 			    "conflicting security options");
8030Sstevel@tonic-gate 			free(mfssnego_init.nfs_flavor);
8040Sstevel@tonic-gate 			return (NFSERR_IO);
8050Sstevel@tonic-gate 		}
8060Sstevel@tonic-gate 		if (nfs_getseconfig_byname(mfssnego_init.nfs_flavor,
807*4590Srm15945 		    &mfssnego_init.nfs_sec)) {
8080Sstevel@tonic-gate 			syslog(loglevel,
8090Sstevel@tonic-gate 			    "error getting %s information from %s",
8100Sstevel@tonic-gate 			    mfssnego_init.nfs_flavor, NFSSEC_CONF);
8110Sstevel@tonic-gate 			free(mfssnego_init.nfs_flavor);
8120Sstevel@tonic-gate 			return (NFSERR_IO);
8130Sstevel@tonic-gate 		}
8140Sstevel@tonic-gate 		free(mfssnego_init.nfs_flavor);
8150Sstevel@tonic-gate 	}
8160Sstevel@tonic-gate 
8170Sstevel@tonic-gate nextentry:
8180Sstevel@tonic-gate 	skipentry = 0;
8190Sstevel@tonic-gate 
8200Sstevel@tonic-gate 	got_val = nopt(&m, MNTOPT_VERS, (int *)&nfsvers);
8210Sstevel@tonic-gate 	if (!got_val)
8220Sstevel@tonic-gate 		nfsvers = 0;	/* "unspecified" */
8230Sstevel@tonic-gate 	if (set_versrange(nfsvers, &vers, &versmin) != 0) {
8240Sstevel@tonic-gate 		syslog(LOG_ERR, "Incorrect NFS version specified for %s",
825*4590Srm15945 		    mntpnt);
8260Sstevel@tonic-gate 		last_error = NFSERR_NOENT;
8270Sstevel@tonic-gate 		goto ret;
8280Sstevel@tonic-gate 	}
8290Sstevel@tonic-gate 
8300Sstevel@tonic-gate 	if (nfsvers != 0) {
8310Sstevel@tonic-gate 		pubversmax = pubversmin = nfsvers;
8320Sstevel@tonic-gate 	} else {
8330Sstevel@tonic-gate 		pubversmax = vers;
8340Sstevel@tonic-gate 		pubversmin = versmin;
8350Sstevel@tonic-gate 	}
8360Sstevel@tonic-gate 
8370Sstevel@tonic-gate 	/*
8380Sstevel@tonic-gate 	 * Walk the whole list, pinging and collecting version
8390Sstevel@tonic-gate 	 * info so that we can make sure the mount will be
8400Sstevel@tonic-gate 	 * homogeneous with respect to version.
8410Sstevel@tonic-gate 	 *
8420Sstevel@tonic-gate 	 * If we have a version preference, this is easy; we'll
8430Sstevel@tonic-gate 	 * just reject anything that doesn't match.
8440Sstevel@tonic-gate 	 *
8450Sstevel@tonic-gate 	 * If not, we want to try to provide the best compromise
8460Sstevel@tonic-gate 	 * that considers proximity, preference for a higher version,
8470Sstevel@tonic-gate 	 * sorted order, and number of replicas.  We will count
8480Sstevel@tonic-gate 	 * the number of V2 and V3 replicas and also the number
8490Sstevel@tonic-gate 	 * which are "near", i.e. the localhost or on the same
8500Sstevel@tonic-gate 	 * subnet.
8510Sstevel@tonic-gate 	 */
8520Sstevel@tonic-gate 	for (mfs = mfs_in; mfs; mfs = mfs->mfs_next) {
8530Sstevel@tonic-gate 
8540Sstevel@tonic-gate 
8550Sstevel@tonic-gate 		if (mfs->mfs_ignore)
8560Sstevel@tonic-gate 			continue;
8570Sstevel@tonic-gate 
8583767Sjs195444 		/*
8593767Sjs195444 		 * If the host is '[a:d:d:r:e:s:s'],
8603767Sjs195444 		 * only use 'a:d:d:r:e:s:s' for communication
8613767Sjs195444 		 */
8623767Sjs195444 		host = strdup(mfs->mfs_host);
8633767Sjs195444 		if (host == NULL) {
8643767Sjs195444 			syslog(LOG_ERR, "nfsmount: no memory");
8653767Sjs195444 			last_error = NFSERR_IO;
8663767Sjs195444 			goto out;
8673767Sjs195444 		}
8683767Sjs195444 		unbracket(&host);
8693767Sjs195444 
8700Sstevel@tonic-gate 		(void) memcpy(&mfssnego, &mfssnego_init, sizeof (mfs_snego_t));
8710Sstevel@tonic-gate 
8720Sstevel@tonic-gate 		if (use_pubfh == TRUE || mfs->mfs_flags & MFS_URL) {
8730Sstevel@tonic-gate 			char *path;
8740Sstevel@tonic-gate 
8750Sstevel@tonic-gate 			if (nfs_port != 0 && mfs->mfs_port != 0 &&
8760Sstevel@tonic-gate 			    nfs_port != mfs->mfs_port) {
8770Sstevel@tonic-gate 
8780Sstevel@tonic-gate 				syslog(LOG_ERR, "nfsmount: port (%u) in nfs URL"
879*4590Srm15945 				    " not the same as port (%d) in port "
880*4590Srm15945 				    "option\n", mfs->mfs_port, nfs_port);
8810Sstevel@tonic-gate 				last_error = NFSERR_IO;
8820Sstevel@tonic-gate 				goto out;
8830Sstevel@tonic-gate 
8840Sstevel@tonic-gate 			} else if (nfs_port != 0)
8850Sstevel@tonic-gate 				thisport = nfs_port;
8860Sstevel@tonic-gate 			else
8870Sstevel@tonic-gate 				thisport = mfs->mfs_port;
8880Sstevel@tonic-gate 
8890Sstevel@tonic-gate 			dir = mfs->mfs_dir;
8900Sstevel@tonic-gate 
8910Sstevel@tonic-gate 			if ((mfs->mfs_flags & MFS_URL) == 0) {
8920Sstevel@tonic-gate 				path = malloc(strlen(dir) + 2);
8930Sstevel@tonic-gate 				if (path == NULL) {
8940Sstevel@tonic-gate 					syslog(LOG_ERR, "nfsmount: no memory");
8950Sstevel@tonic-gate 					last_error = NFSERR_IO;
8960Sstevel@tonic-gate 					goto out;
8970Sstevel@tonic-gate 				}
8980Sstevel@tonic-gate 				path[0] = (char)WNL_NATIVEPATH;
8990Sstevel@tonic-gate 				(void) strcpy(&path[1], dir);
9000Sstevel@tonic-gate 			} else {
9010Sstevel@tonic-gate 				path = dir;
9020Sstevel@tonic-gate 			}
9030Sstevel@tonic-gate 
9040Sstevel@tonic-gate 			argp = (struct nfs_args *)
905*4590Srm15945 			    malloc(sizeof (struct nfs_args));
9060Sstevel@tonic-gate 
9070Sstevel@tonic-gate 			if (!argp) {
9080Sstevel@tonic-gate 				if (path != dir)
9090Sstevel@tonic-gate 					free(path);
9100Sstevel@tonic-gate 				syslog(LOG_ERR, "nfsmount: no memory");
9110Sstevel@tonic-gate 				last_error = NFSERR_IO;
9120Sstevel@tonic-gate 				goto out;
9130Sstevel@tonic-gate 			}
9140Sstevel@tonic-gate 			(void) memset(argp, 0, sizeof (*argp));
9150Sstevel@tonic-gate 
9160Sstevel@tonic-gate 			/*
9170Sstevel@tonic-gate 			 * RDMA support
9180Sstevel@tonic-gate 			 * By now Mount argument struct has been allocated,
9190Sstevel@tonic-gate 			 * either a pub_fh path will be taken or the regular
9200Sstevel@tonic-gate 			 * one. So here if a protocol was specified and it
9210Sstevel@tonic-gate 			 * was not rdma we let it be, else we set DO_RDMA.
9220Sstevel@tonic-gate 			 * If no proto was there we advise on trying RDMA.
9230Sstevel@tonic-gate 			 */
9240Sstevel@tonic-gate 			if (nfs_proto) {
9250Sstevel@tonic-gate 				if (strcmp(nfs_proto, "rdma") == 0) {
9260Sstevel@tonic-gate 					free(nfs_proto);
9270Sstevel@tonic-gate 					nfs_proto = NULL;
9280Sstevel@tonic-gate 					argp->flags |= NFSMNT_DORDMA;
9290Sstevel@tonic-gate 				}
9300Sstevel@tonic-gate 			} else
9310Sstevel@tonic-gate 				argp->flags |= NFSMNT_TRYRDMA;
9320Sstevel@tonic-gate 
9330Sstevel@tonic-gate 			for (pubvers = pubversmax; pubvers >= pubversmin;
9340Sstevel@tonic-gate 			    pubvers--) {
9350Sstevel@tonic-gate 
9360Sstevel@tonic-gate 				nconf = NULL;
9370Sstevel@tonic-gate 				argp->addr = get_pubfh(host, pubvers, &mfssnego,
9380Sstevel@tonic-gate 				    &nconf, nfs_proto, thisport, NULL,
9390Sstevel@tonic-gate 				    &argp->fh, TRUE, path);
9400Sstevel@tonic-gate 
9410Sstevel@tonic-gate 				if (argp->addr != NULL)
9420Sstevel@tonic-gate 					break;
9430Sstevel@tonic-gate 
9440Sstevel@tonic-gate 				if (nconf != NULL)
9450Sstevel@tonic-gate 					freenetconfigent(nconf);
9460Sstevel@tonic-gate 			}
9470Sstevel@tonic-gate 
9480Sstevel@tonic-gate 			if (path != dir)
9490Sstevel@tonic-gate 				free(path);
9500Sstevel@tonic-gate 
9510Sstevel@tonic-gate 			if (argp->addr != NULL) {
9520Sstevel@tonic-gate 
9530Sstevel@tonic-gate 				/*
9540Sstevel@tonic-gate 				 * The use of llock option for NFSv4
9550Sstevel@tonic-gate 				 * mounts is not required since file
9560Sstevel@tonic-gate 				 * locking is included within the protocol
9570Sstevel@tonic-gate 				 */
9580Sstevel@tonic-gate 				if (pubvers != NFS_V4)
9590Sstevel@tonic-gate 					argp->flags |= NFSMNT_LLOCK;
9600Sstevel@tonic-gate 
9610Sstevel@tonic-gate 				argp->flags |= NFSMNT_PUBLIC;
9620Sstevel@tonic-gate 
963*4590Srm15945 				vers = pubvers;
9640Sstevel@tonic-gate 				mfs->mfs_args = argp;
9650Sstevel@tonic-gate 				mfs->mfs_version = pubvers;
9660Sstevel@tonic-gate 				mfs->mfs_nconf = nconf;
9670Sstevel@tonic-gate 				mfs->mfs_flags |= MFS_FH_VIA_WEBNFS;
9680Sstevel@tonic-gate 
9690Sstevel@tonic-gate 			} else {
9700Sstevel@tonic-gate 				free(argp);
9710Sstevel@tonic-gate 
9720Sstevel@tonic-gate 				/*
9730Sstevel@tonic-gate 				 * If -public was specified, give up
9740Sstevel@tonic-gate 				 * on this entry now.
9750Sstevel@tonic-gate 				 */
9760Sstevel@tonic-gate 				if (use_pubfh == TRUE) {
9770Sstevel@tonic-gate 					syslog(loglevel,
9780Sstevel@tonic-gate 					    "%s: no public file handle support",
9790Sstevel@tonic-gate 					    host);
9800Sstevel@tonic-gate 					last_error = NFSERR_NOENT;
9810Sstevel@tonic-gate 					mfs->mfs_ignore = 1;
9820Sstevel@tonic-gate 					continue;
9830Sstevel@tonic-gate 				}
9840Sstevel@tonic-gate 
9850Sstevel@tonic-gate 				/*
9860Sstevel@tonic-gate 				 * Back off to a conventional mount.
9870Sstevel@tonic-gate 				 *
9880Sstevel@tonic-gate 				 * URL's can contain escape characters. Get
9890Sstevel@tonic-gate 				 * rid of them.
9900Sstevel@tonic-gate 				 */
9910Sstevel@tonic-gate 				path = malloc(strlen(dir) + 2);
9920Sstevel@tonic-gate 
9930Sstevel@tonic-gate 				if (path == NULL) {
9940Sstevel@tonic-gate 					syslog(LOG_ERR, "nfsmount: no memory");
9950Sstevel@tonic-gate 					last_error = NFSERR_IO;
9960Sstevel@tonic-gate 					goto out;
9970Sstevel@tonic-gate 				}
9980Sstevel@tonic-gate 
9990Sstevel@tonic-gate 				strcpy(path, dir);
10000Sstevel@tonic-gate 				URLparse(path);
10010Sstevel@tonic-gate 				mfs->mfs_dir = path;
10020Sstevel@tonic-gate 				mfs->mfs_flags |= MFS_ALLOC_DIR;
10030Sstevel@tonic-gate 				mfs->mfs_flags &= ~MFS_URL;
10040Sstevel@tonic-gate 			}
10050Sstevel@tonic-gate 		}
10060Sstevel@tonic-gate 
10070Sstevel@tonic-gate 		if ((mfs->mfs_flags & MFS_FH_VIA_WEBNFS) ==  0) {
10080Sstevel@tonic-gate 			i = pingnfs(host, get_retry(opts) + 1, &vers, versmin,
1009*4590Srm15945 			    0, FALSE, NULL, nfs_proto);
10100Sstevel@tonic-gate 			if (i != RPC_SUCCESS) {
10110Sstevel@tonic-gate 				if (i == RPC_PROGVERSMISMATCH) {
10120Sstevel@tonic-gate 					syslog(loglevel, "server %s: NFS "
1013*4590Srm15945 					    "protocol version mismatch",
1014*4590Srm15945 					    host);
10150Sstevel@tonic-gate 				} else {
10160Sstevel@tonic-gate 					syslog(loglevel, "server %s not "
1017*4590Srm15945 					    "responding", host);
10180Sstevel@tonic-gate 				}
10190Sstevel@tonic-gate 				mfs->mfs_ignore = 1;
10200Sstevel@tonic-gate 				last_error = NFSERR_NOENT;
10210Sstevel@tonic-gate 				continue;
10220Sstevel@tonic-gate 			}
10230Sstevel@tonic-gate 			if (nfsvers != 0 && nfsvers != vers) {
10240Sstevel@tonic-gate 				if (nfs_proto == NULL)
10250Sstevel@tonic-gate 					syslog(loglevel,
1026*4590Srm15945 					    "NFS version %d "
1027*4590Srm15945 					    "not supported by %s",
1028*4590Srm15945 					    nfsvers, host);
10290Sstevel@tonic-gate 				else
10300Sstevel@tonic-gate 					syslog(loglevel,
1031*4590Srm15945 					    "NFS version %d "
1032*4590Srm15945 					    "with proto %s "
1033*4590Srm15945 					    "not supported by %s",
1034*4590Srm15945 					    nfsvers, nfs_proto, host);
10350Sstevel@tonic-gate 				mfs->mfs_ignore = 1;
10360Sstevel@tonic-gate 				last_error = NFSERR_NOENT;
10370Sstevel@tonic-gate 				continue;
10380Sstevel@tonic-gate 			}
10390Sstevel@tonic-gate 		}
10400Sstevel@tonic-gate 
10413767Sjs195444 		free(host);
10423767Sjs195444 
10430Sstevel@tonic-gate 		switch (vers) {
10440Sstevel@tonic-gate 		case NFS_V4: v4cnt++; break;
10450Sstevel@tonic-gate 		case NFS_V3: v3cnt++; break;
10460Sstevel@tonic-gate 		case NFS_VERSION: v2cnt++; break;
10470Sstevel@tonic-gate 		default: break;
10480Sstevel@tonic-gate 		}
10490Sstevel@tonic-gate 
10500Sstevel@tonic-gate 		/*
10510Sstevel@tonic-gate 		 * It's not clear how useful this stuff is if
10520Sstevel@tonic-gate 		 * we are using webnfs across the internet, but it
10530Sstevel@tonic-gate 		 * can't hurt.
10540Sstevel@tonic-gate 		 */
10550Sstevel@tonic-gate 		if (mfs->mfs_distance &&
10560Sstevel@tonic-gate 		    mfs->mfs_distance <= DIST_MYSUB) {
10570Sstevel@tonic-gate 			switch (vers) {
10580Sstevel@tonic-gate 			case NFS_V4: v4near++; break;
10590Sstevel@tonic-gate 			case NFS_V3: v3near++; break;
10600Sstevel@tonic-gate 			case NFS_VERSION: v2near++; break;
10610Sstevel@tonic-gate 			default: break;
10620Sstevel@tonic-gate 			}
10630Sstevel@tonic-gate 		}
10640Sstevel@tonic-gate 
10650Sstevel@tonic-gate 		/*
10660Sstevel@tonic-gate 		 * If the mount is not replicated, we don't want to
10670Sstevel@tonic-gate 		 * ping every entry, so we'll stop here.  This means
10680Sstevel@tonic-gate 		 * that we may have to go back to "nextentry" above
10693767Sjs195444 		 * to consider another entry if we can't get
10700Sstevel@tonic-gate 		 * all the way to mount(2) with this one.
10710Sstevel@tonic-gate 		 */
10720Sstevel@tonic-gate 		if (!replicated)
10730Sstevel@tonic-gate 			break;
10743767Sjs195444 
10750Sstevel@tonic-gate 	}
10760Sstevel@tonic-gate 
10770Sstevel@tonic-gate 	if (nfsvers == 0) {
10780Sstevel@tonic-gate 		/*
10790Sstevel@tonic-gate 		 * Choose the NFS version.
10800Sstevel@tonic-gate 		 * We prefer higher versions, but will choose a one-
10810Sstevel@tonic-gate 		 * version downgrade in service if we can use a local
10820Sstevel@tonic-gate 		 * network interface and avoid a router.
10830Sstevel@tonic-gate 		 */
10840Sstevel@tonic-gate 		if (v4cnt && v4cnt >= v3cnt && (v4near || !v3near))
10850Sstevel@tonic-gate 			nfsvers = NFS_V4;
10860Sstevel@tonic-gate 		else if (v3cnt && v3cnt >= v2cnt && (v3near || !v2near))
10870Sstevel@tonic-gate 			nfsvers = NFS_V3;
10880Sstevel@tonic-gate 		else
10890Sstevel@tonic-gate 			nfsvers = NFS_VERSION;
10900Sstevel@tonic-gate 		if (trace > 2)
10910Sstevel@tonic-gate 			trace_prt(1,
10920Sstevel@tonic-gate 		    "  nfsmount: v4=%d[%d]v3=%d[%d],v2=%d[%d] => v%d.\n",
10930Sstevel@tonic-gate 			    v4cnt, v4near, v3cnt, v3near,
10940Sstevel@tonic-gate 			    v2cnt, v2near, nfsvers);
10950Sstevel@tonic-gate 	}
10960Sstevel@tonic-gate 
10970Sstevel@tonic-gate 	/*
10980Sstevel@tonic-gate 	 * Since we don't support different NFS versions in replicated
10990Sstevel@tonic-gate 	 * mounts, set fstype now.
11000Sstevel@tonic-gate 	 * Also take the opportunity to set
11010Sstevel@tonic-gate 	 * the mount protocol version as appropriate.
11020Sstevel@tonic-gate 	 */
11030Sstevel@tonic-gate 	switch (nfsvers) {
11040Sstevel@tonic-gate 	case NFS_V4:
11050Sstevel@tonic-gate 		fstype = MNTTYPE_NFS4;
11060Sstevel@tonic-gate 		break;
11070Sstevel@tonic-gate 	case NFS_V3:
11080Sstevel@tonic-gate 		fstype = MNTTYPE_NFS3;
11090Sstevel@tonic-gate 		if (use_pubfh == FALSE) {
11100Sstevel@tonic-gate 			mountversmax = MOUNTVERS3;
11110Sstevel@tonic-gate 			versmin = MOUNTVERS3;
11120Sstevel@tonic-gate 		}
11130Sstevel@tonic-gate 		break;
11140Sstevel@tonic-gate 	case NFS_VERSION:
11150Sstevel@tonic-gate 		fstype = MNTTYPE_NFS;
11160Sstevel@tonic-gate 		if (use_pubfh == FALSE) {
11170Sstevel@tonic-gate 			mountversmax = MOUNTVERS_POSIX;
11180Sstevel@tonic-gate 			versmin = MOUNTVERS;
11190Sstevel@tonic-gate 		}
11200Sstevel@tonic-gate 		break;
11210Sstevel@tonic-gate 	}
11220Sstevel@tonic-gate 
11230Sstevel@tonic-gate 	/*
11240Sstevel@tonic-gate 	 * Our goal here is to evaluate each of several possible
11250Sstevel@tonic-gate 	 * replicas and try to come up with a list we can hand
11260Sstevel@tonic-gate 	 * to mount(2).  If we don't have a valid "head" at the
11270Sstevel@tonic-gate 	 * end of this process, it means we have rejected all
11280Sstevel@tonic-gate 	 * potential server:/path tuples.  We will fail quietly
11290Sstevel@tonic-gate 	 * in front of mount(2), and will have printed errors
11300Sstevel@tonic-gate 	 * where we found them.
11310Sstevel@tonic-gate 	 * XXX - do option work outside loop w careful design
11320Sstevel@tonic-gate 	 * XXX - use macro for error condition free handling
11330Sstevel@tonic-gate 	 */
11340Sstevel@tonic-gate 	for (mfs = mfs_in; mfs; mfs = mfs->mfs_next) {
11350Sstevel@tonic-gate 
11360Sstevel@tonic-gate 		/*
11370Sstevel@tonic-gate 		 * Initialize retry and delay values on a per-server basis.
11380Sstevel@tonic-gate 		 */
11390Sstevel@tonic-gate 		retries = get_retry(opts);
11400Sstevel@tonic-gate 		delay = INITDELAY;
11410Sstevel@tonic-gate retry:
11420Sstevel@tonic-gate 		if (mfs->mfs_ignore)
11430Sstevel@tonic-gate 			continue;
11440Sstevel@tonic-gate 
11450Sstevel@tonic-gate 		/*
11460Sstevel@tonic-gate 		 * If we don't have a fh yet, and if this is not a replicated
11470Sstevel@tonic-gate 		 * mount, we haven't done a pingnfs() on the next entry,
11480Sstevel@tonic-gate 		 * so we don't know if the next entry is up or if it
11490Sstevel@tonic-gate 		 * supports an NFS version we like.  So if we had a problem
11500Sstevel@tonic-gate 		 * with an entry, we need to go back and run through some new
11510Sstevel@tonic-gate 		 * code.
11520Sstevel@tonic-gate 		 */
11530Sstevel@tonic-gate 		if ((mfs->mfs_flags & MFS_FH_VIA_WEBNFS) == 0 &&
11540Sstevel@tonic-gate 		    !replicated && skipentry)
11550Sstevel@tonic-gate 			goto nextentry;
11560Sstevel@tonic-gate 
11570Sstevel@tonic-gate 		vers = mountversmax;
11580Sstevel@tonic-gate 		host = mfs->mfs_host;
11590Sstevel@tonic-gate 		dir = mfs->mfs_dir;
11603767Sjs195444 
11613767Sjs195444 		/*
11623767Sjs195444 		 * Remember the possible '[a:d:d:r:e:s:s]' as the address to be
11633767Sjs195444 		 * later passed to mount(2) and used in the mnttab line, but
11643767Sjs195444 		 * only use 'a:d:d:r:e:s:s' for communication
11653767Sjs195444 		 */
11663767Sjs195444 		rhost = strdup(host);
11673767Sjs195444 		if (rhost == NULL) {
11683767Sjs195444 			syslog(LOG_ERR, "nfsmount: no memory");
11693767Sjs195444 			last_error = NFSERR_IO;
11703767Sjs195444 			goto out;
11713767Sjs195444 		}
11723767Sjs195444 		unbracket(&host);
11733767Sjs195444 
11743767Sjs195444 		(void) sprintf(remname, "%s:%s", rhost, dir);
11750Sstevel@tonic-gate 		if (trace > 4 && replicated)
11760Sstevel@tonic-gate 			trace_prt(1, "	nfsmount: examining %s\n", remname);
11770Sstevel@tonic-gate 
11780Sstevel@tonic-gate 		/*
11790Sstevel@tonic-gate 		 * If it's cached we need to get cachefs to mount it.
11800Sstevel@tonic-gate 		 */
11810Sstevel@tonic-gate 		if (cached) {
11820Sstevel@tonic-gate 			char *copts = opts;
11830Sstevel@tonic-gate 
11840Sstevel@tonic-gate 			/*
11850Sstevel@tonic-gate 			 * If we started with a URL we need to turn on
11860Sstevel@tonic-gate 			 * -o public if not on already
11870Sstevel@tonic-gate 			 */
11880Sstevel@tonic-gate 			if (use_pubfh == FALSE &&
11890Sstevel@tonic-gate 			    (mfs->mfs_flags & MFS_FH_VIA_WEBNFS)) {
11900Sstevel@tonic-gate 
11910Sstevel@tonic-gate 				copts = malloc(strlen(opts) +
1192*4590Srm15945 				    strlen(",public")+1);
11930Sstevel@tonic-gate 
11940Sstevel@tonic-gate 				if (copts == NULL) {
11950Sstevel@tonic-gate 					syslog(LOG_ERR, "nfsmount: no memory");
11960Sstevel@tonic-gate 					last_error = NFSERR_IO;
11970Sstevel@tonic-gate 					goto out;
11980Sstevel@tonic-gate 				}
11990Sstevel@tonic-gate 
12000Sstevel@tonic-gate 				strcpy(copts, opts);
12010Sstevel@tonic-gate 
12020Sstevel@tonic-gate 				if (strlen(copts) != 0)
12030Sstevel@tonic-gate 					strcat(copts, ",");
12040Sstevel@tonic-gate 
12050Sstevel@tonic-gate 				strcat(copts, "public");
12060Sstevel@tonic-gate 			}
12070Sstevel@tonic-gate 
12080Sstevel@tonic-gate 			last_error = mount_generic(remname, MNTTYPE_CACHEFS,
1209*4590Srm15945 			    copts, mntpnt, overlay);
12100Sstevel@tonic-gate 
12110Sstevel@tonic-gate 			if (copts != opts)
12120Sstevel@tonic-gate 				free(copts);
12130Sstevel@tonic-gate 
12140Sstevel@tonic-gate 			if (last_error) {
12150Sstevel@tonic-gate 				skipentry = 1;
12160Sstevel@tonic-gate 				mfs->mfs_ignore = 1;
12170Sstevel@tonic-gate 				continue;
12180Sstevel@tonic-gate 			}
12190Sstevel@tonic-gate 			goto out;
12200Sstevel@tonic-gate 		}
12210Sstevel@tonic-gate 
12220Sstevel@tonic-gate 		if (mfs->mfs_args == NULL) {
12230Sstevel@tonic-gate 
12240Sstevel@tonic-gate 			/*
12250Sstevel@tonic-gate 			 * Allocate nfs_args structure
12260Sstevel@tonic-gate 			 */
12270Sstevel@tonic-gate 			argp = (struct nfs_args *)
1228*4590Srm15945 			    malloc(sizeof (struct nfs_args));
12290Sstevel@tonic-gate 
12300Sstevel@tonic-gate 			if (!argp) {
12310Sstevel@tonic-gate 				syslog(LOG_ERR, "nfsmount: no memory");
12320Sstevel@tonic-gate 				last_error = NFSERR_IO;
12330Sstevel@tonic-gate 				goto out;
12340Sstevel@tonic-gate 			}
12350Sstevel@tonic-gate 
12360Sstevel@tonic-gate 			(void) memset(argp, 0, sizeof (*argp));
12370Sstevel@tonic-gate 
12380Sstevel@tonic-gate 			/*
12390Sstevel@tonic-gate 			 * RDMA support
12400Sstevel@tonic-gate 			 * By now Mount argument struct has been allocated,
12410Sstevel@tonic-gate 			 * either a pub_fh path will be taken or the regular
12420Sstevel@tonic-gate 			 * one. So here if a protocol was specified and it
12430Sstevel@tonic-gate 			 * was not rdma we let it be, else we set DO_RDMA.
12440Sstevel@tonic-gate 			 * If no proto was there we advise on trying RDMA.
12450Sstevel@tonic-gate 			 */
12460Sstevel@tonic-gate 			if (nfs_proto) {
12470Sstevel@tonic-gate 				if (strcmp(nfs_proto, "rdma") == 0) {
12480Sstevel@tonic-gate 					free(nfs_proto);
12490Sstevel@tonic-gate 					nfs_proto = NULL;
12500Sstevel@tonic-gate 					argp->flags |= NFSMNT_DORDMA;
12510Sstevel@tonic-gate 				}
12520Sstevel@tonic-gate 			} else
12530Sstevel@tonic-gate 				argp->flags |= NFSMNT_TRYRDMA;
12540Sstevel@tonic-gate 		} else {
12550Sstevel@tonic-gate 			argp = mfs->mfs_args;
12560Sstevel@tonic-gate 			mfs->mfs_args = NULL;
12570Sstevel@tonic-gate 
12580Sstevel@tonic-gate 			/*
12590Sstevel@tonic-gate 			 * Skip entry if we already have file handle but the
12600Sstevel@tonic-gate 			 * NFS version is wrong.
12610Sstevel@tonic-gate 			 */
12620Sstevel@tonic-gate 			if ((mfs->mfs_flags & MFS_FH_VIA_WEBNFS) &&
12630Sstevel@tonic-gate 			    mfs->mfs_version != nfsvers) {
12640Sstevel@tonic-gate 
12650Sstevel@tonic-gate 				free(argp);
12660Sstevel@tonic-gate 				skipentry = 1;
12670Sstevel@tonic-gate 				mfs->mfs_ignore = 1;
12680Sstevel@tonic-gate 				continue;
12690Sstevel@tonic-gate 			}
12700Sstevel@tonic-gate 		}
12710Sstevel@tonic-gate 
12720Sstevel@tonic-gate 		prevhead = head;
12730Sstevel@tonic-gate 		prevtail = tail;
12740Sstevel@tonic-gate 		if (!head)
12750Sstevel@tonic-gate 			head = tail = argp;
12760Sstevel@tonic-gate 		else
12770Sstevel@tonic-gate 			tail = tail->nfs_ext_u.nfs_extB.next = argp;
12780Sstevel@tonic-gate 
12790Sstevel@tonic-gate 		/*
12800Sstevel@tonic-gate 		 * WebNFS and NFSv4 behave similarly in that they
12810Sstevel@tonic-gate 		 * don't use the mount protocol.  Therefore, avoid
12820Sstevel@tonic-gate 		 * mount protocol like things when version 4 is being
12830Sstevel@tonic-gate 		 * used.
12840Sstevel@tonic-gate 		 */
12850Sstevel@tonic-gate 		if ((mfs->mfs_flags & MFS_FH_VIA_WEBNFS) == 0 &&
1286*4590Srm15945 		    nfsvers != NFS_V4) {
1287*4590Srm15945 			timeout.tv_usec = 0;
1288*4590Srm15945 			timeout.tv_sec = rpc_timeout;
1289*4590Srm15945 			rpc_stat = RPC_TIMEDOUT;
1290*4590Srm15945 
1291*4590Srm15945 			/* Create the client handle. */
1292*4590Srm15945 
1293*4590Srm15945 			if (trace > 1) {
1294*4590Srm15945 				trace_prt(1,
1295*4590Srm15945 				    "  nfsmount: Get mount version: request "
1296*4590Srm15945 				    "vers=%d min=%d\n", vers, versmin);
12970Sstevel@tonic-gate 			}
1298*4590Srm15945 
1299*4590Srm15945 			while ((cl = clnt_create_vers(host, MOUNTPROG, &outvers,
1300*4590Srm15945 			    versmin, vers, "udp")) == NULL) {
1301*4590Srm15945 				if (trace > 4) {
1302*4590Srm15945 					trace_prt(1,
1303*4590Srm15945 					    "  nfsmount: Can't get mount "
1304*4590Srm15945 					    "version: rpcerr=%d\n",
1305*4590Srm15945 					    rpc_createerr.cf_stat);
1306*4590Srm15945 				}
1307*4590Srm15945 				if (rpc_createerr.cf_stat == RPC_UNKNOWNHOST ||
1308*4590Srm15945 				    rpc_createerr.cf_stat == RPC_TIMEDOUT)
1309*4590Srm15945 					break;
13100Sstevel@tonic-gate 
13110Sstevel@tonic-gate 			/*
13120Sstevel@tonic-gate 			 * backoff and return lower version to retry the ping.
13130Sstevel@tonic-gate 			 * XXX we should be more careful and handle
13140Sstevel@tonic-gate 			 * RPC_PROGVERSMISMATCH here, because that error
13150Sstevel@tonic-gate 			 * is handled in clnt_create_vers(). It's not done to
13160Sstevel@tonic-gate 			 * stay in sync with the nfs mount command.
13170Sstevel@tonic-gate 			 */
1318*4590Srm15945 				vers--;
1319*4590Srm15945 				if (vers < versmin)
1320*4590Srm15945 					break;
1321*4590Srm15945 				if (trace > 4) {
1322*4590Srm15945 					trace_prt(1,
1323*4590Srm15945 					    "  nfsmount: Try version=%d\n",
1324*4590Srm15945 					    vers);
1325*4590Srm15945 				}
13260Sstevel@tonic-gate 			}
13270Sstevel@tonic-gate 
1328*4590Srm15945 			if (cl == NULL) {
1329*4590Srm15945 				free(argp);
1330*4590Srm15945 				head = prevhead;
1331*4590Srm15945 				tail = prevtail;
1332*4590Srm15945 				if (tail)
1333*4590Srm15945 					tail->nfs_ext_u.nfs_extB.next = NULL;
1334*4590Srm15945 				last_error = NFSERR_NOENT;
1335*4590Srm15945 
1336*4590Srm15945 				if (rpc_createerr.cf_stat != RPC_UNKNOWNHOST &&
1337*4590Srm15945 				    rpc_createerr.cf_stat !=
1338*4590Srm15945 				    RPC_PROGVERSMISMATCH &&
1339*4590Srm15945 				    retries-- > 0) {
1340*4590Srm15945 					DELAY(delay);
1341*4590Srm15945 					goto retry;
1342*4590Srm15945 				}
1343*4590Srm15945 
1344*4590Srm15945 				syslog(loglevel, "%s %s", host,
1345*4590Srm15945 				    clnt_spcreateerror(
1346*4590Srm15945 				    "server not responding"));
1347*4590Srm15945 				skipentry = 1;
1348*4590Srm15945 				mfs->mfs_ignore = 1;
1349*4590Srm15945 				continue;
1350*4590Srm15945 			}
1351*4590Srm15945 			if (trace > 1) {
1352*4590Srm15945 				trace_prt(1,
1353*4590Srm15945 				    "	nfsmount: mount version=%d\n", outvers);
1354*4590Srm15945 			}
13550Sstevel@tonic-gate #ifdef MALLOC_DEBUG
1356*4590Srm15945 			add_alloc("CLNT_HANDLE", cl, 0, __FILE__, __LINE__);
1357*4590Srm15945 			add_alloc("AUTH_HANDLE", cl->cl_auth, 0,
1358*4590Srm15945 			    __FILE__, __LINE__);
13590Sstevel@tonic-gate #endif
13600Sstevel@tonic-gate 
1361*4590Srm15945 			if (__clnt_bindresvport(cl) < 0) {
1362*4590Srm15945 				free(argp);
1363*4590Srm15945 				head = prevhead;
1364*4590Srm15945 				tail = prevtail;
1365*4590Srm15945 				if (tail)
1366*4590Srm15945 					tail->nfs_ext_u.nfs_extB.next = NULL;
1367*4590Srm15945 				last_error = NFSERR_NOENT;
1368*4590Srm15945 
1369*4590Srm15945 				if (retries-- > 0) {
1370*4590Srm15945 					destroy_auth_client_handle(cl);
1371*4590Srm15945 					DELAY(delay);
1372*4590Srm15945 					goto retry;
1373*4590Srm15945 				}
1374*4590Srm15945 
1375*4590Srm15945 				syslog(loglevel, "mount %s: %s", host,
1376*4590Srm15945 				    "Couldn't bind to reserved port");
13770Sstevel@tonic-gate 				destroy_auth_client_handle(cl);
1378*4590Srm15945 				skipentry = 1;
1379*4590Srm15945 				mfs->mfs_ignore = 1;
1380*4590Srm15945 				continue;
13810Sstevel@tonic-gate 			}
13820Sstevel@tonic-gate 
13830Sstevel@tonic-gate #ifdef MALLOC_DEBUG
1384*4590Srm15945 			drop_alloc("AUTH_HANDLE", cl->cl_auth,
1385*4590Srm15945 			    __FILE__, __LINE__);
13860Sstevel@tonic-gate #endif
1387*4590Srm15945 			AUTH_DESTROY(cl->cl_auth);
1388*4590Srm15945 			if ((cl->cl_auth = authsys_create_default()) == NULL) {
1389*4590Srm15945 				free(argp);
1390*4590Srm15945 				head = prevhead;
1391*4590Srm15945 				tail = prevtail;
1392*4590Srm15945 				if (tail)
1393*4590Srm15945 					tail->nfs_ext_u.nfs_extB.next = NULL;
1394*4590Srm15945 				last_error = NFSERR_NOENT;
1395*4590Srm15945 
1396*4590Srm15945 				if (retries-- > 0) {
1397*4590Srm15945 					destroy_auth_client_handle(cl);
1398*4590Srm15945 					DELAY(delay);
1399*4590Srm15945 					goto retry;
1400*4590Srm15945 				}
1401*4590Srm15945 
1402*4590Srm15945 				syslog(loglevel, "mount %s: %s", host,
1403*4590Srm15945 				    "Failed creating default auth handle");
14040Sstevel@tonic-gate 				destroy_auth_client_handle(cl);
1405*4590Srm15945 				skipentry = 1;
1406*4590Srm15945 				mfs->mfs_ignore = 1;
1407*4590Srm15945 				continue;
14080Sstevel@tonic-gate 			}
14090Sstevel@tonic-gate #ifdef MALLOC_DEBUG
1410*4590Srm15945 			add_alloc("AUTH_HANDLE", cl->cl_auth, 0,
1411*4590Srm15945 			    __FILE__, __LINE__);
14120Sstevel@tonic-gate #endif
14130Sstevel@tonic-gate 		} else
1414*4590Srm15945 			cl = NULL;
14150Sstevel@tonic-gate 
14160Sstevel@tonic-gate 		/*
14170Sstevel@tonic-gate 		 * set security options
14180Sstevel@tonic-gate 		 */
14190Sstevel@tonic-gate 		sec_opt = 0;
14200Sstevel@tonic-gate 		(void) memset(&nfs_sec, 0, sizeof (nfs_sec));
14210Sstevel@tonic-gate 		if (hasmntopt(&m, MNTOPT_SECURE) != NULL) {
14220Sstevel@tonic-gate 			if (++sec_opt > 1) {
14230Sstevel@tonic-gate 				syslog(loglevel,
14240Sstevel@tonic-gate 				    "conflicting security options for %s",
14250Sstevel@tonic-gate 				    remname);
14260Sstevel@tonic-gate 				free(argp);
14270Sstevel@tonic-gate 				head = prevhead;
14280Sstevel@tonic-gate 				tail = prevtail;
14290Sstevel@tonic-gate 				if (tail)
14300Sstevel@tonic-gate 					tail->nfs_ext_u.nfs_extB.next = NULL;
14310Sstevel@tonic-gate 				last_error = NFSERR_IO;
14320Sstevel@tonic-gate 				destroy_auth_client_handle(cl);
14330Sstevel@tonic-gate 				skipentry = 1;
14340Sstevel@tonic-gate 				mfs->mfs_ignore = 1;
14350Sstevel@tonic-gate 				continue;
14360Sstevel@tonic-gate 			}
14370Sstevel@tonic-gate 			if (nfs_getseconfig_byname("dh", &nfs_sec)) {
14380Sstevel@tonic-gate 				syslog(loglevel,
14390Sstevel@tonic-gate 				    "error getting dh information from %s",
14400Sstevel@tonic-gate 				    NFSSEC_CONF);
14410Sstevel@tonic-gate 				free(argp);
14420Sstevel@tonic-gate 				head = prevhead;
14430Sstevel@tonic-gate 				tail = prevtail;
14440Sstevel@tonic-gate 				if (tail)
14450Sstevel@tonic-gate 					tail->nfs_ext_u.nfs_extB.next = NULL;
14460Sstevel@tonic-gate 				last_error = NFSERR_IO;
14470Sstevel@tonic-gate 				destroy_auth_client_handle(cl);
14480Sstevel@tonic-gate 				skipentry = 1;
14490Sstevel@tonic-gate 				mfs->mfs_ignore = 1;
14500Sstevel@tonic-gate 				continue;
14510Sstevel@tonic-gate 			}
14520Sstevel@tonic-gate 		}
14530Sstevel@tonic-gate 
14540Sstevel@tonic-gate 		nfs_flavor = NULL;
14550Sstevel@tonic-gate 		/*
14560Sstevel@tonic-gate 		 * Have to workaround the fact that hasmntopt() returns true
14570Sstevel@tonic-gate 		 * when comparing "secure" (in &m) with "sec".
14580Sstevel@tonic-gate 		 */
14590Sstevel@tonic-gate 		if (hasmntopt(&m, "sec=") != NULL) {
14600Sstevel@tonic-gate 			if ((str_opt(&m, MNTOPT_SEC, &nfs_flavor)) == -1) {
14610Sstevel@tonic-gate 				syslog(LOG_ERR, "nfsmount: no memory");
14620Sstevel@tonic-gate 				last_error = NFSERR_IO;
14630Sstevel@tonic-gate 				destroy_auth_client_handle(cl);
14640Sstevel@tonic-gate 				goto out;
14650Sstevel@tonic-gate 			}
14660Sstevel@tonic-gate 		}
14670Sstevel@tonic-gate 
14680Sstevel@tonic-gate 		if (nfs_flavor) {
14690Sstevel@tonic-gate 			if (++sec_opt > 1) {
14700Sstevel@tonic-gate 				syslog(loglevel,
14710Sstevel@tonic-gate 				    "conflicting security options for %s",
14720Sstevel@tonic-gate 				    remname);
14730Sstevel@tonic-gate 				free(nfs_flavor);
14740Sstevel@tonic-gate 				free(argp);
14750Sstevel@tonic-gate 				head = prevhead;
14760Sstevel@tonic-gate 				tail = prevtail;
14770Sstevel@tonic-gate 				if (tail)
14780Sstevel@tonic-gate 					tail->nfs_ext_u.nfs_extB.next = NULL;
14790Sstevel@tonic-gate 				last_error = NFSERR_IO;
14800Sstevel@tonic-gate 				destroy_auth_client_handle(cl);
14810Sstevel@tonic-gate 				skipentry = 1;
14820Sstevel@tonic-gate 				mfs->mfs_ignore = 1;
14830Sstevel@tonic-gate 				continue;
14840Sstevel@tonic-gate 			}
14850Sstevel@tonic-gate 			if (nfs_getseconfig_byname(nfs_flavor, &nfs_sec)) {
14860Sstevel@tonic-gate 				syslog(loglevel,
14870Sstevel@tonic-gate 				    "error getting %s information from %s",
14880Sstevel@tonic-gate 				    nfs_flavor, NFSSEC_CONF);
14890Sstevel@tonic-gate 				free(nfs_flavor);
14900Sstevel@tonic-gate 				free(argp);
14910Sstevel@tonic-gate 				head = prevhead;
14920Sstevel@tonic-gate 				tail = prevtail;
14930Sstevel@tonic-gate 				if (tail)
14940Sstevel@tonic-gate 					tail->nfs_ext_u.nfs_extB.next = NULL;
14950Sstevel@tonic-gate 				last_error = NFSERR_IO;
14960Sstevel@tonic-gate 				destroy_auth_client_handle(cl);
14970Sstevel@tonic-gate 				skipentry = 1;
14980Sstevel@tonic-gate 				mfs->mfs_ignore = 1;
14990Sstevel@tonic-gate 				continue;
15000Sstevel@tonic-gate 			}
15010Sstevel@tonic-gate 			free(nfs_flavor);
15020Sstevel@tonic-gate 		}
15030Sstevel@tonic-gate 
15040Sstevel@tonic-gate 		posix = (nfsvers != NFS_V4 &&
1505*4590Srm15945 		    hasmntopt(&m, MNTOPT_POSIX) != NULL) ? 1 : 0;
15060Sstevel@tonic-gate 
15070Sstevel@tonic-gate 		if ((mfs->mfs_flags & MFS_FH_VIA_WEBNFS) == 0 &&
1508*4590Srm15945 		    nfsvers != NFS_V4) {
1509*4590Srm15945 			bool_t give_up_on_mnt;
1510*4590Srm15945 			bool_t got_mnt_error;
15110Sstevel@tonic-gate 		/*
15120Sstevel@tonic-gate 		 * If we started with a URL, if first byte of path is not "/",
15130Sstevel@tonic-gate 		 * then the mount will likely fail, so we should try again
15140Sstevel@tonic-gate 		 * with a prepended "/".
15150Sstevel@tonic-gate 		 */
1516*4590Srm15945 			if (mfs->mfs_flags & MFS_ALLOC_DIR && *dir != '/')
1517*4590Srm15945 				give_up_on_mnt = FALSE;
1518*4590Srm15945 			else
1519*4590Srm15945 				give_up_on_mnt = TRUE;
1520*4590Srm15945 
1521*4590Srm15945 			got_mnt_error = FALSE;
15220Sstevel@tonic-gate 
15230Sstevel@tonic-gate try_mnt_slash:
1524*4590Srm15945 			if (got_mnt_error == TRUE) {
1525*4590Srm15945 				int i, l;
1526*4590Srm15945 
1527*4590Srm15945 				give_up_on_mnt = TRUE;
1528*4590Srm15945 				l = strlen(dir);
1529*4590Srm15945 
1530*4590Srm15945 				/*
1531*4590Srm15945 				 * Insert a "/" to front of mfs_dir.
1532*4590Srm15945 				 */
1533*4590Srm15945 				for (i = l; i > 0; i--)
1534*4590Srm15945 					dir[i] = dir[i-1];
1535*4590Srm15945 
1536*4590Srm15945 				dir[0] = '/';
1537*4590Srm15945 			}
1538*4590Srm15945 
1539*4590Srm15945 			/* Get fhandle of remote path from server's mountd */
1540*4590Srm15945 
1541*4590Srm15945 			switch (outvers) {
1542*4590Srm15945 			case MOUNTVERS:
1543*4590Srm15945 				if (posix) {
1544*4590Srm15945 					free(argp);
1545*4590Srm15945 					head = prevhead;
1546*4590Srm15945 					tail = prevtail;
1547*4590Srm15945 					if (tail)
1548*4590Srm15945 						tail->nfs_ext_u.nfs_extB.next =
1549*4590Srm15945 						    NULL;
1550*4590Srm15945 					last_error = NFSERR_NOENT;
1551*4590Srm15945 					syslog(loglevel,
1552*4590Srm15945 					    "can't get posix info for %s",
1553*4590Srm15945 					    host);
1554*4590Srm15945 					destroy_auth_client_handle(cl);
1555*4590Srm15945 					skipentry = 1;
1556*4590Srm15945 					mfs->mfs_ignore = 1;
1557*4590Srm15945 					continue;
1558*4590Srm15945 				}
1559*4590Srm15945 		    /* FALLTHRU */
1560*4590Srm15945 			case MOUNTVERS_POSIX:
1561*4590Srm15945 				if (nfsvers == NFS_V3) {
1562*4590Srm15945 					free(argp);
1563*4590Srm15945 					head = prevhead;
1564*4590Srm15945 					tail = prevtail;
1565*4590Srm15945 					if (tail)
1566*4590Srm15945 						tail->nfs_ext_u.nfs_extB.next =
1567*4590Srm15945 						    NULL;
1568*4590Srm15945 					last_error = NFSERR_NOENT;
1569*4590Srm15945 					syslog(loglevel,
1570*4590Srm15945 					    "%s doesn't support NFS Version 3",
1571*4590Srm15945 					    host);
1572*4590Srm15945 					destroy_auth_client_handle(cl);
1573*4590Srm15945 					skipentry = 1;
1574*4590Srm15945 					mfs->mfs_ignore = 1;
1575*4590Srm15945 					continue;
1576*4590Srm15945 				}
1577*4590Srm15945 				rpc_stat = clnt_call(cl, MOUNTPROC_MNT,
1578*4590Srm15945 				    xdr_dirpath, (caddr_t)&dir,
1579*4590Srm15945 				    xdr_fhstatus, (caddr_t)&fhs, timeout);
1580*4590Srm15945 				if (rpc_stat != RPC_SUCCESS) {
1581*4590Srm15945 
1582*4590Srm15945 					if (give_up_on_mnt == FALSE) {
1583*4590Srm15945 						got_mnt_error = TRUE;
1584*4590Srm15945 						goto try_mnt_slash;
1585*4590Srm15945 					}
1586*4590Srm15945 
1587*4590Srm15945 				/*
1588*4590Srm15945 				 * Given the way "clnt_sperror" works, the "%s"
1589*4590Srm15945 				 * immediately following the "not responding"
1590*4590Srm15945 				 * is correct.
1591*4590Srm15945 				 */
1592*4590Srm15945 					free(argp);
1593*4590Srm15945 					head = prevhead;
1594*4590Srm15945 					tail = prevtail;
1595*4590Srm15945 					if (tail)
1596*4590Srm15945 						tail->nfs_ext_u.nfs_extB.next =
1597*4590Srm15945 						    NULL;
1598*4590Srm15945 					last_error = NFSERR_NOENT;
1599*4590Srm15945 
1600*4590Srm15945 					if (retries-- > 0) {
1601*4590Srm15945 						destroy_auth_client_handle(cl);
1602*4590Srm15945 						DELAY(delay);
1603*4590Srm15945 						goto retry;
1604*4590Srm15945 					}
1605*4590Srm15945 
1606*4590Srm15945 					if (trace > 3) {
1607*4590Srm15945 						trace_prt(1,
1608*4590Srm15945 						    "  nfsmount: mount RPC "
1609*4590Srm15945 						    "failed for %s\n",
1610*4590Srm15945 						    host);
1611*4590Srm15945 					}
1612*4590Srm15945 					syslog(loglevel,
1613*4590Srm15945 					    "%s server not responding%s",
1614*4590Srm15945 					    host, clnt_sperror(cl, ""));
1615*4590Srm15945 					destroy_auth_client_handle(cl);
1616*4590Srm15945 					skipentry = 1;
1617*4590Srm15945 					mfs->mfs_ignore = 1;
1618*4590Srm15945 					continue;
1619*4590Srm15945 				}
1620*4590Srm15945 				if ((errno = fhs.fhs_status) != MNT_OK)  {
1621*4590Srm15945 
1622*4590Srm15945 					if (give_up_on_mnt == FALSE) {
1623*4590Srm15945 						got_mnt_error = TRUE;
1624*4590Srm15945 						goto try_mnt_slash;
1625*4590Srm15945 					}
1626*4590Srm15945 
1627*4590Srm15945 					free(argp);
1628*4590Srm15945 					head = prevhead;
1629*4590Srm15945 					tail = prevtail;
1630*4590Srm15945 					if (tail)
1631*4590Srm15945 						tail->nfs_ext_u.nfs_extB.next =
1632*4590Srm15945 						    NULL;
1633*4590Srm15945 					if (errno == EACCES) {
1634*4590Srm15945 						status = NFSERR_ACCES;
1635*4590Srm15945 					} else {
1636*4590Srm15945 						syslog(loglevel, "%s: %m",
1637*4590Srm15945 						    host);
1638*4590Srm15945 						status = NFSERR_IO;
1639*4590Srm15945 					}
1640*4590Srm15945 					if (trace > 3) {
1641*4590Srm15945 						trace_prt(1,
1642*4590Srm15945 						    "  nfsmount: mount RPC gave"
1643*4590Srm15945 						    " %d for %s:%s\n",
1644*4590Srm15945 						    errno, host, dir);
1645*4590Srm15945 					}
1646*4590Srm15945 					last_error = status;
1647*4590Srm15945 					destroy_auth_client_handle(cl);
1648*4590Srm15945 					skipentry = 1;
1649*4590Srm15945 					mfs->mfs_ignore = 1;
1650*4590Srm15945 					continue;
1651*4590Srm15945 				}
1652*4590Srm15945 				argp->fh = malloc((sizeof (fhandle)));
1653*4590Srm15945 				if (!argp->fh) {
1654*4590Srm15945 					syslog(LOG_ERR, "nfsmount: no memory");
1655*4590Srm15945 					last_error = NFSERR_IO;
1656*4590Srm15945 					destroy_auth_client_handle(cl);
1657*4590Srm15945 					goto out;
1658*4590Srm15945 				}
1659*4590Srm15945 				(void) memcpy(argp->fh,
1660*4590Srm15945 				    &fhs.fhstatus_u.fhs_fhandle,
1661*4590Srm15945 				    sizeof (fhandle));
1662*4590Srm15945 				break;
1663*4590Srm15945 			case MOUNTVERS3:
1664*4590Srm15945 				posix = 0;
1665*4590Srm15945 				(void) memset((char *)&res3, '\0',
1666*4590Srm15945 				    sizeof (res3));
1667*4590Srm15945 				rpc_stat = clnt_call(cl, MOUNTPROC_MNT,
1668*4590Srm15945 				    xdr_dirpath, (caddr_t)&dir,
1669*4590Srm15945 				    xdr_mountres3, (caddr_t)&res3, timeout);
1670*4590Srm15945 				if (rpc_stat != RPC_SUCCESS) {
1671*4590Srm15945 
1672*4590Srm15945 					if (give_up_on_mnt == FALSE) {
1673*4590Srm15945 						got_mnt_error = TRUE;
1674*4590Srm15945 						goto try_mnt_slash;
1675*4590Srm15945 					}
1676*4590Srm15945 
1677*4590Srm15945 				/*
1678*4590Srm15945 				 * Given the way "clnt_sperror" works, the "%s"
1679*4590Srm15945 				 * immediately following the "not responding"
1680*4590Srm15945 				 * is correct.
1681*4590Srm15945 				 */
1682*4590Srm15945 					free(argp);
1683*4590Srm15945 					head = prevhead;
1684*4590Srm15945 					tail = prevtail;
1685*4590Srm15945 					if (tail)
1686*4590Srm15945 						tail->nfs_ext_u.nfs_extB.next =
1687*4590Srm15945 						    NULL;
1688*4590Srm15945 					last_error = NFSERR_NOENT;
1689*4590Srm15945 
1690*4590Srm15945 					if (retries-- > 0) {
1691*4590Srm15945 						destroy_auth_client_handle(cl);
1692*4590Srm15945 						DELAY(delay);
1693*4590Srm15945 						goto retry;
1694*4590Srm15945 					}
1695*4590Srm15945 
1696*4590Srm15945 					if (trace > 3) {
1697*4590Srm15945 						trace_prt(1,
1698*4590Srm15945 						    "  nfsmount: mount RPC "
1699*4590Srm15945 						    "failed for %s\n",
1700*4590Srm15945 						    host);
1701*4590Srm15945 					}
1702*4590Srm15945 					syslog(loglevel,
1703*4590Srm15945 					    "%s server not responding%s",
1704*4590Srm15945 					    remname, clnt_sperror(cl, ""));
1705*4590Srm15945 					destroy_auth_client_handle(cl);
1706*4590Srm15945 					skipentry = 1;
1707*4590Srm15945 					mfs->mfs_ignore = 1;
1708*4590Srm15945 					continue;
1709*4590Srm15945 				}
1710*4590Srm15945 				if ((errno = res3.fhs_status) != MNT_OK)  {
1711*4590Srm15945 
1712*4590Srm15945 					if (give_up_on_mnt == FALSE) {
1713*4590Srm15945 						got_mnt_error = TRUE;
1714*4590Srm15945 						goto try_mnt_slash;
1715*4590Srm15945 					}
1716*4590Srm15945 
1717*4590Srm15945 					free(argp);
1718*4590Srm15945 					head = prevhead;
1719*4590Srm15945 					tail = prevtail;
1720*4590Srm15945 					if (tail)
1721*4590Srm15945 						tail->nfs_ext_u.nfs_extB.next =
1722*4590Srm15945 						    NULL;
1723*4590Srm15945 					if (errno == EACCES) {
1724*4590Srm15945 						status = NFSERR_ACCES;
1725*4590Srm15945 					} else {
1726*4590Srm15945 						syslog(loglevel, "%s: %m",
1727*4590Srm15945 						    remname);
1728*4590Srm15945 						status = NFSERR_IO;
1729*4590Srm15945 					}
1730*4590Srm15945 					if (trace > 3) {
1731*4590Srm15945 						trace_prt(1,
1732*4590Srm15945 						    "  nfsmount: mount RPC gave"
1733*4590Srm15945 						    " %d for %s:%s\n",
1734*4590Srm15945 						    errno, host, dir);
1735*4590Srm15945 					}
1736*4590Srm15945 					last_error = status;
1737*4590Srm15945 					destroy_auth_client_handle(cl);
1738*4590Srm15945 					skipentry = 1;
1739*4590Srm15945 					mfs->mfs_ignore = 1;
1740*4590Srm15945 					continue;
1741*4590Srm15945 				}
17420Sstevel@tonic-gate 
17430Sstevel@tonic-gate 			/*
1744*4590Srm15945 			 *  Negotiate the security flavor for nfs_mount
17450Sstevel@tonic-gate 			 */
1746*4590Srm15945 				auths = res3.mountres3_u.mountinfo.
1747*4590Srm15945 				    auth_flavors.auth_flavors_val;
1748*4590Srm15945 				count = res3.mountres3_u.mountinfo.
1749*4590Srm15945 				    auth_flavors.auth_flavors_len;
1750*4590Srm15945 
1751*4590Srm15945 				if (sec_opt) {
1752*4590Srm15945 					for (i = 0; i < count; i++)
1753*4590Srm15945 						if (auths[i] ==
1754*4590Srm15945 						    nfs_sec.sc_nfsnum) {
1755*4590Srm15945 							break;
1756*4590Srm15945 						}
1757*4590Srm15945 					if (i >= count) {
1758*4590Srm15945 						syslog(LOG_ERR,
1759*4590Srm15945 						    "%s: does not support "
1760*4590Srm15945 						    "security \"%s\"\n",
1761*4590Srm15945 						    remname, nfs_sec.sc_name);
1762*4590Srm15945 						clnt_freeres(cl, xdr_mountres3,
1763*4590Srm15945 						    (caddr_t)&res3);
1764*4590Srm15945 						free(argp);
1765*4590Srm15945 						head = prevhead;
1766*4590Srm15945 						tail = prevtail;
1767*4590Srm15945 						if (tail)
1768*4590Srm15945 							tail->nfs_ext_u.
1769*4590Srm15945 							    nfs_extB.next =
1770*4590Srm15945 							    NULL;
1771*4590Srm15945 						last_error = NFSERR_IO;
1772*4590Srm15945 						destroy_auth_client_handle(cl);
1773*4590Srm15945 						skipentry = 1;
1774*4590Srm15945 						mfs->mfs_ignore = 1;
1775*4590Srm15945 						continue;
1776*4590Srm15945 					}
1777*4590Srm15945 				} else if (count > 0) {
1778*4590Srm15945 					for (i = 0; i < count; i++) {
1779*4590Srm15945 						if (!(scerror =
1780*4590Srm15945 						    nfs_getseconfig_bynumber(
1781*4590Srm15945 						    auths[i], &nfs_sec))) {
1782*4590Srm15945 							sec_opt++;
1783*4590Srm15945 							break;
1784*4590Srm15945 						}
1785*4590Srm15945 					}
1786*4590Srm15945 					if (i >= count) {
1787*4590Srm15945 						if (nfs_syslog_scerr(scerror,
1788*4590Srm15945 						    scerror_msg)
1789*4590Srm15945 						    != -1) {
1790*4590Srm15945 							syslog(LOG_ERR,
1791*4590Srm15945 							    "%s cannot be "
1792*4590Srm15945 							    "mounted because it"
1793*4590Srm15945 							    " is shared with "
1794*4590Srm15945 							    "security flavor %d"
1795*4590Srm15945 							    " which %s",
1796*4590Srm15945 							    remname,
1797*4590Srm15945 							    auths[i-1],
1798*4590Srm15945 							    scerror_msg);
1799*4590Srm15945 						}
1800*4590Srm15945 						clnt_freeres(cl, xdr_mountres3,
1801*4590Srm15945 						    (caddr_t)&res3);
1802*4590Srm15945 						free(argp);
1803*4590Srm15945 						head = prevhead;
1804*4590Srm15945 						tail = prevtail;
1805*4590Srm15945 						if (tail)
1806*4590Srm15945 							tail->nfs_ext_u.
1807*4590Srm15945 							    nfs_extB.next =
1808*4590Srm15945 							    NULL;
1809*4590Srm15945 						last_error = NFSERR_IO;
1810*4590Srm15945 						destroy_auth_client_handle(cl);
1811*4590Srm15945 						skipentry = 1;
1812*4590Srm15945 						mfs->mfs_ignore = 1;
1813*4590Srm15945 						continue;
1814*4590Srm15945 						}
1815*4590Srm15945 				}
1816*4590Srm15945 
1817*4590Srm15945 				fh3.fh3_length =
1818*4590Srm15945 				    res3.mountres3_u.mountinfo.fhandle.
1819*4590Srm15945 				    fhandle3_len;
1820*4590Srm15945 				(void) memcpy(fh3.fh3_u.data,
1821*4590Srm15945 				    res3.mountres3_u.mountinfo.fhandle.
1822*4590Srm15945 				    fhandle3_val,
1823*4590Srm15945 				    fh3.fh3_length);
1824*4590Srm15945 				clnt_freeres(cl, xdr_mountres3,
1825*4590Srm15945 				    (caddr_t)&res3);
1826*4590Srm15945 				argp->fh = malloc(sizeof (nfs_fh3));
1827*4590Srm15945 				if (!argp->fh) {
1828*4590Srm15945 					syslog(LOG_ERR, "nfsmount: no memory");
1829*4590Srm15945 					last_error = NFSERR_IO;
1830*4590Srm15945 					destroy_auth_client_handle(cl);
1831*4590Srm15945 					goto out;
1832*4590Srm15945 				}
1833*4590Srm15945 				(void) memcpy(argp->fh, &fh3, sizeof (nfs_fh3));
1834*4590Srm15945 				break;
1835*4590Srm15945 			default:
18360Sstevel@tonic-gate 				free(argp);
18370Sstevel@tonic-gate 				head = prevhead;
18380Sstevel@tonic-gate 				tail = prevtail;
18390Sstevel@tonic-gate 				if (tail)
18400Sstevel@tonic-gate 					tail->nfs_ext_u.nfs_extB.next = NULL;
18410Sstevel@tonic-gate 				last_error = NFSERR_NOENT;
18420Sstevel@tonic-gate 				syslog(loglevel,
1843*4590Srm15945 				    "unknown MOUNT version %ld on %s",
1844*4590Srm15945 				    vers, remname);
18450Sstevel@tonic-gate 				destroy_auth_client_handle(cl);
18460Sstevel@tonic-gate 				skipentry = 1;
18470Sstevel@tonic-gate 				mfs->mfs_ignore = 1;
18480Sstevel@tonic-gate 				continue;
1849*4590Srm15945 			} /* switch */
18500Sstevel@tonic-gate 		}
18510Sstevel@tonic-gate 		if (nfsvers == NFS_V4) {
18520Sstevel@tonic-gate 			argp->fh = strdup(dir);
18530Sstevel@tonic-gate 			if (argp->fh == NULL) {
18540Sstevel@tonic-gate 				syslog(LOG_ERR, "nfsmount: no memory");
18550Sstevel@tonic-gate 				last_error = NFSERR_IO;
18560Sstevel@tonic-gate 				goto out;
18570Sstevel@tonic-gate 			}
18580Sstevel@tonic-gate 		}
18590Sstevel@tonic-gate 
18600Sstevel@tonic-gate 		if (trace > 4)
18610Sstevel@tonic-gate 			trace_prt(1, "	nfsmount: have %s filehandle for %s\n",
18620Sstevel@tonic-gate 			    fstype, remname);
18630Sstevel@tonic-gate 
18640Sstevel@tonic-gate 		argp->flags |= NFSMNT_NEWARGS;
18650Sstevel@tonic-gate 		argp->flags |= NFSMNT_INT;	/* default is "intr" */
18663767Sjs195444 		argp->flags |= NFSMNT_HOSTNAME;
18672170Sevanl 		argp->hostname = strdup(host);
18683767Sjs195444 		if (argp->hostname == NULL) {
18693767Sjs195444 			syslog(LOG_ERR, "nfsmount: no memory");
18703767Sjs195444 			last_error = NFSERR_IO;
18713767Sjs195444 			goto out;
18723767Sjs195444 		}
18730Sstevel@tonic-gate 
18740Sstevel@tonic-gate 		/*
18750Sstevel@tonic-gate 		 * In this case, we want NFSv4 to behave like
18760Sstevel@tonic-gate 		 * non-WebNFS so that we get the server address.
18770Sstevel@tonic-gate 		 */
18780Sstevel@tonic-gate 		if ((mfs->mfs_flags & MFS_FH_VIA_WEBNFS) == 0) {
18790Sstevel@tonic-gate 			nconf = NULL;
18800Sstevel@tonic-gate 
18810Sstevel@tonic-gate 			if (nfs_port != 0)
18820Sstevel@tonic-gate 				thisport = nfs_port;
18830Sstevel@tonic-gate 			else
18840Sstevel@tonic-gate 				thisport = mfs->mfs_port;
18850Sstevel@tonic-gate 
18860Sstevel@tonic-gate 			/*
18870Sstevel@tonic-gate 			 * For NFSv4, we want to avoid rpcbind, so call
18880Sstevel@tonic-gate 			 * get_server_stuff() directly to tell it that
18890Sstevel@tonic-gate 			 * we want to go "direct_to_server".  Otherwise,
18900Sstevel@tonic-gate 			 * do what has always been done.
18910Sstevel@tonic-gate 			 */
18920Sstevel@tonic-gate 			if (nfsvers == NFS_V4) {
18930Sstevel@tonic-gate 				enum clnt_stat cstat;
18940Sstevel@tonic-gate 				argp->addr = get_server_stuff(SERVER_ADDR,
1895*4590Srm15945 				    host, NFS_PROGRAM, nfsvers, NULL,
1896*4590Srm15945 				    &nconf, nfs_proto, thisport, NULL,
1897*4590Srm15945 				    NULL, TRUE, NULL, &cstat);
18980Sstevel@tonic-gate 			} else {
18990Sstevel@tonic-gate 				argp->addr = get_addr(host, NFS_PROGRAM,
1900*4590Srm15945 				    nfsvers, &nconf, nfs_proto,
1901*4590Srm15945 				    thisport, NULL);
19020Sstevel@tonic-gate 			}
19030Sstevel@tonic-gate 
19040Sstevel@tonic-gate 			if (argp->addr == NULL) {
19052170Sevanl 				if (argp->hostname)
19062170Sevanl 					free(argp->hostname);
19070Sstevel@tonic-gate 				free(argp->fh);
19080Sstevel@tonic-gate 				free(argp);
19090Sstevel@tonic-gate 				head = prevhead;
19100Sstevel@tonic-gate 				tail = prevtail;
19110Sstevel@tonic-gate 				if (tail)
19120Sstevel@tonic-gate 					tail->nfs_ext_u.nfs_extB.next = NULL;
19130Sstevel@tonic-gate 				last_error = NFSERR_NOENT;
19140Sstevel@tonic-gate 
19150Sstevel@tonic-gate 				if (retries-- > 0) {
19160Sstevel@tonic-gate 					destroy_auth_client_handle(cl);
19170Sstevel@tonic-gate 					DELAY(delay);
19180Sstevel@tonic-gate 					goto retry;
19190Sstevel@tonic-gate 				}
19200Sstevel@tonic-gate 
19210Sstevel@tonic-gate 				syslog(loglevel, "%s: no NFS service", host);
19220Sstevel@tonic-gate 				destroy_auth_client_handle(cl);
19230Sstevel@tonic-gate 				skipentry = 1;
19240Sstevel@tonic-gate 				mfs->mfs_ignore = 1;
19250Sstevel@tonic-gate 				continue;
19260Sstevel@tonic-gate 			}
19270Sstevel@tonic-gate 			if (trace > 4)
19280Sstevel@tonic-gate 				trace_prt(1,
19290Sstevel@tonic-gate 				    "\tnfsmount: have net address for %s\n",
19300Sstevel@tonic-gate 				    remname);
19310Sstevel@tonic-gate 
19320Sstevel@tonic-gate 		} else {
19330Sstevel@tonic-gate 			nconf = mfs->mfs_nconf;
19340Sstevel@tonic-gate 			mfs->mfs_nconf = NULL;
19350Sstevel@tonic-gate 		}
19360Sstevel@tonic-gate 
19370Sstevel@tonic-gate 		argp->flags |= NFSMNT_KNCONF;
19380Sstevel@tonic-gate 		argp->knconf = get_knconf(nconf);
19390Sstevel@tonic-gate 		if (argp->knconf == NULL) {
19400Sstevel@tonic-gate 			netbuf_free(argp->addr);
19410Sstevel@tonic-gate 			freenetconfigent(nconf);
19422170Sevanl 			if (argp->hostname)
19432170Sevanl 				free(argp->hostname);
19440Sstevel@tonic-gate 			free(argp->fh);
19450Sstevel@tonic-gate 			free(argp);
19460Sstevel@tonic-gate 			head = prevhead;
19470Sstevel@tonic-gate 			tail = prevtail;
19480Sstevel@tonic-gate 			if (tail)
19490Sstevel@tonic-gate 				tail->nfs_ext_u.nfs_extB.next = NULL;
19500Sstevel@tonic-gate 			last_error = NFSERR_NOSPC;
19510Sstevel@tonic-gate 			destroy_auth_client_handle(cl);
19520Sstevel@tonic-gate 			skipentry = 1;
19530Sstevel@tonic-gate 			mfs->mfs_ignore = 1;
19540Sstevel@tonic-gate 			continue;
19550Sstevel@tonic-gate 		}
19560Sstevel@tonic-gate 		if (trace > 4)
19570Sstevel@tonic-gate 			trace_prt(1,
19580Sstevel@tonic-gate 			    "\tnfsmount: have net config for %s\n",
19590Sstevel@tonic-gate 			    remname);
19600Sstevel@tonic-gate 
19610Sstevel@tonic-gate 		if (hasmntopt(&m, MNTOPT_SOFT) != NULL) {
19620Sstevel@tonic-gate 			argp->flags |= NFSMNT_SOFT;
19630Sstevel@tonic-gate 		}
19640Sstevel@tonic-gate 		if (hasmntopt(&m, MNTOPT_NOINTR) != NULL) {
19650Sstevel@tonic-gate 			argp->flags &= ~(NFSMNT_INT);
19660Sstevel@tonic-gate 		}
19670Sstevel@tonic-gate 		if (hasmntopt(&m, MNTOPT_NOAC) != NULL) {
19680Sstevel@tonic-gate 			argp->flags |= NFSMNT_NOAC;
19690Sstevel@tonic-gate 		}
19700Sstevel@tonic-gate 		if (hasmntopt(&m, MNTOPT_NOCTO) != NULL) {
19710Sstevel@tonic-gate 			argp->flags |= NFSMNT_NOCTO;
19720Sstevel@tonic-gate 		}
19730Sstevel@tonic-gate 		if (hasmntopt(&m, MNTOPT_FORCEDIRECTIO) != NULL) {
19740Sstevel@tonic-gate 			argp->flags |= NFSMNT_DIRECTIO;
19750Sstevel@tonic-gate 		}
19760Sstevel@tonic-gate 		if (hasmntopt(&m, MNTOPT_NOFORCEDIRECTIO) != NULL) {
19770Sstevel@tonic-gate 			argp->flags &= ~(NFSMNT_DIRECTIO);
19780Sstevel@tonic-gate 		}
19790Sstevel@tonic-gate 
19800Sstevel@tonic-gate 		/*
19810Sstevel@tonic-gate 		 * Set up security data for argp->nfs_ext_u.nfs_extB.secdata.
19820Sstevel@tonic-gate 		 */
19830Sstevel@tonic-gate 		if (mfssnego.snego_done) {
19840Sstevel@tonic-gate 			memcpy(&nfs_sec, &mfssnego.nfs_sec,
1985*4590Srm15945 			    sizeof (seconfig_t));
19860Sstevel@tonic-gate 		} else if (!sec_opt) {
19870Sstevel@tonic-gate 			/*
19880Sstevel@tonic-gate 			 * Get default security mode.
19890Sstevel@tonic-gate 			 */
19900Sstevel@tonic-gate 			if (nfs_getseconfig_default(&nfs_sec)) {
19910Sstevel@tonic-gate 				syslog(loglevel,
19920Sstevel@tonic-gate 				    "error getting default security entry\n");
19930Sstevel@tonic-gate 				free_knconf(argp->knconf);
19940Sstevel@tonic-gate 				netbuf_free(argp->addr);
19950Sstevel@tonic-gate 				freenetconfigent(nconf);
19962170Sevanl 				if (argp->hostname)
19972170Sevanl 					free(argp->hostname);
19980Sstevel@tonic-gate 				free(argp->fh);
19990Sstevel@tonic-gate 				free(argp);
20000Sstevel@tonic-gate 				head = prevhead;
20010Sstevel@tonic-gate 				tail = prevtail;
20020Sstevel@tonic-gate 				if (tail)
20030Sstevel@tonic-gate 					tail->nfs_ext_u.nfs_extB.next = NULL;
20040Sstevel@tonic-gate 				last_error = NFSERR_NOSPC;
20050Sstevel@tonic-gate 				destroy_auth_client_handle(cl);
20060Sstevel@tonic-gate 				skipentry = 1;
20070Sstevel@tonic-gate 				mfs->mfs_ignore = 1;
20080Sstevel@tonic-gate 				continue;
20090Sstevel@tonic-gate 			}
20100Sstevel@tonic-gate 			argp->flags |= NFSMNT_SECDEFAULT;
20110Sstevel@tonic-gate 		}
20120Sstevel@tonic-gate 
20130Sstevel@tonic-gate 		/*
20140Sstevel@tonic-gate 		 * For AUTH_DH
20150Sstevel@tonic-gate 		 * get the network address for the time service on
20160Sstevel@tonic-gate 		 * the server.	If an RPC based time service is
20170Sstevel@tonic-gate 		 * not available then try the IP time service.
20180Sstevel@tonic-gate 		 *
20190Sstevel@tonic-gate 		 * Eventurally, we want to move this code to nfs_clnt_secdata()
20200Sstevel@tonic-gate 		 * when autod_nfs.c and mount.c can share the same
20210Sstevel@tonic-gate 		 * get_the_addr/get_the_stuff routine.
20220Sstevel@tonic-gate 		 */
20230Sstevel@tonic-gate 		secflags = 0;
20240Sstevel@tonic-gate 		syncaddr = NULL;
20250Sstevel@tonic-gate 		retaddrs = NULL;
20260Sstevel@tonic-gate 
20270Sstevel@tonic-gate 		if (nfs_sec.sc_rpcnum == AUTH_DH || nfsvers == NFS_V4) {
20280Sstevel@tonic-gate 		/*
20290Sstevel@tonic-gate 		 * If not using the public fh and not NFS_V4, we can try
20300Sstevel@tonic-gate 		 * talking RPCBIND. Otherwise, assume that firewalls
20310Sstevel@tonic-gate 		 * prevent us from doing that.
20320Sstevel@tonic-gate 		 */
2033*4590Srm15945 			if ((mfs->mfs_flags & MFS_FH_VIA_WEBNFS) == 0 &&
2034*4590Srm15945 			    nfsvers != NFS_V4) {
20350Sstevel@tonic-gate 			syncaddr = get_the_stuff(SERVER_ADDR, host, RPCBPROG,
2036*4590Srm15945 			    RPCBVERS, NULL, nconf, 0, NULL, NULL, FALSE,
2037*4590Srm15945 			    NULL, NULL);
20380Sstevel@tonic-gate 			}
20390Sstevel@tonic-gate 
2040*4590Srm15945 			if (syncaddr != NULL) {
2041*4590Srm15945 				/* for flags in sec_data */
2042*4590Srm15945 				secflags |= AUTH_F_RPCTIMESYNC;
2043*4590Srm15945 			} else {
2044*4590Srm15945 				struct nd_hostserv hs;
2045*4590Srm15945 				int error;
2046*4590Srm15945 
2047*4590Srm15945 				hs.h_host = host;
2048*4590Srm15945 				hs.h_serv = "timserver";
2049*4590Srm15945 				error = netdir_getbyname(nconf, &hs, &retaddrs);
2050*4590Srm15945 
2051*4590Srm15945 				if (error != ND_OK &&
2052*4590Srm15945 				    nfs_sec.sc_rpcnum == AUTH_DH) {
2053*4590Srm15945 					syslog(loglevel,
2054*4590Srm15945 					    "%s: secure: no time service\n",
2055*4590Srm15945 					    host);
2056*4590Srm15945 					free_knconf(argp->knconf);
2057*4590Srm15945 					netbuf_free(argp->addr);
2058*4590Srm15945 					freenetconfigent(nconf);
2059*4590Srm15945 					if (argp->hostname)
2060*4590Srm15945 						free(argp->hostname);
2061*4590Srm15945 					free(argp->fh);
2062*4590Srm15945 					free(argp);
2063*4590Srm15945 					head = prevhead;
2064*4590Srm15945 					tail = prevtail;
2065*4590Srm15945 					if (tail)
2066*4590Srm15945 						tail->nfs_ext_u.nfs_extB.next =
2067*4590Srm15945 						    NULL;
2068*4590Srm15945 					last_error = NFSERR_IO;
2069*4590Srm15945 					destroy_auth_client_handle(cl);
2070*4590Srm15945 					skipentry = 1;
2071*4590Srm15945 					mfs->mfs_ignore = 1;
2072*4590Srm15945 					continue;
2073*4590Srm15945 				}
2074*4590Srm15945 
2075*4590Srm15945 				if (error == ND_OK)
2076*4590Srm15945 					syncaddr = retaddrs->n_addrs;
20770Sstevel@tonic-gate 
20780Sstevel@tonic-gate 			/*
20790Sstevel@tonic-gate 			 * For potential usage by NFS V4 when AUTH_DH
20800Sstevel@tonic-gate 			 * is negotiated via SECINFO in the kernel.
20810Sstevel@tonic-gate 			 */
2082*4590Srm15945 				if (nfsvers == NFS_V4 && syncaddr &&
20830Sstevel@tonic-gate 				    host2netname(netname, host, NULL)) {
2084*4590Srm15945 					argp->syncaddr =
2085*4590Srm15945 					    malloc(sizeof (struct netbuf));
2086*4590Srm15945 					argp->syncaddr->buf =
2087*4590Srm15945 					    malloc(syncaddr->len);
2088*4590Srm15945 					(void) memcpy(argp->syncaddr->buf,
2089*4590Srm15945 					    syncaddr->buf, syncaddr->len);
2090*4590Srm15945 					argp->syncaddr->len = syncaddr->len;
2091*4590Srm15945 					argp->syncaddr->maxlen =
2092*4590Srm15945 					    syncaddr->maxlen;
2093*4590Srm15945 					argp->netname = strdup(netname);
2094*4590Srm15945 					argp->flags |= NFSMNT_SECURE;
2095*4590Srm15945 				}
2096*4590Srm15945 			} /* syncaddr */
20970Sstevel@tonic-gate 		} /* AUTH_DH */
20980Sstevel@tonic-gate 
20991676Sjpk 		/*
21001676Sjpk 		 * TSOL notes: automountd in tsol extension
21011676Sjpk 		 * has "read down" capability, i.e. we allow
21021676Sjpk 		 * a user to trigger an nfs mount into a lower
21031676Sjpk 		 * labeled zone. We achieve this by always having
21041676Sjpk 		 * root issue the mount request so that the
21051676Sjpk 		 * lookup ops can go past /zone/<zone_name>
21061676Sjpk 		 * on the server side.
21071676Sjpk 		 */
21081676Sjpk 		if (is_system_labeled())
21091676Sjpk 			nfs_sec.sc_uid = (uid_t)0;
21101676Sjpk 		else
21113391Ssemery 			nfs_sec.sc_uid = uid;
21120Sstevel@tonic-gate 		/*
21130Sstevel@tonic-gate 		 * If AUTH_DH is a chosen flavor now, its data will be stored
21140Sstevel@tonic-gate 		 * in the sec_data structure via nfs_clnt_secdata().
21150Sstevel@tonic-gate 		 */
21160Sstevel@tonic-gate 		if (!(secdata = nfs_clnt_secdata(&nfs_sec, host, argp->knconf,
2117*4590Srm15945 		    syncaddr, secflags))) {
21180Sstevel@tonic-gate 			syslog(LOG_ERR,
2119*4590Srm15945 			    "errors constructing security related data\n");
21200Sstevel@tonic-gate 			if (secflags & AUTH_F_RPCTIMESYNC)
21210Sstevel@tonic-gate 				netbuf_free(syncaddr);
21220Sstevel@tonic-gate 			else if (retaddrs)
21230Sstevel@tonic-gate 				netdir_free(retaddrs, ND_ADDRLIST);
21240Sstevel@tonic-gate 			if (argp->syncaddr)
21250Sstevel@tonic-gate 				netbuf_free(argp->syncaddr);
21260Sstevel@tonic-gate 			if (argp->netname)
21270Sstevel@tonic-gate 				free(argp->netname);
21282170Sevanl 			if (argp->hostname)
21292170Sevanl 				free(argp->hostname);
21300Sstevel@tonic-gate 			free_knconf(argp->knconf);
21310Sstevel@tonic-gate 			netbuf_free(argp->addr);
21320Sstevel@tonic-gate 			freenetconfigent(nconf);
21330Sstevel@tonic-gate 			free(argp->fh);
21340Sstevel@tonic-gate 			free(argp);
21350Sstevel@tonic-gate 			head = prevhead;
21360Sstevel@tonic-gate 			tail = prevtail;
21370Sstevel@tonic-gate 			if (tail)
21380Sstevel@tonic-gate 				tail->nfs_ext_u.nfs_extB.next = NULL;
21390Sstevel@tonic-gate 			last_error = NFSERR_IO;
21400Sstevel@tonic-gate 			destroy_auth_client_handle(cl);
21410Sstevel@tonic-gate 			skipentry = 1;
21420Sstevel@tonic-gate 			mfs->mfs_ignore = 1;
21430Sstevel@tonic-gate 			continue;
21440Sstevel@tonic-gate 		}
21450Sstevel@tonic-gate 		NFS_ARGS_EXTB_secdata(*argp, secdata);
21460Sstevel@tonic-gate 		/* end of security stuff */
21470Sstevel@tonic-gate 
21480Sstevel@tonic-gate 		if (trace > 4)
21490Sstevel@tonic-gate 			trace_prt(1,
21500Sstevel@tonic-gate 			    "  nfsmount: have secure info for %s\n", remname);
21510Sstevel@tonic-gate 
21520Sstevel@tonic-gate 		if (hasmntopt(&m, MNTOPT_GRPID) != NULL) {
21530Sstevel@tonic-gate 			argp->flags |= NFSMNT_GRPID;
21540Sstevel@tonic-gate 		}
21550Sstevel@tonic-gate 		if (nopt(&m, MNTOPT_RSIZE, &argp->rsize)) {
21560Sstevel@tonic-gate 			argp->flags |= NFSMNT_RSIZE;
21570Sstevel@tonic-gate 		}
21580Sstevel@tonic-gate 		if (nopt(&m, MNTOPT_WSIZE, &argp->wsize)) {
21590Sstevel@tonic-gate 			argp->flags |= NFSMNT_WSIZE;
21600Sstevel@tonic-gate 		}
21610Sstevel@tonic-gate 		if (nopt(&m, MNTOPT_TIMEO, &argp->timeo)) {
21620Sstevel@tonic-gate 			argp->flags |= NFSMNT_TIMEO;
21630Sstevel@tonic-gate 		}
21640Sstevel@tonic-gate 		if (nopt(&m, MNTOPT_RETRANS, &argp->retrans)) {
21650Sstevel@tonic-gate 			argp->flags |= NFSMNT_RETRANS;
21660Sstevel@tonic-gate 		}
21670Sstevel@tonic-gate 		if (nopt(&m, MNTOPT_ACTIMEO, &argp->acregmax)) {
21680Sstevel@tonic-gate 			argp->flags |= NFSMNT_ACREGMAX;
21690Sstevel@tonic-gate 			argp->flags |= NFSMNT_ACDIRMAX;
21700Sstevel@tonic-gate 			argp->flags |= NFSMNT_ACDIRMIN;
21710Sstevel@tonic-gate 			argp->flags |= NFSMNT_ACREGMIN;
21720Sstevel@tonic-gate 			argp->acdirmin = argp->acregmin = argp->acdirmax
2173*4590Srm15945 			    = argp->acregmax;
21740Sstevel@tonic-gate 		} else {
21750Sstevel@tonic-gate 			if (nopt(&m, MNTOPT_ACREGMIN, &argp->acregmin)) {
21760Sstevel@tonic-gate 				argp->flags |= NFSMNT_ACREGMIN;
21770Sstevel@tonic-gate 			}
21780Sstevel@tonic-gate 			if (nopt(&m, MNTOPT_ACREGMAX, &argp->acregmax)) {
21790Sstevel@tonic-gate 				argp->flags |= NFSMNT_ACREGMAX;
21800Sstevel@tonic-gate 			}
21810Sstevel@tonic-gate 			if (nopt(&m, MNTOPT_ACDIRMIN, &argp->acdirmin)) {
21820Sstevel@tonic-gate 				argp->flags |= NFSMNT_ACDIRMIN;
21830Sstevel@tonic-gate 			}
21840Sstevel@tonic-gate 			if (nopt(&m, MNTOPT_ACDIRMAX, &argp->acdirmax)) {
21850Sstevel@tonic-gate 				argp->flags |= NFSMNT_ACDIRMAX;
21860Sstevel@tonic-gate 			}
21870Sstevel@tonic-gate 		}
21880Sstevel@tonic-gate 
21890Sstevel@tonic-gate 		if (posix) {
21900Sstevel@tonic-gate 			argp->pathconf = NULL;
21910Sstevel@tonic-gate 			if (error = get_pathconf(cl, dir, remname,
21920Sstevel@tonic-gate 			    &argp->pathconf, retries)) {
21930Sstevel@tonic-gate 				if (secflags & AUTH_F_RPCTIMESYNC)
21940Sstevel@tonic-gate 					netbuf_free(syncaddr);
21950Sstevel@tonic-gate 				else if (retaddrs)
21960Sstevel@tonic-gate 					netdir_free(retaddrs, ND_ADDRLIST);
21970Sstevel@tonic-gate 				free_knconf(argp->knconf);
21980Sstevel@tonic-gate 				netbuf_free(argp->addr);
21990Sstevel@tonic-gate 				freenetconfigent(nconf);
22000Sstevel@tonic-gate 				nfs_free_secdata(
2201*4590Srm15945 				    argp->nfs_ext_u.nfs_extB.secdata);
22020Sstevel@tonic-gate 				if (argp->syncaddr)
22030Sstevel@tonic-gate 					netbuf_free(argp->syncaddr);
22040Sstevel@tonic-gate 				if (argp->netname)
22050Sstevel@tonic-gate 					free(argp->netname);
22062170Sevanl 				if (argp->hostname)
22072170Sevanl 					free(argp->hostname);
22080Sstevel@tonic-gate 				free(argp->fh);
22090Sstevel@tonic-gate 				free(argp);
22100Sstevel@tonic-gate 				head = prevhead;
22110Sstevel@tonic-gate 				tail = prevtail;
22120Sstevel@tonic-gate 				if (tail)
22130Sstevel@tonic-gate 					tail->nfs_ext_u.nfs_extB.next = NULL;
22140Sstevel@tonic-gate 				last_error = NFSERR_IO;
22150Sstevel@tonic-gate 
22160Sstevel@tonic-gate 				if (error == RET_RETRY && retries-- > 0) {
22170Sstevel@tonic-gate 					destroy_auth_client_handle(cl);
22180Sstevel@tonic-gate 					DELAY(delay);
22190Sstevel@tonic-gate 					goto retry;
22200Sstevel@tonic-gate 				}
22210Sstevel@tonic-gate 
22220Sstevel@tonic-gate 				destroy_auth_client_handle(cl);
22230Sstevel@tonic-gate 				skipentry = 1;
22240Sstevel@tonic-gate 				mfs->mfs_ignore = 1;
22250Sstevel@tonic-gate 				continue;
22260Sstevel@tonic-gate 			}
22270Sstevel@tonic-gate 			argp->flags |= NFSMNT_POSIX;
22280Sstevel@tonic-gate 			if (trace > 4)
22290Sstevel@tonic-gate 				trace_prt(1,
22300Sstevel@tonic-gate 				    "  nfsmount: have pathconf for %s\n",
22310Sstevel@tonic-gate 				    remname);
22320Sstevel@tonic-gate 		}
22330Sstevel@tonic-gate 
22340Sstevel@tonic-gate 		/*
22350Sstevel@tonic-gate 		 * free loop-specific data structures
22360Sstevel@tonic-gate 		 */
22370Sstevel@tonic-gate 		destroy_auth_client_handle(cl);
22380Sstevel@tonic-gate 		freenetconfigent(nconf);
22390Sstevel@tonic-gate 		if (secflags & AUTH_F_RPCTIMESYNC)
22400Sstevel@tonic-gate 			netbuf_free(syncaddr);
22410Sstevel@tonic-gate 		else if (retaddrs)
22420Sstevel@tonic-gate 			netdir_free(retaddrs, ND_ADDRLIST);
22430Sstevel@tonic-gate 
22440Sstevel@tonic-gate 		/*
22450Sstevel@tonic-gate 		 * Decide whether to use remote host's lockd or local locking.
22460Sstevel@tonic-gate 		 * If we are using the public fh, we've already turned
22470Sstevel@tonic-gate 		 * LLOCK on.
22480Sstevel@tonic-gate 		 */
22490Sstevel@tonic-gate 		if (hasmntopt(&m, MNTOPT_LLOCK))
22500Sstevel@tonic-gate 			argp->flags |= NFSMNT_LLOCK;
22510Sstevel@tonic-gate 		if (!(argp->flags & NFSMNT_LLOCK) && nfsvers == NFS_VERSION &&
2252*4590Srm15945 		    remote_lock(host, argp->fh)) {
22530Sstevel@tonic-gate 			syslog(loglevel, "No network locking on %s : "
22540Sstevel@tonic-gate 			"contact admin to install server change", host);
22550Sstevel@tonic-gate 			argp->flags |= NFSMNT_LLOCK;
22560Sstevel@tonic-gate 		}
22570Sstevel@tonic-gate 
22580Sstevel@tonic-gate 		/*
22590Sstevel@tonic-gate 		 * Build a string for /etc/mnttab.
22600Sstevel@tonic-gate 		 * If possible, coalesce strings with same 'dir' info.
22610Sstevel@tonic-gate 		 */
22620Sstevel@tonic-gate 		if ((mfs->mfs_flags & MFS_URL) == 0) {
22630Sstevel@tonic-gate 			char *tmp;
22640Sstevel@tonic-gate 
22650Sstevel@tonic-gate 			if (mnttabcnt) {
22660Sstevel@tonic-gate 				p = strrchr(mnttabtext, (int)':');
22670Sstevel@tonic-gate 				if (!p || strcmp(p+1, dir) != 0) {
22680Sstevel@tonic-gate 					mnttabcnt += strlen(remname) + 2;
22690Sstevel@tonic-gate 				} else {
22700Sstevel@tonic-gate 					*p = '\0';
22713767Sjs195444 					mnttabcnt += strlen(rhost) + 2;
22720Sstevel@tonic-gate 				}
22730Sstevel@tonic-gate 				if ((tmp = realloc(mnttabtext,
22740Sstevel@tonic-gate 				    mnttabcnt)) != NULL) {
22750Sstevel@tonic-gate 					mnttabtext = tmp;
22760Sstevel@tonic-gate 					strcat(mnttabtext, ",");
22770Sstevel@tonic-gate 				} else {
22780Sstevel@tonic-gate 					free(mnttabtext);
22790Sstevel@tonic-gate 					mnttabtext = NULL;
22800Sstevel@tonic-gate 				}
22810Sstevel@tonic-gate 			} else {
22820Sstevel@tonic-gate 				mnttabcnt = strlen(remname) + 1;
22830Sstevel@tonic-gate 				if ((mnttabtext = malloc(mnttabcnt)) != NULL)
22840Sstevel@tonic-gate 					mnttabtext[0] = '\0';
22850Sstevel@tonic-gate 			}
22860Sstevel@tonic-gate 
22870Sstevel@tonic-gate 			if (mnttabtext != NULL)
22880Sstevel@tonic-gate 				strcat(mnttabtext, remname);
22890Sstevel@tonic-gate 
22900Sstevel@tonic-gate 		} else {
22910Sstevel@tonic-gate 			char *tmp;
22920Sstevel@tonic-gate 			int more_cnt = 0;
22930Sstevel@tonic-gate 			char sport[16];
22940Sstevel@tonic-gate 
22950Sstevel@tonic-gate 			more_cnt += strlen("nfs://");
22960Sstevel@tonic-gate 			more_cnt += strlen(mfs->mfs_host);
22970Sstevel@tonic-gate 
22980Sstevel@tonic-gate 			if (mfs->mfs_port != 0) {
22990Sstevel@tonic-gate 				(void) sprintf(sport, ":%u", mfs->mfs_port);
23000Sstevel@tonic-gate 			} else
23010Sstevel@tonic-gate 				sport[0] = '\0';
23020Sstevel@tonic-gate 
23030Sstevel@tonic-gate 			more_cnt += strlen(sport);
23040Sstevel@tonic-gate 			more_cnt += 1; /* "/" */
23050Sstevel@tonic-gate 			more_cnt += strlen(mfs->mfs_dir);
23060Sstevel@tonic-gate 
23070Sstevel@tonic-gate 			if (mnttabcnt) {
23080Sstevel@tonic-gate 				more_cnt += 1; /* "," */
23090Sstevel@tonic-gate 				mnttabcnt += more_cnt;
23100Sstevel@tonic-gate 
23110Sstevel@tonic-gate 				if ((tmp = realloc(mnttabtext,
23120Sstevel@tonic-gate 				    mnttabcnt)) != NULL) {
23130Sstevel@tonic-gate 					mnttabtext = tmp;
23140Sstevel@tonic-gate 					strcat(mnttabtext, ",");
23150Sstevel@tonic-gate 				} else {
23160Sstevel@tonic-gate 					free(mnttabtext);
23170Sstevel@tonic-gate 					mnttabtext = NULL;
23180Sstevel@tonic-gate 				}
23190Sstevel@tonic-gate 			} else {
23200Sstevel@tonic-gate 				mnttabcnt = more_cnt + 1;
23210Sstevel@tonic-gate 				if ((mnttabtext = malloc(mnttabcnt)) != NULL)
23220Sstevel@tonic-gate 					mnttabtext[0] = '\0';
23230Sstevel@tonic-gate 			}
23240Sstevel@tonic-gate 
23250Sstevel@tonic-gate 			if (mnttabtext != NULL) {
23260Sstevel@tonic-gate 				strcat(mnttabtext, "nfs://");
23270Sstevel@tonic-gate 				strcat(mnttabtext, mfs->mfs_host);
23280Sstevel@tonic-gate 				strcat(mnttabtext, sport);
23290Sstevel@tonic-gate 				strcat(mnttabtext, "/");
23300Sstevel@tonic-gate 				strcat(mnttabtext, mfs->mfs_dir);
23310Sstevel@tonic-gate 			}
23320Sstevel@tonic-gate 		}
23330Sstevel@tonic-gate 
23340Sstevel@tonic-gate 		if (!mnttabtext) {
23350Sstevel@tonic-gate 			syslog(LOG_ERR, "nfsmount: no memory");
23360Sstevel@tonic-gate 			last_error = NFSERR_IO;
23370Sstevel@tonic-gate 			goto out;
23380Sstevel@tonic-gate 		}
23390Sstevel@tonic-gate 
23400Sstevel@tonic-gate 		/*
23410Sstevel@tonic-gate 		 * At least one entry, can call mount(2).
23420Sstevel@tonic-gate 		 */
23430Sstevel@tonic-gate 		entries++;
23440Sstevel@tonic-gate 
23450Sstevel@tonic-gate 		/*
23460Sstevel@tonic-gate 		 * If replication was defeated, don't do more work
23470Sstevel@tonic-gate 		 */
23480Sstevel@tonic-gate 		if (!replicated)
23490Sstevel@tonic-gate 			break;
23500Sstevel@tonic-gate 	}
23510Sstevel@tonic-gate 
23520Sstevel@tonic-gate 
23530Sstevel@tonic-gate 	/*
23540Sstevel@tonic-gate 	 * Did we get through all possibilities without success?
23550Sstevel@tonic-gate 	 */
23560Sstevel@tonic-gate 	if (!entries)
23570Sstevel@tonic-gate 		goto out;
23580Sstevel@tonic-gate 
23590Sstevel@tonic-gate 	/* Make "xattr" the default if "noxattr" is not specified. */
23600Sstevel@tonic-gate 	strcpy(mopts, opts);
23610Sstevel@tonic-gate 	if (!hasmntopt(&m, MNTOPT_NOXATTR) && !hasmntopt(&m, MNTOPT_XATTR)) {
23620Sstevel@tonic-gate 		if (strlen(mopts) > 0)
23630Sstevel@tonic-gate 			strcat(mopts, ",");
23640Sstevel@tonic-gate 		strcat(mopts, "xattr");
23650Sstevel@tonic-gate 	}
23660Sstevel@tonic-gate 
23670Sstevel@tonic-gate 	/*
23680Sstevel@tonic-gate 	 * enable services as needed.
23690Sstevel@tonic-gate 	 */
23700Sstevel@tonic-gate 	{
23710Sstevel@tonic-gate 		char **sl;
23720Sstevel@tonic-gate 
23730Sstevel@tonic-gate 		if (strcmp(fstype, MNTTYPE_NFS4) == 0)
23740Sstevel@tonic-gate 			sl = service_list_v4;
23750Sstevel@tonic-gate 		else
23760Sstevel@tonic-gate 			sl = service_list;
23770Sstevel@tonic-gate 
23781676Sjpk 		(void) _check_services(sl);
23790Sstevel@tonic-gate 	}
23800Sstevel@tonic-gate 
23810Sstevel@tonic-gate 	/*
23820Sstevel@tonic-gate 	 * Whew; do the mount, at last.
23830Sstevel@tonic-gate 	 */
23840Sstevel@tonic-gate 	if (trace > 1) {
23850Sstevel@tonic-gate 		trace_prt(1, "	mount %s %s (%s)\n", mnttabtext, mntpnt, mopts);
23860Sstevel@tonic-gate 	}
23870Sstevel@tonic-gate 
23882170Sevanl 	/*
23892170Sevanl 	 * If no action list pointer then do the mount, otherwise
23902170Sevanl 	 * build the actions list pointer with the mount information.
23912170Sevanl 	 * so the mount can be done in the kernel.
23922170Sevanl 	 */
23932170Sevanl 	if (alp == NULL) {
23942170Sevanl 		if (mount(mnttabtext, mntpnt, flags | MS_DATA, fstype,
2395*4590Srm15945 		    head, sizeof (*head), mopts, MAX_MNTOPT_STR) < 0) {
23962170Sevanl 			if (trace > 1)
23972170Sevanl 				trace_prt(1, "	Mount of %s on %s: %d\n",
2398*4590Srm15945 				    mnttabtext, mntpnt, errno);
23992170Sevanl 			if (errno != EBUSY || verbose)
24002170Sevanl 				syslog(LOG_ERR,
24010Sstevel@tonic-gate 				"Mount of %s on %s: %m", mnttabtext, mntpnt);
24022170Sevanl 			last_error = NFSERR_IO;
24032170Sevanl 			goto out;
24042170Sevanl 		}
24052170Sevanl 
24062170Sevanl 		last_error = NFS_OK;
24072170Sevanl 		if (stat(mntpnt, &stbuf) == 0) {
24082170Sevanl 			if (trace > 1) {
24092170Sevanl 				trace_prt(1, "	mount %s dev=%x rdev=%x OK\n",
2410*4590Srm15945 				    mnttabtext, stbuf.st_dev, stbuf.st_rdev);
24112170Sevanl 			}
24122170Sevanl 		} else {
24132170Sevanl 			if (trace > 1) {
24142170Sevanl 				trace_prt(1, "	mount %s OK\n", mnttabtext);
24152170Sevanl 				trace_prt(1, "	stat of %s failed\n", mntpnt);
24162170Sevanl 			}
24172170Sevanl 
24180Sstevel@tonic-gate 		}
24190Sstevel@tonic-gate 	} else {
24202170Sevanl 		alp->action.action = AUTOFS_MOUNT_RQ;
24212170Sevanl 		alp->action.action_list_entry_u.mounta.spec =
2422*4590Srm15945 		    strdup(mnttabtext);
24232170Sevanl 		alp->action.action_list_entry_u.mounta.dir = strdup(mntpnt);
24242170Sevanl 		alp->action.action_list_entry_u.mounta.flags =
2425*4590Srm15945 		    flags | MS_DATA;
24262170Sevanl 		alp->action.action_list_entry_u.mounta.fstype =
2427*4590Srm15945 		    strdup(fstype);
24282170Sevanl 		alp->action.action_list_entry_u.mounta.dataptr = (char *)head;
24292170Sevanl 		alp->action.action_list_entry_u.mounta.datalen =
2430*4590Srm15945 		    sizeof (*head);
24312170Sevanl 		mntopts = malloc(strlen(mopts) + 1);
24322170Sevanl 		strcpy(mntopts, mopts);
24332170Sevanl 		mntopts[strlen(mopts)] = '\0';
24342170Sevanl 		alp->action.action_list_entry_u.mounta.optptr = mntopts;
24352170Sevanl 		alp->action.action_list_entry_u.mounta.optlen =
2436*4590Srm15945 		    strlen(mntopts) + 1;
24372170Sevanl 		last_error = NFS_OK;
24382170Sevanl 		goto ret;
24390Sstevel@tonic-gate 	}
24400Sstevel@tonic-gate 
24410Sstevel@tonic-gate out:
24420Sstevel@tonic-gate 	argp = head;
24430Sstevel@tonic-gate 	while (argp) {
24440Sstevel@tonic-gate 		if (argp->pathconf)
24450Sstevel@tonic-gate 			free(argp->pathconf);
24460Sstevel@tonic-gate 		free_knconf(argp->knconf);
24470Sstevel@tonic-gate 		netbuf_free(argp->addr);
24480Sstevel@tonic-gate 		if (argp->syncaddr)
24490Sstevel@tonic-gate 			netbuf_free(argp->syncaddr);
24500Sstevel@tonic-gate 		if (argp->netname) {
24510Sstevel@tonic-gate 			free(argp->netname);
24520Sstevel@tonic-gate 		}
24532170Sevanl 		if (argp->hostname)
24542170Sevanl 			free(argp->hostname);
24550Sstevel@tonic-gate 		nfs_free_secdata(argp->nfs_ext_u.nfs_extB.secdata);
24560Sstevel@tonic-gate 		free(argp->fh);
24570Sstevel@tonic-gate 		head = argp;
24580Sstevel@tonic-gate 		argp = argp->nfs_ext_u.nfs_extB.next;
24590Sstevel@tonic-gate 		free(head);
24600Sstevel@tonic-gate 	}
24610Sstevel@tonic-gate ret:
24620Sstevel@tonic-gate 	if (nfs_proto)
24630Sstevel@tonic-gate 		free(nfs_proto);
24640Sstevel@tonic-gate 	if (mnttabtext)
24650Sstevel@tonic-gate 		free(mnttabtext);
24660Sstevel@tonic-gate 
24670Sstevel@tonic-gate 	for (mfs = mfs_in; mfs; mfs = mfs->mfs_next) {
24680Sstevel@tonic-gate 
24690Sstevel@tonic-gate 		if (mfs->mfs_flags & MFS_ALLOC_DIR) {
24700Sstevel@tonic-gate 			free(mfs->mfs_dir);
24710Sstevel@tonic-gate 			mfs->mfs_dir = NULL;
24720Sstevel@tonic-gate 			mfs->mfs_flags &= ~MFS_ALLOC_DIR;
24730Sstevel@tonic-gate 		}
24740Sstevel@tonic-gate 
24752170Sevanl 		if (mfs->mfs_args != NULL && alp == NULL) {
24760Sstevel@tonic-gate 			free(mfs->mfs_args);
24770Sstevel@tonic-gate 			mfs->mfs_args = NULL;
24780Sstevel@tonic-gate 		}
24790Sstevel@tonic-gate 
24800Sstevel@tonic-gate 		if (mfs->mfs_nconf != NULL) {
24810Sstevel@tonic-gate 			freenetconfigent(mfs->mfs_nconf);
24820Sstevel@tonic-gate 			mfs->mfs_nconf = NULL;
24830Sstevel@tonic-gate 		}
24840Sstevel@tonic-gate 	}
24850Sstevel@tonic-gate 
24860Sstevel@tonic-gate 	return (last_error);
24870Sstevel@tonic-gate }
24880Sstevel@tonic-gate 
24890Sstevel@tonic-gate /*
24900Sstevel@tonic-gate  * get_pathconf(cl, path, fsname, pcnf, cretries)
24910Sstevel@tonic-gate  * ugliness that requires that ppathcnf and pathcnf stay consistent
24920Sstevel@tonic-gate  * cretries is a copy of retries used to determine when to syslog
24930Sstevel@tonic-gate  * on retry situations.
24940Sstevel@tonic-gate  */
24950Sstevel@tonic-gate static int
24960Sstevel@tonic-gate get_pathconf(CLIENT *cl, char *path, char *fsname, struct pathcnf **pcnf,
24970Sstevel@tonic-gate 	int cretries)
24980Sstevel@tonic-gate {
24990Sstevel@tonic-gate 	struct ppathcnf *p = NULL;
25000Sstevel@tonic-gate 	enum clnt_stat rpc_stat;
25010Sstevel@tonic-gate 	struct timeval timeout;
25020Sstevel@tonic-gate 
25030Sstevel@tonic-gate 	p = (struct ppathcnf *)malloc(sizeof (struct ppathcnf));
25040Sstevel@tonic-gate 	if (p == NULL) {
25050Sstevel@tonic-gate 		syslog(LOG_ERR, "get_pathconf: Out of memory");
25060Sstevel@tonic-gate 		return (RET_ERR);
25070Sstevel@tonic-gate 	}
25080Sstevel@tonic-gate 	memset((caddr_t)p, 0, sizeof (struct ppathcnf));
25090Sstevel@tonic-gate 
25100Sstevel@tonic-gate 	timeout.tv_sec = 10;
25110Sstevel@tonic-gate 	timeout.tv_usec = 0;
25120Sstevel@tonic-gate 	rpc_stat = clnt_call(cl, MOUNTPROC_PATHCONF,
25130Sstevel@tonic-gate 	    xdr_dirpath, (caddr_t)&path, xdr_ppathcnf, (caddr_t)p, timeout);
25140Sstevel@tonic-gate 	if (rpc_stat != RPC_SUCCESS) {
25150Sstevel@tonic-gate 		if (cretries-- <= 0) {
25160Sstevel@tonic-gate 			syslog(LOG_ERR,
25170Sstevel@tonic-gate 			    "get_pathconf: %s: server not responding: %s",
25180Sstevel@tonic-gate 			    fsname, clnt_sperror(cl, ""));
25190Sstevel@tonic-gate 		}
25200Sstevel@tonic-gate 		free(p);
25210Sstevel@tonic-gate 		return (RET_RETRY);
25220Sstevel@tonic-gate 	}
25230Sstevel@tonic-gate 	if (_PC_ISSET(_PC_ERROR, p->pc_mask)) {
25240Sstevel@tonic-gate 		syslog(LOG_ERR, "get_pathconf: no info for %s", fsname);
25250Sstevel@tonic-gate 		free(p);
25260Sstevel@tonic-gate 		return (RET_ERR);
25270Sstevel@tonic-gate 	}
25280Sstevel@tonic-gate 	*pcnf = (struct pathcnf *)p;
25290Sstevel@tonic-gate 	return (RET_OK);
25300Sstevel@tonic-gate }
25310Sstevel@tonic-gate 
25322170Sevanl struct knetconfig *
25330Sstevel@tonic-gate get_knconf(nconf)
25340Sstevel@tonic-gate 	struct netconfig *nconf;
25350Sstevel@tonic-gate {
25360Sstevel@tonic-gate 	struct stat stbuf;
25370Sstevel@tonic-gate 	struct knetconfig *k;
25380Sstevel@tonic-gate 
25390Sstevel@tonic-gate 	if (stat(nconf->nc_device, &stbuf) < 0) {
25400Sstevel@tonic-gate 		syslog(LOG_ERR, "get_knconf: stat %s: %m", nconf->nc_device);
25410Sstevel@tonic-gate 		return (NULL);
25420Sstevel@tonic-gate 	}
25430Sstevel@tonic-gate 	k = (struct knetconfig *)malloc(sizeof (*k));
25440Sstevel@tonic-gate 	if (k == NULL)
25450Sstevel@tonic-gate 		goto nomem;
25460Sstevel@tonic-gate 	k->knc_semantics = nconf->nc_semantics;
25470Sstevel@tonic-gate 	k->knc_protofmly = strdup(nconf->nc_protofmly);
25480Sstevel@tonic-gate 	if (k->knc_protofmly == NULL)
25490Sstevel@tonic-gate 		goto nomem;
25500Sstevel@tonic-gate 	k->knc_proto = strdup(nconf->nc_proto);
25510Sstevel@tonic-gate 	if (k->knc_proto == NULL)
25520Sstevel@tonic-gate 		goto nomem;
25530Sstevel@tonic-gate 	k->knc_rdev = stbuf.st_rdev;
25540Sstevel@tonic-gate 
25550Sstevel@tonic-gate 	return (k);
25560Sstevel@tonic-gate 
25570Sstevel@tonic-gate nomem:
25580Sstevel@tonic-gate 	syslog(LOG_ERR, "get_knconf: no memory");
25590Sstevel@tonic-gate 	free_knconf(k);
25600Sstevel@tonic-gate 	return (NULL);
25610Sstevel@tonic-gate }
25620Sstevel@tonic-gate 
25632170Sevanl void
25640Sstevel@tonic-gate free_knconf(k)
25650Sstevel@tonic-gate 	struct knetconfig *k;
25660Sstevel@tonic-gate {
25670Sstevel@tonic-gate 	if (k == NULL)
25680Sstevel@tonic-gate 		return;
25690Sstevel@tonic-gate 	if (k->knc_protofmly)
25700Sstevel@tonic-gate 		free(k->knc_protofmly);
25710Sstevel@tonic-gate 	if (k->knc_proto)
25720Sstevel@tonic-gate 		free(k->knc_proto);
25730Sstevel@tonic-gate 	free(k);
25740Sstevel@tonic-gate }
25750Sstevel@tonic-gate 
25762170Sevanl void
25770Sstevel@tonic-gate netbuf_free(nb)
25780Sstevel@tonic-gate 	struct netbuf *nb;
25790Sstevel@tonic-gate {
25800Sstevel@tonic-gate 	if (nb == NULL)
25810Sstevel@tonic-gate 		return;
25820Sstevel@tonic-gate 	if (nb->buf)
25830Sstevel@tonic-gate 		free(nb->buf);
25840Sstevel@tonic-gate 	free(nb);
25850Sstevel@tonic-gate }
25860Sstevel@tonic-gate 
25870Sstevel@tonic-gate #define	SMALL_HOSTNAME		20
25880Sstevel@tonic-gate #define	SMALL_PROTONAME		10
25890Sstevel@tonic-gate #define	SMALL_PROTOFMLYNAME		10
25900Sstevel@tonic-gate 
25910Sstevel@tonic-gate struct portmap_cache {
25920Sstevel@tonic-gate 	int cache_prog;
25930Sstevel@tonic-gate 	int cache_vers;
25940Sstevel@tonic-gate 	time_t cache_time;
25950Sstevel@tonic-gate 	char cache_small_hosts[SMALL_HOSTNAME + 1];
25960Sstevel@tonic-gate 	char *cache_hostname;
25970Sstevel@tonic-gate 	char *cache_proto;
25980Sstevel@tonic-gate 	char *cache_protofmly;
25990Sstevel@tonic-gate 	char cache_small_protofmly[SMALL_PROTOFMLYNAME + 1];
26000Sstevel@tonic-gate 	char cache_small_proto[SMALL_PROTONAME + 1];
26010Sstevel@tonic-gate 	struct netbuf cache_srv_addr;
26020Sstevel@tonic-gate 	struct portmap_cache *cache_prev, *cache_next;
26030Sstevel@tonic-gate };
26040Sstevel@tonic-gate 
26050Sstevel@tonic-gate rwlock_t portmap_cache_lock;
26060Sstevel@tonic-gate static int portmap_cache_valid_time = 30;
26070Sstevel@tonic-gate struct portmap_cache *portmap_cache_head, *portmap_cache_tail;
26080Sstevel@tonic-gate 
26090Sstevel@tonic-gate #ifdef MALLOC_DEBUG
26100Sstevel@tonic-gate void
26110Sstevel@tonic-gate portmap_cache_flush()
26120Sstevel@tonic-gate {
26130Sstevel@tonic-gate 	struct  portmap_cache *next = NULL, *cp;
26140Sstevel@tonic-gate 
26150Sstevel@tonic-gate 	(void) rw_wrlock(&portmap_cache_lock);
26160Sstevel@tonic-gate 	for (cp = portmap_cache_head; cp; cp = cp->cache_next) {
26170Sstevel@tonic-gate 		if (cp->cache_hostname != NULL &&
26180Sstevel@tonic-gate 		    cp->cache_hostname !=
26190Sstevel@tonic-gate 		    cp->cache_small_hosts)
26200Sstevel@tonic-gate 			free(cp->cache_hostname);
26210Sstevel@tonic-gate 		if (cp->cache_proto != NULL &&
26220Sstevel@tonic-gate 		    cp->cache_proto !=
26230Sstevel@tonic-gate 		    cp->cache_small_proto)
26240Sstevel@tonic-gate 			free(cp->cache_proto);
26250Sstevel@tonic-gate 		if (cp->cache_srv_addr.buf != NULL)
26260Sstevel@tonic-gate 			free(cp->cache_srv_addr.buf);
26270Sstevel@tonic-gate 		next = cp->cache_next;
26280Sstevel@tonic-gate 		free(cp);
26290Sstevel@tonic-gate 	}
26300Sstevel@tonic-gate 	portmap_cache_head = NULL;
26310Sstevel@tonic-gate 	portmap_cache_tail = NULL;
26320Sstevel@tonic-gate 	(void) rw_unlock(&portmap_cache_lock);
26330Sstevel@tonic-gate }
26340Sstevel@tonic-gate #endif
26350Sstevel@tonic-gate 
26360Sstevel@tonic-gate /*
26370Sstevel@tonic-gate  * Returns 1 if the entry is found in the cache, 0 otherwise.
26380Sstevel@tonic-gate  */
26390Sstevel@tonic-gate static int
26400Sstevel@tonic-gate portmap_cache_lookup(hostname, prog, vers, nconf, addrp)
26410Sstevel@tonic-gate 	char *hostname;
26420Sstevel@tonic-gate 	rpcprog_t prog;
26430Sstevel@tonic-gate 	rpcvers_t vers;
26440Sstevel@tonic-gate 	struct netconfig *nconf;
26450Sstevel@tonic-gate 	struct netbuf *addrp;
26460Sstevel@tonic-gate {
26470Sstevel@tonic-gate 	struct	portmap_cache *cachep, *prev, *next = NULL, *cp;
26480Sstevel@tonic-gate 	int	retval = 0;
26490Sstevel@tonic-gate 
26500Sstevel@tonic-gate 	timenow = time(NULL);
26510Sstevel@tonic-gate 
26520Sstevel@tonic-gate 	(void) rw_rdlock(&portmap_cache_lock);
26530Sstevel@tonic-gate 
26540Sstevel@tonic-gate 	/*
26550Sstevel@tonic-gate 	 * Increment the portmap cache counters for # accesses and lookups
26560Sstevel@tonic-gate 	 * Use a smaller factor (100 vs 1000 for the host cache) since
26570Sstevel@tonic-gate 	 * initial analysis shows this cache is looked up 10% that of the
26580Sstevel@tonic-gate 	 * host cache.
26590Sstevel@tonic-gate 	 */
26600Sstevel@tonic-gate #ifdef CACHE_DEBUG
26610Sstevel@tonic-gate 	portmap_cache_accesses++;
26620Sstevel@tonic-gate 	portmap_cache_lookups++;
26630Sstevel@tonic-gate 	if ((portmap_cache_lookups%100) == 0)
26640Sstevel@tonic-gate 		trace_portmap_cache();
26650Sstevel@tonic-gate #endif /* CACHE_DEBUG */
26660Sstevel@tonic-gate 
26670Sstevel@tonic-gate 	for (cachep = portmap_cache_head; cachep;
26680Sstevel@tonic-gate 		cachep = cachep->cache_next) {
26690Sstevel@tonic-gate 		if (timenow > cachep->cache_time) {
26700Sstevel@tonic-gate 			/*
26710Sstevel@tonic-gate 			 * We stumbled across an entry in the cache which
26720Sstevel@tonic-gate 			 * has timed out. Free up all the entries that
26730Sstevel@tonic-gate 			 * were added before it, which will positionally
26740Sstevel@tonic-gate 			 * be after this entry. And adjust neighboring
26750Sstevel@tonic-gate 			 * pointers.
26760Sstevel@tonic-gate 			 * When we drop the lock and re-acquire it, we
26770Sstevel@tonic-gate 			 * need to start from the beginning.
26780Sstevel@tonic-gate 			 */
26790Sstevel@tonic-gate 			(void) rw_unlock(&portmap_cache_lock);
26800Sstevel@tonic-gate 			(void) rw_wrlock(&portmap_cache_lock);
26810Sstevel@tonic-gate 			for (cp = portmap_cache_head;
26820Sstevel@tonic-gate 				cp && (cp->cache_time >= timenow);
26830Sstevel@tonic-gate 				cp = cp->cache_next)
26840Sstevel@tonic-gate 				;
26850Sstevel@tonic-gate 			if (cp == NULL)
26860Sstevel@tonic-gate 				goto done;
26870Sstevel@tonic-gate 			/*
26880Sstevel@tonic-gate 			 * Adjust the link of the predecessor.
26890Sstevel@tonic-gate 			 * Make the tail point to the new last entry.
26900Sstevel@tonic-gate 			 */
26910Sstevel@tonic-gate 			prev = cp->cache_prev;
26920Sstevel@tonic-gate 			if (prev == NULL) {
26930Sstevel@tonic-gate 				portmap_cache_head = NULL;
26940Sstevel@tonic-gate 				portmap_cache_tail = NULL;
26950Sstevel@tonic-gate 			} else {
26960Sstevel@tonic-gate 				prev->cache_next = NULL;
26970Sstevel@tonic-gate 				portmap_cache_tail = prev;
26980Sstevel@tonic-gate 			}
26990Sstevel@tonic-gate 			for (; cp; cp = next) {
27000Sstevel@tonic-gate 				if (cp->cache_hostname != NULL &&
27010Sstevel@tonic-gate 				    cp->cache_hostname !=
27020Sstevel@tonic-gate 				    cp->cache_small_hosts)
27030Sstevel@tonic-gate 					free(cp->cache_hostname);
27040Sstevel@tonic-gate 				if (cp->cache_proto != NULL &&
27050Sstevel@tonic-gate 				    cp->cache_proto !=
27060Sstevel@tonic-gate 				    cp->cache_small_proto)
27070Sstevel@tonic-gate 					free(cp->cache_proto);
27080Sstevel@tonic-gate 				if (cp->cache_srv_addr.buf != NULL)
27090Sstevel@tonic-gate 					free(cp->cache_srv_addr.buf);
27100Sstevel@tonic-gate 				next = cp->cache_next;
27110Sstevel@tonic-gate 				free(cp);
27120Sstevel@tonic-gate 			}
27130Sstevel@tonic-gate 			goto done;
27140Sstevel@tonic-gate 		}
27150Sstevel@tonic-gate 		if (cachep->cache_hostname == NULL ||
27160Sstevel@tonic-gate 		    prog != cachep->cache_prog || vers != cachep->cache_vers ||
27170Sstevel@tonic-gate 		    strcmp(nconf->nc_proto, cachep->cache_proto) != 0 ||
27180Sstevel@tonic-gate 		    strcmp(nconf->nc_protofmly, cachep->cache_protofmly) != 0 ||
27190Sstevel@tonic-gate 		    strcmp(hostname, cachep->cache_hostname) != 0)
27200Sstevel@tonic-gate 			continue;
27210Sstevel@tonic-gate 		/*
27220Sstevel@tonic-gate 		 * Cache Hit.
27230Sstevel@tonic-gate 		 */
27240Sstevel@tonic-gate #ifdef CACHE_DEBUG
27250Sstevel@tonic-gate 		portmap_cache_hits++;	/* up portmap cache hit counter */
27260Sstevel@tonic-gate #endif /* CACHE_DEBUG */
27270Sstevel@tonic-gate 		addrp->len = cachep->cache_srv_addr.len;
27280Sstevel@tonic-gate 		memcpy(addrp->buf, cachep->cache_srv_addr.buf, addrp->len);
27290Sstevel@tonic-gate 		retval = 1;
27300Sstevel@tonic-gate 		break;
27310Sstevel@tonic-gate 	}
27320Sstevel@tonic-gate done:
27330Sstevel@tonic-gate 	(void) rw_unlock(&portmap_cache_lock);
27340Sstevel@tonic-gate 	return (retval);
27350Sstevel@tonic-gate }
27360Sstevel@tonic-gate 
27370Sstevel@tonic-gate static void
27380Sstevel@tonic-gate portmap_cache_enter(hostname, prog, vers, nconf, addrp)
27390Sstevel@tonic-gate 	char *hostname;
27400Sstevel@tonic-gate 	rpcprog_t prog;
27410Sstevel@tonic-gate 	rpcvers_t vers;
27420Sstevel@tonic-gate 	struct netconfig *nconf;
27430Sstevel@tonic-gate 	struct netbuf *addrp;
27440Sstevel@tonic-gate {
27450Sstevel@tonic-gate 	struct portmap_cache *cachep;
27460Sstevel@tonic-gate 	int protofmlylen;
27470Sstevel@tonic-gate 	int protolen, hostnamelen;
27480Sstevel@tonic-gate 
27490Sstevel@tonic-gate 	timenow = time(NULL);
27500Sstevel@tonic-gate 
27510Sstevel@tonic-gate 	cachep = malloc(sizeof (struct portmap_cache));
27520Sstevel@tonic-gate 	if (cachep == NULL)
27530Sstevel@tonic-gate 		return;
27540Sstevel@tonic-gate 	memset((char *)cachep, 0, sizeof (*cachep));
27550Sstevel@tonic-gate 
27560Sstevel@tonic-gate 	hostnamelen = strlen(hostname);
27570Sstevel@tonic-gate 	if (hostnamelen <= SMALL_HOSTNAME)
27580Sstevel@tonic-gate 		cachep->cache_hostname = cachep->cache_small_hosts;
27590Sstevel@tonic-gate 	else {
27600Sstevel@tonic-gate 		cachep->cache_hostname = malloc(hostnamelen + 1);
27610Sstevel@tonic-gate 		if (cachep->cache_hostname == NULL)
27620Sstevel@tonic-gate 			goto nomem;
27630Sstevel@tonic-gate 	}
27640Sstevel@tonic-gate 	strcpy(cachep->cache_hostname, hostname);
27650Sstevel@tonic-gate 	protolen = strlen(nconf->nc_proto);
27660Sstevel@tonic-gate 	if (protolen <= SMALL_PROTONAME)
27670Sstevel@tonic-gate 		cachep->cache_proto = cachep->cache_small_proto;
27680Sstevel@tonic-gate 	else {
27690Sstevel@tonic-gate 		cachep->cache_proto = malloc(protolen + 1);
27700Sstevel@tonic-gate 		if (cachep->cache_proto == NULL)
27710Sstevel@tonic-gate 			goto nomem;
27720Sstevel@tonic-gate 	}
27730Sstevel@tonic-gate 	protofmlylen = strlen(nconf->nc_protofmly);
27740Sstevel@tonic-gate 	if (protofmlylen <= SMALL_PROTOFMLYNAME)
27750Sstevel@tonic-gate 		cachep->cache_protofmly = cachep->cache_small_protofmly;
27760Sstevel@tonic-gate 	else {
27770Sstevel@tonic-gate 		cachep->cache_protofmly = malloc(protofmlylen + 1);
27780Sstevel@tonic-gate 		if (cachep->cache_protofmly == NULL)
27790Sstevel@tonic-gate 			goto nomem;
27800Sstevel@tonic-gate 	}
27810Sstevel@tonic-gate 
27820Sstevel@tonic-gate 	strcpy(cachep->cache_proto, nconf->nc_proto);
27830Sstevel@tonic-gate 	cachep->cache_prog = prog;
27840Sstevel@tonic-gate 	cachep->cache_vers = vers;
27850Sstevel@tonic-gate 	cachep->cache_time = timenow + portmap_cache_valid_time;
27860Sstevel@tonic-gate 	cachep->cache_srv_addr.len = addrp->len;
27870Sstevel@tonic-gate 	cachep->cache_srv_addr.buf = malloc(addrp->len);
27880Sstevel@tonic-gate 	if (cachep->cache_srv_addr.buf == NULL)
27890Sstevel@tonic-gate 		goto nomem;
27900Sstevel@tonic-gate 	memcpy(cachep->cache_srv_addr.buf, addrp->buf, addrp->maxlen);
27910Sstevel@tonic-gate 	cachep->cache_prev = NULL;
27920Sstevel@tonic-gate 	(void) rw_wrlock(&portmap_cache_lock);
27930Sstevel@tonic-gate 	/*
27940Sstevel@tonic-gate 	 * There's a window in which we could have multiple threads making
27950Sstevel@tonic-gate 	 * the same cache entry. This can be avoided by walking the cache
27960Sstevel@tonic-gate 	 * once again here to check and see if there are duplicate entries
27970Sstevel@tonic-gate 	 * (after grabbing the write lock). This isn't fatal and I'm not
27980Sstevel@tonic-gate 	 * going to bother with this.
27990Sstevel@tonic-gate 	 */
28000Sstevel@tonic-gate #ifdef CACHE_DEBUG
28010Sstevel@tonic-gate 	portmap_cache_accesses++;	/* up portmap cache access counter */
28020Sstevel@tonic-gate #endif /* CACHE_DEBUG */
28030Sstevel@tonic-gate 	cachep->cache_next = portmap_cache_head;
28040Sstevel@tonic-gate 	if (portmap_cache_head != NULL)
28050Sstevel@tonic-gate 		portmap_cache_head->cache_prev = cachep;
28060Sstevel@tonic-gate 	portmap_cache_head = cachep;
28070Sstevel@tonic-gate 	(void) rw_unlock(&portmap_cache_lock);
28080Sstevel@tonic-gate 	return;
28090Sstevel@tonic-gate 
28100Sstevel@tonic-gate nomem:
28110Sstevel@tonic-gate 	syslog(LOG_ERR, "portmap_cache_enter: Memory allocation failed");
28120Sstevel@tonic-gate 	if (cachep->cache_srv_addr.buf)
28130Sstevel@tonic-gate 		free(cachep->cache_srv_addr.buf);
28140Sstevel@tonic-gate 	if (cachep->cache_proto && protolen > SMALL_PROTONAME)
28150Sstevel@tonic-gate 		free(cachep->cache_proto);
28160Sstevel@tonic-gate 	if (cachep->cache_hostname && hostnamelen > SMALL_HOSTNAME)
28170Sstevel@tonic-gate 		free(cachep->cache_hostname);
28180Sstevel@tonic-gate 	if (cachep->cache_protofmly && protofmlylen > SMALL_PROTOFMLYNAME)
28190Sstevel@tonic-gate 		free(cachep->cache_protofmly);
28200Sstevel@tonic-gate 	if (cachep)
28210Sstevel@tonic-gate 		free(cachep);
28220Sstevel@tonic-gate 	cachep = NULL;
28230Sstevel@tonic-gate }
28240Sstevel@tonic-gate 
28250Sstevel@tonic-gate static int
28260Sstevel@tonic-gate get_cached_srv_addr(char *hostname, rpcprog_t prog, rpcvers_t vers,
28270Sstevel@tonic-gate 	struct netconfig *nconf, struct netbuf *addrp)
28280Sstevel@tonic-gate {
28290Sstevel@tonic-gate 	if (portmap_cache_lookup(hostname, prog, vers, nconf, addrp))
28300Sstevel@tonic-gate 		return (1);
28310Sstevel@tonic-gate 	if (rpcb_getaddr(prog, vers, nconf, addrp, hostname) == 0)
28320Sstevel@tonic-gate 		return (0);
28330Sstevel@tonic-gate 	portmap_cache_enter(hostname, prog, vers, nconf, addrp);
28340Sstevel@tonic-gate 	return (1);
28350Sstevel@tonic-gate }
28360Sstevel@tonic-gate 
28370Sstevel@tonic-gate /*
28380Sstevel@tonic-gate  * Get the network address on "hostname" for program "prog"
28390Sstevel@tonic-gate  * with version "vers" by using the nconf configuration data
28400Sstevel@tonic-gate  * passed in.
28410Sstevel@tonic-gate  *
28420Sstevel@tonic-gate  * If the address of a netconfig pointer is null then
28430Sstevel@tonic-gate  * information is not sufficient and no netbuf will be returned.
28440Sstevel@tonic-gate  *
28450Sstevel@tonic-gate  * tinfo argument is for matching the get_the_addr() defined in
28460Sstevel@tonic-gate  * ../nfs/mount/mount.c
28470Sstevel@tonic-gate  */
28482170Sevanl void *
28490Sstevel@tonic-gate get_the_stuff(
28500Sstevel@tonic-gate 	enum type_of_stuff type_of_stuff,
28510Sstevel@tonic-gate 	char *hostname,
28520Sstevel@tonic-gate 	rpcprog_t prog,
28530Sstevel@tonic-gate 	rpcprog_t vers,
28540Sstevel@tonic-gate 	mfs_snego_t *mfssnego,
28550Sstevel@tonic-gate 	struct netconfig *nconf,
28560Sstevel@tonic-gate 	ushort_t port,
28570Sstevel@tonic-gate 	struct t_info *tinfo,
28580Sstevel@tonic-gate 	caddr_t *fhp,
28590Sstevel@tonic-gate 	bool_t direct_to_server,
28600Sstevel@tonic-gate 	char *fspath,
28610Sstevel@tonic-gate 	enum clnt_stat *cstat)
28620Sstevel@tonic-gate 
28630Sstevel@tonic-gate {
28640Sstevel@tonic-gate 	struct netbuf *nb = NULL;
28650Sstevel@tonic-gate 	struct t_bind *tbind = NULL;
28660Sstevel@tonic-gate 	int fd = -1;
28670Sstevel@tonic-gate 	enum clnt_stat cs = RPC_TIMEDOUT;
28680Sstevel@tonic-gate 	CLIENT *cl = NULL;
28690Sstevel@tonic-gate 	struct timeval tv;
28700Sstevel@tonic-gate 	AUTH *ah = NULL;
28710Sstevel@tonic-gate 	AUTH *new_ah = NULL;
28720Sstevel@tonic-gate 	struct snego_t snego;
28730Sstevel@tonic-gate 
28740Sstevel@tonic-gate 	if (nconf == NULL) {
28750Sstevel@tonic-gate 		goto done;
28760Sstevel@tonic-gate 	}
28770Sstevel@tonic-gate 
28780Sstevel@tonic-gate 	if (prog == NFS_PROGRAM && vers == NFS_V4)
28790Sstevel@tonic-gate 		if (strncasecmp(nconf->nc_proto, NC_UDP, strlen(NC_UDP)) == 0)
28800Sstevel@tonic-gate 			goto done;
28810Sstevel@tonic-gate 
28820Sstevel@tonic-gate 	if ((fd = t_open(nconf->nc_device, O_RDWR, tinfo)) < 0) {
28830Sstevel@tonic-gate 		goto done;
28840Sstevel@tonic-gate 	}
28850Sstevel@tonic-gate 
28860Sstevel@tonic-gate 	/* LINTED pointer alignment */
28870Sstevel@tonic-gate 	if ((tbind = (struct t_bind *)t_alloc(fd, T_BIND, T_ADDR))
28880Sstevel@tonic-gate 		    == NULL) {
28890Sstevel@tonic-gate 			goto done;
28900Sstevel@tonic-gate 	}
28910Sstevel@tonic-gate 
28920Sstevel@tonic-gate 	if (direct_to_server == TRUE) {
28930Sstevel@tonic-gate 		struct nd_hostserv hs;
28940Sstevel@tonic-gate 		struct nd_addrlist *retaddrs;
28950Sstevel@tonic-gate 		hs.h_host = hostname;
28960Sstevel@tonic-gate 
28970Sstevel@tonic-gate 		if (trace > 1)
28980Sstevel@tonic-gate 			trace_prt(1, "	get_the_stuff: %s call "
28990Sstevel@tonic-gate 				"direct to server %s\n",
29000Sstevel@tonic-gate 				type_of_stuff == SERVER_FH ? "pub fh" :
29010Sstevel@tonic-gate 				type_of_stuff == SERVER_ADDR ? "get address" :
29020Sstevel@tonic-gate 				type_of_stuff == SERVER_PING ? "ping" :
29030Sstevel@tonic-gate 				"unknown", hostname);
29040Sstevel@tonic-gate 		if (port == 0)
29050Sstevel@tonic-gate 			hs.h_serv = "nfs";
29060Sstevel@tonic-gate 		else
29070Sstevel@tonic-gate 			hs.h_serv = NULL;
29080Sstevel@tonic-gate 
29090Sstevel@tonic-gate 		if (netdir_getbyname(nconf, &hs, &retaddrs) != ND_OK) {
29100Sstevel@tonic-gate 			goto done;
29110Sstevel@tonic-gate 		}
29120Sstevel@tonic-gate 		memcpy(tbind->addr.buf, retaddrs->n_addrs->buf,
29130Sstevel@tonic-gate 			retaddrs->n_addrs->len);
29140Sstevel@tonic-gate 		tbind->addr.len = retaddrs->n_addrs->len;
29150Sstevel@tonic-gate 		netdir_free((void *)retaddrs, ND_ADDRLIST);
29160Sstevel@tonic-gate 		if (port) {
29170Sstevel@tonic-gate 			/* LINTED pointer alignment */
29180Sstevel@tonic-gate 
29190Sstevel@tonic-gate 			if (strcmp(nconf->nc_protofmly, NC_INET) == NULL)
29200Sstevel@tonic-gate 				((struct sockaddr_in *)
29210Sstevel@tonic-gate 				tbind->addr.buf)->sin_port =
29220Sstevel@tonic-gate 					htons((ushort_t)port);
29230Sstevel@tonic-gate 			else if (strcmp(nconf->nc_protofmly, NC_INET6) == NULL)
29240Sstevel@tonic-gate 				((struct sockaddr_in6 *)
29250Sstevel@tonic-gate 				tbind->addr.buf)->sin6_port =
29260Sstevel@tonic-gate 					htons((ushort_t)port);
29270Sstevel@tonic-gate 		}
29280Sstevel@tonic-gate 
29290Sstevel@tonic-gate 		if (type_of_stuff == SERVER_FH) {
29300Sstevel@tonic-gate 			if (netdir_options(nconf, ND_SET_RESERVEDPORT, fd,
29310Sstevel@tonic-gate 				NULL) == -1)
29320Sstevel@tonic-gate 				if (trace > 1)
29330Sstevel@tonic-gate 					trace_prt(1, "\tget_the_stuff: "
29340Sstevel@tonic-gate 						"ND_SET_RESERVEDPORT(%s) "
29350Sstevel@tonic-gate 						"failed\n", hostname);
29360Sstevel@tonic-gate 		}
29370Sstevel@tonic-gate 
29380Sstevel@tonic-gate 		cl = clnt_tli_create(fd, nconf, &tbind->addr, prog,
29390Sstevel@tonic-gate 			vers, 0, 0);
29400Sstevel@tonic-gate 
29410Sstevel@tonic-gate 		if (trace > 1)
29420Sstevel@tonic-gate 			trace_prt(1, "	get_the_stuff: clnt_tli_create(%s) "
29430Sstevel@tonic-gate 				"returned %p\n", hostname, cl);
29440Sstevel@tonic-gate 		if (cl == NULL)
29450Sstevel@tonic-gate 			goto done;
29460Sstevel@tonic-gate #ifdef MALLOC_DEBUG
29470Sstevel@tonic-gate 		add_alloc("CLNT_HANDLE", cl, 0, __FILE__, __LINE__);
29480Sstevel@tonic-gate 		add_alloc("AUTH_HANDLE", cl->cl_auth, 0,
29490Sstevel@tonic-gate 			__FILE__, __LINE__);
29500Sstevel@tonic-gate #endif
29510Sstevel@tonic-gate 
29520Sstevel@tonic-gate 		switch (type_of_stuff) {
29530Sstevel@tonic-gate 		case SERVER_FH:
29540Sstevel@tonic-gate 		    {
29550Sstevel@tonic-gate 		    enum snego_stat sec;
29560Sstevel@tonic-gate 
29570Sstevel@tonic-gate 		    ah = authsys_create_default();
29580Sstevel@tonic-gate 		    if (ah != NULL) {
29590Sstevel@tonic-gate #ifdef MALLOC_DEBUG
29600Sstevel@tonic-gate 			drop_alloc("AUTH_HANDLE", cl->cl_auth,
29610Sstevel@tonic-gate 				__FILE__, __LINE__);
29620Sstevel@tonic-gate #endif
29630Sstevel@tonic-gate 			AUTH_DESTROY(cl->cl_auth);
29640Sstevel@tonic-gate 			cl->cl_auth = ah;
29650Sstevel@tonic-gate #ifdef MALLOC_DEBUG
29660Sstevel@tonic-gate 			add_alloc("AUTH_HANDLE", cl->cl_auth, 0,
29670Sstevel@tonic-gate 				__FILE__, __LINE__);
29680Sstevel@tonic-gate #endif
29690Sstevel@tonic-gate 		    }
29700Sstevel@tonic-gate 
29710Sstevel@tonic-gate 		    if (!mfssnego->snego_done && vers != NFS_V4) {
29720Sstevel@tonic-gate 			/*
29730Sstevel@tonic-gate 			 * negotiate sec flavor.
29740Sstevel@tonic-gate 			 */
29750Sstevel@tonic-gate 			snego.cnt = 0;
29760Sstevel@tonic-gate 			if ((sec = nfs_sec_nego(vers, cl, fspath, &snego)) ==
29770Sstevel@tonic-gate 				SNEGO_SUCCESS) {
29780Sstevel@tonic-gate 			    int jj;
29790Sstevel@tonic-gate 
29800Sstevel@tonic-gate 			/*
29810Sstevel@tonic-gate 			 * check if server supports the one
29820Sstevel@tonic-gate 			 * specified in the sec= option.
29830Sstevel@tonic-gate 			 */
29840Sstevel@tonic-gate 			    if (mfssnego->sec_opt) {
29850Sstevel@tonic-gate 				for (jj = 0; jj < snego.cnt; jj++) {
29860Sstevel@tonic-gate 				    if (snego.array[jj] ==
29870Sstevel@tonic-gate 					mfssnego->nfs_sec.sc_nfsnum) {
29880Sstevel@tonic-gate 					mfssnego->snego_done = TRUE;
29890Sstevel@tonic-gate 					break;
29900Sstevel@tonic-gate 				    }
29910Sstevel@tonic-gate 				}
29920Sstevel@tonic-gate 			    }
29930Sstevel@tonic-gate 
29940Sstevel@tonic-gate 			/*
29950Sstevel@tonic-gate 			 * find a common sec flavor
29960Sstevel@tonic-gate 			 */
29970Sstevel@tonic-gate 			    if (!mfssnego->snego_done) {
29980Sstevel@tonic-gate 				for (jj = 0; jj < snego.cnt; jj++) {
29990Sstevel@tonic-gate 				    if (!nfs_getseconfig_bynumber(
30000Sstevel@tonic-gate 					snego.array[jj], &mfssnego->nfs_sec)) {
30010Sstevel@tonic-gate 					mfssnego->snego_done = TRUE;
30020Sstevel@tonic-gate 					break;
30030Sstevel@tonic-gate 				    }
30040Sstevel@tonic-gate 				}
30050Sstevel@tonic-gate 			    }
30060Sstevel@tonic-gate 			    if (!mfssnego->snego_done)
30070Sstevel@tonic-gate 				return (NULL);
30080Sstevel@tonic-gate 
30090Sstevel@tonic-gate 			/*
30100Sstevel@tonic-gate 			 * Now that the flavor has been
30110Sstevel@tonic-gate 			 * negotiated, get the fh.
30120Sstevel@tonic-gate 			 *
30130Sstevel@tonic-gate 			 * First, create an auth handle using the negotiated
30140Sstevel@tonic-gate 			 * sec flavor in the next lookup to
30150Sstevel@tonic-gate 			 * fetch the filehandle.
30160Sstevel@tonic-gate 			 */
30170Sstevel@tonic-gate 			    new_ah = nfs_create_ah(cl, hostname,
30180Sstevel@tonic-gate 					&mfssnego->nfs_sec);
30190Sstevel@tonic-gate 			    if (new_ah == NULL)
30200Sstevel@tonic-gate 				goto done;
30210Sstevel@tonic-gate #ifdef MALLOC_DEBUG
30220Sstevel@tonic-gate 			    drop_alloc("AUTH_HANDLE", cl->cl_auth,
30230Sstevel@tonic-gate 				__FILE__, __LINE__);
30240Sstevel@tonic-gate #endif
30250Sstevel@tonic-gate 			    AUTH_DESTROY(cl->cl_auth);
30260Sstevel@tonic-gate 			    cl->cl_auth = new_ah;
30270Sstevel@tonic-gate #ifdef MALLOC_DEBUG
30280Sstevel@tonic-gate 			    add_alloc("AUTH_HANDLE", cl->cl_auth, 0,
30290Sstevel@tonic-gate 				__FILE__, __LINE__);
30300Sstevel@tonic-gate #endif
30310Sstevel@tonic-gate 			} else if (sec == SNEGO_ARRAY_TOO_SMALL ||
30320Sstevel@tonic-gate 			    sec == SNEGO_FAILURE) {
30330Sstevel@tonic-gate 			    goto done;
30340Sstevel@tonic-gate 			}
30350Sstevel@tonic-gate 			/*
30360Sstevel@tonic-gate 			 * Note that if sec == SNEGO_DEF_VALID
30370Sstevel@tonic-gate 			 * the default sec flavor is acceptable.
30380Sstevel@tonic-gate 			 * Use it to get the filehandle.
30390Sstevel@tonic-gate 			 */
30400Sstevel@tonic-gate 		    }
30410Sstevel@tonic-gate 		    }
30420Sstevel@tonic-gate 
30430Sstevel@tonic-gate 		    switch (vers) {
30440Sstevel@tonic-gate 		    case NFS_VERSION:
30450Sstevel@tonic-gate 			    {
30460Sstevel@tonic-gate 			    wnl_diropargs arg;
30470Sstevel@tonic-gate 			    wnl_diropres *res;
30480Sstevel@tonic-gate 
30490Sstevel@tonic-gate 			    memset((char *)&arg.dir, 0, sizeof (wnl_fh));
30500Sstevel@tonic-gate 			    arg.name = fspath;
30510Sstevel@tonic-gate 			    res = wnlproc_lookup_2(&arg, cl);
30520Sstevel@tonic-gate 
30530Sstevel@tonic-gate 			    if (res == NULL || res->status != NFS_OK)
30540Sstevel@tonic-gate 				    goto done;
30550Sstevel@tonic-gate 			    *fhp = malloc(sizeof (wnl_fh));
30560Sstevel@tonic-gate 
30570Sstevel@tonic-gate 			    if (*fhp == NULL) {
30580Sstevel@tonic-gate 				    syslog(LOG_ERR, "no memory\n");
30590Sstevel@tonic-gate 				    goto done;
30600Sstevel@tonic-gate 			    }
30610Sstevel@tonic-gate 
30620Sstevel@tonic-gate 			    memcpy((char *)*fhp,
30630Sstevel@tonic-gate 			    (char *)&res->wnl_diropres_u.wnl_diropres.file,
30640Sstevel@tonic-gate 				sizeof (wnl_fh));
30650Sstevel@tonic-gate 			    cs = RPC_SUCCESS;
30660Sstevel@tonic-gate 			    }
30670Sstevel@tonic-gate 			    break;
30680Sstevel@tonic-gate 		    case NFS_V3:
30690Sstevel@tonic-gate 			    {
30700Sstevel@tonic-gate 			    WNL_LOOKUP3args arg;
30710Sstevel@tonic-gate 			    WNL_LOOKUP3res *res;
30720Sstevel@tonic-gate 			    nfs_fh3 *fh3p;
30730Sstevel@tonic-gate 
30740Sstevel@tonic-gate 			    memset((char *)&arg.what.dir, 0, sizeof (wnl_fh3));
30750Sstevel@tonic-gate 			    arg.what.name = fspath;
30760Sstevel@tonic-gate 			    res = wnlproc3_lookup_3(&arg, cl);
30770Sstevel@tonic-gate 
30780Sstevel@tonic-gate 			    if (res == NULL || res->status != NFS3_OK)
30790Sstevel@tonic-gate 				    goto done;
30800Sstevel@tonic-gate 
30810Sstevel@tonic-gate 			    fh3p = (nfs_fh3 *)malloc(sizeof (*fh3p));
30820Sstevel@tonic-gate 
30830Sstevel@tonic-gate 			    if (fh3p == NULL) {
30840Sstevel@tonic-gate 				    syslog(LOG_ERR, "no memory\n");
30850Sstevel@tonic-gate 				    CLNT_FREERES(cl, xdr_WNL_LOOKUP3res,
30860Sstevel@tonic-gate 					(char *)res);
30870Sstevel@tonic-gate 				    goto done;
30880Sstevel@tonic-gate 			    }
30890Sstevel@tonic-gate 
30900Sstevel@tonic-gate 			    fh3p->fh3_length = res->
30910Sstevel@tonic-gate 				WNL_LOOKUP3res_u.res_ok.object.data.data_len;
30920Sstevel@tonic-gate 			    memcpy(fh3p->fh3_u.data, res->
30930Sstevel@tonic-gate 				WNL_LOOKUP3res_u.res_ok.object.data.data_val,
30940Sstevel@tonic-gate 				fh3p->fh3_length);
30950Sstevel@tonic-gate 
30960Sstevel@tonic-gate 			    *fhp = (caddr_t)fh3p;
30970Sstevel@tonic-gate 
30980Sstevel@tonic-gate 			    CLNT_FREERES(cl, xdr_WNL_LOOKUP3res, (char *)res);
30990Sstevel@tonic-gate 			    cs = RPC_SUCCESS;
31000Sstevel@tonic-gate 			    }
31010Sstevel@tonic-gate 			    break;
31020Sstevel@tonic-gate 		    case NFS_V4:
3103*4590Srm15945 			    tv.tv_sec = 10;
3104*4590Srm15945 			    tv.tv_usec = 0;
3105*4590Srm15945 			    cs = clnt_call(cl, NULLPROC, xdr_void, 0,
3106*4590Srm15945 				xdr_void, 0, tv);
3107*4590Srm15945 			    if (cs != RPC_SUCCESS)
3108*4590Srm15945 				    goto done;
31090Sstevel@tonic-gate 			    *fhp = strdup(fspath);
31100Sstevel@tonic-gate 			    break;
31110Sstevel@tonic-gate 		    }
31120Sstevel@tonic-gate 		    break;
31130Sstevel@tonic-gate 		case SERVER_ADDR:
31140Sstevel@tonic-gate 		case SERVER_PING:
31150Sstevel@tonic-gate 			tv.tv_sec = 10;
31160Sstevel@tonic-gate 			tv.tv_usec = 0;
31170Sstevel@tonic-gate 			cs = clnt_call(cl, NULLPROC, xdr_void, 0,
31180Sstevel@tonic-gate 				xdr_void, 0, tv);
31190Sstevel@tonic-gate 			if (trace > 1)
31200Sstevel@tonic-gate 				trace_prt(1,
31210Sstevel@tonic-gate 					"get_the_stuff: clnt_call(%s) "
31220Sstevel@tonic-gate 					"returned %s\n",
31230Sstevel@tonic-gate 				hostname,
31240Sstevel@tonic-gate 					cs == RPC_SUCCESS ? "success" :
31250Sstevel@tonic-gate 					"failure");
31260Sstevel@tonic-gate 
31270Sstevel@tonic-gate 			if (cs != RPC_SUCCESS)
31280Sstevel@tonic-gate 				goto done;
31290Sstevel@tonic-gate 			break;
31300Sstevel@tonic-gate 		}
31310Sstevel@tonic-gate 
31320Sstevel@tonic-gate 	} else if (type_of_stuff != SERVER_FH) {
31330Sstevel@tonic-gate 
31340Sstevel@tonic-gate 		if (type_of_stuff == SERVER_ADDR) {
31350Sstevel@tonic-gate 			if (get_cached_srv_addr(hostname, prog, vers, nconf,
31360Sstevel@tonic-gate 			    &tbind->addr) == 0)
31370Sstevel@tonic-gate 				goto done;
31380Sstevel@tonic-gate 		}
31390Sstevel@tonic-gate 
31400Sstevel@tonic-gate 		if (port) {
31410Sstevel@tonic-gate 			/* LINTED pointer alignment */
31420Sstevel@tonic-gate 			if (strcmp(nconf->nc_protofmly, NC_INET) == NULL)
31430Sstevel@tonic-gate 				((struct sockaddr_in *)
31440Sstevel@tonic-gate 				tbind->addr.buf)->sin_port =
31450Sstevel@tonic-gate 					htons((ushort_t)port);
31460Sstevel@tonic-gate 			else if (strcmp(nconf->nc_protofmly, NC_INET6) == NULL)
31470Sstevel@tonic-gate 				((struct sockaddr_in6 *)
31480Sstevel@tonic-gate 				tbind->addr.buf)->sin6_port =
31490Sstevel@tonic-gate 					htons((ushort_t)port);
31500Sstevel@tonic-gate 			cl = clnt_tli_create(fd, nconf, &tbind->addr,
31510Sstevel@tonic-gate 				prog, vers, 0, 0);
31520Sstevel@tonic-gate 			if (cl == NULL)
31530Sstevel@tonic-gate 				goto done;
31540Sstevel@tonic-gate #ifdef MALLOC_DEBUG
31550Sstevel@tonic-gate 			add_alloc("CLNT_HANDLE", cl, 0, __FILE__, __LINE__);
31560Sstevel@tonic-gate 			add_alloc("AUTH_HANDLE", cl->cl_auth, 0,
31570Sstevel@tonic-gate 				__FILE__, __LINE__);
31580Sstevel@tonic-gate #endif
31590Sstevel@tonic-gate 			tv.tv_sec = 10;
31600Sstevel@tonic-gate 			tv.tv_usec = 0;
31610Sstevel@tonic-gate 			cs = clnt_call(cl, NULLPROC, xdr_void, 0, xdr_void,
31620Sstevel@tonic-gate 				0, tv);
31630Sstevel@tonic-gate 			if (cs != RPC_SUCCESS)
31640Sstevel@tonic-gate 				goto done;
31650Sstevel@tonic-gate 		}
31660Sstevel@tonic-gate 
31670Sstevel@tonic-gate 	} else {
31680Sstevel@tonic-gate 		/* can't happen */
31690Sstevel@tonic-gate 		goto done;
31700Sstevel@tonic-gate 	}
31710Sstevel@tonic-gate 
31720Sstevel@tonic-gate 	if (type_of_stuff != SERVER_PING) {
31730Sstevel@tonic-gate 
31740Sstevel@tonic-gate 		cs = RPC_SYSTEMERROR;
31750Sstevel@tonic-gate 
31760Sstevel@tonic-gate 		/*
31770Sstevel@tonic-gate 		 * Make a copy of the netbuf to return
31780Sstevel@tonic-gate 		 */
31790Sstevel@tonic-gate 		nb = (struct netbuf *)malloc(sizeof (struct netbuf));
31800Sstevel@tonic-gate 		if (nb == NULL) {
31810Sstevel@tonic-gate 			syslog(LOG_ERR, "no memory\n");
31820Sstevel@tonic-gate 			goto done;
31830Sstevel@tonic-gate 		}
31840Sstevel@tonic-gate 		*nb = tbind->addr;
31850Sstevel@tonic-gate 		nb->buf = (char *)malloc(nb->maxlen);
31860Sstevel@tonic-gate 		if (nb->buf == NULL) {
31870Sstevel@tonic-gate 			syslog(LOG_ERR, "no memory\n");
31880Sstevel@tonic-gate 			free(nb);
31890Sstevel@tonic-gate 			nb = NULL;
31900Sstevel@tonic-gate 			goto done;
31910Sstevel@tonic-gate 		}
31920Sstevel@tonic-gate 		(void) memcpy(nb->buf, tbind->addr.buf, tbind->addr.len);
31930Sstevel@tonic-gate 
31940Sstevel@tonic-gate 		cs = RPC_SUCCESS;
31950Sstevel@tonic-gate 	}
31960Sstevel@tonic-gate 
31970Sstevel@tonic-gate done:
31980Sstevel@tonic-gate 	if (cl != NULL) {
31990Sstevel@tonic-gate 		if (ah != NULL) {
32000Sstevel@tonic-gate #ifdef MALLOC_DEBUG
32010Sstevel@tonic-gate 			drop_alloc("AUTH_HANDLE", cl->cl_auth,
32020Sstevel@tonic-gate 				__FILE__, __LINE__);
32030Sstevel@tonic-gate #endif
32040Sstevel@tonic-gate 			AUTH_DESTROY(cl->cl_auth);
32050Sstevel@tonic-gate 			cl->cl_auth = NULL;
32060Sstevel@tonic-gate 		}
32070Sstevel@tonic-gate #ifdef MALLOC_DEBUG
32080Sstevel@tonic-gate 		drop_alloc("CLNT_HANDLE", cl, __FILE__, __LINE__);
32090Sstevel@tonic-gate #endif
32100Sstevel@tonic-gate 		clnt_destroy(cl);
32110Sstevel@tonic-gate 	}
32120Sstevel@tonic-gate 
32130Sstevel@tonic-gate 	if (tbind) {
32140Sstevel@tonic-gate 		t_free((char *)tbind, T_BIND);
32150Sstevel@tonic-gate 		tbind = NULL;
32160Sstevel@tonic-gate 	}
32170Sstevel@tonic-gate 
32180Sstevel@tonic-gate 	if (fd >= 0)
32190Sstevel@tonic-gate 		(void) t_close(fd);
32200Sstevel@tonic-gate 
32210Sstevel@tonic-gate 	if (cstat != NULL)
32220Sstevel@tonic-gate 		*cstat = cs;
32230Sstevel@tonic-gate 
32240Sstevel@tonic-gate 	return (nb);
32250Sstevel@tonic-gate }
32260Sstevel@tonic-gate 
32270Sstevel@tonic-gate /*
32280Sstevel@tonic-gate  * Get a network address on "hostname" for program "prog"
32290Sstevel@tonic-gate  * with version "vers".  If the port number is specified (non zero)
32300Sstevel@tonic-gate  * then try for a TCP/UDP transport and set the port number of the
32310Sstevel@tonic-gate  * resulting IP address.
32320Sstevel@tonic-gate  *
32330Sstevel@tonic-gate  * If the address of a netconfig pointer was passed and
32340Sstevel@tonic-gate  * if it's not null, use it as the netconfig otherwise
32350Sstevel@tonic-gate  * assign the address of the netconfig that was used to
32360Sstevel@tonic-gate  * establish contact with the service.
32370Sstevel@tonic-gate  *
32380Sstevel@tonic-gate  * tinfo argument is for matching the get_addr() defined in
32390Sstevel@tonic-gate  * ../nfs/mount/mount.c
32400Sstevel@tonic-gate  */
32410Sstevel@tonic-gate 
32420Sstevel@tonic-gate static struct netbuf *
32430Sstevel@tonic-gate get_addr(char *hostname, rpcprog_t prog, rpcvers_t vers,
32440Sstevel@tonic-gate 	struct netconfig **nconfp, char *proto, ushort_t port,
32450Sstevel@tonic-gate 	struct t_info *tinfo)
32460Sstevel@tonic-gate 
32470Sstevel@tonic-gate {
32480Sstevel@tonic-gate 	enum clnt_stat cstat;
32490Sstevel@tonic-gate 
32500Sstevel@tonic-gate 	return (get_server_stuff(SERVER_ADDR, hostname, prog, vers, NULL,
32510Sstevel@tonic-gate 		nconfp, proto, port, tinfo, NULL, FALSE, NULL, &cstat));
32520Sstevel@tonic-gate }
32530Sstevel@tonic-gate 
32540Sstevel@tonic-gate static struct netbuf *
32550Sstevel@tonic-gate get_pubfh(char *hostname, rpcvers_t vers, mfs_snego_t *mfssnego,
32560Sstevel@tonic-gate 	struct netconfig **nconfp, char *proto, ushort_t port,
32570Sstevel@tonic-gate 	struct t_info *tinfo, caddr_t *fhp, bool_t get_pubfh, char *fspath)
32580Sstevel@tonic-gate {
32590Sstevel@tonic-gate 	enum clnt_stat cstat;
32600Sstevel@tonic-gate 
32610Sstevel@tonic-gate 	return (get_server_stuff(SERVER_FH, hostname, NFS_PROGRAM, vers,
3262*4590Srm15945 	    mfssnego, nconfp, proto, port, tinfo, fhp, get_pubfh, fspath,
3263*4590Srm15945 	    &cstat));
32640Sstevel@tonic-gate }
32650Sstevel@tonic-gate 
32660Sstevel@tonic-gate static enum clnt_stat
32670Sstevel@tonic-gate get_ping(char *hostname, rpcprog_t prog, rpcvers_t vers,
32680Sstevel@tonic-gate 	struct netconfig **nconfp, ushort_t port, bool_t direct_to_server)
32690Sstevel@tonic-gate {
32700Sstevel@tonic-gate 	enum clnt_stat cstat;
32710Sstevel@tonic-gate 
32720Sstevel@tonic-gate 	(void) get_server_stuff(SERVER_PING, hostname, prog, vers, NULL, nconfp,
3273*4590Srm15945 	    NULL, port, NULL, NULL, direct_to_server, NULL, &cstat);
32740Sstevel@tonic-gate 
32750Sstevel@tonic-gate 	return (cstat);
32760Sstevel@tonic-gate }
32770Sstevel@tonic-gate 
32782170Sevanl void *
32790Sstevel@tonic-gate get_server_stuff(
32800Sstevel@tonic-gate 	enum type_of_stuff type_of_stuff,
32810Sstevel@tonic-gate 	char *hostname,
32820Sstevel@tonic-gate 	rpcprog_t prog,
32830Sstevel@tonic-gate 	rpcvers_t vers,
32840Sstevel@tonic-gate 	mfs_snego_t *mfssnego,
32850Sstevel@tonic-gate 	struct netconfig **nconfp,
32860Sstevel@tonic-gate 	char *proto,
32870Sstevel@tonic-gate 	ushort_t port,			/* may be zero */
32880Sstevel@tonic-gate 	struct t_info *tinfo,
32890Sstevel@tonic-gate 	caddr_t *fhp,
32900Sstevel@tonic-gate 	bool_t direct_to_server,
32910Sstevel@tonic-gate 	char *fspath,
32920Sstevel@tonic-gate 	enum clnt_stat *cstatp)
32930Sstevel@tonic-gate {
32940Sstevel@tonic-gate 	struct netbuf *nb = NULL;
32950Sstevel@tonic-gate 	struct netconfig *nconf = NULL;
32960Sstevel@tonic-gate 	NCONF_HANDLE *nc = NULL;
32970Sstevel@tonic-gate 	int nthtry = FIRST_TRY;
32980Sstevel@tonic-gate 
32990Sstevel@tonic-gate 	if (nconfp && *nconfp)
33000Sstevel@tonic-gate 		return (get_the_stuff(type_of_stuff, hostname, prog, vers,
3301*4590Srm15945 		    mfssnego, *nconfp, port, tinfo, fhp, direct_to_server,
3302*4590Srm15945 		    fspath, cstatp));
33030Sstevel@tonic-gate 
33040Sstevel@tonic-gate 
33050Sstevel@tonic-gate 	/*
33060Sstevel@tonic-gate 	 * No nconf passed in.
33070Sstevel@tonic-gate 	 *
33080Sstevel@tonic-gate 	 * Try to get a nconf from /etc/netconfig.
33090Sstevel@tonic-gate 	 * First choice is COTS, second is CLTS unless proto
33100Sstevel@tonic-gate 	 * is specified.  When we retry, we reset the
33110Sstevel@tonic-gate 	 * netconfig list, so that we search the whole list
33120Sstevel@tonic-gate 	 * for the next choice.
33130Sstevel@tonic-gate 	 */
33140Sstevel@tonic-gate 	if ((nc = setnetpath()) == NULL)
33150Sstevel@tonic-gate 		goto done;
33160Sstevel@tonic-gate 
33170Sstevel@tonic-gate 	/*
33180Sstevel@tonic-gate 	 * If proto is specified, then only search for the match,
33190Sstevel@tonic-gate 	 * otherwise try COTS first, if failed, then try CLTS.
33200Sstevel@tonic-gate 	 */
33210Sstevel@tonic-gate 	if (proto) {
33220Sstevel@tonic-gate 
33230Sstevel@tonic-gate 		while (nconf = getnetpath(nc)) {
33240Sstevel@tonic-gate 			if (strcmp(nconf->nc_proto, proto))
33250Sstevel@tonic-gate 				continue;
33260Sstevel@tonic-gate 			/*
33270Sstevel@tonic-gate 			 * If the port number is specified then TCP/UDP
33280Sstevel@tonic-gate 			 * is needed. Otherwise any cots/clts will do.
33290Sstevel@tonic-gate 			 */
33300Sstevel@tonic-gate 			if (port)  {
33310Sstevel@tonic-gate 				if ((strcmp(nconf->nc_protofmly, NC_INET) &&
33320Sstevel@tonic-gate 				    strcmp(nconf->nc_protofmly, NC_INET6)) ||
33330Sstevel@tonic-gate 				    (strcmp(nconf->nc_proto, NC_TCP) &&
33340Sstevel@tonic-gate 				    strcmp(nconf->nc_proto, NC_UDP)))
33350Sstevel@tonic-gate 					continue;
33360Sstevel@tonic-gate 			}
33370Sstevel@tonic-gate 
33380Sstevel@tonic-gate 			nb = get_the_stuff(type_of_stuff, hostname, prog, vers,
3339*4590Srm15945 			    mfssnego, nconf, port, tinfo, fhp,
3340*4590Srm15945 			    direct_to_server, fspath, cstatp);
33410Sstevel@tonic-gate 
33420Sstevel@tonic-gate 			if (*cstatp == RPC_SUCCESS)
33430Sstevel@tonic-gate 				break;
33440Sstevel@tonic-gate 
33450Sstevel@tonic-gate 			assert(nb == NULL);
33460Sstevel@tonic-gate 
33470Sstevel@tonic-gate 		} /* end of while */
33480Sstevel@tonic-gate 
33490Sstevel@tonic-gate 		if (nconf == NULL)
33500Sstevel@tonic-gate 			goto done;
33510Sstevel@tonic-gate 
33520Sstevel@tonic-gate 	} else {
33530Sstevel@tonic-gate retry:
33540Sstevel@tonic-gate 		while (nconf = getnetpath(nc)) {
33550Sstevel@tonic-gate 			if (nconf->nc_flag & NC_VISIBLE) {
3356*4590Srm15945 				if (nthtry == FIRST_TRY) {
3357*4590Srm15945 					if ((nconf->nc_semantics ==
3358*4590Srm15945 					    NC_TPI_COTS_ORD) ||
3359*4590Srm15945 					    (nconf->nc_semantics ==
3360*4590Srm15945 					    NC_TPI_COTS)) {
3361*4590Srm15945 						if (port == 0)
3362*4590Srm15945 							break;
3363*4590Srm15945 						if ((strcmp(nconf->nc_protofmly,
3364*4590Srm15945 						    NC_INET) == 0 ||
3365*4590Srm15945 						    strcmp(nconf->nc_protofmly,
3366*4590Srm15945 						    NC_INET6) == 0) &&
3367*4590Srm15945 						    (strcmp(nconf->nc_proto,
3368*4590Srm15945 						    NC_TCP) == 0))
3369*4590Srm15945 							break;
3370*4590Srm15945 					}
33710Sstevel@tonic-gate 				}
3372*4590Srm15945 				if (nthtry == SECOND_TRY) {
3373*4590Srm15945 					if (nconf->nc_semantics ==
3374*4590Srm15945 					    NC_TPI_CLTS) {
3375*4590Srm15945 						if (port == 0)
3376*4590Srm15945 							break;
3377*4590Srm15945 						if ((strcmp(nconf->nc_protofmly,
3378*4590Srm15945 						    NC_INET) == 0 ||
3379*4590Srm15945 						    strcmp(nconf->nc_protofmly,
3380*4590Srm15945 						    NC_INET6) == 0) &&
3381*4590Srm15945 						    (strcmp(nconf->nc_proto,
3382*4590Srm15945 						    NC_UDP) == 0))
3383*4590Srm15945 							break;
3384*4590Srm15945 					}
33850Sstevel@tonic-gate 				}
33860Sstevel@tonic-gate 			}
3387*4590Srm15945 		} /* while */
3388*4590Srm15945 		if (nconf == NULL) {
33890Sstevel@tonic-gate 			if (++nthtry <= MNT_PREF_LISTLEN) {
33900Sstevel@tonic-gate 				endnetpath(nc);
33910Sstevel@tonic-gate 				if ((nc = setnetpath()) == NULL)
33920Sstevel@tonic-gate 					goto done;
33930Sstevel@tonic-gate 				goto retry;
33940Sstevel@tonic-gate 			} else
33950Sstevel@tonic-gate 				goto done;
3396*4590Srm15945 		} else {
33970Sstevel@tonic-gate 			nb = get_the_stuff(type_of_stuff, hostname, prog, vers,
33980Sstevel@tonic-gate 			    mfssnego, nconf, port, tinfo, fhp, direct_to_server,
33990Sstevel@tonic-gate 			    fspath, cstatp);
34000Sstevel@tonic-gate 			if (*cstatp != RPC_SUCCESS)
34010Sstevel@tonic-gate 				/*
34020Sstevel@tonic-gate 				 * Continue the same search path in the
34030Sstevel@tonic-gate 				 * netconfig db until no more matched nconf
34040Sstevel@tonic-gate 				 * (nconf == NULL).
34050Sstevel@tonic-gate 				 */
34060Sstevel@tonic-gate 				goto retry;
3407*4590Srm15945 		}
34080Sstevel@tonic-gate 	} /* if !proto */
34090Sstevel@tonic-gate 
34100Sstevel@tonic-gate 	/*
34110Sstevel@tonic-gate 	 * Got nconf and nb.  Now dup the netconfig structure (nconf)
34120Sstevel@tonic-gate 	 * and return it thru nconfp.
34130Sstevel@tonic-gate 	 */
34140Sstevel@tonic-gate 	*nconfp = getnetconfigent(nconf->nc_netid);
34150Sstevel@tonic-gate 	if (*nconfp == NULL) {
34160Sstevel@tonic-gate 		syslog(LOG_ERR, "no memory\n");
34170Sstevel@tonic-gate 		free(nb);
34180Sstevel@tonic-gate 		nb = NULL;
34190Sstevel@tonic-gate 	}
34200Sstevel@tonic-gate done:
34210Sstevel@tonic-gate 	if (nc)
34220Sstevel@tonic-gate 		endnetpath(nc);
34230Sstevel@tonic-gate 	return (nb);
34240Sstevel@tonic-gate }
34250Sstevel@tonic-gate 
34260Sstevel@tonic-gate 
34270Sstevel@tonic-gate /*
34280Sstevel@tonic-gate  * Sends a null call to the remote host's (NFS program, versp). versp
34290Sstevel@tonic-gate  * may be "NULL" in which case the default maximum version is used.
34300Sstevel@tonic-gate  * Upon return, versp contains the maximum version supported iff versp!= NULL.
34310Sstevel@tonic-gate  */
34320Sstevel@tonic-gate enum clnt_stat
34330Sstevel@tonic-gate pingnfs(
34340Sstevel@tonic-gate 	char *hostpart,
34350Sstevel@tonic-gate 	int attempts,
34360Sstevel@tonic-gate 	rpcvers_t *versp,
34370Sstevel@tonic-gate 	rpcvers_t versmin,
34383767Sjs195444 	ushort_t port,			/* may be zero */
34390Sstevel@tonic-gate 	bool_t usepub,
34400Sstevel@tonic-gate 	char *path,
34410Sstevel@tonic-gate 	char *proto)
34420Sstevel@tonic-gate {
34430Sstevel@tonic-gate 	CLIENT *cl = NULL;
34440Sstevel@tonic-gate 	struct timeval rpc_to_new = {15, 0};
34450Sstevel@tonic-gate 	static struct timeval rpc_rtrans_new = {-1, -1};
34460Sstevel@tonic-gate 	enum clnt_stat clnt_stat;
34470Sstevel@tonic-gate 	int i, j;
34480Sstevel@tonic-gate 	rpcvers_t versmax;	/* maximum version to try against server */
34490Sstevel@tonic-gate 	rpcvers_t outvers;	/* version supported by host on last call */
34500Sstevel@tonic-gate 	rpcvers_t vers_to_try;	/* to try different versions against host */
34513767Sjs195444 	char *hostname;
34520Sstevel@tonic-gate 	struct netconfig *nconf;
34530Sstevel@tonic-gate 
34543767Sjs195444 	hostname = strdup(hostpart);
34553767Sjs195444 	if (hostname == NULL) {
34563767Sjs195444 		return (RPC_SYSTEMERROR);
34573767Sjs195444 	}
34583767Sjs195444 	unbracket(&hostname);
34593767Sjs195444 
34600Sstevel@tonic-gate 	if (path != NULL && strcmp(hostname, "nfs") == 0 &&
34610Sstevel@tonic-gate 	    strncmp(path, "//", 2) == 0) {
34620Sstevel@tonic-gate 		char *sport;
34630Sstevel@tonic-gate 
34640Sstevel@tonic-gate 		hostname = strdup(path+2);
34650Sstevel@tonic-gate 
34660Sstevel@tonic-gate 		if (hostname == NULL)
34670Sstevel@tonic-gate 			return (RPC_SYSTEMERROR);
34680Sstevel@tonic-gate 
34690Sstevel@tonic-gate 		path = strchr(hostname, '/');
34700Sstevel@tonic-gate 
34710Sstevel@tonic-gate 		/*
34720Sstevel@tonic-gate 		 * This cannot happen. If it does, give up
34730Sstevel@tonic-gate 		 * on the ping as this is obviously a corrupt
34740Sstevel@tonic-gate 		 * entry.
34750Sstevel@tonic-gate 		 */
34760Sstevel@tonic-gate 		if (path == NULL) {
34770Sstevel@tonic-gate 			free(hostname);
34780Sstevel@tonic-gate 			return (RPC_SUCCESS);
34790Sstevel@tonic-gate 		}
34800Sstevel@tonic-gate 
34810Sstevel@tonic-gate 		/*
34820Sstevel@tonic-gate 		 * Probable end point of host string.
34830Sstevel@tonic-gate 		 */
34840Sstevel@tonic-gate 		*path = '\0';
34850Sstevel@tonic-gate 
34860Sstevel@tonic-gate 		sport = strchr(hostname, ':');
34870Sstevel@tonic-gate 
34880Sstevel@tonic-gate 		if (sport != NULL && sport < path) {
34890Sstevel@tonic-gate 
34900Sstevel@tonic-gate 			/*
34910Sstevel@tonic-gate 			 * Actual end point of host string.
34920Sstevel@tonic-gate 			 */
34930Sstevel@tonic-gate 			*sport = '\0';
34940Sstevel@tonic-gate 			port = htons((ushort_t)atoi(sport+1));
34950Sstevel@tonic-gate 		}
34960Sstevel@tonic-gate 
34970Sstevel@tonic-gate 		usepub = TRUE;
34980Sstevel@tonic-gate 	}
34990Sstevel@tonic-gate 
35000Sstevel@tonic-gate 	/* Pick up the default versions and then set them appropriately */
35010Sstevel@tonic-gate 	if (versp) {
35020Sstevel@tonic-gate 		versmax = *versp;
35030Sstevel@tonic-gate 		/* use versmin passed in */
35040Sstevel@tonic-gate 	} else {
35050Sstevel@tonic-gate 		read_default_nfs();
35060Sstevel@tonic-gate 		set_versrange(0, &versmax, &versmin);
35070Sstevel@tonic-gate 	}
35080Sstevel@tonic-gate 
35090Sstevel@tonic-gate 	if (proto &&
35100Sstevel@tonic-gate 	    strncasecmp(proto, NC_UDP, strlen(NC_UDP)) == 0 &&
35110Sstevel@tonic-gate 	    versmax == NFS_V4) {
35120Sstevel@tonic-gate 		if (versmin == NFS_V4) {
35130Sstevel@tonic-gate 			if (versp) {
35140Sstevel@tonic-gate 				*versp = versmax - 1;
35150Sstevel@tonic-gate 				return (RPC_SUCCESS);
35160Sstevel@tonic-gate 			}
35170Sstevel@tonic-gate 			return (RPC_PROGUNAVAIL);
35180Sstevel@tonic-gate 		} else {
35190Sstevel@tonic-gate 			versmax--;
35200Sstevel@tonic-gate 		}
35210Sstevel@tonic-gate 	}
35220Sstevel@tonic-gate 
35230Sstevel@tonic-gate 	if (versp)
35240Sstevel@tonic-gate 		*versp = versmax;
35250Sstevel@tonic-gate 
35260Sstevel@tonic-gate 	switch (cache_check(hostname, versp, proto)) {
35270Sstevel@tonic-gate 	case GOODHOST:
35280Sstevel@tonic-gate 		if (hostname != hostpart)
35290Sstevel@tonic-gate 			free(hostname);
35300Sstevel@tonic-gate 		return (RPC_SUCCESS);
35310Sstevel@tonic-gate 	case DEADHOST:
35320Sstevel@tonic-gate 		if (hostname != hostpart)
35330Sstevel@tonic-gate 			free(hostname);
35340Sstevel@tonic-gate 		return (RPC_TIMEDOUT);
35350Sstevel@tonic-gate 	case NOHOST:
35360Sstevel@tonic-gate 	default:
35370Sstevel@tonic-gate 		break;
35380Sstevel@tonic-gate 	}
35390Sstevel@tonic-gate 
35400Sstevel@tonic-gate 	/*
35410Sstevel@tonic-gate 	 * XXX The retransmission time rpcbrmttime is a global defined
35420Sstevel@tonic-gate 	 * in the rpc library (rpcb_clnt.c). We use (and like) the default
35430Sstevel@tonic-gate 	 * value of 15 sec in the rpc library. The code below is to protect
35440Sstevel@tonic-gate 	 * us in case it changes. This need not be done under a lock since
35450Sstevel@tonic-gate 	 * any # of threads entering this function will get the same
35460Sstevel@tonic-gate 	 * retransmission value.
35470Sstevel@tonic-gate 	 */
35480Sstevel@tonic-gate 	if (rpc_rtrans_new.tv_sec == -1 && rpc_rtrans_new.tv_usec == -1) {
35490Sstevel@tonic-gate 		__rpc_control(CLCR_GET_RPCB_RMTTIME, (char *)&rpc_rtrans_new);
35500Sstevel@tonic-gate 		if (rpc_rtrans_new.tv_sec != 15 && rpc_rtrans_new.tv_sec != 0)
35510Sstevel@tonic-gate 			if (trace > 1)
35520Sstevel@tonic-gate 				trace_prt(1, "RPC library rttimer changed\n");
35530Sstevel@tonic-gate 	}
35540Sstevel@tonic-gate 
35550Sstevel@tonic-gate 	/*
35560Sstevel@tonic-gate 	 * XXX Manipulate the total timeout to get the number of
35570Sstevel@tonic-gate 	 * desired retransmissions. This code is heavily dependant on
35580Sstevel@tonic-gate 	 * the RPC backoff mechanism in clnt_dg_call (clnt_dg.c).
35590Sstevel@tonic-gate 	 */
35600Sstevel@tonic-gate 	for (i = 0, j = rpc_rtrans_new.tv_sec; i < attempts-1; i++) {
35610Sstevel@tonic-gate 		if (j < RPC_MAX_BACKOFF)
35620Sstevel@tonic-gate 			j *= 2;
35630Sstevel@tonic-gate 		else
35640Sstevel@tonic-gate 			j = RPC_MAX_BACKOFF;
35650Sstevel@tonic-gate 		rpc_to_new.tv_sec += j;
35660Sstevel@tonic-gate 	}
35670Sstevel@tonic-gate 
35680Sstevel@tonic-gate 	vers_to_try = versmax;
35690Sstevel@tonic-gate 
35700Sstevel@tonic-gate 	/*
35710Sstevel@tonic-gate 	 * check the host's version within the timeout
35720Sstevel@tonic-gate 	 */
35730Sstevel@tonic-gate 	if (trace > 1)
35740Sstevel@tonic-gate 		trace_prt(1, "	ping: %s timeout=%ld request vers=%d min=%d\n",
3575*4590Srm15945 		    hostname, rpc_to_new.tv_sec, versmax, versmin);
35760Sstevel@tonic-gate 
35770Sstevel@tonic-gate 	if (usepub == FALSE) {
3578*4590Srm15945 		do {
3579*4590Srm15945 			/*
3580*4590Srm15945 			 * If NFSv4, then we do the same thing as is used
3581*4590Srm15945 			 * for public filehandles so that we avoid rpcbind
3582*4590Srm15945 			 */
3583*4590Srm15945 			if (vers_to_try == NFS_V4) {
3584*4590Srm15945 				if (trace > 4) {
35850Sstevel@tonic-gate 				trace_prt(1, "  pingnfs: Trying ping via "
3586*4590Srm15945 				    "\"circuit_v\"\n");
35870Sstevel@tonic-gate 				}
35880Sstevel@tonic-gate 
3589*4590Srm15945 				cl = clnt_create_service_timed(hostname, "nfs",
3590*4590Srm15945 				    NFS_PROGRAM, vers_to_try,
3591*4590Srm15945 				    port, "circuit_v", &rpc_to_new);
3592*4590Srm15945 				if (cl != NULL) {
3593*4590Srm15945 					outvers = vers_to_try;
3594*4590Srm15945 					break;
3595*4590Srm15945 				}
35960Sstevel@tonic-gate 				if (trace > 4) {
3597*4590Srm15945 					trace_prt(1,
3598*4590Srm15945 					    "  pingnfs: Can't ping via "
3599*4590Srm15945 					    "\"circuit_v\" %s: RPC error=%d\n",
3600*4590Srm15945 					    hostname, rpc_createerr.cf_stat);
36010Sstevel@tonic-gate 				}
3602*4590Srm15945 
3603*4590Srm15945 			} else {
3604*4590Srm15945 				cl = clnt_create_vers_timed(hostname,
3605*4590Srm15945 				    NFS_PROGRAM, &outvers, versmin, vers_to_try,
3606*4590Srm15945 				    "datagram_v", &rpc_to_new);
3607*4590Srm15945 				if (cl != NULL)
36080Sstevel@tonic-gate 					break;
36090Sstevel@tonic-gate 				if (trace > 4) {
3610*4590Srm15945 					trace_prt(1,
3611*4590Srm15945 					    "  pingnfs: Can't ping via "
3612*4590Srm15945 					    "\"datagram_v\"%s: RPC error=%d\n",
3613*4590Srm15945 					    hostname, rpc_createerr.cf_stat);
3614*4590Srm15945 				}
3615*4590Srm15945 				if (rpc_createerr.cf_stat == RPC_UNKNOWNHOST ||
3616*4590Srm15945 				    rpc_createerr.cf_stat == RPC_TIMEDOUT)
3617*4590Srm15945 					break;
3618*4590Srm15945 				if (rpc_createerr.cf_stat ==
3619*4590Srm15945 				    RPC_PROGNOTREGISTERED) {
3620*4590Srm15945 					if (trace > 4) {
3621*4590Srm15945 						trace_prt(1,
3622*4590Srm15945 						    "  pingnfs: Trying ping "
3623*4590Srm15945 						    "via \"circuit_v\"\n");
3624*4590Srm15945 					}
3625*4590Srm15945 					cl = clnt_create_vers_timed(hostname,
3626*4590Srm15945 					    NFS_PROGRAM, &outvers,
3627*4590Srm15945 					    versmin, vers_to_try,
3628*4590Srm15945 					    "circuit_v", &rpc_to_new);
3629*4590Srm15945 					if (cl != NULL)
3630*4590Srm15945 						break;
3631*4590Srm15945 					if (trace > 4) {
3632*4590Srm15945 						trace_prt(1,
3633*4590Srm15945 						    "  pingnfs: Can't ping "
3634*4590Srm15945 						    "via \"circuit_v\" %s: "
3635*4590Srm15945 						    "RPC error=%d\n",
3636*4590Srm15945 						    hostname,
3637*4590Srm15945 						    rpc_createerr.cf_stat);
3638*4590Srm15945 					}
36390Sstevel@tonic-gate 				}
36400Sstevel@tonic-gate 			}
36410Sstevel@tonic-gate 
36420Sstevel@tonic-gate 		/*
36430Sstevel@tonic-gate 		 * backoff and return lower version to retry the ping.
36440Sstevel@tonic-gate 		 * XXX we should be more careful and handle
36450Sstevel@tonic-gate 		 * RPC_PROGVERSMISMATCH here, because that error is handled
36460Sstevel@tonic-gate 		 * in clnt_create_vers(). It's not done to stay in sync
36470Sstevel@tonic-gate 		 * with the nfs mount command.
36480Sstevel@tonic-gate 		 */
3649*4590Srm15945 			vers_to_try--;
3650*4590Srm15945 			if (vers_to_try < versmin)
3651*4590Srm15945 				break;
3652*4590Srm15945 			if (versp != NULL) {	/* recheck the cache */
3653*4590Srm15945 				*versp = vers_to_try;
3654*4590Srm15945 				if (trace > 4) {
3655*4590Srm15945 					trace_prt(1,
3656*4590Srm15945 					    "  pingnfs: check cache: vers=%d\n",
3657*4590Srm15945 					    *versp);
3658*4590Srm15945 				}
3659*4590Srm15945 				switch (cache_check(hostname, versp, proto)) {
3660*4590Srm15945 				case GOODHOST:
3661*4590Srm15945 					if (hostname != hostpart)
3662*4590Srm15945 						free(hostname);
3663*4590Srm15945 					return (RPC_SUCCESS);
3664*4590Srm15945 				case DEADHOST:
3665*4590Srm15945 					if (hostname != hostpart)
3666*4590Srm15945 						free(hostname);
3667*4590Srm15945 					return (RPC_TIMEDOUT);
3668*4590Srm15945 				case NOHOST:
3669*4590Srm15945 				default:
3670*4590Srm15945 					break;
3671*4590Srm15945 				}
3672*4590Srm15945 			}
3673*4590Srm15945 			if (trace > 4) {
3674*4590Srm15945 				trace_prt(1, "  pingnfs: Try version=%d\n",
3675*4590Srm15945 				    vers_to_try);
3676*4590Srm15945 			}
3677*4590Srm15945 		} while (cl == NULL);
3678*4590Srm15945 
3679*4590Srm15945 
3680*4590Srm15945 		if (cl == NULL) {
3681*4590Srm15945 			if (verbose)
3682*4590Srm15945 				syslog(LOG_ERR, "pingnfs: %s%s",
36830Sstevel@tonic-gate 				    hostname, clnt_spcreateerror(""));
3684*4590Srm15945 			clnt_stat = rpc_createerr.cf_stat;
3685*4590Srm15945 		} else {
3686*4590Srm15945 			clnt_destroy(cl);
3687*4590Srm15945 			clnt_stat = RPC_SUCCESS;
3688*4590Srm15945 		}
36890Sstevel@tonic-gate 
36900Sstevel@tonic-gate 	} else {
36910Sstevel@tonic-gate 		for (vers_to_try = versmax; vers_to_try >= versmin;
36920Sstevel@tonic-gate 		    vers_to_try--) {
36930Sstevel@tonic-gate 
36940Sstevel@tonic-gate 			nconf = NULL;
36950Sstevel@tonic-gate 
36960Sstevel@tonic-gate 			if (trace > 4) {
36970Sstevel@tonic-gate 				trace_prt(1, "  pingnfs: Try version=%d "
3698*4590Srm15945 				    "using get_ping()\n", vers_to_try);
36990Sstevel@tonic-gate 			}
37000Sstevel@tonic-gate 
37010Sstevel@tonic-gate 			clnt_stat = get_ping(hostname, NFS_PROGRAM,
3702*4590Srm15945 			    vers_to_try, &nconf, port, TRUE);
37030Sstevel@tonic-gate 
37040Sstevel@tonic-gate 			if (nconf != NULL)
37050Sstevel@tonic-gate 				freenetconfigent(nconf);
37060Sstevel@tonic-gate 
37070Sstevel@tonic-gate 			if (clnt_stat == RPC_SUCCESS) {
37080Sstevel@tonic-gate 				outvers = vers_to_try;
37090Sstevel@tonic-gate 				break;
37100Sstevel@tonic-gate 			}
37110Sstevel@tonic-gate 		}
37120Sstevel@tonic-gate 	}
37130Sstevel@tonic-gate 
37140Sstevel@tonic-gate 	if (trace > 1)
37150Sstevel@tonic-gate 		clnt_stat == RPC_SUCCESS ?
3716*4590Srm15945 		    trace_prt(1, "	pingnfs OK: nfs version=%d\n", outvers):
3717*4590Srm15945 		    trace_prt(1, "	pingnfs FAIL: can't get nfs version\n");
37180Sstevel@tonic-gate 
37190Sstevel@tonic-gate 	if (clnt_stat == RPC_SUCCESS) {
37200Sstevel@tonic-gate 		cache_enter(hostname, versmax, outvers, proto, GOODHOST);
37210Sstevel@tonic-gate 		if (versp != NULL)
37220Sstevel@tonic-gate 			*versp = outvers;
37230Sstevel@tonic-gate 	} else
37240Sstevel@tonic-gate 		cache_enter(hostname, versmax, versmax, proto, DEADHOST);
37250Sstevel@tonic-gate 
37260Sstevel@tonic-gate 	if (hostpart != hostname)
37270Sstevel@tonic-gate 		free(hostname);
37280Sstevel@tonic-gate 
37290Sstevel@tonic-gate 	return (clnt_stat);
37300Sstevel@tonic-gate }
37310Sstevel@tonic-gate 
37320Sstevel@tonic-gate #define	MNTTYPE_LOFS	"lofs"
37330Sstevel@tonic-gate 
37340Sstevel@tonic-gate int
37350Sstevel@tonic-gate loopbackmount(fsname, dir, mntopts, overlay)
37360Sstevel@tonic-gate 	char *fsname;		/* Directory being mounted */
37370Sstevel@tonic-gate 	char *dir;		/* Directory being mounted on */
37380Sstevel@tonic-gate 	char *mntopts;
37390Sstevel@tonic-gate 	int overlay;
37400Sstevel@tonic-gate {
37410Sstevel@tonic-gate 	struct mnttab mnt;
37420Sstevel@tonic-gate 	int flags = 0;
37430Sstevel@tonic-gate 	char fstype[] = MNTTYPE_LOFS;
37440Sstevel@tonic-gate 	int dirlen;
37450Sstevel@tonic-gate 	struct stat st;
37460Sstevel@tonic-gate 	char optbuf[MAX_MNTOPT_STR];
37470Sstevel@tonic-gate 
37480Sstevel@tonic-gate 	dirlen = strlen(dir);
37490Sstevel@tonic-gate 	if (dir[dirlen-1] == ' ')
37500Sstevel@tonic-gate 		dirlen--;
37510Sstevel@tonic-gate 
37520Sstevel@tonic-gate 	if (dirlen == strlen(fsname) &&
37530Sstevel@tonic-gate 		strncmp(fsname, dir, dirlen) == 0) {
37540Sstevel@tonic-gate 		syslog(LOG_ERR,
37550Sstevel@tonic-gate 			"Mount of %s on %s would result in deadlock, aborted\n",
37560Sstevel@tonic-gate 			fsname, dir);
37570Sstevel@tonic-gate 		return (RET_ERR);
37580Sstevel@tonic-gate 	}
37590Sstevel@tonic-gate 	mnt.mnt_mntopts = mntopts;
37600Sstevel@tonic-gate 	if (hasmntopt(&mnt, MNTOPT_RO) != NULL)
37610Sstevel@tonic-gate 		flags |= MS_RDONLY;
37620Sstevel@tonic-gate 
37630Sstevel@tonic-gate 	(void) strlcpy(optbuf, mntopts, sizeof (optbuf));
37640Sstevel@tonic-gate 
37650Sstevel@tonic-gate 	if (overlay)
37660Sstevel@tonic-gate 		flags |= MS_OVERLAY;
37670Sstevel@tonic-gate 
37680Sstevel@tonic-gate 	if (trace > 1)
37690Sstevel@tonic-gate 		trace_prt(1,
37700Sstevel@tonic-gate 			"  loopbackmount: fsname=%s, dir=%s, flags=%d\n",
37710Sstevel@tonic-gate 			fsname, dir, flags);
37720Sstevel@tonic-gate 
37731676Sjpk 	if (is_system_labeled()) {
37741676Sjpk 		if (create_homedir((const char *)fsname,
37751676Sjpk 		    (const char *)dir) == 0) {
37761676Sjpk 			return (NFSERR_NOENT);
37771676Sjpk 		}
37781676Sjpk 	}
37791676Sjpk 
37800Sstevel@tonic-gate 	if (mount(fsname, dir, flags | MS_DATA | MS_OPTIONSTR, fstype,
37810Sstevel@tonic-gate 	    NULL, 0, optbuf, sizeof (optbuf)) < 0) {
37820Sstevel@tonic-gate 		syslog(LOG_ERR, "Mount of %s on %s: %m", fsname, dir);
37830Sstevel@tonic-gate 		return (RET_ERR);
37840Sstevel@tonic-gate 	}
37850Sstevel@tonic-gate 
37860Sstevel@tonic-gate 	if (stat(dir, &st) == 0) {
37870Sstevel@tonic-gate 		if (trace > 1) {
37880Sstevel@tonic-gate 			trace_prt(1,
37890Sstevel@tonic-gate 			    "  loopbackmount of %s on %s dev=%x rdev=%x OK\n",
37900Sstevel@tonic-gate 			    fsname, dir, st.st_dev, st.st_rdev);
37910Sstevel@tonic-gate 		}
37920Sstevel@tonic-gate 	} else {
37930Sstevel@tonic-gate 		if (trace > 1) {
37940Sstevel@tonic-gate 			trace_prt(1,
37950Sstevel@tonic-gate 			    "  loopbackmount of %s on %s OK\n", fsname, dir);
37960Sstevel@tonic-gate 			trace_prt(1, "	stat of %s failed\n", dir);
37970Sstevel@tonic-gate 		}
37980Sstevel@tonic-gate 	}
37990Sstevel@tonic-gate 
38000Sstevel@tonic-gate 	return (0);
38010Sstevel@tonic-gate }
38020Sstevel@tonic-gate 
38030Sstevel@tonic-gate /*
38040Sstevel@tonic-gate  * Look for the value of a numeric option of the form foo=x.  If found, set
38050Sstevel@tonic-gate  * *valp to the value and return non-zero.  If not found or the option is
38060Sstevel@tonic-gate  * malformed, return zero.
38070Sstevel@tonic-gate  */
38080Sstevel@tonic-gate 
38090Sstevel@tonic-gate int
38100Sstevel@tonic-gate nopt(mnt, opt, valp)
38110Sstevel@tonic-gate 	struct mnttab *mnt;
38120Sstevel@tonic-gate 	char *opt;
38130Sstevel@tonic-gate 	int *valp;			/* OUT */
38140Sstevel@tonic-gate {
38150Sstevel@tonic-gate 	char *equal;
38160Sstevel@tonic-gate 	char *str;
38170Sstevel@tonic-gate 
38180Sstevel@tonic-gate 	/*
38190Sstevel@tonic-gate 	 * We should never get a null pointer, but if we do, it's better to
38200Sstevel@tonic-gate 	 * ignore the option than to dump core.
38210Sstevel@tonic-gate 	 */
38220Sstevel@tonic-gate 
38230Sstevel@tonic-gate 	if (valp == NULL) {
38240Sstevel@tonic-gate 		syslog(LOG_DEBUG, "null pointer for %s option", opt);
38250Sstevel@tonic-gate 		return (0);
38260Sstevel@tonic-gate 	}
38270Sstevel@tonic-gate 
38280Sstevel@tonic-gate 	if (str = hasmntopt(mnt, opt)) {
38290Sstevel@tonic-gate 		if (equal = strchr(str, '=')) {
38300Sstevel@tonic-gate 			*valp = atoi(&equal[1]);
38310Sstevel@tonic-gate 			return (1);
38320Sstevel@tonic-gate 		} else {
38330Sstevel@tonic-gate 			syslog(LOG_ERR, "Bad numeric option '%s'", str);
38340Sstevel@tonic-gate 		}
38350Sstevel@tonic-gate 	}
38360Sstevel@tonic-gate 	return (0);
38370Sstevel@tonic-gate }
38380Sstevel@tonic-gate 
3839249Sjwahlig int
38400Sstevel@tonic-gate nfsunmount(mnt)
38410Sstevel@tonic-gate 	struct mnttab *mnt;
38420Sstevel@tonic-gate {
38430Sstevel@tonic-gate 	struct timeval timeout;
38440Sstevel@tonic-gate 	CLIENT *cl;
38450Sstevel@tonic-gate 	enum clnt_stat rpc_stat;
38460Sstevel@tonic-gate 	char *host, *path;
38470Sstevel@tonic-gate 	struct replica *list;
38480Sstevel@tonic-gate 	int i, count = 0;
38490Sstevel@tonic-gate 	int isv4mount = is_v4_mount(mnt->mnt_mountp);
38500Sstevel@tonic-gate 
38510Sstevel@tonic-gate 	if (trace > 1)
38520Sstevel@tonic-gate 		trace_prt(1, "	nfsunmount: umount %s\n", mnt->mnt_mountp);
38530Sstevel@tonic-gate 
38540Sstevel@tonic-gate 	if (umount(mnt->mnt_mountp) < 0) {
38550Sstevel@tonic-gate 		if (trace > 1)
38560Sstevel@tonic-gate 			trace_prt(1, "	nfsunmount: umount %s FAILED\n",
38570Sstevel@tonic-gate 				mnt->mnt_mountp);
38580Sstevel@tonic-gate 		if (errno)
38590Sstevel@tonic-gate 			return (errno);
38600Sstevel@tonic-gate 	}
38610Sstevel@tonic-gate 
38620Sstevel@tonic-gate 	/*
38630Sstevel@tonic-gate 	 * If this is a NFSv4 mount, the mount protocol was not used
38640Sstevel@tonic-gate 	 * so we just return.
38650Sstevel@tonic-gate 	 */
38660Sstevel@tonic-gate 	if (isv4mount) {
38670Sstevel@tonic-gate 		if (trace > 1)
38680Sstevel@tonic-gate 			trace_prt(1, "	nfsunmount: umount %s OK\n",
38690Sstevel@tonic-gate 				mnt->mnt_mountp);
38700Sstevel@tonic-gate 		return (0);
38710Sstevel@tonic-gate 	}
38720Sstevel@tonic-gate 
38730Sstevel@tonic-gate 	/*
38740Sstevel@tonic-gate 	 * If mounted with -o public, then no need to contact server
38750Sstevel@tonic-gate 	 * because mount protocol was not used.
38760Sstevel@tonic-gate 	 */
38770Sstevel@tonic-gate 	if (hasmntopt(mnt, MNTOPT_PUBLIC) != NULL) {
38780Sstevel@tonic-gate 		return (0);
38790Sstevel@tonic-gate 	}
38800Sstevel@tonic-gate 
38810Sstevel@tonic-gate 	/*
38820Sstevel@tonic-gate 	 * The rest of this code is advisory to the server.
38830Sstevel@tonic-gate 	 * If it fails return success anyway.
38840Sstevel@tonic-gate 	 */
38850Sstevel@tonic-gate 
38860Sstevel@tonic-gate 	list = parse_replica(mnt->mnt_special, &count);
38870Sstevel@tonic-gate 	if (!list) {
38880Sstevel@tonic-gate 		if (count >= 0)
38890Sstevel@tonic-gate 			syslog(LOG_ERR,
38900Sstevel@tonic-gate 			    "Memory allocation failed: %m");
38910Sstevel@tonic-gate 		return (ENOMEM);
38920Sstevel@tonic-gate 	}
38930Sstevel@tonic-gate 
38940Sstevel@tonic-gate 	for (i = 0; i < count; i++) {
38950Sstevel@tonic-gate 
38960Sstevel@tonic-gate 		host = list[i].host;
38970Sstevel@tonic-gate 		path = list[i].path;
38980Sstevel@tonic-gate 
38990Sstevel@tonic-gate 		/*
39000Sstevel@tonic-gate 		 * Skip file systems mounted using WebNFS, because mount
39010Sstevel@tonic-gate 		 * protocol was not used.
39020Sstevel@tonic-gate 		 */
39030Sstevel@tonic-gate 		if (strcmp(host, "nfs") == 0 && strncmp(path, "//", 2) == 0)
39040Sstevel@tonic-gate 			continue;
39050Sstevel@tonic-gate 
39060Sstevel@tonic-gate 		cl = clnt_create(host, MOUNTPROG, MOUNTVERS, "datagram_v");
39070Sstevel@tonic-gate 		if (cl == NULL)
39080Sstevel@tonic-gate 			break;
39090Sstevel@tonic-gate #ifdef MALLOC_DEBUG
39100Sstevel@tonic-gate 		add_alloc("CLNT_HANDLE", cl, 0, __FILE__, __LINE__);
39110Sstevel@tonic-gate 		add_alloc("AUTH_HANDLE", cl->cl_auth, 0,
39120Sstevel@tonic-gate 			__FILE__, __LINE__);
39130Sstevel@tonic-gate #endif
39140Sstevel@tonic-gate 		if (__clnt_bindresvport(cl) < 0) {
39150Sstevel@tonic-gate 			if (verbose)
39160Sstevel@tonic-gate 				syslog(LOG_ERR, "umount %s:%s: %s",
39170Sstevel@tonic-gate 					host, path,
39180Sstevel@tonic-gate 					"Couldn't bind to reserved port");
39190Sstevel@tonic-gate 			destroy_auth_client_handle(cl);
39200Sstevel@tonic-gate 			continue;
39210Sstevel@tonic-gate 		}
39220Sstevel@tonic-gate #ifdef MALLOC_DEBUG
39230Sstevel@tonic-gate 		drop_alloc("AUTH_HANDLE", cl->cl_auth, __FILE__, __LINE__);
39240Sstevel@tonic-gate #endif
39250Sstevel@tonic-gate 		AUTH_DESTROY(cl->cl_auth);
39260Sstevel@tonic-gate 		if ((cl->cl_auth = authsys_create_default()) == NULL) {
39270Sstevel@tonic-gate 			if (verbose)
39280Sstevel@tonic-gate 				syslog(LOG_ERR, "umount %s:%s: %s",
39290Sstevel@tonic-gate 					host, path,
39300Sstevel@tonic-gate 					"Failed creating default auth handle");
39310Sstevel@tonic-gate 			destroy_auth_client_handle(cl);
39320Sstevel@tonic-gate 			continue;
39330Sstevel@tonic-gate 		}
39340Sstevel@tonic-gate #ifdef MALLOC_DEBUG
39350Sstevel@tonic-gate 		add_alloc("AUTH_HANDLE", cl->cl_auth, 0, __FILE__, __LINE__);
39360Sstevel@tonic-gate #endif
39370Sstevel@tonic-gate 		timeout.tv_usec = 0;
39380Sstevel@tonic-gate 		timeout.tv_sec = 5;
39390Sstevel@tonic-gate 		rpc_stat = clnt_call(cl, MOUNTPROC_UMNT, xdr_dirpath,
39400Sstevel@tonic-gate 			    (caddr_t)&path, xdr_void, (char *)NULL, timeout);
39410Sstevel@tonic-gate 		if (verbose && rpc_stat != RPC_SUCCESS)
39420Sstevel@tonic-gate 			syslog(LOG_ERR, "%s: %s",
39430Sstevel@tonic-gate 				host, clnt_sperror(cl, "unmount"));
39440Sstevel@tonic-gate 		destroy_auth_client_handle(cl);
39450Sstevel@tonic-gate 	}
39460Sstevel@tonic-gate 
39470Sstevel@tonic-gate 	free_replica(list, count);
39480Sstevel@tonic-gate 
39490Sstevel@tonic-gate 	if (trace > 1)
39500Sstevel@tonic-gate 		trace_prt(1, "	nfsunmount: umount %s OK\n", mnt->mnt_mountp);
39510Sstevel@tonic-gate 
39520Sstevel@tonic-gate done:
39530Sstevel@tonic-gate 	return (0);
39540Sstevel@tonic-gate }
39550Sstevel@tonic-gate 
39560Sstevel@tonic-gate /*
39570Sstevel@tonic-gate  * Put a new entry in the cache chain by prepending it to the front.
39580Sstevel@tonic-gate  * If there isn't enough memory then just give up.
39590Sstevel@tonic-gate  */
39600Sstevel@tonic-gate static void
39610Sstevel@tonic-gate cache_enter(host, reqvers, outvers, proto, state)
39620Sstevel@tonic-gate 	char *host;
39630Sstevel@tonic-gate 	rpcvers_t reqvers;
39640Sstevel@tonic-gate 	rpcvers_t outvers;
39650Sstevel@tonic-gate 	char *proto;
39660Sstevel@tonic-gate 	int state;
39670Sstevel@tonic-gate {
39680Sstevel@tonic-gate 	struct cache_entry *entry;
39690Sstevel@tonic-gate 	int cache_time = 30;	/* sec */
39700Sstevel@tonic-gate 
39710Sstevel@tonic-gate 	timenow = time(NULL);
39720Sstevel@tonic-gate 
39730Sstevel@tonic-gate 	entry = (struct cache_entry *)malloc(sizeof (struct cache_entry));
39740Sstevel@tonic-gate 	if (entry == NULL)
39750Sstevel@tonic-gate 		return;
39760Sstevel@tonic-gate 	(void) memset((caddr_t)entry, 0, sizeof (struct cache_entry));
39770Sstevel@tonic-gate 	entry->cache_host = strdup(host);
39780Sstevel@tonic-gate 	if (entry->cache_host == NULL) {
39790Sstevel@tonic-gate 		cache_free(entry);
39800Sstevel@tonic-gate 		return;
39810Sstevel@tonic-gate 	}
39820Sstevel@tonic-gate 	entry->cache_reqvers = reqvers;
39830Sstevel@tonic-gate 	entry->cache_outvers = outvers;
39840Sstevel@tonic-gate 	entry->cache_proto = (proto == NULL ? NULL : strdup(proto));
39850Sstevel@tonic-gate 	entry->cache_state = state;
39860Sstevel@tonic-gate 	entry->cache_time = timenow + cache_time;
39870Sstevel@tonic-gate 	(void) rw_wrlock(&cache_lock);
39880Sstevel@tonic-gate #ifdef CACHE_DEBUG
39890Sstevel@tonic-gate 	host_cache_accesses++;		/* up host cache access counter */
39900Sstevel@tonic-gate #endif /* CACHE DEBUG */
39910Sstevel@tonic-gate 	entry->cache_next = cache_head;
39920Sstevel@tonic-gate 	cache_head = entry;
39930Sstevel@tonic-gate 	(void) rw_unlock(&cache_lock);
39940Sstevel@tonic-gate }
39950Sstevel@tonic-gate 
39960Sstevel@tonic-gate static int
39970Sstevel@tonic-gate cache_check(host, versp, proto)
39980Sstevel@tonic-gate 	char *host;
39990Sstevel@tonic-gate 	rpcvers_t *versp;
40000Sstevel@tonic-gate 	char *proto;
40010Sstevel@tonic-gate {
40020Sstevel@tonic-gate 	int state = NOHOST;
40030Sstevel@tonic-gate 	struct cache_entry *ce, *prev;
40040Sstevel@tonic-gate 
40050Sstevel@tonic-gate 	timenow = time(NULL);
40060Sstevel@tonic-gate 
40070Sstevel@tonic-gate 	(void) rw_rdlock(&cache_lock);
40080Sstevel@tonic-gate 
40090Sstevel@tonic-gate #ifdef CACHE_DEBUG
40100Sstevel@tonic-gate 	/* Increment the lookup and access counters for the host cache */
40110Sstevel@tonic-gate 	host_cache_accesses++;
40120Sstevel@tonic-gate 	host_cache_lookups++;
40130Sstevel@tonic-gate 	if ((host_cache_lookups%1000) == 0)
40140Sstevel@tonic-gate 		trace_host_cache();
40150Sstevel@tonic-gate #endif /* CACHE DEBUG */
40160Sstevel@tonic-gate 
40170Sstevel@tonic-gate 	for (ce = cache_head; ce; ce = ce->cache_next) {
40180Sstevel@tonic-gate 		if (timenow > ce->cache_time) {
40190Sstevel@tonic-gate 			(void) rw_unlock(&cache_lock);
40200Sstevel@tonic-gate 			(void) rw_wrlock(&cache_lock);
40210Sstevel@tonic-gate 			for (prev = NULL, ce = cache_head; ce;
40220Sstevel@tonic-gate 				prev = ce, ce = ce->cache_next) {
40230Sstevel@tonic-gate 				if (timenow > ce->cache_time) {
40240Sstevel@tonic-gate 					cache_free(ce);
40250Sstevel@tonic-gate 					if (prev)
40260Sstevel@tonic-gate 						prev->cache_next = NULL;
40270Sstevel@tonic-gate 					else
40280Sstevel@tonic-gate 						cache_head = NULL;
40290Sstevel@tonic-gate 					break;
40300Sstevel@tonic-gate 				}
40310Sstevel@tonic-gate 			}
40320Sstevel@tonic-gate 			(void) rw_unlock(&cache_lock);
40330Sstevel@tonic-gate 			return (state);
40340Sstevel@tonic-gate 		}
40350Sstevel@tonic-gate 		if (strcmp(host, ce->cache_host) != 0)
40360Sstevel@tonic-gate 			continue;
40370Sstevel@tonic-gate 		if ((proto == NULL && ce->cache_proto != NULL) ||
40380Sstevel@tonic-gate 		    (proto != NULL && ce->cache_proto == NULL))
40390Sstevel@tonic-gate 			continue;
40400Sstevel@tonic-gate 		if (proto != NULL &&
40410Sstevel@tonic-gate 		    strcmp(proto, ce->cache_proto) != 0)
40420Sstevel@tonic-gate 			continue;
40430Sstevel@tonic-gate 
40440Sstevel@tonic-gate 		if (versp == NULL ||
40450Sstevel@tonic-gate 			(versp != NULL && *versp == ce->cache_reqvers) ||
40460Sstevel@tonic-gate 			(versp != NULL && *versp == ce->cache_outvers)) {
40470Sstevel@tonic-gate 				if (versp != NULL)
40480Sstevel@tonic-gate 					*versp = ce->cache_outvers;
40490Sstevel@tonic-gate 				state = ce->cache_state;
40500Sstevel@tonic-gate 
40510Sstevel@tonic-gate 				/* increment the host cache hit counters */
40520Sstevel@tonic-gate #ifdef CACHE_DEBUG
40530Sstevel@tonic-gate 				if (state == GOODHOST)
40540Sstevel@tonic-gate 					goodhost_cache_hits++;
40550Sstevel@tonic-gate 				if (state == DEADHOST)
40560Sstevel@tonic-gate 					deadhost_cache_hits++;
40570Sstevel@tonic-gate #endif /* CACHE_DEBUG */
40580Sstevel@tonic-gate 				(void) rw_unlock(&cache_lock);
40590Sstevel@tonic-gate 				return (state);
40600Sstevel@tonic-gate 		}
40610Sstevel@tonic-gate 	}
40620Sstevel@tonic-gate 	(void) rw_unlock(&cache_lock);
40630Sstevel@tonic-gate 	return (state);
40640Sstevel@tonic-gate }
40650Sstevel@tonic-gate 
40660Sstevel@tonic-gate /*
40670Sstevel@tonic-gate  * Free a cache entry and all entries
40680Sstevel@tonic-gate  * further down the chain since they
40690Sstevel@tonic-gate  * will also be expired.
40700Sstevel@tonic-gate  */
40710Sstevel@tonic-gate static void
40720Sstevel@tonic-gate cache_free(entry)
40730Sstevel@tonic-gate 	struct cache_entry *entry;
40740Sstevel@tonic-gate {
40750Sstevel@tonic-gate 	struct cache_entry *ce, *next = NULL;
40760Sstevel@tonic-gate 
40770Sstevel@tonic-gate 	for (ce = entry; ce; ce = next) {
40780Sstevel@tonic-gate 		if (ce->cache_host)
40790Sstevel@tonic-gate 			free(ce->cache_host);
40800Sstevel@tonic-gate 		if (ce->cache_proto)
40810Sstevel@tonic-gate 			free(ce->cache_proto);
40820Sstevel@tonic-gate 		next = ce->cache_next;
40830Sstevel@tonic-gate 		free(ce);
40840Sstevel@tonic-gate 	}
40850Sstevel@tonic-gate }
40860Sstevel@tonic-gate 
40870Sstevel@tonic-gate #ifdef MALLOC_DEBUG
40880Sstevel@tonic-gate void
40890Sstevel@tonic-gate cache_flush()
40900Sstevel@tonic-gate {
40910Sstevel@tonic-gate 	(void) rw_wrlock(&cache_lock);
40920Sstevel@tonic-gate 	cache_free(cache_head);
40930Sstevel@tonic-gate 	cache_head = NULL;
40940Sstevel@tonic-gate 	(void) rw_unlock(&cache_lock);
40950Sstevel@tonic-gate }
40960Sstevel@tonic-gate 
40970Sstevel@tonic-gate void
40980Sstevel@tonic-gate flush_caches()
40990Sstevel@tonic-gate {
41000Sstevel@tonic-gate 	mutex_lock(&cleanup_lock);
41010Sstevel@tonic-gate 	cond_signal(&cleanup_start_cv);
41020Sstevel@tonic-gate 	(void) cond_wait(&cleanup_done_cv, &cleanup_lock);
41030Sstevel@tonic-gate 	mutex_unlock(&cleanup_lock);
41040Sstevel@tonic-gate 	cache_flush();
41050Sstevel@tonic-gate 	portmap_cache_flush();
41060Sstevel@tonic-gate }
41070Sstevel@tonic-gate #endif
41080Sstevel@tonic-gate 
41090Sstevel@tonic-gate /*
41100Sstevel@tonic-gate  * Returns 1, if port option is NFS_PORT or
41110Sstevel@tonic-gate  *	nfsd is running on the port given
41120Sstevel@tonic-gate  * Returns 0, if both port is not NFS_PORT and nfsd is not
41130Sstevel@tonic-gate  *	running on the port.
41140Sstevel@tonic-gate  */
41150Sstevel@tonic-gate 
41160Sstevel@tonic-gate static int
41170Sstevel@tonic-gate is_nfs_port(char *opts)
41180Sstevel@tonic-gate {
41190Sstevel@tonic-gate 	struct mnttab m;
41200Sstevel@tonic-gate 	uint_t nfs_port = 0;
41210Sstevel@tonic-gate 	struct servent sv;
41220Sstevel@tonic-gate 	char buf[256];
41230Sstevel@tonic-gate 	int got_port;
41240Sstevel@tonic-gate 
41250Sstevel@tonic-gate 	m.mnt_mntopts = opts;
41260Sstevel@tonic-gate 
41270Sstevel@tonic-gate 	/*
41280Sstevel@tonic-gate 	 * Get port specified in options list, if any.
41290Sstevel@tonic-gate 	 */
41300Sstevel@tonic-gate 	got_port = nopt(&m, MNTOPT_PORT, (int *)&nfs_port);
41310Sstevel@tonic-gate 
41320Sstevel@tonic-gate 	/*
41330Sstevel@tonic-gate 	 * if no port specified or it is same as NFS_PORT return nfs
41340Sstevel@tonic-gate 	 * To use any other daemon the port number should be different
41350Sstevel@tonic-gate 	 */
41360Sstevel@tonic-gate 	if (!got_port || nfs_port == NFS_PORT)
41370Sstevel@tonic-gate 		return (1);
41380Sstevel@tonic-gate 	/*
41390Sstevel@tonic-gate 	 * If daemon is nfsd, return nfs
41400Sstevel@tonic-gate 	 */
41410Sstevel@tonic-gate 	if (getservbyport_r(nfs_port, NULL, &sv, buf, 256) == &sv &&
4142*4590Srm15945 	    strcmp(sv.s_name, "nfsd") == 0)
41430Sstevel@tonic-gate 		return (1);
41440Sstevel@tonic-gate 
41450Sstevel@tonic-gate 	/*
41460Sstevel@tonic-gate 	 * daemon is not nfs
41470Sstevel@tonic-gate 	 */
41480Sstevel@tonic-gate 	return (0);
41490Sstevel@tonic-gate }
41500Sstevel@tonic-gate 
41510Sstevel@tonic-gate 
41520Sstevel@tonic-gate /*
41530Sstevel@tonic-gate  * destroy_auth_client_handle(cl)
41540Sstevel@tonic-gate  * destroys the created client handle
41550Sstevel@tonic-gate  */
41562170Sevanl void
41570Sstevel@tonic-gate destroy_auth_client_handle(CLIENT *cl)
41580Sstevel@tonic-gate {
41590Sstevel@tonic-gate 	if (cl) {
41600Sstevel@tonic-gate 		if (cl->cl_auth) {
41610Sstevel@tonic-gate #ifdef MALLOC_DEBUG
41620Sstevel@tonic-gate 			drop_alloc("AUTH_HANDLE", cl->cl_auth,
4163*4590Srm15945 			    __FILE__, __LINE__);
41640Sstevel@tonic-gate #endif
41650Sstevel@tonic-gate 			AUTH_DESTROY(cl->cl_auth);
41660Sstevel@tonic-gate 			cl->cl_auth = NULL;
41670Sstevel@tonic-gate 		}
41680Sstevel@tonic-gate #ifdef MALLOC_DEBUG
41690Sstevel@tonic-gate 		drop_alloc("CLNT_HANDLE", cl,
4170*4590Srm15945 		    __FILE__, __LINE__);
41710Sstevel@tonic-gate #endif
41720Sstevel@tonic-gate 		clnt_destroy(cl);
41730Sstevel@tonic-gate 	}
41740Sstevel@tonic-gate }
41750Sstevel@tonic-gate 
41760Sstevel@tonic-gate 
41770Sstevel@tonic-gate /*
41780Sstevel@tonic-gate  * Attempt to figure out which version of NFS to use in pingnfs().  If
41790Sstevel@tonic-gate  * the version number was specified (i.e., non-zero), then use it.
41800Sstevel@tonic-gate  * Otherwise, default to the compiled-in default or the default as set
41810Sstevel@tonic-gate  * by the /etc/default/nfs configuration (as read by read_default().
41820Sstevel@tonic-gate  */
41830Sstevel@tonic-gate int
41840Sstevel@tonic-gate set_versrange(rpcvers_t nfsvers, rpcvers_t *vers, rpcvers_t *versmin)
41850Sstevel@tonic-gate {
41860Sstevel@tonic-gate 	switch (nfsvers) {
41870Sstevel@tonic-gate 	case 0:
41880Sstevel@tonic-gate 		*vers = vers_max_default;
41890Sstevel@tonic-gate 		*versmin = vers_min_default;
41900Sstevel@tonic-gate 		break;
41910Sstevel@tonic-gate 	case NFS_V4:
41920Sstevel@tonic-gate 		*vers = NFS_V4;
41930Sstevel@tonic-gate 		*versmin = NFS_V4;
41940Sstevel@tonic-gate 		break;
41950Sstevel@tonic-gate 	case NFS_V3:
41960Sstevel@tonic-gate 		*vers = NFS_V3;
41970Sstevel@tonic-gate 		*versmin = NFS_V3;
41980Sstevel@tonic-gate 		break;
41990Sstevel@tonic-gate 	case NFS_VERSION:
42000Sstevel@tonic-gate 		*vers = NFS_VERSION;		/* version 2 */
42010Sstevel@tonic-gate 		*versmin = NFS_VERSMIN;		/* version 2 */
42020Sstevel@tonic-gate 		break;
42030Sstevel@tonic-gate 	default:
42040Sstevel@tonic-gate 		return (-1);
42050Sstevel@tonic-gate 	}
42060Sstevel@tonic-gate 	return (0);
42070Sstevel@tonic-gate }
42080Sstevel@tonic-gate 
42090Sstevel@tonic-gate #ifdef CACHE_DEBUG
42100Sstevel@tonic-gate /*
42110Sstevel@tonic-gate  * trace_portmap_cache()
42120Sstevel@tonic-gate  * traces the portmap cache values at desired points
42130Sstevel@tonic-gate  */
42140Sstevel@tonic-gate static void
42150Sstevel@tonic-gate trace_portmap_cache()
42160Sstevel@tonic-gate {
42170Sstevel@tonic-gate 	syslog(LOG_ERR, "portmap_cache: accesses=%d lookups=%d hits=%d\n",
4218*4590Srm15945 	    portmap_cache_accesses, portmap_cache_lookups,
4219*4590Srm15945 	    portmap_cache_hits);
42200Sstevel@tonic-gate }
42210Sstevel@tonic-gate 
42220Sstevel@tonic-gate /*
42230Sstevel@tonic-gate  * trace_host_cache()
42240Sstevel@tonic-gate  * traces the host cache values at desired points
42250Sstevel@tonic-gate  */
42260Sstevel@tonic-gate static void
42270Sstevel@tonic-gate trace_host_cache()
42280Sstevel@tonic-gate {
42290Sstevel@tonic-gate 	syslog(LOG_ERR,
4230*4590Srm15945 	    "host_cache: accesses=%d lookups=%d deadhits=%d goodhits=%d\n",
4231*4590Srm15945 	    host_cache_accesses, host_cache_lookups, deadhost_cache_hits,
4232*4590Srm15945 	    goodhost_cache_hits);
42330Sstevel@tonic-gate }
42340Sstevel@tonic-gate #endif /* CACHE_DEBUG */
42350Sstevel@tonic-gate 
42360Sstevel@tonic-gate /*
42370Sstevel@tonic-gate  * Read the /etc/default/nfs configuration file to determine if the
42380Sstevel@tonic-gate  * client has been configured for a new min/max for the NFS version to
42390Sstevel@tonic-gate  * use.
42400Sstevel@tonic-gate  */
42410Sstevel@tonic-gate 
42420Sstevel@tonic-gate #define	NFS_DEFAULT_CHECK 60  /* Seconds to check for nfs default changes */
42430Sstevel@tonic-gate 
42440Sstevel@tonic-gate static void
42450Sstevel@tonic-gate read_default_nfs(void)
42460Sstevel@tonic-gate {
42470Sstevel@tonic-gate 	static time_t lastread = 0;
42480Sstevel@tonic-gate 	struct stat buf;
42490Sstevel@tonic-gate 	char *defval;
42500Sstevel@tonic-gate 	int errno;
42510Sstevel@tonic-gate 	int tmp;
42520Sstevel@tonic-gate 
42530Sstevel@tonic-gate 	/*
42540Sstevel@tonic-gate 	 * Fail silently if we can't stat the default nfs config file
42550Sstevel@tonic-gate 	 */
42560Sstevel@tonic-gate 	if (stat(NFSADMIN, &buf))
42570Sstevel@tonic-gate 		return;
42580Sstevel@tonic-gate 
42590Sstevel@tonic-gate 	if (buf.st_mtime == lastread)
42600Sstevel@tonic-gate 		return;
42610Sstevel@tonic-gate 
42620Sstevel@tonic-gate 	/*
42630Sstevel@tonic-gate 	 * Fail silently if error in opening the default nfs config file
42640Sstevel@tonic-gate 	 * We'll check back in NFS_DEFAULT_CHECK seconds
42650Sstevel@tonic-gate 	 */
42660Sstevel@tonic-gate 	if ((defopen(NFSADMIN)) == 0) {
42670Sstevel@tonic-gate 		if ((defval = defread("NFS_CLIENT_VERSMIN=")) != NULL) {
42680Sstevel@tonic-gate 			errno = 0;
42690Sstevel@tonic-gate 			tmp = strtol(defval, (char **)NULL, 10);
42700Sstevel@tonic-gate 			if (errno == 0) {
42710Sstevel@tonic-gate 				vers_min_default = tmp;
42720Sstevel@tonic-gate 			}
42730Sstevel@tonic-gate 		}
42740Sstevel@tonic-gate 		if ((defval = defread("NFS_CLIENT_VERSMAX=")) != NULL) {
42750Sstevel@tonic-gate 			errno = 0;
42760Sstevel@tonic-gate 			tmp = strtol(defval, (char **)NULL, 10);
42770Sstevel@tonic-gate 			if (errno == 0) {
42780Sstevel@tonic-gate 				vers_max_default = tmp;
42790Sstevel@tonic-gate 			}
42800Sstevel@tonic-gate 		}
42810Sstevel@tonic-gate 		/* close defaults file */
42820Sstevel@tonic-gate 		defopen(NULL);
42830Sstevel@tonic-gate 
42840Sstevel@tonic-gate 		lastread = buf.st_mtime;
42850Sstevel@tonic-gate 
42860Sstevel@tonic-gate 		/*
42870Sstevel@tonic-gate 		 * Quick sanity check on the values picked up from the
42880Sstevel@tonic-gate 		 * defaults file.  Make sure that a mistake wasn't
42890Sstevel@tonic-gate 		 * made that will confuse things later on.
42900Sstevel@tonic-gate 		 * If so, reset to compiled-in defaults
42910Sstevel@tonic-gate 		 */
42920Sstevel@tonic-gate 		if (vers_min_default > vers_max_default ||
4293*4590Srm15945 		    vers_min_default < NFS_VERSMIN ||
4294*4590Srm15945 		    vers_max_default > NFS_VERSMAX) {
42950Sstevel@tonic-gate 			if (trace > 1) {
42960Sstevel@tonic-gate 				trace_prt(1,
42970Sstevel@tonic-gate 	"  read_default: version minimum/maximum incorrectly configured\n");
42980Sstevel@tonic-gate 				trace_prt(1,
42990Sstevel@tonic-gate "  read_default: config is min=%d, max%d. Resetting to min=%d, max%d\n",
4300*4590Srm15945 				    vers_min_default, vers_max_default,
4301*4590Srm15945 				    NFS_VERSMIN_DEFAULT,
4302*4590Srm15945 				    NFS_VERSMAX_DEFAULT);
43030Sstevel@tonic-gate 			}
43040Sstevel@tonic-gate 			vers_min_default = NFS_VERSMIN_DEFAULT;
43050Sstevel@tonic-gate 			vers_max_default = NFS_VERSMAX_DEFAULT;
43060Sstevel@tonic-gate 		}
43070Sstevel@tonic-gate 	}
43080Sstevel@tonic-gate }
43090Sstevel@tonic-gate 
43100Sstevel@tonic-gate /*
43110Sstevel@tonic-gate  *  Find the mnttab entry that corresponds to "name".
43120Sstevel@tonic-gate  *  We're not sure what the name represents: either
43130Sstevel@tonic-gate  *  a mountpoint name, or a special name (server:/path).
43140Sstevel@tonic-gate  *  Return the last entry in the file that matches.
43150Sstevel@tonic-gate  */
43160Sstevel@tonic-gate static struct extmnttab *
43170Sstevel@tonic-gate mnttab_find(dirname)
43180Sstevel@tonic-gate 	char *dirname;
43190Sstevel@tonic-gate {
43200Sstevel@tonic-gate 	FILE *fp;
43210Sstevel@tonic-gate 	struct extmnttab mnt;
43220Sstevel@tonic-gate 	struct extmnttab *res = NULL;
43230Sstevel@tonic-gate 
43240Sstevel@tonic-gate 	fp = fopen(MNTTAB, "r");
43250Sstevel@tonic-gate 	if (fp == NULL) {
43260Sstevel@tonic-gate 		if (trace > 1)
43270Sstevel@tonic-gate 			trace_prt(1, "	mnttab_find: unable to open mnttab\n");
43280Sstevel@tonic-gate 		return (NULL);
43290Sstevel@tonic-gate 	}
43300Sstevel@tonic-gate 	while (getextmntent(fp, &mnt, sizeof (struct extmnttab)) == 0) {
43310Sstevel@tonic-gate 		if (strcmp(mnt.mnt_mountp, dirname) == 0 ||
43320Sstevel@tonic-gate 		    strcmp(mnt.mnt_special, dirname) == 0) {
43330Sstevel@tonic-gate 			if (res)
43340Sstevel@tonic-gate 				fsfreemnttab(res);
43350Sstevel@tonic-gate 			res = fsdupmnttab(&mnt);
43360Sstevel@tonic-gate 		}
43370Sstevel@tonic-gate 	}
43380Sstevel@tonic-gate 
43390Sstevel@tonic-gate 	resetmnttab(fp);
43400Sstevel@tonic-gate 	fclose(fp);
43410Sstevel@tonic-gate 	if (res == NULL) {
43420Sstevel@tonic-gate 		if (trace > 1)
43430Sstevel@tonic-gate 			trace_prt(1, "	mnttab_find: unable to find %s\n",
43440Sstevel@tonic-gate 				dirname);
43450Sstevel@tonic-gate 	}
43460Sstevel@tonic-gate 	return (res);
43470Sstevel@tonic-gate }
43480Sstevel@tonic-gate 
43490Sstevel@tonic-gate /*
43500Sstevel@tonic-gate  * This function's behavior is taken from nfsstat.
43510Sstevel@tonic-gate  * Trying to determine what NFS version was used for the mount.
43520Sstevel@tonic-gate  */
43530Sstevel@tonic-gate static int
43540Sstevel@tonic-gate is_v4_mount(char *mntpath)
43550Sstevel@tonic-gate {
43560Sstevel@tonic-gate 	kstat_ctl_t *kc = NULL;		/* libkstat cookie */
43570Sstevel@tonic-gate 	kstat_t *ksp;
43580Sstevel@tonic-gate 	ulong_t fsid;
43590Sstevel@tonic-gate 	struct mntinfo_kstat mik;
43600Sstevel@tonic-gate 	struct extmnttab *mntp;
43610Sstevel@tonic-gate 	uint_t mnt_minor;
43620Sstevel@tonic-gate 
43630Sstevel@tonic-gate 	if ((mntp = mnttab_find(mntpath)) == NULL)
43640Sstevel@tonic-gate 		return (FALSE);
43650Sstevel@tonic-gate 
43660Sstevel@tonic-gate 	/* save the minor number and free the struct so we don't forget */
43670Sstevel@tonic-gate 	mnt_minor = mntp->mnt_minor;
43680Sstevel@tonic-gate 	fsfreemnttab(mntp);
43690Sstevel@tonic-gate 
43700Sstevel@tonic-gate 	if ((kc = kstat_open()) == NULL)
43710Sstevel@tonic-gate 		return (FALSE);
43720Sstevel@tonic-gate 
43730Sstevel@tonic-gate 	for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
43740Sstevel@tonic-gate 		if (ksp->ks_type != KSTAT_TYPE_RAW)
43750Sstevel@tonic-gate 			continue;
43760Sstevel@tonic-gate 		if (strcmp(ksp->ks_module, "nfs") != 0)
43770Sstevel@tonic-gate 			continue;
43780Sstevel@tonic-gate 		if (strcmp(ksp->ks_name, "mntinfo") != 0)
43790Sstevel@tonic-gate 			continue;
43800Sstevel@tonic-gate 		if (mnt_minor != ksp->ks_instance)
43810Sstevel@tonic-gate 			continue;
43820Sstevel@tonic-gate 
43830Sstevel@tonic-gate 		if (kstat_read(kc, ksp, &mik) == -1)
43840Sstevel@tonic-gate 			continue;
43850Sstevel@tonic-gate 
43860Sstevel@tonic-gate 		(void) kstat_close(kc);
43870Sstevel@tonic-gate 		if (mik.mik_vers == 4)
43880Sstevel@tonic-gate 			return (TRUE);
43890Sstevel@tonic-gate 		else
43900Sstevel@tonic-gate 			return (FALSE);
43910Sstevel@tonic-gate 	}
43920Sstevel@tonic-gate 	(void) kstat_close(kc);
43930Sstevel@tonic-gate 
43940Sstevel@tonic-gate 	return (FALSE);
43950Sstevel@tonic-gate }
43961676Sjpk 
43971676Sjpk static int
43981676Sjpk create_homedir(const char *src, const char *dst) {
43991676Sjpk 
44001676Sjpk 	struct stat stbuf;
44011676Sjpk 	char *dst_username;
44021676Sjpk 	struct passwd *pwd, pwds;
44031676Sjpk 	char buf_pwd[NSS_BUFLEN_PASSWD];
44041676Sjpk 	int homedir_len;
44051676Sjpk 	int dst_dir_len;
44061676Sjpk 	int src_dir_len;
44071676Sjpk 
44081676Sjpk 	if (trace > 1)
44091676Sjpk 		trace_prt(1, "entered create_homedir\n");
44101676Sjpk 
44111676Sjpk 	if (stat(src, &stbuf) == 0) {
44121676Sjpk 		if (trace > 1)
44131676Sjpk 			trace_prt(1, "src exists\n");
44141676Sjpk 		return (1);
44151676Sjpk 	}
44161676Sjpk 
44171676Sjpk 	dst_username = strrchr(dst, '/');
44181676Sjpk 	if (dst_username) {
44191676Sjpk 		dst_username++; /* Skip over slash */
44201676Sjpk 		pwd = getpwnam_r(dst_username, &pwds, buf_pwd,
44211676Sjpk 		    sizeof (buf_pwd));
44221676Sjpk 		if (pwd == NULL) {
44231676Sjpk 			return (0);
44241676Sjpk 		}
44251676Sjpk 	} else {
44261676Sjpk 		return (0);
44271676Sjpk 	}
44281676Sjpk 
44291676Sjpk 	homedir_len = strlen(pwd->pw_dir);
44301676Sjpk 	dst_dir_len = strlen(dst) - homedir_len;
44311676Sjpk 	src_dir_len = strlen(src) - homedir_len;
44321676Sjpk 
44331676Sjpk 	/* Check that the paths are in the same zone */
44341676Sjpk 	if (src_dir_len < dst_dir_len ||
44351676Sjpk 	    (strncmp(dst, src, dst_dir_len) != 0)) {
44361676Sjpk 		if (trace > 1)
44371676Sjpk 			trace_prt(1, "	paths don't match\n");
44381676Sjpk 		return (0);
44391676Sjpk 	}
44401676Sjpk 	/* Check that mountpoint is an auto_home entry */
44411676Sjpk 	if (dst_dir_len < 0 ||
44421676Sjpk 	    (strcmp(pwd->pw_dir, dst + dst_dir_len) != 0)) {
44431676Sjpk 		return (0);
44441676Sjpk 	}
44451676Sjpk 
44461676Sjpk 	/* Check that source is an home directory entry */
44471676Sjpk 	if (src_dir_len < 0 ||
44481676Sjpk 	    (strcmp(pwd->pw_dir, src + src_dir_len) != 0)) {
44491676Sjpk 		if (trace > 1)
44501676Sjpk 			trace_prt(1, "	homedir (2) doesn't match %s\n",
44511676Sjpk 		src+src_dir_len);
44521676Sjpk 		return (0);
44531676Sjpk 	}
44541676Sjpk 
44551676Sjpk 	if (mkdir(src,
44561676Sjpk 	    S_IRUSR | S_IWUSR | S_IXUSR | S_IXGRP | S_IXOTH) == -1) {
44571676Sjpk 		if (trace > 1) {
44581676Sjpk 			trace_prt(1, "	Couldn't mkdir %s\n", src);
44591676Sjpk 		}
44601676Sjpk 		return (0);
44611676Sjpk 	}
44621676Sjpk 
44631676Sjpk 	if (chown(src, pwd->pw_uid, pwd->pw_gid) == -1) {
44641676Sjpk 		unlink(src);
44651676Sjpk 		return (0);
44661676Sjpk 	}
44671676Sjpk 
44681676Sjpk 	/* Created new home directory for the user */
44691676Sjpk 	return (1);
44701676Sjpk }
44712170Sevanl 
44722170Sevanl void
44732170Sevanl free_nfs_args(struct nfs_args *argp)
44742170Sevanl {
44752170Sevanl 	struct nfs_args *oldp;
44762170Sevanl 	while (argp) {
44772170Sevanl 		if (argp->pathconf)
44782170Sevanl 			free(argp->pathconf);
44792170Sevanl 		if (argp->knconf)
44802170Sevanl 			free_knconf(argp->knconf);
44812170Sevanl 		if (argp->addr)
44822170Sevanl 			netbuf_free(argp->addr);
44832170Sevanl 		if (argp->syncaddr)
44842170Sevanl 			netbuf_free(argp->syncaddr);
44852170Sevanl 		if (argp->netname)
44862170Sevanl 			free(argp->netname);
44872170Sevanl 		if (argp->hostname)
44882170Sevanl 			free(argp->hostname);
44892170Sevanl 		if (argp->nfs_ext_u.nfs_extB.secdata)
44902170Sevanl 			nfs_free_secdata(argp->nfs_ext_u.nfs_extB.secdata);
44912170Sevanl 		if (argp->fh)
44922170Sevanl 			free(argp->fh);
44932170Sevanl 		if (argp->nfs_ext_u.nfs_extA.secdata) {
44942170Sevanl 			sec_data_t	*sd;
44952170Sevanl 			sd = argp->nfs_ext_u.nfs_extA.secdata;
44962170Sevanl 			if (sd == NULL)
44972170Sevanl 				break;
44982170Sevanl 			switch (sd->rpcflavor) {
44992170Sevanl 			case AUTH_NONE:
45002170Sevanl 			case AUTH_UNIX:
45012170Sevanl 			case AUTH_LOOPBACK:
45022170Sevanl 				break;
45032170Sevanl 			case AUTH_DES:
45042170Sevanl 			{
45052170Sevanl 				dh_k4_clntdata_t	*dhk4;
45062170Sevanl 				dhk4 = (dh_k4_clntdata_t *)sd->data;
45072170Sevanl 				if (dhk4 == NULL)
45082170Sevanl 					break;
45092170Sevanl 				if (dhk4->syncaddr.buf)
45102170Sevanl 					free(dhk4->syncaddr.buf);
45112170Sevanl 				if (dhk4->knconf->knc_protofmly)
45122170Sevanl 					free(dhk4->knconf->knc_protofmly);
45132170Sevanl 				if (dhk4->knconf->knc_proto)
45142170Sevanl 					free(dhk4->knconf->knc_proto);
45152170Sevanl 				if (dhk4->knconf)
45162170Sevanl 					free(dhk4->knconf);
45172170Sevanl 				if (dhk4->netname)
45182170Sevanl 					free(dhk4->netname);
45192170Sevanl 				free(dhk4);
45202170Sevanl 				break;
45212170Sevanl 			}
45222170Sevanl 			case RPCSEC_GSS:
45232170Sevanl 			{
45242170Sevanl 				gss_clntdata_t	*gss;
45252170Sevanl 				gss = (gss_clntdata_t *)sd->data;
45262170Sevanl 				if (gss == NULL)
45272170Sevanl 					break;
45282170Sevanl 				if (gss->mechanism.elements)
45292170Sevanl 					free(gss->mechanism.elements);
45302170Sevanl 				free(gss);
45312170Sevanl 				break;
45322170Sevanl 			}
45332170Sevanl 			}
45342170Sevanl 		}
45352170Sevanl 		oldp = argp;
45362170Sevanl 		if (argp->nfs_args_ext == NFS_ARGS_EXTB)
45372170Sevanl 			argp = argp->nfs_ext_u.nfs_extB.next;
45382170Sevanl 		else
45392170Sevanl 			argp = NULL;
45402170Sevanl 		free(oldp);
45412170Sevanl 	}
45422170Sevanl }
4543