xref: /onnv-gate/usr/src/cmd/fs.d/autofs/autod_nfs.c (revision 5302:eec6aeacde6e)
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 
88*5302Sth199096 extern void set_nfsv4_ephemeral_mount_to(void);
89*5302Sth199096 
900Sstevel@tonic-gate extern char *nfs_get_qop_name();
910Sstevel@tonic-gate extern AUTH *nfs_create_ah();
920Sstevel@tonic-gate extern enum snego_stat nfs_sec_nego();
930Sstevel@tonic-gate 
940Sstevel@tonic-gate #define	MAXHOSTS	512
950Sstevel@tonic-gate 
960Sstevel@tonic-gate /* number of transports to try */
970Sstevel@tonic-gate #define	MNT_PREF_LISTLEN	2
980Sstevel@tonic-gate #define	FIRST_TRY		1
990Sstevel@tonic-gate #define	SECOND_TRY		2
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate #define	MNTTYPE_CACHEFS "cachefs"
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate /*
1040Sstevel@tonic-gate  * host cache states
1050Sstevel@tonic-gate  */
1060Sstevel@tonic-gate #define	NOHOST		0
1070Sstevel@tonic-gate #define	GOODHOST	1
1080Sstevel@tonic-gate #define	DEADHOST	2
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate #define	NFS_ARGS_EXTB_secdata(args, secdata) \
1110Sstevel@tonic-gate 	{ (args).nfs_args_ext = NFS_ARGS_EXTB, \
1120Sstevel@tonic-gate 	(args).nfs_ext_u.nfs_extB.secdata = secdata; }
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate struct cache_entry {
1150Sstevel@tonic-gate 	struct	cache_entry *cache_next;
1160Sstevel@tonic-gate 	char	*cache_host;
1170Sstevel@tonic-gate 	time_t	cache_time;
1180Sstevel@tonic-gate 	int	cache_state;
1190Sstevel@tonic-gate 	rpcvers_t cache_reqvers;
1200Sstevel@tonic-gate 	rpcvers_t cache_outvers;
1210Sstevel@tonic-gate 	char	*cache_proto;
1220Sstevel@tonic-gate };
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate struct mfs_snego_t {
1250Sstevel@tonic-gate 	int sec_opt;
1260Sstevel@tonic-gate 	bool_t snego_done;
1270Sstevel@tonic-gate 	char *nfs_flavor;
1280Sstevel@tonic-gate 	seconfig_t nfs_sec;
1290Sstevel@tonic-gate };
1300Sstevel@tonic-gate typedef struct mfs_snego_t mfs_snego_t;
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate static struct cache_entry *cache_head = NULL;
1330Sstevel@tonic-gate rwlock_t cache_lock;	/* protect the cache chain */
1340Sstevel@tonic-gate 
1353391Ssemery static enum nfsstat nfsmount(struct mapfs *, char *, char *, int, int, uid_t,
1363391Ssemery 	action_list *);
1370Sstevel@tonic-gate static int is_nfs_port(char *);
1380Sstevel@tonic-gate 
1392170Sevanl void netbuf_free(struct netbuf *);
1402170Sevanl struct knetconfig *get_knconf(struct netconfig *);
1412170Sevanl void free_knconf(struct knetconfig *);
1420Sstevel@tonic-gate static int get_pathconf(CLIENT *, char *, char *, struct pathcnf **, int);
1430Sstevel@tonic-gate static struct mapfs *enum_servers(struct mapent *, char *);
1440Sstevel@tonic-gate static struct mapfs *get_mysubnet_servers(struct mapfs *);
1450Sstevel@tonic-gate static int subnet_test(int af, struct sioc_addrreq *);
1460Sstevel@tonic-gate static	struct	netbuf *get_addr(char *, rpcprog_t, rpcvers_t,
1470Sstevel@tonic-gate 	struct netconfig **, char *, ushort_t, struct t_info *);
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate static	struct	netbuf *get_pubfh(char *, rpcvers_t, mfs_snego_t *,
1500Sstevel@tonic-gate 	struct netconfig **, char *, ushort_t, struct t_info *, caddr_t *,
1510Sstevel@tonic-gate 	bool_t, char *);
1520Sstevel@tonic-gate 
1531676Sjpk static int create_homedir(const char *, const char *);
1541676Sjpk 
1550Sstevel@tonic-gate enum type_of_stuff {
1560Sstevel@tonic-gate 	SERVER_ADDR = 0,
1570Sstevel@tonic-gate 	SERVER_PING = 1,
1580Sstevel@tonic-gate 	SERVER_FH = 2
1590Sstevel@tonic-gate };
1600Sstevel@tonic-gate 
1612170Sevanl void *get_server_stuff(enum type_of_stuff, char *, rpcprog_t,
1620Sstevel@tonic-gate 	rpcvers_t, mfs_snego_t *, struct netconfig **, char *, ushort_t,
1630Sstevel@tonic-gate 	struct t_info *, caddr_t *, bool_t, char *, enum clnt_stat *);
1640Sstevel@tonic-gate 
1652170Sevanl void *get_the_stuff(enum type_of_stuff, char *, rpcprog_t,
1660Sstevel@tonic-gate 	rpcvers_t, mfs_snego_t *, struct netconfig *, ushort_t, struct t_info *,
1670Sstevel@tonic-gate 	caddr_t *, bool_t, char *, enum clnt_stat *);
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate struct mapfs *add_mfs(struct mapfs *, int, struct mapfs **, struct mapfs **);
1700Sstevel@tonic-gate void free_mfs(struct mapfs *);
1710Sstevel@tonic-gate static void dump_mfs(struct mapfs *, char *, int);
1720Sstevel@tonic-gate static char *dump_distance(struct mapfs *);
1730Sstevel@tonic-gate static void cache_free(struct cache_entry *);
1740Sstevel@tonic-gate static int cache_check(char *, rpcvers_t *, char *);
1750Sstevel@tonic-gate static void cache_enter(char *, rpcvers_t, rpcvers_t, char *, int);
1762170Sevanl void destroy_auth_client_handle(CLIENT *cl);
1770Sstevel@tonic-gate 
1780Sstevel@tonic-gate #ifdef CACHE_DEBUG
1790Sstevel@tonic-gate static void trace_host_cache();
1800Sstevel@tonic-gate static void trace_portmap_cache();
1810Sstevel@tonic-gate #endif /* CACHE_DEBUG */
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate static int rpc_timeout = 20;
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate #ifdef CACHE_DEBUG
1860Sstevel@tonic-gate /*
1870Sstevel@tonic-gate  * host cache counters. These variables do not need to be protected
1880Sstevel@tonic-gate  * by mutex's. They have been added to measure the utility of the
1890Sstevel@tonic-gate  * goodhost/deadhost cache in the lazy hierarchical mounting scheme.
1900Sstevel@tonic-gate  */
1910Sstevel@tonic-gate static int host_cache_accesses = 0;
1920Sstevel@tonic-gate static int host_cache_lookups = 0;
1930Sstevel@tonic-gate static int deadhost_cache_hits = 0;
1940Sstevel@tonic-gate static int goodhost_cache_hits = 0;
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate /*
1970Sstevel@tonic-gate  * portmap cache counters. These variables do not need to be protected
1980Sstevel@tonic-gate  * by mutex's. They have been added to measure the utility of the portmap
1990Sstevel@tonic-gate  * cache in the lazy hierarchical mounting scheme.
2000Sstevel@tonic-gate  */
2010Sstevel@tonic-gate static int portmap_cache_accesses = 0;
2020Sstevel@tonic-gate static int portmap_cache_lookups = 0;
2030Sstevel@tonic-gate static int portmap_cache_hits = 0;
2040Sstevel@tonic-gate #endif /* CACHE_DEBUG */
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate /*
2070Sstevel@tonic-gate  * There are the defaults (range) for the client when determining
2080Sstevel@tonic-gate  * which NFS version to use when probing the server (see above).
2090Sstevel@tonic-gate  * These will only be used when the vers mount option is not used and
2100Sstevel@tonic-gate  * these may be reset if /etc/default/nfs is configured to do so.
2110Sstevel@tonic-gate  */
2120Sstevel@tonic-gate static rpcvers_t vers_max_default = NFS_VERSMAX_DEFAULT;
2130Sstevel@tonic-gate static rpcvers_t vers_min_default = NFS_VERSMIN_DEFAULT;
2140Sstevel@tonic-gate 
2150Sstevel@tonic-gate /*
2160Sstevel@tonic-gate  * list of support services needed
2170Sstevel@tonic-gate  */
2180Sstevel@tonic-gate static char	*service_list[] = { STATD, LOCKD, NULL };
2190Sstevel@tonic-gate static char	*service_list_v4[] = { STATD, LOCKD, NFS4CBD, NFSMAPID, NULL };
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate static void read_default_nfs(void);
2220Sstevel@tonic-gate static int is_v4_mount(char *);
2230Sstevel@tonic-gate static void start_nfs4cbd(void);
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate int
2262170Sevanl mount_nfs(
2272170Sevanl 	struct mapent *me,
2282170Sevanl 	char *mntpnt,
2292170Sevanl 	char *prevhost,
2302170Sevanl 	int overlay,
2313391Ssemery 	uid_t uid,
2322170Sevanl 	action_list **alpp)
2330Sstevel@tonic-gate {
2340Sstevel@tonic-gate 	struct mapfs *mfs, *mp;
2350Sstevel@tonic-gate 	int err = -1;
2360Sstevel@tonic-gate 	int cached;
2372170Sevanl 	action_list *alp;
2382170Sevanl 
2392170Sevanl 
2402170Sevanl 	alp = *alpp;
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate 	read_default_nfs();
2430Sstevel@tonic-gate 
2440Sstevel@tonic-gate 	mfs = enum_servers(me, prevhost);
2450Sstevel@tonic-gate 	if (mfs == NULL)
2460Sstevel@tonic-gate 		return (ENOENT);
2470Sstevel@tonic-gate 
2480Sstevel@tonic-gate 	/*
2490Sstevel@tonic-gate 	 * Try loopback if we have something on localhost; if nothing
2500Sstevel@tonic-gate 	 * works, we will fall back to NFS
2510Sstevel@tonic-gate 	 */
2520Sstevel@tonic-gate 	if (is_nfs_port(me->map_mntopts)) {
2530Sstevel@tonic-gate 		for (mp = mfs; mp; mp = mp->mfs_next) {
2540Sstevel@tonic-gate 			if (self_check(mp->mfs_host)) {
2550Sstevel@tonic-gate 				err = loopbackmount(mp->mfs_dir,
2564590Srm15945 				    mntpnt, me->map_mntopts, overlay);
2570Sstevel@tonic-gate 				if (err) {
2580Sstevel@tonic-gate 					mp->mfs_ignore = 1;
2590Sstevel@tonic-gate 				} else {
2602170Sevanl 					/*
2612170Sevanl 					 * Free action_list if there
2622170Sevanl 					 * is one as it is not needed.
2632170Sevanl 					 * Make sure to set alpp to null
2642170Sevanl 					 * so caller doesn't try to free it
2652170Sevanl 					 * again.
2662170Sevanl 					 */
2672170Sevanl 					if (*alpp) {
2682170Sevanl 						free(*alpp);
2692170Sevanl 						*alpp = NULL;
2702170Sevanl 					}
2710Sstevel@tonic-gate 					break;
2720Sstevel@tonic-gate 				}
2730Sstevel@tonic-gate 			}
2740Sstevel@tonic-gate 		}
2750Sstevel@tonic-gate 	}
2760Sstevel@tonic-gate 	if (err) {
2770Sstevel@tonic-gate 		cached = strcmp(me->map_mounter, MNTTYPE_CACHEFS) == 0;
2780Sstevel@tonic-gate 		err = nfsmount(mfs, mntpnt, me->map_mntopts,
2794590Srm15945 		    cached, overlay, uid, alp);
2800Sstevel@tonic-gate 		if (err && trace > 1) {
2810Sstevel@tonic-gate 			trace_prt(1, "	Couldn't mount %s:%s, err=%d\n",
2824590Srm15945 			    mfs->mfs_host, mfs->mfs_dir, err);
2830Sstevel@tonic-gate 		}
2840Sstevel@tonic-gate 	}
2850Sstevel@tonic-gate 	free_mfs(mfs);
2860Sstevel@tonic-gate 	return (err);
2870Sstevel@tonic-gate }
2880Sstevel@tonic-gate 
2890Sstevel@tonic-gate 
2900Sstevel@tonic-gate /*
2910Sstevel@tonic-gate  * Using the new ioctl SIOCTONLINK to determine if a host is on the same
2920Sstevel@tonic-gate  * subnet. Remove the old network, subnet check.
2930Sstevel@tonic-gate  */
2940Sstevel@tonic-gate 
2950Sstevel@tonic-gate static struct mapfs *
2960Sstevel@tonic-gate get_mysubnet_servers(struct mapfs *mfs_in)
2970Sstevel@tonic-gate {
2980Sstevel@tonic-gate 	int s;
2990Sstevel@tonic-gate 	struct mapfs *mfs, *p, *mfs_head = NULL, *mfs_tail = NULL;
3000Sstevel@tonic-gate 
3010Sstevel@tonic-gate 	struct netconfig *nconf;
3020Sstevel@tonic-gate 	NCONF_HANDLE *nc = NULL;
3030Sstevel@tonic-gate 	struct nd_hostserv hs;
3040Sstevel@tonic-gate 	struct nd_addrlist *retaddrs;
3050Sstevel@tonic-gate 	struct netbuf *nb;
3060Sstevel@tonic-gate 	struct sioc_addrreq areq;
3070Sstevel@tonic-gate 	int res;
3080Sstevel@tonic-gate 	int af;
3090Sstevel@tonic-gate 	int i;
3100Sstevel@tonic-gate 	int sa_size;
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate 	hs.h_serv = "rpcbind";
3130Sstevel@tonic-gate 
3140Sstevel@tonic-gate 	for (mfs = mfs_in; mfs; mfs = mfs->mfs_next) {
3150Sstevel@tonic-gate 		nc = setnetconfig();
3160Sstevel@tonic-gate 
3170Sstevel@tonic-gate 		while (nconf = getnetconfig(nc)) {
3180Sstevel@tonic-gate 
3190Sstevel@tonic-gate 			/*
3200Sstevel@tonic-gate 			 * Care about INET family only. proto_done flag
3210Sstevel@tonic-gate 			 * indicates if we have already covered this
3220Sstevel@tonic-gate 			 * protocol family. If so skip it
3230Sstevel@tonic-gate 			 */
3240Sstevel@tonic-gate 			if (((strcmp(nconf->nc_protofmly, NC_INET6) == 0) ||
3254590Srm15945 			    (strcmp(nconf->nc_protofmly, NC_INET) == 0)) &&
3264590Srm15945 			    (nconf->nc_semantics == NC_TPI_CLTS)) {
3270Sstevel@tonic-gate 			} else
3280Sstevel@tonic-gate 				continue;
3290Sstevel@tonic-gate 
3300Sstevel@tonic-gate 			hs.h_host = mfs->mfs_host;
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate 			if (netdir_getbyname(nconf, &hs, &retaddrs) != ND_OK)
3330Sstevel@tonic-gate 				continue;
3340Sstevel@tonic-gate 
3350Sstevel@tonic-gate 			/*
3360Sstevel@tonic-gate 			 * For each host address see if it's on our
3370Sstevel@tonic-gate 			 * local subnet.
3380Sstevel@tonic-gate 			 */
3390Sstevel@tonic-gate 
3400Sstevel@tonic-gate 			if (strcmp(nconf->nc_protofmly, NC_INET6) == 0)
3410Sstevel@tonic-gate 				af = AF_INET6;
3420Sstevel@tonic-gate 			else
3430Sstevel@tonic-gate 				af = AF_INET;
3440Sstevel@tonic-gate 			nb = retaddrs->n_addrs;
3450Sstevel@tonic-gate 			for (i = 0; i < retaddrs->n_cnt; i++, nb++) {
3460Sstevel@tonic-gate 				memset(&areq.sa_addr, 0, sizeof (areq.sa_addr));
3470Sstevel@tonic-gate 				memcpy(&areq.sa_addr, nb->buf, MIN(nb->len,
3484590Srm15945 				    sizeof (areq.sa_addr)));
3490Sstevel@tonic-gate 				if (res = subnet_test(af, &areq)) {
3500Sstevel@tonic-gate 					p = add_mfs(mfs, DIST_MYNET,
3514590Srm15945 					    &mfs_head, &mfs_tail);
3520Sstevel@tonic-gate 					if (!p) {
3530Sstevel@tonic-gate 						netdir_free(retaddrs,
3544590Srm15945 						    ND_ADDRLIST);
3550Sstevel@tonic-gate 						endnetconfig(nc);
3560Sstevel@tonic-gate 						return (NULL);
3570Sstevel@tonic-gate 					}
3580Sstevel@tonic-gate 					break;
3590Sstevel@tonic-gate 				}
3600Sstevel@tonic-gate 			}  /* end of every host */
3610Sstevel@tonic-gate 			if (trace > 2) {
3620Sstevel@tonic-gate 				trace_prt(1, "get_mysubnet_servers: host=%s "
3634590Srm15945 				    "netid=%s res=%s\n", mfs->mfs_host,
3644590Srm15945 				    nconf->nc_netid, res == 1?"SUC":"FAIL");
3650Sstevel@tonic-gate 			}
3660Sstevel@tonic-gate 
3670Sstevel@tonic-gate 			netdir_free(retaddrs, ND_ADDRLIST);
3680Sstevel@tonic-gate 		} /* end of while */
3690Sstevel@tonic-gate 
3700Sstevel@tonic-gate 		endnetconfig(nc);
3710Sstevel@tonic-gate 
3720Sstevel@tonic-gate 	} /* end of every map */
3730Sstevel@tonic-gate 
3740Sstevel@tonic-gate 	return (mfs_head);
3750Sstevel@tonic-gate 
3760Sstevel@tonic-gate }
3770Sstevel@tonic-gate 
3780Sstevel@tonic-gate int
3790Sstevel@tonic-gate subnet_test(int af, struct sioc_addrreq *areq)
3800Sstevel@tonic-gate {
3810Sstevel@tonic-gate 	int s;
3820Sstevel@tonic-gate 
3830Sstevel@tonic-gate 	if ((s = socket(af, SOCK_DGRAM, 0)) < 0) {
3840Sstevel@tonic-gate 		return (0);
3850Sstevel@tonic-gate 	}
3860Sstevel@tonic-gate 
3870Sstevel@tonic-gate 	areq->sa_res = -1;
3880Sstevel@tonic-gate 
3890Sstevel@tonic-gate 	if (ioctl(s, SIOCTONLINK, (caddr_t)areq) < 0) {
3900Sstevel@tonic-gate 		syslog(LOG_ERR, "subnet_test:SIOCTONLINK failed");
3910Sstevel@tonic-gate 		return (0);
3920Sstevel@tonic-gate 	}
3930Sstevel@tonic-gate 	close(s);
3940Sstevel@tonic-gate 	if (areq->sa_res == 1)
3950Sstevel@tonic-gate 		return (1);
3960Sstevel@tonic-gate 	else
3970Sstevel@tonic-gate 		return (0);
3980Sstevel@tonic-gate 
3990Sstevel@tonic-gate 
4000Sstevel@tonic-gate }
4010Sstevel@tonic-gate 
4020Sstevel@tonic-gate /*
4030Sstevel@tonic-gate  * ping a bunch of hosts at once and sort by who responds first
4040Sstevel@tonic-gate  */
4050Sstevel@tonic-gate static struct mapfs *
4060Sstevel@tonic-gate sort_servers(struct mapfs *mfs_in, int timeout)
4070Sstevel@tonic-gate {
4080Sstevel@tonic-gate 	struct mapfs *m1 = NULL;
4090Sstevel@tonic-gate 	enum clnt_stat clnt_stat;
4100Sstevel@tonic-gate 
4110Sstevel@tonic-gate 	if (!mfs_in)
4120Sstevel@tonic-gate 		return (NULL);
4130Sstevel@tonic-gate 
4140Sstevel@tonic-gate 	clnt_stat = nfs_cast(mfs_in, &m1, timeout);
4150Sstevel@tonic-gate 
4160Sstevel@tonic-gate 	if (!m1) {
4170Sstevel@tonic-gate 		char buff[2048] = {'\0'};
4180Sstevel@tonic-gate 
4190Sstevel@tonic-gate 		for (m1 = mfs_in; m1; m1 = m1->mfs_next) {
4200Sstevel@tonic-gate 			(void) strcat(buff, m1->mfs_host);
4210Sstevel@tonic-gate 			if (m1->mfs_next)
4220Sstevel@tonic-gate 				(void) strcat(buff, ",");
4230Sstevel@tonic-gate 		}
4240Sstevel@tonic-gate 
4250Sstevel@tonic-gate 		syslog(LOG_ERR, "servers %s not responding: %s",
4264590Srm15945 		    buff, clnt_sperrno(clnt_stat));
4270Sstevel@tonic-gate 	}
4280Sstevel@tonic-gate 
4290Sstevel@tonic-gate 	return (m1);
4300Sstevel@tonic-gate }
4310Sstevel@tonic-gate 
4320Sstevel@tonic-gate /*
4330Sstevel@tonic-gate  * Add a mapfs entry to the list described by *mfs_head and *mfs_tail,
4340Sstevel@tonic-gate  * provided it is not marked "ignored" and isn't a dupe of ones we've
4350Sstevel@tonic-gate  * already seen.
4360Sstevel@tonic-gate  */
4370Sstevel@tonic-gate struct mapfs *
4380Sstevel@tonic-gate add_mfs(struct mapfs *mfs, int distance, struct mapfs **mfs_head,
4390Sstevel@tonic-gate 	struct mapfs **mfs_tail)
4400Sstevel@tonic-gate {
4410Sstevel@tonic-gate 	struct mapfs *tmp, *new;
4420Sstevel@tonic-gate 
4430Sstevel@tonic-gate 	for (tmp = *mfs_head; tmp; tmp = tmp->mfs_next)
4440Sstevel@tonic-gate 		if ((strcmp(tmp->mfs_host, mfs->mfs_host) == 0 &&
4450Sstevel@tonic-gate 		    strcmp(tmp->mfs_dir, mfs->mfs_dir) == 0) ||
4464590Srm15945 		    mfs->mfs_ignore)
4470Sstevel@tonic-gate 			return (*mfs_head);
4480Sstevel@tonic-gate 	new = (struct mapfs *)malloc(sizeof (struct mapfs));
4490Sstevel@tonic-gate 	if (!new) {
4500Sstevel@tonic-gate 		syslog(LOG_ERR, "Memory allocation failed: %m");
4510Sstevel@tonic-gate 		return (NULL);
4520Sstevel@tonic-gate 	}
4530Sstevel@tonic-gate 	bcopy(mfs, new, sizeof (struct mapfs));
4540Sstevel@tonic-gate 	new->mfs_next = NULL;
4550Sstevel@tonic-gate 	if (distance)
4560Sstevel@tonic-gate 		new->mfs_distance = distance;
4570Sstevel@tonic-gate 	if (!*mfs_head)
4580Sstevel@tonic-gate 		*mfs_tail = *mfs_head = new;
4590Sstevel@tonic-gate 	else {
4600Sstevel@tonic-gate 		(*mfs_tail)->mfs_next = new;
4610Sstevel@tonic-gate 		*mfs_tail = new;
4620Sstevel@tonic-gate 	}
4630Sstevel@tonic-gate 	return (*mfs_head);
4640Sstevel@tonic-gate }
4650Sstevel@tonic-gate 
4660Sstevel@tonic-gate static void
4670Sstevel@tonic-gate dump_mfs(struct mapfs *mfs, char *message, int level)
4680Sstevel@tonic-gate {
4690Sstevel@tonic-gate 	struct mapfs *m1;
4700Sstevel@tonic-gate 
4710Sstevel@tonic-gate 	if (trace <= level)
4720Sstevel@tonic-gate 		return;
4730Sstevel@tonic-gate 
4740Sstevel@tonic-gate 	trace_prt(1, "%s", message);
4750Sstevel@tonic-gate 	if (!mfs) {
4760Sstevel@tonic-gate 		trace_prt(0, "mfs is null\n");
4770Sstevel@tonic-gate 		return;
4780Sstevel@tonic-gate 	}
4790Sstevel@tonic-gate 	for (m1 = mfs; m1; m1 = m1->mfs_next)
4800Sstevel@tonic-gate 		trace_prt(0, "%s[%s] ", m1->mfs_host, dump_distance(m1));
4810Sstevel@tonic-gate 	trace_prt(0, "\n");
4820Sstevel@tonic-gate }
4830Sstevel@tonic-gate 
4840Sstevel@tonic-gate static char *
4850Sstevel@tonic-gate dump_distance(struct mapfs *mfs)
4860Sstevel@tonic-gate {
4870Sstevel@tonic-gate 	switch (mfs->mfs_distance) {
4880Sstevel@tonic-gate 	case 0:			return ("zero");
4890Sstevel@tonic-gate 	case DIST_SELF:		return ("self");
4900Sstevel@tonic-gate 	case DIST_MYSUB:	return ("mysub");
4910Sstevel@tonic-gate 	case DIST_MYNET:	return ("mynet");
4920Sstevel@tonic-gate 	case DIST_OTHER:	return ("other");
4930Sstevel@tonic-gate 	default:		return ("other");
4940Sstevel@tonic-gate 	}
4950Sstevel@tonic-gate }
4960Sstevel@tonic-gate 
4970Sstevel@tonic-gate /*
4980Sstevel@tonic-gate  * Walk linked list "raw", building a new list consisting of members
4990Sstevel@tonic-gate  * NOT found in list "filter", returning the result.
5000Sstevel@tonic-gate  */
5010Sstevel@tonic-gate static struct mapfs *
5020Sstevel@tonic-gate filter_mfs(struct mapfs *raw, struct mapfs *filter)
5030Sstevel@tonic-gate {
5040Sstevel@tonic-gate 	struct mapfs *mfs, *p, *mfs_head = NULL, *mfs_tail = NULL;
5050Sstevel@tonic-gate 	int skip;
5060Sstevel@tonic-gate 
5070Sstevel@tonic-gate 	if (!raw)
5080Sstevel@tonic-gate 		return (NULL);
5090Sstevel@tonic-gate 	for (mfs = raw; mfs; mfs = mfs->mfs_next) {
5100Sstevel@tonic-gate 		for (skip = 0, p = filter; p; p = p->mfs_next) {
5110Sstevel@tonic-gate 			if (strcmp(p->mfs_host, mfs->mfs_host) == 0 &&
5120Sstevel@tonic-gate 			    strcmp(p->mfs_dir, mfs->mfs_dir) == 0) {
5130Sstevel@tonic-gate 				skip = 1;
5140Sstevel@tonic-gate 				break;
5150Sstevel@tonic-gate 			}
5160Sstevel@tonic-gate 		}
5170Sstevel@tonic-gate 		if (skip)
5180Sstevel@tonic-gate 			continue;
5190Sstevel@tonic-gate 		p = add_mfs(mfs, 0, &mfs_head, &mfs_tail);
5200Sstevel@tonic-gate 		if (!p)
5210Sstevel@tonic-gate 			return (NULL);
5220Sstevel@tonic-gate 	}
5230Sstevel@tonic-gate 	return (mfs_head);
5240Sstevel@tonic-gate }
5250Sstevel@tonic-gate 
5260Sstevel@tonic-gate /*
5270Sstevel@tonic-gate  * Walk a linked list of mapfs structs, freeing each member.
5280Sstevel@tonic-gate  */
5290Sstevel@tonic-gate void
5300Sstevel@tonic-gate free_mfs(struct mapfs *mfs)
5310Sstevel@tonic-gate {
5320Sstevel@tonic-gate 	struct mapfs *tmp;
5330Sstevel@tonic-gate 
5340Sstevel@tonic-gate 	while (mfs) {
5350Sstevel@tonic-gate 		tmp = mfs->mfs_next;
5360Sstevel@tonic-gate 		free(mfs);
5370Sstevel@tonic-gate 		mfs = tmp;
5380Sstevel@tonic-gate 	}
5390Sstevel@tonic-gate }
5400Sstevel@tonic-gate 
5410Sstevel@tonic-gate /*
5420Sstevel@tonic-gate  * New code for NFS client failover: we need to carry and sort
5430Sstevel@tonic-gate  * lists of server possibilities rather than return a single
5440Sstevel@tonic-gate  * entry.  It preserves previous behaviour of sorting first by
5450Sstevel@tonic-gate  * locality (loopback-or-preferred/subnet/net/other) and then
5460Sstevel@tonic-gate  * by ping times.  We'll short-circuit this process when we
5470Sstevel@tonic-gate  * have ENOUGH or more entries.
5480Sstevel@tonic-gate  */
5490Sstevel@tonic-gate static struct mapfs *
5500Sstevel@tonic-gate enum_servers(struct mapent *me, char *preferred)
5510Sstevel@tonic-gate {
5520Sstevel@tonic-gate 	struct mapfs *p, *m1, *m2, *mfs_head = NULL, *mfs_tail = NULL;
5530Sstevel@tonic-gate 
5540Sstevel@tonic-gate 	/*
5550Sstevel@tonic-gate 	 * Short-circuit for simple cases.
5560Sstevel@tonic-gate 	 */
5570Sstevel@tonic-gate 	if (!me->map_fs->mfs_next) {
5580Sstevel@tonic-gate 		p = add_mfs(me->map_fs, DIST_OTHER, &mfs_head, &mfs_tail);
5590Sstevel@tonic-gate 		if (!p)
5600Sstevel@tonic-gate 			return (NULL);
5610Sstevel@tonic-gate 		return (mfs_head);
5620Sstevel@tonic-gate 	}
5630Sstevel@tonic-gate 
5640Sstevel@tonic-gate 	dump_mfs(me->map_fs, "	enum_servers: mapent: ", 2);
5650Sstevel@tonic-gate 
5660Sstevel@tonic-gate 	/*
5670Sstevel@tonic-gate 	 * get addresses & see if any are myself
5680Sstevel@tonic-gate 	 * or were mounted from previously in a
5690Sstevel@tonic-gate 	 * hierarchical mount.
5700Sstevel@tonic-gate 	 */
5710Sstevel@tonic-gate 	if (trace > 2)
5720Sstevel@tonic-gate 		trace_prt(1, "	enum_servers: looking for pref/self\n");
5730Sstevel@tonic-gate 	for (m1 = me->map_fs; m1; m1 = m1->mfs_next) {
5740Sstevel@tonic-gate 		if (m1->mfs_ignore)
5750Sstevel@tonic-gate 			continue;
5760Sstevel@tonic-gate 		if (self_check(m1->mfs_host) ||
5770Sstevel@tonic-gate 		    strcmp(m1->mfs_host, preferred) == 0) {
5780Sstevel@tonic-gate 			p = add_mfs(m1, DIST_SELF, &mfs_head, &mfs_tail);
5790Sstevel@tonic-gate 			if (!p)
5800Sstevel@tonic-gate 				return (NULL);
5810Sstevel@tonic-gate 		}
5820Sstevel@tonic-gate 	}
5830Sstevel@tonic-gate 	if (trace > 2 && m1)
5840Sstevel@tonic-gate 		trace_prt(1, "	enum_servers: pref/self found, %s\n",
5854590Srm15945 		    m1->mfs_host);
5860Sstevel@tonic-gate 
5870Sstevel@tonic-gate 	/*
5880Sstevel@tonic-gate 	 * look for entries on this subnet
5890Sstevel@tonic-gate 	 */
5900Sstevel@tonic-gate 	dump_mfs(m1, "	enum_servers: input of get_mysubnet_servers: ", 2);
5910Sstevel@tonic-gate 	m1 = get_mysubnet_servers(me->map_fs);
5920Sstevel@tonic-gate 	dump_mfs(m1, "	enum_servers: output of get_mysubnet_servers: ", 3);
5930Sstevel@tonic-gate 	if (m1 && m1->mfs_next) {
5940Sstevel@tonic-gate 		m2 = sort_servers(m1, rpc_timeout / 2);
5950Sstevel@tonic-gate 		dump_mfs(m2, "	enum_servers: output of sort_servers: ", 3);
5960Sstevel@tonic-gate 		free_mfs(m1);
5970Sstevel@tonic-gate 		m1 = m2;
5980Sstevel@tonic-gate 	}
5990Sstevel@tonic-gate 
6000Sstevel@tonic-gate 	for (m2 = m1; m2; m2 = m2->mfs_next) {
6010Sstevel@tonic-gate 		p = add_mfs(m2, 0, &mfs_head, &mfs_tail);
6020Sstevel@tonic-gate 		if (!p)
6030Sstevel@tonic-gate 			return (NULL);
6040Sstevel@tonic-gate 	}
6050Sstevel@tonic-gate 	if (m1)
6060Sstevel@tonic-gate 		free_mfs(m1);
6070Sstevel@tonic-gate 
6080Sstevel@tonic-gate 	/*
6090Sstevel@tonic-gate 	 * add the rest of the entries at the end
6100Sstevel@tonic-gate 	 */
6110Sstevel@tonic-gate 	m1 = filter_mfs(me->map_fs, mfs_head);
6120Sstevel@tonic-gate 	dump_mfs(m1, "	enum_servers: etc: output of filter_mfs: ", 3);
6130Sstevel@tonic-gate 	m2 = sort_servers(m1, rpc_timeout / 2);
6140Sstevel@tonic-gate 	dump_mfs(m2, "	enum_servers: etc: output of sort_servers: ", 3);
6150Sstevel@tonic-gate 	if (m1)
6160Sstevel@tonic-gate 		free_mfs(m1);
6170Sstevel@tonic-gate 	m1 = m2;
6180Sstevel@tonic-gate 	for (m2 = m1; m2; m2 = m2->mfs_next) {
6190Sstevel@tonic-gate 		p = add_mfs(m2, DIST_OTHER, &mfs_head, &mfs_tail);
6200Sstevel@tonic-gate 		if (!p)
6210Sstevel@tonic-gate 			return (NULL);
6220Sstevel@tonic-gate 	}
6230Sstevel@tonic-gate 	if (m1)
6240Sstevel@tonic-gate 		free_mfs(m1);
6250Sstevel@tonic-gate 
6260Sstevel@tonic-gate done:
6270Sstevel@tonic-gate 	dump_mfs(mfs_head, "  enum_servers: output: ", 1);
6280Sstevel@tonic-gate 	return (mfs_head);
6290Sstevel@tonic-gate }
6300Sstevel@tonic-gate 
6310Sstevel@tonic-gate static enum nfsstat
6322170Sevanl nfsmount(
6332170Sevanl 	struct mapfs *mfs_in,
6342170Sevanl 	char *mntpnt, char *opts,
6352170Sevanl 	int cached, int overlay,
6363391Ssemery 	uid_t uid,
6372170Sevanl 	action_list *alp)
6380Sstevel@tonic-gate {
6390Sstevel@tonic-gate 	CLIENT *cl;
6400Sstevel@tonic-gate 	char remname[MAXPATHLEN], *mnttabtext = NULL;
6410Sstevel@tonic-gate 	char mopts[MAX_MNTOPT_STR];
6420Sstevel@tonic-gate 	char netname[MAXNETNAMELEN+1];
6432170Sevanl 	char	*mntopts = NULL;
6440Sstevel@tonic-gate 	int mnttabcnt = 0;
6450Sstevel@tonic-gate 	int loglevel;
6460Sstevel@tonic-gate 	struct mnttab m;
6470Sstevel@tonic-gate 	struct nfs_args *argp = NULL, *head = NULL, *tail = NULL,
6484590Srm15945 	    *prevhead, *prevtail;
6490Sstevel@tonic-gate 	int flags;
6500Sstevel@tonic-gate 	struct fhstatus fhs;
6510Sstevel@tonic-gate 	struct timeval timeout;
6520Sstevel@tonic-gate 	enum clnt_stat rpc_stat;
6530Sstevel@tonic-gate 	enum nfsstat status;
6540Sstevel@tonic-gate 	struct stat stbuf;
6550Sstevel@tonic-gate 	struct netconfig *nconf;
6560Sstevel@tonic-gate 	rpcvers_t vers, versmin; /* used to negotiate nfs version in pingnfs */
6570Sstevel@tonic-gate 				/* and mount version with mountd */
6580Sstevel@tonic-gate 	rpcvers_t outvers;	/* final version to be used during mount() */
6590Sstevel@tonic-gate 	rpcvers_t nfsvers;	/* version in map options, 0 if not there */
6600Sstevel@tonic-gate 	rpcvers_t mountversmax;	/* tracks the max mountvers during retries */
6610Sstevel@tonic-gate 
6620Sstevel@tonic-gate 	/* used to negotiate nfs version using webnfs */
6630Sstevel@tonic-gate 	rpcvers_t pubvers, pubversmin, pubversmax;
6640Sstevel@tonic-gate 	int posix;
6650Sstevel@tonic-gate 	struct nd_addrlist *retaddrs;
6660Sstevel@tonic-gate 	struct mountres3 res3;
6670Sstevel@tonic-gate 	nfs_fh3 fh3;
6680Sstevel@tonic-gate 	char *fstype;
6690Sstevel@tonic-gate 	int count, i;
6700Sstevel@tonic-gate 	char scerror_msg[MAXMSGLEN];
6710Sstevel@tonic-gate 	int *auths;
6720Sstevel@tonic-gate 	int delay;
6730Sstevel@tonic-gate 	int retries;
6740Sstevel@tonic-gate 	char *nfs_proto = NULL;
6750Sstevel@tonic-gate 	uint_t nfs_port = 0;
6763767Sjs195444 	char *p, *host, *rhost, *dir;
6770Sstevel@tonic-gate 	struct mapfs *mfs = NULL;
6780Sstevel@tonic-gate 	int error, last_error = 0;
6790Sstevel@tonic-gate 	int replicated;
6800Sstevel@tonic-gate 	int entries = 0;
6810Sstevel@tonic-gate 	int v2cnt = 0, v3cnt = 0, v4cnt = 0;
6820Sstevel@tonic-gate 	int v2near = 0, v3near = 0, v4near = 0;
6830Sstevel@tonic-gate 	int skipentry = 0;
6840Sstevel@tonic-gate 	char *nfs_flavor;
6850Sstevel@tonic-gate 	seconfig_t nfs_sec;
6860Sstevel@tonic-gate 	int sec_opt, scerror;
6870Sstevel@tonic-gate 	struct sec_data *secdata;
6880Sstevel@tonic-gate 	int secflags;
6890Sstevel@tonic-gate 	struct netbuf *syncaddr;
6900Sstevel@tonic-gate 	bool_t	use_pubfh;
6910Sstevel@tonic-gate 	ushort_t thisport;
6920Sstevel@tonic-gate 	int got_val;
6930Sstevel@tonic-gate 	mfs_snego_t mfssnego_init, mfssnego;
6940Sstevel@tonic-gate 
6950Sstevel@tonic-gate 	dump_mfs(mfs_in, "  nfsmount: input: ", 2);
6960Sstevel@tonic-gate 	replicated = (mfs_in->mfs_next != NULL);
6970Sstevel@tonic-gate 	m.mnt_mntopts = opts;
6980Sstevel@tonic-gate 	if (replicated && hasmntopt(&m, MNTOPT_SOFT)) {
6990Sstevel@tonic-gate 		if (verbose)
7000Sstevel@tonic-gate 			syslog(LOG_WARNING,
7010Sstevel@tonic-gate 		    "mount on %s is soft and will not be replicated.", mntpnt);
7020Sstevel@tonic-gate 		replicated = 0;
7030Sstevel@tonic-gate 	}
7040Sstevel@tonic-gate 	if (replicated && !hasmntopt(&m, MNTOPT_RO)) {
7050Sstevel@tonic-gate 		if (verbose)
7060Sstevel@tonic-gate 			syslog(LOG_WARNING,
7070Sstevel@tonic-gate 		    "mount on %s is not read-only and will not be replicated.",
7080Sstevel@tonic-gate 			    mntpnt);
7090Sstevel@tonic-gate 		replicated = 0;
7100Sstevel@tonic-gate 	}
7110Sstevel@tonic-gate 	if (replicated && cached) {
7120Sstevel@tonic-gate 		if (verbose)
7130Sstevel@tonic-gate 			syslog(LOG_WARNING,
7140Sstevel@tonic-gate 		    "mount on %s is cached and will not be replicated.",
7154590Srm15945 			    mntpnt);
7160Sstevel@tonic-gate 		replicated = 0;
7170Sstevel@tonic-gate 	}
7180Sstevel@tonic-gate 	if (replicated)
7190Sstevel@tonic-gate 		loglevel = LOG_WARNING;
7200Sstevel@tonic-gate 	else
7210Sstevel@tonic-gate 		loglevel = LOG_ERR;
7220Sstevel@tonic-gate 
7230Sstevel@tonic-gate 	if (trace > 1) {
7240Sstevel@tonic-gate 		if (replicated)
7250Sstevel@tonic-gate 			trace_prt(1, "	nfsmount: replicated mount on %s %s:\n",
7264590Srm15945 			    mntpnt, opts);
7270Sstevel@tonic-gate 		else
7280Sstevel@tonic-gate 			trace_prt(1, "	nfsmount: standard mount on %s %s:\n",
7294590Srm15945 			    mntpnt, opts);
7300Sstevel@tonic-gate 		for (mfs = mfs_in; mfs; mfs = mfs->mfs_next)
7310Sstevel@tonic-gate 			trace_prt(1, "	  %s:%s\n",
7324590Srm15945 			    mfs->mfs_host, mfs->mfs_dir);
7330Sstevel@tonic-gate 	}
7340Sstevel@tonic-gate 
7350Sstevel@tonic-gate 	/*
7360Sstevel@tonic-gate 	 * Make sure mountpoint is safe to mount on
7370Sstevel@tonic-gate 	 */
7380Sstevel@tonic-gate 	if (lstat(mntpnt, &stbuf) < 0) {
7390Sstevel@tonic-gate 		syslog(LOG_ERR, "Couldn't stat %s: %m", mntpnt);
7400Sstevel@tonic-gate 		return (NFSERR_NOENT);
7410Sstevel@tonic-gate 	}
7420Sstevel@tonic-gate 
7430Sstevel@tonic-gate 	/*
7440Sstevel@tonic-gate 	 * Get protocol specified in options list, if any.
7450Sstevel@tonic-gate 	 */
7460Sstevel@tonic-gate 	if ((str_opt(&m, "proto", &nfs_proto)) == -1) {
7470Sstevel@tonic-gate 		return (NFSERR_NOENT);
7480Sstevel@tonic-gate 	}
7490Sstevel@tonic-gate 
7500Sstevel@tonic-gate 	/*
7510Sstevel@tonic-gate 	 * Get port specified in options list, if any.
7520Sstevel@tonic-gate 	 */
7530Sstevel@tonic-gate 	got_val = nopt(&m, MNTOPT_PORT, (int *)&nfs_port);
7540Sstevel@tonic-gate 	if (!got_val)
7550Sstevel@tonic-gate 		nfs_port = 0;	/* "unspecified" */
7560Sstevel@tonic-gate 	if (nfs_port > USHRT_MAX) {
7570Sstevel@tonic-gate 		syslog(LOG_ERR, "%s: invalid port number %d", mntpnt, nfs_port);
7580Sstevel@tonic-gate 		return (NFSERR_NOENT);
7590Sstevel@tonic-gate 	}
7600Sstevel@tonic-gate 
7610Sstevel@tonic-gate 	/*
7620Sstevel@tonic-gate 	 * Set mount(2) flags here, outside of the loop.
7630Sstevel@tonic-gate 	 */
7640Sstevel@tonic-gate 	flags = MS_OPTIONSTR;
7650Sstevel@tonic-gate 	flags |= (hasmntopt(&m, MNTOPT_RO) == NULL) ? 0 : MS_RDONLY;
7660Sstevel@tonic-gate 	flags |= (hasmntopt(&m, MNTOPT_NOSUID) == NULL) ? 0 : MS_NOSUID;
7670Sstevel@tonic-gate 	flags |= overlay ? MS_OVERLAY : 0;
7680Sstevel@tonic-gate 	if (mntpnt[strlen(mntpnt) - 1] != ' ')
7690Sstevel@tonic-gate 		/* direct mount point without offsets */
7700Sstevel@tonic-gate 		flags |= MS_OVERLAY;
7710Sstevel@tonic-gate 
7720Sstevel@tonic-gate 	use_pubfh = (hasmntopt(&m, MNTOPT_PUBLIC) == NULL) ? FALSE : TRUE;
7730Sstevel@tonic-gate 
7740Sstevel@tonic-gate 	(void) memset(&mfssnego_init, 0, sizeof (mfs_snego_t));
7750Sstevel@tonic-gate 	if (hasmntopt(&m, MNTOPT_SECURE) != NULL) {
7760Sstevel@tonic-gate 		if (++mfssnego_init.sec_opt > 1) {
7770Sstevel@tonic-gate 			syslog(loglevel,
7780Sstevel@tonic-gate 			    "conflicting security options");
7790Sstevel@tonic-gate 			return (NFSERR_IO);
7800Sstevel@tonic-gate 		}
7810Sstevel@tonic-gate 		if (nfs_getseconfig_byname("dh", &mfssnego_init.nfs_sec)) {
7820Sstevel@tonic-gate 			syslog(loglevel,
7830Sstevel@tonic-gate 			    "error getting dh information from %s",
7840Sstevel@tonic-gate 			    NFSSEC_CONF);
7850Sstevel@tonic-gate 			return (NFSERR_IO);
7860Sstevel@tonic-gate 		}
7870Sstevel@tonic-gate 	}
7880Sstevel@tonic-gate 
7894955Sjasmith 	if (hasmntopt(&m, MNTOPT_SEC) != NULL) {
7900Sstevel@tonic-gate 		if ((str_opt(&m, MNTOPT_SEC,
7914590Srm15945 		    &mfssnego_init.nfs_flavor)) == -1) {
7920Sstevel@tonic-gate 			syslog(LOG_ERR, "nfsmount: no memory");
7930Sstevel@tonic-gate 			return (NFSERR_IO);
7940Sstevel@tonic-gate 		}
7950Sstevel@tonic-gate 	}
7960Sstevel@tonic-gate 
7970Sstevel@tonic-gate 	if (mfssnego_init.nfs_flavor) {
7980Sstevel@tonic-gate 		if (++mfssnego_init.sec_opt > 1) {
7990Sstevel@tonic-gate 			syslog(loglevel,
8000Sstevel@tonic-gate 			    "conflicting security options");
8010Sstevel@tonic-gate 			free(mfssnego_init.nfs_flavor);
8020Sstevel@tonic-gate 			return (NFSERR_IO);
8030Sstevel@tonic-gate 		}
8040Sstevel@tonic-gate 		if (nfs_getseconfig_byname(mfssnego_init.nfs_flavor,
8054590Srm15945 		    &mfssnego_init.nfs_sec)) {
8060Sstevel@tonic-gate 			syslog(loglevel,
8070Sstevel@tonic-gate 			    "error getting %s information from %s",
8080Sstevel@tonic-gate 			    mfssnego_init.nfs_flavor, NFSSEC_CONF);
8090Sstevel@tonic-gate 			free(mfssnego_init.nfs_flavor);
8100Sstevel@tonic-gate 			return (NFSERR_IO);
8110Sstevel@tonic-gate 		}
8120Sstevel@tonic-gate 		free(mfssnego_init.nfs_flavor);
8130Sstevel@tonic-gate 	}
8140Sstevel@tonic-gate 
8150Sstevel@tonic-gate nextentry:
8160Sstevel@tonic-gate 	skipentry = 0;
8170Sstevel@tonic-gate 
8180Sstevel@tonic-gate 	got_val = nopt(&m, MNTOPT_VERS, (int *)&nfsvers);
8190Sstevel@tonic-gate 	if (!got_val)
8200Sstevel@tonic-gate 		nfsvers = 0;	/* "unspecified" */
8210Sstevel@tonic-gate 	if (set_versrange(nfsvers, &vers, &versmin) != 0) {
8220Sstevel@tonic-gate 		syslog(LOG_ERR, "Incorrect NFS version specified for %s",
8234590Srm15945 		    mntpnt);
8240Sstevel@tonic-gate 		last_error = NFSERR_NOENT;
8250Sstevel@tonic-gate 		goto ret;
8260Sstevel@tonic-gate 	}
8270Sstevel@tonic-gate 
8280Sstevel@tonic-gate 	if (nfsvers != 0) {
8290Sstevel@tonic-gate 		pubversmax = pubversmin = nfsvers;
8300Sstevel@tonic-gate 	} else {
8310Sstevel@tonic-gate 		pubversmax = vers;
8320Sstevel@tonic-gate 		pubversmin = versmin;
8330Sstevel@tonic-gate 	}
8340Sstevel@tonic-gate 
8350Sstevel@tonic-gate 	/*
8360Sstevel@tonic-gate 	 * Walk the whole list, pinging and collecting version
8370Sstevel@tonic-gate 	 * info so that we can make sure the mount will be
8380Sstevel@tonic-gate 	 * homogeneous with respect to version.
8390Sstevel@tonic-gate 	 *
8400Sstevel@tonic-gate 	 * If we have a version preference, this is easy; we'll
8410Sstevel@tonic-gate 	 * just reject anything that doesn't match.
8420Sstevel@tonic-gate 	 *
8430Sstevel@tonic-gate 	 * If not, we want to try to provide the best compromise
8440Sstevel@tonic-gate 	 * that considers proximity, preference for a higher version,
8450Sstevel@tonic-gate 	 * sorted order, and number of replicas.  We will count
8460Sstevel@tonic-gate 	 * the number of V2 and V3 replicas and also the number
8470Sstevel@tonic-gate 	 * which are "near", i.e. the localhost or on the same
8480Sstevel@tonic-gate 	 * subnet.
8490Sstevel@tonic-gate 	 */
8500Sstevel@tonic-gate 	for (mfs = mfs_in; mfs; mfs = mfs->mfs_next) {
8510Sstevel@tonic-gate 
8520Sstevel@tonic-gate 
8530Sstevel@tonic-gate 		if (mfs->mfs_ignore)
8540Sstevel@tonic-gate 			continue;
8550Sstevel@tonic-gate 
8563767Sjs195444 		/*
8573767Sjs195444 		 * If the host is '[a:d:d:r:e:s:s'],
8583767Sjs195444 		 * only use 'a:d:d:r:e:s:s' for communication
8593767Sjs195444 		 */
8603767Sjs195444 		host = strdup(mfs->mfs_host);
8613767Sjs195444 		if (host == NULL) {
8623767Sjs195444 			syslog(LOG_ERR, "nfsmount: no memory");
8633767Sjs195444 			last_error = NFSERR_IO;
8643767Sjs195444 			goto out;
8653767Sjs195444 		}
8663767Sjs195444 		unbracket(&host);
8673767Sjs195444 
8680Sstevel@tonic-gate 		(void) memcpy(&mfssnego, &mfssnego_init, sizeof (mfs_snego_t));
8690Sstevel@tonic-gate 
8700Sstevel@tonic-gate 		if (use_pubfh == TRUE || mfs->mfs_flags & MFS_URL) {
8710Sstevel@tonic-gate 			char *path;
8720Sstevel@tonic-gate 
8730Sstevel@tonic-gate 			if (nfs_port != 0 && mfs->mfs_port != 0 &&
8740Sstevel@tonic-gate 			    nfs_port != mfs->mfs_port) {
8750Sstevel@tonic-gate 
8760Sstevel@tonic-gate 				syslog(LOG_ERR, "nfsmount: port (%u) in nfs URL"
8774590Srm15945 				    " not the same as port (%d) in port "
8784590Srm15945 				    "option\n", mfs->mfs_port, nfs_port);
8790Sstevel@tonic-gate 				last_error = NFSERR_IO;
8800Sstevel@tonic-gate 				goto out;
8810Sstevel@tonic-gate 
8820Sstevel@tonic-gate 			} else if (nfs_port != 0)
8830Sstevel@tonic-gate 				thisport = nfs_port;
8840Sstevel@tonic-gate 			else
8850Sstevel@tonic-gate 				thisport = mfs->mfs_port;
8860Sstevel@tonic-gate 
8870Sstevel@tonic-gate 			dir = mfs->mfs_dir;
8880Sstevel@tonic-gate 
8890Sstevel@tonic-gate 			if ((mfs->mfs_flags & MFS_URL) == 0) {
8900Sstevel@tonic-gate 				path = malloc(strlen(dir) + 2);
8910Sstevel@tonic-gate 				if (path == NULL) {
8920Sstevel@tonic-gate 					syslog(LOG_ERR, "nfsmount: no memory");
8930Sstevel@tonic-gate 					last_error = NFSERR_IO;
8940Sstevel@tonic-gate 					goto out;
8950Sstevel@tonic-gate 				}
8960Sstevel@tonic-gate 				path[0] = (char)WNL_NATIVEPATH;
8970Sstevel@tonic-gate 				(void) strcpy(&path[1], dir);
8980Sstevel@tonic-gate 			} else {
8990Sstevel@tonic-gate 				path = dir;
9000Sstevel@tonic-gate 			}
9010Sstevel@tonic-gate 
9020Sstevel@tonic-gate 			argp = (struct nfs_args *)
9034590Srm15945 			    malloc(sizeof (struct nfs_args));
9040Sstevel@tonic-gate 
9050Sstevel@tonic-gate 			if (!argp) {
9060Sstevel@tonic-gate 				if (path != dir)
9070Sstevel@tonic-gate 					free(path);
9080Sstevel@tonic-gate 				syslog(LOG_ERR, "nfsmount: no memory");
9090Sstevel@tonic-gate 				last_error = NFSERR_IO;
9100Sstevel@tonic-gate 				goto out;
9110Sstevel@tonic-gate 			}
9120Sstevel@tonic-gate 			(void) memset(argp, 0, sizeof (*argp));
9130Sstevel@tonic-gate 
9140Sstevel@tonic-gate 			/*
9150Sstevel@tonic-gate 			 * RDMA support
9160Sstevel@tonic-gate 			 * By now Mount argument struct has been allocated,
9170Sstevel@tonic-gate 			 * either a pub_fh path will be taken or the regular
9180Sstevel@tonic-gate 			 * one. So here if a protocol was specified and it
9190Sstevel@tonic-gate 			 * was not rdma we let it be, else we set DO_RDMA.
9200Sstevel@tonic-gate 			 * If no proto was there we advise on trying RDMA.
9210Sstevel@tonic-gate 			 */
9220Sstevel@tonic-gate 			if (nfs_proto) {
9230Sstevel@tonic-gate 				if (strcmp(nfs_proto, "rdma") == 0) {
9240Sstevel@tonic-gate 					free(nfs_proto);
9250Sstevel@tonic-gate 					nfs_proto = NULL;
9260Sstevel@tonic-gate 					argp->flags |= NFSMNT_DORDMA;
9270Sstevel@tonic-gate 				}
9280Sstevel@tonic-gate 			} else
9290Sstevel@tonic-gate 				argp->flags |= NFSMNT_TRYRDMA;
9300Sstevel@tonic-gate 
9310Sstevel@tonic-gate 			for (pubvers = pubversmax; pubvers >= pubversmin;
9320Sstevel@tonic-gate 			    pubvers--) {
9330Sstevel@tonic-gate 
9340Sstevel@tonic-gate 				nconf = NULL;
9350Sstevel@tonic-gate 				argp->addr = get_pubfh(host, pubvers, &mfssnego,
9360Sstevel@tonic-gate 				    &nconf, nfs_proto, thisport, NULL,
9370Sstevel@tonic-gate 				    &argp->fh, TRUE, path);
9380Sstevel@tonic-gate 
9390Sstevel@tonic-gate 				if (argp->addr != NULL)
9400Sstevel@tonic-gate 					break;
9410Sstevel@tonic-gate 
9420Sstevel@tonic-gate 				if (nconf != NULL)
9430Sstevel@tonic-gate 					freenetconfigent(nconf);
9440Sstevel@tonic-gate 			}
9450Sstevel@tonic-gate 
9460Sstevel@tonic-gate 			if (path != dir)
9470Sstevel@tonic-gate 				free(path);
9480Sstevel@tonic-gate 
9490Sstevel@tonic-gate 			if (argp->addr != NULL) {
9500Sstevel@tonic-gate 
9510Sstevel@tonic-gate 				/*
9520Sstevel@tonic-gate 				 * The use of llock option for NFSv4
9530Sstevel@tonic-gate 				 * mounts is not required since file
9540Sstevel@tonic-gate 				 * locking is included within the protocol
9550Sstevel@tonic-gate 				 */
9560Sstevel@tonic-gate 				if (pubvers != NFS_V4)
9570Sstevel@tonic-gate 					argp->flags |= NFSMNT_LLOCK;
9580Sstevel@tonic-gate 
9590Sstevel@tonic-gate 				argp->flags |= NFSMNT_PUBLIC;
9600Sstevel@tonic-gate 
9614590Srm15945 				vers = pubvers;
9620Sstevel@tonic-gate 				mfs->mfs_args = argp;
9630Sstevel@tonic-gate 				mfs->mfs_version = pubvers;
9640Sstevel@tonic-gate 				mfs->mfs_nconf = nconf;
9650Sstevel@tonic-gate 				mfs->mfs_flags |= MFS_FH_VIA_WEBNFS;
9660Sstevel@tonic-gate 
9670Sstevel@tonic-gate 			} else {
9680Sstevel@tonic-gate 				free(argp);
9690Sstevel@tonic-gate 
9700Sstevel@tonic-gate 				/*
9710Sstevel@tonic-gate 				 * If -public was specified, give up
9720Sstevel@tonic-gate 				 * on this entry now.
9730Sstevel@tonic-gate 				 */
9740Sstevel@tonic-gate 				if (use_pubfh == TRUE) {
9750Sstevel@tonic-gate 					syslog(loglevel,
9760Sstevel@tonic-gate 					    "%s: no public file handle support",
9770Sstevel@tonic-gate 					    host);
9780Sstevel@tonic-gate 					last_error = NFSERR_NOENT;
9790Sstevel@tonic-gate 					mfs->mfs_ignore = 1;
9800Sstevel@tonic-gate 					continue;
9810Sstevel@tonic-gate 				}
9820Sstevel@tonic-gate 
9830Sstevel@tonic-gate 				/*
9840Sstevel@tonic-gate 				 * Back off to a conventional mount.
9850Sstevel@tonic-gate 				 *
9860Sstevel@tonic-gate 				 * URL's can contain escape characters. Get
9870Sstevel@tonic-gate 				 * rid of them.
9880Sstevel@tonic-gate 				 */
9890Sstevel@tonic-gate 				path = malloc(strlen(dir) + 2);
9900Sstevel@tonic-gate 
9910Sstevel@tonic-gate 				if (path == NULL) {
9920Sstevel@tonic-gate 					syslog(LOG_ERR, "nfsmount: no memory");
9930Sstevel@tonic-gate 					last_error = NFSERR_IO;
9940Sstevel@tonic-gate 					goto out;
9950Sstevel@tonic-gate 				}
9960Sstevel@tonic-gate 
9970Sstevel@tonic-gate 				strcpy(path, dir);
9980Sstevel@tonic-gate 				URLparse(path);
9990Sstevel@tonic-gate 				mfs->mfs_dir = path;
10000Sstevel@tonic-gate 				mfs->mfs_flags |= MFS_ALLOC_DIR;
10010Sstevel@tonic-gate 				mfs->mfs_flags &= ~MFS_URL;
10020Sstevel@tonic-gate 			}
10030Sstevel@tonic-gate 		}
10040Sstevel@tonic-gate 
10050Sstevel@tonic-gate 		if ((mfs->mfs_flags & MFS_FH_VIA_WEBNFS) ==  0) {
10060Sstevel@tonic-gate 			i = pingnfs(host, get_retry(opts) + 1, &vers, versmin,
10074590Srm15945 			    0, FALSE, NULL, nfs_proto);
10080Sstevel@tonic-gate 			if (i != RPC_SUCCESS) {
10090Sstevel@tonic-gate 				if (i == RPC_PROGVERSMISMATCH) {
10100Sstevel@tonic-gate 					syslog(loglevel, "server %s: NFS "
10114590Srm15945 					    "protocol version mismatch",
10124590Srm15945 					    host);
10130Sstevel@tonic-gate 				} else {
10140Sstevel@tonic-gate 					syslog(loglevel, "server %s not "
10154590Srm15945 					    "responding", host);
10160Sstevel@tonic-gate 				}
10170Sstevel@tonic-gate 				mfs->mfs_ignore = 1;
10180Sstevel@tonic-gate 				last_error = NFSERR_NOENT;
10190Sstevel@tonic-gate 				continue;
10200Sstevel@tonic-gate 			}
10210Sstevel@tonic-gate 			if (nfsvers != 0 && nfsvers != vers) {
10220Sstevel@tonic-gate 				if (nfs_proto == NULL)
10230Sstevel@tonic-gate 					syslog(loglevel,
10244590Srm15945 					    "NFS version %d "
10254590Srm15945 					    "not supported by %s",
10264590Srm15945 					    nfsvers, host);
10270Sstevel@tonic-gate 				else
10280Sstevel@tonic-gate 					syslog(loglevel,
10294590Srm15945 					    "NFS version %d "
10304590Srm15945 					    "with proto %s "
10314590Srm15945 					    "not supported by %s",
10324590Srm15945 					    nfsvers, nfs_proto, host);
10330Sstevel@tonic-gate 				mfs->mfs_ignore = 1;
10340Sstevel@tonic-gate 				last_error = NFSERR_NOENT;
10350Sstevel@tonic-gate 				continue;
10360Sstevel@tonic-gate 			}
10370Sstevel@tonic-gate 		}
10380Sstevel@tonic-gate 
10393767Sjs195444 		free(host);
10403767Sjs195444 
10410Sstevel@tonic-gate 		switch (vers) {
10420Sstevel@tonic-gate 		case NFS_V4: v4cnt++; break;
10430Sstevel@tonic-gate 		case NFS_V3: v3cnt++; break;
10440Sstevel@tonic-gate 		case NFS_VERSION: v2cnt++; break;
10450Sstevel@tonic-gate 		default: break;
10460Sstevel@tonic-gate 		}
10470Sstevel@tonic-gate 
10480Sstevel@tonic-gate 		/*
10490Sstevel@tonic-gate 		 * It's not clear how useful this stuff is if
10500Sstevel@tonic-gate 		 * we are using webnfs across the internet, but it
10510Sstevel@tonic-gate 		 * can't hurt.
10520Sstevel@tonic-gate 		 */
10530Sstevel@tonic-gate 		if (mfs->mfs_distance &&
10540Sstevel@tonic-gate 		    mfs->mfs_distance <= DIST_MYSUB) {
10550Sstevel@tonic-gate 			switch (vers) {
10560Sstevel@tonic-gate 			case NFS_V4: v4near++; break;
10570Sstevel@tonic-gate 			case NFS_V3: v3near++; break;
10580Sstevel@tonic-gate 			case NFS_VERSION: v2near++; break;
10590Sstevel@tonic-gate 			default: break;
10600Sstevel@tonic-gate 			}
10610Sstevel@tonic-gate 		}
10620Sstevel@tonic-gate 
10630Sstevel@tonic-gate 		/*
10640Sstevel@tonic-gate 		 * If the mount is not replicated, we don't want to
10650Sstevel@tonic-gate 		 * ping every entry, so we'll stop here.  This means
10660Sstevel@tonic-gate 		 * that we may have to go back to "nextentry" above
10673767Sjs195444 		 * to consider another entry if we can't get
10680Sstevel@tonic-gate 		 * all the way to mount(2) with this one.
10690Sstevel@tonic-gate 		 */
10700Sstevel@tonic-gate 		if (!replicated)
10710Sstevel@tonic-gate 			break;
10723767Sjs195444 
10730Sstevel@tonic-gate 	}
10740Sstevel@tonic-gate 
10750Sstevel@tonic-gate 	if (nfsvers == 0) {
10760Sstevel@tonic-gate 		/*
10770Sstevel@tonic-gate 		 * Choose the NFS version.
10780Sstevel@tonic-gate 		 * We prefer higher versions, but will choose a one-
10790Sstevel@tonic-gate 		 * version downgrade in service if we can use a local
10800Sstevel@tonic-gate 		 * network interface and avoid a router.
10810Sstevel@tonic-gate 		 */
10820Sstevel@tonic-gate 		if (v4cnt && v4cnt >= v3cnt && (v4near || !v3near))
10830Sstevel@tonic-gate 			nfsvers = NFS_V4;
10840Sstevel@tonic-gate 		else if (v3cnt && v3cnt >= v2cnt && (v3near || !v2near))
10850Sstevel@tonic-gate 			nfsvers = NFS_V3;
10860Sstevel@tonic-gate 		else
10870Sstevel@tonic-gate 			nfsvers = NFS_VERSION;
10880Sstevel@tonic-gate 		if (trace > 2)
10890Sstevel@tonic-gate 			trace_prt(1,
10900Sstevel@tonic-gate 		    "  nfsmount: v4=%d[%d]v3=%d[%d],v2=%d[%d] => v%d.\n",
10910Sstevel@tonic-gate 			    v4cnt, v4near, v3cnt, v3near,
10920Sstevel@tonic-gate 			    v2cnt, v2near, nfsvers);
10930Sstevel@tonic-gate 	}
10940Sstevel@tonic-gate 
10950Sstevel@tonic-gate 	/*
10960Sstevel@tonic-gate 	 * Since we don't support different NFS versions in replicated
10970Sstevel@tonic-gate 	 * mounts, set fstype now.
10980Sstevel@tonic-gate 	 * Also take the opportunity to set
10990Sstevel@tonic-gate 	 * the mount protocol version as appropriate.
11000Sstevel@tonic-gate 	 */
11010Sstevel@tonic-gate 	switch (nfsvers) {
11020Sstevel@tonic-gate 	case NFS_V4:
11030Sstevel@tonic-gate 		fstype = MNTTYPE_NFS4;
11040Sstevel@tonic-gate 		break;
11050Sstevel@tonic-gate 	case NFS_V3:
11060Sstevel@tonic-gate 		fstype = MNTTYPE_NFS3;
11070Sstevel@tonic-gate 		if (use_pubfh == FALSE) {
11080Sstevel@tonic-gate 			mountversmax = MOUNTVERS3;
11090Sstevel@tonic-gate 			versmin = MOUNTVERS3;
11100Sstevel@tonic-gate 		}
11110Sstevel@tonic-gate 		break;
11120Sstevel@tonic-gate 	case NFS_VERSION:
11130Sstevel@tonic-gate 		fstype = MNTTYPE_NFS;
11140Sstevel@tonic-gate 		if (use_pubfh == FALSE) {
11150Sstevel@tonic-gate 			mountversmax = MOUNTVERS_POSIX;
11160Sstevel@tonic-gate 			versmin = MOUNTVERS;
11170Sstevel@tonic-gate 		}
11180Sstevel@tonic-gate 		break;
11190Sstevel@tonic-gate 	}
11200Sstevel@tonic-gate 
11210Sstevel@tonic-gate 	/*
11220Sstevel@tonic-gate 	 * Our goal here is to evaluate each of several possible
11230Sstevel@tonic-gate 	 * replicas and try to come up with a list we can hand
11240Sstevel@tonic-gate 	 * to mount(2).  If we don't have a valid "head" at the
11250Sstevel@tonic-gate 	 * end of this process, it means we have rejected all
11260Sstevel@tonic-gate 	 * potential server:/path tuples.  We will fail quietly
11270Sstevel@tonic-gate 	 * in front of mount(2), and will have printed errors
11280Sstevel@tonic-gate 	 * where we found them.
11290Sstevel@tonic-gate 	 * XXX - do option work outside loop w careful design
11300Sstevel@tonic-gate 	 * XXX - use macro for error condition free handling
11310Sstevel@tonic-gate 	 */
11320Sstevel@tonic-gate 	for (mfs = mfs_in; mfs; mfs = mfs->mfs_next) {
11330Sstevel@tonic-gate 
11340Sstevel@tonic-gate 		/*
11350Sstevel@tonic-gate 		 * Initialize retry and delay values on a per-server basis.
11360Sstevel@tonic-gate 		 */
11370Sstevel@tonic-gate 		retries = get_retry(opts);
11380Sstevel@tonic-gate 		delay = INITDELAY;
11390Sstevel@tonic-gate retry:
11400Sstevel@tonic-gate 		if (mfs->mfs_ignore)
11410Sstevel@tonic-gate 			continue;
11420Sstevel@tonic-gate 
11430Sstevel@tonic-gate 		/*
11440Sstevel@tonic-gate 		 * If we don't have a fh yet, and if this is not a replicated
11450Sstevel@tonic-gate 		 * mount, we haven't done a pingnfs() on the next entry,
11460Sstevel@tonic-gate 		 * so we don't know if the next entry is up or if it
11470Sstevel@tonic-gate 		 * supports an NFS version we like.  So if we had a problem
11480Sstevel@tonic-gate 		 * with an entry, we need to go back and run through some new
11490Sstevel@tonic-gate 		 * code.
11500Sstevel@tonic-gate 		 */
11510Sstevel@tonic-gate 		if ((mfs->mfs_flags & MFS_FH_VIA_WEBNFS) == 0 &&
11520Sstevel@tonic-gate 		    !replicated && skipentry)
11530Sstevel@tonic-gate 			goto nextentry;
11540Sstevel@tonic-gate 
11550Sstevel@tonic-gate 		vers = mountversmax;
11560Sstevel@tonic-gate 		host = mfs->mfs_host;
11570Sstevel@tonic-gate 		dir = mfs->mfs_dir;
11583767Sjs195444 
11593767Sjs195444 		/*
11603767Sjs195444 		 * Remember the possible '[a:d:d:r:e:s:s]' as the address to be
11613767Sjs195444 		 * later passed to mount(2) and used in the mnttab line, but
11623767Sjs195444 		 * only use 'a:d:d:r:e:s:s' for communication
11633767Sjs195444 		 */
11643767Sjs195444 		rhost = strdup(host);
11653767Sjs195444 		if (rhost == NULL) {
11663767Sjs195444 			syslog(LOG_ERR, "nfsmount: no memory");
11673767Sjs195444 			last_error = NFSERR_IO;
11683767Sjs195444 			goto out;
11693767Sjs195444 		}
11703767Sjs195444 		unbracket(&host);
11713767Sjs195444 
11723767Sjs195444 		(void) sprintf(remname, "%s:%s", rhost, dir);
11730Sstevel@tonic-gate 		if (trace > 4 && replicated)
11740Sstevel@tonic-gate 			trace_prt(1, "	nfsmount: examining %s\n", remname);
11750Sstevel@tonic-gate 
11760Sstevel@tonic-gate 		/*
11770Sstevel@tonic-gate 		 * If it's cached we need to get cachefs to mount it.
11780Sstevel@tonic-gate 		 */
11790Sstevel@tonic-gate 		if (cached) {
11800Sstevel@tonic-gate 			char *copts = opts;
11810Sstevel@tonic-gate 
11820Sstevel@tonic-gate 			/*
11830Sstevel@tonic-gate 			 * If we started with a URL we need to turn on
11840Sstevel@tonic-gate 			 * -o public if not on already
11850Sstevel@tonic-gate 			 */
11860Sstevel@tonic-gate 			if (use_pubfh == FALSE &&
11870Sstevel@tonic-gate 			    (mfs->mfs_flags & MFS_FH_VIA_WEBNFS)) {
11880Sstevel@tonic-gate 
11890Sstevel@tonic-gate 				copts = malloc(strlen(opts) +
11904590Srm15945 				    strlen(",public")+1);
11910Sstevel@tonic-gate 
11920Sstevel@tonic-gate 				if (copts == NULL) {
11930Sstevel@tonic-gate 					syslog(LOG_ERR, "nfsmount: no memory");
11940Sstevel@tonic-gate 					last_error = NFSERR_IO;
11950Sstevel@tonic-gate 					goto out;
11960Sstevel@tonic-gate 				}
11970Sstevel@tonic-gate 
11980Sstevel@tonic-gate 				strcpy(copts, opts);
11990Sstevel@tonic-gate 
12000Sstevel@tonic-gate 				if (strlen(copts) != 0)
12010Sstevel@tonic-gate 					strcat(copts, ",");
12020Sstevel@tonic-gate 
12030Sstevel@tonic-gate 				strcat(copts, "public");
12040Sstevel@tonic-gate 			}
12050Sstevel@tonic-gate 
12060Sstevel@tonic-gate 			last_error = mount_generic(remname, MNTTYPE_CACHEFS,
12074590Srm15945 			    copts, mntpnt, overlay);
12080Sstevel@tonic-gate 
12090Sstevel@tonic-gate 			if (copts != opts)
12100Sstevel@tonic-gate 				free(copts);
12110Sstevel@tonic-gate 
12120Sstevel@tonic-gate 			if (last_error) {
12130Sstevel@tonic-gate 				skipentry = 1;
12140Sstevel@tonic-gate 				mfs->mfs_ignore = 1;
12150Sstevel@tonic-gate 				continue;
12160Sstevel@tonic-gate 			}
12170Sstevel@tonic-gate 			goto out;
12180Sstevel@tonic-gate 		}
12190Sstevel@tonic-gate 
12200Sstevel@tonic-gate 		if (mfs->mfs_args == NULL) {
12210Sstevel@tonic-gate 
12220Sstevel@tonic-gate 			/*
12230Sstevel@tonic-gate 			 * Allocate nfs_args structure
12240Sstevel@tonic-gate 			 */
12250Sstevel@tonic-gate 			argp = (struct nfs_args *)
12264590Srm15945 			    malloc(sizeof (struct nfs_args));
12270Sstevel@tonic-gate 
12280Sstevel@tonic-gate 			if (!argp) {
12290Sstevel@tonic-gate 				syslog(LOG_ERR, "nfsmount: no memory");
12300Sstevel@tonic-gate 				last_error = NFSERR_IO;
12310Sstevel@tonic-gate 				goto out;
12320Sstevel@tonic-gate 			}
12330Sstevel@tonic-gate 
12340Sstevel@tonic-gate 			(void) memset(argp, 0, sizeof (*argp));
12350Sstevel@tonic-gate 
12360Sstevel@tonic-gate 			/*
12370Sstevel@tonic-gate 			 * RDMA support
12380Sstevel@tonic-gate 			 * By now Mount argument struct has been allocated,
12390Sstevel@tonic-gate 			 * either a pub_fh path will be taken or the regular
12400Sstevel@tonic-gate 			 * one. So here if a protocol was specified and it
12410Sstevel@tonic-gate 			 * was not rdma we let it be, else we set DO_RDMA.
12420Sstevel@tonic-gate 			 * If no proto was there we advise on trying RDMA.
12430Sstevel@tonic-gate 			 */
12440Sstevel@tonic-gate 			if (nfs_proto) {
12450Sstevel@tonic-gate 				if (strcmp(nfs_proto, "rdma") == 0) {
12460Sstevel@tonic-gate 					free(nfs_proto);
12470Sstevel@tonic-gate 					nfs_proto = NULL;
12480Sstevel@tonic-gate 					argp->flags |= NFSMNT_DORDMA;
12490Sstevel@tonic-gate 				}
12500Sstevel@tonic-gate 			} else
12510Sstevel@tonic-gate 				argp->flags |= NFSMNT_TRYRDMA;
12520Sstevel@tonic-gate 		} else {
12530Sstevel@tonic-gate 			argp = mfs->mfs_args;
12540Sstevel@tonic-gate 			mfs->mfs_args = NULL;
12550Sstevel@tonic-gate 
12560Sstevel@tonic-gate 			/*
12570Sstevel@tonic-gate 			 * Skip entry if we already have file handle but the
12580Sstevel@tonic-gate 			 * NFS version is wrong.
12590Sstevel@tonic-gate 			 */
12600Sstevel@tonic-gate 			if ((mfs->mfs_flags & MFS_FH_VIA_WEBNFS) &&
12610Sstevel@tonic-gate 			    mfs->mfs_version != nfsvers) {
12620Sstevel@tonic-gate 
12630Sstevel@tonic-gate 				free(argp);
12640Sstevel@tonic-gate 				skipentry = 1;
12650Sstevel@tonic-gate 				mfs->mfs_ignore = 1;
12660Sstevel@tonic-gate 				continue;
12670Sstevel@tonic-gate 			}
12680Sstevel@tonic-gate 		}
12690Sstevel@tonic-gate 
12700Sstevel@tonic-gate 		prevhead = head;
12710Sstevel@tonic-gate 		prevtail = tail;
12720Sstevel@tonic-gate 		if (!head)
12730Sstevel@tonic-gate 			head = tail = argp;
12740Sstevel@tonic-gate 		else
12750Sstevel@tonic-gate 			tail = tail->nfs_ext_u.nfs_extB.next = argp;
12760Sstevel@tonic-gate 
12770Sstevel@tonic-gate 		/*
12780Sstevel@tonic-gate 		 * WebNFS and NFSv4 behave similarly in that they
12790Sstevel@tonic-gate 		 * don't use the mount protocol.  Therefore, avoid
12800Sstevel@tonic-gate 		 * mount protocol like things when version 4 is being
12810Sstevel@tonic-gate 		 * used.
12820Sstevel@tonic-gate 		 */
12830Sstevel@tonic-gate 		if ((mfs->mfs_flags & MFS_FH_VIA_WEBNFS) == 0 &&
12844590Srm15945 		    nfsvers != NFS_V4) {
12854590Srm15945 			timeout.tv_usec = 0;
12864590Srm15945 			timeout.tv_sec = rpc_timeout;
12874590Srm15945 			rpc_stat = RPC_TIMEDOUT;
12884590Srm15945 
12894590Srm15945 			/* Create the client handle. */
12904590Srm15945 
12914590Srm15945 			if (trace > 1) {
12924590Srm15945 				trace_prt(1,
12934590Srm15945 				    "  nfsmount: Get mount version: request "
12944590Srm15945 				    "vers=%d min=%d\n", vers, versmin);
12950Sstevel@tonic-gate 			}
12964590Srm15945 
12974590Srm15945 			while ((cl = clnt_create_vers(host, MOUNTPROG, &outvers,
12984590Srm15945 			    versmin, vers, "udp")) == NULL) {
12994590Srm15945 				if (trace > 4) {
13004590Srm15945 					trace_prt(1,
13014590Srm15945 					    "  nfsmount: Can't get mount "
13024590Srm15945 					    "version: rpcerr=%d\n",
13034590Srm15945 					    rpc_createerr.cf_stat);
13044590Srm15945 				}
13054590Srm15945 				if (rpc_createerr.cf_stat == RPC_UNKNOWNHOST ||
13064590Srm15945 				    rpc_createerr.cf_stat == RPC_TIMEDOUT)
13074590Srm15945 					break;
13080Sstevel@tonic-gate 
13090Sstevel@tonic-gate 			/*
13100Sstevel@tonic-gate 			 * backoff and return lower version to retry the ping.
13110Sstevel@tonic-gate 			 * XXX we should be more careful and handle
13120Sstevel@tonic-gate 			 * RPC_PROGVERSMISMATCH here, because that error
13130Sstevel@tonic-gate 			 * is handled in clnt_create_vers(). It's not done to
13140Sstevel@tonic-gate 			 * stay in sync with the nfs mount command.
13150Sstevel@tonic-gate 			 */
13164590Srm15945 				vers--;
13174590Srm15945 				if (vers < versmin)
13184590Srm15945 					break;
13194590Srm15945 				if (trace > 4) {
13204590Srm15945 					trace_prt(1,
13214590Srm15945 					    "  nfsmount: Try version=%d\n",
13224590Srm15945 					    vers);
13234590Srm15945 				}
13240Sstevel@tonic-gate 			}
13250Sstevel@tonic-gate 
13264590Srm15945 			if (cl == NULL) {
13274590Srm15945 				free(argp);
13284590Srm15945 				head = prevhead;
13294590Srm15945 				tail = prevtail;
13304590Srm15945 				if (tail)
13314590Srm15945 					tail->nfs_ext_u.nfs_extB.next = NULL;
13324590Srm15945 				last_error = NFSERR_NOENT;
13334590Srm15945 
13344590Srm15945 				if (rpc_createerr.cf_stat != RPC_UNKNOWNHOST &&
13354590Srm15945 				    rpc_createerr.cf_stat !=
13364590Srm15945 				    RPC_PROGVERSMISMATCH &&
13374590Srm15945 				    retries-- > 0) {
13384590Srm15945 					DELAY(delay);
13394590Srm15945 					goto retry;
13404590Srm15945 				}
13414590Srm15945 
13424590Srm15945 				syslog(loglevel, "%s %s", host,
13434590Srm15945 				    clnt_spcreateerror(
13444590Srm15945 				    "server not responding"));
13454590Srm15945 				skipentry = 1;
13464590Srm15945 				mfs->mfs_ignore = 1;
13474590Srm15945 				continue;
13484590Srm15945 			}
13494590Srm15945 			if (trace > 1) {
13504590Srm15945 				trace_prt(1,
13514590Srm15945 				    "	nfsmount: mount version=%d\n", outvers);
13524590Srm15945 			}
13530Sstevel@tonic-gate #ifdef MALLOC_DEBUG
13544590Srm15945 			add_alloc("CLNT_HANDLE", cl, 0, __FILE__, __LINE__);
13554590Srm15945 			add_alloc("AUTH_HANDLE", cl->cl_auth, 0,
13564590Srm15945 			    __FILE__, __LINE__);
13570Sstevel@tonic-gate #endif
13580Sstevel@tonic-gate 
13594590Srm15945 			if (__clnt_bindresvport(cl) < 0) {
13604590Srm15945 				free(argp);
13614590Srm15945 				head = prevhead;
13624590Srm15945 				tail = prevtail;
13634590Srm15945 				if (tail)
13644590Srm15945 					tail->nfs_ext_u.nfs_extB.next = NULL;
13654590Srm15945 				last_error = NFSERR_NOENT;
13664590Srm15945 
13674590Srm15945 				if (retries-- > 0) {
13684590Srm15945 					destroy_auth_client_handle(cl);
13694590Srm15945 					DELAY(delay);
13704590Srm15945 					goto retry;
13714590Srm15945 				}
13724590Srm15945 
13734590Srm15945 				syslog(loglevel, "mount %s: %s", host,
13744590Srm15945 				    "Couldn't bind to reserved port");
13750Sstevel@tonic-gate 				destroy_auth_client_handle(cl);
13764590Srm15945 				skipentry = 1;
13774590Srm15945 				mfs->mfs_ignore = 1;
13784590Srm15945 				continue;
13790Sstevel@tonic-gate 			}
13800Sstevel@tonic-gate 
13810Sstevel@tonic-gate #ifdef MALLOC_DEBUG
13824590Srm15945 			drop_alloc("AUTH_HANDLE", cl->cl_auth,
13834590Srm15945 			    __FILE__, __LINE__);
13840Sstevel@tonic-gate #endif
13854590Srm15945 			AUTH_DESTROY(cl->cl_auth);
13864590Srm15945 			if ((cl->cl_auth = authsys_create_default()) == NULL) {
13874590Srm15945 				free(argp);
13884590Srm15945 				head = prevhead;
13894590Srm15945 				tail = prevtail;
13904590Srm15945 				if (tail)
13914590Srm15945 					tail->nfs_ext_u.nfs_extB.next = NULL;
13924590Srm15945 				last_error = NFSERR_NOENT;
13934590Srm15945 
13944590Srm15945 				if (retries-- > 0) {
13954590Srm15945 					destroy_auth_client_handle(cl);
13964590Srm15945 					DELAY(delay);
13974590Srm15945 					goto retry;
13984590Srm15945 				}
13994590Srm15945 
14004590Srm15945 				syslog(loglevel, "mount %s: %s", host,
14014590Srm15945 				    "Failed creating default auth handle");
14020Sstevel@tonic-gate 				destroy_auth_client_handle(cl);
14034590Srm15945 				skipentry = 1;
14044590Srm15945 				mfs->mfs_ignore = 1;
14054590Srm15945 				continue;
14060Sstevel@tonic-gate 			}
14070Sstevel@tonic-gate #ifdef MALLOC_DEBUG
14084590Srm15945 			add_alloc("AUTH_HANDLE", cl->cl_auth, 0,
14094590Srm15945 			    __FILE__, __LINE__);
14100Sstevel@tonic-gate #endif
14110Sstevel@tonic-gate 		} else
14124590Srm15945 			cl = NULL;
14130Sstevel@tonic-gate 
14140Sstevel@tonic-gate 		/*
14150Sstevel@tonic-gate 		 * set security options
14160Sstevel@tonic-gate 		 */
14170Sstevel@tonic-gate 		sec_opt = 0;
14180Sstevel@tonic-gate 		(void) memset(&nfs_sec, 0, sizeof (nfs_sec));
14190Sstevel@tonic-gate 		if (hasmntopt(&m, MNTOPT_SECURE) != NULL) {
14200Sstevel@tonic-gate 			if (++sec_opt > 1) {
14210Sstevel@tonic-gate 				syslog(loglevel,
14220Sstevel@tonic-gate 				    "conflicting security options for %s",
14230Sstevel@tonic-gate 				    remname);
14240Sstevel@tonic-gate 				free(argp);
14250Sstevel@tonic-gate 				head = prevhead;
14260Sstevel@tonic-gate 				tail = prevtail;
14270Sstevel@tonic-gate 				if (tail)
14280Sstevel@tonic-gate 					tail->nfs_ext_u.nfs_extB.next = NULL;
14290Sstevel@tonic-gate 				last_error = NFSERR_IO;
14300Sstevel@tonic-gate 				destroy_auth_client_handle(cl);
14310Sstevel@tonic-gate 				skipentry = 1;
14320Sstevel@tonic-gate 				mfs->mfs_ignore = 1;
14330Sstevel@tonic-gate 				continue;
14340Sstevel@tonic-gate 			}
14350Sstevel@tonic-gate 			if (nfs_getseconfig_byname("dh", &nfs_sec)) {
14360Sstevel@tonic-gate 				syslog(loglevel,
14370Sstevel@tonic-gate 				    "error getting dh information from %s",
14380Sstevel@tonic-gate 				    NFSSEC_CONF);
14390Sstevel@tonic-gate 				free(argp);
14400Sstevel@tonic-gate 				head = prevhead;
14410Sstevel@tonic-gate 				tail = prevtail;
14420Sstevel@tonic-gate 				if (tail)
14430Sstevel@tonic-gate 					tail->nfs_ext_u.nfs_extB.next = NULL;
14440Sstevel@tonic-gate 				last_error = NFSERR_IO;
14450Sstevel@tonic-gate 				destroy_auth_client_handle(cl);
14460Sstevel@tonic-gate 				skipentry = 1;
14470Sstevel@tonic-gate 				mfs->mfs_ignore = 1;
14480Sstevel@tonic-gate 				continue;
14490Sstevel@tonic-gate 			}
14500Sstevel@tonic-gate 		}
14510Sstevel@tonic-gate 
14520Sstevel@tonic-gate 		nfs_flavor = NULL;
14534955Sjasmith 		if (hasmntopt(&m, MNTOPT_SEC) != NULL) {
14540Sstevel@tonic-gate 			if ((str_opt(&m, MNTOPT_SEC, &nfs_flavor)) == -1) {
14550Sstevel@tonic-gate 				syslog(LOG_ERR, "nfsmount: no memory");
14560Sstevel@tonic-gate 				last_error = NFSERR_IO;
14570Sstevel@tonic-gate 				destroy_auth_client_handle(cl);
14580Sstevel@tonic-gate 				goto out;
14590Sstevel@tonic-gate 			}
14600Sstevel@tonic-gate 		}
14610Sstevel@tonic-gate 
14620Sstevel@tonic-gate 		if (nfs_flavor) {
14630Sstevel@tonic-gate 			if (++sec_opt > 1) {
14640Sstevel@tonic-gate 				syslog(loglevel,
14650Sstevel@tonic-gate 				    "conflicting security options for %s",
14660Sstevel@tonic-gate 				    remname);
14670Sstevel@tonic-gate 				free(nfs_flavor);
14680Sstevel@tonic-gate 				free(argp);
14690Sstevel@tonic-gate 				head = prevhead;
14700Sstevel@tonic-gate 				tail = prevtail;
14710Sstevel@tonic-gate 				if (tail)
14720Sstevel@tonic-gate 					tail->nfs_ext_u.nfs_extB.next = NULL;
14730Sstevel@tonic-gate 				last_error = NFSERR_IO;
14740Sstevel@tonic-gate 				destroy_auth_client_handle(cl);
14750Sstevel@tonic-gate 				skipentry = 1;
14760Sstevel@tonic-gate 				mfs->mfs_ignore = 1;
14770Sstevel@tonic-gate 				continue;
14780Sstevel@tonic-gate 			}
14790Sstevel@tonic-gate 			if (nfs_getseconfig_byname(nfs_flavor, &nfs_sec)) {
14800Sstevel@tonic-gate 				syslog(loglevel,
14810Sstevel@tonic-gate 				    "error getting %s information from %s",
14820Sstevel@tonic-gate 				    nfs_flavor, NFSSEC_CONF);
14830Sstevel@tonic-gate 				free(nfs_flavor);
14840Sstevel@tonic-gate 				free(argp);
14850Sstevel@tonic-gate 				head = prevhead;
14860Sstevel@tonic-gate 				tail = prevtail;
14870Sstevel@tonic-gate 				if (tail)
14880Sstevel@tonic-gate 					tail->nfs_ext_u.nfs_extB.next = NULL;
14890Sstevel@tonic-gate 				last_error = NFSERR_IO;
14900Sstevel@tonic-gate 				destroy_auth_client_handle(cl);
14910Sstevel@tonic-gate 				skipentry = 1;
14920Sstevel@tonic-gate 				mfs->mfs_ignore = 1;
14930Sstevel@tonic-gate 				continue;
14940Sstevel@tonic-gate 			}
14950Sstevel@tonic-gate 			free(nfs_flavor);
14960Sstevel@tonic-gate 		}
14970Sstevel@tonic-gate 
14980Sstevel@tonic-gate 		posix = (nfsvers != NFS_V4 &&
14994590Srm15945 		    hasmntopt(&m, MNTOPT_POSIX) != NULL) ? 1 : 0;
15000Sstevel@tonic-gate 
15010Sstevel@tonic-gate 		if ((mfs->mfs_flags & MFS_FH_VIA_WEBNFS) == 0 &&
15024590Srm15945 		    nfsvers != NFS_V4) {
15034590Srm15945 			bool_t give_up_on_mnt;
15044590Srm15945 			bool_t got_mnt_error;
15050Sstevel@tonic-gate 		/*
15060Sstevel@tonic-gate 		 * If we started with a URL, if first byte of path is not "/",
15070Sstevel@tonic-gate 		 * then the mount will likely fail, so we should try again
15080Sstevel@tonic-gate 		 * with a prepended "/".
15090Sstevel@tonic-gate 		 */
15104590Srm15945 			if (mfs->mfs_flags & MFS_ALLOC_DIR && *dir != '/')
15114590Srm15945 				give_up_on_mnt = FALSE;
15124590Srm15945 			else
15134590Srm15945 				give_up_on_mnt = TRUE;
15144590Srm15945 
15154590Srm15945 			got_mnt_error = FALSE;
15160Sstevel@tonic-gate 
15170Sstevel@tonic-gate try_mnt_slash:
15184590Srm15945 			if (got_mnt_error == TRUE) {
15194590Srm15945 				int i, l;
15204590Srm15945 
15214590Srm15945 				give_up_on_mnt = TRUE;
15224590Srm15945 				l = strlen(dir);
15234590Srm15945 
15244590Srm15945 				/*
15254590Srm15945 				 * Insert a "/" to front of mfs_dir.
15264590Srm15945 				 */
15274590Srm15945 				for (i = l; i > 0; i--)
15284590Srm15945 					dir[i] = dir[i-1];
15294590Srm15945 
15304590Srm15945 				dir[0] = '/';
15314590Srm15945 			}
15324590Srm15945 
15334590Srm15945 			/* Get fhandle of remote path from server's mountd */
15344590Srm15945 
15354590Srm15945 			switch (outvers) {
15364590Srm15945 			case MOUNTVERS:
15374590Srm15945 				if (posix) {
15384590Srm15945 					free(argp);
15394590Srm15945 					head = prevhead;
15404590Srm15945 					tail = prevtail;
15414590Srm15945 					if (tail)
15424590Srm15945 						tail->nfs_ext_u.nfs_extB.next =
15434590Srm15945 						    NULL;
15444590Srm15945 					last_error = NFSERR_NOENT;
15454590Srm15945 					syslog(loglevel,
15464590Srm15945 					    "can't get posix info for %s",
15474590Srm15945 					    host);
15484590Srm15945 					destroy_auth_client_handle(cl);
15494590Srm15945 					skipentry = 1;
15504590Srm15945 					mfs->mfs_ignore = 1;
15514590Srm15945 					continue;
15524590Srm15945 				}
15534590Srm15945 		    /* FALLTHRU */
15544590Srm15945 			case MOUNTVERS_POSIX:
15554590Srm15945 				if (nfsvers == NFS_V3) {
15564590Srm15945 					free(argp);
15574590Srm15945 					head = prevhead;
15584590Srm15945 					tail = prevtail;
15594590Srm15945 					if (tail)
15604590Srm15945 						tail->nfs_ext_u.nfs_extB.next =
15614590Srm15945 						    NULL;
15624590Srm15945 					last_error = NFSERR_NOENT;
15634590Srm15945 					syslog(loglevel,
15644590Srm15945 					    "%s doesn't support NFS Version 3",
15654590Srm15945 					    host);
15664590Srm15945 					destroy_auth_client_handle(cl);
15674590Srm15945 					skipentry = 1;
15684590Srm15945 					mfs->mfs_ignore = 1;
15694590Srm15945 					continue;
15704590Srm15945 				}
15714590Srm15945 				rpc_stat = clnt_call(cl, MOUNTPROC_MNT,
15724590Srm15945 				    xdr_dirpath, (caddr_t)&dir,
15734590Srm15945 				    xdr_fhstatus, (caddr_t)&fhs, timeout);
15744590Srm15945 				if (rpc_stat != RPC_SUCCESS) {
15754590Srm15945 
15764590Srm15945 					if (give_up_on_mnt == FALSE) {
15774590Srm15945 						got_mnt_error = TRUE;
15784590Srm15945 						goto try_mnt_slash;
15794590Srm15945 					}
15804590Srm15945 
15814590Srm15945 				/*
15824590Srm15945 				 * Given the way "clnt_sperror" works, the "%s"
15834590Srm15945 				 * immediately following the "not responding"
15844590Srm15945 				 * is correct.
15854590Srm15945 				 */
15864590Srm15945 					free(argp);
15874590Srm15945 					head = prevhead;
15884590Srm15945 					tail = prevtail;
15894590Srm15945 					if (tail)
15904590Srm15945 						tail->nfs_ext_u.nfs_extB.next =
15914590Srm15945 						    NULL;
15924590Srm15945 					last_error = NFSERR_NOENT;
15934590Srm15945 
15944590Srm15945 					if (retries-- > 0) {
15954590Srm15945 						destroy_auth_client_handle(cl);
15964590Srm15945 						DELAY(delay);
15974590Srm15945 						goto retry;
15984590Srm15945 					}
15994590Srm15945 
16004590Srm15945 					if (trace > 3) {
16014590Srm15945 						trace_prt(1,
16024590Srm15945 						    "  nfsmount: mount RPC "
16034590Srm15945 						    "failed for %s\n",
16044590Srm15945 						    host);
16054590Srm15945 					}
16064590Srm15945 					syslog(loglevel,
16074590Srm15945 					    "%s server not responding%s",
16084590Srm15945 					    host, clnt_sperror(cl, ""));
16094590Srm15945 					destroy_auth_client_handle(cl);
16104590Srm15945 					skipentry = 1;
16114590Srm15945 					mfs->mfs_ignore = 1;
16124590Srm15945 					continue;
16134590Srm15945 				}
16144590Srm15945 				if ((errno = fhs.fhs_status) != MNT_OK)  {
16154590Srm15945 
16164590Srm15945 					if (give_up_on_mnt == FALSE) {
16174590Srm15945 						got_mnt_error = TRUE;
16184590Srm15945 						goto try_mnt_slash;
16194590Srm15945 					}
16204590Srm15945 
16214590Srm15945 					free(argp);
16224590Srm15945 					head = prevhead;
16234590Srm15945 					tail = prevtail;
16244590Srm15945 					if (tail)
16254590Srm15945 						tail->nfs_ext_u.nfs_extB.next =
16264590Srm15945 						    NULL;
16274590Srm15945 					if (errno == EACCES) {
16284590Srm15945 						status = NFSERR_ACCES;
16294590Srm15945 					} else {
16304590Srm15945 						syslog(loglevel, "%s: %m",
16314590Srm15945 						    host);
16324590Srm15945 						status = NFSERR_IO;
16334590Srm15945 					}
16344590Srm15945 					if (trace > 3) {
16354590Srm15945 						trace_prt(1,
16364590Srm15945 						    "  nfsmount: mount RPC gave"
16374590Srm15945 						    " %d for %s:%s\n",
16384590Srm15945 						    errno, host, dir);
16394590Srm15945 					}
16404590Srm15945 					last_error = status;
16414590Srm15945 					destroy_auth_client_handle(cl);
16424590Srm15945 					skipentry = 1;
16434590Srm15945 					mfs->mfs_ignore = 1;
16444590Srm15945 					continue;
16454590Srm15945 				}
16464590Srm15945 				argp->fh = malloc((sizeof (fhandle)));
16474590Srm15945 				if (!argp->fh) {
16484590Srm15945 					syslog(LOG_ERR, "nfsmount: no memory");
16494590Srm15945 					last_error = NFSERR_IO;
16504590Srm15945 					destroy_auth_client_handle(cl);
16514590Srm15945 					goto out;
16524590Srm15945 				}
16534590Srm15945 				(void) memcpy(argp->fh,
16544590Srm15945 				    &fhs.fhstatus_u.fhs_fhandle,
16554590Srm15945 				    sizeof (fhandle));
16564590Srm15945 				break;
16574590Srm15945 			case MOUNTVERS3:
16584590Srm15945 				posix = 0;
16594590Srm15945 				(void) memset((char *)&res3, '\0',
16604590Srm15945 				    sizeof (res3));
16614590Srm15945 				rpc_stat = clnt_call(cl, MOUNTPROC_MNT,
16624590Srm15945 				    xdr_dirpath, (caddr_t)&dir,
16634590Srm15945 				    xdr_mountres3, (caddr_t)&res3, timeout);
16644590Srm15945 				if (rpc_stat != RPC_SUCCESS) {
16654590Srm15945 
16664590Srm15945 					if (give_up_on_mnt == FALSE) {
16674590Srm15945 						got_mnt_error = TRUE;
16684590Srm15945 						goto try_mnt_slash;
16694590Srm15945 					}
16704590Srm15945 
16714590Srm15945 				/*
16724590Srm15945 				 * Given the way "clnt_sperror" works, the "%s"
16734590Srm15945 				 * immediately following the "not responding"
16744590Srm15945 				 * is correct.
16754590Srm15945 				 */
16764590Srm15945 					free(argp);
16774590Srm15945 					head = prevhead;
16784590Srm15945 					tail = prevtail;
16794590Srm15945 					if (tail)
16804590Srm15945 						tail->nfs_ext_u.nfs_extB.next =
16814590Srm15945 						    NULL;
16824590Srm15945 					last_error = NFSERR_NOENT;
16834590Srm15945 
16844590Srm15945 					if (retries-- > 0) {
16854590Srm15945 						destroy_auth_client_handle(cl);
16864590Srm15945 						DELAY(delay);
16874590Srm15945 						goto retry;
16884590Srm15945 					}
16894590Srm15945 
16904590Srm15945 					if (trace > 3) {
16914590Srm15945 						trace_prt(1,
16924590Srm15945 						    "  nfsmount: mount RPC "
16934590Srm15945 						    "failed for %s\n",
16944590Srm15945 						    host);
16954590Srm15945 					}
16964590Srm15945 					syslog(loglevel,
16974590Srm15945 					    "%s server not responding%s",
16984590Srm15945 					    remname, clnt_sperror(cl, ""));
16994590Srm15945 					destroy_auth_client_handle(cl);
17004590Srm15945 					skipentry = 1;
17014590Srm15945 					mfs->mfs_ignore = 1;
17024590Srm15945 					continue;
17034590Srm15945 				}
17044590Srm15945 				if ((errno = res3.fhs_status) != MNT_OK)  {
17054590Srm15945 
17064590Srm15945 					if (give_up_on_mnt == FALSE) {
17074590Srm15945 						got_mnt_error = TRUE;
17084590Srm15945 						goto try_mnt_slash;
17094590Srm15945 					}
17104590Srm15945 
17114590Srm15945 					free(argp);
17124590Srm15945 					head = prevhead;
17134590Srm15945 					tail = prevtail;
17144590Srm15945 					if (tail)
17154590Srm15945 						tail->nfs_ext_u.nfs_extB.next =
17164590Srm15945 						    NULL;
17174590Srm15945 					if (errno == EACCES) {
17184590Srm15945 						status = NFSERR_ACCES;
17194590Srm15945 					} else {
17204590Srm15945 						syslog(loglevel, "%s: %m",
17214590Srm15945 						    remname);
17224590Srm15945 						status = NFSERR_IO;
17234590Srm15945 					}
17244590Srm15945 					if (trace > 3) {
17254590Srm15945 						trace_prt(1,
17264590Srm15945 						    "  nfsmount: mount RPC gave"
17274590Srm15945 						    " %d for %s:%s\n",
17284590Srm15945 						    errno, host, dir);
17294590Srm15945 					}
17304590Srm15945 					last_error = status;
17314590Srm15945 					destroy_auth_client_handle(cl);
17324590Srm15945 					skipentry = 1;
17334590Srm15945 					mfs->mfs_ignore = 1;
17344590Srm15945 					continue;
17354590Srm15945 				}
17360Sstevel@tonic-gate 
17370Sstevel@tonic-gate 			/*
17384590Srm15945 			 *  Negotiate the security flavor for nfs_mount
17390Sstevel@tonic-gate 			 */
17404590Srm15945 				auths = res3.mountres3_u.mountinfo.
17414590Srm15945 				    auth_flavors.auth_flavors_val;
17424590Srm15945 				count = res3.mountres3_u.mountinfo.
17434590Srm15945 				    auth_flavors.auth_flavors_len;
17444590Srm15945 
17454590Srm15945 				if (sec_opt) {
17464590Srm15945 					for (i = 0; i < count; i++)
17474590Srm15945 						if (auths[i] ==
17484590Srm15945 						    nfs_sec.sc_nfsnum) {
17494590Srm15945 							break;
17504590Srm15945 						}
17514590Srm15945 					if (i >= count) {
17524590Srm15945 						syslog(LOG_ERR,
17534590Srm15945 						    "%s: does not support "
17544590Srm15945 						    "security \"%s\"\n",
17554590Srm15945 						    remname, nfs_sec.sc_name);
17564590Srm15945 						clnt_freeres(cl, xdr_mountres3,
17574590Srm15945 						    (caddr_t)&res3);
17584590Srm15945 						free(argp);
17594590Srm15945 						head = prevhead;
17604590Srm15945 						tail = prevtail;
17614590Srm15945 						if (tail)
17624590Srm15945 							tail->nfs_ext_u.
17634590Srm15945 							    nfs_extB.next =
17644590Srm15945 							    NULL;
17654590Srm15945 						last_error = NFSERR_IO;
17664590Srm15945 						destroy_auth_client_handle(cl);
17674590Srm15945 						skipentry = 1;
17684590Srm15945 						mfs->mfs_ignore = 1;
17694590Srm15945 						continue;
17704590Srm15945 					}
17714590Srm15945 				} else if (count > 0) {
17724590Srm15945 					for (i = 0; i < count; i++) {
17734590Srm15945 						if (!(scerror =
17744590Srm15945 						    nfs_getseconfig_bynumber(
17754590Srm15945 						    auths[i], &nfs_sec))) {
17764590Srm15945 							sec_opt++;
17774590Srm15945 							break;
17784590Srm15945 						}
17794590Srm15945 					}
17804590Srm15945 					if (i >= count) {
17814590Srm15945 						if (nfs_syslog_scerr(scerror,
17824590Srm15945 						    scerror_msg)
17834590Srm15945 						    != -1) {
17844590Srm15945 							syslog(LOG_ERR,
17854590Srm15945 							    "%s cannot be "
17864590Srm15945 							    "mounted because it"
17874590Srm15945 							    " is shared with "
17884590Srm15945 							    "security flavor %d"
17894590Srm15945 							    " which %s",
17904590Srm15945 							    remname,
17914590Srm15945 							    auths[i-1],
17924590Srm15945 							    scerror_msg);
17934590Srm15945 						}
17944590Srm15945 						clnt_freeres(cl, xdr_mountres3,
17954590Srm15945 						    (caddr_t)&res3);
17964590Srm15945 						free(argp);
17974590Srm15945 						head = prevhead;
17984590Srm15945 						tail = prevtail;
17994590Srm15945 						if (tail)
18004590Srm15945 							tail->nfs_ext_u.
18014590Srm15945 							    nfs_extB.next =
18024590Srm15945 							    NULL;
18034590Srm15945 						last_error = NFSERR_IO;
18044590Srm15945 						destroy_auth_client_handle(cl);
18054590Srm15945 						skipentry = 1;
18064590Srm15945 						mfs->mfs_ignore = 1;
18074590Srm15945 						continue;
18084590Srm15945 						}
18094590Srm15945 				}
18104590Srm15945 
18114590Srm15945 				fh3.fh3_length =
18124590Srm15945 				    res3.mountres3_u.mountinfo.fhandle.
18134590Srm15945 				    fhandle3_len;
18144590Srm15945 				(void) memcpy(fh3.fh3_u.data,
18154590Srm15945 				    res3.mountres3_u.mountinfo.fhandle.
18164590Srm15945 				    fhandle3_val,
18174590Srm15945 				    fh3.fh3_length);
18184590Srm15945 				clnt_freeres(cl, xdr_mountres3,
18194590Srm15945 				    (caddr_t)&res3);
18204590Srm15945 				argp->fh = malloc(sizeof (nfs_fh3));
18214590Srm15945 				if (!argp->fh) {
18224590Srm15945 					syslog(LOG_ERR, "nfsmount: no memory");
18234590Srm15945 					last_error = NFSERR_IO;
18244590Srm15945 					destroy_auth_client_handle(cl);
18254590Srm15945 					goto out;
18264590Srm15945 				}
18274590Srm15945 				(void) memcpy(argp->fh, &fh3, sizeof (nfs_fh3));
18284590Srm15945 				break;
18294590Srm15945 			default:
18300Sstevel@tonic-gate 				free(argp);
18310Sstevel@tonic-gate 				head = prevhead;
18320Sstevel@tonic-gate 				tail = prevtail;
18330Sstevel@tonic-gate 				if (tail)
18340Sstevel@tonic-gate 					tail->nfs_ext_u.nfs_extB.next = NULL;
18350Sstevel@tonic-gate 				last_error = NFSERR_NOENT;
18360Sstevel@tonic-gate 				syslog(loglevel,
18374590Srm15945 				    "unknown MOUNT version %ld on %s",
18384590Srm15945 				    vers, remname);
18390Sstevel@tonic-gate 				destroy_auth_client_handle(cl);
18400Sstevel@tonic-gate 				skipentry = 1;
18410Sstevel@tonic-gate 				mfs->mfs_ignore = 1;
18420Sstevel@tonic-gate 				continue;
18434590Srm15945 			} /* switch */
18440Sstevel@tonic-gate 		}
18450Sstevel@tonic-gate 		if (nfsvers == NFS_V4) {
18460Sstevel@tonic-gate 			argp->fh = strdup(dir);
18470Sstevel@tonic-gate 			if (argp->fh == NULL) {
18480Sstevel@tonic-gate 				syslog(LOG_ERR, "nfsmount: no memory");
18490Sstevel@tonic-gate 				last_error = NFSERR_IO;
18500Sstevel@tonic-gate 				goto out;
18510Sstevel@tonic-gate 			}
18520Sstevel@tonic-gate 		}
18530Sstevel@tonic-gate 
18540Sstevel@tonic-gate 		if (trace > 4)
18550Sstevel@tonic-gate 			trace_prt(1, "	nfsmount: have %s filehandle for %s\n",
18560Sstevel@tonic-gate 			    fstype, remname);
18570Sstevel@tonic-gate 
18580Sstevel@tonic-gate 		argp->flags |= NFSMNT_NEWARGS;
18590Sstevel@tonic-gate 		argp->flags |= NFSMNT_INT;	/* default is "intr" */
18603767Sjs195444 		argp->flags |= NFSMNT_HOSTNAME;
18612170Sevanl 		argp->hostname = strdup(host);
18623767Sjs195444 		if (argp->hostname == NULL) {
18633767Sjs195444 			syslog(LOG_ERR, "nfsmount: no memory");
18643767Sjs195444 			last_error = NFSERR_IO;
18653767Sjs195444 			goto out;
18663767Sjs195444 		}
18670Sstevel@tonic-gate 
18680Sstevel@tonic-gate 		/*
18690Sstevel@tonic-gate 		 * In this case, we want NFSv4 to behave like
18700Sstevel@tonic-gate 		 * non-WebNFS so that we get the server address.
18710Sstevel@tonic-gate 		 */
18720Sstevel@tonic-gate 		if ((mfs->mfs_flags & MFS_FH_VIA_WEBNFS) == 0) {
18730Sstevel@tonic-gate 			nconf = NULL;
18740Sstevel@tonic-gate 
18750Sstevel@tonic-gate 			if (nfs_port != 0)
18760Sstevel@tonic-gate 				thisport = nfs_port;
18770Sstevel@tonic-gate 			else
18780Sstevel@tonic-gate 				thisport = mfs->mfs_port;
18790Sstevel@tonic-gate 
18800Sstevel@tonic-gate 			/*
18810Sstevel@tonic-gate 			 * For NFSv4, we want to avoid rpcbind, so call
18820Sstevel@tonic-gate 			 * get_server_stuff() directly to tell it that
18830Sstevel@tonic-gate 			 * we want to go "direct_to_server".  Otherwise,
18840Sstevel@tonic-gate 			 * do what has always been done.
18850Sstevel@tonic-gate 			 */
18860Sstevel@tonic-gate 			if (nfsvers == NFS_V4) {
18870Sstevel@tonic-gate 				enum clnt_stat cstat;
18880Sstevel@tonic-gate 				argp->addr = get_server_stuff(SERVER_ADDR,
18894590Srm15945 				    host, NFS_PROGRAM, nfsvers, NULL,
18904590Srm15945 				    &nconf, nfs_proto, thisport, NULL,
18914590Srm15945 				    NULL, TRUE, NULL, &cstat);
18920Sstevel@tonic-gate 			} else {
18930Sstevel@tonic-gate 				argp->addr = get_addr(host, NFS_PROGRAM,
18944590Srm15945 				    nfsvers, &nconf, nfs_proto,
18954590Srm15945 				    thisport, NULL);
18960Sstevel@tonic-gate 			}
18970Sstevel@tonic-gate 
18980Sstevel@tonic-gate 			if (argp->addr == NULL) {
18992170Sevanl 				if (argp->hostname)
19002170Sevanl 					free(argp->hostname);
19010Sstevel@tonic-gate 				free(argp->fh);
19020Sstevel@tonic-gate 				free(argp);
19030Sstevel@tonic-gate 				head = prevhead;
19040Sstevel@tonic-gate 				tail = prevtail;
19050Sstevel@tonic-gate 				if (tail)
19060Sstevel@tonic-gate 					tail->nfs_ext_u.nfs_extB.next = NULL;
19070Sstevel@tonic-gate 				last_error = NFSERR_NOENT;
19080Sstevel@tonic-gate 
19090Sstevel@tonic-gate 				if (retries-- > 0) {
19100Sstevel@tonic-gate 					destroy_auth_client_handle(cl);
19110Sstevel@tonic-gate 					DELAY(delay);
19120Sstevel@tonic-gate 					goto retry;
19130Sstevel@tonic-gate 				}
19140Sstevel@tonic-gate 
19150Sstevel@tonic-gate 				syslog(loglevel, "%s: no NFS service", host);
19160Sstevel@tonic-gate 				destroy_auth_client_handle(cl);
19170Sstevel@tonic-gate 				skipentry = 1;
19180Sstevel@tonic-gate 				mfs->mfs_ignore = 1;
19190Sstevel@tonic-gate 				continue;
19200Sstevel@tonic-gate 			}
19210Sstevel@tonic-gate 			if (trace > 4)
19220Sstevel@tonic-gate 				trace_prt(1,
19230Sstevel@tonic-gate 				    "\tnfsmount: have net address for %s\n",
19240Sstevel@tonic-gate 				    remname);
19250Sstevel@tonic-gate 
19260Sstevel@tonic-gate 		} else {
19270Sstevel@tonic-gate 			nconf = mfs->mfs_nconf;
19280Sstevel@tonic-gate 			mfs->mfs_nconf = NULL;
19290Sstevel@tonic-gate 		}
19300Sstevel@tonic-gate 
19310Sstevel@tonic-gate 		argp->flags |= NFSMNT_KNCONF;
19320Sstevel@tonic-gate 		argp->knconf = get_knconf(nconf);
19330Sstevel@tonic-gate 		if (argp->knconf == NULL) {
19340Sstevel@tonic-gate 			netbuf_free(argp->addr);
19350Sstevel@tonic-gate 			freenetconfigent(nconf);
19362170Sevanl 			if (argp->hostname)
19372170Sevanl 				free(argp->hostname);
19380Sstevel@tonic-gate 			free(argp->fh);
19390Sstevel@tonic-gate 			free(argp);
19400Sstevel@tonic-gate 			head = prevhead;
19410Sstevel@tonic-gate 			tail = prevtail;
19420Sstevel@tonic-gate 			if (tail)
19430Sstevel@tonic-gate 				tail->nfs_ext_u.nfs_extB.next = NULL;
19440Sstevel@tonic-gate 			last_error = NFSERR_NOSPC;
19450Sstevel@tonic-gate 			destroy_auth_client_handle(cl);
19460Sstevel@tonic-gate 			skipentry = 1;
19470Sstevel@tonic-gate 			mfs->mfs_ignore = 1;
19480Sstevel@tonic-gate 			continue;
19490Sstevel@tonic-gate 		}
19500Sstevel@tonic-gate 		if (trace > 4)
19510Sstevel@tonic-gate 			trace_prt(1,
19520Sstevel@tonic-gate 			    "\tnfsmount: have net config for %s\n",
19530Sstevel@tonic-gate 			    remname);
19540Sstevel@tonic-gate 
19550Sstevel@tonic-gate 		if (hasmntopt(&m, MNTOPT_SOFT) != NULL) {
19560Sstevel@tonic-gate 			argp->flags |= NFSMNT_SOFT;
19570Sstevel@tonic-gate 		}
19580Sstevel@tonic-gate 		if (hasmntopt(&m, MNTOPT_NOINTR) != NULL) {
19590Sstevel@tonic-gate 			argp->flags &= ~(NFSMNT_INT);
19600Sstevel@tonic-gate 		}
19610Sstevel@tonic-gate 		if (hasmntopt(&m, MNTOPT_NOAC) != NULL) {
19620Sstevel@tonic-gate 			argp->flags |= NFSMNT_NOAC;
19630Sstevel@tonic-gate 		}
19640Sstevel@tonic-gate 		if (hasmntopt(&m, MNTOPT_NOCTO) != NULL) {
19650Sstevel@tonic-gate 			argp->flags |= NFSMNT_NOCTO;
19660Sstevel@tonic-gate 		}
19670Sstevel@tonic-gate 		if (hasmntopt(&m, MNTOPT_FORCEDIRECTIO) != NULL) {
19680Sstevel@tonic-gate 			argp->flags |= NFSMNT_DIRECTIO;
19690Sstevel@tonic-gate 		}
19700Sstevel@tonic-gate 		if (hasmntopt(&m, MNTOPT_NOFORCEDIRECTIO) != NULL) {
19710Sstevel@tonic-gate 			argp->flags &= ~(NFSMNT_DIRECTIO);
19720Sstevel@tonic-gate 		}
19730Sstevel@tonic-gate 
19740Sstevel@tonic-gate 		/*
19750Sstevel@tonic-gate 		 * Set up security data for argp->nfs_ext_u.nfs_extB.secdata.
19760Sstevel@tonic-gate 		 */
19770Sstevel@tonic-gate 		if (mfssnego.snego_done) {
19780Sstevel@tonic-gate 			memcpy(&nfs_sec, &mfssnego.nfs_sec,
19794590Srm15945 			    sizeof (seconfig_t));
19800Sstevel@tonic-gate 		} else if (!sec_opt) {
19810Sstevel@tonic-gate 			/*
19820Sstevel@tonic-gate 			 * Get default security mode.
19830Sstevel@tonic-gate 			 */
19840Sstevel@tonic-gate 			if (nfs_getseconfig_default(&nfs_sec)) {
19850Sstevel@tonic-gate 				syslog(loglevel,
19860Sstevel@tonic-gate 				    "error getting default security entry\n");
19870Sstevel@tonic-gate 				free_knconf(argp->knconf);
19880Sstevel@tonic-gate 				netbuf_free(argp->addr);
19890Sstevel@tonic-gate 				freenetconfigent(nconf);
19902170Sevanl 				if (argp->hostname)
19912170Sevanl 					free(argp->hostname);
19920Sstevel@tonic-gate 				free(argp->fh);
19930Sstevel@tonic-gate 				free(argp);
19940Sstevel@tonic-gate 				head = prevhead;
19950Sstevel@tonic-gate 				tail = prevtail;
19960Sstevel@tonic-gate 				if (tail)
19970Sstevel@tonic-gate 					tail->nfs_ext_u.nfs_extB.next = NULL;
19980Sstevel@tonic-gate 				last_error = NFSERR_NOSPC;
19990Sstevel@tonic-gate 				destroy_auth_client_handle(cl);
20000Sstevel@tonic-gate 				skipentry = 1;
20010Sstevel@tonic-gate 				mfs->mfs_ignore = 1;
20020Sstevel@tonic-gate 				continue;
20030Sstevel@tonic-gate 			}
20040Sstevel@tonic-gate 			argp->flags |= NFSMNT_SECDEFAULT;
20050Sstevel@tonic-gate 		}
20060Sstevel@tonic-gate 
20070Sstevel@tonic-gate 		/*
20080Sstevel@tonic-gate 		 * For AUTH_DH
20090Sstevel@tonic-gate 		 * get the network address for the time service on
20100Sstevel@tonic-gate 		 * the server.	If an RPC based time service is
20110Sstevel@tonic-gate 		 * not available then try the IP time service.
20120Sstevel@tonic-gate 		 *
20130Sstevel@tonic-gate 		 * Eventurally, we want to move this code to nfs_clnt_secdata()
20140Sstevel@tonic-gate 		 * when autod_nfs.c and mount.c can share the same
20150Sstevel@tonic-gate 		 * get_the_addr/get_the_stuff routine.
20160Sstevel@tonic-gate 		 */
20170Sstevel@tonic-gate 		secflags = 0;
20180Sstevel@tonic-gate 		syncaddr = NULL;
20190Sstevel@tonic-gate 		retaddrs = NULL;
20200Sstevel@tonic-gate 
20210Sstevel@tonic-gate 		if (nfs_sec.sc_rpcnum == AUTH_DH || nfsvers == NFS_V4) {
20220Sstevel@tonic-gate 		/*
20230Sstevel@tonic-gate 		 * If not using the public fh and not NFS_V4, we can try
20240Sstevel@tonic-gate 		 * talking RPCBIND. Otherwise, assume that firewalls
20250Sstevel@tonic-gate 		 * prevent us from doing that.
20260Sstevel@tonic-gate 		 */
20274590Srm15945 			if ((mfs->mfs_flags & MFS_FH_VIA_WEBNFS) == 0 &&
20284590Srm15945 			    nfsvers != NFS_V4) {
20290Sstevel@tonic-gate 			syncaddr = get_the_stuff(SERVER_ADDR, host, RPCBPROG,
20304590Srm15945 			    RPCBVERS, NULL, nconf, 0, NULL, NULL, FALSE,
20314590Srm15945 			    NULL, NULL);
20320Sstevel@tonic-gate 			}
20330Sstevel@tonic-gate 
20344590Srm15945 			if (syncaddr != NULL) {
20354590Srm15945 				/* for flags in sec_data */
20364590Srm15945 				secflags |= AUTH_F_RPCTIMESYNC;
20374590Srm15945 			} else {
20384590Srm15945 				struct nd_hostserv hs;
20394590Srm15945 				int error;
20404590Srm15945 
20414590Srm15945 				hs.h_host = host;
20424590Srm15945 				hs.h_serv = "timserver";
20434590Srm15945 				error = netdir_getbyname(nconf, &hs, &retaddrs);
20444590Srm15945 
20454590Srm15945 				if (error != ND_OK &&
20464590Srm15945 				    nfs_sec.sc_rpcnum == AUTH_DH) {
20474590Srm15945 					syslog(loglevel,
20484590Srm15945 					    "%s: secure: no time service\n",
20494590Srm15945 					    host);
20504590Srm15945 					free_knconf(argp->knconf);
20514590Srm15945 					netbuf_free(argp->addr);
20524590Srm15945 					freenetconfigent(nconf);
20534590Srm15945 					if (argp->hostname)
20544590Srm15945 						free(argp->hostname);
20554590Srm15945 					free(argp->fh);
20564590Srm15945 					free(argp);
20574590Srm15945 					head = prevhead;
20584590Srm15945 					tail = prevtail;
20594590Srm15945 					if (tail)
20604590Srm15945 						tail->nfs_ext_u.nfs_extB.next =
20614590Srm15945 						    NULL;
20624590Srm15945 					last_error = NFSERR_IO;
20634590Srm15945 					destroy_auth_client_handle(cl);
20644590Srm15945 					skipentry = 1;
20654590Srm15945 					mfs->mfs_ignore = 1;
20664590Srm15945 					continue;
20674590Srm15945 				}
20684590Srm15945 
20694590Srm15945 				if (error == ND_OK)
20704590Srm15945 					syncaddr = retaddrs->n_addrs;
20710Sstevel@tonic-gate 
20720Sstevel@tonic-gate 			/*
20730Sstevel@tonic-gate 			 * For potential usage by NFS V4 when AUTH_DH
20740Sstevel@tonic-gate 			 * is negotiated via SECINFO in the kernel.
20750Sstevel@tonic-gate 			 */
20764590Srm15945 				if (nfsvers == NFS_V4 && syncaddr &&
20770Sstevel@tonic-gate 				    host2netname(netname, host, NULL)) {
20784590Srm15945 					argp->syncaddr =
20794590Srm15945 					    malloc(sizeof (struct netbuf));
20804590Srm15945 					argp->syncaddr->buf =
20814590Srm15945 					    malloc(syncaddr->len);
20824590Srm15945 					(void) memcpy(argp->syncaddr->buf,
20834590Srm15945 					    syncaddr->buf, syncaddr->len);
20844590Srm15945 					argp->syncaddr->len = syncaddr->len;
20854590Srm15945 					argp->syncaddr->maxlen =
20864590Srm15945 					    syncaddr->maxlen;
20874590Srm15945 					argp->netname = strdup(netname);
20884590Srm15945 					argp->flags |= NFSMNT_SECURE;
20894590Srm15945 				}
20904590Srm15945 			} /* syncaddr */
20910Sstevel@tonic-gate 		} /* AUTH_DH */
20920Sstevel@tonic-gate 
20931676Sjpk 		/*
20941676Sjpk 		 * TSOL notes: automountd in tsol extension
20951676Sjpk 		 * has "read down" capability, i.e. we allow
20961676Sjpk 		 * a user to trigger an nfs mount into a lower
20971676Sjpk 		 * labeled zone. We achieve this by always having
20981676Sjpk 		 * root issue the mount request so that the
20991676Sjpk 		 * lookup ops can go past /zone/<zone_name>
21001676Sjpk 		 * on the server side.
21011676Sjpk 		 */
21021676Sjpk 		if (is_system_labeled())
21031676Sjpk 			nfs_sec.sc_uid = (uid_t)0;
21041676Sjpk 		else
21053391Ssemery 			nfs_sec.sc_uid = uid;
21060Sstevel@tonic-gate 		/*
21070Sstevel@tonic-gate 		 * If AUTH_DH is a chosen flavor now, its data will be stored
21080Sstevel@tonic-gate 		 * in the sec_data structure via nfs_clnt_secdata().
21090Sstevel@tonic-gate 		 */
21100Sstevel@tonic-gate 		if (!(secdata = nfs_clnt_secdata(&nfs_sec, host, argp->knconf,
21114590Srm15945 		    syncaddr, secflags))) {
21120Sstevel@tonic-gate 			syslog(LOG_ERR,
21134590Srm15945 			    "errors constructing security related data\n");
21140Sstevel@tonic-gate 			if (secflags & AUTH_F_RPCTIMESYNC)
21150Sstevel@tonic-gate 				netbuf_free(syncaddr);
21160Sstevel@tonic-gate 			else if (retaddrs)
21170Sstevel@tonic-gate 				netdir_free(retaddrs, ND_ADDRLIST);
21180Sstevel@tonic-gate 			if (argp->syncaddr)
21190Sstevel@tonic-gate 				netbuf_free(argp->syncaddr);
21200Sstevel@tonic-gate 			if (argp->netname)
21210Sstevel@tonic-gate 				free(argp->netname);
21222170Sevanl 			if (argp->hostname)
21232170Sevanl 				free(argp->hostname);
21240Sstevel@tonic-gate 			free_knconf(argp->knconf);
21250Sstevel@tonic-gate 			netbuf_free(argp->addr);
21260Sstevel@tonic-gate 			freenetconfigent(nconf);
21270Sstevel@tonic-gate 			free(argp->fh);
21280Sstevel@tonic-gate 			free(argp);
21290Sstevel@tonic-gate 			head = prevhead;
21300Sstevel@tonic-gate 			tail = prevtail;
21310Sstevel@tonic-gate 			if (tail)
21320Sstevel@tonic-gate 				tail->nfs_ext_u.nfs_extB.next = NULL;
21330Sstevel@tonic-gate 			last_error = NFSERR_IO;
21340Sstevel@tonic-gate 			destroy_auth_client_handle(cl);
21350Sstevel@tonic-gate 			skipentry = 1;
21360Sstevel@tonic-gate 			mfs->mfs_ignore = 1;
21370Sstevel@tonic-gate 			continue;
21380Sstevel@tonic-gate 		}
21390Sstevel@tonic-gate 		NFS_ARGS_EXTB_secdata(*argp, secdata);
21400Sstevel@tonic-gate 		/* end of security stuff */
21410Sstevel@tonic-gate 
21420Sstevel@tonic-gate 		if (trace > 4)
21430Sstevel@tonic-gate 			trace_prt(1,
21440Sstevel@tonic-gate 			    "  nfsmount: have secure info for %s\n", remname);
21450Sstevel@tonic-gate 
21460Sstevel@tonic-gate 		if (hasmntopt(&m, MNTOPT_GRPID) != NULL) {
21470Sstevel@tonic-gate 			argp->flags |= NFSMNT_GRPID;
21480Sstevel@tonic-gate 		}
21490Sstevel@tonic-gate 		if (nopt(&m, MNTOPT_RSIZE, &argp->rsize)) {
21500Sstevel@tonic-gate 			argp->flags |= NFSMNT_RSIZE;
21510Sstevel@tonic-gate 		}
21520Sstevel@tonic-gate 		if (nopt(&m, MNTOPT_WSIZE, &argp->wsize)) {
21530Sstevel@tonic-gate 			argp->flags |= NFSMNT_WSIZE;
21540Sstevel@tonic-gate 		}
21550Sstevel@tonic-gate 		if (nopt(&m, MNTOPT_TIMEO, &argp->timeo)) {
21560Sstevel@tonic-gate 			argp->flags |= NFSMNT_TIMEO;
21570Sstevel@tonic-gate 		}
21580Sstevel@tonic-gate 		if (nopt(&m, MNTOPT_RETRANS, &argp->retrans)) {
21590Sstevel@tonic-gate 			argp->flags |= NFSMNT_RETRANS;
21600Sstevel@tonic-gate 		}
21610Sstevel@tonic-gate 		if (nopt(&m, MNTOPT_ACTIMEO, &argp->acregmax)) {
21620Sstevel@tonic-gate 			argp->flags |= NFSMNT_ACREGMAX;
21630Sstevel@tonic-gate 			argp->flags |= NFSMNT_ACDIRMAX;
21640Sstevel@tonic-gate 			argp->flags |= NFSMNT_ACDIRMIN;
21650Sstevel@tonic-gate 			argp->flags |= NFSMNT_ACREGMIN;
21660Sstevel@tonic-gate 			argp->acdirmin = argp->acregmin = argp->acdirmax
21674590Srm15945 			    = argp->acregmax;
21680Sstevel@tonic-gate 		} else {
21690Sstevel@tonic-gate 			if (nopt(&m, MNTOPT_ACREGMIN, &argp->acregmin)) {
21700Sstevel@tonic-gate 				argp->flags |= NFSMNT_ACREGMIN;
21710Sstevel@tonic-gate 			}
21720Sstevel@tonic-gate 			if (nopt(&m, MNTOPT_ACREGMAX, &argp->acregmax)) {
21730Sstevel@tonic-gate 				argp->flags |= NFSMNT_ACREGMAX;
21740Sstevel@tonic-gate 			}
21750Sstevel@tonic-gate 			if (nopt(&m, MNTOPT_ACDIRMIN, &argp->acdirmin)) {
21760Sstevel@tonic-gate 				argp->flags |= NFSMNT_ACDIRMIN;
21770Sstevel@tonic-gate 			}
21780Sstevel@tonic-gate 			if (nopt(&m, MNTOPT_ACDIRMAX, &argp->acdirmax)) {
21790Sstevel@tonic-gate 				argp->flags |= NFSMNT_ACDIRMAX;
21800Sstevel@tonic-gate 			}
21810Sstevel@tonic-gate 		}
21820Sstevel@tonic-gate 
21830Sstevel@tonic-gate 		if (posix) {
21840Sstevel@tonic-gate 			argp->pathconf = NULL;
21850Sstevel@tonic-gate 			if (error = get_pathconf(cl, dir, remname,
21860Sstevel@tonic-gate 			    &argp->pathconf, retries)) {
21870Sstevel@tonic-gate 				if (secflags & AUTH_F_RPCTIMESYNC)
21880Sstevel@tonic-gate 					netbuf_free(syncaddr);
21890Sstevel@tonic-gate 				else if (retaddrs)
21900Sstevel@tonic-gate 					netdir_free(retaddrs, ND_ADDRLIST);
21910Sstevel@tonic-gate 				free_knconf(argp->knconf);
21920Sstevel@tonic-gate 				netbuf_free(argp->addr);
21930Sstevel@tonic-gate 				freenetconfigent(nconf);
21940Sstevel@tonic-gate 				nfs_free_secdata(
21954590Srm15945 				    argp->nfs_ext_u.nfs_extB.secdata);
21960Sstevel@tonic-gate 				if (argp->syncaddr)
21970Sstevel@tonic-gate 					netbuf_free(argp->syncaddr);
21980Sstevel@tonic-gate 				if (argp->netname)
21990Sstevel@tonic-gate 					free(argp->netname);
22002170Sevanl 				if (argp->hostname)
22012170Sevanl 					free(argp->hostname);
22020Sstevel@tonic-gate 				free(argp->fh);
22030Sstevel@tonic-gate 				free(argp);
22040Sstevel@tonic-gate 				head = prevhead;
22050Sstevel@tonic-gate 				tail = prevtail;
22060Sstevel@tonic-gate 				if (tail)
22070Sstevel@tonic-gate 					tail->nfs_ext_u.nfs_extB.next = NULL;
22080Sstevel@tonic-gate 				last_error = NFSERR_IO;
22090Sstevel@tonic-gate 
22100Sstevel@tonic-gate 				if (error == RET_RETRY && retries-- > 0) {
22110Sstevel@tonic-gate 					destroy_auth_client_handle(cl);
22120Sstevel@tonic-gate 					DELAY(delay);
22130Sstevel@tonic-gate 					goto retry;
22140Sstevel@tonic-gate 				}
22150Sstevel@tonic-gate 
22160Sstevel@tonic-gate 				destroy_auth_client_handle(cl);
22170Sstevel@tonic-gate 				skipentry = 1;
22180Sstevel@tonic-gate 				mfs->mfs_ignore = 1;
22190Sstevel@tonic-gate 				continue;
22200Sstevel@tonic-gate 			}
22210Sstevel@tonic-gate 			argp->flags |= NFSMNT_POSIX;
22220Sstevel@tonic-gate 			if (trace > 4)
22230Sstevel@tonic-gate 				trace_prt(1,
22240Sstevel@tonic-gate 				    "  nfsmount: have pathconf for %s\n",
22250Sstevel@tonic-gate 				    remname);
22260Sstevel@tonic-gate 		}
22270Sstevel@tonic-gate 
22280Sstevel@tonic-gate 		/*
22290Sstevel@tonic-gate 		 * free loop-specific data structures
22300Sstevel@tonic-gate 		 */
22310Sstevel@tonic-gate 		destroy_auth_client_handle(cl);
22320Sstevel@tonic-gate 		freenetconfigent(nconf);
22330Sstevel@tonic-gate 		if (secflags & AUTH_F_RPCTIMESYNC)
22340Sstevel@tonic-gate 			netbuf_free(syncaddr);
22350Sstevel@tonic-gate 		else if (retaddrs)
22360Sstevel@tonic-gate 			netdir_free(retaddrs, ND_ADDRLIST);
22370Sstevel@tonic-gate 
22380Sstevel@tonic-gate 		/*
22390Sstevel@tonic-gate 		 * Decide whether to use remote host's lockd or local locking.
22400Sstevel@tonic-gate 		 * If we are using the public fh, we've already turned
22410Sstevel@tonic-gate 		 * LLOCK on.
22420Sstevel@tonic-gate 		 */
22430Sstevel@tonic-gate 		if (hasmntopt(&m, MNTOPT_LLOCK))
22440Sstevel@tonic-gate 			argp->flags |= NFSMNT_LLOCK;
22450Sstevel@tonic-gate 		if (!(argp->flags & NFSMNT_LLOCK) && nfsvers == NFS_VERSION &&
22464590Srm15945 		    remote_lock(host, argp->fh)) {
22470Sstevel@tonic-gate 			syslog(loglevel, "No network locking on %s : "
22480Sstevel@tonic-gate 			"contact admin to install server change", host);
22490Sstevel@tonic-gate 			argp->flags |= NFSMNT_LLOCK;
22500Sstevel@tonic-gate 		}
22510Sstevel@tonic-gate 
22520Sstevel@tonic-gate 		/*
22530Sstevel@tonic-gate 		 * Build a string for /etc/mnttab.
22540Sstevel@tonic-gate 		 * If possible, coalesce strings with same 'dir' info.
22550Sstevel@tonic-gate 		 */
22560Sstevel@tonic-gate 		if ((mfs->mfs_flags & MFS_URL) == 0) {
22570Sstevel@tonic-gate 			char *tmp;
22580Sstevel@tonic-gate 
22590Sstevel@tonic-gate 			if (mnttabcnt) {
22600Sstevel@tonic-gate 				p = strrchr(mnttabtext, (int)':');
22610Sstevel@tonic-gate 				if (!p || strcmp(p+1, dir) != 0) {
22620Sstevel@tonic-gate 					mnttabcnt += strlen(remname) + 2;
22630Sstevel@tonic-gate 				} else {
22640Sstevel@tonic-gate 					*p = '\0';
22653767Sjs195444 					mnttabcnt += strlen(rhost) + 2;
22660Sstevel@tonic-gate 				}
22670Sstevel@tonic-gate 				if ((tmp = realloc(mnttabtext,
22680Sstevel@tonic-gate 				    mnttabcnt)) != NULL) {
22690Sstevel@tonic-gate 					mnttabtext = tmp;
22700Sstevel@tonic-gate 					strcat(mnttabtext, ",");
22710Sstevel@tonic-gate 				} else {
22720Sstevel@tonic-gate 					free(mnttabtext);
22730Sstevel@tonic-gate 					mnttabtext = NULL;
22740Sstevel@tonic-gate 				}
22750Sstevel@tonic-gate 			} else {
22760Sstevel@tonic-gate 				mnttabcnt = strlen(remname) + 1;
22770Sstevel@tonic-gate 				if ((mnttabtext = malloc(mnttabcnt)) != NULL)
22780Sstevel@tonic-gate 					mnttabtext[0] = '\0';
22790Sstevel@tonic-gate 			}
22800Sstevel@tonic-gate 
22810Sstevel@tonic-gate 			if (mnttabtext != NULL)
22820Sstevel@tonic-gate 				strcat(mnttabtext, remname);
22830Sstevel@tonic-gate 
22840Sstevel@tonic-gate 		} else {
22850Sstevel@tonic-gate 			char *tmp;
22860Sstevel@tonic-gate 			int more_cnt = 0;
22870Sstevel@tonic-gate 			char sport[16];
22880Sstevel@tonic-gate 
22890Sstevel@tonic-gate 			more_cnt += strlen("nfs://");
22900Sstevel@tonic-gate 			more_cnt += strlen(mfs->mfs_host);
22910Sstevel@tonic-gate 
22920Sstevel@tonic-gate 			if (mfs->mfs_port != 0) {
22930Sstevel@tonic-gate 				(void) sprintf(sport, ":%u", mfs->mfs_port);
22940Sstevel@tonic-gate 			} else
22950Sstevel@tonic-gate 				sport[0] = '\0';
22960Sstevel@tonic-gate 
22970Sstevel@tonic-gate 			more_cnt += strlen(sport);
22980Sstevel@tonic-gate 			more_cnt += 1; /* "/" */
22990Sstevel@tonic-gate 			more_cnt += strlen(mfs->mfs_dir);
23000Sstevel@tonic-gate 
23010Sstevel@tonic-gate 			if (mnttabcnt) {
23020Sstevel@tonic-gate 				more_cnt += 1; /* "," */
23030Sstevel@tonic-gate 				mnttabcnt += more_cnt;
23040Sstevel@tonic-gate 
23050Sstevel@tonic-gate 				if ((tmp = realloc(mnttabtext,
23060Sstevel@tonic-gate 				    mnttabcnt)) != NULL) {
23070Sstevel@tonic-gate 					mnttabtext = tmp;
23080Sstevel@tonic-gate 					strcat(mnttabtext, ",");
23090Sstevel@tonic-gate 				} else {
23100Sstevel@tonic-gate 					free(mnttabtext);
23110Sstevel@tonic-gate 					mnttabtext = NULL;
23120Sstevel@tonic-gate 				}
23130Sstevel@tonic-gate 			} else {
23140Sstevel@tonic-gate 				mnttabcnt = more_cnt + 1;
23150Sstevel@tonic-gate 				if ((mnttabtext = malloc(mnttabcnt)) != NULL)
23160Sstevel@tonic-gate 					mnttabtext[0] = '\0';
23170Sstevel@tonic-gate 			}
23180Sstevel@tonic-gate 
23190Sstevel@tonic-gate 			if (mnttabtext != NULL) {
23200Sstevel@tonic-gate 				strcat(mnttabtext, "nfs://");
23210Sstevel@tonic-gate 				strcat(mnttabtext, mfs->mfs_host);
23220Sstevel@tonic-gate 				strcat(mnttabtext, sport);
23230Sstevel@tonic-gate 				strcat(mnttabtext, "/");
23240Sstevel@tonic-gate 				strcat(mnttabtext, mfs->mfs_dir);
23250Sstevel@tonic-gate 			}
23260Sstevel@tonic-gate 		}
23270Sstevel@tonic-gate 
23280Sstevel@tonic-gate 		if (!mnttabtext) {
23290Sstevel@tonic-gate 			syslog(LOG_ERR, "nfsmount: no memory");
23300Sstevel@tonic-gate 			last_error = NFSERR_IO;
23310Sstevel@tonic-gate 			goto out;
23320Sstevel@tonic-gate 		}
23330Sstevel@tonic-gate 
23340Sstevel@tonic-gate 		/*
23350Sstevel@tonic-gate 		 * At least one entry, can call mount(2).
23360Sstevel@tonic-gate 		 */
23370Sstevel@tonic-gate 		entries++;
23380Sstevel@tonic-gate 
23390Sstevel@tonic-gate 		/*
23400Sstevel@tonic-gate 		 * If replication was defeated, don't do more work
23410Sstevel@tonic-gate 		 */
23420Sstevel@tonic-gate 		if (!replicated)
23430Sstevel@tonic-gate 			break;
23440Sstevel@tonic-gate 	}
23450Sstevel@tonic-gate 
23460Sstevel@tonic-gate 
23470Sstevel@tonic-gate 	/*
23480Sstevel@tonic-gate 	 * Did we get through all possibilities without success?
23490Sstevel@tonic-gate 	 */
23500Sstevel@tonic-gate 	if (!entries)
23510Sstevel@tonic-gate 		goto out;
23520Sstevel@tonic-gate 
23530Sstevel@tonic-gate 	/* Make "xattr" the default if "noxattr" is not specified. */
23540Sstevel@tonic-gate 	strcpy(mopts, opts);
23550Sstevel@tonic-gate 	if (!hasmntopt(&m, MNTOPT_NOXATTR) && !hasmntopt(&m, MNTOPT_XATTR)) {
23560Sstevel@tonic-gate 		if (strlen(mopts) > 0)
23570Sstevel@tonic-gate 			strcat(mopts, ",");
23580Sstevel@tonic-gate 		strcat(mopts, "xattr");
23590Sstevel@tonic-gate 	}
23600Sstevel@tonic-gate 
23610Sstevel@tonic-gate 	/*
23620Sstevel@tonic-gate 	 * enable services as needed.
23630Sstevel@tonic-gate 	 */
23640Sstevel@tonic-gate 	{
23650Sstevel@tonic-gate 		char **sl;
23660Sstevel@tonic-gate 
23670Sstevel@tonic-gate 		if (strcmp(fstype, MNTTYPE_NFS4) == 0)
23680Sstevel@tonic-gate 			sl = service_list_v4;
23690Sstevel@tonic-gate 		else
23700Sstevel@tonic-gate 			sl = service_list;
23710Sstevel@tonic-gate 
23721676Sjpk 		(void) _check_services(sl);
23730Sstevel@tonic-gate 	}
23740Sstevel@tonic-gate 
23750Sstevel@tonic-gate 	/*
23760Sstevel@tonic-gate 	 * Whew; do the mount, at last.
23770Sstevel@tonic-gate 	 */
23780Sstevel@tonic-gate 	if (trace > 1) {
23790Sstevel@tonic-gate 		trace_prt(1, "	mount %s %s (%s)\n", mnttabtext, mntpnt, mopts);
23800Sstevel@tonic-gate 	}
23810Sstevel@tonic-gate 
23822170Sevanl 	/*
2383*5302Sth199096 	 * About to do a nfs mount, make sure the mount_to is set for
2384*5302Sth199096 	 * potential ephemeral mounts with NFSv4.
2385*5302Sth199096 	 */
2386*5302Sth199096 	set_nfsv4_ephemeral_mount_to();
2387*5302Sth199096 
2388*5302Sth199096 	/*
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,
23954590Srm15945 		    head, sizeof (*head), mopts, MAX_MNTOPT_STR) < 0) {
23962170Sevanl 			if (trace > 1)
23972170Sevanl 				trace_prt(1, "	Mount of %s on %s: %d\n",
23984590Srm15945 				    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",
24104590Srm15945 				    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 =
24224590Srm15945 		    strdup(mnttabtext);
24232170Sevanl 		alp->action.action_list_entry_u.mounta.dir = strdup(mntpnt);
24242170Sevanl 		alp->action.action_list_entry_u.mounta.flags =
24254590Srm15945 		    flags | MS_DATA;
24262170Sevanl 		alp->action.action_list_entry_u.mounta.fstype =
24274590Srm15945 		    strdup(fstype);
24282170Sevanl 		alp->action.action_list_entry_u.mounta.dataptr = (char *)head;
24292170Sevanl 		alp->action.action_list_entry_u.mounta.datalen =
24304590Srm15945 		    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 =
24364590Srm15945 		    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:
31034590Srm15945 			    tv.tv_sec = 10;
31044590Srm15945 			    tv.tv_usec = 0;
31054590Srm15945 			    cs = clnt_call(cl, NULLPROC, xdr_void, 0,
31064590Srm15945 				xdr_void, 0, tv);
31074590Srm15945 			    if (cs != RPC_SUCCESS)
31084590Srm15945 				    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,
32624590Srm15945 	    mfssnego, nconfp, proto, port, tinfo, fhp, get_pubfh, fspath,
32634590Srm15945 	    &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,
32734590Srm15945 	    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,
33014590Srm15945 		    mfssnego, *nconfp, port, tinfo, fhp, direct_to_server,
33024590Srm15945 		    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,
33394590Srm15945 			    mfssnego, nconf, port, tinfo, fhp,
33404590Srm15945 			    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) {
33564590Srm15945 				if (nthtry == FIRST_TRY) {
33574590Srm15945 					if ((nconf->nc_semantics ==
33584590Srm15945 					    NC_TPI_COTS_ORD) ||
33594590Srm15945 					    (nconf->nc_semantics ==
33604590Srm15945 					    NC_TPI_COTS)) {
33614590Srm15945 						if (port == 0)
33624590Srm15945 							break;
33634590Srm15945 						if ((strcmp(nconf->nc_protofmly,
33644590Srm15945 						    NC_INET) == 0 ||
33654590Srm15945 						    strcmp(nconf->nc_protofmly,
33664590Srm15945 						    NC_INET6) == 0) &&
33674590Srm15945 						    (strcmp(nconf->nc_proto,
33684590Srm15945 						    NC_TCP) == 0))
33694590Srm15945 							break;
33704590Srm15945 					}
33710Sstevel@tonic-gate 				}
33724590Srm15945 				if (nthtry == SECOND_TRY) {
33734590Srm15945 					if (nconf->nc_semantics ==
33744590Srm15945 					    NC_TPI_CLTS) {
33754590Srm15945 						if (port == 0)
33764590Srm15945 							break;
33774590Srm15945 						if ((strcmp(nconf->nc_protofmly,
33784590Srm15945 						    NC_INET) == 0 ||
33794590Srm15945 						    strcmp(nconf->nc_protofmly,
33804590Srm15945 						    NC_INET6) == 0) &&
33814590Srm15945 						    (strcmp(nconf->nc_proto,
33824590Srm15945 						    NC_UDP) == 0))
33834590Srm15945 							break;
33844590Srm15945 					}
33850Sstevel@tonic-gate 				}
33860Sstevel@tonic-gate 			}
33874590Srm15945 		} /* while */
33884590Srm15945 		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;
33964590Srm15945 		} 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;
34074590Srm15945 		}
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",
35754590Srm15945 		    hostname, rpc_to_new.tv_sec, versmax, versmin);
35760Sstevel@tonic-gate 
35770Sstevel@tonic-gate 	if (usepub == FALSE) {
35784590Srm15945 		do {
35794590Srm15945 			/*
35804590Srm15945 			 * If NFSv4, then we do the same thing as is used
35814590Srm15945 			 * for public filehandles so that we avoid rpcbind
35824590Srm15945 			 */
35834590Srm15945 			if (vers_to_try == NFS_V4) {
35844590Srm15945 				if (trace > 4) {
35850Sstevel@tonic-gate 				trace_prt(1, "  pingnfs: Trying ping via "
35864590Srm15945 				    "\"circuit_v\"\n");
35870Sstevel@tonic-gate 				}
35880Sstevel@tonic-gate 
35894590Srm15945 				cl = clnt_create_service_timed(hostname, "nfs",
35904590Srm15945 				    NFS_PROGRAM, vers_to_try,
35914590Srm15945 				    port, "circuit_v", &rpc_to_new);
35924590Srm15945 				if (cl != NULL) {
35934590Srm15945 					outvers = vers_to_try;
35944590Srm15945 					break;
35954590Srm15945 				}
35960Sstevel@tonic-gate 				if (trace > 4) {
35974590Srm15945 					trace_prt(1,
35984590Srm15945 					    "  pingnfs: Can't ping via "
35994590Srm15945 					    "\"circuit_v\" %s: RPC error=%d\n",
36004590Srm15945 					    hostname, rpc_createerr.cf_stat);
36010Sstevel@tonic-gate 				}
36024590Srm15945 
36034590Srm15945 			} else {
36044590Srm15945 				cl = clnt_create_vers_timed(hostname,
36054590Srm15945 				    NFS_PROGRAM, &outvers, versmin, vers_to_try,
36064590Srm15945 				    "datagram_v", &rpc_to_new);
36074590Srm15945 				if (cl != NULL)
36080Sstevel@tonic-gate 					break;
36090Sstevel@tonic-gate 				if (trace > 4) {
36104590Srm15945 					trace_prt(1,
36114590Srm15945 					    "  pingnfs: Can't ping via "
36124590Srm15945 					    "\"datagram_v\"%s: RPC error=%d\n",
36134590Srm15945 					    hostname, rpc_createerr.cf_stat);
36144590Srm15945 				}
36154590Srm15945 				if (rpc_createerr.cf_stat == RPC_UNKNOWNHOST ||
36164590Srm15945 				    rpc_createerr.cf_stat == RPC_TIMEDOUT)
36174590Srm15945 					break;
36184590Srm15945 				if (rpc_createerr.cf_stat ==
36194590Srm15945 				    RPC_PROGNOTREGISTERED) {
36204590Srm15945 					if (trace > 4) {
36214590Srm15945 						trace_prt(1,
36224590Srm15945 						    "  pingnfs: Trying ping "
36234590Srm15945 						    "via \"circuit_v\"\n");
36244590Srm15945 					}
36254590Srm15945 					cl = clnt_create_vers_timed(hostname,
36264590Srm15945 					    NFS_PROGRAM, &outvers,
36274590Srm15945 					    versmin, vers_to_try,
36284590Srm15945 					    "circuit_v", &rpc_to_new);
36294590Srm15945 					if (cl != NULL)
36304590Srm15945 						break;
36314590Srm15945 					if (trace > 4) {
36324590Srm15945 						trace_prt(1,
36334590Srm15945 						    "  pingnfs: Can't ping "
36344590Srm15945 						    "via \"circuit_v\" %s: "
36354590Srm15945 						    "RPC error=%d\n",
36364590Srm15945 						    hostname,
36374590Srm15945 						    rpc_createerr.cf_stat);
36384590Srm15945 					}
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 		 */
36494590Srm15945 			vers_to_try--;
36504590Srm15945 			if (vers_to_try < versmin)
36514590Srm15945 				break;
36524590Srm15945 			if (versp != NULL) {	/* recheck the cache */
36534590Srm15945 				*versp = vers_to_try;
36544590Srm15945 				if (trace > 4) {
36554590Srm15945 					trace_prt(1,
36564590Srm15945 					    "  pingnfs: check cache: vers=%d\n",
36574590Srm15945 					    *versp);
36584590Srm15945 				}
36594590Srm15945 				switch (cache_check(hostname, versp, proto)) {
36604590Srm15945 				case GOODHOST:
36614590Srm15945 					if (hostname != hostpart)
36624590Srm15945 						free(hostname);
36634590Srm15945 					return (RPC_SUCCESS);
36644590Srm15945 				case DEADHOST:
36654590Srm15945 					if (hostname != hostpart)
36664590Srm15945 						free(hostname);
36674590Srm15945 					return (RPC_TIMEDOUT);
36684590Srm15945 				case NOHOST:
36694590Srm15945 				default:
36704590Srm15945 					break;
36714590Srm15945 				}
36724590Srm15945 			}
36734590Srm15945 			if (trace > 4) {
36744590Srm15945 				trace_prt(1, "  pingnfs: Try version=%d\n",
36754590Srm15945 				    vers_to_try);
36764590Srm15945 			}
36774590Srm15945 		} while (cl == NULL);
36784590Srm15945 
36794590Srm15945 
36804590Srm15945 		if (cl == NULL) {
36814590Srm15945 			if (verbose)
36824590Srm15945 				syslog(LOG_ERR, "pingnfs: %s%s",
36830Sstevel@tonic-gate 				    hostname, clnt_spcreateerror(""));
36844590Srm15945 			clnt_stat = rpc_createerr.cf_stat;
36854590Srm15945 		} else {
36864590Srm15945 			clnt_destroy(cl);
36874590Srm15945 			clnt_stat = RPC_SUCCESS;
36884590Srm15945 		}
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 "
36984590Srm15945 				    "using get_ping()\n", vers_to_try);
36990Sstevel@tonic-gate 			}
37000Sstevel@tonic-gate 
37010Sstevel@tonic-gate 			clnt_stat = get_ping(hostname, NFS_PROGRAM,
37024590Srm15945 			    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 ?
37164590Srm15945 		    trace_prt(1, "	pingnfs OK: nfs version=%d\n", outvers):
37174590Srm15945 		    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 &&
41424590Srm15945 	    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,
41634590Srm15945 			    __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,
41704590Srm15945 		    __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",
42184590Srm15945 	    portmap_cache_accesses, portmap_cache_lookups,
42194590Srm15945 	    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,
42304590Srm15945 	    "host_cache: accesses=%d lookups=%d deadhits=%d goodhits=%d\n",
42314590Srm15945 	    host_cache_accesses, host_cache_lookups, deadhost_cache_hits,
42324590Srm15945 	    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 ||
42934590Srm15945 		    vers_min_default < NFS_VERSMIN ||
42944590Srm15945 		    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",
43004590Srm15945 				    vers_min_default, vers_max_default,
43014590Srm15945 				    NFS_VERSMIN_DEFAULT,
43024590Srm15945 				    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