xref: /onnv-gate/usr/src/uts/common/rpc/svc.c (revision 11967:f91b268929d9)
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
51676Sjpk  * Common Development and Distribution License (the "License").
61676Sjpk  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
21390Sraf 
220Sstevel@tonic-gate /*
23*11967SKaren.Rochford@Sun.COM  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*
280Sstevel@tonic-gate  * Copyright 1993 OpenVision Technologies, Inc., All Rights Reserved.
290Sstevel@tonic-gate  */
300Sstevel@tonic-gate 
310Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985,  1986, 1987, 1988, 1989 AT&T	*/
320Sstevel@tonic-gate /*	  All Rights Reserved  	*/
330Sstevel@tonic-gate 
340Sstevel@tonic-gate /*
350Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley 4.3 BSD
360Sstevel@tonic-gate  * under license from the Regents of the University of California.
370Sstevel@tonic-gate  */
380Sstevel@tonic-gate 
390Sstevel@tonic-gate /*
400Sstevel@tonic-gate  * Server-side remote procedure call interface.
410Sstevel@tonic-gate  *
420Sstevel@tonic-gate  * Master transport handle (SVCMASTERXPRT).
430Sstevel@tonic-gate  *   The master transport handle structure is shared among service
440Sstevel@tonic-gate  *   threads processing events on the transport. Some fields in the
450Sstevel@tonic-gate  *   master structure are protected by locks
460Sstevel@tonic-gate  *   - xp_req_lock protects the request queue:
470Sstevel@tonic-gate  *	xp_req_head, xp_req_tail
480Sstevel@tonic-gate  *   - xp_thread_lock protects the thread (clone) counts
490Sstevel@tonic-gate  *	xp_threads, xp_detached_threads, xp_wq
500Sstevel@tonic-gate  *   Each master transport is registered to exactly one thread pool.
510Sstevel@tonic-gate  *
520Sstevel@tonic-gate  * Clone transport handle (SVCXPRT)
530Sstevel@tonic-gate  *   The clone transport handle structure is a per-service-thread handle
540Sstevel@tonic-gate  *   to the transport. The structure carries all the fields/buffers used
550Sstevel@tonic-gate  *   for request processing. A service thread or, in other words, a clone
560Sstevel@tonic-gate  *   structure, can be linked to an arbitrary master structure to process
570Sstevel@tonic-gate  *   requests on this transport. The master handle keeps track of reference
580Sstevel@tonic-gate  *   counts of threads (clones) linked to it. A service thread can switch
590Sstevel@tonic-gate  *   to another transport by unlinking its clone handle from the current
600Sstevel@tonic-gate  *   transport and linking to a new one. Switching is relatively inexpensive
610Sstevel@tonic-gate  *   but it involves locking (master's xprt->xp_thread_lock).
620Sstevel@tonic-gate  *
630Sstevel@tonic-gate  * Pools.
640Sstevel@tonic-gate  *   A pool represents a kernel RPC service (NFS, Lock Manager, etc.).
650Sstevel@tonic-gate  *   Transports related to the service are registered to the service pool.
660Sstevel@tonic-gate  *   Service threads can switch between different transports in the pool.
670Sstevel@tonic-gate  *   Thus, each service has its own pool of service threads. The maximum
680Sstevel@tonic-gate  *   number of threads in a pool is pool->p_maxthreads. This limit allows
690Sstevel@tonic-gate  *   to restrict resource usage by the service. Some fields are protected
700Sstevel@tonic-gate  *   by locks:
710Sstevel@tonic-gate  *   - p_req_lock protects several counts and flags:
720Sstevel@tonic-gate  *	p_reqs, p_walkers, p_asleep, p_drowsy, p_req_cv
730Sstevel@tonic-gate  *   - p_thread_lock governs other thread counts:
740Sstevel@tonic-gate  *	p_threads, p_detached_threads, p_reserved_threads, p_closing
750Sstevel@tonic-gate  *
760Sstevel@tonic-gate  *   In addition, each pool contains a doubly-linked list of transports,
770Sstevel@tonic-gate  *   an `xprt-ready' queue and a creator thread (see below). Threads in
780Sstevel@tonic-gate  *   the pool share some other parameters such as stack size and
790Sstevel@tonic-gate  *   polling timeout.
800Sstevel@tonic-gate  *
810Sstevel@tonic-gate  *   Pools are initialized through the svc_pool_create() function called from
820Sstevel@tonic-gate  *   the nfssys() system call. However, thread creation must be done by
830Sstevel@tonic-gate  *   the userland agent. This is done by using SVCPOOL_WAIT and
840Sstevel@tonic-gate  *   SVCPOOL_RUN arguments to nfssys(), which call svc_wait() and
850Sstevel@tonic-gate  *   svc_do_run(), respectively. Once the pool has been initialized,
860Sstevel@tonic-gate  *   the userland process must set up a 'creator' thread. This thread
870Sstevel@tonic-gate  *   should park itself in the kernel by calling svc_wait(). If
880Sstevel@tonic-gate  *   svc_wait() returns successfully, it should fork off a new worker
890Sstevel@tonic-gate  *   thread, which then calls svc_do_run() in order to get work. When
900Sstevel@tonic-gate  *   that thread is complete, svc_do_run() will return, and the user
910Sstevel@tonic-gate  *   program should call thr_exit().
920Sstevel@tonic-gate  *
930Sstevel@tonic-gate  *   When we try to register a new pool and there is an old pool with
940Sstevel@tonic-gate  *   the same id in the doubly linked pool list (this happens when we kill
950Sstevel@tonic-gate  *   and restart nfsd or lockd), then we unlink the old pool from the list
960Sstevel@tonic-gate  *   and mark its state as `closing'. After that the transports can still
970Sstevel@tonic-gate  *   process requests but new transports won't be registered. When all the
980Sstevel@tonic-gate  *   transports and service threads associated with the pool are gone the
990Sstevel@tonic-gate  *   creator thread (see below) will clean up the pool structure and exit.
1000Sstevel@tonic-gate  *
1010Sstevel@tonic-gate  * svc_queuereq() and svc_run().
1020Sstevel@tonic-gate  *   The kernel RPC server is interrupt driven. The svc_queuereq() interrupt
1030Sstevel@tonic-gate  *   routine is called to deliver an RPC request. The service threads
1040Sstevel@tonic-gate  *   loop in svc_run(). The interrupt function queues a request on the
1050Sstevel@tonic-gate  *   transport's queue and it makes sure that the request is serviced.
1060Sstevel@tonic-gate  *   It may either wake up one of sleeping threads, or ask for a new thread
1070Sstevel@tonic-gate  *   to be created, or, if the previous request is just being picked up, do
1080Sstevel@tonic-gate  *   nothing. In the last case the service thread that is picking up the
1090Sstevel@tonic-gate  *   previous request will wake up or create the next thread. After a service
1100Sstevel@tonic-gate  *   thread processes a request and sends a reply it returns to svc_run()
1110Sstevel@tonic-gate  *   and svc_run() calls svc_poll() to find new input.
1120Sstevel@tonic-gate  *
1134741Sgt29601  *   There is no longer an "inconsistent" but "safe" optimization in the
1144741Sgt29601  *   svc_queuereq() code. This "inconsistent" state was leading to
1154741Sgt29601  *   inconsistencies between the actual number of requests and the value
1164741Sgt29601  *   of p_reqs (the total number of requests). Because of this, hangs were
1174741Sgt29601  *   occurring in svc_poll() where p_reqs was greater than one and no
1184741Sgt29601  *   requests were found on the request queues.
1190Sstevel@tonic-gate  *
1200Sstevel@tonic-gate  * svc_poll().
1210Sstevel@tonic-gate  *   In order to avoid unnecessary locking, which causes performance
1220Sstevel@tonic-gate  *   problems, we always look for a pending request on the current transport.
1230Sstevel@tonic-gate  *   If there is none we take a hint from the pool's `xprt-ready' queue.
1240Sstevel@tonic-gate  *   If the queue had an overflow we switch to the `drain' mode checking
1250Sstevel@tonic-gate  *   each transport  in the pool's transport list. Once we find a
1260Sstevel@tonic-gate  *   master transport handle with a pending request we latch the request
1270Sstevel@tonic-gate  *   lock on this transport and return to svc_run(). If the request
1280Sstevel@tonic-gate  *   belongs to a transport different than the one the service thread is
1290Sstevel@tonic-gate  *   linked to we need to unlink and link again.
1300Sstevel@tonic-gate  *
1310Sstevel@tonic-gate  *   A service thread goes asleep when there are no pending
1320Sstevel@tonic-gate  *   requests on the transports registered on the pool's transports.
1330Sstevel@tonic-gate  *   All the pool's threads sleep on the same condition variable.
1340Sstevel@tonic-gate  *   If a thread has been sleeping for too long period of time
1350Sstevel@tonic-gate  *   (by default 5 seconds) it wakes up and exits.  Also when a transport
1360Sstevel@tonic-gate  *   is closing sleeping threads wake up to unlink from this transport.
1370Sstevel@tonic-gate  *
1380Sstevel@tonic-gate  * The `xprt-ready' queue.
1390Sstevel@tonic-gate  *   If a service thread finds no request on a transport it is currently linked
1400Sstevel@tonic-gate  *   to it will find another transport with a pending request. To make
1410Sstevel@tonic-gate  *   this search more efficient each pool has an `xprt-ready' queue.
1420Sstevel@tonic-gate  *   The queue is a FIFO. When the interrupt routine queues a request it also
1430Sstevel@tonic-gate  *   inserts a pointer to the transport into the `xprt-ready' queue. A
1440Sstevel@tonic-gate  *   thread looking for a transport with a pending request can pop up a
1450Sstevel@tonic-gate  *   transport and check for a request. The request can be already gone
1460Sstevel@tonic-gate  *   since it could be taken by a thread linked to that transport. In such a
1470Sstevel@tonic-gate  *   case we try the next hint. The `xprt-ready' queue has fixed size (by
1480Sstevel@tonic-gate  *   default 256 nodes). If it overflows svc_poll() has to switch to the
1490Sstevel@tonic-gate  *   less efficient but safe `drain' mode and walk through the pool's
1500Sstevel@tonic-gate  *   transport list.
1510Sstevel@tonic-gate  *
1520Sstevel@tonic-gate  *   Both the svc_poll() loop and the `xprt-ready' queue are optimized
1530Sstevel@tonic-gate  *   for the peak load case that is for the situation when the queue is not
1540Sstevel@tonic-gate  *   empty, there are all the time few pending requests, and a service
1550Sstevel@tonic-gate  *   thread which has just processed a request does not go asleep but picks
1560Sstevel@tonic-gate  *   up immediately the next request.
1570Sstevel@tonic-gate  *
1580Sstevel@tonic-gate  * Thread creator.
1590Sstevel@tonic-gate  *   Each pool has a thread creator associated with it. The creator thread
1600Sstevel@tonic-gate  *   sleeps on a condition variable and waits for a signal to create a
1610Sstevel@tonic-gate  *   service thread. The actual thread creation is done in userland by
1620Sstevel@tonic-gate  *   the method described in "Pools" above.
1630Sstevel@tonic-gate  *
1640Sstevel@tonic-gate  *   Signaling threads should turn on the `creator signaled' flag, and
1650Sstevel@tonic-gate  *   can avoid sending signals when the flag is on. The flag is cleared
1660Sstevel@tonic-gate  *   when the thread is created.
1670Sstevel@tonic-gate  *
1680Sstevel@tonic-gate  *   When the pool is in closing state (ie it has been already unregistered
1690Sstevel@tonic-gate  *   from the pool list) the last thread on the last transport in the pool
1700Sstevel@tonic-gate  *   should turn the p_creator_exit flag on. The creator thread will
1710Sstevel@tonic-gate  *   clean up the pool structure and exit.
1720Sstevel@tonic-gate  *
1730Sstevel@tonic-gate  * Thread reservation; Detaching service threads.
1740Sstevel@tonic-gate  *   A service thread can detach itself to block for an extended amount
1750Sstevel@tonic-gate  *   of time. However, to keep the service active we need to guarantee
1760Sstevel@tonic-gate  *   at least pool->p_redline non-detached threads that can process incoming
1770Sstevel@tonic-gate  *   requests. This, the maximum number of detached and reserved threads is
1780Sstevel@tonic-gate  *   p->p_maxthreads - p->p_redline. A service thread should first acquire
1790Sstevel@tonic-gate  *   a reservation, and if the reservation was granted it can detach itself.
1800Sstevel@tonic-gate  *   If a reservation was granted but the thread does not detach itself
1810Sstevel@tonic-gate  *   it should cancel the reservation before it returns to svc_run().
1820Sstevel@tonic-gate  */
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate #include <sys/param.h>
1850Sstevel@tonic-gate #include <sys/types.h>
1860Sstevel@tonic-gate #include <rpc/types.h>
1870Sstevel@tonic-gate #include <sys/socket.h>
1880Sstevel@tonic-gate #include <sys/time.h>
1890Sstevel@tonic-gate #include <sys/tiuser.h>
1900Sstevel@tonic-gate #include <sys/t_kuser.h>
1910Sstevel@tonic-gate #include <netinet/in.h>
1920Sstevel@tonic-gate #include <rpc/xdr.h>
1930Sstevel@tonic-gate #include <rpc/auth.h>
1940Sstevel@tonic-gate #include <rpc/clnt.h>
1950Sstevel@tonic-gate #include <rpc/rpc_msg.h>
1960Sstevel@tonic-gate #include <rpc/svc.h>
1970Sstevel@tonic-gate #include <sys/proc.h>
1980Sstevel@tonic-gate #include <sys/user.h>
1990Sstevel@tonic-gate #include <sys/stream.h>
2000Sstevel@tonic-gate #include <sys/strsubr.h>
2010Sstevel@tonic-gate #include <sys/tihdr.h>
2020Sstevel@tonic-gate #include <sys/debug.h>
2030Sstevel@tonic-gate #include <sys/cmn_err.h>
2040Sstevel@tonic-gate #include <sys/file.h>
2050Sstevel@tonic-gate #include <sys/systm.h>
2060Sstevel@tonic-gate #include <sys/callb.h>
2070Sstevel@tonic-gate #include <sys/vtrace.h>
2080Sstevel@tonic-gate #include <sys/zone.h>
2090Sstevel@tonic-gate #include <nfs/nfs.h>
2101676Sjpk #include <sys/tsol/label_macro.h>
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate #define	RQCRED_SIZE	400	/* this size is excessive */
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate /*
2150Sstevel@tonic-gate  * Defines for svc_poll()
2160Sstevel@tonic-gate  */
2170Sstevel@tonic-gate #define	SVC_EXPRTGONE ((SVCMASTERXPRT *)1)	/* Transport is closing */
2180Sstevel@tonic-gate #define	SVC_ETIMEDOUT ((SVCMASTERXPRT *)2)	/* Timeout */
2190Sstevel@tonic-gate #define	SVC_EINTR ((SVCMASTERXPRT *)3)		/* Interrupted by signal */
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate /*
2220Sstevel@tonic-gate  * Default stack size for service threads.
2230Sstevel@tonic-gate  */
2240Sstevel@tonic-gate #define	DEFAULT_SVC_RUN_STKSIZE		(0)	/* default kernel stack */
2250Sstevel@tonic-gate 
2260Sstevel@tonic-gate int    svc_default_stksize = DEFAULT_SVC_RUN_STKSIZE;
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate /*
2290Sstevel@tonic-gate  * Default polling timeout for service threads.
2300Sstevel@tonic-gate  * Multiplied by hz when used.
2310Sstevel@tonic-gate  */
2320Sstevel@tonic-gate #define	DEFAULT_SVC_POLL_TIMEOUT	(5)	/* seconds */
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate clock_t svc_default_timeout = DEFAULT_SVC_POLL_TIMEOUT;
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate /*
2370Sstevel@tonic-gate  * Size of the `xprt-ready' queue.
2380Sstevel@tonic-gate  */
2390Sstevel@tonic-gate #define	DEFAULT_SVC_QSIZE		(256)	/* qnodes */
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate size_t svc_default_qsize = DEFAULT_SVC_QSIZE;
2420Sstevel@tonic-gate 
2430Sstevel@tonic-gate /*
2440Sstevel@tonic-gate  * Default limit for the number of service threads.
2450Sstevel@tonic-gate  */
2460Sstevel@tonic-gate #define	DEFAULT_SVC_MAXTHREADS		(INT16_MAX)
2470Sstevel@tonic-gate 
2480Sstevel@tonic-gate int    svc_default_maxthreads = DEFAULT_SVC_MAXTHREADS;
2490Sstevel@tonic-gate 
2500Sstevel@tonic-gate /*
2510Sstevel@tonic-gate  * Maximum number of requests from the same transport (in `drain' mode).
2520Sstevel@tonic-gate  */
2530Sstevel@tonic-gate #define	DEFAULT_SVC_MAX_SAME_XPRT	(8)
2540Sstevel@tonic-gate 
2550Sstevel@tonic-gate int    svc_default_max_same_xprt = DEFAULT_SVC_MAX_SAME_XPRT;
2560Sstevel@tonic-gate 
2570Sstevel@tonic-gate 
2580Sstevel@tonic-gate /*
2590Sstevel@tonic-gate  * Default `Redline' of non-detached threads.
2600Sstevel@tonic-gate  * Total number of detached and reserved threads in an RPC server
2610Sstevel@tonic-gate  * thread pool is limited to pool->p_maxthreads - svc_redline.
2620Sstevel@tonic-gate  */
2630Sstevel@tonic-gate #define	DEFAULT_SVC_REDLINE		(1)
2640Sstevel@tonic-gate 
2650Sstevel@tonic-gate int    svc_default_redline = DEFAULT_SVC_REDLINE;
2660Sstevel@tonic-gate 
2670Sstevel@tonic-gate /*
2680Sstevel@tonic-gate  * A node for the `xprt-ready' queue.
2690Sstevel@tonic-gate  * See below.
2700Sstevel@tonic-gate  */
2710Sstevel@tonic-gate struct __svcxprt_qnode {
2720Sstevel@tonic-gate 	__SVCXPRT_QNODE	*q_next;
2730Sstevel@tonic-gate 	SVCMASTERXPRT	*q_xprt;
2740Sstevel@tonic-gate };
2750Sstevel@tonic-gate 
2760Sstevel@tonic-gate /*
2770Sstevel@tonic-gate  * Global SVC variables (private).
2780Sstevel@tonic-gate  */
2790Sstevel@tonic-gate struct svc_globals {
2800Sstevel@tonic-gate 	SVCPOOL		*svc_pools;
2810Sstevel@tonic-gate 	kmutex_t	svc_plock;
2820Sstevel@tonic-gate };
2830Sstevel@tonic-gate 
2840Sstevel@tonic-gate /*
2850Sstevel@tonic-gate  * Debug variable to check for rdma based
2860Sstevel@tonic-gate  * transport startup and cleanup. Contorlled
2870Sstevel@tonic-gate  * through /etc/system. Off by default.
2880Sstevel@tonic-gate  */
2890Sstevel@tonic-gate int rdma_check = 0;
2900Sstevel@tonic-gate 
2910Sstevel@tonic-gate /*
2920Sstevel@tonic-gate  * Authentication parameters list.
2930Sstevel@tonic-gate  */
2940Sstevel@tonic-gate static caddr_t rqcred_head;
2950Sstevel@tonic-gate static kmutex_t rqcred_lock;
2960Sstevel@tonic-gate 
2970Sstevel@tonic-gate /*
2980Sstevel@tonic-gate  * Pointers to transport specific `rele' routines in rpcmod (set from rpcmod).
2990Sstevel@tonic-gate  */
3000Sstevel@tonic-gate void	(*rpc_rele)(queue_t *, mblk_t *) = NULL;
3010Sstevel@tonic-gate void	(*mir_rele)(queue_t *, mblk_t *) = NULL;
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate /* ARGSUSED */
3040Sstevel@tonic-gate void
rpc_rdma_rele(queue_t * q,mblk_t * mp)3050Sstevel@tonic-gate rpc_rdma_rele(queue_t *q, mblk_t *mp)
3060Sstevel@tonic-gate {
3070Sstevel@tonic-gate }
3080Sstevel@tonic-gate void    (*rdma_rele)(queue_t *, mblk_t *) = rpc_rdma_rele;
3090Sstevel@tonic-gate 
3100Sstevel@tonic-gate 
3110Sstevel@tonic-gate /*
3120Sstevel@tonic-gate  * This macro picks which `rele' routine to use, based on the transport type.
3130Sstevel@tonic-gate  */
3140Sstevel@tonic-gate #define	RELE_PROC(xprt) \
3150Sstevel@tonic-gate 	((xprt)->xp_type == T_RDMA ? rdma_rele : \
3160Sstevel@tonic-gate 	(((xprt)->xp_type == T_CLTS) ? rpc_rele : mir_rele))
3170Sstevel@tonic-gate 
3180Sstevel@tonic-gate /*
3190Sstevel@tonic-gate  * If true, then keep quiet about version mismatch.
3200Sstevel@tonic-gate  * This macro is for broadcast RPC only. We have no broadcast RPC in
3210Sstevel@tonic-gate  * kernel now but one may define a flag in the transport structure
3220Sstevel@tonic-gate  * and redefine this macro.
3230Sstevel@tonic-gate  */
3240Sstevel@tonic-gate #define	version_keepquiet(xprt)	(FALSE)
3250Sstevel@tonic-gate 
3260Sstevel@tonic-gate /*
3270Sstevel@tonic-gate  * ZSD key used to retrieve zone-specific svc globals
3280Sstevel@tonic-gate  */
3290Sstevel@tonic-gate static zone_key_t svc_zone_key;
3300Sstevel@tonic-gate 
3310Sstevel@tonic-gate static void svc_callout_free(SVCMASTERXPRT *);
3320Sstevel@tonic-gate static void svc_xprt_qinit(SVCPOOL *, size_t);
3330Sstevel@tonic-gate static void svc_xprt_qdestroy(SVCPOOL *);
3340Sstevel@tonic-gate static void svc_thread_creator(SVCPOOL *);
3350Sstevel@tonic-gate static void svc_creator_signal(SVCPOOL *);
3360Sstevel@tonic-gate static void svc_creator_signalexit(SVCPOOL *);
3370Sstevel@tonic-gate static void svc_pool_unregister(struct svc_globals *, SVCPOOL *);
3380Sstevel@tonic-gate static int svc_run(SVCPOOL *);
3390Sstevel@tonic-gate 
3400Sstevel@tonic-gate /* ARGSUSED */
3410Sstevel@tonic-gate static void *
svc_zoneinit(zoneid_t zoneid)3420Sstevel@tonic-gate svc_zoneinit(zoneid_t zoneid)
3430Sstevel@tonic-gate {
3440Sstevel@tonic-gate 	struct svc_globals *svc;
3450Sstevel@tonic-gate 
3460Sstevel@tonic-gate 	svc = kmem_alloc(sizeof (*svc), KM_SLEEP);
3470Sstevel@tonic-gate 	mutex_init(&svc->svc_plock, NULL, MUTEX_DEFAULT, NULL);
3480Sstevel@tonic-gate 	svc->svc_pools = NULL;
3490Sstevel@tonic-gate 	return (svc);
3500Sstevel@tonic-gate }
3510Sstevel@tonic-gate 
3520Sstevel@tonic-gate /* ARGSUSED */
3530Sstevel@tonic-gate static void
svc_zoneshutdown(zoneid_t zoneid,void * arg)3540Sstevel@tonic-gate svc_zoneshutdown(zoneid_t zoneid, void *arg)
3550Sstevel@tonic-gate {
3560Sstevel@tonic-gate 	struct svc_globals *svc = arg;
3570Sstevel@tonic-gate 	SVCPOOL *pool;
3580Sstevel@tonic-gate 
3590Sstevel@tonic-gate 	mutex_enter(&svc->svc_plock);
3600Sstevel@tonic-gate 	while ((pool = svc->svc_pools) != NULL) {
3610Sstevel@tonic-gate 		svc_pool_unregister(svc, pool);
3620Sstevel@tonic-gate 	}
3630Sstevel@tonic-gate 	mutex_exit(&svc->svc_plock);
3640Sstevel@tonic-gate }
3650Sstevel@tonic-gate 
3660Sstevel@tonic-gate /* ARGSUSED */
3670Sstevel@tonic-gate static void
svc_zonefini(zoneid_t zoneid,void * arg)3680Sstevel@tonic-gate svc_zonefini(zoneid_t zoneid, void *arg)
3690Sstevel@tonic-gate {
3700Sstevel@tonic-gate 	struct svc_globals *svc = arg;
3710Sstevel@tonic-gate 
3720Sstevel@tonic-gate 	ASSERT(svc->svc_pools == NULL);
3730Sstevel@tonic-gate 	mutex_destroy(&svc->svc_plock);
3740Sstevel@tonic-gate 	kmem_free(svc, sizeof (*svc));
3750Sstevel@tonic-gate }
3760Sstevel@tonic-gate 
3770Sstevel@tonic-gate /*
3780Sstevel@tonic-gate  * Global SVC init routine.
3790Sstevel@tonic-gate  * Initialize global generic and transport type specific structures
3800Sstevel@tonic-gate  * used by the kernel RPC server side. This routine is called only
3810Sstevel@tonic-gate  * once when the module is being loaded.
3820Sstevel@tonic-gate  */
3830Sstevel@tonic-gate void
svc_init()3840Sstevel@tonic-gate svc_init()
3850Sstevel@tonic-gate {
3860Sstevel@tonic-gate 	zone_key_create(&svc_zone_key, svc_zoneinit, svc_zoneshutdown,
3870Sstevel@tonic-gate 	    svc_zonefini);
3880Sstevel@tonic-gate 	svc_cots_init();
3890Sstevel@tonic-gate 	svc_clts_init();
3900Sstevel@tonic-gate }
3910Sstevel@tonic-gate 
3920Sstevel@tonic-gate /*
3930Sstevel@tonic-gate  * Destroy the SVCPOOL structure.
3940Sstevel@tonic-gate  */
3950Sstevel@tonic-gate static void
svc_pool_cleanup(SVCPOOL * pool)3960Sstevel@tonic-gate svc_pool_cleanup(SVCPOOL *pool)
3970Sstevel@tonic-gate {
3980Sstevel@tonic-gate 	ASSERT(pool->p_threads + pool->p_detached_threads == 0);
3990Sstevel@tonic-gate 	ASSERT(pool->p_lcount == 0);
4000Sstevel@tonic-gate 	ASSERT(pool->p_closing);
4010Sstevel@tonic-gate 
4020Sstevel@tonic-gate 	/*
4030Sstevel@tonic-gate 	 * Call the user supplied shutdown function.  This is done
4040Sstevel@tonic-gate 	 * here so the user of the pool will be able to cleanup
4050Sstevel@tonic-gate 	 * service related resources.
4060Sstevel@tonic-gate 	 */
4070Sstevel@tonic-gate 	if (pool->p_shutdown != NULL)
4080Sstevel@tonic-gate 		(pool->p_shutdown)();
4090Sstevel@tonic-gate 
4100Sstevel@tonic-gate 	/* Destroy `xprt-ready' queue */
4110Sstevel@tonic-gate 	svc_xprt_qdestroy(pool);
4120Sstevel@tonic-gate 
4130Sstevel@tonic-gate 	/* Destroy transport list */
4140Sstevel@tonic-gate 	rw_destroy(&pool->p_lrwlock);
4150Sstevel@tonic-gate 
4160Sstevel@tonic-gate 	/* Destroy locks and condition variables */
4170Sstevel@tonic-gate 	mutex_destroy(&pool->p_thread_lock);
4180Sstevel@tonic-gate 	mutex_destroy(&pool->p_req_lock);
4190Sstevel@tonic-gate 	cv_destroy(&pool->p_req_cv);
4200Sstevel@tonic-gate 
4210Sstevel@tonic-gate 	/* Destroy creator's locks and condition variables */
4220Sstevel@tonic-gate 	mutex_destroy(&pool->p_creator_lock);
4230Sstevel@tonic-gate 	cv_destroy(&pool->p_creator_cv);
4240Sstevel@tonic-gate 	mutex_destroy(&pool->p_user_lock);
4250Sstevel@tonic-gate 	cv_destroy(&pool->p_user_cv);
4260Sstevel@tonic-gate 
4270Sstevel@tonic-gate 	/* Free pool structure */
4280Sstevel@tonic-gate 	kmem_free(pool, sizeof (SVCPOOL));
4290Sstevel@tonic-gate }
4300Sstevel@tonic-gate 
4310Sstevel@tonic-gate /*
4320Sstevel@tonic-gate  * If all the transports and service threads are already gone
4330Sstevel@tonic-gate  * signal the creator thread to clean up and exit.
4340Sstevel@tonic-gate  */
4350Sstevel@tonic-gate static bool_t
svc_pool_tryexit(SVCPOOL * pool)4360Sstevel@tonic-gate svc_pool_tryexit(SVCPOOL *pool)
4370Sstevel@tonic-gate {
4380Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pool->p_thread_lock));
4390Sstevel@tonic-gate 	ASSERT(pool->p_closing);
4400Sstevel@tonic-gate 
4410Sstevel@tonic-gate 	if (pool->p_threads + pool->p_detached_threads == 0) {
4420Sstevel@tonic-gate 		rw_enter(&pool->p_lrwlock, RW_READER);
4430Sstevel@tonic-gate 		if (pool->p_lcount == 0) {
4440Sstevel@tonic-gate 			/*
4450Sstevel@tonic-gate 			 * Release the locks before sending a signal.
4460Sstevel@tonic-gate 			 */
4470Sstevel@tonic-gate 			rw_exit(&pool->p_lrwlock);
4480Sstevel@tonic-gate 			mutex_exit(&pool->p_thread_lock);
4490Sstevel@tonic-gate 
4500Sstevel@tonic-gate 			/*
4510Sstevel@tonic-gate 			 * Notify the creator thread to clean up and exit
4520Sstevel@tonic-gate 			 *
4530Sstevel@tonic-gate 			 * NOTICE: No references to the pool beyond this point!
4540Sstevel@tonic-gate 			 *		   The pool is being destroyed.
4550Sstevel@tonic-gate 			 */
4560Sstevel@tonic-gate 			ASSERT(!MUTEX_HELD(&pool->p_thread_lock));
4570Sstevel@tonic-gate 			svc_creator_signalexit(pool);
4580Sstevel@tonic-gate 
4590Sstevel@tonic-gate 			return (TRUE);
4600Sstevel@tonic-gate 		}
4610Sstevel@tonic-gate 		rw_exit(&pool->p_lrwlock);
4620Sstevel@tonic-gate 	}
4630Sstevel@tonic-gate 
4640Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pool->p_thread_lock));
4650Sstevel@tonic-gate 	return (FALSE);
4660Sstevel@tonic-gate }
4670Sstevel@tonic-gate 
4680Sstevel@tonic-gate /*
4690Sstevel@tonic-gate  * Find a pool with a given id.
4700Sstevel@tonic-gate  */
4710Sstevel@tonic-gate static SVCPOOL *
svc_pool_find(struct svc_globals * svc,int id)4720Sstevel@tonic-gate svc_pool_find(struct svc_globals *svc, int id)
4730Sstevel@tonic-gate {
4740Sstevel@tonic-gate 	SVCPOOL *pool;
4750Sstevel@tonic-gate 
4760Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&svc->svc_plock));
4770Sstevel@tonic-gate 
4780Sstevel@tonic-gate 	/*
4790Sstevel@tonic-gate 	 * Search the list for a pool with a matching id
4800Sstevel@tonic-gate 	 * and register the transport handle with that pool.
4810Sstevel@tonic-gate 	 */
4820Sstevel@tonic-gate 	for (pool = svc->svc_pools; pool; pool = pool->p_next)
4830Sstevel@tonic-gate 		if (pool->p_id == id)
4840Sstevel@tonic-gate 			return (pool);
4850Sstevel@tonic-gate 
4860Sstevel@tonic-gate 	return (NULL);
4870Sstevel@tonic-gate }
4880Sstevel@tonic-gate 
4890Sstevel@tonic-gate /*
4900Sstevel@tonic-gate  * PSARC 2003/523 Contract Private Interface
4910Sstevel@tonic-gate  * svc_do_run
4920Sstevel@tonic-gate  * Changes must be reviewed by Solaris File Sharing
4930Sstevel@tonic-gate  * Changes must be communicated to contract-2003-523@sun.com
4940Sstevel@tonic-gate  */
4950Sstevel@tonic-gate int
svc_do_run(int id)4960Sstevel@tonic-gate svc_do_run(int id)
4970Sstevel@tonic-gate {
4980Sstevel@tonic-gate 	SVCPOOL *pool;
4990Sstevel@tonic-gate 	int err = 0;
5000Sstevel@tonic-gate 	struct svc_globals *svc;
5010Sstevel@tonic-gate 
5020Sstevel@tonic-gate 	svc = zone_getspecific(svc_zone_key, curproc->p_zone);
5030Sstevel@tonic-gate 	mutex_enter(&svc->svc_plock);
5040Sstevel@tonic-gate 
5050Sstevel@tonic-gate 	pool = svc_pool_find(svc, id);
5060Sstevel@tonic-gate 
5070Sstevel@tonic-gate 	mutex_exit(&svc->svc_plock);
5080Sstevel@tonic-gate 
5090Sstevel@tonic-gate 	if (pool == NULL)
5100Sstevel@tonic-gate 		return (ENOENT);
5110Sstevel@tonic-gate 
5120Sstevel@tonic-gate 	/*
5130Sstevel@tonic-gate 	 * Increment counter of pool threads now
5140Sstevel@tonic-gate 	 * that a thread has been created.
5150Sstevel@tonic-gate 	 */
5160Sstevel@tonic-gate 	mutex_enter(&pool->p_thread_lock);
5170Sstevel@tonic-gate 	pool->p_threads++;
5180Sstevel@tonic-gate 	mutex_exit(&pool->p_thread_lock);
5190Sstevel@tonic-gate 
5200Sstevel@tonic-gate 	/* Give work to the new thread. */
5210Sstevel@tonic-gate 	err = svc_run(pool);
5220Sstevel@tonic-gate 
5230Sstevel@tonic-gate 	return (err);
5240Sstevel@tonic-gate }
5250Sstevel@tonic-gate 
5260Sstevel@tonic-gate /*
5270Sstevel@tonic-gate  * Unregister a pool from the pool list.
5280Sstevel@tonic-gate  * Set the closing state. If all the transports and service threads
5290Sstevel@tonic-gate  * are already gone signal the creator thread to clean up and exit.
5300Sstevel@tonic-gate  */
5310Sstevel@tonic-gate static void
svc_pool_unregister(struct svc_globals * svc,SVCPOOL * pool)5320Sstevel@tonic-gate svc_pool_unregister(struct svc_globals *svc, SVCPOOL *pool)
5330Sstevel@tonic-gate {
5340Sstevel@tonic-gate 	SVCPOOL *next = pool->p_next;
5350Sstevel@tonic-gate 	SVCPOOL *prev = pool->p_prev;
5360Sstevel@tonic-gate 
5370Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&svc->svc_plock));
5380Sstevel@tonic-gate 
5390Sstevel@tonic-gate 	/* Remove from the list */
5400Sstevel@tonic-gate 	if (pool == svc->svc_pools)
5410Sstevel@tonic-gate 		svc->svc_pools = next;
5420Sstevel@tonic-gate 	if (next)
5430Sstevel@tonic-gate 		next->p_prev = prev;
5440Sstevel@tonic-gate 	if (prev)
5450Sstevel@tonic-gate 		prev->p_next = next;
5460Sstevel@tonic-gate 	pool->p_next = pool->p_prev = NULL;
5470Sstevel@tonic-gate 
5480Sstevel@tonic-gate 	/*
5490Sstevel@tonic-gate 	 * Offline the pool. Mark the pool as closing.
5500Sstevel@tonic-gate 	 * If there are no transports in this pool notify
5510Sstevel@tonic-gate 	 * the creator thread to clean it up and exit.
5520Sstevel@tonic-gate 	 */
5530Sstevel@tonic-gate 	mutex_enter(&pool->p_thread_lock);
5540Sstevel@tonic-gate 	if (pool->p_offline != NULL)
5550Sstevel@tonic-gate 		(pool->p_offline)();
5560Sstevel@tonic-gate 	pool->p_closing = TRUE;
5570Sstevel@tonic-gate 	if (svc_pool_tryexit(pool))
5580Sstevel@tonic-gate 		return;
5590Sstevel@tonic-gate 	mutex_exit(&pool->p_thread_lock);
5600Sstevel@tonic-gate }
5610Sstevel@tonic-gate 
5620Sstevel@tonic-gate /*
5630Sstevel@tonic-gate  * Register a pool with a given id in the global doubly linked pool list.
5640Sstevel@tonic-gate  * - if there is a pool with the same id in the list then unregister it
5650Sstevel@tonic-gate  * - insert the new pool into the list.
5660Sstevel@tonic-gate  */
5670Sstevel@tonic-gate static void
svc_pool_register(struct svc_globals * svc,SVCPOOL * pool,int id)5680Sstevel@tonic-gate svc_pool_register(struct svc_globals *svc, SVCPOOL *pool, int id)
5690Sstevel@tonic-gate {
5700Sstevel@tonic-gate 	SVCPOOL *old_pool;
5710Sstevel@tonic-gate 
5720Sstevel@tonic-gate 	/*
5730Sstevel@tonic-gate 	 * If there is a pool with the same id then remove it from
5740Sstevel@tonic-gate 	 * the list and mark the pool as closing.
5750Sstevel@tonic-gate 	 */
5760Sstevel@tonic-gate 	mutex_enter(&svc->svc_plock);
5770Sstevel@tonic-gate 
5780Sstevel@tonic-gate 	if (old_pool = svc_pool_find(svc, id))
5790Sstevel@tonic-gate 		svc_pool_unregister(svc, old_pool);
5800Sstevel@tonic-gate 
5810Sstevel@tonic-gate 	/* Insert into the doubly linked list */
5820Sstevel@tonic-gate 	pool->p_id = id;
5830Sstevel@tonic-gate 	pool->p_next = svc->svc_pools;
5840Sstevel@tonic-gate 	pool->p_prev = NULL;
5850Sstevel@tonic-gate 	if (svc->svc_pools)
5860Sstevel@tonic-gate 		svc->svc_pools->p_prev = pool;
5870Sstevel@tonic-gate 	svc->svc_pools = pool;
5880Sstevel@tonic-gate 
5890Sstevel@tonic-gate 	mutex_exit(&svc->svc_plock);
5900Sstevel@tonic-gate }
5910Sstevel@tonic-gate 
5920Sstevel@tonic-gate /*
5930Sstevel@tonic-gate  * Initialize a newly created pool structure
5940Sstevel@tonic-gate  */
5950Sstevel@tonic-gate static int
svc_pool_init(SVCPOOL * pool,uint_t maxthreads,uint_t redline,uint_t qsize,uint_t timeout,uint_t stksize,uint_t max_same_xprt)5960Sstevel@tonic-gate svc_pool_init(SVCPOOL *pool, uint_t maxthreads, uint_t redline,
5970Sstevel@tonic-gate 	uint_t qsize, uint_t timeout, uint_t stksize, uint_t max_same_xprt)
5980Sstevel@tonic-gate {
5990Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(curthread);
6000Sstevel@tonic-gate 
6010Sstevel@tonic-gate 	ASSERT(pool);
6020Sstevel@tonic-gate 
6030Sstevel@tonic-gate 	if (maxthreads == 0)
6040Sstevel@tonic-gate 		maxthreads = svc_default_maxthreads;
6050Sstevel@tonic-gate 	if (redline == 0)
6060Sstevel@tonic-gate 		redline = svc_default_redline;
6070Sstevel@tonic-gate 	if (qsize == 0)
6080Sstevel@tonic-gate 		qsize = svc_default_qsize;
6090Sstevel@tonic-gate 	if (timeout == 0)
6100Sstevel@tonic-gate 		timeout = svc_default_timeout;
6110Sstevel@tonic-gate 	if (stksize == 0)
6120Sstevel@tonic-gate 		stksize = svc_default_stksize;
6130Sstevel@tonic-gate 	if (max_same_xprt == 0)
6140Sstevel@tonic-gate 		max_same_xprt = svc_default_max_same_xprt;
6150Sstevel@tonic-gate 
6160Sstevel@tonic-gate 	if (maxthreads < redline)
6170Sstevel@tonic-gate 		return (EINVAL);
6180Sstevel@tonic-gate 
6190Sstevel@tonic-gate 	/* Allocate and initialize the `xprt-ready' queue */
6200Sstevel@tonic-gate 	svc_xprt_qinit(pool, qsize);
6210Sstevel@tonic-gate 
6220Sstevel@tonic-gate 	/* Initialize doubly-linked xprt list */
6230Sstevel@tonic-gate 	rw_init(&pool->p_lrwlock, NULL, RW_DEFAULT, NULL);
6240Sstevel@tonic-gate 
6250Sstevel@tonic-gate 	/*
6260Sstevel@tonic-gate 	 * Setting lwp_childstksz on the current lwp so that
6270Sstevel@tonic-gate 	 * descendants of this lwp get the modified stacksize, if
6280Sstevel@tonic-gate 	 * it is defined. It is important that either this lwp or
6290Sstevel@tonic-gate 	 * one of its descendants do the actual servicepool thread
6300Sstevel@tonic-gate 	 * creation to maintain the stacksize inheritance.
6310Sstevel@tonic-gate 	 */
6320Sstevel@tonic-gate 	if (lwp != NULL)
6330Sstevel@tonic-gate 		lwp->lwp_childstksz = stksize;
6340Sstevel@tonic-gate 
6350Sstevel@tonic-gate 	/* Initialize thread limits, locks and condition variables */
6360Sstevel@tonic-gate 	pool->p_maxthreads = maxthreads;
6370Sstevel@tonic-gate 	pool->p_redline = redline;
6380Sstevel@tonic-gate 	pool->p_timeout = timeout * hz;
6390Sstevel@tonic-gate 	pool->p_stksize = stksize;
6400Sstevel@tonic-gate 	pool->p_max_same_xprt = max_same_xprt;
6410Sstevel@tonic-gate 	mutex_init(&pool->p_thread_lock, NULL, MUTEX_DEFAULT, NULL);
6420Sstevel@tonic-gate 	mutex_init(&pool->p_req_lock, NULL, MUTEX_DEFAULT, NULL);
6430Sstevel@tonic-gate 	cv_init(&pool->p_req_cv, NULL, CV_DEFAULT, NULL);
6440Sstevel@tonic-gate 
6450Sstevel@tonic-gate 	/* Initialize userland creator */
6460Sstevel@tonic-gate 	pool->p_user_exit = FALSE;
6470Sstevel@tonic-gate 	pool->p_signal_create_thread = FALSE;
6480Sstevel@tonic-gate 	pool->p_user_waiting = FALSE;
6490Sstevel@tonic-gate 	mutex_init(&pool->p_user_lock, NULL, MUTEX_DEFAULT, NULL);
6500Sstevel@tonic-gate 	cv_init(&pool->p_user_cv, NULL, CV_DEFAULT, NULL);
6510Sstevel@tonic-gate 
6520Sstevel@tonic-gate 	/* Initialize the creator and start the creator thread */
6530Sstevel@tonic-gate 	pool->p_creator_exit = FALSE;
6540Sstevel@tonic-gate 	mutex_init(&pool->p_creator_lock, NULL, MUTEX_DEFAULT, NULL);
6550Sstevel@tonic-gate 	cv_init(&pool->p_creator_cv, NULL, CV_DEFAULT, NULL);
6560Sstevel@tonic-gate 
6570Sstevel@tonic-gate 	(void) zthread_create(NULL, pool->p_stksize, svc_thread_creator,
6580Sstevel@tonic-gate 	    pool, 0, minclsyspri);
6590Sstevel@tonic-gate 
6600Sstevel@tonic-gate 	return (0);
6610Sstevel@tonic-gate }
6620Sstevel@tonic-gate 
6630Sstevel@tonic-gate /*
6640Sstevel@tonic-gate  * PSARC 2003/523 Contract Private Interface
6650Sstevel@tonic-gate  * svc_pool_create
6660Sstevel@tonic-gate  * Changes must be reviewed by Solaris File Sharing
6670Sstevel@tonic-gate  * Changes must be communicated to contract-2003-523@sun.com
6680Sstevel@tonic-gate  *
6690Sstevel@tonic-gate  * Create an kernel RPC server-side thread/transport pool.
6700Sstevel@tonic-gate  *
6710Sstevel@tonic-gate  * This is public interface for creation of a server RPC thread pool
6720Sstevel@tonic-gate  * for a given service provider. Transports registered with the pool's id
6730Sstevel@tonic-gate  * will be served by a pool's threads. This function is called from the
6740Sstevel@tonic-gate  * nfssys() system call.
6750Sstevel@tonic-gate  */
6760Sstevel@tonic-gate int
svc_pool_create(struct svcpool_args * args)6770Sstevel@tonic-gate svc_pool_create(struct svcpool_args *args)
6780Sstevel@tonic-gate {
6790Sstevel@tonic-gate 	SVCPOOL *pool;
6800Sstevel@tonic-gate 	int error;
6810Sstevel@tonic-gate 	struct svc_globals *svc;
6820Sstevel@tonic-gate 
6830Sstevel@tonic-gate 	/*
6840Sstevel@tonic-gate 	 * Caller should check credentials in a way appropriate
6850Sstevel@tonic-gate 	 * in the context of the call.
6860Sstevel@tonic-gate 	 */
6870Sstevel@tonic-gate 
6880Sstevel@tonic-gate 	svc = zone_getspecific(svc_zone_key, curproc->p_zone);
6890Sstevel@tonic-gate 	/* Allocate a new pool */
6900Sstevel@tonic-gate 	pool = kmem_zalloc(sizeof (SVCPOOL), KM_SLEEP);
6910Sstevel@tonic-gate 
6920Sstevel@tonic-gate 	/*
6930Sstevel@tonic-gate 	 * Initialize the pool structure and create a creator thread.
6940Sstevel@tonic-gate 	 */
6950Sstevel@tonic-gate 	error = svc_pool_init(pool, args->maxthreads, args->redline,
6960Sstevel@tonic-gate 	    args->qsize, args->timeout, args->stksize, args->max_same_xprt);
6970Sstevel@tonic-gate 
6980Sstevel@tonic-gate 	if (error) {
6990Sstevel@tonic-gate 		kmem_free(pool, sizeof (SVCPOOL));
7000Sstevel@tonic-gate 		return (error);
7010Sstevel@tonic-gate 	}
7020Sstevel@tonic-gate 
7030Sstevel@tonic-gate 	/* Register the pool with the global pool list */
7040Sstevel@tonic-gate 	svc_pool_register(svc, pool, args->id);
7050Sstevel@tonic-gate 
7060Sstevel@tonic-gate 	return (0);
7070Sstevel@tonic-gate }
7080Sstevel@tonic-gate 
7090Sstevel@tonic-gate int
svc_pool_control(int id,int cmd,void * arg)7100Sstevel@tonic-gate svc_pool_control(int id, int cmd, void *arg)
7110Sstevel@tonic-gate {
7120Sstevel@tonic-gate 	SVCPOOL *pool;
7130Sstevel@tonic-gate 	struct svc_globals *svc;
7140Sstevel@tonic-gate 
7150Sstevel@tonic-gate 	svc = zone_getspecific(svc_zone_key, curproc->p_zone);
7160Sstevel@tonic-gate 
7170Sstevel@tonic-gate 	switch (cmd) {
7180Sstevel@tonic-gate 	case SVCPSET_SHUTDOWN_PROC:
7190Sstevel@tonic-gate 		/*
7200Sstevel@tonic-gate 		 * Search the list for a pool with a matching id
7210Sstevel@tonic-gate 		 * and register the transport handle with that pool.
7220Sstevel@tonic-gate 		 */
7230Sstevel@tonic-gate 		mutex_enter(&svc->svc_plock);
7240Sstevel@tonic-gate 
7250Sstevel@tonic-gate 		if ((pool = svc_pool_find(svc, id)) == NULL) {
7260Sstevel@tonic-gate 			mutex_exit(&svc->svc_plock);
7270Sstevel@tonic-gate 			return (ENOENT);
7280Sstevel@tonic-gate 		}
7290Sstevel@tonic-gate 		/*
7300Sstevel@tonic-gate 		 * Grab the transport list lock before releasing the
7310Sstevel@tonic-gate 		 * pool list lock
7320Sstevel@tonic-gate 		 */
7330Sstevel@tonic-gate 		rw_enter(&pool->p_lrwlock, RW_WRITER);
7340Sstevel@tonic-gate 		mutex_exit(&svc->svc_plock);
7350Sstevel@tonic-gate 
7360Sstevel@tonic-gate 		pool->p_shutdown = *((void (*)())arg);
7370Sstevel@tonic-gate 
7380Sstevel@tonic-gate 		rw_exit(&pool->p_lrwlock);
7390Sstevel@tonic-gate 
7400Sstevel@tonic-gate 		return (0);
7410Sstevel@tonic-gate 	case SVCPSET_UNREGISTER_PROC:
7420Sstevel@tonic-gate 		/*
7430Sstevel@tonic-gate 		 * Search the list for a pool with a matching id
7440Sstevel@tonic-gate 		 * and register the unregister callback handle with that pool.
7450Sstevel@tonic-gate 		 */
7460Sstevel@tonic-gate 		mutex_enter(&svc->svc_plock);
7470Sstevel@tonic-gate 
7480Sstevel@tonic-gate 		if ((pool = svc_pool_find(svc, id)) == NULL) {
7490Sstevel@tonic-gate 			mutex_exit(&svc->svc_plock);
7500Sstevel@tonic-gate 			return (ENOENT);
7510Sstevel@tonic-gate 		}
7520Sstevel@tonic-gate 		/*
7530Sstevel@tonic-gate 		 * Grab the transport list lock before releasing the
7540Sstevel@tonic-gate 		 * pool list lock
7550Sstevel@tonic-gate 		 */
7560Sstevel@tonic-gate 		rw_enter(&pool->p_lrwlock, RW_WRITER);
7570Sstevel@tonic-gate 		mutex_exit(&svc->svc_plock);
7580Sstevel@tonic-gate 
7590Sstevel@tonic-gate 		pool->p_offline = *((void (*)())arg);
7600Sstevel@tonic-gate 
7610Sstevel@tonic-gate 		rw_exit(&pool->p_lrwlock);
7620Sstevel@tonic-gate 
7630Sstevel@tonic-gate 		return (0);
7640Sstevel@tonic-gate 	default:
7650Sstevel@tonic-gate 		return (EINVAL);
7660Sstevel@tonic-gate 	}
7670Sstevel@tonic-gate }
7680Sstevel@tonic-gate 
7690Sstevel@tonic-gate /*
7700Sstevel@tonic-gate  * Pool's transport list manipulation routines.
7710Sstevel@tonic-gate  * - svc_xprt_register()
7720Sstevel@tonic-gate  * - svc_xprt_unregister()
7730Sstevel@tonic-gate  *
7740Sstevel@tonic-gate  * svc_xprt_register() is called from svc_tli_kcreate() to
7750Sstevel@tonic-gate  * insert a new master transport handle into the doubly linked
7760Sstevel@tonic-gate  * list of server transport handles (one list per pool).
7770Sstevel@tonic-gate  *
7780Sstevel@tonic-gate  * The list is used by svc_poll(), when it operates in `drain'
7790Sstevel@tonic-gate  * mode, to search for a next transport with a pending request.
7800Sstevel@tonic-gate  */
7810Sstevel@tonic-gate 
7820Sstevel@tonic-gate int
svc_xprt_register(SVCMASTERXPRT * xprt,int id)7830Sstevel@tonic-gate svc_xprt_register(SVCMASTERXPRT *xprt, int id)
7840Sstevel@tonic-gate {
7850Sstevel@tonic-gate 	SVCMASTERXPRT *prev, *next;
7860Sstevel@tonic-gate 	SVCPOOL *pool;
7870Sstevel@tonic-gate 	struct svc_globals *svc;
7880Sstevel@tonic-gate 
7890Sstevel@tonic-gate 	svc = zone_getspecific(svc_zone_key, curproc->p_zone);
7900Sstevel@tonic-gate 	/*
7910Sstevel@tonic-gate 	 * Search the list for a pool with a matching id
7920Sstevel@tonic-gate 	 * and register the transport handle with that pool.
7930Sstevel@tonic-gate 	 */
7940Sstevel@tonic-gate 	mutex_enter(&svc->svc_plock);
7950Sstevel@tonic-gate 
7960Sstevel@tonic-gate 	if ((pool = svc_pool_find(svc, id)) == NULL) {
7970Sstevel@tonic-gate 		mutex_exit(&svc->svc_plock);
7980Sstevel@tonic-gate 		return (ENOENT);
7990Sstevel@tonic-gate 	}
8000Sstevel@tonic-gate 
8010Sstevel@tonic-gate 	/* Grab the transport list lock before releasing the pool list lock */
8020Sstevel@tonic-gate 	rw_enter(&pool->p_lrwlock, RW_WRITER);
8030Sstevel@tonic-gate 	mutex_exit(&svc->svc_plock);
8040Sstevel@tonic-gate 
8050Sstevel@tonic-gate 	/* Don't register new transports when the pool is in closing state */
8060Sstevel@tonic-gate 	if (pool->p_closing) {
8070Sstevel@tonic-gate 		rw_exit(&pool->p_lrwlock);
8080Sstevel@tonic-gate 		return (EBUSY);
8090Sstevel@tonic-gate 	}
8100Sstevel@tonic-gate 
8110Sstevel@tonic-gate 	/*
8120Sstevel@tonic-gate 	 * Initialize xp_pool to point to the pool.
8130Sstevel@tonic-gate 	 * We don't want to go through the pool list every time.
8140Sstevel@tonic-gate 	 */
8150Sstevel@tonic-gate 	xprt->xp_pool = pool;
8160Sstevel@tonic-gate 
8170Sstevel@tonic-gate 	/*
8180Sstevel@tonic-gate 	 * Insert a transport handle into the list.
8190Sstevel@tonic-gate 	 * The list head points to the most recently inserted transport.
8200Sstevel@tonic-gate 	 */
8210Sstevel@tonic-gate 	if (pool->p_lhead == NULL)
8220Sstevel@tonic-gate 		pool->p_lhead = xprt->xp_prev = xprt->xp_next = xprt;
8230Sstevel@tonic-gate 	else {
8240Sstevel@tonic-gate 		next = pool->p_lhead;
8250Sstevel@tonic-gate 		prev = pool->p_lhead->xp_prev;
8260Sstevel@tonic-gate 
8270Sstevel@tonic-gate 		xprt->xp_next = next;
8280Sstevel@tonic-gate 		xprt->xp_prev = prev;
8290Sstevel@tonic-gate 
8300Sstevel@tonic-gate 		pool->p_lhead = prev->xp_next = next->xp_prev = xprt;
8310Sstevel@tonic-gate 	}
8320Sstevel@tonic-gate 
8330Sstevel@tonic-gate 	/* Increment the transports count */
8340Sstevel@tonic-gate 	pool->p_lcount++;
8350Sstevel@tonic-gate 
8360Sstevel@tonic-gate 	rw_exit(&pool->p_lrwlock);
8370Sstevel@tonic-gate 	return (0);
8380Sstevel@tonic-gate }
8390Sstevel@tonic-gate 
8400Sstevel@tonic-gate /*
8410Sstevel@tonic-gate  * Called from svc_xprt_cleanup() to remove a master transport handle
8420Sstevel@tonic-gate  * from the pool's list of server transports (when a transport is
8430Sstevel@tonic-gate  * being destroyed).
8440Sstevel@tonic-gate  */
8450Sstevel@tonic-gate void
svc_xprt_unregister(SVCMASTERXPRT * xprt)8460Sstevel@tonic-gate svc_xprt_unregister(SVCMASTERXPRT *xprt)
8470Sstevel@tonic-gate {
8480Sstevel@tonic-gate 	SVCPOOL *pool = xprt->xp_pool;
8490Sstevel@tonic-gate 
8500Sstevel@tonic-gate 	/*
8510Sstevel@tonic-gate 	 * Unlink xprt from the list.
8520Sstevel@tonic-gate 	 * If the list head points to this xprt then move it
8530Sstevel@tonic-gate 	 * to the next xprt or reset to NULL if this is the last
8540Sstevel@tonic-gate 	 * xprt in the list.
8550Sstevel@tonic-gate 	 */
8560Sstevel@tonic-gate 	rw_enter(&pool->p_lrwlock, RW_WRITER);
8570Sstevel@tonic-gate 
8580Sstevel@tonic-gate 	if (xprt == xprt->xp_next)
8590Sstevel@tonic-gate 		pool->p_lhead = NULL;
8600Sstevel@tonic-gate 	else {
8610Sstevel@tonic-gate 		SVCMASTERXPRT *next = xprt->xp_next;
8620Sstevel@tonic-gate 		SVCMASTERXPRT *prev = xprt->xp_prev;
8630Sstevel@tonic-gate 
8640Sstevel@tonic-gate 		next->xp_prev = prev;
8650Sstevel@tonic-gate 		prev->xp_next = next;
8660Sstevel@tonic-gate 
8670Sstevel@tonic-gate 		if (pool->p_lhead == xprt)
8680Sstevel@tonic-gate 			pool->p_lhead = next;
8690Sstevel@tonic-gate 	}
8700Sstevel@tonic-gate 
8710Sstevel@tonic-gate 	xprt->xp_next = xprt->xp_prev = NULL;
8720Sstevel@tonic-gate 
8730Sstevel@tonic-gate 	/* Decrement list count */
8740Sstevel@tonic-gate 	pool->p_lcount--;
8750Sstevel@tonic-gate 
8760Sstevel@tonic-gate 	rw_exit(&pool->p_lrwlock);
8770Sstevel@tonic-gate }
8780Sstevel@tonic-gate 
8790Sstevel@tonic-gate static void
svc_xprt_qdestroy(SVCPOOL * pool)8800Sstevel@tonic-gate svc_xprt_qdestroy(SVCPOOL *pool)
8810Sstevel@tonic-gate {
8820Sstevel@tonic-gate 	mutex_destroy(&pool->p_qend_lock);
8830Sstevel@tonic-gate 	kmem_free(pool->p_qbody, pool->p_qsize * sizeof (__SVCXPRT_QNODE));
8840Sstevel@tonic-gate }
8850Sstevel@tonic-gate 
8860Sstevel@tonic-gate /*
8870Sstevel@tonic-gate  * Initialize an `xprt-ready' queue for a given pool.
8880Sstevel@tonic-gate  */
8890Sstevel@tonic-gate static void
svc_xprt_qinit(SVCPOOL * pool,size_t qsize)8900Sstevel@tonic-gate svc_xprt_qinit(SVCPOOL *pool, size_t qsize)
8910Sstevel@tonic-gate {
8920Sstevel@tonic-gate 	int i;
8930Sstevel@tonic-gate 
8940Sstevel@tonic-gate 	pool->p_qsize = qsize;
8950Sstevel@tonic-gate 	pool->p_qbody = kmem_zalloc(pool->p_qsize * sizeof (__SVCXPRT_QNODE),
8960Sstevel@tonic-gate 	    KM_SLEEP);
8970Sstevel@tonic-gate 
8980Sstevel@tonic-gate 	for (i = 0; i < pool->p_qsize - 1; i++)
8990Sstevel@tonic-gate 		pool->p_qbody[i].q_next = &(pool->p_qbody[i+1]);
9000Sstevel@tonic-gate 
9010Sstevel@tonic-gate 	pool->p_qbody[pool->p_qsize-1].q_next = &(pool->p_qbody[0]);
9020Sstevel@tonic-gate 	pool->p_qtop = &(pool->p_qbody[0]);
9030Sstevel@tonic-gate 	pool->p_qend = &(pool->p_qbody[0]);
9040Sstevel@tonic-gate 
9050Sstevel@tonic-gate 	mutex_init(&pool->p_qend_lock, NULL, MUTEX_DEFAULT, NULL);
9060Sstevel@tonic-gate }
9070Sstevel@tonic-gate 
9080Sstevel@tonic-gate /*
9090Sstevel@tonic-gate  * Called from the svc_queuereq() interrupt routine to queue
9100Sstevel@tonic-gate  * a hint for svc_poll() which transport has a pending request.
9110Sstevel@tonic-gate  * - insert a pointer to xprt into the xprt-ready queue (FIFO)
9120Sstevel@tonic-gate  * - if the xprt-ready queue is full turn the overflow flag on.
9130Sstevel@tonic-gate  *
9140Sstevel@tonic-gate  * NOTICE: pool->p_qtop is protected by the the pool's request lock
9150Sstevel@tonic-gate  * and the caller (svc_queuereq()) must hold the lock.
9160Sstevel@tonic-gate  */
9170Sstevel@tonic-gate static void
svc_xprt_qput(SVCPOOL * pool,SVCMASTERXPRT * xprt)9180Sstevel@tonic-gate svc_xprt_qput(SVCPOOL *pool, SVCMASTERXPRT *xprt)
9190Sstevel@tonic-gate {
9200Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pool->p_req_lock));
9210Sstevel@tonic-gate 
9220Sstevel@tonic-gate 	/* If the overflow flag is there is nothing we can do */
9230Sstevel@tonic-gate 	if (pool->p_qoverflow)
9240Sstevel@tonic-gate 		return;
9250Sstevel@tonic-gate 
9260Sstevel@tonic-gate 	/* If the queue is full turn the overflow flag on and exit */
9270Sstevel@tonic-gate 	if (pool->p_qtop->q_next == pool->p_qend) {
9280Sstevel@tonic-gate 		mutex_enter(&pool->p_qend_lock);
9290Sstevel@tonic-gate 		if (pool->p_qtop->q_next == pool->p_qend) {
9300Sstevel@tonic-gate 			pool->p_qoverflow = TRUE;
9310Sstevel@tonic-gate 			mutex_exit(&pool->p_qend_lock);
9320Sstevel@tonic-gate 			return;
9330Sstevel@tonic-gate 		}
9340Sstevel@tonic-gate 		mutex_exit(&pool->p_qend_lock);
9350Sstevel@tonic-gate 	}
9360Sstevel@tonic-gate 
9370Sstevel@tonic-gate 	/* Insert a hint and move pool->p_qtop */
9380Sstevel@tonic-gate 	pool->p_qtop->q_xprt = xprt;
9390Sstevel@tonic-gate 	pool->p_qtop = pool->p_qtop->q_next;
9400Sstevel@tonic-gate }
9410Sstevel@tonic-gate 
9420Sstevel@tonic-gate /*
9430Sstevel@tonic-gate  * Called from svc_poll() to get a hint which transport has a
9440Sstevel@tonic-gate  * pending request. Returns a pointer to a transport or NULL if the
9450Sstevel@tonic-gate  * `xprt-ready' queue is empty.
9460Sstevel@tonic-gate  *
9470Sstevel@tonic-gate  * Since we do not acquire the pool's request lock while checking if
9480Sstevel@tonic-gate  * the queue is empty we may miss a request that is just being delivered.
9490Sstevel@tonic-gate  * However this is ok since svc_poll() will retry again until the
9500Sstevel@tonic-gate  * count indicates that there are pending requests for this pool.
9510Sstevel@tonic-gate  */
9520Sstevel@tonic-gate static SVCMASTERXPRT *
svc_xprt_qget(SVCPOOL * pool)9530Sstevel@tonic-gate svc_xprt_qget(SVCPOOL *pool)
9540Sstevel@tonic-gate {
9550Sstevel@tonic-gate 	SVCMASTERXPRT *xprt;
9560Sstevel@tonic-gate 
9570Sstevel@tonic-gate 	mutex_enter(&pool->p_qend_lock);
9580Sstevel@tonic-gate 	do {
9590Sstevel@tonic-gate 		/*
9600Sstevel@tonic-gate 		 * If the queue is empty return NULL.
9610Sstevel@tonic-gate 		 * Since we do not acquire the pool's request lock which
9620Sstevel@tonic-gate 		 * protects pool->p_qtop this is not exact check. However,
9630Sstevel@tonic-gate 		 * this is safe - if we miss a request here svc_poll()
9640Sstevel@tonic-gate 		 * will retry again.
9650Sstevel@tonic-gate 		 */
9660Sstevel@tonic-gate 		if (pool->p_qend == pool->p_qtop) {
9670Sstevel@tonic-gate 			mutex_exit(&pool->p_qend_lock);
9680Sstevel@tonic-gate 			return (NULL);
9690Sstevel@tonic-gate 		}
9700Sstevel@tonic-gate 
9710Sstevel@tonic-gate 		/* Get a hint and move pool->p_qend */
9720Sstevel@tonic-gate 		xprt = pool->p_qend->q_xprt;
9730Sstevel@tonic-gate 		pool->p_qend = pool->p_qend->q_next;
9740Sstevel@tonic-gate 
9750Sstevel@tonic-gate 		/* Skip fields deleted by svc_xprt_qdelete()	 */
9760Sstevel@tonic-gate 	} while (xprt == NULL);
9770Sstevel@tonic-gate 	mutex_exit(&pool->p_qend_lock);
9780Sstevel@tonic-gate 
9790Sstevel@tonic-gate 	return (xprt);
9800Sstevel@tonic-gate }
9810Sstevel@tonic-gate 
9820Sstevel@tonic-gate /*
9830Sstevel@tonic-gate  * Delete all the references to a transport handle that
9840Sstevel@tonic-gate  * is being destroyed from the xprt-ready queue.
9850Sstevel@tonic-gate  * Deleted pointers are replaced with NULLs.
9860Sstevel@tonic-gate  */
9870Sstevel@tonic-gate static void
svc_xprt_qdelete(SVCPOOL * pool,SVCMASTERXPRT * xprt)9880Sstevel@tonic-gate svc_xprt_qdelete(SVCPOOL *pool, SVCMASTERXPRT *xprt)
9890Sstevel@tonic-gate {
9900Sstevel@tonic-gate 	__SVCXPRT_QNODE *q = pool->p_qend;
9910Sstevel@tonic-gate 	__SVCXPRT_QNODE *qtop = pool->p_qtop;
9920Sstevel@tonic-gate 
9930Sstevel@tonic-gate 	/*
9940Sstevel@tonic-gate 	 * Delete all the references to xprt between the current
9950Sstevel@tonic-gate 	 * position of pool->p_qend and current pool->p_qtop.
9960Sstevel@tonic-gate 	 */
9970Sstevel@tonic-gate 	for (;;) {
9980Sstevel@tonic-gate 		if (q->q_xprt == xprt)
9990Sstevel@tonic-gate 			q->q_xprt = NULL;
10000Sstevel@tonic-gate 		if (q == qtop)
10010Sstevel@tonic-gate 			return;
10020Sstevel@tonic-gate 		q = q->q_next;
10030Sstevel@tonic-gate 	}
10040Sstevel@tonic-gate }
10050Sstevel@tonic-gate 
10060Sstevel@tonic-gate /*
10070Sstevel@tonic-gate  * Destructor for a master server transport handle.
10080Sstevel@tonic-gate  * - if there are no more non-detached threads linked to this transport
10090Sstevel@tonic-gate  *   then, if requested, call xp_closeproc (we don't wait for detached
10100Sstevel@tonic-gate  *   threads linked to this transport to complete).
10110Sstevel@tonic-gate  * - if there are no more threads linked to this
10120Sstevel@tonic-gate  *   transport then
10130Sstevel@tonic-gate  *   a) remove references to this transport from the xprt-ready queue
10140Sstevel@tonic-gate  *   b) remove a reference to this transport from the pool's transport list
10150Sstevel@tonic-gate  *   c) call a transport specific `destroy' function
10160Sstevel@tonic-gate  *   d) cancel remaining thread reservations.
10170Sstevel@tonic-gate  *
10180Sstevel@tonic-gate  * NOTICE: Caller must hold the transport's thread lock.
10190Sstevel@tonic-gate  */
10200Sstevel@tonic-gate static void
svc_xprt_cleanup(SVCMASTERXPRT * xprt,bool_t detached)10210Sstevel@tonic-gate svc_xprt_cleanup(SVCMASTERXPRT *xprt, bool_t detached)
10220Sstevel@tonic-gate {
10230Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&xprt->xp_thread_lock));
10240Sstevel@tonic-gate 	ASSERT(xprt->xp_wq == NULL);
10250Sstevel@tonic-gate 
10260Sstevel@tonic-gate 	/*
10270Sstevel@tonic-gate 	 * If called from the last non-detached thread
10280Sstevel@tonic-gate 	 * it should call the closeproc on this transport.
10290Sstevel@tonic-gate 	 */
10300Sstevel@tonic-gate 	if (!detached && xprt->xp_threads == 0 && xprt->xp_closeproc) {
10310Sstevel@tonic-gate 		(*(xprt->xp_closeproc)) (xprt);
10320Sstevel@tonic-gate 	}
10330Sstevel@tonic-gate 
10340Sstevel@tonic-gate 	if (xprt->xp_threads + xprt->xp_detached_threads > 0)
10350Sstevel@tonic-gate 		mutex_exit(&xprt->xp_thread_lock);
10360Sstevel@tonic-gate 	else {
10370Sstevel@tonic-gate 		/* Remove references to xprt from the `xprt-ready' queue */
10380Sstevel@tonic-gate 		svc_xprt_qdelete(xprt->xp_pool, xprt);
10390Sstevel@tonic-gate 
10400Sstevel@tonic-gate 		/* Unregister xprt from the pool's transport list */
10410Sstevel@tonic-gate 		svc_xprt_unregister(xprt);
10420Sstevel@tonic-gate 		svc_callout_free(xprt);
10430Sstevel@tonic-gate 		SVC_DESTROY(xprt);
10440Sstevel@tonic-gate 	}
10450Sstevel@tonic-gate }
10460Sstevel@tonic-gate 
10470Sstevel@tonic-gate /*
10480Sstevel@tonic-gate  * Find a dispatch routine for a given prog/vers pair.
10490Sstevel@tonic-gate  * This function is called from svc_getreq() to search the callout
10500Sstevel@tonic-gate  * table for an entry with a matching RPC program number `prog'
10510Sstevel@tonic-gate  * and a version range that covers `vers'.
10520Sstevel@tonic-gate  * - if it finds a matching entry it returns pointer to the dispatch routine
10530Sstevel@tonic-gate  * - otherwise it returns NULL and, if `minp' or `maxp' are not NULL,
10540Sstevel@tonic-gate  *   fills them with, respectively, lowest version and highest version
10550Sstevel@tonic-gate  *   supported for the program `prog'
10560Sstevel@tonic-gate  */
10570Sstevel@tonic-gate static SVC_DISPATCH *
svc_callout_find(SVCXPRT * xprt,rpcprog_t prog,rpcvers_t vers,rpcvers_t * vers_min,rpcvers_t * vers_max)10580Sstevel@tonic-gate svc_callout_find(SVCXPRT *xprt, rpcprog_t prog, rpcvers_t vers,
10590Sstevel@tonic-gate     rpcvers_t *vers_min, rpcvers_t *vers_max)
10600Sstevel@tonic-gate {
10610Sstevel@tonic-gate 	SVC_CALLOUT_TABLE *sct = xprt->xp_sct;
10620Sstevel@tonic-gate 	int i;
10630Sstevel@tonic-gate 
10640Sstevel@tonic-gate 	*vers_min = ~(rpcvers_t)0;
10650Sstevel@tonic-gate 	*vers_max = 0;
10660Sstevel@tonic-gate 
10670Sstevel@tonic-gate 	for (i = 0; i < sct->sct_size; i++) {
10680Sstevel@tonic-gate 		SVC_CALLOUT *sc = &sct->sct_sc[i];
10690Sstevel@tonic-gate 
10700Sstevel@tonic-gate 		if (prog == sc->sc_prog) {
10710Sstevel@tonic-gate 			if (vers >= sc->sc_versmin && vers <= sc->sc_versmax)
10720Sstevel@tonic-gate 				return (sc->sc_dispatch);
10730Sstevel@tonic-gate 
10740Sstevel@tonic-gate 			if (*vers_max < sc->sc_versmax)
10750Sstevel@tonic-gate 				*vers_max = sc->sc_versmax;
10760Sstevel@tonic-gate 			if (*vers_min > sc->sc_versmin)
10770Sstevel@tonic-gate 				*vers_min = sc->sc_versmin;
10780Sstevel@tonic-gate 		}
10790Sstevel@tonic-gate 	}
10800Sstevel@tonic-gate 
10810Sstevel@tonic-gate 	return (NULL);
10820Sstevel@tonic-gate }
10830Sstevel@tonic-gate 
10840Sstevel@tonic-gate /*
10850Sstevel@tonic-gate  * Optionally free callout table allocated for this transport by
10860Sstevel@tonic-gate  * the service provider.
10870Sstevel@tonic-gate  */
10880Sstevel@tonic-gate static void
svc_callout_free(SVCMASTERXPRT * xprt)10890Sstevel@tonic-gate svc_callout_free(SVCMASTERXPRT *xprt)
10900Sstevel@tonic-gate {
10910Sstevel@tonic-gate 	SVC_CALLOUT_TABLE *sct = xprt->xp_sct;
10920Sstevel@tonic-gate 
10930Sstevel@tonic-gate 	if (sct->sct_free) {
10940Sstevel@tonic-gate 		kmem_free(sct->sct_sc, sct->sct_size * sizeof (SVC_CALLOUT));
10950Sstevel@tonic-gate 		kmem_free(sct, sizeof (SVC_CALLOUT_TABLE));
10960Sstevel@tonic-gate 	}
10970Sstevel@tonic-gate }
10980Sstevel@tonic-gate 
10990Sstevel@tonic-gate /*
11000Sstevel@tonic-gate  * Send a reply to an RPC request
11010Sstevel@tonic-gate  *
11020Sstevel@tonic-gate  * PSARC 2003/523 Contract Private Interface
11030Sstevel@tonic-gate  * svc_sendreply
11040Sstevel@tonic-gate  * Changes must be reviewed by Solaris File Sharing
11050Sstevel@tonic-gate  * Changes must be communicated to contract-2003-523@sun.com
11060Sstevel@tonic-gate  */
11070Sstevel@tonic-gate bool_t
svc_sendreply(const SVCXPRT * clone_xprt,const xdrproc_t xdr_results,const caddr_t xdr_location)11080Sstevel@tonic-gate svc_sendreply(const SVCXPRT *clone_xprt, const xdrproc_t xdr_results,
11090Sstevel@tonic-gate     const caddr_t xdr_location)
11100Sstevel@tonic-gate {
11110Sstevel@tonic-gate 	struct rpc_msg rply;
11120Sstevel@tonic-gate 
11130Sstevel@tonic-gate 	rply.rm_direction = REPLY;
11140Sstevel@tonic-gate 	rply.rm_reply.rp_stat = MSG_ACCEPTED;
11150Sstevel@tonic-gate 	rply.acpted_rply.ar_verf = clone_xprt->xp_verf;
11160Sstevel@tonic-gate 	rply.acpted_rply.ar_stat = SUCCESS;
11170Sstevel@tonic-gate 	rply.acpted_rply.ar_results.where = xdr_location;
11180Sstevel@tonic-gate 	rply.acpted_rply.ar_results.proc = xdr_results;
11190Sstevel@tonic-gate 
11200Sstevel@tonic-gate 	return (SVC_REPLY((SVCXPRT *)clone_xprt, &rply));
11210Sstevel@tonic-gate }
11220Sstevel@tonic-gate 
11230Sstevel@tonic-gate /*
11240Sstevel@tonic-gate  * No procedure error reply
11250Sstevel@tonic-gate  *
11260Sstevel@tonic-gate  * PSARC 2003/523 Contract Private Interface
11270Sstevel@tonic-gate  * svcerr_noproc
11280Sstevel@tonic-gate  * Changes must be reviewed by Solaris File Sharing
11290Sstevel@tonic-gate  * Changes must be communicated to contract-2003-523@sun.com
11300Sstevel@tonic-gate  */
11310Sstevel@tonic-gate void
svcerr_noproc(const SVCXPRT * clone_xprt)11320Sstevel@tonic-gate svcerr_noproc(const SVCXPRT *clone_xprt)
11330Sstevel@tonic-gate {
11340Sstevel@tonic-gate 	struct rpc_msg rply;
11350Sstevel@tonic-gate 
11360Sstevel@tonic-gate 	rply.rm_direction = REPLY;
11370Sstevel@tonic-gate 	rply.rm_reply.rp_stat = MSG_ACCEPTED;
11380Sstevel@tonic-gate 	rply.acpted_rply.ar_verf = clone_xprt->xp_verf;
11390Sstevel@tonic-gate 	rply.acpted_rply.ar_stat = PROC_UNAVAIL;
11400Sstevel@tonic-gate 	SVC_FREERES((SVCXPRT *)clone_xprt);
11410Sstevel@tonic-gate 	SVC_REPLY((SVCXPRT *)clone_xprt, &rply);
11420Sstevel@tonic-gate }
11430Sstevel@tonic-gate 
11440Sstevel@tonic-gate /*
11450Sstevel@tonic-gate  * Can't decode arguments error reply
11460Sstevel@tonic-gate  *
11470Sstevel@tonic-gate  * PSARC 2003/523 Contract Private Interface
11480Sstevel@tonic-gate  * svcerr_decode
11490Sstevel@tonic-gate  * Changes must be reviewed by Solaris File Sharing
11500Sstevel@tonic-gate  * Changes must be communicated to contract-2003-523@sun.com
11510Sstevel@tonic-gate  */
11520Sstevel@tonic-gate void
svcerr_decode(const SVCXPRT * clone_xprt)11530Sstevel@tonic-gate svcerr_decode(const SVCXPRT *clone_xprt)
11540Sstevel@tonic-gate {
11550Sstevel@tonic-gate 	struct rpc_msg rply;
11560Sstevel@tonic-gate 
11570Sstevel@tonic-gate 	rply.rm_direction = REPLY;
11580Sstevel@tonic-gate 	rply.rm_reply.rp_stat = MSG_ACCEPTED;
11590Sstevel@tonic-gate 	rply.acpted_rply.ar_verf = clone_xprt->xp_verf;
11600Sstevel@tonic-gate 	rply.acpted_rply.ar_stat = GARBAGE_ARGS;
11610Sstevel@tonic-gate 	SVC_FREERES((SVCXPRT *)clone_xprt);
11620Sstevel@tonic-gate 	SVC_REPLY((SVCXPRT *)clone_xprt, &rply);
11630Sstevel@tonic-gate }
11640Sstevel@tonic-gate 
11650Sstevel@tonic-gate /*
11660Sstevel@tonic-gate  * Some system error
11670Sstevel@tonic-gate  */
11680Sstevel@tonic-gate void
svcerr_systemerr(const SVCXPRT * clone_xprt)11690Sstevel@tonic-gate svcerr_systemerr(const SVCXPRT *clone_xprt)
11700Sstevel@tonic-gate {
11710Sstevel@tonic-gate 	struct rpc_msg rply;
11720Sstevel@tonic-gate 
11730Sstevel@tonic-gate 	rply.rm_direction = REPLY;
11740Sstevel@tonic-gate 	rply.rm_reply.rp_stat = MSG_ACCEPTED;
11750Sstevel@tonic-gate 	rply.acpted_rply.ar_verf = clone_xprt->xp_verf;
11760Sstevel@tonic-gate 	rply.acpted_rply.ar_stat = SYSTEM_ERR;
11770Sstevel@tonic-gate 	SVC_FREERES((SVCXPRT *)clone_xprt);
11780Sstevel@tonic-gate 	SVC_REPLY((SVCXPRT *)clone_xprt, &rply);
11790Sstevel@tonic-gate }
11800Sstevel@tonic-gate 
11810Sstevel@tonic-gate /*
11820Sstevel@tonic-gate  * Authentication error reply
11830Sstevel@tonic-gate  */
11840Sstevel@tonic-gate void
svcerr_auth(const SVCXPRT * clone_xprt,const enum auth_stat why)11850Sstevel@tonic-gate svcerr_auth(const SVCXPRT *clone_xprt, const enum auth_stat why)
11860Sstevel@tonic-gate {
11870Sstevel@tonic-gate 	struct rpc_msg rply;
11880Sstevel@tonic-gate 
11890Sstevel@tonic-gate 	rply.rm_direction = REPLY;
11900Sstevel@tonic-gate 	rply.rm_reply.rp_stat = MSG_DENIED;
11910Sstevel@tonic-gate 	rply.rjcted_rply.rj_stat = AUTH_ERROR;
11920Sstevel@tonic-gate 	rply.rjcted_rply.rj_why = why;
11930Sstevel@tonic-gate 	SVC_FREERES((SVCXPRT *)clone_xprt);
11940Sstevel@tonic-gate 	SVC_REPLY((SVCXPRT *)clone_xprt, &rply);
11950Sstevel@tonic-gate }
11960Sstevel@tonic-gate 
11970Sstevel@tonic-gate /*
11980Sstevel@tonic-gate  * Authentication too weak error reply
11990Sstevel@tonic-gate  */
12000Sstevel@tonic-gate void
svcerr_weakauth(const SVCXPRT * clone_xprt)12010Sstevel@tonic-gate svcerr_weakauth(const SVCXPRT *clone_xprt)
12020Sstevel@tonic-gate {
12030Sstevel@tonic-gate 	svcerr_auth((SVCXPRT *)clone_xprt, AUTH_TOOWEAK);
12040Sstevel@tonic-gate }
12050Sstevel@tonic-gate 
12060Sstevel@tonic-gate /*
12076786Srmesta  * Authentication error; bad credentials
12086786Srmesta  */
12096786Srmesta void
svcerr_badcred(const SVCXPRT * clone_xprt)12106786Srmesta svcerr_badcred(const SVCXPRT *clone_xprt)
12116786Srmesta {
12126786Srmesta 	struct rpc_msg rply;
12136786Srmesta 
12146786Srmesta 	rply.rm_direction = REPLY;
12156786Srmesta 	rply.rm_reply.rp_stat = MSG_DENIED;
12166786Srmesta 	rply.rjcted_rply.rj_stat = AUTH_ERROR;
12176786Srmesta 	rply.rjcted_rply.rj_why = AUTH_BADCRED;
12186786Srmesta 	SVC_FREERES((SVCXPRT *)clone_xprt);
12196786Srmesta 	SVC_REPLY((SVCXPRT *)clone_xprt, &rply);
12206786Srmesta }
12216786Srmesta 
12226786Srmesta /*
12230Sstevel@tonic-gate  * Program unavailable error reply
12240Sstevel@tonic-gate  *
12250Sstevel@tonic-gate  * PSARC 2003/523 Contract Private Interface
12260Sstevel@tonic-gate  * svcerr_noprog
12270Sstevel@tonic-gate  * Changes must be reviewed by Solaris File Sharing
12280Sstevel@tonic-gate  * Changes must be communicated to contract-2003-523@sun.com
12290Sstevel@tonic-gate  */
12300Sstevel@tonic-gate void
svcerr_noprog(const SVCXPRT * clone_xprt)12310Sstevel@tonic-gate svcerr_noprog(const SVCXPRT *clone_xprt)
12320Sstevel@tonic-gate {
12330Sstevel@tonic-gate 	struct rpc_msg rply;
12340Sstevel@tonic-gate 
12350Sstevel@tonic-gate 	rply.rm_direction = REPLY;
12360Sstevel@tonic-gate 	rply.rm_reply.rp_stat = MSG_ACCEPTED;
12370Sstevel@tonic-gate 	rply.acpted_rply.ar_verf = clone_xprt->xp_verf;
12380Sstevel@tonic-gate 	rply.acpted_rply.ar_stat = PROG_UNAVAIL;
12390Sstevel@tonic-gate 	SVC_FREERES((SVCXPRT *)clone_xprt);
12400Sstevel@tonic-gate 	SVC_REPLY((SVCXPRT *)clone_xprt, &rply);
12410Sstevel@tonic-gate }
12420Sstevel@tonic-gate 
12430Sstevel@tonic-gate /*
12440Sstevel@tonic-gate  * Program version mismatch error reply
12450Sstevel@tonic-gate  *
12460Sstevel@tonic-gate  * PSARC 2003/523 Contract Private Interface
12470Sstevel@tonic-gate  * svcerr_progvers
12480Sstevel@tonic-gate  * Changes must be reviewed by Solaris File Sharing
12490Sstevel@tonic-gate  * Changes must be communicated to contract-2003-523@sun.com
12500Sstevel@tonic-gate  */
12510Sstevel@tonic-gate void
svcerr_progvers(const SVCXPRT * clone_xprt,const rpcvers_t low_vers,const rpcvers_t high_vers)12520Sstevel@tonic-gate svcerr_progvers(const SVCXPRT *clone_xprt,
12530Sstevel@tonic-gate     const rpcvers_t low_vers, const rpcvers_t high_vers)
12540Sstevel@tonic-gate {
12550Sstevel@tonic-gate 	struct rpc_msg rply;
12560Sstevel@tonic-gate 
12570Sstevel@tonic-gate 	rply.rm_direction = REPLY;
12580Sstevel@tonic-gate 	rply.rm_reply.rp_stat = MSG_ACCEPTED;
12590Sstevel@tonic-gate 	rply.acpted_rply.ar_verf = clone_xprt->xp_verf;
12600Sstevel@tonic-gate 	rply.acpted_rply.ar_stat = PROG_MISMATCH;
12610Sstevel@tonic-gate 	rply.acpted_rply.ar_vers.low = low_vers;
12620Sstevel@tonic-gate 	rply.acpted_rply.ar_vers.high = high_vers;
12630Sstevel@tonic-gate 	SVC_FREERES((SVCXPRT *)clone_xprt);
12640Sstevel@tonic-gate 	SVC_REPLY((SVCXPRT *)clone_xprt, &rply);
12650Sstevel@tonic-gate }
12660Sstevel@tonic-gate 
12670Sstevel@tonic-gate /*
12680Sstevel@tonic-gate  * Get server side input from some transport.
12690Sstevel@tonic-gate  *
12700Sstevel@tonic-gate  * Statement of authentication parameters management:
12710Sstevel@tonic-gate  * This function owns and manages all authentication parameters, specifically
12720Sstevel@tonic-gate  * the "raw" parameters (msg.rm_call.cb_cred and msg.rm_call.cb_verf) and
12730Sstevel@tonic-gate  * the "cooked" credentials (rqst->rq_clntcred).
12740Sstevel@tonic-gate  * However, this function does not know the structure of the cooked
12750Sstevel@tonic-gate  * credentials, so it make the following assumptions:
12760Sstevel@tonic-gate  *   a) the structure is contiguous (no pointers), and
12770Sstevel@tonic-gate  *   b) the cred structure size does not exceed RQCRED_SIZE bytes.
12780Sstevel@tonic-gate  * In all events, all three parameters are freed upon exit from this routine.
12790Sstevel@tonic-gate  * The storage is trivially managed on the call stack in user land, but
12800Sstevel@tonic-gate  * is malloced in kernel land.
12810Sstevel@tonic-gate  *
12820Sstevel@tonic-gate  * Note: the xprt's xp_svc_lock is not held while the service's dispatch
12830Sstevel@tonic-gate  * routine is running.	If we decide to implement svc_unregister(), we'll
12840Sstevel@tonic-gate  * need to decide whether it's okay for a thread to unregister a service
12850Sstevel@tonic-gate  * while a request is being processed.	If we decide that this is a
12860Sstevel@tonic-gate  * problem, we can probably use some sort of reference counting scheme to
12870Sstevel@tonic-gate  * keep the callout entry from going away until the request has completed.
12880Sstevel@tonic-gate  */
12890Sstevel@tonic-gate static void
svc_getreq(SVCXPRT * clone_xprt,mblk_t * mp)12900Sstevel@tonic-gate svc_getreq(
12910Sstevel@tonic-gate 	SVCXPRT *clone_xprt,	/* clone transport handle */
12920Sstevel@tonic-gate 	mblk_t *mp)
12930Sstevel@tonic-gate {
12940Sstevel@tonic-gate 	struct rpc_msg msg;
12950Sstevel@tonic-gate 	struct svc_req r;
12960Sstevel@tonic-gate 	char  *cred_area;	/* too big to allocate on call stack */
12970Sstevel@tonic-gate 
12980Sstevel@tonic-gate 	TRACE_0(TR_FAC_KRPC, TR_SVC_GETREQ_START,
12990Sstevel@tonic-gate 	    "svc_getreq_start:");
13000Sstevel@tonic-gate 
13010Sstevel@tonic-gate 	ASSERT(clone_xprt->xp_master != NULL);
13028778SErik.Nordmark@Sun.COM 	ASSERT(!is_system_labeled() || msg_getcred(mp, NULL) != NULL ||
13031676Sjpk 	    mp->b_datap->db_type != M_DATA);
13040Sstevel@tonic-gate 
13050Sstevel@tonic-gate 	/*
13060Sstevel@tonic-gate 	 * Firstly, allocate the authentication parameters' storage
13070Sstevel@tonic-gate 	 */
13080Sstevel@tonic-gate 	mutex_enter(&rqcred_lock);
13090Sstevel@tonic-gate 	if (rqcred_head) {
13100Sstevel@tonic-gate 		cred_area = rqcred_head;
13110Sstevel@tonic-gate 
13120Sstevel@tonic-gate 		/* LINTED pointer alignment */
13130Sstevel@tonic-gate 		rqcred_head = *(caddr_t *)rqcred_head;
13140Sstevel@tonic-gate 		mutex_exit(&rqcred_lock);
13150Sstevel@tonic-gate 	} else {
13160Sstevel@tonic-gate 		mutex_exit(&rqcred_lock);
13170Sstevel@tonic-gate 		cred_area = kmem_alloc(2 * MAX_AUTH_BYTES + RQCRED_SIZE,
13180Sstevel@tonic-gate 		    KM_SLEEP);
13190Sstevel@tonic-gate 	}
13200Sstevel@tonic-gate 	msg.rm_call.cb_cred.oa_base = cred_area;
13210Sstevel@tonic-gate 	msg.rm_call.cb_verf.oa_base = &(cred_area[MAX_AUTH_BYTES]);
13220Sstevel@tonic-gate 	r.rq_clntcred = &(cred_area[2 * MAX_AUTH_BYTES]);
13230Sstevel@tonic-gate 
13240Sstevel@tonic-gate 	/*
13251676Sjpk 	 * underlying transport recv routine may modify mblk data
13261676Sjpk 	 * and make it difficult to extract label afterwards. So
13271676Sjpk 	 * get the label from the raw mblk data now.
13281676Sjpk 	 */
13291676Sjpk 	if (is_system_labeled()) {
13308778SErik.Nordmark@Sun.COM 		cred_t *cr;
13311676Sjpk 
13321676Sjpk 		r.rq_label = kmem_alloc(sizeof (bslabel_t), KM_SLEEP);
13338778SErik.Nordmark@Sun.COM 		cr = msg_getcred(mp, NULL);
13348778SErik.Nordmark@Sun.COM 		ASSERT(cr != NULL);
13358778SErik.Nordmark@Sun.COM 
13368778SErik.Nordmark@Sun.COM 		bcopy(label2bslabel(crgetlabel(cr)), r.rq_label,
13371676Sjpk 		    sizeof (bslabel_t));
13381676Sjpk 	} else {
13391676Sjpk 		r.rq_label = NULL;
13401676Sjpk 	}
13411676Sjpk 
13421676Sjpk 	/*
13430Sstevel@tonic-gate 	 * Now receive a message from the transport.
13440Sstevel@tonic-gate 	 */
13450Sstevel@tonic-gate 	if (SVC_RECV(clone_xprt, mp, &msg)) {
13460Sstevel@tonic-gate 		void (*dispatchroutine) (struct svc_req *, SVCXPRT *);
13470Sstevel@tonic-gate 		rpcvers_t vers_min;
13480Sstevel@tonic-gate 		rpcvers_t vers_max;
13490Sstevel@tonic-gate 		bool_t no_dispatch;
13500Sstevel@tonic-gate 		enum auth_stat why;
13510Sstevel@tonic-gate 
13520Sstevel@tonic-gate 		/*
13530Sstevel@tonic-gate 		 * Find the registered program and call its
13540Sstevel@tonic-gate 		 * dispatch routine.
13550Sstevel@tonic-gate 		 */
13560Sstevel@tonic-gate 		r.rq_xprt = clone_xprt;
13570Sstevel@tonic-gate 		r.rq_prog = msg.rm_call.cb_prog;
13580Sstevel@tonic-gate 		r.rq_vers = msg.rm_call.cb_vers;
13590Sstevel@tonic-gate 		r.rq_proc = msg.rm_call.cb_proc;
13600Sstevel@tonic-gate 		r.rq_cred = msg.rm_call.cb_cred;
13610Sstevel@tonic-gate 
13620Sstevel@tonic-gate 		/*
13630Sstevel@tonic-gate 		 * First authenticate the message.
13640Sstevel@tonic-gate 		 */
13650Sstevel@tonic-gate 		TRACE_0(TR_FAC_KRPC, TR_SVC_GETREQ_AUTH_START,
13660Sstevel@tonic-gate 		    "svc_getreq_auth_start:");
13670Sstevel@tonic-gate 		if ((why = sec_svc_msg(&r, &msg, &no_dispatch)) != AUTH_OK) {
13680Sstevel@tonic-gate 			TRACE_1(TR_FAC_KRPC, TR_SVC_GETREQ_AUTH_END,
13690Sstevel@tonic-gate 			    "svc_getreq_auth_end:(%S)", "failed");
13700Sstevel@tonic-gate 			svcerr_auth(clone_xprt, why);
13710Sstevel@tonic-gate 			/*
13720Sstevel@tonic-gate 			 * Free the arguments.
13730Sstevel@tonic-gate 			 */
13740Sstevel@tonic-gate 			(void) SVC_FREEARGS(clone_xprt, NULL, NULL);
13750Sstevel@tonic-gate 		} else if (no_dispatch) {
13760Sstevel@tonic-gate 			/*
13770Sstevel@tonic-gate 			 * XXX - when bug id 4053736 is done, remove
13780Sstevel@tonic-gate 			 * the SVC_FREEARGS() call.
13790Sstevel@tonic-gate 			 */
13800Sstevel@tonic-gate 			(void) SVC_FREEARGS(clone_xprt, NULL, NULL);
13810Sstevel@tonic-gate 		} else {
13820Sstevel@tonic-gate 			TRACE_1(TR_FAC_KRPC, TR_SVC_GETREQ_AUTH_END,
13830Sstevel@tonic-gate 			    "svc_getreq_auth_end:(%S)", "good");
13840Sstevel@tonic-gate 
13850Sstevel@tonic-gate 			dispatchroutine = svc_callout_find(clone_xprt,
13860Sstevel@tonic-gate 			    r.rq_prog, r.rq_vers, &vers_min, &vers_max);
13870Sstevel@tonic-gate 
13880Sstevel@tonic-gate 			if (dispatchroutine) {
13890Sstevel@tonic-gate 				(*dispatchroutine) (&r, clone_xprt);
13900Sstevel@tonic-gate 			} else {
13910Sstevel@tonic-gate 				/*
13920Sstevel@tonic-gate 				 * If we got here, the program or version
13930Sstevel@tonic-gate 				 * is not served ...
13940Sstevel@tonic-gate 				 */
13950Sstevel@tonic-gate 				if (vers_max == 0 ||
13960Sstevel@tonic-gate 				    version_keepquiet(clone_xprt))
13970Sstevel@tonic-gate 					svcerr_noprog(clone_xprt);
13980Sstevel@tonic-gate 				else
13990Sstevel@tonic-gate 					svcerr_progvers(clone_xprt, vers_min,
14000Sstevel@tonic-gate 					    vers_max);
14010Sstevel@tonic-gate 
14020Sstevel@tonic-gate 				/*
14030Sstevel@tonic-gate 				 * Free the arguments. For successful calls
14040Sstevel@tonic-gate 				 * this is done by the dispatch routine.
14050Sstevel@tonic-gate 				 */
14060Sstevel@tonic-gate 				(void) SVC_FREEARGS(clone_xprt, NULL, NULL);
14070Sstevel@tonic-gate 				/* Fall through to ... */
14080Sstevel@tonic-gate 			}
14090Sstevel@tonic-gate 			/*
14100Sstevel@tonic-gate 			 * Call cleanup procedure for RPCSEC_GSS.
14110Sstevel@tonic-gate 			 * This is a hack since there is currently no
14120Sstevel@tonic-gate 			 * op, such as SVC_CLEANAUTH. rpc_gss_cleanup
14130Sstevel@tonic-gate 			 * should only be called for a non null proc.
14140Sstevel@tonic-gate 			 * Null procs in RPC GSS are overloaded to
14150Sstevel@tonic-gate 			 * provide context setup and control. The main
14160Sstevel@tonic-gate 			 * purpose of rpc_gss_cleanup is to decrement the
14170Sstevel@tonic-gate 			 * reference count associated with the cached
14180Sstevel@tonic-gate 			 * GSS security context. We should never get here
14190Sstevel@tonic-gate 			 * for an RPCSEC_GSS null proc since *no_dispatch
14200Sstevel@tonic-gate 			 * would have been set to true from sec_svc_msg above.
14210Sstevel@tonic-gate 			 */
14220Sstevel@tonic-gate 			if (r.rq_cred.oa_flavor == RPCSEC_GSS)
14230Sstevel@tonic-gate 				rpc_gss_cleanup(clone_xprt);
14240Sstevel@tonic-gate 		}
14250Sstevel@tonic-gate 	}
14260Sstevel@tonic-gate 
14271676Sjpk 	if (r.rq_label != NULL)
14281676Sjpk 		kmem_free(r.rq_label, sizeof (bslabel_t));
14291676Sjpk 
14300Sstevel@tonic-gate 	/*
14310Sstevel@tonic-gate 	 * Free authentication parameters' storage
14320Sstevel@tonic-gate 	 */
14330Sstevel@tonic-gate 	mutex_enter(&rqcred_lock);
14340Sstevel@tonic-gate 	/* LINTED pointer alignment */
14350Sstevel@tonic-gate 	*(caddr_t *)cred_area = rqcred_head;
14360Sstevel@tonic-gate 	rqcred_head = cred_area;
14370Sstevel@tonic-gate 	mutex_exit(&rqcred_lock);
14380Sstevel@tonic-gate }
14390Sstevel@tonic-gate 
14400Sstevel@tonic-gate /*
14410Sstevel@tonic-gate  * Allocate new clone transport handle.
14420Sstevel@tonic-gate  */
144310721SGlenn.Barry@Sun.COM SVCXPRT *
svc_clone_init(void)14440Sstevel@tonic-gate svc_clone_init(void)
14450Sstevel@tonic-gate {
14460Sstevel@tonic-gate 	SVCXPRT *clone_xprt;
14470Sstevel@tonic-gate 
14480Sstevel@tonic-gate 	clone_xprt = kmem_zalloc(sizeof (SVCXPRT), KM_SLEEP);
14490Sstevel@tonic-gate 	clone_xprt->xp_cred = crget();
14500Sstevel@tonic-gate 	return (clone_xprt);
14510Sstevel@tonic-gate }
14520Sstevel@tonic-gate 
14530Sstevel@tonic-gate /*
14540Sstevel@tonic-gate  * Free memory allocated by svc_clone_init.
14550Sstevel@tonic-gate  */
145610721SGlenn.Barry@Sun.COM void
svc_clone_free(SVCXPRT * clone_xprt)14570Sstevel@tonic-gate svc_clone_free(SVCXPRT *clone_xprt)
14580Sstevel@tonic-gate {
14590Sstevel@tonic-gate 	/* Fre credentials from crget() */
14600Sstevel@tonic-gate 	if (clone_xprt->xp_cred)
14610Sstevel@tonic-gate 		crfree(clone_xprt->xp_cred);
14620Sstevel@tonic-gate 	kmem_free(clone_xprt, sizeof (SVCXPRT));
14630Sstevel@tonic-gate }
14640Sstevel@tonic-gate 
14650Sstevel@tonic-gate /*
14660Sstevel@tonic-gate  * Link a per-thread clone transport handle to a master
14670Sstevel@tonic-gate  * - increment a thread reference count on the master
14680Sstevel@tonic-gate  * - copy some of the master's fields to the clone
14690Sstevel@tonic-gate  * - call a transport specific clone routine.
14700Sstevel@tonic-gate  */
147110721SGlenn.Barry@Sun.COM void
svc_clone_link(SVCMASTERXPRT * xprt,SVCXPRT * clone_xprt,SVCXPRT * clone_xprt2)1472*11967SKaren.Rochford@Sun.COM svc_clone_link(SVCMASTERXPRT *xprt, SVCXPRT *clone_xprt, SVCXPRT *clone_xprt2)
14730Sstevel@tonic-gate {
14740Sstevel@tonic-gate 	cred_t *cred = clone_xprt->xp_cred;
14750Sstevel@tonic-gate 
14760Sstevel@tonic-gate 	ASSERT(cred);
14770Sstevel@tonic-gate 
14780Sstevel@tonic-gate 	/*
14790Sstevel@tonic-gate 	 * Bump up master's thread count.
14800Sstevel@tonic-gate 	 * Linking a per-thread clone transport handle to a master
14810Sstevel@tonic-gate 	 * associates a service thread with the master.
14820Sstevel@tonic-gate 	 */
14830Sstevel@tonic-gate 	mutex_enter(&xprt->xp_thread_lock);
14840Sstevel@tonic-gate 	xprt->xp_threads++;
14850Sstevel@tonic-gate 	mutex_exit(&xprt->xp_thread_lock);
14860Sstevel@tonic-gate 
14870Sstevel@tonic-gate 	/* Clear everything */
14880Sstevel@tonic-gate 	bzero(clone_xprt, sizeof (SVCXPRT));
14890Sstevel@tonic-gate 
14900Sstevel@tonic-gate 	/* Set pointer to the master transport stucture */
14910Sstevel@tonic-gate 	clone_xprt->xp_master = xprt;
14920Sstevel@tonic-gate 
14930Sstevel@tonic-gate 	/* Structure copy of all the common fields */
14940Sstevel@tonic-gate 	clone_xprt->xp_xpc = xprt->xp_xpc;
14950Sstevel@tonic-gate 
14960Sstevel@tonic-gate 	/* Restore per-thread fields (xp_cred) */
14970Sstevel@tonic-gate 	clone_xprt->xp_cred = cred;
14980Sstevel@tonic-gate 
1499*11967SKaren.Rochford@Sun.COM 	if (clone_xprt2)
1500*11967SKaren.Rochford@Sun.COM 		SVC_CLONE_XPRT(clone_xprt2, clone_xprt);
15010Sstevel@tonic-gate }
15020Sstevel@tonic-gate 
15030Sstevel@tonic-gate /*
15040Sstevel@tonic-gate  * Unlink a non-detached clone transport handle from a master
15050Sstevel@tonic-gate  * - decrement a thread reference count on the master
15060Sstevel@tonic-gate  * - if the transport is closing (xp_wq is NULL) call svc_xprt_cleanup();
15070Sstevel@tonic-gate  *   if this is the last non-detached/absolute thread on this transport
15080Sstevel@tonic-gate  *   then it will close/destroy the transport
15090Sstevel@tonic-gate  * - call transport specific function to destroy the clone handle
15100Sstevel@tonic-gate  * - clear xp_master to avoid recursion.
15110Sstevel@tonic-gate  */
151210721SGlenn.Barry@Sun.COM void
svc_clone_unlink(SVCXPRT * clone_xprt)15130Sstevel@tonic-gate svc_clone_unlink(SVCXPRT *clone_xprt)
15140Sstevel@tonic-gate {
15150Sstevel@tonic-gate 	SVCMASTERXPRT *xprt = clone_xprt->xp_master;
15160Sstevel@tonic-gate 
15170Sstevel@tonic-gate 	/* This cannot be a detached thread */
15180Sstevel@tonic-gate 	ASSERT(!clone_xprt->xp_detached);
15190Sstevel@tonic-gate 	ASSERT(xprt->xp_threads > 0);
15200Sstevel@tonic-gate 
15210Sstevel@tonic-gate 	/* Decrement a reference count on the transport */
15220Sstevel@tonic-gate 	mutex_enter(&xprt->xp_thread_lock);
15230Sstevel@tonic-gate 	xprt->xp_threads--;
15240Sstevel@tonic-gate 
15250Sstevel@tonic-gate 	/* svc_xprt_cleanup() unlocks xp_thread_lock or destroys xprt */
15260Sstevel@tonic-gate 	if (xprt->xp_wq)
15270Sstevel@tonic-gate 		mutex_exit(&xprt->xp_thread_lock);
15280Sstevel@tonic-gate 	else
15290Sstevel@tonic-gate 		svc_xprt_cleanup(xprt, FALSE);
15300Sstevel@tonic-gate 
15310Sstevel@tonic-gate 	/* Call a transport specific clone `destroy' function */
15320Sstevel@tonic-gate 	SVC_CLONE_DESTROY(clone_xprt);
15330Sstevel@tonic-gate 
15340Sstevel@tonic-gate 	/* Clear xp_master */
15350Sstevel@tonic-gate 	clone_xprt->xp_master = NULL;
15360Sstevel@tonic-gate }
15370Sstevel@tonic-gate 
15380Sstevel@tonic-gate /*
15390Sstevel@tonic-gate  * Unlink a detached clone transport handle from a master
15400Sstevel@tonic-gate  * - decrement the thread count on the master
15410Sstevel@tonic-gate  * - if the transport is closing (xp_wq is NULL) call svc_xprt_cleanup();
15420Sstevel@tonic-gate  *   if this is the last thread on this transport then it will destroy
15430Sstevel@tonic-gate  *   the transport.
15440Sstevel@tonic-gate  * - call a transport specific function to destroy the clone handle
15450Sstevel@tonic-gate  * - clear xp_master to avoid recursion.
15460Sstevel@tonic-gate  */
15470Sstevel@tonic-gate static void
svc_clone_unlinkdetached(SVCXPRT * clone_xprt)15480Sstevel@tonic-gate svc_clone_unlinkdetached(SVCXPRT *clone_xprt)
15490Sstevel@tonic-gate {
15500Sstevel@tonic-gate 	SVCMASTERXPRT *xprt = clone_xprt->xp_master;
15510Sstevel@tonic-gate 
15520Sstevel@tonic-gate 	/* This must be a detached thread */
15530Sstevel@tonic-gate 	ASSERT(clone_xprt->xp_detached);
15540Sstevel@tonic-gate 	ASSERT(xprt->xp_detached_threads > 0);
15550Sstevel@tonic-gate 	ASSERT(xprt->xp_threads + xprt->xp_detached_threads > 0);
15560Sstevel@tonic-gate 
15570Sstevel@tonic-gate 	/* Grab xprt->xp_thread_lock and decrement link counts */
15580Sstevel@tonic-gate 	mutex_enter(&xprt->xp_thread_lock);
15590Sstevel@tonic-gate 	xprt->xp_detached_threads--;
15600Sstevel@tonic-gate 
15610Sstevel@tonic-gate 	/* svc_xprt_cleanup() unlocks xp_thread_lock or destroys xprt */
15620Sstevel@tonic-gate 	if (xprt->xp_wq)
15630Sstevel@tonic-gate 		mutex_exit(&xprt->xp_thread_lock);
15640Sstevel@tonic-gate 	else
15650Sstevel@tonic-gate 		svc_xprt_cleanup(xprt, TRUE);
15660Sstevel@tonic-gate 
15670Sstevel@tonic-gate 	/* Call transport specific clone `destroy' function */
15680Sstevel@tonic-gate 	SVC_CLONE_DESTROY(clone_xprt);
15690Sstevel@tonic-gate 
15700Sstevel@tonic-gate 	/* Clear xp_master */
15710Sstevel@tonic-gate 	clone_xprt->xp_master = NULL;
15720Sstevel@tonic-gate }
15730Sstevel@tonic-gate 
15740Sstevel@tonic-gate /*
15750Sstevel@tonic-gate  * Try to exit a non-detached service thread
15760Sstevel@tonic-gate  * - check if there are enough threads left
15770Sstevel@tonic-gate  * - if this thread (ie its clone transport handle) are linked
15780Sstevel@tonic-gate  *   to a master transport then unlink it
15790Sstevel@tonic-gate  * - free the clone structure
15800Sstevel@tonic-gate  * - return to userland for thread exit
15810Sstevel@tonic-gate  *
15820Sstevel@tonic-gate  * If this is the last non-detached or the last thread on this
15830Sstevel@tonic-gate  * transport then the call to svc_clone_unlink() will, respectively,
15840Sstevel@tonic-gate  * close and/or destroy the transport.
15850Sstevel@tonic-gate  */
15860Sstevel@tonic-gate static void
svc_thread_exit(SVCPOOL * pool,SVCXPRT * clone_xprt)15870Sstevel@tonic-gate svc_thread_exit(SVCPOOL *pool, SVCXPRT *clone_xprt)
15880Sstevel@tonic-gate {
15890Sstevel@tonic-gate 	if (clone_xprt->xp_master)
15900Sstevel@tonic-gate 		svc_clone_unlink(clone_xprt);
15910Sstevel@tonic-gate 	svc_clone_free(clone_xprt);
15920Sstevel@tonic-gate 
15930Sstevel@tonic-gate 	mutex_enter(&pool->p_thread_lock);
15940Sstevel@tonic-gate 	pool->p_threads--;
15950Sstevel@tonic-gate 	if (pool->p_closing && svc_pool_tryexit(pool))
15960Sstevel@tonic-gate 		/* return -  thread exit will be handled at user level */
15970Sstevel@tonic-gate 		return;
15980Sstevel@tonic-gate 	mutex_exit(&pool->p_thread_lock);
15990Sstevel@tonic-gate 
16000Sstevel@tonic-gate 	/* return -  thread exit will be handled at user level */
16010Sstevel@tonic-gate }
16020Sstevel@tonic-gate 
16030Sstevel@tonic-gate /*
16040Sstevel@tonic-gate  * Exit a detached service thread that returned to svc_run
16050Sstevel@tonic-gate  * - decrement the `detached thread' count for the pool
16060Sstevel@tonic-gate  * - unlink the detached clone transport handle from the master
16070Sstevel@tonic-gate  * - free the clone structure
16080Sstevel@tonic-gate  * - return to userland for thread exit
16090Sstevel@tonic-gate  *
16100Sstevel@tonic-gate  * If this is the last thread on this transport then the call
16110Sstevel@tonic-gate  * to svc_clone_unlinkdetached() will destroy the transport.
16120Sstevel@tonic-gate  */
16130Sstevel@tonic-gate static void
svc_thread_exitdetached(SVCPOOL * pool,SVCXPRT * clone_xprt)16140Sstevel@tonic-gate svc_thread_exitdetached(SVCPOOL *pool, SVCXPRT *clone_xprt)
16150Sstevel@tonic-gate {
16160Sstevel@tonic-gate 	/* This must be a detached thread */
16170Sstevel@tonic-gate 	ASSERT(clone_xprt->xp_master);
16180Sstevel@tonic-gate 	ASSERT(clone_xprt->xp_detached);
16190Sstevel@tonic-gate 	ASSERT(!MUTEX_HELD(&pool->p_thread_lock));
16200Sstevel@tonic-gate 
16210Sstevel@tonic-gate 	svc_clone_unlinkdetached(clone_xprt);
16220Sstevel@tonic-gate 	svc_clone_free(clone_xprt);
16230Sstevel@tonic-gate 
16240Sstevel@tonic-gate 	mutex_enter(&pool->p_thread_lock);
16250Sstevel@tonic-gate 
16260Sstevel@tonic-gate 	ASSERT(pool->p_reserved_threads >= 0);
16270Sstevel@tonic-gate 	ASSERT(pool->p_detached_threads > 0);
16280Sstevel@tonic-gate 
16290Sstevel@tonic-gate 	pool->p_detached_threads--;
16300Sstevel@tonic-gate 	if (pool->p_closing && svc_pool_tryexit(pool))
16310Sstevel@tonic-gate 		/* return -  thread exit will be handled at user level */
16320Sstevel@tonic-gate 		return;
16330Sstevel@tonic-gate 	mutex_exit(&pool->p_thread_lock);
16340Sstevel@tonic-gate 
16350Sstevel@tonic-gate 	/* return -  thread exit will be handled at user level */
16360Sstevel@tonic-gate }
16370Sstevel@tonic-gate 
16380Sstevel@tonic-gate /*
16390Sstevel@tonic-gate  * PSARC 2003/523 Contract Private Interface
16400Sstevel@tonic-gate  * svc_wait
16410Sstevel@tonic-gate  * Changes must be reviewed by Solaris File Sharing
16420Sstevel@tonic-gate  * Changes must be communicated to contract-2003-523@sun.com
16430Sstevel@tonic-gate  */
16440Sstevel@tonic-gate int
svc_wait(int id)16450Sstevel@tonic-gate svc_wait(int id)
16460Sstevel@tonic-gate {
16470Sstevel@tonic-gate 	SVCPOOL *pool;
16480Sstevel@tonic-gate 	int	err = 0;
16490Sstevel@tonic-gate 	struct svc_globals *svc;
16500Sstevel@tonic-gate 
16510Sstevel@tonic-gate 	svc = zone_getspecific(svc_zone_key, curproc->p_zone);
16520Sstevel@tonic-gate 	mutex_enter(&svc->svc_plock);
16530Sstevel@tonic-gate 	pool = svc_pool_find(svc, id);
16540Sstevel@tonic-gate 	mutex_exit(&svc->svc_plock);
16550Sstevel@tonic-gate 
16560Sstevel@tonic-gate 	if (pool == NULL)
16570Sstevel@tonic-gate 		return (ENOENT);
16580Sstevel@tonic-gate 
16590Sstevel@tonic-gate 	mutex_enter(&pool->p_user_lock);
16600Sstevel@tonic-gate 
16610Sstevel@tonic-gate 	/* Check if there's already a user thread waiting on this pool */
16620Sstevel@tonic-gate 	if (pool->p_user_waiting) {
16630Sstevel@tonic-gate 		mutex_exit(&pool->p_user_lock);
16640Sstevel@tonic-gate 		return (EBUSY);
16650Sstevel@tonic-gate 	}
16660Sstevel@tonic-gate 
16670Sstevel@tonic-gate 	pool->p_user_waiting = TRUE;
16680Sstevel@tonic-gate 
16690Sstevel@tonic-gate 	/* Go to sleep, waiting for the signaled flag. */
16700Sstevel@tonic-gate 	while (!pool->p_signal_create_thread && !pool->p_user_exit) {
16710Sstevel@tonic-gate 		if (cv_wait_sig(&pool->p_user_cv, &pool->p_user_lock) == 0) {
16720Sstevel@tonic-gate 			/* Interrupted, return to handle exit or signal */
16730Sstevel@tonic-gate 			pool->p_user_waiting = FALSE;
16740Sstevel@tonic-gate 			pool->p_signal_create_thread = FALSE;
16750Sstevel@tonic-gate 			mutex_exit(&pool->p_user_lock);
16760Sstevel@tonic-gate 
16770Sstevel@tonic-gate 			/*
16780Sstevel@tonic-gate 			 * Thread has been interrupted and therefore
16790Sstevel@tonic-gate 			 * the service daemon is leaving as well so
16800Sstevel@tonic-gate 			 * let's go ahead and remove the service
16810Sstevel@tonic-gate 			 * pool at this time.
16820Sstevel@tonic-gate 			 */
16830Sstevel@tonic-gate 			mutex_enter(&svc->svc_plock);
16840Sstevel@tonic-gate 			svc_pool_unregister(svc, pool);
16850Sstevel@tonic-gate 			mutex_exit(&svc->svc_plock);
16860Sstevel@tonic-gate 
16870Sstevel@tonic-gate 			return (EINTR);
16880Sstevel@tonic-gate 		}
16890Sstevel@tonic-gate 	}
16900Sstevel@tonic-gate 
16910Sstevel@tonic-gate 	pool->p_signal_create_thread = FALSE;
16920Sstevel@tonic-gate 	pool->p_user_waiting = FALSE;
16930Sstevel@tonic-gate 
16940Sstevel@tonic-gate 	/*
16950Sstevel@tonic-gate 	 * About to exit the service pool. Set return value
16960Sstevel@tonic-gate 	 * to let the userland code know our intent. Signal
16970Sstevel@tonic-gate 	 * svc_thread_creator() so that it can clean up the
16980Sstevel@tonic-gate 	 * pool structure.
16990Sstevel@tonic-gate 	 */
17000Sstevel@tonic-gate 	if (pool->p_user_exit) {
17010Sstevel@tonic-gate 		err = ECANCELED;
17020Sstevel@tonic-gate 		cv_signal(&pool->p_user_cv);
17030Sstevel@tonic-gate 	}
17040Sstevel@tonic-gate 
17050Sstevel@tonic-gate 	mutex_exit(&pool->p_user_lock);
17060Sstevel@tonic-gate 
17070Sstevel@tonic-gate 	/* Return to userland with error code, for possible thread creation. */
17080Sstevel@tonic-gate 	return (err);
17090Sstevel@tonic-gate }
17100Sstevel@tonic-gate 
17110Sstevel@tonic-gate /*
17120Sstevel@tonic-gate  * `Service threads' creator thread.
17130Sstevel@tonic-gate  * The creator thread waits for a signal to create new thread.
17140Sstevel@tonic-gate  */
17150Sstevel@tonic-gate static void
svc_thread_creator(SVCPOOL * pool)17160Sstevel@tonic-gate svc_thread_creator(SVCPOOL *pool)
17170Sstevel@tonic-gate {
17180Sstevel@tonic-gate 	callb_cpr_t cpr_info;	/* CPR info for the creator thread */
17190Sstevel@tonic-gate 
17200Sstevel@tonic-gate 	CALLB_CPR_INIT(&cpr_info, &pool->p_creator_lock, callb_generic_cpr,
17210Sstevel@tonic-gate 	    "svc_thread_creator");
17220Sstevel@tonic-gate 
17230Sstevel@tonic-gate 	for (;;) {
17240Sstevel@tonic-gate 		mutex_enter(&pool->p_creator_lock);
17250Sstevel@tonic-gate 
17260Sstevel@tonic-gate 		/* Check if someone set the exit flag */
17270Sstevel@tonic-gate 		if (pool->p_creator_exit)
17280Sstevel@tonic-gate 			break;
17290Sstevel@tonic-gate 
17300Sstevel@tonic-gate 		/* Clear the `signaled' flag and go asleep */
17310Sstevel@tonic-gate 		pool->p_creator_signaled = FALSE;
17320Sstevel@tonic-gate 
17330Sstevel@tonic-gate 		CALLB_CPR_SAFE_BEGIN(&cpr_info);
17340Sstevel@tonic-gate 		cv_wait(&pool->p_creator_cv, &pool->p_creator_lock);
17350Sstevel@tonic-gate 		CALLB_CPR_SAFE_END(&cpr_info, &pool->p_creator_lock);
17360Sstevel@tonic-gate 
17370Sstevel@tonic-gate 		/* Check if someone signaled to exit */
17380Sstevel@tonic-gate 		if (pool->p_creator_exit)
17390Sstevel@tonic-gate 			break;
17400Sstevel@tonic-gate 
17410Sstevel@tonic-gate 		mutex_exit(&pool->p_creator_lock);
17420Sstevel@tonic-gate 
17430Sstevel@tonic-gate 		mutex_enter(&pool->p_thread_lock);
17440Sstevel@tonic-gate 
17450Sstevel@tonic-gate 		/*
17460Sstevel@tonic-gate 		 * When the pool is in closing state and all the transports
17470Sstevel@tonic-gate 		 * are gone the creator should not create any new threads.
17480Sstevel@tonic-gate 		 */
17490Sstevel@tonic-gate 		if (pool->p_closing) {
17500Sstevel@tonic-gate 			rw_enter(&pool->p_lrwlock, RW_READER);
17510Sstevel@tonic-gate 			if (pool->p_lcount == 0) {
17520Sstevel@tonic-gate 				rw_exit(&pool->p_lrwlock);
17530Sstevel@tonic-gate 				mutex_exit(&pool->p_thread_lock);
17540Sstevel@tonic-gate 				continue;
17550Sstevel@tonic-gate 			}
17560Sstevel@tonic-gate 			rw_exit(&pool->p_lrwlock);
17570Sstevel@tonic-gate 		}
17580Sstevel@tonic-gate 
17590Sstevel@tonic-gate 		/*
17600Sstevel@tonic-gate 		 * Create a new service thread now.
17610Sstevel@tonic-gate 		 */
17620Sstevel@tonic-gate 		ASSERT(pool->p_reserved_threads >= 0);
17630Sstevel@tonic-gate 		ASSERT(pool->p_detached_threads >= 0);
17640Sstevel@tonic-gate 
17650Sstevel@tonic-gate 		if (pool->p_threads + pool->p_detached_threads <
17660Sstevel@tonic-gate 		    pool->p_maxthreads) {
17670Sstevel@tonic-gate 			/*
17680Sstevel@tonic-gate 			 * Signal the service pool wait thread
17690Sstevel@tonic-gate 			 * only if it hasn't already been signaled.
17700Sstevel@tonic-gate 			 */
17710Sstevel@tonic-gate 			mutex_enter(&pool->p_user_lock);
17720Sstevel@tonic-gate 			if (pool->p_signal_create_thread == FALSE) {
17730Sstevel@tonic-gate 				pool->p_signal_create_thread = TRUE;
17740Sstevel@tonic-gate 				cv_signal(&pool->p_user_cv);
17750Sstevel@tonic-gate 			}
17760Sstevel@tonic-gate 			mutex_exit(&pool->p_user_lock);
17770Sstevel@tonic-gate 
17780Sstevel@tonic-gate 		}
17790Sstevel@tonic-gate 
17800Sstevel@tonic-gate 		mutex_exit(&pool->p_thread_lock);
17810Sstevel@tonic-gate 	}
17820Sstevel@tonic-gate 
17830Sstevel@tonic-gate 	/*
17840Sstevel@tonic-gate 	 * Pool is closed. Cleanup and exit.
17850Sstevel@tonic-gate 	 */
17860Sstevel@tonic-gate 
17870Sstevel@tonic-gate 	/* Signal userland creator thread that it can stop now. */
17880Sstevel@tonic-gate 	mutex_enter(&pool->p_user_lock);
17890Sstevel@tonic-gate 	pool->p_user_exit = TRUE;
17900Sstevel@tonic-gate 	cv_broadcast(&pool->p_user_cv);
17910Sstevel@tonic-gate 	mutex_exit(&pool->p_user_lock);
17920Sstevel@tonic-gate 
17930Sstevel@tonic-gate 	/* Wait for svc_wait() to be done with the pool */
17940Sstevel@tonic-gate 	mutex_enter(&pool->p_user_lock);
17950Sstevel@tonic-gate 	while (pool->p_user_waiting) {
17960Sstevel@tonic-gate 		CALLB_CPR_SAFE_BEGIN(&cpr_info);
17970Sstevel@tonic-gate 		cv_wait(&pool->p_user_cv, &pool->p_user_lock);
17980Sstevel@tonic-gate 		CALLB_CPR_SAFE_END(&cpr_info, &pool->p_creator_lock);
17990Sstevel@tonic-gate 	}
18000Sstevel@tonic-gate 	mutex_exit(&pool->p_user_lock);
18010Sstevel@tonic-gate 
18020Sstevel@tonic-gate 	CALLB_CPR_EXIT(&cpr_info);
18030Sstevel@tonic-gate 	svc_pool_cleanup(pool);
18040Sstevel@tonic-gate 	zthread_exit();
18050Sstevel@tonic-gate }
18060Sstevel@tonic-gate 
18070Sstevel@tonic-gate /*
18080Sstevel@tonic-gate  * If the creator thread  is idle signal it to create
18090Sstevel@tonic-gate  * a new service thread.
18100Sstevel@tonic-gate  */
18110Sstevel@tonic-gate static void
svc_creator_signal(SVCPOOL * pool)18120Sstevel@tonic-gate svc_creator_signal(SVCPOOL *pool)
18130Sstevel@tonic-gate {
18140Sstevel@tonic-gate 	mutex_enter(&pool->p_creator_lock);
18150Sstevel@tonic-gate 	if (pool->p_creator_signaled == FALSE) {
18160Sstevel@tonic-gate 		pool->p_creator_signaled = TRUE;
18170Sstevel@tonic-gate 		cv_signal(&pool->p_creator_cv);
18180Sstevel@tonic-gate 	}
18190Sstevel@tonic-gate 	mutex_exit(&pool->p_creator_lock);
18200Sstevel@tonic-gate }
18210Sstevel@tonic-gate 
18220Sstevel@tonic-gate /*
18230Sstevel@tonic-gate  * Notify the creator thread to clean up and exit.
18240Sstevel@tonic-gate  */
18250Sstevel@tonic-gate static void
svc_creator_signalexit(SVCPOOL * pool)18260Sstevel@tonic-gate svc_creator_signalexit(SVCPOOL *pool)
18270Sstevel@tonic-gate {
18280Sstevel@tonic-gate 	mutex_enter(&pool->p_creator_lock);
18290Sstevel@tonic-gate 	pool->p_creator_exit = TRUE;
18300Sstevel@tonic-gate 	cv_signal(&pool->p_creator_cv);
18310Sstevel@tonic-gate 	mutex_exit(&pool->p_creator_lock);
18320Sstevel@tonic-gate }
18330Sstevel@tonic-gate 
18340Sstevel@tonic-gate /*
18350Sstevel@tonic-gate  * Polling part of the svc_run().
18360Sstevel@tonic-gate  * - search for a transport with a pending request
18370Sstevel@tonic-gate  * - when one is found then latch the request lock and return to svc_run()
18380Sstevel@tonic-gate  * - if there is no request go asleep and wait for a signal
18390Sstevel@tonic-gate  * - handle two exceptions:
18400Sstevel@tonic-gate  *   a) current transport is closing
18410Sstevel@tonic-gate  *   b) timeout waiting for a new request
18420Sstevel@tonic-gate  *   in both cases return to svc_run()
18430Sstevel@tonic-gate  */
18440Sstevel@tonic-gate static SVCMASTERXPRT *
svc_poll(SVCPOOL * pool,SVCMASTERXPRT * xprt,SVCXPRT * clone_xprt)18450Sstevel@tonic-gate svc_poll(SVCPOOL *pool, SVCMASTERXPRT *xprt, SVCXPRT *clone_xprt)
18460Sstevel@tonic-gate {
18470Sstevel@tonic-gate 	/*
18480Sstevel@tonic-gate 	 * Main loop iterates until
18490Sstevel@tonic-gate 	 * a) we find a pending request,
18500Sstevel@tonic-gate 	 * b) detect that the current transport is closing
18510Sstevel@tonic-gate 	 * c) time out waiting for a new request.
18520Sstevel@tonic-gate 	 */
18530Sstevel@tonic-gate 	for (;;) {
18540Sstevel@tonic-gate 		SVCMASTERXPRT *next;
18550Sstevel@tonic-gate 		clock_t timeleft;
18560Sstevel@tonic-gate 
18570Sstevel@tonic-gate 		/*
18580Sstevel@tonic-gate 		 * Step 1.
18590Sstevel@tonic-gate 		 * Check if there is a pending request on the current
18600Sstevel@tonic-gate 		 * transport handle so that we can avoid cloning.
18610Sstevel@tonic-gate 		 * If so then decrement the `pending-request' count for
18620Sstevel@tonic-gate 		 * the pool and return to svc_run().
18630Sstevel@tonic-gate 		 *
18640Sstevel@tonic-gate 		 * We need to prevent a potential starvation. When
18650Sstevel@tonic-gate 		 * a selected transport has all pending requests coming in
18660Sstevel@tonic-gate 		 * all the time then the service threads will never switch to
18670Sstevel@tonic-gate 		 * another transport. With a limited number of service
18680Sstevel@tonic-gate 		 * threads some transports may be never serviced.
18690Sstevel@tonic-gate 		 * To prevent such a scenario we pick up at most
18700Sstevel@tonic-gate 		 * pool->p_max_same_xprt requests from the same transport
18710Sstevel@tonic-gate 		 * and then take a hint from the xprt-ready queue or walk
18720Sstevel@tonic-gate 		 * the transport list.
18730Sstevel@tonic-gate 		 */
18740Sstevel@tonic-gate 		if (xprt && xprt->xp_req_head && (!pool->p_qoverflow ||
18750Sstevel@tonic-gate 		    clone_xprt->xp_same_xprt++ < pool->p_max_same_xprt)) {
18760Sstevel@tonic-gate 			mutex_enter(&xprt->xp_req_lock);
18770Sstevel@tonic-gate 			if (xprt->xp_req_head) {
18780Sstevel@tonic-gate 				mutex_enter(&pool->p_req_lock);
18790Sstevel@tonic-gate 				pool->p_reqs--;
18804741Sgt29601 				if (pool->p_reqs == 0)
18814741Sgt29601 					pool->p_qoverflow = FALSE;
18820Sstevel@tonic-gate 				mutex_exit(&pool->p_req_lock);
18830Sstevel@tonic-gate 
18840Sstevel@tonic-gate 				return (xprt);
18850Sstevel@tonic-gate 			}
18860Sstevel@tonic-gate 			mutex_exit(&xprt->xp_req_lock);
18870Sstevel@tonic-gate 		}
18880Sstevel@tonic-gate 		clone_xprt->xp_same_xprt = 0;
18890Sstevel@tonic-gate 
18900Sstevel@tonic-gate 		/*
18910Sstevel@tonic-gate 		 * Step 2.
18920Sstevel@tonic-gate 		 * If there is no request on the current transport try to
18930Sstevel@tonic-gate 		 * find another transport with a pending request.
18940Sstevel@tonic-gate 		 */
18950Sstevel@tonic-gate 		mutex_enter(&pool->p_req_lock);
18960Sstevel@tonic-gate 		pool->p_walkers++;
18970Sstevel@tonic-gate 		mutex_exit(&pool->p_req_lock);
18980Sstevel@tonic-gate 
18990Sstevel@tonic-gate 		/*
19000Sstevel@tonic-gate 		 * Make sure that transports will not be destroyed just
19010Sstevel@tonic-gate 		 * while we are checking them.
19020Sstevel@tonic-gate 		 */
19030Sstevel@tonic-gate 		rw_enter(&pool->p_lrwlock, RW_READER);
19040Sstevel@tonic-gate 
19050Sstevel@tonic-gate 		for (;;) {
19060Sstevel@tonic-gate 			SVCMASTERXPRT *hint;
19070Sstevel@tonic-gate 
19080Sstevel@tonic-gate 			/*
19090Sstevel@tonic-gate 			 * Get the next transport from the xprt-ready queue.
19100Sstevel@tonic-gate 			 * This is a hint. There is no guarantee that the
19110Sstevel@tonic-gate 			 * transport still has a pending request since it
19120Sstevel@tonic-gate 			 * could be picked up by another thread in step 1.
19130Sstevel@tonic-gate 			 *
19140Sstevel@tonic-gate 			 * If the transport has a pending request then keep
19150Sstevel@tonic-gate 			 * it locked. Decrement the `pending-requests' for
19160Sstevel@tonic-gate 			 * the pool and `walking-threads' counts, and return
19170Sstevel@tonic-gate 			 * to svc_run().
19180Sstevel@tonic-gate 			 */
19190Sstevel@tonic-gate 			hint = svc_xprt_qget(pool);
19200Sstevel@tonic-gate 
19210Sstevel@tonic-gate 			if (hint && hint->xp_req_head) {
19220Sstevel@tonic-gate 				mutex_enter(&hint->xp_req_lock);
19230Sstevel@tonic-gate 				if (hint->xp_req_head) {
19240Sstevel@tonic-gate 					rw_exit(&pool->p_lrwlock);
19250Sstevel@tonic-gate 
19260Sstevel@tonic-gate 					mutex_enter(&pool->p_req_lock);
19270Sstevel@tonic-gate 					pool->p_reqs--;
19284741Sgt29601 					if (pool->p_reqs == 0)
19294741Sgt29601 						pool->p_qoverflow = FALSE;
19300Sstevel@tonic-gate 					pool->p_walkers--;
19310Sstevel@tonic-gate 					mutex_exit(&pool->p_req_lock);
19320Sstevel@tonic-gate 
19330Sstevel@tonic-gate 					return (hint);
19340Sstevel@tonic-gate 				}
19350Sstevel@tonic-gate 				mutex_exit(&hint->xp_req_lock);
19360Sstevel@tonic-gate 			}
19370Sstevel@tonic-gate 
19380Sstevel@tonic-gate 			/*
19390Sstevel@tonic-gate 			 * If there was no hint in the xprt-ready queue then
19400Sstevel@tonic-gate 			 * - if there is less pending requests than polling
19410Sstevel@tonic-gate 			 *   threads go asleep
19420Sstevel@tonic-gate 			 * - otherwise check if there was an overflow in the
19430Sstevel@tonic-gate 			 *   xprt-ready queue; if so, then we need to break
19440Sstevel@tonic-gate 			 *   the `drain' mode
19450Sstevel@tonic-gate 			 */
19460Sstevel@tonic-gate 			if (hint == NULL) {
19470Sstevel@tonic-gate 				if (pool->p_reqs < pool->p_walkers) {
19480Sstevel@tonic-gate 					mutex_enter(&pool->p_req_lock);
19490Sstevel@tonic-gate 					if (pool->p_reqs < pool->p_walkers)
19500Sstevel@tonic-gate 						goto sleep;
19510Sstevel@tonic-gate 					mutex_exit(&pool->p_req_lock);
19520Sstevel@tonic-gate 				}
19530Sstevel@tonic-gate 				if (pool->p_qoverflow) {
19540Sstevel@tonic-gate 					break;
19550Sstevel@tonic-gate 				}
19560Sstevel@tonic-gate 			}
19570Sstevel@tonic-gate 		}
19580Sstevel@tonic-gate 
19590Sstevel@tonic-gate 		/*
19600Sstevel@tonic-gate 		 * If there was an overflow in the xprt-ready queue then we
19610Sstevel@tonic-gate 		 * need to switch to the `drain' mode, i.e. walk through the
19620Sstevel@tonic-gate 		 * pool's transport list and search for a transport with a
19630Sstevel@tonic-gate 		 * pending request. If we manage to drain all the pending
19640Sstevel@tonic-gate 		 * requests then we can clear the overflow flag. This will
19650Sstevel@tonic-gate 		 * switch svc_poll() back to taking hints from the xprt-ready
19660Sstevel@tonic-gate 		 * queue (which is generally more efficient).
19670Sstevel@tonic-gate 		 *
19680Sstevel@tonic-gate 		 * If there are no registered transports simply go asleep.
19690Sstevel@tonic-gate 		 */
19700Sstevel@tonic-gate 		if (xprt == NULL && pool->p_lhead == NULL) {
19710Sstevel@tonic-gate 			mutex_enter(&pool->p_req_lock);
19720Sstevel@tonic-gate 			goto sleep;
19730Sstevel@tonic-gate 		}
19740Sstevel@tonic-gate 
19750Sstevel@tonic-gate 		/*
19760Sstevel@tonic-gate 		 * `Walk' through the pool's list of master server
19770Sstevel@tonic-gate 		 * transport handles. Continue to loop until there are less
19780Sstevel@tonic-gate 		 * looping threads then pending requests.
19790Sstevel@tonic-gate 		 */
19800Sstevel@tonic-gate 		next = xprt ? xprt->xp_next : pool->p_lhead;
19810Sstevel@tonic-gate 
19820Sstevel@tonic-gate 		for (;;) {
19830Sstevel@tonic-gate 			/*
19840Sstevel@tonic-gate 			 * Check if there is a request on this transport.
19850Sstevel@tonic-gate 			 *
19860Sstevel@tonic-gate 			 * Since blocking on a locked mutex is very expensive
19870Sstevel@tonic-gate 			 * check for a request without a lock first. If we miss
19880Sstevel@tonic-gate 			 * a request that is just being delivered but this will
19890Sstevel@tonic-gate 			 * cost at most one full walk through the list.
19900Sstevel@tonic-gate 			 */
19910Sstevel@tonic-gate 			if (next->xp_req_head) {
19920Sstevel@tonic-gate 				/*
19930Sstevel@tonic-gate 				 * Check again, now with a lock.
19940Sstevel@tonic-gate 				 */
19950Sstevel@tonic-gate 				mutex_enter(&next->xp_req_lock);
19960Sstevel@tonic-gate 				if (next->xp_req_head) {
19970Sstevel@tonic-gate 					rw_exit(&pool->p_lrwlock);
19980Sstevel@tonic-gate 
19990Sstevel@tonic-gate 					mutex_enter(&pool->p_req_lock);
20000Sstevel@tonic-gate 					pool->p_reqs--;
20014741Sgt29601 					if (pool->p_reqs == 0)
20024741Sgt29601 						pool->p_qoverflow = FALSE;
20030Sstevel@tonic-gate 					pool->p_walkers--;
20040Sstevel@tonic-gate 					mutex_exit(&pool->p_req_lock);
20050Sstevel@tonic-gate 
20060Sstevel@tonic-gate 					return (next);
20070Sstevel@tonic-gate 				}
20080Sstevel@tonic-gate 				mutex_exit(&next->xp_req_lock);
20090Sstevel@tonic-gate 			}
20100Sstevel@tonic-gate 
20110Sstevel@tonic-gate 			/*
20120Sstevel@tonic-gate 			 * Continue to `walk' through the pool's
20130Sstevel@tonic-gate 			 * transport list until there is less requests
20140Sstevel@tonic-gate 			 * than walkers. Check this condition without
20150Sstevel@tonic-gate 			 * a lock first to avoid contention on a mutex.
20160Sstevel@tonic-gate 			 */
20170Sstevel@tonic-gate 			if (pool->p_reqs < pool->p_walkers) {
20184741Sgt29601 				/* Check again, now with the lock. */
20190Sstevel@tonic-gate 				mutex_enter(&pool->p_req_lock);
20200Sstevel@tonic-gate 				if (pool->p_reqs < pool->p_walkers)
20210Sstevel@tonic-gate 					break;	/* goto sleep */
20220Sstevel@tonic-gate 				mutex_exit(&pool->p_req_lock);
20230Sstevel@tonic-gate 			}
20240Sstevel@tonic-gate 
20250Sstevel@tonic-gate 			next = next->xp_next;
20260Sstevel@tonic-gate 		}
20270Sstevel@tonic-gate 
20280Sstevel@tonic-gate 	sleep:
20290Sstevel@tonic-gate 		/*
20300Sstevel@tonic-gate 		 * No work to do. Stop the `walk' and go asleep.
20310Sstevel@tonic-gate 		 * Decrement the `walking-threads' count for the pool.
20320Sstevel@tonic-gate 		 */
20330Sstevel@tonic-gate 		pool->p_walkers--;
20340Sstevel@tonic-gate 		rw_exit(&pool->p_lrwlock);
20350Sstevel@tonic-gate 
20360Sstevel@tonic-gate 		/*
20370Sstevel@tonic-gate 		 * Count us as asleep, mark this thread as safe
20380Sstevel@tonic-gate 		 * for suspend and wait for a request.
20390Sstevel@tonic-gate 		 */
20400Sstevel@tonic-gate 		pool->p_asleep++;
204111066Srafael.vanoni@sun.com 		timeleft = cv_reltimedwait_sig(&pool->p_req_cv,
204211066Srafael.vanoni@sun.com 		    &pool->p_req_lock, pool->p_timeout, TR_CLOCK_TICK);
20430Sstevel@tonic-gate 
20440Sstevel@tonic-gate 		/*
20450Sstevel@tonic-gate 		 * If the drowsy flag is on this means that
20460Sstevel@tonic-gate 		 * someone has signaled a wakeup. In such a case
20470Sstevel@tonic-gate 		 * the `asleep-threads' count has already updated
20480Sstevel@tonic-gate 		 * so just clear the flag.
20490Sstevel@tonic-gate 		 *
20500Sstevel@tonic-gate 		 * If the drowsy flag is off then we need to update
20510Sstevel@tonic-gate 		 * the `asleep-threads' count.
20520Sstevel@tonic-gate 		 */
20530Sstevel@tonic-gate 		if (pool->p_drowsy) {
20540Sstevel@tonic-gate 			pool->p_drowsy = FALSE;
20550Sstevel@tonic-gate 			/*
20560Sstevel@tonic-gate 			 * If the thread is here because it timedout,
20570Sstevel@tonic-gate 			 * instead of returning SVC_ETIMEDOUT, it is
20580Sstevel@tonic-gate 			 * time to do some more work.
20590Sstevel@tonic-gate 			 */
20600Sstevel@tonic-gate 			if (timeleft == -1)
20610Sstevel@tonic-gate 				timeleft = 1;
20620Sstevel@tonic-gate 		} else {
20630Sstevel@tonic-gate 			pool->p_asleep--;
20640Sstevel@tonic-gate 		}
20650Sstevel@tonic-gate 		mutex_exit(&pool->p_req_lock);
20660Sstevel@tonic-gate 
20670Sstevel@tonic-gate 		/*
20680Sstevel@tonic-gate 		 * If we received a signal while waiting for a
20690Sstevel@tonic-gate 		 * request, inform svc_run(), so that we can return
20708139SVallish.Vaidyeshwara@Sun.COM 		 * to user level and exit.
20710Sstevel@tonic-gate 		 */
20720Sstevel@tonic-gate 		if (timeleft == 0)
20730Sstevel@tonic-gate 			return (SVC_EINTR);
20740Sstevel@tonic-gate 
20750Sstevel@tonic-gate 		/*
20760Sstevel@tonic-gate 		 * If the current transport is gone then notify
20770Sstevel@tonic-gate 		 * svc_run() to unlink from it.
20780Sstevel@tonic-gate 		 */
20790Sstevel@tonic-gate 		if (xprt && xprt->xp_wq == NULL)
20800Sstevel@tonic-gate 			return (SVC_EXPRTGONE);
20810Sstevel@tonic-gate 
20820Sstevel@tonic-gate 		/*
20830Sstevel@tonic-gate 		 * If we have timed out waiting for a request inform
20840Sstevel@tonic-gate 		 * svc_run() that we probably don't need this thread.
20850Sstevel@tonic-gate 		 */
20860Sstevel@tonic-gate 		if (timeleft == -1)
20870Sstevel@tonic-gate 			return (SVC_ETIMEDOUT);
20880Sstevel@tonic-gate 	}
20890Sstevel@tonic-gate }
20900Sstevel@tonic-gate 
20910Sstevel@tonic-gate /*
20920Sstevel@tonic-gate  * Main loop of the kernel RPC server
20930Sstevel@tonic-gate  * - wait for input (find a transport with a pending request).
20940Sstevel@tonic-gate  * - dequeue the request
20950Sstevel@tonic-gate  * - call a registered server routine to process the requests
20960Sstevel@tonic-gate  *
20970Sstevel@tonic-gate  * There can many threads running concurrently in this loop
20980Sstevel@tonic-gate  * on the same or on different transports.
20990Sstevel@tonic-gate  */
21000Sstevel@tonic-gate static int
svc_run(SVCPOOL * pool)21010Sstevel@tonic-gate svc_run(SVCPOOL *pool)
21020Sstevel@tonic-gate {
21030Sstevel@tonic-gate 	SVCMASTERXPRT *xprt = NULL;	/* master transport handle  */
21040Sstevel@tonic-gate 	SVCXPRT *clone_xprt;	/* clone for this thread    */
21050Sstevel@tonic-gate 	proc_t *p = ttoproc(curthread);
21060Sstevel@tonic-gate 
21070Sstevel@tonic-gate 	/* Allocate a clone transport handle for this thread */
21080Sstevel@tonic-gate 	clone_xprt = svc_clone_init();
21090Sstevel@tonic-gate 
21100Sstevel@tonic-gate 	/*
21110Sstevel@tonic-gate 	 * The loop iterates until the thread becomes
21120Sstevel@tonic-gate 	 * idle too long or the transport is gone.
21130Sstevel@tonic-gate 	 */
21140Sstevel@tonic-gate 	for (;;) {
21150Sstevel@tonic-gate 		SVCMASTERXPRT *next;
21160Sstevel@tonic-gate 		mblk_t *mp;
21170Sstevel@tonic-gate 
21180Sstevel@tonic-gate 		TRACE_0(TR_FAC_KRPC, TR_SVC_RUN, "svc_run");
21190Sstevel@tonic-gate 
21200Sstevel@tonic-gate 		/*
21210Sstevel@tonic-gate 		 * If the process is exiting/killed, return
21220Sstevel@tonic-gate 		 * immediately without processing any more
21230Sstevel@tonic-gate 		 * requests.
21240Sstevel@tonic-gate 		 */
2125390Sraf 		if (p->p_flag & (SEXITING | SKILLED)) {
21260Sstevel@tonic-gate 			svc_thread_exit(pool, clone_xprt);
21278139SVallish.Vaidyeshwara@Sun.COM 			return (EINTR);
21280Sstevel@tonic-gate 		}
21290Sstevel@tonic-gate 
21300Sstevel@tonic-gate 		/* Find a transport with a pending request */
21310Sstevel@tonic-gate 		next = svc_poll(pool, xprt, clone_xprt);
21320Sstevel@tonic-gate 
21330Sstevel@tonic-gate 		/*
21340Sstevel@tonic-gate 		 * If svc_poll() finds a transport with a request
21350Sstevel@tonic-gate 		 * it latches xp_req_lock on it. Therefore we need
21360Sstevel@tonic-gate 		 * to dequeue the request and release the lock as
21370Sstevel@tonic-gate 		 * soon as possible.
21380Sstevel@tonic-gate 		 */
21390Sstevel@tonic-gate 		ASSERT(next != NULL &&
21400Sstevel@tonic-gate 		    (next == SVC_EXPRTGONE ||
21410Sstevel@tonic-gate 		    next == SVC_ETIMEDOUT ||
21420Sstevel@tonic-gate 		    next == SVC_EINTR ||
21430Sstevel@tonic-gate 		    MUTEX_HELD(&next->xp_req_lock)));
21440Sstevel@tonic-gate 
21450Sstevel@tonic-gate 		/* Ooops! Current transport is closing. Unlink now */
21460Sstevel@tonic-gate 		if (next == SVC_EXPRTGONE) {
21470Sstevel@tonic-gate 			svc_clone_unlink(clone_xprt);
21480Sstevel@tonic-gate 			xprt = NULL;
21490Sstevel@tonic-gate 			continue;
21500Sstevel@tonic-gate 		}
21510Sstevel@tonic-gate 
21520Sstevel@tonic-gate 		/* Ooops! Timeout while waiting for a request. Exit */
21530Sstevel@tonic-gate 		if (next == SVC_ETIMEDOUT) {
21540Sstevel@tonic-gate 			svc_thread_exit(pool, clone_xprt);
21550Sstevel@tonic-gate 			return (0);
21560Sstevel@tonic-gate 		}
21570Sstevel@tonic-gate 
21580Sstevel@tonic-gate 		/*
21590Sstevel@tonic-gate 		 * Interrupted by a signal while waiting for a
21608139SVallish.Vaidyeshwara@Sun.COM 		 * request. Return to userspace and exit.
21610Sstevel@tonic-gate 		 */
21620Sstevel@tonic-gate 		if (next == SVC_EINTR) {
21630Sstevel@tonic-gate 			svc_thread_exit(pool, clone_xprt);
21640Sstevel@tonic-gate 			return (EINTR);
21650Sstevel@tonic-gate 		}
21660Sstevel@tonic-gate 
21670Sstevel@tonic-gate 		/*
21680Sstevel@tonic-gate 		 * De-queue the request and release the request lock
21690Sstevel@tonic-gate 		 * on this transport (latched by svc_poll()).
21700Sstevel@tonic-gate 		 */
21710Sstevel@tonic-gate 		mp = next->xp_req_head;
21720Sstevel@tonic-gate 		next->xp_req_head = mp->b_next;
21730Sstevel@tonic-gate 		mp->b_next = (mblk_t *)0;
21740Sstevel@tonic-gate 
21750Sstevel@tonic-gate 		TRACE_2(TR_FAC_KRPC, TR_NFSFP_QUE_REQ_DEQ,
21760Sstevel@tonic-gate 		    "rpc_que_req_deq:pool %p mp %p", pool, mp);
21770Sstevel@tonic-gate 		mutex_exit(&next->xp_req_lock);
21780Sstevel@tonic-gate 
21790Sstevel@tonic-gate 		/*
21800Sstevel@tonic-gate 		 * If this is a new request on a current transport then
21810Sstevel@tonic-gate 		 * the clone structure is already properly initialized.
21820Sstevel@tonic-gate 		 * Otherwise, if the request is on a different transport,
21830Sstevel@tonic-gate 		 * unlink from the current master and link to
21840Sstevel@tonic-gate 		 * the one we got a request on.
21850Sstevel@tonic-gate 		 */
21860Sstevel@tonic-gate 		if (next != xprt) {
21870Sstevel@tonic-gate 			if (xprt)
21880Sstevel@tonic-gate 				svc_clone_unlink(clone_xprt);
2189*11967SKaren.Rochford@Sun.COM 			svc_clone_link(next, clone_xprt, NULL);
21900Sstevel@tonic-gate 			xprt = next;
21910Sstevel@tonic-gate 		}
21920Sstevel@tonic-gate 
21930Sstevel@tonic-gate 		/*
21940Sstevel@tonic-gate 		 * If there are more requests and req_cv hasn't
21950Sstevel@tonic-gate 		 * been signaled yet then wake up one more thread now.
21960Sstevel@tonic-gate 		 *
21970Sstevel@tonic-gate 		 * We avoid signaling req_cv until the most recently
21980Sstevel@tonic-gate 		 * signaled thread wakes up and gets CPU to clear
21990Sstevel@tonic-gate 		 * the `drowsy' flag.
22000Sstevel@tonic-gate 		 */
22010Sstevel@tonic-gate 		if (!(pool->p_drowsy || pool->p_reqs <= pool->p_walkers ||
22020Sstevel@tonic-gate 		    pool->p_asleep == 0)) {
22030Sstevel@tonic-gate 			mutex_enter(&pool->p_req_lock);
22040Sstevel@tonic-gate 
22050Sstevel@tonic-gate 			if (pool->p_drowsy || pool->p_reqs <= pool->p_walkers ||
22060Sstevel@tonic-gate 			    pool->p_asleep == 0)
22070Sstevel@tonic-gate 				mutex_exit(&pool->p_req_lock);
22080Sstevel@tonic-gate 			else {
22090Sstevel@tonic-gate 				pool->p_asleep--;
22100Sstevel@tonic-gate 				pool->p_drowsy = TRUE;
22110Sstevel@tonic-gate 
22120Sstevel@tonic-gate 				cv_signal(&pool->p_req_cv);
22130Sstevel@tonic-gate 				mutex_exit(&pool->p_req_lock);
22140Sstevel@tonic-gate 			}
22150Sstevel@tonic-gate 		}
22160Sstevel@tonic-gate 
22170Sstevel@tonic-gate 		/*
22180Sstevel@tonic-gate 		 * If there are no asleep/signaled threads, we are
22190Sstevel@tonic-gate 		 * still below pool->p_maxthreads limit, and no thread is
22200Sstevel@tonic-gate 		 * currently being created then signal the creator
22210Sstevel@tonic-gate 		 * for one more service thread.
22220Sstevel@tonic-gate 		 *
22230Sstevel@tonic-gate 		 * The asleep and drowsy checks are not protected
22240Sstevel@tonic-gate 		 * by a lock since it hurts performance and a wrong
22250Sstevel@tonic-gate 		 * decision is not essential.
22260Sstevel@tonic-gate 		 */
22270Sstevel@tonic-gate 		if (pool->p_asleep == 0 && !pool->p_drowsy &&
22280Sstevel@tonic-gate 		    pool->p_threads + pool->p_detached_threads <
22290Sstevel@tonic-gate 		    pool->p_maxthreads)
22300Sstevel@tonic-gate 			svc_creator_signal(pool);
22310Sstevel@tonic-gate 
22320Sstevel@tonic-gate 		/*
22330Sstevel@tonic-gate 		 * Process the request.
22340Sstevel@tonic-gate 		 */
22350Sstevel@tonic-gate 		svc_getreq(clone_xprt, mp);
22360Sstevel@tonic-gate 
22370Sstevel@tonic-gate 		/* If thread had a reservation it should have been canceled */
22380Sstevel@tonic-gate 		ASSERT(!clone_xprt->xp_reserved);
22390Sstevel@tonic-gate 
22400Sstevel@tonic-gate 		/*
22410Sstevel@tonic-gate 		 * If the clone is marked detached then exit.
22420Sstevel@tonic-gate 		 * The rpcmod slot has already been released
22430Sstevel@tonic-gate 		 * when we detached this thread.
22440Sstevel@tonic-gate 		 */
22450Sstevel@tonic-gate 		if (clone_xprt->xp_detached) {
22460Sstevel@tonic-gate 			svc_thread_exitdetached(pool, clone_xprt);
22470Sstevel@tonic-gate 			return (0);
22480Sstevel@tonic-gate 		}
22490Sstevel@tonic-gate 
22500Sstevel@tonic-gate 		/*
22510Sstevel@tonic-gate 		 * Release our reference on the rpcmod
22520Sstevel@tonic-gate 		 * slot attached to xp_wq->q_ptr.
22530Sstevel@tonic-gate 		 */
22540Sstevel@tonic-gate 		(*RELE_PROC(xprt)) (clone_xprt->xp_wq, NULL);
22550Sstevel@tonic-gate 	}
22560Sstevel@tonic-gate 	/* NOTREACHED */
22570Sstevel@tonic-gate }
22580Sstevel@tonic-gate 
22590Sstevel@tonic-gate /*
22600Sstevel@tonic-gate  * Flush any pending requests for the queue and
22610Sstevel@tonic-gate  * and free the associated mblks.
22620Sstevel@tonic-gate  */
22630Sstevel@tonic-gate void
svc_queueclean(queue_t * q)22640Sstevel@tonic-gate svc_queueclean(queue_t *q)
22650Sstevel@tonic-gate {
22660Sstevel@tonic-gate 	SVCMASTERXPRT *xprt = ((void **) q->q_ptr)[0];
22670Sstevel@tonic-gate 	mblk_t *mp;
22684741Sgt29601 	SVCPOOL *pool;
22690Sstevel@tonic-gate 
22700Sstevel@tonic-gate 	/*
22710Sstevel@tonic-gate 	 * clean up the requests
22720Sstevel@tonic-gate 	 */
22730Sstevel@tonic-gate 	mutex_enter(&xprt->xp_req_lock);
22744741Sgt29601 	pool = xprt->xp_pool;
22750Sstevel@tonic-gate 	while ((mp = xprt->xp_req_head) != NULL) {
22764741Sgt29601 		/* remove the request from the list and decrement p_reqs */
22770Sstevel@tonic-gate 		xprt->xp_req_head = mp->b_next;
22784741Sgt29601 		mutex_enter(&pool->p_req_lock);
22790Sstevel@tonic-gate 		mp->b_next = (mblk_t *)0;
22804741Sgt29601 		pool->p_reqs--;
22814741Sgt29601 		mutex_exit(&pool->p_req_lock);
22820Sstevel@tonic-gate 		(*RELE_PROC(xprt)) (xprt->xp_wq, mp);
22830Sstevel@tonic-gate 	}
22840Sstevel@tonic-gate 	mutex_exit(&xprt->xp_req_lock);
22850Sstevel@tonic-gate }
22860Sstevel@tonic-gate 
22870Sstevel@tonic-gate /*
22880Sstevel@tonic-gate  * This routine is called by rpcmod to inform kernel RPC that a
22890Sstevel@tonic-gate  * queue is closing. It is called after all the requests have been
22900Sstevel@tonic-gate  * picked up (that is after all the slots on the queue have
22910Sstevel@tonic-gate  * been released by kernel RPC). It is also guaranteed that no more
22920Sstevel@tonic-gate  * request will be delivered on this transport.
22930Sstevel@tonic-gate  *
22940Sstevel@tonic-gate  * - clear xp_wq to mark the master server transport handle as closing
22950Sstevel@tonic-gate  * - if there are no more threads on this transport close/destroy it
22960Sstevel@tonic-gate  * - otherwise, broadcast threads sleeping in svc_poll(); the last
22970Sstevel@tonic-gate  *   thread will close/destroy the transport.
22980Sstevel@tonic-gate  */
22990Sstevel@tonic-gate void
svc_queueclose(queue_t * q)23000Sstevel@tonic-gate svc_queueclose(queue_t *q)
23010Sstevel@tonic-gate {
23020Sstevel@tonic-gate 	SVCMASTERXPRT *xprt = ((void **) q->q_ptr)[0];
23030Sstevel@tonic-gate 
23040Sstevel@tonic-gate 	if (xprt == NULL) {
23050Sstevel@tonic-gate 		/*
23060Sstevel@tonic-gate 		 * If there is no master xprt associated with this stream,
23070Sstevel@tonic-gate 		 * then there is nothing to do.  This happens regularly
23080Sstevel@tonic-gate 		 * with connection-oriented listening streams created by
23090Sstevel@tonic-gate 		 * nfsd.
23100Sstevel@tonic-gate 		 */
23110Sstevel@tonic-gate 		return;
23120Sstevel@tonic-gate 	}
23130Sstevel@tonic-gate 
23140Sstevel@tonic-gate 	mutex_enter(&xprt->xp_thread_lock);
23150Sstevel@tonic-gate 
23160Sstevel@tonic-gate 	ASSERT(xprt->xp_req_head == NULL);
23170Sstevel@tonic-gate 	ASSERT(xprt->xp_wq != NULL);
23180Sstevel@tonic-gate 
23190Sstevel@tonic-gate 	xprt->xp_wq = NULL;
23200Sstevel@tonic-gate 
23210Sstevel@tonic-gate 	if (xprt->xp_threads == 0) {
23220Sstevel@tonic-gate 		SVCPOOL *pool = xprt->xp_pool;
23230Sstevel@tonic-gate 
23240Sstevel@tonic-gate 		/*
23250Sstevel@tonic-gate 		 * svc_xprt_cleanup() destroys the transport
23260Sstevel@tonic-gate 		 * or releases the transport thread lock
23270Sstevel@tonic-gate 		 */
23280Sstevel@tonic-gate 		svc_xprt_cleanup(xprt, FALSE);
23290Sstevel@tonic-gate 
23300Sstevel@tonic-gate 		mutex_enter(&pool->p_thread_lock);
23310Sstevel@tonic-gate 
23320Sstevel@tonic-gate 		/*
23330Sstevel@tonic-gate 		 * If the pool is in closing state and this was
23340Sstevel@tonic-gate 		 * the last transport in the pool then signal the creator
23350Sstevel@tonic-gate 		 * thread to clean up and exit.
23360Sstevel@tonic-gate 		 */
23370Sstevel@tonic-gate 		if (pool->p_closing && svc_pool_tryexit(pool)) {
23380Sstevel@tonic-gate 			return;
23390Sstevel@tonic-gate 		}
23400Sstevel@tonic-gate 		mutex_exit(&pool->p_thread_lock);
23410Sstevel@tonic-gate 	} else {
23420Sstevel@tonic-gate 		/*
23430Sstevel@tonic-gate 		 * Wakeup threads sleeping in svc_poll() so that they
23440Sstevel@tonic-gate 		 * unlink from the transport
23450Sstevel@tonic-gate 		 */
23460Sstevel@tonic-gate 		mutex_enter(&xprt->xp_pool->p_req_lock);
23470Sstevel@tonic-gate 		cv_broadcast(&xprt->xp_pool->p_req_cv);
23480Sstevel@tonic-gate 		mutex_exit(&xprt->xp_pool->p_req_lock);
23490Sstevel@tonic-gate 
23500Sstevel@tonic-gate 		/*
23510Sstevel@tonic-gate 		 *  NOTICE: No references to the master transport structure
23520Sstevel@tonic-gate 		 *	    beyond this point!
23530Sstevel@tonic-gate 		 */
23540Sstevel@tonic-gate 		mutex_exit(&xprt->xp_thread_lock);
23550Sstevel@tonic-gate 	}
23560Sstevel@tonic-gate }
23570Sstevel@tonic-gate 
23580Sstevel@tonic-gate /*
23590Sstevel@tonic-gate  * Interrupt `request delivery' routine called from rpcmod
23600Sstevel@tonic-gate  * - put a request at the tail of the transport request queue
23610Sstevel@tonic-gate  * - insert a hint for svc_poll() into the xprt-ready queue
23620Sstevel@tonic-gate  * - increment the `pending-requests' count for the pool
23630Sstevel@tonic-gate  * - wake up a thread sleeping in svc_poll() if necessary
23640Sstevel@tonic-gate  * - if all the threads are running ask the creator for a new one.
23650Sstevel@tonic-gate  */
23660Sstevel@tonic-gate void
svc_queuereq(queue_t * q,mblk_t * mp)23670Sstevel@tonic-gate svc_queuereq(queue_t *q, mblk_t *mp)
23680Sstevel@tonic-gate {
23690Sstevel@tonic-gate 	SVCMASTERXPRT *xprt = ((void **) q->q_ptr)[0];
23700Sstevel@tonic-gate 	SVCPOOL *pool = xprt->xp_pool;
23710Sstevel@tonic-gate 
23720Sstevel@tonic-gate 	TRACE_0(TR_FAC_KRPC, TR_SVC_QUEUEREQ_START, "svc_queuereq_start");
23730Sstevel@tonic-gate 
23748778SErik.Nordmark@Sun.COM 	ASSERT(!is_system_labeled() || msg_getcred(mp, NULL) != NULL ||
23751676Sjpk 	    mp->b_datap->db_type != M_DATA);
23761676Sjpk 
23770Sstevel@tonic-gate 	/*
23780Sstevel@tonic-gate 	 * Step 1.
23794741Sgt29601 	 * Grab the transport's request lock and the
23804741Sgt29601 	 * pool's request lock so that when we put
23810Sstevel@tonic-gate 	 * the request at the tail of the transport's
23824741Sgt29601 	 * request queue, possibly put the request on
23834741Sgt29601 	 * the xprt ready queue and increment the
23844741Sgt29601 	 * pending request count it looks atomic.
23850Sstevel@tonic-gate 	 */
23860Sstevel@tonic-gate 	mutex_enter(&xprt->xp_req_lock);
23874741Sgt29601 	mutex_enter(&pool->p_req_lock);
23880Sstevel@tonic-gate 	if (xprt->xp_req_head == NULL)
23890Sstevel@tonic-gate 		xprt->xp_req_head = mp;
23900Sstevel@tonic-gate 	else
23910Sstevel@tonic-gate 		xprt->xp_req_tail->b_next = mp;
23920Sstevel@tonic-gate 	xprt->xp_req_tail = mp;
23930Sstevel@tonic-gate 
23940Sstevel@tonic-gate 	/*
23950Sstevel@tonic-gate 	 * Step 2.
23964741Sgt29601 	 * Insert a hint into the xprt-ready queue, increment
23974741Sgt29601 	 * `pending-requests' count for the pool, and wake up
23984741Sgt29601 	 * a thread sleeping in svc_poll() if necessary.
23990Sstevel@tonic-gate 	 */
24000Sstevel@tonic-gate 
24010Sstevel@tonic-gate 	/* Insert pointer to this transport into the xprt-ready queue */
24020Sstevel@tonic-gate 	svc_xprt_qput(pool, xprt);
24030Sstevel@tonic-gate 
24040Sstevel@tonic-gate 	/* Increment the `pending-requests' count for the pool */
24050Sstevel@tonic-gate 	pool->p_reqs++;
24060Sstevel@tonic-gate 
24070Sstevel@tonic-gate 	TRACE_2(TR_FAC_KRPC, TR_NFSFP_QUE_REQ_ENQ,
24080Sstevel@tonic-gate 	    "rpc_que_req_enq:pool %p mp %p", pool, mp);
24090Sstevel@tonic-gate 
24100Sstevel@tonic-gate 	/*
24110Sstevel@tonic-gate 	 * If there are more requests and req_cv hasn't
24120Sstevel@tonic-gate 	 * been signaled yet then wake up one more thread now.
24130Sstevel@tonic-gate 	 *
24140Sstevel@tonic-gate 	 * We avoid signaling req_cv until the most recently
24150Sstevel@tonic-gate 	 * signaled thread wakes up and gets CPU to clear
24160Sstevel@tonic-gate 	 * the `drowsy' flag.
24170Sstevel@tonic-gate 	 */
24180Sstevel@tonic-gate 	if (pool->p_drowsy || pool->p_reqs <= pool->p_walkers ||
24190Sstevel@tonic-gate 	    pool->p_asleep == 0) {
24200Sstevel@tonic-gate 		mutex_exit(&pool->p_req_lock);
24210Sstevel@tonic-gate 	} else {
24220Sstevel@tonic-gate 		pool->p_drowsy = TRUE;
24230Sstevel@tonic-gate 		pool->p_asleep--;
24240Sstevel@tonic-gate 
24250Sstevel@tonic-gate 		/*
24260Sstevel@tonic-gate 		 * Signal wakeup and drop the request lock.
24270Sstevel@tonic-gate 		 */
24280Sstevel@tonic-gate 		cv_signal(&pool->p_req_cv);
24290Sstevel@tonic-gate 		mutex_exit(&pool->p_req_lock);
24300Sstevel@tonic-gate 	}
24314741Sgt29601 	mutex_exit(&xprt->xp_req_lock);
24320Sstevel@tonic-gate 
24330Sstevel@tonic-gate 	/*
24340Sstevel@tonic-gate 	 * Step 3.
24350Sstevel@tonic-gate 	 * If there are no asleep/signaled threads, we are
24360Sstevel@tonic-gate 	 * still below pool->p_maxthreads limit, and no thread is
24370Sstevel@tonic-gate 	 * currently being created then signal the creator
24380Sstevel@tonic-gate 	 * for one more service thread.
24390Sstevel@tonic-gate 	 *
24400Sstevel@tonic-gate 	 * The asleep and drowsy checks are not not protected
24410Sstevel@tonic-gate 	 * by a lock since it hurts performance and a wrong
24420Sstevel@tonic-gate 	 * decision is not essential.
24430Sstevel@tonic-gate 	 */
24440Sstevel@tonic-gate 	if (pool->p_asleep == 0 && !pool->p_drowsy &&
24454741Sgt29601 	    pool->p_threads + pool->p_detached_threads < pool->p_maxthreads)
24460Sstevel@tonic-gate 		svc_creator_signal(pool);
24470Sstevel@tonic-gate 
24480Sstevel@tonic-gate 	TRACE_1(TR_FAC_KRPC, TR_SVC_QUEUEREQ_END,
24490Sstevel@tonic-gate 	    "svc_queuereq_end:(%S)", "end");
24500Sstevel@tonic-gate }
24510Sstevel@tonic-gate 
24520Sstevel@tonic-gate /*
24530Sstevel@tonic-gate  * Reserve a service thread so that it can be detached later.
24540Sstevel@tonic-gate  * This reservation is required to make sure that when it tries to
24550Sstevel@tonic-gate  * detach itself the total number of detached threads does not exceed
24560Sstevel@tonic-gate  * pool->p_maxthreads - pool->p_redline (i.e. that we can have
24570Sstevel@tonic-gate  * up to pool->p_redline non-detached threads).
24580Sstevel@tonic-gate  *
24590Sstevel@tonic-gate  * If the thread does not detach itself later, it should cancel the
24600Sstevel@tonic-gate  * reservation before returning to svc_run().
24610Sstevel@tonic-gate  *
24620Sstevel@tonic-gate  * - check if there is room for more reserved/detached threads
24630Sstevel@tonic-gate  * - if so, then increment the `reserved threads' count for the pool
24640Sstevel@tonic-gate  * - mark the thread as reserved (setting the flag in the clone transport
24650Sstevel@tonic-gate  *   handle for this thread
24660Sstevel@tonic-gate  * - returns 1 if the reservation succeeded, 0 if it failed.
24670Sstevel@tonic-gate  */
24680Sstevel@tonic-gate int
svc_reserve_thread(SVCXPRT * clone_xprt)24690Sstevel@tonic-gate svc_reserve_thread(SVCXPRT *clone_xprt)
24700Sstevel@tonic-gate {
24710Sstevel@tonic-gate 	SVCPOOL *pool = clone_xprt->xp_master->xp_pool;
24720Sstevel@tonic-gate 
24730Sstevel@tonic-gate 	/* Recursive reservations are not allowed */
24740Sstevel@tonic-gate 	ASSERT(!clone_xprt->xp_reserved);
24750Sstevel@tonic-gate 	ASSERT(!clone_xprt->xp_detached);
24760Sstevel@tonic-gate 
24770Sstevel@tonic-gate 	/* Check pool counts if there is room for reservation */
24780Sstevel@tonic-gate 	mutex_enter(&pool->p_thread_lock);
24790Sstevel@tonic-gate 	if (pool->p_reserved_threads + pool->p_detached_threads >=
24804741Sgt29601 	    pool->p_maxthreads - pool->p_redline) {
24810Sstevel@tonic-gate 		mutex_exit(&pool->p_thread_lock);
24820Sstevel@tonic-gate 		return (0);
24830Sstevel@tonic-gate 	}
24840Sstevel@tonic-gate 	pool->p_reserved_threads++;
24850Sstevel@tonic-gate 	mutex_exit(&pool->p_thread_lock);
24860Sstevel@tonic-gate 
24870Sstevel@tonic-gate 	/* Mark the thread (clone handle) as reserved */
24880Sstevel@tonic-gate 	clone_xprt->xp_reserved = TRUE;
24890Sstevel@tonic-gate 
24900Sstevel@tonic-gate 	return (1);
24910Sstevel@tonic-gate }
24920Sstevel@tonic-gate 
24930Sstevel@tonic-gate /*
24940Sstevel@tonic-gate  * Cancel a reservation for a thread.
24950Sstevel@tonic-gate  * - decrement the `reserved threads' count for the pool
24960Sstevel@tonic-gate  * - clear the flag in the clone transport handle for this thread.
24970Sstevel@tonic-gate  */
24980Sstevel@tonic-gate void
svc_unreserve_thread(SVCXPRT * clone_xprt)24990Sstevel@tonic-gate svc_unreserve_thread(SVCXPRT *clone_xprt)
25000Sstevel@tonic-gate {
25010Sstevel@tonic-gate 	SVCPOOL *pool = clone_xprt->xp_master->xp_pool;
25020Sstevel@tonic-gate 
25030Sstevel@tonic-gate 	/* Thread must have a reservation */
25040Sstevel@tonic-gate 	ASSERT(clone_xprt->xp_reserved);
25050Sstevel@tonic-gate 	ASSERT(!clone_xprt->xp_detached);
25060Sstevel@tonic-gate 
25070Sstevel@tonic-gate 	/* Decrement global count */
25080Sstevel@tonic-gate 	mutex_enter(&pool->p_thread_lock);
25090Sstevel@tonic-gate 	pool->p_reserved_threads--;
25100Sstevel@tonic-gate 	mutex_exit(&pool->p_thread_lock);
25110Sstevel@tonic-gate 
25120Sstevel@tonic-gate 	/* Clear reservation flag */
25130Sstevel@tonic-gate 	clone_xprt->xp_reserved = FALSE;
25140Sstevel@tonic-gate }
25150Sstevel@tonic-gate 
25160Sstevel@tonic-gate /*
25170Sstevel@tonic-gate  * Detach a thread from its transport, so that it can block for an
25180Sstevel@tonic-gate  * extended time.  Because the transport can be closed after the thread is
25190Sstevel@tonic-gate  * detached, the thread should have already sent off a reply if it was
25200Sstevel@tonic-gate  * going to send one.
25210Sstevel@tonic-gate  *
25220Sstevel@tonic-gate  * - decrement `non-detached threads' count and increment `detached threads'
25230Sstevel@tonic-gate  *   counts for the transport
25240Sstevel@tonic-gate  * - decrement the  `non-detached threads' and `reserved threads'
25250Sstevel@tonic-gate  *   counts and increment the `detached threads' count for the pool
25260Sstevel@tonic-gate  * - release the rpcmod slot
25270Sstevel@tonic-gate  * - mark the clone (thread) as detached.
25280Sstevel@tonic-gate  *
25290Sstevel@tonic-gate  * No need to return a pointer to the thread's CPR information, since
25300Sstevel@tonic-gate  * the thread has a userland identity.
25310Sstevel@tonic-gate  *
25320Sstevel@tonic-gate  * NOTICE: a thread must not detach itself without making a prior reservation
25330Sstevel@tonic-gate  *	   through svc_thread_reserve().
25340Sstevel@tonic-gate  */
25350Sstevel@tonic-gate callb_cpr_t *
svc_detach_thread(SVCXPRT * clone_xprt)25360Sstevel@tonic-gate svc_detach_thread(SVCXPRT *clone_xprt)
25370Sstevel@tonic-gate {
25380Sstevel@tonic-gate 	SVCMASTERXPRT *xprt = clone_xprt->xp_master;
25390Sstevel@tonic-gate 	SVCPOOL *pool = xprt->xp_pool;
25400Sstevel@tonic-gate 
25410Sstevel@tonic-gate 	/* Thread must have a reservation */
25420Sstevel@tonic-gate 	ASSERT(clone_xprt->xp_reserved);
25430Sstevel@tonic-gate 	ASSERT(!clone_xprt->xp_detached);
25440Sstevel@tonic-gate 
25450Sstevel@tonic-gate 	/* Bookkeeping for this transport */
25460Sstevel@tonic-gate 	mutex_enter(&xprt->xp_thread_lock);
25470Sstevel@tonic-gate 	xprt->xp_threads--;
25480Sstevel@tonic-gate 	xprt->xp_detached_threads++;
25490Sstevel@tonic-gate 	mutex_exit(&xprt->xp_thread_lock);
25500Sstevel@tonic-gate 
25510Sstevel@tonic-gate 	/* Bookkeeping for the pool */
25520Sstevel@tonic-gate 	mutex_enter(&pool->p_thread_lock);
25530Sstevel@tonic-gate 	pool->p_threads--;
25540Sstevel@tonic-gate 	pool->p_reserved_threads--;
25550Sstevel@tonic-gate 	pool->p_detached_threads++;
25560Sstevel@tonic-gate 	mutex_exit(&pool->p_thread_lock);
25570Sstevel@tonic-gate 
25580Sstevel@tonic-gate 	/* Release an rpcmod slot for this request */
25590Sstevel@tonic-gate 	(*RELE_PROC(xprt)) (clone_xprt->xp_wq, NULL);
25600Sstevel@tonic-gate 
25610Sstevel@tonic-gate 	/* Mark the clone (thread) as detached */
25620Sstevel@tonic-gate 	clone_xprt->xp_reserved = FALSE;
25630Sstevel@tonic-gate 	clone_xprt->xp_detached = TRUE;
25640Sstevel@tonic-gate 
25650Sstevel@tonic-gate 	return (NULL);
25660Sstevel@tonic-gate }
25670Sstevel@tonic-gate 
25680Sstevel@tonic-gate /*
25690Sstevel@tonic-gate  * This routine is responsible for extracting RDMA plugin master XPRT,
25700Sstevel@tonic-gate  * unregister from the SVCPOOL and initiate plugin specific cleanup.
25710Sstevel@tonic-gate  * It is passed a list/group of rdma transports as records which are
25720Sstevel@tonic-gate  * active in a given registered or unregistered kRPC thread pool. Its shuts
25730Sstevel@tonic-gate  * all active rdma transports in that pool. If the thread active on the trasport
25740Sstevel@tonic-gate  * happens to be last thread for that pool, it will signal the creater thread
25750Sstevel@tonic-gate  * to cleanup the pool and destroy the xprt in svc_queueclose()
25760Sstevel@tonic-gate  */
25770Sstevel@tonic-gate void
rdma_stop(rdma_xprt_group_t * rdma_xprts)25788695SRajkumar.Sivaprakasam@Sun.COM rdma_stop(rdma_xprt_group_t *rdma_xprts)
25790Sstevel@tonic-gate {
25800Sstevel@tonic-gate 	SVCMASTERXPRT *xprt;
25810Sstevel@tonic-gate 	rdma_xprt_record_t *curr_rec;
25820Sstevel@tonic-gate 	queue_t *q;
25830Sstevel@tonic-gate 	mblk_t *mp;
25848695SRajkumar.Sivaprakasam@Sun.COM 	int i, rtg_count;
25854741Sgt29601 	SVCPOOL *pool;
25860Sstevel@tonic-gate 
25878695SRajkumar.Sivaprakasam@Sun.COM 	if (rdma_xprts->rtg_count == 0)
25880Sstevel@tonic-gate 		return;
25890Sstevel@tonic-gate 
25908695SRajkumar.Sivaprakasam@Sun.COM 	rtg_count = rdma_xprts->rtg_count;
25918695SRajkumar.Sivaprakasam@Sun.COM 
25928695SRajkumar.Sivaprakasam@Sun.COM 	for (i = 0; i < rtg_count; i++) {
25938695SRajkumar.Sivaprakasam@Sun.COM 		curr_rec = rdma_xprts->rtg_listhead;
25948695SRajkumar.Sivaprakasam@Sun.COM 		rdma_xprts->rtg_listhead = curr_rec->rtr_next;
25958695SRajkumar.Sivaprakasam@Sun.COM 		rdma_xprts->rtg_count--;
25960Sstevel@tonic-gate 		curr_rec->rtr_next = NULL;
25970Sstevel@tonic-gate 		xprt = curr_rec->rtr_xprt_ptr;
25980Sstevel@tonic-gate 		q = xprt->xp_wq;
25990Sstevel@tonic-gate 		svc_rdma_kstop(xprt);
26000Sstevel@tonic-gate 
26010Sstevel@tonic-gate 		mutex_enter(&xprt->xp_req_lock);
26024741Sgt29601 		pool = xprt->xp_pool;
26030Sstevel@tonic-gate 		while ((mp = xprt->xp_req_head) != NULL) {
26044741Sgt29601 			/*
26054741Sgt29601 			 * remove the request from the list and
26064741Sgt29601 			 * decrement p_reqs
26074741Sgt29601 			 */
26080Sstevel@tonic-gate 			xprt->xp_req_head = mp->b_next;
26094741Sgt29601 			mutex_enter(&pool->p_req_lock);
26100Sstevel@tonic-gate 			mp->b_next = (mblk_t *)0;
26114741Sgt29601 			pool->p_reqs--;
26124741Sgt29601 			mutex_exit(&pool->p_req_lock);
26138695SRajkumar.Sivaprakasam@Sun.COM 			if (mp) {
26148695SRajkumar.Sivaprakasam@Sun.COM 				rdma_recv_data_t *rdp = (rdma_recv_data_t *)
26158695SRajkumar.Sivaprakasam@Sun.COM 				    mp->b_rptr;
26168695SRajkumar.Sivaprakasam@Sun.COM 				RDMA_BUF_FREE(rdp->conn, &rdp->rpcmsg);
26178695SRajkumar.Sivaprakasam@Sun.COM 				RDMA_REL_CONN(rdp->conn);
26180Sstevel@tonic-gate 				freemsg(mp);
26198695SRajkumar.Sivaprakasam@Sun.COM 			}
26200Sstevel@tonic-gate 		}
26210Sstevel@tonic-gate 		mutex_exit(&xprt->xp_req_lock);
26220Sstevel@tonic-gate 		svc_queueclose(q);
26230Sstevel@tonic-gate #ifdef	DEBUG
26240Sstevel@tonic-gate 		if (rdma_check)
26250Sstevel@tonic-gate 			cmn_err(CE_NOTE, "rdma_stop: Exited svc_queueclose\n");
26260Sstevel@tonic-gate #endif
26270Sstevel@tonic-gate 		/*
26280Sstevel@tonic-gate 		 * Free the rdma transport record for the expunged rdma
26290Sstevel@tonic-gate 		 * based master transport handle.
26300Sstevel@tonic-gate 		 */
26310Sstevel@tonic-gate 		kmem_free(curr_rec, sizeof (rdma_xprt_record_t));
26328695SRajkumar.Sivaprakasam@Sun.COM 		if (!rdma_xprts->rtg_listhead)
26330Sstevel@tonic-gate 			break;
26340Sstevel@tonic-gate 	}
26350Sstevel@tonic-gate }
263610721SGlenn.Barry@Sun.COM 
263710721SGlenn.Barry@Sun.COM 
263810721SGlenn.Barry@Sun.COM /*
263910721SGlenn.Barry@Sun.COM  * rpc_msg_dup/rpc_msg_free
264010721SGlenn.Barry@Sun.COM  * Currently only used by svc_rpcsec_gss.c but put in this file as it
264110721SGlenn.Barry@Sun.COM  * may be useful to others in the future.
264210721SGlenn.Barry@Sun.COM  * But future consumers should be careful cuz so far
264310721SGlenn.Barry@Sun.COM  *   - only tested/used for call msgs (not reply)
264410721SGlenn.Barry@Sun.COM  *   - only tested/used with call verf oa_length==0
264510721SGlenn.Barry@Sun.COM  */
264610721SGlenn.Barry@Sun.COM struct rpc_msg *
rpc_msg_dup(struct rpc_msg * src)264710721SGlenn.Barry@Sun.COM rpc_msg_dup(struct rpc_msg *src)
264810721SGlenn.Barry@Sun.COM {
264910721SGlenn.Barry@Sun.COM 	struct rpc_msg *dst;
265010721SGlenn.Barry@Sun.COM 	struct opaque_auth oa_src, oa_dst;
265110721SGlenn.Barry@Sun.COM 
265210721SGlenn.Barry@Sun.COM 	dst = kmem_alloc(sizeof (*dst), KM_SLEEP);
265310721SGlenn.Barry@Sun.COM 
265410721SGlenn.Barry@Sun.COM 	dst->rm_xid = src->rm_xid;
265510721SGlenn.Barry@Sun.COM 	dst->rm_direction = src->rm_direction;
265610721SGlenn.Barry@Sun.COM 
265710721SGlenn.Barry@Sun.COM 	dst->rm_call.cb_rpcvers = src->rm_call.cb_rpcvers;
265810721SGlenn.Barry@Sun.COM 	dst->rm_call.cb_prog = src->rm_call.cb_prog;
265910721SGlenn.Barry@Sun.COM 	dst->rm_call.cb_vers = src->rm_call.cb_vers;
266010721SGlenn.Barry@Sun.COM 	dst->rm_call.cb_proc = src->rm_call.cb_proc;
266110721SGlenn.Barry@Sun.COM 
266210721SGlenn.Barry@Sun.COM 	/* dup opaque auth call body cred */
266310721SGlenn.Barry@Sun.COM 	oa_src = src->rm_call.cb_cred;
266410721SGlenn.Barry@Sun.COM 
266510721SGlenn.Barry@Sun.COM 	oa_dst.oa_flavor = oa_src.oa_flavor;
266610721SGlenn.Barry@Sun.COM 	oa_dst.oa_base = kmem_alloc(oa_src.oa_length, KM_SLEEP);
266710721SGlenn.Barry@Sun.COM 
266810721SGlenn.Barry@Sun.COM 	bcopy(oa_src.oa_base, oa_dst.oa_base, oa_src.oa_length);
266910721SGlenn.Barry@Sun.COM 	oa_dst.oa_length = oa_src.oa_length;
267010721SGlenn.Barry@Sun.COM 
267110721SGlenn.Barry@Sun.COM 	dst->rm_call.cb_cred = oa_dst;
267210721SGlenn.Barry@Sun.COM 
267310721SGlenn.Barry@Sun.COM 	/* dup or just alloc opaque auth call body verifier */
267410721SGlenn.Barry@Sun.COM 	if (src->rm_call.cb_verf.oa_length > 0) {
267510721SGlenn.Barry@Sun.COM 		oa_src = src->rm_call.cb_verf;
267610721SGlenn.Barry@Sun.COM 
267710721SGlenn.Barry@Sun.COM 		oa_dst.oa_flavor = oa_src.oa_flavor;
267810721SGlenn.Barry@Sun.COM 		oa_dst.oa_base = kmem_alloc(oa_src.oa_length, KM_SLEEP);
267910721SGlenn.Barry@Sun.COM 
268010721SGlenn.Barry@Sun.COM 		bcopy(oa_src.oa_base, oa_dst.oa_base, oa_src.oa_length);
268110721SGlenn.Barry@Sun.COM 		oa_dst.oa_length = oa_src.oa_length;
268210721SGlenn.Barry@Sun.COM 
268310721SGlenn.Barry@Sun.COM 		dst->rm_call.cb_verf = oa_dst;
268410721SGlenn.Barry@Sun.COM 	} else {
268510721SGlenn.Barry@Sun.COM 		oa_dst.oa_flavor = -1;  /* will be set later */
268610721SGlenn.Barry@Sun.COM 		oa_dst.oa_base = kmem_alloc(MAX_AUTH_BYTES, KM_SLEEP);
268710721SGlenn.Barry@Sun.COM 
268810721SGlenn.Barry@Sun.COM 		oa_dst.oa_length = 0;   /* will be set later */
268910721SGlenn.Barry@Sun.COM 
269010721SGlenn.Barry@Sun.COM 		dst->rm_call.cb_verf = oa_dst;
269110721SGlenn.Barry@Sun.COM 	}
269210721SGlenn.Barry@Sun.COM 	return (dst);
269310721SGlenn.Barry@Sun.COM 
269410721SGlenn.Barry@Sun.COM error:
269510721SGlenn.Barry@Sun.COM 	kmem_free(dst->rm_call.cb_cred.oa_base,	dst->rm_call.cb_cred.oa_length);
269610721SGlenn.Barry@Sun.COM 	kmem_free(dst, sizeof (*dst));
269710721SGlenn.Barry@Sun.COM 	return (NULL);
269810721SGlenn.Barry@Sun.COM }
269910721SGlenn.Barry@Sun.COM 
270010721SGlenn.Barry@Sun.COM void
rpc_msg_free(struct rpc_msg ** msg,int cb_verf_oa_length)270110721SGlenn.Barry@Sun.COM rpc_msg_free(struct rpc_msg **msg, int cb_verf_oa_length)
270210721SGlenn.Barry@Sun.COM {
270310721SGlenn.Barry@Sun.COM 	struct rpc_msg *m = *msg;
270410721SGlenn.Barry@Sun.COM 
270510721SGlenn.Barry@Sun.COM 	kmem_free(m->rm_call.cb_cred.oa_base, m->rm_call.cb_cred.oa_length);
270610721SGlenn.Barry@Sun.COM 	m->rm_call.cb_cred.oa_base = NULL;
270710721SGlenn.Barry@Sun.COM 	m->rm_call.cb_cred.oa_length = 0;
270810721SGlenn.Barry@Sun.COM 
270910721SGlenn.Barry@Sun.COM 	kmem_free(m->rm_call.cb_verf.oa_base, cb_verf_oa_length);
271010721SGlenn.Barry@Sun.COM 	m->rm_call.cb_verf.oa_base = NULL;
271110721SGlenn.Barry@Sun.COM 	m->rm_call.cb_verf.oa_length = 0;
271210721SGlenn.Barry@Sun.COM 
271310721SGlenn.Barry@Sun.COM 	kmem_free(m, sizeof (*m));
271410721SGlenn.Barry@Sun.COM 	m = NULL;
271510721SGlenn.Barry@Sun.COM }
2716