xref: /illumos-gate/usr/src/uts/common/rpc/svc_cots.c (revision 4a3b05278938491ea95d557939c130d3eb17cfd4)
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
5e810a982Svv149972  * Common Development and Distribution License (the "License").
6e810a982Svv149972  * 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  */
21cf98b944SMarcel Telka 
227c478bd9Sstevel@tonic-gate /*
23cf98b944SMarcel Telka  * Copyright 2015 Nexenta Systems, Inc.  All rights reserved.
240a4b0810SKaren Rochford  * Copyright (c) 1993, 2010, Oracle and/or its affiliates. All rights reserved.
25201f5ebaSSebastien Roy  * Copyright (c) 2012 by Delphix. All rights reserved.
26*4a3b0527SAndy Fiddaman  * Copyright 2012 Marcel Telka <marcel@telka.sk>
27*4a3b0527SAndy Fiddaman  * Copyright 2018 OmniOS Community Edition (OmniOSce) Association.
287c478bd9Sstevel@tonic-gate  */
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
317c478bd9Sstevel@tonic-gate /*	All Rights Reserved	*/
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate /*
347c478bd9Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley 4.3 BSD
357c478bd9Sstevel@tonic-gate  * under license from the Regents of the University of California.
367c478bd9Sstevel@tonic-gate  */
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate /*
397c478bd9Sstevel@tonic-gate  * svc_cots.c
407c478bd9Sstevel@tonic-gate  * Server side for connection-oriented RPC in the kernel.
417c478bd9Sstevel@tonic-gate  *
427c478bd9Sstevel@tonic-gate  */
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate #include <sys/param.h>
457c478bd9Sstevel@tonic-gate #include <sys/types.h>
467c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
477c478bd9Sstevel@tonic-gate #include <sys/file.h>
487c478bd9Sstevel@tonic-gate #include <sys/stream.h>
497c478bd9Sstevel@tonic-gate #include <sys/strsubr.h>
507c478bd9Sstevel@tonic-gate #include <sys/strsun.h>
517c478bd9Sstevel@tonic-gate #include <sys/stropts.h>
527c478bd9Sstevel@tonic-gate #include <sys/tiuser.h>
537c478bd9Sstevel@tonic-gate #include <sys/timod.h>
547c478bd9Sstevel@tonic-gate #include <sys/tihdr.h>
557c478bd9Sstevel@tonic-gate #include <sys/fcntl.h>
567c478bd9Sstevel@tonic-gate #include <sys/errno.h>
577c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
587c478bd9Sstevel@tonic-gate #include <sys/systm.h>
597c478bd9Sstevel@tonic-gate #include <sys/debug.h>
607c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
617c478bd9Sstevel@tonic-gate #include <sys/kstat.h>
627c478bd9Sstevel@tonic-gate #include <sys/vtrace.h>
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate #include <rpc/types.h>
657c478bd9Sstevel@tonic-gate #include <rpc/xdr.h>
667c478bd9Sstevel@tonic-gate #include <rpc/auth.h>
677c478bd9Sstevel@tonic-gate #include <rpc/rpc_msg.h>
687c478bd9Sstevel@tonic-gate #include <rpc/svc.h>
69e810a982Svv149972 #include <inet/ip.h>
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate #define	COTS_MAX_ALLOCSIZE	2048
727c478bd9Sstevel@tonic-gate #define	MSG_OFFSET		128	/* offset of call into the mblk */
737c478bd9Sstevel@tonic-gate #define	RM_HDR_SIZE		4	/* record mark header size */
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate /*
767c478bd9Sstevel@tonic-gate  * Routines exported through ops vector.
777c478bd9Sstevel@tonic-gate  */
787c478bd9Sstevel@tonic-gate static bool_t		svc_cots_krecv(SVCXPRT *, mblk_t *, struct rpc_msg *);
797c478bd9Sstevel@tonic-gate static bool_t		svc_cots_ksend(SVCXPRT *, struct rpc_msg *);
807c478bd9Sstevel@tonic-gate static bool_t		svc_cots_kgetargs(SVCXPRT *, xdrproc_t, caddr_t);
817c478bd9Sstevel@tonic-gate static bool_t		svc_cots_kfreeargs(SVCXPRT *, xdrproc_t, caddr_t);
827c478bd9Sstevel@tonic-gate static void		svc_cots_kdestroy(SVCMASTERXPRT *);
837c478bd9Sstevel@tonic-gate static int		svc_cots_kdup(struct svc_req *, caddr_t, int,
847c478bd9Sstevel@tonic-gate 				struct dupreq **, bool_t *);
857c478bd9Sstevel@tonic-gate static void		svc_cots_kdupdone(struct dupreq *, caddr_t,
867c478bd9Sstevel@tonic-gate 				void (*)(), int, int);
877c478bd9Sstevel@tonic-gate static int32_t		*svc_cots_kgetres(SVCXPRT *, int);
887c478bd9Sstevel@tonic-gate static void		svc_cots_kfreeres(SVCXPRT *);
897c478bd9Sstevel@tonic-gate static void		svc_cots_kclone_destroy(SVCXPRT *);
907c478bd9Sstevel@tonic-gate static void		svc_cots_kstart(SVCMASTERXPRT *);
910a4b0810SKaren Rochford static void		svc_cots_ktattrs(SVCXPRT *, int, void **);
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate /*
947c478bd9Sstevel@tonic-gate  * Server transport operations vector.
957c478bd9Sstevel@tonic-gate  */
967c478bd9Sstevel@tonic-gate struct svc_ops svc_cots_op = {
977c478bd9Sstevel@tonic-gate 	svc_cots_krecv,		/* Get requests */
987c478bd9Sstevel@tonic-gate 	svc_cots_kgetargs,	/* Deserialize arguments */
997c478bd9Sstevel@tonic-gate 	svc_cots_ksend,		/* Send reply */
1007c478bd9Sstevel@tonic-gate 	svc_cots_kfreeargs,	/* Free argument data space */
1017c478bd9Sstevel@tonic-gate 	svc_cots_kdestroy,	/* Destroy transport handle */
1027c478bd9Sstevel@tonic-gate 	svc_cots_kdup,		/* Check entry in dup req cache */
1037c478bd9Sstevel@tonic-gate 	svc_cots_kdupdone,	/* Mark entry in dup req cache as done */
1047c478bd9Sstevel@tonic-gate 	svc_cots_kgetres,	/* Get pointer to response buffer */
1057c478bd9Sstevel@tonic-gate 	svc_cots_kfreeres,	/* Destroy pre-serialized response header */
1067c478bd9Sstevel@tonic-gate 	svc_cots_kclone_destroy, /* Destroy a clone xprt */
10760536ef9SKaren Rochford 	svc_cots_kstart,	/* Tell `ready-to-receive' to rpcmod */
1080a4b0810SKaren Rochford 	NULL,			/* Transport specific clone xprt */
109*4a3b0527SAndy Fiddaman 	svc_cots_ktattrs,	/* Transport Attributes */
110*4a3b0527SAndy Fiddaman 	mir_svc_hold,		/* Increment transport reference count */
111*4a3b0527SAndy Fiddaman 	mir_svc_release		/* Decrement transport reference count */
1127c478bd9Sstevel@tonic-gate };
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate /*
1157c478bd9Sstevel@tonic-gate  * Master transport private data.
1167c478bd9Sstevel@tonic-gate  * Kept in xprt->xp_p2.
1177c478bd9Sstevel@tonic-gate  */
1187c478bd9Sstevel@tonic-gate struct cots_master_data {
1197c478bd9Sstevel@tonic-gate 	char	*cmd_src_addr;	/* client's address */
1207c478bd9Sstevel@tonic-gate 	int	cmd_xprt_started; /* flag for clone routine to call */
1217c478bd9Sstevel@tonic-gate 				/* rpcmod's start routine. */
1227c478bd9Sstevel@tonic-gate 	struct rpc_cots_server *cmd_stats;	/* stats for zone */
1237c478bd9Sstevel@tonic-gate };
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate /*
1267c478bd9Sstevel@tonic-gate  * Transport private data.
1277c478bd9Sstevel@tonic-gate  * Kept in clone_xprt->xp_p2buf.
1287c478bd9Sstevel@tonic-gate  */
1297c478bd9Sstevel@tonic-gate typedef struct cots_data {
1307c478bd9Sstevel@tonic-gate 	mblk_t	*cd_mp;		/* pre-allocated reply message */
1317c478bd9Sstevel@tonic-gate 	mblk_t	*cd_req_mp;	/* request message */
1327c478bd9Sstevel@tonic-gate } cots_data_t;
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate /*
1357c478bd9Sstevel@tonic-gate  * Server statistics
1367c478bd9Sstevel@tonic-gate  * NOTE: This structure type is duplicated in the NFS fast path.
1377c478bd9Sstevel@tonic-gate  */
1387c478bd9Sstevel@tonic-gate static const struct rpc_cots_server {
1397c478bd9Sstevel@tonic-gate 	kstat_named_t	rscalls;
1407c478bd9Sstevel@tonic-gate 	kstat_named_t	rsbadcalls;
1417c478bd9Sstevel@tonic-gate 	kstat_named_t	rsnullrecv;
1427c478bd9Sstevel@tonic-gate 	kstat_named_t	rsbadlen;
1437c478bd9Sstevel@tonic-gate 	kstat_named_t	rsxdrcall;
1447c478bd9Sstevel@tonic-gate 	kstat_named_t	rsdupchecks;
1457c478bd9Sstevel@tonic-gate 	kstat_named_t	rsdupreqs;
1467c478bd9Sstevel@tonic-gate } cots_rsstat_tmpl = {
1477c478bd9Sstevel@tonic-gate 	{ "calls",	KSTAT_DATA_UINT64 },
1487c478bd9Sstevel@tonic-gate 	{ "badcalls",	KSTAT_DATA_UINT64 },
1497c478bd9Sstevel@tonic-gate 	{ "nullrecv",	KSTAT_DATA_UINT64 },
1507c478bd9Sstevel@tonic-gate 	{ "badlen",	KSTAT_DATA_UINT64 },
1517c478bd9Sstevel@tonic-gate 	{ "xdrcall",	KSTAT_DATA_UINT64 },
1527c478bd9Sstevel@tonic-gate 	{ "dupchecks",	KSTAT_DATA_UINT64 },
1537c478bd9Sstevel@tonic-gate 	{ "dupreqs",	KSTAT_DATA_UINT64 }
1547c478bd9Sstevel@tonic-gate };
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate #define	CLONE2STATS(clone_xprt)	\
1577c478bd9Sstevel@tonic-gate 	((struct cots_master_data *)(clone_xprt)->xp_master->xp_p2)->cmd_stats
1587c478bd9Sstevel@tonic-gate #define	RSSTAT_INCR(s, x)	\
1591a5e258fSJosef 'Jeff' Sipek 	atomic_inc_64(&(s)->x.value.ui64)
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate /*
1627c478bd9Sstevel@tonic-gate  * Pointer to a transport specific `ready to receive' function in rpcmod
1637c478bd9Sstevel@tonic-gate  * (set from rpcmod).
1647c478bd9Sstevel@tonic-gate  */
1657c478bd9Sstevel@tonic-gate void    (*mir_start)(queue_t *);
1667c478bd9Sstevel@tonic-gate uint_t	*svc_max_msg_sizep;
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate /*
1697c478bd9Sstevel@tonic-gate  * the address size of the underlying transport can sometimes be
1707c478bd9Sstevel@tonic-gate  * unknown (tinfo->ADDR_size == -1).  For this case, it is
1717c478bd9Sstevel@tonic-gate  * necessary to figure out what the size is so the correct amount
1727c478bd9Sstevel@tonic-gate  * of data is allocated.  This is an itterative process:
1737c478bd9Sstevel@tonic-gate  *	1. take a good guess (use T_MINADDRSIZE)
1747c478bd9Sstevel@tonic-gate  *	2. try it.
1757c478bd9Sstevel@tonic-gate  *	3. if it works then everything is ok
1767c478bd9Sstevel@tonic-gate  *	4. if the error is ENAMETOLONG, double the guess
1777c478bd9Sstevel@tonic-gate  *	5. go back to step 2.
1787c478bd9Sstevel@tonic-gate  */
1797c478bd9Sstevel@tonic-gate #define	T_UNKNOWNADDRSIZE	(-1)
1807c478bd9Sstevel@tonic-gate #define	T_MINADDRSIZE	32
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate /*
1837c478bd9Sstevel@tonic-gate  * Create a transport record.
1847c478bd9Sstevel@tonic-gate  * The transport record, output buffer, and private data structure
1857c478bd9Sstevel@tonic-gate  * are allocated.  The output buffer is serialized into using xdrmem.
1867c478bd9Sstevel@tonic-gate  * There is one transport record per user process which implements a
1877c478bd9Sstevel@tonic-gate  * set of services.
1887c478bd9Sstevel@tonic-gate  */
1897c478bd9Sstevel@tonic-gate static kmutex_t cots_kcreate_lock;
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate int
svc_cots_kcreate(file_t * fp,uint_t max_msgsize,struct T_info_ack * tinfo,SVCMASTERXPRT ** nxprt)1927c478bd9Sstevel@tonic-gate svc_cots_kcreate(file_t *fp, uint_t max_msgsize, struct T_info_ack *tinfo,
1937c478bd9Sstevel@tonic-gate     SVCMASTERXPRT **nxprt)
1947c478bd9Sstevel@tonic-gate {
1957c478bd9Sstevel@tonic-gate 	struct cots_master_data *cmd;
196e810a982Svv149972 	int err, retval;
1977c478bd9Sstevel@tonic-gate 	SVCMASTERXPRT *xprt;
1987c478bd9Sstevel@tonic-gate 	struct rpcstat *rpcstat;
199e810a982Svv149972 	struct T_addr_ack *ack_p;
200e810a982Svv149972 	struct strioctl getaddr;
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate 	if (nxprt == NULL)
2037c478bd9Sstevel@tonic-gate 		return (EINVAL);
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 	rpcstat = zone_getspecific(rpcstat_zone_key, curproc->p_zone);
2067c478bd9Sstevel@tonic-gate 	ASSERT(rpcstat != NULL);
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 	xprt = kmem_zalloc(sizeof (SVCMASTERXPRT), KM_SLEEP);
2097c478bd9Sstevel@tonic-gate 
210e810a982Svv149972 	cmd = kmem_zalloc(sizeof (*cmd) + sizeof (*ack_p)
211e810a982Svv149972 	    + (2 * sizeof (sin6_t)), KM_SLEEP);
212e810a982Svv149972 
213e810a982Svv149972 	ack_p = (struct T_addr_ack *)&cmd[1];
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 	if ((tinfo->TIDU_size > COTS_MAX_ALLOCSIZE) ||
2167c478bd9Sstevel@tonic-gate 	    (tinfo->TIDU_size <= 0))
2177c478bd9Sstevel@tonic-gate 		xprt->xp_msg_size = COTS_MAX_ALLOCSIZE;
2187c478bd9Sstevel@tonic-gate 	else {
2197c478bd9Sstevel@tonic-gate 		xprt->xp_msg_size = tinfo->TIDU_size -
2207c478bd9Sstevel@tonic-gate 		    (tinfo->TIDU_size % BYTES_PER_XDR_UNIT);
2217c478bd9Sstevel@tonic-gate 	}
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 	xprt->xp_ops = &svc_cots_op;
2247c478bd9Sstevel@tonic-gate 	xprt->xp_p2 = (caddr_t)cmd;
2257c478bd9Sstevel@tonic-gate 	cmd->cmd_xprt_started = 0;
2267c478bd9Sstevel@tonic-gate 	cmd->cmd_stats = rpcstat->rpc_cots_server;
2277c478bd9Sstevel@tonic-gate 
228e810a982Svv149972 	getaddr.ic_cmd = TI_GETINFO;
229e810a982Svv149972 	getaddr.ic_timout = -1;
230e810a982Svv149972 	getaddr.ic_len = sizeof (*ack_p) + (2 * sizeof (sin6_t));
231e810a982Svv149972 	getaddr.ic_dp = (char *)ack_p;
232e810a982Svv149972 	ack_p->PRIM_type = T_ADDR_REQ;
2337c478bd9Sstevel@tonic-gate 
234e810a982Svv149972 	err = strioctl(fp->f_vnode, I_STR, (intptr_t)&getaddr,
235e810a982Svv149972 	    0, K_TO_K, CRED(), &retval);
2367c478bd9Sstevel@tonic-gate 	if (err) {
237e810a982Svv149972 		kmem_free(cmd, sizeof (*cmd) + sizeof (*ack_p) +
238e810a982Svv149972 		    (2 * sizeof (sin6_t)));
2397c478bd9Sstevel@tonic-gate 		kmem_free(xprt, sizeof (SVCMASTERXPRT));
2407c478bd9Sstevel@tonic-gate 		return (err);
2417c478bd9Sstevel@tonic-gate 	}
2427c478bd9Sstevel@tonic-gate 
243e810a982Svv149972 	xprt->xp_rtaddr.maxlen = ack_p->REMADDR_length;
244e810a982Svv149972 	xprt->xp_rtaddr.len = ack_p->REMADDR_length;
245e810a982Svv149972 	cmd->cmd_src_addr = xprt->xp_rtaddr.buf =
246e810a982Svv149972 	    (char *)ack_p + ack_p->REMADDR_offset;
247e810a982Svv149972 
248e810a982Svv149972 	xprt->xp_lcladdr.maxlen = ack_p->LOCADDR_length;
249e810a982Svv149972 	xprt->xp_lcladdr.len = ack_p->LOCADDR_length;
250e810a982Svv149972 	xprt->xp_lcladdr.buf = (char *)ack_p + ack_p->LOCADDR_offset;
251e810a982Svv149972 
2527c478bd9Sstevel@tonic-gate 	/*
2537c478bd9Sstevel@tonic-gate 	 * If the current sanity check size in rpcmod is smaller
2547c478bd9Sstevel@tonic-gate 	 * than the size needed for this xprt, then increase
2557c478bd9Sstevel@tonic-gate 	 * the sanity check.
2567c478bd9Sstevel@tonic-gate 	 */
2577c478bd9Sstevel@tonic-gate 	if (max_msgsize != 0 && svc_max_msg_sizep &&
2587c478bd9Sstevel@tonic-gate 	    max_msgsize > *svc_max_msg_sizep) {
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate 		/* This check needs a lock */
2617c478bd9Sstevel@tonic-gate 		mutex_enter(&cots_kcreate_lock);
2627c478bd9Sstevel@tonic-gate 		if (svc_max_msg_sizep && max_msgsize > *svc_max_msg_sizep)
2637c478bd9Sstevel@tonic-gate 			*svc_max_msg_sizep = max_msgsize;
2647c478bd9Sstevel@tonic-gate 		mutex_exit(&cots_kcreate_lock);
2657c478bd9Sstevel@tonic-gate 	}
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 	*nxprt = xprt;
268e810a982Svv149972 
2697c478bd9Sstevel@tonic-gate 	return (0);
2707c478bd9Sstevel@tonic-gate }
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate /*
2737c478bd9Sstevel@tonic-gate  * Destroy a master transport record.
2747c478bd9Sstevel@tonic-gate  * Frees the space allocated for a transport record.
2757c478bd9Sstevel@tonic-gate  */
2767c478bd9Sstevel@tonic-gate static void
svc_cots_kdestroy(SVCMASTERXPRT * xprt)2777c478bd9Sstevel@tonic-gate svc_cots_kdestroy(SVCMASTERXPRT *xprt)
2787c478bd9Sstevel@tonic-gate {
2797c478bd9Sstevel@tonic-gate 	struct cots_master_data *cmd = (struct cots_master_data *)xprt->xp_p2;
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate 	ASSERT(cmd);
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate 	if (xprt->xp_netid)
2847c478bd9Sstevel@tonic-gate 		kmem_free(xprt->xp_netid, strlen(xprt->xp_netid) + 1);
2857c478bd9Sstevel@tonic-gate 	if (xprt->xp_addrmask.maxlen)
2867c478bd9Sstevel@tonic-gate 		kmem_free(xprt->xp_addrmask.buf, xprt->xp_addrmask.maxlen);
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate 	mutex_destroy(&xprt->xp_req_lock);
2897c478bd9Sstevel@tonic-gate 	mutex_destroy(&xprt->xp_thread_lock);
2907c478bd9Sstevel@tonic-gate 
291e810a982Svv149972 	kmem_free(cmd, sizeof (*cmd) + sizeof (struct T_addr_ack) +
292e810a982Svv149972 	    (2 * sizeof (sin6_t)));
293e810a982Svv149972 
2947c478bd9Sstevel@tonic-gate 	kmem_free(xprt, sizeof (SVCMASTERXPRT));
2957c478bd9Sstevel@tonic-gate }
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate /*
2987c478bd9Sstevel@tonic-gate  * svc_tli_kcreate() calls this function at the end to tell
2997c478bd9Sstevel@tonic-gate  * rpcmod that the transport is ready to receive requests.
3007c478bd9Sstevel@tonic-gate  */
3017c478bd9Sstevel@tonic-gate static void
svc_cots_kstart(SVCMASTERXPRT * xprt)3027c478bd9Sstevel@tonic-gate svc_cots_kstart(SVCMASTERXPRT *xprt)
3037c478bd9Sstevel@tonic-gate {
3047c478bd9Sstevel@tonic-gate 	struct cots_master_data *cmd = (struct cots_master_data *)xprt->xp_p2;
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 	if (cmd->cmd_xprt_started == 0) {
3077c478bd9Sstevel@tonic-gate 		/*
3087c478bd9Sstevel@tonic-gate 		 * Acquire the xp_req_lock in order to use xp_wq
3097c478bd9Sstevel@tonic-gate 		 * safely (we don't want to qenable a queue that has
3107c478bd9Sstevel@tonic-gate 		 * already been closed).
3117c478bd9Sstevel@tonic-gate 		 */
3127c478bd9Sstevel@tonic-gate 		mutex_enter(&xprt->xp_req_lock);
3137c478bd9Sstevel@tonic-gate 		if (cmd->cmd_xprt_started == 0 &&
3147c478bd9Sstevel@tonic-gate 		    xprt->xp_wq != NULL) {
3157c478bd9Sstevel@tonic-gate 			(*mir_start)(xprt->xp_wq);
3167c478bd9Sstevel@tonic-gate 			cmd->cmd_xprt_started = 1;
3177c478bd9Sstevel@tonic-gate 		}
3187c478bd9Sstevel@tonic-gate 		mutex_exit(&xprt->xp_req_lock);
3197c478bd9Sstevel@tonic-gate 	}
3207c478bd9Sstevel@tonic-gate }
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate /*
3237c478bd9Sstevel@tonic-gate  * Transport-type specific part of svc_xprt_cleanup().
3247c478bd9Sstevel@tonic-gate  */
3257c478bd9Sstevel@tonic-gate static void
svc_cots_kclone_destroy(SVCXPRT * clone_xprt)3267c478bd9Sstevel@tonic-gate svc_cots_kclone_destroy(SVCXPRT *clone_xprt)
3277c478bd9Sstevel@tonic-gate {
3287c478bd9Sstevel@tonic-gate 	cots_data_t *cd = (cots_data_t *)clone_xprt->xp_p2buf;
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate 	if (cd->cd_req_mp) {
3317c478bd9Sstevel@tonic-gate 		freemsg(cd->cd_req_mp);
3327c478bd9Sstevel@tonic-gate 		cd->cd_req_mp = (mblk_t *)0;
3337c478bd9Sstevel@tonic-gate 	}
3347c478bd9Sstevel@tonic-gate 	ASSERT(cd->cd_mp == NULL);
3357c478bd9Sstevel@tonic-gate }
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate /*
3380a4b0810SKaren Rochford  * Transport Attributes.
3390a4b0810SKaren Rochford  */
3400a4b0810SKaren Rochford static void
svc_cots_ktattrs(SVCXPRT * clone_xprt,int attrflag,void ** tattr)3410a4b0810SKaren Rochford svc_cots_ktattrs(SVCXPRT *clone_xprt, int attrflag, void **tattr)
3420a4b0810SKaren Rochford {
3430a4b0810SKaren Rochford 	*tattr = NULL;
3440a4b0810SKaren Rochford 
3450a4b0810SKaren Rochford 	switch (attrflag) {
3460a4b0810SKaren Rochford 	case SVC_TATTR_ADDRMASK:
3470a4b0810SKaren Rochford 		*tattr = (void *)&clone_xprt->xp_master->xp_addrmask;
3480a4b0810SKaren Rochford 	}
3490a4b0810SKaren Rochford }
3500a4b0810SKaren Rochford 
3510a4b0810SKaren Rochford /*
3527c478bd9Sstevel@tonic-gate  * Receive rpc requests.
3537c478bd9Sstevel@tonic-gate  * Checks if the message is intact, and deserializes the call packet.
3547c478bd9Sstevel@tonic-gate  */
3557c478bd9Sstevel@tonic-gate static bool_t
svc_cots_krecv(SVCXPRT * clone_xprt,mblk_t * mp,struct rpc_msg * msg)3567c478bd9Sstevel@tonic-gate svc_cots_krecv(SVCXPRT *clone_xprt, mblk_t *mp, struct rpc_msg *msg)
3577c478bd9Sstevel@tonic-gate {
3587c478bd9Sstevel@tonic-gate 	cots_data_t *cd = (cots_data_t *)clone_xprt->xp_p2buf;
3597c478bd9Sstevel@tonic-gate 	XDR *xdrs = &clone_xprt->xp_xdrin;
3607c478bd9Sstevel@tonic-gate 	struct rpc_cots_server *stats = CLONE2STATS(clone_xprt);
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate 	TRACE_0(TR_FAC_KRPC, TR_SVC_COTS_KRECV_START,
3637c478bd9Sstevel@tonic-gate 	    "svc_cots_krecv_start:");
3647c478bd9Sstevel@tonic-gate 	RPCLOG(4, "svc_cots_krecv_start clone_xprt = %p:\n",
3657c478bd9Sstevel@tonic-gate 	    (void *)clone_xprt);
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate 	RSSTAT_INCR(stats, rscalls);
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate 	if (mp->b_datap->db_type != M_DATA) {
3707c478bd9Sstevel@tonic-gate 		RPCLOG(16, "svc_cots_krecv bad db_type %d\n",
3717c478bd9Sstevel@tonic-gate 		    mp->b_datap->db_type);
3727c478bd9Sstevel@tonic-gate 		goto bad;
3737c478bd9Sstevel@tonic-gate 	}
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate 	xdrmblk_init(xdrs, mp, XDR_DECODE, 0);
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate 	TRACE_0(TR_FAC_KRPC, TR_XDR_CALLMSG_START,
3787c478bd9Sstevel@tonic-gate 	    "xdr_callmsg_start:");
3797c478bd9Sstevel@tonic-gate 	RPCLOG0(4, "xdr_callmsg_start:\n");
3807c478bd9Sstevel@tonic-gate 	if (!xdr_callmsg(xdrs, msg)) {
381cf98b944SMarcel Telka 		XDR_DESTROY(xdrs);
3827c478bd9Sstevel@tonic-gate 		TRACE_1(TR_FAC_KRPC, TR_XDR_CALLMSG_END,
3837c478bd9Sstevel@tonic-gate 		    "xdr_callmsg_end:(%S)", "bad");
3847c478bd9Sstevel@tonic-gate 		RPCLOG0(1, "svc_cots_krecv xdr_callmsg failure\n");
3857c478bd9Sstevel@tonic-gate 		RSSTAT_INCR(stats, rsxdrcall);
3867c478bd9Sstevel@tonic-gate 		goto bad;
3877c478bd9Sstevel@tonic-gate 	}
3887c478bd9Sstevel@tonic-gate 	TRACE_1(TR_FAC_KRPC, TR_XDR_CALLMSG_END,
3897c478bd9Sstevel@tonic-gate 	    "xdr_callmsg_end:(%S)", "good");
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate 	clone_xprt->xp_xid = msg->rm_xid;
3927c478bd9Sstevel@tonic-gate 	cd->cd_req_mp = mp;
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate 	TRACE_1(TR_FAC_KRPC, TR_SVC_COTS_KRECV_END,
3957c478bd9Sstevel@tonic-gate 	    "svc_cots_krecv_end:(%S)", "good");
3967c478bd9Sstevel@tonic-gate 	RPCLOG0(4, "svc_cots_krecv_end:good\n");
3977c478bd9Sstevel@tonic-gate 	return (TRUE);
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate bad:
4007c478bd9Sstevel@tonic-gate 	if (mp)
4017c478bd9Sstevel@tonic-gate 		freemsg(mp);
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate 	RSSTAT_INCR(stats, rsbadcalls);
4047c478bd9Sstevel@tonic-gate 	TRACE_1(TR_FAC_KRPC, TR_SVC_COTS_KRECV_END,
4057c478bd9Sstevel@tonic-gate 	    "svc_cots_krecv_end:(%S)", "bad");
4067c478bd9Sstevel@tonic-gate 	return (FALSE);
4077c478bd9Sstevel@tonic-gate }
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate /*
4107c478bd9Sstevel@tonic-gate  * Send rpc reply.
4117c478bd9Sstevel@tonic-gate  */
4127c478bd9Sstevel@tonic-gate static bool_t
svc_cots_ksend(SVCXPRT * clone_xprt,struct rpc_msg * msg)4137c478bd9Sstevel@tonic-gate svc_cots_ksend(SVCXPRT *clone_xprt, struct rpc_msg *msg)
4147c478bd9Sstevel@tonic-gate {
4157c478bd9Sstevel@tonic-gate 	/* LINTED pointer alignment */
4167c478bd9Sstevel@tonic-gate 	cots_data_t *cd = (cots_data_t *)clone_xprt->xp_p2buf;
4177c478bd9Sstevel@tonic-gate 	XDR *xdrs = &(clone_xprt->xp_xdrout);
4187c478bd9Sstevel@tonic-gate 	int retval = FALSE;
4197c478bd9Sstevel@tonic-gate 	mblk_t *mp;
4207c478bd9Sstevel@tonic-gate 	xdrproc_t xdr_results;
4217c478bd9Sstevel@tonic-gate 	caddr_t xdr_location;
4227c478bd9Sstevel@tonic-gate 	bool_t has_args;
4237c478bd9Sstevel@tonic-gate 
4247c478bd9Sstevel@tonic-gate 	TRACE_0(TR_FAC_KRPC, TR_SVC_COTS_KSEND_START,
4257c478bd9Sstevel@tonic-gate 	    "svc_cots_ksend_start:");
4267c478bd9Sstevel@tonic-gate 
4277c478bd9Sstevel@tonic-gate 	/*
4287c478bd9Sstevel@tonic-gate 	 * If there is a result procedure specified in the reply message,
4297c478bd9Sstevel@tonic-gate 	 * it will be processed in the xdr_replymsg and SVCAUTH_WRAP.
4307c478bd9Sstevel@tonic-gate 	 * We need to make sure it won't be processed twice, so we null
4317c478bd9Sstevel@tonic-gate 	 * it for xdr_replymsg here.
4327c478bd9Sstevel@tonic-gate 	 */
4337c478bd9Sstevel@tonic-gate 	has_args = FALSE;
4347c478bd9Sstevel@tonic-gate 	if (msg->rm_reply.rp_stat == MSG_ACCEPTED &&
4357c478bd9Sstevel@tonic-gate 	    msg->rm_reply.rp_acpt.ar_stat == SUCCESS) {
4367c478bd9Sstevel@tonic-gate 		if ((xdr_results = msg->acpted_rply.ar_results.proc) != NULL) {
4377c478bd9Sstevel@tonic-gate 			has_args = TRUE;
4387c478bd9Sstevel@tonic-gate 			xdr_location = msg->acpted_rply.ar_results.where;
4397c478bd9Sstevel@tonic-gate 			msg->acpted_rply.ar_results.proc = xdr_void;
4407c478bd9Sstevel@tonic-gate 			msg->acpted_rply.ar_results.where = NULL;
4417c478bd9Sstevel@tonic-gate 		}
4427c478bd9Sstevel@tonic-gate 	}
4437c478bd9Sstevel@tonic-gate 
4447c478bd9Sstevel@tonic-gate 	mp = cd->cd_mp;
4457c478bd9Sstevel@tonic-gate 	if (mp) {
4467c478bd9Sstevel@tonic-gate 		/*
4477c478bd9Sstevel@tonic-gate 		 * The program above pre-allocated an mblk and put
4487c478bd9Sstevel@tonic-gate 		 * the data in place.
4497c478bd9Sstevel@tonic-gate 		 */
4507c478bd9Sstevel@tonic-gate 		cd->cd_mp = (mblk_t *)NULL;
4517c478bd9Sstevel@tonic-gate 		if (!(xdr_replymsg_body(xdrs, msg) &&
4527c478bd9Sstevel@tonic-gate 		    (!has_args || SVCAUTH_WRAP(&clone_xprt->xp_auth, xdrs,
4537c478bd9Sstevel@tonic-gate 		    xdr_results, xdr_location)))) {
454cf98b944SMarcel Telka 			XDR_DESTROY(xdrs);
4557c478bd9Sstevel@tonic-gate 			RPCLOG0(1, "svc_cots_ksend: "
4567c478bd9Sstevel@tonic-gate 			    "xdr_replymsg_body/SVCAUTH_WRAP failed\n");
4577c478bd9Sstevel@tonic-gate 			freemsg(mp);
4587c478bd9Sstevel@tonic-gate 			goto out;
4597c478bd9Sstevel@tonic-gate 		}
4607c478bd9Sstevel@tonic-gate 	} else {
4617c478bd9Sstevel@tonic-gate 		int	len;
4627c478bd9Sstevel@tonic-gate 		int	mpsize;
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate 		/*
4657c478bd9Sstevel@tonic-gate 		 * Leave space for protocol headers.
4667c478bd9Sstevel@tonic-gate 		 */
4677c478bd9Sstevel@tonic-gate 		len = MSG_OFFSET + clone_xprt->xp_msg_size;
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate 		/*
4707c478bd9Sstevel@tonic-gate 		 * Allocate an initial mblk for the response data.
4717c478bd9Sstevel@tonic-gate 		 */
4727c478bd9Sstevel@tonic-gate 		while (!(mp = allocb(len, BPRI_LO))) {
4737c478bd9Sstevel@tonic-gate 			RPCLOG0(16, "svc_cots_ksend: allocb failed failed\n");
4747c478bd9Sstevel@tonic-gate 			if (strwaitbuf(len, BPRI_LO)) {
4757c478bd9Sstevel@tonic-gate 				TRACE_1(TR_FAC_KRPC, TR_SVC_COTS_KSEND_END,
4767c478bd9Sstevel@tonic-gate 				    "svc_cots_ksend_end:(%S)", "strwaitbuf");
4777c478bd9Sstevel@tonic-gate 				RPCLOG0(1,
4787c478bd9Sstevel@tonic-gate 				    "svc_cots_ksend: strwaitbuf failed\n");
4797c478bd9Sstevel@tonic-gate 				goto out;
4807c478bd9Sstevel@tonic-gate 			}
4817c478bd9Sstevel@tonic-gate 		}
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate 		/*
484cf98b944SMarcel Telka 		 * Initialize the XDR encode stream.  Additional mblks
4857c478bd9Sstevel@tonic-gate 		 * will be allocated if necessary.  They will be TIDU
4867c478bd9Sstevel@tonic-gate 		 * sized.
4877c478bd9Sstevel@tonic-gate 		 */
4887c478bd9Sstevel@tonic-gate 		xdrmblk_init(xdrs, mp, XDR_ENCODE, clone_xprt->xp_msg_size);
4897c478bd9Sstevel@tonic-gate 		mpsize = MBLKSIZE(mp);
4907c478bd9Sstevel@tonic-gate 		ASSERT(mpsize >= len);
4917c478bd9Sstevel@tonic-gate 		ASSERT(mp->b_rptr == mp->b_datap->db_base);
4927c478bd9Sstevel@tonic-gate 
4937c478bd9Sstevel@tonic-gate 		/*
4947c478bd9Sstevel@tonic-gate 		 * If the size of mblk is not appreciably larger than what we
4957c478bd9Sstevel@tonic-gate 		 * asked, then resize the mblk to exactly len bytes. Reason for
4967c478bd9Sstevel@tonic-gate 		 * this: suppose len is 1600 bytes, the tidu is 1460 bytes
4977c478bd9Sstevel@tonic-gate 		 * (from TCP over ethernet), and the arguments to RPC require
4987c478bd9Sstevel@tonic-gate 		 * 2800 bytes. Ideally we want the protocol to render two
4997c478bd9Sstevel@tonic-gate 		 * ~1400 byte segments over the wire. If allocb() gives us a 2k
5007c478bd9Sstevel@tonic-gate 		 * mblk, and we allocate a second mblk for the rest, the
5017c478bd9Sstevel@tonic-gate 		 * protocol module may generate 3 segments over the wire:
5027c478bd9Sstevel@tonic-gate 		 * 1460 bytes for the first, 448 (2048 - 1600) for the 2nd, and
5037c478bd9Sstevel@tonic-gate 		 * 892 for the 3rd. If we "waste" 448 bytes in the first mblk,
5047c478bd9Sstevel@tonic-gate 		 * the XDR encoding will generate two ~1400 byte mblks, and the
5057c478bd9Sstevel@tonic-gate 		 * protocol module is more likely to produce properly sized
5067c478bd9Sstevel@tonic-gate 		 * segments.
5077c478bd9Sstevel@tonic-gate 		 */
5087c478bd9Sstevel@tonic-gate 		if ((mpsize >> 1) <= len) {
5097c478bd9Sstevel@tonic-gate 			mp->b_rptr += (mpsize - len);
5107c478bd9Sstevel@tonic-gate 		}
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@tonic-gate 		/*
5137c478bd9Sstevel@tonic-gate 		 * Adjust b_rptr to reserve space for the non-data protocol
5147c478bd9Sstevel@tonic-gate 		 * headers that any downstream modules might like to add, and
5157c478bd9Sstevel@tonic-gate 		 * for the record marking header.
5167c478bd9Sstevel@tonic-gate 		 */
5177c478bd9Sstevel@tonic-gate 		mp->b_rptr += (MSG_OFFSET + RM_HDR_SIZE);
5187c478bd9Sstevel@tonic-gate 
5197c478bd9Sstevel@tonic-gate 		XDR_SETPOS(xdrs, (uint_t)(mp->b_rptr - mp->b_datap->db_base));
5207c478bd9Sstevel@tonic-gate 		ASSERT(mp->b_wptr == mp->b_rptr);
5217c478bd9Sstevel@tonic-gate 
5227c478bd9Sstevel@tonic-gate 		msg->rm_xid = clone_xprt->xp_xid;
5237c478bd9Sstevel@tonic-gate 
5247c478bd9Sstevel@tonic-gate 		TRACE_0(TR_FAC_KRPC, TR_XDR_REPLYMSG_START,
5257c478bd9Sstevel@tonic-gate 		    "xdr_replymsg_start:");
5267c478bd9Sstevel@tonic-gate 		if (!(xdr_replymsg(xdrs, msg) &&
5277c478bd9Sstevel@tonic-gate 		    (!has_args || SVCAUTH_WRAP(&clone_xprt->xp_auth, xdrs,
5287c478bd9Sstevel@tonic-gate 		    xdr_results, xdr_location)))) {
529cf98b944SMarcel Telka 			XDR_DESTROY(xdrs);
5307c478bd9Sstevel@tonic-gate 			TRACE_1(TR_FAC_KRPC, TR_XDR_REPLYMSG_END,
5317c478bd9Sstevel@tonic-gate 			    "xdr_replymsg_end:(%S)", "bad");
5327c478bd9Sstevel@tonic-gate 			freemsg(mp);
5337c478bd9Sstevel@tonic-gate 			RPCLOG0(1, "svc_cots_ksend: xdr_replymsg/SVCAUTH_WRAP "
5347c478bd9Sstevel@tonic-gate 			    "failed\n");
5357c478bd9Sstevel@tonic-gate 			goto out;
5367c478bd9Sstevel@tonic-gate 		}
5377c478bd9Sstevel@tonic-gate 		TRACE_1(TR_FAC_KRPC, TR_XDR_REPLYMSG_END,
5387c478bd9Sstevel@tonic-gate 		    "xdr_replymsg_end:(%S)", "good");
5397c478bd9Sstevel@tonic-gate 	}
5407c478bd9Sstevel@tonic-gate 
541cf98b944SMarcel Telka 	XDR_DESTROY(xdrs);
542cf98b944SMarcel Telka 
5437c478bd9Sstevel@tonic-gate 	put(clone_xprt->xp_wq, mp);
5447c478bd9Sstevel@tonic-gate 	retval = TRUE;
5457c478bd9Sstevel@tonic-gate 
5467c478bd9Sstevel@tonic-gate out:
5477c478bd9Sstevel@tonic-gate 	/*
5487c478bd9Sstevel@tonic-gate 	 * This is completely disgusting.  If public is set it is
5497c478bd9Sstevel@tonic-gate 	 * a pointer to a structure whose first field is the address
5507c478bd9Sstevel@tonic-gate 	 * of the function to free that structure and any related
5517c478bd9Sstevel@tonic-gate 	 * stuff.  (see rrokfree in nfs_xdr.c).
5527c478bd9Sstevel@tonic-gate 	 */
5537c478bd9Sstevel@tonic-gate 	if (xdrs->x_public) {
5547c478bd9Sstevel@tonic-gate 		/* LINTED pointer alignment */
5557c478bd9Sstevel@tonic-gate 		(**((int (**)())xdrs->x_public))(xdrs->x_public);
5567c478bd9Sstevel@tonic-gate 	}
5577c478bd9Sstevel@tonic-gate 
5587c478bd9Sstevel@tonic-gate 	TRACE_1(TR_FAC_KRPC, TR_SVC_COTS_KSEND_END,
5597c478bd9Sstevel@tonic-gate 	    "svc_cots_ksend_end:(%S)", "done");
5607c478bd9Sstevel@tonic-gate 	return (retval);
5617c478bd9Sstevel@tonic-gate }
5627c478bd9Sstevel@tonic-gate 
5637c478bd9Sstevel@tonic-gate /*
5647c478bd9Sstevel@tonic-gate  * Deserialize arguments.
5657c478bd9Sstevel@tonic-gate  */
5667c478bd9Sstevel@tonic-gate static bool_t
svc_cots_kgetargs(SVCXPRT * clone_xprt,xdrproc_t xdr_args,caddr_t args_ptr)5677c478bd9Sstevel@tonic-gate svc_cots_kgetargs(SVCXPRT *clone_xprt, xdrproc_t xdr_args,
5687c478bd9Sstevel@tonic-gate     caddr_t args_ptr)
5697c478bd9Sstevel@tonic-gate {
5707c478bd9Sstevel@tonic-gate 	return (SVCAUTH_UNWRAP(&clone_xprt->xp_auth, &clone_xprt->xp_xdrin,
5717c478bd9Sstevel@tonic-gate 	    xdr_args, args_ptr));
5727c478bd9Sstevel@tonic-gate }
5737c478bd9Sstevel@tonic-gate 
5747c478bd9Sstevel@tonic-gate static bool_t
svc_cots_kfreeargs(SVCXPRT * clone_xprt,xdrproc_t xdr_args,caddr_t args_ptr)5757c478bd9Sstevel@tonic-gate svc_cots_kfreeargs(SVCXPRT *clone_xprt, xdrproc_t xdr_args,
5767c478bd9Sstevel@tonic-gate     caddr_t args_ptr)
5777c478bd9Sstevel@tonic-gate {
5787c478bd9Sstevel@tonic-gate 	cots_data_t *cd = (cots_data_t *)clone_xprt->xp_p2buf;
579cf98b944SMarcel Telka 	/* LINTED pointer alignment */
580cf98b944SMarcel Telka 	XDR *xdrs = &clone_xprt->xp_xdrin;
5817c478bd9Sstevel@tonic-gate 	mblk_t *mp;
5827c478bd9Sstevel@tonic-gate 	bool_t retval;
5837c478bd9Sstevel@tonic-gate 
5847c478bd9Sstevel@tonic-gate 	/*
5857c478bd9Sstevel@tonic-gate 	 * It is important to call the XDR routine before
5867c478bd9Sstevel@tonic-gate 	 * freeing the request mblk.  Structures in the
5877c478bd9Sstevel@tonic-gate 	 * XDR data may point into the mblk and require that
5887c478bd9Sstevel@tonic-gate 	 * the memory be intact during the free routine.
5897c478bd9Sstevel@tonic-gate 	 */
5907c478bd9Sstevel@tonic-gate 	if (args_ptr) {
5917c478bd9Sstevel@tonic-gate 		xdrs->x_op = XDR_FREE;
5927c478bd9Sstevel@tonic-gate 		retval = (*xdr_args)(xdrs, args_ptr);
5937c478bd9Sstevel@tonic-gate 	} else
5947c478bd9Sstevel@tonic-gate 		retval = TRUE;
5957c478bd9Sstevel@tonic-gate 
596cf98b944SMarcel Telka 	XDR_DESTROY(xdrs);
597cf98b944SMarcel Telka 
5987c478bd9Sstevel@tonic-gate 	if ((mp = cd->cd_req_mp) != NULL) {
5997c478bd9Sstevel@tonic-gate 		cd->cd_req_mp = (mblk_t *)0;
6007c478bd9Sstevel@tonic-gate 		freemsg(mp);
6017c478bd9Sstevel@tonic-gate 	}
6027c478bd9Sstevel@tonic-gate 
6037c478bd9Sstevel@tonic-gate 	return (retval);
6047c478bd9Sstevel@tonic-gate }
6057c478bd9Sstevel@tonic-gate 
6067c478bd9Sstevel@tonic-gate static int32_t *
svc_cots_kgetres(SVCXPRT * clone_xprt,int size)6077c478bd9Sstevel@tonic-gate svc_cots_kgetres(SVCXPRT *clone_xprt, int size)
6087c478bd9Sstevel@tonic-gate {
6097c478bd9Sstevel@tonic-gate 	/* LINTED pointer alignment */
6107c478bd9Sstevel@tonic-gate 	cots_data_t *cd = (cots_data_t *)clone_xprt->xp_p2buf;
6117c478bd9Sstevel@tonic-gate 	XDR *xdrs = &clone_xprt->xp_xdrout;
6127c478bd9Sstevel@tonic-gate 	mblk_t *mp;
6137c478bd9Sstevel@tonic-gate 	int32_t *buf;
6147c478bd9Sstevel@tonic-gate 	struct rpc_msg rply;
6157c478bd9Sstevel@tonic-gate 	int len;
6167c478bd9Sstevel@tonic-gate 	int mpsize;
6177c478bd9Sstevel@tonic-gate 
6187c478bd9Sstevel@tonic-gate 	/*
6197c478bd9Sstevel@tonic-gate 	 * Leave space for protocol headers.
6207c478bd9Sstevel@tonic-gate 	 */
6217c478bd9Sstevel@tonic-gate 	len = MSG_OFFSET + clone_xprt->xp_msg_size;
6227c478bd9Sstevel@tonic-gate 
6237c478bd9Sstevel@tonic-gate 	/*
6247c478bd9Sstevel@tonic-gate 	 * Allocate an initial mblk for the response data.
6257c478bd9Sstevel@tonic-gate 	 */
6267c478bd9Sstevel@tonic-gate 	while ((mp = allocb(len, BPRI_LO)) == NULL) {
6277c478bd9Sstevel@tonic-gate 		if (strwaitbuf(len, BPRI_LO))
628cf98b944SMarcel Telka 			return (NULL);
6297c478bd9Sstevel@tonic-gate 	}
6307c478bd9Sstevel@tonic-gate 
6317c478bd9Sstevel@tonic-gate 	/*
632cf98b944SMarcel Telka 	 * Initialize the XDR encode stream.  Additional mblks
6337c478bd9Sstevel@tonic-gate 	 * will be allocated if necessary.  They will be TIDU
6347c478bd9Sstevel@tonic-gate 	 * sized.
6357c478bd9Sstevel@tonic-gate 	 */
6367c478bd9Sstevel@tonic-gate 	xdrmblk_init(xdrs, mp, XDR_ENCODE, clone_xprt->xp_msg_size);
6377c478bd9Sstevel@tonic-gate 	mpsize = MBLKSIZE(mp);
6387c478bd9Sstevel@tonic-gate 	ASSERT(mpsize >= len);
6397c478bd9Sstevel@tonic-gate 	ASSERT(mp->b_rptr == mp->b_datap->db_base);
6407c478bd9Sstevel@tonic-gate 
6417c478bd9Sstevel@tonic-gate 	/*
6427c478bd9Sstevel@tonic-gate 	 * If the size of mblk is not appreciably larger than what we
6437c478bd9Sstevel@tonic-gate 	 * asked, then resize the mblk to exactly len bytes. Reason for
6447c478bd9Sstevel@tonic-gate 	 * this: suppose len is 1600 bytes, the tidu is 1460 bytes
6457c478bd9Sstevel@tonic-gate 	 * (from TCP over ethernet), and the arguments to RPC require
6467c478bd9Sstevel@tonic-gate 	 * 2800 bytes. Ideally we want the protocol to render two
6477c478bd9Sstevel@tonic-gate 	 * ~1400 byte segments over the wire. If allocb() gives us a 2k
6487c478bd9Sstevel@tonic-gate 	 * mblk, and we allocate a second mblk for the rest, the
6497c478bd9Sstevel@tonic-gate 	 * protocol module may generate 3 segments over the wire:
6507c478bd9Sstevel@tonic-gate 	 * 1460 bytes for the first, 448 (2048 - 1600) for the 2nd, and
6517c478bd9Sstevel@tonic-gate 	 * 892 for the 3rd. If we "waste" 448 bytes in the first mblk,
6527c478bd9Sstevel@tonic-gate 	 * the XDR encoding will generate two ~1400 byte mblks, and the
6537c478bd9Sstevel@tonic-gate 	 * protocol module is more likely to produce properly sized
6547c478bd9Sstevel@tonic-gate 	 * segments.
6557c478bd9Sstevel@tonic-gate 	 */
6567c478bd9Sstevel@tonic-gate 	if ((mpsize >> 1) <= len) {
6577c478bd9Sstevel@tonic-gate 		mp->b_rptr += (mpsize - len);
6587c478bd9Sstevel@tonic-gate 	}
6597c478bd9Sstevel@tonic-gate 
6607c478bd9Sstevel@tonic-gate 	/*
6617c478bd9Sstevel@tonic-gate 	 * Adjust b_rptr to reserve space for the non-data protocol
6627c478bd9Sstevel@tonic-gate 	 * headers that any downstream modules might like to add, and
6637c478bd9Sstevel@tonic-gate 	 * for the record marking header.
6647c478bd9Sstevel@tonic-gate 	 */
6657c478bd9Sstevel@tonic-gate 	mp->b_rptr += (MSG_OFFSET + RM_HDR_SIZE);
6667c478bd9Sstevel@tonic-gate 
6677c478bd9Sstevel@tonic-gate 	XDR_SETPOS(xdrs, (uint_t)(mp->b_rptr - mp->b_datap->db_base));
6687c478bd9Sstevel@tonic-gate 	ASSERT(mp->b_wptr == mp->b_rptr);
6697c478bd9Sstevel@tonic-gate 
6707c478bd9Sstevel@tonic-gate 	/*
6717c478bd9Sstevel@tonic-gate 	 * Assume a successful RPC since most of them are.
6727c478bd9Sstevel@tonic-gate 	 */
6737c478bd9Sstevel@tonic-gate 	rply.rm_xid = clone_xprt->xp_xid;
6747c478bd9Sstevel@tonic-gate 	rply.rm_direction = REPLY;
6757c478bd9Sstevel@tonic-gate 	rply.rm_reply.rp_stat = MSG_ACCEPTED;
6767c478bd9Sstevel@tonic-gate 	rply.acpted_rply.ar_verf = clone_xprt->xp_verf;
6777c478bd9Sstevel@tonic-gate 	rply.acpted_rply.ar_stat = SUCCESS;
6787c478bd9Sstevel@tonic-gate 
6797c478bd9Sstevel@tonic-gate 	if (!xdr_replymsg_hdr(xdrs, &rply)) {
680cf98b944SMarcel Telka 		XDR_DESTROY(xdrs);
6817c478bd9Sstevel@tonic-gate 		freeb(mp);
6827c478bd9Sstevel@tonic-gate 		return (NULL);
6837c478bd9Sstevel@tonic-gate 	}
6847c478bd9Sstevel@tonic-gate 
6857c478bd9Sstevel@tonic-gate 	buf = XDR_INLINE(xdrs, size);
6867c478bd9Sstevel@tonic-gate 	if (buf == NULL) {
687cf98b944SMarcel Telka 		XDR_DESTROY(xdrs);
6887c478bd9Sstevel@tonic-gate 		ASSERT(cd->cd_mp == NULL);
6897c478bd9Sstevel@tonic-gate 		freemsg(mp);
6907c478bd9Sstevel@tonic-gate 	} else {
6917c478bd9Sstevel@tonic-gate 		cd->cd_mp = mp;
6927c478bd9Sstevel@tonic-gate 	}
6937c478bd9Sstevel@tonic-gate 	return (buf);
6947c478bd9Sstevel@tonic-gate }
6957c478bd9Sstevel@tonic-gate 
6967c478bd9Sstevel@tonic-gate static void
svc_cots_kfreeres(SVCXPRT * clone_xprt)6977c478bd9Sstevel@tonic-gate svc_cots_kfreeres(SVCXPRT *clone_xprt)
6987c478bd9Sstevel@tonic-gate {
6997c478bd9Sstevel@tonic-gate 	cots_data_t *cd;
7007c478bd9Sstevel@tonic-gate 	mblk_t *mp;
7017c478bd9Sstevel@tonic-gate 
7027c478bd9Sstevel@tonic-gate 	cd = (cots_data_t *)clone_xprt->xp_p2buf;
7037c478bd9Sstevel@tonic-gate 	if ((mp = cd->cd_mp) != NULL) {
704cf98b944SMarcel Telka 		XDR_DESTROY(&clone_xprt->xp_xdrout);
7057c478bd9Sstevel@tonic-gate 		cd->cd_mp = (mblk_t *)NULL;
7067c478bd9Sstevel@tonic-gate 		freemsg(mp);
7077c478bd9Sstevel@tonic-gate 	}
7087c478bd9Sstevel@tonic-gate }
7097c478bd9Sstevel@tonic-gate 
7107c478bd9Sstevel@tonic-gate /*
7117c478bd9Sstevel@tonic-gate  * the dup cacheing routines below provide a cache of non-failure
7127c478bd9Sstevel@tonic-gate  * transaction id's.  rpc service routines can use this to detect
7137c478bd9Sstevel@tonic-gate  * retransmissions and re-send a non-failure response.
7147c478bd9Sstevel@tonic-gate  */
7157c478bd9Sstevel@tonic-gate 
7167c478bd9Sstevel@tonic-gate /*
7177c478bd9Sstevel@tonic-gate  * MAXDUPREQS is the number of cached items.  It should be adjusted
7187c478bd9Sstevel@tonic-gate  * to the service load so that there is likely to be a response entry
7197c478bd9Sstevel@tonic-gate  * when the first retransmission comes in.
7207c478bd9Sstevel@tonic-gate  */
721201f5ebaSSebastien Roy #define	MAXDUPREQS	8192
7227c478bd9Sstevel@tonic-gate 
7237c478bd9Sstevel@tonic-gate /*
724b7f07133SMarcel Telka  * This should be appropriately scaled to MAXDUPREQS.  To produce as less as
725b7f07133SMarcel Telka  * possible collisions it is suggested to set this to a prime.
7267c478bd9Sstevel@tonic-gate  */
727201f5ebaSSebastien Roy #define	DRHASHSZ	2053
7287c478bd9Sstevel@tonic-gate 
7297c478bd9Sstevel@tonic-gate #define	XIDHASH(xid)	((xid) % DRHASHSZ)
7307c478bd9Sstevel@tonic-gate #define	DRHASH(dr)	XIDHASH((dr)->dr_xid)
7317c478bd9Sstevel@tonic-gate #define	REQTOXID(req)	((req)->rq_xprt->xp_xid)
7327c478bd9Sstevel@tonic-gate 
7337c478bd9Sstevel@tonic-gate static int	cotsndupreqs = 0;
73459418bdeSgt29601@anthrax int	cotsmaxdupreqs = MAXDUPREQS;
7357c478bd9Sstevel@tonic-gate static kmutex_t cotsdupreq_lock;
7367c478bd9Sstevel@tonic-gate static struct dupreq *cotsdrhashtbl[DRHASHSZ];
7377c478bd9Sstevel@tonic-gate static int	cotsdrhashstat[DRHASHSZ];
7387c478bd9Sstevel@tonic-gate 
7397c478bd9Sstevel@tonic-gate static void unhash(struct dupreq *);
7407c478bd9Sstevel@tonic-gate 
7417c478bd9Sstevel@tonic-gate /*
7427c478bd9Sstevel@tonic-gate  * cotsdrmru points to the head of a circular linked list in lru order.
7437c478bd9Sstevel@tonic-gate  * cotsdrmru->dr_next == drlru
7447c478bd9Sstevel@tonic-gate  */
7457c478bd9Sstevel@tonic-gate struct dupreq *cotsdrmru;
7467c478bd9Sstevel@tonic-gate 
7477c478bd9Sstevel@tonic-gate /*
7487c478bd9Sstevel@tonic-gate  * PSARC 2003/523 Contract Private Interface
7497c478bd9Sstevel@tonic-gate  * svc_cots_kdup
7507c478bd9Sstevel@tonic-gate  * Changes must be reviewed by Solaris File Sharing
7517c478bd9Sstevel@tonic-gate  * Changes must be communicated to contract-2003-523@sun.com
7527c478bd9Sstevel@tonic-gate  *
7537c478bd9Sstevel@tonic-gate  * svc_cots_kdup searches the request cache and returns 0 if the
7547c478bd9Sstevel@tonic-gate  * request is not found in the cache.  If it is found, then it
7557c478bd9Sstevel@tonic-gate  * returns the state of the request (in progress or done) and
7567c478bd9Sstevel@tonic-gate  * the status or attributes that were part of the original reply.
7577c478bd9Sstevel@tonic-gate  *
7587c478bd9Sstevel@tonic-gate  * If DUP_DONE (there is a duplicate) svc_cots_kdup copies over the
7597c478bd9Sstevel@tonic-gate  * value of the response. In that case, also return in *dupcachedp
7607c478bd9Sstevel@tonic-gate  * whether the response free routine is cached in the dupreq - in which case
7617c478bd9Sstevel@tonic-gate  * the caller should not be freeing it, because it will be done later
7627c478bd9Sstevel@tonic-gate  * in the svc_cots_kdup code when the dupreq is reused.
7637c478bd9Sstevel@tonic-gate  */
7647c478bd9Sstevel@tonic-gate static int
svc_cots_kdup(struct svc_req * req,caddr_t res,int size,struct dupreq ** drpp,bool_t * dupcachedp)7657c478bd9Sstevel@tonic-gate svc_cots_kdup(struct svc_req *req, caddr_t res, int size, struct dupreq **drpp,
7667c478bd9Sstevel@tonic-gate     bool_t *dupcachedp)
7677c478bd9Sstevel@tonic-gate {
7687c478bd9Sstevel@tonic-gate 	struct rpc_cots_server *stats = CLONE2STATS(req->rq_xprt);
7697c478bd9Sstevel@tonic-gate 	struct dupreq *dr;
7707c478bd9Sstevel@tonic-gate 	uint32_t xid;
7717c478bd9Sstevel@tonic-gate 	uint32_t drhash;
7727c478bd9Sstevel@tonic-gate 	int status;
7737c478bd9Sstevel@tonic-gate 
7747c478bd9Sstevel@tonic-gate 	xid = REQTOXID(req);
7757c478bd9Sstevel@tonic-gate 	mutex_enter(&cotsdupreq_lock);
7767c478bd9Sstevel@tonic-gate 	RSSTAT_INCR(stats, rsdupchecks);
7777c478bd9Sstevel@tonic-gate 	/*
7787c478bd9Sstevel@tonic-gate 	 * Check to see whether an entry already exists in the cache.
7797c478bd9Sstevel@tonic-gate 	 */
7807c478bd9Sstevel@tonic-gate 	dr = cotsdrhashtbl[XIDHASH(xid)];
7817c478bd9Sstevel@tonic-gate 	while (dr != NULL) {
7827c478bd9Sstevel@tonic-gate 		if (dr->dr_xid == xid &&
7837c478bd9Sstevel@tonic-gate 		    dr->dr_proc == req->rq_proc &&
7847c478bd9Sstevel@tonic-gate 		    dr->dr_prog == req->rq_prog &&
7857c478bd9Sstevel@tonic-gate 		    dr->dr_vers == req->rq_vers &&
7867c478bd9Sstevel@tonic-gate 		    dr->dr_addr.len == req->rq_xprt->xp_rtaddr.len &&
7877c478bd9Sstevel@tonic-gate 		    bcmp((caddr_t)dr->dr_addr.buf,
7887c478bd9Sstevel@tonic-gate 		    (caddr_t)req->rq_xprt->xp_rtaddr.buf,
7897c478bd9Sstevel@tonic-gate 		    dr->dr_addr.len) == 0) {
7907c478bd9Sstevel@tonic-gate 			status = dr->dr_status;
7917c478bd9Sstevel@tonic-gate 			if (status == DUP_DONE) {
7927c478bd9Sstevel@tonic-gate 				bcopy(dr->dr_resp.buf, res, size);
7937c478bd9Sstevel@tonic-gate 				if (dupcachedp != NULL)
7947c478bd9Sstevel@tonic-gate 					*dupcachedp = (dr->dr_resfree != NULL);
7957c478bd9Sstevel@tonic-gate 				TRACE_0(TR_FAC_KRPC, TR_SVC_COTS_KDUP_DONE,
7967c478bd9Sstevel@tonic-gate 				    "svc_cots_kdup: DUP_DONE");
7977c478bd9Sstevel@tonic-gate 			} else {
7987c478bd9Sstevel@tonic-gate 				dr->dr_status = DUP_INPROGRESS;
7997c478bd9Sstevel@tonic-gate 				*drpp = dr;
8007c478bd9Sstevel@tonic-gate 				TRACE_0(TR_FAC_KRPC,
8017c478bd9Sstevel@tonic-gate 				    TR_SVC_COTS_KDUP_INPROGRESS,
8027c478bd9Sstevel@tonic-gate 				    "svc_cots_kdup: DUP_INPROGRESS");
8037c478bd9Sstevel@tonic-gate 			}
8047c478bd9Sstevel@tonic-gate 			RSSTAT_INCR(stats, rsdupreqs);
8057c478bd9Sstevel@tonic-gate 			mutex_exit(&cotsdupreq_lock);
8067c478bd9Sstevel@tonic-gate 			return (status);
8077c478bd9Sstevel@tonic-gate 		}
8087c478bd9Sstevel@tonic-gate 		dr = dr->dr_chain;
8097c478bd9Sstevel@tonic-gate 	}
8107c478bd9Sstevel@tonic-gate 
8117c478bd9Sstevel@tonic-gate 	/*
8127c478bd9Sstevel@tonic-gate 	 * There wasn't an entry, either allocate a new one or recycle
8137c478bd9Sstevel@tonic-gate 	 * an old one.
8147c478bd9Sstevel@tonic-gate 	 */
8157c478bd9Sstevel@tonic-gate 	if (cotsndupreqs < cotsmaxdupreqs) {
8167c478bd9Sstevel@tonic-gate 		dr = kmem_alloc(sizeof (*dr), KM_NOSLEEP);
8177c478bd9Sstevel@tonic-gate 		if (dr == NULL) {
8187c478bd9Sstevel@tonic-gate 			mutex_exit(&cotsdupreq_lock);
8197c478bd9Sstevel@tonic-gate 			return (DUP_ERROR);
8207c478bd9Sstevel@tonic-gate 		}
8217c478bd9Sstevel@tonic-gate 		dr->dr_resp.buf = NULL;
8227c478bd9Sstevel@tonic-gate 		dr->dr_resp.maxlen = 0;
8237c478bd9Sstevel@tonic-gate 		dr->dr_addr.buf = NULL;
8247c478bd9Sstevel@tonic-gate 		dr->dr_addr.maxlen = 0;
8257c478bd9Sstevel@tonic-gate 		if (cotsdrmru) {
8267c478bd9Sstevel@tonic-gate 			dr->dr_next = cotsdrmru->dr_next;
8277c478bd9Sstevel@tonic-gate 			cotsdrmru->dr_next = dr;
8287c478bd9Sstevel@tonic-gate 		} else {
8297c478bd9Sstevel@tonic-gate 			dr->dr_next = dr;
8307c478bd9Sstevel@tonic-gate 		}
8317c478bd9Sstevel@tonic-gate 		cotsndupreqs++;
8327c478bd9Sstevel@tonic-gate 	} else {
8337c478bd9Sstevel@tonic-gate 		dr = cotsdrmru->dr_next;
8347c478bd9Sstevel@tonic-gate 		while (dr->dr_status == DUP_INPROGRESS) {
8357c478bd9Sstevel@tonic-gate 			dr = dr->dr_next;
8367c478bd9Sstevel@tonic-gate 			if (dr == cotsdrmru->dr_next) {
8377c478bd9Sstevel@tonic-gate 				cmn_err(CE_WARN, "svc_cots_kdup no slots free");
8387c478bd9Sstevel@tonic-gate 				mutex_exit(&cotsdupreq_lock);
8397c478bd9Sstevel@tonic-gate 				return (DUP_ERROR);
8407c478bd9Sstevel@tonic-gate 			}
8417c478bd9Sstevel@tonic-gate 		}
8427c478bd9Sstevel@tonic-gate 		unhash(dr);
8437c478bd9Sstevel@tonic-gate 		if (dr->dr_resfree) {
8447c478bd9Sstevel@tonic-gate 			(*dr->dr_resfree)(dr->dr_resp.buf);
8457c478bd9Sstevel@tonic-gate 		}
8467c478bd9Sstevel@tonic-gate 	}
8477c478bd9Sstevel@tonic-gate 	dr->dr_resfree = NULL;
8487c478bd9Sstevel@tonic-gate 	cotsdrmru = dr;
8497c478bd9Sstevel@tonic-gate 
8507c478bd9Sstevel@tonic-gate 	dr->dr_xid = REQTOXID(req);
8517c478bd9Sstevel@tonic-gate 	dr->dr_prog = req->rq_prog;
8527c478bd9Sstevel@tonic-gate 	dr->dr_vers = req->rq_vers;
8537c478bd9Sstevel@tonic-gate 	dr->dr_proc = req->rq_proc;
8547c478bd9Sstevel@tonic-gate 	if (dr->dr_addr.maxlen < req->rq_xprt->xp_rtaddr.len) {
8557c478bd9Sstevel@tonic-gate 		if (dr->dr_addr.buf != NULL)
8567c478bd9Sstevel@tonic-gate 			kmem_free(dr->dr_addr.buf, dr->dr_addr.maxlen);
8577c478bd9Sstevel@tonic-gate 		dr->dr_addr.maxlen = req->rq_xprt->xp_rtaddr.len;
8587c478bd9Sstevel@tonic-gate 		dr->dr_addr.buf = kmem_alloc(dr->dr_addr.maxlen, KM_NOSLEEP);
8597c478bd9Sstevel@tonic-gate 		if (dr->dr_addr.buf == NULL) {
8607c478bd9Sstevel@tonic-gate 			dr->dr_addr.maxlen = 0;
8617c478bd9Sstevel@tonic-gate 			dr->dr_status = DUP_DROP;
8627c478bd9Sstevel@tonic-gate 			mutex_exit(&cotsdupreq_lock);
8637c478bd9Sstevel@tonic-gate 			return (DUP_ERROR);
8647c478bd9Sstevel@tonic-gate 		}
8657c478bd9Sstevel@tonic-gate 	}
8667c478bd9Sstevel@tonic-gate 	dr->dr_addr.len = req->rq_xprt->xp_rtaddr.len;
8677c478bd9Sstevel@tonic-gate 	bcopy(req->rq_xprt->xp_rtaddr.buf, dr->dr_addr.buf, dr->dr_addr.len);
8687c478bd9Sstevel@tonic-gate 	if (dr->dr_resp.maxlen < size) {
8697c478bd9Sstevel@tonic-gate 		if (dr->dr_resp.buf != NULL)
8707c478bd9Sstevel@tonic-gate 			kmem_free(dr->dr_resp.buf, dr->dr_resp.maxlen);
8717c478bd9Sstevel@tonic-gate 		dr->dr_resp.maxlen = (unsigned int)size;
8727c478bd9Sstevel@tonic-gate 		dr->dr_resp.buf = kmem_alloc(size, KM_NOSLEEP);
8737c478bd9Sstevel@tonic-gate 		if (dr->dr_resp.buf == NULL) {
8747c478bd9Sstevel@tonic-gate 			dr->dr_resp.maxlen = 0;
8757c478bd9Sstevel@tonic-gate 			dr->dr_status = DUP_DROP;
8767c478bd9Sstevel@tonic-gate 			mutex_exit(&cotsdupreq_lock);
8777c478bd9Sstevel@tonic-gate 			return (DUP_ERROR);
8787c478bd9Sstevel@tonic-gate 		}
8797c478bd9Sstevel@tonic-gate 	}
8807c478bd9Sstevel@tonic-gate 	dr->dr_status = DUP_INPROGRESS;
8817c478bd9Sstevel@tonic-gate 
8827c478bd9Sstevel@tonic-gate 	drhash = (uint32_t)DRHASH(dr);
8837c478bd9Sstevel@tonic-gate 	dr->dr_chain = cotsdrhashtbl[drhash];
8847c478bd9Sstevel@tonic-gate 	cotsdrhashtbl[drhash] = dr;
8857c478bd9Sstevel@tonic-gate 	cotsdrhashstat[drhash]++;
8867c478bd9Sstevel@tonic-gate 	mutex_exit(&cotsdupreq_lock);
8877c478bd9Sstevel@tonic-gate 	*drpp = dr;
8887c478bd9Sstevel@tonic-gate 	return (DUP_NEW);
8897c478bd9Sstevel@tonic-gate }
8907c478bd9Sstevel@tonic-gate 
8917c478bd9Sstevel@tonic-gate /*
8927c478bd9Sstevel@tonic-gate  * PSARC 2003/523 Contract Private Interface
8937c478bd9Sstevel@tonic-gate  * svc_cots_kdupdone
8947c478bd9Sstevel@tonic-gate  * Changes must be reviewed by Solaris File Sharing
8957c478bd9Sstevel@tonic-gate  * Changes must be communicated to contract-2003-523@sun.com
8967c478bd9Sstevel@tonic-gate  *
8977c478bd9Sstevel@tonic-gate  * svc_cots_kdupdone marks the request done (DUP_DONE or DUP_DROP)
8987c478bd9Sstevel@tonic-gate  * and stores the response.
8997c478bd9Sstevel@tonic-gate  */
9007c478bd9Sstevel@tonic-gate static void
svc_cots_kdupdone(struct dupreq * dr,caddr_t res,void (* dis_resfree)(),int size,int status)9017c478bd9Sstevel@tonic-gate svc_cots_kdupdone(struct dupreq *dr, caddr_t res, void (*dis_resfree)(),
9027c478bd9Sstevel@tonic-gate     int size, int status)
9037c478bd9Sstevel@tonic-gate {
9047c478bd9Sstevel@tonic-gate 	ASSERT(dr->dr_resfree == NULL);
9057c478bd9Sstevel@tonic-gate 	if (status == DUP_DONE) {
9067c478bd9Sstevel@tonic-gate 		bcopy(res, dr->dr_resp.buf, size);
9077c478bd9Sstevel@tonic-gate 		dr->dr_resfree = dis_resfree;
9087c478bd9Sstevel@tonic-gate 	}
9097c478bd9Sstevel@tonic-gate 	dr->dr_status = status;
9107c478bd9Sstevel@tonic-gate }
9117c478bd9Sstevel@tonic-gate 
9127c478bd9Sstevel@tonic-gate /*
9137c478bd9Sstevel@tonic-gate  * This routine expects that the mutex, cotsdupreq_lock, is already held.
9147c478bd9Sstevel@tonic-gate  */
9157c478bd9Sstevel@tonic-gate static void
unhash(struct dupreq * dr)9167c478bd9Sstevel@tonic-gate unhash(struct dupreq *dr)
9177c478bd9Sstevel@tonic-gate {
9187c478bd9Sstevel@tonic-gate 	struct dupreq *drt;
9197c478bd9Sstevel@tonic-gate 	struct dupreq *drtprev = NULL;
9207c478bd9Sstevel@tonic-gate 	uint32_t drhash;
9217c478bd9Sstevel@tonic-gate 
9227c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cotsdupreq_lock));
9237c478bd9Sstevel@tonic-gate 
9247c478bd9Sstevel@tonic-gate 	drhash = (uint32_t)DRHASH(dr);
9257c478bd9Sstevel@tonic-gate 	drt = cotsdrhashtbl[drhash];
9267c478bd9Sstevel@tonic-gate 	while (drt != NULL) {
9277c478bd9Sstevel@tonic-gate 		if (drt == dr) {
9287c478bd9Sstevel@tonic-gate 			cotsdrhashstat[drhash]--;
9297c478bd9Sstevel@tonic-gate 			if (drtprev == NULL) {
9307c478bd9Sstevel@tonic-gate 				cotsdrhashtbl[drhash] = drt->dr_chain;
9317c478bd9Sstevel@tonic-gate 			} else {
9327c478bd9Sstevel@tonic-gate 				drtprev->dr_chain = drt->dr_chain;
9337c478bd9Sstevel@tonic-gate 			}
9347c478bd9Sstevel@tonic-gate 			return;
9357c478bd9Sstevel@tonic-gate 		}
9367c478bd9Sstevel@tonic-gate 		drtprev = drt;
9377c478bd9Sstevel@tonic-gate 		drt = drt->dr_chain;
9387c478bd9Sstevel@tonic-gate 	}
9397c478bd9Sstevel@tonic-gate }
9407c478bd9Sstevel@tonic-gate 
9417c478bd9Sstevel@tonic-gate void
svc_cots_stats_init(zoneid_t zoneid,struct rpc_cots_server ** statsp)9427c478bd9Sstevel@tonic-gate svc_cots_stats_init(zoneid_t zoneid, struct rpc_cots_server **statsp)
9437c478bd9Sstevel@tonic-gate {
9447c478bd9Sstevel@tonic-gate 	*statsp = (struct rpc_cots_server *)rpcstat_zone_init_common(zoneid,
9457c478bd9Sstevel@tonic-gate 	    "unix", "rpc_cots_server", (const kstat_named_t *)&cots_rsstat_tmpl,
9467c478bd9Sstevel@tonic-gate 	    sizeof (cots_rsstat_tmpl));
9477c478bd9Sstevel@tonic-gate }
9487c478bd9Sstevel@tonic-gate 
9497c478bd9Sstevel@tonic-gate void
svc_cots_stats_fini(zoneid_t zoneid,struct rpc_cots_server ** statsp)9507c478bd9Sstevel@tonic-gate svc_cots_stats_fini(zoneid_t zoneid, struct rpc_cots_server **statsp)
9517c478bd9Sstevel@tonic-gate {
9527c478bd9Sstevel@tonic-gate 	rpcstat_zone_fini_common(zoneid, "unix", "rpc_cots_server");
9537c478bd9Sstevel@tonic-gate 	kmem_free(*statsp, sizeof (cots_rsstat_tmpl));
9547c478bd9Sstevel@tonic-gate }
9557c478bd9Sstevel@tonic-gate 
9567c478bd9Sstevel@tonic-gate void
svc_cots_init(void)9577c478bd9Sstevel@tonic-gate svc_cots_init(void)
9587c478bd9Sstevel@tonic-gate {
9597c478bd9Sstevel@tonic-gate 	/*
9607c478bd9Sstevel@tonic-gate 	 * Check to make sure that the cots private data will fit into
9617c478bd9Sstevel@tonic-gate 	 * the stack buffer allocated by svc_run.  The ASSERT is a safety
9627c478bd9Sstevel@tonic-gate 	 * net if the cots_data_t structure ever changes.
9637c478bd9Sstevel@tonic-gate 	 */
9647c478bd9Sstevel@tonic-gate 	/*CONSTANTCONDITION*/
9657c478bd9Sstevel@tonic-gate 	ASSERT(sizeof (cots_data_t) <= SVC_P2LEN);
9667c478bd9Sstevel@tonic-gate 
9677c478bd9Sstevel@tonic-gate 	mutex_init(&cots_kcreate_lock, NULL, MUTEX_DEFAULT, NULL);
9687c478bd9Sstevel@tonic-gate 	mutex_init(&cotsdupreq_lock, NULL, MUTEX_DEFAULT, NULL);
9697c478bd9Sstevel@tonic-gate }
970