10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
23*766Scarlsonj  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved  	*/
290Sstevel@tonic-gate 
300Sstevel@tonic-gate 
310Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 */
320Sstevel@tonic-gate 
330Sstevel@tonic-gate /*
340Sstevel@tonic-gate  * Define and initialize MT client/server data.
350Sstevel@tonic-gate  */
360Sstevel@tonic-gate 
370Sstevel@tonic-gate #include	<sys/types.h>
380Sstevel@tonic-gate #include	<sys/t_lock.h>
390Sstevel@tonic-gate #include	<sys/kstat.h>
400Sstevel@tonic-gate #include	<sys/systm.h>
410Sstevel@tonic-gate #include	<sys/zone.h>
420Sstevel@tonic-gate 
430Sstevel@tonic-gate #include	<rpc/types.h>
440Sstevel@tonic-gate #include	<rpc/auth.h>
450Sstevel@tonic-gate #include	<rpc/clnt.h>
460Sstevel@tonic-gate 
470Sstevel@tonic-gate kmutex_t xid_lock;		/* XID allocation */
480Sstevel@tonic-gate kmutex_t clnt_pending_lock;	/* for list of pending calls awaiting replies */
490Sstevel@tonic-gate kmutex_t clnt_max_msg_lock;	/* updating max message sanity check for cots */
500Sstevel@tonic-gate 
510Sstevel@tonic-gate zone_key_t	rpcstat_zone_key;
520Sstevel@tonic-gate 
530Sstevel@tonic-gate /*
540Sstevel@tonic-gate  * rpcstat_zone_[init|fini]_common() ends up being nearly identical to
550Sstevel@tonic-gate  * nfsstat_zone_[init|fini]_common().  Due to them necessarily being in
560Sstevel@tonic-gate  * different modules, however, we end up needing to duplicate the code.
570Sstevel@tonic-gate  */
580Sstevel@tonic-gate kstat_named_t *
590Sstevel@tonic-gate rpcstat_zone_init_common(zoneid_t zoneid, const char *module, const char *name,
600Sstevel@tonic-gate     const kstat_named_t *template, size_t template_size)
610Sstevel@tonic-gate {
620Sstevel@tonic-gate 	kstat_t *ksp;
630Sstevel@tonic-gate 	kstat_named_t *ks_data;
640Sstevel@tonic-gate 
650Sstevel@tonic-gate 
660Sstevel@tonic-gate /*
670Sstevel@tonic-gate  * PSARC 2001/697 Contract Private Interface
680Sstevel@tonic-gate  * rpc_clts_client
690Sstevel@tonic-gate  * rpc_cots_client
700Sstevel@tonic-gate  * Changes must be reviewed by Solaris File Sharing
710Sstevel@tonic-gate  * Changes must be communicated to contract-2001-697@sun.com
720Sstevel@tonic-gate  *
730Sstevel@tonic-gate  */
740Sstevel@tonic-gate 	ks_data = kmem_alloc(template_size, KM_SLEEP);
750Sstevel@tonic-gate 	bcopy(template, ks_data, template_size);
760Sstevel@tonic-gate 	if ((ksp = kstat_create_zone((char *)module, 0, (char *)name, "rpc",
770Sstevel@tonic-gate 	    KSTAT_TYPE_NAMED, template_size / sizeof (kstat_named_t),
780Sstevel@tonic-gate 	    KSTAT_FLAG_VIRTUAL | KSTAT_FLAG_WRITABLE, zoneid)) != NULL) {
790Sstevel@tonic-gate 		ksp->ks_data = ks_data;
800Sstevel@tonic-gate 		kstat_install(ksp);
810Sstevel@tonic-gate 	}
820Sstevel@tonic-gate 	return (ks_data);
830Sstevel@tonic-gate }
840Sstevel@tonic-gate 
850Sstevel@tonic-gate void
860Sstevel@tonic-gate rpcstat_zone_fini_common(zoneid_t zoneid, const char *module, const char *name)
870Sstevel@tonic-gate {
880Sstevel@tonic-gate 	kstat_delete_byname_zone((char *)module, 0, (char *)name, zoneid);
890Sstevel@tonic-gate }
900Sstevel@tonic-gate 
910Sstevel@tonic-gate static void *
920Sstevel@tonic-gate mt_kstat_zone_init(zoneid_t zoneid)
930Sstevel@tonic-gate {
940Sstevel@tonic-gate 	struct rpcstat *rpcstat;
950Sstevel@tonic-gate 
960Sstevel@tonic-gate 	rpcstat = kmem_alloc(sizeof (*rpcstat), KM_SLEEP);
970Sstevel@tonic-gate 
980Sstevel@tonic-gate 	clnt_clts_stats_init(zoneid, &rpcstat->rpc_clts_client);
990Sstevel@tonic-gate 	svc_clts_stats_init(zoneid, &rpcstat->rpc_clts_server);
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate 	clnt_cots_stats_init(zoneid, &rpcstat->rpc_cots_client);
1020Sstevel@tonic-gate 	svc_cots_stats_init(zoneid, &rpcstat->rpc_cots_server);
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate 	return (rpcstat);
1050Sstevel@tonic-gate }
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate /*
1080Sstevel@tonic-gate  * Deletes the previously allocated "rpc" kstats
1090Sstevel@tonic-gate  */
1100Sstevel@tonic-gate static void
1110Sstevel@tonic-gate mt_kstat_zone_fini(zoneid_t zoneid, void *data)
1120Sstevel@tonic-gate {
1130Sstevel@tonic-gate 	struct rpcstat *rpcstat = data;
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate 	clnt_cots_stats_fini(zoneid, &rpcstat->rpc_cots_client);
1160Sstevel@tonic-gate 	svc_cots_stats_fini(zoneid, &rpcstat->rpc_cots_server);
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate 	clnt_clts_stats_fini(zoneid, &rpcstat->rpc_clts_client);
1190Sstevel@tonic-gate 	svc_clts_stats_fini(zoneid, &rpcstat->rpc_clts_server);
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate 	kmem_free(rpcstat, sizeof (*rpcstat));
1220Sstevel@tonic-gate }
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate void
1250Sstevel@tonic-gate mt_kstat_init(void)
1260Sstevel@tonic-gate {
1270Sstevel@tonic-gate 	zone_key_create(&rpcstat_zone_key, mt_kstat_zone_init, NULL,
1280Sstevel@tonic-gate 	    mt_kstat_zone_fini);
1290Sstevel@tonic-gate }
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate void
1320Sstevel@tonic-gate mt_kstat_fini(void)
1330Sstevel@tonic-gate {
1340Sstevel@tonic-gate 	(void) zone_key_delete(rpcstat_zone_key);
1350Sstevel@tonic-gate }
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate static bool_t	clnt_xid_initialized = FALSE;
1380Sstevel@tonic-gate static uint32_t clnt_xid = 0;	/* transaction id used by all clients */
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate uint32_t
1410Sstevel@tonic-gate alloc_xid(void)
1420Sstevel@tonic-gate {
1430Sstevel@tonic-gate 	uint32_t  xid;
1440Sstevel@tonic-gate 	timestruc_t now;
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate 	/*
1470Sstevel@tonic-gate 	 * Do a one time initialzation to better utilize the number
1480Sstevel@tonic-gate 	 * space.
1490Sstevel@tonic-gate 	 */
1500Sstevel@tonic-gate 	mutex_enter(&xid_lock);
1510Sstevel@tonic-gate 	if (clnt_xid_initialized == FALSE) {
1520Sstevel@tonic-gate 		clnt_xid_initialized = TRUE;
1530Sstevel@tonic-gate 		gethrestime(&now);
1540Sstevel@tonic-gate 		clnt_xid = (uint32_t)((now.tv_sec << 20) |
1550Sstevel@tonic-gate 		    (now.tv_nsec >> 10));
1560Sstevel@tonic-gate 	}
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate 	xid = clnt_xid++;
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate 	/*
1610Sstevel@tonic-gate 	 * Don't return a zero xid.  This could happen if the initialization
1620Sstevel@tonic-gate 	 * happens to return zero or if clnt_xid wraps.
1630Sstevel@tonic-gate 	 */
1640Sstevel@tonic-gate 	if (xid == 0)
1650Sstevel@tonic-gate 		xid = clnt_xid++;
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate 	mutex_exit(&xid_lock);
1680Sstevel@tonic-gate 	return (xid);
1690Sstevel@tonic-gate }
170*766Scarlsonj 
171*766Scarlsonj /*
172*766Scarlsonj  * These functions are temporary and designed for the upgrade-workaround only.
173*766Scarlsonj  * They cannot be used for general zone-crossing RPC client support, and will
174*766Scarlsonj  * be removed shortly.
175*766Scarlsonj  */
176*766Scarlsonj struct zone *
177*766Scarlsonj rpc_zone(void)
178*766Scarlsonj {
179*766Scarlsonj 	return (nfs_global_client_only != 0 ? global_zone : curproc->p_zone);
180*766Scarlsonj }
181*766Scarlsonj 
182*766Scarlsonj zoneid_t
183*766Scarlsonj rpc_zoneid(void)
184*766Scarlsonj {
185*766Scarlsonj 	return (nfs_global_client_only != 0 ? GLOBAL_ZONEID : getzoneid());
186*766Scarlsonj }
187