xref: /titanic_44/usr/src/cmd/fs.d/autofs/autod_nfs.c (revision bbe876c07ed632b8f85e195d41e7948382064a95)
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  */
21*bbe876c0SMarcel Telka 
227c478bd9Sstevel@tonic-gate /*
23*bbe876c0SMarcel 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>
73*bbe876c0SMarcel 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 
92b9238976Sth199096 extern void set_nfsv4_ephemeral_mount_to(void);
93b9238976Sth199096 
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 #define	MNTTYPE_CACHEFS "cachefs"
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate /*
1037c478bd9Sstevel@tonic-gate  * host cache states
1047c478bd9Sstevel@tonic-gate  */
1057c478bd9Sstevel@tonic-gate #define	NOHOST		0
1067c478bd9Sstevel@tonic-gate #define	GOODHOST	1
1077c478bd9Sstevel@tonic-gate #define	DEADHOST	2
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate #define	NFS_ARGS_EXTB_secdata(args, secdata) \
1107c478bd9Sstevel@tonic-gate 	{ (args).nfs_args_ext = NFS_ARGS_EXTB, \
1117c478bd9Sstevel@tonic-gate 	(args).nfs_ext_u.nfs_extB.secdata = secdata; }
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate struct cache_entry {
1147c478bd9Sstevel@tonic-gate 	struct	cache_entry *cache_next;
1157c478bd9Sstevel@tonic-gate 	char	*cache_host;
1167c478bd9Sstevel@tonic-gate 	time_t	cache_time;
1177c478bd9Sstevel@tonic-gate 	int	cache_state;
1187c478bd9Sstevel@tonic-gate 	rpcvers_t cache_reqvers;
1197c478bd9Sstevel@tonic-gate 	rpcvers_t cache_outvers;
1207c478bd9Sstevel@tonic-gate 	char	*cache_proto;
1217c478bd9Sstevel@tonic-gate };
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate struct mfs_snego_t {
1247c478bd9Sstevel@tonic-gate 	int sec_opt;
1257c478bd9Sstevel@tonic-gate 	bool_t snego_done;
1267c478bd9Sstevel@tonic-gate 	char *nfs_flavor;
1277c478bd9Sstevel@tonic-gate 	seconfig_t nfs_sec;
1287c478bd9Sstevel@tonic-gate };
1297c478bd9Sstevel@tonic-gate typedef struct mfs_snego_t mfs_snego_t;
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate static struct cache_entry *cache_head = NULL;
1327c478bd9Sstevel@tonic-gate rwlock_t cache_lock;	/* protect the cache chain */
1337c478bd9Sstevel@tonic-gate 
1343bfb48feSsemery static enum nfsstat nfsmount(struct mapfs *, char *, char *, int, int, uid_t,
1353bfb48feSsemery 	action_list *);
1367c478bd9Sstevel@tonic-gate static int is_nfs_port(char *);
1377c478bd9Sstevel@tonic-gate 
1382f172c55SRobert Thurlow static void netbuf_free(struct netbuf *);
1397c478bd9Sstevel@tonic-gate static int get_pathconf(CLIENT *, char *, char *, struct pathcnf **, int);
1407c478bd9Sstevel@tonic-gate static struct mapfs *enum_servers(struct mapent *, char *);
1417c478bd9Sstevel@tonic-gate static struct mapfs *get_mysubnet_servers(struct mapfs *);
1427c478bd9Sstevel@tonic-gate static int subnet_test(int af, struct sioc_addrreq *);
1437c478bd9Sstevel@tonic-gate static	struct	netbuf *get_addr(char *, rpcprog_t, rpcvers_t,
1447c478bd9Sstevel@tonic-gate 	struct netconfig **, char *, ushort_t, struct t_info *);
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate static	struct	netbuf *get_pubfh(char *, rpcvers_t, mfs_snego_t *,
1477c478bd9Sstevel@tonic-gate 	struct netconfig **, char *, ushort_t, struct t_info *, caddr_t *,
1487c478bd9Sstevel@tonic-gate 	bool_t, char *);
1497c478bd9Sstevel@tonic-gate 
15045916cd2Sjpk static int create_homedir(const char *, const char *);
15145916cd2Sjpk 
1527c478bd9Sstevel@tonic-gate enum type_of_stuff {
1537c478bd9Sstevel@tonic-gate 	SERVER_ADDR = 0,
1547c478bd9Sstevel@tonic-gate 	SERVER_PING = 1,
1557c478bd9Sstevel@tonic-gate 	SERVER_FH = 2
1567c478bd9Sstevel@tonic-gate };
1577c478bd9Sstevel@tonic-gate 
1582f172c55SRobert Thurlow static void *get_server_netinfo(enum type_of_stuff, char *, rpcprog_t,
1597c478bd9Sstevel@tonic-gate 	rpcvers_t, mfs_snego_t *, struct netconfig **, char *, ushort_t,
1607c478bd9Sstevel@tonic-gate 	struct t_info *, caddr_t *, bool_t, char *, enum clnt_stat *);
1612f172c55SRobert Thurlow static void *get_netconfig_info(enum type_of_stuff, char *, rpcprog_t,
1622f172c55SRobert Thurlow 	rpcvers_t, struct netconfig *, ushort_t, struct t_info *,
1632f172c55SRobert Thurlow 	struct t_bind *, caddr_t *, bool_t, char *, enum clnt_stat *,
1642f172c55SRobert Thurlow 	mfs_snego_t *);
1652f172c55SRobert Thurlow static void *get_server_addrorping(char *, rpcprog_t, rpcvers_t,
1662f172c55SRobert Thurlow 	struct netconfig *, ushort_t, struct t_info *, struct t_bind *,
1672f172c55SRobert Thurlow 	caddr_t *, bool_t, char *, enum clnt_stat *, int);
1682f172c55SRobert Thurlow static void *get_server_fh(char *, rpcprog_t, rpcvers_t, mfs_snego_t *,
1692f172c55SRobert Thurlow 	struct netconfig *, ushort_t, struct t_info *, struct t_bind *,
1707c478bd9Sstevel@tonic-gate 	caddr_t *, bool_t, char *, enum clnt_stat *);
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate struct mapfs *add_mfs(struct mapfs *, int, struct mapfs **, struct mapfs **);
1737c478bd9Sstevel@tonic-gate void free_mfs(struct mapfs *);
1747c478bd9Sstevel@tonic-gate static void dump_mfs(struct mapfs *, char *, int);
1757c478bd9Sstevel@tonic-gate static char *dump_distance(struct mapfs *);
1767c478bd9Sstevel@tonic-gate static void cache_free(struct cache_entry *);
1777c478bd9Sstevel@tonic-gate static int cache_check(char *, rpcvers_t *, char *);
1787c478bd9Sstevel@tonic-gate static void cache_enter(char *, rpcvers_t, rpcvers_t, char *, int);
17939d3e169Sevanl void destroy_auth_client_handle(CLIENT *cl);
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate #ifdef CACHE_DEBUG
1827c478bd9Sstevel@tonic-gate static void trace_host_cache();
1837c478bd9Sstevel@tonic-gate static void trace_portmap_cache();
1847c478bd9Sstevel@tonic-gate #endif /* CACHE_DEBUG */
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate static int rpc_timeout = 20;
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate #ifdef CACHE_DEBUG
1897c478bd9Sstevel@tonic-gate /*
1907c478bd9Sstevel@tonic-gate  * host cache counters. These variables do not need to be protected
1917c478bd9Sstevel@tonic-gate  * by mutex's. They have been added to measure the utility of the
1927c478bd9Sstevel@tonic-gate  * goodhost/deadhost cache in the lazy hierarchical mounting scheme.
1937c478bd9Sstevel@tonic-gate  */
1947c478bd9Sstevel@tonic-gate static int host_cache_accesses = 0;
1957c478bd9Sstevel@tonic-gate static int host_cache_lookups = 0;
1967c478bd9Sstevel@tonic-gate static int deadhost_cache_hits = 0;
1977c478bd9Sstevel@tonic-gate static int goodhost_cache_hits = 0;
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate /*
2007c478bd9Sstevel@tonic-gate  * portmap cache counters. These variables do not need to be protected
2017c478bd9Sstevel@tonic-gate  * by mutex's. They have been added to measure the utility of the portmap
2027c478bd9Sstevel@tonic-gate  * cache in the lazy hierarchical mounting scheme.
2037c478bd9Sstevel@tonic-gate  */
2047c478bd9Sstevel@tonic-gate static int portmap_cache_accesses = 0;
2057c478bd9Sstevel@tonic-gate static int portmap_cache_lookups = 0;
2067c478bd9Sstevel@tonic-gate static int portmap_cache_hits = 0;
2077c478bd9Sstevel@tonic-gate #endif /* CACHE_DEBUG */
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate /*
2107c478bd9Sstevel@tonic-gate  * There are the defaults (range) for the client when determining
2117c478bd9Sstevel@tonic-gate  * which NFS version to use when probing the server (see above).
2127c478bd9Sstevel@tonic-gate  * These will only be used when the vers mount option is not used and
2137c478bd9Sstevel@tonic-gate  * these may be reset if /etc/default/nfs is configured to do so.
2147c478bd9Sstevel@tonic-gate  */
2157c478bd9Sstevel@tonic-gate static rpcvers_t vers_max_default = NFS_VERSMAX_DEFAULT;
2167c478bd9Sstevel@tonic-gate static rpcvers_t vers_min_default = NFS_VERSMIN_DEFAULT;
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate /*
2197c478bd9Sstevel@tonic-gate  * list of support services needed
2207c478bd9Sstevel@tonic-gate  */
2217c478bd9Sstevel@tonic-gate static char	*service_list[] = { STATD, LOCKD, NULL };
2227c478bd9Sstevel@tonic-gate static char	*service_list_v4[] = { STATD, LOCKD, NFS4CBD, NFSMAPID, NULL };
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate static void read_default_nfs(void);
2257c478bd9Sstevel@tonic-gate static int is_v4_mount(char *);
2267c478bd9Sstevel@tonic-gate static void start_nfs4cbd(void);
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate int
mount_nfs(struct mapent * me,char * mntpnt,char * prevhost,int overlay,uid_t uid,action_list ** alpp)22939d3e169Sevanl mount_nfs(
23039d3e169Sevanl 	struct mapent *me,
23139d3e169Sevanl 	char *mntpnt,
23239d3e169Sevanl 	char *prevhost,
23339d3e169Sevanl 	int overlay,
2343bfb48feSsemery 	uid_t uid,
23539d3e169Sevanl 	action_list **alpp)
2367c478bd9Sstevel@tonic-gate {
2377c478bd9Sstevel@tonic-gate 	struct mapfs *mfs, *mp;
2387c478bd9Sstevel@tonic-gate 	int err = -1;
2397c478bd9Sstevel@tonic-gate 	int cached;
24039d3e169Sevanl 	action_list *alp;
241a05556d6SNagakiran Rajashekar 	char *dir;
24239d3e169Sevanl 
24339d3e169Sevanl 
24439d3e169Sevanl 	alp = *alpp;
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate 	read_default_nfs();
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 	mfs = enum_servers(me, prevhost);
2497c478bd9Sstevel@tonic-gate 	if (mfs == NULL)
2507c478bd9Sstevel@tonic-gate 		return (ENOENT);
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 	/*
2537c478bd9Sstevel@tonic-gate 	 * Try loopback if we have something on localhost; if nothing
2547c478bd9Sstevel@tonic-gate 	 * works, we will fall back to NFS
2557c478bd9Sstevel@tonic-gate 	 */
2567c478bd9Sstevel@tonic-gate 	if (is_nfs_port(me->map_mntopts)) {
2577c478bd9Sstevel@tonic-gate 		for (mp = mfs; mp; mp = mp->mfs_next) {
2587c478bd9Sstevel@tonic-gate 			if (self_check(mp->mfs_host)) {
2597c478bd9Sstevel@tonic-gate 				err = loopbackmount(mp->mfs_dir,
2607c478bd9Sstevel@tonic-gate 				    mntpnt, me->map_mntopts, overlay);
2617c478bd9Sstevel@tonic-gate 				if (err) {
2627c478bd9Sstevel@tonic-gate 					mp->mfs_ignore = 1;
2637c478bd9Sstevel@tonic-gate 				} else {
26439d3e169Sevanl 					/*
26539d3e169Sevanl 					 * Free action_list if there
26639d3e169Sevanl 					 * is one as it is not needed.
26739d3e169Sevanl 					 * Make sure to set alpp to null
26839d3e169Sevanl 					 * so caller doesn't try to free it
26939d3e169Sevanl 					 * again.
27039d3e169Sevanl 					 */
27139d3e169Sevanl 					if (*alpp) {
27239d3e169Sevanl 						free(*alpp);
27339d3e169Sevanl 						*alpp = NULL;
27439d3e169Sevanl 					}
2757c478bd9Sstevel@tonic-gate 					break;
2767c478bd9Sstevel@tonic-gate 				}
2777c478bd9Sstevel@tonic-gate 			}
2787c478bd9Sstevel@tonic-gate 		}
2797c478bd9Sstevel@tonic-gate 	}
2807c478bd9Sstevel@tonic-gate 	if (err) {
2817c478bd9Sstevel@tonic-gate 		cached = strcmp(me->map_mounter, MNTTYPE_CACHEFS) == 0;
282a05556d6SNagakiran Rajashekar 		dir = strdup(mfs->mfs_dir);
2837c478bd9Sstevel@tonic-gate 		err = nfsmount(mfs, mntpnt, me->map_mntopts,
2843bfb48feSsemery 		    cached, overlay, uid, alp);
2857c478bd9Sstevel@tonic-gate 		if (err && trace > 1) {
2867c478bd9Sstevel@tonic-gate 			trace_prt(1, "  Couldn't mount %s:%s, err=%d\n",
287a05556d6SNagakiran Rajashekar 			    mfs->mfs_host ? mfs->mfs_host : "",
288a05556d6SNagakiran Rajashekar 			    mfs->mfs_dir ? mfs->mfs_dir : dir, err);
2897c478bd9Sstevel@tonic-gate 		}
290a05556d6SNagakiran Rajashekar 		free(dir);
2917c478bd9Sstevel@tonic-gate 	}
2927c478bd9Sstevel@tonic-gate 	free_mfs(mfs);
2937c478bd9Sstevel@tonic-gate 	return (err);
2947c478bd9Sstevel@tonic-gate }
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate /*
2987c478bd9Sstevel@tonic-gate  * Using the new ioctl SIOCTONLINK to determine if a host is on the same
2997c478bd9Sstevel@tonic-gate  * subnet. Remove the old network, subnet check.
3007c478bd9Sstevel@tonic-gate  */
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate static struct mapfs *
get_mysubnet_servers(struct mapfs * mfs_in)3037c478bd9Sstevel@tonic-gate get_mysubnet_servers(struct mapfs *mfs_in)
3047c478bd9Sstevel@tonic-gate {
3057c478bd9Sstevel@tonic-gate 	int s;
3067c478bd9Sstevel@tonic-gate 	struct mapfs *mfs, *p, *mfs_head = NULL, *mfs_tail = NULL;
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate 	struct netconfig *nconf;
3097c478bd9Sstevel@tonic-gate 	NCONF_HANDLE *nc = NULL;
3107c478bd9Sstevel@tonic-gate 	struct nd_hostserv hs;
3117c478bd9Sstevel@tonic-gate 	struct nd_addrlist *retaddrs;
3127c478bd9Sstevel@tonic-gate 	struct netbuf *nb;
3137c478bd9Sstevel@tonic-gate 	struct sioc_addrreq areq;
3147c478bd9Sstevel@tonic-gate 	int res;
3157c478bd9Sstevel@tonic-gate 	int af;
3167c478bd9Sstevel@tonic-gate 	int i;
3177c478bd9Sstevel@tonic-gate 	int sa_size;
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate 	hs.h_serv = "rpcbind";
3207c478bd9Sstevel@tonic-gate 
3217c478bd9Sstevel@tonic-gate 	for (mfs = mfs_in; mfs; mfs = mfs->mfs_next) {
3227c478bd9Sstevel@tonic-gate 		nc = setnetconfig();
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate 		while (nconf = getnetconfig(nc)) {
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 			/*
3277c478bd9Sstevel@tonic-gate 			 * Care about INET family only. proto_done flag
3287c478bd9Sstevel@tonic-gate 			 * indicates if we have already covered this
3297c478bd9Sstevel@tonic-gate 			 * protocol family. If so skip it
3307c478bd9Sstevel@tonic-gate 			 */
3317c478bd9Sstevel@tonic-gate 			if (((strcmp(nconf->nc_protofmly, NC_INET6) == 0) ||
3327c478bd9Sstevel@tonic-gate 			    (strcmp(nconf->nc_protofmly, NC_INET) == 0)) &&
3337c478bd9Sstevel@tonic-gate 			    (nconf->nc_semantics == NC_TPI_CLTS)) {
3347c478bd9Sstevel@tonic-gate 			} else
3357c478bd9Sstevel@tonic-gate 				continue;
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate 			hs.h_host = mfs->mfs_host;
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate 			if (netdir_getbyname(nconf, &hs, &retaddrs) != ND_OK)
3407c478bd9Sstevel@tonic-gate 				continue;
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate 			/*
3437c478bd9Sstevel@tonic-gate 			 * For each host address see if it's on our
3447c478bd9Sstevel@tonic-gate 			 * local subnet.
3457c478bd9Sstevel@tonic-gate 			 */
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate 			if (strcmp(nconf->nc_protofmly, NC_INET6) == 0)
3487c478bd9Sstevel@tonic-gate 				af = AF_INET6;
3497c478bd9Sstevel@tonic-gate 			else
3507c478bd9Sstevel@tonic-gate 				af = AF_INET;
3517c478bd9Sstevel@tonic-gate 			nb = retaddrs->n_addrs;
3527c478bd9Sstevel@tonic-gate 			for (i = 0; i < retaddrs->n_cnt; i++, nb++) {
3537c478bd9Sstevel@tonic-gate 				memset(&areq.sa_addr, 0, sizeof (areq.sa_addr));
3547c478bd9Sstevel@tonic-gate 				memcpy(&areq.sa_addr, nb->buf, MIN(nb->len,
3557c478bd9Sstevel@tonic-gate 				    sizeof (areq.sa_addr)));
3567c478bd9Sstevel@tonic-gate 				if (res = subnet_test(af, &areq)) {
3577c478bd9Sstevel@tonic-gate 					p = add_mfs(mfs, DIST_MYNET,
3587c478bd9Sstevel@tonic-gate 					    &mfs_head, &mfs_tail);
3597c478bd9Sstevel@tonic-gate 					if (!p) {
3607c478bd9Sstevel@tonic-gate 						netdir_free(retaddrs,
3617c478bd9Sstevel@tonic-gate 						    ND_ADDRLIST);
3627c478bd9Sstevel@tonic-gate 						endnetconfig(nc);
3637c478bd9Sstevel@tonic-gate 						return (NULL);
3647c478bd9Sstevel@tonic-gate 					}
3657c478bd9Sstevel@tonic-gate 					break;
3667c478bd9Sstevel@tonic-gate 				}
3677c478bd9Sstevel@tonic-gate 			}  /* end of every host */
3687c478bd9Sstevel@tonic-gate 			if (trace > 2) {
3697c478bd9Sstevel@tonic-gate 				trace_prt(1, "get_mysubnet_servers: host=%s "
3707c478bd9Sstevel@tonic-gate 				    "netid=%s res=%s\n", mfs->mfs_host,
3717c478bd9Sstevel@tonic-gate 				    nconf->nc_netid, res == 1?"SUC":"FAIL");
3727c478bd9Sstevel@tonic-gate 			}
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate 			netdir_free(retaddrs, ND_ADDRLIST);
3757c478bd9Sstevel@tonic-gate 		} /* end of while */
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate 		endnetconfig(nc);
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate 	} /* end of every map */
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate 	return (mfs_head);
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate }
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate int
subnet_test(int af,struct sioc_addrreq * areq)3867c478bd9Sstevel@tonic-gate subnet_test(int af, struct sioc_addrreq *areq)
3877c478bd9Sstevel@tonic-gate {
3887c478bd9Sstevel@tonic-gate 	int s;
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate 	if ((s = socket(af, SOCK_DGRAM, 0)) < 0) {
3917c478bd9Sstevel@tonic-gate 		return (0);
3927c478bd9Sstevel@tonic-gate 	}
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate 	areq->sa_res = -1;
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCTONLINK, (caddr_t)areq) < 0) {
3977c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "subnet_test:SIOCTONLINK failed");
3987c478bd9Sstevel@tonic-gate 		return (0);
3997c478bd9Sstevel@tonic-gate 	}
4007c478bd9Sstevel@tonic-gate 	close(s);
4017c478bd9Sstevel@tonic-gate 	if (areq->sa_res == 1)
4027c478bd9Sstevel@tonic-gate 		return (1);
4037c478bd9Sstevel@tonic-gate 	else
4047c478bd9Sstevel@tonic-gate 		return (0);
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate 
4077c478bd9Sstevel@tonic-gate }
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate /*
4107c478bd9Sstevel@tonic-gate  * ping a bunch of hosts at once and sort by who responds first
4117c478bd9Sstevel@tonic-gate  */
4127c478bd9Sstevel@tonic-gate static struct mapfs *
sort_servers(struct mapfs * mfs_in,int timeout)4137c478bd9Sstevel@tonic-gate sort_servers(struct mapfs *mfs_in, int timeout)
4147c478bd9Sstevel@tonic-gate {
4157c478bd9Sstevel@tonic-gate 	struct mapfs *m1 = NULL;
4167c478bd9Sstevel@tonic-gate 	enum clnt_stat clnt_stat;
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate 	if (!mfs_in)
4197c478bd9Sstevel@tonic-gate 		return (NULL);
4207c478bd9Sstevel@tonic-gate 
4217c478bd9Sstevel@tonic-gate 	clnt_stat = nfs_cast(mfs_in, &m1, timeout);
4227c478bd9Sstevel@tonic-gate 
4237c478bd9Sstevel@tonic-gate 	if (!m1) {
4247c478bd9Sstevel@tonic-gate 		char buff[2048] = {'\0'};
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate 		for (m1 = mfs_in; m1; m1 = m1->mfs_next) {
4277c478bd9Sstevel@tonic-gate 			(void) strcat(buff, m1->mfs_host);
4287c478bd9Sstevel@tonic-gate 			if (m1->mfs_next)
4297c478bd9Sstevel@tonic-gate 				(void) strcat(buff, ",");
4307c478bd9Sstevel@tonic-gate 		}
4317c478bd9Sstevel@tonic-gate 
4327c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "servers %s not responding: %s",
4337c478bd9Sstevel@tonic-gate 		    buff, clnt_sperrno(clnt_stat));
4347c478bd9Sstevel@tonic-gate 	}
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate 	return (m1);
4377c478bd9Sstevel@tonic-gate }
4387c478bd9Sstevel@tonic-gate 
4397c478bd9Sstevel@tonic-gate /*
4407c478bd9Sstevel@tonic-gate  * Add a mapfs entry to the list described by *mfs_head and *mfs_tail,
4417c478bd9Sstevel@tonic-gate  * provided it is not marked "ignored" and isn't a dupe of ones we've
4427c478bd9Sstevel@tonic-gate  * already seen.
4437c478bd9Sstevel@tonic-gate  */
4447c478bd9Sstevel@tonic-gate struct mapfs *
add_mfs(struct mapfs * mfs,int distance,struct mapfs ** mfs_head,struct mapfs ** mfs_tail)4457c478bd9Sstevel@tonic-gate add_mfs(struct mapfs *mfs, int distance, struct mapfs **mfs_head,
4467c478bd9Sstevel@tonic-gate 	struct mapfs **mfs_tail)
4477c478bd9Sstevel@tonic-gate {
4487c478bd9Sstevel@tonic-gate 	struct mapfs *tmp, *new;
4497c478bd9Sstevel@tonic-gate 
4507c478bd9Sstevel@tonic-gate 	for (tmp = *mfs_head; tmp; tmp = tmp->mfs_next)
4517c478bd9Sstevel@tonic-gate 		if ((strcmp(tmp->mfs_host, mfs->mfs_host) == 0 &&
4527c478bd9Sstevel@tonic-gate 		    strcmp(tmp->mfs_dir, mfs->mfs_dir) == 0) ||
4537c478bd9Sstevel@tonic-gate 		    mfs->mfs_ignore)
4547c478bd9Sstevel@tonic-gate 			return (*mfs_head);
4557c478bd9Sstevel@tonic-gate 	new = (struct mapfs *)malloc(sizeof (struct mapfs));
4567c478bd9Sstevel@tonic-gate 	if (!new) {
4577c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "Memory allocation failed: %m");
4587c478bd9Sstevel@tonic-gate 		return (NULL);
4597c478bd9Sstevel@tonic-gate 	}
4607c478bd9Sstevel@tonic-gate 	bcopy(mfs, new, sizeof (struct mapfs));
4617c478bd9Sstevel@tonic-gate 	new->mfs_next = NULL;
4627c478bd9Sstevel@tonic-gate 	if (distance)
4637c478bd9Sstevel@tonic-gate 		new->mfs_distance = distance;
4647c478bd9Sstevel@tonic-gate 	if (!*mfs_head)
4657c478bd9Sstevel@tonic-gate 		*mfs_tail = *mfs_head = new;
4667c478bd9Sstevel@tonic-gate 	else {
4677c478bd9Sstevel@tonic-gate 		(*mfs_tail)->mfs_next = new;
4687c478bd9Sstevel@tonic-gate 		*mfs_tail = new;
4697c478bd9Sstevel@tonic-gate 	}
4707c478bd9Sstevel@tonic-gate 	return (*mfs_head);
4717c478bd9Sstevel@tonic-gate }
4727c478bd9Sstevel@tonic-gate 
4737c478bd9Sstevel@tonic-gate static void
dump_mfs(struct mapfs * mfs,char * message,int level)4747c478bd9Sstevel@tonic-gate dump_mfs(struct mapfs *mfs, char *message, int level)
4757c478bd9Sstevel@tonic-gate {
4767c478bd9Sstevel@tonic-gate 	struct mapfs *m1;
4777c478bd9Sstevel@tonic-gate 
4787c478bd9Sstevel@tonic-gate 	if (trace <= level)
4797c478bd9Sstevel@tonic-gate 		return;
4807c478bd9Sstevel@tonic-gate 
4817c478bd9Sstevel@tonic-gate 	trace_prt(1, "%s", message);
4827c478bd9Sstevel@tonic-gate 	if (!mfs) {
4837c478bd9Sstevel@tonic-gate 		trace_prt(0, "mfs is null\n");
4847c478bd9Sstevel@tonic-gate 		return;
4857c478bd9Sstevel@tonic-gate 	}
4867c478bd9Sstevel@tonic-gate 	for (m1 = mfs; m1; m1 = m1->mfs_next)
4877c478bd9Sstevel@tonic-gate 		trace_prt(0, "%s[%s] ", m1->mfs_host, dump_distance(m1));
4887c478bd9Sstevel@tonic-gate 	trace_prt(0, "\n");
4897c478bd9Sstevel@tonic-gate }
4907c478bd9Sstevel@tonic-gate 
4917c478bd9Sstevel@tonic-gate static char *
dump_distance(struct mapfs * mfs)4927c478bd9Sstevel@tonic-gate dump_distance(struct mapfs *mfs)
4937c478bd9Sstevel@tonic-gate {
4947c478bd9Sstevel@tonic-gate 	switch (mfs->mfs_distance) {
4957c478bd9Sstevel@tonic-gate 	case 0:			return ("zero");
4967c478bd9Sstevel@tonic-gate 	case DIST_SELF:		return ("self");
4977c478bd9Sstevel@tonic-gate 	case DIST_MYSUB:	return ("mysub");
4987c478bd9Sstevel@tonic-gate 	case DIST_MYNET:	return ("mynet");
4997c478bd9Sstevel@tonic-gate 	case DIST_OTHER:	return ("other");
5007c478bd9Sstevel@tonic-gate 	default:		return ("other");
5017c478bd9Sstevel@tonic-gate 	}
5027c478bd9Sstevel@tonic-gate }
5037c478bd9Sstevel@tonic-gate 
5047c478bd9Sstevel@tonic-gate /*
5057c478bd9Sstevel@tonic-gate  * Walk linked list "raw", building a new list consisting of members
5067c478bd9Sstevel@tonic-gate  * NOT found in list "filter", returning the result.
5077c478bd9Sstevel@tonic-gate  */
5087c478bd9Sstevel@tonic-gate static struct mapfs *
filter_mfs(struct mapfs * raw,struct mapfs * filter)5097c478bd9Sstevel@tonic-gate filter_mfs(struct mapfs *raw, struct mapfs *filter)
5107c478bd9Sstevel@tonic-gate {
5117c478bd9Sstevel@tonic-gate 	struct mapfs *mfs, *p, *mfs_head = NULL, *mfs_tail = NULL;
5127c478bd9Sstevel@tonic-gate 	int skip;
5137c478bd9Sstevel@tonic-gate 
5147c478bd9Sstevel@tonic-gate 	if (!raw)
5157c478bd9Sstevel@tonic-gate 		return (NULL);
5167c478bd9Sstevel@tonic-gate 	for (mfs = raw; mfs; mfs = mfs->mfs_next) {
5177c478bd9Sstevel@tonic-gate 		for (skip = 0, p = filter; p; p = p->mfs_next) {
5187c478bd9Sstevel@tonic-gate 			if (strcmp(p->mfs_host, mfs->mfs_host) == 0 &&
5197c478bd9Sstevel@tonic-gate 			    strcmp(p->mfs_dir, mfs->mfs_dir) == 0) {
5207c478bd9Sstevel@tonic-gate 				skip = 1;
5217c478bd9Sstevel@tonic-gate 				break;
5227c478bd9Sstevel@tonic-gate 			}
5237c478bd9Sstevel@tonic-gate 		}
5247c478bd9Sstevel@tonic-gate 		if (skip)
5257c478bd9Sstevel@tonic-gate 			continue;
5267c478bd9Sstevel@tonic-gate 		p = add_mfs(mfs, 0, &mfs_head, &mfs_tail);
5277c478bd9Sstevel@tonic-gate 		if (!p)
5287c478bd9Sstevel@tonic-gate 			return (NULL);
5297c478bd9Sstevel@tonic-gate 	}
5307c478bd9Sstevel@tonic-gate 	return (mfs_head);
5317c478bd9Sstevel@tonic-gate }
5327c478bd9Sstevel@tonic-gate 
5337c478bd9Sstevel@tonic-gate /*
5347c478bd9Sstevel@tonic-gate  * Walk a linked list of mapfs structs, freeing each member.
5357c478bd9Sstevel@tonic-gate  */
5367c478bd9Sstevel@tonic-gate void
free_mfs(struct mapfs * mfs)5377c478bd9Sstevel@tonic-gate free_mfs(struct mapfs *mfs)
5387c478bd9Sstevel@tonic-gate {
5397c478bd9Sstevel@tonic-gate 	struct mapfs *tmp;
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate 	while (mfs) {
5427c478bd9Sstevel@tonic-gate 		tmp = mfs->mfs_next;
5437c478bd9Sstevel@tonic-gate 		free(mfs);
5447c478bd9Sstevel@tonic-gate 		mfs = tmp;
5457c478bd9Sstevel@tonic-gate 	}
5467c478bd9Sstevel@tonic-gate }
5477c478bd9Sstevel@tonic-gate 
5487c478bd9Sstevel@tonic-gate /*
5497c478bd9Sstevel@tonic-gate  * New code for NFS client failover: we need to carry and sort
5507c478bd9Sstevel@tonic-gate  * lists of server possibilities rather than return a single
5517c478bd9Sstevel@tonic-gate  * entry.  It preserves previous behaviour of sorting first by
5527c478bd9Sstevel@tonic-gate  * locality (loopback-or-preferred/subnet/net/other) and then
5537c478bd9Sstevel@tonic-gate  * by ping times.  We'll short-circuit this process when we
5547c478bd9Sstevel@tonic-gate  * have ENOUGH or more entries.
5557c478bd9Sstevel@tonic-gate  */
5567c478bd9Sstevel@tonic-gate static struct mapfs *
enum_servers(struct mapent * me,char * preferred)5577c478bd9Sstevel@tonic-gate enum_servers(struct mapent *me, char *preferred)
5587c478bd9Sstevel@tonic-gate {
5597c478bd9Sstevel@tonic-gate 	struct mapfs *p, *m1, *m2, *mfs_head = NULL, *mfs_tail = NULL;
5607c478bd9Sstevel@tonic-gate 
5617c478bd9Sstevel@tonic-gate 	/*
5627c478bd9Sstevel@tonic-gate 	 * Short-circuit for simple cases.
5637c478bd9Sstevel@tonic-gate 	 */
5647c478bd9Sstevel@tonic-gate 	if (!me->map_fs->mfs_next) {
5657c478bd9Sstevel@tonic-gate 		p = add_mfs(me->map_fs, DIST_OTHER, &mfs_head, &mfs_tail);
5667c478bd9Sstevel@tonic-gate 		if (!p)
5677c478bd9Sstevel@tonic-gate 			return (NULL);
5687c478bd9Sstevel@tonic-gate 		return (mfs_head);
5697c478bd9Sstevel@tonic-gate 	}
5707c478bd9Sstevel@tonic-gate 
5717c478bd9Sstevel@tonic-gate 	dump_mfs(me->map_fs, "	enum_servers: mapent: ", 2);
5727c478bd9Sstevel@tonic-gate 
5737c478bd9Sstevel@tonic-gate 	/*
5747c478bd9Sstevel@tonic-gate 	 * get addresses & see if any are myself
5757c478bd9Sstevel@tonic-gate 	 * or were mounted from previously in a
5767c478bd9Sstevel@tonic-gate 	 * hierarchical mount.
5777c478bd9Sstevel@tonic-gate 	 */
5787c478bd9Sstevel@tonic-gate 	if (trace > 2)
5797c478bd9Sstevel@tonic-gate 		trace_prt(1, "	enum_servers: looking for pref/self\n");
5807c478bd9Sstevel@tonic-gate 	for (m1 = me->map_fs; m1; m1 = m1->mfs_next) {
5817c478bd9Sstevel@tonic-gate 		if (m1->mfs_ignore)
5827c478bd9Sstevel@tonic-gate 			continue;
5837c478bd9Sstevel@tonic-gate 		if (self_check(m1->mfs_host) ||
5847c478bd9Sstevel@tonic-gate 		    strcmp(m1->mfs_host, preferred) == 0) {
5857c478bd9Sstevel@tonic-gate 			p = add_mfs(m1, DIST_SELF, &mfs_head, &mfs_tail);
5867c478bd9Sstevel@tonic-gate 			if (!p)
5877c478bd9Sstevel@tonic-gate 				return (NULL);
5887c478bd9Sstevel@tonic-gate 		}
5897c478bd9Sstevel@tonic-gate 	}
5907c478bd9Sstevel@tonic-gate 	if (trace > 2 && m1)
5917c478bd9Sstevel@tonic-gate 		trace_prt(1, "	enum_servers: pref/self found, %s\n",
5927c478bd9Sstevel@tonic-gate 		    m1->mfs_host);
5937c478bd9Sstevel@tonic-gate 
5947c478bd9Sstevel@tonic-gate 	/*
5957c478bd9Sstevel@tonic-gate 	 * look for entries on this subnet
5967c478bd9Sstevel@tonic-gate 	 */
5977c478bd9Sstevel@tonic-gate 	dump_mfs(m1, "	enum_servers: input of get_mysubnet_servers: ", 2);
5987c478bd9Sstevel@tonic-gate 	m1 = get_mysubnet_servers(me->map_fs);
5997c478bd9Sstevel@tonic-gate 	dump_mfs(m1, "	enum_servers: output of get_mysubnet_servers: ", 3);
6007c478bd9Sstevel@tonic-gate 	if (m1 && m1->mfs_next) {
6017c478bd9Sstevel@tonic-gate 		m2 = sort_servers(m1, rpc_timeout / 2);
6027c478bd9Sstevel@tonic-gate 		dump_mfs(m2, "	enum_servers: output of sort_servers: ", 3);
6037c478bd9Sstevel@tonic-gate 		free_mfs(m1);
6047c478bd9Sstevel@tonic-gate 		m1 = m2;
6057c478bd9Sstevel@tonic-gate 	}
6067c478bd9Sstevel@tonic-gate 
6077c478bd9Sstevel@tonic-gate 	for (m2 = m1; m2; m2 = m2->mfs_next) {
6087c478bd9Sstevel@tonic-gate 		p = add_mfs(m2, 0, &mfs_head, &mfs_tail);
6097c478bd9Sstevel@tonic-gate 		if (!p)
6107c478bd9Sstevel@tonic-gate 			return (NULL);
6117c478bd9Sstevel@tonic-gate 	}
6127c478bd9Sstevel@tonic-gate 	if (m1)
6137c478bd9Sstevel@tonic-gate 		free_mfs(m1);
6147c478bd9Sstevel@tonic-gate 
6157c478bd9Sstevel@tonic-gate 	/*
6167c478bd9Sstevel@tonic-gate 	 * add the rest of the entries at the end
6177c478bd9Sstevel@tonic-gate 	 */
6187c478bd9Sstevel@tonic-gate 	m1 = filter_mfs(me->map_fs, mfs_head);
6197c478bd9Sstevel@tonic-gate 	dump_mfs(m1, "	enum_servers: etc: output of filter_mfs: ", 3);
6207c478bd9Sstevel@tonic-gate 	m2 = sort_servers(m1, rpc_timeout / 2);
6217c478bd9Sstevel@tonic-gate 	dump_mfs(m2, "	enum_servers: etc: output of sort_servers: ", 3);
6227c478bd9Sstevel@tonic-gate 	if (m1)
6237c478bd9Sstevel@tonic-gate 		free_mfs(m1);
6247c478bd9Sstevel@tonic-gate 	m1 = m2;
6257c478bd9Sstevel@tonic-gate 	for (m2 = m1; m2; m2 = m2->mfs_next) {
6267c478bd9Sstevel@tonic-gate 		p = add_mfs(m2, DIST_OTHER, &mfs_head, &mfs_tail);
6277c478bd9Sstevel@tonic-gate 		if (!p)
6287c478bd9Sstevel@tonic-gate 			return (NULL);
6297c478bd9Sstevel@tonic-gate 	}
6307c478bd9Sstevel@tonic-gate 	if (m1)
6317c478bd9Sstevel@tonic-gate 		free_mfs(m1);
6327c478bd9Sstevel@tonic-gate 
6337c478bd9Sstevel@tonic-gate done:
6347c478bd9Sstevel@tonic-gate 	dump_mfs(mfs_head, "  enum_servers: output: ", 1);
6357c478bd9Sstevel@tonic-gate 	return (mfs_head);
6367c478bd9Sstevel@tonic-gate }
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate static enum nfsstat
nfsmount(struct mapfs * mfs_in,char * mntpnt,char * opts,int cached,int overlay,uid_t uid,action_list * alp)63939d3e169Sevanl nfsmount(
64039d3e169Sevanl 	struct mapfs *mfs_in,
64139d3e169Sevanl 	char *mntpnt, char *opts,
64239d3e169Sevanl 	int cached, int overlay,
6433bfb48feSsemery 	uid_t uid,
64439d3e169Sevanl 	action_list *alp)
6457c478bd9Sstevel@tonic-gate {
6467c478bd9Sstevel@tonic-gate 	CLIENT *cl;
6477c478bd9Sstevel@tonic-gate 	char remname[MAXPATHLEN], *mnttabtext = NULL;
6487c478bd9Sstevel@tonic-gate 	char mopts[MAX_MNTOPT_STR];
6497c478bd9Sstevel@tonic-gate 	char netname[MAXNETNAMELEN+1];
65039d3e169Sevanl 	char	*mntopts = NULL;
6517c478bd9Sstevel@tonic-gate 	int mnttabcnt = 0;
6527c478bd9Sstevel@tonic-gate 	int loglevel;
6537c478bd9Sstevel@tonic-gate 	struct mnttab m;
6547c478bd9Sstevel@tonic-gate 	struct nfs_args *argp = NULL, *head = NULL, *tail = NULL,
6557c478bd9Sstevel@tonic-gate 	    *prevhead, *prevtail;
6567c478bd9Sstevel@tonic-gate 	int flags;
6577c478bd9Sstevel@tonic-gate 	struct fhstatus fhs;
6587c478bd9Sstevel@tonic-gate 	struct timeval timeout;
6597c478bd9Sstevel@tonic-gate 	enum clnt_stat rpc_stat;
6607c478bd9Sstevel@tonic-gate 	enum nfsstat status;
6617c478bd9Sstevel@tonic-gate 	struct stat stbuf;
6627c478bd9Sstevel@tonic-gate 	struct netconfig *nconf;
6637c478bd9Sstevel@tonic-gate 	rpcvers_t vers, versmin; /* used to negotiate nfs version in pingnfs */
6647c478bd9Sstevel@tonic-gate 				/* and mount version with mountd */
6657c478bd9Sstevel@tonic-gate 	rpcvers_t outvers;	/* final version to be used during mount() */
6667c478bd9Sstevel@tonic-gate 	rpcvers_t nfsvers;	/* version in map options, 0 if not there */
6677c478bd9Sstevel@tonic-gate 	rpcvers_t mountversmax;	/* tracks the max mountvers during retries */
6687c478bd9Sstevel@tonic-gate 
6697c478bd9Sstevel@tonic-gate 	/* used to negotiate nfs version using webnfs */
6707c478bd9Sstevel@tonic-gate 	rpcvers_t pubvers, pubversmin, pubversmax;
6717c478bd9Sstevel@tonic-gate 	int posix;
6727c478bd9Sstevel@tonic-gate 	struct nd_addrlist *retaddrs;
6737c478bd9Sstevel@tonic-gate 	struct mountres3 res3;
6747c478bd9Sstevel@tonic-gate 	nfs_fh3 fh3;
6757c478bd9Sstevel@tonic-gate 	char *fstype;
6767c478bd9Sstevel@tonic-gate 	int count, i;
6777c478bd9Sstevel@tonic-gate 	char scerror_msg[MAXMSGLEN];
6787c478bd9Sstevel@tonic-gate 	int *auths;
6797c478bd9Sstevel@tonic-gate 	int delay;
6807c478bd9Sstevel@tonic-gate 	int retries;
6817c478bd9Sstevel@tonic-gate 	char *nfs_proto = NULL;
6827c478bd9Sstevel@tonic-gate 	uint_t nfs_port = 0;
6836a6d3e5eSjs195444 	char *p, *host, *rhost, *dir;
6847c478bd9Sstevel@tonic-gate 	struct mapfs *mfs = NULL;
6857c478bd9Sstevel@tonic-gate 	int error, last_error = 0;
6867c478bd9Sstevel@tonic-gate 	int replicated;
6877c478bd9Sstevel@tonic-gate 	int entries = 0;
6887c478bd9Sstevel@tonic-gate 	int v2cnt = 0, v3cnt = 0, v4cnt = 0;
6897c478bd9Sstevel@tonic-gate 	int v2near = 0, v3near = 0, v4near = 0;
6907c478bd9Sstevel@tonic-gate 	int skipentry = 0;
6917c478bd9Sstevel@tonic-gate 	char *nfs_flavor;
6927c478bd9Sstevel@tonic-gate 	seconfig_t nfs_sec;
6937c478bd9Sstevel@tonic-gate 	int sec_opt, scerror;
6947c478bd9Sstevel@tonic-gate 	struct sec_data *secdata;
6957c478bd9Sstevel@tonic-gate 	int secflags;
6967c478bd9Sstevel@tonic-gate 	struct netbuf *syncaddr;
6977c478bd9Sstevel@tonic-gate 	bool_t	use_pubfh;
6987c478bd9Sstevel@tonic-gate 	ushort_t thisport;
6997c478bd9Sstevel@tonic-gate 	int got_val;
7007c478bd9Sstevel@tonic-gate 	mfs_snego_t mfssnego_init, mfssnego;
7017c478bd9Sstevel@tonic-gate 
7027c478bd9Sstevel@tonic-gate 	dump_mfs(mfs_in, "  nfsmount: input: ", 2);
7037c478bd9Sstevel@tonic-gate 	replicated = (mfs_in->mfs_next != NULL);
7047c478bd9Sstevel@tonic-gate 	m.mnt_mntopts = opts;
7057c478bd9Sstevel@tonic-gate 	if (replicated && hasmntopt(&m, MNTOPT_SOFT)) {
7067c478bd9Sstevel@tonic-gate 		if (verbose)
7077c478bd9Sstevel@tonic-gate 			syslog(LOG_WARNING,
7087c478bd9Sstevel@tonic-gate 		    "mount on %s is soft and will not be replicated.", mntpnt);
7097c478bd9Sstevel@tonic-gate 		replicated = 0;
7107c478bd9Sstevel@tonic-gate 	}
7117c478bd9Sstevel@tonic-gate 	if (replicated && !hasmntopt(&m, MNTOPT_RO)) {
7127c478bd9Sstevel@tonic-gate 		if (verbose)
7137c478bd9Sstevel@tonic-gate 			syslog(LOG_WARNING,
7147c478bd9Sstevel@tonic-gate 		    "mount on %s is not read-only and will not be replicated.",
7157c478bd9Sstevel@tonic-gate 			    mntpnt);
7167c478bd9Sstevel@tonic-gate 		replicated = 0;
7177c478bd9Sstevel@tonic-gate 	}
7187c478bd9Sstevel@tonic-gate 	if (replicated && cached) {
7197c478bd9Sstevel@tonic-gate 		if (verbose)
7207c478bd9Sstevel@tonic-gate 			syslog(LOG_WARNING,
7217c478bd9Sstevel@tonic-gate 		    "mount on %s is cached and will not be replicated.",
7227c478bd9Sstevel@tonic-gate 			    mntpnt);
7237c478bd9Sstevel@tonic-gate 		replicated = 0;
7247c478bd9Sstevel@tonic-gate 	}
7257c478bd9Sstevel@tonic-gate 	if (replicated)
7267c478bd9Sstevel@tonic-gate 		loglevel = LOG_WARNING;
7277c478bd9Sstevel@tonic-gate 	else
7287c478bd9Sstevel@tonic-gate 		loglevel = LOG_ERR;
7297c478bd9Sstevel@tonic-gate 
7307c478bd9Sstevel@tonic-gate 	if (trace > 1) {
7317c478bd9Sstevel@tonic-gate 		if (replicated)
7327c478bd9Sstevel@tonic-gate 			trace_prt(1, "	nfsmount: replicated mount on %s %s:\n",
7337c478bd9Sstevel@tonic-gate 			    mntpnt, opts);
7347c478bd9Sstevel@tonic-gate 		else
7357c478bd9Sstevel@tonic-gate 			trace_prt(1, "	nfsmount: standard mount on %s %s:\n",
7367c478bd9Sstevel@tonic-gate 			    mntpnt, opts);
7377c478bd9Sstevel@tonic-gate 		for (mfs = mfs_in; mfs; mfs = mfs->mfs_next)
7387c478bd9Sstevel@tonic-gate 			trace_prt(1, "	  %s:%s\n",
7397c478bd9Sstevel@tonic-gate 			    mfs->mfs_host, mfs->mfs_dir);
7407c478bd9Sstevel@tonic-gate 	}
7417c478bd9Sstevel@tonic-gate 
7427c478bd9Sstevel@tonic-gate 	/*
7437c478bd9Sstevel@tonic-gate 	 * Make sure mountpoint is safe to mount on
7447c478bd9Sstevel@tonic-gate 	 */
7457c478bd9Sstevel@tonic-gate 	if (lstat(mntpnt, &stbuf) < 0) {
7467c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "Couldn't stat %s: %m", mntpnt);
7477c478bd9Sstevel@tonic-gate 		return (NFSERR_NOENT);
7487c478bd9Sstevel@tonic-gate 	}
7497c478bd9Sstevel@tonic-gate 
7507c478bd9Sstevel@tonic-gate 	/*
7517c478bd9Sstevel@tonic-gate 	 * Get protocol specified in options list, if any.
7527c478bd9Sstevel@tonic-gate 	 */
7537c478bd9Sstevel@tonic-gate 	if ((str_opt(&m, "proto", &nfs_proto)) == -1) {
7547c478bd9Sstevel@tonic-gate 		return (NFSERR_NOENT);
7557c478bd9Sstevel@tonic-gate 	}
7567c478bd9Sstevel@tonic-gate 
7577c478bd9Sstevel@tonic-gate 	/*
7587c478bd9Sstevel@tonic-gate 	 * Get port specified in options list, if any.
7597c478bd9Sstevel@tonic-gate 	 */
7607c478bd9Sstevel@tonic-gate 	got_val = nopt(&m, MNTOPT_PORT, (int *)&nfs_port);
7617c478bd9Sstevel@tonic-gate 	if (!got_val)
7627c478bd9Sstevel@tonic-gate 		nfs_port = 0;	/* "unspecified" */
7637c478bd9Sstevel@tonic-gate 	if (nfs_port > USHRT_MAX) {
7647c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "%s: invalid port number %d", mntpnt, nfs_port);
7657c478bd9Sstevel@tonic-gate 		return (NFSERR_NOENT);
7667c478bd9Sstevel@tonic-gate 	}
7677c478bd9Sstevel@tonic-gate 
7687c478bd9Sstevel@tonic-gate 	/*
7697c478bd9Sstevel@tonic-gate 	 * Set mount(2) flags here, outside of the loop.
7707c478bd9Sstevel@tonic-gate 	 */
7717c478bd9Sstevel@tonic-gate 	flags = MS_OPTIONSTR;
7727c478bd9Sstevel@tonic-gate 	flags |= (hasmntopt(&m, MNTOPT_RO) == NULL) ? 0 : MS_RDONLY;
7737c478bd9Sstevel@tonic-gate 	flags |= (hasmntopt(&m, MNTOPT_NOSUID) == NULL) ? 0 : MS_NOSUID;
7747c478bd9Sstevel@tonic-gate 	flags |= overlay ? MS_OVERLAY : 0;
7757c478bd9Sstevel@tonic-gate 	if (mntpnt[strlen(mntpnt) - 1] != ' ')
7767c478bd9Sstevel@tonic-gate 		/* direct mount point without offsets */
7777c478bd9Sstevel@tonic-gate 		flags |= MS_OVERLAY;
7787c478bd9Sstevel@tonic-gate 
7797c478bd9Sstevel@tonic-gate 	use_pubfh = (hasmntopt(&m, MNTOPT_PUBLIC) == NULL) ? FALSE : TRUE;
7807c478bd9Sstevel@tonic-gate 
7817c478bd9Sstevel@tonic-gate 	(void) memset(&mfssnego_init, 0, sizeof (mfs_snego_t));
7827c478bd9Sstevel@tonic-gate 	if (hasmntopt(&m, MNTOPT_SECURE) != NULL) {
7837c478bd9Sstevel@tonic-gate 		if (++mfssnego_init.sec_opt > 1) {
7847c478bd9Sstevel@tonic-gate 			syslog(loglevel,
7857c478bd9Sstevel@tonic-gate 			    "conflicting security options");
7867c478bd9Sstevel@tonic-gate 			return (NFSERR_IO);
7877c478bd9Sstevel@tonic-gate 		}
7887c478bd9Sstevel@tonic-gate 		if (nfs_getseconfig_byname("dh", &mfssnego_init.nfs_sec)) {
7897c478bd9Sstevel@tonic-gate 			syslog(loglevel,
7907c478bd9Sstevel@tonic-gate 			    "error getting dh information from %s",
7917c478bd9Sstevel@tonic-gate 			    NFSSEC_CONF);
7927c478bd9Sstevel@tonic-gate 			return (NFSERR_IO);
7937c478bd9Sstevel@tonic-gate 		}
7947c478bd9Sstevel@tonic-gate 	}
7957c478bd9Sstevel@tonic-gate 
796d7c6abe8Sjasmith 	if (hasmntopt(&m, MNTOPT_SEC) != NULL) {
7977c478bd9Sstevel@tonic-gate 		if ((str_opt(&m, MNTOPT_SEC,
7987c478bd9Sstevel@tonic-gate 		    &mfssnego_init.nfs_flavor)) == -1) {
7997c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "nfsmount: no memory");
8007c478bd9Sstevel@tonic-gate 			return (NFSERR_IO);
8017c478bd9Sstevel@tonic-gate 		}
8027c478bd9Sstevel@tonic-gate 	}
8037c478bd9Sstevel@tonic-gate 
8047c478bd9Sstevel@tonic-gate 	if (mfssnego_init.nfs_flavor) {
8057c478bd9Sstevel@tonic-gate 		if (++mfssnego_init.sec_opt > 1) {
8067c478bd9Sstevel@tonic-gate 			syslog(loglevel,
8077c478bd9Sstevel@tonic-gate 			    "conflicting security options");
8087c478bd9Sstevel@tonic-gate 			free(mfssnego_init.nfs_flavor);
8097c478bd9Sstevel@tonic-gate 			return (NFSERR_IO);
8107c478bd9Sstevel@tonic-gate 		}
8117c478bd9Sstevel@tonic-gate 		if (nfs_getseconfig_byname(mfssnego_init.nfs_flavor,
8127c478bd9Sstevel@tonic-gate 		    &mfssnego_init.nfs_sec)) {
8137c478bd9Sstevel@tonic-gate 			syslog(loglevel,
8147c478bd9Sstevel@tonic-gate 			    "error getting %s information from %s",
8157c478bd9Sstevel@tonic-gate 			    mfssnego_init.nfs_flavor, NFSSEC_CONF);
8167c478bd9Sstevel@tonic-gate 			free(mfssnego_init.nfs_flavor);
8177c478bd9Sstevel@tonic-gate 			return (NFSERR_IO);
8187c478bd9Sstevel@tonic-gate 		}
8197c478bd9Sstevel@tonic-gate 		free(mfssnego_init.nfs_flavor);
8207c478bd9Sstevel@tonic-gate 	}
8217c478bd9Sstevel@tonic-gate 
8227c478bd9Sstevel@tonic-gate nextentry:
8237c478bd9Sstevel@tonic-gate 	skipentry = 0;
8247c478bd9Sstevel@tonic-gate 
8257c478bd9Sstevel@tonic-gate 	got_val = nopt(&m, MNTOPT_VERS, (int *)&nfsvers);
8267c478bd9Sstevel@tonic-gate 	if (!got_val)
8277c478bd9Sstevel@tonic-gate 		nfsvers = 0;	/* "unspecified" */
8287c478bd9Sstevel@tonic-gate 	if (set_versrange(nfsvers, &vers, &versmin) != 0) {
8297c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "Incorrect NFS version specified for %s",
8307c478bd9Sstevel@tonic-gate 		    mntpnt);
8317c478bd9Sstevel@tonic-gate 		last_error = NFSERR_NOENT;
8327c478bd9Sstevel@tonic-gate 		goto ret;
8337c478bd9Sstevel@tonic-gate 	}
8347c478bd9Sstevel@tonic-gate 
8357c478bd9Sstevel@tonic-gate 	if (nfsvers != 0) {
8367c478bd9Sstevel@tonic-gate 		pubversmax = pubversmin = nfsvers;
8377c478bd9Sstevel@tonic-gate 	} else {
8387c478bd9Sstevel@tonic-gate 		pubversmax = vers;
8397c478bd9Sstevel@tonic-gate 		pubversmin = versmin;
8407c478bd9Sstevel@tonic-gate 	}
8417c478bd9Sstevel@tonic-gate 
8427c478bd9Sstevel@tonic-gate 	/*
8437c478bd9Sstevel@tonic-gate 	 * Walk the whole list, pinging and collecting version
8447c478bd9Sstevel@tonic-gate 	 * info so that we can make sure the mount will be
8457c478bd9Sstevel@tonic-gate 	 * homogeneous with respect to version.
8467c478bd9Sstevel@tonic-gate 	 *
8477c478bd9Sstevel@tonic-gate 	 * If we have a version preference, this is easy; we'll
8487c478bd9Sstevel@tonic-gate 	 * just reject anything that doesn't match.
8497c478bd9Sstevel@tonic-gate 	 *
8507c478bd9Sstevel@tonic-gate 	 * If not, we want to try to provide the best compromise
8517c478bd9Sstevel@tonic-gate 	 * that considers proximity, preference for a higher version,
8527c478bd9Sstevel@tonic-gate 	 * sorted order, and number of replicas.  We will count
8537c478bd9Sstevel@tonic-gate 	 * the number of V2 and V3 replicas and also the number
8547c478bd9Sstevel@tonic-gate 	 * which are "near", i.e. the localhost or on the same
8557c478bd9Sstevel@tonic-gate 	 * subnet.
8567c478bd9Sstevel@tonic-gate 	 */
8577c478bd9Sstevel@tonic-gate 	for (mfs = mfs_in; mfs; mfs = mfs->mfs_next) {
8587c478bd9Sstevel@tonic-gate 
8597c478bd9Sstevel@tonic-gate 
8607c478bd9Sstevel@tonic-gate 		if (mfs->mfs_ignore)
8617c478bd9Sstevel@tonic-gate 			continue;
8627c478bd9Sstevel@tonic-gate 
8636a6d3e5eSjs195444 		/*
8646a6d3e5eSjs195444 		 * If the host is '[a:d:d:r:e:s:s'],
8656a6d3e5eSjs195444 		 * only use 'a:d:d:r:e:s:s' for communication
8666a6d3e5eSjs195444 		 */
8676a6d3e5eSjs195444 		host = strdup(mfs->mfs_host);
8686a6d3e5eSjs195444 		if (host == NULL) {
8696a6d3e5eSjs195444 			syslog(LOG_ERR, "nfsmount: no memory");
8706a6d3e5eSjs195444 			last_error = NFSERR_IO;
8716a6d3e5eSjs195444 			goto out;
8726a6d3e5eSjs195444 		}
8736a6d3e5eSjs195444 		unbracket(&host);
8746a6d3e5eSjs195444 
8757c478bd9Sstevel@tonic-gate 		(void) memcpy(&mfssnego, &mfssnego_init, sizeof (mfs_snego_t));
8767c478bd9Sstevel@tonic-gate 
8777c478bd9Sstevel@tonic-gate 		if (use_pubfh == TRUE || mfs->mfs_flags & MFS_URL) {
8787c478bd9Sstevel@tonic-gate 			char *path;
8797c478bd9Sstevel@tonic-gate 
8807c478bd9Sstevel@tonic-gate 			if (nfs_port != 0 && mfs->mfs_port != 0 &&
8817c478bd9Sstevel@tonic-gate 			    nfs_port != mfs->mfs_port) {
8827c478bd9Sstevel@tonic-gate 
8837c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR, "nfsmount: port (%u) in nfs URL"
8847c478bd9Sstevel@tonic-gate 				    " not the same as port (%d) in port "
8857c478bd9Sstevel@tonic-gate 				    "option\n", mfs->mfs_port, nfs_port);
8867c478bd9Sstevel@tonic-gate 				last_error = NFSERR_IO;
8877c478bd9Sstevel@tonic-gate 				goto out;
8887c478bd9Sstevel@tonic-gate 
8897c478bd9Sstevel@tonic-gate 			} else if (nfs_port != 0)
8907c478bd9Sstevel@tonic-gate 				thisport = nfs_port;
8917c478bd9Sstevel@tonic-gate 			else
8927c478bd9Sstevel@tonic-gate 				thisport = mfs->mfs_port;
8937c478bd9Sstevel@tonic-gate 
8947c478bd9Sstevel@tonic-gate 			dir = mfs->mfs_dir;
8957c478bd9Sstevel@tonic-gate 
8967c478bd9Sstevel@tonic-gate 			if ((mfs->mfs_flags & MFS_URL) == 0) {
8977c478bd9Sstevel@tonic-gate 				path = malloc(strlen(dir) + 2);
8987c478bd9Sstevel@tonic-gate 				if (path == NULL) {
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 				path[0] = (char)WNL_NATIVEPATH;
9047c478bd9Sstevel@tonic-gate 				(void) strcpy(&path[1], dir);
9057c478bd9Sstevel@tonic-gate 			} else {
9067c478bd9Sstevel@tonic-gate 				path = dir;
9077c478bd9Sstevel@tonic-gate 			}
9087c478bd9Sstevel@tonic-gate 
9097c478bd9Sstevel@tonic-gate 			argp = (struct nfs_args *)
9107c478bd9Sstevel@tonic-gate 			    malloc(sizeof (struct nfs_args));
9117c478bd9Sstevel@tonic-gate 
9127c478bd9Sstevel@tonic-gate 			if (!argp) {
9137c478bd9Sstevel@tonic-gate 				if (path != dir)
9147c478bd9Sstevel@tonic-gate 					free(path);
9157c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR, "nfsmount: no memory");
9167c478bd9Sstevel@tonic-gate 				last_error = NFSERR_IO;
9177c478bd9Sstevel@tonic-gate 				goto out;
9187c478bd9Sstevel@tonic-gate 			}
9197c478bd9Sstevel@tonic-gate 			(void) memset(argp, 0, sizeof (*argp));
9207c478bd9Sstevel@tonic-gate 
9217c478bd9Sstevel@tonic-gate 			/*
9227c478bd9Sstevel@tonic-gate 			 * RDMA support
9237c478bd9Sstevel@tonic-gate 			 * By now Mount argument struct has been allocated,
9247c478bd9Sstevel@tonic-gate 			 * either a pub_fh path will be taken or the regular
9257c478bd9Sstevel@tonic-gate 			 * one. So here if a protocol was specified and it
9267c478bd9Sstevel@tonic-gate 			 * was not rdma we let it be, else we set DO_RDMA.
9277c478bd9Sstevel@tonic-gate 			 * If no proto was there we advise on trying RDMA.
9287c478bd9Sstevel@tonic-gate 			 */
9297c478bd9Sstevel@tonic-gate 			if (nfs_proto) {
9307c478bd9Sstevel@tonic-gate 				if (strcmp(nfs_proto, "rdma") == 0) {
9317c478bd9Sstevel@tonic-gate 					free(nfs_proto);
9327c478bd9Sstevel@tonic-gate 					nfs_proto = NULL;
9337c478bd9Sstevel@tonic-gate 					argp->flags |= NFSMNT_DORDMA;
9347c478bd9Sstevel@tonic-gate 				}
9357c478bd9Sstevel@tonic-gate 			} else
9367c478bd9Sstevel@tonic-gate 				argp->flags |= NFSMNT_TRYRDMA;
9377c478bd9Sstevel@tonic-gate 
9387c478bd9Sstevel@tonic-gate 			for (pubvers = pubversmax; pubvers >= pubversmin;
9397c478bd9Sstevel@tonic-gate 			    pubvers--) {
9407c478bd9Sstevel@tonic-gate 
9417c478bd9Sstevel@tonic-gate 				nconf = NULL;
9427c478bd9Sstevel@tonic-gate 				argp->addr = get_pubfh(host, pubvers, &mfssnego,
9437c478bd9Sstevel@tonic-gate 				    &nconf, nfs_proto, thisport, NULL,
9447c478bd9Sstevel@tonic-gate 				    &argp->fh, TRUE, path);
9457c478bd9Sstevel@tonic-gate 
9467c478bd9Sstevel@tonic-gate 				if (argp->addr != NULL)
9477c478bd9Sstevel@tonic-gate 					break;
9487c478bd9Sstevel@tonic-gate 
9497c478bd9Sstevel@tonic-gate 				if (nconf != NULL)
9507c478bd9Sstevel@tonic-gate 					freenetconfigent(nconf);
9517c478bd9Sstevel@tonic-gate 			}
9527c478bd9Sstevel@tonic-gate 
9537c478bd9Sstevel@tonic-gate 			if (path != dir)
9547c478bd9Sstevel@tonic-gate 				free(path);
9557c478bd9Sstevel@tonic-gate 
9567c478bd9Sstevel@tonic-gate 			if (argp->addr != NULL) {
9577c478bd9Sstevel@tonic-gate 
9587c478bd9Sstevel@tonic-gate 				/*
9597c478bd9Sstevel@tonic-gate 				 * The use of llock option for NFSv4
9607c478bd9Sstevel@tonic-gate 				 * mounts is not required since file
9617c478bd9Sstevel@tonic-gate 				 * locking is included within the protocol
9627c478bd9Sstevel@tonic-gate 				 */
9637c478bd9Sstevel@tonic-gate 				if (pubvers != NFS_V4)
9647c478bd9Sstevel@tonic-gate 					argp->flags |= NFSMNT_LLOCK;
9657c478bd9Sstevel@tonic-gate 
9667c478bd9Sstevel@tonic-gate 				argp->flags |= NFSMNT_PUBLIC;
9677c478bd9Sstevel@tonic-gate 
9681bbc88acSrm15945 				vers = pubvers;
9697c478bd9Sstevel@tonic-gate 				mfs->mfs_args = argp;
9707c478bd9Sstevel@tonic-gate 				mfs->mfs_version = pubvers;
9717c478bd9Sstevel@tonic-gate 				mfs->mfs_nconf = nconf;
9727c478bd9Sstevel@tonic-gate 				mfs->mfs_flags |= MFS_FH_VIA_WEBNFS;
9737c478bd9Sstevel@tonic-gate 
9747c478bd9Sstevel@tonic-gate 			} else {
9757c478bd9Sstevel@tonic-gate 				free(argp);
9767c478bd9Sstevel@tonic-gate 
9777c478bd9Sstevel@tonic-gate 				/*
9787c478bd9Sstevel@tonic-gate 				 * If -public was specified, give up
9797c478bd9Sstevel@tonic-gate 				 * on this entry now.
9807c478bd9Sstevel@tonic-gate 				 */
9817c478bd9Sstevel@tonic-gate 				if (use_pubfh == TRUE) {
9827c478bd9Sstevel@tonic-gate 					syslog(loglevel,
9837c478bd9Sstevel@tonic-gate 					    "%s: no public file handle support",
9847c478bd9Sstevel@tonic-gate 					    host);
9857c478bd9Sstevel@tonic-gate 					last_error = NFSERR_NOENT;
9867c478bd9Sstevel@tonic-gate 					mfs->mfs_ignore = 1;
9877c478bd9Sstevel@tonic-gate 					continue;
9887c478bd9Sstevel@tonic-gate 				}
9897c478bd9Sstevel@tonic-gate 
9907c478bd9Sstevel@tonic-gate 				/*
9917c478bd9Sstevel@tonic-gate 				 * Back off to a conventional mount.
9927c478bd9Sstevel@tonic-gate 				 *
9937c478bd9Sstevel@tonic-gate 				 * URL's can contain escape characters. Get
9947c478bd9Sstevel@tonic-gate 				 * rid of them.
9957c478bd9Sstevel@tonic-gate 				 */
9967c478bd9Sstevel@tonic-gate 				path = malloc(strlen(dir) + 2);
9977c478bd9Sstevel@tonic-gate 
9987c478bd9Sstevel@tonic-gate 				if (path == NULL) {
9997c478bd9Sstevel@tonic-gate 					syslog(LOG_ERR, "nfsmount: no memory");
10007c478bd9Sstevel@tonic-gate 					last_error = NFSERR_IO;
10017c478bd9Sstevel@tonic-gate 					goto out;
10027c478bd9Sstevel@tonic-gate 				}
10037c478bd9Sstevel@tonic-gate 
10047c478bd9Sstevel@tonic-gate 				strcpy(path, dir);
10057c478bd9Sstevel@tonic-gate 				URLparse(path);
10067c478bd9Sstevel@tonic-gate 				mfs->mfs_dir = path;
10077c478bd9Sstevel@tonic-gate 				mfs->mfs_flags |= MFS_ALLOC_DIR;
10087c478bd9Sstevel@tonic-gate 				mfs->mfs_flags &= ~MFS_URL;
10097c478bd9Sstevel@tonic-gate 			}
10107c478bd9Sstevel@tonic-gate 		}
10117c478bd9Sstevel@tonic-gate 
10127c478bd9Sstevel@tonic-gate 		if ((mfs->mfs_flags & MFS_FH_VIA_WEBNFS) ==  0) {
10137c478bd9Sstevel@tonic-gate 			i = pingnfs(host, get_retry(opts) + 1, &vers, versmin,
10147c478bd9Sstevel@tonic-gate 			    0, FALSE, NULL, nfs_proto);
10157c478bd9Sstevel@tonic-gate 			if (i != RPC_SUCCESS) {
10167c478bd9Sstevel@tonic-gate 				if (i == RPC_PROGVERSMISMATCH) {
10177c478bd9Sstevel@tonic-gate 					syslog(loglevel, "server %s: NFS "
10187c478bd9Sstevel@tonic-gate 					    "protocol version mismatch",
10197c478bd9Sstevel@tonic-gate 					    host);
10207c478bd9Sstevel@tonic-gate 				} else {
10217c478bd9Sstevel@tonic-gate 					syslog(loglevel, "server %s not "
10227c478bd9Sstevel@tonic-gate 					    "responding", host);
10237c478bd9Sstevel@tonic-gate 				}
10247c478bd9Sstevel@tonic-gate 				mfs->mfs_ignore = 1;
10257c478bd9Sstevel@tonic-gate 				last_error = NFSERR_NOENT;
10267c478bd9Sstevel@tonic-gate 				continue;
10277c478bd9Sstevel@tonic-gate 			}
10287c478bd9Sstevel@tonic-gate 			if (nfsvers != 0 && nfsvers != vers) {
10297c478bd9Sstevel@tonic-gate 				if (nfs_proto == NULL)
10307c478bd9Sstevel@tonic-gate 					syslog(loglevel,
10317c478bd9Sstevel@tonic-gate 					    "NFS version %d "
10327c478bd9Sstevel@tonic-gate 					    "not supported by %s",
10337c478bd9Sstevel@tonic-gate 					    nfsvers, host);
10347c478bd9Sstevel@tonic-gate 				else
10357c478bd9Sstevel@tonic-gate 					syslog(loglevel,
10367c478bd9Sstevel@tonic-gate 					    "NFS version %d "
10377c478bd9Sstevel@tonic-gate 					    "with proto %s "
10387c478bd9Sstevel@tonic-gate 					    "not supported by %s",
10397c478bd9Sstevel@tonic-gate 					    nfsvers, nfs_proto, host);
10407c478bd9Sstevel@tonic-gate 				mfs->mfs_ignore = 1;
10417c478bd9Sstevel@tonic-gate 				last_error = NFSERR_NOENT;
10427c478bd9Sstevel@tonic-gate 				continue;
10437c478bd9Sstevel@tonic-gate 			}
10447c478bd9Sstevel@tonic-gate 		}
10457c478bd9Sstevel@tonic-gate 
10466a6d3e5eSjs195444 		free(host);
10476a6d3e5eSjs195444 
10487c478bd9Sstevel@tonic-gate 		switch (vers) {
10497c478bd9Sstevel@tonic-gate 		case NFS_V4: v4cnt++; break;
10507c478bd9Sstevel@tonic-gate 		case NFS_V3: v3cnt++; break;
10517c478bd9Sstevel@tonic-gate 		case NFS_VERSION: v2cnt++; break;
10527c478bd9Sstevel@tonic-gate 		default: break;
10537c478bd9Sstevel@tonic-gate 		}
10547c478bd9Sstevel@tonic-gate 
10557c478bd9Sstevel@tonic-gate 		/*
10567c478bd9Sstevel@tonic-gate 		 * It's not clear how useful this stuff is if
10577c478bd9Sstevel@tonic-gate 		 * we are using webnfs across the internet, but it
10587c478bd9Sstevel@tonic-gate 		 * can't hurt.
10597c478bd9Sstevel@tonic-gate 		 */
10607c478bd9Sstevel@tonic-gate 		if (mfs->mfs_distance &&
10617c478bd9Sstevel@tonic-gate 		    mfs->mfs_distance <= DIST_MYSUB) {
10627c478bd9Sstevel@tonic-gate 			switch (vers) {
10637c478bd9Sstevel@tonic-gate 			case NFS_V4: v4near++; break;
10647c478bd9Sstevel@tonic-gate 			case NFS_V3: v3near++; break;
10657c478bd9Sstevel@tonic-gate 			case NFS_VERSION: v2near++; break;
10667c478bd9Sstevel@tonic-gate 			default: break;
10677c478bd9Sstevel@tonic-gate 			}
10687c478bd9Sstevel@tonic-gate 		}
10697c478bd9Sstevel@tonic-gate 
10707c478bd9Sstevel@tonic-gate 		/*
10717c478bd9Sstevel@tonic-gate 		 * If the mount is not replicated, we don't want to
10727c478bd9Sstevel@tonic-gate 		 * ping every entry, so we'll stop here.  This means
10737c478bd9Sstevel@tonic-gate 		 * that we may have to go back to "nextentry" above
10746a6d3e5eSjs195444 		 * to consider another entry if we can't get
10757c478bd9Sstevel@tonic-gate 		 * all the way to mount(2) with this one.
10767c478bd9Sstevel@tonic-gate 		 */
10777c478bd9Sstevel@tonic-gate 		if (!replicated)
10787c478bd9Sstevel@tonic-gate 			break;
10796a6d3e5eSjs195444 
10807c478bd9Sstevel@tonic-gate 	}
10817c478bd9Sstevel@tonic-gate 
10827c478bd9Sstevel@tonic-gate 	if (nfsvers == 0) {
10837c478bd9Sstevel@tonic-gate 		/*
10847c478bd9Sstevel@tonic-gate 		 * Choose the NFS version.
10857c478bd9Sstevel@tonic-gate 		 * We prefer higher versions, but will choose a one-
10867c478bd9Sstevel@tonic-gate 		 * version downgrade in service if we can use a local
10877c478bd9Sstevel@tonic-gate 		 * network interface and avoid a router.
10887c478bd9Sstevel@tonic-gate 		 */
10897c478bd9Sstevel@tonic-gate 		if (v4cnt && v4cnt >= v3cnt && (v4near || !v3near))
10907c478bd9Sstevel@tonic-gate 			nfsvers = NFS_V4;
10917c478bd9Sstevel@tonic-gate 		else if (v3cnt && v3cnt >= v2cnt && (v3near || !v2near))
10927c478bd9Sstevel@tonic-gate 			nfsvers = NFS_V3;
10937c478bd9Sstevel@tonic-gate 		else
10947c478bd9Sstevel@tonic-gate 			nfsvers = NFS_VERSION;
10957c478bd9Sstevel@tonic-gate 		if (trace > 2)
10967c478bd9Sstevel@tonic-gate 			trace_prt(1,
10977c478bd9Sstevel@tonic-gate 		    "  nfsmount: v4=%d[%d]v3=%d[%d],v2=%d[%d] => v%d.\n",
10987c478bd9Sstevel@tonic-gate 			    v4cnt, v4near, v3cnt, v3near,
10997c478bd9Sstevel@tonic-gate 			    v2cnt, v2near, nfsvers);
11007c478bd9Sstevel@tonic-gate 	}
11017c478bd9Sstevel@tonic-gate 
11027c478bd9Sstevel@tonic-gate 	/*
11037c478bd9Sstevel@tonic-gate 	 * Since we don't support different NFS versions in replicated
11047c478bd9Sstevel@tonic-gate 	 * mounts, set fstype now.
11057c478bd9Sstevel@tonic-gate 	 * Also take the opportunity to set
11067c478bd9Sstevel@tonic-gate 	 * the mount protocol version as appropriate.
11077c478bd9Sstevel@tonic-gate 	 */
11087c478bd9Sstevel@tonic-gate 	switch (nfsvers) {
11097c478bd9Sstevel@tonic-gate 	case NFS_V4:
11107c478bd9Sstevel@tonic-gate 		fstype = MNTTYPE_NFS4;
11117c478bd9Sstevel@tonic-gate 		break;
11127c478bd9Sstevel@tonic-gate 	case NFS_V3:
11137c478bd9Sstevel@tonic-gate 		fstype = MNTTYPE_NFS3;
11147c478bd9Sstevel@tonic-gate 		if (use_pubfh == FALSE) {
11157c478bd9Sstevel@tonic-gate 			mountversmax = MOUNTVERS3;
11167c478bd9Sstevel@tonic-gate 			versmin = MOUNTVERS3;
11177c478bd9Sstevel@tonic-gate 		}
11187c478bd9Sstevel@tonic-gate 		break;
11197c478bd9Sstevel@tonic-gate 	case NFS_VERSION:
11207c478bd9Sstevel@tonic-gate 		fstype = MNTTYPE_NFS;
11217c478bd9Sstevel@tonic-gate 		if (use_pubfh == FALSE) {
11227c478bd9Sstevel@tonic-gate 			mountversmax = MOUNTVERS_POSIX;
11237c478bd9Sstevel@tonic-gate 			versmin = MOUNTVERS;
11247c478bd9Sstevel@tonic-gate 		}
11257c478bd9Sstevel@tonic-gate 		break;
11267c478bd9Sstevel@tonic-gate 	}
11277c478bd9Sstevel@tonic-gate 
11287c478bd9Sstevel@tonic-gate 	/*
11297c478bd9Sstevel@tonic-gate 	 * Our goal here is to evaluate each of several possible
11307c478bd9Sstevel@tonic-gate 	 * replicas and try to come up with a list we can hand
11317c478bd9Sstevel@tonic-gate 	 * to mount(2).  If we don't have a valid "head" at the
11327c478bd9Sstevel@tonic-gate 	 * end of this process, it means we have rejected all
11337c478bd9Sstevel@tonic-gate 	 * potential server:/path tuples.  We will fail quietly
11347c478bd9Sstevel@tonic-gate 	 * in front of mount(2), and will have printed errors
11357c478bd9Sstevel@tonic-gate 	 * where we found them.
11367c478bd9Sstevel@tonic-gate 	 * XXX - do option work outside loop w careful design
11377c478bd9Sstevel@tonic-gate 	 * XXX - use macro for error condition free handling
11387c478bd9Sstevel@tonic-gate 	 */
11397c478bd9Sstevel@tonic-gate 	for (mfs = mfs_in; mfs; mfs = mfs->mfs_next) {
11407c478bd9Sstevel@tonic-gate 
11417c478bd9Sstevel@tonic-gate 		/*
11427c478bd9Sstevel@tonic-gate 		 * Initialize retry and delay values on a per-server basis.
11437c478bd9Sstevel@tonic-gate 		 */
11447c478bd9Sstevel@tonic-gate 		retries = get_retry(opts);
11457c478bd9Sstevel@tonic-gate 		delay = INITDELAY;
11467c478bd9Sstevel@tonic-gate retry:
11477c478bd9Sstevel@tonic-gate 		if (mfs->mfs_ignore)
11487c478bd9Sstevel@tonic-gate 			continue;
11497c478bd9Sstevel@tonic-gate 
11507c478bd9Sstevel@tonic-gate 		/*
11517c478bd9Sstevel@tonic-gate 		 * If we don't have a fh yet, and if this is not a replicated
11527c478bd9Sstevel@tonic-gate 		 * mount, we haven't done a pingnfs() on the next entry,
11537c478bd9Sstevel@tonic-gate 		 * so we don't know if the next entry is up or if it
11547c478bd9Sstevel@tonic-gate 		 * supports an NFS version we like.  So if we had a problem
11557c478bd9Sstevel@tonic-gate 		 * with an entry, we need to go back and run through some new
11567c478bd9Sstevel@tonic-gate 		 * code.
11577c478bd9Sstevel@tonic-gate 		 */
11587c478bd9Sstevel@tonic-gate 		if ((mfs->mfs_flags & MFS_FH_VIA_WEBNFS) == 0 &&
11597c478bd9Sstevel@tonic-gate 		    !replicated && skipentry)
11607c478bd9Sstevel@tonic-gate 			goto nextentry;
11617c478bd9Sstevel@tonic-gate 
11627c478bd9Sstevel@tonic-gate 		vers = mountversmax;
11637c478bd9Sstevel@tonic-gate 		host = mfs->mfs_host;
11647c478bd9Sstevel@tonic-gate 		dir = mfs->mfs_dir;
11656a6d3e5eSjs195444 
11666a6d3e5eSjs195444 		/*
11676a6d3e5eSjs195444 		 * Remember the possible '[a:d:d:r:e:s:s]' as the address to be
11686a6d3e5eSjs195444 		 * later passed to mount(2) and used in the mnttab line, but
11696a6d3e5eSjs195444 		 * only use 'a:d:d:r:e:s:s' for communication
11706a6d3e5eSjs195444 		 */
11716a6d3e5eSjs195444 		rhost = strdup(host);
11726a6d3e5eSjs195444 		if (rhost == NULL) {
11736a6d3e5eSjs195444 			syslog(LOG_ERR, "nfsmount: no memory");
11746a6d3e5eSjs195444 			last_error = NFSERR_IO;
11756a6d3e5eSjs195444 			goto out;
11766a6d3e5eSjs195444 		}
11776a6d3e5eSjs195444 		unbracket(&host);
11786a6d3e5eSjs195444 
11796a6d3e5eSjs195444 		(void) sprintf(remname, "%s:%s", rhost, dir);
11807c478bd9Sstevel@tonic-gate 		if (trace > 4 && replicated)
11817c478bd9Sstevel@tonic-gate 			trace_prt(1, "	nfsmount: examining %s\n", remname);
11827c478bd9Sstevel@tonic-gate 
11837c478bd9Sstevel@tonic-gate 		/*
11847c478bd9Sstevel@tonic-gate 		 * If it's cached we need to get cachefs to mount it.
11857c478bd9Sstevel@tonic-gate 		 */
11867c478bd9Sstevel@tonic-gate 		if (cached) {
11877c478bd9Sstevel@tonic-gate 			char *copts = opts;
11887c478bd9Sstevel@tonic-gate 
11897c478bd9Sstevel@tonic-gate 			/*
11907c478bd9Sstevel@tonic-gate 			 * If we started with a URL we need to turn on
11917c478bd9Sstevel@tonic-gate 			 * -o public if not on already
11927c478bd9Sstevel@tonic-gate 			 */
11937c478bd9Sstevel@tonic-gate 			if (use_pubfh == FALSE &&
11947c478bd9Sstevel@tonic-gate 			    (mfs->mfs_flags & MFS_FH_VIA_WEBNFS)) {
11957c478bd9Sstevel@tonic-gate 
11967c478bd9Sstevel@tonic-gate 				copts = malloc(strlen(opts) +
11977c478bd9Sstevel@tonic-gate 				    strlen(",public")+1);
11987c478bd9Sstevel@tonic-gate 
11997c478bd9Sstevel@tonic-gate 				if (copts == NULL) {
12007c478bd9Sstevel@tonic-gate 					syslog(LOG_ERR, "nfsmount: no memory");
12017c478bd9Sstevel@tonic-gate 					last_error = NFSERR_IO;
12027c478bd9Sstevel@tonic-gate 					goto out;
12037c478bd9Sstevel@tonic-gate 				}
12047c478bd9Sstevel@tonic-gate 
12057c478bd9Sstevel@tonic-gate 				strcpy(copts, opts);
12067c478bd9Sstevel@tonic-gate 
12077c478bd9Sstevel@tonic-gate 				if (strlen(copts) != 0)
12087c478bd9Sstevel@tonic-gate 					strcat(copts, ",");
12097c478bd9Sstevel@tonic-gate 
12107c478bd9Sstevel@tonic-gate 				strcat(copts, "public");
12117c478bd9Sstevel@tonic-gate 			}
12127c478bd9Sstevel@tonic-gate 
12137c478bd9Sstevel@tonic-gate 			last_error = mount_generic(remname, MNTTYPE_CACHEFS,
12147c478bd9Sstevel@tonic-gate 			    copts, mntpnt, overlay);
12157c478bd9Sstevel@tonic-gate 
12167c478bd9Sstevel@tonic-gate 			if (copts != opts)
12177c478bd9Sstevel@tonic-gate 				free(copts);
12187c478bd9Sstevel@tonic-gate 
12197c478bd9Sstevel@tonic-gate 			if (last_error) {
12207c478bd9Sstevel@tonic-gate 				skipentry = 1;
12217c478bd9Sstevel@tonic-gate 				mfs->mfs_ignore = 1;
12227c478bd9Sstevel@tonic-gate 				continue;
12237c478bd9Sstevel@tonic-gate 			}
12247c478bd9Sstevel@tonic-gate 			goto out;
12257c478bd9Sstevel@tonic-gate 		}
12267c478bd9Sstevel@tonic-gate 
12277c478bd9Sstevel@tonic-gate 		if (mfs->mfs_args == NULL) {
12287c478bd9Sstevel@tonic-gate 
12297c478bd9Sstevel@tonic-gate 			/*
12307c478bd9Sstevel@tonic-gate 			 * Allocate nfs_args structure
12317c478bd9Sstevel@tonic-gate 			 */
12327c478bd9Sstevel@tonic-gate 			argp = (struct nfs_args *)
12337c478bd9Sstevel@tonic-gate 			    malloc(sizeof (struct nfs_args));
12347c478bd9Sstevel@tonic-gate 
12357c478bd9Sstevel@tonic-gate 			if (!argp) {
12367c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR, "nfsmount: no memory");
12377c478bd9Sstevel@tonic-gate 				last_error = NFSERR_IO;
12387c478bd9Sstevel@tonic-gate 				goto out;
12397c478bd9Sstevel@tonic-gate 			}
12407c478bd9Sstevel@tonic-gate 
12417c478bd9Sstevel@tonic-gate 			(void) memset(argp, 0, sizeof (*argp));
12427c478bd9Sstevel@tonic-gate 
12437c478bd9Sstevel@tonic-gate 			/*
12447c478bd9Sstevel@tonic-gate 			 * RDMA support
12457c478bd9Sstevel@tonic-gate 			 * By now Mount argument struct has been allocated,
12467c478bd9Sstevel@tonic-gate 			 * either a pub_fh path will be taken or the regular
12477c478bd9Sstevel@tonic-gate 			 * one. So here if a protocol was specified and it
12487c478bd9Sstevel@tonic-gate 			 * was not rdma we let it be, else we set DO_RDMA.
12497c478bd9Sstevel@tonic-gate 			 * If no proto was there we advise on trying RDMA.
12507c478bd9Sstevel@tonic-gate 			 */
12517c478bd9Sstevel@tonic-gate 			if (nfs_proto) {
12527c478bd9Sstevel@tonic-gate 				if (strcmp(nfs_proto, "rdma") == 0) {
12537c478bd9Sstevel@tonic-gate 					free(nfs_proto);
12547c478bd9Sstevel@tonic-gate 					nfs_proto = NULL;
12557c478bd9Sstevel@tonic-gate 					argp->flags |= NFSMNT_DORDMA;
12567c478bd9Sstevel@tonic-gate 				}
12577c478bd9Sstevel@tonic-gate 			} else
12587c478bd9Sstevel@tonic-gate 				argp->flags |= NFSMNT_TRYRDMA;
12597c478bd9Sstevel@tonic-gate 		} else {
12607c478bd9Sstevel@tonic-gate 			argp = mfs->mfs_args;
12617c478bd9Sstevel@tonic-gate 			mfs->mfs_args = NULL;
12627c478bd9Sstevel@tonic-gate 
12637c478bd9Sstevel@tonic-gate 			/*
12647c478bd9Sstevel@tonic-gate 			 * Skip entry if we already have file handle but the
12657c478bd9Sstevel@tonic-gate 			 * NFS version is wrong.
12667c478bd9Sstevel@tonic-gate 			 */
12677c478bd9Sstevel@tonic-gate 			if ((mfs->mfs_flags & MFS_FH_VIA_WEBNFS) &&
12687c478bd9Sstevel@tonic-gate 			    mfs->mfs_version != nfsvers) {
12697c478bd9Sstevel@tonic-gate 
12707c478bd9Sstevel@tonic-gate 				free(argp);
12717c478bd9Sstevel@tonic-gate 				skipentry = 1;
12727c478bd9Sstevel@tonic-gate 				mfs->mfs_ignore = 1;
12737c478bd9Sstevel@tonic-gate 				continue;
12747c478bd9Sstevel@tonic-gate 			}
12757c478bd9Sstevel@tonic-gate 		}
12767c478bd9Sstevel@tonic-gate 
12777c478bd9Sstevel@tonic-gate 		prevhead = head;
12787c478bd9Sstevel@tonic-gate 		prevtail = tail;
12797c478bd9Sstevel@tonic-gate 		if (!head)
12807c478bd9Sstevel@tonic-gate 			head = tail = argp;
12817c478bd9Sstevel@tonic-gate 		else
12827c478bd9Sstevel@tonic-gate 			tail = tail->nfs_ext_u.nfs_extB.next = argp;
12837c478bd9Sstevel@tonic-gate 
12847c478bd9Sstevel@tonic-gate 		/*
12857c478bd9Sstevel@tonic-gate 		 * WebNFS and NFSv4 behave similarly in that they
12867c478bd9Sstevel@tonic-gate 		 * don't use the mount protocol.  Therefore, avoid
12877c478bd9Sstevel@tonic-gate 		 * mount protocol like things when version 4 is being
12887c478bd9Sstevel@tonic-gate 		 * used.
12897c478bd9Sstevel@tonic-gate 		 */
12907c478bd9Sstevel@tonic-gate 		if ((mfs->mfs_flags & MFS_FH_VIA_WEBNFS) == 0 &&
12917c478bd9Sstevel@tonic-gate 		    nfsvers != NFS_V4) {
12927c478bd9Sstevel@tonic-gate 			timeout.tv_usec = 0;
12937c478bd9Sstevel@tonic-gate 			timeout.tv_sec = rpc_timeout;
12947c478bd9Sstevel@tonic-gate 			rpc_stat = RPC_TIMEDOUT;
12957c478bd9Sstevel@tonic-gate 
12967c478bd9Sstevel@tonic-gate 			/* Create the client handle. */
12977c478bd9Sstevel@tonic-gate 
12987c478bd9Sstevel@tonic-gate 			if (trace > 1) {
12991bbc88acSrm15945 				trace_prt(1,
13001bbc88acSrm15945 				    "  nfsmount: Get mount version: request "
13017c478bd9Sstevel@tonic-gate 				    "vers=%d min=%d\n", vers, versmin);
13027c478bd9Sstevel@tonic-gate 			}
13037c478bd9Sstevel@tonic-gate 
13047c478bd9Sstevel@tonic-gate 			while ((cl = clnt_create_vers(host, MOUNTPROG, &outvers,
13057c478bd9Sstevel@tonic-gate 			    versmin, vers, "udp")) == NULL) {
13067c478bd9Sstevel@tonic-gate 				if (trace > 4) {
13077c478bd9Sstevel@tonic-gate 					trace_prt(1,
13081bbc88acSrm15945 					    "  nfsmount: Can't get mount "
13091bbc88acSrm15945 					    "version: rpcerr=%d\n",
13107c478bd9Sstevel@tonic-gate 					    rpc_createerr.cf_stat);
13117c478bd9Sstevel@tonic-gate 				}
13127c478bd9Sstevel@tonic-gate 				if (rpc_createerr.cf_stat == RPC_UNKNOWNHOST ||
13137c478bd9Sstevel@tonic-gate 				    rpc_createerr.cf_stat == RPC_TIMEDOUT)
13147c478bd9Sstevel@tonic-gate 					break;
13157c478bd9Sstevel@tonic-gate 
13167c478bd9Sstevel@tonic-gate 			/*
13177c478bd9Sstevel@tonic-gate 			 * backoff and return lower version to retry the ping.
13187c478bd9Sstevel@tonic-gate 			 * XXX we should be more careful and handle
13197c478bd9Sstevel@tonic-gate 			 * RPC_PROGVERSMISMATCH here, because that error
13207c478bd9Sstevel@tonic-gate 			 * is handled in clnt_create_vers(). It's not done to
13217c478bd9Sstevel@tonic-gate 			 * stay in sync with the nfs mount command.
13227c478bd9Sstevel@tonic-gate 			 */
13237c478bd9Sstevel@tonic-gate 				vers--;
13247c478bd9Sstevel@tonic-gate 				if (vers < versmin)
13257c478bd9Sstevel@tonic-gate 					break;
13267c478bd9Sstevel@tonic-gate 				if (trace > 4) {
13271bbc88acSrm15945 					trace_prt(1,
13281bbc88acSrm15945 					    "  nfsmount: Try version=%d\n",
13291bbc88acSrm15945 					    vers);
13307c478bd9Sstevel@tonic-gate 				}
13317c478bd9Sstevel@tonic-gate 			}
13327c478bd9Sstevel@tonic-gate 
13337c478bd9Sstevel@tonic-gate 			if (cl == NULL) {
13347c478bd9Sstevel@tonic-gate 				free(argp);
13357c478bd9Sstevel@tonic-gate 				head = prevhead;
13367c478bd9Sstevel@tonic-gate 				tail = prevtail;
13377c478bd9Sstevel@tonic-gate 				if (tail)
13387c478bd9Sstevel@tonic-gate 					tail->nfs_ext_u.nfs_extB.next = NULL;
13397c478bd9Sstevel@tonic-gate 				last_error = NFSERR_NOENT;
13407c478bd9Sstevel@tonic-gate 
13417c478bd9Sstevel@tonic-gate 				if (rpc_createerr.cf_stat != RPC_UNKNOWNHOST &&
13421bbc88acSrm15945 				    rpc_createerr.cf_stat !=
13431bbc88acSrm15945 				    RPC_PROGVERSMISMATCH &&
13447c478bd9Sstevel@tonic-gate 				    retries-- > 0) {
13451bbc88acSrm15945 					DELAY(delay);
13467c478bd9Sstevel@tonic-gate 					goto retry;
13477c478bd9Sstevel@tonic-gate 				}
13487c478bd9Sstevel@tonic-gate 
13497c478bd9Sstevel@tonic-gate 				syslog(loglevel, "%s %s", host,
13501bbc88acSrm15945 				    clnt_spcreateerror(
13511bbc88acSrm15945 				    "server not responding"));
13527c478bd9Sstevel@tonic-gate 				skipentry = 1;
13537c478bd9Sstevel@tonic-gate 				mfs->mfs_ignore = 1;
13547c478bd9Sstevel@tonic-gate 				continue;
13557c478bd9Sstevel@tonic-gate 			}
13567c478bd9Sstevel@tonic-gate 			if (trace > 1) {
13571bbc88acSrm15945 				trace_prt(1,
13581bbc88acSrm15945 				    "	nfsmount: mount version=%d\n", outvers);
13597c478bd9Sstevel@tonic-gate 			}
13607c478bd9Sstevel@tonic-gate #ifdef MALLOC_DEBUG
13617c478bd9Sstevel@tonic-gate 			add_alloc("CLNT_HANDLE", cl, 0, __FILE__, __LINE__);
13627c478bd9Sstevel@tonic-gate 			add_alloc("AUTH_HANDLE", cl->cl_auth, 0,
13637c478bd9Sstevel@tonic-gate 			    __FILE__, __LINE__);
13647c478bd9Sstevel@tonic-gate #endif
13657c478bd9Sstevel@tonic-gate 
13667c478bd9Sstevel@tonic-gate 			if (__clnt_bindresvport(cl) < 0) {
13677c478bd9Sstevel@tonic-gate 				free(argp);
13687c478bd9Sstevel@tonic-gate 				head = prevhead;
13697c478bd9Sstevel@tonic-gate 				tail = prevtail;
13707c478bd9Sstevel@tonic-gate 				if (tail)
13717c478bd9Sstevel@tonic-gate 					tail->nfs_ext_u.nfs_extB.next = NULL;
13727c478bd9Sstevel@tonic-gate 				last_error = NFSERR_NOENT;
13737c478bd9Sstevel@tonic-gate 
13747c478bd9Sstevel@tonic-gate 				if (retries-- > 0) {
13757c478bd9Sstevel@tonic-gate 					destroy_auth_client_handle(cl);
13767c478bd9Sstevel@tonic-gate 					DELAY(delay);
13777c478bd9Sstevel@tonic-gate 					goto retry;
13787c478bd9Sstevel@tonic-gate 				}
13797c478bd9Sstevel@tonic-gate 
13807c478bd9Sstevel@tonic-gate 				syslog(loglevel, "mount %s: %s", host,
13817c478bd9Sstevel@tonic-gate 				    "Couldn't bind to reserved port");
13827c478bd9Sstevel@tonic-gate 				destroy_auth_client_handle(cl);
13837c478bd9Sstevel@tonic-gate 				skipentry = 1;
13847c478bd9Sstevel@tonic-gate 				mfs->mfs_ignore = 1;
13857c478bd9Sstevel@tonic-gate 				continue;
13867c478bd9Sstevel@tonic-gate 			}
13877c478bd9Sstevel@tonic-gate 
13887c478bd9Sstevel@tonic-gate #ifdef MALLOC_DEBUG
13891bbc88acSrm15945 			drop_alloc("AUTH_HANDLE", cl->cl_auth,
13901bbc88acSrm15945 			    __FILE__, __LINE__);
13917c478bd9Sstevel@tonic-gate #endif
13927c478bd9Sstevel@tonic-gate 			AUTH_DESTROY(cl->cl_auth);
13937c478bd9Sstevel@tonic-gate 			if ((cl->cl_auth = authsys_create_default()) == NULL) {
13947c478bd9Sstevel@tonic-gate 				free(argp);
13957c478bd9Sstevel@tonic-gate 				head = prevhead;
13967c478bd9Sstevel@tonic-gate 				tail = prevtail;
13977c478bd9Sstevel@tonic-gate 				if (tail)
13987c478bd9Sstevel@tonic-gate 					tail->nfs_ext_u.nfs_extB.next = NULL;
13997c478bd9Sstevel@tonic-gate 				last_error = NFSERR_NOENT;
14007c478bd9Sstevel@tonic-gate 
14017c478bd9Sstevel@tonic-gate 				if (retries-- > 0) {
14027c478bd9Sstevel@tonic-gate 					destroy_auth_client_handle(cl);
14037c478bd9Sstevel@tonic-gate 					DELAY(delay);
14047c478bd9Sstevel@tonic-gate 					goto retry;
14057c478bd9Sstevel@tonic-gate 				}
14067c478bd9Sstevel@tonic-gate 
14077c478bd9Sstevel@tonic-gate 				syslog(loglevel, "mount %s: %s", host,
14087c478bd9Sstevel@tonic-gate 				    "Failed creating default auth handle");
14097c478bd9Sstevel@tonic-gate 				destroy_auth_client_handle(cl);
14107c478bd9Sstevel@tonic-gate 				skipentry = 1;
14117c478bd9Sstevel@tonic-gate 				mfs->mfs_ignore = 1;
14127c478bd9Sstevel@tonic-gate 				continue;
14137c478bd9Sstevel@tonic-gate 			}
14147c478bd9Sstevel@tonic-gate #ifdef MALLOC_DEBUG
14157c478bd9Sstevel@tonic-gate 			add_alloc("AUTH_HANDLE", cl->cl_auth, 0,
14167c478bd9Sstevel@tonic-gate 			    __FILE__, __LINE__);
14177c478bd9Sstevel@tonic-gate #endif
14187c478bd9Sstevel@tonic-gate 		} else
14197c478bd9Sstevel@tonic-gate 			cl = NULL;
14207c478bd9Sstevel@tonic-gate 
14217c478bd9Sstevel@tonic-gate 		/*
14227c478bd9Sstevel@tonic-gate 		 * set security options
14237c478bd9Sstevel@tonic-gate 		 */
14247c478bd9Sstevel@tonic-gate 		sec_opt = 0;
14257c478bd9Sstevel@tonic-gate 		(void) memset(&nfs_sec, 0, sizeof (nfs_sec));
14267c478bd9Sstevel@tonic-gate 		if (hasmntopt(&m, MNTOPT_SECURE) != NULL) {
14277c478bd9Sstevel@tonic-gate 			if (++sec_opt > 1) {
14287c478bd9Sstevel@tonic-gate 				syslog(loglevel,
14297c478bd9Sstevel@tonic-gate 				    "conflicting security options for %s",
14307c478bd9Sstevel@tonic-gate 				    remname);
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 			if (nfs_getseconfig_byname("dh", &nfs_sec)) {
14437c478bd9Sstevel@tonic-gate 				syslog(loglevel,
14447c478bd9Sstevel@tonic-gate 				    "error getting dh information from %s",
14457c478bd9Sstevel@tonic-gate 				    NFSSEC_CONF);
14467c478bd9Sstevel@tonic-gate 				free(argp);
14477c478bd9Sstevel@tonic-gate 				head = prevhead;
14487c478bd9Sstevel@tonic-gate 				tail = prevtail;
14497c478bd9Sstevel@tonic-gate 				if (tail)
14507c478bd9Sstevel@tonic-gate 					tail->nfs_ext_u.nfs_extB.next = NULL;
14517c478bd9Sstevel@tonic-gate 				last_error = NFSERR_IO;
14527c478bd9Sstevel@tonic-gate 				destroy_auth_client_handle(cl);
14537c478bd9Sstevel@tonic-gate 				skipentry = 1;
14547c478bd9Sstevel@tonic-gate 				mfs->mfs_ignore = 1;
14557c478bd9Sstevel@tonic-gate 				continue;
14567c478bd9Sstevel@tonic-gate 			}
14577c478bd9Sstevel@tonic-gate 		}
14587c478bd9Sstevel@tonic-gate 
14597c478bd9Sstevel@tonic-gate 		nfs_flavor = NULL;
1460d7c6abe8Sjasmith 		if (hasmntopt(&m, MNTOPT_SEC) != NULL) {
14617c478bd9Sstevel@tonic-gate 			if ((str_opt(&m, MNTOPT_SEC, &nfs_flavor)) == -1) {
14627c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR, "nfsmount: no memory");
14637c478bd9Sstevel@tonic-gate 				last_error = NFSERR_IO;
14647c478bd9Sstevel@tonic-gate 				destroy_auth_client_handle(cl);
14657c478bd9Sstevel@tonic-gate 				goto out;
14667c478bd9Sstevel@tonic-gate 			}
14677c478bd9Sstevel@tonic-gate 		}
14687c478bd9Sstevel@tonic-gate 
14697c478bd9Sstevel@tonic-gate 		if (nfs_flavor) {
14707c478bd9Sstevel@tonic-gate 			if (++sec_opt > 1) {
14717c478bd9Sstevel@tonic-gate 				syslog(loglevel,
14727c478bd9Sstevel@tonic-gate 				    "conflicting security options for %s",
14737c478bd9Sstevel@tonic-gate 				    remname);
14747c478bd9Sstevel@tonic-gate 				free(nfs_flavor);
14757c478bd9Sstevel@tonic-gate 				free(argp);
14767c478bd9Sstevel@tonic-gate 				head = prevhead;
14777c478bd9Sstevel@tonic-gate 				tail = prevtail;
14787c478bd9Sstevel@tonic-gate 				if (tail)
14797c478bd9Sstevel@tonic-gate 					tail->nfs_ext_u.nfs_extB.next = NULL;
14807c478bd9Sstevel@tonic-gate 				last_error = NFSERR_IO;
14817c478bd9Sstevel@tonic-gate 				destroy_auth_client_handle(cl);
14827c478bd9Sstevel@tonic-gate 				skipentry = 1;
14837c478bd9Sstevel@tonic-gate 				mfs->mfs_ignore = 1;
14847c478bd9Sstevel@tonic-gate 				continue;
14857c478bd9Sstevel@tonic-gate 			}
14867c478bd9Sstevel@tonic-gate 			if (nfs_getseconfig_byname(nfs_flavor, &nfs_sec)) {
14877c478bd9Sstevel@tonic-gate 				syslog(loglevel,
14887c478bd9Sstevel@tonic-gate 				    "error getting %s information from %s",
14897c478bd9Sstevel@tonic-gate 				    nfs_flavor, NFSSEC_CONF);
14907c478bd9Sstevel@tonic-gate 				free(nfs_flavor);
14917c478bd9Sstevel@tonic-gate 				free(argp);
14927c478bd9Sstevel@tonic-gate 				head = prevhead;
14937c478bd9Sstevel@tonic-gate 				tail = prevtail;
14947c478bd9Sstevel@tonic-gate 				if (tail)
14957c478bd9Sstevel@tonic-gate 					tail->nfs_ext_u.nfs_extB.next = NULL;
14967c478bd9Sstevel@tonic-gate 				last_error = NFSERR_IO;
14977c478bd9Sstevel@tonic-gate 				destroy_auth_client_handle(cl);
14987c478bd9Sstevel@tonic-gate 				skipentry = 1;
14997c478bd9Sstevel@tonic-gate 				mfs->mfs_ignore = 1;
15007c478bd9Sstevel@tonic-gate 				continue;
15017c478bd9Sstevel@tonic-gate 			}
15027c478bd9Sstevel@tonic-gate 			free(nfs_flavor);
15037c478bd9Sstevel@tonic-gate 		}
15047c478bd9Sstevel@tonic-gate 
15057c478bd9Sstevel@tonic-gate 		posix = (nfsvers != NFS_V4 &&
15067c478bd9Sstevel@tonic-gate 		    hasmntopt(&m, MNTOPT_POSIX) != NULL) ? 1 : 0;
15077c478bd9Sstevel@tonic-gate 
15087c478bd9Sstevel@tonic-gate 		if ((mfs->mfs_flags & MFS_FH_VIA_WEBNFS) == 0 &&
15097c478bd9Sstevel@tonic-gate 		    nfsvers != NFS_V4) {
15107c478bd9Sstevel@tonic-gate 			bool_t give_up_on_mnt;
15117c478bd9Sstevel@tonic-gate 			bool_t got_mnt_error;
15127c478bd9Sstevel@tonic-gate 		/*
15137c478bd9Sstevel@tonic-gate 		 * If we started with a URL, if first byte of path is not "/",
15147c478bd9Sstevel@tonic-gate 		 * then the mount will likely fail, so we should try again
15157c478bd9Sstevel@tonic-gate 		 * with a prepended "/".
15167c478bd9Sstevel@tonic-gate 		 */
15177c478bd9Sstevel@tonic-gate 			if (mfs->mfs_flags & MFS_ALLOC_DIR && *dir != '/')
15187c478bd9Sstevel@tonic-gate 				give_up_on_mnt = FALSE;
15197c478bd9Sstevel@tonic-gate 			else
15207c478bd9Sstevel@tonic-gate 				give_up_on_mnt = TRUE;
15217c478bd9Sstevel@tonic-gate 
15227c478bd9Sstevel@tonic-gate 			got_mnt_error = FALSE;
15237c478bd9Sstevel@tonic-gate 
15247c478bd9Sstevel@tonic-gate try_mnt_slash:
15257c478bd9Sstevel@tonic-gate 			if (got_mnt_error == TRUE) {
15267c478bd9Sstevel@tonic-gate 				int i, l;
15277c478bd9Sstevel@tonic-gate 
15287c478bd9Sstevel@tonic-gate 				give_up_on_mnt = TRUE;
15297c478bd9Sstevel@tonic-gate 				l = strlen(dir);
15307c478bd9Sstevel@tonic-gate 
15317c478bd9Sstevel@tonic-gate 				/*
15327c478bd9Sstevel@tonic-gate 				 * Insert a "/" to front of mfs_dir.
15337c478bd9Sstevel@tonic-gate 				 */
15347c478bd9Sstevel@tonic-gate 				for (i = l; i > 0; i--)
15357c478bd9Sstevel@tonic-gate 					dir[i] = dir[i-1];
15367c478bd9Sstevel@tonic-gate 
15377c478bd9Sstevel@tonic-gate 				dir[0] = '/';
15387c478bd9Sstevel@tonic-gate 			}
15397c478bd9Sstevel@tonic-gate 
15407c478bd9Sstevel@tonic-gate 			/* Get fhandle of remote path from server's mountd */
15417c478bd9Sstevel@tonic-gate 
15427c478bd9Sstevel@tonic-gate 			switch (outvers) {
15437c478bd9Sstevel@tonic-gate 			case MOUNTVERS:
15447c478bd9Sstevel@tonic-gate 				if (posix) {
15457c478bd9Sstevel@tonic-gate 					free(argp);
15467c478bd9Sstevel@tonic-gate 					head = prevhead;
15477c478bd9Sstevel@tonic-gate 					tail = prevtail;
15487c478bd9Sstevel@tonic-gate 					if (tail)
15491bbc88acSrm15945 						tail->nfs_ext_u.nfs_extB.next =
15501bbc88acSrm15945 						    NULL;
15517c478bd9Sstevel@tonic-gate 					last_error = NFSERR_NOENT;
15521bbc88acSrm15945 					syslog(loglevel,
15531bbc88acSrm15945 					    "can't get posix info for %s",
15547c478bd9Sstevel@tonic-gate 					    host);
15557c478bd9Sstevel@tonic-gate 					destroy_auth_client_handle(cl);
15567c478bd9Sstevel@tonic-gate 					skipentry = 1;
15577c478bd9Sstevel@tonic-gate 					mfs->mfs_ignore = 1;
15587c478bd9Sstevel@tonic-gate 					continue;
15597c478bd9Sstevel@tonic-gate 				}
15607c478bd9Sstevel@tonic-gate 		    /* FALLTHRU */
15617c478bd9Sstevel@tonic-gate 			case MOUNTVERS_POSIX:
15627c478bd9Sstevel@tonic-gate 				if (nfsvers == NFS_V3) {
15637c478bd9Sstevel@tonic-gate 					free(argp);
15647c478bd9Sstevel@tonic-gate 					head = prevhead;
15657c478bd9Sstevel@tonic-gate 					tail = prevtail;
15667c478bd9Sstevel@tonic-gate 					if (tail)
15671bbc88acSrm15945 						tail->nfs_ext_u.nfs_extB.next =
15681bbc88acSrm15945 						    NULL;
15697c478bd9Sstevel@tonic-gate 					last_error = NFSERR_NOENT;
15707c478bd9Sstevel@tonic-gate 					syslog(loglevel,
15717c478bd9Sstevel@tonic-gate 					    "%s doesn't support NFS Version 3",
15727c478bd9Sstevel@tonic-gate 					    host);
15737c478bd9Sstevel@tonic-gate 					destroy_auth_client_handle(cl);
15747c478bd9Sstevel@tonic-gate 					skipentry = 1;
15757c478bd9Sstevel@tonic-gate 					mfs->mfs_ignore = 1;
15767c478bd9Sstevel@tonic-gate 					continue;
15777c478bd9Sstevel@tonic-gate 				}
15787c478bd9Sstevel@tonic-gate 				rpc_stat = clnt_call(cl, MOUNTPROC_MNT,
15797c478bd9Sstevel@tonic-gate 				    xdr_dirpath, (caddr_t)&dir,
15807c478bd9Sstevel@tonic-gate 				    xdr_fhstatus, (caddr_t)&fhs, timeout);
15817c478bd9Sstevel@tonic-gate 				if (rpc_stat != RPC_SUCCESS) {
15827c478bd9Sstevel@tonic-gate 
15837c478bd9Sstevel@tonic-gate 					if (give_up_on_mnt == FALSE) {
15847c478bd9Sstevel@tonic-gate 						got_mnt_error = TRUE;
15857c478bd9Sstevel@tonic-gate 						goto try_mnt_slash;
15867c478bd9Sstevel@tonic-gate 					}
15877c478bd9Sstevel@tonic-gate 
15887c478bd9Sstevel@tonic-gate 				/*
15897c478bd9Sstevel@tonic-gate 				 * Given the way "clnt_sperror" works, the "%s"
15907c478bd9Sstevel@tonic-gate 				 * immediately following the "not responding"
15917c478bd9Sstevel@tonic-gate 				 * is correct.
15927c478bd9Sstevel@tonic-gate 				 */
15937c478bd9Sstevel@tonic-gate 					free(argp);
15947c478bd9Sstevel@tonic-gate 					head = prevhead;
15957c478bd9Sstevel@tonic-gate 					tail = prevtail;
15967c478bd9Sstevel@tonic-gate 					if (tail)
15971bbc88acSrm15945 						tail->nfs_ext_u.nfs_extB.next =
15981bbc88acSrm15945 						    NULL;
15997c478bd9Sstevel@tonic-gate 					last_error = NFSERR_NOENT;
16007c478bd9Sstevel@tonic-gate 
16017c478bd9Sstevel@tonic-gate 					if (retries-- > 0) {
16027c478bd9Sstevel@tonic-gate 						destroy_auth_client_handle(cl);
16037c478bd9Sstevel@tonic-gate 						DELAY(delay);
16047c478bd9Sstevel@tonic-gate 						goto retry;
16057c478bd9Sstevel@tonic-gate 					}
16067c478bd9Sstevel@tonic-gate 
16077c478bd9Sstevel@tonic-gate 					if (trace > 3) {
16087c478bd9Sstevel@tonic-gate 						trace_prt(1,
16091bbc88acSrm15945 						    "  nfsmount: mount RPC "
16101bbc88acSrm15945 						    "failed for %s\n",
16117c478bd9Sstevel@tonic-gate 						    host);
16127c478bd9Sstevel@tonic-gate 					}
16131bbc88acSrm15945 					syslog(loglevel,
16141bbc88acSrm15945 					    "%s server not responding%s",
16157c478bd9Sstevel@tonic-gate 					    host, clnt_sperror(cl, ""));
16167c478bd9Sstevel@tonic-gate 					destroy_auth_client_handle(cl);
16177c478bd9Sstevel@tonic-gate 					skipentry = 1;
16187c478bd9Sstevel@tonic-gate 					mfs->mfs_ignore = 1;
16197c478bd9Sstevel@tonic-gate 					continue;
16207c478bd9Sstevel@tonic-gate 				}
16217c478bd9Sstevel@tonic-gate 				if ((errno = fhs.fhs_status) != MNT_OK)  {
16227c478bd9Sstevel@tonic-gate 
16237c478bd9Sstevel@tonic-gate 					if (give_up_on_mnt == FALSE) {
16247c478bd9Sstevel@tonic-gate 						got_mnt_error = TRUE;
16257c478bd9Sstevel@tonic-gate 						goto try_mnt_slash;
16267c478bd9Sstevel@tonic-gate 					}
16277c478bd9Sstevel@tonic-gate 
16287c478bd9Sstevel@tonic-gate 					free(argp);
16297c478bd9Sstevel@tonic-gate 					head = prevhead;
16307c478bd9Sstevel@tonic-gate 					tail = prevtail;
16317c478bd9Sstevel@tonic-gate 					if (tail)
16321bbc88acSrm15945 						tail->nfs_ext_u.nfs_extB.next =
16331bbc88acSrm15945 						    NULL;
16347c478bd9Sstevel@tonic-gate 					if (errno == EACCES) {
16357c478bd9Sstevel@tonic-gate 						status = NFSERR_ACCES;
16367c478bd9Sstevel@tonic-gate 					} else {
16371bbc88acSrm15945 						syslog(loglevel, "%s: %m",
16381bbc88acSrm15945 						    host);
16397c478bd9Sstevel@tonic-gate 						status = NFSERR_IO;
16407c478bd9Sstevel@tonic-gate 					}
16417c478bd9Sstevel@tonic-gate 					if (trace > 3) {
16421bbc88acSrm15945 						trace_prt(1,
16431bbc88acSrm15945 						    "  nfsmount: mount RPC gave"
16447c478bd9Sstevel@tonic-gate 						    " %d for %s:%s\n",
16457c478bd9Sstevel@tonic-gate 						    errno, host, dir);
16467c478bd9Sstevel@tonic-gate 					}
16477c478bd9Sstevel@tonic-gate 					last_error = status;
16487c478bd9Sstevel@tonic-gate 					destroy_auth_client_handle(cl);
16497c478bd9Sstevel@tonic-gate 					skipentry = 1;
16507c478bd9Sstevel@tonic-gate 					mfs->mfs_ignore = 1;
16517c478bd9Sstevel@tonic-gate 					continue;
16527c478bd9Sstevel@tonic-gate 				}
16537c478bd9Sstevel@tonic-gate 				argp->fh = malloc((sizeof (fhandle)));
16547c478bd9Sstevel@tonic-gate 				if (!argp->fh) {
16557c478bd9Sstevel@tonic-gate 					syslog(LOG_ERR, "nfsmount: no memory");
16567c478bd9Sstevel@tonic-gate 					last_error = NFSERR_IO;
16577c478bd9Sstevel@tonic-gate 					destroy_auth_client_handle(cl);
16587c478bd9Sstevel@tonic-gate 					goto out;
16597c478bd9Sstevel@tonic-gate 				}
16601bbc88acSrm15945 				(void) memcpy(argp->fh,
16611bbc88acSrm15945 				    &fhs.fhstatus_u.fhs_fhandle,
16627c478bd9Sstevel@tonic-gate 				    sizeof (fhandle));
16637c478bd9Sstevel@tonic-gate 				break;
16647c478bd9Sstevel@tonic-gate 			case MOUNTVERS3:
16657c478bd9Sstevel@tonic-gate 				posix = 0;
16661bbc88acSrm15945 				(void) memset((char *)&res3, '\0',
16671bbc88acSrm15945 				    sizeof (res3));
16687c478bd9Sstevel@tonic-gate 				rpc_stat = clnt_call(cl, MOUNTPROC_MNT,
16697c478bd9Sstevel@tonic-gate 				    xdr_dirpath, (caddr_t)&dir,
16707c478bd9Sstevel@tonic-gate 				    xdr_mountres3, (caddr_t)&res3, timeout);
16717c478bd9Sstevel@tonic-gate 				if (rpc_stat != RPC_SUCCESS) {
16727c478bd9Sstevel@tonic-gate 
16737c478bd9Sstevel@tonic-gate 					if (give_up_on_mnt == FALSE) {
16747c478bd9Sstevel@tonic-gate 						got_mnt_error = TRUE;
16757c478bd9Sstevel@tonic-gate 						goto try_mnt_slash;
16767c478bd9Sstevel@tonic-gate 					}
16777c478bd9Sstevel@tonic-gate 
16787c478bd9Sstevel@tonic-gate 				/*
16797c478bd9Sstevel@tonic-gate 				 * Given the way "clnt_sperror" works, the "%s"
16807c478bd9Sstevel@tonic-gate 				 * immediately following the "not responding"
16817c478bd9Sstevel@tonic-gate 				 * is correct.
16827c478bd9Sstevel@tonic-gate 				 */
16837c478bd9Sstevel@tonic-gate 					free(argp);
16847c478bd9Sstevel@tonic-gate 					head = prevhead;
16857c478bd9Sstevel@tonic-gate 					tail = prevtail;
16867c478bd9Sstevel@tonic-gate 					if (tail)
16871bbc88acSrm15945 						tail->nfs_ext_u.nfs_extB.next =
16881bbc88acSrm15945 						    NULL;
16897c478bd9Sstevel@tonic-gate 					last_error = NFSERR_NOENT;
16907c478bd9Sstevel@tonic-gate 
16917c478bd9Sstevel@tonic-gate 					if (retries-- > 0) {
16927c478bd9Sstevel@tonic-gate 						destroy_auth_client_handle(cl);
16937c478bd9Sstevel@tonic-gate 						DELAY(delay);
16947c478bd9Sstevel@tonic-gate 						goto retry;
16957c478bd9Sstevel@tonic-gate 					}
16967c478bd9Sstevel@tonic-gate 
16977c478bd9Sstevel@tonic-gate 					if (trace > 3) {
16987c478bd9Sstevel@tonic-gate 						trace_prt(1,
16991bbc88acSrm15945 						    "  nfsmount: mount RPC "
17001bbc88acSrm15945 						    "failed for %s\n",
17017c478bd9Sstevel@tonic-gate 						    host);
17027c478bd9Sstevel@tonic-gate 					}
17031bbc88acSrm15945 					syslog(loglevel,
17041bbc88acSrm15945 					    "%s server not responding%s",
17057c478bd9Sstevel@tonic-gate 					    remname, clnt_sperror(cl, ""));
17067c478bd9Sstevel@tonic-gate 					destroy_auth_client_handle(cl);
17077c478bd9Sstevel@tonic-gate 					skipentry = 1;
17087c478bd9Sstevel@tonic-gate 					mfs->mfs_ignore = 1;
17097c478bd9Sstevel@tonic-gate 					continue;
17107c478bd9Sstevel@tonic-gate 				}
17117c478bd9Sstevel@tonic-gate 				if ((errno = res3.fhs_status) != MNT_OK)  {
17127c478bd9Sstevel@tonic-gate 
17137c478bd9Sstevel@tonic-gate 					if (give_up_on_mnt == FALSE) {
17147c478bd9Sstevel@tonic-gate 						got_mnt_error = TRUE;
17157c478bd9Sstevel@tonic-gate 						goto try_mnt_slash;
17167c478bd9Sstevel@tonic-gate 					}
17177c478bd9Sstevel@tonic-gate 
17187c478bd9Sstevel@tonic-gate 					free(argp);
17197c478bd9Sstevel@tonic-gate 					head = prevhead;
17207c478bd9Sstevel@tonic-gate 					tail = prevtail;
17217c478bd9Sstevel@tonic-gate 					if (tail)
17221bbc88acSrm15945 						tail->nfs_ext_u.nfs_extB.next =
17231bbc88acSrm15945 						    NULL;
17247c478bd9Sstevel@tonic-gate 					if (errno == EACCES) {
17257c478bd9Sstevel@tonic-gate 						status = NFSERR_ACCES;
17267c478bd9Sstevel@tonic-gate 					} else {
17271bbc88acSrm15945 						syslog(loglevel, "%s: %m",
17281bbc88acSrm15945 						    remname);
17297c478bd9Sstevel@tonic-gate 						status = NFSERR_IO;
17307c478bd9Sstevel@tonic-gate 					}
17317c478bd9Sstevel@tonic-gate 					if (trace > 3) {
17321bbc88acSrm15945 						trace_prt(1,
17331bbc88acSrm15945 						    "  nfsmount: mount RPC gave"
17347c478bd9Sstevel@tonic-gate 						    " %d for %s:%s\n",
17357c478bd9Sstevel@tonic-gate 						    errno, host, dir);
17367c478bd9Sstevel@tonic-gate 					}
17377c478bd9Sstevel@tonic-gate 					last_error = status;
17387c478bd9Sstevel@tonic-gate 					destroy_auth_client_handle(cl);
17397c478bd9Sstevel@tonic-gate 					skipentry = 1;
17407c478bd9Sstevel@tonic-gate 					mfs->mfs_ignore = 1;
17417c478bd9Sstevel@tonic-gate 					continue;
17427c478bd9Sstevel@tonic-gate 				}
17437c478bd9Sstevel@tonic-gate 
17447c478bd9Sstevel@tonic-gate 			/*
17457c478bd9Sstevel@tonic-gate 			 *  Negotiate the security flavor for nfs_mount
17467c478bd9Sstevel@tonic-gate 			 */
17471bbc88acSrm15945 				auths = res3.mountres3_u.mountinfo.
17481bbc88acSrm15945 				    auth_flavors.auth_flavors_val;
17491bbc88acSrm15945 				count = res3.mountres3_u.mountinfo.
17501bbc88acSrm15945 				    auth_flavors.auth_flavors_len;
17517c478bd9Sstevel@tonic-gate 
17527c478bd9Sstevel@tonic-gate 				if (sec_opt) {
17537c478bd9Sstevel@tonic-gate 					for (i = 0; i < count; i++)
17541bbc88acSrm15945 						if (auths[i] ==
17551bbc88acSrm15945 						    nfs_sec.sc_nfsnum) {
17567c478bd9Sstevel@tonic-gate 							break;
17577c478bd9Sstevel@tonic-gate 						}
17587c478bd9Sstevel@tonic-gate 					if (i >= count) {
17597c478bd9Sstevel@tonic-gate 						syslog(LOG_ERR,
17601bbc88acSrm15945 						    "%s: does not support "
17611bbc88acSrm15945 						    "security \"%s\"\n",
17627c478bd9Sstevel@tonic-gate 						    remname, nfs_sec.sc_name);
17637c478bd9Sstevel@tonic-gate 						clnt_freeres(cl, xdr_mountres3,
17647c478bd9Sstevel@tonic-gate 						    (caddr_t)&res3);
17657c478bd9Sstevel@tonic-gate 						free(argp);
17667c478bd9Sstevel@tonic-gate 						head = prevhead;
17677c478bd9Sstevel@tonic-gate 						tail = prevtail;
17687c478bd9Sstevel@tonic-gate 						if (tail)
17691bbc88acSrm15945 							tail->nfs_ext_u.
17701bbc88acSrm15945 							    nfs_extB.next =
17711bbc88acSrm15945 							    NULL;
17727c478bd9Sstevel@tonic-gate 						last_error = NFSERR_IO;
17737c478bd9Sstevel@tonic-gate 						destroy_auth_client_handle(cl);
17747c478bd9Sstevel@tonic-gate 						skipentry = 1;
17757c478bd9Sstevel@tonic-gate 						mfs->mfs_ignore = 1;
17767c478bd9Sstevel@tonic-gate 						continue;
17777c478bd9Sstevel@tonic-gate 					}
17781bbc88acSrm15945 				} else if (count > 0) {
17797c478bd9Sstevel@tonic-gate 					for (i = 0; i < count; i++) {
17807c478bd9Sstevel@tonic-gate 						if (!(scerror =
17811bbc88acSrm15945 						    nfs_getseconfig_bynumber(
17821bbc88acSrm15945 						    auths[i], &nfs_sec))) {
17837c478bd9Sstevel@tonic-gate 							sec_opt++;
17847c478bd9Sstevel@tonic-gate 							break;
17857c478bd9Sstevel@tonic-gate 						}
17867c478bd9Sstevel@tonic-gate 					}
17877c478bd9Sstevel@tonic-gate 					if (i >= count) {
17887c478bd9Sstevel@tonic-gate 						if (nfs_syslog_scerr(scerror,
17897c478bd9Sstevel@tonic-gate 						    scerror_msg)
17907c478bd9Sstevel@tonic-gate 						    != -1) {
17917c478bd9Sstevel@tonic-gate 							syslog(LOG_ERR,
17921bbc88acSrm15945 							    "%s cannot be "
17931bbc88acSrm15945 							    "mounted because it"
17941bbc88acSrm15945 							    " is shared with "
17951bbc88acSrm15945 							    "security flavor %d"
17961bbc88acSrm15945 							    " which %s",
17977c478bd9Sstevel@tonic-gate 							    remname,
17987c478bd9Sstevel@tonic-gate 							    auths[i-1],
17997c478bd9Sstevel@tonic-gate 							    scerror_msg);
18007c478bd9Sstevel@tonic-gate 						}
18017c478bd9Sstevel@tonic-gate 						clnt_freeres(cl, xdr_mountres3,
18027c478bd9Sstevel@tonic-gate 						    (caddr_t)&res3);
18037c478bd9Sstevel@tonic-gate 						free(argp);
18047c478bd9Sstevel@tonic-gate 						head = prevhead;
18057c478bd9Sstevel@tonic-gate 						tail = prevtail;
18067c478bd9Sstevel@tonic-gate 						if (tail)
18071bbc88acSrm15945 							tail->nfs_ext_u.
18081bbc88acSrm15945 							    nfs_extB.next =
18091bbc88acSrm15945 							    NULL;
18107c478bd9Sstevel@tonic-gate 						last_error = NFSERR_IO;
18117c478bd9Sstevel@tonic-gate 						destroy_auth_client_handle(cl);
18127c478bd9Sstevel@tonic-gate 						skipentry = 1;
18137c478bd9Sstevel@tonic-gate 						mfs->mfs_ignore = 1;
18147c478bd9Sstevel@tonic-gate 						continue;
18157c478bd9Sstevel@tonic-gate 						}
18167c478bd9Sstevel@tonic-gate 				}
18177c478bd9Sstevel@tonic-gate 
18187c478bd9Sstevel@tonic-gate 				fh3.fh3_length =
18191bbc88acSrm15945 				    res3.mountres3_u.mountinfo.fhandle.
18201bbc88acSrm15945 				    fhandle3_len;
18217c478bd9Sstevel@tonic-gate 				(void) memcpy(fh3.fh3_u.data,
18221bbc88acSrm15945 				    res3.mountres3_u.mountinfo.fhandle.
18231bbc88acSrm15945 				    fhandle3_val,
18247c478bd9Sstevel@tonic-gate 				    fh3.fh3_length);
18257c478bd9Sstevel@tonic-gate 				clnt_freeres(cl, xdr_mountres3,
18267c478bd9Sstevel@tonic-gate 				    (caddr_t)&res3);
18277c478bd9Sstevel@tonic-gate 				argp->fh = malloc(sizeof (nfs_fh3));
18287c478bd9Sstevel@tonic-gate 				if (!argp->fh) {
18297c478bd9Sstevel@tonic-gate 					syslog(LOG_ERR, "nfsmount: no memory");
18307c478bd9Sstevel@tonic-gate 					last_error = NFSERR_IO;
18317c478bd9Sstevel@tonic-gate 					destroy_auth_client_handle(cl);
18327c478bd9Sstevel@tonic-gate 					goto out;
18337c478bd9Sstevel@tonic-gate 				}
18347c478bd9Sstevel@tonic-gate 				(void) memcpy(argp->fh, &fh3, sizeof (nfs_fh3));
18357c478bd9Sstevel@tonic-gate 				break;
18367c478bd9Sstevel@tonic-gate 			default:
18377c478bd9Sstevel@tonic-gate 				free(argp);
18387c478bd9Sstevel@tonic-gate 				head = prevhead;
18397c478bd9Sstevel@tonic-gate 				tail = prevtail;
18407c478bd9Sstevel@tonic-gate 				if (tail)
18417c478bd9Sstevel@tonic-gate 					tail->nfs_ext_u.nfs_extB.next = NULL;
18427c478bd9Sstevel@tonic-gate 				last_error = NFSERR_NOENT;
18431bbc88acSrm15945 				syslog(loglevel,
18441bbc88acSrm15945 				    "unknown MOUNT version %ld on %s",
18457c478bd9Sstevel@tonic-gate 				    vers, remname);
18467c478bd9Sstevel@tonic-gate 				destroy_auth_client_handle(cl);
18477c478bd9Sstevel@tonic-gate 				skipentry = 1;
18487c478bd9Sstevel@tonic-gate 				mfs->mfs_ignore = 1;
18497c478bd9Sstevel@tonic-gate 				continue;
18507c478bd9Sstevel@tonic-gate 			} /* switch */
18517c478bd9Sstevel@tonic-gate 		}
18527c478bd9Sstevel@tonic-gate 		if (nfsvers == NFS_V4) {
18537c478bd9Sstevel@tonic-gate 			argp->fh = strdup(dir);
18547c478bd9Sstevel@tonic-gate 			if (argp->fh == NULL) {
18557c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR, "nfsmount: no memory");
18567c478bd9Sstevel@tonic-gate 				last_error = NFSERR_IO;
18577c478bd9Sstevel@tonic-gate 				goto out;
18587c478bd9Sstevel@tonic-gate 			}
18597c478bd9Sstevel@tonic-gate 		}
18607c478bd9Sstevel@tonic-gate 
18617c478bd9Sstevel@tonic-gate 		if (trace > 4)
18627c478bd9Sstevel@tonic-gate 			trace_prt(1, "	nfsmount: have %s filehandle for %s\n",
18637c478bd9Sstevel@tonic-gate 			    fstype, remname);
18647c478bd9Sstevel@tonic-gate 
18657c478bd9Sstevel@tonic-gate 		argp->flags |= NFSMNT_NEWARGS;
18667c478bd9Sstevel@tonic-gate 		argp->flags |= NFSMNT_INT;	/* default is "intr" */
18677c478bd9Sstevel@tonic-gate 		argp->flags |= NFSMNT_HOSTNAME;
18686a6d3e5eSjs195444 		argp->hostname = strdup(host);
18696a6d3e5eSjs195444 		if (argp->hostname == NULL) {
18706a6d3e5eSjs195444 			syslog(LOG_ERR, "nfsmount: no memory");
18716a6d3e5eSjs195444 			last_error = NFSERR_IO;
18726a6d3e5eSjs195444 			goto out;
18736a6d3e5eSjs195444 		}
18747c478bd9Sstevel@tonic-gate 
18757c478bd9Sstevel@tonic-gate 		/*
18767c478bd9Sstevel@tonic-gate 		 * In this case, we want NFSv4 to behave like
18777c478bd9Sstevel@tonic-gate 		 * non-WebNFS so that we get the server address.
18787c478bd9Sstevel@tonic-gate 		 */
18797c478bd9Sstevel@tonic-gate 		if ((mfs->mfs_flags & MFS_FH_VIA_WEBNFS) == 0) {
18807c478bd9Sstevel@tonic-gate 			nconf = NULL;
18817c478bd9Sstevel@tonic-gate 
18827c478bd9Sstevel@tonic-gate 			if (nfs_port != 0)
18837c478bd9Sstevel@tonic-gate 				thisport = nfs_port;
18847c478bd9Sstevel@tonic-gate 			else
18857c478bd9Sstevel@tonic-gate 				thisport = mfs->mfs_port;
18867c478bd9Sstevel@tonic-gate 
18877c478bd9Sstevel@tonic-gate 			/*
18887c478bd9Sstevel@tonic-gate 			 * For NFSv4, we want to avoid rpcbind, so call
18892f172c55SRobert Thurlow 			 * get_server_netinfo() directly to tell it that
18907c478bd9Sstevel@tonic-gate 			 * we want to go "direct_to_server".  Otherwise,
18917c478bd9Sstevel@tonic-gate 			 * do what has always been done.
18927c478bd9Sstevel@tonic-gate 			 */
18937c478bd9Sstevel@tonic-gate 			if (nfsvers == NFS_V4) {
18947c478bd9Sstevel@tonic-gate 				enum clnt_stat cstat;
18952f172c55SRobert Thurlow 
18962f172c55SRobert Thurlow 				argp->addr = get_server_netinfo(SERVER_ADDR,
18977c478bd9Sstevel@tonic-gate 				    host, NFS_PROGRAM, nfsvers, NULL,
18987c478bd9Sstevel@tonic-gate 				    &nconf, nfs_proto, thisport, NULL,
18997c478bd9Sstevel@tonic-gate 				    NULL, TRUE, NULL, &cstat);
19007c478bd9Sstevel@tonic-gate 			} else {
19017c478bd9Sstevel@tonic-gate 				argp->addr = get_addr(host, NFS_PROGRAM,
19027c478bd9Sstevel@tonic-gate 				    nfsvers, &nconf, nfs_proto,
19037c478bd9Sstevel@tonic-gate 				    thisport, NULL);
19047c478bd9Sstevel@tonic-gate 			}
19057c478bd9Sstevel@tonic-gate 
19067c478bd9Sstevel@tonic-gate 			if (argp->addr == NULL) {
190739d3e169Sevanl 				if (argp->hostname)
190839d3e169Sevanl 					free(argp->hostname);
19097c478bd9Sstevel@tonic-gate 				free(argp->fh);
19107c478bd9Sstevel@tonic-gate 				free(argp);
19117c478bd9Sstevel@tonic-gate 				head = prevhead;
19127c478bd9Sstevel@tonic-gate 				tail = prevtail;
19137c478bd9Sstevel@tonic-gate 				if (tail)
19147c478bd9Sstevel@tonic-gate 					tail->nfs_ext_u.nfs_extB.next = NULL;
19157c478bd9Sstevel@tonic-gate 				last_error = NFSERR_NOENT;
19167c478bd9Sstevel@tonic-gate 
19177c478bd9Sstevel@tonic-gate 				if (retries-- > 0) {
19187c478bd9Sstevel@tonic-gate 					destroy_auth_client_handle(cl);
19197c478bd9Sstevel@tonic-gate 					DELAY(delay);
19207c478bd9Sstevel@tonic-gate 					goto retry;
19217c478bd9Sstevel@tonic-gate 				}
19227c478bd9Sstevel@tonic-gate 
19237c478bd9Sstevel@tonic-gate 				syslog(loglevel, "%s: no NFS service", host);
19247c478bd9Sstevel@tonic-gate 				destroy_auth_client_handle(cl);
19257c478bd9Sstevel@tonic-gate 				skipentry = 1;
19267c478bd9Sstevel@tonic-gate 				mfs->mfs_ignore = 1;
19277c478bd9Sstevel@tonic-gate 				continue;
19287c478bd9Sstevel@tonic-gate 			}
19297c478bd9Sstevel@tonic-gate 			if (trace > 4)
19307c478bd9Sstevel@tonic-gate 				trace_prt(1,
19317c478bd9Sstevel@tonic-gate 				    "\tnfsmount: have net address for %s\n",
19327c478bd9Sstevel@tonic-gate 				    remname);
19337c478bd9Sstevel@tonic-gate 
19347c478bd9Sstevel@tonic-gate 		} else {
19357c478bd9Sstevel@tonic-gate 			nconf = mfs->mfs_nconf;
19367c478bd9Sstevel@tonic-gate 			mfs->mfs_nconf = NULL;
19377c478bd9Sstevel@tonic-gate 		}
19387c478bd9Sstevel@tonic-gate 
19397c478bd9Sstevel@tonic-gate 		argp->flags |= NFSMNT_KNCONF;
19407c478bd9Sstevel@tonic-gate 		argp->knconf = get_knconf(nconf);
19417c478bd9Sstevel@tonic-gate 		if (argp->knconf == NULL) {
19427c478bd9Sstevel@tonic-gate 			netbuf_free(argp->addr);
19437c478bd9Sstevel@tonic-gate 			freenetconfigent(nconf);
194439d3e169Sevanl 			if (argp->hostname)
194539d3e169Sevanl 				free(argp->hostname);
19467c478bd9Sstevel@tonic-gate 			free(argp->fh);
19477c478bd9Sstevel@tonic-gate 			free(argp);
19487c478bd9Sstevel@tonic-gate 			head = prevhead;
19497c478bd9Sstevel@tonic-gate 			tail = prevtail;
19507c478bd9Sstevel@tonic-gate 			if (tail)
19517c478bd9Sstevel@tonic-gate 				tail->nfs_ext_u.nfs_extB.next = NULL;
19527c478bd9Sstevel@tonic-gate 			last_error = NFSERR_NOSPC;
19537c478bd9Sstevel@tonic-gate 			destroy_auth_client_handle(cl);
19547c478bd9Sstevel@tonic-gate 			skipentry = 1;
19557c478bd9Sstevel@tonic-gate 			mfs->mfs_ignore = 1;
19567c478bd9Sstevel@tonic-gate 			continue;
19577c478bd9Sstevel@tonic-gate 		}
19587c478bd9Sstevel@tonic-gate 		if (trace > 4)
19597c478bd9Sstevel@tonic-gate 			trace_prt(1,
19607c478bd9Sstevel@tonic-gate 			    "\tnfsmount: have net config for %s\n",
19617c478bd9Sstevel@tonic-gate 			    remname);
19627c478bd9Sstevel@tonic-gate 
19637c478bd9Sstevel@tonic-gate 		if (hasmntopt(&m, MNTOPT_SOFT) != NULL) {
19647c478bd9Sstevel@tonic-gate 			argp->flags |= NFSMNT_SOFT;
19657c478bd9Sstevel@tonic-gate 		}
19667c478bd9Sstevel@tonic-gate 		if (hasmntopt(&m, MNTOPT_NOINTR) != NULL) {
19677c478bd9Sstevel@tonic-gate 			argp->flags &= ~(NFSMNT_INT);
19687c478bd9Sstevel@tonic-gate 		}
19697c478bd9Sstevel@tonic-gate 		if (hasmntopt(&m, MNTOPT_NOAC) != NULL) {
19707c478bd9Sstevel@tonic-gate 			argp->flags |= NFSMNT_NOAC;
19717c478bd9Sstevel@tonic-gate 		}
19727c478bd9Sstevel@tonic-gate 		if (hasmntopt(&m, MNTOPT_NOCTO) != NULL) {
19737c478bd9Sstevel@tonic-gate 			argp->flags |= NFSMNT_NOCTO;
19747c478bd9Sstevel@tonic-gate 		}
19757c478bd9Sstevel@tonic-gate 		if (hasmntopt(&m, MNTOPT_FORCEDIRECTIO) != NULL) {
19767c478bd9Sstevel@tonic-gate 			argp->flags |= NFSMNT_DIRECTIO;
19777c478bd9Sstevel@tonic-gate 		}
19787c478bd9Sstevel@tonic-gate 		if (hasmntopt(&m, MNTOPT_NOFORCEDIRECTIO) != NULL) {
19797c478bd9Sstevel@tonic-gate 			argp->flags &= ~(NFSMNT_DIRECTIO);
19807c478bd9Sstevel@tonic-gate 		}
19817c478bd9Sstevel@tonic-gate 
19827c478bd9Sstevel@tonic-gate 		/*
19837c478bd9Sstevel@tonic-gate 		 * Set up security data for argp->nfs_ext_u.nfs_extB.secdata.
19847c478bd9Sstevel@tonic-gate 		 */
19857c478bd9Sstevel@tonic-gate 		if (mfssnego.snego_done) {
19867c478bd9Sstevel@tonic-gate 			memcpy(&nfs_sec, &mfssnego.nfs_sec,
19877c478bd9Sstevel@tonic-gate 			    sizeof (seconfig_t));
19887c478bd9Sstevel@tonic-gate 		} else if (!sec_opt) {
19897c478bd9Sstevel@tonic-gate 			/*
19907c478bd9Sstevel@tonic-gate 			 * Get default security mode.
19917c478bd9Sstevel@tonic-gate 			 */
19927c478bd9Sstevel@tonic-gate 			if (nfs_getseconfig_default(&nfs_sec)) {
19937c478bd9Sstevel@tonic-gate 				syslog(loglevel,
19947c478bd9Sstevel@tonic-gate 				    "error getting default security entry\n");
19957c478bd9Sstevel@tonic-gate 				free_knconf(argp->knconf);
19967c478bd9Sstevel@tonic-gate 				netbuf_free(argp->addr);
19977c478bd9Sstevel@tonic-gate 				freenetconfigent(nconf);
199839d3e169Sevanl 				if (argp->hostname)
199939d3e169Sevanl 					free(argp->hostname);
20007c478bd9Sstevel@tonic-gate 				free(argp->fh);
20017c478bd9Sstevel@tonic-gate 				free(argp);
20027c478bd9Sstevel@tonic-gate 				head = prevhead;
20037c478bd9Sstevel@tonic-gate 				tail = prevtail;
20047c478bd9Sstevel@tonic-gate 				if (tail)
20057c478bd9Sstevel@tonic-gate 					tail->nfs_ext_u.nfs_extB.next = NULL;
20067c478bd9Sstevel@tonic-gate 				last_error = NFSERR_NOSPC;
20077c478bd9Sstevel@tonic-gate 				destroy_auth_client_handle(cl);
20087c478bd9Sstevel@tonic-gate 				skipentry = 1;
20097c478bd9Sstevel@tonic-gate 				mfs->mfs_ignore = 1;
20107c478bd9Sstevel@tonic-gate 				continue;
20117c478bd9Sstevel@tonic-gate 			}
20127c478bd9Sstevel@tonic-gate 			argp->flags |= NFSMNT_SECDEFAULT;
20137c478bd9Sstevel@tonic-gate 		}
20147c478bd9Sstevel@tonic-gate 
20157c478bd9Sstevel@tonic-gate 		/*
20167c478bd9Sstevel@tonic-gate 		 * For AUTH_DH
20177c478bd9Sstevel@tonic-gate 		 * get the network address for the time service on
20187c478bd9Sstevel@tonic-gate 		 * the server.	If an RPC based time service is
20197c478bd9Sstevel@tonic-gate 		 * not available then try the IP time service.
20207c478bd9Sstevel@tonic-gate 		 *
20217c478bd9Sstevel@tonic-gate 		 * Eventurally, we want to move this code to nfs_clnt_secdata()
20227c478bd9Sstevel@tonic-gate 		 * when autod_nfs.c and mount.c can share the same
20232f172c55SRobert Thurlow 		 * get_the_addr/get_netconfig_info routine.
20247c478bd9Sstevel@tonic-gate 		 */
20257c478bd9Sstevel@tonic-gate 		secflags = 0;
20267c478bd9Sstevel@tonic-gate 		syncaddr = NULL;
20277c478bd9Sstevel@tonic-gate 		retaddrs = NULL;
20287c478bd9Sstevel@tonic-gate 
20297c478bd9Sstevel@tonic-gate 		if (nfs_sec.sc_rpcnum == AUTH_DH || nfsvers == NFS_V4) {
20307c478bd9Sstevel@tonic-gate 		/*
20317c478bd9Sstevel@tonic-gate 		 * If not using the public fh and not NFS_V4, we can try
20327c478bd9Sstevel@tonic-gate 		 * talking RPCBIND. Otherwise, assume that firewalls
20337c478bd9Sstevel@tonic-gate 		 * prevent us from doing that.
20347c478bd9Sstevel@tonic-gate 		 */
20357c478bd9Sstevel@tonic-gate 			if ((mfs->mfs_flags & MFS_FH_VIA_WEBNFS) == 0 &&
20367c478bd9Sstevel@tonic-gate 			    nfsvers != NFS_V4) {
20372f172c55SRobert Thurlow 				enum clnt_stat cstat;
20382f172c55SRobert Thurlow 				syncaddr = get_server_netinfo(SERVER_ADDR,
20392f172c55SRobert Thurlow 				    host, RPCBPROG, RPCBVERS, NULL, &nconf,
20402f172c55SRobert Thurlow 				    NULL, 0, NULL, NULL, FALSE, NULL, &cstat);
20417c478bd9Sstevel@tonic-gate 			}
20427c478bd9Sstevel@tonic-gate 
20437c478bd9Sstevel@tonic-gate 			if (syncaddr != NULL) {
20447c478bd9Sstevel@tonic-gate 				/* for flags in sec_data */
20457c478bd9Sstevel@tonic-gate 				secflags |= AUTH_F_RPCTIMESYNC;
20467c478bd9Sstevel@tonic-gate 			} else {
20477c478bd9Sstevel@tonic-gate 				struct nd_hostserv hs;
20487c478bd9Sstevel@tonic-gate 				int error;
20497c478bd9Sstevel@tonic-gate 
20507c478bd9Sstevel@tonic-gate 				hs.h_host = host;
20517c478bd9Sstevel@tonic-gate 				hs.h_serv = "timserver";
20527c478bd9Sstevel@tonic-gate 				error = netdir_getbyname(nconf, &hs, &retaddrs);
20537c478bd9Sstevel@tonic-gate 
20541bbc88acSrm15945 				if (error != ND_OK &&
20551bbc88acSrm15945 				    nfs_sec.sc_rpcnum == AUTH_DH) {
20567c478bd9Sstevel@tonic-gate 					syslog(loglevel,
20571bbc88acSrm15945 					    "%s: secure: no time service\n",
20581bbc88acSrm15945 					    host);
20597c478bd9Sstevel@tonic-gate 					free_knconf(argp->knconf);
20607c478bd9Sstevel@tonic-gate 					netbuf_free(argp->addr);
20617c478bd9Sstevel@tonic-gate 					freenetconfigent(nconf);
206239d3e169Sevanl 					if (argp->hostname)
206339d3e169Sevanl 						free(argp->hostname);
20647c478bd9Sstevel@tonic-gate 					free(argp->fh);
20657c478bd9Sstevel@tonic-gate 					free(argp);
20667c478bd9Sstevel@tonic-gate 					head = prevhead;
20677c478bd9Sstevel@tonic-gate 					tail = prevtail;
20687c478bd9Sstevel@tonic-gate 					if (tail)
20691bbc88acSrm15945 						tail->nfs_ext_u.nfs_extB.next =
20701bbc88acSrm15945 						    NULL;
20717c478bd9Sstevel@tonic-gate 					last_error = NFSERR_IO;
20727c478bd9Sstevel@tonic-gate 					destroy_auth_client_handle(cl);
20737c478bd9Sstevel@tonic-gate 					skipentry = 1;
20747c478bd9Sstevel@tonic-gate 					mfs->mfs_ignore = 1;
20757c478bd9Sstevel@tonic-gate 					continue;
20767c478bd9Sstevel@tonic-gate 				}
20777c478bd9Sstevel@tonic-gate 
20787c478bd9Sstevel@tonic-gate 				if (error == ND_OK)
20797c478bd9Sstevel@tonic-gate 					syncaddr = retaddrs->n_addrs;
20807c478bd9Sstevel@tonic-gate 
20817c478bd9Sstevel@tonic-gate 			/*
20827c478bd9Sstevel@tonic-gate 			 * For potential usage by NFS V4 when AUTH_DH
20837c478bd9Sstevel@tonic-gate 			 * is negotiated via SECINFO in the kernel.
20847c478bd9Sstevel@tonic-gate 			 */
20857c478bd9Sstevel@tonic-gate 				if (nfsvers == NFS_V4 && syncaddr &&
20867c478bd9Sstevel@tonic-gate 				    host2netname(netname, host, NULL)) {
20871bbc88acSrm15945 					argp->syncaddr =
20881bbc88acSrm15945 					    malloc(sizeof (struct netbuf));
20891bbc88acSrm15945 					argp->syncaddr->buf =
20901bbc88acSrm15945 					    malloc(syncaddr->len);
20917c478bd9Sstevel@tonic-gate 					(void) memcpy(argp->syncaddr->buf,
20927c478bd9Sstevel@tonic-gate 					    syncaddr->buf, syncaddr->len);
20937c478bd9Sstevel@tonic-gate 					argp->syncaddr->len = syncaddr->len;
20941bbc88acSrm15945 					argp->syncaddr->maxlen =
20951bbc88acSrm15945 					    syncaddr->maxlen;
20967c478bd9Sstevel@tonic-gate 					argp->netname = strdup(netname);
20977c478bd9Sstevel@tonic-gate 					argp->flags |= NFSMNT_SECURE;
20987c478bd9Sstevel@tonic-gate 				}
20997c478bd9Sstevel@tonic-gate 			} /* syncaddr */
21007c478bd9Sstevel@tonic-gate 		} /* AUTH_DH */
21017c478bd9Sstevel@tonic-gate 
210245916cd2Sjpk 		/*
210345916cd2Sjpk 		 * TSOL notes: automountd in tsol extension
210445916cd2Sjpk 		 * has "read down" capability, i.e. we allow
210545916cd2Sjpk 		 * a user to trigger an nfs mount into a lower
210645916cd2Sjpk 		 * labeled zone. We achieve this by always having
210745916cd2Sjpk 		 * root issue the mount request so that the
210845916cd2Sjpk 		 * lookup ops can go past /zone/<zone_name>
210945916cd2Sjpk 		 * on the server side.
211045916cd2Sjpk 		 */
211145916cd2Sjpk 		if (is_system_labeled())
211245916cd2Sjpk 			nfs_sec.sc_uid = (uid_t)0;
211345916cd2Sjpk 		else
21143bfb48feSsemery 			nfs_sec.sc_uid = uid;
21157c478bd9Sstevel@tonic-gate 		/*
21167c478bd9Sstevel@tonic-gate 		 * If AUTH_DH is a chosen flavor now, its data will be stored
21177c478bd9Sstevel@tonic-gate 		 * in the sec_data structure via nfs_clnt_secdata().
21187c478bd9Sstevel@tonic-gate 		 */
21197c478bd9Sstevel@tonic-gate 		if (!(secdata = nfs_clnt_secdata(&nfs_sec, host, argp->knconf,
21207c478bd9Sstevel@tonic-gate 		    syncaddr, secflags))) {
21217c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR,
21227c478bd9Sstevel@tonic-gate 			    "errors constructing security related data\n");
21237c478bd9Sstevel@tonic-gate 			if (secflags & AUTH_F_RPCTIMESYNC)
21247c478bd9Sstevel@tonic-gate 				netbuf_free(syncaddr);
21257c478bd9Sstevel@tonic-gate 			else if (retaddrs)
21267c478bd9Sstevel@tonic-gate 				netdir_free(retaddrs, ND_ADDRLIST);
21277c478bd9Sstevel@tonic-gate 			if (argp->syncaddr)
21287c478bd9Sstevel@tonic-gate 				netbuf_free(argp->syncaddr);
21297c478bd9Sstevel@tonic-gate 			if (argp->netname)
21307c478bd9Sstevel@tonic-gate 				free(argp->netname);
213139d3e169Sevanl 			if (argp->hostname)
213239d3e169Sevanl 				free(argp->hostname);
21337c478bd9Sstevel@tonic-gate 			free_knconf(argp->knconf);
21347c478bd9Sstevel@tonic-gate 			netbuf_free(argp->addr);
21357c478bd9Sstevel@tonic-gate 			freenetconfigent(nconf);
21367c478bd9Sstevel@tonic-gate 			free(argp->fh);
21377c478bd9Sstevel@tonic-gate 			free(argp);
21387c478bd9Sstevel@tonic-gate 			head = prevhead;
21397c478bd9Sstevel@tonic-gate 			tail = prevtail;
21407c478bd9Sstevel@tonic-gate 			if (tail)
21417c478bd9Sstevel@tonic-gate 				tail->nfs_ext_u.nfs_extB.next = NULL;
21427c478bd9Sstevel@tonic-gate 			last_error = NFSERR_IO;
21437c478bd9Sstevel@tonic-gate 			destroy_auth_client_handle(cl);
21447c478bd9Sstevel@tonic-gate 			skipentry = 1;
21457c478bd9Sstevel@tonic-gate 			mfs->mfs_ignore = 1;
21467c478bd9Sstevel@tonic-gate 			continue;
21477c478bd9Sstevel@tonic-gate 		}
21487c478bd9Sstevel@tonic-gate 		NFS_ARGS_EXTB_secdata(*argp, secdata);
21497c478bd9Sstevel@tonic-gate 		/* end of security stuff */
21507c478bd9Sstevel@tonic-gate 
21517c478bd9Sstevel@tonic-gate 		if (trace > 4)
21527c478bd9Sstevel@tonic-gate 			trace_prt(1,
21537c478bd9Sstevel@tonic-gate 			    "  nfsmount: have secure info for %s\n", remname);
21547c478bd9Sstevel@tonic-gate 
21557c478bd9Sstevel@tonic-gate 		if (hasmntopt(&m, MNTOPT_GRPID) != NULL) {
21567c478bd9Sstevel@tonic-gate 			argp->flags |= NFSMNT_GRPID;
21577c478bd9Sstevel@tonic-gate 		}
21587c478bd9Sstevel@tonic-gate 		if (nopt(&m, MNTOPT_RSIZE, &argp->rsize)) {
21597c478bd9Sstevel@tonic-gate 			argp->flags |= NFSMNT_RSIZE;
21607c478bd9Sstevel@tonic-gate 		}
21617c478bd9Sstevel@tonic-gate 		if (nopt(&m, MNTOPT_WSIZE, &argp->wsize)) {
21627c478bd9Sstevel@tonic-gate 			argp->flags |= NFSMNT_WSIZE;
21637c478bd9Sstevel@tonic-gate 		}
21647c478bd9Sstevel@tonic-gate 		if (nopt(&m, MNTOPT_TIMEO, &argp->timeo)) {
21657c478bd9Sstevel@tonic-gate 			argp->flags |= NFSMNT_TIMEO;
21667c478bd9Sstevel@tonic-gate 		}
21677c478bd9Sstevel@tonic-gate 		if (nopt(&m, MNTOPT_RETRANS, &argp->retrans)) {
21687c478bd9Sstevel@tonic-gate 			argp->flags |= NFSMNT_RETRANS;
21697c478bd9Sstevel@tonic-gate 		}
21707c478bd9Sstevel@tonic-gate 		if (nopt(&m, MNTOPT_ACTIMEO, &argp->acregmax)) {
21717c478bd9Sstevel@tonic-gate 			argp->flags |= NFSMNT_ACREGMAX;
21727c478bd9Sstevel@tonic-gate 			argp->flags |= NFSMNT_ACDIRMAX;
21737c478bd9Sstevel@tonic-gate 			argp->flags |= NFSMNT_ACDIRMIN;
21747c478bd9Sstevel@tonic-gate 			argp->flags |= NFSMNT_ACREGMIN;
21757c478bd9Sstevel@tonic-gate 			argp->acdirmin = argp->acregmin = argp->acdirmax
21767c478bd9Sstevel@tonic-gate 			    = argp->acregmax;
21777c478bd9Sstevel@tonic-gate 		} else {
21787c478bd9Sstevel@tonic-gate 			if (nopt(&m, MNTOPT_ACREGMIN, &argp->acregmin)) {
21797c478bd9Sstevel@tonic-gate 				argp->flags |= NFSMNT_ACREGMIN;
21807c478bd9Sstevel@tonic-gate 			}
21817c478bd9Sstevel@tonic-gate 			if (nopt(&m, MNTOPT_ACREGMAX, &argp->acregmax)) {
21827c478bd9Sstevel@tonic-gate 				argp->flags |= NFSMNT_ACREGMAX;
21837c478bd9Sstevel@tonic-gate 			}
21847c478bd9Sstevel@tonic-gate 			if (nopt(&m, MNTOPT_ACDIRMIN, &argp->acdirmin)) {
21857c478bd9Sstevel@tonic-gate 				argp->flags |= NFSMNT_ACDIRMIN;
21867c478bd9Sstevel@tonic-gate 			}
21877c478bd9Sstevel@tonic-gate 			if (nopt(&m, MNTOPT_ACDIRMAX, &argp->acdirmax)) {
21887c478bd9Sstevel@tonic-gate 				argp->flags |= NFSMNT_ACDIRMAX;
21897c478bd9Sstevel@tonic-gate 			}
21907c478bd9Sstevel@tonic-gate 		}
21917c478bd9Sstevel@tonic-gate 
21927c478bd9Sstevel@tonic-gate 		if (posix) {
21937c478bd9Sstevel@tonic-gate 			argp->pathconf = NULL;
21947c478bd9Sstevel@tonic-gate 			if (error = get_pathconf(cl, dir, remname,
21957c478bd9Sstevel@tonic-gate 			    &argp->pathconf, retries)) {
21967c478bd9Sstevel@tonic-gate 				if (secflags & AUTH_F_RPCTIMESYNC)
21977c478bd9Sstevel@tonic-gate 					netbuf_free(syncaddr);
21987c478bd9Sstevel@tonic-gate 				else if (retaddrs)
21997c478bd9Sstevel@tonic-gate 					netdir_free(retaddrs, ND_ADDRLIST);
22007c478bd9Sstevel@tonic-gate 				free_knconf(argp->knconf);
22017c478bd9Sstevel@tonic-gate 				netbuf_free(argp->addr);
22027c478bd9Sstevel@tonic-gate 				freenetconfigent(nconf);
22037c478bd9Sstevel@tonic-gate 				nfs_free_secdata(
22047c478bd9Sstevel@tonic-gate 				    argp->nfs_ext_u.nfs_extB.secdata);
22057c478bd9Sstevel@tonic-gate 				if (argp->syncaddr)
22067c478bd9Sstevel@tonic-gate 					netbuf_free(argp->syncaddr);
22077c478bd9Sstevel@tonic-gate 				if (argp->netname)
22087c478bd9Sstevel@tonic-gate 					free(argp->netname);
220939d3e169Sevanl 				if (argp->hostname)
221039d3e169Sevanl 					free(argp->hostname);
22117c478bd9Sstevel@tonic-gate 				free(argp->fh);
22127c478bd9Sstevel@tonic-gate 				free(argp);
22137c478bd9Sstevel@tonic-gate 				head = prevhead;
22147c478bd9Sstevel@tonic-gate 				tail = prevtail;
22157c478bd9Sstevel@tonic-gate 				if (tail)
22167c478bd9Sstevel@tonic-gate 					tail->nfs_ext_u.nfs_extB.next = NULL;
22177c478bd9Sstevel@tonic-gate 				last_error = NFSERR_IO;
22187c478bd9Sstevel@tonic-gate 
22197c478bd9Sstevel@tonic-gate 				if (error == RET_RETRY && retries-- > 0) {
22207c478bd9Sstevel@tonic-gate 					destroy_auth_client_handle(cl);
22217c478bd9Sstevel@tonic-gate 					DELAY(delay);
22227c478bd9Sstevel@tonic-gate 					goto retry;
22237c478bd9Sstevel@tonic-gate 				}
22247c478bd9Sstevel@tonic-gate 
22257c478bd9Sstevel@tonic-gate 				destroy_auth_client_handle(cl);
22267c478bd9Sstevel@tonic-gate 				skipentry = 1;
22277c478bd9Sstevel@tonic-gate 				mfs->mfs_ignore = 1;
22287c478bd9Sstevel@tonic-gate 				continue;
22297c478bd9Sstevel@tonic-gate 			}
22307c478bd9Sstevel@tonic-gate 			argp->flags |= NFSMNT_POSIX;
22317c478bd9Sstevel@tonic-gate 			if (trace > 4)
22327c478bd9Sstevel@tonic-gate 				trace_prt(1,
22337c478bd9Sstevel@tonic-gate 				    "  nfsmount: have pathconf for %s\n",
22347c478bd9Sstevel@tonic-gate 				    remname);
22357c478bd9Sstevel@tonic-gate 		}
22367c478bd9Sstevel@tonic-gate 
22377c478bd9Sstevel@tonic-gate 		/*
22387c478bd9Sstevel@tonic-gate 		 * free loop-specific data structures
22397c478bd9Sstevel@tonic-gate 		 */
22407c478bd9Sstevel@tonic-gate 		destroy_auth_client_handle(cl);
22417c478bd9Sstevel@tonic-gate 		freenetconfigent(nconf);
22427c478bd9Sstevel@tonic-gate 		if (secflags & AUTH_F_RPCTIMESYNC)
22437c478bd9Sstevel@tonic-gate 			netbuf_free(syncaddr);
22447c478bd9Sstevel@tonic-gate 		else if (retaddrs)
22457c478bd9Sstevel@tonic-gate 			netdir_free(retaddrs, ND_ADDRLIST);
22467c478bd9Sstevel@tonic-gate 
22477c478bd9Sstevel@tonic-gate 		/*
22487c478bd9Sstevel@tonic-gate 		 * Decide whether to use remote host's lockd or local locking.
22497c478bd9Sstevel@tonic-gate 		 * If we are using the public fh, we've already turned
22507c478bd9Sstevel@tonic-gate 		 * LLOCK on.
22517c478bd9Sstevel@tonic-gate 		 */
22527c478bd9Sstevel@tonic-gate 		if (hasmntopt(&m, MNTOPT_LLOCK))
22537c478bd9Sstevel@tonic-gate 			argp->flags |= NFSMNT_LLOCK;
22547c478bd9Sstevel@tonic-gate 		if (!(argp->flags & NFSMNT_LLOCK) && nfsvers == NFS_VERSION &&
22557c478bd9Sstevel@tonic-gate 		    remote_lock(host, argp->fh)) {
22567c478bd9Sstevel@tonic-gate 			syslog(loglevel, "No network locking on %s : "
22577c478bd9Sstevel@tonic-gate 			"contact admin to install server change", host);
22587c478bd9Sstevel@tonic-gate 			argp->flags |= NFSMNT_LLOCK;
22597c478bd9Sstevel@tonic-gate 		}
22607c478bd9Sstevel@tonic-gate 
22617c478bd9Sstevel@tonic-gate 		/*
22627c478bd9Sstevel@tonic-gate 		 * Build a string for /etc/mnttab.
22637c478bd9Sstevel@tonic-gate 		 * If possible, coalesce strings with same 'dir' info.
22647c478bd9Sstevel@tonic-gate 		 */
22657c478bd9Sstevel@tonic-gate 		if ((mfs->mfs_flags & MFS_URL) == 0) {
22667c478bd9Sstevel@tonic-gate 			char *tmp;
22677c478bd9Sstevel@tonic-gate 
22687c478bd9Sstevel@tonic-gate 			if (mnttabcnt) {
22697c478bd9Sstevel@tonic-gate 				p = strrchr(mnttabtext, (int)':');
22707c478bd9Sstevel@tonic-gate 				if (!p || strcmp(p+1, dir) != 0) {
22717c478bd9Sstevel@tonic-gate 					mnttabcnt += strlen(remname) + 2;
22727c478bd9Sstevel@tonic-gate 				} else {
22737c478bd9Sstevel@tonic-gate 					*p = '\0';
22746a6d3e5eSjs195444 					mnttabcnt += strlen(rhost) + 2;
22757c478bd9Sstevel@tonic-gate 				}
22767c478bd9Sstevel@tonic-gate 				if ((tmp = realloc(mnttabtext,
22777c478bd9Sstevel@tonic-gate 				    mnttabcnt)) != NULL) {
22787c478bd9Sstevel@tonic-gate 					mnttabtext = tmp;
22797c478bd9Sstevel@tonic-gate 					strcat(mnttabtext, ",");
22807c478bd9Sstevel@tonic-gate 				} else {
22817c478bd9Sstevel@tonic-gate 					free(mnttabtext);
22827c478bd9Sstevel@tonic-gate 					mnttabtext = NULL;
22837c478bd9Sstevel@tonic-gate 				}
22847c478bd9Sstevel@tonic-gate 			} else {
22857c478bd9Sstevel@tonic-gate 				mnttabcnt = strlen(remname) + 1;
22867c478bd9Sstevel@tonic-gate 				if ((mnttabtext = malloc(mnttabcnt)) != NULL)
22877c478bd9Sstevel@tonic-gate 					mnttabtext[0] = '\0';
22887c478bd9Sstevel@tonic-gate 			}
22897c478bd9Sstevel@tonic-gate 
22907c478bd9Sstevel@tonic-gate 			if (mnttabtext != NULL)
22917c478bd9Sstevel@tonic-gate 				strcat(mnttabtext, remname);
22927c478bd9Sstevel@tonic-gate 
22937c478bd9Sstevel@tonic-gate 		} else {
22947c478bd9Sstevel@tonic-gate 			char *tmp;
22957c478bd9Sstevel@tonic-gate 			int more_cnt = 0;
22967c478bd9Sstevel@tonic-gate 			char sport[16];
22977c478bd9Sstevel@tonic-gate 
22987c478bd9Sstevel@tonic-gate 			more_cnt += strlen("nfs://");
22997c478bd9Sstevel@tonic-gate 			more_cnt += strlen(mfs->mfs_host);
23007c478bd9Sstevel@tonic-gate 
23017c478bd9Sstevel@tonic-gate 			if (mfs->mfs_port != 0) {
23027c478bd9Sstevel@tonic-gate 				(void) sprintf(sport, ":%u", mfs->mfs_port);
23037c478bd9Sstevel@tonic-gate 			} else
23047c478bd9Sstevel@tonic-gate 				sport[0] = '\0';
23057c478bd9Sstevel@tonic-gate 
23067c478bd9Sstevel@tonic-gate 			more_cnt += strlen(sport);
23077c478bd9Sstevel@tonic-gate 			more_cnt += 1; /* "/" */
23087c478bd9Sstevel@tonic-gate 			more_cnt += strlen(mfs->mfs_dir);
23097c478bd9Sstevel@tonic-gate 
23107c478bd9Sstevel@tonic-gate 			if (mnttabcnt) {
23117c478bd9Sstevel@tonic-gate 				more_cnt += 1; /* "," */
23127c478bd9Sstevel@tonic-gate 				mnttabcnt += more_cnt;
23137c478bd9Sstevel@tonic-gate 
23147c478bd9Sstevel@tonic-gate 				if ((tmp = realloc(mnttabtext,
23157c478bd9Sstevel@tonic-gate 				    mnttabcnt)) != NULL) {
23167c478bd9Sstevel@tonic-gate 					mnttabtext = tmp;
23177c478bd9Sstevel@tonic-gate 					strcat(mnttabtext, ",");
23187c478bd9Sstevel@tonic-gate 				} else {
23197c478bd9Sstevel@tonic-gate 					free(mnttabtext);
23207c478bd9Sstevel@tonic-gate 					mnttabtext = NULL;
23217c478bd9Sstevel@tonic-gate 				}
23227c478bd9Sstevel@tonic-gate 			} else {
23237c478bd9Sstevel@tonic-gate 				mnttabcnt = more_cnt + 1;
23247c478bd9Sstevel@tonic-gate 				if ((mnttabtext = malloc(mnttabcnt)) != NULL)
23257c478bd9Sstevel@tonic-gate 					mnttabtext[0] = '\0';
23267c478bd9Sstevel@tonic-gate 			}
23277c478bd9Sstevel@tonic-gate 
23287c478bd9Sstevel@tonic-gate 			if (mnttabtext != NULL) {
23297c478bd9Sstevel@tonic-gate 				strcat(mnttabtext, "nfs://");
23307c478bd9Sstevel@tonic-gate 				strcat(mnttabtext, mfs->mfs_host);
23317c478bd9Sstevel@tonic-gate 				strcat(mnttabtext, sport);
23327c478bd9Sstevel@tonic-gate 				strcat(mnttabtext, "/");
23337c478bd9Sstevel@tonic-gate 				strcat(mnttabtext, mfs->mfs_dir);
23347c478bd9Sstevel@tonic-gate 			}
23357c478bd9Sstevel@tonic-gate 		}
23367c478bd9Sstevel@tonic-gate 
23377c478bd9Sstevel@tonic-gate 		if (!mnttabtext) {
23387c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "nfsmount: no memory");
23397c478bd9Sstevel@tonic-gate 			last_error = NFSERR_IO;
23407c478bd9Sstevel@tonic-gate 			goto out;
23417c478bd9Sstevel@tonic-gate 		}
23427c478bd9Sstevel@tonic-gate 
23437c478bd9Sstevel@tonic-gate 		/*
23447c478bd9Sstevel@tonic-gate 		 * At least one entry, can call mount(2).
23457c478bd9Sstevel@tonic-gate 		 */
23467c478bd9Sstevel@tonic-gate 		entries++;
23477c478bd9Sstevel@tonic-gate 
23487c478bd9Sstevel@tonic-gate 		/*
23497c478bd9Sstevel@tonic-gate 		 * If replication was defeated, don't do more work
23507c478bd9Sstevel@tonic-gate 		 */
23517c478bd9Sstevel@tonic-gate 		if (!replicated)
23527c478bd9Sstevel@tonic-gate 			break;
23537c478bd9Sstevel@tonic-gate 	}
23547c478bd9Sstevel@tonic-gate 
23557c478bd9Sstevel@tonic-gate 
23567c478bd9Sstevel@tonic-gate 	/*
23577c478bd9Sstevel@tonic-gate 	 * Did we get through all possibilities without success?
23587c478bd9Sstevel@tonic-gate 	 */
23597c478bd9Sstevel@tonic-gate 	if (!entries)
23607c478bd9Sstevel@tonic-gate 		goto out;
23617c478bd9Sstevel@tonic-gate 
23627c478bd9Sstevel@tonic-gate 	/* Make "xattr" the default if "noxattr" is not specified. */
23637c478bd9Sstevel@tonic-gate 	strcpy(mopts, opts);
23647c478bd9Sstevel@tonic-gate 	if (!hasmntopt(&m, MNTOPT_NOXATTR) && !hasmntopt(&m, MNTOPT_XATTR)) {
23657c478bd9Sstevel@tonic-gate 		if (strlen(mopts) > 0)
23667c478bd9Sstevel@tonic-gate 			strcat(mopts, ",");
23677c478bd9Sstevel@tonic-gate 		strcat(mopts, "xattr");
23687c478bd9Sstevel@tonic-gate 	}
23697c478bd9Sstevel@tonic-gate 
23707c478bd9Sstevel@tonic-gate 	/*
23717c478bd9Sstevel@tonic-gate 	 * enable services as needed.
23727c478bd9Sstevel@tonic-gate 	 */
23737c478bd9Sstevel@tonic-gate 	{
23747c478bd9Sstevel@tonic-gate 		char **sl;
23757c478bd9Sstevel@tonic-gate 
23767c478bd9Sstevel@tonic-gate 		if (strcmp(fstype, MNTTYPE_NFS4) == 0)
23777c478bd9Sstevel@tonic-gate 			sl = service_list_v4;
23787c478bd9Sstevel@tonic-gate 		else
23797c478bd9Sstevel@tonic-gate 			sl = service_list;
23807c478bd9Sstevel@tonic-gate 
238145916cd2Sjpk 		(void) _check_services(sl);
23827c478bd9Sstevel@tonic-gate 	}
23837c478bd9Sstevel@tonic-gate 
23847c478bd9Sstevel@tonic-gate 	/*
23857c478bd9Sstevel@tonic-gate 	 * Whew; do the mount, at last.
23867c478bd9Sstevel@tonic-gate 	 */
23877c478bd9Sstevel@tonic-gate 	if (trace > 1) {
23887c478bd9Sstevel@tonic-gate 		trace_prt(1, "	mount %s %s (%s)\n", mnttabtext, mntpnt, mopts);
23897c478bd9Sstevel@tonic-gate 	}
23907c478bd9Sstevel@tonic-gate 
239139d3e169Sevanl 	/*
2392b9238976Sth199096 	 * About to do a nfs mount, make sure the mount_to is set for
2393b9238976Sth199096 	 * potential ephemeral mounts with NFSv4.
2394b9238976Sth199096 	 */
2395b9238976Sth199096 	set_nfsv4_ephemeral_mount_to();
2396b9238976Sth199096 
2397b9238976Sth199096 	/*
239839d3e169Sevanl 	 * If no action list pointer then do the mount, otherwise
239939d3e169Sevanl 	 * build the actions list pointer with the mount information.
240039d3e169Sevanl 	 * so the mount can be done in the kernel.
240139d3e169Sevanl 	 */
240239d3e169Sevanl 	if (alp == NULL) {
24037c478bd9Sstevel@tonic-gate 		if (mount(mnttabtext, mntpnt, flags | MS_DATA, fstype,
24047c478bd9Sstevel@tonic-gate 		    head, sizeof (*head), mopts, MAX_MNTOPT_STR) < 0) {
24057c478bd9Sstevel@tonic-gate 			if (trace > 1)
24067c478bd9Sstevel@tonic-gate 				trace_prt(1, "	Mount of %s on %s: %d\n",
24077c478bd9Sstevel@tonic-gate 				    mnttabtext, mntpnt, errno);
24087c478bd9Sstevel@tonic-gate 			if (errno != EBUSY || verbose)
24097c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR,
24107c478bd9Sstevel@tonic-gate 				"Mount of %s on %s: %m", mnttabtext, mntpnt);
24117c478bd9Sstevel@tonic-gate 			last_error = NFSERR_IO;
24127c478bd9Sstevel@tonic-gate 			goto out;
24137c478bd9Sstevel@tonic-gate 		}
24147c478bd9Sstevel@tonic-gate 
24157c478bd9Sstevel@tonic-gate 		last_error = NFS_OK;
24167c478bd9Sstevel@tonic-gate 		if (stat(mntpnt, &stbuf) == 0) {
24177c478bd9Sstevel@tonic-gate 			if (trace > 1) {
24187c478bd9Sstevel@tonic-gate 				trace_prt(1, "	mount %s dev=%x rdev=%x OK\n",
24197c478bd9Sstevel@tonic-gate 				    mnttabtext, stbuf.st_dev, stbuf.st_rdev);
24207c478bd9Sstevel@tonic-gate 			}
24217c478bd9Sstevel@tonic-gate 		} else {
24227c478bd9Sstevel@tonic-gate 			if (trace > 1) {
24237c478bd9Sstevel@tonic-gate 				trace_prt(1, "	mount %s OK\n", mnttabtext);
24247c478bd9Sstevel@tonic-gate 				trace_prt(1, "	stat of %s failed\n", mntpnt);
24257c478bd9Sstevel@tonic-gate 			}
242639d3e169Sevanl 
242739d3e169Sevanl 		}
242839d3e169Sevanl 	} else {
242939d3e169Sevanl 		alp->action.action = AUTOFS_MOUNT_RQ;
243039d3e169Sevanl 		alp->action.action_list_entry_u.mounta.spec =
243139d3e169Sevanl 		    strdup(mnttabtext);
243239d3e169Sevanl 		alp->action.action_list_entry_u.mounta.dir = strdup(mntpnt);
243339d3e169Sevanl 		alp->action.action_list_entry_u.mounta.flags =
243439d3e169Sevanl 		    flags | MS_DATA;
243539d3e169Sevanl 		alp->action.action_list_entry_u.mounta.fstype =
243639d3e169Sevanl 		    strdup(fstype);
243739d3e169Sevanl 		alp->action.action_list_entry_u.mounta.dataptr = (char *)head;
243839d3e169Sevanl 		alp->action.action_list_entry_u.mounta.datalen =
243939d3e169Sevanl 		    sizeof (*head);
244039d3e169Sevanl 		mntopts = malloc(strlen(mopts) + 1);
244139d3e169Sevanl 		strcpy(mntopts, mopts);
244239d3e169Sevanl 		mntopts[strlen(mopts)] = '\0';
244339d3e169Sevanl 		alp->action.action_list_entry_u.mounta.optptr = mntopts;
244439d3e169Sevanl 		alp->action.action_list_entry_u.mounta.optlen =
244539d3e169Sevanl 		    strlen(mntopts) + 1;
244639d3e169Sevanl 		last_error = NFS_OK;
244739d3e169Sevanl 		goto ret;
24487c478bd9Sstevel@tonic-gate 	}
24497c478bd9Sstevel@tonic-gate 
24507c478bd9Sstevel@tonic-gate out:
24517c478bd9Sstevel@tonic-gate 	argp = head;
24527c478bd9Sstevel@tonic-gate 	while (argp) {
24537c478bd9Sstevel@tonic-gate 		if (argp->pathconf)
24547c478bd9Sstevel@tonic-gate 			free(argp->pathconf);
24557c478bd9Sstevel@tonic-gate 		free_knconf(argp->knconf);
24567c478bd9Sstevel@tonic-gate 		netbuf_free(argp->addr);
24577c478bd9Sstevel@tonic-gate 		if (argp->syncaddr)
24587c478bd9Sstevel@tonic-gate 			netbuf_free(argp->syncaddr);
24597c478bd9Sstevel@tonic-gate 		if (argp->netname) {
24607c478bd9Sstevel@tonic-gate 			free(argp->netname);
24617c478bd9Sstevel@tonic-gate 		}
246239d3e169Sevanl 		if (argp->hostname)
246339d3e169Sevanl 			free(argp->hostname);
24647c478bd9Sstevel@tonic-gate 		nfs_free_secdata(argp->nfs_ext_u.nfs_extB.secdata);
24657c478bd9Sstevel@tonic-gate 		free(argp->fh);
24667c478bd9Sstevel@tonic-gate 		head = argp;
24677c478bd9Sstevel@tonic-gate 		argp = argp->nfs_ext_u.nfs_extB.next;
24687c478bd9Sstevel@tonic-gate 		free(head);
24697c478bd9Sstevel@tonic-gate 	}
24707c478bd9Sstevel@tonic-gate ret:
24717c478bd9Sstevel@tonic-gate 	if (nfs_proto)
24727c478bd9Sstevel@tonic-gate 		free(nfs_proto);
24737c478bd9Sstevel@tonic-gate 	if (mnttabtext)
24747c478bd9Sstevel@tonic-gate 		free(mnttabtext);
24757c478bd9Sstevel@tonic-gate 
24767c478bd9Sstevel@tonic-gate 	for (mfs = mfs_in; mfs; mfs = mfs->mfs_next) {
24777c478bd9Sstevel@tonic-gate 
24787c478bd9Sstevel@tonic-gate 		if (mfs->mfs_flags & MFS_ALLOC_DIR) {
24797c478bd9Sstevel@tonic-gate 			free(mfs->mfs_dir);
24807c478bd9Sstevel@tonic-gate 			mfs->mfs_dir = NULL;
24817c478bd9Sstevel@tonic-gate 			mfs->mfs_flags &= ~MFS_ALLOC_DIR;
24827c478bd9Sstevel@tonic-gate 		}
24837c478bd9Sstevel@tonic-gate 
248439d3e169Sevanl 		if (mfs->mfs_args != NULL && alp == NULL) {
24857c478bd9Sstevel@tonic-gate 			free(mfs->mfs_args);
24867c478bd9Sstevel@tonic-gate 			mfs->mfs_args = NULL;
24877c478bd9Sstevel@tonic-gate 		}
24887c478bd9Sstevel@tonic-gate 
24897c478bd9Sstevel@tonic-gate 		if (mfs->mfs_nconf != NULL) {
24907c478bd9Sstevel@tonic-gate 			freenetconfigent(mfs->mfs_nconf);
24917c478bd9Sstevel@tonic-gate 			mfs->mfs_nconf = NULL;
24927c478bd9Sstevel@tonic-gate 		}
24937c478bd9Sstevel@tonic-gate 	}
24947c478bd9Sstevel@tonic-gate 
24957c478bd9Sstevel@tonic-gate 	return (last_error);
24967c478bd9Sstevel@tonic-gate }
24977c478bd9Sstevel@tonic-gate 
24987c478bd9Sstevel@tonic-gate /*
24997c478bd9Sstevel@tonic-gate  * get_pathconf(cl, path, fsname, pcnf, cretries)
25007c478bd9Sstevel@tonic-gate  * ugliness that requires that ppathcnf and pathcnf stay consistent
25017c478bd9Sstevel@tonic-gate  * cretries is a copy of retries used to determine when to syslog
25027c478bd9Sstevel@tonic-gate  * on retry situations.
25037c478bd9Sstevel@tonic-gate  */
25047c478bd9Sstevel@tonic-gate static int
get_pathconf(CLIENT * cl,char * path,char * fsname,struct pathcnf ** pcnf,int cretries)25057c478bd9Sstevel@tonic-gate get_pathconf(CLIENT *cl, char *path, char *fsname, struct pathcnf **pcnf,
25067c478bd9Sstevel@tonic-gate 	int cretries)
25077c478bd9Sstevel@tonic-gate {
25087c478bd9Sstevel@tonic-gate 	struct ppathcnf *p = NULL;
25097c478bd9Sstevel@tonic-gate 	enum clnt_stat rpc_stat;
25107c478bd9Sstevel@tonic-gate 	struct timeval timeout;
25117c478bd9Sstevel@tonic-gate 
25127c478bd9Sstevel@tonic-gate 	p = (struct ppathcnf *)malloc(sizeof (struct ppathcnf));
25137c478bd9Sstevel@tonic-gate 	if (p == NULL) {
25147c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "get_pathconf: Out of memory");
25157c478bd9Sstevel@tonic-gate 		return (RET_ERR);
25167c478bd9Sstevel@tonic-gate 	}
25177c478bd9Sstevel@tonic-gate 	memset((caddr_t)p, 0, sizeof (struct ppathcnf));
25187c478bd9Sstevel@tonic-gate 
25197c478bd9Sstevel@tonic-gate 	timeout.tv_sec = 10;
25207c478bd9Sstevel@tonic-gate 	timeout.tv_usec = 0;
25217c478bd9Sstevel@tonic-gate 	rpc_stat = clnt_call(cl, MOUNTPROC_PATHCONF,
25227c478bd9Sstevel@tonic-gate 	    xdr_dirpath, (caddr_t)&path, xdr_ppathcnf, (caddr_t)p, timeout);
25237c478bd9Sstevel@tonic-gate 	if (rpc_stat != RPC_SUCCESS) {
25247c478bd9Sstevel@tonic-gate 		if (cretries-- <= 0) {
25257c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR,
25267c478bd9Sstevel@tonic-gate 			    "get_pathconf: %s: server not responding: %s",
25277c478bd9Sstevel@tonic-gate 			    fsname, clnt_sperror(cl, ""));
25287c478bd9Sstevel@tonic-gate 		}
25297c478bd9Sstevel@tonic-gate 		free(p);
25307c478bd9Sstevel@tonic-gate 		return (RET_RETRY);
25317c478bd9Sstevel@tonic-gate 	}
25327c478bd9Sstevel@tonic-gate 	if (_PC_ISSET(_PC_ERROR, p->pc_mask)) {
25337c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "get_pathconf: no info for %s", fsname);
25347c478bd9Sstevel@tonic-gate 		free(p);
25357c478bd9Sstevel@tonic-gate 		return (RET_ERR);
25367c478bd9Sstevel@tonic-gate 	}
25377c478bd9Sstevel@tonic-gate 	*pcnf = (struct pathcnf *)p;
25387c478bd9Sstevel@tonic-gate 	return (RET_OK);
25397c478bd9Sstevel@tonic-gate }
25407c478bd9Sstevel@tonic-gate 
254139d3e169Sevanl void
netbuf_free(nb)25427c478bd9Sstevel@tonic-gate netbuf_free(nb)
25437c478bd9Sstevel@tonic-gate 	struct netbuf *nb;
25447c478bd9Sstevel@tonic-gate {
25457c478bd9Sstevel@tonic-gate 	if (nb == NULL)
25467c478bd9Sstevel@tonic-gate 		return;
25477c478bd9Sstevel@tonic-gate 	if (nb->buf)
25487c478bd9Sstevel@tonic-gate 		free(nb->buf);
25497c478bd9Sstevel@tonic-gate 	free(nb);
25507c478bd9Sstevel@tonic-gate }
25517c478bd9Sstevel@tonic-gate 
25527c478bd9Sstevel@tonic-gate #define	SMALL_HOSTNAME		20
25537c478bd9Sstevel@tonic-gate #define	SMALL_PROTONAME		10
25547c478bd9Sstevel@tonic-gate #define	SMALL_PROTOFMLYNAME		10
25557c478bd9Sstevel@tonic-gate 
25567c478bd9Sstevel@tonic-gate struct portmap_cache {
25577c478bd9Sstevel@tonic-gate 	int cache_prog;
25587c478bd9Sstevel@tonic-gate 	int cache_vers;
25597c478bd9Sstevel@tonic-gate 	time_t cache_time;
25607c478bd9Sstevel@tonic-gate 	char cache_small_hosts[SMALL_HOSTNAME + 1];
25617c478bd9Sstevel@tonic-gate 	char *cache_hostname;
25627c478bd9Sstevel@tonic-gate 	char *cache_proto;
25637c478bd9Sstevel@tonic-gate 	char *cache_protofmly;
25647c478bd9Sstevel@tonic-gate 	char cache_small_protofmly[SMALL_PROTOFMLYNAME + 1];
25657c478bd9Sstevel@tonic-gate 	char cache_small_proto[SMALL_PROTONAME + 1];
25667c478bd9Sstevel@tonic-gate 	struct netbuf cache_srv_addr;
25677c478bd9Sstevel@tonic-gate 	struct portmap_cache *cache_prev, *cache_next;
25687c478bd9Sstevel@tonic-gate };
25697c478bd9Sstevel@tonic-gate 
25707c478bd9Sstevel@tonic-gate rwlock_t portmap_cache_lock;
25717c478bd9Sstevel@tonic-gate static int portmap_cache_valid_time = 30;
25727c478bd9Sstevel@tonic-gate struct portmap_cache *portmap_cache_head, *portmap_cache_tail;
25737c478bd9Sstevel@tonic-gate 
25747c478bd9Sstevel@tonic-gate #ifdef MALLOC_DEBUG
25757c478bd9Sstevel@tonic-gate void
portmap_cache_flush()25767c478bd9Sstevel@tonic-gate portmap_cache_flush()
25777c478bd9Sstevel@tonic-gate {
25787c478bd9Sstevel@tonic-gate 	struct  portmap_cache *next = NULL, *cp;
25797c478bd9Sstevel@tonic-gate 
25807c478bd9Sstevel@tonic-gate 	(void) rw_wrlock(&portmap_cache_lock);
25817c478bd9Sstevel@tonic-gate 	for (cp = portmap_cache_head; cp; cp = cp->cache_next) {
25827c478bd9Sstevel@tonic-gate 		if (cp->cache_hostname != NULL &&
25837c478bd9Sstevel@tonic-gate 		    cp->cache_hostname !=
25847c478bd9Sstevel@tonic-gate 		    cp->cache_small_hosts)
25857c478bd9Sstevel@tonic-gate 			free(cp->cache_hostname);
25867c478bd9Sstevel@tonic-gate 		if (cp->cache_proto != NULL &&
25877c478bd9Sstevel@tonic-gate 		    cp->cache_proto !=
25887c478bd9Sstevel@tonic-gate 		    cp->cache_small_proto)
25897c478bd9Sstevel@tonic-gate 			free(cp->cache_proto);
25907c478bd9Sstevel@tonic-gate 		if (cp->cache_srv_addr.buf != NULL)
25917c478bd9Sstevel@tonic-gate 			free(cp->cache_srv_addr.buf);
25927c478bd9Sstevel@tonic-gate 		next = cp->cache_next;
25937c478bd9Sstevel@tonic-gate 		free(cp);
25947c478bd9Sstevel@tonic-gate 	}
25957c478bd9Sstevel@tonic-gate 	portmap_cache_head = NULL;
25967c478bd9Sstevel@tonic-gate 	portmap_cache_tail = NULL;
25977c478bd9Sstevel@tonic-gate 	(void) rw_unlock(&portmap_cache_lock);
25987c478bd9Sstevel@tonic-gate }
25997c478bd9Sstevel@tonic-gate #endif
26007c478bd9Sstevel@tonic-gate 
26017c478bd9Sstevel@tonic-gate /*
26027c478bd9Sstevel@tonic-gate  * Returns 1 if the entry is found in the cache, 0 otherwise.
26037c478bd9Sstevel@tonic-gate  */
26047c478bd9Sstevel@tonic-gate static int
portmap_cache_lookup(hostname,prog,vers,nconf,addrp)26057c478bd9Sstevel@tonic-gate portmap_cache_lookup(hostname, prog, vers, nconf, addrp)
26067c478bd9Sstevel@tonic-gate 	char *hostname;
26077c478bd9Sstevel@tonic-gate 	rpcprog_t prog;
26087c478bd9Sstevel@tonic-gate 	rpcvers_t vers;
26097c478bd9Sstevel@tonic-gate 	struct netconfig *nconf;
26107c478bd9Sstevel@tonic-gate 	struct netbuf *addrp;
26117c478bd9Sstevel@tonic-gate {
26127c478bd9Sstevel@tonic-gate 	struct	portmap_cache *cachep, *prev, *next = NULL, *cp;
26137c478bd9Sstevel@tonic-gate 	int	retval = 0;
26147c478bd9Sstevel@tonic-gate 
26157c478bd9Sstevel@tonic-gate 	timenow = time(NULL);
26167c478bd9Sstevel@tonic-gate 
26177c478bd9Sstevel@tonic-gate 	(void) rw_rdlock(&portmap_cache_lock);
26187c478bd9Sstevel@tonic-gate 
26197c478bd9Sstevel@tonic-gate 	/*
26207c478bd9Sstevel@tonic-gate 	 * Increment the portmap cache counters for # accesses and lookups
26217c478bd9Sstevel@tonic-gate 	 * Use a smaller factor (100 vs 1000 for the host cache) since
26227c478bd9Sstevel@tonic-gate 	 * initial analysis shows this cache is looked up 10% that of the
26237c478bd9Sstevel@tonic-gate 	 * host cache.
26247c478bd9Sstevel@tonic-gate 	 */
26257c478bd9Sstevel@tonic-gate #ifdef CACHE_DEBUG
26267c478bd9Sstevel@tonic-gate 	portmap_cache_accesses++;
26277c478bd9Sstevel@tonic-gate 	portmap_cache_lookups++;
26287c478bd9Sstevel@tonic-gate 	if ((portmap_cache_lookups%100) == 0)
26297c478bd9Sstevel@tonic-gate 		trace_portmap_cache();
26307c478bd9Sstevel@tonic-gate #endif /* CACHE_DEBUG */
26317c478bd9Sstevel@tonic-gate 
26327c478bd9Sstevel@tonic-gate 	for (cachep = portmap_cache_head; cachep;
26337c478bd9Sstevel@tonic-gate 		cachep = cachep->cache_next) {
26347c478bd9Sstevel@tonic-gate 		if (timenow > cachep->cache_time) {
26357c478bd9Sstevel@tonic-gate 			/*
26367c478bd9Sstevel@tonic-gate 			 * We stumbled across an entry in the cache which
26377c478bd9Sstevel@tonic-gate 			 * has timed out. Free up all the entries that
26387c478bd9Sstevel@tonic-gate 			 * were added before it, which will positionally
26397c478bd9Sstevel@tonic-gate 			 * be after this entry. And adjust neighboring
26407c478bd9Sstevel@tonic-gate 			 * pointers.
26417c478bd9Sstevel@tonic-gate 			 * When we drop the lock and re-acquire it, we
26427c478bd9Sstevel@tonic-gate 			 * need to start from the beginning.
26437c478bd9Sstevel@tonic-gate 			 */
26447c478bd9Sstevel@tonic-gate 			(void) rw_unlock(&portmap_cache_lock);
26457c478bd9Sstevel@tonic-gate 			(void) rw_wrlock(&portmap_cache_lock);
26467c478bd9Sstevel@tonic-gate 			for (cp = portmap_cache_head;
26477c478bd9Sstevel@tonic-gate 				cp && (cp->cache_time >= timenow);
26487c478bd9Sstevel@tonic-gate 				cp = cp->cache_next)
26497c478bd9Sstevel@tonic-gate 				;
26507c478bd9Sstevel@tonic-gate 			if (cp == NULL)
26517c478bd9Sstevel@tonic-gate 				goto done;
26527c478bd9Sstevel@tonic-gate 			/*
26537c478bd9Sstevel@tonic-gate 			 * Adjust the link of the predecessor.
26547c478bd9Sstevel@tonic-gate 			 * Make the tail point to the new last entry.
26557c478bd9Sstevel@tonic-gate 			 */
26567c478bd9Sstevel@tonic-gate 			prev = cp->cache_prev;
26577c478bd9Sstevel@tonic-gate 			if (prev == NULL) {
26587c478bd9Sstevel@tonic-gate 				portmap_cache_head = NULL;
26597c478bd9Sstevel@tonic-gate 				portmap_cache_tail = NULL;
26607c478bd9Sstevel@tonic-gate 			} else {
26617c478bd9Sstevel@tonic-gate 				prev->cache_next = NULL;
26627c478bd9Sstevel@tonic-gate 				portmap_cache_tail = prev;
26637c478bd9Sstevel@tonic-gate 			}
26647c478bd9Sstevel@tonic-gate 			for (; cp; cp = next) {
26657c478bd9Sstevel@tonic-gate 				if (cp->cache_hostname != NULL &&
26667c478bd9Sstevel@tonic-gate 				    cp->cache_hostname !=
26677c478bd9Sstevel@tonic-gate 				    cp->cache_small_hosts)
26687c478bd9Sstevel@tonic-gate 					free(cp->cache_hostname);
26697c478bd9Sstevel@tonic-gate 				if (cp->cache_proto != NULL &&
26707c478bd9Sstevel@tonic-gate 				    cp->cache_proto !=
26717c478bd9Sstevel@tonic-gate 				    cp->cache_small_proto)
26727c478bd9Sstevel@tonic-gate 					free(cp->cache_proto);
26737c478bd9Sstevel@tonic-gate 				if (cp->cache_srv_addr.buf != NULL)
26747c478bd9Sstevel@tonic-gate 					free(cp->cache_srv_addr.buf);
26757c478bd9Sstevel@tonic-gate 				next = cp->cache_next;
26767c478bd9Sstevel@tonic-gate 				free(cp);
26777c478bd9Sstevel@tonic-gate 			}
26787c478bd9Sstevel@tonic-gate 			goto done;
26797c478bd9Sstevel@tonic-gate 		}
26807c478bd9Sstevel@tonic-gate 		if (cachep->cache_hostname == NULL ||
26817c478bd9Sstevel@tonic-gate 		    prog != cachep->cache_prog || vers != cachep->cache_vers ||
26827c478bd9Sstevel@tonic-gate 		    strcmp(nconf->nc_proto, cachep->cache_proto) != 0 ||
26837c478bd9Sstevel@tonic-gate 		    strcmp(nconf->nc_protofmly, cachep->cache_protofmly) != 0 ||
26847c478bd9Sstevel@tonic-gate 		    strcmp(hostname, cachep->cache_hostname) != 0)
26857c478bd9Sstevel@tonic-gate 			continue;
26867c478bd9Sstevel@tonic-gate 		/*
26877c478bd9Sstevel@tonic-gate 		 * Cache Hit.
26887c478bd9Sstevel@tonic-gate 		 */
26897c478bd9Sstevel@tonic-gate #ifdef CACHE_DEBUG
26907c478bd9Sstevel@tonic-gate 		portmap_cache_hits++;	/* up portmap cache hit counter */
26917c478bd9Sstevel@tonic-gate #endif /* CACHE_DEBUG */
26927c478bd9Sstevel@tonic-gate 		addrp->len = cachep->cache_srv_addr.len;
26937c478bd9Sstevel@tonic-gate 		memcpy(addrp->buf, cachep->cache_srv_addr.buf, addrp->len);
26947c478bd9Sstevel@tonic-gate 		retval = 1;
26957c478bd9Sstevel@tonic-gate 		break;
26967c478bd9Sstevel@tonic-gate 	}
26977c478bd9Sstevel@tonic-gate done:
26987c478bd9Sstevel@tonic-gate 	(void) rw_unlock(&portmap_cache_lock);
26997c478bd9Sstevel@tonic-gate 	return (retval);
27007c478bd9Sstevel@tonic-gate }
27017c478bd9Sstevel@tonic-gate 
27027c478bd9Sstevel@tonic-gate static void
portmap_cache_enter(hostname,prog,vers,nconf,addrp)27037c478bd9Sstevel@tonic-gate portmap_cache_enter(hostname, prog, vers, nconf, addrp)
27047c478bd9Sstevel@tonic-gate 	char *hostname;
27057c478bd9Sstevel@tonic-gate 	rpcprog_t prog;
27067c478bd9Sstevel@tonic-gate 	rpcvers_t vers;
27077c478bd9Sstevel@tonic-gate 	struct netconfig *nconf;
27087c478bd9Sstevel@tonic-gate 	struct netbuf *addrp;
27097c478bd9Sstevel@tonic-gate {
27107c478bd9Sstevel@tonic-gate 	struct portmap_cache *cachep;
27117c478bd9Sstevel@tonic-gate 	int protofmlylen;
27127c478bd9Sstevel@tonic-gate 	int protolen, hostnamelen;
27137c478bd9Sstevel@tonic-gate 
27147c478bd9Sstevel@tonic-gate 	timenow = time(NULL);
27157c478bd9Sstevel@tonic-gate 
27167c478bd9Sstevel@tonic-gate 	cachep = malloc(sizeof (struct portmap_cache));
27177c478bd9Sstevel@tonic-gate 	if (cachep == NULL)
27187c478bd9Sstevel@tonic-gate 		return;
27197c478bd9Sstevel@tonic-gate 	memset((char *)cachep, 0, sizeof (*cachep));
27207c478bd9Sstevel@tonic-gate 
27217c478bd9Sstevel@tonic-gate 	hostnamelen = strlen(hostname);
27227c478bd9Sstevel@tonic-gate 	if (hostnamelen <= SMALL_HOSTNAME)
27237c478bd9Sstevel@tonic-gate 		cachep->cache_hostname = cachep->cache_small_hosts;
27247c478bd9Sstevel@tonic-gate 	else {
27257c478bd9Sstevel@tonic-gate 		cachep->cache_hostname = malloc(hostnamelen + 1);
27267c478bd9Sstevel@tonic-gate 		if (cachep->cache_hostname == NULL)
27277c478bd9Sstevel@tonic-gate 			goto nomem;
27287c478bd9Sstevel@tonic-gate 	}
27297c478bd9Sstevel@tonic-gate 	strcpy(cachep->cache_hostname, hostname);
27307c478bd9Sstevel@tonic-gate 	protolen = strlen(nconf->nc_proto);
27317c478bd9Sstevel@tonic-gate 	if (protolen <= SMALL_PROTONAME)
27327c478bd9Sstevel@tonic-gate 		cachep->cache_proto = cachep->cache_small_proto;
27337c478bd9Sstevel@tonic-gate 	else {
27347c478bd9Sstevel@tonic-gate 		cachep->cache_proto = malloc(protolen + 1);
27357c478bd9Sstevel@tonic-gate 		if (cachep->cache_proto == NULL)
27367c478bd9Sstevel@tonic-gate 			goto nomem;
27377c478bd9Sstevel@tonic-gate 	}
27387c478bd9Sstevel@tonic-gate 	protofmlylen = strlen(nconf->nc_protofmly);
27397c478bd9Sstevel@tonic-gate 	if (protofmlylen <= SMALL_PROTOFMLYNAME)
27407c478bd9Sstevel@tonic-gate 		cachep->cache_protofmly = cachep->cache_small_protofmly;
27417c478bd9Sstevel@tonic-gate 	else {
27427c478bd9Sstevel@tonic-gate 		cachep->cache_protofmly = malloc(protofmlylen + 1);
27437c478bd9Sstevel@tonic-gate 		if (cachep->cache_protofmly == NULL)
27447c478bd9Sstevel@tonic-gate 			goto nomem;
27457c478bd9Sstevel@tonic-gate 	}
27467c478bd9Sstevel@tonic-gate 
27477c478bd9Sstevel@tonic-gate 	strcpy(cachep->cache_proto, nconf->nc_proto);
27487c478bd9Sstevel@tonic-gate 	cachep->cache_prog = prog;
27497c478bd9Sstevel@tonic-gate 	cachep->cache_vers = vers;
27507c478bd9Sstevel@tonic-gate 	cachep->cache_time = timenow + portmap_cache_valid_time;
27517c478bd9Sstevel@tonic-gate 	cachep->cache_srv_addr.len = addrp->len;
27527c478bd9Sstevel@tonic-gate 	cachep->cache_srv_addr.buf = malloc(addrp->len);
27537c478bd9Sstevel@tonic-gate 	if (cachep->cache_srv_addr.buf == NULL)
27547c478bd9Sstevel@tonic-gate 		goto nomem;
27557c478bd9Sstevel@tonic-gate 	memcpy(cachep->cache_srv_addr.buf, addrp->buf, addrp->maxlen);
27567c478bd9Sstevel@tonic-gate 	cachep->cache_prev = NULL;
27577c478bd9Sstevel@tonic-gate 	(void) rw_wrlock(&portmap_cache_lock);
27587c478bd9Sstevel@tonic-gate 	/*
27597c478bd9Sstevel@tonic-gate 	 * There's a window in which we could have multiple threads making
27607c478bd9Sstevel@tonic-gate 	 * the same cache entry. This can be avoided by walking the cache
27617c478bd9Sstevel@tonic-gate 	 * once again here to check and see if there are duplicate entries
27627c478bd9Sstevel@tonic-gate 	 * (after grabbing the write lock). This isn't fatal and I'm not
27637c478bd9Sstevel@tonic-gate 	 * going to bother with this.
27647c478bd9Sstevel@tonic-gate 	 */
27657c478bd9Sstevel@tonic-gate #ifdef CACHE_DEBUG
27667c478bd9Sstevel@tonic-gate 	portmap_cache_accesses++;	/* up portmap cache access counter */
27677c478bd9Sstevel@tonic-gate #endif /* CACHE_DEBUG */
27687c478bd9Sstevel@tonic-gate 	cachep->cache_next = portmap_cache_head;
27697c478bd9Sstevel@tonic-gate 	if (portmap_cache_head != NULL)
27707c478bd9Sstevel@tonic-gate 		portmap_cache_head->cache_prev = cachep;
27717c478bd9Sstevel@tonic-gate 	portmap_cache_head = cachep;
27727c478bd9Sstevel@tonic-gate 	(void) rw_unlock(&portmap_cache_lock);
27737c478bd9Sstevel@tonic-gate 	return;
27747c478bd9Sstevel@tonic-gate 
27757c478bd9Sstevel@tonic-gate nomem:
27767c478bd9Sstevel@tonic-gate 	syslog(LOG_ERR, "portmap_cache_enter: Memory allocation failed");
27777c478bd9Sstevel@tonic-gate 	if (cachep->cache_srv_addr.buf)
27787c478bd9Sstevel@tonic-gate 		free(cachep->cache_srv_addr.buf);
27797c478bd9Sstevel@tonic-gate 	if (cachep->cache_proto && protolen > SMALL_PROTONAME)
27807c478bd9Sstevel@tonic-gate 		free(cachep->cache_proto);
27817c478bd9Sstevel@tonic-gate 	if (cachep->cache_hostname && hostnamelen > SMALL_HOSTNAME)
27827c478bd9Sstevel@tonic-gate 		free(cachep->cache_hostname);
27837c478bd9Sstevel@tonic-gate 	if (cachep->cache_protofmly && protofmlylen > SMALL_PROTOFMLYNAME)
27847c478bd9Sstevel@tonic-gate 		free(cachep->cache_protofmly);
27857c478bd9Sstevel@tonic-gate 	if (cachep)
27867c478bd9Sstevel@tonic-gate 		free(cachep);
27877c478bd9Sstevel@tonic-gate 	cachep = NULL;
27887c478bd9Sstevel@tonic-gate }
27897c478bd9Sstevel@tonic-gate 
27907c478bd9Sstevel@tonic-gate static int
get_cached_srv_addr(char * hostname,rpcprog_t prog,rpcvers_t vers,struct netconfig * nconf,struct netbuf * addrp)27917c478bd9Sstevel@tonic-gate get_cached_srv_addr(char *hostname, rpcprog_t prog, rpcvers_t vers,
27927c478bd9Sstevel@tonic-gate 	struct netconfig *nconf, struct netbuf *addrp)
27937c478bd9Sstevel@tonic-gate {
27947c478bd9Sstevel@tonic-gate 	if (portmap_cache_lookup(hostname, prog, vers, nconf, addrp))
27957c478bd9Sstevel@tonic-gate 		return (1);
27967c478bd9Sstevel@tonic-gate 	if (rpcb_getaddr(prog, vers, nconf, addrp, hostname) == 0)
27977c478bd9Sstevel@tonic-gate 		return (0);
27987c478bd9Sstevel@tonic-gate 	portmap_cache_enter(hostname, prog, vers, nconf, addrp);
27997c478bd9Sstevel@tonic-gate 	return (1);
28007c478bd9Sstevel@tonic-gate }
28017c478bd9Sstevel@tonic-gate 
28027c478bd9Sstevel@tonic-gate /*
28032f172c55SRobert Thurlow  * Get a network address on "hostname" for program "prog"
28042f172c55SRobert Thurlow  * with version "vers".  If the port number is specified (non zero)
28052f172c55SRobert Thurlow  * then try for a TCP/UDP transport and set the port number of the
28062f172c55SRobert Thurlow  * resulting IP address.
28077c478bd9Sstevel@tonic-gate  *
28082f172c55SRobert Thurlow  * If the address of a netconfig pointer was passed and
28092f172c55SRobert Thurlow  * if it's not null, use it as the netconfig otherwise
28102f172c55SRobert Thurlow  * assign the address of the netconfig that was used to
28112f172c55SRobert Thurlow  * establish contact with the service.
28127c478bd9Sstevel@tonic-gate  *
28132f172c55SRobert Thurlow  * tinfo argument is for matching the get_addr() defined in
28147c478bd9Sstevel@tonic-gate  * ../nfs/mount/mount.c
28157c478bd9Sstevel@tonic-gate  */
28162f172c55SRobert Thurlow 
28172f172c55SRobert Thurlow 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)28182f172c55SRobert Thurlow get_addr(char *hostname, rpcprog_t prog, rpcvers_t vers,
28192f172c55SRobert Thurlow 	struct netconfig **nconfp, char *proto, ushort_t port,
28202f172c55SRobert Thurlow 	struct t_info *tinfo)
28212f172c55SRobert Thurlow 
28222f172c55SRobert Thurlow {
28232f172c55SRobert Thurlow 	enum clnt_stat cstat;
28242f172c55SRobert Thurlow 
28252f172c55SRobert Thurlow 	return (get_server_netinfo(SERVER_ADDR, hostname, prog, vers, NULL,
28262f172c55SRobert Thurlow 		nconfp, proto, port, tinfo, NULL, FALSE, NULL, &cstat));
28272f172c55SRobert Thurlow }
28282f172c55SRobert Thurlow 
28292f172c55SRobert Thurlow 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)28302f172c55SRobert Thurlow get_pubfh(char *hostname, rpcvers_t vers, mfs_snego_t *mfssnego,
28312f172c55SRobert Thurlow 	struct netconfig **nconfp, char *proto, ushort_t port,
28322f172c55SRobert Thurlow 	struct t_info *tinfo, caddr_t *fhp, bool_t get_pubfh, char *fspath)
28332f172c55SRobert Thurlow {
28342f172c55SRobert Thurlow 	enum clnt_stat cstat;
28352f172c55SRobert Thurlow 
28362f172c55SRobert Thurlow 	return (get_server_netinfo(SERVER_FH, hostname, NFS_PROGRAM, vers,
28372f172c55SRobert Thurlow 	    mfssnego, nconfp, proto, port, tinfo, fhp, get_pubfh, fspath,
28382f172c55SRobert Thurlow 	    &cstat));
28392f172c55SRobert Thurlow }
28402f172c55SRobert Thurlow 
28412f172c55SRobert Thurlow 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)28422f172c55SRobert Thurlow get_ping(char *hostname, rpcprog_t prog, rpcvers_t vers,
28432f172c55SRobert Thurlow 	struct netconfig **nconfp, ushort_t port, bool_t direct_to_server)
28442f172c55SRobert Thurlow {
28452f172c55SRobert Thurlow 	enum clnt_stat cstat;
28462f172c55SRobert Thurlow 
28472f172c55SRobert Thurlow 	(void) get_server_netinfo(SERVER_PING, hostname, prog,
28482f172c55SRobert Thurlow 	    vers, NULL, nconfp, NULL, port, NULL, NULL,
28492f172c55SRobert Thurlow 	    direct_to_server, NULL, &cstat);
28502f172c55SRobert Thurlow 
28512f172c55SRobert Thurlow 	return (cstat);
28522f172c55SRobert Thurlow }
28532f172c55SRobert Thurlow 
285439d3e169Sevanl 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)28552f172c55SRobert Thurlow get_server_netinfo(
28567c478bd9Sstevel@tonic-gate 	enum type_of_stuff type_of_stuff,
28577c478bd9Sstevel@tonic-gate 	char *hostname,
28587c478bd9Sstevel@tonic-gate 	rpcprog_t prog,
28592f172c55SRobert Thurlow 	rpcvers_t vers,
28607c478bd9Sstevel@tonic-gate 	mfs_snego_t *mfssnego,
28612f172c55SRobert Thurlow 	struct netconfig **nconfp,
28622f172c55SRobert Thurlow 	char *proto,
28632f172c55SRobert Thurlow 	ushort_t port,			/* may be zero */
28647c478bd9Sstevel@tonic-gate 	struct t_info *tinfo,
28657c478bd9Sstevel@tonic-gate 	caddr_t *fhp,
28667c478bd9Sstevel@tonic-gate 	bool_t direct_to_server,
28677c478bd9Sstevel@tonic-gate 	char *fspath,
28682f172c55SRobert Thurlow 	enum clnt_stat *cstatp)
28697c478bd9Sstevel@tonic-gate {
28707c478bd9Sstevel@tonic-gate 	struct netbuf *nb = NULL;
28712f172c55SRobert Thurlow 	struct netconfig *nconf = NULL;
28722f172c55SRobert Thurlow 	NCONF_HANDLE *nc = NULL;
28732f172c55SRobert Thurlow 	int error = 0;
28742f172c55SRobert Thurlow 	int fd = 0;
28757c478bd9Sstevel@tonic-gate 	struct t_bind *tbind = NULL;
28762f172c55SRobert Thurlow 	int nthtry = FIRST_TRY;
28772f172c55SRobert Thurlow 
28782f172c55SRobert Thurlow 	if (nconfp && *nconfp) {
28792f172c55SRobert Thurlow 		return (get_netconfig_info(type_of_stuff, hostname,
28802f172c55SRobert Thurlow 		    prog, vers, nconf, port, tinfo, tbind, fhp,
28812f172c55SRobert Thurlow 		    direct_to_server, fspath, cstatp, mfssnego));
28822f172c55SRobert Thurlow 	}
28832f172c55SRobert Thurlow 
28842f172c55SRobert Thurlow 	/*
28852f172c55SRobert Thurlow 	 * No nconf passed in.
28862f172c55SRobert Thurlow 	 *
28872f172c55SRobert Thurlow 	 * Try to get a nconf from /etc/netconfig.
28882f172c55SRobert Thurlow 	 * First choice is COTS, second is CLTS unless proto
28892f172c55SRobert Thurlow 	 * is specified.  When we retry, we reset the
28902f172c55SRobert Thurlow 	 * netconfig list, so that we search the whole list
28912f172c55SRobert Thurlow 	 * for the next choice.
28922f172c55SRobert Thurlow 	 */
28932f172c55SRobert Thurlow 	if ((nc = setnetpath()) == NULL)
28942f172c55SRobert Thurlow 		goto done;
28952f172c55SRobert Thurlow 
28962f172c55SRobert Thurlow 	/*
28972f172c55SRobert Thurlow 	 * If proto is specified, then only search for the match,
28982f172c55SRobert Thurlow 	 * otherwise try COTS first, if failed, then try CLTS.
28992f172c55SRobert Thurlow 	 */
29002f172c55SRobert Thurlow 	if (proto) {
29012f172c55SRobert Thurlow 		while ((nconf = getnetpath(nc)) != NULL) {
29022f172c55SRobert Thurlow 			if (strcmp(nconf->nc_proto, proto))
29032f172c55SRobert Thurlow 				continue;
29042f172c55SRobert Thurlow 			/*
29052f172c55SRobert Thurlow 			 * If the port number is specified then TCP/UDP
29062f172c55SRobert Thurlow 			 * is needed. Otherwise any cots/clts will do.
29072f172c55SRobert Thurlow 			 */
29082f172c55SRobert Thurlow 			if (port)  {
29092f172c55SRobert Thurlow 				if ((strcmp(nconf->nc_protofmly, NC_INET) &&
29102f172c55SRobert Thurlow 				    strcmp(nconf->nc_protofmly, NC_INET6)) ||
29112f172c55SRobert Thurlow 				    (strcmp(nconf->nc_proto, NC_TCP) &&
29122f172c55SRobert Thurlow 				    strcmp(nconf->nc_proto, NC_UDP)))
29132f172c55SRobert Thurlow 					continue;
29142f172c55SRobert Thurlow 			}
29152f172c55SRobert Thurlow 			nb = get_netconfig_info(type_of_stuff, hostname,
29162f172c55SRobert Thurlow 			    prog, vers, nconf, port, tinfo, tbind, fhp,
29172f172c55SRobert Thurlow 			    direct_to_server, fspath, cstatp, mfssnego);
29182f172c55SRobert Thurlow 			if (*cstatp == RPC_SUCCESS)
29192f172c55SRobert Thurlow 				break;
29202f172c55SRobert Thurlow 
29212f172c55SRobert Thurlow 			assert(nb == NULL);
29222f172c55SRobert Thurlow 
29232f172c55SRobert Thurlow 		}
29242f172c55SRobert Thurlow 		if (nconf == NULL)
29252f172c55SRobert Thurlow 			goto done;
29262f172c55SRobert Thurlow 	} else {
29272f172c55SRobert Thurlow retry:
29282f172c55SRobert Thurlow 		while ((nconf = getnetpath(nc)) != NULL) {
29292f172c55SRobert Thurlow 			if (nconf->nc_flag & NC_VISIBLE) {
29302f172c55SRobert Thurlow 				if (nthtry == FIRST_TRY) {
29312f172c55SRobert Thurlow 					if ((nconf->nc_semantics ==
29322f172c55SRobert Thurlow 					    NC_TPI_COTS_ORD) ||
29332f172c55SRobert Thurlow 					    (nconf->nc_semantics ==
29342f172c55SRobert Thurlow 					    NC_TPI_COTS)) {
29352f172c55SRobert Thurlow 						if (port == 0)
29362f172c55SRobert Thurlow 							break;
29372f172c55SRobert Thurlow 						if ((strcmp(nconf->nc_protofmly,
29382f172c55SRobert Thurlow 						    NC_INET) == 0 ||
29392f172c55SRobert Thurlow 						    strcmp(nconf->nc_protofmly,
29402f172c55SRobert Thurlow 						    NC_INET6) == 0) &&
29412f172c55SRobert Thurlow 						    (strcmp(nconf->nc_proto,
29422f172c55SRobert Thurlow 						    NC_TCP) == 0))
29432f172c55SRobert Thurlow 							break;
29442f172c55SRobert Thurlow 					}
29452f172c55SRobert Thurlow 				}
29462f172c55SRobert Thurlow 				if (nthtry == SECOND_TRY) {
29472f172c55SRobert Thurlow 					if (nconf->nc_semantics ==
29482f172c55SRobert Thurlow 					    NC_TPI_CLTS) {
29492f172c55SRobert Thurlow 						if (port == 0)
29502f172c55SRobert Thurlow 							break;
29512f172c55SRobert Thurlow 						if ((strcmp(nconf->nc_protofmly,
29522f172c55SRobert Thurlow 						    NC_INET) == 0 ||
29532f172c55SRobert Thurlow 						    strcmp(nconf->nc_protofmly,
29542f172c55SRobert Thurlow 						    NC_INET6) == 0) &&
29552f172c55SRobert Thurlow 						    (strcmp(nconf->nc_proto,
29562f172c55SRobert Thurlow 						    NC_UDP) == 0))
29572f172c55SRobert Thurlow 							break;
29582f172c55SRobert Thurlow 					}
29592f172c55SRobert Thurlow 				}
29602f172c55SRobert Thurlow 			}
29612f172c55SRobert Thurlow 		}
29622f172c55SRobert Thurlow 
29632f172c55SRobert Thurlow 		if (nconf == NULL) {
29642f172c55SRobert Thurlow 			if (++nthtry <= MNT_PREF_LISTLEN) {
29652f172c55SRobert Thurlow 				endnetpath(nc);
29662f172c55SRobert Thurlow 				if ((nc = setnetpath()) == NULL)
29672f172c55SRobert Thurlow 					goto done;
29682f172c55SRobert Thurlow 				goto retry;
29692f172c55SRobert Thurlow 			} else
29702f172c55SRobert Thurlow 				goto done;
29712f172c55SRobert Thurlow 		} else {
29722f172c55SRobert Thurlow 			nb = get_netconfig_info(type_of_stuff, hostname,
29732f172c55SRobert Thurlow 			    prog, vers, nconf, port, tinfo, tbind, fhp,
29742f172c55SRobert Thurlow 			    direct_to_server, fspath, cstatp, mfssnego);
29752f172c55SRobert Thurlow 			if (*cstatp != RPC_SUCCESS)
29762f172c55SRobert Thurlow 				/*
29772f172c55SRobert Thurlow 				 * Continue the same search path in the
29782f172c55SRobert Thurlow 				 * netconfig db until no more matched nconf
29792f172c55SRobert Thurlow 				 * (nconf == NULL).
29802f172c55SRobert Thurlow 				 */
29812f172c55SRobert Thurlow 				goto retry;
29822f172c55SRobert Thurlow 		}
29832f172c55SRobert Thurlow 	}
29842f172c55SRobert Thurlow 
29852f172c55SRobert Thurlow 	/*
29862f172c55SRobert Thurlow 	 * Got nconf and nb.  Now dup the netconfig structure (nconf)
29872f172c55SRobert Thurlow 	 * and return it thru nconfp.
29882f172c55SRobert Thurlow 	 */
29892f172c55SRobert Thurlow 	if (nconf != NULL) {
29902f172c55SRobert Thurlow 		if ((*nconfp = getnetconfigent(nconf->nc_netid)) == NULL) {
29912f172c55SRobert Thurlow 			syslog(LOG_ERR, "no memory\n");
29922f172c55SRobert Thurlow 			free(nb);
29932f172c55SRobert Thurlow 			nb = NULL;
29942f172c55SRobert Thurlow 		}
29952f172c55SRobert Thurlow 	} else {
29962f172c55SRobert Thurlow 		*nconfp = NULL;
29972f172c55SRobert Thurlow 	}
29982f172c55SRobert Thurlow done:
29992f172c55SRobert Thurlow 	if (nc)
30002f172c55SRobert Thurlow 		endnetpath(nc);
30012f172c55SRobert Thurlow 	return (nb);
30022f172c55SRobert Thurlow }
30032f172c55SRobert Thurlow 
30042f172c55SRobert 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)30052f172c55SRobert Thurlow get_server_fh(char *hostname, rpcprog_t	prog, rpcvers_t	vers,
30062f172c55SRobert Thurlow 	mfs_snego_t *mfssnego, struct netconfig *nconf, ushort_t port,
30072f172c55SRobert Thurlow 	struct t_info *tinfo, struct t_bind *tbind, caddr_t *fhp,
30082f172c55SRobert Thurlow 	bool_t direct_to_server, char *fspath, enum clnt_stat *cstat)
30092f172c55SRobert Thurlow {
30107c478bd9Sstevel@tonic-gate 	AUTH *ah = NULL;
30117c478bd9Sstevel@tonic-gate 	AUTH *new_ah = NULL;
30127c478bd9Sstevel@tonic-gate 	struct snego_t	snego;
30132f172c55SRobert Thurlow 	enum clnt_stat cs = RPC_TIMEDOUT;
30142f172c55SRobert Thurlow 	struct timeval tv;
30152f172c55SRobert Thurlow 	bool_t file_handle = 1;
30162f172c55SRobert Thurlow 	enum snego_stat sec;
30172f172c55SRobert Thurlow 	CLIENT *cl = NULL;
30182f172c55SRobert Thurlow 	int fd = -1;
30192f172c55SRobert Thurlow 	struct netbuf *nb = NULL;
30207c478bd9Sstevel@tonic-gate 
30212f172c55SRobert Thurlow 	if (direct_to_server != TRUE)
30222f172c55SRobert Thurlow 		return (NULL);
30237c478bd9Sstevel@tonic-gate 
30247c478bd9Sstevel@tonic-gate 	if (prog == NFS_PROGRAM && vers == NFS_V4)
30257c478bd9Sstevel@tonic-gate 		if (strncasecmp(nconf->nc_proto, NC_UDP, strlen(NC_UDP)) == 0)
30267c478bd9Sstevel@tonic-gate 			goto done;
30277c478bd9Sstevel@tonic-gate 
30282f172c55SRobert Thurlow 	if ((fd = t_open(nconf->nc_device, O_RDWR, tinfo)) < 0)
30297c478bd9Sstevel@tonic-gate 		goto done;
30307c478bd9Sstevel@tonic-gate 
30317c478bd9Sstevel@tonic-gate 	/* LINTED pointer alignment */
30322f172c55SRobert Thurlow 	if ((tbind = (struct t_bind *)t_alloc(fd, T_BIND, T_ADDR)) == NULL)
30332f172c55SRobert Thurlow 		goto done;
30342f172c55SRobert Thurlow 
30352f172c55SRobert Thurlow 	if (setup_nb_parms(nconf, tbind, tinfo, hostname, fd,
30362f172c55SRobert Thurlow 	    direct_to_server, port, prog, vers, file_handle) < 0) {
30377c478bd9Sstevel@tonic-gate 		goto done;
30387c478bd9Sstevel@tonic-gate 	}
30397c478bd9Sstevel@tonic-gate 
30402f172c55SRobert Thurlow 	cl = clnt_tli_create(fd, nconf, &tbind->addr, prog, vers, 0, 0);
30417c478bd9Sstevel@tonic-gate 	if (cl == NULL)
30427c478bd9Sstevel@tonic-gate 		goto done;
30437c478bd9Sstevel@tonic-gate 
30447c478bd9Sstevel@tonic-gate 	ah = authsys_create_default();
30457c478bd9Sstevel@tonic-gate 	if (ah != NULL) {
30467c478bd9Sstevel@tonic-gate #ifdef MALLOC_DEBUG
30477c478bd9Sstevel@tonic-gate 		drop_alloc("AUTH_HANDLE", cl->cl_auth,
30487c478bd9Sstevel@tonic-gate 		    __FILE__, __LINE__);
30497c478bd9Sstevel@tonic-gate #endif
30507c478bd9Sstevel@tonic-gate 		AUTH_DESTROY(cl->cl_auth);
30517c478bd9Sstevel@tonic-gate 		cl->cl_auth = ah;
30527c478bd9Sstevel@tonic-gate #ifdef MALLOC_DEBUG
30537c478bd9Sstevel@tonic-gate 		add_alloc("AUTH_HANDLE", cl->cl_auth, 0,
30547c478bd9Sstevel@tonic-gate 		    __FILE__, __LINE__);
30557c478bd9Sstevel@tonic-gate #endif
30567c478bd9Sstevel@tonic-gate 	}
30577c478bd9Sstevel@tonic-gate 
30587c478bd9Sstevel@tonic-gate 	if (!mfssnego->snego_done && vers != NFS_V4) {
30597c478bd9Sstevel@tonic-gate 		/*
30607c478bd9Sstevel@tonic-gate 		 * negotiate sec flavor.
30617c478bd9Sstevel@tonic-gate 		 */
30627c478bd9Sstevel@tonic-gate 		snego.cnt = 0;
30637c478bd9Sstevel@tonic-gate 		if ((sec = nfs_sec_nego(vers, cl, fspath, &snego)) ==
30647c478bd9Sstevel@tonic-gate 		    SNEGO_SUCCESS) {
30657c478bd9Sstevel@tonic-gate 			int jj;
30667c478bd9Sstevel@tonic-gate 
30677c478bd9Sstevel@tonic-gate 			/*
30687c478bd9Sstevel@tonic-gate 			 * check if server supports the one
30697c478bd9Sstevel@tonic-gate 			 * specified in the sec= option.
30707c478bd9Sstevel@tonic-gate 			 */
30717c478bd9Sstevel@tonic-gate 			if (mfssnego->sec_opt) {
30727c478bd9Sstevel@tonic-gate 				for (jj = 0; jj < snego.cnt; jj++) {
30737c478bd9Sstevel@tonic-gate 					if (snego.array[jj] ==
30747c478bd9Sstevel@tonic-gate 					    mfssnego->nfs_sec.sc_nfsnum) {
30757c478bd9Sstevel@tonic-gate 						mfssnego->snego_done = TRUE;
30767c478bd9Sstevel@tonic-gate 						break;
30777c478bd9Sstevel@tonic-gate 					}
30787c478bd9Sstevel@tonic-gate 				}
30797c478bd9Sstevel@tonic-gate 			}
30807c478bd9Sstevel@tonic-gate 
30817c478bd9Sstevel@tonic-gate 			/*
30827c478bd9Sstevel@tonic-gate 			 * find a common sec flavor
30837c478bd9Sstevel@tonic-gate 			 */
30847c478bd9Sstevel@tonic-gate 			if (!mfssnego->snego_done) {
30857c478bd9Sstevel@tonic-gate 				for (jj = 0; jj < snego.cnt; jj++) {
30867c478bd9Sstevel@tonic-gate 					if (!nfs_getseconfig_bynumber(
30872f172c55SRobert Thurlow 					    snego.array[jj],
30882f172c55SRobert Thurlow 					    &mfssnego->nfs_sec)) {
30897c478bd9Sstevel@tonic-gate 						mfssnego->snego_done = TRUE;
30907c478bd9Sstevel@tonic-gate 						break;
30917c478bd9Sstevel@tonic-gate 					}
30927c478bd9Sstevel@tonic-gate 				}
30937c478bd9Sstevel@tonic-gate 			}
30947c478bd9Sstevel@tonic-gate 			if (!mfssnego->snego_done)
30952f172c55SRobert Thurlow 				goto done;
30967c478bd9Sstevel@tonic-gate 			/*
30977c478bd9Sstevel@tonic-gate 			 * Now that the flavor has been
30987c478bd9Sstevel@tonic-gate 			 * negotiated, get the fh.
30997c478bd9Sstevel@tonic-gate 			 *
31007c478bd9Sstevel@tonic-gate 			 * First, create an auth handle using the negotiated
31017c478bd9Sstevel@tonic-gate 			 * sec flavor in the next lookup to
31027c478bd9Sstevel@tonic-gate 			 * fetch the filehandle.
31037c478bd9Sstevel@tonic-gate 			 */
31047c478bd9Sstevel@tonic-gate 			new_ah = nfs_create_ah(cl, hostname,
31057c478bd9Sstevel@tonic-gate 			    &mfssnego->nfs_sec);
31067c478bd9Sstevel@tonic-gate 			if (new_ah  == NULL)
31077c478bd9Sstevel@tonic-gate 				goto done;
31087c478bd9Sstevel@tonic-gate #ifdef MALLOC_DEBUG
31097c478bd9Sstevel@tonic-gate 			drop_alloc("AUTH_HANDLE", cl->cl_auth,
31107c478bd9Sstevel@tonic-gate 			    __FILE__, __LINE__);
31117c478bd9Sstevel@tonic-gate #endif
31127c478bd9Sstevel@tonic-gate 			AUTH_DESTROY(cl->cl_auth);
31137c478bd9Sstevel@tonic-gate 			cl->cl_auth = new_ah;
31147c478bd9Sstevel@tonic-gate #ifdef MALLOC_DEBUG
31157c478bd9Sstevel@tonic-gate 			add_alloc("AUTH_HANDLE", cl->cl_auth, 0,
31167c478bd9Sstevel@tonic-gate 			    __FILE__, __LINE__);
31177c478bd9Sstevel@tonic-gate #endif
31187c478bd9Sstevel@tonic-gate 		} else if (sec == SNEGO_ARRAY_TOO_SMALL ||
31197c478bd9Sstevel@tonic-gate 		    sec == SNEGO_FAILURE) {
31207c478bd9Sstevel@tonic-gate 			goto done;
31217c478bd9Sstevel@tonic-gate 		}
31227c478bd9Sstevel@tonic-gate 	}
31237c478bd9Sstevel@tonic-gate 
31247c478bd9Sstevel@tonic-gate 	switch (vers) {
31257c478bd9Sstevel@tonic-gate 	case NFS_VERSION:
31267c478bd9Sstevel@tonic-gate 		{
31277c478bd9Sstevel@tonic-gate 		wnl_diropargs arg;
3128bfa62c28SVallish Vaidyeshwara 		wnl_diropres res;
31297c478bd9Sstevel@tonic-gate 
31307c478bd9Sstevel@tonic-gate 		memset((char *)&arg.dir, 0, sizeof (wnl_fh));
3131bfa62c28SVallish Vaidyeshwara 		memset((char *)&res, 0, sizeof (wnl_diropres));
31327c478bd9Sstevel@tonic-gate 		arg.name = fspath;
3133bfa62c28SVallish Vaidyeshwara 		if (wnlproc_lookup_2(&arg, &res, cl) !=
31342c2d21e9SRichard Lowe 		    RPC_SUCCESS || res.status != WNL_OK)
31357c478bd9Sstevel@tonic-gate 			goto done;
31367c478bd9Sstevel@tonic-gate 		*fhp = malloc(sizeof (wnl_fh));
31377c478bd9Sstevel@tonic-gate 
31387c478bd9Sstevel@tonic-gate 		if (*fhp == NULL) {
31397c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "no memory\n");
31407c478bd9Sstevel@tonic-gate 			goto done;
31417c478bd9Sstevel@tonic-gate 		}
31427c478bd9Sstevel@tonic-gate 
31437c478bd9Sstevel@tonic-gate 		memcpy((char *)*fhp,
3144bfa62c28SVallish Vaidyeshwara 		    (char *)&res.wnl_diropres_u.wnl_diropres.file,
31457c478bd9Sstevel@tonic-gate 		    sizeof (wnl_fh));
31467c478bd9Sstevel@tonic-gate 		cs = RPC_SUCCESS;
31477c478bd9Sstevel@tonic-gate 		}
31487c478bd9Sstevel@tonic-gate 		break;
31497c478bd9Sstevel@tonic-gate 	case NFS_V3:
31507c478bd9Sstevel@tonic-gate 		{
31517c478bd9Sstevel@tonic-gate 		WNL_LOOKUP3args arg;
3152bfa62c28SVallish Vaidyeshwara 		WNL_LOOKUP3res res;
31537c478bd9Sstevel@tonic-gate 		nfs_fh3 *fh3p;
31547c478bd9Sstevel@tonic-gate 
31557c478bd9Sstevel@tonic-gate 		memset((char *)&arg.what.dir, 0, sizeof (wnl_fh3));
3156bfa62c28SVallish Vaidyeshwara 		memset((char *)&res, 0, sizeof (WNL_LOOKUP3res));
31577c478bd9Sstevel@tonic-gate 		arg.what.name = fspath;
3158bfa62c28SVallish Vaidyeshwara 		if (wnlproc3_lookup_3(&arg, &res, cl) !=
31592c2d21e9SRichard Lowe 		    RPC_SUCCESS || res.status != WNL3_OK)
31607c478bd9Sstevel@tonic-gate 			goto done;
31617c478bd9Sstevel@tonic-gate 
31627c478bd9Sstevel@tonic-gate 		fh3p = (nfs_fh3 *)malloc(sizeof (*fh3p));
31637c478bd9Sstevel@tonic-gate 
31647c478bd9Sstevel@tonic-gate 		if (fh3p == NULL) {
31657c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "no memory\n");
31667c478bd9Sstevel@tonic-gate 			goto done;
31677c478bd9Sstevel@tonic-gate 		}
31687c478bd9Sstevel@tonic-gate 
31692f172c55SRobert Thurlow 		fh3p->fh3_length =
31702f172c55SRobert Thurlow 		    res.WNL_LOOKUP3res_u.res_ok.object.data.data_len;
31712f172c55SRobert Thurlow 		memcpy(fh3p->fh3_u.data,
31722f172c55SRobert Thurlow 		    res.WNL_LOOKUP3res_u.res_ok.object.data.data_val,
31737c478bd9Sstevel@tonic-gate 		    fh3p->fh3_length);
31747c478bd9Sstevel@tonic-gate 
31757c478bd9Sstevel@tonic-gate 		*fhp = (caddr_t)fh3p;
31767c478bd9Sstevel@tonic-gate 
31777c478bd9Sstevel@tonic-gate 		cs = RPC_SUCCESS;
31787c478bd9Sstevel@tonic-gate 		}
31797c478bd9Sstevel@tonic-gate 		break;
31807c478bd9Sstevel@tonic-gate 	case NFS_V4:
31811bbc88acSrm15945 		tv.tv_sec = 10;
31821bbc88acSrm15945 		tv.tv_usec = 0;
31831bbc88acSrm15945 		cs = clnt_call(cl, NULLPROC, xdr_void, 0,
31841bbc88acSrm15945 		    xdr_void, 0, tv);
31851bbc88acSrm15945 		if (cs != RPC_SUCCESS)
31861bbc88acSrm15945 			goto done;
31872f172c55SRobert Thurlow 
31887c478bd9Sstevel@tonic-gate 		*fhp = strdup(fspath);
31892f172c55SRobert Thurlow 		if (fhp == NULL) {
31907c478bd9Sstevel@tonic-gate 			cs = RPC_SYSTEMERROR;
31912f172c55SRobert Thurlow 			goto done;
31922f172c55SRobert Thurlow 		}
31932f172c55SRobert Thurlow 		break;
31942f172c55SRobert Thurlow 	}
31957c478bd9Sstevel@tonic-gate 	nb = (struct netbuf *)malloc(sizeof (struct netbuf));
31967c478bd9Sstevel@tonic-gate 	if (nb == NULL) {
31977c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "no memory\n");
31982f172c55SRobert Thurlow 		cs = RPC_SYSTEMERROR;
31997c478bd9Sstevel@tonic-gate 		goto done;
32007c478bd9Sstevel@tonic-gate 	}
32012f172c55SRobert Thurlow 	nb->buf = (char *)malloc(tbind->addr.maxlen);
32027c478bd9Sstevel@tonic-gate 	if (nb->buf == NULL) {
32037c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "no memory\n");
32047c478bd9Sstevel@tonic-gate 		free(nb);
32057c478bd9Sstevel@tonic-gate 		nb = NULL;
32062f172c55SRobert Thurlow 		cs = RPC_SYSTEMERROR;
32077c478bd9Sstevel@tonic-gate 		goto done;
32087c478bd9Sstevel@tonic-gate 	}
32097c478bd9Sstevel@tonic-gate 	(void) memcpy(nb->buf, tbind->addr.buf, tbind->addr.len);
32102f172c55SRobert Thurlow 	nb->len = tbind->addr.len;
32112f172c55SRobert Thurlow 	nb->maxlen = tbind->addr.maxlen;
32127c478bd9Sstevel@tonic-gate done:
32137c478bd9Sstevel@tonic-gate 	if (cstat != NULL)
32147c478bd9Sstevel@tonic-gate 		*cstat = cs;
32152f172c55SRobert Thurlow 	destroy_auth_client_handle(cl);
32162f172c55SRobert Thurlow 	cleanup_tli_parms(tbind, fd);
32177c478bd9Sstevel@tonic-gate 	return (nb);
32187c478bd9Sstevel@tonic-gate }
32197c478bd9Sstevel@tonic-gate 
32207c478bd9Sstevel@tonic-gate /*
32217c478bd9Sstevel@tonic-gate  * Sends a null call to the remote host's (NFS program, versp). versp
32227c478bd9Sstevel@tonic-gate  * may be "NULL" in which case the default maximum version is used.
32237c478bd9Sstevel@tonic-gate  * Upon return, versp contains the maximum version supported iff versp!= NULL.
32247c478bd9Sstevel@tonic-gate  */
32257c478bd9Sstevel@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)32267c478bd9Sstevel@tonic-gate pingnfs(
32277c478bd9Sstevel@tonic-gate 	char *hostpart,
32287c478bd9Sstevel@tonic-gate 	int attempts,
32297c478bd9Sstevel@tonic-gate 	rpcvers_t *versp,
32307c478bd9Sstevel@tonic-gate 	rpcvers_t versmin,
32316a6d3e5eSjs195444 	ushort_t port,			/* may be zero */
32327c478bd9Sstevel@tonic-gate 	bool_t usepub,
32337c478bd9Sstevel@tonic-gate 	char *path,
32347c478bd9Sstevel@tonic-gate 	char *proto)
32357c478bd9Sstevel@tonic-gate {
32367c478bd9Sstevel@tonic-gate 	CLIENT *cl = NULL;
32377c478bd9Sstevel@tonic-gate 	struct timeval rpc_to_new = {15, 0};
32387c478bd9Sstevel@tonic-gate 	static struct timeval rpc_rtrans_new = {-1, -1};
32397c478bd9Sstevel@tonic-gate 	enum clnt_stat clnt_stat;
32407c478bd9Sstevel@tonic-gate 	int i, j;
32417c478bd9Sstevel@tonic-gate 	rpcvers_t versmax;	/* maximum version to try against server */
32427c478bd9Sstevel@tonic-gate 	rpcvers_t outvers;	/* version supported by host on last call */
32437c478bd9Sstevel@tonic-gate 	rpcvers_t vers_to_try;	/* to try different versions against host */
32446a6d3e5eSjs195444 	char *hostname;
32457c478bd9Sstevel@tonic-gate 	struct netconfig *nconf;
32467c478bd9Sstevel@tonic-gate 
32476a6d3e5eSjs195444 	hostname = strdup(hostpart);
32486a6d3e5eSjs195444 	if (hostname == NULL) {
32496a6d3e5eSjs195444 		return (RPC_SYSTEMERROR);
32506a6d3e5eSjs195444 	}
32516a6d3e5eSjs195444 	unbracket(&hostname);
32526a6d3e5eSjs195444 
32537c478bd9Sstevel@tonic-gate 	if (path != NULL && strcmp(hostname, "nfs") == 0 &&
32547c478bd9Sstevel@tonic-gate 	    strncmp(path, "//", 2) == 0) {
32557c478bd9Sstevel@tonic-gate 		char *sport;
32567c478bd9Sstevel@tonic-gate 
32577c478bd9Sstevel@tonic-gate 		hostname = strdup(path+2);
32587c478bd9Sstevel@tonic-gate 
32597c478bd9Sstevel@tonic-gate 		if (hostname == NULL)
32607c478bd9Sstevel@tonic-gate 			return (RPC_SYSTEMERROR);
32617c478bd9Sstevel@tonic-gate 
32627c478bd9Sstevel@tonic-gate 		path = strchr(hostname, '/');
32637c478bd9Sstevel@tonic-gate 
32647c478bd9Sstevel@tonic-gate 		/*
32657c478bd9Sstevel@tonic-gate 		 * This cannot happen. If it does, give up
32667c478bd9Sstevel@tonic-gate 		 * on the ping as this is obviously a corrupt
32677c478bd9Sstevel@tonic-gate 		 * entry.
32687c478bd9Sstevel@tonic-gate 		 */
32697c478bd9Sstevel@tonic-gate 		if (path == NULL) {
32707c478bd9Sstevel@tonic-gate 			free(hostname);
32717c478bd9Sstevel@tonic-gate 			return (RPC_SUCCESS);
32727c478bd9Sstevel@tonic-gate 		}
32737c478bd9Sstevel@tonic-gate 
32747c478bd9Sstevel@tonic-gate 		/*
32757c478bd9Sstevel@tonic-gate 		 * Probable end point of host string.
32767c478bd9Sstevel@tonic-gate 		 */
32777c478bd9Sstevel@tonic-gate 		*path = '\0';
32787c478bd9Sstevel@tonic-gate 
32797c478bd9Sstevel@tonic-gate 		sport = strchr(hostname, ':');
32807c478bd9Sstevel@tonic-gate 
32817c478bd9Sstevel@tonic-gate 		if (sport != NULL && sport < path) {
32827c478bd9Sstevel@tonic-gate 
32837c478bd9Sstevel@tonic-gate 			/*
32847c478bd9Sstevel@tonic-gate 			 * Actual end point of host string.
32857c478bd9Sstevel@tonic-gate 			 */
32867c478bd9Sstevel@tonic-gate 			*sport = '\0';
32877c478bd9Sstevel@tonic-gate 			port = htons((ushort_t)atoi(sport+1));
32887c478bd9Sstevel@tonic-gate 		}
32897c478bd9Sstevel@tonic-gate 
32907c478bd9Sstevel@tonic-gate 		usepub = TRUE;
32917c478bd9Sstevel@tonic-gate 	}
32927c478bd9Sstevel@tonic-gate 
32937c478bd9Sstevel@tonic-gate 	/* Pick up the default versions and then set them appropriately */
32947c478bd9Sstevel@tonic-gate 	if (versp) {
32957c478bd9Sstevel@tonic-gate 		versmax = *versp;
32967c478bd9Sstevel@tonic-gate 		/* use versmin passed in */
32977c478bd9Sstevel@tonic-gate 	} else {
32987c478bd9Sstevel@tonic-gate 		read_default_nfs();
32997c478bd9Sstevel@tonic-gate 		set_versrange(0, &versmax, &versmin);
33007c478bd9Sstevel@tonic-gate 	}
33017c478bd9Sstevel@tonic-gate 
33027c478bd9Sstevel@tonic-gate 	if (proto &&
33037c478bd9Sstevel@tonic-gate 	    strncasecmp(proto, NC_UDP, strlen(NC_UDP)) == 0 &&
33047c478bd9Sstevel@tonic-gate 	    versmax == NFS_V4) {
33057c478bd9Sstevel@tonic-gate 		if (versmin == NFS_V4) {
33067c478bd9Sstevel@tonic-gate 			if (versp) {
33077c478bd9Sstevel@tonic-gate 				*versp = versmax - 1;
33087c478bd9Sstevel@tonic-gate 				return (RPC_SUCCESS);
33097c478bd9Sstevel@tonic-gate 			}
33107c478bd9Sstevel@tonic-gate 			return (RPC_PROGUNAVAIL);
33117c478bd9Sstevel@tonic-gate 		} else {
33127c478bd9Sstevel@tonic-gate 			versmax--;
33137c478bd9Sstevel@tonic-gate 		}
33147c478bd9Sstevel@tonic-gate 	}
33157c478bd9Sstevel@tonic-gate 
33167c478bd9Sstevel@tonic-gate 	if (versp)
33177c478bd9Sstevel@tonic-gate 		*versp = versmax;
33187c478bd9Sstevel@tonic-gate 
33197c478bd9Sstevel@tonic-gate 	switch (cache_check(hostname, versp, proto)) {
33207c478bd9Sstevel@tonic-gate 	case GOODHOST:
33217c478bd9Sstevel@tonic-gate 		if (hostname != hostpart)
33227c478bd9Sstevel@tonic-gate 			free(hostname);
33237c478bd9Sstevel@tonic-gate 		return (RPC_SUCCESS);
33247c478bd9Sstevel@tonic-gate 	case DEADHOST:
33257c478bd9Sstevel@tonic-gate 		if (hostname != hostpart)
33267c478bd9Sstevel@tonic-gate 			free(hostname);
33277c478bd9Sstevel@tonic-gate 		return (RPC_TIMEDOUT);
33287c478bd9Sstevel@tonic-gate 	case NOHOST:
33297c478bd9Sstevel@tonic-gate 	default:
33307c478bd9Sstevel@tonic-gate 		break;
33317c478bd9Sstevel@tonic-gate 	}
33327c478bd9Sstevel@tonic-gate 
33337c478bd9Sstevel@tonic-gate 	/*
33347c478bd9Sstevel@tonic-gate 	 * XXX The retransmission time rpcbrmttime is a global defined
33357c478bd9Sstevel@tonic-gate 	 * in the rpc library (rpcb_clnt.c). We use (and like) the default
33367c478bd9Sstevel@tonic-gate 	 * value of 15 sec in the rpc library. The code below is to protect
33377c478bd9Sstevel@tonic-gate 	 * us in case it changes. This need not be done under a lock since
33387c478bd9Sstevel@tonic-gate 	 * any # of threads entering this function will get the same
33397c478bd9Sstevel@tonic-gate 	 * retransmission value.
33407c478bd9Sstevel@tonic-gate 	 */
33417c478bd9Sstevel@tonic-gate 	if (rpc_rtrans_new.tv_sec == -1 && rpc_rtrans_new.tv_usec == -1) {
33427c478bd9Sstevel@tonic-gate 		__rpc_control(CLCR_GET_RPCB_RMTTIME, (char *)&rpc_rtrans_new);
33437c478bd9Sstevel@tonic-gate 		if (rpc_rtrans_new.tv_sec != 15 && rpc_rtrans_new.tv_sec != 0)
33447c478bd9Sstevel@tonic-gate 			if (trace > 1)
33457c478bd9Sstevel@tonic-gate 				trace_prt(1, "RPC library rttimer changed\n");
33467c478bd9Sstevel@tonic-gate 	}
33477c478bd9Sstevel@tonic-gate 
33487c478bd9Sstevel@tonic-gate 	/*
33497c478bd9Sstevel@tonic-gate 	 * XXX Manipulate the total timeout to get the number of
33507c478bd9Sstevel@tonic-gate 	 * desired retransmissions. This code is heavily dependant on
33517c478bd9Sstevel@tonic-gate 	 * the RPC backoff mechanism in clnt_dg_call (clnt_dg.c).
33527c478bd9Sstevel@tonic-gate 	 */
33537c478bd9Sstevel@tonic-gate 	for (i = 0, j = rpc_rtrans_new.tv_sec; i < attempts-1; i++) {
33547c478bd9Sstevel@tonic-gate 		if (j < RPC_MAX_BACKOFF)
33557c478bd9Sstevel@tonic-gate 			j *= 2;
33567c478bd9Sstevel@tonic-gate 		else
33577c478bd9Sstevel@tonic-gate 			j = RPC_MAX_BACKOFF;
33587c478bd9Sstevel@tonic-gate 		rpc_to_new.tv_sec += j;
33597c478bd9Sstevel@tonic-gate 	}
33607c478bd9Sstevel@tonic-gate 
33617c478bd9Sstevel@tonic-gate 	vers_to_try = versmax;
33627c478bd9Sstevel@tonic-gate 
33637c478bd9Sstevel@tonic-gate 	/*
33647c478bd9Sstevel@tonic-gate 	 * check the host's version within the timeout
33657c478bd9Sstevel@tonic-gate 	 */
33667c478bd9Sstevel@tonic-gate 	if (trace > 1)
33677c478bd9Sstevel@tonic-gate 		trace_prt(1, "	ping: %s timeout=%ld request vers=%d min=%d\n",
33687c478bd9Sstevel@tonic-gate 		    hostname, rpc_to_new.tv_sec, versmax, versmin);
33697c478bd9Sstevel@tonic-gate 
33707c478bd9Sstevel@tonic-gate 	if (usepub == FALSE) {
33717c478bd9Sstevel@tonic-gate 		do {
33727c478bd9Sstevel@tonic-gate 			/*
33737c478bd9Sstevel@tonic-gate 			 * If NFSv4, then we do the same thing as is used
33747c478bd9Sstevel@tonic-gate 			 * for public filehandles so that we avoid rpcbind
33757c478bd9Sstevel@tonic-gate 			 */
33767c478bd9Sstevel@tonic-gate 			if (vers_to_try == NFS_V4) {
33777c478bd9Sstevel@tonic-gate 				if (trace > 4) {
33787c478bd9Sstevel@tonic-gate 				trace_prt(1, "  pingnfs: Trying ping via "
33797c478bd9Sstevel@tonic-gate 				    "\"circuit_v\"\n");
33807c478bd9Sstevel@tonic-gate 				}
33817c478bd9Sstevel@tonic-gate 
33821bbc88acSrm15945 				cl = clnt_create_service_timed(hostname, "nfs",
33831bbc88acSrm15945 				    NFS_PROGRAM, vers_to_try,
33841bbc88acSrm15945 				    port, "circuit_v", &rpc_to_new);
33851bbc88acSrm15945 				if (cl != NULL) {
33867c478bd9Sstevel@tonic-gate 					outvers = vers_to_try;
33877c478bd9Sstevel@tonic-gate 					break;
33887c478bd9Sstevel@tonic-gate 				}
33897c478bd9Sstevel@tonic-gate 				if (trace > 4) {
33901bbc88acSrm15945 					trace_prt(1,
33911bbc88acSrm15945 					    "  pingnfs: Can't ping via "
33927c478bd9Sstevel@tonic-gate 					    "\"circuit_v\" %s: RPC error=%d\n",
33937c478bd9Sstevel@tonic-gate 					    hostname, rpc_createerr.cf_stat);
33947c478bd9Sstevel@tonic-gate 				}
33957c478bd9Sstevel@tonic-gate 
33967c478bd9Sstevel@tonic-gate 			} else {
33971bbc88acSrm15945 				cl = clnt_create_vers_timed(hostname,
33981bbc88acSrm15945 				    NFS_PROGRAM, &outvers, versmin, vers_to_try,
33991bbc88acSrm15945 				    "datagram_v", &rpc_to_new);
34001bbc88acSrm15945 				if (cl != NULL)
34017c478bd9Sstevel@tonic-gate 					break;
34027c478bd9Sstevel@tonic-gate 				if (trace > 4) {
34031bbc88acSrm15945 					trace_prt(1,
34041bbc88acSrm15945 					    "  pingnfs: Can't ping via "
34057c478bd9Sstevel@tonic-gate 					    "\"datagram_v\"%s: RPC error=%d\n",
34067c478bd9Sstevel@tonic-gate 					    hostname, rpc_createerr.cf_stat);
34077c478bd9Sstevel@tonic-gate 				}
34087c478bd9Sstevel@tonic-gate 				if (rpc_createerr.cf_stat == RPC_UNKNOWNHOST ||
34097c478bd9Sstevel@tonic-gate 				    rpc_createerr.cf_stat == RPC_TIMEDOUT)
34107c478bd9Sstevel@tonic-gate 					break;
34111bbc88acSrm15945 				if (rpc_createerr.cf_stat ==
34121bbc88acSrm15945 				    RPC_PROGNOTREGISTERED) {
34137c478bd9Sstevel@tonic-gate 					if (trace > 4) {
34141bbc88acSrm15945 						trace_prt(1,
34151bbc88acSrm15945 						    "  pingnfs: Trying ping "
34167c478bd9Sstevel@tonic-gate 						    "via \"circuit_v\"\n");
34177c478bd9Sstevel@tonic-gate 					}
34181bbc88acSrm15945 					cl = clnt_create_vers_timed(hostname,
34197c478bd9Sstevel@tonic-gate 					    NFS_PROGRAM, &outvers,
34207c478bd9Sstevel@tonic-gate 					    versmin, vers_to_try,
34211bbc88acSrm15945 					    "circuit_v", &rpc_to_new);
34221bbc88acSrm15945 					if (cl != NULL)
34237c478bd9Sstevel@tonic-gate 						break;
34247c478bd9Sstevel@tonic-gate 					if (trace > 4) {
34251bbc88acSrm15945 						trace_prt(1,
34261bbc88acSrm15945 						    "  pingnfs: Can't ping "
34277c478bd9Sstevel@tonic-gate 						    "via \"circuit_v\" %s: "
34287c478bd9Sstevel@tonic-gate 						    "RPC error=%d\n",
34297c478bd9Sstevel@tonic-gate 						    hostname,
34307c478bd9Sstevel@tonic-gate 						    rpc_createerr.cf_stat);
34317c478bd9Sstevel@tonic-gate 					}
34327c478bd9Sstevel@tonic-gate 				}
34337c478bd9Sstevel@tonic-gate 			}
34347c478bd9Sstevel@tonic-gate 
34357c478bd9Sstevel@tonic-gate 		/*
34367c478bd9Sstevel@tonic-gate 		 * backoff and return lower version to retry the ping.
34377c478bd9Sstevel@tonic-gate 		 * XXX we should be more careful and handle
34387c478bd9Sstevel@tonic-gate 		 * RPC_PROGVERSMISMATCH here, because that error is handled
34397c478bd9Sstevel@tonic-gate 		 * in clnt_create_vers(). It's not done to stay in sync
34407c478bd9Sstevel@tonic-gate 		 * with the nfs mount command.
34417c478bd9Sstevel@tonic-gate 		 */
34427c478bd9Sstevel@tonic-gate 			vers_to_try--;
34437c478bd9Sstevel@tonic-gate 			if (vers_to_try < versmin)
34447c478bd9Sstevel@tonic-gate 				break;
34457c478bd9Sstevel@tonic-gate 			if (versp != NULL) {	/* recheck the cache */
34467c478bd9Sstevel@tonic-gate 				*versp = vers_to_try;
34477c478bd9Sstevel@tonic-gate 				if (trace > 4) {
34487c478bd9Sstevel@tonic-gate 					trace_prt(1,
34497c478bd9Sstevel@tonic-gate 					    "  pingnfs: check cache: vers=%d\n",
34507c478bd9Sstevel@tonic-gate 					    *versp);
34517c478bd9Sstevel@tonic-gate 				}
34527c478bd9Sstevel@tonic-gate 				switch (cache_check(hostname, versp, proto)) {
34537c478bd9Sstevel@tonic-gate 				case GOODHOST:
34547c478bd9Sstevel@tonic-gate 					if (hostname != hostpart)
34557c478bd9Sstevel@tonic-gate 						free(hostname);
34567c478bd9Sstevel@tonic-gate 					return (RPC_SUCCESS);
34577c478bd9Sstevel@tonic-gate 				case DEADHOST:
34587c478bd9Sstevel@tonic-gate 					if (hostname != hostpart)
34597c478bd9Sstevel@tonic-gate 						free(hostname);
34607c478bd9Sstevel@tonic-gate 					return (RPC_TIMEDOUT);
34617c478bd9Sstevel@tonic-gate 				case NOHOST:
34627c478bd9Sstevel@tonic-gate 				default:
34637c478bd9Sstevel@tonic-gate 					break;
34647c478bd9Sstevel@tonic-gate 				}
34657c478bd9Sstevel@tonic-gate 			}
34667c478bd9Sstevel@tonic-gate 			if (trace > 4) {
34677c478bd9Sstevel@tonic-gate 				trace_prt(1, "  pingnfs: Try version=%d\n",
34687c478bd9Sstevel@tonic-gate 				    vers_to_try);
34697c478bd9Sstevel@tonic-gate 			}
34707c478bd9Sstevel@tonic-gate 		} while (cl == NULL);
34717c478bd9Sstevel@tonic-gate 
34727c478bd9Sstevel@tonic-gate 
34737c478bd9Sstevel@tonic-gate 		if (cl == NULL) {
34747c478bd9Sstevel@tonic-gate 			if (verbose)
34757c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR, "pingnfs: %s%s",
34767c478bd9Sstevel@tonic-gate 				    hostname, clnt_spcreateerror(""));
34777c478bd9Sstevel@tonic-gate 			clnt_stat = rpc_createerr.cf_stat;
34787c478bd9Sstevel@tonic-gate 		} else {
34797c478bd9Sstevel@tonic-gate 			clnt_destroy(cl);
34807c478bd9Sstevel@tonic-gate 			clnt_stat = RPC_SUCCESS;
34817c478bd9Sstevel@tonic-gate 		}
34827c478bd9Sstevel@tonic-gate 
34837c478bd9Sstevel@tonic-gate 	} else {
34847c478bd9Sstevel@tonic-gate 		for (vers_to_try = versmax; vers_to_try >= versmin;
34857c478bd9Sstevel@tonic-gate 		    vers_to_try--) {
34867c478bd9Sstevel@tonic-gate 
34877c478bd9Sstevel@tonic-gate 			nconf = NULL;
34887c478bd9Sstevel@tonic-gate 
34897c478bd9Sstevel@tonic-gate 			if (trace > 4) {
34907c478bd9Sstevel@tonic-gate 				trace_prt(1, "  pingnfs: Try version=%d "
34917c478bd9Sstevel@tonic-gate 				    "using get_ping()\n", vers_to_try);
34927c478bd9Sstevel@tonic-gate 			}
34937c478bd9Sstevel@tonic-gate 
34947c478bd9Sstevel@tonic-gate 			clnt_stat = get_ping(hostname, NFS_PROGRAM,
34957c478bd9Sstevel@tonic-gate 			    vers_to_try, &nconf, port, TRUE);
34967c478bd9Sstevel@tonic-gate 
34977c478bd9Sstevel@tonic-gate 			if (nconf != NULL)
34987c478bd9Sstevel@tonic-gate 				freenetconfigent(nconf);
34997c478bd9Sstevel@tonic-gate 
35007c478bd9Sstevel@tonic-gate 			if (clnt_stat == RPC_SUCCESS) {
35017c478bd9Sstevel@tonic-gate 				outvers = vers_to_try;
35027c478bd9Sstevel@tonic-gate 				break;
35037c478bd9Sstevel@tonic-gate 			}
35047c478bd9Sstevel@tonic-gate 		}
35057c478bd9Sstevel@tonic-gate 	}
35067c478bd9Sstevel@tonic-gate 
35077c478bd9Sstevel@tonic-gate 	if (trace > 1)
35087c478bd9Sstevel@tonic-gate 		clnt_stat == RPC_SUCCESS ?
35097c478bd9Sstevel@tonic-gate 		    trace_prt(1, "	pingnfs OK: nfs version=%d\n", outvers):
35107c478bd9Sstevel@tonic-gate 		    trace_prt(1, "	pingnfs FAIL: can't get nfs version\n");
35117c478bd9Sstevel@tonic-gate 
35127c478bd9Sstevel@tonic-gate 	if (clnt_stat == RPC_SUCCESS) {
35137c478bd9Sstevel@tonic-gate 		cache_enter(hostname, versmax, outvers, proto, GOODHOST);
35147c478bd9Sstevel@tonic-gate 		if (versp != NULL)
35157c478bd9Sstevel@tonic-gate 			*versp = outvers;
35167c478bd9Sstevel@tonic-gate 	} else
35177c478bd9Sstevel@tonic-gate 		cache_enter(hostname, versmax, versmax, proto, DEADHOST);
35187c478bd9Sstevel@tonic-gate 
35197c478bd9Sstevel@tonic-gate 	if (hostpart != hostname)
35207c478bd9Sstevel@tonic-gate 		free(hostname);
35217c478bd9Sstevel@tonic-gate 
35227c478bd9Sstevel@tonic-gate 	return (clnt_stat);
35237c478bd9Sstevel@tonic-gate }
35247c478bd9Sstevel@tonic-gate 
35257c478bd9Sstevel@tonic-gate #define	MNTTYPE_LOFS	"lofs"
35267c478bd9Sstevel@tonic-gate 
35277c478bd9Sstevel@tonic-gate int
loopbackmount(fsname,dir,mntopts,overlay)35287c478bd9Sstevel@tonic-gate loopbackmount(fsname, dir, mntopts, overlay)
35297c478bd9Sstevel@tonic-gate 	char *fsname;		/* Directory being mounted */
35307c478bd9Sstevel@tonic-gate 	char *dir;		/* Directory being mounted on */
35317c478bd9Sstevel@tonic-gate 	char *mntopts;
35327c478bd9Sstevel@tonic-gate 	int overlay;
35337c478bd9Sstevel@tonic-gate {
35347c478bd9Sstevel@tonic-gate 	struct mnttab mnt;
35357c478bd9Sstevel@tonic-gate 	int flags = 0;
35367c478bd9Sstevel@tonic-gate 	char fstype[] = MNTTYPE_LOFS;
35377c478bd9Sstevel@tonic-gate 	int dirlen;
35387c478bd9Sstevel@tonic-gate 	struct stat st;
35397c478bd9Sstevel@tonic-gate 	char optbuf[MAX_MNTOPT_STR];
35407c478bd9Sstevel@tonic-gate 
35417c478bd9Sstevel@tonic-gate 	dirlen = strlen(dir);
35427c478bd9Sstevel@tonic-gate 	if (dir[dirlen-1] == ' ')
35437c478bd9Sstevel@tonic-gate 		dirlen--;
35447c478bd9Sstevel@tonic-gate 
35457c478bd9Sstevel@tonic-gate 	if (dirlen == strlen(fsname) &&
35467c478bd9Sstevel@tonic-gate 		strncmp(fsname, dir, dirlen) == 0) {
35477c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR,
35487c478bd9Sstevel@tonic-gate 			"Mount of %s on %s would result in deadlock, aborted\n",
35497c478bd9Sstevel@tonic-gate 			fsname, dir);
35507c478bd9Sstevel@tonic-gate 		return (RET_ERR);
35517c478bd9Sstevel@tonic-gate 	}
35527c478bd9Sstevel@tonic-gate 	mnt.mnt_mntopts = mntopts;
35537c478bd9Sstevel@tonic-gate 	if (hasmntopt(&mnt, MNTOPT_RO) != NULL)
35547c478bd9Sstevel@tonic-gate 		flags |= MS_RDONLY;
35557c478bd9Sstevel@tonic-gate 
35567c478bd9Sstevel@tonic-gate 	(void) strlcpy(optbuf, mntopts, sizeof (optbuf));
35577c478bd9Sstevel@tonic-gate 
35587c478bd9Sstevel@tonic-gate 	if (overlay)
35597c478bd9Sstevel@tonic-gate 		flags |= MS_OVERLAY;
35607c478bd9Sstevel@tonic-gate 
35617c478bd9Sstevel@tonic-gate 	if (trace > 1)
35627c478bd9Sstevel@tonic-gate 		trace_prt(1,
35637c478bd9Sstevel@tonic-gate 			"  loopbackmount: fsname=%s, dir=%s, flags=%d\n",
35647c478bd9Sstevel@tonic-gate 			fsname, dir, flags);
35657c478bd9Sstevel@tonic-gate 
356645916cd2Sjpk 	if (is_system_labeled()) {
356745916cd2Sjpk 		if (create_homedir((const char *)fsname,
356845916cd2Sjpk 		    (const char *)dir) == 0) {
356945916cd2Sjpk 			return (NFSERR_NOENT);
357045916cd2Sjpk 		}
357145916cd2Sjpk 	}
357245916cd2Sjpk 
35737c478bd9Sstevel@tonic-gate 	if (mount(fsname, dir, flags | MS_DATA | MS_OPTIONSTR, fstype,
35747c478bd9Sstevel@tonic-gate 	    NULL, 0, optbuf, sizeof (optbuf)) < 0) {
35757c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "Mount of %s on %s: %m", fsname, dir);
35767c478bd9Sstevel@tonic-gate 		return (RET_ERR);
35777c478bd9Sstevel@tonic-gate 	}
35787c478bd9Sstevel@tonic-gate 
35797c478bd9Sstevel@tonic-gate 	if (stat(dir, &st) == 0) {
35807c478bd9Sstevel@tonic-gate 		if (trace > 1) {
35817c478bd9Sstevel@tonic-gate 			trace_prt(1,
35827c478bd9Sstevel@tonic-gate 			    "  loopbackmount of %s on %s dev=%x rdev=%x OK\n",
35837c478bd9Sstevel@tonic-gate 			    fsname, dir, st.st_dev, st.st_rdev);
35847c478bd9Sstevel@tonic-gate 		}
35857c478bd9Sstevel@tonic-gate 	} else {
35867c478bd9Sstevel@tonic-gate 		if (trace > 1) {
35877c478bd9Sstevel@tonic-gate 			trace_prt(1,
35887c478bd9Sstevel@tonic-gate 			    "  loopbackmount of %s on %s OK\n", fsname, dir);
35897c478bd9Sstevel@tonic-gate 			trace_prt(1, "	stat of %s failed\n", dir);
35907c478bd9Sstevel@tonic-gate 		}
35917c478bd9Sstevel@tonic-gate 	}
35927c478bd9Sstevel@tonic-gate 
35937c478bd9Sstevel@tonic-gate 	return (0);
35947c478bd9Sstevel@tonic-gate }
35957c478bd9Sstevel@tonic-gate 
35967c478bd9Sstevel@tonic-gate /*
35977c478bd9Sstevel@tonic-gate  * Look for the value of a numeric option of the form foo=x.  If found, set
35987c478bd9Sstevel@tonic-gate  * *valp to the value and return non-zero.  If not found or the option is
35997c478bd9Sstevel@tonic-gate  * malformed, return zero.
36007c478bd9Sstevel@tonic-gate  */
36017c478bd9Sstevel@tonic-gate 
36027c478bd9Sstevel@tonic-gate int
nopt(mnt,opt,valp)36037c478bd9Sstevel@tonic-gate nopt(mnt, opt, valp)
36047c478bd9Sstevel@tonic-gate 	struct mnttab *mnt;
36057c478bd9Sstevel@tonic-gate 	char *opt;
36067c478bd9Sstevel@tonic-gate 	int *valp;			/* OUT */
36077c478bd9Sstevel@tonic-gate {
36087c478bd9Sstevel@tonic-gate 	char *equal;
36097c478bd9Sstevel@tonic-gate 	char *str;
36107c478bd9Sstevel@tonic-gate 
36117c478bd9Sstevel@tonic-gate 	/*
36127c478bd9Sstevel@tonic-gate 	 * We should never get a null pointer, but if we do, it's better to
36137c478bd9Sstevel@tonic-gate 	 * ignore the option than to dump core.
36147c478bd9Sstevel@tonic-gate 	 */
36157c478bd9Sstevel@tonic-gate 
36167c478bd9Sstevel@tonic-gate 	if (valp == NULL) {
36177c478bd9Sstevel@tonic-gate 		syslog(LOG_DEBUG, "null pointer for %s option", opt);
36187c478bd9Sstevel@tonic-gate 		return (0);
36197c478bd9Sstevel@tonic-gate 	}
36207c478bd9Sstevel@tonic-gate 
36217c478bd9Sstevel@tonic-gate 	if (str = hasmntopt(mnt, opt)) {
36227c478bd9Sstevel@tonic-gate 		if (equal = strchr(str, '=')) {
36237c478bd9Sstevel@tonic-gate 			*valp = atoi(&equal[1]);
36247c478bd9Sstevel@tonic-gate 			return (1);
36257c478bd9Sstevel@tonic-gate 		} else {
36267c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "Bad numeric option '%s'", str);
36277c478bd9Sstevel@tonic-gate 		}
36287c478bd9Sstevel@tonic-gate 	}
36297c478bd9Sstevel@tonic-gate 	return (0);
36307c478bd9Sstevel@tonic-gate }
36317c478bd9Sstevel@tonic-gate 
363211606941Sjwahlig int
nfsunmount(mnt)36337c478bd9Sstevel@tonic-gate nfsunmount(mnt)
36347c478bd9Sstevel@tonic-gate 	struct mnttab *mnt;
36357c478bd9Sstevel@tonic-gate {
36367c478bd9Sstevel@tonic-gate 	struct timeval timeout;
36377c478bd9Sstevel@tonic-gate 	CLIENT *cl;
36387c478bd9Sstevel@tonic-gate 	enum clnt_stat rpc_stat;
36397c478bd9Sstevel@tonic-gate 	char *host, *path;
36407c478bd9Sstevel@tonic-gate 	struct replica *list;
36417c478bd9Sstevel@tonic-gate 	int i, count = 0;
36427c478bd9Sstevel@tonic-gate 	int isv4mount = is_v4_mount(mnt->mnt_mountp);
36437c478bd9Sstevel@tonic-gate 
36447c478bd9Sstevel@tonic-gate 	if (trace > 1)
36457c478bd9Sstevel@tonic-gate 		trace_prt(1, "	nfsunmount: umount %s\n", mnt->mnt_mountp);
36467c478bd9Sstevel@tonic-gate 
36477c478bd9Sstevel@tonic-gate 	if (umount(mnt->mnt_mountp) < 0) {
36487c478bd9Sstevel@tonic-gate 		if (trace > 1)
36497c478bd9Sstevel@tonic-gate 			trace_prt(1, "	nfsunmount: umount %s FAILED\n",
36507c478bd9Sstevel@tonic-gate 				mnt->mnt_mountp);
36517c478bd9Sstevel@tonic-gate 		if (errno)
36527c478bd9Sstevel@tonic-gate 			return (errno);
36537c478bd9Sstevel@tonic-gate 	}
36547c478bd9Sstevel@tonic-gate 
36557c478bd9Sstevel@tonic-gate 	/*
36567c478bd9Sstevel@tonic-gate 	 * If this is a NFSv4 mount, the mount protocol was not used
36577c478bd9Sstevel@tonic-gate 	 * so we just return.
36587c478bd9Sstevel@tonic-gate 	 */
36597c478bd9Sstevel@tonic-gate 	if (isv4mount) {
36607c478bd9Sstevel@tonic-gate 		if (trace > 1)
36617c478bd9Sstevel@tonic-gate 			trace_prt(1, "	nfsunmount: umount %s OK\n",
36627c478bd9Sstevel@tonic-gate 				mnt->mnt_mountp);
36637c478bd9Sstevel@tonic-gate 		return (0);
36647c478bd9Sstevel@tonic-gate 	}
36657c478bd9Sstevel@tonic-gate 
36667c478bd9Sstevel@tonic-gate 	/*
36677c478bd9Sstevel@tonic-gate 	 * If mounted with -o public, then no need to contact server
36687c478bd9Sstevel@tonic-gate 	 * because mount protocol was not used.
36697c478bd9Sstevel@tonic-gate 	 */
36707c478bd9Sstevel@tonic-gate 	if (hasmntopt(mnt, MNTOPT_PUBLIC) != NULL) {
36717c478bd9Sstevel@tonic-gate 		return (0);
36727c478bd9Sstevel@tonic-gate 	}
36737c478bd9Sstevel@tonic-gate 
36747c478bd9Sstevel@tonic-gate 	/*
36757c478bd9Sstevel@tonic-gate 	 * The rest of this code is advisory to the server.
36767c478bd9Sstevel@tonic-gate 	 * If it fails return success anyway.
36777c478bd9Sstevel@tonic-gate 	 */
36787c478bd9Sstevel@tonic-gate 
36797c478bd9Sstevel@tonic-gate 	list = parse_replica(mnt->mnt_special, &count);
36807c478bd9Sstevel@tonic-gate 	if (!list) {
36817c478bd9Sstevel@tonic-gate 		if (count >= 0)
36827c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR,
36837c478bd9Sstevel@tonic-gate 			    "Memory allocation failed: %m");
36847c478bd9Sstevel@tonic-gate 		return (ENOMEM);
36857c478bd9Sstevel@tonic-gate 	}
36867c478bd9Sstevel@tonic-gate 
36877c478bd9Sstevel@tonic-gate 	for (i = 0; i < count; i++) {
36887c478bd9Sstevel@tonic-gate 
36897c478bd9Sstevel@tonic-gate 		host = list[i].host;
36907c478bd9Sstevel@tonic-gate 		path = list[i].path;
36917c478bd9Sstevel@tonic-gate 
36927c478bd9Sstevel@tonic-gate 		/*
36937c478bd9Sstevel@tonic-gate 		 * Skip file systems mounted using WebNFS, because mount
36947c478bd9Sstevel@tonic-gate 		 * protocol was not used.
36957c478bd9Sstevel@tonic-gate 		 */
36967c478bd9Sstevel@tonic-gate 		if (strcmp(host, "nfs") == 0 && strncmp(path, "//", 2) == 0)
36977c478bd9Sstevel@tonic-gate 			continue;
36987c478bd9Sstevel@tonic-gate 
36997c478bd9Sstevel@tonic-gate 		cl = clnt_create(host, MOUNTPROG, MOUNTVERS, "datagram_v");
37007c478bd9Sstevel@tonic-gate 		if (cl == NULL)
37017c478bd9Sstevel@tonic-gate 			break;
37027c478bd9Sstevel@tonic-gate #ifdef MALLOC_DEBUG
37037c478bd9Sstevel@tonic-gate 		add_alloc("CLNT_HANDLE", cl, 0, __FILE__, __LINE__);
37047c478bd9Sstevel@tonic-gate 		add_alloc("AUTH_HANDLE", cl->cl_auth, 0,
37057c478bd9Sstevel@tonic-gate 			__FILE__, __LINE__);
37067c478bd9Sstevel@tonic-gate #endif
37077c478bd9Sstevel@tonic-gate 		if (__clnt_bindresvport(cl) < 0) {
37087c478bd9Sstevel@tonic-gate 			if (verbose)
37097c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR, "umount %s:%s: %s",
37107c478bd9Sstevel@tonic-gate 					host, path,
37117c478bd9Sstevel@tonic-gate 					"Couldn't bind to reserved port");
37127c478bd9Sstevel@tonic-gate 			destroy_auth_client_handle(cl);
37137c478bd9Sstevel@tonic-gate 			continue;
37147c478bd9Sstevel@tonic-gate 		}
37157c478bd9Sstevel@tonic-gate #ifdef MALLOC_DEBUG
37167c478bd9Sstevel@tonic-gate 		drop_alloc("AUTH_HANDLE", cl->cl_auth, __FILE__, __LINE__);
37177c478bd9Sstevel@tonic-gate #endif
37187c478bd9Sstevel@tonic-gate 		AUTH_DESTROY(cl->cl_auth);
37197c478bd9Sstevel@tonic-gate 		if ((cl->cl_auth = authsys_create_default()) == NULL) {
37207c478bd9Sstevel@tonic-gate 			if (verbose)
37217c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR, "umount %s:%s: %s",
37227c478bd9Sstevel@tonic-gate 					host, path,
37237c478bd9Sstevel@tonic-gate 					"Failed creating default auth handle");
37247c478bd9Sstevel@tonic-gate 			destroy_auth_client_handle(cl);
37257c478bd9Sstevel@tonic-gate 			continue;
37267c478bd9Sstevel@tonic-gate 		}
37277c478bd9Sstevel@tonic-gate #ifdef MALLOC_DEBUG
37287c478bd9Sstevel@tonic-gate 		add_alloc("AUTH_HANDLE", cl->cl_auth, 0, __FILE__, __LINE__);
37297c478bd9Sstevel@tonic-gate #endif
37307c478bd9Sstevel@tonic-gate 		timeout.tv_usec = 0;
37317c478bd9Sstevel@tonic-gate 		timeout.tv_sec = 5;
37327c478bd9Sstevel@tonic-gate 		rpc_stat = clnt_call(cl, MOUNTPROC_UMNT, xdr_dirpath,
37337c478bd9Sstevel@tonic-gate 			    (caddr_t)&path, xdr_void, (char *)NULL, timeout);
37347c478bd9Sstevel@tonic-gate 		if (verbose && rpc_stat != RPC_SUCCESS)
37357c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "%s: %s",
37367c478bd9Sstevel@tonic-gate 				host, clnt_sperror(cl, "unmount"));
37377c478bd9Sstevel@tonic-gate 		destroy_auth_client_handle(cl);
37387c478bd9Sstevel@tonic-gate 	}
37397c478bd9Sstevel@tonic-gate 
37407c478bd9Sstevel@tonic-gate 	free_replica(list, count);
37417c478bd9Sstevel@tonic-gate 
37427c478bd9Sstevel@tonic-gate 	if (trace > 1)
37437c478bd9Sstevel@tonic-gate 		trace_prt(1, "	nfsunmount: umount %s OK\n", mnt->mnt_mountp);
37447c478bd9Sstevel@tonic-gate 
37457c478bd9Sstevel@tonic-gate done:
37467c478bd9Sstevel@tonic-gate 	return (0);
37477c478bd9Sstevel@tonic-gate }
37487c478bd9Sstevel@tonic-gate 
37497c478bd9Sstevel@tonic-gate /*
37507c478bd9Sstevel@tonic-gate  * Put a new entry in the cache chain by prepending it to the front.
37517c478bd9Sstevel@tonic-gate  * If there isn't enough memory then just give up.
37527c478bd9Sstevel@tonic-gate  */
37537c478bd9Sstevel@tonic-gate static void
cache_enter(host,reqvers,outvers,proto,state)37547c478bd9Sstevel@tonic-gate cache_enter(host, reqvers, outvers, proto, state)
37557c478bd9Sstevel@tonic-gate 	char *host;
37567c478bd9Sstevel@tonic-gate 	rpcvers_t reqvers;
37577c478bd9Sstevel@tonic-gate 	rpcvers_t outvers;
37587c478bd9Sstevel@tonic-gate 	char *proto;
37597c478bd9Sstevel@tonic-gate 	int state;
37607c478bd9Sstevel@tonic-gate {
37617c478bd9Sstevel@tonic-gate 	struct cache_entry *entry;
37627c478bd9Sstevel@tonic-gate 	int cache_time = 30;	/* sec */
37637c478bd9Sstevel@tonic-gate 
37647c478bd9Sstevel@tonic-gate 	timenow = time(NULL);
37657c478bd9Sstevel@tonic-gate 
37667c478bd9Sstevel@tonic-gate 	entry = (struct cache_entry *)malloc(sizeof (struct cache_entry));
37677c478bd9Sstevel@tonic-gate 	if (entry == NULL)
37687c478bd9Sstevel@tonic-gate 		return;
37697c478bd9Sstevel@tonic-gate 	(void) memset((caddr_t)entry, 0, sizeof (struct cache_entry));
37707c478bd9Sstevel@tonic-gate 	entry->cache_host = strdup(host);
37717c478bd9Sstevel@tonic-gate 	if (entry->cache_host == NULL) {
37727c478bd9Sstevel@tonic-gate 		cache_free(entry);
37737c478bd9Sstevel@tonic-gate 		return;
37747c478bd9Sstevel@tonic-gate 	}
37757c478bd9Sstevel@tonic-gate 	entry->cache_reqvers = reqvers;
37767c478bd9Sstevel@tonic-gate 	entry->cache_outvers = outvers;
37777c478bd9Sstevel@tonic-gate 	entry->cache_proto = (proto == NULL ? NULL : strdup(proto));
37787c478bd9Sstevel@tonic-gate 	entry->cache_state = state;
37797c478bd9Sstevel@tonic-gate 	entry->cache_time = timenow + cache_time;
37807c478bd9Sstevel@tonic-gate 	(void) rw_wrlock(&cache_lock);
37817c478bd9Sstevel@tonic-gate #ifdef CACHE_DEBUG
37827c478bd9Sstevel@tonic-gate 	host_cache_accesses++;		/* up host cache access counter */
37837c478bd9Sstevel@tonic-gate #endif /* CACHE DEBUG */
37847c478bd9Sstevel@tonic-gate 	entry->cache_next = cache_head;
37857c478bd9Sstevel@tonic-gate 	cache_head = entry;
37867c478bd9Sstevel@tonic-gate 	(void) rw_unlock(&cache_lock);
37877c478bd9Sstevel@tonic-gate }
37887c478bd9Sstevel@tonic-gate 
37897c478bd9Sstevel@tonic-gate static int
cache_check(host,versp,proto)37907c478bd9Sstevel@tonic-gate cache_check(host, versp, proto)
37917c478bd9Sstevel@tonic-gate 	char *host;
37927c478bd9Sstevel@tonic-gate 	rpcvers_t *versp;
37937c478bd9Sstevel@tonic-gate 	char *proto;
37947c478bd9Sstevel@tonic-gate {
37957c478bd9Sstevel@tonic-gate 	int state = NOHOST;
37967c478bd9Sstevel@tonic-gate 	struct cache_entry *ce, *prev;
37977c478bd9Sstevel@tonic-gate 
37987c478bd9Sstevel@tonic-gate 	timenow = time(NULL);
37997c478bd9Sstevel@tonic-gate 
38007c478bd9Sstevel@tonic-gate 	(void) rw_rdlock(&cache_lock);
38017c478bd9Sstevel@tonic-gate 
38027c478bd9Sstevel@tonic-gate #ifdef CACHE_DEBUG
38037c478bd9Sstevel@tonic-gate 	/* Increment the lookup and access counters for the host cache */
38047c478bd9Sstevel@tonic-gate 	host_cache_accesses++;
38057c478bd9Sstevel@tonic-gate 	host_cache_lookups++;
38067c478bd9Sstevel@tonic-gate 	if ((host_cache_lookups%1000) == 0)
38077c478bd9Sstevel@tonic-gate 		trace_host_cache();
38087c478bd9Sstevel@tonic-gate #endif /* CACHE DEBUG */
38097c478bd9Sstevel@tonic-gate 
38107c478bd9Sstevel@tonic-gate 	for (ce = cache_head; ce; ce = ce->cache_next) {
38117c478bd9Sstevel@tonic-gate 		if (timenow > ce->cache_time) {
38127c478bd9Sstevel@tonic-gate 			(void) rw_unlock(&cache_lock);
38137c478bd9Sstevel@tonic-gate 			(void) rw_wrlock(&cache_lock);
38147c478bd9Sstevel@tonic-gate 			for (prev = NULL, ce = cache_head; ce;
38157c478bd9Sstevel@tonic-gate 				prev = ce, ce = ce->cache_next) {
38167c478bd9Sstevel@tonic-gate 				if (timenow > ce->cache_time) {
38177c478bd9Sstevel@tonic-gate 					cache_free(ce);
38187c478bd9Sstevel@tonic-gate 					if (prev)
38197c478bd9Sstevel@tonic-gate 						prev->cache_next = NULL;
38207c478bd9Sstevel@tonic-gate 					else
38217c478bd9Sstevel@tonic-gate 						cache_head = NULL;
38227c478bd9Sstevel@tonic-gate 					break;
38237c478bd9Sstevel@tonic-gate 				}
38247c478bd9Sstevel@tonic-gate 			}
38257c478bd9Sstevel@tonic-gate 			(void) rw_unlock(&cache_lock);
38267c478bd9Sstevel@tonic-gate 			return (state);
38277c478bd9Sstevel@tonic-gate 		}
38287c478bd9Sstevel@tonic-gate 		if (strcmp(host, ce->cache_host) != 0)
38297c478bd9Sstevel@tonic-gate 			continue;
38307c478bd9Sstevel@tonic-gate 		if ((proto == NULL && ce->cache_proto != NULL) ||
38317c478bd9Sstevel@tonic-gate 		    (proto != NULL && ce->cache_proto == NULL))
38327c478bd9Sstevel@tonic-gate 			continue;
38337c478bd9Sstevel@tonic-gate 		if (proto != NULL &&
38347c478bd9Sstevel@tonic-gate 		    strcmp(proto, ce->cache_proto) != 0)
38357c478bd9Sstevel@tonic-gate 			continue;
38367c478bd9Sstevel@tonic-gate 
38377c478bd9Sstevel@tonic-gate 		if (versp == NULL ||
38387c478bd9Sstevel@tonic-gate 			(versp != NULL && *versp == ce->cache_reqvers) ||
38397c478bd9Sstevel@tonic-gate 			(versp != NULL && *versp == ce->cache_outvers)) {
38407c478bd9Sstevel@tonic-gate 				if (versp != NULL)
38417c478bd9Sstevel@tonic-gate 					*versp = ce->cache_outvers;
38427c478bd9Sstevel@tonic-gate 				state = ce->cache_state;
38437c478bd9Sstevel@tonic-gate 
38447c478bd9Sstevel@tonic-gate 				/* increment the host cache hit counters */
38457c478bd9Sstevel@tonic-gate #ifdef CACHE_DEBUG
38467c478bd9Sstevel@tonic-gate 				if (state == GOODHOST)
38477c478bd9Sstevel@tonic-gate 					goodhost_cache_hits++;
38487c478bd9Sstevel@tonic-gate 				if (state == DEADHOST)
38497c478bd9Sstevel@tonic-gate 					deadhost_cache_hits++;
38507c478bd9Sstevel@tonic-gate #endif /* CACHE_DEBUG */
38517c478bd9Sstevel@tonic-gate 				(void) rw_unlock(&cache_lock);
38527c478bd9Sstevel@tonic-gate 				return (state);
38537c478bd9Sstevel@tonic-gate 		}
38547c478bd9Sstevel@tonic-gate 	}
38557c478bd9Sstevel@tonic-gate 	(void) rw_unlock(&cache_lock);
38567c478bd9Sstevel@tonic-gate 	return (state);
38577c478bd9Sstevel@tonic-gate }
38587c478bd9Sstevel@tonic-gate 
38597c478bd9Sstevel@tonic-gate /*
38607c478bd9Sstevel@tonic-gate  * Free a cache entry and all entries
38617c478bd9Sstevel@tonic-gate  * further down the chain since they
38627c478bd9Sstevel@tonic-gate  * will also be expired.
38637c478bd9Sstevel@tonic-gate  */
38647c478bd9Sstevel@tonic-gate static void
cache_free(entry)38657c478bd9Sstevel@tonic-gate cache_free(entry)
38667c478bd9Sstevel@tonic-gate 	struct cache_entry *entry;
38677c478bd9Sstevel@tonic-gate {
38687c478bd9Sstevel@tonic-gate 	struct cache_entry *ce, *next = NULL;
38697c478bd9Sstevel@tonic-gate 
38707c478bd9Sstevel@tonic-gate 	for (ce = entry; ce; ce = next) {
38717c478bd9Sstevel@tonic-gate 		if (ce->cache_host)
38727c478bd9Sstevel@tonic-gate 			free(ce->cache_host);
38737c478bd9Sstevel@tonic-gate 		if (ce->cache_proto)
38747c478bd9Sstevel@tonic-gate 			free(ce->cache_proto);
38757c478bd9Sstevel@tonic-gate 		next = ce->cache_next;
38767c478bd9Sstevel@tonic-gate 		free(ce);
38777c478bd9Sstevel@tonic-gate 	}
38787c478bd9Sstevel@tonic-gate }
38797c478bd9Sstevel@tonic-gate 
38807c478bd9Sstevel@tonic-gate #ifdef MALLOC_DEBUG
38817c478bd9Sstevel@tonic-gate void
cache_flush()38827c478bd9Sstevel@tonic-gate cache_flush()
38837c478bd9Sstevel@tonic-gate {
38847c478bd9Sstevel@tonic-gate 	(void) rw_wrlock(&cache_lock);
38857c478bd9Sstevel@tonic-gate 	cache_free(cache_head);
38867c478bd9Sstevel@tonic-gate 	cache_head = NULL;
38877c478bd9Sstevel@tonic-gate 	(void) rw_unlock(&cache_lock);
38887c478bd9Sstevel@tonic-gate }
38897c478bd9Sstevel@tonic-gate 
38907c478bd9Sstevel@tonic-gate void
flush_caches()38917c478bd9Sstevel@tonic-gate flush_caches()
38927c478bd9Sstevel@tonic-gate {
38937c478bd9Sstevel@tonic-gate 	mutex_lock(&cleanup_lock);
38947c478bd9Sstevel@tonic-gate 	cond_signal(&cleanup_start_cv);
38957c478bd9Sstevel@tonic-gate 	(void) cond_wait(&cleanup_done_cv, &cleanup_lock);
38967c478bd9Sstevel@tonic-gate 	mutex_unlock(&cleanup_lock);
38977c478bd9Sstevel@tonic-gate 	cache_flush();
38987c478bd9Sstevel@tonic-gate 	portmap_cache_flush();
38997c478bd9Sstevel@tonic-gate }
39007c478bd9Sstevel@tonic-gate #endif
39017c478bd9Sstevel@tonic-gate 
39027c478bd9Sstevel@tonic-gate /*
39037c478bd9Sstevel@tonic-gate  * Returns 1, if port option is NFS_PORT or
39047c478bd9Sstevel@tonic-gate  *	nfsd is running on the port given
39057c478bd9Sstevel@tonic-gate  * Returns 0, if both port is not NFS_PORT and nfsd is not
39067c478bd9Sstevel@tonic-gate  *	running on the port.
39077c478bd9Sstevel@tonic-gate  */
39087c478bd9Sstevel@tonic-gate 
39097c478bd9Sstevel@tonic-gate static int
is_nfs_port(char * opts)39107c478bd9Sstevel@tonic-gate is_nfs_port(char *opts)
39117c478bd9Sstevel@tonic-gate {
39127c478bd9Sstevel@tonic-gate 	struct mnttab m;
39137c478bd9Sstevel@tonic-gate 	uint_t nfs_port = 0;
39147c478bd9Sstevel@tonic-gate 	struct servent sv;
39157c478bd9Sstevel@tonic-gate 	char buf[256];
39167c478bd9Sstevel@tonic-gate 	int got_port;
39177c478bd9Sstevel@tonic-gate 
39187c478bd9Sstevel@tonic-gate 	m.mnt_mntopts = opts;
39197c478bd9Sstevel@tonic-gate 
39207c478bd9Sstevel@tonic-gate 	/*
39217c478bd9Sstevel@tonic-gate 	 * Get port specified in options list, if any.
39227c478bd9Sstevel@tonic-gate 	 */
39237c478bd9Sstevel@tonic-gate 	got_port = nopt(&m, MNTOPT_PORT, (int *)&nfs_port);
39247c478bd9Sstevel@tonic-gate 
39257c478bd9Sstevel@tonic-gate 	/*
39267c478bd9Sstevel@tonic-gate 	 * if no port specified or it is same as NFS_PORT return nfs
39277c478bd9Sstevel@tonic-gate 	 * To use any other daemon the port number should be different
39287c478bd9Sstevel@tonic-gate 	 */
39297c478bd9Sstevel@tonic-gate 	if (!got_port || nfs_port == NFS_PORT)
39307c478bd9Sstevel@tonic-gate 		return (1);
39317c478bd9Sstevel@tonic-gate 	/*
39327c478bd9Sstevel@tonic-gate 	 * If daemon is nfsd, return nfs
39337c478bd9Sstevel@tonic-gate 	 */
39347c478bd9Sstevel@tonic-gate 	if (getservbyport_r(nfs_port, NULL, &sv, buf, 256) == &sv &&
39357c478bd9Sstevel@tonic-gate 	    strcmp(sv.s_name, "nfsd") == 0)
39367c478bd9Sstevel@tonic-gate 		return (1);
39377c478bd9Sstevel@tonic-gate 
39387c478bd9Sstevel@tonic-gate 	/*
39397c478bd9Sstevel@tonic-gate 	 * daemon is not nfs
39407c478bd9Sstevel@tonic-gate 	 */
39417c478bd9Sstevel@tonic-gate 	return (0);
39427c478bd9Sstevel@tonic-gate }
39437c478bd9Sstevel@tonic-gate 
39447c478bd9Sstevel@tonic-gate 
39457c478bd9Sstevel@tonic-gate /*
39467c478bd9Sstevel@tonic-gate  * destroy_auth_client_handle(cl)
39477c478bd9Sstevel@tonic-gate  * destroys the created client handle
39487c478bd9Sstevel@tonic-gate  */
394939d3e169Sevanl void
destroy_auth_client_handle(CLIENT * cl)39507c478bd9Sstevel@tonic-gate destroy_auth_client_handle(CLIENT *cl)
39517c478bd9Sstevel@tonic-gate {
39527c478bd9Sstevel@tonic-gate 	if (cl) {
39537c478bd9Sstevel@tonic-gate 		if (cl->cl_auth) {
39547c478bd9Sstevel@tonic-gate #ifdef MALLOC_DEBUG
39557c478bd9Sstevel@tonic-gate 			drop_alloc("AUTH_HANDLE", cl->cl_auth,
39567c478bd9Sstevel@tonic-gate 			    __FILE__, __LINE__);
39577c478bd9Sstevel@tonic-gate #endif
39587c478bd9Sstevel@tonic-gate 			AUTH_DESTROY(cl->cl_auth);
39597c478bd9Sstevel@tonic-gate 			cl->cl_auth = NULL;
39607c478bd9Sstevel@tonic-gate 		}
39617c478bd9Sstevel@tonic-gate #ifdef MALLOC_DEBUG
39627c478bd9Sstevel@tonic-gate 		drop_alloc("CLNT_HANDLE", cl,
39637c478bd9Sstevel@tonic-gate 		    __FILE__, __LINE__);
39647c478bd9Sstevel@tonic-gate #endif
39657c478bd9Sstevel@tonic-gate 		clnt_destroy(cl);
39667c478bd9Sstevel@tonic-gate 	}
39677c478bd9Sstevel@tonic-gate }
39687c478bd9Sstevel@tonic-gate 
39697c478bd9Sstevel@tonic-gate 
39707c478bd9Sstevel@tonic-gate /*
39717c478bd9Sstevel@tonic-gate  * Attempt to figure out which version of NFS to use in pingnfs().  If
39727c478bd9Sstevel@tonic-gate  * the version number was specified (i.e., non-zero), then use it.
39737c478bd9Sstevel@tonic-gate  * Otherwise, default to the compiled-in default or the default as set
39747c478bd9Sstevel@tonic-gate  * by the /etc/default/nfs configuration (as read by read_default().
39757c478bd9Sstevel@tonic-gate  */
39767c478bd9Sstevel@tonic-gate int
set_versrange(rpcvers_t nfsvers,rpcvers_t * vers,rpcvers_t * versmin)39777c478bd9Sstevel@tonic-gate set_versrange(rpcvers_t nfsvers, rpcvers_t *vers, rpcvers_t *versmin)
39787c478bd9Sstevel@tonic-gate {
39797c478bd9Sstevel@tonic-gate 	switch (nfsvers) {
39807c478bd9Sstevel@tonic-gate 	case 0:
39817c478bd9Sstevel@tonic-gate 		*vers = vers_max_default;
39827c478bd9Sstevel@tonic-gate 		*versmin = vers_min_default;
39837c478bd9Sstevel@tonic-gate 		break;
39847c478bd9Sstevel@tonic-gate 	case NFS_V4:
39857c478bd9Sstevel@tonic-gate 		*vers = NFS_V4;
39867c478bd9Sstevel@tonic-gate 		*versmin = NFS_V4;
39877c478bd9Sstevel@tonic-gate 		break;
39887c478bd9Sstevel@tonic-gate 	case NFS_V3:
39897c478bd9Sstevel@tonic-gate 		*vers = NFS_V3;
39907c478bd9Sstevel@tonic-gate 		*versmin = NFS_V3;
39917c478bd9Sstevel@tonic-gate 		break;
39927c478bd9Sstevel@tonic-gate 	case NFS_VERSION:
39937c478bd9Sstevel@tonic-gate 		*vers = NFS_VERSION;		/* version 2 */
39947c478bd9Sstevel@tonic-gate 		*versmin = NFS_VERSMIN;		/* version 2 */
39957c478bd9Sstevel@tonic-gate 		break;
39967c478bd9Sstevel@tonic-gate 	default:
39977c478bd9Sstevel@tonic-gate 		return (-1);
39987c478bd9Sstevel@tonic-gate 	}
39997c478bd9Sstevel@tonic-gate 	return (0);
40007c478bd9Sstevel@tonic-gate }
40017c478bd9Sstevel@tonic-gate 
40027c478bd9Sstevel@tonic-gate #ifdef CACHE_DEBUG
40037c478bd9Sstevel@tonic-gate /*
40047c478bd9Sstevel@tonic-gate  * trace_portmap_cache()
40057c478bd9Sstevel@tonic-gate  * traces the portmap cache values at desired points
40067c478bd9Sstevel@tonic-gate  */
40077c478bd9Sstevel@tonic-gate static void
trace_portmap_cache()40087c478bd9Sstevel@tonic-gate trace_portmap_cache()
40097c478bd9Sstevel@tonic-gate {
40107c478bd9Sstevel@tonic-gate 	syslog(LOG_ERR, "portmap_cache: accesses=%d lookups=%d hits=%d\n",
40117c478bd9Sstevel@tonic-gate 	    portmap_cache_accesses, portmap_cache_lookups,
40127c478bd9Sstevel@tonic-gate 	    portmap_cache_hits);
40137c478bd9Sstevel@tonic-gate }
40147c478bd9Sstevel@tonic-gate 
40157c478bd9Sstevel@tonic-gate /*
40167c478bd9Sstevel@tonic-gate  * trace_host_cache()
40177c478bd9Sstevel@tonic-gate  * traces the host cache values at desired points
40187c478bd9Sstevel@tonic-gate  */
40197c478bd9Sstevel@tonic-gate static void
trace_host_cache()40207c478bd9Sstevel@tonic-gate trace_host_cache()
40217c478bd9Sstevel@tonic-gate {
40227c478bd9Sstevel@tonic-gate 	syslog(LOG_ERR,
40237c478bd9Sstevel@tonic-gate 	    "host_cache: accesses=%d lookups=%d deadhits=%d goodhits=%d\n",
40247c478bd9Sstevel@tonic-gate 	    host_cache_accesses, host_cache_lookups, deadhost_cache_hits,
40257c478bd9Sstevel@tonic-gate 	    goodhost_cache_hits);
40267c478bd9Sstevel@tonic-gate }
40277c478bd9Sstevel@tonic-gate #endif /* CACHE_DEBUG */
40287c478bd9Sstevel@tonic-gate 
40297c478bd9Sstevel@tonic-gate /*
4030dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States  * Read the NFS SMF properties to determine if the
40317c478bd9Sstevel@tonic-gate  * client has been configured for a new min/max for the NFS version to
40327c478bd9Sstevel@tonic-gate  * use.
40337c478bd9Sstevel@tonic-gate  */
40347c478bd9Sstevel@tonic-gate 
4035dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States #define	SVC_NFS_CLIENT	"svc:/network/nfs/client"
40367c478bd9Sstevel@tonic-gate 
40377c478bd9Sstevel@tonic-gate static void
read_default_nfs(void)40387c478bd9Sstevel@tonic-gate read_default_nfs(void)
40397c478bd9Sstevel@tonic-gate {
40407c478bd9Sstevel@tonic-gate 	static time_t lastread = 0;
40417c478bd9Sstevel@tonic-gate 	struct stat buf;
4042dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	char defval[4];
4043dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	int errno, bufsz;
4044dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	int tmp, ret = 0;
40457c478bd9Sstevel@tonic-gate 
4046dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	bufsz = 4;
4047dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	ret = nfs_smf_get_prop("client_versmin", defval, DEFAULT_INSTANCE,
4048dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	    SCF_TYPE_INTEGER, SVC_NFS_CLIENT, &bufsz);
4049dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	if (ret == SA_OK) {
40507c478bd9Sstevel@tonic-gate 		errno = 0;
40517c478bd9Sstevel@tonic-gate 		tmp = strtol(defval, (char **)NULL, 10);
40527c478bd9Sstevel@tonic-gate 		if (errno == 0) {
40537c478bd9Sstevel@tonic-gate 			vers_min_default = tmp;
40547c478bd9Sstevel@tonic-gate 		}
40557c478bd9Sstevel@tonic-gate 	}
4056dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 
4057dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	bufsz = 4;
4058dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	ret = nfs_smf_get_prop("client_versmax", defval, DEFAULT_INSTANCE,
4059dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	    SCF_TYPE_INTEGER, SVC_NFS_CLIENT, &bufsz);
4060dd51520eSPavan Mettu - Oracle Corporation - Menlo Park United States 	if (ret == SA_OK) {
40617c478bd9Sstevel@tonic-gate 		errno = 0;
40627c478bd9Sstevel@tonic-gate 		tmp = strtol(defval, (char **)NULL, 10);
40637c478bd9Sstevel@tonic-gate 		if (errno == 0) {
40647c478bd9Sstevel@tonic-gate 			vers_max_default = tmp;
40657c478bd9Sstevel@tonic-gate 		}
40667c478bd9Sstevel@tonic-gate 	}
40677c478bd9Sstevel@tonic-gate 
40687c478bd9Sstevel@tonic-gate 	lastread = buf.st_mtime;
40697c478bd9Sstevel@tonic-gate 
40707c478bd9Sstevel@tonic-gate 	/*
40717c478bd9Sstevel@tonic-gate 	 * Quick sanity check on the values picked up from the
40727c478bd9Sstevel@tonic-gate 	 * defaults file.  Make sure that a mistake wasn't
40737c478bd9Sstevel@tonic-gate 	 * made that will confuse things later on.
40747c478bd9Sstevel@tonic-gate 	 * If so, reset to compiled-in defaults
40757c478bd9Sstevel@tonic-gate 	 */
40767c478bd9Sstevel@tonic-gate 	if (vers_min_default > vers_max_default ||
40777c478bd9Sstevel@tonic-gate 	    vers_min_default < NFS_VERSMIN ||
40787c478bd9Sstevel@tonic-gate 	    vers_max_default > NFS_VERSMAX) {
40797c478bd9Sstevel@tonic-gate 		if (trace > 1) {
40807c478bd9Sstevel@tonic-gate 			trace_prt(1,
40817c478bd9Sstevel@tonic-gate 	"  read_default: version minimum/maximum incorrectly configured\n");
40827c478bd9Sstevel@tonic-gate 			trace_prt(1,
40837c478bd9Sstevel@tonic-gate "  read_default: config is min=%d, max%d. Resetting to min=%d, max%d\n",
40847c478bd9Sstevel@tonic-gate 			    vers_min_default, vers_max_default,
40857c478bd9Sstevel@tonic-gate 			    NFS_VERSMIN_DEFAULT,
40867c478bd9Sstevel@tonic-gate 			    NFS_VERSMAX_DEFAULT);
40877c478bd9Sstevel@tonic-gate 		}
40887c478bd9Sstevel@tonic-gate 		vers_min_default = NFS_VERSMIN_DEFAULT;
40897c478bd9Sstevel@tonic-gate 		vers_max_default = NFS_VERSMAX_DEFAULT;
40907c478bd9Sstevel@tonic-gate 	}
40917c478bd9Sstevel@tonic-gate }
40927c478bd9Sstevel@tonic-gate 
40937c478bd9Sstevel@tonic-gate /*
40947c478bd9Sstevel@tonic-gate  *  Find the mnttab entry that corresponds to "name".
40957c478bd9Sstevel@tonic-gate  *  We're not sure what the name represents: either
40967c478bd9Sstevel@tonic-gate  *  a mountpoint name, or a special name (server:/path).
40977c478bd9Sstevel@tonic-gate  *  Return the last entry in the file that matches.
40987c478bd9Sstevel@tonic-gate  */
40997c478bd9Sstevel@tonic-gate static struct extmnttab *
mnttab_find(dirname)41007c478bd9Sstevel@tonic-gate mnttab_find(dirname)
41017c478bd9Sstevel@tonic-gate 	char *dirname;
41027c478bd9Sstevel@tonic-gate {
41037c478bd9Sstevel@tonic-gate 	FILE *fp;
41047c478bd9Sstevel@tonic-gate 	struct extmnttab mnt;
41057c478bd9Sstevel@tonic-gate 	struct extmnttab *res = NULL;
41067c478bd9Sstevel@tonic-gate 
41077c478bd9Sstevel@tonic-gate 	fp = fopen(MNTTAB, "r");
41087c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
41097c478bd9Sstevel@tonic-gate 		if (trace > 1)
41107c478bd9Sstevel@tonic-gate 			trace_prt(1, "	mnttab_find: unable to open mnttab\n");
41117c478bd9Sstevel@tonic-gate 		return (NULL);
41127c478bd9Sstevel@tonic-gate 	}
41137c478bd9Sstevel@tonic-gate 	while (getextmntent(fp, &mnt, sizeof (struct extmnttab)) == 0) {
41147c478bd9Sstevel@tonic-gate 		if (strcmp(mnt.mnt_mountp, dirname) == 0 ||
41157c478bd9Sstevel@tonic-gate 		    strcmp(mnt.mnt_special, dirname) == 0) {
41167c478bd9Sstevel@tonic-gate 			if (res)
41177c478bd9Sstevel@tonic-gate 				fsfreemnttab(res);
41187c478bd9Sstevel@tonic-gate 			res = fsdupmnttab(&mnt);
41197c478bd9Sstevel@tonic-gate 		}
41207c478bd9Sstevel@tonic-gate 	}
41217c478bd9Sstevel@tonic-gate 
41227c478bd9Sstevel@tonic-gate 	resetmnttab(fp);
41237c478bd9Sstevel@tonic-gate 	fclose(fp);
41247c478bd9Sstevel@tonic-gate 	if (res == NULL) {
41257c478bd9Sstevel@tonic-gate 		if (trace > 1)
41267c478bd9Sstevel@tonic-gate 			trace_prt(1, "	mnttab_find: unable to find %s\n",
41277c478bd9Sstevel@tonic-gate 				dirname);
41287c478bd9Sstevel@tonic-gate 	}
41297c478bd9Sstevel@tonic-gate 	return (res);
41307c478bd9Sstevel@tonic-gate }
41317c478bd9Sstevel@tonic-gate 
41327c478bd9Sstevel@tonic-gate /*
41337c478bd9Sstevel@tonic-gate  * This function's behavior is taken from nfsstat.
41347c478bd9Sstevel@tonic-gate  * Trying to determine what NFS version was used for the mount.
41357c478bd9Sstevel@tonic-gate  */
41367c478bd9Sstevel@tonic-gate static int
is_v4_mount(char * mntpath)41377c478bd9Sstevel@tonic-gate is_v4_mount(char *mntpath)
41387c478bd9Sstevel@tonic-gate {
41397c478bd9Sstevel@tonic-gate 	kstat_ctl_t *kc = NULL;		/* libkstat cookie */
41407c478bd9Sstevel@tonic-gate 	kstat_t *ksp;
41417c478bd9Sstevel@tonic-gate 	ulong_t fsid;
41427c478bd9Sstevel@tonic-gate 	struct mntinfo_kstat mik;
41437c478bd9Sstevel@tonic-gate 	struct extmnttab *mntp;
41447c478bd9Sstevel@tonic-gate 	uint_t mnt_minor;
41457c478bd9Sstevel@tonic-gate 
41467c478bd9Sstevel@tonic-gate 	if ((mntp = mnttab_find(mntpath)) == NULL)
41477c478bd9Sstevel@tonic-gate 		return (FALSE);
41487c478bd9Sstevel@tonic-gate 
41497c478bd9Sstevel@tonic-gate 	/* save the minor number and free the struct so we don't forget */
41507c478bd9Sstevel@tonic-gate 	mnt_minor = mntp->mnt_minor;
41517c478bd9Sstevel@tonic-gate 	fsfreemnttab(mntp);
41527c478bd9Sstevel@tonic-gate 
41537c478bd9Sstevel@tonic-gate 	if ((kc = kstat_open()) == NULL)
41547c478bd9Sstevel@tonic-gate 		return (FALSE);
41557c478bd9Sstevel@tonic-gate 
41567c478bd9Sstevel@tonic-gate 	for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
41577c478bd9Sstevel@tonic-gate 		if (ksp->ks_type != KSTAT_TYPE_RAW)
41587c478bd9Sstevel@tonic-gate 			continue;
41597c478bd9Sstevel@tonic-gate 		if (strcmp(ksp->ks_module, "nfs") != 0)
41607c478bd9Sstevel@tonic-gate 			continue;
41617c478bd9Sstevel@tonic-gate 		if (strcmp(ksp->ks_name, "mntinfo") != 0)
41627c478bd9Sstevel@tonic-gate 			continue;
41637c478bd9Sstevel@tonic-gate 		if (mnt_minor != ksp->ks_instance)
41647c478bd9Sstevel@tonic-gate 			continue;
41657c478bd9Sstevel@tonic-gate 
41667c478bd9Sstevel@tonic-gate 		if (kstat_read(kc, ksp, &mik) == -1)
41677c478bd9Sstevel@tonic-gate 			continue;
41687c478bd9Sstevel@tonic-gate 
41697c478bd9Sstevel@tonic-gate 		(void) kstat_close(kc);
41707c478bd9Sstevel@tonic-gate 		if (mik.mik_vers == 4)
41717c478bd9Sstevel@tonic-gate 			return (TRUE);
41727c478bd9Sstevel@tonic-gate 		else
41737c478bd9Sstevel@tonic-gate 			return (FALSE);
41747c478bd9Sstevel@tonic-gate 	}
41757c478bd9Sstevel@tonic-gate 	(void) kstat_close(kc);
41767c478bd9Sstevel@tonic-gate 
41777c478bd9Sstevel@tonic-gate 	return (FALSE);
41787c478bd9Sstevel@tonic-gate }
417945916cd2Sjpk 
418045916cd2Sjpk static int
create_homedir(const char * src,const char * dst)418145916cd2Sjpk create_homedir(const char *src, const char *dst) {
418245916cd2Sjpk 
418345916cd2Sjpk 	struct stat stbuf;
418445916cd2Sjpk 	char *dst_username;
418545916cd2Sjpk 	struct passwd *pwd, pwds;
418645916cd2Sjpk 	char buf_pwd[NSS_BUFLEN_PASSWD];
418745916cd2Sjpk 	int homedir_len;
418845916cd2Sjpk 	int dst_dir_len;
418945916cd2Sjpk 	int src_dir_len;
419045916cd2Sjpk 
419145916cd2Sjpk 	if (trace > 1)
419245916cd2Sjpk 		trace_prt(1, "entered create_homedir\n");
419345916cd2Sjpk 
419445916cd2Sjpk 	if (stat(src, &stbuf) == 0) {
419545916cd2Sjpk 		if (trace > 1)
419645916cd2Sjpk 			trace_prt(1, "src exists\n");
419745916cd2Sjpk 		return (1);
419845916cd2Sjpk 	}
419945916cd2Sjpk 
420045916cd2Sjpk 	dst_username = strrchr(dst, '/');
420145916cd2Sjpk 	if (dst_username) {
420245916cd2Sjpk 		dst_username++; /* Skip over slash */
420345916cd2Sjpk 		pwd = getpwnam_r(dst_username, &pwds, buf_pwd,
420445916cd2Sjpk 		    sizeof (buf_pwd));
420545916cd2Sjpk 		if (pwd == NULL) {
420645916cd2Sjpk 			return (0);
420745916cd2Sjpk 		}
420845916cd2Sjpk 	} else {
420945916cd2Sjpk 		return (0);
421045916cd2Sjpk 	}
421145916cd2Sjpk 
421245916cd2Sjpk 	homedir_len = strlen(pwd->pw_dir);
421345916cd2Sjpk 	dst_dir_len = strlen(dst) - homedir_len;
421445916cd2Sjpk 	src_dir_len = strlen(src) - homedir_len;
421545916cd2Sjpk 
421645916cd2Sjpk 	/* Check that the paths are in the same zone */
421745916cd2Sjpk 	if (src_dir_len < dst_dir_len ||
421845916cd2Sjpk 	    (strncmp(dst, src, dst_dir_len) != 0)) {
421945916cd2Sjpk 		if (trace > 1)
422045916cd2Sjpk 			trace_prt(1, "	paths don't match\n");
422145916cd2Sjpk 		return (0);
422245916cd2Sjpk 	}
422345916cd2Sjpk 	/* Check that mountpoint is an auto_home entry */
422445916cd2Sjpk 	if (dst_dir_len < 0 ||
422545916cd2Sjpk 	    (strcmp(pwd->pw_dir, dst + dst_dir_len) != 0)) {
422645916cd2Sjpk 		return (0);
422745916cd2Sjpk 	}
422845916cd2Sjpk 
422945916cd2Sjpk 	/* Check that source is an home directory entry */
423045916cd2Sjpk 	if (src_dir_len < 0 ||
423145916cd2Sjpk 	    (strcmp(pwd->pw_dir, src + src_dir_len) != 0)) {
423245916cd2Sjpk 		if (trace > 1)
423345916cd2Sjpk 			trace_prt(1, "	homedir (2) doesn't match %s\n",
423445916cd2Sjpk 		src+src_dir_len);
423545916cd2Sjpk 		return (0);
423645916cd2Sjpk 	}
423745916cd2Sjpk 
423845916cd2Sjpk 	if (mkdir(src,
423945916cd2Sjpk 	    S_IRUSR | S_IWUSR | S_IXUSR | S_IXGRP | S_IXOTH) == -1) {
424045916cd2Sjpk 		if (trace > 1) {
424145916cd2Sjpk 			trace_prt(1, "	Couldn't mkdir %s\n", src);
424245916cd2Sjpk 		}
424345916cd2Sjpk 		return (0);
424445916cd2Sjpk 	}
424545916cd2Sjpk 
424645916cd2Sjpk 	if (chown(src, pwd->pw_uid, pwd->pw_gid) == -1) {
424745916cd2Sjpk 		unlink(src);
424845916cd2Sjpk 		return (0);
424945916cd2Sjpk 	}
425045916cd2Sjpk 
425145916cd2Sjpk 	/* Created new home directory for the user */
425245916cd2Sjpk 	return (1);
425345916cd2Sjpk }
425439d3e169Sevanl 
425539d3e169Sevanl void
free_nfs_args(struct nfs_args * argp)425639d3e169Sevanl free_nfs_args(struct nfs_args *argp)
425739d3e169Sevanl {
425839d3e169Sevanl 	struct nfs_args *oldp;
425939d3e169Sevanl 	while (argp) {
426039d3e169Sevanl 		if (argp->pathconf)
426139d3e169Sevanl 			free(argp->pathconf);
426239d3e169Sevanl 		if (argp->knconf)
426339d3e169Sevanl 			free_knconf(argp->knconf);
426439d3e169Sevanl 		if (argp->addr)
426539d3e169Sevanl 			netbuf_free(argp->addr);
426639d3e169Sevanl 		if (argp->syncaddr)
426739d3e169Sevanl 			netbuf_free(argp->syncaddr);
426839d3e169Sevanl 		if (argp->netname)
426939d3e169Sevanl 			free(argp->netname);
427039d3e169Sevanl 		if (argp->hostname)
427139d3e169Sevanl 			free(argp->hostname);
427239d3e169Sevanl 		if (argp->nfs_ext_u.nfs_extB.secdata)
427339d3e169Sevanl 			nfs_free_secdata(argp->nfs_ext_u.nfs_extB.secdata);
427439d3e169Sevanl 		if (argp->fh)
427539d3e169Sevanl 			free(argp->fh);
427639d3e169Sevanl 		if (argp->nfs_ext_u.nfs_extA.secdata) {
427739d3e169Sevanl 			sec_data_t	*sd;
427839d3e169Sevanl 			sd = argp->nfs_ext_u.nfs_extA.secdata;
427939d3e169Sevanl 			if (sd == NULL)
428039d3e169Sevanl 				break;
428139d3e169Sevanl 			switch (sd->rpcflavor) {
428239d3e169Sevanl 			case AUTH_NONE:
428339d3e169Sevanl 			case AUTH_UNIX:
428439d3e169Sevanl 			case AUTH_LOOPBACK:
428539d3e169Sevanl 				break;
428639d3e169Sevanl 			case AUTH_DES:
428739d3e169Sevanl 			{
428839d3e169Sevanl 				dh_k4_clntdata_t	*dhk4;
428939d3e169Sevanl 				dhk4 = (dh_k4_clntdata_t *)sd->data;
429039d3e169Sevanl 				if (dhk4 == NULL)
429139d3e169Sevanl 					break;
429239d3e169Sevanl 				if (dhk4->syncaddr.buf)
429339d3e169Sevanl 					free(dhk4->syncaddr.buf);
429439d3e169Sevanl 				if (dhk4->knconf->knc_protofmly)
429539d3e169Sevanl 					free(dhk4->knconf->knc_protofmly);
429639d3e169Sevanl 				if (dhk4->knconf->knc_proto)
429739d3e169Sevanl 					free(dhk4->knconf->knc_proto);
429839d3e169Sevanl 				if (dhk4->knconf)
429939d3e169Sevanl 					free(dhk4->knconf);
430039d3e169Sevanl 				if (dhk4->netname)
430139d3e169Sevanl 					free(dhk4->netname);
430239d3e169Sevanl 				free(dhk4);
430339d3e169Sevanl 				break;
430439d3e169Sevanl 			}
430539d3e169Sevanl 			case RPCSEC_GSS:
430639d3e169Sevanl 			{
430739d3e169Sevanl 				gss_clntdata_t	*gss;
430839d3e169Sevanl 				gss = (gss_clntdata_t *)sd->data;
430939d3e169Sevanl 				if (gss == NULL)
431039d3e169Sevanl 					break;
431139d3e169Sevanl 				if (gss->mechanism.elements)
431239d3e169Sevanl 					free(gss->mechanism.elements);
431339d3e169Sevanl 				free(gss);
431439d3e169Sevanl 				break;
431539d3e169Sevanl 			}
431639d3e169Sevanl 			}
431739d3e169Sevanl 		}
431839d3e169Sevanl 		oldp = argp;
431939d3e169Sevanl 		if (argp->nfs_args_ext == NFS_ARGS_EXTB)
432039d3e169Sevanl 			argp = argp->nfs_ext_u.nfs_extB.next;
432139d3e169Sevanl 		else
432239d3e169Sevanl 			argp = NULL;
432339d3e169Sevanl 		free(oldp);
432439d3e169Sevanl 	}
432539d3e169Sevanl }
43262f172c55SRobert Thurlow 
43272f172c55SRobert 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)43282f172c55SRobert Thurlow get_netconfig_info(enum type_of_stuff type_of_stuff, char *hostname,
43292f172c55SRobert Thurlow 	rpcprog_t prog, rpcvers_t vers, struct netconfig *nconf,
43302f172c55SRobert Thurlow 	ushort_t port, struct t_info *tinfo, struct t_bind *tbind,
43312f172c55SRobert Thurlow 	caddr_t *fhp, bool_t direct_to_server, char *fspath,
43322f172c55SRobert Thurlow 	enum clnt_stat *cstat, mfs_snego_t *mfssnego)
43332f172c55SRobert Thurlow {
43342f172c55SRobert Thurlow 	struct netconfig *nb = NULL;
43352f172c55SRobert Thurlow 	int ping_server = 0;
43362f172c55SRobert Thurlow 
43372f172c55SRobert Thurlow 
43382f172c55SRobert Thurlow 	if (nconf == NULL)
43392f172c55SRobert Thurlow 		return (NULL);
43402f172c55SRobert Thurlow 
43412f172c55SRobert Thurlow 	switch (type_of_stuff) {
43422f172c55SRobert Thurlow 	case SERVER_FH:
43432f172c55SRobert Thurlow 		nb = get_server_fh(hostname, prog, vers, mfssnego,
43442f172c55SRobert Thurlow 		    nconf, port, tinfo, tbind, fhp, direct_to_server,
43452f172c55SRobert Thurlow 		    fspath, cstat);
43462f172c55SRobert Thurlow 		break;
43472f172c55SRobert Thurlow 	case SERVER_PING:
43482f172c55SRobert Thurlow 		ping_server = 1;
43492f172c55SRobert Thurlow 	case SERVER_ADDR:
43502f172c55SRobert Thurlow 		nb = get_server_addrorping(hostname, prog, vers,
43512f172c55SRobert Thurlow 		    nconf, port, tinfo, tbind, fhp, direct_to_server,
43522f172c55SRobert Thurlow 		    fspath, cstat, ping_server);
43532f172c55SRobert Thurlow 		break;
43542f172c55SRobert Thurlow 	default:
43552f172c55SRobert Thurlow 		assert(nb != NULL);
43562f172c55SRobert Thurlow 	}
43572f172c55SRobert Thurlow 	return (nb);
43582f172c55SRobert Thurlow }
43592f172c55SRobert Thurlow 
43602f172c55SRobert Thurlow /*
43612f172c55SRobert Thurlow  * Get the server address or can we ping it or not.
43622f172c55SRobert Thurlow  * Check the portmap cache first for server address.
43632f172c55SRobert Thurlow  * If no entries there, ping the server with a NULLPROC rpc.
43642f172c55SRobert Thurlow  */
43652f172c55SRobert 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)43662f172c55SRobert Thurlow get_server_addrorping(char *hostname, rpcprog_t prog, rpcvers_t vers,
43672f172c55SRobert Thurlow 	struct netconfig *nconf, ushort_t port, struct t_info *tinfo,
43682f172c55SRobert Thurlow 	struct t_bind *tbind, caddr_t *fhp, bool_t direct_to_server,
43692f172c55SRobert Thurlow 	char *fspath, enum clnt_stat *cstat, int ping_server)
43702f172c55SRobert Thurlow {
43712f172c55SRobert Thurlow 	struct timeval tv;
43722f172c55SRobert Thurlow 	enum clnt_stat cs = RPC_TIMEDOUT;
43732f172c55SRobert Thurlow 	struct netbuf *nb = NULL;
43742f172c55SRobert Thurlow 	CLIENT *cl = NULL;
43752f172c55SRobert Thurlow 	int fd = -1;
43762f172c55SRobert Thurlow 
43772f172c55SRobert Thurlow 	if (prog == NFS_PROGRAM && vers == NFS_V4)
43782f172c55SRobert Thurlow 		if (strncasecmp(nconf->nc_proto, NC_UDP, strlen(NC_UDP)) == 0)
43792f172c55SRobert Thurlow 			goto done;
43802f172c55SRobert Thurlow 
43812f172c55SRobert Thurlow 	if ((fd = t_open(nconf->nc_device, O_RDWR, tinfo)) < 0) {
43822f172c55SRobert Thurlow 		goto done;
43832f172c55SRobert Thurlow 	}
43842f172c55SRobert Thurlow 
43852f172c55SRobert Thurlow 	/* LINTED pointer alignment */
43862f172c55SRobert Thurlow 	if ((tbind = (struct t_bind *)t_alloc(fd, T_BIND, T_ADDR))
43872f172c55SRobert Thurlow 	    == NULL) {
43882f172c55SRobert Thurlow 		goto done;
43892f172c55SRobert Thurlow 	}
43902f172c55SRobert Thurlow 
43912f172c55SRobert Thurlow 	if (direct_to_server != TRUE) {
43922f172c55SRobert Thurlow 		if (!ping_server) {
43932f172c55SRobert Thurlow 			if (get_cached_srv_addr(hostname, prog, vers,
43942f172c55SRobert Thurlow 			    nconf, &tbind->addr) == 0)
43952f172c55SRobert Thurlow 				goto done;
43962f172c55SRobert Thurlow 		} else {
43972f172c55SRobert Thurlow 			if (port == 0)
43982f172c55SRobert Thurlow 				goto done;
43992f172c55SRobert Thurlow 		}
44002f172c55SRobert Thurlow 	}
44012f172c55SRobert Thurlow 	if (setup_nb_parms(nconf, tbind, tinfo, hostname,
44022f172c55SRobert Thurlow 	    fd, direct_to_server, port, prog, vers, 0) < 0)
44032f172c55SRobert Thurlow 		goto done;
44042f172c55SRobert Thurlow 
44052f172c55SRobert Thurlow 	if (port || (direct_to_server == TRUE)) {
44062f172c55SRobert Thurlow 		tv.tv_sec = 10;
44072f172c55SRobert Thurlow 		tv.tv_usec = 0;
44082f172c55SRobert Thurlow 		cl = clnt_tli_create(fd, nconf, &tbind->addr,
44092f172c55SRobert Thurlow 		    prog, vers, 0, 0);
44102f172c55SRobert Thurlow 		if (cl == NULL)
44112f172c55SRobert Thurlow 			goto done;
44122f172c55SRobert Thurlow 
44132f172c55SRobert Thurlow 		cs = clnt_call(cl, NULLPROC, xdr_void, 0,
44142f172c55SRobert Thurlow 		    xdr_void, 0, tv);
44152f172c55SRobert Thurlow 		if (cs != RPC_SUCCESS) {
44162f172c55SRobert Thurlow 			syslog(LOG_ERR, "error is %d", cs);
44172f172c55SRobert Thurlow 			goto done;
44182f172c55SRobert Thurlow 		}
44192f172c55SRobert Thurlow 	}
44202f172c55SRobert Thurlow 	if (!ping_server) {
44212f172c55SRobert Thurlow 		nb = (struct netbuf *)malloc(sizeof (struct netbuf));
44222f172c55SRobert Thurlow 		if (nb == NULL) {
44232f172c55SRobert Thurlow 			syslog(LOG_ERR, "no memory\n");
44242f172c55SRobert Thurlow 			goto done;
44252f172c55SRobert Thurlow 		}
44262f172c55SRobert Thurlow 		nb->buf = (char *)malloc(tbind->addr.maxlen);
44272f172c55SRobert Thurlow 		if (nb->buf == NULL) {
44282f172c55SRobert Thurlow 			syslog(LOG_ERR, "no memory\n");
44292f172c55SRobert Thurlow 			free(nb);
44302f172c55SRobert Thurlow 			nb = NULL;
44312f172c55SRobert Thurlow 			goto done;
44322f172c55SRobert Thurlow 		}
44332f172c55SRobert Thurlow 		(void) memcpy(nb->buf, tbind->addr.buf, tbind->addr.len);
44342f172c55SRobert Thurlow 		nb->len = tbind->addr.len;
44352f172c55SRobert Thurlow 		nb->maxlen = tbind->addr.maxlen;
44362f172c55SRobert Thurlow 		cs = RPC_SUCCESS;
44372f172c55SRobert Thurlow 	}
44382f172c55SRobert Thurlow done:
44392f172c55SRobert Thurlow 	destroy_auth_client_handle(cl);
44402f172c55SRobert Thurlow 	cleanup_tli_parms(tbind, fd);
44412f172c55SRobert Thurlow 	*cstat = cs;
44422f172c55SRobert Thurlow 	return (nb);
44432f172c55SRobert Thurlow }
4444