xref: /illumos-gate/usr/src/cmd/fs.d/autofs/autod_nfs.c (revision f45bbb21)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
545916cd2Sjpk  * Common Development and Distribution License (the "License").
645916cd2Sjpk  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21bbe876c0SMarcel Telka 
227c478bd9Sstevel@tonic-gate /*
23bbe876c0SMarcel Telka  * Copyright 2015 Nexenta Systems, Inc.  All rights reserved.
24a05556d6SNagakiran Rajashekar  * Copyright (c) 1992, 2010, Oracle and/or its affiliates. All rights reserved.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #include <stdio.h>
287c478bd9Sstevel@tonic-gate #include <unistd.h>
297c478bd9Sstevel@tonic-gate #include <stdlib.h>
307c478bd9Sstevel@tonic-gate #include <ctype.h>
317c478bd9Sstevel@tonic-gate #include <syslog.h>
327c478bd9Sstevel@tonic-gate #include <string.h>
337c478bd9Sstevel@tonic-gate #include <deflt.h>
347c478bd9Sstevel@tonic-gate #include <kstat.h>
357c478bd9Sstevel@tonic-gate #include <sys/param.h>
367c478bd9Sstevel@tonic-gate #include <sys/types.h>
377c478bd9Sstevel@tonic-gate #include <sys/time.h>
387c478bd9Sstevel@tonic-gate #include <sys/stat.h>
397c478bd9Sstevel@tonic-gate #include <sys/wait.h>
407c478bd9Sstevel@tonic-gate #include <sys/socket.h>
417c478bd9Sstevel@tonic-gate #include <netinet/in.h>
427c478bd9Sstevel@tonic-gate #include <signal.h>
437c478bd9Sstevel@tonic-gate #include <sys/signal.h>
447c478bd9Sstevel@tonic-gate #include <rpc/rpc.h>
457c478bd9Sstevel@tonic-gate #include <rpc/pmap_clnt.h>
467c478bd9Sstevel@tonic-gate #include <sys/mount.h>
477c478bd9Sstevel@tonic-gate #include <sys/mntent.h>
487c478bd9Sstevel@tonic-gate #include <sys/mnttab.h>
497c478bd9Sstevel@tonic-gate #include <sys/fstyp.h>
507c478bd9Sstevel@tonic-gate #include <sys/fsid.h>
517c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
527c478bd9Sstevel@tonic-gate #include <netdb.h>
537c478bd9Sstevel@tonic-gate #include <netconfig.h>
547c478bd9Sstevel@tonic-gate #include <netdir.h>
557c478bd9Sstevel@tonic-gate #include <errno.h>
567c478bd9Sstevel@tonic-gate #define	NFSCLIENT
577c478bd9Sstevel@tonic-gate #include <nfs/nfs.h>
587c478bd9Sstevel@tonic-gate #include <nfs/mount.h>
597c478bd9Sstevel@tonic-gate #include <rpcsvc/mount.h>
607c478bd9Sstevel@tonic-gate #include <rpc/nettype.h>
617c478bd9Sstevel@tonic-gate #include <locale.h>
627c478bd9Sstevel@tonic-gate #include <setjmp.h>
637c478bd9Sstevel@tonic-gate #include <sys/socket.h>
647c478bd9Sstevel@tonic-gate #include <thread.h>
657c478bd9Sstevel@tonic-gate #include <limits.h>
667c478bd9Sstevel@tonic-gate #include <nss_dbdefs.h>			/* for NSS_BUFLEN_HOSTS */
677c478bd9Sstevel@tonic-gate #include <nfs/nfs_sec.h>
687c478bd9Sstevel@tonic-gate #include <sys/sockio.h>
697c478bd9Sstevel@tonic-gate #include <net/if.h>
707c478bd9Sstevel@tonic-gate #include <assert.h>
717c478bd9Sstevel@tonic-gate #include <nfs/nfs_clnt.h>
727c478bd9Sstevel@tonic-gate #include <rpcsvc/nfs4_prot.h>
73bbe876c0SMarcel Telka #include <nfs/nfs4.h>
747c478bd9Sstevel@tonic-gate #define	NO_RDDIR_CACHE
757c478bd9Sstevel@tonic-gate #include "automount.h"
767c478bd9Sstevel@tonic-gate #include "replica.h"
777c478bd9Sstevel@tonic-gate #include "nfs_subr.h"
787c478bd9Sstevel@tonic-gate #include "webnfs.h"
792f172c55SRobert Thurlow #include "nfs_resolve.h"
807c478bd9Sstevel@tonic-gate #include <sys/sockio.h>
817c478bd9Sstevel@tonic-gate #include <net/if.h>
827c478bd9Sstevel@tonic-gate #include <rpcsvc/daemon_utils.h>
8345916cd2Sjpk #include <pwd.h>
8445916cd2Sjpk #include <strings.h>
8545916cd2Sjpk #include <tsol/label.h>
8645916cd2Sjpk #include <zone.h>
87dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States #include <limits.h>
88dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States #include <libscf.h>
89dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States #include <libshare.h>
90dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States #include "smfcfg.h"
917c478bd9Sstevel@tonic-gate 
92b9238976Sth extern void set_nfsv4_ephemeral_mount_to(void);
93b9238976Sth 
947c478bd9Sstevel@tonic-gate extern char *nfs_get_qop_name();
957c478bd9Sstevel@tonic-gate extern AUTH *nfs_create_ah();
967c478bd9Sstevel@tonic-gate extern enum snego_stat nfs_sec_nego();
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate #define	MAXHOSTS	512
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate /*
1017c478bd9Sstevel@tonic-gate  * host cache states
1027c478bd9Sstevel@tonic-gate  */
1037c478bd9Sstevel@tonic-gate #define	NOHOST		0
1047c478bd9Sstevel@tonic-gate #define	GOODHOST	1
1057c478bd9Sstevel@tonic-gate #define	DEADHOST	2
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate #define	NFS_ARGS_EXTB_secdata(args, secdata) \
1087c478bd9Sstevel@tonic-gate 	{ (args).nfs_args_ext = NFS_ARGS_EXTB, \
1097c478bd9Sstevel@tonic-gate 	(args).nfs_ext_u.nfs_extB.secdata = secdata; }
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate struct cache_entry {
1127c478bd9Sstevel@tonic-gate 	struct	cache_entry *cache_next;
1137c478bd9Sstevel@tonic-gate 	char	*cache_host;
1147c478bd9Sstevel@tonic-gate 	time_t	cache_time;
1157c478bd9Sstevel@tonic-gate 	int	cache_state;
1167c478bd9Sstevel@tonic-gate 	rpcvers_t cache_reqvers;
1177c478bd9Sstevel@tonic-gate 	rpcvers_t cache_outvers;
1187c478bd9Sstevel@tonic-gate 	char	*cache_proto;
1197c478bd9Sstevel@tonic-gate };
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate struct mfs_snego_t {
1227c478bd9Sstevel@tonic-gate 	int sec_opt;
1237c478bd9Sstevel@tonic-gate 	bool_t snego_done;
1247c478bd9Sstevel@tonic-gate 	char *nfs_flavor;
1257c478bd9Sstevel@tonic-gate 	seconfig_t nfs_sec;
1267c478bd9Sstevel@tonic-gate };
1277c478bd9Sstevel@tonic-gate typedef struct mfs_snego_t mfs_snego_t;
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate static struct cache_entry *cache_head = NULL;
1307c478bd9Sstevel@tonic-gate rwlock_t cache_lock;	/* protect the cache chain */
1317c478bd9Sstevel@tonic-gate 
132bd93c05dSAlexander Eremin static enum nfsstat nfsmount(struct mapfs *, char *, char *, int, uid_t,
1333bfb48feSsemery 	action_list *);
1347c478bd9Sstevel@tonic-gate static int is_nfs_port(char *);
1357c478bd9Sstevel@tonic-gate 
1362f172c55SRobert Thurlow static void netbuf_free(struct netbuf *);
1377c478bd9Sstevel@tonic-gate static int get_pathconf(CLIENT *, char *, char *, struct pathcnf **, int);
1387c478bd9Sstevel@tonic-gate static struct mapfs *enum_servers(struct mapent *, char *);
1397c478bd9Sstevel@tonic-gate static struct mapfs *get_mysubnet_servers(struct mapfs *);
1407c478bd9Sstevel@tonic-gate static int subnet_test(int af, struct sioc_addrreq *);
1417c478bd9Sstevel@tonic-gate static	struct	netbuf *get_addr(char *, rpcprog_t, rpcvers_t,
1427c478bd9Sstevel@tonic-gate 	struct netconfig **, char *, ushort_t, struct t_info *);
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate static	struct	netbuf *get_pubfh(char *, rpcvers_t, mfs_snego_t *,
1457c478bd9Sstevel@tonic-gate 	struct netconfig **, char *, ushort_t, struct t_info *, caddr_t *,
1467c478bd9Sstevel@tonic-gate 	bool_t, char *);
1477c478bd9Sstevel@tonic-gate 
14845916cd2Sjpk static int create_homedir(const char *, const char *);
14945916cd2Sjpk 
1507c478bd9Sstevel@tonic-gate enum type_of_stuff {
1517c478bd9Sstevel@tonic-gate 	SERVER_ADDR = 0,
1527c478bd9Sstevel@tonic-gate 	SERVER_PING = 1,
1537c478bd9Sstevel@tonic-gate 	SERVER_FH = 2
1547c478bd9Sstevel@tonic-gate };
1557c478bd9Sstevel@tonic-gate 
1562f172c55SRobert Thurlow static void *get_server_netinfo(enum type_of_stuff, char *, rpcprog_t,
1577c478bd9Sstevel@tonic-gate 	rpcvers_t, mfs_snego_t *, struct netconfig **, char *, ushort_t,
1587c478bd9Sstevel@tonic-gate 	struct t_info *, caddr_t *, bool_t, char *, enum clnt_stat *);
1592f172c55SRobert Thurlow static void *get_netconfig_info(enum type_of_stuff, char *, rpcprog_t,
1602f172c55SRobert Thurlow 	rpcvers_t, struct netconfig *, ushort_t, struct t_info *,
1612f172c55SRobert Thurlow 	struct t_bind *, caddr_t *, bool_t, char *, enum clnt_stat *,
1622f172c55SRobert Thurlow 	mfs_snego_t *);
1632f172c55SRobert Thurlow static void *get_server_addrorping(char *, rpcprog_t, rpcvers_t,
1642f172c55SRobert Thurlow 	struct netconfig *, ushort_t, struct t_info *, struct t_bind *,
1652f172c55SRobert Thurlow 	caddr_t *, bool_t, char *, enum clnt_stat *, int);
1662f172c55SRobert Thurlow static void *get_server_fh(char *, rpcprog_t, rpcvers_t, mfs_snego_t *,
1672f172c55SRobert Thurlow 	struct netconfig *, ushort_t, struct t_info *, struct t_bind *,
1687c478bd9Sstevel@tonic-gate 	caddr_t *, bool_t, char *, enum clnt_stat *);
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate struct mapfs *add_mfs(struct mapfs *, int, struct mapfs **, struct mapfs **);
1717c478bd9Sstevel@tonic-gate void free_mfs(struct mapfs *);
1727c478bd9Sstevel@tonic-gate static void dump_mfs(struct mapfs *, char *, int);
1737c478bd9Sstevel@tonic-gate static char *dump_distance(struct mapfs *);
1747c478bd9Sstevel@tonic-gate static void cache_free(struct cache_entry *);
1757c478bd9Sstevel@tonic-gate static int cache_check(char *, rpcvers_t *, char *);
1767c478bd9Sstevel@tonic-gate static void cache_enter(char *, rpcvers_t, rpcvers_t, char *, int);
17739d3e169Sevanl void destroy_auth_client_handle(CLIENT *cl);
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate #ifdef CACHE_DEBUG
1804dc70696SToomas Soome static void trace_host_cache(void);
1814dc70696SToomas Soome static void trace_portmap_cache(void);
1827c478bd9Sstevel@tonic-gate #endif /* CACHE_DEBUG */
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate static int rpc_timeout = 20;
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate #ifdef CACHE_DEBUG
1877c478bd9Sstevel@tonic-gate /*
1887c478bd9Sstevel@tonic-gate  * host cache counters. These variables do not need to be protected
1897c478bd9Sstevel@tonic-gate  * by mutex's. They have been added to measure the utility of the
1907c478bd9Sstevel@tonic-gate  * goodhost/deadhost cache in the lazy hierarchical mounting scheme.
1917c478bd9Sstevel@tonic-gate  */
1927c478bd9Sstevel@tonic-gate static int host_cache_accesses = 0;
1937c478bd9Sstevel@tonic-gate static int host_cache_lookups = 0;
1947c478bd9Sstevel@tonic-gate static int deadhost_cache_hits = 0;
1957c478bd9Sstevel@tonic-gate static int goodhost_cache_hits = 0;
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate /*
1987c478bd9Sstevel@tonic-gate  * portmap cache counters. These variables do not need to be protected
1997c478bd9Sstevel@tonic-gate  * by mutex's. They have been added to measure the utility of the portmap
2007c478bd9Sstevel@tonic-gate  * cache in the lazy hierarchical mounting scheme.
2017c478bd9Sstevel@tonic-gate  */
2027c478bd9Sstevel@tonic-gate static int portmap_cache_accesses = 0;
2037c478bd9Sstevel@tonic-gate static int portmap_cache_lookups = 0;
2047c478bd9Sstevel@tonic-gate static int portmap_cache_hits = 0;
2057c478bd9Sstevel@tonic-gate #endif /* CACHE_DEBUG */
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate /*
2087c478bd9Sstevel@tonic-gate  * There are the defaults (range) for the client when determining
2097c478bd9Sstevel@tonic-gate  * which NFS version to use when probing the server (see above).
2107c478bd9Sstevel@tonic-gate  * These will only be used when the vers mount option is not used and
2117c478bd9Sstevel@tonic-gate  * these may be reset if /etc/default/nfs is configured to do so.
2127c478bd9Sstevel@tonic-gate  */
2137c478bd9Sstevel@tonic-gate static rpcvers_t vers_max_default = NFS_VERSMAX_DEFAULT;
2147c478bd9Sstevel@tonic-gate static rpcvers_t vers_min_default = NFS_VERSMIN_DEFAULT;
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate /*
2177c478bd9Sstevel@tonic-gate  * list of support services needed
2187c478bd9Sstevel@tonic-gate  */
2197c478bd9Sstevel@tonic-gate static char	*service_list[] = { STATD, LOCKD, NULL };
2207c478bd9Sstevel@tonic-gate static char	*service_list_v4[] = { STATD, LOCKD, NFS4CBD, NFSMAPID, NULL };
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate static void read_default_nfs(void);
2237c478bd9Sstevel@tonic-gate static int is_v4_mount(char *);
2247c478bd9Sstevel@tonic-gate static void start_nfs4cbd(void);
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate int
mount_nfs(struct mapent * me,char * mntpnt,char * prevhost,int overlay,uid_t uid,action_list ** alpp)2274dc70696SToomas Soome mount_nfs(struct mapent *me, char *mntpnt, char *prevhost, int overlay,
2284dc70696SToomas Soome     uid_t uid, action_list **alpp)
2297c478bd9Sstevel@tonic-gate {
2307c478bd9Sstevel@tonic-gate 	struct mapfs *mfs, *mp;
2317c478bd9Sstevel@tonic-gate 	int err = -1;
23239d3e169Sevanl 	action_list *alp;
233a05556d6SNagakiran Rajashekar 	char *dir;
23439d3e169Sevanl 
23539d3e169Sevanl 
23639d3e169Sevanl 	alp = *alpp;
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 	read_default_nfs();
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate 	mfs = enum_servers(me, prevhost);
2417c478bd9Sstevel@tonic-gate 	if (mfs == NULL)
2427c478bd9Sstevel@tonic-gate 		return (ENOENT);
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate 	/*
2457c478bd9Sstevel@tonic-gate 	 * Try loopback if we have something on localhost; if nothing
2467c478bd9Sstevel@tonic-gate 	 * works, we will fall back to NFS
2477c478bd9Sstevel@tonic-gate 	 */
2487c478bd9Sstevel@tonic-gate 	if (is_nfs_port(me->map_mntopts)) {
2497c478bd9Sstevel@tonic-gate 		for (mp = mfs; mp; mp = mp->mfs_next) {
2507c478bd9Sstevel@tonic-gate 			if (self_check(mp->mfs_host)) {
2517c478bd9Sstevel@tonic-gate 				err = loopbackmount(mp->mfs_dir,
2521bbc88acSrm 				    mntpnt, me->map_mntopts, overlay);
2537c478bd9Sstevel@tonic-gate 				if (err) {
2547c478bd9Sstevel@tonic-gate 					mp->mfs_ignore = 1;
2557c478bd9Sstevel@tonic-gate 				} else {
25639d3e169Sevanl 					/*
25739d3e169Sevanl 					 * Free action_list if there
25839d3e169Sevanl 					 * is one as it is not needed.
25939d3e169Sevanl 					 * Make sure to set alpp to null
26039d3e169Sevanl 					 * so caller doesn't try to free it
26139d3e169Sevanl 					 * again.
26239d3e169Sevanl 					 */
26339d3e169Sevanl 					if (*alpp) {
26439d3e169Sevanl 						free(*alpp);
26539d3e169Sevanl 						*alpp = NULL;
26639d3e169Sevanl 					}
2677c478bd9Sstevel@tonic-gate 					break;
2687c478bd9Sstevel@tonic-gate 				}
2697c478bd9Sstevel@tonic-gate 			}
2707c478bd9Sstevel@tonic-gate 		}
2717c478bd9Sstevel@tonic-gate 	}
2727c478bd9Sstevel@tonic-gate 	if (err) {
273a05556d6SNagakiran Rajashekar 		dir = strdup(mfs->mfs_dir);
2747c478bd9Sstevel@tonic-gate 		err = nfsmount(mfs, mntpnt, me->map_mntopts,
275bd93c05dSAlexander Eremin 		    overlay, uid, alp);
2767c478bd9Sstevel@tonic-gate 		if (err && trace > 1) {
277a05556d6SNagakiran Rajashekar 			trace_prt(1, "  Couldn't mount %s:%s, err=%d\n",
278a05556d6SNagakiran Rajashekar 			    mfs->mfs_host ? mfs->mfs_host : "",
279a05556d6SNagakiran Rajashekar 			    mfs->mfs_dir ? mfs->mfs_dir : dir, err);
2807c478bd9Sstevel@tonic-gate 		}
281a05556d6SNagakiran Rajashekar 		free(dir);
2827c478bd9Sstevel@tonic-gate 	}
2837c478bd9Sstevel@tonic-gate 	free_mfs(mfs);
2847c478bd9Sstevel@tonic-gate 	return (err);
2857c478bd9Sstevel@tonic-gate }
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate /*
2897c478bd9Sstevel@tonic-gate  * Using the new ioctl SIOCTONLINK to determine if a host is on the same
2907c478bd9Sstevel@tonic-gate  * subnet. Remove the old network, subnet check.
2917c478bd9Sstevel@tonic-gate  */
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate static struct mapfs *
get_mysubnet_servers(struct mapfs * mfs_in)2947c478bd9Sstevel@tonic-gate get_mysubnet_servers(struct mapfs *mfs_in)
2957c478bd9Sstevel@tonic-gate {
2967c478bd9Sstevel@tonic-gate 	int s;
2977c478bd9Sstevel@tonic-gate 	struct mapfs *mfs, *p, *mfs_head = NULL, *mfs_tail = NULL;
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 	struct netconfig *nconf;
3007c478bd9Sstevel@tonic-gate 	NCONF_HANDLE *nc = NULL;
3017c478bd9Sstevel@tonic-gate 	struct nd_hostserv hs;
3027c478bd9Sstevel@tonic-gate 	struct nd_addrlist *retaddrs;
3037c478bd9Sstevel@tonic-gate 	struct netbuf *nb;
3047c478bd9Sstevel@tonic-gate 	struct sioc_addrreq areq;
3057c478bd9Sstevel@tonic-gate 	int res;
3067c478bd9Sstevel@tonic-gate 	int af;
3077c478bd9Sstevel@tonic-gate 	int i;
3087c478bd9Sstevel@tonic-gate 	int sa_size;
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate 	hs.h_serv = "rpcbind";
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate 	for (mfs = mfs_in; mfs; mfs = mfs->mfs_next) {
3137c478bd9Sstevel@tonic-gate 		nc = setnetconfig();
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate 		while (nconf = getnetconfig(nc)) {
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 			/*
3187c478bd9Sstevel@tonic-gate 			 * Care about INET family only. proto_done flag
3197c478bd9Sstevel@tonic-gate 			 * indicates if we have already covered this
3207c478bd9Sstevel@tonic-gate 			 * protocol family. If so skip it
3217c478bd9Sstevel@tonic-gate 			 */
3227c478bd9Sstevel@tonic-gate 			if (((strcmp(nconf->nc_protofmly, NC_INET6) == 0) ||
3231bbc88acSrm 			    (strcmp(nconf->nc_protofmly, NC_INET) == 0)) &&
3241bbc88acSrm 			    (nconf->nc_semantics == NC_TPI_CLTS)) {
3257c478bd9Sstevel@tonic-gate 			} else
3267c478bd9Sstevel@tonic-gate 				continue;
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate 			hs.h_host = mfs->mfs_host;
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate 			if (netdir_getbyname(nconf, &hs, &retaddrs) != ND_OK)
3317c478bd9Sstevel@tonic-gate 				continue;
3327c478bd9Sstevel@tonic-gate 
3337c478bd9Sstevel@tonic-gate 			/*
3347c478bd9Sstevel@tonic-gate 			 * For each host address see if it's on our
3357c478bd9Sstevel@tonic-gate 			 * local subnet.
3367c478bd9Sstevel@tonic-gate 			 */
3377c478bd9Sstevel@tonic-gate 
3387c478bd9Sstevel@tonic-gate 			if (strcmp(nconf->nc_protofmly, NC_INET6) == 0)
3397c478bd9Sstevel@tonic-gate 				af = AF_INET6;
3407c478bd9Sstevel@tonic-gate 			else
3417c478bd9Sstevel@tonic-gate 				af = AF_INET;
3427c478bd9Sstevel@tonic-gate 			nb = retaddrs->n_addrs;
3437c478bd9Sstevel@tonic-gate 			for (i = 0; i < retaddrs->n_cnt; i++, nb++) {
3447c478bd9Sstevel@tonic-gate 				memset(&areq.sa_addr, 0, sizeof (areq.sa_addr));
3457c478bd9Sstevel@tonic-gate 				memcpy(&areq.sa_addr, nb->buf, MIN(nb->len,
3461bbc88acSrm 				    sizeof (areq.sa_addr)));
3477c478bd9Sstevel@tonic-gate 				if (res = subnet_test(af, &areq)) {
3487c478bd9Sstevel@tonic-gate 					p = add_mfs(mfs, DIST_MYNET,
3491bbc88acSrm 					    &mfs_head, &mfs_tail);
3507c478bd9Sstevel@tonic-gate 					if (!p) {
3517c478bd9Sstevel@tonic-gate 						netdir_free(retaddrs,
3521bbc88acSrm 						    ND_ADDRLIST);
3537c478bd9Sstevel@tonic-gate 						endnetconfig(nc);
3547c478bd9Sstevel@tonic-gate 						return (NULL);
3557c478bd9Sstevel@tonic-gate 					}
3567c478bd9Sstevel@tonic-gate 					break;
3577c478bd9Sstevel@tonic-gate 				}
3587c478bd9Sstevel@tonic-gate 			}  /* end of every host */
3597c478bd9Sstevel@tonic-gate 			if (trace > 2) {
3607c478bd9Sstevel@tonic-gate 				trace_prt(1, "get_mysubnet_servers: host=%s "
3611bbc88acSrm 				    "netid=%s res=%s\n", mfs->mfs_host,
3621bbc88acSrm 				    nconf->nc_netid, res == 1?"SUC":"FAIL");
3637c478bd9Sstevel@tonic-gate 			}
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate 			netdir_free(retaddrs, ND_ADDRLIST);
3667c478bd9Sstevel@tonic-gate 		} /* end of while */
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate 		endnetconfig(nc);
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate 	} /* end of every map */
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate 	return (mfs_head);
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate }
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate int
subnet_test(int af,struct sioc_addrreq * areq)3777c478bd9Sstevel@tonic-gate subnet_test(int af, struct sioc_addrreq *areq)
3787c478bd9Sstevel@tonic-gate {
3797c478bd9Sstevel@tonic-gate 	int s;
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate 	if ((s = socket(af, SOCK_DGRAM, 0)) < 0) {
3827c478bd9Sstevel@tonic-gate 		return (0);
3837c478bd9Sstevel@tonic-gate 	}
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate 	areq->sa_res = -1;
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCTONLINK, (caddr_t)areq) < 0) {
3887c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "subnet_test:SIOCTONLINK failed");
3897c478bd9Sstevel@tonic-gate 		return (0);
3907c478bd9Sstevel@tonic-gate 	}
3917c478bd9Sstevel@tonic-gate 	close(s);
3927c478bd9Sstevel@tonic-gate 	if (areq->sa_res == 1)
3937c478bd9Sstevel@tonic-gate 		return (1);
3947c478bd9Sstevel@tonic-gate 	else
3957c478bd9Sstevel@tonic-gate 		return (0);
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate }
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate /*
4017c478bd9Sstevel@tonic-gate  * ping a bunch of hosts at once and sort by who responds first
4027c478bd9Sstevel@tonic-gate  */
4037c478bd9Sstevel@tonic-gate static struct mapfs *
sort_servers(struct mapfs * mfs_in,int timeout)4047c478bd9Sstevel@tonic-gate sort_servers(struct mapfs *mfs_in, int timeout)
4057c478bd9Sstevel@tonic-gate {
4067c478bd9Sstevel@tonic-gate 	struct mapfs *m1 = NULL;
4077c478bd9Sstevel@tonic-gate 	enum clnt_stat clnt_stat;
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate 	if (!mfs_in)
4107c478bd9Sstevel@tonic-gate 		return (NULL);
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate 	clnt_stat = nfs_cast(mfs_in, &m1, timeout);
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate 	if (!m1) {
4157c478bd9Sstevel@tonic-gate 		char buff[2048] = {'\0'};
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate 		for (m1 = mfs_in; m1; m1 = m1->mfs_next) {
4187c478bd9Sstevel@tonic-gate 			(void) strcat(buff, m1->mfs_host);
4197c478bd9Sstevel@tonic-gate 			if (m1->mfs_next)
4207c478bd9Sstevel@tonic-gate 				(void) strcat(buff, ",");
4217c478bd9Sstevel@tonic-gate 		}
4227c478bd9Sstevel@tonic-gate 
4237c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "servers %s not responding: %s",
4241bbc88acSrm 		    buff, clnt_sperrno(clnt_stat));
4257c478bd9Sstevel@tonic-gate 	}
4267c478bd9Sstevel@tonic-gate 
4277c478bd9Sstevel@tonic-gate 	return (m1);
4287c478bd9Sstevel@tonic-gate }
4297c478bd9Sstevel@tonic-gate 
4307c478bd9Sstevel@tonic-gate /*
4317c478bd9Sstevel@tonic-gate  * Add a mapfs entry to the list described by *mfs_head and *mfs_tail,
4327c478bd9Sstevel@tonic-gate  * provided it is not marked "ignored" and isn't a dupe of ones we've
4337c478bd9Sstevel@tonic-gate  * already seen.
4347c478bd9Sstevel@tonic-gate  */
4357c478bd9Sstevel@tonic-gate struct mapfs *
add_mfs(struct mapfs * mfs,int distance,struct mapfs ** mfs_head,struct mapfs ** mfs_tail)4367c478bd9Sstevel@tonic-gate add_mfs(struct mapfs *mfs, int distance, struct mapfs **mfs_head,
4374dc70696SToomas Soome     struct mapfs **mfs_tail)
4387c478bd9Sstevel@tonic-gate {
4397c478bd9Sstevel@tonic-gate 	struct mapfs *tmp, *new;
4407c478bd9Sstevel@tonic-gate 
4417c478bd9Sstevel@tonic-gate 	for (tmp = *mfs_head; tmp; tmp = tmp->mfs_next)
4427c478bd9Sstevel@tonic-gate 		if ((strcmp(tmp->mfs_host, mfs->mfs_host) == 0 &&
4437c478bd9Sstevel@tonic-gate 		    strcmp(tmp->mfs_dir, mfs->mfs_dir) == 0) ||
4441bbc88acSrm 		    mfs->mfs_ignore)
4457c478bd9Sstevel@tonic-gate 			return (*mfs_head);
4467c478bd9Sstevel@tonic-gate 	new = (struct mapfs *)malloc(sizeof (struct mapfs));
4477c478bd9Sstevel@tonic-gate 	if (!new) {
4487c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "Memory allocation failed: %m");
4497c478bd9Sstevel@tonic-gate 		return (NULL);
4507c478bd9Sstevel@tonic-gate 	}
4517c478bd9Sstevel@tonic-gate 	bcopy(mfs, new, sizeof (struct mapfs));
4527c478bd9Sstevel@tonic-gate 	new->mfs_next = NULL;
4537c478bd9Sstevel@tonic-gate 	if (distance)
4547c478bd9Sstevel@tonic-gate 		new->mfs_distance = distance;
4557c478bd9Sstevel@tonic-gate 	if (!*mfs_head)
4567c478bd9Sstevel@tonic-gate 		*mfs_tail = *mfs_head = new;
4577c478bd9Sstevel@tonic-gate 	else {
4587c478bd9Sstevel@tonic-gate 		(*mfs_tail)->mfs_next = new;
4597c478bd9Sstevel@tonic-gate 		*mfs_tail = new;
4607c478bd9Sstevel@tonic-gate 	}
4617c478bd9Sstevel@tonic-gate 	return (*mfs_head);
4627c478bd9Sstevel@tonic-gate }
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate static void
dump_mfs(struct mapfs * mfs,char * message,int level)4657c478bd9Sstevel@tonic-gate dump_mfs(struct mapfs *mfs, char *message, int level)
4667c478bd9Sstevel@tonic-gate {
4677c478bd9Sstevel@tonic-gate 	struct mapfs *m1;
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate 	if (trace <= level)
4707c478bd9Sstevel@tonic-gate 		return;
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate 	trace_prt(1, "%s", message);
4737c478bd9Sstevel@tonic-gate 	if (!mfs) {
4747c478bd9Sstevel@tonic-gate 		trace_prt(0, "mfs is null\n");
4757c478bd9Sstevel@tonic-gate 		return;
4767c478bd9Sstevel@tonic-gate 	}
4777c478bd9Sstevel@tonic-gate 	for (m1 = mfs; m1; m1 = m1->mfs_next)
4787c478bd9Sstevel@tonic-gate 		trace_prt(0, "%s[%s] ", m1->mfs_host, dump_distance(m1));
4797c478bd9Sstevel@tonic-gate 	trace_prt(0, "\n");
4807c478bd9Sstevel@tonic-gate }
4817c478bd9Sstevel@tonic-gate 
4827c478bd9Sstevel@tonic-gate static char *
dump_distance(struct mapfs * mfs)4837c478bd9Sstevel@tonic-gate dump_distance(struct mapfs *mfs)
4847c478bd9Sstevel@tonic-gate {
4857c478bd9Sstevel@tonic-gate 	switch (mfs->mfs_distance) {
4867c478bd9Sstevel@tonic-gate 	case 0:			return ("zero");
4877c478bd9Sstevel@tonic-gate 	case DIST_SELF:		return ("self");
4887c478bd9Sstevel@tonic-gate 	case DIST_MYSUB:	return ("mysub");
4897c478bd9Sstevel@tonic-gate 	case DIST_MYNET:	return ("mynet");
4907c478bd9Sstevel@tonic-gate 	case DIST_OTHER:	return ("other");
4917c478bd9Sstevel@tonic-gate 	default:		return ("other");
4927c478bd9Sstevel@tonic-gate 	}
4937c478bd9Sstevel@tonic-gate }
4947c478bd9Sstevel@tonic-gate 
4957c478bd9Sstevel@tonic-gate /*
4967c478bd9Sstevel@tonic-gate  * Walk linked list "raw", building a new list consisting of members
4977c478bd9Sstevel@tonic-gate  * NOT found in list "filter", returning the result.
4987c478bd9Sstevel@tonic-gate  */
4997c478bd9Sstevel@tonic-gate static struct mapfs *
filter_mfs(struct mapfs * raw,struct mapfs * filter)5007c478bd9Sstevel@tonic-gate filter_mfs(struct mapfs *raw, struct mapfs *filter)
5017c478bd9Sstevel@tonic-gate {
5027c478bd9Sstevel@tonic-gate 	struct mapfs *mfs, *p, *mfs_head = NULL, *mfs_tail = NULL;
5037c478bd9Sstevel@tonic-gate 	int skip;
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate 	if (!raw)
5067c478bd9Sstevel@tonic-gate 		return (NULL);
5077c478bd9Sstevel@tonic-gate 	for (mfs = raw; mfs; mfs = mfs->mfs_next) {
5087c478bd9Sstevel@tonic-gate 		for (skip = 0, p = filter; p; p = p->mfs_next) {
5097c478bd9Sstevel@tonic-gate 			if (strcmp(p->mfs_host, mfs->mfs_host) == 0 &&
5107c478bd9Sstevel@tonic-gate 			    strcmp(p->mfs_dir, mfs->mfs_dir) == 0) {
5117c478bd9Sstevel@tonic-gate 				skip = 1;
5127c478bd9Sstevel@tonic-gate 				break;
5137c478bd9Sstevel@tonic-gate 			}
5147c478bd9Sstevel@tonic-gate 		}
5157c478bd9Sstevel@tonic-gate 		if (skip)
5167c478bd9Sstevel@tonic-gate 			continue;
5177c478bd9Sstevel@tonic-gate 		p = add_mfs(mfs, 0, &mfs_head, &mfs_tail);
5187c478bd9Sstevel@tonic-gate 		if (!p)
5197c478bd9Sstevel@tonic-gate 			return (NULL);
5207c478bd9Sstevel@tonic-gate 	}
5217c478bd9Sstevel@tonic-gate 	return (mfs_head);
5227c478bd9Sstevel@tonic-gate }
5237c478bd9Sstevel@tonic-gate 
5247c478bd9Sstevel@tonic-gate /*
5257c478bd9Sstevel@tonic-gate  * Walk a linked list of mapfs structs, freeing each member.
5267c478bd9Sstevel@tonic-gate  */
5277c478bd9Sstevel@tonic-gate void
free_mfs(struct mapfs * mfs)5287c478bd9Sstevel@tonic-gate free_mfs(struct mapfs *mfs)
5297c478bd9Sstevel@tonic-gate {
5307c478bd9Sstevel@tonic-gate 	struct mapfs *tmp;
5317c478bd9Sstevel@tonic-gate 
5327c478bd9Sstevel@tonic-gate 	while (mfs) {
5337c478bd9Sstevel@tonic-gate 		tmp = mfs->mfs_next;
5347c478bd9Sstevel@tonic-gate 		free(mfs);
5357c478bd9Sstevel@tonic-gate 		mfs = tmp;
5367c478bd9Sstevel@tonic-gate 	}
5377c478bd9Sstevel@tonic-gate }
5387c478bd9Sstevel@tonic-gate 
5397c478bd9Sstevel@tonic-gate /*
5407c478bd9Sstevel@tonic-gate  * New code for NFS client failover: we need to carry and sort
5417c478bd9Sstevel@tonic-gate  * lists of server possibilities rather than return a single
5427c478bd9Sstevel@tonic-gate  * entry.  It preserves previous behaviour of sorting first by
5437c478bd9Sstevel@tonic-gate  * locality (loopback-or-preferred/subnet/net/other) and then
5447c478bd9Sstevel@tonic-gate  * by ping times.  We'll short-circuit this process when we
5457c478bd9Sstevel@tonic-gate  * have ENOUGH or more entries.
5467c478bd9Sstevel@tonic-gate  */
5477c478bd9Sstevel@tonic-gate static struct mapfs *
enum_servers(struct mapent * me,char * preferred)5487c478bd9Sstevel@tonic-gate enum_servers(struct mapent *me, char *preferred)
5497c478bd9Sstevel@tonic-gate {
5507c478bd9Sstevel@tonic-gate 	struct mapfs *p, *m1, *m2, *mfs_head = NULL, *mfs_tail = NULL;
5517c478bd9Sstevel@tonic-gate 
5527c478bd9Sstevel@tonic-gate 	/*
5537c478bd9Sstevel@tonic-gate 	 * Short-circuit for simple cases.
5547c478bd9Sstevel@tonic-gate 	 */
5557c478bd9Sstevel@tonic-gate 	if (!me->map_fs->mfs_next) {
5567c478bd9Sstevel@tonic-gate 		p = add_mfs(me->map_fs, DIST_OTHER, &mfs_head, &mfs_tail);
5577c478bd9Sstevel@tonic-gate 		if (!p)
5587c478bd9Sstevel@tonic-gate 			return (NULL);
5597c478bd9Sstevel@tonic-gate 		return (mfs_head);
5607c478bd9Sstevel@tonic-gate 	}
5617c478bd9Sstevel@tonic-gate 
5627c478bd9Sstevel@tonic-gate 	dump_mfs(me->map_fs, "	enum_servers: mapent: ", 2);
5637c478bd9Sstevel@tonic-gate 
5647c478bd9Sstevel@tonic-gate 	/*
5657c478bd9Sstevel@tonic-gate 	 * get addresses & see if any are myself
5667c478bd9Sstevel@tonic-gate 	 * or were mounted from previously in a
5677c478bd9Sstevel@tonic-gate 	 * hierarchical mount.
5687c478bd9Sstevel@tonic-gate 	 */
5697c478bd9Sstevel@tonic-gate 	if (trace > 2)
5707c478bd9Sstevel@tonic-gate 		trace_prt(1, "	enum_servers: looking for pref/self\n");
5717c478bd9Sstevel@tonic-gate 	for (m1 = me->map_fs; m1; m1 = m1->mfs_next) {
5727c478bd9Sstevel@tonic-gate 		if (m1->mfs_ignore)
5737c478bd9Sstevel@tonic-gate 			continue;
5747c478bd9Sstevel@tonic-gate 		if (self_check(m1->mfs_host) ||
5757c478bd9Sstevel@tonic-gate 		    strcmp(m1->mfs_host, preferred) == 0) {
5767c478bd9Sstevel@tonic-gate 			p = add_mfs(m1, DIST_SELF, &mfs_head, &mfs_tail);
5777c478bd9Sstevel@tonic-gate 			if (!p)
5787c478bd9Sstevel@tonic-gate 				return (NULL);
5797c478bd9Sstevel@tonic-gate 		}
5807c478bd9Sstevel@tonic-gate 	}
5817c478bd9Sstevel@tonic-gate 	if (trace > 2 && m1)
5827c478bd9Sstevel@tonic-gate 		trace_prt(1, "	enum_servers: pref/self found, %s\n",
5831bbc88acSrm 		    m1->mfs_host);
5847c478bd9Sstevel@tonic-gate 
5857c478bd9Sstevel@tonic-gate 	/*
5867c478bd9Sstevel@tonic-gate 	 * look for entries on this subnet
5877c478bd9Sstevel@tonic-gate 	 */
5887c478bd9Sstevel@tonic-gate 	dump_mfs(m1, "	enum_servers: input of get_mysubnet_servers: ", 2);
5897c478bd9Sstevel@tonic-gate 	m1 = get_mysubnet_servers(me->map_fs);
5907c478bd9Sstevel@tonic-gate 	dump_mfs(m1, "	enum_servers: output of get_mysubnet_servers: ", 3);
5917c478bd9Sstevel@tonic-gate 	if (m1 && m1->mfs_next) {
5927c478bd9Sstevel@tonic-gate 		m2 = sort_servers(m1, rpc_timeout / 2);
5937c478bd9Sstevel@tonic-gate 		dump_mfs(m2, "	enum_servers: output of sort_servers: ", 3);
5947c478bd9Sstevel@tonic-gate 		free_mfs(m1);
5957c478bd9Sstevel@tonic-gate 		m1 = m2;
5967c478bd9Sstevel@tonic-gate 	}
5977c478bd9Sstevel@tonic-gate 
5987c478bd9Sstevel@tonic-gate 	for (m2 = m1; m2; m2 = m2->mfs_next) {
5997c478bd9Sstevel@tonic-gate 		p = add_mfs(m2, 0, &mfs_head, &mfs_tail);
6007c478bd9Sstevel@tonic-gate 		if (!p)
6017c478bd9Sstevel@tonic-gate 			return (NULL);
6027c478bd9Sstevel@tonic-gate 	}
6037c478bd9Sstevel@tonic-gate 	if (m1)
6047c478bd9Sstevel@tonic-gate 		free_mfs(m1);
6057c478bd9Sstevel@tonic-gate 
6067c478bd9Sstevel@tonic-gate 	/*
6077c478bd9Sstevel@tonic-gate 	 * add the rest of the entries at the end
6087c478bd9Sstevel@tonic-gate 	 */
6097c478bd9Sstevel@tonic-gate 	m1 = filter_mfs(me->map_fs, mfs_head);
6107c478bd9Sstevel@tonic-gate 	dump_mfs(m1, "	enum_servers: etc: output of filter_mfs: ", 3);
6117c478bd9Sstevel@tonic-gate 	m2 = sort_servers(m1, rpc_timeout / 2);
6127c478bd9Sstevel@tonic-gate 	dump_mfs(m2, "	enum_servers: etc: output of sort_servers: ", 3);
6137c478bd9Sstevel@tonic-gate 	if (m1)
6147c478bd9Sstevel@tonic-gate 		free_mfs(m1);
6157c478bd9Sstevel@tonic-gate 	m1 = m2;
6167c478bd9Sstevel@tonic-gate 	for (m2 = m1; m2; m2 = m2->mfs_next) {
6177c478bd9Sstevel@tonic-gate 		p = add_mfs(m2, DIST_OTHER, &mfs_head, &mfs_tail);
6187c478bd9Sstevel@tonic-gate 		if (!p)
6197c478bd9Sstevel@tonic-gate 			return (NULL);
6207c478bd9Sstevel@tonic-gate 	}
6217c478bd9Sstevel@tonic-gate 	if (m1)
6227c478bd9Sstevel@tonic-gate 		free_mfs(m1);
6237c478bd9Sstevel@tonic-gate 
6247c478bd9Sstevel@tonic-gate done:
6257c478bd9Sstevel@tonic-gate 	dump_mfs(mfs_head, "  enum_servers: output: ", 1);
6267c478bd9Sstevel@tonic-gate 	return (mfs_head);
6277c478bd9Sstevel@tonic-gate }
6287c478bd9Sstevel@tonic-gate 
6297c478bd9Sstevel@tonic-gate static enum nfsstat
nfsmount(struct mapfs * mfs_in,char * mntpnt,char * opts,int overlay,uid_t uid,action_list * alp)63039d3e169Sevanl nfsmount(
63139d3e169Sevanl 	struct mapfs *mfs_in,
63239d3e169Sevanl 	char *mntpnt, char *opts,
633bd93c05dSAlexander Eremin 	int overlay,
6343bfb48feSsemery 	uid_t uid,
63539d3e169Sevanl 	action_list *alp)
6367c478bd9Sstevel@tonic-gate {
6377c478bd9Sstevel@tonic-gate 	CLIENT *cl;
6387c478bd9Sstevel@tonic-gate 	char remname[MAXPATHLEN], *mnttabtext = NULL;
6397c478bd9Sstevel@tonic-gate 	char mopts[MAX_MNTOPT_STR];
6407c478bd9Sstevel@tonic-gate 	char netname[MAXNETNAMELEN+1];
64139d3e169Sevanl 	char	*mntopts = NULL;
6427c478bd9Sstevel@tonic-gate 	int mnttabcnt = 0;
6437c478bd9Sstevel@tonic-gate 	int loglevel;
6447c478bd9Sstevel@tonic-gate 	struct mnttab m;
6457c478bd9Sstevel@tonic-gate 	struct nfs_args *argp = NULL, *head = NULL, *tail = NULL,
6461bbc88acSrm 	    *prevhead, *prevtail;
6477c478bd9Sstevel@tonic-gate 	int flags;
6487c478bd9Sstevel@tonic-gate 	struct fhstatus fhs;
6497c478bd9Sstevel@tonic-gate 	struct timeval timeout;
6507c478bd9Sstevel@tonic-gate 	enum clnt_stat rpc_stat;
6517c478bd9Sstevel@tonic-gate 	enum nfsstat status;
6527c478bd9Sstevel@tonic-gate 	struct stat stbuf;
6537c478bd9Sstevel@tonic-gate 	struct netconfig *nconf;
6547c478bd9Sstevel@tonic-gate 	rpcvers_t vers, versmin; /* used to negotiate nfs version in pingnfs */
6557c478bd9Sstevel@tonic-gate 				/* and mount version with mountd */
6567c478bd9Sstevel@tonic-gate 	rpcvers_t outvers;	/* final version to be used during mount() */
6577c478bd9Sstevel@tonic-gate 	rpcvers_t nfsvers;	/* version in map options, 0 if not there */
6587c478bd9Sstevel@tonic-gate 	rpcvers_t mountversmax;	/* tracks the max mountvers during retries */
6597c478bd9Sstevel@tonic-gate 
6607c478bd9Sstevel@tonic-gate 	/* used to negotiate nfs version using webnfs */
6617c478bd9Sstevel@tonic-gate 	rpcvers_t pubvers, pubversmin, pubversmax;
6627c478bd9Sstevel@tonic-gate 	int posix;
6637c478bd9Sstevel@tonic-gate 	struct nd_addrlist *retaddrs;
6647c478bd9Sstevel@tonic-gate 	struct mountres3 res3;
6657c478bd9Sstevel@tonic-gate 	nfs_fh3 fh3;
6667c478bd9Sstevel@tonic-gate 	char *fstype;
6677c478bd9Sstevel@tonic-gate 	int count, i;
6687c478bd9Sstevel@tonic-gate 	char scerror_msg[MAXMSGLEN];
6697c478bd9Sstevel@tonic-gate 	int *auths;
6707c478bd9Sstevel@tonic-gate 	int delay;
6717c478bd9Sstevel@tonic-gate 	int retries;
6727c478bd9Sstevel@tonic-gate 	char *nfs_proto = NULL;
6737c478bd9Sstevel@tonic-gate 	uint_t nfs_port = 0;
6746a6d3e5eSjs 	char *p, *host, *rhost, *dir;
6757c478bd9Sstevel@tonic-gate 	struct mapfs *mfs = NULL;
6767c478bd9Sstevel@tonic-gate 	int error, last_error = 0;
6777c478bd9Sstevel@tonic-gate 	int replicated;
6787c478bd9Sstevel@tonic-gate 	int entries = 0;
6797c478bd9Sstevel@tonic-gate 	int v2cnt = 0, v3cnt = 0, v4cnt = 0;
6807c478bd9Sstevel@tonic-gate 	int v2near = 0, v3near = 0, v4near = 0;
6817c478bd9Sstevel@tonic-gate 	int skipentry = 0;
6827c478bd9Sstevel@tonic-gate 	char *nfs_flavor;
6837c478bd9Sstevel@tonic-gate 	seconfig_t nfs_sec;
6847c478bd9Sstevel@tonic-gate 	int sec_opt, scerror;
6857c478bd9Sstevel@tonic-gate 	struct sec_data *secdata;
6867c478bd9Sstevel@tonic-gate 	int secflags;
6877c478bd9Sstevel@tonic-gate 	struct netbuf *syncaddr;
6887c478bd9Sstevel@tonic-gate 	bool_t	use_pubfh;
6897c478bd9Sstevel@tonic-gate 	ushort_t thisport;
6907c478bd9Sstevel@tonic-gate 	int got_val;
6917c478bd9Sstevel@tonic-gate 	mfs_snego_t mfssnego_init, mfssnego;
6927c478bd9Sstevel@tonic-gate 
6937c478bd9Sstevel@tonic-gate 	dump_mfs(mfs_in, "  nfsmount: input: ", 2);
6947c478bd9Sstevel@tonic-gate 	replicated = (mfs_in->mfs_next != NULL);
6957c478bd9Sstevel@tonic-gate 	m.mnt_mntopts = opts;
6967c478bd9Sstevel@tonic-gate 	if (replicated && hasmntopt(&m, MNTOPT_SOFT)) {
6977c478bd9Sstevel@tonic-gate 		if (verbose)
6987c478bd9Sstevel@tonic-gate 			syslog(LOG_WARNING,
6997c478bd9Sstevel@tonic-gate 		    "mount on %s is soft and will not be replicated.", mntpnt);
7007c478bd9Sstevel@tonic-gate 		replicated = 0;
7017c478bd9Sstevel@tonic-gate 	}
7027c478bd9Sstevel@tonic-gate 	if (replicated && !hasmntopt(&m, MNTOPT_RO)) {
7037c478bd9Sstevel@tonic-gate 		if (verbose)
7047c478bd9Sstevel@tonic-gate 			syslog(LOG_WARNING,
7057c478bd9Sstevel@tonic-gate 		    "mount on %s is not read-only and will not be replicated.",
7067c478bd9Sstevel@tonic-gate 			    mntpnt);
7077c478bd9Sstevel@tonic-gate 		replicated = 0;
7087c478bd9Sstevel@tonic-gate 	}
7097c478bd9Sstevel@tonic-gate 	if (replicated)
7107c478bd9Sstevel@tonic-gate 		loglevel = LOG_WARNING;
7117c478bd9Sstevel@tonic-gate 	else
7127c478bd9Sstevel@tonic-gate 		loglevel = LOG_ERR;
7137c478bd9Sstevel@tonic-gate 
7147c478bd9Sstevel@tonic-gate 	if (trace > 1) {
7157c478bd9Sstevel@tonic-gate 		if (replicated)
7167c478bd9Sstevel@tonic-gate 			trace_prt(1, "	nfsmount: replicated mount on %s %s:\n",
7171bbc88acSrm 			    mntpnt, opts);
7187c478bd9Sstevel@tonic-gate 		else
7197c478bd9Sstevel@tonic-gate 			trace_prt(1, "	nfsmount: standard mount on %s %s:\n",
7201bbc88acSrm 			    mntpnt, opts);
7217c478bd9Sstevel@tonic-gate 		for (mfs = mfs_in; mfs; mfs = mfs->mfs_next)
7227c478bd9Sstevel@tonic-gate 			trace_prt(1, "	  %s:%s\n",
7231bbc88acSrm 			    mfs->mfs_host, mfs->mfs_dir);
7247c478bd9Sstevel@tonic-gate 	}
7257c478bd9Sstevel@tonic-gate 
7267c478bd9Sstevel@tonic-gate 	/*
7277c478bd9Sstevel@tonic-gate 	 * Make sure mountpoint is safe to mount on
7287c478bd9Sstevel@tonic-gate 	 */
7297c478bd9Sstevel@tonic-gate 	if (lstat(mntpnt, &stbuf) < 0) {
7307c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "Couldn't stat %s: %m", mntpnt);
7317c478bd9Sstevel@tonic-gate 		return (NFSERR_NOENT);
7327c478bd9Sstevel@tonic-gate 	}
7337c478bd9Sstevel@tonic-gate 
7347c478bd9Sstevel@tonic-gate 	/*
7357c478bd9Sstevel@tonic-gate 	 * Get protocol specified in options list, if any.
7367c478bd9Sstevel@tonic-gate 	 */
7377c478bd9Sstevel@tonic-gate 	if ((str_opt(&m, "proto", &nfs_proto)) == -1) {
7387c478bd9Sstevel@tonic-gate 		return (NFSERR_NOENT);
7397c478bd9Sstevel@tonic-gate 	}
7407c478bd9Sstevel@tonic-gate 
7417c478bd9Sstevel@tonic-gate 	/*
7427c478bd9Sstevel@tonic-gate 	 * Get port specified in options list, if any.
7437c478bd9Sstevel@tonic-gate 	 */
7447c478bd9Sstevel@tonic-gate 	got_val = nopt(&m, MNTOPT_PORT, (int *)&nfs_port);
7457c478bd9Sstevel@tonic-gate 	if (!got_val)
7467c478bd9Sstevel@tonic-gate 		nfs_port = 0;	/* "unspecified" */
7477c478bd9Sstevel@tonic-gate 	if (nfs_port > USHRT_MAX) {
7487c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "%s: invalid port number %d", mntpnt, nfs_port);
7497c478bd9Sstevel@tonic-gate 		return (NFSERR_NOENT);
7507c478bd9Sstevel@tonic-gate 	}
7517c478bd9Sstevel@tonic-gate 
7527c478bd9Sstevel@tonic-gate 	/*
7537c478bd9Sstevel@tonic-gate 	 * Set mount(2) flags here, outside of the loop.
7547c478bd9Sstevel@tonic-gate 	 */
7557c478bd9Sstevel@tonic-gate 	flags = MS_OPTIONSTR;
7567c478bd9Sstevel@tonic-gate 	flags |= (hasmntopt(&m, MNTOPT_RO) == NULL) ? 0 : MS_RDONLY;
7577c478bd9Sstevel@tonic-gate 	flags |= (hasmntopt(&m, MNTOPT_NOSUID) == NULL) ? 0 : MS_NOSUID;
7587c478bd9Sstevel@tonic-gate 	flags |= overlay ? MS_OVERLAY : 0;
7597c478bd9Sstevel@tonic-gate 	if (mntpnt[strlen(mntpnt) - 1] != ' ')
7607c478bd9Sstevel@tonic-gate 		/* direct mount point without offsets */
7617c478bd9Sstevel@tonic-gate 		flags |= MS_OVERLAY;
7627c478bd9Sstevel@tonic-gate 
7637c478bd9Sstevel@tonic-gate 	use_pubfh = (hasmntopt(&m, MNTOPT_PUBLIC) == NULL) ? FALSE : TRUE;
7647c478bd9Sstevel@tonic-gate 
7657c478bd9Sstevel@tonic-gate 	(void) memset(&mfssnego_init, 0, sizeof (mfs_snego_t));
7667c478bd9Sstevel@tonic-gate 	if (hasmntopt(&m, MNTOPT_SECURE) != NULL) {
7677c478bd9Sstevel@tonic-gate 		if (++mfssnego_init.sec_opt > 1) {
7687c478bd9Sstevel@tonic-gate 			syslog(loglevel,
7697c478bd9Sstevel@tonic-gate 			    "conflicting security options");
7707c478bd9Sstevel@tonic-gate 			return (NFSERR_IO);
7717c478bd9Sstevel@tonic-gate 		}
7727c478bd9Sstevel@tonic-gate 		if (nfs_getseconfig_byname("dh", &mfssnego_init.nfs_sec)) {
7737c478bd9Sstevel@tonic-gate 			syslog(loglevel,
7747c478bd9Sstevel@tonic-gate 			    "error getting dh information from %s",
7757c478bd9Sstevel@tonic-gate 			    NFSSEC_CONF);
7767c478bd9Sstevel@tonic-gate 			return (NFSERR_IO);
7777c478bd9Sstevel@tonic-gate 		}
7787c478bd9Sstevel@tonic-gate 	}
7797c478bd9Sstevel@tonic-gate 
780d7c6abe8Sjasmith 	if (hasmntopt(&m, MNTOPT_SEC) != NULL) {
7817c478bd9Sstevel@tonic-gate 		if ((str_opt(&m, MNTOPT_SEC,
7821bbc88acSrm 		    &mfssnego_init.nfs_flavor)) == -1) {
7837c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "nfsmount: no memory");
7847c478bd9Sstevel@tonic-gate 			return (NFSERR_IO);
7857c478bd9Sstevel@tonic-gate 		}
7867c478bd9Sstevel@tonic-gate 	}
7877c478bd9Sstevel@tonic-gate 
7887c478bd9Sstevel@tonic-gate 	if (mfssnego_init.nfs_flavor) {
7897c478bd9Sstevel@tonic-gate 		if (++mfssnego_init.sec_opt > 1) {
7907c478bd9Sstevel@tonic-gate 			syslog(loglevel,
7917c478bd9Sstevel@tonic-gate 			    "conflicting security options");
7927c478bd9Sstevel@tonic-gate 			free(mfssnego_init.nfs_flavor);
7937c478bd9Sstevel@tonic-gate 			return (NFSERR_IO);
7947c478bd9Sstevel@tonic-gate 		}
7957c478bd9Sstevel@tonic-gate 		if (nfs_getseconfig_byname(mfssnego_init.nfs_flavor,
7961bbc88acSrm 		    &mfssnego_init.nfs_sec)) {
7977c478bd9Sstevel@tonic-gate 			syslog(loglevel,
7987c478bd9Sstevel@tonic-gate 			    "error getting %s information from %s",
7997c478bd9Sstevel@tonic-gate 			    mfssnego_init.nfs_flavor, NFSSEC_CONF);
8007c478bd9Sstevel@tonic-gate 			free(mfssnego_init.nfs_flavor);
8017c478bd9Sstevel@tonic-gate 			return (NFSERR_IO);
8027c478bd9Sstevel@tonic-gate 		}
8037c478bd9Sstevel@tonic-gate 		free(mfssnego_init.nfs_flavor);
8047c478bd9Sstevel@tonic-gate 	}
8057c478bd9Sstevel@tonic-gate 
8067c478bd9Sstevel@tonic-gate nextentry:
8077c478bd9Sstevel@tonic-gate 	skipentry = 0;
8087c478bd9Sstevel@tonic-gate 
8097c478bd9Sstevel@tonic-gate 	got_val = nopt(&m, MNTOPT_VERS, (int *)&nfsvers);
8107c478bd9Sstevel@tonic-gate 	if (!got_val)
8117c478bd9Sstevel@tonic-gate 		nfsvers = 0;	/* "unspecified" */
8127c478bd9Sstevel@tonic-gate 	if (set_versrange(nfsvers, &vers, &versmin) != 0) {
8137c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "Incorrect NFS version specified for %s",
8141bbc88acSrm 		    mntpnt);
8157c478bd9Sstevel@tonic-gate 		last_error = NFSERR_NOENT;
8167c478bd9Sstevel@tonic-gate 		goto ret;
8177c478bd9Sstevel@tonic-gate 	}
8187c478bd9Sstevel@tonic-gate 
8197c478bd9Sstevel@tonic-gate 	if (nfsvers != 0) {
8207c478bd9Sstevel@tonic-gate 		pubversmax = pubversmin = nfsvers;
8217c478bd9Sstevel@tonic-gate 	} else {
8227c478bd9Sstevel@tonic-gate 		pubversmax = vers;
8237c478bd9Sstevel@tonic-gate 		pubversmin = versmin;
8247c478bd9Sstevel@tonic-gate 	}
8257c478bd9Sstevel@tonic-gate 
8267c478bd9Sstevel@tonic-gate 	/*
8277c478bd9Sstevel@tonic-gate 	 * Walk the whole list, pinging and collecting version
8287c478bd9Sstevel@tonic-gate 	 * info so that we can make sure the mount will be
8297c478bd9Sstevel@tonic-gate 	 * homogeneous with respect to version.
8307c478bd9Sstevel@tonic-gate 	 *
8317c478bd9Sstevel@tonic-gate 	 * If we have a version preference, this is easy; we'll
8327c478bd9Sstevel@tonic-gate 	 * just reject anything that doesn't match.
8337c478bd9Sstevel@tonic-gate 	 *
8347c478bd9Sstevel@tonic-gate 	 * If not, we want to try to provide the best compromise
8357c478bd9Sstevel@tonic-gate 	 * that considers proximity, preference for a higher version,
8367c478bd9Sstevel@tonic-gate 	 * sorted order, and number of replicas.  We will count
8377c478bd9Sstevel@tonic-gate 	 * the number of V2 and V3 replicas and also the number
8387c478bd9Sstevel@tonic-gate 	 * which are "near", i.e. the localhost or on the same
8397c478bd9Sstevel@tonic-gate 	 * subnet.
8407c478bd9Sstevel@tonic-gate 	 */
8417c478bd9Sstevel@tonic-gate 	for (mfs = mfs_in; mfs; mfs = mfs->mfs_next) {
8427c478bd9Sstevel@tonic-gate 
8437c478bd9Sstevel@tonic-gate 
8447c478bd9Sstevel@tonic-gate 		if (mfs->mfs_ignore)
8457c478bd9Sstevel@tonic-gate 			continue;
8467c478bd9Sstevel@tonic-gate 
8476a6d3e5eSjs 		/*
8486a6d3e5eSjs 		 * If the host is '[a:d:d:r:e:s:s'],
8496a6d3e5eSjs 		 * only use 'a:d:d:r:e:s:s' for communication
8506a6d3e5eSjs 		 */
8516a6d3e5eSjs 		host = strdup(mfs->mfs_host);
8526a6d3e5eSjs 		if (host == NULL) {
8536a6d3e5eSjs 			syslog(LOG_ERR, "nfsmount: no memory");
8546a6d3e5eSjs 			last_error = NFSERR_IO;
8556a6d3e5eSjs 			goto out;
8566a6d3e5eSjs 		}
8576a6d3e5eSjs 		unbracket(&host);
8586a6d3e5eSjs 
8597c478bd9Sstevel@tonic-gate 		(void) memcpy(&mfssnego, &mfssnego_init, sizeof (mfs_snego_t));
8607c478bd9Sstevel@tonic-gate 
8617c478bd9Sstevel@tonic-gate 		if (use_pubfh == TRUE || mfs->mfs_flags & MFS_URL) {
8627c478bd9Sstevel@tonic-gate 			char *path;
8637c478bd9Sstevel@tonic-gate 
8647c478bd9Sstevel@tonic-gate 			if (nfs_port != 0 && mfs->mfs_port != 0 &&
8657c478bd9Sstevel@tonic-gate 			    nfs_port != mfs->mfs_port) {
8667c478bd9Sstevel@tonic-gate 
8677c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR, "nfsmount: port (%u) in nfs URL"
8681bbc88acSrm 				    " not the same as port (%d) in port "
8691bbc88acSrm 				    "option\n", mfs->mfs_port, nfs_port);
8707c478bd9Sstevel@tonic-gate 				last_error = NFSERR_IO;
8717c478bd9Sstevel@tonic-gate 				goto out;
8727c478bd9Sstevel@tonic-gate 
8737c478bd9Sstevel@tonic-gate 			} else if (nfs_port != 0)
8747c478bd9Sstevel@tonic-gate 				thisport = nfs_port;
8757c478bd9Sstevel@tonic-gate 			else
8767c478bd9Sstevel@tonic-gate 				thisport = mfs->mfs_port;
8777c478bd9Sstevel@tonic-gate 
8787c478bd9Sstevel@tonic-gate 			dir = mfs->mfs_dir;
8797c478bd9Sstevel@tonic-gate 
8807c478bd9Sstevel@tonic-gate 			if ((mfs->mfs_flags & MFS_URL) == 0) {
8817c478bd9Sstevel@tonic-gate 				path = malloc(strlen(dir) + 2);
8827c478bd9Sstevel@tonic-gate 				if (path == NULL) {
8837c478bd9Sstevel@tonic-gate 					syslog(LOG_ERR, "nfsmount: no memory");
8847c478bd9Sstevel@tonic-gate 					last_error = NFSERR_IO;
8857c478bd9Sstevel@tonic-gate 					goto out;
8867c478bd9Sstevel@tonic-gate 				}
8877c478bd9Sstevel@tonic-gate 				path[0] = (char)WNL_NATIVEPATH;
8887c478bd9Sstevel@tonic-gate 				(void) strcpy(&path[1], dir);
8897c478bd9Sstevel@tonic-gate 			} else {
8907c478bd9Sstevel@tonic-gate 				path = dir;
8917c478bd9Sstevel@tonic-gate 			}
8927c478bd9Sstevel@tonic-gate 
8937c478bd9Sstevel@tonic-gate 			argp = (struct nfs_args *)
8941bbc88acSrm 			    malloc(sizeof (struct nfs_args));
8957c478bd9Sstevel@tonic-gate 
8967c478bd9Sstevel@tonic-gate 			if (!argp) {
8977c478bd9Sstevel@tonic-gate 				if (path != dir)
8987c478bd9Sstevel@tonic-gate 					free(path);
8997c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR, "nfsmount: no memory");
9007c478bd9Sstevel@tonic-gate 				last_error = NFSERR_IO;
9017c478bd9Sstevel@tonic-gate 				goto out;
9027c478bd9Sstevel@tonic-gate 			}
9037c478bd9Sstevel@tonic-gate 			(void) memset(argp, 0, sizeof (*argp));
9047c478bd9Sstevel@tonic-gate 
9057c478bd9Sstevel@tonic-gate 			/*
9067c478bd9Sstevel@tonic-gate 			 * RDMA support
9077c478bd9Sstevel@tonic-gate 			 * By now Mount argument struct has been allocated,
9087c478bd9Sstevel@tonic-gate 			 * either a pub_fh path will be taken or the regular
9097c478bd9Sstevel@tonic-gate 			 * one. So here if a protocol was specified and it
9107c478bd9Sstevel@tonic-gate 			 * was not rdma we let it be, else we set DO_RDMA.
9117c478bd9Sstevel@tonic-gate 			 * If no proto was there we advise on trying RDMA.
9127c478bd9Sstevel@tonic-gate 			 */
9137c478bd9Sstevel@tonic-gate 			if (nfs_proto) {
9147c478bd9Sstevel@tonic-gate 				if (strcmp(nfs_proto, "rdma") == 0) {
9157c478bd9Sstevel@tonic-gate 					free(nfs_proto);
9167c478bd9Sstevel@tonic-gate 					nfs_proto = NULL;
9177c478bd9Sstevel@tonic-gate 					argp->flags |= NFSMNT_DORDMA;
9187c478bd9Sstevel@tonic-gate 				}
9197c478bd9Sstevel@tonic-gate 			} else
9207c478bd9Sstevel@tonic-gate 				argp->flags |= NFSMNT_TRYRDMA;
9217c478bd9Sstevel@tonic-gate 
9227c478bd9Sstevel@tonic-gate 			for (pubvers = pubversmax; pubvers >= pubversmin;
9237c478bd9Sstevel@tonic-gate 			    pubvers--) {
9247c478bd9Sstevel@tonic-gate 
9257c478bd9Sstevel@tonic-gate 				nconf = NULL;
9267c478bd9Sstevel@tonic-gate 				argp->addr = get_pubfh(host, pubvers, &mfssnego,
9277c478bd9Sstevel@tonic-gate 				    &nconf, nfs_proto, thisport, NULL,
9287c478bd9Sstevel@tonic-gate 				    &argp->fh, TRUE, path);
9297c478bd9Sstevel@tonic-gate 
9307c478bd9Sstevel@tonic-gate 				if (argp->addr != NULL)
9317c478bd9Sstevel@tonic-gate 					break;
9327c478bd9Sstevel@tonic-gate 
9337c478bd9Sstevel@tonic-gate 				if (nconf != NULL)
9347c478bd9Sstevel@tonic-gate 					freenetconfigent(nconf);
9357c478bd9Sstevel@tonic-gate 			}
9367c478bd9Sstevel@tonic-gate 
9377c478bd9Sstevel@tonic-gate 			if (path != dir)
9387c478bd9Sstevel@tonic-gate 				free(path);
9397c478bd9Sstevel@tonic-gate 
9407c478bd9Sstevel@tonic-gate 			if (argp->addr != NULL) {
9417c478bd9Sstevel@tonic-gate 
9427c478bd9Sstevel@tonic-gate 				/*
9437c478bd9Sstevel@tonic-gate 				 * The use of llock option for NFSv4
9447c478bd9Sstevel@tonic-gate 				 * mounts is not required since file
9457c478bd9Sstevel@tonic-gate 				 * locking is included within the protocol
9467c478bd9Sstevel@tonic-gate 				 */
9477c478bd9Sstevel@tonic-gate 				if (pubvers != NFS_V4)
9487c478bd9Sstevel@tonic-gate 					argp->flags |= NFSMNT_LLOCK;
9497c478bd9Sstevel@tonic-gate 
9507c478bd9Sstevel@tonic-gate 				argp->flags |= NFSMNT_PUBLIC;
9517c478bd9Sstevel@tonic-gate 
9521bbc88acSrm 				vers = pubvers;
9537c478bd9Sstevel@tonic-gate 				mfs->mfs_args = argp;
9547c478bd9Sstevel@tonic-gate 				mfs->mfs_version = pubvers;
9557c478bd9Sstevel@tonic-gate 				mfs->mfs_nconf = nconf;
9567c478bd9Sstevel@tonic-gate 				mfs->mfs_flags |= MFS_FH_VIA_WEBNFS;
9577c478bd9Sstevel@tonic-gate 
9587c478bd9Sstevel@tonic-gate 			} else {
9597c478bd9Sstevel@tonic-gate 				free(argp);
9607c478bd9Sstevel@tonic-gate 
9617c478bd9Sstevel@tonic-gate 				/*
9627c478bd9Sstevel@tonic-gate 				 * If -public was specified, give up
9637c478bd9Sstevel@tonic-gate 				 * on this entry now.
9647c478bd9Sstevel@tonic-gate 				 */
9657c478bd9Sstevel@tonic-gate 				if (use_pubfh == TRUE) {
9667c478bd9Sstevel@tonic-gate 					syslog(loglevel,
9677c478bd9Sstevel@tonic-gate 					    "%s: no public file handle support",
9687c478bd9Sstevel@tonic-gate 					    host);
9697c478bd9Sstevel@tonic-gate 					last_error = NFSERR_NOENT;
9707c478bd9Sstevel@tonic-gate 					mfs->mfs_ignore = 1;
9717c478bd9Sstevel@tonic-gate 					continue;
9727c478bd9Sstevel@tonic-gate 				}
9737c478bd9Sstevel@tonic-gate 
9747c478bd9Sstevel@tonic-gate 				/*
9757c478bd9Sstevel@tonic-gate 				 * Back off to a conventional mount.
9767c478bd9Sstevel@tonic-gate 				 *
9777c478bd9Sstevel@tonic-gate 				 * URL's can contain escape characters. Get
9787c478bd9Sstevel@tonic-gate 				 * rid of them.
9797c478bd9Sstevel@tonic-gate 				 */
9807c478bd9Sstevel@tonic-gate 				path = malloc(strlen(dir) + 2);
9817c478bd9Sstevel@tonic-gate 
9827c478bd9Sstevel@tonic-gate 				if (path == NULL) {
9837c478bd9Sstevel@tonic-gate 					syslog(LOG_ERR, "nfsmount: no memory");
9847c478bd9Sstevel@tonic-gate 					last_error = NFSERR_IO;
9857c478bd9Sstevel@tonic-gate 					goto out;
9867c478bd9Sstevel@tonic-gate 				}
9877c478bd9Sstevel@tonic-gate 
9887c478bd9Sstevel@tonic-gate 				strcpy(path, dir);
9897c478bd9Sstevel@tonic-gate 				URLparse(path);
9907c478bd9Sstevel@tonic-gate 				mfs->mfs_dir = path;
9917c478bd9Sstevel@tonic-gate 				mfs->mfs_flags |= MFS_ALLOC_DIR;
9927c478bd9Sstevel@tonic-gate 				mfs->mfs_flags &= ~MFS_URL;
9937c478bd9Sstevel@tonic-gate 			}
9947c478bd9Sstevel@tonic-gate 		}
9957c478bd9Sstevel@tonic-gate 
9967c478bd9Sstevel@tonic-gate 		if ((mfs->mfs_flags & MFS_FH_VIA_WEBNFS) ==  0) {
9977c478bd9Sstevel@tonic-gate 			i = pingnfs(host, get_retry(opts) + 1, &vers, versmin,
9981bbc88acSrm 			    0, FALSE, NULL, nfs_proto);
9997c478bd9Sstevel@tonic-gate 			if (i != RPC_SUCCESS) {
10007c478bd9Sstevel@tonic-gate 				if (i == RPC_PROGVERSMISMATCH) {
10017c478bd9Sstevel@tonic-gate 					syslog(loglevel, "server %s: NFS "
10021bbc88acSrm 					    "protocol version mismatch",
10031bbc88acSrm 					    host);
10047c478bd9Sstevel@tonic-gate 				} else {
10057c478bd9Sstevel@tonic-gate 					syslog(loglevel, "server %s not "
10061bbc88acSrm 					    "responding", host);
10077c478bd9Sstevel@tonic-gate 				}
10087c478bd9Sstevel@tonic-gate 				mfs->mfs_ignore = 1;
10097c478bd9Sstevel@tonic-gate 				last_error = NFSERR_NOENT;
10107c478bd9Sstevel@tonic-gate 				continue;
10117c478bd9Sstevel@tonic-gate 			}
10127c478bd9Sstevel@tonic-gate 			if (nfsvers != 0 && nfsvers != vers) {
10137c478bd9Sstevel@tonic-gate 				if (nfs_proto == NULL)
10147c478bd9Sstevel@tonic-gate 					syslog(loglevel,
10151bbc88acSrm 					    "NFS version %d "
10161bbc88acSrm 					    "not supported by %s",
10171bbc88acSrm 					    nfsvers, host);
10187c478bd9Sstevel@tonic-gate 				else
10197c478bd9Sstevel@tonic-gate 					syslog(loglevel,
10201bbc88acSrm 					    "NFS version %d "
10211bbc88acSrm 					    "with proto %s "
10221bbc88acSrm 					    "not supported by %s",
10231bbc88acSrm 					    nfsvers, nfs_proto, host);
10247c478bd9Sstevel@tonic-gate 				mfs->mfs_ignore = 1;
10257c478bd9Sstevel@tonic-gate 				last_error = NFSERR_NOENT;
10267c478bd9Sstevel@tonic-gate 				continue;
10277c478bd9Sstevel@tonic-gate 			}
10287c478bd9Sstevel@tonic-gate 		}
10297c478bd9Sstevel@tonic-gate 
10306a6d3e5eSjs 		free(host);
10316a6d3e5eSjs 
10327c478bd9Sstevel@tonic-gate 		switch (vers) {
10337c478bd9Sstevel@tonic-gate 		case NFS_V4: v4cnt++; break;
10347c478bd9Sstevel@tonic-gate 		case NFS_V3: v3cnt++; break;
10357c478bd9Sstevel@tonic-gate 		case NFS_VERSION: v2cnt++; break;
10367c478bd9Sstevel@tonic-gate 		default: break;
10377c478bd9Sstevel@tonic-gate 		}
10387c478bd9Sstevel@tonic-gate 
10397c478bd9Sstevel@tonic-gate 		/*
10407c478bd9Sstevel@tonic-gate 		 * It's not clear how useful this stuff is if
10417c478bd9Sstevel@tonic-gate 		 * we are using webnfs across the internet, but it
10427c478bd9Sstevel@tonic-gate 		 * can't hurt.
10437c478bd9Sstevel@tonic-gate 		 */
10447c478bd9Sstevel@tonic-gate 		if (mfs->mfs_distance &&
10457c478bd9Sstevel@tonic-gate 		    mfs->mfs_distance <= DIST_MYSUB) {
10467c478bd9Sstevel@tonic-gate 			switch (vers) {
10477c478bd9Sstevel@tonic-gate 			case NFS_V4: v4near++; break;
10487c478bd9Sstevel@tonic-gate 			case NFS_V3: v3near++; break;
10497c478bd9Sstevel@tonic-gate 			case NFS_VERSION: v2near++; break;
10507c478bd9Sstevel@tonic-gate 			default: break;
10517c478bd9Sstevel@tonic-gate 			}
10527c478bd9Sstevel@tonic-gate 		}
10537c478bd9Sstevel@tonic-gate 
10547c478bd9Sstevel@tonic-gate 		/*
10557c478bd9Sstevel@tonic-gate 		 * If the mount is not replicated, we don't want to
10567c478bd9Sstevel@tonic-gate 		 * ping every entry, so we'll stop here.  This means
10577c478bd9Sstevel@tonic-gate 		 * that we may have to go back to "nextentry" above
10586a6d3e5eSjs 		 * to consider another entry if we can't get
10597c478bd9Sstevel@tonic-gate 		 * all the way to mount(2) with this one.
10607c478bd9Sstevel@tonic-gate 		 */
10617c478bd9Sstevel@tonic-gate 		if (!replicated)
10627c478bd9Sstevel@tonic-gate 			break;
10636a6d3e5eSjs 
10647c478bd9Sstevel@tonic-gate 	}
10657c478bd9Sstevel@tonic-gate 
10667c478bd9Sstevel@tonic-gate 	if (nfsvers == 0) {
10677c478bd9Sstevel@tonic-gate 		/*
10687c478bd9Sstevel@tonic-gate 		 * Choose the NFS version.
10697c478bd9Sstevel@tonic-gate 		 * We prefer higher versions, but will choose a one-
10707c478bd9Sstevel@tonic-gate 		 * version downgrade in service if we can use a local
10717c478bd9Sstevel@tonic-gate 		 * network interface and avoid a router.
10727c478bd9Sstevel@tonic-gate 		 */
10737c478bd9Sstevel@tonic-gate 		if (v4cnt && v4cnt >= v3cnt && (v4near || !v3near))
10747c478bd9Sstevel@tonic-gate 			nfsvers = NFS_V4;
10757c478bd9Sstevel@tonic-gate 		else if (v3cnt && v3cnt >= v2cnt && (v3near || !v2near))
10767c478bd9Sstevel@tonic-gate 			nfsvers = NFS_V3;
10777c478bd9Sstevel@tonic-gate 		else
10787c478bd9Sstevel@tonic-gate 			nfsvers = NFS_VERSION;
10797c478bd9Sstevel@tonic-gate 		if (trace > 2)
10807c478bd9Sstevel@tonic-gate 			trace_prt(1,
10817c478bd9Sstevel@tonic-gate 		    "  nfsmount: v4=%d[%d]v3=%d[%d],v2=%d[%d] => v%d.\n",
10827c478bd9Sstevel@tonic-gate 			    v4cnt, v4near, v3cnt, v3near,
10837c478bd9Sstevel@tonic-gate 			    v2cnt, v2near, nfsvers);
10847c478bd9Sstevel@tonic-gate 	}
10857c478bd9Sstevel@tonic-gate 
10867c478bd9Sstevel@tonic-gate 	/*
10877c478bd9Sstevel@tonic-gate 	 * Since we don't support different NFS versions in replicated
10887c478bd9Sstevel@tonic-gate 	 * mounts, set fstype now.
10897c478bd9Sstevel@tonic-gate 	 * Also take the opportunity to set
10907c478bd9Sstevel@tonic-gate 	 * the mount protocol version as appropriate.
10917c478bd9Sstevel@tonic-gate 	 */
10927c478bd9Sstevel@tonic-gate 	switch (nfsvers) {
10937c478bd9Sstevel@tonic-gate 	case NFS_V4:
10947c478bd9Sstevel@tonic-gate 		fstype = MNTTYPE_NFS4;
10957c478bd9Sstevel@tonic-gate 		break;
10967c478bd9Sstevel@tonic-gate 	case NFS_V3:
10977c478bd9Sstevel@tonic-gate 		fstype = MNTTYPE_NFS3;
10987c478bd9Sstevel@tonic-gate 		if (use_pubfh == FALSE) {
10997c478bd9Sstevel@tonic-gate 			mountversmax = MOUNTVERS3;
11007c478bd9Sstevel@tonic-gate 			versmin = MOUNTVERS3;
11017c478bd9Sstevel@tonic-gate 		}
11027c478bd9Sstevel@tonic-gate 		break;
11037c478bd9Sstevel@tonic-gate 	case NFS_VERSION:
11047c478bd9Sstevel@tonic-gate 		fstype = MNTTYPE_NFS;
11057c478bd9Sstevel@tonic-gate 		if (use_pubfh == FALSE) {
11067c478bd9Sstevel@tonic-gate 			mountversmax = MOUNTVERS_POSIX;
11077c478bd9Sstevel@tonic-gate 			versmin = MOUNTVERS;
11087c478bd9Sstevel@tonic-gate 		}
11097c478bd9Sstevel@tonic-gate 		break;
11107c478bd9Sstevel@tonic-gate 	}
11117c478bd9Sstevel@tonic-gate 
11127c478bd9Sstevel@tonic-gate 	/*
11137c478bd9Sstevel@tonic-gate 	 * Our goal here is to evaluate each of several possible
11147c478bd9Sstevel@tonic-gate 	 * replicas and try to come up with a list we can hand
11157c478bd9Sstevel@tonic-gate 	 * to mount(2).  If we don't have a valid "head" at the
11167c478bd9Sstevel@tonic-gate 	 * end of this process, it means we have rejected all
11177c478bd9Sstevel@tonic-gate 	 * potential server:/path tuples.  We will fail quietly
11187c478bd9Sstevel@tonic-gate 	 * in front of mount(2), and will have printed errors
11197c478bd9Sstevel@tonic-gate 	 * where we found them.
11207c478bd9Sstevel@tonic-gate 	 * XXX - do option work outside loop w careful design
11217c478bd9Sstevel@tonic-gate 	 * XXX - use macro for error condition free handling
11227c478bd9Sstevel@tonic-gate 	 */
11237c478bd9Sstevel@tonic-gate 	for (mfs = mfs_in; mfs; mfs = mfs->mfs_next) {
11247c478bd9Sstevel@tonic-gate 
11257c478bd9Sstevel@tonic-gate 		/*
11267c478bd9Sstevel@tonic-gate 		 * Initialize retry and delay values on a per-server basis.
11277c478bd9Sstevel@tonic-gate 		 */
11287c478bd9Sstevel@tonic-gate 		retries = get_retry(opts);
11297c478bd9Sstevel@tonic-gate 		delay = INITDELAY;
11307c478bd9Sstevel@tonic-gate retry:
11317c478bd9Sstevel@tonic-gate 		if (mfs->mfs_ignore)
11327c478bd9Sstevel@tonic-gate 			continue;
11337c478bd9Sstevel@tonic-gate 
11347c478bd9Sstevel@tonic-gate 		/*
11357c478bd9Sstevel@tonic-gate 		 * If we don't have a fh yet, and if this is not a replicated
11367c478bd9Sstevel@tonic-gate 		 * mount, we haven't done a pingnfs() on the next entry,
11377c478bd9Sstevel@tonic-gate 		 * so we don't know if the next entry is up or if it
11387c478bd9Sstevel@tonic-gate 		 * supports an NFS version we like.  So if we had a problem
11397c478bd9Sstevel@tonic-gate 		 * with an entry, we need to go back and run through some new
11407c478bd9Sstevel@tonic-gate 		 * code.
11417c478bd9Sstevel@tonic-gate 		 */
11427c478bd9Sstevel@tonic-gate 		if ((mfs->mfs_flags & MFS_FH_VIA_WEBNFS) == 0 &&
11437c478bd9Sstevel@tonic-gate 		    !replicated && skipentry)
11447c478bd9Sstevel@tonic-gate 			goto nextentry;
11457c478bd9Sstevel@tonic-gate 
11467c478bd9Sstevel@tonic-gate 		vers = mountversmax;
11477c478bd9Sstevel@tonic-gate 		host = mfs->mfs_host;
11487c478bd9Sstevel@tonic-gate 		dir = mfs->mfs_dir;
11496a6d3e5eSjs 
11506a6d3e5eSjs 		/*
11516a6d3e5eSjs 		 * Remember the possible '[a:d:d:r:e:s:s]' as the address to be
11526a6d3e5eSjs 		 * later passed to mount(2) and used in the mnttab line, but
11536a6d3e5eSjs 		 * only use 'a:d:d:r:e:s:s' for communication
11546a6d3e5eSjs 		 */
11556a6d3e5eSjs 		rhost = strdup(host);
11566a6d3e5eSjs 		if (rhost == NULL) {
11576a6d3e5eSjs 			syslog(LOG_ERR, "nfsmount: no memory");
11586a6d3e5eSjs 			last_error = NFSERR_IO;
11596a6d3e5eSjs 			goto out;
11606a6d3e5eSjs 		}
11616a6d3e5eSjs 		unbracket(&host);
11626a6d3e5eSjs 
11636a6d3e5eSjs 		(void) sprintf(remname, "%s:%s", rhost, dir);
11647c478bd9Sstevel@tonic-gate 		if (trace > 4 && replicated)
11657c478bd9Sstevel@tonic-gate 			trace_prt(1, "	nfsmount: examining %s\n", remname);
11667c478bd9Sstevel@tonic-gate 
11677c478bd9Sstevel@tonic-gate 		if (mfs->mfs_args == NULL) {
11687c478bd9Sstevel@tonic-gate 
11697c478bd9Sstevel@tonic-gate 			/*
11707c478bd9Sstevel@tonic-gate 			 * Allocate nfs_args structure
11717c478bd9Sstevel@tonic-gate 			 */
11727c478bd9Sstevel@tonic-gate 			argp = (struct nfs_args *)
11731bbc88acSrm 			    malloc(sizeof (struct nfs_args));
11747c478bd9Sstevel@tonic-gate 
11757c478bd9Sstevel@tonic-gate 			if (!argp) {
11767c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR, "nfsmount: no memory");
11777c478bd9Sstevel@tonic-gate 				last_error = NFSERR_IO;
11787c478bd9Sstevel@tonic-gate 				goto out;
11797c478bd9Sstevel@tonic-gate 			}
11807c478bd9Sstevel@tonic-gate 
11817c478bd9Sstevel@tonic-gate 			(void) memset(argp, 0, sizeof (*argp));
11827c478bd9Sstevel@tonic-gate 
11837c478bd9Sstevel@tonic-gate 			/*
11847c478bd9Sstevel@tonic-gate 			 * RDMA support
11857c478bd9Sstevel@tonic-gate 			 * By now Mount argument struct has been allocated,
11867c478bd9Sstevel@tonic-gate 			 * either a pub_fh path will be taken or the regular
11877c478bd9Sstevel@tonic-gate 			 * one. So here if a protocol was specified and it
11887c478bd9Sstevel@tonic-gate 			 * was not rdma we let it be, else we set DO_RDMA.
11897c478bd9Sstevel@tonic-gate 			 * If no proto was there we advise on trying RDMA.
11907c478bd9Sstevel@tonic-gate 			 */
11917c478bd9Sstevel@tonic-gate 			if (nfs_proto) {
11927c478bd9Sstevel@tonic-gate 				if (strcmp(nfs_proto, "rdma") == 0) {
11937c478bd9Sstevel@tonic-gate 					free(nfs_proto);
11947c478bd9Sstevel@tonic-gate 					nfs_proto = NULL;
11957c478bd9Sstevel@tonic-gate 					argp->flags |= NFSMNT_DORDMA;
11967c478bd9Sstevel@tonic-gate 				}
11977c478bd9Sstevel@tonic-gate 			} else
11987c478bd9Sstevel@tonic-gate 				argp->flags |= NFSMNT_TRYRDMA;
11997c478bd9Sstevel@tonic-gate 		} else {
12007c478bd9Sstevel@tonic-gate 			argp = mfs->mfs_args;
12017c478bd9Sstevel@tonic-gate 			mfs->mfs_args = NULL;
12027c478bd9Sstevel@tonic-gate 
12037c478bd9Sstevel@tonic-gate 			/*
12047c478bd9Sstevel@tonic-gate 			 * Skip entry if we already have file handle but the
12057c478bd9Sstevel@tonic-gate 			 * NFS version is wrong.
12067c478bd9Sstevel@tonic-gate 			 */
12077c478bd9Sstevel@tonic-gate 			if ((mfs->mfs_flags & MFS_FH_VIA_WEBNFS) &&
12087c478bd9Sstevel@tonic-gate 			    mfs->mfs_version != nfsvers) {
12097c478bd9Sstevel@tonic-gate 
12107c478bd9Sstevel@tonic-gate 				free(argp);
12117c478bd9Sstevel@tonic-gate 				skipentry = 1;
12127c478bd9Sstevel@tonic-gate 				mfs->mfs_ignore = 1;
12137c478bd9Sstevel@tonic-gate 				continue;
12147c478bd9Sstevel@tonic-gate 			}
12157c478bd9Sstevel@tonic-gate 		}
12167c478bd9Sstevel@tonic-gate 
12177c478bd9Sstevel@tonic-gate 		prevhead = head;
12187c478bd9Sstevel@tonic-gate 		prevtail = tail;
12197c478bd9Sstevel@tonic-gate 		if (!head)
12207c478bd9Sstevel@tonic-gate 			head = tail = argp;
12217c478bd9Sstevel@tonic-gate 		else
12227c478bd9Sstevel@tonic-gate 			tail = tail->nfs_ext_u.nfs_extB.next = argp;
12237c478bd9Sstevel@tonic-gate 
12247c478bd9Sstevel@tonic-gate 		/*
12257c478bd9Sstevel@tonic-gate 		 * WebNFS and NFSv4 behave similarly in that they
12267c478bd9Sstevel@tonic-gate 		 * don't use the mount protocol.  Therefore, avoid
12277c478bd9Sstevel@tonic-gate 		 * mount protocol like things when version 4 is being
12287c478bd9Sstevel@tonic-gate 		 * used.
12297c478bd9Sstevel@tonic-gate 		 */
12307c478bd9Sstevel@tonic-gate 		if ((mfs->mfs_flags & MFS_FH_VIA_WEBNFS) == 0 &&
12311bbc88acSrm 		    nfsvers != NFS_V4) {
12321bbc88acSrm 			timeout.tv_usec = 0;
12331bbc88acSrm 			timeout.tv_sec = rpc_timeout;
12341bbc88acSrm 			rpc_stat = RPC_TIMEDOUT;
12357c478bd9Sstevel@tonic-gate 
12361bbc88acSrm 			/* Create the client handle. */
12377c478bd9Sstevel@tonic-gate 
12381bbc88acSrm 			if (trace > 1) {
12391bbc88acSrm 				trace_prt(1,
12401bbc88acSrm 				    "  nfsmount: Get mount version: request "
12411bbc88acSrm 				    "vers=%d min=%d\n", vers, versmin);
12427c478bd9Sstevel@tonic-gate 			}
12431bbc88acSrm 
12441bbc88acSrm 			while ((cl = clnt_create_vers(host, MOUNTPROG, &outvers,
12451bbc88acSrm 			    versmin, vers, "udp")) == NULL) {
12461bbc88acSrm 				if (trace > 4) {
12471bbc88acSrm 					trace_prt(1,
12481bbc88acSrm 					    "  nfsmount: Can't get mount "
12491bbc88acSrm 					    "version: rpcerr=%d\n",
12501bbc88acSrm 					    rpc_createerr.cf_stat);
12511bbc88acSrm 				}
12521bbc88acSrm 				if (rpc_createerr.cf_stat == RPC_UNKNOWNHOST ||
12531bbc88acSrm 				    rpc_createerr.cf_stat == RPC_TIMEDOUT)
12541bbc88acSrm 					break;
12557c478bd9Sstevel@tonic-gate 
12567c478bd9Sstevel@tonic-gate 			/*
12577c478bd9Sstevel@tonic-gate 			 * backoff and return lower version to retry the ping.
12587c478bd9Sstevel@tonic-gate 			 * XXX we should be more careful and handle
12597c478bd9Sstevel@tonic-gate 			 * RPC_PROGVERSMISMATCH here, because that error
12607c478bd9Sstevel@tonic-gate 			 * is handled in clnt_create_vers(). It's not done to
12617c478bd9Sstevel@tonic-gate 			 * stay in sync with the nfs mount command.
12627c478bd9Sstevel@tonic-gate 			 */
12631bbc88acSrm 				vers--;
12641bbc88acSrm 				if (vers < versmin)
12651bbc88acSrm 					break;
12661bbc88acSrm 				if (trace > 4) {
12671bbc88acSrm 					trace_prt(1,
12681bbc88acSrm 					    "  nfsmount: Try version=%d\n",
12691bbc88acSrm 					    vers);
12701bbc88acSrm 				}
12717c478bd9Sstevel@tonic-gate 			}
12727c478bd9Sstevel@tonic-gate 
12731bbc88acSrm 			if (cl == NULL) {
12741bbc88acSrm 				free(argp);
12751bbc88acSrm 				head = prevhead;
12761bbc88acSrm 				tail = prevtail;
12771bbc88acSrm 				if (tail)
12781bbc88acSrm 					tail->nfs_ext_u.nfs_extB.next = NULL;
12791bbc88acSrm 				last_error = NFSERR_NOENT;
12807c478bd9Sstevel@tonic-gate 
12811bbc88acSrm 				if (rpc_createerr.cf_stat != RPC_UNKNOWNHOST &&
12821bbc88acSrm 				    rpc_createerr.cf_stat !=
12831bbc88acSrm 				    RPC_PROGVERSMISMATCH &&
12841bbc88acSrm 				    retries-- > 0) {
12851bbc88acSrm 					DELAY(delay);
12861bbc88acSrm 					goto retry;
12871bbc88acSrm 				}
12887c478bd9Sstevel@tonic-gate 
12891bbc88acSrm 				syslog(loglevel, "%s %s", host,
12901bbc88acSrm 				    clnt_spcreateerror(
12911bbc88acSrm 				    "server not responding"));
12921bbc88acSrm 				skipentry = 1;
12931bbc88acSrm 				mfs->mfs_ignore = 1;
12941bbc88acSrm 				continue;
12951bbc88acSrm 			}
12961bbc88acSrm 			if (trace > 1) {
12971bbc88acSrm 				trace_prt(1,
12981bbc88acSrm 				    "	nfsmount: mount version=%d\n", outvers);
12991bbc88acSrm 			}
13007c478bd9Sstevel@tonic-gate #ifdef MALLOC_DEBUG
13011bbc88acSrm 			add_alloc("CLNT_HANDLE", cl, 0, __FILE__, __LINE__);
13021bbc88acSrm 			add_alloc("AUTH_HANDLE", cl->cl_auth, 0,
13031bbc88acSrm 			    __FILE__, __LINE__);
13047c478bd9Sstevel@tonic-gate #endif
13057c478bd9Sstevel@tonic-gate 
13061bbc88acSrm 			if (__clnt_bindresvport(cl) < 0) {
13071bbc88acSrm 				free(argp);
13081bbc88acSrm 				head = prevhead;
13091bbc88acSrm 				tail = prevtail;
13101bbc88acSrm 				if (tail)
13111bbc88acSrm 					tail->nfs_ext_u.nfs_extB.next = NULL;
13121bbc88acSrm 				last_error = NFSERR_NOENT;
13131bbc88acSrm 
13141bbc88acSrm 				if (retries-- > 0) {
13151bbc88acSrm 					destroy_auth_client_handle(cl);
13161bbc88acSrm 					DELAY(delay);
13171bbc88acSrm 					goto retry;
13181bbc88acSrm 				}
13197c478bd9Sstevel@tonic-gate 
13201bbc88acSrm 				syslog(loglevel, "mount %s: %s", host,
13211bbc88acSrm 				    "Couldn't bind to reserved port");
13227c478bd9Sstevel@tonic-gate 				destroy_auth_client_handle(cl);
13231bbc88acSrm 				skipentry = 1;
13241bbc88acSrm 				mfs->mfs_ignore = 1;
13251bbc88acSrm 				continue;
13267c478bd9Sstevel@tonic-gate 			}
13277c478bd9Sstevel@tonic-gate 
13287c478bd9Sstevel@tonic-gate #ifdef MALLOC_DEBUG
13291bbc88acSrm 			drop_alloc("AUTH_HANDLE", cl->cl_auth,
13301bbc88acSrm 			    __FILE__, __LINE__);
13317c478bd9Sstevel@tonic-gate #endif
13321bbc88acSrm 			AUTH_DESTROY(cl->cl_auth);
13331bbc88acSrm 			if ((cl->cl_auth = authsys_create_default()) == NULL) {
13341bbc88acSrm 				free(argp);
13351bbc88acSrm 				head = prevhead;
13361bbc88acSrm 				tail = prevtail;
13371bbc88acSrm 				if (tail)
13381bbc88acSrm 					tail->nfs_ext_u.nfs_extB.next = NULL;
13391bbc88acSrm 				last_error = NFSERR_NOENT;
13401bbc88acSrm 
13411bbc88acSrm 				if (retries-- > 0) {
13421bbc88acSrm 					destroy_auth_client_handle(cl);
13431bbc88acSrm 					DELAY(delay);
13441bbc88acSrm 					goto retry;
13451bbc88acSrm 				}
13467c478bd9Sstevel@tonic-gate 
13471bbc88acSrm 				syslog(loglevel, "mount %s: %s", host,
13481bbc88acSrm 				    "Failed creating default auth handle");
13497c478bd9Sstevel@tonic-gate 				destroy_auth_client_handle(cl);
13501bbc88acSrm 				skipentry = 1;
13511bbc88acSrm 				mfs->mfs_ignore = 1;
13521bbc88acSrm 				continue;
13537c478bd9Sstevel@tonic-gate 			}
13547c478bd9Sstevel@tonic-gate #ifdef MALLOC_DEBUG
13551bbc88acSrm 			add_alloc("AUTH_HANDLE", cl->cl_auth, 0,
13561bbc88acSrm 			    __FILE__, __LINE__);
13577c478bd9Sstevel@tonic-gate #endif
13587c478bd9Sstevel@tonic-gate 		} else
13591bbc88acSrm 			cl = NULL;
13607c478bd9Sstevel@tonic-gate 
13617c478bd9Sstevel@tonic-gate 		/*
13627c478bd9Sstevel@tonic-gate 		 * set security options
13637c478bd9Sstevel@tonic-gate 		 */
13647c478bd9Sstevel@tonic-gate 		sec_opt = 0;
13657c478bd9Sstevel@tonic-gate 		(void) memset(&nfs_sec, 0, sizeof (nfs_sec));
13667c478bd9Sstevel@tonic-gate 		if (hasmntopt(&m, MNTOPT_SECURE) != NULL) {
13677c478bd9Sstevel@tonic-gate 			if (++sec_opt > 1) {
13687c478bd9Sstevel@tonic-gate 				syslog(loglevel,
13697c478bd9Sstevel@tonic-gate 				    "conflicting security options for %s",
13707c478bd9Sstevel@tonic-gate 				    remname);
13717c478bd9Sstevel@tonic-gate 				free(argp);
13727c478bd9Sstevel@tonic-gate 				head = prevhead;
13737c478bd9Sstevel@tonic-gate 				tail = prevtail;
13747c478bd9Sstevel@tonic-gate 				if (tail)
13757c478bd9Sstevel@tonic-gate 					tail->nfs_ext_u.nfs_extB.next = NULL;
13767c478bd9Sstevel@tonic-gate 				last_error = NFSERR_IO;
13777c478bd9Sstevel@tonic-gate 				destroy_auth_client_handle(cl);
13787c478bd9Sstevel@tonic-gate 				skipentry = 1;
13797c478bd9Sstevel@tonic-gate 				mfs->mfs_ignore = 1;
13807c478bd9Sstevel@tonic-gate 				continue;
13817c478bd9Sstevel@tonic-gate 			}
13827c478bd9Sstevel@tonic-gate 			if (nfs_getseconfig_byname("dh", &nfs_sec)) {
13837c478bd9Sstevel@tonic-gate 				syslog(loglevel,
13847c478bd9Sstevel@tonic-gate 				    "error getting dh information from %s",
13857c478bd9Sstevel@tonic-gate 				    NFSSEC_CONF);
13867c478bd9Sstevel@tonic-gate 				free(argp);
13877c478bd9Sstevel@tonic-gate 				head = prevhead;
13887c478bd9Sstevel@tonic-gate 				tail = prevtail;
13897c478bd9Sstevel@tonic-gate 				if (tail)
13907c478bd9Sstevel@tonic-gate 					tail->nfs_ext_u.nfs_extB.next = NULL;
13917c478bd9Sstevel@tonic-gate 				last_error = NFSERR_IO;
13927c478bd9Sstevel@tonic-gate 				destroy_auth_client_handle(cl);
13937c478bd9Sstevel@tonic-gate 				skipentry = 1;
13947c478bd9Sstevel@tonic-gate 				mfs->mfs_ignore = 1;
13957c478bd9Sstevel@tonic-gate 				continue;
13967c478bd9Sstevel@tonic-gate 			}
13977c478bd9Sstevel@tonic-gate 		}
13987c478bd9Sstevel@tonic-gate 
13997c478bd9Sstevel@tonic-gate 		nfs_flavor = NULL;
1400d7c6abe8Sjasmith 		if (hasmntopt(&m, MNTOPT_SEC) != NULL) {
14017c478bd9Sstevel@tonic-gate 			if ((str_opt(&m, MNTOPT_SEC, &nfs_flavor)) == -1) {
14027c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR, "nfsmount: no memory");
14037c478bd9Sstevel@tonic-gate 				last_error = NFSERR_IO;
14047c478bd9Sstevel@tonic-gate 				destroy_auth_client_handle(cl);
14057c478bd9Sstevel@tonic-gate 				goto out;
14067c478bd9Sstevel@tonic-gate 			}
14077c478bd9Sstevel@tonic-gate 		}
14087c478bd9Sstevel@tonic-gate 
14097c478bd9Sstevel@tonic-gate 		if (nfs_flavor) {
14107c478bd9Sstevel@tonic-gate 			if (++sec_opt > 1) {
14117c478bd9Sstevel@tonic-gate 				syslog(loglevel,
14127c478bd9Sstevel@tonic-gate 				    "conflicting security options for %s",
14137c478bd9Sstevel@tonic-gate 				    remname);
14147c478bd9Sstevel@tonic-gate 				free(nfs_flavor);
14157c478bd9Sstevel@tonic-gate 				free(argp);
14167c478bd9Sstevel@tonic-gate 				head = prevhead;
14177c478bd9Sstevel@tonic-gate 				tail = prevtail;
14187c478bd9Sstevel@tonic-gate 				if (tail)
14197c478bd9Sstevel@tonic-gate 					tail->nfs_ext_u.nfs_extB.next = NULL;
14207c478bd9Sstevel@tonic-gate 				last_error = NFSERR_IO;
14217c478bd9Sstevel@tonic-gate 				destroy_auth_client_handle(cl);
14227c478bd9Sstevel@tonic-gate 				skipentry = 1;
14237c478bd9Sstevel@tonic-gate 				mfs->mfs_ignore = 1;
14247c478bd9Sstevel@tonic-gate 				continue;
14257c478bd9Sstevel@tonic-gate 			}
14267c478bd9Sstevel@tonic-gate 			if (nfs_getseconfig_byname(nfs_flavor, &nfs_sec)) {
14277c478bd9Sstevel@tonic-gate 				syslog(loglevel,
14287c478bd9Sstevel@tonic-gate 				    "error getting %s information from %s",
14297c478bd9Sstevel@tonic-gate 				    nfs_flavor, NFSSEC_CONF);
14307c478bd9Sstevel@tonic-gate 				free(nfs_flavor);
14317c478bd9Sstevel@tonic-gate 				free(argp);
14327c478bd9Sstevel@tonic-gate 				head = prevhead;
14337c478bd9Sstevel@tonic-gate 				tail = prevtail;
14347c478bd9Sstevel@tonic-gate 				if (tail)
14357c478bd9Sstevel@tonic-gate 					tail->nfs_ext_u.nfs_extB.next = NULL;
14367c478bd9Sstevel@tonic-gate 				last_error = NFSERR_IO;
14377c478bd9Sstevel@tonic-gate 				destroy_auth_client_handle(cl);
14387c478bd9Sstevel@tonic-gate 				skipentry = 1;
14397c478bd9Sstevel@tonic-gate 				mfs->mfs_ignore = 1;
14407c478bd9Sstevel@tonic-gate 				continue;
14417c478bd9Sstevel@tonic-gate 			}
14427c478bd9Sstevel@tonic-gate 			free(nfs_flavor);
14437c478bd9Sstevel@tonic-gate 		}
14447c478bd9Sstevel@tonic-gate 
14457c478bd9Sstevel@tonic-gate 		posix = (nfsvers != NFS_V4 &&
14461bbc88acSrm 		    hasmntopt(&m, MNTOPT_POSIX) != NULL) ? 1 : 0;
14477c478bd9Sstevel@tonic-gate 
14487c478bd9Sstevel@tonic-gate 		if ((mfs->mfs_flags & MFS_FH_VIA_WEBNFS) == 0 &&
14491bbc88acSrm 		    nfsvers != NFS_V4) {
14501bbc88acSrm 			bool_t give_up_on_mnt;
14511bbc88acSrm 			bool_t got_mnt_error;
14527c478bd9Sstevel@tonic-gate 		/*
14537c478bd9Sstevel@tonic-gate 		 * If we started with a URL, if first byte of path is not "/",
14547c478bd9Sstevel@tonic-gate 		 * then the mount will likely fail, so we should try again
14557c478bd9Sstevel@tonic-gate 		 * with a prepended "/".
14567c478bd9Sstevel@tonic-gate 		 */
14571bbc88acSrm 			if (mfs->mfs_flags & MFS_ALLOC_DIR && *dir != '/')
14581bbc88acSrm 				give_up_on_mnt = FALSE;
14591bbc88acSrm 			else
14601bbc88acSrm 				give_up_on_mnt = TRUE;
14617c478bd9Sstevel@tonic-gate 
14621bbc88acSrm 			got_mnt_error = FALSE;
14637c478bd9Sstevel@tonic-gate 
14647c478bd9Sstevel@tonic-gate try_mnt_slash:
14651bbc88acSrm 			if (got_mnt_error == TRUE) {
14661bbc88acSrm 				int i, l;
14677c478bd9Sstevel@tonic-gate 
14681bbc88acSrm 				give_up_on_mnt = TRUE;
14691bbc88acSrm 				l = strlen(dir);
14707c478bd9Sstevel@tonic-gate 
14711bbc88acSrm 				/*
14721bbc88acSrm 				 * Insert a "/" to front of mfs_dir.
14731bbc88acSrm 				 */
14741bbc88acSrm 				for (i = l; i > 0; i--)
14751bbc88acSrm 					dir[i] = dir[i-1];
14767c478bd9Sstevel@tonic-gate 
14771bbc88acSrm 				dir[0] = '/';
14781bbc88acSrm 			}
14797c478bd9Sstevel@tonic-gate 
14801bbc88acSrm 			/* Get fhandle of remote path from server's mountd */
14817c478bd9Sstevel@tonic-gate 
14821bbc88acSrm 			switch (outvers) {
14831bbc88acSrm 			case MOUNTVERS:
14841bbc88acSrm 				if (posix) {
14851bbc88acSrm 					free(argp);
14861bbc88acSrm 					head = prevhead;
14871bbc88acSrm 					tail = prevtail;
14881bbc88acSrm 					if (tail)
14891bbc88acSrm 						tail->nfs_ext_u.nfs_extB.next =
14901bbc88acSrm 						    NULL;
14911bbc88acSrm 					last_error = NFSERR_NOENT;
14921bbc88acSrm 					syslog(loglevel,
14931bbc88acSrm 					    "can't get posix info for %s",
14941bbc88acSrm 					    host);
14951bbc88acSrm 					destroy_auth_client_handle(cl);
14961bbc88acSrm 					skipentry = 1;
14971bbc88acSrm 					mfs->mfs_ignore = 1;
14981bbc88acSrm 					continue;
14991bbc88acSrm 				}
15007c478bd9Sstevel@tonic-gate 		    /* FALLTHRU */
15011bbc88acSrm 			case MOUNTVERS_POSIX:
15021bbc88acSrm 				if (nfsvers == NFS_V3) {
15031bbc88acSrm 					free(argp);
15041bbc88acSrm 					head = prevhead;
15051bbc88acSrm 					tail = prevtail;
15061bbc88acSrm 					if (tail)
15071bbc88acSrm 						tail->nfs_ext_u.nfs_extB.next =
15081bbc88acSrm 						    NULL;
15091bbc88acSrm 					last_error = NFSERR_NOENT;
15101bbc88acSrm 					syslog(loglevel,
15111bbc88acSrm 					    "%s doesn't support NFS Version 3",
15121bbc88acSrm 					    host);
15131bbc88acSrm 					destroy_auth_client_handle(cl);
15141bbc88acSrm 					skipentry = 1;
15151bbc88acSrm 					mfs->mfs_ignore = 1;
15161bbc88acSrm 					continue;
15177c478bd9Sstevel@tonic-gate 				}
15181bbc88acSrm 				rpc_stat = clnt_call(cl, MOUNTPROC_MNT,
15191bbc88acSrm 				    xdr_dirpath, (caddr_t)&dir,
15201bbc88acSrm 				    xdr_fhstatus, (caddr_t)&fhs, timeout);
15211bbc88acSrm 				if (rpc_stat != RPC_SUCCESS) {
15221bbc88acSrm 
15231bbc88acSrm 					if (give_up_on_mnt == FALSE) {
15241bbc88acSrm 						got_mnt_error = TRUE;
15251bbc88acSrm 						goto try_mnt_slash;
15261bbc88acSrm 					}
15277c478bd9Sstevel@tonic-gate 
15287c478bd9Sstevel@tonic-gate 				/*
15297c478bd9Sstevel@tonic-gate 				 * Given the way "clnt_sperror" works, the "%s"
15307c478bd9Sstevel@tonic-gate 				 * immediately following the "not responding"
15317c478bd9Sstevel@tonic-gate 				 * is correct.
15327c478bd9Sstevel@tonic-gate 				 */
15331bbc88acSrm 					free(argp);
15341bbc88acSrm 					head = prevhead;
15351bbc88acSrm 					tail = prevtail;
15361bbc88acSrm 					if (tail)
15371bbc88acSrm 						tail->nfs_ext_u.nfs_extB.next =
15381bbc88acSrm 						    NULL;
15391bbc88acSrm 					last_error = NFSERR_NOENT;
15407c478bd9Sstevel@tonic-gate 
15411bbc88acSrm 					if (retries-- > 0) {
15421bbc88acSrm 						destroy_auth_client_handle(cl);
15431bbc88acSrm 						DELAY(delay);
15441bbc88acSrm 						goto retry;
15451bbc88acSrm 					}
15467c478bd9Sstevel@tonic-gate 
15471bbc88acSrm 					if (trace > 3) {
15481bbc88acSrm 						trace_prt(1,
15491bbc88acSrm 						    "  nfsmount: mount RPC "
15501bbc88acSrm 						    "failed for %s\n",
15511bbc88acSrm 						    host);
15521bbc88acSrm 					}
15531bbc88acSrm 					syslog(loglevel,
15541bbc88acSrm 					    "%s server not responding%s",
15551bbc88acSrm 					    host, clnt_sperror(cl, ""));
15561bbc88acSrm 					destroy_auth_client_handle(cl);
15571bbc88acSrm 					skipentry = 1;
15581bbc88acSrm 					mfs->mfs_ignore = 1;
15591bbc88acSrm 					continue;
15607c478bd9Sstevel@tonic-gate 				}
15611bbc88acSrm 				if ((errno = fhs.fhs_status) != MNT_OK)  {
15627c478bd9Sstevel@tonic-gate 
15631bbc88acSrm 					if (give_up_on_mnt == FALSE) {
15641bbc88acSrm 						got_mnt_error = TRUE;
15651bbc88acSrm 						goto try_mnt_slash;
15661bbc88acSrm 					}
15677c478bd9Sstevel@tonic-gate 
15681bbc88acSrm 					free(argp);
15691bbc88acSrm 					head = prevhead;
15701bbc88acSrm 					tail = prevtail;
15711bbc88acSrm 					if (tail)
15721bbc88acSrm 						tail->nfs_ext_u.nfs_extB.next =
15731bbc88acSrm 						    NULL;
15741bbc88acSrm 					if (errno == EACCES) {
15751bbc88acSrm 						status = NFSERR_ACCES;
15761bbc88acSrm 					} else {
15771bbc88acSrm 						syslog(loglevel, "%s: %m",
15781bbc88acSrm 						    host);
15791bbc88acSrm 						status = NFSERR_IO;
15801bbc88acSrm 					}
15811bbc88acSrm 					if (trace > 3) {
15821bbc88acSrm 						trace_prt(1,
15831bbc88acSrm 						    "  nfsmount: mount RPC gave"
15841bbc88acSrm 						    " %d for %s:%s\n",
15851bbc88acSrm 						    errno, host, dir);
15861bbc88acSrm 					}
15871bbc88acSrm 					last_error = status;
15881bbc88acSrm 					destroy_auth_client_handle(cl);
15891bbc88acSrm 					skipentry = 1;
15901bbc88acSrm 					mfs->mfs_ignore = 1;
15911bbc88acSrm 					continue;
15927c478bd9Sstevel@tonic-gate 				}
15931bbc88acSrm 				argp->fh = malloc((sizeof (fhandle)));
15941bbc88acSrm 				if (!argp->fh) {
15951bbc88acSrm 					syslog(LOG_ERR, "nfsmount: no memory");
15961bbc88acSrm 					last_error = NFSERR_IO;
15971bbc88acSrm 					destroy_auth_client_handle(cl);
15981bbc88acSrm 					goto out;
15997c478bd9Sstevel@tonic-gate 				}
16001bbc88acSrm 				(void) memcpy(argp->fh,
16011bbc88acSrm 				    &fhs.fhstatus_u.fhs_fhandle,
16021bbc88acSrm 				    sizeof (fhandle));
16031bbc88acSrm 				break;
16041bbc88acSrm 			case MOUNTVERS3:
16051bbc88acSrm 				posix = 0;
16061bbc88acSrm 				(void) memset((char *)&res3, '\0',
16071bbc88acSrm 				    sizeof (res3));
16081bbc88acSrm 				rpc_stat = clnt_call(cl, MOUNTPROC_MNT,
16091bbc88acSrm 				    xdr_dirpath, (caddr_t)&dir,
16101bbc88acSrm 				    xdr_mountres3, (caddr_t)&res3, timeout);
16111bbc88acSrm 				if (rpc_stat != RPC_SUCCESS) {
16121bbc88acSrm 
16131bbc88acSrm 					if (give_up_on_mnt == FALSE) {
16141bbc88acSrm 						got_mnt_error = TRUE;
16151bbc88acSrm 						goto try_mnt_slash;
16161bbc88acSrm 					}
16177c478bd9Sstevel@tonic-gate 
16187c478bd9Sstevel@tonic-gate 				/*
16197c478bd9Sstevel@tonic-gate 				 * Given the way "clnt_sperror" works, the "%s"
16207c478bd9Sstevel@tonic-gate 				 * immediately following the "not responding"
16217c478bd9Sstevel@tonic-gate 				 * is correct.
16227c478bd9Sstevel@tonic-gate 				 */
16231bbc88acSrm 					free(argp);
16241bbc88acSrm 					head = prevhead;
16251bbc88acSrm 					tail = prevtail;
16261bbc88acSrm 					if (tail)
16271bbc88acSrm 						tail->nfs_ext_u.nfs_extB.next =
16281bbc88acSrm 						    NULL;
16291bbc88acSrm 					last_error = NFSERR_NOENT;
16307c478bd9Sstevel@tonic-gate 
16311bbc88acSrm 					if (retries-- > 0) {
16321bbc88acSrm 						destroy_auth_client_handle(cl);
16331bbc88acSrm 						DELAY(delay);
16341bbc88acSrm 						goto retry;
16351bbc88acSrm 					}
16367c478bd9Sstevel@tonic-gate 
16371bbc88acSrm 					if (trace > 3) {
16381bbc88acSrm 						trace_prt(1,
16391bbc88acSrm 						    "  nfsmount: mount RPC "
16401bbc88acSrm 						    "failed for %s\n",
16411bbc88acSrm 						    host);
16421bbc88acSrm 					}
16431bbc88acSrm 					syslog(loglevel,
16441bbc88acSrm 					    "%s server not responding%s",
16451bbc88acSrm 					    remname, clnt_sperror(cl, ""));
16461bbc88acSrm 					destroy_auth_client_handle(cl);
16471bbc88acSrm 					skipentry = 1;
16481bbc88acSrm 					mfs->mfs_ignore = 1;
16491bbc88acSrm 					continue;
16507c478bd9Sstevel@tonic-gate 				}
16511bbc88acSrm 				if ((errno = res3.fhs_status) != MNT_OK)  {
16527c478bd9Sstevel@tonic-gate 
16531bbc88acSrm 					if (give_up_on_mnt == FALSE) {
16541bbc88acSrm 						got_mnt_error = TRUE;
16551bbc88acSrm 						goto try_mnt_slash;
16567c478bd9Sstevel@tonic-gate 					}
16571bbc88acSrm 
16587c478bd9Sstevel@tonic-gate 					free(argp);
16597c478bd9Sstevel@tonic-gate 					head = prevhead;
16607c478bd9Sstevel@tonic-gate 					tail = prevtail;
16617c478bd9Sstevel@tonic-gate 					if (tail)
16621bbc88acSrm 						tail->nfs_ext_u.nfs_extB.next =
16631bbc88acSrm 						    NULL;
16641bbc88acSrm 					if (errno == EACCES) {
16651bbc88acSrm 						status = NFSERR_ACCES;
16661bbc88acSrm 					} else {
16671bbc88acSrm 						syslog(loglevel, "%s: %m",
16681bbc88acSrm 						    remname);
16691bbc88acSrm 						status = NFSERR_IO;
16701bbc88acSrm 					}
16711bbc88acSrm 					if (trace > 3) {
16721bbc88acSrm 						trace_prt(1,
16731bbc88acSrm 						    "  nfsmount: mount RPC gave"
16741bbc88acSrm 						    " %d for %s:%s\n",
16751bbc88acSrm 						    errno, host, dir);
16761bbc88acSrm 					}
16771bbc88acSrm 					last_error = status;
16787c478bd9Sstevel@tonic-gate 					destroy_auth_client_handle(cl);
16797c478bd9Sstevel@tonic-gate 					skipentry = 1;
16807c478bd9Sstevel@tonic-gate 					mfs->mfs_ignore = 1;
16817c478bd9Sstevel@tonic-gate 					continue;
16827c478bd9Sstevel@tonic-gate 				}
16831bbc88acSrm 
16841bbc88acSrm 			/*
16851bbc88acSrm 			 *  Negotiate the security flavor for nfs_mount
16861bbc88acSrm 			 */
16871bbc88acSrm 				auths = res3.mountres3_u.mountinfo.
16881bbc88acSrm 				    auth_flavors.auth_flavors_val;
16891bbc88acSrm 				count = res3.mountres3_u.mountinfo.
16901bbc88acSrm 				    auth_flavors.auth_flavors_len;
16911bbc88acSrm 
16921bbc88acSrm 				if (sec_opt) {
16931bbc88acSrm 					for (i = 0; i < count; i++)
16941bbc88acSrm 						if (auths[i] ==
16951bbc88acSrm 						    nfs_sec.sc_nfsnum) {
16961bbc88acSrm 							break;
16971bbc88acSrm 						}
16981bbc88acSrm 					if (i >= count) {
16991bbc88acSrm 						syslog(LOG_ERR,
17001bbc88acSrm 						    "%s: does not support "
17011bbc88acSrm 						    "security \"%s\"\n",
17021bbc88acSrm 						    remname, nfs_sec.sc_name);
17031bbc88acSrm 						clnt_freeres(cl, xdr_mountres3,
17041bbc88acSrm 						    (caddr_t)&res3);
17051bbc88acSrm 						free(argp);
17061bbc88acSrm 						head = prevhead;
17071bbc88acSrm 						tail = prevtail;
17081bbc88acSrm 						if (tail)
17091bbc88acSrm 							tail->nfs_ext_u.
17101bbc88acSrm 							    nfs_extB.next =
17111bbc88acSrm 							    NULL;
17121bbc88acSrm 						last_error = NFSERR_IO;
17131bbc88acSrm 						destroy_auth_client_handle(cl);
17141bbc88acSrm 						skipentry = 1;
17151bbc88acSrm 						mfs->mfs_ignore = 1;
17161bbc88acSrm 						continue;
17171bbc88acSrm 					}
17181bbc88acSrm 				} else if (count > 0) {
17197c478bd9Sstevel@tonic-gate 					for (i = 0; i < count; i++) {
17201bbc88acSrm 						if (!(scerror =
17211bbc88acSrm 						    nfs_getseconfig_bynumber(
17221bbc88acSrm 						    auths[i], &nfs_sec))) {
17231bbc88acSrm 							sec_opt++;
17241bbc88acSrm 							break;
17251bbc88acSrm 						}
17267c478bd9Sstevel@tonic-gate 					}
17277c478bd9Sstevel@tonic-gate 					if (i >= count) {
17287c478bd9Sstevel@tonic-gate 						if (nfs_syslog_scerr(scerror,
17291bbc88acSrm 						    scerror_msg)
17301bbc88acSrm 						    != -1) {
17317c478bd9Sstevel@tonic-gate 							syslog(LOG_ERR,
17321bbc88acSrm 							    "%s cannot be "
17331bbc88acSrm 							    "mounted because it"
17341bbc88acSrm 							    " is shared with "
17351bbc88acSrm 							    "security flavor %d"
17361bbc88acSrm 							    " which %s",
17371bbc88acSrm 							    remname,
17381bbc88acSrm 							    auths[i-1],
17391bbc88acSrm 							    scerror_msg);
17407c478bd9Sstevel@tonic-gate 						}
17417c478bd9Sstevel@tonic-gate 						clnt_freeres(cl, xdr_mountres3,
17427c478bd9Sstevel@tonic-gate 						    (caddr_t)&res3);
17437c478bd9Sstevel@tonic-gate 						free(argp);
17447c478bd9Sstevel@tonic-gate 						head = prevhead;
17457c478bd9Sstevel@tonic-gate 						tail = prevtail;
17467c478bd9Sstevel@tonic-gate 						if (tail)
17471bbc88acSrm 							tail->nfs_ext_u.
17481bbc88acSrm 							    nfs_extB.next =
17491bbc88acSrm 							    NULL;
17507c478bd9Sstevel@tonic-gate 						last_error = NFSERR_IO;
17517c478bd9Sstevel@tonic-gate 						destroy_auth_client_handle(cl);
17527c478bd9Sstevel@tonic-gate 						skipentry = 1;
17537c478bd9Sstevel@tonic-gate 						mfs->mfs_ignore = 1;
17547c478bd9Sstevel@tonic-gate 						continue;
17551bbc88acSrm 						}
17567c478bd9Sstevel@tonic-gate 				}
17577c478bd9Sstevel@tonic-gate 
17581bbc88acSrm 				fh3.fh3_length =
17591bbc88acSrm 				    res3.mountres3_u.mountinfo.fhandle.
17601bbc88acSrm 				    fhandle3_len;
17611bbc88acSrm 				(void) memcpy(fh3.fh3_u.data,
17621bbc88acSrm 				    res3.mountres3_u.mountinfo.fhandle.
17631bbc88acSrm 				    fhandle3_val,
17641bbc88acSrm 				    fh3.fh3_length);
17651bbc88acSrm 				clnt_freeres(cl, xdr_mountres3,
17661bbc88acSrm 				    (caddr_t)&res3);
17671bbc88acSrm 				argp->fh = malloc(sizeof (nfs_fh3));
17681bbc88acSrm 				if (!argp->fh) {
17691bbc88acSrm 					syslog(LOG_ERR, "nfsmount: no memory");
17701bbc88acSrm 					last_error = NFSERR_IO;
17711bbc88acSrm 					destroy_auth_client_handle(cl);
17721bbc88acSrm 					goto out;
17731bbc88acSrm 				}
17741bbc88acSrm 				(void) memcpy(argp->fh, &fh3, sizeof (nfs_fh3));
17751bbc88acSrm 				break;
17761bbc88acSrm 			default:
17771bbc88acSrm 				free(argp);
17781bbc88acSrm 				head = prevhead;
17791bbc88acSrm 				tail = prevtail;
17801bbc88acSrm 				if (tail)
17811bbc88acSrm 					tail->nfs_ext_u.nfs_extB.next = NULL;
17821bbc88acSrm 				last_error = NFSERR_NOENT;
17831bbc88acSrm 				syslog(loglevel,
17841bbc88acSrm 				    "unknown MOUNT version %ld on %s",
17851bbc88acSrm 				    vers, remname);
17867c478bd9Sstevel@tonic-gate 				destroy_auth_client_handle(cl);
17871bbc88acSrm 				skipentry = 1;
17881bbc88acSrm 				mfs->mfs_ignore = 1;
17891bbc88acSrm 				continue;
17901bbc88acSrm 			} /* switch */
17917c478bd9Sstevel@tonic-gate 		}
17927c478bd9Sstevel@tonic-gate 		if (nfsvers == NFS_V4) {
17937c478bd9Sstevel@tonic-gate 			argp->fh = strdup(dir);
17947c478bd9Sstevel@tonic-gate 			if (argp->fh == NULL) {
17957c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR, "nfsmount: no memory");
17967c478bd9Sstevel@tonic-gate 				last_error = NFSERR_IO;
17977c478bd9Sstevel@tonic-gate 				goto out;
17987c478bd9Sstevel@tonic-gate 			}
17997c478bd9Sstevel@tonic-gate 		}
18007c478bd9Sstevel@tonic-gate 
18017c478bd9Sstevel@tonic-gate 		if (trace > 4)
18027c478bd9Sstevel@tonic-gate 			trace_prt(1, "	nfsmount: have %s filehandle for %s\n",
18037c478bd9Sstevel@tonic-gate 			    fstype, remname);
18047c478bd9Sstevel@tonic-gate 
18057c478bd9Sstevel@tonic-gate 		argp->flags |= NFSMNT_NEWARGS;
18067c478bd9Sstevel@tonic-gate 		argp->flags |= NFSMNT_INT;	/* default is "intr" */
18077c478bd9Sstevel@tonic-gate 		argp->flags |= NFSMNT_HOSTNAME;
18086a6d3e5eSjs 		argp->hostname = strdup(host);
18096a6d3e5eSjs 		if (argp->hostname == NULL) {
18106a6d3e5eSjs 			syslog(LOG_ERR, "nfsmount: no memory");
18116a6d3e5eSjs 			last_error = NFSERR_IO;
18126a6d3e5eSjs 			goto out;
18136a6d3e5eSjs 		}
18147c478bd9Sstevel@tonic-gate 
18157c478bd9Sstevel@tonic-gate 		/*
18167c478bd9Sstevel@tonic-gate 		 * In this case, we want NFSv4 to behave like
18177c478bd9Sstevel@tonic-gate 		 * non-WebNFS so that we get the server address.
18187c478bd9Sstevel@tonic-gate 		 */
18197c478bd9Sstevel@tonic-gate 		if ((mfs->mfs_flags & MFS_FH_VIA_WEBNFS) == 0) {
18207c478bd9Sstevel@tonic-gate 			nconf = NULL;
18217c478bd9Sstevel@tonic-gate 
18227c478bd9Sstevel@tonic-gate 			if (nfs_port != 0)
18237c478bd9Sstevel@tonic-gate 				thisport = nfs_port;
18247c478bd9Sstevel@tonic-gate 			else
18257c478bd9Sstevel@tonic-gate 				thisport = mfs->mfs_port;
18267c478bd9Sstevel@tonic-gate 
18277c478bd9Sstevel@tonic-gate 			/*
18287c478bd9Sstevel@tonic-gate 			 * For NFSv4, we want to avoid rpcbind, so call
18292f172c55SRobert Thurlow 			 * get_server_netinfo() directly to tell it that
18307c478bd9Sstevel@tonic-gate 			 * we want to go "direct_to_server".  Otherwise,
18317c478bd9Sstevel@tonic-gate 			 * do what has always been done.
18327c478bd9Sstevel@tonic-gate 			 */
18337c478bd9Sstevel@tonic-gate 			if (nfsvers == NFS_V4) {
18347c478bd9Sstevel@tonic-gate 				enum clnt_stat cstat;
18352f172c55SRobert Thurlow 
18362f172c55SRobert Thurlow 				argp->addr = get_server_netinfo(SERVER_ADDR,
18371bbc88acSrm 				    host, NFS_PROGRAM, nfsvers, NULL,
18381bbc88acSrm 				    &nconf, nfs_proto, thisport, NULL,
18391bbc88acSrm 				    NULL, TRUE, NULL, &cstat);
18407c478bd9Sstevel@tonic-gate 			} else {
18417c478bd9Sstevel@tonic-gate 				argp->addr = get_addr(host, NFS_PROGRAM,
18421bbc88acSrm 				    nfsvers, &nconf, nfs_proto,
18431bbc88acSrm 				    thisport, NULL);
18447c478bd9Sstevel@tonic-gate 			}
18457c478bd9Sstevel@tonic-gate 
18467c478bd9Sstevel@tonic-gate 			if (argp->addr == NULL) {
184739d3e169Sevanl 				if (argp->hostname)
184839d3e169Sevanl 					free(argp->hostname);
18497c478bd9Sstevel@tonic-gate 				free(argp->fh);
18507c478bd9Sstevel@tonic-gate 				free(argp);
18517c478bd9Sstevel@tonic-gate 				head = prevhead;
18527c478bd9Sstevel@tonic-gate 				tail = prevtail;
18537c478bd9Sstevel@tonic-gate 				if (tail)
18547c478bd9Sstevel@tonic-gate 					tail->nfs_ext_u.nfs_extB.next = NULL;
18557c478bd9Sstevel@tonic-gate 				last_error = NFSERR_NOENT;
18567c478bd9Sstevel@tonic-gate 
18577c478bd9Sstevel@tonic-gate 				if (retries-- > 0) {
18587c478bd9Sstevel@tonic-gate 					destroy_auth_client_handle(cl);
18597c478bd9Sstevel@tonic-gate 					DELAY(delay);
18607c478bd9Sstevel@tonic-gate 					goto retry;
18617c478bd9Sstevel@tonic-gate 				}
18627c478bd9Sstevel@tonic-gate 
18637c478bd9Sstevel@tonic-gate 				syslog(loglevel, "%s: no NFS service", host);
18647c478bd9Sstevel@tonic-gate 				destroy_auth_client_handle(cl);
18657c478bd9Sstevel@tonic-gate 				skipentry = 1;
18667c478bd9Sstevel@tonic-gate 				mfs->mfs_ignore = 1;
18677c478bd9Sstevel@tonic-gate 				continue;
18687c478bd9Sstevel@tonic-gate 			}
18697c478bd9Sstevel@tonic-gate 			if (trace > 4)
18707c478bd9Sstevel@tonic-gate 				trace_prt(1,
18717c478bd9Sstevel@tonic-gate 				    "\tnfsmount: have net address for %s\n",
18727c478bd9Sstevel@tonic-gate 				    remname);
18737c478bd9Sstevel@tonic-gate 
18747c478bd9Sstevel@tonic-gate 		} else {
18757c478bd9Sstevel@tonic-gate 			nconf = mfs->mfs_nconf;
18767c478bd9Sstevel@tonic-gate 			mfs->mfs_nconf = NULL;
18777c478bd9Sstevel@tonic-gate 		}
18787c478bd9Sstevel@tonic-gate 
18797c478bd9Sstevel@tonic-gate 		argp->flags |= NFSMNT_KNCONF;
18807c478bd9Sstevel@tonic-gate 		argp->knconf = get_knconf(nconf);
18817c478bd9Sstevel@tonic-gate 		if (argp->knconf == NULL) {
18827c478bd9Sstevel@tonic-gate 			netbuf_free(argp->addr);
18837c478bd9Sstevel@tonic-gate 			freenetconfigent(nconf);
188439d3e169Sevanl 			if (argp->hostname)
188539d3e169Sevanl 				free(argp->hostname);
18867c478bd9Sstevel@tonic-gate 			free(argp->fh);
18877c478bd9Sstevel@tonic-gate 			free(argp);
18887c478bd9Sstevel@tonic-gate 			head = prevhead;
18897c478bd9Sstevel@tonic-gate 			tail = prevtail;
18907c478bd9Sstevel@tonic-gate 			if (tail)
18917c478bd9Sstevel@tonic-gate 				tail->nfs_ext_u.nfs_extB.next = NULL;
18927c478bd9Sstevel@tonic-gate 			last_error = NFSERR_NOSPC;
18937c478bd9Sstevel@tonic-gate 			destroy_auth_client_handle(cl);
18947c478bd9Sstevel@tonic-gate 			skipentry = 1;
18957c478bd9Sstevel@tonic-gate 			mfs->mfs_ignore = 1;
18967c478bd9Sstevel@tonic-gate 			continue;
18977c478bd9Sstevel@tonic-gate 		}
18987c478bd9Sstevel@tonic-gate 		if (trace > 4)
18997c478bd9Sstevel@tonic-gate 			trace_prt(1,
19007c478bd9Sstevel@tonic-gate 			    "\tnfsmount: have net config for %s\n",
19017c478bd9Sstevel@tonic-gate 			    remname);
19027c478bd9Sstevel@tonic-gate 
19037c478bd9Sstevel@tonic-gate 		if (hasmntopt(&m, MNTOPT_SOFT) != NULL) {
19047c478bd9Sstevel@tonic-gate 			argp->flags |= NFSMNT_SOFT;
19057c478bd9Sstevel@tonic-gate 		}
19067c478bd9Sstevel@tonic-gate 		if (hasmntopt(&m, MNTOPT_NOINTR) != NULL) {
19077c478bd9Sstevel@tonic-gate 			argp->flags &= ~(NFSMNT_INT);
19087c478bd9Sstevel@tonic-gate 		}
19097c478bd9Sstevel@tonic-gate 		if (hasmntopt(&m, MNTOPT_NOAC) != NULL) {
19107c478bd9Sstevel@tonic-gate 			argp->flags |= NFSMNT_NOAC;
19117c478bd9Sstevel@tonic-gate 		}
19127c478bd9Sstevel@tonic-gate 		if (hasmntopt(&m, MNTOPT_NOCTO) != NULL) {
19137c478bd9Sstevel@tonic-gate 			argp->flags |= NFSMNT_NOCTO;
19147c478bd9Sstevel@tonic-gate 		}
19157c478bd9Sstevel@tonic-gate 		if (hasmntopt(&m, MNTOPT_FORCEDIRECTIO) != NULL) {
19167c478bd9Sstevel@tonic-gate 			argp->flags |= NFSMNT_DIRECTIO;
19177c478bd9Sstevel@tonic-gate 		}
19187c478bd9Sstevel@tonic-gate 		if (hasmntopt(&m, MNTOPT_NOFORCEDIRECTIO) != NULL) {
19197c478bd9Sstevel@tonic-gate 			argp->flags &= ~(NFSMNT_DIRECTIO);
19207c478bd9Sstevel@tonic-gate 		}
19217c478bd9Sstevel@tonic-gate 
19227c478bd9Sstevel@tonic-gate 		/*
19237c478bd9Sstevel@tonic-gate 		 * Set up security data for argp->nfs_ext_u.nfs_extB.secdata.
19247c478bd9Sstevel@tonic-gate 		 */
19257c478bd9Sstevel@tonic-gate 		if (mfssnego.snego_done) {
19267c478bd9Sstevel@tonic-gate 			memcpy(&nfs_sec, &mfssnego.nfs_sec,
19271bbc88acSrm 			    sizeof (seconfig_t));
19287c478bd9Sstevel@tonic-gate 		} else if (!sec_opt) {
19297c478bd9Sstevel@tonic-gate 			/*
19307c478bd9Sstevel@tonic-gate 			 * Get default security mode.
19317c478bd9Sstevel@tonic-gate 			 */
19327c478bd9Sstevel@tonic-gate 			if (nfs_getseconfig_default(&nfs_sec)) {
19337c478bd9Sstevel@tonic-gate 				syslog(loglevel,
19347c478bd9Sstevel@tonic-gate 				    "error getting default security entry\n");
19357c478bd9Sstevel@tonic-gate 				free_knconf(argp->knconf);
19367c478bd9Sstevel@tonic-gate 				netbuf_free(argp->addr);
19377c478bd9Sstevel@tonic-gate 				freenetconfigent(nconf);
193839d3e169Sevanl 				if (argp->hostname)
193939d3e169Sevanl 					free(argp->hostname);
19407c478bd9Sstevel@tonic-gate 				free(argp->fh);
19417c478bd9Sstevel@tonic-gate 				free(argp);
19427c478bd9Sstevel@tonic-gate 				head = prevhead;
19437c478bd9Sstevel@tonic-gate 				tail = prevtail;
19447c478bd9Sstevel@tonic-gate 				if (tail)
19457c478bd9Sstevel@tonic-gate 					tail->nfs_ext_u.nfs_extB.next = NULL;
19467c478bd9Sstevel@tonic-gate 				last_error = NFSERR_NOSPC;
19477c478bd9Sstevel@tonic-gate 				destroy_auth_client_handle(cl);
19487c478bd9Sstevel@tonic-gate 				skipentry = 1;
19497c478bd9Sstevel@tonic-gate 				mfs->mfs_ignore = 1;
19507c478bd9Sstevel@tonic-gate 				continue;
19517c478bd9Sstevel@tonic-gate 			}
19527c478bd9Sstevel@tonic-gate 			argp->flags |= NFSMNT_SECDEFAULT;
19537c478bd9Sstevel@tonic-gate 		}
19547c478bd9Sstevel@tonic-gate 
19557c478bd9Sstevel@tonic-gate 		/*
19567c478bd9Sstevel@tonic-gate 		 * For AUTH_DH
19577c478bd9Sstevel@tonic-gate 		 * get the network address for the time service on
19587c478bd9Sstevel@tonic-gate 		 * the server.	If an RPC based time service is
19597c478bd9Sstevel@tonic-gate 		 * not available then try the IP time service.
19607c478bd9Sstevel@tonic-gate 		 *
19617c478bd9Sstevel@tonic-gate 		 * Eventurally, we want to move this code to nfs_clnt_secdata()
19627c478bd9Sstevel@tonic-gate 		 * when autod_nfs.c and mount.c can share the same
19632f172c55SRobert Thurlow 		 * get_the_addr/get_netconfig_info routine.
19647c478bd9Sstevel@tonic-gate 		 */
19657c478bd9Sstevel@tonic-gate 		secflags = 0;
19667c478bd9Sstevel@tonic-gate 		syncaddr = NULL;
19677c478bd9Sstevel@tonic-gate 		retaddrs = NULL;
19687c478bd9Sstevel@tonic-gate 
19697c478bd9Sstevel@tonic-gate 		if (nfs_sec.sc_rpcnum == AUTH_DH || nfsvers == NFS_V4) {
19707c478bd9Sstevel@tonic-gate 		/*
19717c478bd9Sstevel@tonic-gate 		 * If not using the public fh and not NFS_V4, we can try
19727c478bd9Sstevel@tonic-gate 		 * talking RPCBIND. Otherwise, assume that firewalls
19737c478bd9Sstevel@tonic-gate 		 * prevent us from doing that.
19747c478bd9Sstevel@tonic-gate 		 */
19751bbc88acSrm 			if ((mfs->mfs_flags & MFS_FH_VIA_WEBNFS) == 0 &&
19761bbc88acSrm 			    nfsvers != NFS_V4) {
19772f172c55SRobert Thurlow 				enum clnt_stat cstat;
19782f172c55SRobert Thurlow 				syncaddr = get_server_netinfo(SERVER_ADDR,
19792f172c55SRobert Thurlow 				    host, RPCBPROG, RPCBVERS, NULL, &nconf,
19802f172c55SRobert Thurlow 				    NULL, 0, NULL, NULL, FALSE, NULL, &cstat);
19811bbc88acSrm 			}
19827c478bd9Sstevel@tonic-gate 
19831bbc88acSrm 			if (syncaddr != NULL) {
19841bbc88acSrm 				/* for flags in sec_data */
19851bbc88acSrm 				secflags |= AUTH_F_RPCTIMESYNC;
19861bbc88acSrm 			} else {
19871bbc88acSrm 				struct nd_hostserv hs;
19881bbc88acSrm 				int error;
19897c478bd9Sstevel@tonic-gate 
19901bbc88acSrm 				hs.h_host = host;
19911bbc88acSrm 				hs.h_serv = "timserver";
19921bbc88acSrm 				error = netdir_getbyname(nconf, &hs, &retaddrs);
19937c478bd9Sstevel@tonic-gate 
19941bbc88acSrm 				if (error != ND_OK &&
19951bbc88acSrm 				    nfs_sec.sc_rpcnum == AUTH_DH) {
19961bbc88acSrm 					syslog(loglevel,
19971bbc88acSrm 					    "%s: secure: no time service\n",
19981bbc88acSrm 					    host);
19991bbc88acSrm 					free_knconf(argp->knconf);
20001bbc88acSrm 					netbuf_free(argp->addr);
20011bbc88acSrm 					freenetconfigent(nconf);
20021bbc88acSrm 					if (argp->hostname)
20031bbc88acSrm 						free(argp->hostname);
20041bbc88acSrm 					free(argp->fh);
20051bbc88acSrm 					free(argp);
20061bbc88acSrm 					head = prevhead;
20071bbc88acSrm 					tail = prevtail;
20081bbc88acSrm 					if (tail)
20091bbc88acSrm 						tail->nfs_ext_u.nfs_extB.next =
20101bbc88acSrm 						    NULL;
20111bbc88acSrm 					last_error = NFSERR_IO;
20121bbc88acSrm 					destroy_auth_client_handle(cl);
20131bbc88acSrm 					skipentry = 1;
20141bbc88acSrm 					mfs->mfs_ignore = 1;
20151bbc88acSrm 					continue;
20161bbc88acSrm 				}
20177c478bd9Sstevel@tonic-gate 
20181bbc88acSrm 				if (error == ND_OK)
20191bbc88acSrm 					syncaddr = retaddrs->n_addrs;
20207c478bd9Sstevel@tonic-gate 
20217c478bd9Sstevel@tonic-gate 			/*
20227c478bd9Sstevel@tonic-gate 			 * For potential usage by NFS V4 when AUTH_DH
20237c478bd9Sstevel@tonic-gate 			 * is negotiated via SECINFO in the kernel.
20247c478bd9Sstevel@tonic-gate 			 */
20251bbc88acSrm 				if (nfsvers == NFS_V4 && syncaddr &&
20267c478bd9Sstevel@tonic-gate 				    host2netname(netname, host, NULL)) {
20271bbc88acSrm 					argp->syncaddr =
20281bbc88acSrm 					    malloc(sizeof (struct netbuf));
20291bbc88acSrm 					argp->syncaddr->buf =
20301bbc88acSrm 					    malloc(syncaddr->len);
20311bbc88acSrm 					(void) memcpy(argp->syncaddr->buf,
20321bbc88acSrm 					    syncaddr->buf, syncaddr->len);
20331bbc88acSrm 					argp->syncaddr->len = syncaddr->len;
20341bbc88acSrm 					argp->syncaddr->maxlen =
20351bbc88acSrm 					    syncaddr->maxlen;
20361bbc88acSrm 					argp->netname = strdup(netname);
20371bbc88acSrm 					argp->flags |= NFSMNT_SECURE;
20381bbc88acSrm 				}
20391bbc88acSrm 			} /* syncaddr */
20407c478bd9Sstevel@tonic-gate 		} /* AUTH_DH */
20417c478bd9Sstevel@tonic-gate 
204245916cd2Sjpk 		/*
204345916cd2Sjpk 		 * TSOL notes: automountd in tsol extension
204445916cd2Sjpk 		 * has "read down" capability, i.e. we allow
204545916cd2Sjpk 		 * a user to trigger an nfs mount into a lower
204645916cd2Sjpk 		 * labeled zone. We achieve this by always having
204745916cd2Sjpk 		 * root issue the mount request so that the
204845916cd2Sjpk 		 * lookup ops can go past /zone/<zone_name>
204945916cd2Sjpk 		 * on the server side.
205045916cd2Sjpk 		 */
205145916cd2Sjpk 		if (is_system_labeled())
205245916cd2Sjpk 			nfs_sec.sc_uid = (uid_t)0;
205345916cd2Sjpk 		else
20543bfb48feSsemery 			nfs_sec.sc_uid = uid;
20557c478bd9Sstevel@tonic-gate 		/*
20567c478bd9Sstevel@tonic-gate 		 * If AUTH_DH is a chosen flavor now, its data will be stored
20577c478bd9Sstevel@tonic-gate 		 * in the sec_data structure via nfs_clnt_secdata().
20587c478bd9Sstevel@tonic-gate 		 */
20597c478bd9Sstevel@tonic-gate 		if (!(secdata = nfs_clnt_secdata(&nfs_sec, host, argp->knconf,
20601bbc88acSrm 		    syncaddr, secflags))) {
20617c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR,
20621bbc88acSrm 			    "errors constructing security related data\n");
20637c478bd9Sstevel@tonic-gate 			if (secflags & AUTH_F_RPCTIMESYNC)
20647c478bd9Sstevel@tonic-gate 				netbuf_free(syncaddr);
20657c478bd9Sstevel@tonic-gate 			else if (retaddrs)
20667c478bd9Sstevel@tonic-gate 				netdir_free(retaddrs, ND_ADDRLIST);
20677c478bd9Sstevel@tonic-gate 			if (argp->syncaddr)
20687c478bd9Sstevel@tonic-gate 				netbuf_free(argp->syncaddr);
20697c478bd9Sstevel@tonic-gate 			if (argp->netname)
20707c478bd9Sstevel@tonic-gate 				free(argp->netname);
207139d3e169Sevanl 			if (argp->hostname)
207239d3e169Sevanl 				free(argp->hostname);
20737c478bd9Sstevel@tonic-gate 			free_knconf(argp->knconf);
20747c478bd9Sstevel@tonic-gate 			netbuf_free(argp->addr);
20757c478bd9Sstevel@tonic-gate 			freenetconfigent(nconf);
20767c478bd9Sstevel@tonic-gate 			free(argp->fh);
20777c478bd9Sstevel@tonic-gate 			free(argp);
20787c478bd9Sstevel@tonic-gate 			head = prevhead;
20797c478bd9Sstevel@tonic-gate 			tail = prevtail;
20807c478bd9Sstevel@tonic-gate 			if (tail)
20817c478bd9Sstevel@tonic-gate 				tail->nfs_ext_u.nfs_extB.next = NULL;
20827c478bd9Sstevel@tonic-gate 			last_error = NFSERR_IO;
20837c478bd9Sstevel@tonic-gate 			destroy_auth_client_handle(cl);
20847c478bd9Sstevel@tonic-gate 			skipentry = 1;
20857c478bd9Sstevel@tonic-gate 			mfs->mfs_ignore = 1;
20867c478bd9Sstevel@tonic-gate 			continue;
20877c478bd9Sstevel@tonic-gate 		}
20887c478bd9Sstevel@tonic-gate 		NFS_ARGS_EXTB_secdata(*argp, secdata);
20897c478bd9Sstevel@tonic-gate 		/* end of security stuff */
20907c478bd9Sstevel@tonic-gate 
20917c478bd9Sstevel@tonic-gate 		if (trace > 4)
20927c478bd9Sstevel@tonic-gate 			trace_prt(1,
20937c478bd9Sstevel@tonic-gate 			    "  nfsmount: have secure info for %s\n", remname);
20947c478bd9Sstevel@tonic-gate 
20957c478bd9Sstevel@tonic-gate 		if (hasmntopt(&m, MNTOPT_GRPID) != NULL) {
20967c478bd9Sstevel@tonic-gate 			argp->flags |= NFSMNT_GRPID;
20977c478bd9Sstevel@tonic-gate 		}
20987c478bd9Sstevel@tonic-gate 		if (nopt(&m, MNTOPT_RSIZE, &argp->rsize)) {
20997c478bd9Sstevel@tonic-gate 			argp->flags |= NFSMNT_RSIZE;
21007c478bd9Sstevel@tonic-gate 		}
21017c478bd9Sstevel@tonic-gate 		if (nopt(&m, MNTOPT_WSIZE, &argp->wsize)) {
21027c478bd9Sstevel@tonic-gate 			argp->flags |= NFSMNT_WSIZE;
21037c478bd9Sstevel@tonic-gate 		}
21047c478bd9Sstevel@tonic-gate 		if (nopt(&m, MNTOPT_TIMEO, &argp->timeo)) {
21057c478bd9Sstevel@tonic-gate 			argp->flags |= NFSMNT_TIMEO;
21067c478bd9Sstevel@tonic-gate 		}
21077c478bd9Sstevel@tonic-gate 		if (nopt(&m, MNTOPT_RETRANS, &argp->retrans)) {
21087c478bd9Sstevel@tonic-gate 			argp->flags |= NFSMNT_RETRANS;
21097c478bd9Sstevel@tonic-gate 		}
21107c478bd9Sstevel@tonic-gate 		if (nopt(&m, MNTOPT_ACTIMEO, &argp->acregmax)) {
21117c478bd9Sstevel@tonic-gate 			argp->flags |= NFSMNT_ACREGMAX;
21127c478bd9Sstevel@tonic-gate 			argp->flags |= NFSMNT_ACDIRMAX;
21137c478bd9Sstevel@tonic-gate 			argp->flags |= NFSMNT_ACDIRMIN;
21147c478bd9Sstevel@tonic-gate 			argp->flags |= NFSMNT_ACREGMIN;
21157c478bd9Sstevel@tonic-gate 			argp->acdirmin = argp->acregmin = argp->acdirmax
21161bbc88acSrm 			    = argp->acregmax;
21177c478bd9Sstevel@tonic-gate 		} else {
21187c478bd9Sstevel@tonic-gate 			if (nopt(&m, MNTOPT_ACREGMIN, &argp->acregmin)) {
21197c478bd9Sstevel@tonic-gate 				argp->flags |= NFSMNT_ACREGMIN;
21207c478bd9Sstevel@tonic-gate 			}
21217c478bd9Sstevel@tonic-gate 			if (nopt(&m, MNTOPT_ACREGMAX, &argp->acregmax)) {
21227c478bd9Sstevel@tonic-gate 				argp->flags |= NFSMNT_ACREGMAX;
21237c478bd9Sstevel@tonic-gate 			}
21247c478bd9Sstevel@tonic-gate 			if (nopt(&m, MNTOPT_ACDIRMIN, &argp->acdirmin)) {
21257c478bd9Sstevel@tonic-gate 				argp->flags |= NFSMNT_ACDIRMIN;
21267c478bd9Sstevel@tonic-gate 			}
21277c478bd9Sstevel@tonic-gate 			if (nopt(&m, MNTOPT_ACDIRMAX, &argp->acdirmax)) {
21287c478bd9Sstevel@tonic-gate 				argp->flags |= NFSMNT_ACDIRMAX;
21297c478bd9Sstevel@tonic-gate 			}
21307c478bd9Sstevel@tonic-gate 		}
21317c478bd9Sstevel@tonic-gate 
21327c478bd9Sstevel@tonic-gate 		if (posix) {
21337c478bd9Sstevel@tonic-gate 			argp->pathconf = NULL;
21347c478bd9Sstevel@tonic-gate 			if (error = get_pathconf(cl, dir, remname,
21357c478bd9Sstevel@tonic-gate 			    &argp->pathconf, retries)) {
21367c478bd9Sstevel@tonic-gate 				if (secflags & AUTH_F_RPCTIMESYNC)
21377c478bd9Sstevel@tonic-gate 					netbuf_free(syncaddr);
21387c478bd9Sstevel@tonic-gate 				else if (retaddrs)
21397c478bd9Sstevel@tonic-gate 					netdir_free(retaddrs, ND_ADDRLIST);
21407c478bd9Sstevel@tonic-gate 				free_knconf(argp->knconf);
21417c478bd9Sstevel@tonic-gate 				netbuf_free(argp->addr);
21427c478bd9Sstevel@tonic-gate 				freenetconfigent(nconf);
21437c478bd9Sstevel@tonic-gate 				nfs_free_secdata(
21441bbc88acSrm 				    argp->nfs_ext_u.nfs_extB.secdata);
21457c478bd9Sstevel@tonic-gate 				if (argp->syncaddr)
21467c478bd9Sstevel@tonic-gate 					netbuf_free(argp->syncaddr);
21477c478bd9Sstevel@tonic-gate 				if (argp->netname)
21487c478bd9Sstevel@tonic-gate 					free(argp->netname);
214939d3e169Sevanl 				if (argp->hostname)
215039d3e169Sevanl 					free(argp->hostname);
21517c478bd9Sstevel@tonic-gate 				free(argp->fh);
21527c478bd9Sstevel@tonic-gate 				free(argp);
21537c478bd9Sstevel@tonic-gate 				head = prevhead;
21547c478bd9Sstevel@tonic-gate 				tail = prevtail;
21557c478bd9Sstevel@tonic-gate 				if (tail)
21567c478bd9Sstevel@tonic-gate 					tail->nfs_ext_u.nfs_extB.next = NULL;
21577c478bd9Sstevel@tonic-gate 				last_error = NFSERR_IO;
21587c478bd9Sstevel@tonic-gate 
21597c478bd9Sstevel@tonic-gate 				if (error == RET_RETRY && retries-- > 0) {
21607c478bd9Sstevel@tonic-gate 					destroy_auth_client_handle(cl);
21617c478bd9Sstevel@tonic-gate 					DELAY(delay);
21627c478bd9Sstevel@tonic-gate 					goto retry;
21637c478bd9Sstevel@tonic-gate 				}
21647c478bd9Sstevel@tonic-gate 
21657c478bd9Sstevel@tonic-gate 				destroy_auth_client_handle(cl);
21667c478bd9Sstevel@tonic-gate 				skipentry = 1;
21677c478bd9Sstevel@tonic-gate 				mfs->mfs_ignore = 1;
21687c478bd9Sstevel@tonic-gate 				continue;
21697c478bd9Sstevel@tonic-gate 			}
21707c478bd9Sstevel@tonic-gate 			argp->flags |= NFSMNT_POSIX;
21717c478bd9Sstevel@tonic-gate 			if (trace > 4)
21727c478bd9Sstevel@tonic-gate 				trace_prt(1,
21737c478bd9Sstevel@tonic-gate 				    "  nfsmount: have pathconf for %s\n",
21747c478bd9Sstevel@tonic-gate 				    remname);
21757c478bd9Sstevel@tonic-gate 		}
21767c478bd9Sstevel@tonic-gate 
21777c478bd9Sstevel@tonic-gate 		/*
21787c478bd9Sstevel@tonic-gate 		 * free loop-specific data structures
21797c478bd9Sstevel@tonic-gate 		 */
21807c478bd9Sstevel@tonic-gate 		destroy_auth_client_handle(cl);
21817c478bd9Sstevel@tonic-gate 		freenetconfigent(nconf);
21827c478bd9Sstevel@tonic-gate 		if (secflags & AUTH_F_RPCTIMESYNC)
21837c478bd9Sstevel@tonic-gate 			netbuf_free(syncaddr);
21847c478bd9Sstevel@tonic-gate 		else if (retaddrs)
21857c478bd9Sstevel@tonic-gate 			netdir_free(retaddrs, ND_ADDRLIST);
21867c478bd9Sstevel@tonic-gate 
21877c478bd9Sstevel@tonic-gate 		/*
21887c478bd9Sstevel@tonic-gate 		 * Decide whether to use remote host's lockd or local locking.
21897c478bd9Sstevel@tonic-gate 		 * If we are using the public fh, we've already turned
21907c478bd9Sstevel@tonic-gate 		 * LLOCK on.
21917c478bd9Sstevel@tonic-gate 		 */
21927c478bd9Sstevel@tonic-gate 		if (hasmntopt(&m, MNTOPT_LLOCK))
21937c478bd9Sstevel@tonic-gate 			argp->flags |= NFSMNT_LLOCK;
21947c478bd9Sstevel@tonic-gate 		if (!(argp->flags & NFSMNT_LLOCK) && nfsvers == NFS_VERSION &&
21951bbc88acSrm 		    remote_lock(host, argp->fh)) {
21967c478bd9Sstevel@tonic-gate 			syslog(loglevel, "No network locking on %s : "
21977c478bd9Sstevel@tonic-gate 			"contact admin to install server change", host);
21987c478bd9Sstevel@tonic-gate 			argp->flags |= NFSMNT_LLOCK;
21997c478bd9Sstevel@tonic-gate 		}
22007c478bd9Sstevel@tonic-gate 
22017c478bd9Sstevel@tonic-gate 		/*
22027c478bd9Sstevel@tonic-gate 		 * Build a string for /etc/mnttab.
22037c478bd9Sstevel@tonic-gate 		 * If possible, coalesce strings with same 'dir' info.
22047c478bd9Sstevel@tonic-gate 		 */
22057c478bd9Sstevel@tonic-gate 		if ((mfs->mfs_flags & MFS_URL) == 0) {
22067c478bd9Sstevel@tonic-gate 			char *tmp;
22077c478bd9Sstevel@tonic-gate 
22087c478bd9Sstevel@tonic-gate 			if (mnttabcnt) {
22097c478bd9Sstevel@tonic-gate 				p = strrchr(mnttabtext, (int)':');
22107c478bd9Sstevel@tonic-gate 				if (!p || strcmp(p+1, dir) != 0) {
22117c478bd9Sstevel@tonic-gate 					mnttabcnt += strlen(remname) + 2;
22127c478bd9Sstevel@tonic-gate 				} else {
22137c478bd9Sstevel@tonic-gate 					*p = '\0';
22146a6d3e5eSjs 					mnttabcnt += strlen(rhost) + 2;
22157c478bd9Sstevel@tonic-gate 				}
22167c478bd9Sstevel@tonic-gate 				if ((tmp = realloc(mnttabtext,
22177c478bd9Sstevel@tonic-gate 				    mnttabcnt)) != NULL) {
22187c478bd9Sstevel@tonic-gate 					mnttabtext = tmp;
22197c478bd9Sstevel@tonic-gate 					strcat(mnttabtext, ",");
22207c478bd9Sstevel@tonic-gate 				} else {
22217c478bd9Sstevel@tonic-gate 					free(mnttabtext);
22227c478bd9Sstevel@tonic-gate 					mnttabtext = NULL;
22237c478bd9Sstevel@tonic-gate 				}
22247c478bd9Sstevel@tonic-gate 			} else {
22257c478bd9Sstevel@tonic-gate 				mnttabcnt = strlen(remname) + 1;
22267c478bd9Sstevel@tonic-gate 				if ((mnttabtext = malloc(mnttabcnt)) != NULL)
22277c478bd9Sstevel@tonic-gate 					mnttabtext[0] = '\0';
22287c478bd9Sstevel@tonic-gate 			}
22297c478bd9Sstevel@tonic-gate 
22307c478bd9Sstevel@tonic-gate 			if (mnttabtext != NULL)
22317c478bd9Sstevel@tonic-gate 				strcat(mnttabtext, remname);
22327c478bd9Sstevel@tonic-gate 
22337c478bd9Sstevel@tonic-gate 		} else {
22347c478bd9Sstevel@tonic-gate 			char *tmp;
22357c478bd9Sstevel@tonic-gate 			int more_cnt = 0;
22367c478bd9Sstevel@tonic-gate 			char sport[16];
22377c478bd9Sstevel@tonic-gate 
22387c478bd9Sstevel@tonic-gate 			more_cnt += strlen("nfs://");
22397c478bd9Sstevel@tonic-gate 			more_cnt += strlen(mfs->mfs_host);
22407c478bd9Sstevel@tonic-gate 
22417c478bd9Sstevel@tonic-gate 			if (mfs->mfs_port != 0) {
22427c478bd9Sstevel@tonic-gate 				(void) sprintf(sport, ":%u", mfs->mfs_port);
22437c478bd9Sstevel@tonic-gate 			} else
22447c478bd9Sstevel@tonic-gate 				sport[0] = '\0';
22457c478bd9Sstevel@tonic-gate 
22467c478bd9Sstevel@tonic-gate 			more_cnt += strlen(sport);
22477c478bd9Sstevel@tonic-gate 			more_cnt += 1; /* "/" */
22487c478bd9Sstevel@tonic-gate 			more_cnt += strlen(mfs->mfs_dir);
22497c478bd9Sstevel@tonic-gate 
22507c478bd9Sstevel@tonic-gate 			if (mnttabcnt) {
22517c478bd9Sstevel@tonic-gate 				more_cnt += 1; /* "," */
22527c478bd9Sstevel@tonic-gate 				mnttabcnt += more_cnt;
22537c478bd9Sstevel@tonic-gate 
22547c478bd9Sstevel@tonic-gate 				if ((tmp = realloc(mnttabtext,
22557c478bd9Sstevel@tonic-gate 				    mnttabcnt)) != NULL) {
22567c478bd9Sstevel@tonic-gate 					mnttabtext = tmp;
22577c478bd9Sstevel@tonic-gate 					strcat(mnttabtext, ",");
22587c478bd9Sstevel@tonic-gate 				} else {
22597c478bd9Sstevel@tonic-gate 					free(mnttabtext);
22607c478bd9Sstevel@tonic-gate 					mnttabtext = NULL;
22617c478bd9Sstevel@tonic-gate 				}
22627c478bd9Sstevel@tonic-gate 			} else {
22637c478bd9Sstevel@tonic-gate 				mnttabcnt = more_cnt + 1;
22647c478bd9Sstevel@tonic-gate 				if ((mnttabtext = malloc(mnttabcnt)) != NULL)
22657c478bd9Sstevel@tonic-gate 					mnttabtext[0] = '\0';
22667c478bd9Sstevel@tonic-gate 			}
22677c478bd9Sstevel@tonic-gate 
22687c478bd9Sstevel@tonic-gate 			if (mnttabtext != NULL) {
22697c478bd9Sstevel@tonic-gate 				strcat(mnttabtext, "nfs://");
22707c478bd9Sstevel@tonic-gate 				strcat(mnttabtext, mfs->mfs_host);
22717c478bd9Sstevel@tonic-gate 				strcat(mnttabtext, sport);
22727c478bd9Sstevel@tonic-gate 				strcat(mnttabtext, "/");
22737c478bd9Sstevel@tonic-gate 				strcat(mnttabtext, mfs->mfs_dir);
22747c478bd9Sstevel@tonic-gate 			}
22757c478bd9Sstevel@tonic-gate 		}
22767c478bd9Sstevel@tonic-gate 
22777c478bd9Sstevel@tonic-gate 		if (!mnttabtext) {
22787c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "nfsmount: no memory");
22797c478bd9Sstevel@tonic-gate 			last_error = NFSERR_IO;
22807c478bd9Sstevel@tonic-gate 			goto out;
22817c478bd9Sstevel@tonic-gate 		}
22827c478bd9Sstevel@tonic-gate 
22837c478bd9Sstevel@tonic-gate 		/*
22847c478bd9Sstevel@tonic-gate 		 * At least one entry, can call mount(2).
22857c478bd9Sstevel@tonic-gate 		 */
22867c478bd9Sstevel@tonic-gate 		entries++;
22877c478bd9Sstevel@tonic-gate 
22887c478bd9Sstevel@tonic-gate 		/*
22897c478bd9Sstevel@tonic-gate 		 * If replication was defeated, don't do more work
22907c478bd9Sstevel@tonic-gate 		 */
22917c478bd9Sstevel@tonic-gate 		if (!replicated)
22927c478bd9Sstevel@tonic-gate 			break;
22937c478bd9Sstevel@tonic-gate 	}
22947c478bd9Sstevel@tonic-gate 
22957c478bd9Sstevel@tonic-gate 
22967c478bd9Sstevel@tonic-gate 	/*
22977c478bd9Sstevel@tonic-gate 	 * Did we get through all possibilities without success?
22987c478bd9Sstevel@tonic-gate 	 */
22997c478bd9Sstevel@tonic-gate 	if (!entries)
23007c478bd9Sstevel@tonic-gate 		goto out;
23017c478bd9Sstevel@tonic-gate 
23027c478bd9Sstevel@tonic-gate 	/* Make "xattr" the default if "noxattr" is not specified. */
23037c478bd9Sstevel@tonic-gate 	strcpy(mopts, opts);
23047c478bd9Sstevel@tonic-gate 	if (!hasmntopt(&m, MNTOPT_NOXATTR) && !hasmntopt(&m, MNTOPT_XATTR)) {
23057c478bd9Sstevel@tonic-gate 		if (strlen(mopts) > 0)
23067c478bd9Sstevel@tonic-gate 			strcat(mopts, ",");
23077c478bd9Sstevel@tonic-gate 		strcat(mopts, "xattr");
23087c478bd9Sstevel@tonic-gate 	}
23097c478bd9Sstevel@tonic-gate 
23107c478bd9Sstevel@tonic-gate 	/*
23117c478bd9Sstevel@tonic-gate 	 * enable services as needed.
23127c478bd9Sstevel@tonic-gate 	 */
23137c478bd9Sstevel@tonic-gate 	{
23147c478bd9Sstevel@tonic-gate 		char **sl;
23157c478bd9Sstevel@tonic-gate 
23167c478bd9Sstevel@tonic-gate 		if (strcmp(fstype, MNTTYPE_NFS4) == 0)
23177c478bd9Sstevel@tonic-gate 			sl = service_list_v4;
23187c478bd9Sstevel@tonic-gate 		else
23197c478bd9Sstevel@tonic-gate 			sl = service_list;
23207c478bd9Sstevel@tonic-gate 
232145916cd2Sjpk 		(void) _check_services(sl);
23227c478bd9Sstevel@tonic-gate 	}
23237c478bd9Sstevel@tonic-gate 
23247c478bd9Sstevel@tonic-gate 	/*
23257c478bd9Sstevel@tonic-gate 	 * Whew; do the mount, at last.
23267c478bd9Sstevel@tonic-gate 	 */
23277c478bd9Sstevel@tonic-gate 	if (trace > 1) {
23287c478bd9Sstevel@tonic-gate 		trace_prt(1, "	mount %s %s (%s)\n", mnttabtext, mntpnt, mopts);
23297c478bd9Sstevel@tonic-gate 	}
23307c478bd9Sstevel@tonic-gate 
2331b9238976Sth 	/*
2332b9238976Sth 	 * About to do a nfs mount, make sure the mount_to is set for
2333b9238976Sth 	 * potential ephemeral mounts with NFSv4.
2334b9238976Sth 	 */
2335b9238976Sth 	set_nfsv4_ephemeral_mount_to();
2336b9238976Sth 
233739d3e169Sevanl 	/*
233839d3e169Sevanl 	 * If no action list pointer then do the mount, otherwise
233939d3e169Sevanl 	 * build the actions list pointer with the mount information.
234039d3e169Sevanl 	 * so the mount can be done in the kernel.
234139d3e169Sevanl 	 */
234239d3e169Sevanl 	if (alp == NULL) {
234339d3e169Sevanl 		if (mount(mnttabtext, mntpnt, flags | MS_DATA, fstype,
23441bbc88acSrm 		    head, sizeof (*head), mopts, MAX_MNTOPT_STR) < 0) {
234539d3e169Sevanl 			if (trace > 1)
234639d3e169Sevanl 				trace_prt(1, "	Mount of %s on %s: %d\n",
23471bbc88acSrm 				    mnttabtext, mntpnt, errno);
234839d3e169Sevanl 			if (errno != EBUSY || verbose)
234939d3e169Sevanl 				syslog(LOG_ERR,
23507c478bd9Sstevel@tonic-gate 				"Mount of %s on %s: %m", mnttabtext, mntpnt);
235139d3e169Sevanl 			last_error = NFSERR_IO;
235239d3e169Sevanl 			goto out;
235339d3e169Sevanl 		}
23547c478bd9Sstevel@tonic-gate 
235539d3e169Sevanl 		last_error = NFS_OK;
235639d3e169Sevanl 		if (stat(mntpnt, &stbuf) == 0) {
235739d3e169Sevanl 			if (trace > 1) {
235839d3e169Sevanl 				trace_prt(1, "	mount %s dev=%x rdev=%x OK\n",
23591bbc88acSrm 				    mnttabtext, stbuf.st_dev, stbuf.st_rdev);
236039d3e169Sevanl 			}
236139d3e169Sevanl 		} else {
236239d3e169Sevanl 			if (trace > 1) {
236339d3e169Sevanl 				trace_prt(1, "	mount %s OK\n", mnttabtext);
236439d3e169Sevanl 				trace_prt(1, "	stat of %s failed\n", mntpnt);
236539d3e169Sevanl 			}
236639d3e169Sevanl 
23677c478bd9Sstevel@tonic-gate 		}
23687c478bd9Sstevel@tonic-gate 	} else {
236939d3e169Sevanl 		alp->action.action = AUTOFS_MOUNT_RQ;
237039d3e169Sevanl 		alp->action.action_list_entry_u.mounta.spec =
23711bbc88acSrm 		    strdup(mnttabtext);
237239d3e169Sevanl 		alp->action.action_list_entry_u.mounta.dir = strdup(mntpnt);
237339d3e169Sevanl 		alp->action.action_list_entry_u.mounta.flags =
23741bbc88acSrm 		    flags | MS_DATA;
237539d3e169Sevanl 		alp->action.action_list_entry_u.mounta.fstype =
23761bbc88acSrm 		    strdup(fstype);
237739d3e169Sevanl 		alp->action.action_list_entry_u.mounta.dataptr = (char *)head;
237839d3e169Sevanl 		alp->action.action_list_entry_u.mounta.datalen =
23791bbc88acSrm 		    sizeof (*head);
238039d3e169Sevanl 		mntopts = malloc(strlen(mopts) + 1);
238139d3e169Sevanl 		strcpy(mntopts, mopts);
238239d3e169Sevanl 		mntopts[strlen(mopts)] = '\0';
238339d3e169Sevanl 		alp->action.action_list_entry_u.mounta.optptr = mntopts;
238439d3e169Sevanl 		alp->action.action_list_entry_u.mounta.optlen =
23851bbc88acSrm 		    strlen(mntopts) + 1;
238639d3e169Sevanl 		last_error = NFS_OK;
238739d3e169Sevanl 		goto ret;
23887c478bd9Sstevel@tonic-gate 	}
23897c478bd9Sstevel@tonic-gate 
23907c478bd9Sstevel@tonic-gate out:
23917c478bd9Sstevel@tonic-gate 	argp = head;
23927c478bd9Sstevel@tonic-gate 	while (argp) {
23937c478bd9Sstevel@tonic-gate 		if (argp->pathconf)
23947c478bd9Sstevel@tonic-gate 			free(argp->pathconf);
23957c478bd9Sstevel@tonic-gate 		free_knconf(argp->knconf);
23967c478bd9Sstevel@tonic-gate 		netbuf_free(argp->addr);
23977c478bd9Sstevel@tonic-gate 		if (argp->syncaddr)
23987c478bd9Sstevel@tonic-gate 			netbuf_free(argp->syncaddr);
23997c478bd9Sstevel@tonic-gate 		if (argp->netname) {
24007c478bd9Sstevel@tonic-gate 			free(argp->netname);
24017c478bd9Sstevel@tonic-gate 		}
240239d3e169Sevanl 		if (argp->hostname)
240339d3e169Sevanl 			free(argp->hostname);
24047c478bd9Sstevel@tonic-gate 		nfs_free_secdata(argp->nfs_ext_u.nfs_extB.secdata);
24057c478bd9Sstevel@tonic-gate 		free(argp->fh);
24067c478bd9Sstevel@tonic-gate 		head = argp;
24077c478bd9Sstevel@tonic-gate 		argp = argp->nfs_ext_u.nfs_extB.next;
24087c478bd9Sstevel@tonic-gate 		free(head);
24097c478bd9Sstevel@tonic-gate 	}
24107c478bd9Sstevel@tonic-gate ret:
24117c478bd9Sstevel@tonic-gate 	if (nfs_proto)
24127c478bd9Sstevel@tonic-gate 		free(nfs_proto);
24137c478bd9Sstevel@tonic-gate 	if (mnttabtext)
24147c478bd9Sstevel@tonic-gate 		free(mnttabtext);
24157c478bd9Sstevel@tonic-gate 
24167c478bd9Sstevel@tonic-gate 	for (mfs = mfs_in; mfs; mfs = mfs->mfs_next) {
24177c478bd9Sstevel@tonic-gate 
24187c478bd9Sstevel@tonic-gate 		if (mfs->mfs_flags & MFS_ALLOC_DIR) {
24197c478bd9Sstevel@tonic-gate 			free(mfs->mfs_dir);
24207c478bd9Sstevel@tonic-gate 			mfs->mfs_dir = NULL;
24217c478bd9Sstevel@tonic-gate 			mfs->mfs_flags &= ~MFS_ALLOC_DIR;
24227c478bd9Sstevel@tonic-gate 		}
24237c478bd9Sstevel@tonic-gate 
242439d3e169Sevanl 		if (mfs->mfs_args != NULL && alp == NULL) {
24257c478bd9Sstevel@tonic-gate 			free(mfs->mfs_args);
24267c478bd9Sstevel@tonic-gate 			mfs->mfs_args = NULL;
24277c478bd9Sstevel@tonic-gate 		}
24287c478bd9Sstevel@tonic-gate 
24297c478bd9Sstevel@tonic-gate 		if (mfs->mfs_nconf != NULL) {
24307c478bd9Sstevel@tonic-gate 			freenetconfigent(mfs->mfs_nconf);
24317c478bd9Sstevel@tonic-gate 			mfs->mfs_nconf = NULL;
24327c478bd9Sstevel@tonic-gate 		}
24337c478bd9Sstevel@tonic-gate 	}
24347c478bd9Sstevel@tonic-gate 
24357c478bd9Sstevel@tonic-gate 	return (last_error);
24367c478bd9Sstevel@tonic-gate }
24377c478bd9Sstevel@tonic-gate 
24387c478bd9Sstevel@tonic-gate /*
24397c478bd9Sstevel@tonic-gate  * get_pathconf(cl, path, fsname, pcnf, cretries)
24407c478bd9Sstevel@tonic-gate  * ugliness that requires that ppathcnf and pathcnf stay consistent
24417c478bd9Sstevel@tonic-gate  * cretries is a copy of retries used to determine when to syslog
24427c478bd9Sstevel@tonic-gate  * on retry situations.
24437c478bd9Sstevel@tonic-gate  */
24447c478bd9Sstevel@tonic-gate static int
get_pathconf(CLIENT * cl,char * path,char * fsname,struct pathcnf ** pcnf,int cretries)24457c478bd9Sstevel@tonic-gate get_pathconf(CLIENT *cl, char *path, char *fsname, struct pathcnf **pcnf,
24464dc70696SToomas Soome     int cretries)
24477c478bd9Sstevel@tonic-gate {
24487c478bd9Sstevel@tonic-gate 	struct ppathcnf *p = NULL;
24497c478bd9Sstevel@tonic-gate 	enum clnt_stat rpc_stat;
24507c478bd9Sstevel@tonic-gate 	struct timeval timeout;
24517c478bd9Sstevel@tonic-gate 
24527c478bd9Sstevel@tonic-gate 	p = (struct ppathcnf *)malloc(sizeof (struct ppathcnf));
24537c478bd9Sstevel@tonic-gate 	if (p == NULL) {
24547c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "get_pathconf: Out of memory");
24557c478bd9Sstevel@tonic-gate 		return (RET_ERR);
24567c478bd9Sstevel@tonic-gate 	}
24577c478bd9Sstevel@tonic-gate 	memset((caddr_t)p, 0, sizeof (struct ppathcnf));
24587c478bd9Sstevel@tonic-gate 
24597c478bd9Sstevel@tonic-gate 	timeout.tv_sec = 10;
24607c478bd9Sstevel@tonic-gate 	timeout.tv_usec = 0;
24617c478bd9Sstevel@tonic-gate 	rpc_stat = clnt_call(cl, MOUNTPROC_PATHCONF,
24627c478bd9Sstevel@tonic-gate 	    xdr_dirpath, (caddr_t)&path, xdr_ppathcnf, (caddr_t)p, timeout);
24637c478bd9Sstevel@tonic-gate 	if (rpc_stat != RPC_SUCCESS) {
24647c478bd9Sstevel@tonic-gate 		if (cretries-- <= 0) {
24657c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR,
24667c478bd9Sstevel@tonic-gate 			    "get_pathconf: %s: server not responding: %s",
24677c478bd9Sstevel@tonic-gate 			    fsname, clnt_sperror(cl, ""));
24687c478bd9Sstevel@tonic-gate 		}
24697c478bd9Sstevel@tonic-gate 		free(p);
24707c478bd9Sstevel@tonic-gate 		return (RET_RETRY);
24717c478bd9Sstevel@tonic-gate 	}
24727c478bd9Sstevel@tonic-gate 	if (_PC_ISSET(_PC_ERROR, p->pc_mask)) {
24737c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "get_pathconf: no info for %s", fsname);
24747c478bd9Sstevel@tonic-gate 		free(p);
24757c478bd9Sstevel@tonic-gate 		return (RET_ERR);
24767c478bd9Sstevel@tonic-gate 	}
24777c478bd9Sstevel@tonic-gate 	*pcnf = (struct pathcnf *)p;
24787c478bd9Sstevel@tonic-gate 	return (RET_OK);
24797c478bd9Sstevel@tonic-gate }
24807c478bd9Sstevel@tonic-gate 
248139d3e169Sevanl void
netbuf_free(struct netbuf * nb)24824dc70696SToomas Soome netbuf_free(struct netbuf *nb)
24837c478bd9Sstevel@tonic-gate {
24847c478bd9Sstevel@tonic-gate 	if (nb == NULL)
24857c478bd9Sstevel@tonic-gate 		return;
24864dc70696SToomas Soome 	free(nb->buf);
24877c478bd9Sstevel@tonic-gate 	free(nb);
24887c478bd9Sstevel@tonic-gate }
24897c478bd9Sstevel@tonic-gate 
24907c478bd9Sstevel@tonic-gate #define	SMALL_HOSTNAME		20
24917c478bd9Sstevel@tonic-gate #define	SMALL_PROTONAME		10
24927c478bd9Sstevel@tonic-gate #define	SMALL_PROTOFMLYNAME		10
24937c478bd9Sstevel@tonic-gate 
24947c478bd9Sstevel@tonic-gate struct portmap_cache {
24957c478bd9Sstevel@tonic-gate 	int cache_prog;
24967c478bd9Sstevel@tonic-gate 	int cache_vers;
24977c478bd9Sstevel@tonic-gate 	time_t cache_time;
24987c478bd9Sstevel@tonic-gate 	char cache_small_hosts[SMALL_HOSTNAME + 1];
24997c478bd9Sstevel@tonic-gate 	char *cache_hostname;
25007c478bd9Sstevel@tonic-gate 	char *cache_proto;
25017c478bd9Sstevel@tonic-gate 	char *cache_protofmly;
25027c478bd9Sstevel@tonic-gate 	char cache_small_protofmly[SMALL_PROTOFMLYNAME + 1];
25037c478bd9Sstevel@tonic-gate 	char cache_small_proto[SMALL_PROTONAME + 1];
25047c478bd9Sstevel@tonic-gate 	struct netbuf cache_srv_addr;
25057c478bd9Sstevel@tonic-gate 	struct portmap_cache *cache_prev, *cache_next;
25067c478bd9Sstevel@tonic-gate };
25077c478bd9Sstevel@tonic-gate 
25087c478bd9Sstevel@tonic-gate rwlock_t portmap_cache_lock;
25097c478bd9Sstevel@tonic-gate static int portmap_cache_valid_time = 30;
25107c478bd9Sstevel@tonic-gate struct portmap_cache *portmap_cache_head, *portmap_cache_tail;
25117c478bd9Sstevel@tonic-gate 
25127c478bd9Sstevel@tonic-gate #ifdef MALLOC_DEBUG
25137c478bd9Sstevel@tonic-gate void
portmap_cache_flush(void)25144dc70696SToomas Soome portmap_cache_flush(void)
25157c478bd9Sstevel@tonic-gate {
25167c478bd9Sstevel@tonic-gate 	struct  portmap_cache *next = NULL, *cp;
25177c478bd9Sstevel@tonic-gate 
25187c478bd9Sstevel@tonic-gate 	(void) rw_wrlock(&portmap_cache_lock);
25197c478bd9Sstevel@tonic-gate 	for (cp = portmap_cache_head; cp; cp = cp->cache_next) {
25207c478bd9Sstevel@tonic-gate 		if (cp->cache_hostname != NULL &&
25217c478bd9Sstevel@tonic-gate 		    cp->cache_hostname !=
25227c478bd9Sstevel@tonic-gate 		    cp->cache_small_hosts)
25237c478bd9Sstevel@tonic-gate 			free(cp->cache_hostname);
25247c478bd9Sstevel@tonic-gate 		if (cp->cache_proto != NULL &&
25257c478bd9Sstevel@tonic-gate 		    cp->cache_proto !=
25267c478bd9Sstevel@tonic-gate 		    cp->cache_small_proto)
25277c478bd9Sstevel@tonic-gate 			free(cp->cache_proto);
25287c478bd9Sstevel@tonic-gate 		if (cp->cache_srv_addr.buf != NULL)
25297c478bd9Sstevel@tonic-gate 			free(cp->cache_srv_addr.buf);
25307c478bd9Sstevel@tonic-gate 		next = cp->cache_next;
25317c478bd9Sstevel@tonic-gate 		free(cp);
25327c478bd9Sstevel@tonic-gate 	}
25337c478bd9Sstevel@tonic-gate 	portmap_cache_head = NULL;
25347c478bd9Sstevel@tonic-gate 	portmap_cache_tail = NULL;
25357c478bd9Sstevel@tonic-gate 	(void) rw_unlock(&portmap_cache_lock);
25367c478bd9Sstevel@tonic-gate }
25377c478bd9Sstevel@tonic-gate #endif
25387c478bd9Sstevel@tonic-gate 
25397c478bd9Sstevel@tonic-gate /*
25407c478bd9Sstevel@tonic-gate  * Returns 1 if the entry is found in the cache, 0 otherwise.
25417c478bd9Sstevel@tonic-gate  */
25427c478bd9Sstevel@tonic-gate static int
portmap_cache_lookup(char * hostname,rpcprog_t prog,rpcvers_t vers,struct netconfig * nconf,struct netbuf * addrp)25434dc70696SToomas Soome portmap_cache_lookup(char *hostname, rpcprog_t prog, rpcvers_t vers,
25444dc70696SToomas Soome     struct netconfig *nconf, struct netbuf *addrp)
25457c478bd9Sstevel@tonic-gate {
25467c478bd9Sstevel@tonic-gate 	struct	portmap_cache *cachep, *prev, *next = NULL, *cp;
25477c478bd9Sstevel@tonic-gate 	int	retval = 0;
25487c478bd9Sstevel@tonic-gate 
25497c478bd9Sstevel@tonic-gate 	timenow = time(NULL);
25507c478bd9Sstevel@tonic-gate 
25517c478bd9Sstevel@tonic-gate 	(void) rw_rdlock(&portmap_cache_lock);
25527c478bd9Sstevel@tonic-gate 
25537c478bd9Sstevel@tonic-gate 	/*
25547c478bd9Sstevel@tonic-gate 	 * Increment the portmap cache counters for # accesses and lookups
25557c478bd9Sstevel@tonic-gate 	 * Use a smaller factor (100 vs 1000 for the host cache) since
25567c478bd9Sstevel@tonic-gate 	 * initial analysis shows this cache is looked up 10% that of the
25577c478bd9Sstevel@tonic-gate 	 * host cache.
25587c478bd9Sstevel@tonic-gate 	 */
25597c478bd9Sstevel@tonic-gate #ifdef CACHE_DEBUG
25607c478bd9Sstevel@tonic-gate 	portmap_cache_accesses++;
25617c478bd9Sstevel@tonic-gate 	portmap_cache_lookups++;
25627c478bd9Sstevel@tonic-gate 	if ((portmap_cache_lookups%100) == 0)
25637c478bd9Sstevel@tonic-gate 		trace_portmap_cache();
25647c478bd9Sstevel@tonic-gate #endif /* CACHE_DEBUG */
25657c478bd9Sstevel@tonic-gate 
25667c478bd9Sstevel@tonic-gate 	for (cachep = portmap_cache_head; cachep;
25674dc70696SToomas Soome 	    cachep = cachep->cache_next) {
25687c478bd9Sstevel@tonic-gate 		if (timenow > cachep->cache_time) {
25697c478bd9Sstevel@tonic-gate 			/*
25707c478bd9Sstevel@tonic-gate 			 * We stumbled across an entry in the cache which
25717c478bd9Sstevel@tonic-gate 			 * has timed out. Free up all the entries that
25727c478bd9Sstevel@tonic-gate 			 * were added before it, which will positionally
25737c478bd9Sstevel@tonic-gate 			 * be after this entry. And adjust neighboring
25747c478bd9Sstevel@tonic-gate 			 * pointers.
25757c478bd9Sstevel@tonic-gate 			 * When we drop the lock and re-acquire it, we
25767c478bd9Sstevel@tonic-gate 			 * need to start from the beginning.
25777c478bd9Sstevel@tonic-gate 			 */
25787c478bd9Sstevel@tonic-gate 			(void) rw_unlock(&portmap_cache_lock);
25797c478bd9Sstevel@tonic-gate 			(void) rw_wrlock(&portmap_cache_lock);
25807c478bd9Sstevel@tonic-gate 			for (cp = portmap_cache_head;
25814dc70696SToomas Soome 			    cp && (cp->cache_time >= timenow);
25824dc70696SToomas Soome 			    cp = cp->cache_next)
25837c478bd9Sstevel@tonic-gate 				;
25847c478bd9Sstevel@tonic-gate 			if (cp == NULL)
25857c478bd9Sstevel@tonic-gate 				goto done;
25867c478bd9Sstevel@tonic-gate 			/*
25877c478bd9Sstevel@tonic-gate 			 * Adjust the link of the predecessor.
25887c478bd9Sstevel@tonic-gate 			 * Make the tail point to the new last entry.
25897c478bd9Sstevel@tonic-gate 			 */
25907c478bd9Sstevel@tonic-gate 			prev = cp->cache_prev;
25917c478bd9Sstevel@tonic-gate 			if (prev == NULL) {
25927c478bd9Sstevel@tonic-gate 				portmap_cache_head = NULL;
25937c478bd9Sstevel@tonic-gate 				portmap_cache_tail = NULL;
25947c478bd9Sstevel@tonic-gate 			} else {
25957c478bd9Sstevel@tonic-gate 				prev->cache_next = NULL;
25967c478bd9Sstevel@tonic-gate 				portmap_cache_tail = prev;
25977c478bd9Sstevel@tonic-gate 			}
25987c478bd9Sstevel@tonic-gate 			for (; cp; cp = next) {
25997c478bd9Sstevel@tonic-gate 				if (cp->cache_hostname != NULL &&
26007c478bd9Sstevel@tonic-gate 				    cp->cache_hostname !=
26017c478bd9Sstevel@tonic-gate 				    cp->cache_small_hosts)
26027c478bd9Sstevel@tonic-gate 					free(cp->cache_hostname);
26037c478bd9Sstevel@tonic-gate 				if (cp->cache_proto != NULL &&
26047c478bd9Sstevel@tonic-gate 				    cp->cache_proto !=
26057c478bd9Sstevel@tonic-gate 				    cp->cache_small_proto)
26067c478bd9Sstevel@tonic-gate 					free(cp->cache_proto);
26077c478bd9Sstevel@tonic-gate 				if (cp->cache_srv_addr.buf != NULL)
26087c478bd9Sstevel@tonic-gate 					free(cp->cache_srv_addr.buf);
26097c478bd9Sstevel@tonic-gate 				next = cp->cache_next;
26107c478bd9Sstevel@tonic-gate 				free(cp);
26117c478bd9Sstevel@tonic-gate 			}
26127c478bd9Sstevel@tonic-gate 			goto done;
26137c478bd9Sstevel@tonic-gate 		}
26147c478bd9Sstevel@tonic-gate 		if (cachep->cache_hostname == NULL ||
26157c478bd9Sstevel@tonic-gate 		    prog != cachep->cache_prog || vers != cachep->cache_vers ||
26167c478bd9Sstevel@tonic-gate 		    strcmp(nconf->nc_proto, cachep->cache_proto) != 0 ||
26177c478bd9Sstevel@tonic-gate 		    strcmp(nconf->nc_protofmly, cachep->cache_protofmly) != 0 ||
26187c478bd9Sstevel@tonic-gate 		    strcmp(hostname, cachep->cache_hostname) != 0)
26197c478bd9Sstevel@tonic-gate 			continue;
26207c478bd9Sstevel@tonic-gate 		/*
26217c478bd9Sstevel@tonic-gate 		 * Cache Hit.
26227c478bd9Sstevel@tonic-gate 		 */
26237c478bd9Sstevel@tonic-gate #ifdef CACHE_DEBUG
26247c478bd9Sstevel@tonic-gate 		portmap_cache_hits++;	/* up portmap cache hit counter */
26257c478bd9Sstevel@tonic-gate #endif /* CACHE_DEBUG */
26267c478bd9Sstevel@tonic-gate 		addrp->len = cachep->cache_srv_addr.len;
26277c478bd9Sstevel@tonic-gate 		memcpy(addrp->buf, cachep->cache_srv_addr.buf, addrp->len);
26287c478bd9Sstevel@tonic-gate 		retval = 1;
26297c478bd9Sstevel@tonic-gate 		break;
26307c478bd9Sstevel@tonic-gate 	}
26317c478bd9Sstevel@tonic-gate done:
26327c478bd9Sstevel@tonic-gate 	(void) rw_unlock(&portmap_cache_lock);
26337c478bd9Sstevel@tonic-gate 	return (retval);
26347c478bd9Sstevel@tonic-gate }
26357c478bd9Sstevel@tonic-gate 
26367c478bd9Sstevel@tonic-gate static void
portmap_cache_enter(char * hostname,rpcprog_t prog,rpcvers_t vers,struct netconfig * nconf,struct netbuf * addrp)26374dc70696SToomas Soome portmap_cache_enter(char *hostname, rpcprog_t prog, rpcvers_t vers,
26384dc70696SToomas Soome     struct netconfig *nconf, struct netbuf *addrp)
26397c478bd9Sstevel@tonic-gate {
26407c478bd9Sstevel@tonic-gate 	struct portmap_cache *cachep;
26417c478bd9Sstevel@tonic-gate 	int protofmlylen;
26427c478bd9Sstevel@tonic-gate 	int protolen, hostnamelen;
26437c478bd9Sstevel@tonic-gate 
26447c478bd9Sstevel@tonic-gate 	timenow = time(NULL);
26457c478bd9Sstevel@tonic-gate 
26467c478bd9Sstevel@tonic-gate 	cachep = malloc(sizeof (struct portmap_cache));
26477c478bd9Sstevel@tonic-gate 	if (cachep == NULL)
26487c478bd9Sstevel@tonic-gate 		return;
26497c478bd9Sstevel@tonic-gate 	memset((char *)cachep, 0, sizeof (*cachep));
26507c478bd9Sstevel@tonic-gate 
26517c478bd9Sstevel@tonic-gate 	hostnamelen = strlen(hostname);
26527c478bd9Sstevel@tonic-gate 	if (hostnamelen <= SMALL_HOSTNAME)
26537c478bd9Sstevel@tonic-gate 		cachep->cache_hostname = cachep->cache_small_hosts;
26547c478bd9Sstevel@tonic-gate 	else {
26557c478bd9Sstevel@tonic-gate 		cachep->cache_hostname = malloc(hostnamelen + 1);
26567c478bd9Sstevel@tonic-gate 		if (cachep->cache_hostname == NULL)
26577c478bd9Sstevel@tonic-gate 			goto nomem;
26587c478bd9Sstevel@tonic-gate 	}
26597c478bd9Sstevel@tonic-gate 	strcpy(cachep->cache_hostname, hostname);
26607c478bd9Sstevel@tonic-gate 	protolen = strlen(nconf->nc_proto);
26617c478bd9Sstevel@tonic-gate 	if (protolen <= SMALL_PROTONAME)
26627c478bd9Sstevel@tonic-gate 		cachep->cache_proto = cachep->cache_small_proto;
26637c478bd9Sstevel@tonic-gate 	else {
26647c478bd9Sstevel@tonic-gate 		cachep->cache_proto = malloc(protolen + 1);
26657c478bd9Sstevel@tonic-gate 		if (cachep->cache_proto == NULL)
26667c478bd9Sstevel@tonic-gate 			goto nomem;
26677c478bd9Sstevel@tonic-gate 	}
26687c478bd9Sstevel@tonic-gate 	protofmlylen = strlen(nconf->nc_protofmly);
26697c478bd9Sstevel@tonic-gate 	if (protofmlylen <= SMALL_PROTOFMLYNAME)
26707c478bd9Sstevel@tonic-gate 		cachep->cache_protofmly = cachep->cache_small_protofmly;
26717c478bd9Sstevel@tonic-gate 	else {
26727c478bd9Sstevel@tonic-gate 		cachep->cache_protofmly = malloc(protofmlylen + 1);
26737c478bd9Sstevel@tonic-gate 		if (cachep->cache_protofmly == NULL)
26747c478bd9Sstevel@tonic-gate 			goto nomem;
26757c478bd9Sstevel@tonic-gate 	}
26767c478bd9Sstevel@tonic-gate 
26777c478bd9Sstevel@tonic-gate 	strcpy(cachep->cache_proto, nconf->nc_proto);
26787c478bd9Sstevel@tonic-gate 	cachep->cache_prog = prog;
26797c478bd9Sstevel@tonic-gate 	cachep->cache_vers = vers;
26807c478bd9Sstevel@tonic-gate 	cachep->cache_time = timenow + portmap_cache_valid_time;
26817c478bd9Sstevel@tonic-gate 	cachep->cache_srv_addr.len = addrp->len;
26827c478bd9Sstevel@tonic-gate 	cachep->cache_srv_addr.buf = malloc(addrp->len);
26837c478bd9Sstevel@tonic-gate 	if (cachep->cache_srv_addr.buf == NULL)
26847c478bd9Sstevel@tonic-gate 		goto nomem;
26857c478bd9Sstevel@tonic-gate 	memcpy(cachep->cache_srv_addr.buf, addrp->buf, addrp->maxlen);
26867c478bd9Sstevel@tonic-gate 	cachep->cache_prev = NULL;
26877c478bd9Sstevel@tonic-gate 	(void) rw_wrlock(&portmap_cache_lock);
26887c478bd9Sstevel@tonic-gate 	/*
26897c478bd9Sstevel@tonic-gate 	 * There's a window in which we could have multiple threads making
26907c478bd9Sstevel@tonic-gate 	 * the same cache entry. This can be avoided by walking the cache
26917c478bd9Sstevel@tonic-gate 	 * once again here to check and see if there are duplicate entries
26927c478bd9Sstevel@tonic-gate 	 * (after grabbing the write lock). This isn't fatal and I'm not
26937c478bd9Sstevel@tonic-gate 	 * going to bother with this.
26947c478bd9Sstevel@tonic-gate 	 */
26957c478bd9Sstevel@tonic-gate #ifdef CACHE_DEBUG
26967c478bd9Sstevel@tonic-gate 	portmap_cache_accesses++;	/* up portmap cache access counter */
26977c478bd9Sstevel@tonic-gate #endif /* CACHE_DEBUG */
26987c478bd9Sstevel@tonic-gate 	cachep->cache_next = portmap_cache_head;
26997c478bd9Sstevel@tonic-gate 	if (portmap_cache_head != NULL)
27007c478bd9Sstevel@tonic-gate 		portmap_cache_head->cache_prev = cachep;
27017c478bd9Sstevel@tonic-gate 	portmap_cache_head = cachep;
27027c478bd9Sstevel@tonic-gate 	(void) rw_unlock(&portmap_cache_lock);
27037c478bd9Sstevel@tonic-gate 	return;
27047c478bd9Sstevel@tonic-gate 
27057c478bd9Sstevel@tonic-gate nomem:
27067c478bd9Sstevel@tonic-gate 	syslog(LOG_ERR, "portmap_cache_enter: Memory allocation failed");
27077c478bd9Sstevel@tonic-gate 	if (cachep->cache_srv_addr.buf)
27087c478bd9Sstevel@tonic-gate 		free(cachep->cache_srv_addr.buf);
27097c478bd9Sstevel@tonic-gate 	if (cachep->cache_proto && protolen > SMALL_PROTONAME)
27107c478bd9Sstevel@tonic-gate 		free(cachep->cache_proto);
27117c478bd9Sstevel@tonic-gate 	if (cachep->cache_hostname && hostnamelen > SMALL_HOSTNAME)
27127c478bd9Sstevel@tonic-gate 		free(cachep->cache_hostname);
27137c478bd9Sstevel@tonic-gate 	if (cachep->cache_protofmly && protofmlylen > SMALL_PROTOFMLYNAME)
27147c478bd9Sstevel@tonic-gate 		free(cachep->cache_protofmly);
27157c478bd9Sstevel@tonic-gate 	if (cachep)
27167c478bd9Sstevel@tonic-gate 		free(cachep);
27177c478bd9Sstevel@tonic-gate 	cachep = NULL;
27187c478bd9Sstevel@tonic-gate }
27197c478bd9Sstevel@tonic-gate 
27207c478bd9Sstevel@tonic-gate static int
get_cached_srv_addr(char * hostname,rpcprog_t prog,rpcvers_t vers,struct netconfig * nconf,struct netbuf * addrp)27217c478bd9Sstevel@tonic-gate get_cached_srv_addr(char *hostname, rpcprog_t prog, rpcvers_t vers,
27224dc70696SToomas Soome     struct netconfig *nconf, struct netbuf *addrp)
27237c478bd9Sstevel@tonic-gate {
27247c478bd9Sstevel@tonic-gate 	if (portmap_cache_lookup(hostname, prog, vers, nconf, addrp))
27257c478bd9Sstevel@tonic-gate 		return (1);
27267c478bd9Sstevel@tonic-gate 	if (rpcb_getaddr(prog, vers, nconf, addrp, hostname) == 0)
27277c478bd9Sstevel@tonic-gate 		return (0);
27287c478bd9Sstevel@tonic-gate 	portmap_cache_enter(hostname, prog, vers, nconf, addrp);
27297c478bd9Sstevel@tonic-gate 	return (1);
27307c478bd9Sstevel@tonic-gate }
27317c478bd9Sstevel@tonic-gate 
27327c478bd9Sstevel@tonic-gate /*
27337c478bd9Sstevel@tonic-gate  * Get a network address on "hostname" for program "prog"
27347c478bd9Sstevel@tonic-gate  * with version "vers".  If the port number is specified (non zero)
27357c478bd9Sstevel@tonic-gate  * then try for a TCP/UDP transport and set the port number of the
27367c478bd9Sstevel@tonic-gate  * resulting IP address.
27377c478bd9Sstevel@tonic-gate  *
27387c478bd9Sstevel@tonic-gate  * If the address of a netconfig pointer was passed and
27397c478bd9Sstevel@tonic-gate  * if it's not null, use it as the netconfig otherwise
27407c478bd9Sstevel@tonic-gate  * assign the address of the netconfig that was used to
27417c478bd9Sstevel@tonic-gate  * establish contact with the service.
27427c478bd9Sstevel@tonic-gate  *
27437c478bd9Sstevel@tonic-gate  * tinfo argument is for matching the get_addr() defined in
27447c478bd9Sstevel@tonic-gate  * ../nfs/mount/mount.c
27457c478bd9Sstevel@tonic-gate  */
27467c478bd9Sstevel@tonic-gate 
27477c478bd9Sstevel@tonic-gate static struct netbuf *
get_addr(char * hostname,rpcprog_t prog,rpcvers_t vers,struct netconfig ** nconfp,char * proto,ushort_t port,struct t_info * tinfo)27487c478bd9Sstevel@tonic-gate get_addr(char *hostname, rpcprog_t prog, rpcvers_t vers,
27494dc70696SToomas Soome     struct netconfig **nconfp, char *proto, ushort_t port,
27504dc70696SToomas Soome     struct t_info *tinfo)
27517c478bd9Sstevel@tonic-gate {
27527c478bd9Sstevel@tonic-gate 	enum clnt_stat cstat;
27537c478bd9Sstevel@tonic-gate 
27542f172c55SRobert Thurlow 	return (get_server_netinfo(SERVER_ADDR, hostname, prog, vers, NULL,
27554dc70696SToomas Soome 	    nconfp, proto, port, tinfo, NULL, FALSE, NULL, &cstat));
27567c478bd9Sstevel@tonic-gate }
27577c478bd9Sstevel@tonic-gate 
27587c478bd9Sstevel@tonic-gate static struct netbuf *
get_pubfh(char * hostname,rpcvers_t vers,mfs_snego_t * mfssnego,struct netconfig ** nconfp,char * proto,ushort_t port,struct t_info * tinfo,caddr_t * fhp,bool_t get_pubfh,char * fspath)27597c478bd9Sstevel@tonic-gate get_pubfh(char *hostname, rpcvers_t vers, mfs_snego_t *mfssnego,
27604dc70696SToomas Soome     struct netconfig **nconfp, char *proto, ushort_t port,
27614dc70696SToomas Soome     struct t_info *tinfo, caddr_t *fhp, bool_t get_pubfh, char *fspath)
27627c478bd9Sstevel@tonic-gate {
27637c478bd9Sstevel@tonic-gate 	enum clnt_stat cstat;
27647c478bd9Sstevel@tonic-gate 
27652f172c55SRobert Thurlow 	return (get_server_netinfo(SERVER_FH, hostname, NFS_PROGRAM, vers,
27661bbc88acSrm 	    mfssnego, nconfp, proto, port, tinfo, fhp, get_pubfh, fspath,
27671bbc88acSrm 	    &cstat));
27687c478bd9Sstevel@tonic-gate }
27697c478bd9Sstevel@tonic-gate 
27707c478bd9Sstevel@tonic-gate static enum clnt_stat
get_ping(char * hostname,rpcprog_t prog,rpcvers_t vers,struct netconfig ** nconfp,ushort_t port,bool_t direct_to_server)27717c478bd9Sstevel@tonic-gate get_ping(char *hostname, rpcprog_t prog, rpcvers_t vers,
27724dc70696SToomas Soome     struct netconfig **nconfp, ushort_t port, bool_t direct_to_server)
27737c478bd9Sstevel@tonic-gate {
27747c478bd9Sstevel@tonic-gate 	enum clnt_stat cstat;
27757c478bd9Sstevel@tonic-gate 
27762f172c55SRobert Thurlow 	(void) get_server_netinfo(SERVER_PING, hostname, prog,
27772f172c55SRobert Thurlow 	    vers, NULL, nconfp, NULL, port, NULL, NULL,
27782f172c55SRobert Thurlow 	    direct_to_server, NULL, &cstat);
27797c478bd9Sstevel@tonic-gate 
27807c478bd9Sstevel@tonic-gate 	return (cstat);
27817c478bd9Sstevel@tonic-gate }
27827c478bd9Sstevel@tonic-gate 
278339d3e169Sevanl void *
get_server_netinfo(enum type_of_stuff type_of_stuff,char * hostname,rpcprog_t prog,rpcvers_t vers,mfs_snego_t * mfssnego,struct netconfig ** nconfp,char * proto,ushort_t port,struct t_info * tinfo,caddr_t * fhp,bool_t direct_to_server,char * fspath,enum clnt_stat * cstatp)27842f172c55SRobert Thurlow get_server_netinfo(
27857c478bd9Sstevel@tonic-gate 	enum type_of_stuff type_of_stuff,
27867c478bd9Sstevel@tonic-gate 	char *hostname,
27877c478bd9Sstevel@tonic-gate 	rpcprog_t prog,
27887c478bd9Sstevel@tonic-gate 	rpcvers_t vers,
27897c478bd9Sstevel@tonic-gate 	mfs_snego_t *mfssnego,
27907c478bd9Sstevel@tonic-gate 	struct netconfig **nconfp,
27917c478bd9Sstevel@tonic-gate 	char *proto,
27927c478bd9Sstevel@tonic-gate 	ushort_t port,			/* may be zero */
27937c478bd9Sstevel@tonic-gate 	struct t_info *tinfo,
27947c478bd9Sstevel@tonic-gate 	caddr_t *fhp,
27957c478bd9Sstevel@tonic-gate 	bool_t direct_to_server,
27967c478bd9Sstevel@tonic-gate 	char *fspath,
27977c478bd9Sstevel@tonic-gate 	enum clnt_stat *cstatp)
27987c478bd9Sstevel@tonic-gate {
27997c478bd9Sstevel@tonic-gate 	struct netbuf *nb = NULL;
28007c478bd9Sstevel@tonic-gate 	struct netconfig *nconf = NULL;
28017c478bd9Sstevel@tonic-gate 	NCONF_HANDLE *nc = NULL;
28022f172c55SRobert Thurlow 	int error = 0;
28032f172c55SRobert Thurlow 	int fd = 0;
28042f172c55SRobert Thurlow 	struct t_bind *tbind = NULL;
28057c478bd9Sstevel@tonic-gate 	int nthtry = FIRST_TRY;
28067c478bd9Sstevel@tonic-gate 
28072f172c55SRobert Thurlow 	if (nconfp && *nconfp) {
28082f172c55SRobert Thurlow 		return (get_netconfig_info(type_of_stuff, hostname,
28092f172c55SRobert Thurlow 		    prog, vers, nconf, port, tinfo, tbind, fhp,
28102f172c55SRobert Thurlow 		    direct_to_server, fspath, cstatp, mfssnego));
28112f172c55SRobert Thurlow 	}
28127c478bd9Sstevel@tonic-gate 
28137c478bd9Sstevel@tonic-gate 	/*
28147c478bd9Sstevel@tonic-gate 	 * No nconf passed in.
28157c478bd9Sstevel@tonic-gate 	 *
28167c478bd9Sstevel@tonic-gate 	 * Try to get a nconf from /etc/netconfig.
28177c478bd9Sstevel@tonic-gate 	 * First choice is COTS, second is CLTS unless proto
28187c478bd9Sstevel@tonic-gate 	 * is specified.  When we retry, we reset the
28197c478bd9Sstevel@tonic-gate 	 * netconfig list, so that we search the whole list
28207c478bd9Sstevel@tonic-gate 	 * for the next choice.
28217c478bd9Sstevel@tonic-gate 	 */
28227c478bd9Sstevel@tonic-gate 	if ((nc = setnetpath()) == NULL)
28237c478bd9Sstevel@tonic-gate 		goto done;
28247c478bd9Sstevel@tonic-gate 
28257c478bd9Sstevel@tonic-gate 	/*
28267c478bd9Sstevel@tonic-gate 	 * If proto is specified, then only search for the match,
28277c478bd9Sstevel@tonic-gate 	 * otherwise try COTS first, if failed, then try CLTS.
28287c478bd9Sstevel@tonic-gate 	 */
28297c478bd9Sstevel@tonic-gate 	if (proto) {
28302f172c55SRobert Thurlow 		while ((nconf = getnetpath(nc)) != NULL) {
28317c478bd9Sstevel@tonic-gate 			if (strcmp(nconf->nc_proto, proto))
28327c478bd9Sstevel@tonic-gate 				continue;
28337c478bd9Sstevel@tonic-gate 			/*
28347c478bd9Sstevel@tonic-gate 			 * If the port number is specified then TCP/UDP
28357c478bd9Sstevel@tonic-gate 			 * is needed. Otherwise any cots/clts will do.
28367c478bd9Sstevel@tonic-gate 			 */
28377c478bd9Sstevel@tonic-gate 			if (port)  {
28387c478bd9Sstevel@tonic-gate 				if ((strcmp(nconf->nc_protofmly, NC_INET) &&
28397c478bd9Sstevel@tonic-gate 				    strcmp(nconf->nc_protofmly, NC_INET6)) ||
28407c478bd9Sstevel@tonic-gate 				    (strcmp(nconf->nc_proto, NC_TCP) &&
28417c478bd9Sstevel@tonic-gate 				    strcmp(nconf->nc_proto, NC_UDP)))
28427c478bd9Sstevel@tonic-gate 					continue;
28437c478bd9Sstevel@tonic-gate 			}
28442f172c55SRobert Thurlow 			nb = get_netconfig_info(type_of_stuff, hostname,
28452f172c55SRobert Thurlow 			    prog, vers, nconf, port, tinfo, tbind, fhp,
28462f172c55SRobert Thurlow 			    direct_to_server, fspath, cstatp, mfssnego);
28477c478bd9Sstevel@tonic-gate 			if (*cstatp == RPC_SUCCESS)
28487c478bd9Sstevel@tonic-gate 				break;
28497c478bd9Sstevel@tonic-gate 
28507c478bd9Sstevel@tonic-gate 			assert(nb == NULL);
28517c478bd9Sstevel@tonic-gate 
28522f172c55SRobert Thurlow 		}
28537c478bd9Sstevel@tonic-gate 		if (nconf == NULL)
28547c478bd9Sstevel@tonic-gate 			goto done;
28557c478bd9Sstevel@tonic-gate 	} else {
28567c478bd9Sstevel@tonic-gate retry:
28572f172c55SRobert Thurlow 		while ((nconf = getnetpath(nc)) != NULL) {
28587c478bd9Sstevel@tonic-gate 			if (nconf->nc_flag & NC_VISIBLE) {
28591bbc88acSrm 				if (nthtry == FIRST_TRY) {
28601bbc88acSrm 					if ((nconf->nc_semantics ==
28611bbc88acSrm 					    NC_TPI_COTS_ORD) ||
28621bbc88acSrm 					    (nconf->nc_semantics ==
28631bbc88acSrm 					    NC_TPI_COTS)) {
28641bbc88acSrm 						if (port == 0)
28651bbc88acSrm 							break;
28661bbc88acSrm 						if ((strcmp(nconf->nc_protofmly,
28671bbc88acSrm 						    NC_INET) == 0 ||
28681bbc88acSrm 						    strcmp(nconf->nc_protofmly,
28691bbc88acSrm 						    NC_INET6) == 0) &&
28701bbc88acSrm 						    (strcmp(nconf->nc_proto,
28711bbc88acSrm 						    NC_TCP) == 0))
28721bbc88acSrm 							break;
28731bbc88acSrm 					}
28747c478bd9Sstevel@tonic-gate 				}
28751bbc88acSrm 				if (nthtry == SECOND_TRY) {
28761bbc88acSrm 					if (nconf->nc_semantics ==
28771bbc88acSrm 					    NC_TPI_CLTS) {
28781bbc88acSrm 						if (port == 0)
28791bbc88acSrm 							break;
28801bbc88acSrm 						if ((strcmp(nconf->nc_protofmly,
28811bbc88acSrm 						    NC_INET) == 0 ||
28821bbc88acSrm 						    strcmp(nconf->nc_protofmly,
28831bbc88acSrm 						    NC_INET6) == 0) &&
28841bbc88acSrm 						    (strcmp(nconf->nc_proto,
28851bbc88acSrm 						    NC_UDP) == 0))
28861bbc88acSrm 							break;
28871bbc88acSrm 					}
28887c478bd9Sstevel@tonic-gate 				}
28897c478bd9Sstevel@tonic-gate 			}
28902f172c55SRobert Thurlow 		}
28912f172c55SRobert Thurlow 
28921bbc88acSrm 		if (nconf == NULL) {
28937c478bd9Sstevel@tonic-gate 			if (++nthtry <= MNT_PREF_LISTLEN) {
28947c478bd9Sstevel@tonic-gate 				endnetpath(nc);
28957c478bd9Sstevel@tonic-gate 				if ((nc = setnetpath()) == NULL)
28967c478bd9Sstevel@tonic-gate 					goto done;
28977c478bd9Sstevel@tonic-gate 				goto retry;
28987c478bd9Sstevel@tonic-gate 			} else
28997c478bd9Sstevel@tonic-gate 				goto done;
29001bbc88acSrm 		} else {
29012f172c55SRobert Thurlow 			nb = get_netconfig_info(type_of_stuff, hostname,
29022f172c55SRobert Thurlow 			    prog, vers, nconf, port, tinfo, tbind, fhp,
29032f172c55SRobert Thurlow 			    direct_to_server, fspath, cstatp, mfssnego);
29047c478bd9Sstevel@tonic-gate 			if (*cstatp != RPC_SUCCESS)
29057c478bd9Sstevel@tonic-gate 				/*
29067c478bd9Sstevel@tonic-gate 				 * Continue the same search path in the
29077c478bd9Sstevel@tonic-gate 				 * netconfig db until no more matched nconf
29087c478bd9Sstevel@tonic-gate 				 * (nconf == NULL).
29097c478bd9Sstevel@tonic-gate 				 */
29107c478bd9Sstevel@tonic-gate 				goto retry;
29111bbc88acSrm 		}
29122f172c55SRobert Thurlow 	}
29137c478bd9Sstevel@tonic-gate 
29147c478bd9Sstevel@tonic-gate 	/*
29157c478bd9Sstevel@tonic-gate 	 * Got nconf and nb.  Now dup the netconfig structure (nconf)
29167c478bd9Sstevel@tonic-gate 	 * and return it thru nconfp.
29177c478bd9Sstevel@tonic-gate 	 */
29182f172c55SRobert Thurlow 	if (nconf != NULL) {
29192f172c55SRobert Thurlow 		if ((*nconfp = getnetconfigent(nconf->nc_netid)) == NULL) {
29202f172c55SRobert Thurlow 			syslog(LOG_ERR, "no memory\n");
29212f172c55SRobert Thurlow 			free(nb);
29222f172c55SRobert Thurlow 			nb = NULL;
29232f172c55SRobert Thurlow 		}
29242f172c55SRobert Thurlow 	} else {
29252f172c55SRobert Thurlow 		*nconfp = NULL;
29267c478bd9Sstevel@tonic-gate 	}
29277c478bd9Sstevel@tonic-gate done:
29287c478bd9Sstevel@tonic-gate 	if (nc)
29297c478bd9Sstevel@tonic-gate 		endnetpath(nc);
29307c478bd9Sstevel@tonic-gate 	return (nb);
29317c478bd9Sstevel@tonic-gate }
29327c478bd9Sstevel@tonic-gate 
29332f172c55SRobert Thurlow void *
get_server_fh(char * hostname,rpcprog_t prog,rpcvers_t vers,mfs_snego_t * mfssnego,struct netconfig * nconf,ushort_t port,struct t_info * tinfo,struct t_bind * tbind,caddr_t * fhp,bool_t direct_to_server,char * fspath,enum clnt_stat * cstat)29342f172c55SRobert Thurlow get_server_fh(char *hostname, rpcprog_t	prog, rpcvers_t	vers,
29354dc70696SToomas Soome     mfs_snego_t *mfssnego, struct netconfig *nconf, ushort_t port,
29364dc70696SToomas Soome     struct t_info *tinfo, struct t_bind *tbind, caddr_t *fhp,
29374dc70696SToomas Soome     bool_t direct_to_server, char *fspath, enum clnt_stat *cstat)
29382f172c55SRobert Thurlow {
29392f172c55SRobert Thurlow 	AUTH *ah = NULL;
29402f172c55SRobert Thurlow 	AUTH *new_ah = NULL;
29412f172c55SRobert Thurlow 	struct snego_t	snego;
29422f172c55SRobert Thurlow 	enum clnt_stat cs = RPC_TIMEDOUT;
29432f172c55SRobert Thurlow 	struct timeval tv;
29442f172c55SRobert Thurlow 	bool_t file_handle = 1;
29452f172c55SRobert Thurlow 	enum snego_stat sec;
29462f172c55SRobert Thurlow 	CLIENT *cl = NULL;
29472f172c55SRobert Thurlow 	int fd = -1;
29482f172c55SRobert Thurlow 	struct netbuf *nb = NULL;
29492f172c55SRobert Thurlow 
29502f172c55SRobert Thurlow 	if (direct_to_server != TRUE)
29512f172c55SRobert Thurlow 		return (NULL);
29522f172c55SRobert Thurlow 
29532f172c55SRobert Thurlow 	if (prog == NFS_PROGRAM && vers == NFS_V4)
29542f172c55SRobert Thurlow 		if (strncasecmp(nconf->nc_proto, NC_UDP, strlen(NC_UDP)) == 0)
29552f172c55SRobert Thurlow 			goto done;
29562f172c55SRobert Thurlow 
29572f172c55SRobert Thurlow 	if ((fd = t_open(nconf->nc_device, O_RDWR, tinfo)) < 0)
29582f172c55SRobert Thurlow 		goto done;
29592f172c55SRobert Thurlow 
29602f172c55SRobert Thurlow 	/* LINTED pointer alignment */
29612f172c55SRobert Thurlow 	if ((tbind = (struct t_bind *)t_alloc(fd, T_BIND, T_ADDR)) == NULL)
29622f172c55SRobert Thurlow 		goto done;
29632f172c55SRobert Thurlow 
29642f172c55SRobert Thurlow 	if (setup_nb_parms(nconf, tbind, tinfo, hostname, fd,
29652f172c55SRobert Thurlow 	    direct_to_server, port, prog, vers, file_handle) < 0) {
29662f172c55SRobert Thurlow 		goto done;
29672f172c55SRobert Thurlow 	}
29682f172c55SRobert Thurlow 
29692f172c55SRobert Thurlow 	cl = clnt_tli_create(fd, nconf, &tbind->addr, prog, vers, 0, 0);
29702f172c55SRobert Thurlow 	if (cl == NULL)
29712f172c55SRobert Thurlow 		goto done;
29722f172c55SRobert Thurlow 
29732f172c55SRobert Thurlow 	ah = authsys_create_default();
29742f172c55SRobert Thurlow 	if (ah != NULL) {
29752f172c55SRobert Thurlow #ifdef MALLOC_DEBUG
29762f172c55SRobert Thurlow 		drop_alloc("AUTH_HANDLE", cl->cl_auth,
29772f172c55SRobert Thurlow 		    __FILE__, __LINE__);
29782f172c55SRobert Thurlow #endif
29792f172c55SRobert Thurlow 		AUTH_DESTROY(cl->cl_auth);
29802f172c55SRobert Thurlow 		cl->cl_auth = ah;
29812f172c55SRobert Thurlow #ifdef MALLOC_DEBUG
29822f172c55SRobert Thurlow 		add_alloc("AUTH_HANDLE", cl->cl_auth, 0,
29832f172c55SRobert Thurlow 		    __FILE__, __LINE__);
29842f172c55SRobert Thurlow #endif
29852f172c55SRobert Thurlow 	}
29862f172c55SRobert Thurlow 
29872f172c55SRobert Thurlow 	if (!mfssnego->snego_done && vers != NFS_V4) {
29882f172c55SRobert Thurlow 		/*
29892f172c55SRobert Thurlow 		 * negotiate sec flavor.
29902f172c55SRobert Thurlow 		 */
29912f172c55SRobert Thurlow 		snego.cnt = 0;
29922f172c55SRobert Thurlow 		if ((sec = nfs_sec_nego(vers, cl, fspath, &snego)) ==
29932f172c55SRobert Thurlow 		    SNEGO_SUCCESS) {
29942f172c55SRobert Thurlow 			int jj;
29952f172c55SRobert Thurlow 
29962f172c55SRobert Thurlow 			/*
29972f172c55SRobert Thurlow 			 * check if server supports the one
29982f172c55SRobert Thurlow 			 * specified in the sec= option.
29992f172c55SRobert Thurlow 			 */
30002f172c55SRobert Thurlow 			if (mfssnego->sec_opt) {
30012f172c55SRobert Thurlow 				for (jj = 0; jj < snego.cnt; jj++) {
30022f172c55SRobert Thurlow 					if (snego.array[jj] ==
30032f172c55SRobert Thurlow 					    mfssnego->nfs_sec.sc_nfsnum) {
30042f172c55SRobert Thurlow 						mfssnego->snego_done = TRUE;
30052f172c55SRobert Thurlow 						break;
30062f172c55SRobert Thurlow 					}
30072f172c55SRobert Thurlow 				}
30082f172c55SRobert Thurlow 			}
30092f172c55SRobert Thurlow 
30102f172c55SRobert Thurlow 			/*
30112f172c55SRobert Thurlow 			 * find a common sec flavor
30122f172c55SRobert Thurlow 			 */
30132f172c55SRobert Thurlow 			if (!mfssnego->snego_done) {
30142f172c55SRobert Thurlow 				for (jj = 0; jj < snego.cnt; jj++) {
30152f172c55SRobert Thurlow 					if (!nfs_getseconfig_bynumber(
30162f172c55SRobert Thurlow 					    snego.array[jj],
30172f172c55SRobert Thurlow 					    &mfssnego->nfs_sec)) {
30182f172c55SRobert Thurlow 						mfssnego->snego_done = TRUE;
30192f172c55SRobert Thurlow 						break;
30202f172c55SRobert Thurlow 					}
30212f172c55SRobert Thurlow 				}
30222f172c55SRobert Thurlow 			}
30232f172c55SRobert Thurlow 			if (!mfssnego->snego_done)
30242f172c55SRobert Thurlow 				goto done;
30252f172c55SRobert Thurlow 			/*
30262f172c55SRobert Thurlow 			 * Now that the flavor has been
30272f172c55SRobert Thurlow 			 * negotiated, get the fh.
30282f172c55SRobert Thurlow 			 *
30292f172c55SRobert Thurlow 			 * First, create an auth handle using the negotiated
30302f172c55SRobert Thurlow 			 * sec flavor in the next lookup to
30312f172c55SRobert Thurlow 			 * fetch the filehandle.
30322f172c55SRobert Thurlow 			 */
30332f172c55SRobert Thurlow 			new_ah = nfs_create_ah(cl, hostname,
30342f172c55SRobert Thurlow 			    &mfssnego->nfs_sec);
30352f172c55SRobert Thurlow 			if (new_ah  == NULL)
30362f172c55SRobert Thurlow 				goto done;
30372f172c55SRobert Thurlow #ifdef MALLOC_DEBUG
30382f172c55SRobert Thurlow 			drop_alloc("AUTH_HANDLE", cl->cl_auth,
30392f172c55SRobert Thurlow 			    __FILE__, __LINE__);
30402f172c55SRobert Thurlow #endif
30412f172c55SRobert Thurlow 			AUTH_DESTROY(cl->cl_auth);
30422f172c55SRobert Thurlow 			cl->cl_auth = new_ah;
30432f172c55SRobert Thurlow #ifdef MALLOC_DEBUG
30442f172c55SRobert Thurlow 			add_alloc("AUTH_HANDLE", cl->cl_auth, 0,
30452f172c55SRobert Thurlow 			    __FILE__, __LINE__);
30462f172c55SRobert Thurlow #endif
30472f172c55SRobert Thurlow 		} else if (sec == SNEGO_ARRAY_TOO_SMALL ||
30482f172c55SRobert Thurlow 		    sec == SNEGO_FAILURE) {
30492f172c55SRobert Thurlow 			goto done;
30502f172c55SRobert Thurlow 		}
30512f172c55SRobert Thurlow 	}
30522f172c55SRobert Thurlow 
30532f172c55SRobert Thurlow 	switch (vers) {
30542f172c55SRobert Thurlow 	case NFS_VERSION:
30552f172c55SRobert Thurlow 		{
30562f172c55SRobert Thurlow 		wnl_diropargs arg;
30572f172c55SRobert Thurlow 		wnl_diropres res;
30582f172c55SRobert Thurlow 
30592f172c55SRobert Thurlow 		memset((char *)&arg.dir, 0, sizeof (wnl_fh));
30602f172c55SRobert Thurlow 		memset((char *)&res, 0, sizeof (wnl_diropres));
30612f172c55SRobert Thurlow 		arg.name = fspath;
30622f172c55SRobert Thurlow 		if (wnlproc_lookup_2(&arg, &res, cl) !=
30632c2d21e9SRichard Lowe 		    RPC_SUCCESS || res.status != WNL_OK)
30642f172c55SRobert Thurlow 			goto done;
30652f172c55SRobert Thurlow 		*fhp = malloc(sizeof (wnl_fh));
30662f172c55SRobert Thurlow 
30672f172c55SRobert Thurlow 		if (*fhp == NULL) {
30682f172c55SRobert Thurlow 			syslog(LOG_ERR, "no memory\n");
30692f172c55SRobert Thurlow 			goto done;
30702f172c55SRobert Thurlow 		}
30712f172c55SRobert Thurlow 
30722f172c55SRobert Thurlow 		memcpy((char *)*fhp,
30732f172c55SRobert Thurlow 		    (char *)&res.wnl_diropres_u.wnl_diropres.file,
30742f172c55SRobert Thurlow 		    sizeof (wnl_fh));
30752f172c55SRobert Thurlow 		cs = RPC_SUCCESS;
30762f172c55SRobert Thurlow 		}
30772f172c55SRobert Thurlow 		break;
30782f172c55SRobert Thurlow 	case NFS_V3:
30792f172c55SRobert Thurlow 		{
30802f172c55SRobert Thurlow 		WNL_LOOKUP3args arg;
30812f172c55SRobert Thurlow 		WNL_LOOKUP3res res;
30822f172c55SRobert Thurlow 		nfs_fh3 *fh3p;
30832f172c55SRobert Thurlow 
30842f172c55SRobert Thurlow 		memset((char *)&arg.what.dir, 0, sizeof (wnl_fh3));
30852f172c55SRobert Thurlow 		memset((char *)&res, 0, sizeof (WNL_LOOKUP3res));
30862f172c55SRobert Thurlow 		arg.what.name = fspath;
30872f172c55SRobert Thurlow 		if (wnlproc3_lookup_3(&arg, &res, cl) !=
30882c2d21e9SRichard Lowe 		    RPC_SUCCESS || res.status != WNL3_OK)
30892f172c55SRobert Thurlow 			goto done;
30902f172c55SRobert Thurlow 
30912f172c55SRobert Thurlow 		fh3p = (nfs_fh3 *)malloc(sizeof (*fh3p));
30922f172c55SRobert Thurlow 
30932f172c55SRobert Thurlow 		if (fh3p == NULL) {
30942f172c55SRobert Thurlow 			syslog(LOG_ERR, "no memory\n");
30952f172c55SRobert Thurlow 			goto done;
30962f172c55SRobert Thurlow 		}
30972f172c55SRobert Thurlow 
30982f172c55SRobert Thurlow 		fh3p->fh3_length =
30992f172c55SRobert Thurlow 		    res.WNL_LOOKUP3res_u.res_ok.object.data.data_len;
31002f172c55SRobert Thurlow 		memcpy(fh3p->fh3_u.data,
31012f172c55SRobert Thurlow 		    res.WNL_LOOKUP3res_u.res_ok.object.data.data_val,
31022f172c55SRobert Thurlow 		    fh3p->fh3_length);
31032f172c55SRobert Thurlow 
31042f172c55SRobert Thurlow 		*fhp = (caddr_t)fh3p;
31052f172c55SRobert Thurlow 
31062f172c55SRobert Thurlow 		cs = RPC_SUCCESS;
31072f172c55SRobert Thurlow 		}
31082f172c55SRobert Thurlow 		break;
31092f172c55SRobert Thurlow 	case NFS_V4:
31102f172c55SRobert Thurlow 		tv.tv_sec = 10;
31112f172c55SRobert Thurlow 		tv.tv_usec = 0;
31122f172c55SRobert Thurlow 		cs = clnt_call(cl, NULLPROC, xdr_void, 0,
31132f172c55SRobert Thurlow 		    xdr_void, 0, tv);
31142f172c55SRobert Thurlow 		if (cs != RPC_SUCCESS)
31152f172c55SRobert Thurlow 			goto done;
31162f172c55SRobert Thurlow 
31172f172c55SRobert Thurlow 		*fhp = strdup(fspath);
31182f172c55SRobert Thurlow 		if (fhp == NULL) {
31192f172c55SRobert Thurlow 			cs = RPC_SYSTEMERROR;
31202f172c55SRobert Thurlow 			goto done;
31212f172c55SRobert Thurlow 		}
31222f172c55SRobert Thurlow 		break;
31232f172c55SRobert Thurlow 	}
31242f172c55SRobert Thurlow 	nb = (struct netbuf *)malloc(sizeof (struct netbuf));
31252f172c55SRobert Thurlow 	if (nb == NULL) {
31262f172c55SRobert Thurlow 		syslog(LOG_ERR, "no memory\n");
31272f172c55SRobert Thurlow 		cs = RPC_SYSTEMERROR;
31282f172c55SRobert Thurlow 		goto done;
31292f172c55SRobert Thurlow 	}
31302f172c55SRobert Thurlow 	nb->buf = (char *)malloc(tbind->addr.maxlen);
31312f172c55SRobert Thurlow 	if (nb->buf == NULL) {
31322f172c55SRobert Thurlow 		syslog(LOG_ERR, "no memory\n");
31332f172c55SRobert Thurlow 		free(nb);
31342f172c55SRobert Thurlow 		nb = NULL;
31352f172c55SRobert Thurlow 		cs = RPC_SYSTEMERROR;
31362f172c55SRobert Thurlow 		goto done;
31372f172c55SRobert Thurlow 	}
31382f172c55SRobert Thurlow 	(void) memcpy(nb->buf, tbind->addr.buf, tbind->addr.len);
31392f172c55SRobert Thurlow 	nb->len = tbind->addr.len;
31402f172c55SRobert Thurlow 	nb->maxlen = tbind->addr.maxlen;
31412f172c55SRobert Thurlow done:
31422f172c55SRobert Thurlow 	if (cstat != NULL)
31432f172c55SRobert Thurlow 		*cstat = cs;
31442f172c55SRobert Thurlow 	destroy_auth_client_handle(cl);
31452f172c55SRobert Thurlow 	cleanup_tli_parms(tbind, fd);
31462f172c55SRobert Thurlow 	return (nb);
31472f172c55SRobert Thurlow }
31487c478bd9Sstevel@tonic-gate 
31497c478bd9Sstevel@tonic-gate /*
31507c478bd9Sstevel@tonic-gate  * Sends a null call to the remote host's (NFS program, versp). versp
31517c478bd9Sstevel@tonic-gate  * may be "NULL" in which case the default maximum version is used.
31527c478bd9Sstevel@tonic-gate  * Upon return, versp contains the maximum version supported iff versp!= NULL.
31537c478bd9Sstevel@tonic-gate  */
31547c478bd9Sstevel@tonic-gate enum clnt_stat
pingnfs(char * hostpart,int attempts,rpcvers_t * versp,rpcvers_t versmin,ushort_t port,bool_t usepub,char * path,char * proto)31557c478bd9Sstevel@tonic-gate pingnfs(
31567c478bd9Sstevel@tonic-gate 	char *hostpart,
31577c478bd9Sstevel@tonic-gate 	int attempts,
31587c478bd9Sstevel@tonic-gate 	rpcvers_t *versp,
31597c478bd9Sstevel@tonic-gate 	rpcvers_t versmin,
31606a6d3e5eSjs 	ushort_t port,			/* may be zero */
31617c478bd9Sstevel@tonic-gate 	bool_t usepub,
31627c478bd9Sstevel@tonic-gate 	char *path,
31637c478bd9Sstevel@tonic-gate 	char *proto)
31647c478bd9Sstevel@tonic-gate {
31657c478bd9Sstevel@tonic-gate 	CLIENT *cl = NULL;
31667c478bd9Sstevel@tonic-gate 	struct timeval rpc_to_new = {15, 0};
31677c478bd9Sstevel@tonic-gate 	static struct timeval rpc_rtrans_new = {-1, -1};
31687c478bd9Sstevel@tonic-gate 	enum clnt_stat clnt_stat;
31697c478bd9Sstevel@tonic-gate 	int i, j;
31707c478bd9Sstevel@tonic-gate 	rpcvers_t versmax;	/* maximum version to try against server */
31717c478bd9Sstevel@tonic-gate 	rpcvers_t outvers;	/* version supported by host on last call */
31727c478bd9Sstevel@tonic-gate 	rpcvers_t vers_to_try;	/* to try different versions against host */
31736a6d3e5eSjs 	char *hostname;
31747c478bd9Sstevel@tonic-gate 	struct netconfig *nconf;
31757c478bd9Sstevel@tonic-gate 
31766a6d3e5eSjs 	hostname = strdup(hostpart);
31776a6d3e5eSjs 	if (hostname == NULL) {
31786a6d3e5eSjs 		return (RPC_SYSTEMERROR);
31796a6d3e5eSjs 	}
31806a6d3e5eSjs 	unbracket(&hostname);
31816a6d3e5eSjs 
31827c478bd9Sstevel@tonic-gate 	if (path != NULL && strcmp(hostname, "nfs") == 0 &&
31837c478bd9Sstevel@tonic-gate 	    strncmp(path, "//", 2) == 0) {
31847c478bd9Sstevel@tonic-gate 		char *sport;
31857c478bd9Sstevel@tonic-gate 
31867c478bd9Sstevel@tonic-gate 		hostname = strdup(path+2);
31877c478bd9Sstevel@tonic-gate 
31887c478bd9Sstevel@tonic-gate 		if (hostname == NULL)
31897c478bd9Sstevel@tonic-gate 			return (RPC_SYSTEMERROR);
31907c478bd9Sstevel@tonic-gate 
31917c478bd9Sstevel@tonic-gate 		path = strchr(hostname, '/');
31927c478bd9Sstevel@tonic-gate 
31937c478bd9Sstevel@tonic-gate 		/*
31947c478bd9Sstevel@tonic-gate 		 * This cannot happen. If it does, give up
31957c478bd9Sstevel@tonic-gate 		 * on the ping as this is obviously a corrupt
31967c478bd9Sstevel@tonic-gate 		 * entry.
31977c478bd9Sstevel@tonic-gate 		 */
31987c478bd9Sstevel@tonic-gate 		if (path == NULL) {
31997c478bd9Sstevel@tonic-gate 			free(hostname);
32007c478bd9Sstevel@tonic-gate 			return (RPC_SUCCESS);
32017c478bd9Sstevel@tonic-gate 		}
32027c478bd9Sstevel@tonic-gate 
32037c478bd9Sstevel@tonic-gate 		/*
32047c478bd9Sstevel@tonic-gate 		 * Probable end point of host string.
32057c478bd9Sstevel@tonic-gate 		 */
32067c478bd9Sstevel@tonic-gate 		*path = '\0';
32077c478bd9Sstevel@tonic-gate 
32087c478bd9Sstevel@tonic-gate 		sport = strchr(hostname, ':');
32097c478bd9Sstevel@tonic-gate 
32107c478bd9Sstevel@tonic-gate 		if (sport != NULL && sport < path) {
32117c478bd9Sstevel@tonic-gate 
32127c478bd9Sstevel@tonic-gate 			/*
32137c478bd9Sstevel@tonic-gate 			 * Actual end point of host string.
32147c478bd9Sstevel@tonic-gate 			 */
32157c478bd9Sstevel@tonic-gate 			*sport = '\0';
32167c478bd9Sstevel@tonic-gate 			port = htons((ushort_t)atoi(sport+1));
32177c478bd9Sstevel@tonic-gate 		}
32187c478bd9Sstevel@tonic-gate 
32197c478bd9Sstevel@tonic-gate 		usepub = TRUE;
32207c478bd9Sstevel@tonic-gate 	}
32217c478bd9Sstevel@tonic-gate 
32227c478bd9Sstevel@tonic-gate 	/* Pick up the default versions and then set them appropriately */
32237c478bd9Sstevel@tonic-gate 	if (versp) {
32247c478bd9Sstevel@tonic-gate 		versmax = *versp;
32257c478bd9Sstevel@tonic-gate 		/* use versmin passed in */
32267c478bd9Sstevel@tonic-gate 	} else {
32277c478bd9Sstevel@tonic-gate 		read_default_nfs();
32287c478bd9Sstevel@tonic-gate 		set_versrange(0, &versmax, &versmin);
32297c478bd9Sstevel@tonic-gate 	}
32307c478bd9Sstevel@tonic-gate 
32317c478bd9Sstevel@tonic-gate 	if (proto &&
32327c478bd9Sstevel@tonic-gate 	    strncasecmp(proto, NC_UDP, strlen(NC_UDP)) == 0 &&
32337c478bd9Sstevel@tonic-gate 	    versmax == NFS_V4) {
32347c478bd9Sstevel@tonic-gate 		if (versmin == NFS_V4) {
32357c478bd9Sstevel@tonic-gate 			if (versp) {
32367c478bd9Sstevel@tonic-gate 				*versp = versmax - 1;
32377c478bd9Sstevel@tonic-gate 				return (RPC_SUCCESS);
32387c478bd9Sstevel@tonic-gate 			}
32397c478bd9Sstevel@tonic-gate 			return (RPC_PROGUNAVAIL);
32407c478bd9Sstevel@tonic-gate 		} else {
32417c478bd9Sstevel@tonic-gate 			versmax--;
32427c478bd9Sstevel@tonic-gate 		}
32437c478bd9Sstevel@tonic-gate 	}
32447c478bd9Sstevel@tonic-gate 
32457c478bd9Sstevel@tonic-gate 	if (versp)
32467c478bd9Sstevel@tonic-gate 		*versp = versmax;
32477c478bd9Sstevel@tonic-gate 
32487c478bd9Sstevel@tonic-gate 	switch (cache_check(hostname, versp, proto)) {
32497c478bd9Sstevel@tonic-gate 	case GOODHOST:
32507c478bd9Sstevel@tonic-gate 		if (hostname != hostpart)
32517c478bd9Sstevel@tonic-gate 			free(hostname);
32527c478bd9Sstevel@tonic-gate 		return (RPC_SUCCESS);
32537c478bd9Sstevel@tonic-gate 	case DEADHOST:
32547c478bd9Sstevel@tonic-gate 		if (hostname != hostpart)
32557c478bd9Sstevel@tonic-gate 			free(hostname);
32567c478bd9Sstevel@tonic-gate 		return (RPC_TIMEDOUT);
32577c478bd9Sstevel@tonic-gate 	case NOHOST:
32587c478bd9Sstevel@tonic-gate 	default:
32597c478bd9Sstevel@tonic-gate 		break;
32607c478bd9Sstevel@tonic-gate 	}
32617c478bd9Sstevel@tonic-gate 
32627c478bd9Sstevel@tonic-gate 	/*
32637c478bd9Sstevel@tonic-gate 	 * XXX The retransmission time rpcbrmttime is a global defined
32647c478bd9Sstevel@tonic-gate 	 * in the rpc library (rpcb_clnt.c). We use (and like) the default
32657c478bd9Sstevel@tonic-gate 	 * value of 15 sec in the rpc library. The code below is to protect
32667c478bd9Sstevel@tonic-gate 	 * us in case it changes. This need not be done under a lock since
32677c478bd9Sstevel@tonic-gate 	 * any # of threads entering this function will get the same
32687c478bd9Sstevel@tonic-gate 	 * retransmission value.
32697c478bd9Sstevel@tonic-gate 	 */
32707c478bd9Sstevel@tonic-gate 	if (rpc_rtrans_new.tv_sec == -1 && rpc_rtrans_new.tv_usec == -1) {
32717c478bd9Sstevel@tonic-gate 		__rpc_control(CLCR_GET_RPCB_RMTTIME, (char *)&rpc_rtrans_new);
32727c478bd9Sstevel@tonic-gate 		if (rpc_rtrans_new.tv_sec != 15 && rpc_rtrans_new.tv_sec != 0)
32737c478bd9Sstevel@tonic-gate 			if (trace > 1)
32747c478bd9Sstevel@tonic-gate 				trace_prt(1, "RPC library rttimer changed\n");
32757c478bd9Sstevel@tonic-gate 	}
32767c478bd9Sstevel@tonic-gate 
32777c478bd9Sstevel@tonic-gate 	/*
32787c478bd9Sstevel@tonic-gate 	 * XXX Manipulate the total timeout to get the number of
32797c478bd9Sstevel@tonic-gate 	 * desired retransmissions. This code is heavily dependant on
32807c478bd9Sstevel@tonic-gate 	 * the RPC backoff mechanism in clnt_dg_call (clnt_dg.c).
32817c478bd9Sstevel@tonic-gate 	 */
32827c478bd9Sstevel@tonic-gate 	for (i = 0, j = rpc_rtrans_new.tv_sec; i < attempts-1; i++) {
32837c478bd9Sstevel@tonic-gate 		if (j < RPC_MAX_BACKOFF)
32847c478bd9Sstevel@tonic-gate 			j *= 2;
32857c478bd9Sstevel@tonic-gate 		else
32867c478bd9Sstevel@tonic-gate 			j = RPC_MAX_BACKOFF;
32877c478bd9Sstevel@tonic-gate 		rpc_to_new.tv_sec += j;
32887c478bd9Sstevel@tonic-gate 	}
32897c478bd9Sstevel@tonic-gate 
32907c478bd9Sstevel@tonic-gate 	vers_to_try = versmax;
32917c478bd9Sstevel@tonic-gate 
32927c478bd9Sstevel@tonic-gate 	/*
32937c478bd9Sstevel@tonic-gate 	 * check the host's version within the timeout
32947c478bd9Sstevel@tonic-gate 	 */
32957c478bd9Sstevel@tonic-gate 	if (trace > 1)
32967c478bd9Sstevel@tonic-gate 		trace_prt(1, "	ping: %s timeout=%ld request vers=%d min=%d\n",
32971bbc88acSrm 		    hostname, rpc_to_new.tv_sec, versmax, versmin);
32987c478bd9Sstevel@tonic-gate 
32997c478bd9Sstevel@tonic-gate 	if (usepub == FALSE) {
33001bbc88acSrm 		do {
33011bbc88acSrm 			/*
33021bbc88acSrm 			 * If NFSv4, then we do the same thing as is used
33031bbc88acSrm 			 * for public filehandles so that we avoid rpcbind
33041bbc88acSrm 			 */
33051bbc88acSrm 			if (vers_to_try == NFS_V4) {
33061bbc88acSrm 				if (trace > 4) {
33077c478bd9Sstevel@tonic-gate 				trace_prt(1, "  pingnfs: Trying ping via "
33081bbc88acSrm 				    "\"circuit_v\"\n");
33097c478bd9Sstevel@tonic-gate 				}
33107c478bd9Sstevel@tonic-gate 
33111bbc88acSrm 				cl = clnt_create_service_timed(hostname, "nfs",
33121bbc88acSrm 				    NFS_PROGRAM, vers_to_try,
33131bbc88acSrm 				    port, "circuit_v", &rpc_to_new);
33141bbc88acSrm 				if (cl != NULL) {
33151bbc88acSrm 					outvers = vers_to_try;
33161bbc88acSrm 					break;
33171bbc88acSrm 				}
33187c478bd9Sstevel@tonic-gate 				if (trace > 4) {
33191bbc88acSrm 					trace_prt(1,
33201bbc88acSrm 					    "  pingnfs: Can't ping via "
33211bbc88acSrm 					    "\"circuit_v\" %s: RPC error=%d\n",
33221bbc88acSrm 					    hostname, rpc_createerr.cf_stat);
33237c478bd9Sstevel@tonic-gate 				}
33241bbc88acSrm 
33251bbc88acSrm 			} else {
33261bbc88acSrm 				cl = clnt_create_vers_timed(hostname,
33271bbc88acSrm 				    NFS_PROGRAM, &outvers, versmin, vers_to_try,
33281bbc88acSrm 				    "datagram_v", &rpc_to_new);
33291bbc88acSrm 				if (cl != NULL)
33307c478bd9Sstevel@tonic-gate 					break;
33317c478bd9Sstevel@tonic-gate 				if (trace > 4) {
33321bbc88acSrm 					trace_prt(1,
33331bbc88acSrm 					    "  pingnfs: Can't ping via "
33341bbc88acSrm 					    "\"datagram_v\"%s: RPC error=%d\n",
33351bbc88acSrm 					    hostname, rpc_createerr.cf_stat);
33361bbc88acSrm 				}
33371bbc88acSrm 				if (rpc_createerr.cf_stat == RPC_UNKNOWNHOST ||
33381bbc88acSrm 				    rpc_createerr.cf_stat == RPC_TIMEDOUT)
33391bbc88acSrm 					break;
33401bbc88acSrm 				if (rpc_createerr.cf_stat ==
33411bbc88acSrm 				    RPC_PROGNOTREGISTERED) {
33421bbc88acSrm 					if (trace > 4) {
33431bbc88acSrm 						trace_prt(1,
33441bbc88acSrm 						    "  pingnfs: Trying ping "
33451bbc88acSrm 						    "via \"circuit_v\"\n");
33461bbc88acSrm 					}
33471bbc88acSrm 					cl = clnt_create_vers_timed(hostname,
33481bbc88acSrm 					    NFS_PROGRAM, &outvers,
33491bbc88acSrm 					    versmin, vers_to_try,
33501bbc88acSrm 					    "circuit_v", &rpc_to_new);
33511bbc88acSrm 					if (cl != NULL)
33521bbc88acSrm 						break;
33531bbc88acSrm 					if (trace > 4) {
33541bbc88acSrm 						trace_prt(1,
33551bbc88acSrm 						    "  pingnfs: Can't ping "
33561bbc88acSrm 						    "via \"circuit_v\" %s: "
33571bbc88acSrm 						    "RPC error=%d\n",
33581bbc88acSrm 						    hostname,
33591bbc88acSrm 						    rpc_createerr.cf_stat);
33601bbc88acSrm 					}
33617c478bd9Sstevel@tonic-gate 				}
33627c478bd9Sstevel@tonic-gate 			}
33637c478bd9Sstevel@tonic-gate 
33647c478bd9Sstevel@tonic-gate 		/*
33657c478bd9Sstevel@tonic-gate 		 * backoff and return lower version to retry the ping.
33667c478bd9Sstevel@tonic-gate 		 * XXX we should be more careful and handle
33677c478bd9Sstevel@tonic-gate 		 * RPC_PROGVERSMISMATCH here, because that error is handled
33687c478bd9Sstevel@tonic-gate 		 * in clnt_create_vers(). It's not done to stay in sync
33697c478bd9Sstevel@tonic-gate 		 * with the nfs mount command.
33707c478bd9Sstevel@tonic-gate 		 */
33711bbc88acSrm 			vers_to_try--;
33721bbc88acSrm 			if (vers_to_try < versmin)
33731bbc88acSrm 				break;
33741bbc88acSrm 			if (versp != NULL) {	/* recheck the cache */
33751bbc88acSrm 				*versp = vers_to_try;
33761bbc88acSrm 				if (trace > 4) {
33771bbc88acSrm 					trace_prt(1,
33781bbc88acSrm 					    "  pingnfs: check cache: vers=%d\n",
33791bbc88acSrm 					    *versp);
33801bbc88acSrm 				}
33811bbc88acSrm 				switch (cache_check(hostname, versp, proto)) {
33821bbc88acSrm 				case GOODHOST:
33831bbc88acSrm 					if (hostname != hostpart)
33841bbc88acSrm 						free(hostname);
33851bbc88acSrm 					return (RPC_SUCCESS);
33861bbc88acSrm 				case DEADHOST:
33871bbc88acSrm 					if (hostname != hostpart)
33881bbc88acSrm 						free(hostname);
33891bbc88acSrm 					return (RPC_TIMEDOUT);
33901bbc88acSrm 				case NOHOST:
33911bbc88acSrm 				default:
33921bbc88acSrm 					break;
33931bbc88acSrm 				}
33941bbc88acSrm 			}
33951bbc88acSrm 			if (trace > 4) {
33961bbc88acSrm 				trace_prt(1, "  pingnfs: Try version=%d\n",
33971bbc88acSrm 				    vers_to_try);
33981bbc88acSrm 			}
33991bbc88acSrm 		} while (cl == NULL);
34007c478bd9Sstevel@tonic-gate 
34017c478bd9Sstevel@tonic-gate 
34021bbc88acSrm 		if (cl == NULL) {
34031bbc88acSrm 			if (verbose)
34041bbc88acSrm 				syslog(LOG_ERR, "pingnfs: %s%s",
34057c478bd9Sstevel@tonic-gate 				    hostname, clnt_spcreateerror(""));
34061bbc88acSrm 			clnt_stat = rpc_createerr.cf_stat;
34071bbc88acSrm 		} else {
34081bbc88acSrm 			clnt_destroy(cl);
34091bbc88acSrm 			clnt_stat = RPC_SUCCESS;
34101bbc88acSrm 		}
34117c478bd9Sstevel@tonic-gate 
34127c478bd9Sstevel@tonic-gate 	} else {
34137c478bd9Sstevel@tonic-gate 		for (vers_to_try = versmax; vers_to_try >= versmin;
34147c478bd9Sstevel@tonic-gate 		    vers_to_try--) {
34157c478bd9Sstevel@tonic-gate 
34167c478bd9Sstevel@tonic-gate 			nconf = NULL;
34177c478bd9Sstevel@tonic-gate 
34187c478bd9Sstevel@tonic-gate 			if (trace > 4) {
34197c478bd9Sstevel@tonic-gate 				trace_prt(1, "  pingnfs: Try version=%d "
34201bbc88acSrm 				    "using get_ping()\n", vers_to_try);
34217c478bd9Sstevel@tonic-gate 			}
34227c478bd9Sstevel@tonic-gate 
34237c478bd9Sstevel@tonic-gate 			clnt_stat = get_ping(hostname, NFS_PROGRAM,
34241bbc88acSrm 			    vers_to_try, &nconf, port, TRUE);
34257c478bd9Sstevel@tonic-gate 
34267c478bd9Sstevel@tonic-gate 			if (nconf != NULL)
34277c478bd9Sstevel@tonic-gate 				freenetconfigent(nconf);
34287c478bd9Sstevel@tonic-gate 
34297c478bd9Sstevel@tonic-gate 			if (clnt_stat == RPC_SUCCESS) {
34307c478bd9Sstevel@tonic-gate 				outvers = vers_to_try;
34317c478bd9Sstevel@tonic-gate 				break;
34327c478bd9Sstevel@tonic-gate 			}
34337c478bd9Sstevel@tonic-gate 		}
34347c478bd9Sstevel@tonic-gate 	}
34357c478bd9Sstevel@tonic-gate 
34367c478bd9Sstevel@tonic-gate 	if (trace > 1)
34377c478bd9Sstevel@tonic-gate 		clnt_stat == RPC_SUCCESS ?
34381bbc88acSrm 		    trace_prt(1, "	pingnfs OK: nfs version=%d\n", outvers):
34391bbc88acSrm 		    trace_prt(1, "	pingnfs FAIL: can't get nfs version\n");
34407c478bd9Sstevel@tonic-gate 
34417c478bd9Sstevel@tonic-gate 	if (clnt_stat == RPC_SUCCESS) {
34427c478bd9Sstevel@tonic-gate 		cache_enter(hostname, versmax, outvers, proto, GOODHOST);
34437c478bd9Sstevel@tonic-gate 		if (versp != NULL)
34447c478bd9Sstevel@tonic-gate 			*versp = outvers;
34457c478bd9Sstevel@tonic-gate 	} else
34467c478bd9Sstevel@tonic-gate 		cache_enter(hostname, versmax, versmax, proto, DEADHOST);
34477c478bd9Sstevel@tonic-gate 
34487c478bd9Sstevel@tonic-gate 	if (hostpart != hostname)
34497c478bd9Sstevel@tonic-gate 		free(hostname);
34507c478bd9Sstevel@tonic-gate 
34517c478bd9Sstevel@tonic-gate 	return (clnt_stat);
34527c478bd9Sstevel@tonic-gate }
34537c478bd9Sstevel@tonic-gate 
34547c478bd9Sstevel@tonic-gate #define	MNTTYPE_LOFS	"lofs"
34557c478bd9Sstevel@tonic-gate 
34567c478bd9Sstevel@tonic-gate int
loopbackmount(char * fsname,char * dir,char * mntopts,int overlay)34574dc70696SToomas Soome loopbackmount(char *fsname, char *dir, char *mntopts, int overlay)
34587c478bd9Sstevel@tonic-gate {
34597c478bd9Sstevel@tonic-gate 	struct mnttab mnt;
34607c478bd9Sstevel@tonic-gate 	int flags = 0;
34617c478bd9Sstevel@tonic-gate 	char fstype[] = MNTTYPE_LOFS;
34627c478bd9Sstevel@tonic-gate 	int dirlen;
34637c478bd9Sstevel@tonic-gate 	struct stat st;
34647c478bd9Sstevel@tonic-gate 	char optbuf[MAX_MNTOPT_STR];
34657c478bd9Sstevel@tonic-gate 
34667c478bd9Sstevel@tonic-gate 	dirlen = strlen(dir);
34677c478bd9Sstevel@tonic-gate 	if (dir[dirlen-1] == ' ')
34687c478bd9Sstevel@tonic-gate 		dirlen--;
34697c478bd9Sstevel@tonic-gate 
34707c478bd9Sstevel@tonic-gate 	if (dirlen == strlen(fsname) &&
34714dc70696SToomas Soome 	    strncmp(fsname, dir, dirlen) == 0) {
34727c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR,
34734dc70696SToomas Soome 		    "Mount of %s on %s would result in deadlock, aborted\n",
34744dc70696SToomas Soome 		    fsname, dir);
34757c478bd9Sstevel@tonic-gate 		return (RET_ERR);
34767c478bd9Sstevel@tonic-gate 	}
34777c478bd9Sstevel@tonic-gate 	mnt.mnt_mntopts = mntopts;
34787c478bd9Sstevel@tonic-gate 	if (hasmntopt(&mnt, MNTOPT_RO) != NULL)
34797c478bd9Sstevel@tonic-gate 		flags |= MS_RDONLY;
34807c478bd9Sstevel@tonic-gate 
34817c478bd9Sstevel@tonic-gate 	(void) strlcpy(optbuf, mntopts, sizeof (optbuf));
34827c478bd9Sstevel@tonic-gate 
34837c478bd9Sstevel@tonic-gate 	if (overlay)
34847c478bd9Sstevel@tonic-gate 		flags |= MS_OVERLAY;
34857c478bd9Sstevel@tonic-gate 
34864dc70696SToomas Soome 	if (trace > 1) {
34874dc70696SToomas Soome 		trace_prt(1, "  loopbackmount: fsname=%s, dir=%s, flags=%d\n",
34884dc70696SToomas Soome 		    fsname, dir, flags);
34894dc70696SToomas Soome 	}
34907c478bd9Sstevel@tonic-gate 
349145916cd2Sjpk 	if (is_system_labeled()) {
349245916cd2Sjpk 		if (create_homedir((const char *)fsname,
349345916cd2Sjpk 		    (const char *)dir) == 0) {
349445916cd2Sjpk 			return (NFSERR_NOENT);
349545916cd2Sjpk 		}
349645916cd2Sjpk 	}
349745916cd2Sjpk 
34987c478bd9Sstevel@tonic-gate 	if (mount(fsname, dir, flags | MS_DATA | MS_OPTIONSTR, fstype,
34997c478bd9Sstevel@tonic-gate 	    NULL, 0, optbuf, sizeof (optbuf)) < 0) {
35007c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "Mount of %s on %s: %m", fsname, dir);
35017c478bd9Sstevel@tonic-gate 		return (RET_ERR);
35027c478bd9Sstevel@tonic-gate 	}
35037c478bd9Sstevel@tonic-gate 
35047c478bd9Sstevel@tonic-gate 	if (stat(dir, &st) == 0) {
35057c478bd9Sstevel@tonic-gate 		if (trace > 1) {
35067c478bd9Sstevel@tonic-gate 			trace_prt(1,
35077c478bd9Sstevel@tonic-gate 			    "  loopbackmount of %s on %s dev=%x rdev=%x OK\n",
35087c478bd9Sstevel@tonic-gate 			    fsname, dir, st.st_dev, st.st_rdev);
35097c478bd9Sstevel@tonic-gate 		}
35107c478bd9Sstevel@tonic-gate 	} else {
35117c478bd9Sstevel@tonic-gate 		if (trace > 1) {
35127c478bd9Sstevel@tonic-gate 			trace_prt(1,
35137c478bd9Sstevel@tonic-gate 			    "  loopbackmount of %s on %s OK\n", fsname, dir);
35147c478bd9Sstevel@tonic-gate 			trace_prt(1, "	stat of %s failed\n", dir);
35157c478bd9Sstevel@tonic-gate 		}
35167c478bd9Sstevel@tonic-gate 	}
35177c478bd9Sstevel@tonic-gate 
35187c478bd9Sstevel@tonic-gate 	return (0);
35197c478bd9Sstevel@tonic-gate }
35207c478bd9Sstevel@tonic-gate 
35217c478bd9Sstevel@tonic-gate /*
35227c478bd9Sstevel@tonic-gate  * Look for the value of a numeric option of the form foo=x.  If found, set
35237c478bd9Sstevel@tonic-gate  * *valp to the value and return non-zero.  If not found or the option is
35247c478bd9Sstevel@tonic-gate  * malformed, return zero.
35257c478bd9Sstevel@tonic-gate  */
35267c478bd9Sstevel@tonic-gate 
35277c478bd9Sstevel@tonic-gate int
nopt(struct mnttab * mnt,char * opt,int * valp)35284dc70696SToomas Soome nopt(struct mnttab *mnt, char *opt, int *valp)
35297c478bd9Sstevel@tonic-gate {
35307c478bd9Sstevel@tonic-gate 	char *equal;
35317c478bd9Sstevel@tonic-gate 	char *str;
35327c478bd9Sstevel@tonic-gate 
35337c478bd9Sstevel@tonic-gate 	/*
35347c478bd9Sstevel@tonic-gate 	 * We should never get a null pointer, but if we do, it's better to
35357c478bd9Sstevel@tonic-gate 	 * ignore the option than to dump core.
35367c478bd9Sstevel@tonic-gate 	 */
35377c478bd9Sstevel@tonic-gate 
35387c478bd9Sstevel@tonic-gate 	if (valp == NULL) {
35397c478bd9Sstevel@tonic-gate 		syslog(LOG_DEBUG, "null pointer for %s option", opt);
35407c478bd9Sstevel@tonic-gate 		return (0);
35417c478bd9Sstevel@tonic-gate 	}
35427c478bd9Sstevel@tonic-gate 
35437c478bd9Sstevel@tonic-gate 	if (str = hasmntopt(mnt, opt)) {
35447c478bd9Sstevel@tonic-gate 		if (equal = strchr(str, '=')) {
35457c478bd9Sstevel@tonic-gate 			*valp = atoi(&equal[1]);
35467c478bd9Sstevel@tonic-gate 			return (1);
35477c478bd9Sstevel@tonic-gate 		} else {
35487c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "Bad numeric option '%s'", str);
35497c478bd9Sstevel@tonic-gate 		}
35507c478bd9Sstevel@tonic-gate 	}
35517c478bd9Sstevel@tonic-gate 	return (0);
35527c478bd9Sstevel@tonic-gate }
35537c478bd9Sstevel@tonic-gate 
355411606941Sjwahlig int
nfsunmount(struct mnttab * mnt)35554dc70696SToomas Soome nfsunmount(struct mnttab *mnt)
35567c478bd9Sstevel@tonic-gate {
35577c478bd9Sstevel@tonic-gate 	struct timeval timeout;
35587c478bd9Sstevel@tonic-gate 	CLIENT *cl;
35597c478bd9Sstevel@tonic-gate 	enum clnt_stat rpc_stat;
35607c478bd9Sstevel@tonic-gate 	char *host, *path;
35617c478bd9Sstevel@tonic-gate 	struct replica *list;
35627c478bd9Sstevel@tonic-gate 	int i, count = 0;
35637c478bd9Sstevel@tonic-gate 	int isv4mount = is_v4_mount(mnt->mnt_mountp);
35647c478bd9Sstevel@tonic-gate 
35657c478bd9Sstevel@tonic-gate 	if (trace > 1)
35667c478bd9Sstevel@tonic-gate 		trace_prt(1, "	nfsunmount: umount %s\n", mnt->mnt_mountp);
35677c478bd9Sstevel@tonic-gate 
35687c478bd9Sstevel@tonic-gate 	if (umount(mnt->mnt_mountp) < 0) {
35694dc70696SToomas Soome 		if (trace > 1) {
35707c478bd9Sstevel@tonic-gate 			trace_prt(1, "	nfsunmount: umount %s FAILED\n",
35714dc70696SToomas Soome 			    mnt->mnt_mountp);
35724dc70696SToomas Soome 		}
35737c478bd9Sstevel@tonic-gate 		if (errno)
35747c478bd9Sstevel@tonic-gate 			return (errno);
35757c478bd9Sstevel@tonic-gate 	}
35767c478bd9Sstevel@tonic-gate 
35777c478bd9Sstevel@tonic-gate 	/*
35787c478bd9Sstevel@tonic-gate 	 * If this is a NFSv4 mount, the mount protocol was not used
35797c478bd9Sstevel@tonic-gate 	 * so we just return.
35807c478bd9Sstevel@tonic-gate 	 */
35817c478bd9Sstevel@tonic-gate 	if (isv4mount) {
35824dc70696SToomas Soome 		if (trace > 1) {
35837c478bd9Sstevel@tonic-gate 			trace_prt(1, "	nfsunmount: umount %s OK\n",
35844dc70696SToomas Soome 			    mnt->mnt_mountp);
35854dc70696SToomas Soome 		}
35867c478bd9Sstevel@tonic-gate 		return (0);
35877c478bd9Sstevel@tonic-gate 	}
35887c478bd9Sstevel@tonic-gate 
35897c478bd9Sstevel@tonic-gate 	/*
35907c478bd9Sstevel@tonic-gate 	 * If mounted with -o public, then no need to contact server
35917c478bd9Sstevel@tonic-gate 	 * because mount protocol was not used.
35927c478bd9Sstevel@tonic-gate 	 */
35937c478bd9Sstevel@tonic-gate 	if (hasmntopt(mnt, MNTOPT_PUBLIC) != NULL) {
35947c478bd9Sstevel@tonic-gate 		return (0);
35957c478bd9Sstevel@tonic-gate 	}
35967c478bd9Sstevel@tonic-gate 
35977c478bd9Sstevel@tonic-gate 	/*
35987c478bd9Sstevel@tonic-gate 	 * The rest of this code is advisory to the server.
35997c478bd9Sstevel@tonic-gate 	 * If it fails return success anyway.
36007c478bd9Sstevel@tonic-gate 	 */
36017c478bd9Sstevel@tonic-gate 
36027c478bd9Sstevel@tonic-gate 	list = parse_replica(mnt->mnt_special, &count);
36037c478bd9Sstevel@tonic-gate 	if (!list) {
36047c478bd9Sstevel@tonic-gate 		if (count >= 0)
36054dc70696SToomas Soome 			syslog(LOG_ERR, "Memory allocation failed: %m");
36067c478bd9Sstevel@tonic-gate 		return (ENOMEM);
36077c478bd9Sstevel@tonic-gate 	}
36087c478bd9Sstevel@tonic-gate 
36097c478bd9Sstevel@tonic-gate 	for (i = 0; i < count; i++) {
36107c478bd9Sstevel@tonic-gate 
36117c478bd9Sstevel@tonic-gate 		host = list[i].host;
36127c478bd9Sstevel@tonic-gate 		path = list[i].path;
36137c478bd9Sstevel@tonic-gate 
36147c478bd9Sstevel@tonic-gate 		/*
36157c478bd9Sstevel@tonic-gate 		 * Skip file systems mounted using WebNFS, because mount
36167c478bd9Sstevel@tonic-gate 		 * protocol was not used.
36177c478bd9Sstevel@tonic-gate 		 */
36187c478bd9Sstevel@tonic-gate 		if (strcmp(host, "nfs") == 0 && strncmp(path, "//", 2) == 0)
36197c478bd9Sstevel@tonic-gate 			continue;
36207c478bd9Sstevel@tonic-gate 
36217c478bd9Sstevel@tonic-gate 		cl = clnt_create(host, MOUNTPROG, MOUNTVERS, "datagram_v");
36227c478bd9Sstevel@tonic-gate 		if (cl == NULL)
36237c478bd9Sstevel@tonic-gate 			break;
36247c478bd9Sstevel@tonic-gate #ifdef MALLOC_DEBUG
36257c478bd9Sstevel@tonic-gate 		add_alloc("CLNT_HANDLE", cl, 0, __FILE__, __LINE__);
36264dc70696SToomas Soome 		add_alloc("AUTH_HANDLE", cl->cl_auth, 0, __FILE__, __LINE__);
36277c478bd9Sstevel@tonic-gate #endif
36287c478bd9Sstevel@tonic-gate 		if (__clnt_bindresvport(cl) < 0) {
36294dc70696SToomas Soome 			if (verbose) {
36304dc70696SToomas Soome 				syslog(LOG_ERR, "umount %s:%s: %s", host, path,
36314dc70696SToomas Soome 				    "Couldn't bind to reserved port");
36324dc70696SToomas Soome 			}
36337c478bd9Sstevel@tonic-gate 			destroy_auth_client_handle(cl);
36347c478bd9Sstevel@tonic-gate 			continue;
36357c478bd9Sstevel@tonic-gate 		}
36367c478bd9Sstevel@tonic-gate #ifdef MALLOC_DEBUG
36377c478bd9Sstevel@tonic-gate 		drop_alloc("AUTH_HANDLE", cl->cl_auth, __FILE__, __LINE__);
36387c478bd9Sstevel@tonic-gate #endif
36397c478bd9Sstevel@tonic-gate 		AUTH_DESTROY(cl->cl_auth);
36407c478bd9Sstevel@tonic-gate 		if ((cl->cl_auth = authsys_create_default()) == NULL) {
36414dc70696SToomas Soome 			if (verbose) {
36424dc70696SToomas Soome 				syslog(LOG_ERR, "umount %s:%s: %s", host, path,
36434dc70696SToomas Soome 				    "Failed creating default auth handle");
36444dc70696SToomas Soome 			}
36457c478bd9Sstevel@tonic-gate 			destroy_auth_client_handle(cl);
36467c478bd9Sstevel@tonic-gate 			continue;
36477c478bd9Sstevel@tonic-gate 		}
36487c478bd9Sstevel@tonic-gate #ifdef MALLOC_DEBUG
36497c478bd9Sstevel@tonic-gate 		add_alloc("AUTH_HANDLE", cl->cl_auth, 0, __FILE__, __LINE__);
36507c478bd9Sstevel@tonic-gate #endif
36517c478bd9Sstevel@tonic-gate 		timeout.tv_usec = 0;
36527c478bd9Sstevel@tonic-gate 		timeout.tv_sec = 5;
36537c478bd9Sstevel@tonic-gate 		rpc_stat = clnt_call(cl, MOUNTPROC_UMNT, xdr_dirpath,
36544dc70696SToomas Soome 		    (caddr_t)&path, xdr_void, (char *)NULL, timeout);
36554dc70696SToomas Soome 		if (verbose && rpc_stat != RPC_SUCCESS) {
36564dc70696SToomas Soome 			syslog(LOG_ERR, "%s: %s", host,
36574dc70696SToomas Soome 			    clnt_sperror(cl, "unmount"));
36584dc70696SToomas Soome 		}
36597c478bd9Sstevel@tonic-gate 		destroy_auth_client_handle(cl);
36607c478bd9Sstevel@tonic-gate 	}
36617c478bd9Sstevel@tonic-gate 
36627c478bd9Sstevel@tonic-gate 	free_replica(list, count);
36637c478bd9Sstevel@tonic-gate 
36647c478bd9Sstevel@tonic-gate 	if (trace > 1)
36657c478bd9Sstevel@tonic-gate 		trace_prt(1, "	nfsunmount: umount %s OK\n", mnt->mnt_mountp);
36667c478bd9Sstevel@tonic-gate 
36677c478bd9Sstevel@tonic-gate done:
36687c478bd9Sstevel@tonic-gate 	return (0);
36697c478bd9Sstevel@tonic-gate }
36707c478bd9Sstevel@tonic-gate 
36717c478bd9Sstevel@tonic-gate /*
36727c478bd9Sstevel@tonic-gate  * Put a new entry in the cache chain by prepending it to the front.
36737c478bd9Sstevel@tonic-gate  * If there isn't enough memory then just give up.
36747c478bd9Sstevel@tonic-gate  */
36757c478bd9Sstevel@tonic-gate static void
cache_enter(char * host,rpcvers_t reqvers,rpcvers_t outvers,char * proto,int state)36764dc70696SToomas Soome cache_enter(char *host, rpcvers_t reqvers, rpcvers_t outvers, char *proto,
36774dc70696SToomas Soome     int state)
36787c478bd9Sstevel@tonic-gate {
36797c478bd9Sstevel@tonic-gate 	struct cache_entry *entry;
36807c478bd9Sstevel@tonic-gate 	int cache_time = 30;	/* sec */
36817c478bd9Sstevel@tonic-gate 
36827c478bd9Sstevel@tonic-gate 	timenow = time(NULL);
36837c478bd9Sstevel@tonic-gate 
36847c478bd9Sstevel@tonic-gate 	entry = (struct cache_entry *)malloc(sizeof (struct cache_entry));
36857c478bd9Sstevel@tonic-gate 	if (entry == NULL)
36867c478bd9Sstevel@tonic-gate 		return;
36877c478bd9Sstevel@tonic-gate 	(void) memset((caddr_t)entry, 0, sizeof (struct cache_entry));
36887c478bd9Sstevel@tonic-gate 	entry->cache_host = strdup(host);
36897c478bd9Sstevel@tonic-gate 	if (entry->cache_host == NULL) {
36907c478bd9Sstevel@tonic-gate 		cache_free(entry);
36917c478bd9Sstevel@tonic-gate 		return;
36927c478bd9Sstevel@tonic-gate 	}
36937c478bd9Sstevel@tonic-gate 	entry->cache_reqvers = reqvers;
36947c478bd9Sstevel@tonic-gate 	entry->cache_outvers = outvers;
36957c478bd9Sstevel@tonic-gate 	entry->cache_proto = (proto == NULL ? NULL : strdup(proto));
36967c478bd9Sstevel@tonic-gate 	entry->cache_state = state;
36977c478bd9Sstevel@tonic-gate 	entry->cache_time = timenow + cache_time;
36987c478bd9Sstevel@tonic-gate 	(void) rw_wrlock(&cache_lock);
36997c478bd9Sstevel@tonic-gate #ifdef CACHE_DEBUG
37007c478bd9Sstevel@tonic-gate 	host_cache_accesses++;		/* up host cache access counter */
37017c478bd9Sstevel@tonic-gate #endif /* CACHE DEBUG */
37027c478bd9Sstevel@tonic-gate 	entry->cache_next = cache_head;
37037c478bd9Sstevel@tonic-gate 	cache_head = entry;
37047c478bd9Sstevel@tonic-gate 	(void) rw_unlock(&cache_lock);
37057c478bd9Sstevel@tonic-gate }
37067c478bd9Sstevel@tonic-gate 
37077c478bd9Sstevel@tonic-gate static int
cache_check(char * host,rpcvers_t * versp,char * proto)37084dc70696SToomas Soome cache_check(char *host, rpcvers_t *versp, char *proto)
37097c478bd9Sstevel@tonic-gate {
37107c478bd9Sstevel@tonic-gate 	int state = NOHOST;
37117c478bd9Sstevel@tonic-gate 	struct cache_entry *ce, *prev;
37127c478bd9Sstevel@tonic-gate 
37137c478bd9Sstevel@tonic-gate 	timenow = time(NULL);
37147c478bd9Sstevel@tonic-gate 
37157c478bd9Sstevel@tonic-gate 	(void) rw_rdlock(&cache_lock);
37167c478bd9Sstevel@tonic-gate 
37177c478bd9Sstevel@tonic-gate #ifdef CACHE_DEBUG
37187c478bd9Sstevel@tonic-gate 	/* Increment the lookup and access counters for the host cache */
37197c478bd9Sstevel@tonic-gate 	host_cache_accesses++;
37207c478bd9Sstevel@tonic-gate 	host_cache_lookups++;
37217c478bd9Sstevel@tonic-gate 	if ((host_cache_lookups%1000) == 0)
37227c478bd9Sstevel@tonic-gate 		trace_host_cache();
37237c478bd9Sstevel@tonic-gate #endif /* CACHE DEBUG */
37247c478bd9Sstevel@tonic-gate 
37257c478bd9Sstevel@tonic-gate 	for (ce = cache_head; ce; ce = ce->cache_next) {
37267c478bd9Sstevel@tonic-gate 		if (timenow > ce->cache_time) {
37277c478bd9Sstevel@tonic-gate 			(void) rw_unlock(&cache_lock);
37287c478bd9Sstevel@tonic-gate 			(void) rw_wrlock(&cache_lock);
37297c478bd9Sstevel@tonic-gate 			for (prev = NULL, ce = cache_head; ce;
37304dc70696SToomas Soome 			    prev = ce, ce = ce->cache_next) {
37317c478bd9Sstevel@tonic-gate 				if (timenow > ce->cache_time) {
37327c478bd9Sstevel@tonic-gate 					cache_free(ce);
37337c478bd9Sstevel@tonic-gate 					if (prev)
37347c478bd9Sstevel@tonic-gate 						prev->cache_next = NULL;
37357c478bd9Sstevel@tonic-gate 					else
37367c478bd9Sstevel@tonic-gate 						cache_head = NULL;
37377c478bd9Sstevel@tonic-gate 					break;
37387c478bd9Sstevel@tonic-gate 				}
37397c478bd9Sstevel@tonic-gate 			}
37407c478bd9Sstevel@tonic-gate 			(void) rw_unlock(&cache_lock);
37417c478bd9Sstevel@tonic-gate 			return (state);
37427c478bd9Sstevel@tonic-gate 		}
37437c478bd9Sstevel@tonic-gate 		if (strcmp(host, ce->cache_host) != 0)
37447c478bd9Sstevel@tonic-gate 			continue;
37457c478bd9Sstevel@tonic-gate 		if ((proto == NULL && ce->cache_proto != NULL) ||
37467c478bd9Sstevel@tonic-gate 		    (proto != NULL && ce->cache_proto == NULL))
37477c478bd9Sstevel@tonic-gate 			continue;
37487c478bd9Sstevel@tonic-gate 		if (proto != NULL &&
37497c478bd9Sstevel@tonic-gate 		    strcmp(proto, ce->cache_proto) != 0)
37507c478bd9Sstevel@tonic-gate 			continue;
37517c478bd9Sstevel@tonic-gate 
37527c478bd9Sstevel@tonic-gate 		if (versp == NULL ||
37534dc70696SToomas Soome 		    (versp != NULL && *versp == ce->cache_reqvers) ||
37544dc70696SToomas Soome 		    (versp != NULL && *versp == ce->cache_outvers)) {
37554dc70696SToomas Soome 			if (versp != NULL)
37564dc70696SToomas Soome 				*versp = ce->cache_outvers;
37574dc70696SToomas Soome 			state = ce->cache_state;
37587c478bd9Sstevel@tonic-gate 
37594dc70696SToomas Soome 			/* increment the host cache hit counters */
37607c478bd9Sstevel@tonic-gate #ifdef CACHE_DEBUG
37614dc70696SToomas Soome 			if (state == GOODHOST)
37624dc70696SToomas Soome 				goodhost_cache_hits++;
37634dc70696SToomas Soome 			if (state == DEADHOST)
37644dc70696SToomas Soome 				deadhost_cache_hits++;
37657c478bd9Sstevel@tonic-gate #endif /* CACHE_DEBUG */
37664dc70696SToomas Soome 			(void) rw_unlock(&cache_lock);
37674dc70696SToomas Soome 			return (state);
37687c478bd9Sstevel@tonic-gate 		}
37697c478bd9Sstevel@tonic-gate 	}
37707c478bd9Sstevel@tonic-gate 	(void) rw_unlock(&cache_lock);
37717c478bd9Sstevel@tonic-gate 	return (state);
37727c478bd9Sstevel@tonic-gate }
37737c478bd9Sstevel@tonic-gate 
37747c478bd9Sstevel@tonic-gate /*
37757c478bd9Sstevel@tonic-gate  * Free a cache entry and all entries
37767c478bd9Sstevel@tonic-gate  * further down the chain since they
37777c478bd9Sstevel@tonic-gate  * will also be expired.
37787c478bd9Sstevel@tonic-gate  */
37797c478bd9Sstevel@tonic-gate static void
cache_free(struct cache_entry * entry)37804dc70696SToomas Soome cache_free(struct cache_entry *entry)
37817c478bd9Sstevel@tonic-gate {
37827c478bd9Sstevel@tonic-gate 	struct cache_entry *ce, *next = NULL;
37837c478bd9Sstevel@tonic-gate 
37847c478bd9Sstevel@tonic-gate 	for (ce = entry; ce; ce = next) {
37857c478bd9Sstevel@tonic-gate 		if (ce->cache_host)
37867c478bd9Sstevel@tonic-gate 			free(ce->cache_host);
37877c478bd9Sstevel@tonic-gate 		if (ce->cache_proto)
37887c478bd9Sstevel@tonic-gate 			free(ce->cache_proto);
37897c478bd9Sstevel@tonic-gate 		next = ce->cache_next;
37907c478bd9Sstevel@tonic-gate 		free(ce);
37917c478bd9Sstevel@tonic-gate 	}
37927c478bd9Sstevel@tonic-gate }
37937c478bd9Sstevel@tonic-gate 
37947c478bd9Sstevel@tonic-gate #ifdef MALLOC_DEBUG
37957c478bd9Sstevel@tonic-gate void
cache_flush(void)37964dc70696SToomas Soome cache_flush(void)
37977c478bd9Sstevel@tonic-gate {
37987c478bd9Sstevel@tonic-gate 	(void) rw_wrlock(&cache_lock);
37997c478bd9Sstevel@tonic-gate 	cache_free(cache_head);
38007c478bd9Sstevel@tonic-gate 	cache_head = NULL;
38017c478bd9Sstevel@tonic-gate 	(void) rw_unlock(&cache_lock);
38027c478bd9Sstevel@tonic-gate }
38037c478bd9Sstevel@tonic-gate 
38047c478bd9Sstevel@tonic-gate void
flush_caches(void)38054dc70696SToomas Soome flush_caches(void)
38067c478bd9Sstevel@tonic-gate {
38077c478bd9Sstevel@tonic-gate 	mutex_lock(&cleanup_lock);
38087c478bd9Sstevel@tonic-gate 	cond_signal(&cleanup_start_cv);
38097c478bd9Sstevel@tonic-gate 	(void) cond_wait(&cleanup_done_cv, &cleanup_lock);
38107c478bd9Sstevel@tonic-gate 	mutex_unlock(&cleanup_lock);
38117c478bd9Sstevel@tonic-gate 	cache_flush();
38127c478bd9Sstevel@tonic-gate 	portmap_cache_flush();
38137c478bd9Sstevel@tonic-gate }
38147c478bd9Sstevel@tonic-gate #endif
38157c478bd9Sstevel@tonic-gate 
38167c478bd9Sstevel@tonic-gate /*
38177c478bd9Sstevel@tonic-gate  * Returns 1, if port option is NFS_PORT or
38187c478bd9Sstevel@tonic-gate  *	nfsd is running on the port given
38197c478bd9Sstevel@tonic-gate  * Returns 0, if both port is not NFS_PORT and nfsd is not
38207c478bd9Sstevel@tonic-gate  *	running on the port.
38217c478bd9Sstevel@tonic-gate  */
38227c478bd9Sstevel@tonic-gate 
38237c478bd9Sstevel@tonic-gate static int
is_nfs_port(char * opts)38247c478bd9Sstevel@tonic-gate is_nfs_port(char *opts)
38257c478bd9Sstevel@tonic-gate {
38267c478bd9Sstevel@tonic-gate 	struct mnttab m;
38277c478bd9Sstevel@tonic-gate 	uint_t nfs_port = 0;
38287c478bd9Sstevel@tonic-gate 	struct servent sv;
38297c478bd9Sstevel@tonic-gate 	char buf[256];
38307c478bd9Sstevel@tonic-gate 	int got_port;
38317c478bd9Sstevel@tonic-gate 
38327c478bd9Sstevel@tonic-gate 	m.mnt_mntopts = opts;
38337c478bd9Sstevel@tonic-gate 
38347c478bd9Sstevel@tonic-gate 	/*
38357c478bd9Sstevel@tonic-gate 	 * Get port specified in options list, if any.
38367c478bd9Sstevel@tonic-gate 	 */
38377c478bd9Sstevel@tonic-gate 	got_port = nopt(&m, MNTOPT_PORT, (int *)&nfs_port);
38387c478bd9Sstevel@tonic-gate 
38397c478bd9Sstevel@tonic-gate 	/*
38407c478bd9Sstevel@tonic-gate 	 * if no port specified or it is same as NFS_PORT return nfs
38417c478bd9Sstevel@tonic-gate 	 * To use any other daemon the port number should be different
38427c478bd9Sstevel@tonic-gate 	 */
38437c478bd9Sstevel@tonic-gate 	if (!got_port || nfs_port == NFS_PORT)
38447c478bd9Sstevel@tonic-gate 		return (1);
38457c478bd9Sstevel@tonic-gate 	/*
38467c478bd9Sstevel@tonic-gate 	 * If daemon is nfsd, return nfs
38477c478bd9Sstevel@tonic-gate 	 */
38487c478bd9Sstevel@tonic-gate 	if (getservbyport_r(nfs_port, NULL, &sv, buf, 256) == &sv &&
38491bbc88acSrm 	    strcmp(sv.s_name, "nfsd") == 0)
38507c478bd9Sstevel@tonic-gate 		return (1);
38517c478bd9Sstevel@tonic-gate 
38527c478bd9Sstevel@tonic-gate 	/*
38537c478bd9Sstevel@tonic-gate 	 * daemon is not nfs
38547c478bd9Sstevel@tonic-gate 	 */
38557c478bd9Sstevel@tonic-gate 	return (0);
38567c478bd9Sstevel@tonic-gate }
38577c478bd9Sstevel@tonic-gate 
38587c478bd9Sstevel@tonic-gate 
38597c478bd9Sstevel@tonic-gate /*
38607c478bd9Sstevel@tonic-gate  * destroy_auth_client_handle(cl)
38617c478bd9Sstevel@tonic-gate  * destroys the created client handle
38627c478bd9Sstevel@tonic-gate  */
386339d3e169Sevanl void
destroy_auth_client_handle(CLIENT * cl)38647c478bd9Sstevel@tonic-gate destroy_auth_client_handle(CLIENT *cl)
38657c478bd9Sstevel@tonic-gate {
38667c478bd9Sstevel@tonic-gate 	if (cl) {
38677c478bd9Sstevel@tonic-gate 		if (cl->cl_auth) {
38687c478bd9Sstevel@tonic-gate #ifdef MALLOC_DEBUG
38697c478bd9Sstevel@tonic-gate 			drop_alloc("AUTH_HANDLE", cl->cl_auth,
38701bbc88acSrm 			    __FILE__, __LINE__);
38717c478bd9Sstevel@tonic-gate #endif
38727c478bd9Sstevel@tonic-gate 			AUTH_DESTROY(cl->cl_auth);
38737c478bd9Sstevel@tonic-gate 			cl->cl_auth = NULL;
38747c478bd9Sstevel@tonic-gate 		}
38757c478bd9Sstevel@tonic-gate #ifdef MALLOC_DEBUG
38767c478bd9Sstevel@tonic-gate 		drop_alloc("CLNT_HANDLE", cl,
38771bbc88acSrm 		    __FILE__, __LINE__);
38787c478bd9Sstevel@tonic-gate #endif
38797c478bd9Sstevel@tonic-gate 		clnt_destroy(cl);
38807c478bd9Sstevel@tonic-gate 	}
38817c478bd9Sstevel@tonic-gate }
38827c478bd9Sstevel@tonic-gate 
38837c478bd9Sstevel@tonic-gate 
38847c478bd9Sstevel@tonic-gate /*
38857c478bd9Sstevel@tonic-gate  * Attempt to figure out which version of NFS to use in pingnfs().  If
38867c478bd9Sstevel@tonic-gate  * the version number was specified (i.e., non-zero), then use it.
38877c478bd9Sstevel@tonic-gate  * Otherwise, default to the compiled-in default or the default as set
38887c478bd9Sstevel@tonic-gate  * by the /etc/default/nfs configuration (as read by read_default().
38897c478bd9Sstevel@tonic-gate  */
38907c478bd9Sstevel@tonic-gate int
set_versrange(rpcvers_t nfsvers,rpcvers_t * vers,rpcvers_t * versmin)38917c478bd9Sstevel@tonic-gate set_versrange(rpcvers_t nfsvers, rpcvers_t *vers, rpcvers_t *versmin)
38927c478bd9Sstevel@tonic-gate {
38937c478bd9Sstevel@tonic-gate 	switch (nfsvers) {
38947c478bd9Sstevel@tonic-gate 	case 0:
38957c478bd9Sstevel@tonic-gate 		*vers = vers_max_default;
38967c478bd9Sstevel@tonic-gate 		*versmin = vers_min_default;
38977c478bd9Sstevel@tonic-gate 		break;
38987c478bd9Sstevel@tonic-gate 	case NFS_V4:
38997c478bd9Sstevel@tonic-gate 		*vers = NFS_V4;
39007c478bd9Sstevel@tonic-gate 		*versmin = NFS_V4;
39017c478bd9Sstevel@tonic-gate 		break;
39027c478bd9Sstevel@tonic-gate 	case NFS_V3:
39037c478bd9Sstevel@tonic-gate 		*vers = NFS_V3;
39047c478bd9Sstevel@tonic-gate 		*versmin = NFS_V3;
39057c478bd9Sstevel@tonic-gate 		break;
39067c478bd9Sstevel@tonic-gate 	case NFS_VERSION:
39077c478bd9Sstevel@tonic-gate 		*vers = NFS_VERSION;		/* version 2 */
39087c478bd9Sstevel@tonic-gate 		*versmin = NFS_VERSMIN;		/* version 2 */
39097c478bd9Sstevel@tonic-gate 		break;
39107c478bd9Sstevel@tonic-gate 	default:
39117c478bd9Sstevel@tonic-gate 		return (-1);
39127c478bd9Sstevel@tonic-gate 	}
39137c478bd9Sstevel@tonic-gate 	return (0);
39147c478bd9Sstevel@tonic-gate }
39157c478bd9Sstevel@tonic-gate 
39167c478bd9Sstevel@tonic-gate #ifdef CACHE_DEBUG
39177c478bd9Sstevel@tonic-gate /*
39187c478bd9Sstevel@tonic-gate  * trace_portmap_cache()
39197c478bd9Sstevel@tonic-gate  * traces the portmap cache values at desired points
39207c478bd9Sstevel@tonic-gate  */
39217c478bd9Sstevel@tonic-gate static void
trace_portmap_cache(void)39224dc70696SToomas Soome trace_portmap_cache(void)
39237c478bd9Sstevel@tonic-gate {
39247c478bd9Sstevel@tonic-gate 	syslog(LOG_ERR, "portmap_cache: accesses=%d lookups=%d hits=%d\n",
39251bbc88acSrm 	    portmap_cache_accesses, portmap_cache_lookups,
39261bbc88acSrm 	    portmap_cache_hits);
39277c478bd9Sstevel@tonic-gate }
39287c478bd9Sstevel@tonic-gate 
39297c478bd9Sstevel@tonic-gate /*
39307c478bd9Sstevel@tonic-gate  * trace_host_cache()
39317c478bd9Sstevel@tonic-gate  * traces the host cache values at desired points
39327c478bd9Sstevel@tonic-gate  */
39337c478bd9Sstevel@tonic-gate static void
trace_host_cache(void)39344dc70696SToomas Soome trace_host_cache(void)
39357c478bd9Sstevel@tonic-gate {
39367c478bd9Sstevel@tonic-gate 	syslog(LOG_ERR,
39371bbc88acSrm 	    "host_cache: accesses=%d lookups=%d deadhits=%d goodhits=%d\n",
39381bbc88acSrm 	    host_cache_accesses, host_cache_lookups, deadhost_cache_hits,
39391bbc88acSrm 	    goodhost_cache_hits);
39407c478bd9Sstevel@tonic-gate }
39417c478bd9Sstevel@tonic-gate #endif /* CACHE_DEBUG */
39427c478bd9Sstevel@tonic-gate 
39437c478bd9Sstevel@tonic-gate /*
3944dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States  * Read the NFS SMF properties to determine if the
39457c478bd9Sstevel@tonic-gate  * client has been configured for a new min/max for the NFS version to
39467c478bd9Sstevel@tonic-gate  * use.
39477c478bd9Sstevel@tonic-gate  */
39487c478bd9Sstevel@tonic-gate 
3949dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States #define	SVC_NFS_CLIENT	"svc:/network/nfs/client"
39507c478bd9Sstevel@tonic-gate 
39517c478bd9Sstevel@tonic-gate static void
read_default_nfs(void)39527c478bd9Sstevel@tonic-gate read_default_nfs(void)
39537c478bd9Sstevel@tonic-gate {
39547c478bd9Sstevel@tonic-gate 	struct stat buf;
3955dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	char defval[4];
3956dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	int errno, bufsz;
3957dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	int tmp, ret = 0;
3958dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 
3959dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	bufsz = 4;
3960dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	ret = nfs_smf_get_prop("client_versmin", defval, DEFAULT_INSTANCE,
3961dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	    SCF_TYPE_INTEGER, SVC_NFS_CLIENT, &bufsz);
3962dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	if (ret == SA_OK) {
3963dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 		errno = 0;
39644dc70696SToomas Soome 		tmp = strtol(defval, NULL, 10);
3965dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 		if (errno == 0) {
3966dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 			vers_min_default = tmp;
3967dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 		}
3968dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	}
39697c478bd9Sstevel@tonic-gate 
3970dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	bufsz = 4;
3971dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	ret = nfs_smf_get_prop("client_versmax", defval, DEFAULT_INSTANCE,
3972dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	    SCF_TYPE_INTEGER, SVC_NFS_CLIENT, &bufsz);
3973dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	if (ret == SA_OK) {
3974dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 		errno = 0;
39754dc70696SToomas Soome 		tmp = strtol(defval, NULL, 10);
3976dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 		if (errno == 0) {
3977dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 			vers_max_default = tmp;
3978dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 		}
3979dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	}
39807c478bd9Sstevel@tonic-gate 
39817c478bd9Sstevel@tonic-gate 	/*
3982dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	 * Quick sanity check on the values picked up from the
3983dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	 * defaults file.  Make sure that a mistake wasn't
3984dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	 * made that will confuse things later on.
3985dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	 * If so, reset to compiled-in defaults
39867c478bd9Sstevel@tonic-gate 	 */
3987dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	if (vers_min_default > vers_max_default ||
3988dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	    vers_min_default < NFS_VERSMIN ||
3989dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	    vers_max_default > NFS_VERSMAX) {
3990dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 		if (trace > 1) {
3991dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 			trace_prt(1,
39927c478bd9Sstevel@tonic-gate 	"  read_default: version minimum/maximum incorrectly configured\n");
3993dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 			trace_prt(1,
39947c478bd9Sstevel@tonic-gate "  read_default: config is min=%d, max%d. Resetting to min=%d, max%d\n",
3995dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 			    vers_min_default, vers_max_default,
3996dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 			    NFS_VERSMIN_DEFAULT,
3997dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 			    NFS_VERSMAX_DEFAULT);
39987c478bd9Sstevel@tonic-gate 		}
3999dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 		vers_min_default = NFS_VERSMIN_DEFAULT;
4000dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 		vers_max_default = NFS_VERSMAX_DEFAULT;
40017c478bd9Sstevel@tonic-gate 	}
40027c478bd9Sstevel@tonic-gate }
40037c478bd9Sstevel@tonic-gate 
40047c478bd9Sstevel@tonic-gate /*
40057c478bd9Sstevel@tonic-gate  *  Find the mnttab entry that corresponds to "name".
40067c478bd9Sstevel@tonic-gate  *  We're not sure what the name represents: either
40077c478bd9Sstevel@tonic-gate  *  a mountpoint name, or a special name (server:/path).
40087c478bd9Sstevel@tonic-gate  *  Return the last entry in the file that matches.
40097c478bd9Sstevel@tonic-gate  */
40107c478bd9Sstevel@tonic-gate static struct extmnttab *
mnttab_find(char * dirname)40114dc70696SToomas Soome mnttab_find(char *dirname)
40127c478bd9Sstevel@tonic-gate {
40137c478bd9Sstevel@tonic-gate 	FILE *fp;
40147c478bd9Sstevel@tonic-gate 	struct extmnttab mnt;
40157c478bd9Sstevel@tonic-gate 	struct extmnttab *res = NULL;
40167c478bd9Sstevel@tonic-gate 
40177c478bd9Sstevel@tonic-gate 	fp = fopen(MNTTAB, "r");
40187c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
40197c478bd9Sstevel@tonic-gate 		if (trace > 1)
40207c478bd9Sstevel@tonic-gate 			trace_prt(1, "	mnttab_find: unable to open mnttab\n");
40217c478bd9Sstevel@tonic-gate 		return (NULL);
40227c478bd9Sstevel@tonic-gate 	}
40237c478bd9Sstevel@tonic-gate 	while (getextmntent(fp, &mnt, sizeof (struct extmnttab)) == 0) {
40247c478bd9Sstevel@tonic-gate 		if (strcmp(mnt.mnt_mountp, dirname) == 0 ||
40257c478bd9Sstevel@tonic-gate 		    strcmp(mnt.mnt_special, dirname) == 0) {
40267c478bd9Sstevel@tonic-gate 			if (res)
40277c478bd9Sstevel@tonic-gate 				fsfreemnttab(res);
40287c478bd9Sstevel@tonic-gate 			res = fsdupmnttab(&mnt);
40297c478bd9Sstevel@tonic-gate 		}
40307c478bd9Sstevel@tonic-gate 	}
40317c478bd9Sstevel@tonic-gate 
40327c478bd9Sstevel@tonic-gate 	resetmnttab(fp);
40337c478bd9Sstevel@tonic-gate 	fclose(fp);
40347c478bd9Sstevel@tonic-gate 	if (res == NULL) {
40354dc70696SToomas Soome 		if (trace > 1) {
40367c478bd9Sstevel@tonic-gate 			trace_prt(1, "	mnttab_find: unable to find %s\n",
40374dc70696SToomas Soome 			    dirname);
40384dc70696SToomas Soome 		}
40397c478bd9Sstevel@tonic-gate 	}
40407c478bd9Sstevel@tonic-gate 	return (res);
40417c478bd9Sstevel@tonic-gate }
40427c478bd9Sstevel@tonic-gate 
40437c478bd9Sstevel@tonic-gate /*
40447c478bd9Sstevel@tonic-gate  * This function's behavior is taken from nfsstat.
40457c478bd9Sstevel@tonic-gate  * Trying to determine what NFS version was used for the mount.
40467c478bd9Sstevel@tonic-gate  */
40477c478bd9Sstevel@tonic-gate static int
is_v4_mount(char * mntpath)40487c478bd9Sstevel@tonic-gate is_v4_mount(char *mntpath)
40497c478bd9Sstevel@tonic-gate {
40507c478bd9Sstevel@tonic-gate 	kstat_ctl_t *kc = NULL;		/* libkstat cookie */
40517c478bd9Sstevel@tonic-gate 	kstat_t *ksp;
40527c478bd9Sstevel@tonic-gate 	ulong_t fsid;
40537c478bd9Sstevel@tonic-gate 	struct mntinfo_kstat mik;
40547c478bd9Sstevel@tonic-gate 	struct extmnttab *mntp;
40557c478bd9Sstevel@tonic-gate 	uint_t mnt_minor;
40567c478bd9Sstevel@tonic-gate 
40577c478bd9Sstevel@tonic-gate 	if ((mntp = mnttab_find(mntpath)) == NULL)
40587c478bd9Sstevel@tonic-gate 		return (FALSE);
40597c478bd9Sstevel@tonic-gate 
40607c478bd9Sstevel@tonic-gate 	/* save the minor number and free the struct so we don't forget */
40617c478bd9Sstevel@tonic-gate 	mnt_minor = mntp->mnt_minor;
40627c478bd9Sstevel@tonic-gate 	fsfreemnttab(mntp);
40637c478bd9Sstevel@tonic-gate 
40647c478bd9Sstevel@tonic-gate 	if ((kc = kstat_open()) == NULL)
40657c478bd9Sstevel@tonic-gate 		return (FALSE);
40667c478bd9Sstevel@tonic-gate 
40677c478bd9Sstevel@tonic-gate 	for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
40687c478bd9Sstevel@tonic-gate 		if (ksp->ks_type != KSTAT_TYPE_RAW)
40697c478bd9Sstevel@tonic-gate 			continue;
40707c478bd9Sstevel@tonic-gate 		if (strcmp(ksp->ks_module, "nfs") != 0)
40717c478bd9Sstevel@tonic-gate 			continue;
40727c478bd9Sstevel@tonic-gate 		if (strcmp(ksp->ks_name, "mntinfo") != 0)
40737c478bd9Sstevel@tonic-gate 			continue;
40747c478bd9Sstevel@tonic-gate 		if (mnt_minor != ksp->ks_instance)
40757c478bd9Sstevel@tonic-gate 			continue;
40767c478bd9Sstevel@tonic-gate 
40777c478bd9Sstevel@tonic-gate 		if (kstat_read(kc, ksp, &mik) == -1)
40787c478bd9Sstevel@tonic-gate 			continue;
40797c478bd9Sstevel@tonic-gate 
40807c478bd9Sstevel@tonic-gate 		(void) kstat_close(kc);
40817c478bd9Sstevel@tonic-gate 		if (mik.mik_vers == 4)
40827c478bd9Sstevel@tonic-gate 			return (TRUE);
40837c478bd9Sstevel@tonic-gate 		else
40847c478bd9Sstevel@tonic-gate 			return (FALSE);
40857c478bd9Sstevel@tonic-gate 	}
40867c478bd9Sstevel@tonic-gate 	(void) kstat_close(kc);
40877c478bd9Sstevel@tonic-gate 
40887c478bd9Sstevel@tonic-gate 	return (FALSE);
40897c478bd9Sstevel@tonic-gate }
409045916cd2Sjpk 
409145916cd2Sjpk static int
create_homedir(const char * src,const char * dst)40924dc70696SToomas Soome create_homedir(const char *src, const char *dst)
40934dc70696SToomas Soome {
409445916cd2Sjpk 
409545916cd2Sjpk 	struct stat stbuf;
409645916cd2Sjpk 	char *dst_username;
409745916cd2Sjpk 	struct passwd *pwd, pwds;
409845916cd2Sjpk 	char buf_pwd[NSS_BUFLEN_PASSWD];
409945916cd2Sjpk 	int homedir_len;
410045916cd2Sjpk 	int dst_dir_len;
410145916cd2Sjpk 	int src_dir_len;
410245916cd2Sjpk 
410345916cd2Sjpk 	if (trace > 1)
410445916cd2Sjpk 		trace_prt(1, "entered create_homedir\n");
410545916cd2Sjpk 
410645916cd2Sjpk 	if (stat(src, &stbuf) == 0) {
410745916cd2Sjpk 		if (trace > 1)
410845916cd2Sjpk 			trace_prt(1, "src exists\n");
410945916cd2Sjpk 		return (1);
411045916cd2Sjpk 	}
411145916cd2Sjpk 
411245916cd2Sjpk 	dst_username = strrchr(dst, '/');
411345916cd2Sjpk 	if (dst_username) {
411445916cd2Sjpk 		dst_username++; /* Skip over slash */
411545916cd2Sjpk 		pwd = getpwnam_r(dst_username, &pwds, buf_pwd,
411645916cd2Sjpk 		    sizeof (buf_pwd));
411745916cd2Sjpk 		if (pwd == NULL) {
411845916cd2Sjpk 			return (0);
411945916cd2Sjpk 		}
412045916cd2Sjpk 	} else {
412145916cd2Sjpk 		return (0);
412245916cd2Sjpk 	}
412345916cd2Sjpk 
412445916cd2Sjpk 	homedir_len = strlen(pwd->pw_dir);
412545916cd2Sjpk 	dst_dir_len = strlen(dst) - homedir_len;
412645916cd2Sjpk 	src_dir_len = strlen(src) - homedir_len;
412745916cd2Sjpk 
412845916cd2Sjpk 	/* Check that the paths are in the same zone */
412945916cd2Sjpk 	if (src_dir_len < dst_dir_len ||
413045916cd2Sjpk 	    (strncmp(dst, src, dst_dir_len) != 0)) {
413145916cd2Sjpk 		if (trace > 1)
413245916cd2Sjpk 			trace_prt(1, "	paths don't match\n");
413345916cd2Sjpk 		return (0);
413445916cd2Sjpk 	}
413545916cd2Sjpk 	/* Check that mountpoint is an auto_home entry */
413645916cd2Sjpk 	if (dst_dir_len < 0 ||
413745916cd2Sjpk 	    (strcmp(pwd->pw_dir, dst + dst_dir_len) != 0)) {
413845916cd2Sjpk 		return (0);
413945916cd2Sjpk 	}
414045916cd2Sjpk 
414145916cd2Sjpk 	/* Check that source is an home directory entry */
414245916cd2Sjpk 	if (src_dir_len < 0 ||
414345916cd2Sjpk 	    (strcmp(pwd->pw_dir, src + src_dir_len) != 0)) {
41444dc70696SToomas Soome 		if (trace > 1) {
414545916cd2Sjpk 			trace_prt(1, "	homedir (2) doesn't match %s\n",
41464dc70696SToomas Soome 			    src+src_dir_len);
41474dc70696SToomas Soome 		}
414845916cd2Sjpk 		return (0);
414945916cd2Sjpk 	}
415045916cd2Sjpk 
415145916cd2Sjpk 	if (mkdir(src,
415245916cd2Sjpk 	    S_IRUSR | S_IWUSR | S_IXUSR | S_IXGRP | S_IXOTH) == -1) {
415345916cd2Sjpk 		if (trace > 1) {
415445916cd2Sjpk 			trace_prt(1, "	Couldn't mkdir %s\n", src);
415545916cd2Sjpk 		}
415645916cd2Sjpk 		return (0);
415745916cd2Sjpk 	}
415845916cd2Sjpk 
415945916cd2Sjpk 	if (chown(src, pwd->pw_uid, pwd->pw_gid) == -1) {
416045916cd2Sjpk 		unlink(src);
416145916cd2Sjpk 		return (0);
416245916cd2Sjpk 	}
416345916cd2Sjpk 
416445916cd2Sjpk 	/* Created new home directory for the user */
416545916cd2Sjpk 	return (1);
416645916cd2Sjpk }
416739d3e169Sevanl 
416839d3e169Sevanl void
free_nfs_args(struct nfs_args * argp)416939d3e169Sevanl free_nfs_args(struct nfs_args *argp)
417039d3e169Sevanl {
417139d3e169Sevanl 	struct nfs_args *oldp;
417239d3e169Sevanl 	while (argp) {
417339d3e169Sevanl 		if (argp->pathconf)
417439d3e169Sevanl 			free(argp->pathconf);
417539d3e169Sevanl 		if (argp->knconf)
417639d3e169Sevanl 			free_knconf(argp->knconf);
417739d3e169Sevanl 		if (argp->addr)
417839d3e169Sevanl 			netbuf_free(argp->addr);
417939d3e169Sevanl 		if (argp->syncaddr)
418039d3e169Sevanl 			netbuf_free(argp->syncaddr);
418139d3e169Sevanl 		if (argp->netname)
418239d3e169Sevanl 			free(argp->netname);
418339d3e169Sevanl 		if (argp->hostname)
418439d3e169Sevanl 			free(argp->hostname);
418539d3e169Sevanl 		if (argp->nfs_ext_u.nfs_extB.secdata)
418639d3e169Sevanl 			nfs_free_secdata(argp->nfs_ext_u.nfs_extB.secdata);
418739d3e169Sevanl 		if (argp->fh)
418839d3e169Sevanl 			free(argp->fh);
418939d3e169Sevanl 		if (argp->nfs_ext_u.nfs_extA.secdata) {
419039d3e169Sevanl 			sec_data_t	*sd;
419139d3e169Sevanl 			sd = argp->nfs_ext_u.nfs_extA.secdata;
419239d3e169Sevanl 			if (sd == NULL)
419339d3e169Sevanl 				break;
419439d3e169Sevanl 			switch (sd->rpcflavor) {
419539d3e169Sevanl 			case AUTH_NONE:
419639d3e169Sevanl 			case AUTH_UNIX:
419739d3e169Sevanl 			case AUTH_LOOPBACK:
419839d3e169Sevanl 				break;
419939d3e169Sevanl 			case AUTH_DES:
420039d3e169Sevanl 			{
420139d3e169Sevanl 				dh_k4_clntdata_t	*dhk4;
420239d3e169Sevanl 				dhk4 = (dh_k4_clntdata_t *)sd->data;
420339d3e169Sevanl 				if (dhk4 == NULL)
420439d3e169Sevanl 					break;
420539d3e169Sevanl 				if (dhk4->syncaddr.buf)
420639d3e169Sevanl 					free(dhk4->syncaddr.buf);
420739d3e169Sevanl 				if (dhk4->knconf->knc_protofmly)
420839d3e169Sevanl 					free(dhk4->knconf->knc_protofmly);
420939d3e169Sevanl 				if (dhk4->knconf->knc_proto)
421039d3e169Sevanl 					free(dhk4->knconf->knc_proto);
421139d3e169Sevanl 				if (dhk4->knconf)
421239d3e169Sevanl 					free(dhk4->knconf);
421339d3e169Sevanl 				if (dhk4->netname)
421439d3e169Sevanl 					free(dhk4->netname);
421539d3e169Sevanl 				free(dhk4);
421639d3e169Sevanl 				break;
421739d3e169Sevanl 			}
421839d3e169Sevanl 			case RPCSEC_GSS:
421939d3e169Sevanl 			{
422039d3e169Sevanl 				gss_clntdata_t	*gss;
422139d3e169Sevanl 				gss = (gss_clntdata_t *)sd->data;
422239d3e169Sevanl 				if (gss == NULL)
422339d3e169Sevanl 					break;
422439d3e169Sevanl 				if (gss->mechanism.elements)
422539d3e169Sevanl 					free(gss->mechanism.elements);
422639d3e169Sevanl 				free(gss);
422739d3e169Sevanl 				break;
422839d3e169Sevanl 			}
422939d3e169Sevanl 			}
423039d3e169Sevanl 		}
423139d3e169Sevanl 		oldp = argp;
423239d3e169Sevanl 		if (argp->nfs_args_ext == NFS_ARGS_EXTB)
423339d3e169Sevanl 			argp = argp->nfs_ext_u.nfs_extB.next;
423439d3e169Sevanl 		else
423539d3e169Sevanl 			argp = NULL;
423639d3e169Sevanl 		free(oldp);
423739d3e169Sevanl 	}
423839d3e169Sevanl }
42392f172c55SRobert Thurlow 
42402f172c55SRobert Thurlow void *
get_netconfig_info(enum type_of_stuff type_of_stuff,char * hostname,rpcprog_t prog,rpcvers_t vers,struct netconfig * nconf,ushort_t port,struct t_info * tinfo,struct t_bind * tbind,caddr_t * fhp,bool_t direct_to_server,char * fspath,enum clnt_stat * cstat,mfs_snego_t * mfssnego)42412f172c55SRobert Thurlow get_netconfig_info(enum type_of_stuff type_of_stuff, char *hostname,
42424dc70696SToomas Soome     rpcprog_t prog, rpcvers_t vers, struct netconfig *nconf,
42434dc70696SToomas Soome     ushort_t port, struct t_info *tinfo, struct t_bind *tbind,
42444dc70696SToomas Soome     caddr_t *fhp, bool_t direct_to_server, char *fspath,
42454dc70696SToomas Soome     enum clnt_stat *cstat, mfs_snego_t *mfssnego)
42462f172c55SRobert Thurlow {
42472f172c55SRobert Thurlow 	struct netconfig *nb = NULL;
42482f172c55SRobert Thurlow 	int ping_server = 0;
42492f172c55SRobert Thurlow 
42502f172c55SRobert Thurlow 
42512f172c55SRobert Thurlow 	if (nconf == NULL)
42522f172c55SRobert Thurlow 		return (NULL);
42532f172c55SRobert Thurlow 
42542f172c55SRobert Thurlow 	switch (type_of_stuff) {
42552f172c55SRobert Thurlow 	case SERVER_FH:
42562f172c55SRobert Thurlow 		nb = get_server_fh(hostname, prog, vers, mfssnego,
42572f172c55SRobert Thurlow 		    nconf, port, tinfo, tbind, fhp, direct_to_server,
42582f172c55SRobert Thurlow 		    fspath, cstat);
42592f172c55SRobert Thurlow 		break;
42602f172c55SRobert Thurlow 	case SERVER_PING:
42612f172c55SRobert Thurlow 		ping_server = 1;
4262*f45bbb21SToomas Soome 		/* FALLTHROUGH */
42632f172c55SRobert Thurlow 	case SERVER_ADDR:
42642f172c55SRobert Thurlow 		nb = get_server_addrorping(hostname, prog, vers,
42652f172c55SRobert Thurlow 		    nconf, port, tinfo, tbind, fhp, direct_to_server,
42662f172c55SRobert Thurlow 		    fspath, cstat, ping_server);
42672f172c55SRobert Thurlow 		break;
42682f172c55SRobert Thurlow 	default:
42692f172c55SRobert Thurlow 		assert(nb != NULL);
42702f172c55SRobert Thurlow 	}
42712f172c55SRobert Thurlow 	return (nb);
42722f172c55SRobert Thurlow }
42732f172c55SRobert Thurlow 
42742f172c55SRobert Thurlow /*
42752f172c55SRobert Thurlow  * Get the server address or can we ping it or not.
42762f172c55SRobert Thurlow  * Check the portmap cache first for server address.
42772f172c55SRobert Thurlow  * If no entries there, ping the server with a NULLPROC rpc.
42782f172c55SRobert Thurlow  */
42792f172c55SRobert Thurlow void *
get_server_addrorping(char * hostname,rpcprog_t prog,rpcvers_t vers,struct netconfig * nconf,ushort_t port,struct t_info * tinfo,struct t_bind * tbind,caddr_t * fhp,bool_t direct_to_server,char * fspath,enum clnt_stat * cstat,int ping_server)42802f172c55SRobert Thurlow get_server_addrorping(char *hostname, rpcprog_t prog, rpcvers_t vers,
42814dc70696SToomas Soome     struct netconfig *nconf, ushort_t port, struct t_info *tinfo,
42824dc70696SToomas Soome     struct t_bind *tbind, caddr_t *fhp, bool_t direct_to_server,
42834dc70696SToomas Soome     char *fspath, enum clnt_stat *cstat, int ping_server)
42842f172c55SRobert Thurlow {
42852f172c55SRobert Thurlow 	struct timeval tv;
42862f172c55SRobert Thurlow 	enum clnt_stat cs = RPC_TIMEDOUT;
42872f172c55SRobert Thurlow 	struct netbuf *nb = NULL;
42882f172c55SRobert Thurlow 	CLIENT *cl = NULL;
42892f172c55SRobert Thurlow 	int fd = -1;
42902f172c55SRobert Thurlow 
42912f172c55SRobert Thurlow 	if (prog == NFS_PROGRAM && vers == NFS_V4)
42922f172c55SRobert Thurlow 		if (strncasecmp(nconf->nc_proto, NC_UDP, strlen(NC_UDP)) == 0)
42932f172c55SRobert Thurlow 			goto done;
42942f172c55SRobert Thurlow 
42952f172c55SRobert Thurlow 	if ((fd = t_open(nconf->nc_device, O_RDWR, tinfo)) < 0) {
42962f172c55SRobert Thurlow 		goto done;
42972f172c55SRobert Thurlow 	}
42982f172c55SRobert Thurlow 
42992f172c55SRobert Thurlow 	/* LINTED pointer alignment */
43002f172c55SRobert Thurlow 	if ((tbind = (struct t_bind *)t_alloc(fd, T_BIND, T_ADDR))
43012f172c55SRobert Thurlow 	    == NULL) {
43022f172c55SRobert Thurlow 		goto done;
43032f172c55SRobert Thurlow 	}
43042f172c55SRobert Thurlow 
43052f172c55SRobert Thurlow 	if (direct_to_server != TRUE) {
43062f172c55SRobert Thurlow 		if (!ping_server) {
43072f172c55SRobert Thurlow 			if (get_cached_srv_addr(hostname, prog, vers,
43082f172c55SRobert Thurlow 			    nconf, &tbind->addr) == 0)
43092f172c55SRobert Thurlow 				goto done;
43102f172c55SRobert Thurlow 		} else {
43112f172c55SRobert Thurlow 			if (port == 0)
43122f172c55SRobert Thurlow 				goto done;
43132f172c55SRobert Thurlow 		}
43142f172c55SRobert Thurlow 	}
43152f172c55SRobert Thurlow 	if (setup_nb_parms(nconf, tbind, tinfo, hostname,
43162f172c55SRobert Thurlow 	    fd, direct_to_server, port, prog, vers, 0) < 0)
43172f172c55SRobert Thurlow 		goto done;
43182f172c55SRobert Thurlow 
43192f172c55SRobert Thurlow 	if (port || (direct_to_server == TRUE)) {
43202f172c55SRobert Thurlow 		tv.tv_sec = 10;
43212f172c55SRobert Thurlow 		tv.tv_usec = 0;
43222f172c55SRobert Thurlow 		cl = clnt_tli_create(fd, nconf, &tbind->addr,
43232f172c55SRobert Thurlow 		    prog, vers, 0, 0);
43242f172c55SRobert Thurlow 		if (cl == NULL)
43252f172c55SRobert Thurlow 			goto done;
43262f172c55SRobert Thurlow 
43272f172c55SRobert Thurlow 		cs = clnt_call(cl, NULLPROC, xdr_void, 0,
43282f172c55SRobert Thurlow 		    xdr_void, 0, tv);
43292f172c55SRobert Thurlow 		if (cs != RPC_SUCCESS) {
43302f172c55SRobert Thurlow 			syslog(LOG_ERR, "error is %d", cs);
43312f172c55SRobert Thurlow 			goto done;
43322f172c55SRobert Thurlow 		}
43332f172c55SRobert Thurlow 	}
43342f172c55SRobert Thurlow 	if (!ping_server) {
43352f172c55SRobert Thurlow 		nb = (struct netbuf *)malloc(sizeof (struct netbuf));
43362f172c55SRobert Thurlow 		if (nb == NULL) {
43372f172c55SRobert Thurlow 			syslog(LOG_ERR, "no memory\n");
43382f172c55SRobert Thurlow 			goto done;
43392f172c55SRobert Thurlow 		}
43402f172c55SRobert Thurlow 		nb->buf = (char *)malloc(tbind->addr.maxlen);
43412f172c55SRobert Thurlow 		if (nb->buf == NULL) {
43422f172c55SRobert Thurlow 			syslog(LOG_ERR, "no memory\n");
43432f172c55SRobert Thurlow 			free(nb);
43442f172c55SRobert Thurlow 			nb = NULL;
43452f172c55SRobert Thurlow 			goto done;
43462f172c55SRobert Thurlow 		}
43472f172c55SRobert Thurlow 		(void) memcpy(nb->buf, tbind->addr.buf, tbind->addr.len);
43482f172c55SRobert Thurlow 		nb->len = tbind->addr.len;
43492f172c55SRobert Thurlow 		nb->maxlen = tbind->addr.maxlen;
43502f172c55SRobert Thurlow 		cs = RPC_SUCCESS;
43512f172c55SRobert Thurlow 	}
43522f172c55SRobert Thurlow done:
43532f172c55SRobert Thurlow 	destroy_auth_client_handle(cl);
43542f172c55SRobert Thurlow 	cleanup_tli_parms(tbind, fd);
43552f172c55SRobert Thurlow 	*cstat = cs;
43562f172c55SRobert Thurlow 	return (nb);
43572f172c55SRobert Thurlow }
4358