xref: /illumos-gate/usr/src/cmd/fs.d/autofs/nfs_cast.c (revision 2a8bcb4e)
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
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  *	nfs_cast.c : broadcast to a specific group of NFS servers
247c478bd9Sstevel@tonic-gate  *
25*11606941Sjwahlig  *      Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
26*11606941Sjwahlig  *      Use is subject to license terms.
277c478bd9Sstevel@tonic-gate  */
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <stdio.h>
307c478bd9Sstevel@tonic-gate #include <syslog.h>
317c478bd9Sstevel@tonic-gate #include <errno.h>
327c478bd9Sstevel@tonic-gate #include <string.h>
337c478bd9Sstevel@tonic-gate #include <sys/types.h>
347c478bd9Sstevel@tonic-gate #include <sys/time.h>
357c478bd9Sstevel@tonic-gate #include <sys/resource.h>
367c478bd9Sstevel@tonic-gate #include <unistd.h>
377c478bd9Sstevel@tonic-gate #include <stdlib.h>
387c478bd9Sstevel@tonic-gate #include <rpc/rpc.h>
397c478bd9Sstevel@tonic-gate #include <rpc/clnt_soc.h>
407c478bd9Sstevel@tonic-gate #include <rpc/nettype.h>
417c478bd9Sstevel@tonic-gate #include <rpc/pmap_prot.h>
427c478bd9Sstevel@tonic-gate #include <netconfig.h>
437c478bd9Sstevel@tonic-gate #include <netdir.h>
447c478bd9Sstevel@tonic-gate #include <nfs/nfs.h>
457c478bd9Sstevel@tonic-gate #define	NFSCLIENT
467c478bd9Sstevel@tonic-gate #include <locale.h>
477c478bd9Sstevel@tonic-gate #include "automount.h"
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate #define	PENALTY_WEIGHT    100000
507c478bd9Sstevel@tonic-gate 
51*11606941Sjwahlig struct tstamps {
527c478bd9Sstevel@tonic-gate 	struct tstamps	*ts_next;
537c478bd9Sstevel@tonic-gate 	int		ts_penalty;
547c478bd9Sstevel@tonic-gate 	int		ts_inx;
557c478bd9Sstevel@tonic-gate 	int		ts_rcvd;
567c478bd9Sstevel@tonic-gate 	struct timeval	ts_timeval;
577c478bd9Sstevel@tonic-gate };
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate /* A list of addresses - all belonging to the same transport */
607c478bd9Sstevel@tonic-gate 
61*11606941Sjwahlig struct addrs {
627c478bd9Sstevel@tonic-gate 	struct addrs		*addr_next;
637c478bd9Sstevel@tonic-gate 	struct mapfs		*addr_mfs;
647c478bd9Sstevel@tonic-gate 	struct nd_addrlist	*addr_addrs;
657c478bd9Sstevel@tonic-gate 	struct tstamps		*addr_if_tstamps;
667c478bd9Sstevel@tonic-gate };
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate /* A list of connectionless transports */
697c478bd9Sstevel@tonic-gate 
70*11606941Sjwahlig struct transp {
717c478bd9Sstevel@tonic-gate 	struct transp		*tr_next;
727c478bd9Sstevel@tonic-gate 	int			tr_fd;
737c478bd9Sstevel@tonic-gate 	char			*tr_device;
747c478bd9Sstevel@tonic-gate 	struct t_bind		*tr_taddr;
757c478bd9Sstevel@tonic-gate 	struct addrs		*tr_addrs;
767c478bd9Sstevel@tonic-gate };
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate /* A list of map entries and their roundtrip times, for sorting */
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate struct sm {
817c478bd9Sstevel@tonic-gate 	struct mapfs *mfs;
827c478bd9Sstevel@tonic-gate 	struct timeval timeval;
837c478bd9Sstevel@tonic-gate };
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate static void free_transports(struct transp *);
867c478bd9Sstevel@tonic-gate static void calc_resp_time(struct timeval *);
877c478bd9Sstevel@tonic-gate static struct mapfs *sort_responses(struct transp *);
887c478bd9Sstevel@tonic-gate static int host_sm(const void *, const void *b);
897c478bd9Sstevel@tonic-gate static int time_sm(const void *, const void *b);
907c478bd9Sstevel@tonic-gate extern struct mapfs *add_mfs(struct mapfs *, int, struct mapfs **,
917c478bd9Sstevel@tonic-gate 	struct mapfs **);
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate /*
947c478bd9Sstevel@tonic-gate  * This routine is designed to be able to "ping"
957c478bd9Sstevel@tonic-gate  * a list of hosts and create a list of responding
967c478bd9Sstevel@tonic-gate  * hosts sorted by response time.
977c478bd9Sstevel@tonic-gate  * This must be done without any prior
987c478bd9Sstevel@tonic-gate  * contact with the host - therefore the "ping"
997c478bd9Sstevel@tonic-gate  * must be to a "well-known" address.  The outstanding
1007c478bd9Sstevel@tonic-gate  * candidate here is the address of "rpcbind".
1017c478bd9Sstevel@tonic-gate  *
1027c478bd9Sstevel@tonic-gate  * A response to a ping is no guarantee that the host
1037c478bd9Sstevel@tonic-gate  * is running NFS, has a mount daemon, or exports
1047c478bd9Sstevel@tonic-gate  * the required filesystem.  If the subsequent
1057c478bd9Sstevel@tonic-gate  * mount attempt fails then the host will be marked
1067c478bd9Sstevel@tonic-gate  * "ignore" and the host list will be re-pinged
1077c478bd9Sstevel@tonic-gate  * (sans the bad host). This process continues
1087c478bd9Sstevel@tonic-gate  * until a successful mount is achieved or until
1097c478bd9Sstevel@tonic-gate  * there are no hosts left to try.
1107c478bd9Sstevel@tonic-gate  */
1117c478bd9Sstevel@tonic-gate enum clnt_stat
nfs_cast(struct mapfs * mfs_in,struct mapfs ** mfs_out,int timeout)1127c478bd9Sstevel@tonic-gate nfs_cast(struct mapfs *mfs_in, struct mapfs **mfs_out, int timeout)
1137c478bd9Sstevel@tonic-gate {
1147c478bd9Sstevel@tonic-gate 	enum clnt_stat stat;
1157c478bd9Sstevel@tonic-gate 	AUTH *sys_auth = authsys_create_default();
1167c478bd9Sstevel@tonic-gate 	XDR xdr_stream;
1177c478bd9Sstevel@tonic-gate 	register XDR *xdrs = &xdr_stream;
1187c478bd9Sstevel@tonic-gate 	int outlen;
1197c478bd9Sstevel@tonic-gate 	int if_inx;
1207c478bd9Sstevel@tonic-gate 	int tsec;
1217c478bd9Sstevel@tonic-gate 	int flag;
1227c478bd9Sstevel@tonic-gate 	int sent, addr_cnt, rcvd, if_cnt;
1237c478bd9Sstevel@tonic-gate 	fd_set readfds, mask;
1247c478bd9Sstevel@tonic-gate 	register ulong_t xid;		/* xid - unique per addr */
1257c478bd9Sstevel@tonic-gate 	register int i;
1267c478bd9Sstevel@tonic-gate 	struct rpc_msg msg;
1277c478bd9Sstevel@tonic-gate 	struct timeval t, rcv_timeout;
1287c478bd9Sstevel@tonic-gate 	char outbuf[UDPMSGSIZE], inbuf[UDPMSGSIZE];
1297c478bd9Sstevel@tonic-gate 	struct t_unitdata t_udata, t_rdata;
1307c478bd9Sstevel@tonic-gate 	struct nd_hostserv hs;
1317c478bd9Sstevel@tonic-gate 	struct nd_addrlist *retaddrs;
1327c478bd9Sstevel@tonic-gate 	struct transp *tr_head;
1337c478bd9Sstevel@tonic-gate 	struct transp *trans, *prev_trans;
1347c478bd9Sstevel@tonic-gate 	struct addrs *a, *prev_addr;
1357c478bd9Sstevel@tonic-gate 	struct tstamps *ts, *prev_ts;
1367c478bd9Sstevel@tonic-gate 	NCONF_HANDLE *nc = NULL;
1377c478bd9Sstevel@tonic-gate 	struct netconfig *nconf;
1387c478bd9Sstevel@tonic-gate 	struct rlimit rl;
1397c478bd9Sstevel@tonic-gate 	int dtbsize;
1407c478bd9Sstevel@tonic-gate 	struct mapfs *mfs;
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate 	/*
1437c478bd9Sstevel@tonic-gate 	 * For each connectionless transport get a list of
1447c478bd9Sstevel@tonic-gate 	 * host addresses.  Any single host may have
1457c478bd9Sstevel@tonic-gate 	 * addresses on several transports.
1467c478bd9Sstevel@tonic-gate 	 */
1477c478bd9Sstevel@tonic-gate 	addr_cnt = sent = rcvd = 0;
1487c478bd9Sstevel@tonic-gate 	tr_head = NULL;
1497c478bd9Sstevel@tonic-gate 	FD_ZERO(&mask);
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 	/*
1527c478bd9Sstevel@tonic-gate 	 * Set the default select size to be the maximum FD_SETSIZE, unless
1537c478bd9Sstevel@tonic-gate 	 * the current rlimit is lower.
1547c478bd9Sstevel@tonic-gate 	 */
1557c478bd9Sstevel@tonic-gate 	dtbsize = FD_SETSIZE;
1567c478bd9Sstevel@tonic-gate 	if (getrlimit(RLIMIT_NOFILE, &rl) == 0) {
1577c478bd9Sstevel@tonic-gate 		if (rl.rlim_cur < FD_SETSIZE)
1587c478bd9Sstevel@tonic-gate 			dtbsize = rl.rlim_cur;
1597c478bd9Sstevel@tonic-gate 	}
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 	prev_trans = NULL;
1627c478bd9Sstevel@tonic-gate 	prev_addr = NULL;
1637c478bd9Sstevel@tonic-gate 	prev_ts = NULL;
1647c478bd9Sstevel@tonic-gate 	for (mfs = mfs_in; mfs; mfs = mfs->mfs_next) {
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 		if (trace > 2)
1677c478bd9Sstevel@tonic-gate 			trace_prt(1, "nfs_cast: host=%s\n", mfs->mfs_host);
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 		nc = setnetconfig();
1707c478bd9Sstevel@tonic-gate 		if (nc == NULL) {
1717c478bd9Sstevel@tonic-gate 			stat = RPC_CANTSEND;
1727c478bd9Sstevel@tonic-gate 			goto done_broad;
1737c478bd9Sstevel@tonic-gate 		}
1747c478bd9Sstevel@tonic-gate 		while (nconf = getnetconfig(nc)) {
1757c478bd9Sstevel@tonic-gate 			if (!(nconf->nc_flag & NC_VISIBLE) ||
1767c478bd9Sstevel@tonic-gate 			    nconf->nc_semantics != NC_TPI_CLTS ||
1777c478bd9Sstevel@tonic-gate 			    (strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0))
1787c478bd9Sstevel@tonic-gate 				continue;
1797c478bd9Sstevel@tonic-gate 			trans = (struct transp *)malloc(sizeof (*trans));
1807c478bd9Sstevel@tonic-gate 			if (trans == NULL) {
1817c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR, "no memory");
1827c478bd9Sstevel@tonic-gate 				stat = RPC_CANTSEND;
1837c478bd9Sstevel@tonic-gate 				goto done_broad;
1847c478bd9Sstevel@tonic-gate 			}
1857c478bd9Sstevel@tonic-gate 			(void) memset(trans, 0, sizeof (*trans));
1867c478bd9Sstevel@tonic-gate 			if (tr_head == NULL)
1877c478bd9Sstevel@tonic-gate 				tr_head = trans;
1887c478bd9Sstevel@tonic-gate 			else
1897c478bd9Sstevel@tonic-gate 				prev_trans->tr_next = trans;
1907c478bd9Sstevel@tonic-gate 			prev_trans = trans;
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 			trans->tr_fd = t_open(nconf->nc_device, O_RDWR, NULL);
1937c478bd9Sstevel@tonic-gate 			if (trans->tr_fd < 0) {
1947c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR, "nfscast: t_open: %s:%m",
1957c478bd9Sstevel@tonic-gate 					nconf->nc_device);
1967c478bd9Sstevel@tonic-gate 				stat = RPC_CANTSEND;
1977c478bd9Sstevel@tonic-gate 				goto done_broad;
1987c478bd9Sstevel@tonic-gate 			}
1997c478bd9Sstevel@tonic-gate 			if (t_bind(trans->tr_fd, (struct t_bind *)NULL,
2007c478bd9Sstevel@tonic-gate 				(struct t_bind *)NULL) < 0) {
2017c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR, "nfscast: t_bind: %m");
2027c478bd9Sstevel@tonic-gate 				stat = RPC_CANTSEND;
2037c478bd9Sstevel@tonic-gate 				goto done_broad;
2047c478bd9Sstevel@tonic-gate 			}
2057c478bd9Sstevel@tonic-gate 			trans->tr_taddr =
2067c478bd9Sstevel@tonic-gate 				/* LINTED pointer alignment */
2077c478bd9Sstevel@tonic-gate 			(struct t_bind *)t_alloc(trans->tr_fd, T_BIND, T_ADDR);
2087c478bd9Sstevel@tonic-gate 			if (trans->tr_taddr == (struct t_bind *)NULL) {
2097c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR, "nfscast: t_alloc: %m");
2107c478bd9Sstevel@tonic-gate 				stat = RPC_SYSTEMERROR;
2117c478bd9Sstevel@tonic-gate 				goto done_broad;
2127c478bd9Sstevel@tonic-gate 			}
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 			trans->tr_device = nconf->nc_device;
2157c478bd9Sstevel@tonic-gate 			FD_SET(trans->tr_fd, &mask);
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 			if_inx = 0;
2187c478bd9Sstevel@tonic-gate 			hs.h_host = mfs->mfs_host;
2197c478bd9Sstevel@tonic-gate 			hs.h_serv = "rpcbind";
2207c478bd9Sstevel@tonic-gate 			if (netdir_getbyname(nconf, &hs, &retaddrs) == ND_OK) {
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 				/*
2237c478bd9Sstevel@tonic-gate 				 * If mfs->ignore is previously set for
2247c478bd9Sstevel@tonic-gate 				 * this map, clear it. Because a host can
2257c478bd9Sstevel@tonic-gate 				 * have either v6 or v4 address
2267c478bd9Sstevel@tonic-gate 				 */
2277c478bd9Sstevel@tonic-gate 				if (mfs->mfs_ignore == 1)
2287c478bd9Sstevel@tonic-gate 					mfs->mfs_ignore = 0;
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 				a = (struct addrs *)malloc(sizeof (*a));
2317c478bd9Sstevel@tonic-gate 				if (a == NULL) {
2327c478bd9Sstevel@tonic-gate 					syslog(LOG_ERR, "no memory");
2337c478bd9Sstevel@tonic-gate 					stat = RPC_CANTSEND;
2347c478bd9Sstevel@tonic-gate 					goto done_broad;
2357c478bd9Sstevel@tonic-gate 				}
2367c478bd9Sstevel@tonic-gate 				(void) memset(a, 0, sizeof (*a));
2377c478bd9Sstevel@tonic-gate 				if (trans->tr_addrs == NULL)
2387c478bd9Sstevel@tonic-gate 					trans->tr_addrs = a;
2397c478bd9Sstevel@tonic-gate 				else
2407c478bd9Sstevel@tonic-gate 					prev_addr->addr_next = a;
2417c478bd9Sstevel@tonic-gate 				prev_addr = a;
2427c478bd9Sstevel@tonic-gate 				a->addr_if_tstamps = NULL;
2437c478bd9Sstevel@tonic-gate 				a->addr_mfs = mfs;
2447c478bd9Sstevel@tonic-gate 				a->addr_addrs = retaddrs;
2457c478bd9Sstevel@tonic-gate 				if_cnt = retaddrs->n_cnt;
2467c478bd9Sstevel@tonic-gate 				while (if_cnt--) {
2477c478bd9Sstevel@tonic-gate 					ts = (struct tstamps *)
2487c478bd9Sstevel@tonic-gate 						malloc(sizeof (*ts));
2497c478bd9Sstevel@tonic-gate 					if (ts == NULL) {
2507c478bd9Sstevel@tonic-gate 						syslog(LOG_ERR, "no memory");
2517c478bd9Sstevel@tonic-gate 						stat = RPC_CANTSEND;
2527c478bd9Sstevel@tonic-gate 						goto done_broad;
2537c478bd9Sstevel@tonic-gate 					}
2547c478bd9Sstevel@tonic-gate 					(void) memset(ts, 0, sizeof (*ts));
2557c478bd9Sstevel@tonic-gate 					ts->ts_penalty = mfs->mfs_penalty;
2567c478bd9Sstevel@tonic-gate 					if (a->addr_if_tstamps == NULL)
2577c478bd9Sstevel@tonic-gate 						a->addr_if_tstamps = ts;
2587c478bd9Sstevel@tonic-gate 					else
2597c478bd9Sstevel@tonic-gate 						prev_ts->ts_next = ts;
2607c478bd9Sstevel@tonic-gate 					prev_ts = ts;
2617c478bd9Sstevel@tonic-gate 					ts->ts_inx = if_inx++;
2627c478bd9Sstevel@tonic-gate 					addr_cnt++;
2637c478bd9Sstevel@tonic-gate 				}
2647c478bd9Sstevel@tonic-gate 				break;
2657c478bd9Sstevel@tonic-gate 			} else {
2667c478bd9Sstevel@tonic-gate 				mfs->mfs_ignore = 1;
2677c478bd9Sstevel@tonic-gate 				if (verbose)
2687c478bd9Sstevel@tonic-gate 					syslog(LOG_ERR,
2697c478bd9Sstevel@tonic-gate 				"%s:%s address not known",
2707c478bd9Sstevel@tonic-gate 				mfs->mfs_host,
2717c478bd9Sstevel@tonic-gate 				strcmp(nconf->nc_proto, NC_INET)?"IPv6":"IPv4");
2727c478bd9Sstevel@tonic-gate 			}
2737c478bd9Sstevel@tonic-gate 		} /* while */
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate 		endnetconfig(nc);
2767c478bd9Sstevel@tonic-gate 		nc = NULL;
2777c478bd9Sstevel@tonic-gate 	} /* for */
2787c478bd9Sstevel@tonic-gate 	if (addr_cnt == 0) {
2797c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "nfscast: couldn't find addresses");
2807c478bd9Sstevel@tonic-gate 		stat = RPC_CANTSEND;
2817c478bd9Sstevel@tonic-gate 		goto done_broad;
2827c478bd9Sstevel@tonic-gate 	}
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate 	(void) gettimeofday(&t, (struct timezone *)0);
2857c478bd9Sstevel@tonic-gate 	xid = (getpid() ^ t.tv_sec ^ t.tv_usec) & ~0xFF;
2867c478bd9Sstevel@tonic-gate 	t.tv_usec = 0;
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate 	/* serialize the RPC header */
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate 	msg.rm_direction = CALL;
2917c478bd9Sstevel@tonic-gate 	msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
2927c478bd9Sstevel@tonic-gate 	msg.rm_call.cb_prog = RPCBPROG;
2937c478bd9Sstevel@tonic-gate 	/*
2947c478bd9Sstevel@tonic-gate 	 * we can not use RPCBVERS here since it doesn't exist in 4.X,
2957c478bd9Sstevel@tonic-gate 	 * the fix to bug 1139883 has made the 4.X portmapper silent to
2967c478bd9Sstevel@tonic-gate 	 * version mismatches. This causes the RPC call to the remote
2977c478bd9Sstevel@tonic-gate 	 * portmapper to simply be ignored if it's not Version 2.
2987c478bd9Sstevel@tonic-gate 	 */
2997c478bd9Sstevel@tonic-gate 	msg.rm_call.cb_vers = PMAPVERS;
3007c478bd9Sstevel@tonic-gate 	msg.rm_call.cb_proc = NULLPROC;
3017c478bd9Sstevel@tonic-gate 	if (sys_auth == (AUTH *)NULL) {
3027c478bd9Sstevel@tonic-gate 		stat = RPC_SYSTEMERROR;
3037c478bd9Sstevel@tonic-gate 		goto done_broad;
3047c478bd9Sstevel@tonic-gate 	}
3057c478bd9Sstevel@tonic-gate 	msg.rm_call.cb_cred = sys_auth->ah_cred;
3067c478bd9Sstevel@tonic-gate 	msg.rm_call.cb_verf = sys_auth->ah_verf;
3077c478bd9Sstevel@tonic-gate 	xdrmem_create(xdrs, outbuf, sizeof (outbuf), XDR_ENCODE);
3087c478bd9Sstevel@tonic-gate 	if (! xdr_callmsg(xdrs, &msg)) {
3097c478bd9Sstevel@tonic-gate 		stat = RPC_CANTENCODEARGS;
3107c478bd9Sstevel@tonic-gate 		goto done_broad;
3117c478bd9Sstevel@tonic-gate 	}
3127c478bd9Sstevel@tonic-gate 	outlen = (int)xdr_getpos(xdrs);
3137c478bd9Sstevel@tonic-gate 	xdr_destroy(xdrs);
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate 	t_udata.opt.len = 0;
3167c478bd9Sstevel@tonic-gate 	t_udata.udata.buf = outbuf;
3177c478bd9Sstevel@tonic-gate 	t_udata.udata.len = outlen;
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate 	/*
3207c478bd9Sstevel@tonic-gate 	 * Basic loop: send packet to all hosts and wait for response(s).
3217c478bd9Sstevel@tonic-gate 	 * The response timeout grows larger per iteration.
3227c478bd9Sstevel@tonic-gate 	 * A unique xid is assigned to each address in order to
3237c478bd9Sstevel@tonic-gate 	 * correctly match the replies.
3247c478bd9Sstevel@tonic-gate 	 */
3257c478bd9Sstevel@tonic-gate 	for (tsec = 4; timeout > 0; tsec *= 2) {
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate 		timeout -= tsec;
3287c478bd9Sstevel@tonic-gate 		if (timeout <= 0)
3297c478bd9Sstevel@tonic-gate 			tsec += timeout;
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate 		rcv_timeout.tv_sec = tsec;
3327c478bd9Sstevel@tonic-gate 		rcv_timeout.tv_usec = 0;
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate 		sent = 0;
3357c478bd9Sstevel@tonic-gate 		for (trans = tr_head; trans; trans = trans->tr_next) {
3367c478bd9Sstevel@tonic-gate 			for (a = trans->tr_addrs; a; a = a->addr_next) {
3377c478bd9Sstevel@tonic-gate 				struct netbuf *if_netbuf =
3387c478bd9Sstevel@tonic-gate 					a->addr_addrs->n_addrs;
3397c478bd9Sstevel@tonic-gate 				ts = a->addr_if_tstamps;
3407c478bd9Sstevel@tonic-gate 				if_cnt = a->addr_addrs->n_cnt;
3417c478bd9Sstevel@tonic-gate 				while (if_cnt--) {
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate 					/*
3447c478bd9Sstevel@tonic-gate 					 * xid is the first thing in
3457c478bd9Sstevel@tonic-gate 					 * preserialized buffer
3467c478bd9Sstevel@tonic-gate 					 */
3477c478bd9Sstevel@tonic-gate 					/* LINTED pointer alignment */
3487c478bd9Sstevel@tonic-gate 					*((ulong_t *)outbuf) =
3497c478bd9Sstevel@tonic-gate 						htonl(xid + ts->ts_inx);
3507c478bd9Sstevel@tonic-gate 					(void) gettimeofday(&(ts->ts_timeval),
3517c478bd9Sstevel@tonic-gate 						(struct timezone *)0);
3527c478bd9Sstevel@tonic-gate 					/*
3537c478bd9Sstevel@tonic-gate 					 * Check if already received
3547c478bd9Sstevel@tonic-gate 					 * from a previous iteration.
3557c478bd9Sstevel@tonic-gate 					 */
3567c478bd9Sstevel@tonic-gate 					if (ts->ts_rcvd) {
3577c478bd9Sstevel@tonic-gate 						sent++;
3587c478bd9Sstevel@tonic-gate 						ts = ts->ts_next;
3597c478bd9Sstevel@tonic-gate 						continue;
3607c478bd9Sstevel@tonic-gate 					}
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate 					t_udata.addr = *if_netbuf++;
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate 					if (t_sndudata(trans->tr_fd,
3657c478bd9Sstevel@tonic-gate 							&t_udata) == 0) {
3667c478bd9Sstevel@tonic-gate 						sent++;
3677c478bd9Sstevel@tonic-gate 					}
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate 					ts = ts->ts_next;
3707c478bd9Sstevel@tonic-gate 				}
3717c478bd9Sstevel@tonic-gate 			}
3727c478bd9Sstevel@tonic-gate 		}
3737c478bd9Sstevel@tonic-gate 		if (sent == 0) {		/* no packets sent ? */
3747c478bd9Sstevel@tonic-gate 			stat = RPC_CANTSEND;
3757c478bd9Sstevel@tonic-gate 			goto done_broad;
3767c478bd9Sstevel@tonic-gate 		}
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate 		/*
3797c478bd9Sstevel@tonic-gate 		 * Have sent all the packets.  Now collect the responses...
3807c478bd9Sstevel@tonic-gate 		 */
3817c478bd9Sstevel@tonic-gate 		rcvd = 0;
3827c478bd9Sstevel@tonic-gate 	recv_again:
3837c478bd9Sstevel@tonic-gate 		msg.acpted_rply.ar_verf = _null_auth;
3847c478bd9Sstevel@tonic-gate 		msg.acpted_rply.ar_results.proc = xdr_void;
3857c478bd9Sstevel@tonic-gate 		readfds = mask;
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate 		switch (select(dtbsize, &readfds,
3887c478bd9Sstevel@tonic-gate 			(fd_set *)NULL, (fd_set *)NULL, &rcv_timeout)) {
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate 		case 0: /* Timed out */
3917c478bd9Sstevel@tonic-gate 			/*
3927c478bd9Sstevel@tonic-gate 			 * If we got at least one response in the
3937c478bd9Sstevel@tonic-gate 			 * last interval, then don't wait for any
3947c478bd9Sstevel@tonic-gate 			 * more.  In theory we should wait for
3957c478bd9Sstevel@tonic-gate 			 * the max weighting (penalty) value so
3967c478bd9Sstevel@tonic-gate 			 * that a very slow server has a chance to
3977c478bd9Sstevel@tonic-gate 			 * respond but this could take a long time
3987c478bd9Sstevel@tonic-gate 			 * if the admin has set a high weighting
3997c478bd9Sstevel@tonic-gate 			 * value.
4007c478bd9Sstevel@tonic-gate 			 */
4017c478bd9Sstevel@tonic-gate 			if (rcvd > 0)
4027c478bd9Sstevel@tonic-gate 				goto done_broad;
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate 			stat = RPC_TIMEDOUT;
4057c478bd9Sstevel@tonic-gate 			continue;
4067c478bd9Sstevel@tonic-gate 
4077c478bd9Sstevel@tonic-gate 		case -1:  /* some kind of error */
4087c478bd9Sstevel@tonic-gate 			if (errno == EINTR)
4097c478bd9Sstevel@tonic-gate 				goto recv_again;
4107c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "nfscast: select: %m");
4117c478bd9Sstevel@tonic-gate 			if (rcvd == 0)
4127c478bd9Sstevel@tonic-gate 				stat = RPC_CANTRECV;
4137c478bd9Sstevel@tonic-gate 			goto done_broad;
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate 		}  /* end of select results switch */
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate 		for (trans = tr_head; trans; trans = trans->tr_next) {
4187c478bd9Sstevel@tonic-gate 			if (FD_ISSET(trans->tr_fd, &readfds))
4197c478bd9Sstevel@tonic-gate 				break;
4207c478bd9Sstevel@tonic-gate 		}
4217c478bd9Sstevel@tonic-gate 		if (trans == NULL)
4227c478bd9Sstevel@tonic-gate 			goto recv_again;
4237c478bd9Sstevel@tonic-gate 
4247c478bd9Sstevel@tonic-gate 	try_again:
4257c478bd9Sstevel@tonic-gate 		t_rdata.addr = trans->tr_taddr->addr;
4267c478bd9Sstevel@tonic-gate 		t_rdata.udata.buf = inbuf;
4277c478bd9Sstevel@tonic-gate 		t_rdata.udata.maxlen = sizeof (inbuf);
4287c478bd9Sstevel@tonic-gate 		t_rdata.udata.len = 0;
4297c478bd9Sstevel@tonic-gate 		t_rdata.opt.len = 0;
4307c478bd9Sstevel@tonic-gate 		if (t_rcvudata(trans->tr_fd, &t_rdata, &flag) < 0) {
4317c478bd9Sstevel@tonic-gate 			if (errno == EINTR)
4327c478bd9Sstevel@tonic-gate 				goto try_again;
4337c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "nfscast: t_rcvudata: %s:%m",
4347c478bd9Sstevel@tonic-gate 				trans->tr_device);
4357c478bd9Sstevel@tonic-gate 			stat = RPC_CANTRECV;
4367c478bd9Sstevel@tonic-gate 			continue;
4377c478bd9Sstevel@tonic-gate 		}
4387c478bd9Sstevel@tonic-gate 		if (t_rdata.udata.len < sizeof (ulong_t))
4397c478bd9Sstevel@tonic-gate 			goto recv_again;
4407c478bd9Sstevel@tonic-gate 		if (flag & T_MORE) {
4417c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR,
4427c478bd9Sstevel@tonic-gate 				"nfscast: t_rcvudata: %s: buffer overflow",
4437c478bd9Sstevel@tonic-gate 				trans->tr_device);
4447c478bd9Sstevel@tonic-gate 			goto recv_again;
4457c478bd9Sstevel@tonic-gate 		}
4467c478bd9Sstevel@tonic-gate 
4477c478bd9Sstevel@tonic-gate 		/*
4487c478bd9Sstevel@tonic-gate 		 * see if reply transaction id matches sent id.
4497c478bd9Sstevel@tonic-gate 		 * If so, decode the results.
4507c478bd9Sstevel@tonic-gate 		 * Note: received addr is ignored, it could be
4517c478bd9Sstevel@tonic-gate 		 * different from the send addr if the host has
4527c478bd9Sstevel@tonic-gate 		 * more than one addr.
4537c478bd9Sstevel@tonic-gate 		 */
4547c478bd9Sstevel@tonic-gate 		xdrmem_create(xdrs, inbuf, (uint_t)t_rdata.udata.len,
4557c478bd9Sstevel@tonic-gate 								XDR_DECODE);
4567c478bd9Sstevel@tonic-gate 		if (xdr_replymsg(xdrs, &msg)) {
4577c478bd9Sstevel@tonic-gate 		    if (msg.rm_reply.rp_stat == MSG_ACCEPTED &&
4587c478bd9Sstevel@tonic-gate 			(msg.rm_xid & ~0xFF) == xid) {
4597c478bd9Sstevel@tonic-gate 			struct addrs *curr_addr;
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate 			i = msg.rm_xid & 0xFF;
4627c478bd9Sstevel@tonic-gate 			for (curr_addr = trans->tr_addrs; curr_addr;
4637c478bd9Sstevel@tonic-gate 			    curr_addr = curr_addr->addr_next) {
4647c478bd9Sstevel@tonic-gate 			    for (ts = curr_addr->addr_if_tstamps; ts;
4657c478bd9Sstevel@tonic-gate 				ts = ts->ts_next)
4667c478bd9Sstevel@tonic-gate 				if (ts->ts_inx == i && !ts->ts_rcvd) {
4677c478bd9Sstevel@tonic-gate 					ts->ts_rcvd = 1;
4687c478bd9Sstevel@tonic-gate 					calc_resp_time(&ts->ts_timeval);
4697c478bd9Sstevel@tonic-gate 					stat = RPC_SUCCESS;
4707c478bd9Sstevel@tonic-gate 					rcvd++;
4717c478bd9Sstevel@tonic-gate 					break;
4727c478bd9Sstevel@tonic-gate 				}
4737c478bd9Sstevel@tonic-gate 			}
4747c478bd9Sstevel@tonic-gate 		    } /* otherwise, we just ignore the errors ... */
4757c478bd9Sstevel@tonic-gate 		}
4767c478bd9Sstevel@tonic-gate 		xdrs->x_op = XDR_FREE;
4777c478bd9Sstevel@tonic-gate 		msg.acpted_rply.ar_results.proc = xdr_void;
4787c478bd9Sstevel@tonic-gate 		(void) xdr_replymsg(xdrs, &msg);
4797c478bd9Sstevel@tonic-gate 		XDR_DESTROY(xdrs);
4807c478bd9Sstevel@tonic-gate 		if (rcvd == sent)
4817c478bd9Sstevel@tonic-gate 			goto done_broad;
4827c478bd9Sstevel@tonic-gate 		else
4837c478bd9Sstevel@tonic-gate 			goto recv_again;
4847c478bd9Sstevel@tonic-gate 	}
4857c478bd9Sstevel@tonic-gate 	if (!rcvd)
4867c478bd9Sstevel@tonic-gate 		stat = RPC_TIMEDOUT;
4877c478bd9Sstevel@tonic-gate 
4887c478bd9Sstevel@tonic-gate done_broad:
4897c478bd9Sstevel@tonic-gate 	if (rcvd) {
4907c478bd9Sstevel@tonic-gate 		*mfs_out = sort_responses(tr_head);
4917c478bd9Sstevel@tonic-gate 		stat = RPC_SUCCESS;
4927c478bd9Sstevel@tonic-gate 	}
4937c478bd9Sstevel@tonic-gate 	if (nc)
4947c478bd9Sstevel@tonic-gate 		endnetconfig(nc);
4957c478bd9Sstevel@tonic-gate 	free_transports(tr_head);
4967c478bd9Sstevel@tonic-gate 	AUTH_DESTROY(sys_auth);
4977c478bd9Sstevel@tonic-gate 	return (stat);
4987c478bd9Sstevel@tonic-gate }
4997c478bd9Sstevel@tonic-gate 
5007c478bd9Sstevel@tonic-gate /*
5017c478bd9Sstevel@tonic-gate  * Go through all the responses and sort fastest to slowest.
5027c478bd9Sstevel@tonic-gate  * Note that any penalty is added to the response time - so the
5037c478bd9Sstevel@tonic-gate  * fastest response isn't necessarily the one that arrived first.
5047c478bd9Sstevel@tonic-gate  */
5057c478bd9Sstevel@tonic-gate static struct mapfs *
sort_responses(trans)5067c478bd9Sstevel@tonic-gate sort_responses(trans)
5077c478bd9Sstevel@tonic-gate 	struct transp *trans;
5087c478bd9Sstevel@tonic-gate {
5097c478bd9Sstevel@tonic-gate 	struct transp *t;
5107c478bd9Sstevel@tonic-gate 	struct addrs *a;
5117c478bd9Sstevel@tonic-gate 	struct tstamps *ti;
5127c478bd9Sstevel@tonic-gate 	int i, size = 0, allocsize = 10;
5137c478bd9Sstevel@tonic-gate 	struct mapfs *p, *mfs_head = NULL, *mfs_tail = NULL;
5147c478bd9Sstevel@tonic-gate 	struct sm *buffer;
5157c478bd9Sstevel@tonic-gate 
5167c478bd9Sstevel@tonic-gate 	buffer = (struct sm *)malloc(allocsize * sizeof (struct sm));
5177c478bd9Sstevel@tonic-gate 	if (!buffer) {
5187c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "sort_responses: malloc error.\n");
5197c478bd9Sstevel@tonic-gate 		return (NULL);
5207c478bd9Sstevel@tonic-gate 	}
5217c478bd9Sstevel@tonic-gate 
5227c478bd9Sstevel@tonic-gate 	for (t = trans; t; t = t->tr_next) {
5237c478bd9Sstevel@tonic-gate 		for (a = t->tr_addrs; a; a = a->addr_next) {
5247c478bd9Sstevel@tonic-gate 			for (ti = a->addr_if_tstamps;
5257c478bd9Sstevel@tonic-gate 				ti; ti = ti->ts_next) {
5267c478bd9Sstevel@tonic-gate 				if (!ti->ts_rcvd)
5277c478bd9Sstevel@tonic-gate 					continue;
5287c478bd9Sstevel@tonic-gate 				ti->ts_timeval.tv_usec +=
5297c478bd9Sstevel@tonic-gate 					(ti->ts_penalty * PENALTY_WEIGHT);
5307c478bd9Sstevel@tonic-gate 				if (ti->ts_timeval.tv_usec >= 1000000) {
5317c478bd9Sstevel@tonic-gate 					ti->ts_timeval.tv_sec +=
5327c478bd9Sstevel@tonic-gate 					(ti->ts_timeval.tv_usec / 1000000);
5337c478bd9Sstevel@tonic-gate 					ti->ts_timeval.tv_usec =
5347c478bd9Sstevel@tonic-gate 					(ti->ts_timeval.tv_usec % 1000000);
5357c478bd9Sstevel@tonic-gate 				}
5367c478bd9Sstevel@tonic-gate 
5377c478bd9Sstevel@tonic-gate 				if (size >= allocsize) {
5387c478bd9Sstevel@tonic-gate 					allocsize += 10;
5397c478bd9Sstevel@tonic-gate 					buffer = (struct sm *)realloc(buffer,
5407c478bd9Sstevel@tonic-gate 					    allocsize * sizeof (struct sm));
5417c478bd9Sstevel@tonic-gate 					if (!buffer) {
5427c478bd9Sstevel@tonic-gate 						syslog(LOG_ERR,
5437c478bd9Sstevel@tonic-gate 					    "sort_responses: malloc error.\n");
5447c478bd9Sstevel@tonic-gate 						return (NULL);
5457c478bd9Sstevel@tonic-gate 					}
5467c478bd9Sstevel@tonic-gate 				}
5477c478bd9Sstevel@tonic-gate 				buffer[size].timeval = ti->ts_timeval;
5487c478bd9Sstevel@tonic-gate 				buffer[size].mfs = a->addr_mfs;
5497c478bd9Sstevel@tonic-gate 				size++;
5507c478bd9Sstevel@tonic-gate 			}
5517c478bd9Sstevel@tonic-gate 		}
5527c478bd9Sstevel@tonic-gate 	}
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate #ifdef DEBUG
5557c478bd9Sstevel@tonic-gate 	if (trace > 3) {
5567c478bd9Sstevel@tonic-gate 		trace_prt(1, "  sort_responses: before host sort:\n");
5577c478bd9Sstevel@tonic-gate 		for (i = 0; i < size; i++)
5587c478bd9Sstevel@tonic-gate 			trace_prt(1, "    %s %d.%d\n", buffer[i].mfs->mfs_host,
5597c478bd9Sstevel@tonic-gate 			buffer[i].timeval.tv_sec, buffer[i].timeval.tv_usec);
5607c478bd9Sstevel@tonic-gate 		trace_prt(0, "\n");
5617c478bd9Sstevel@tonic-gate 	}
5627c478bd9Sstevel@tonic-gate #endif
5637c478bd9Sstevel@tonic-gate 
5647c478bd9Sstevel@tonic-gate 	qsort((void *)buffer, size, sizeof (struct sm), host_sm);
5657c478bd9Sstevel@tonic-gate 
5667c478bd9Sstevel@tonic-gate 	/*
5677c478bd9Sstevel@tonic-gate 	 * Cope with multiply listed hosts  by choosing first time
5687c478bd9Sstevel@tonic-gate 	 */
5697c478bd9Sstevel@tonic-gate 	for (i = 1; i < size; i++) {
5707c478bd9Sstevel@tonic-gate #ifdef DEBUG
5717c478bd9Sstevel@tonic-gate 		if (trace > 3) {
5727c478bd9Sstevel@tonic-gate 			trace_prt(1, "  sort_responses: comparing %s and %s\n",
5737c478bd9Sstevel@tonic-gate 				buffer[i-1].mfs->mfs_host,
5747c478bd9Sstevel@tonic-gate 				buffer[i].mfs->mfs_host);
5757c478bd9Sstevel@tonic-gate 		}
5767c478bd9Sstevel@tonic-gate #endif
5777c478bd9Sstevel@tonic-gate 		if (strcmp(buffer[i-1].mfs->mfs_host,
5787c478bd9Sstevel@tonic-gate 		    buffer[i].mfs->mfs_host) == 0)
5797c478bd9Sstevel@tonic-gate 			memcpy(&buffer[i].timeval, &buffer[i-1].timeval,
5807c478bd9Sstevel@tonic-gate 				sizeof (struct timeval));
5817c478bd9Sstevel@tonic-gate 	}
5827c478bd9Sstevel@tonic-gate 	if (trace > 3)
5837c478bd9Sstevel@tonic-gate 		trace_prt(0, "\n");
5847c478bd9Sstevel@tonic-gate 
5857c478bd9Sstevel@tonic-gate #ifdef DEBUG
5867c478bd9Sstevel@tonic-gate 	if (trace > 3) {
5877c478bd9Sstevel@tonic-gate 		trace_prt(1, "  sort_responses: before time sort:\n");
5887c478bd9Sstevel@tonic-gate 		for (i = 0; i < size; i++)
5897c478bd9Sstevel@tonic-gate 			trace_prt(1, "    %s %d.%d\n", buffer[i].mfs->mfs_host,
5907c478bd9Sstevel@tonic-gate 			buffer[i].timeval.tv_sec, buffer[i].timeval.tv_usec);
5917c478bd9Sstevel@tonic-gate 		trace_prt(0, "\n");
5927c478bd9Sstevel@tonic-gate 	}
5937c478bd9Sstevel@tonic-gate #endif
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate 	qsort((void *)buffer, size, sizeof (struct sm), time_sm);
5967c478bd9Sstevel@tonic-gate 
5977c478bd9Sstevel@tonic-gate #ifdef DEBUG
5987c478bd9Sstevel@tonic-gate 	if (trace > 3) {
5997c478bd9Sstevel@tonic-gate 		trace_prt(1, "  sort_responses: after sort:\n");
6007c478bd9Sstevel@tonic-gate 		for (i = 0; i < size; i++)
6017c478bd9Sstevel@tonic-gate 			trace_prt(1, "    %s %d.%d\n", buffer[i].mfs->mfs_host,
6027c478bd9Sstevel@tonic-gate 			buffer[i].timeval.tv_sec, buffer[i].timeval.tv_usec);
6037c478bd9Sstevel@tonic-gate 		trace_prt(0, "\n");
6047c478bd9Sstevel@tonic-gate 	}
6057c478bd9Sstevel@tonic-gate #endif
6067c478bd9Sstevel@tonic-gate 
6077c478bd9Sstevel@tonic-gate 	for (i = 0; i < size; i++) {
6087c478bd9Sstevel@tonic-gate #ifdef DEBUG
6097c478bd9Sstevel@tonic-gate 		if (trace > 3) {
6107c478bd9Sstevel@tonic-gate 			trace_prt(1, "  sort_responses: adding %s\n",
6117c478bd9Sstevel@tonic-gate 				buffer[i].mfs->mfs_host);
6127c478bd9Sstevel@tonic-gate 		}
6137c478bd9Sstevel@tonic-gate #endif
6147c478bd9Sstevel@tonic-gate 		p = add_mfs(buffer[i].mfs, 0, &mfs_head, &mfs_tail);
6157c478bd9Sstevel@tonic-gate 		if (!p)
6167c478bd9Sstevel@tonic-gate 			return (NULL);
6177c478bd9Sstevel@tonic-gate 	}
6187c478bd9Sstevel@tonic-gate 	free(buffer);
6197c478bd9Sstevel@tonic-gate 
6207c478bd9Sstevel@tonic-gate 	return (mfs_head);
6217c478bd9Sstevel@tonic-gate }
6227c478bd9Sstevel@tonic-gate 
6237c478bd9Sstevel@tonic-gate 
6247c478bd9Sstevel@tonic-gate /*
6257c478bd9Sstevel@tonic-gate  * Comparison routines called by qsort(3).
6267c478bd9Sstevel@tonic-gate  */
host_sm(const void * a,const void * b)6277c478bd9Sstevel@tonic-gate static int host_sm(const void *a, const void *b)
6287c478bd9Sstevel@tonic-gate {
6297c478bd9Sstevel@tonic-gate 	return (strcmp(((struct sm *)a)->mfs->mfs_host,
6307c478bd9Sstevel@tonic-gate 			((struct sm *)b)->mfs->mfs_host));
6317c478bd9Sstevel@tonic-gate }
6327c478bd9Sstevel@tonic-gate 
time_sm(const void * a,const void * b)6337c478bd9Sstevel@tonic-gate static int time_sm(const void *a, const void *b)
6347c478bd9Sstevel@tonic-gate {
6357c478bd9Sstevel@tonic-gate 	if (timercmp(&(((struct sm *)a)->timeval),
6367c478bd9Sstevel@tonic-gate 	    &(((struct sm *)b)->timeval), < /* cstyle */))
6377c478bd9Sstevel@tonic-gate 		return (-1);
6387c478bd9Sstevel@tonic-gate 	else if (timercmp(&(((struct sm *)a)->timeval),
6397c478bd9Sstevel@tonic-gate 	    &(((struct sm *)b)->timeval), > /* cstyle */))
6407c478bd9Sstevel@tonic-gate 		return (1);
6417c478bd9Sstevel@tonic-gate 	else
6427c478bd9Sstevel@tonic-gate 		return (0);
6437c478bd9Sstevel@tonic-gate }
6447c478bd9Sstevel@tonic-gate 
6457c478bd9Sstevel@tonic-gate /*
6467c478bd9Sstevel@tonic-gate  * Given send_time which is the time a request
6477c478bd9Sstevel@tonic-gate  * was transmitted to a server, subtract it
6487c478bd9Sstevel@tonic-gate  * from the time "now" thereby converting it
6497c478bd9Sstevel@tonic-gate  * to an elapsed time.
6507c478bd9Sstevel@tonic-gate  */
6517c478bd9Sstevel@tonic-gate static void
calc_resp_time(send_time)6527c478bd9Sstevel@tonic-gate calc_resp_time(send_time)
6537c478bd9Sstevel@tonic-gate struct timeval *send_time;
6547c478bd9Sstevel@tonic-gate {
6557c478bd9Sstevel@tonic-gate 	struct timeval time_now;
6567c478bd9Sstevel@tonic-gate 
6577c478bd9Sstevel@tonic-gate 	(void) gettimeofday(&time_now, (struct timezone *)0);
6587c478bd9Sstevel@tonic-gate 	if (time_now.tv_usec <  send_time->tv_usec) {
6597c478bd9Sstevel@tonic-gate 		time_now.tv_sec--;
6607c478bd9Sstevel@tonic-gate 		time_now.tv_usec += 1000000;
6617c478bd9Sstevel@tonic-gate 	}
6627c478bd9Sstevel@tonic-gate 	send_time->tv_sec = time_now.tv_sec - send_time->tv_sec;
6637c478bd9Sstevel@tonic-gate 	send_time->tv_usec = time_now.tv_usec - send_time->tv_usec;
6647c478bd9Sstevel@tonic-gate }
6657c478bd9Sstevel@tonic-gate 
6667c478bd9Sstevel@tonic-gate static void
free_transports(trans)6677c478bd9Sstevel@tonic-gate free_transports(trans)
6687c478bd9Sstevel@tonic-gate 	struct transp *trans;
6697c478bd9Sstevel@tonic-gate {
6707c478bd9Sstevel@tonic-gate 	struct transp *t, *tmpt = NULL;
6717c478bd9Sstevel@tonic-gate 	struct addrs *a, *tmpa = NULL;
6727c478bd9Sstevel@tonic-gate 	struct tstamps *ts, *tmpts = NULL;
6737c478bd9Sstevel@tonic-gate 
6747c478bd9Sstevel@tonic-gate 	for (t = trans; t; t = tmpt) {
6757c478bd9Sstevel@tonic-gate 		if (t->tr_taddr)
6767c478bd9Sstevel@tonic-gate 			(void) t_free((char *)t->tr_taddr, T_BIND);
6777c478bd9Sstevel@tonic-gate 		if (t->tr_fd > 0)
6787c478bd9Sstevel@tonic-gate 			(void) t_close(t->tr_fd);
6797c478bd9Sstevel@tonic-gate 		for (a = t->tr_addrs; a; a = tmpa) {
6807c478bd9Sstevel@tonic-gate 			for (ts = a->addr_if_tstamps; ts; ts = tmpts) {
6817c478bd9Sstevel@tonic-gate 				tmpts = ts->ts_next;
6827c478bd9Sstevel@tonic-gate 				free(ts);
6837c478bd9Sstevel@tonic-gate 			}
6847c478bd9Sstevel@tonic-gate 			(void) netdir_free((char *)a->addr_addrs, ND_ADDRLIST);
6857c478bd9Sstevel@tonic-gate 			tmpa = a->addr_next;
6867c478bd9Sstevel@tonic-gate 			free(a);
6877c478bd9Sstevel@tonic-gate 		}
6887c478bd9Sstevel@tonic-gate 		tmpt = t->tr_next;
6897c478bd9Sstevel@tonic-gate 		free(t);
6907c478bd9Sstevel@tonic-gate 	}
6917c478bd9Sstevel@tonic-gate }
692