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