xref: /onnv-gate/usr/src/uts/common/rpc/svc.c (revision 0)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate /*
28*0Sstevel@tonic-gate  * Copyright 1993 OpenVision Technologies, Inc., All Rights Reserved.
29*0Sstevel@tonic-gate  */
30*0Sstevel@tonic-gate 
31*0Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985,  1986, 1987, 1988, 1989 AT&T	*/
32*0Sstevel@tonic-gate /*	  All Rights Reserved  	*/
33*0Sstevel@tonic-gate 
34*0Sstevel@tonic-gate /*
35*0Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley 4.3 BSD
36*0Sstevel@tonic-gate  * under license from the Regents of the University of California.
37*0Sstevel@tonic-gate  */
38*0Sstevel@tonic-gate 
39*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
40*0Sstevel@tonic-gate 
41*0Sstevel@tonic-gate /*
42*0Sstevel@tonic-gate  * Server-side remote procedure call interface.
43*0Sstevel@tonic-gate  *
44*0Sstevel@tonic-gate  * Master transport handle (SVCMASTERXPRT).
45*0Sstevel@tonic-gate  *   The master transport handle structure is shared among service
46*0Sstevel@tonic-gate  *   threads processing events on the transport. Some fields in the
47*0Sstevel@tonic-gate  *   master structure are protected by locks
48*0Sstevel@tonic-gate  *   - xp_req_lock protects the request queue:
49*0Sstevel@tonic-gate  *	xp_req_head, xp_req_tail
50*0Sstevel@tonic-gate  *   - xp_thread_lock protects the thread (clone) counts
51*0Sstevel@tonic-gate  *	xp_threads, xp_detached_threads, xp_wq
52*0Sstevel@tonic-gate  *   Each master transport is registered to exactly one thread pool.
53*0Sstevel@tonic-gate  *
54*0Sstevel@tonic-gate  * Clone transport handle (SVCXPRT)
55*0Sstevel@tonic-gate  *   The clone transport handle structure is a per-service-thread handle
56*0Sstevel@tonic-gate  *   to the transport. The structure carries all the fields/buffers used
57*0Sstevel@tonic-gate  *   for request processing. A service thread or, in other words, a clone
58*0Sstevel@tonic-gate  *   structure, can be linked to an arbitrary master structure to process
59*0Sstevel@tonic-gate  *   requests on this transport. The master handle keeps track of reference
60*0Sstevel@tonic-gate  *   counts of threads (clones) linked to it. A service thread can switch
61*0Sstevel@tonic-gate  *   to another transport by unlinking its clone handle from the current
62*0Sstevel@tonic-gate  *   transport and linking to a new one. Switching is relatively inexpensive
63*0Sstevel@tonic-gate  *   but it involves locking (master's xprt->xp_thread_lock).
64*0Sstevel@tonic-gate  *
65*0Sstevel@tonic-gate  * Pools.
66*0Sstevel@tonic-gate  *   A pool represents a kernel RPC service (NFS, Lock Manager, etc.).
67*0Sstevel@tonic-gate  *   Transports related to the service are registered to the service pool.
68*0Sstevel@tonic-gate  *   Service threads can switch between different transports in the pool.
69*0Sstevel@tonic-gate  *   Thus, each service has its own pool of service threads. The maximum
70*0Sstevel@tonic-gate  *   number of threads in a pool is pool->p_maxthreads. This limit allows
71*0Sstevel@tonic-gate  *   to restrict resource usage by the service. Some fields are protected
72*0Sstevel@tonic-gate  *   by locks:
73*0Sstevel@tonic-gate  *   - p_req_lock protects several counts and flags:
74*0Sstevel@tonic-gate  *	p_reqs, p_walkers, p_asleep, p_drowsy, p_req_cv
75*0Sstevel@tonic-gate  *   - p_thread_lock governs other thread counts:
76*0Sstevel@tonic-gate  *	p_threads, p_detached_threads, p_reserved_threads, p_closing
77*0Sstevel@tonic-gate  *
78*0Sstevel@tonic-gate  *   In addition, each pool contains a doubly-linked list of transports,
79*0Sstevel@tonic-gate  *   an `xprt-ready' queue and a creator thread (see below). Threads in
80*0Sstevel@tonic-gate  *   the pool share some other parameters such as stack size and
81*0Sstevel@tonic-gate  *   polling timeout.
82*0Sstevel@tonic-gate  *
83*0Sstevel@tonic-gate  *   Pools are initialized through the svc_pool_create() function called from
84*0Sstevel@tonic-gate  *   the nfssys() system call. However, thread creation must be done by
85*0Sstevel@tonic-gate  *   the userland agent. This is done by using SVCPOOL_WAIT and
86*0Sstevel@tonic-gate  *   SVCPOOL_RUN arguments to nfssys(), which call svc_wait() and
87*0Sstevel@tonic-gate  *   svc_do_run(), respectively. Once the pool has been initialized,
88*0Sstevel@tonic-gate  *   the userland process must set up a 'creator' thread. This thread
89*0Sstevel@tonic-gate  *   should park itself in the kernel by calling svc_wait(). If
90*0Sstevel@tonic-gate  *   svc_wait() returns successfully, it should fork off a new worker
91*0Sstevel@tonic-gate  *   thread, which then calls svc_do_run() in order to get work. When
92*0Sstevel@tonic-gate  *   that thread is complete, svc_do_run() will return, and the user
93*0Sstevel@tonic-gate  *   program should call thr_exit().
94*0Sstevel@tonic-gate  *
95*0Sstevel@tonic-gate  *   When we try to register a new pool and there is an old pool with
96*0Sstevel@tonic-gate  *   the same id in the doubly linked pool list (this happens when we kill
97*0Sstevel@tonic-gate  *   and restart nfsd or lockd), then we unlink the old pool from the list
98*0Sstevel@tonic-gate  *   and mark its state as `closing'. After that the transports can still
99*0Sstevel@tonic-gate  *   process requests but new transports won't be registered. When all the
100*0Sstevel@tonic-gate  *   transports and service threads associated with the pool are gone the
101*0Sstevel@tonic-gate  *   creator thread (see below) will clean up the pool structure and exit.
102*0Sstevel@tonic-gate  *
103*0Sstevel@tonic-gate  * svc_queuereq() and svc_run().
104*0Sstevel@tonic-gate  *   The kernel RPC server is interrupt driven. The svc_queuereq() interrupt
105*0Sstevel@tonic-gate  *   routine is called to deliver an RPC request. The service threads
106*0Sstevel@tonic-gate  *   loop in svc_run(). The interrupt function queues a request on the
107*0Sstevel@tonic-gate  *   transport's queue and it makes sure that the request is serviced.
108*0Sstevel@tonic-gate  *   It may either wake up one of sleeping threads, or ask for a new thread
109*0Sstevel@tonic-gate  *   to be created, or, if the previous request is just being picked up, do
110*0Sstevel@tonic-gate  *   nothing. In the last case the service thread that is picking up the
111*0Sstevel@tonic-gate  *   previous request will wake up or create the next thread. After a service
112*0Sstevel@tonic-gate  *   thread processes a request and sends a reply it returns to svc_run()
113*0Sstevel@tonic-gate  *   and svc_run() calls svc_poll() to find new input.
114*0Sstevel@tonic-gate  *
115*0Sstevel@tonic-gate  *   There is an "inconsistent" but "safe" optimization in the
116*0Sstevel@tonic-gate  *   svc_queuereq() code. The request is queued under the transport's
117*0Sstevel@tonic-gate  *   request lock, while the `pending-requests' count is incremented
118*0Sstevel@tonic-gate  *   independently under the pool request lock. Thus, a request can be picked
119*0Sstevel@tonic-gate  *   up by a service thread before the counter is incremented. It may also
120*0Sstevel@tonic-gate  *   happen that the service thread will win the race condition on the pool
121*0Sstevel@tonic-gate  *   lock and it will decrement the count even before the interrupt thread
122*0Sstevel@tonic-gate  *   increments it (so the count can be temporarily negative).
123*0Sstevel@tonic-gate  *
124*0Sstevel@tonic-gate  * svc_poll().
125*0Sstevel@tonic-gate  *   In order to avoid unnecessary locking, which causes performance
126*0Sstevel@tonic-gate  *   problems, we always look for a pending request on the current transport.
127*0Sstevel@tonic-gate  *   If there is none we take a hint from the pool's `xprt-ready' queue.
128*0Sstevel@tonic-gate  *   If the queue had an overflow we switch to the `drain' mode checking
129*0Sstevel@tonic-gate  *   each transport  in the pool's transport list. Once we find a
130*0Sstevel@tonic-gate  *   master transport handle with a pending request we latch the request
131*0Sstevel@tonic-gate  *   lock on this transport and return to svc_run(). If the request
132*0Sstevel@tonic-gate  *   belongs to a transport different than the one the service thread is
133*0Sstevel@tonic-gate  *   linked to we need to unlink and link again.
134*0Sstevel@tonic-gate  *
135*0Sstevel@tonic-gate  *   A service thread goes asleep when there are no pending
136*0Sstevel@tonic-gate  *   requests on the transports registered on the pool's transports.
137*0Sstevel@tonic-gate  *   All the pool's threads sleep on the same condition variable.
138*0Sstevel@tonic-gate  *   If a thread has been sleeping for too long period of time
139*0Sstevel@tonic-gate  *   (by default 5 seconds) it wakes up and exits.  Also when a transport
140*0Sstevel@tonic-gate  *   is closing sleeping threads wake up to unlink from this transport.
141*0Sstevel@tonic-gate  *
142*0Sstevel@tonic-gate  * The `xprt-ready' queue.
143*0Sstevel@tonic-gate  *   If a service thread finds no request on a transport it is currently linked
144*0Sstevel@tonic-gate  *   to it will find another transport with a pending request. To make
145*0Sstevel@tonic-gate  *   this search more efficient each pool has an `xprt-ready' queue.
146*0Sstevel@tonic-gate  *   The queue is a FIFO. When the interrupt routine queues a request it also
147*0Sstevel@tonic-gate  *   inserts a pointer to the transport into the `xprt-ready' queue. A
148*0Sstevel@tonic-gate  *   thread looking for a transport with a pending request can pop up a
149*0Sstevel@tonic-gate  *   transport and check for a request. The request can be already gone
150*0Sstevel@tonic-gate  *   since it could be taken by a thread linked to that transport. In such a
151*0Sstevel@tonic-gate  *   case we try the next hint. The `xprt-ready' queue has fixed size (by
152*0Sstevel@tonic-gate  *   default 256 nodes). If it overflows svc_poll() has to switch to the
153*0Sstevel@tonic-gate  *   less efficient but safe `drain' mode and walk through the pool's
154*0Sstevel@tonic-gate  *   transport list.
155*0Sstevel@tonic-gate  *
156*0Sstevel@tonic-gate  *   Both the svc_poll() loop and the `xprt-ready' queue are optimized
157*0Sstevel@tonic-gate  *   for the peak load case that is for the situation when the queue is not
158*0Sstevel@tonic-gate  *   empty, there are all the time few pending requests, and a service
159*0Sstevel@tonic-gate  *   thread which has just processed a request does not go asleep but picks
160*0Sstevel@tonic-gate  *   up immediately the next request.
161*0Sstevel@tonic-gate  *
162*0Sstevel@tonic-gate  * Thread creator.
163*0Sstevel@tonic-gate  *   Each pool has a thread creator associated with it. The creator thread
164*0Sstevel@tonic-gate  *   sleeps on a condition variable and waits for a signal to create a
165*0Sstevel@tonic-gate  *   service thread. The actual thread creation is done in userland by
166*0Sstevel@tonic-gate  *   the method described in "Pools" above.
167*0Sstevel@tonic-gate  *
168*0Sstevel@tonic-gate  *   Signaling threads should turn on the `creator signaled' flag, and
169*0Sstevel@tonic-gate  *   can avoid sending signals when the flag is on. The flag is cleared
170*0Sstevel@tonic-gate  *   when the thread is created.
171*0Sstevel@tonic-gate  *
172*0Sstevel@tonic-gate  *   When the pool is in closing state (ie it has been already unregistered
173*0Sstevel@tonic-gate  *   from the pool list) the last thread on the last transport in the pool
174*0Sstevel@tonic-gate  *   should turn the p_creator_exit flag on. The creator thread will
175*0Sstevel@tonic-gate  *   clean up the pool structure and exit.
176*0Sstevel@tonic-gate  *
177*0Sstevel@tonic-gate  * Thread reservation; Detaching service threads.
178*0Sstevel@tonic-gate  *   A service thread can detach itself to block for an extended amount
179*0Sstevel@tonic-gate  *   of time. However, to keep the service active we need to guarantee
180*0Sstevel@tonic-gate  *   at least pool->p_redline non-detached threads that can process incoming
181*0Sstevel@tonic-gate  *   requests. This, the maximum number of detached and reserved threads is
182*0Sstevel@tonic-gate  *   p->p_maxthreads - p->p_redline. A service thread should first acquire
183*0Sstevel@tonic-gate  *   a reservation, and if the reservation was granted it can detach itself.
184*0Sstevel@tonic-gate  *   If a reservation was granted but the thread does not detach itself
185*0Sstevel@tonic-gate  *   it should cancel the reservation before it returns to svc_run().
186*0Sstevel@tonic-gate  */
187*0Sstevel@tonic-gate 
188*0Sstevel@tonic-gate #include <sys/param.h>
189*0Sstevel@tonic-gate #include <sys/types.h>
190*0Sstevel@tonic-gate #include <rpc/types.h>
191*0Sstevel@tonic-gate #include <sys/socket.h>
192*0Sstevel@tonic-gate #include <sys/time.h>
193*0Sstevel@tonic-gate #include <sys/tiuser.h>
194*0Sstevel@tonic-gate #include <sys/t_kuser.h>
195*0Sstevel@tonic-gate #include <netinet/in.h>
196*0Sstevel@tonic-gate #include <rpc/xdr.h>
197*0Sstevel@tonic-gate #include <rpc/auth.h>
198*0Sstevel@tonic-gate #include <rpc/clnt.h>
199*0Sstevel@tonic-gate #include <rpc/rpc_msg.h>
200*0Sstevel@tonic-gate #include <rpc/svc.h>
201*0Sstevel@tonic-gate #include <sys/proc.h>
202*0Sstevel@tonic-gate #include <sys/user.h>
203*0Sstevel@tonic-gate #include <sys/stream.h>
204*0Sstevel@tonic-gate #include <sys/strsubr.h>
205*0Sstevel@tonic-gate #include <sys/tihdr.h>
206*0Sstevel@tonic-gate #include <sys/debug.h>
207*0Sstevel@tonic-gate #include <sys/cmn_err.h>
208*0Sstevel@tonic-gate #include <sys/file.h>
209*0Sstevel@tonic-gate #include <sys/systm.h>
210*0Sstevel@tonic-gate #include <sys/callb.h>
211*0Sstevel@tonic-gate #include <sys/vtrace.h>
212*0Sstevel@tonic-gate #include <sys/zone.h>
213*0Sstevel@tonic-gate #include <nfs/nfs.h>
214*0Sstevel@tonic-gate 
215*0Sstevel@tonic-gate #define	RQCRED_SIZE	400	/* this size is excessive */
216*0Sstevel@tonic-gate 
217*0Sstevel@tonic-gate /*
218*0Sstevel@tonic-gate  * Defines for svc_poll()
219*0Sstevel@tonic-gate  */
220*0Sstevel@tonic-gate #define	SVC_EXPRTGONE ((SVCMASTERXPRT *)1)	/* Transport is closing */
221*0Sstevel@tonic-gate #define	SVC_ETIMEDOUT ((SVCMASTERXPRT *)2)	/* Timeout */
222*0Sstevel@tonic-gate #define	SVC_EINTR ((SVCMASTERXPRT *)3)		/* Interrupted by signal */
223*0Sstevel@tonic-gate 
224*0Sstevel@tonic-gate /*
225*0Sstevel@tonic-gate  * Default stack size for service threads.
226*0Sstevel@tonic-gate  */
227*0Sstevel@tonic-gate #define	DEFAULT_SVC_RUN_STKSIZE		(0)	/* default kernel stack */
228*0Sstevel@tonic-gate 
229*0Sstevel@tonic-gate int    svc_default_stksize = DEFAULT_SVC_RUN_STKSIZE;
230*0Sstevel@tonic-gate 
231*0Sstevel@tonic-gate /*
232*0Sstevel@tonic-gate  * Default polling timeout for service threads.
233*0Sstevel@tonic-gate  * Multiplied by hz when used.
234*0Sstevel@tonic-gate  */
235*0Sstevel@tonic-gate #define	DEFAULT_SVC_POLL_TIMEOUT	(5)	/* seconds */
236*0Sstevel@tonic-gate 
237*0Sstevel@tonic-gate clock_t svc_default_timeout = DEFAULT_SVC_POLL_TIMEOUT;
238*0Sstevel@tonic-gate 
239*0Sstevel@tonic-gate /*
240*0Sstevel@tonic-gate  * Size of the `xprt-ready' queue.
241*0Sstevel@tonic-gate  */
242*0Sstevel@tonic-gate #define	DEFAULT_SVC_QSIZE		(256)	/* qnodes */
243*0Sstevel@tonic-gate 
244*0Sstevel@tonic-gate size_t svc_default_qsize = DEFAULT_SVC_QSIZE;
245*0Sstevel@tonic-gate 
246*0Sstevel@tonic-gate /*
247*0Sstevel@tonic-gate  * Default limit for the number of service threads.
248*0Sstevel@tonic-gate  */
249*0Sstevel@tonic-gate #define	DEFAULT_SVC_MAXTHREADS		(INT16_MAX)
250*0Sstevel@tonic-gate 
251*0Sstevel@tonic-gate int    svc_default_maxthreads = DEFAULT_SVC_MAXTHREADS;
252*0Sstevel@tonic-gate 
253*0Sstevel@tonic-gate /*
254*0Sstevel@tonic-gate  * Maximum number of requests from the same transport (in `drain' mode).
255*0Sstevel@tonic-gate  */
256*0Sstevel@tonic-gate #define	DEFAULT_SVC_MAX_SAME_XPRT	(8)
257*0Sstevel@tonic-gate 
258*0Sstevel@tonic-gate int    svc_default_max_same_xprt = DEFAULT_SVC_MAX_SAME_XPRT;
259*0Sstevel@tonic-gate 
260*0Sstevel@tonic-gate 
261*0Sstevel@tonic-gate /*
262*0Sstevel@tonic-gate  * Default `Redline' of non-detached threads.
263*0Sstevel@tonic-gate  * Total number of detached and reserved threads in an RPC server
264*0Sstevel@tonic-gate  * thread pool is limited to pool->p_maxthreads - svc_redline.
265*0Sstevel@tonic-gate  */
266*0Sstevel@tonic-gate #define	DEFAULT_SVC_REDLINE		(1)
267*0Sstevel@tonic-gate 
268*0Sstevel@tonic-gate int    svc_default_redline = DEFAULT_SVC_REDLINE;
269*0Sstevel@tonic-gate 
270*0Sstevel@tonic-gate /*
271*0Sstevel@tonic-gate  * A node for the `xprt-ready' queue.
272*0Sstevel@tonic-gate  * See below.
273*0Sstevel@tonic-gate  */
274*0Sstevel@tonic-gate struct __svcxprt_qnode {
275*0Sstevel@tonic-gate 	__SVCXPRT_QNODE	*q_next;
276*0Sstevel@tonic-gate 	SVCMASTERXPRT	*q_xprt;
277*0Sstevel@tonic-gate };
278*0Sstevel@tonic-gate 
279*0Sstevel@tonic-gate /*
280*0Sstevel@tonic-gate  * Global SVC variables (private).
281*0Sstevel@tonic-gate  */
282*0Sstevel@tonic-gate struct svc_globals {
283*0Sstevel@tonic-gate 	SVCPOOL		*svc_pools;
284*0Sstevel@tonic-gate 	kmutex_t	svc_plock;
285*0Sstevel@tonic-gate };
286*0Sstevel@tonic-gate 
287*0Sstevel@tonic-gate /*
288*0Sstevel@tonic-gate  * Debug variable to check for rdma based
289*0Sstevel@tonic-gate  * transport startup and cleanup. Contorlled
290*0Sstevel@tonic-gate  * through /etc/system. Off by default.
291*0Sstevel@tonic-gate  */
292*0Sstevel@tonic-gate int rdma_check = 0;
293*0Sstevel@tonic-gate 
294*0Sstevel@tonic-gate /*
295*0Sstevel@tonic-gate  * Authentication parameters list.
296*0Sstevel@tonic-gate  */
297*0Sstevel@tonic-gate static caddr_t rqcred_head;
298*0Sstevel@tonic-gate static kmutex_t rqcred_lock;
299*0Sstevel@tonic-gate 
300*0Sstevel@tonic-gate /*
301*0Sstevel@tonic-gate  * Pointers to transport specific `rele' routines in rpcmod (set from rpcmod).
302*0Sstevel@tonic-gate  */
303*0Sstevel@tonic-gate void	(*rpc_rele)(queue_t *, mblk_t *) = NULL;
304*0Sstevel@tonic-gate void	(*mir_rele)(queue_t *, mblk_t *) = NULL;
305*0Sstevel@tonic-gate 
306*0Sstevel@tonic-gate /* ARGSUSED */
307*0Sstevel@tonic-gate void
308*0Sstevel@tonic-gate rpc_rdma_rele(queue_t *q, mblk_t *mp)
309*0Sstevel@tonic-gate {
310*0Sstevel@tonic-gate }
311*0Sstevel@tonic-gate void    (*rdma_rele)(queue_t *, mblk_t *) = rpc_rdma_rele;
312*0Sstevel@tonic-gate 
313*0Sstevel@tonic-gate 
314*0Sstevel@tonic-gate /*
315*0Sstevel@tonic-gate  * This macro picks which `rele' routine to use, based on the transport type.
316*0Sstevel@tonic-gate  */
317*0Sstevel@tonic-gate #define	RELE_PROC(xprt) \
318*0Sstevel@tonic-gate 	((xprt)->xp_type == T_RDMA ? rdma_rele : \
319*0Sstevel@tonic-gate 	(((xprt)->xp_type == T_CLTS) ? rpc_rele : mir_rele))
320*0Sstevel@tonic-gate 
321*0Sstevel@tonic-gate /*
322*0Sstevel@tonic-gate  * If true, then keep quiet about version mismatch.
323*0Sstevel@tonic-gate  * This macro is for broadcast RPC only. We have no broadcast RPC in
324*0Sstevel@tonic-gate  * kernel now but one may define a flag in the transport structure
325*0Sstevel@tonic-gate  * and redefine this macro.
326*0Sstevel@tonic-gate  */
327*0Sstevel@tonic-gate #define	version_keepquiet(xprt)	(FALSE)
328*0Sstevel@tonic-gate 
329*0Sstevel@tonic-gate /*
330*0Sstevel@tonic-gate  * ZSD key used to retrieve zone-specific svc globals
331*0Sstevel@tonic-gate  */
332*0Sstevel@tonic-gate static zone_key_t svc_zone_key;
333*0Sstevel@tonic-gate 
334*0Sstevel@tonic-gate static void svc_callout_free(SVCMASTERXPRT *);
335*0Sstevel@tonic-gate static void svc_xprt_qinit(SVCPOOL *, size_t);
336*0Sstevel@tonic-gate static void svc_xprt_qdestroy(SVCPOOL *);
337*0Sstevel@tonic-gate static void svc_thread_creator(SVCPOOL *);
338*0Sstevel@tonic-gate static void svc_creator_signal(SVCPOOL *);
339*0Sstevel@tonic-gate static void svc_creator_signalexit(SVCPOOL *);
340*0Sstevel@tonic-gate static void svc_pool_unregister(struct svc_globals *, SVCPOOL *);
341*0Sstevel@tonic-gate static int svc_run(SVCPOOL *);
342*0Sstevel@tonic-gate 
343*0Sstevel@tonic-gate /* ARGSUSED */
344*0Sstevel@tonic-gate static void *
345*0Sstevel@tonic-gate svc_zoneinit(zoneid_t zoneid)
346*0Sstevel@tonic-gate {
347*0Sstevel@tonic-gate 	struct svc_globals *svc;
348*0Sstevel@tonic-gate 
349*0Sstevel@tonic-gate 	svc = kmem_alloc(sizeof (*svc), KM_SLEEP);
350*0Sstevel@tonic-gate 	mutex_init(&svc->svc_plock, NULL, MUTEX_DEFAULT, NULL);
351*0Sstevel@tonic-gate 	svc->svc_pools = NULL;
352*0Sstevel@tonic-gate 	return (svc);
353*0Sstevel@tonic-gate }
354*0Sstevel@tonic-gate 
355*0Sstevel@tonic-gate /* ARGSUSED */
356*0Sstevel@tonic-gate static void
357*0Sstevel@tonic-gate svc_zoneshutdown(zoneid_t zoneid, void *arg)
358*0Sstevel@tonic-gate {
359*0Sstevel@tonic-gate 	struct svc_globals *svc = arg;
360*0Sstevel@tonic-gate 	SVCPOOL *pool;
361*0Sstevel@tonic-gate 
362*0Sstevel@tonic-gate 	mutex_enter(&svc->svc_plock);
363*0Sstevel@tonic-gate 	while ((pool = svc->svc_pools) != NULL) {
364*0Sstevel@tonic-gate 		svc_pool_unregister(svc, pool);
365*0Sstevel@tonic-gate 	}
366*0Sstevel@tonic-gate 	mutex_exit(&svc->svc_plock);
367*0Sstevel@tonic-gate }
368*0Sstevel@tonic-gate 
369*0Sstevel@tonic-gate /* ARGSUSED */
370*0Sstevel@tonic-gate static void
371*0Sstevel@tonic-gate svc_zonefini(zoneid_t zoneid, void *arg)
372*0Sstevel@tonic-gate {
373*0Sstevel@tonic-gate 	struct svc_globals *svc = arg;
374*0Sstevel@tonic-gate 
375*0Sstevel@tonic-gate 	ASSERT(svc->svc_pools == NULL);
376*0Sstevel@tonic-gate 	mutex_destroy(&svc->svc_plock);
377*0Sstevel@tonic-gate 	kmem_free(svc, sizeof (*svc));
378*0Sstevel@tonic-gate }
379*0Sstevel@tonic-gate 
380*0Sstevel@tonic-gate /*
381*0Sstevel@tonic-gate  * Global SVC init routine.
382*0Sstevel@tonic-gate  * Initialize global generic and transport type specific structures
383*0Sstevel@tonic-gate  * used by the kernel RPC server side. This routine is called only
384*0Sstevel@tonic-gate  * once when the module is being loaded.
385*0Sstevel@tonic-gate  */
386*0Sstevel@tonic-gate void
387*0Sstevel@tonic-gate svc_init()
388*0Sstevel@tonic-gate {
389*0Sstevel@tonic-gate 	zone_key_create(&svc_zone_key, svc_zoneinit, svc_zoneshutdown,
390*0Sstevel@tonic-gate 	    svc_zonefini);
391*0Sstevel@tonic-gate 	svc_cots_init();
392*0Sstevel@tonic-gate 	svc_clts_init();
393*0Sstevel@tonic-gate }
394*0Sstevel@tonic-gate 
395*0Sstevel@tonic-gate /*
396*0Sstevel@tonic-gate  * Destroy the SVCPOOL structure.
397*0Sstevel@tonic-gate  */
398*0Sstevel@tonic-gate static void
399*0Sstevel@tonic-gate svc_pool_cleanup(SVCPOOL *pool)
400*0Sstevel@tonic-gate {
401*0Sstevel@tonic-gate 	ASSERT(pool->p_threads + pool->p_detached_threads == 0);
402*0Sstevel@tonic-gate 	ASSERT(pool->p_lcount == 0);
403*0Sstevel@tonic-gate 	ASSERT(pool->p_closing);
404*0Sstevel@tonic-gate 
405*0Sstevel@tonic-gate 	/*
406*0Sstevel@tonic-gate 	 * Call the user supplied shutdown function.  This is done
407*0Sstevel@tonic-gate 	 * here so the user of the pool will be able to cleanup
408*0Sstevel@tonic-gate 	 * service related resources.
409*0Sstevel@tonic-gate 	 */
410*0Sstevel@tonic-gate 	if (pool->p_shutdown != NULL)
411*0Sstevel@tonic-gate 		(pool->p_shutdown)();
412*0Sstevel@tonic-gate 
413*0Sstevel@tonic-gate 	/* Destroy `xprt-ready' queue */
414*0Sstevel@tonic-gate 	svc_xprt_qdestroy(pool);
415*0Sstevel@tonic-gate 
416*0Sstevel@tonic-gate 	/* Destroy transport list */
417*0Sstevel@tonic-gate 	rw_destroy(&pool->p_lrwlock);
418*0Sstevel@tonic-gate 
419*0Sstevel@tonic-gate 	/* Destroy locks and condition variables */
420*0Sstevel@tonic-gate 	mutex_destroy(&pool->p_thread_lock);
421*0Sstevel@tonic-gate 	mutex_destroy(&pool->p_req_lock);
422*0Sstevel@tonic-gate 	cv_destroy(&pool->p_req_cv);
423*0Sstevel@tonic-gate 
424*0Sstevel@tonic-gate 	/* Destroy creator's locks and condition variables */
425*0Sstevel@tonic-gate 	mutex_destroy(&pool->p_creator_lock);
426*0Sstevel@tonic-gate 	cv_destroy(&pool->p_creator_cv);
427*0Sstevel@tonic-gate 	mutex_destroy(&pool->p_user_lock);
428*0Sstevel@tonic-gate 	cv_destroy(&pool->p_user_cv);
429*0Sstevel@tonic-gate 
430*0Sstevel@tonic-gate 	/* Free pool structure */
431*0Sstevel@tonic-gate 	kmem_free(pool, sizeof (SVCPOOL));
432*0Sstevel@tonic-gate }
433*0Sstevel@tonic-gate 
434*0Sstevel@tonic-gate /*
435*0Sstevel@tonic-gate  * If all the transports and service threads are already gone
436*0Sstevel@tonic-gate  * signal the creator thread to clean up and exit.
437*0Sstevel@tonic-gate  */
438*0Sstevel@tonic-gate static bool_t
439*0Sstevel@tonic-gate svc_pool_tryexit(SVCPOOL *pool)
440*0Sstevel@tonic-gate {
441*0Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pool->p_thread_lock));
442*0Sstevel@tonic-gate 	ASSERT(pool->p_closing);
443*0Sstevel@tonic-gate 
444*0Sstevel@tonic-gate 	if (pool->p_threads + pool->p_detached_threads == 0) {
445*0Sstevel@tonic-gate 		rw_enter(&pool->p_lrwlock, RW_READER);
446*0Sstevel@tonic-gate 		if (pool->p_lcount == 0) {
447*0Sstevel@tonic-gate 			/*
448*0Sstevel@tonic-gate 			 * Release the locks before sending a signal.
449*0Sstevel@tonic-gate 			 */
450*0Sstevel@tonic-gate 			rw_exit(&pool->p_lrwlock);
451*0Sstevel@tonic-gate 			mutex_exit(&pool->p_thread_lock);
452*0Sstevel@tonic-gate 
453*0Sstevel@tonic-gate 			/*
454*0Sstevel@tonic-gate 			 * Notify the creator thread to clean up and exit
455*0Sstevel@tonic-gate 			 *
456*0Sstevel@tonic-gate 			 * NOTICE: No references to the pool beyond this point!
457*0Sstevel@tonic-gate 			 *		   The pool is being destroyed.
458*0Sstevel@tonic-gate 			 */
459*0Sstevel@tonic-gate 			ASSERT(!MUTEX_HELD(&pool->p_thread_lock));
460*0Sstevel@tonic-gate 			svc_creator_signalexit(pool);
461*0Sstevel@tonic-gate 
462*0Sstevel@tonic-gate 			return (TRUE);
463*0Sstevel@tonic-gate 		}
464*0Sstevel@tonic-gate 		rw_exit(&pool->p_lrwlock);
465*0Sstevel@tonic-gate 	}
466*0Sstevel@tonic-gate 
467*0Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pool->p_thread_lock));
468*0Sstevel@tonic-gate 	return (FALSE);
469*0Sstevel@tonic-gate }
470*0Sstevel@tonic-gate 
471*0Sstevel@tonic-gate /*
472*0Sstevel@tonic-gate  * Find a pool with a given id.
473*0Sstevel@tonic-gate  */
474*0Sstevel@tonic-gate static SVCPOOL *
475*0Sstevel@tonic-gate svc_pool_find(struct svc_globals *svc, int id)
476*0Sstevel@tonic-gate {
477*0Sstevel@tonic-gate 	SVCPOOL *pool;
478*0Sstevel@tonic-gate 
479*0Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&svc->svc_plock));
480*0Sstevel@tonic-gate 
481*0Sstevel@tonic-gate 	/*
482*0Sstevel@tonic-gate 	 * Search the list for a pool with a matching id
483*0Sstevel@tonic-gate 	 * and register the transport handle with that pool.
484*0Sstevel@tonic-gate 	 */
485*0Sstevel@tonic-gate 	for (pool = svc->svc_pools; pool; pool = pool->p_next)
486*0Sstevel@tonic-gate 		if (pool->p_id == id)
487*0Sstevel@tonic-gate 			return (pool);
488*0Sstevel@tonic-gate 
489*0Sstevel@tonic-gate 	return (NULL);
490*0Sstevel@tonic-gate }
491*0Sstevel@tonic-gate 
492*0Sstevel@tonic-gate /*
493*0Sstevel@tonic-gate  * PSARC 2003/523 Contract Private Interface
494*0Sstevel@tonic-gate  * svc_do_run
495*0Sstevel@tonic-gate  * Changes must be reviewed by Solaris File Sharing
496*0Sstevel@tonic-gate  * Changes must be communicated to contract-2003-523@sun.com
497*0Sstevel@tonic-gate  */
498*0Sstevel@tonic-gate int
499*0Sstevel@tonic-gate svc_do_run(int id)
500*0Sstevel@tonic-gate {
501*0Sstevel@tonic-gate 	SVCPOOL *pool;
502*0Sstevel@tonic-gate 	int err = 0;
503*0Sstevel@tonic-gate 	struct svc_globals *svc;
504*0Sstevel@tonic-gate 
505*0Sstevel@tonic-gate 	svc = zone_getspecific(svc_zone_key, curproc->p_zone);
506*0Sstevel@tonic-gate 	mutex_enter(&svc->svc_plock);
507*0Sstevel@tonic-gate 
508*0Sstevel@tonic-gate 	pool = svc_pool_find(svc, id);
509*0Sstevel@tonic-gate 
510*0Sstevel@tonic-gate 	mutex_exit(&svc->svc_plock);
511*0Sstevel@tonic-gate 
512*0Sstevel@tonic-gate 	if (pool == NULL)
513*0Sstevel@tonic-gate 		return (ENOENT);
514*0Sstevel@tonic-gate 
515*0Sstevel@tonic-gate 	/*
516*0Sstevel@tonic-gate 	 * Increment counter of pool threads now
517*0Sstevel@tonic-gate 	 * that a thread has been created.
518*0Sstevel@tonic-gate 	 */
519*0Sstevel@tonic-gate 	mutex_enter(&pool->p_thread_lock);
520*0Sstevel@tonic-gate 	pool->p_threads++;
521*0Sstevel@tonic-gate 	mutex_exit(&pool->p_thread_lock);
522*0Sstevel@tonic-gate 
523*0Sstevel@tonic-gate 	/* Give work to the new thread. */
524*0Sstevel@tonic-gate 	err = svc_run(pool);
525*0Sstevel@tonic-gate 
526*0Sstevel@tonic-gate 	return (err);
527*0Sstevel@tonic-gate }
528*0Sstevel@tonic-gate 
529*0Sstevel@tonic-gate /*
530*0Sstevel@tonic-gate  * Unregister a pool from the pool list.
531*0Sstevel@tonic-gate  * Set the closing state. If all the transports and service threads
532*0Sstevel@tonic-gate  * are already gone signal the creator thread to clean up and exit.
533*0Sstevel@tonic-gate  */
534*0Sstevel@tonic-gate static void
535*0Sstevel@tonic-gate svc_pool_unregister(struct svc_globals *svc, SVCPOOL *pool)
536*0Sstevel@tonic-gate {
537*0Sstevel@tonic-gate 	SVCPOOL *next = pool->p_next;
538*0Sstevel@tonic-gate 	SVCPOOL *prev = pool->p_prev;
539*0Sstevel@tonic-gate 
540*0Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&svc->svc_plock));
541*0Sstevel@tonic-gate 
542*0Sstevel@tonic-gate 	/* Remove from the list */
543*0Sstevel@tonic-gate 	if (pool == svc->svc_pools)
544*0Sstevel@tonic-gate 		svc->svc_pools = next;
545*0Sstevel@tonic-gate 	if (next)
546*0Sstevel@tonic-gate 		next->p_prev = prev;
547*0Sstevel@tonic-gate 	if (prev)
548*0Sstevel@tonic-gate 		prev->p_next = next;
549*0Sstevel@tonic-gate 	pool->p_next = pool->p_prev = NULL;
550*0Sstevel@tonic-gate 
551*0Sstevel@tonic-gate 	/*
552*0Sstevel@tonic-gate 	 * Offline the pool. Mark the pool as closing.
553*0Sstevel@tonic-gate 	 * If there are no transports in this pool notify
554*0Sstevel@tonic-gate 	 * the creator thread to clean it up and exit.
555*0Sstevel@tonic-gate 	 */
556*0Sstevel@tonic-gate 	mutex_enter(&pool->p_thread_lock);
557*0Sstevel@tonic-gate 	if (pool->p_offline != NULL)
558*0Sstevel@tonic-gate 		(pool->p_offline)();
559*0Sstevel@tonic-gate 	pool->p_closing = TRUE;
560*0Sstevel@tonic-gate 	if (svc_pool_tryexit(pool))
561*0Sstevel@tonic-gate 		return;
562*0Sstevel@tonic-gate 	mutex_exit(&pool->p_thread_lock);
563*0Sstevel@tonic-gate }
564*0Sstevel@tonic-gate 
565*0Sstevel@tonic-gate /*
566*0Sstevel@tonic-gate  * Register a pool with a given id in the global doubly linked pool list.
567*0Sstevel@tonic-gate  * - if there is a pool with the same id in the list then unregister it
568*0Sstevel@tonic-gate  * - insert the new pool into the list.
569*0Sstevel@tonic-gate  */
570*0Sstevel@tonic-gate static void
571*0Sstevel@tonic-gate svc_pool_register(struct svc_globals *svc, SVCPOOL *pool, int id)
572*0Sstevel@tonic-gate {
573*0Sstevel@tonic-gate 	SVCPOOL *old_pool;
574*0Sstevel@tonic-gate 
575*0Sstevel@tonic-gate 	/*
576*0Sstevel@tonic-gate 	 * If there is a pool with the same id then remove it from
577*0Sstevel@tonic-gate 	 * the list and mark the pool as closing.
578*0Sstevel@tonic-gate 	 */
579*0Sstevel@tonic-gate 	mutex_enter(&svc->svc_plock);
580*0Sstevel@tonic-gate 
581*0Sstevel@tonic-gate 	if (old_pool = svc_pool_find(svc, id))
582*0Sstevel@tonic-gate 		svc_pool_unregister(svc, old_pool);
583*0Sstevel@tonic-gate 
584*0Sstevel@tonic-gate 	/* Insert into the doubly linked list */
585*0Sstevel@tonic-gate 	pool->p_id = id;
586*0Sstevel@tonic-gate 	pool->p_next = svc->svc_pools;
587*0Sstevel@tonic-gate 	pool->p_prev = NULL;
588*0Sstevel@tonic-gate 	if (svc->svc_pools)
589*0Sstevel@tonic-gate 		svc->svc_pools->p_prev = pool;
590*0Sstevel@tonic-gate 	svc->svc_pools = pool;
591*0Sstevel@tonic-gate 
592*0Sstevel@tonic-gate 	mutex_exit(&svc->svc_plock);
593*0Sstevel@tonic-gate }
594*0Sstevel@tonic-gate 
595*0Sstevel@tonic-gate /*
596*0Sstevel@tonic-gate  * Initialize a newly created pool structure
597*0Sstevel@tonic-gate  */
598*0Sstevel@tonic-gate static int
599*0Sstevel@tonic-gate svc_pool_init(SVCPOOL *pool, uint_t maxthreads, uint_t redline,
600*0Sstevel@tonic-gate 	uint_t qsize, uint_t timeout, uint_t stksize, uint_t max_same_xprt)
601*0Sstevel@tonic-gate {
602*0Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(curthread);
603*0Sstevel@tonic-gate 
604*0Sstevel@tonic-gate 	ASSERT(pool);
605*0Sstevel@tonic-gate 
606*0Sstevel@tonic-gate 	if (maxthreads == 0)
607*0Sstevel@tonic-gate 		maxthreads = svc_default_maxthreads;
608*0Sstevel@tonic-gate 	if (redline == 0)
609*0Sstevel@tonic-gate 		redline = svc_default_redline;
610*0Sstevel@tonic-gate 	if (qsize == 0)
611*0Sstevel@tonic-gate 		qsize = svc_default_qsize;
612*0Sstevel@tonic-gate 	if (timeout == 0)
613*0Sstevel@tonic-gate 		timeout = svc_default_timeout;
614*0Sstevel@tonic-gate 	if (stksize == 0)
615*0Sstevel@tonic-gate 		stksize = svc_default_stksize;
616*0Sstevel@tonic-gate 	if (max_same_xprt == 0)
617*0Sstevel@tonic-gate 		max_same_xprt = svc_default_max_same_xprt;
618*0Sstevel@tonic-gate 
619*0Sstevel@tonic-gate 	if (maxthreads < redline)
620*0Sstevel@tonic-gate 		return (EINVAL);
621*0Sstevel@tonic-gate 
622*0Sstevel@tonic-gate 	/* Allocate and initialize the `xprt-ready' queue */
623*0Sstevel@tonic-gate 	svc_xprt_qinit(pool, qsize);
624*0Sstevel@tonic-gate 
625*0Sstevel@tonic-gate 	/* Initialize doubly-linked xprt list */
626*0Sstevel@tonic-gate 	rw_init(&pool->p_lrwlock, NULL, RW_DEFAULT, NULL);
627*0Sstevel@tonic-gate 
628*0Sstevel@tonic-gate 	/*
629*0Sstevel@tonic-gate 	 * Setting lwp_childstksz on the current lwp so that
630*0Sstevel@tonic-gate 	 * descendants of this lwp get the modified stacksize, if
631*0Sstevel@tonic-gate 	 * it is defined. It is important that either this lwp or
632*0Sstevel@tonic-gate 	 * one of its descendants do the actual servicepool thread
633*0Sstevel@tonic-gate 	 * creation to maintain the stacksize inheritance.
634*0Sstevel@tonic-gate 	 */
635*0Sstevel@tonic-gate 	if (lwp != NULL)
636*0Sstevel@tonic-gate 		lwp->lwp_childstksz = stksize;
637*0Sstevel@tonic-gate 
638*0Sstevel@tonic-gate 	/* Initialize thread limits, locks and condition variables */
639*0Sstevel@tonic-gate 	pool->p_maxthreads = maxthreads;
640*0Sstevel@tonic-gate 	pool->p_redline = redline;
641*0Sstevel@tonic-gate 	pool->p_timeout = timeout * hz;
642*0Sstevel@tonic-gate 	pool->p_stksize = stksize;
643*0Sstevel@tonic-gate 	pool->p_max_same_xprt = max_same_xprt;
644*0Sstevel@tonic-gate 	mutex_init(&pool->p_thread_lock, NULL, MUTEX_DEFAULT, NULL);
645*0Sstevel@tonic-gate 	mutex_init(&pool->p_req_lock, NULL, MUTEX_DEFAULT, NULL);
646*0Sstevel@tonic-gate 	cv_init(&pool->p_req_cv, NULL, CV_DEFAULT, NULL);
647*0Sstevel@tonic-gate 
648*0Sstevel@tonic-gate 	/* Initialize userland creator */
649*0Sstevel@tonic-gate 	pool->p_user_exit = FALSE;
650*0Sstevel@tonic-gate 	pool->p_signal_create_thread = FALSE;
651*0Sstevel@tonic-gate 	pool->p_user_waiting = FALSE;
652*0Sstevel@tonic-gate 	mutex_init(&pool->p_user_lock, NULL, MUTEX_DEFAULT, NULL);
653*0Sstevel@tonic-gate 	cv_init(&pool->p_user_cv, NULL, CV_DEFAULT, NULL);
654*0Sstevel@tonic-gate 
655*0Sstevel@tonic-gate 	/* Initialize the creator and start the creator thread */
656*0Sstevel@tonic-gate 	pool->p_creator_exit = FALSE;
657*0Sstevel@tonic-gate 	mutex_init(&pool->p_creator_lock, NULL, MUTEX_DEFAULT, NULL);
658*0Sstevel@tonic-gate 	cv_init(&pool->p_creator_cv, NULL, CV_DEFAULT, NULL);
659*0Sstevel@tonic-gate 
660*0Sstevel@tonic-gate 	(void) zthread_create(NULL, pool->p_stksize, svc_thread_creator,
661*0Sstevel@tonic-gate 	    pool, 0, minclsyspri);
662*0Sstevel@tonic-gate 
663*0Sstevel@tonic-gate 	return (0);
664*0Sstevel@tonic-gate }
665*0Sstevel@tonic-gate 
666*0Sstevel@tonic-gate /*
667*0Sstevel@tonic-gate  * PSARC 2003/523 Contract Private Interface
668*0Sstevel@tonic-gate  * svc_pool_create
669*0Sstevel@tonic-gate  * Changes must be reviewed by Solaris File Sharing
670*0Sstevel@tonic-gate  * Changes must be communicated to contract-2003-523@sun.com
671*0Sstevel@tonic-gate  *
672*0Sstevel@tonic-gate  * Create an kernel RPC server-side thread/transport pool.
673*0Sstevel@tonic-gate  *
674*0Sstevel@tonic-gate  * This is public interface for creation of a server RPC thread pool
675*0Sstevel@tonic-gate  * for a given service provider. Transports registered with the pool's id
676*0Sstevel@tonic-gate  * will be served by a pool's threads. This function is called from the
677*0Sstevel@tonic-gate  * nfssys() system call.
678*0Sstevel@tonic-gate  */
679*0Sstevel@tonic-gate int
680*0Sstevel@tonic-gate svc_pool_create(struct svcpool_args *args)
681*0Sstevel@tonic-gate {
682*0Sstevel@tonic-gate 	SVCPOOL *pool;
683*0Sstevel@tonic-gate 	int error;
684*0Sstevel@tonic-gate 	struct svc_globals *svc;
685*0Sstevel@tonic-gate 
686*0Sstevel@tonic-gate 	/*
687*0Sstevel@tonic-gate 	 * Caller should check credentials in a way appropriate
688*0Sstevel@tonic-gate 	 * in the context of the call.
689*0Sstevel@tonic-gate 	 */
690*0Sstevel@tonic-gate 
691*0Sstevel@tonic-gate 	svc = zone_getspecific(svc_zone_key, curproc->p_zone);
692*0Sstevel@tonic-gate 	/* Allocate a new pool */
693*0Sstevel@tonic-gate 	pool = kmem_zalloc(sizeof (SVCPOOL), KM_SLEEP);
694*0Sstevel@tonic-gate 
695*0Sstevel@tonic-gate 	/*
696*0Sstevel@tonic-gate 	 * Initialize the pool structure and create a creator thread.
697*0Sstevel@tonic-gate 	 */
698*0Sstevel@tonic-gate 	error = svc_pool_init(pool, args->maxthreads, args->redline,
699*0Sstevel@tonic-gate 	    args->qsize, args->timeout, args->stksize, args->max_same_xprt);
700*0Sstevel@tonic-gate 
701*0Sstevel@tonic-gate 	if (error) {
702*0Sstevel@tonic-gate 		kmem_free(pool, sizeof (SVCPOOL));
703*0Sstevel@tonic-gate 		return (error);
704*0Sstevel@tonic-gate 	}
705*0Sstevel@tonic-gate 
706*0Sstevel@tonic-gate 	/* Register the pool with the global pool list */
707*0Sstevel@tonic-gate 	svc_pool_register(svc, pool, args->id);
708*0Sstevel@tonic-gate 
709*0Sstevel@tonic-gate 	return (0);
710*0Sstevel@tonic-gate }
711*0Sstevel@tonic-gate 
712*0Sstevel@tonic-gate int
713*0Sstevel@tonic-gate svc_pool_control(int id, int cmd, void *arg)
714*0Sstevel@tonic-gate {
715*0Sstevel@tonic-gate 	SVCPOOL *pool;
716*0Sstevel@tonic-gate 	struct svc_globals *svc;
717*0Sstevel@tonic-gate 
718*0Sstevel@tonic-gate 	svc = zone_getspecific(svc_zone_key, curproc->p_zone);
719*0Sstevel@tonic-gate 
720*0Sstevel@tonic-gate 	switch (cmd) {
721*0Sstevel@tonic-gate 	case SVCPSET_SHUTDOWN_PROC:
722*0Sstevel@tonic-gate 		/*
723*0Sstevel@tonic-gate 		 * Search the list for a pool with a matching id
724*0Sstevel@tonic-gate 		 * and register the transport handle with that pool.
725*0Sstevel@tonic-gate 		 */
726*0Sstevel@tonic-gate 		mutex_enter(&svc->svc_plock);
727*0Sstevel@tonic-gate 
728*0Sstevel@tonic-gate 		if ((pool = svc_pool_find(svc, id)) == NULL) {
729*0Sstevel@tonic-gate 			mutex_exit(&svc->svc_plock);
730*0Sstevel@tonic-gate 			return (ENOENT);
731*0Sstevel@tonic-gate 		}
732*0Sstevel@tonic-gate 		/*
733*0Sstevel@tonic-gate 		 * Grab the transport list lock before releasing the
734*0Sstevel@tonic-gate 		 * pool list lock
735*0Sstevel@tonic-gate 		 */
736*0Sstevel@tonic-gate 		rw_enter(&pool->p_lrwlock, RW_WRITER);
737*0Sstevel@tonic-gate 		mutex_exit(&svc->svc_plock);
738*0Sstevel@tonic-gate 
739*0Sstevel@tonic-gate 		pool->p_shutdown = *((void (*)())arg);
740*0Sstevel@tonic-gate 
741*0Sstevel@tonic-gate 		rw_exit(&pool->p_lrwlock);
742*0Sstevel@tonic-gate 
743*0Sstevel@tonic-gate 		return (0);
744*0Sstevel@tonic-gate 	case SVCPSET_UNREGISTER_PROC:
745*0Sstevel@tonic-gate 		/*
746*0Sstevel@tonic-gate 		 * Search the list for a pool with a matching id
747*0Sstevel@tonic-gate 		 * and register the unregister callback handle with that pool.
748*0Sstevel@tonic-gate 		 */
749*0Sstevel@tonic-gate 		mutex_enter(&svc->svc_plock);
750*0Sstevel@tonic-gate 
751*0Sstevel@tonic-gate 		if ((pool = svc_pool_find(svc, id)) == NULL) {
752*0Sstevel@tonic-gate 			mutex_exit(&svc->svc_plock);
753*0Sstevel@tonic-gate 			return (ENOENT);
754*0Sstevel@tonic-gate 		}
755*0Sstevel@tonic-gate 		/*
756*0Sstevel@tonic-gate 		 * Grab the transport list lock before releasing the
757*0Sstevel@tonic-gate 		 * pool list lock
758*0Sstevel@tonic-gate 		 */
759*0Sstevel@tonic-gate 		rw_enter(&pool->p_lrwlock, RW_WRITER);
760*0Sstevel@tonic-gate 		mutex_exit(&svc->svc_plock);
761*0Sstevel@tonic-gate 
762*0Sstevel@tonic-gate 		pool->p_offline = *((void (*)())arg);
763*0Sstevel@tonic-gate 
764*0Sstevel@tonic-gate 		rw_exit(&pool->p_lrwlock);
765*0Sstevel@tonic-gate 
766*0Sstevel@tonic-gate 		return (0);
767*0Sstevel@tonic-gate 	default:
768*0Sstevel@tonic-gate 		return (EINVAL);
769*0Sstevel@tonic-gate 	}
770*0Sstevel@tonic-gate }
771*0Sstevel@tonic-gate 
772*0Sstevel@tonic-gate /*
773*0Sstevel@tonic-gate  * Pool's transport list manipulation routines.
774*0Sstevel@tonic-gate  * - svc_xprt_register()
775*0Sstevel@tonic-gate  * - svc_xprt_unregister()
776*0Sstevel@tonic-gate  *
777*0Sstevel@tonic-gate  * svc_xprt_register() is called from svc_tli_kcreate() to
778*0Sstevel@tonic-gate  * insert a new master transport handle into the doubly linked
779*0Sstevel@tonic-gate  * list of server transport handles (one list per pool).
780*0Sstevel@tonic-gate  *
781*0Sstevel@tonic-gate  * The list is used by svc_poll(), when it operates in `drain'
782*0Sstevel@tonic-gate  * mode, to search for a next transport with a pending request.
783*0Sstevel@tonic-gate  */
784*0Sstevel@tonic-gate 
785*0Sstevel@tonic-gate int
786*0Sstevel@tonic-gate svc_xprt_register(SVCMASTERXPRT *xprt, int id)
787*0Sstevel@tonic-gate {
788*0Sstevel@tonic-gate 	SVCMASTERXPRT *prev, *next;
789*0Sstevel@tonic-gate 	SVCPOOL *pool;
790*0Sstevel@tonic-gate 	struct svc_globals *svc;
791*0Sstevel@tonic-gate 
792*0Sstevel@tonic-gate 	svc = zone_getspecific(svc_zone_key, curproc->p_zone);
793*0Sstevel@tonic-gate 	/*
794*0Sstevel@tonic-gate 	 * Search the list for a pool with a matching id
795*0Sstevel@tonic-gate 	 * and register the transport handle with that pool.
796*0Sstevel@tonic-gate 	 */
797*0Sstevel@tonic-gate 	mutex_enter(&svc->svc_plock);
798*0Sstevel@tonic-gate 
799*0Sstevel@tonic-gate 	if ((pool = svc_pool_find(svc, id)) == NULL) {
800*0Sstevel@tonic-gate 		mutex_exit(&svc->svc_plock);
801*0Sstevel@tonic-gate 		return (ENOENT);
802*0Sstevel@tonic-gate 	}
803*0Sstevel@tonic-gate 
804*0Sstevel@tonic-gate 	/* Grab the transport list lock before releasing the pool list lock */
805*0Sstevel@tonic-gate 	rw_enter(&pool->p_lrwlock, RW_WRITER);
806*0Sstevel@tonic-gate 	mutex_exit(&svc->svc_plock);
807*0Sstevel@tonic-gate 
808*0Sstevel@tonic-gate 	/* Don't register new transports when the pool is in closing state */
809*0Sstevel@tonic-gate 	if (pool->p_closing) {
810*0Sstevel@tonic-gate 		rw_exit(&pool->p_lrwlock);
811*0Sstevel@tonic-gate 		return (EBUSY);
812*0Sstevel@tonic-gate 	}
813*0Sstevel@tonic-gate 
814*0Sstevel@tonic-gate 	/*
815*0Sstevel@tonic-gate 	 * Initialize xp_pool to point to the pool.
816*0Sstevel@tonic-gate 	 * We don't want to go through the pool list every time.
817*0Sstevel@tonic-gate 	 */
818*0Sstevel@tonic-gate 	xprt->xp_pool = pool;
819*0Sstevel@tonic-gate 
820*0Sstevel@tonic-gate 	/*
821*0Sstevel@tonic-gate 	 * Insert a transport handle into the list.
822*0Sstevel@tonic-gate 	 * The list head points to the most recently inserted transport.
823*0Sstevel@tonic-gate 	 */
824*0Sstevel@tonic-gate 	if (pool->p_lhead == NULL)
825*0Sstevel@tonic-gate 		pool->p_lhead = xprt->xp_prev = xprt->xp_next = xprt;
826*0Sstevel@tonic-gate 	else {
827*0Sstevel@tonic-gate 		next = pool->p_lhead;
828*0Sstevel@tonic-gate 		prev = pool->p_lhead->xp_prev;
829*0Sstevel@tonic-gate 
830*0Sstevel@tonic-gate 		xprt->xp_next = next;
831*0Sstevel@tonic-gate 		xprt->xp_prev = prev;
832*0Sstevel@tonic-gate 
833*0Sstevel@tonic-gate 		pool->p_lhead = prev->xp_next = next->xp_prev = xprt;
834*0Sstevel@tonic-gate 	}
835*0Sstevel@tonic-gate 
836*0Sstevel@tonic-gate 	/* Increment the transports count */
837*0Sstevel@tonic-gate 	pool->p_lcount++;
838*0Sstevel@tonic-gate 
839*0Sstevel@tonic-gate 	rw_exit(&pool->p_lrwlock);
840*0Sstevel@tonic-gate 	return (0);
841*0Sstevel@tonic-gate }
842*0Sstevel@tonic-gate 
843*0Sstevel@tonic-gate /*
844*0Sstevel@tonic-gate  * Called from svc_xprt_cleanup() to remove a master transport handle
845*0Sstevel@tonic-gate  * from the pool's list of server transports (when a transport is
846*0Sstevel@tonic-gate  * being destroyed).
847*0Sstevel@tonic-gate  */
848*0Sstevel@tonic-gate void
849*0Sstevel@tonic-gate svc_xprt_unregister(SVCMASTERXPRT *xprt)
850*0Sstevel@tonic-gate {
851*0Sstevel@tonic-gate 	SVCPOOL *pool = xprt->xp_pool;
852*0Sstevel@tonic-gate 
853*0Sstevel@tonic-gate 	/*
854*0Sstevel@tonic-gate 	 * Unlink xprt from the list.
855*0Sstevel@tonic-gate 	 * If the list head points to this xprt then move it
856*0Sstevel@tonic-gate 	 * to the next xprt or reset to NULL if this is the last
857*0Sstevel@tonic-gate 	 * xprt in the list.
858*0Sstevel@tonic-gate 	 */
859*0Sstevel@tonic-gate 	rw_enter(&pool->p_lrwlock, RW_WRITER);
860*0Sstevel@tonic-gate 
861*0Sstevel@tonic-gate 	if (xprt == xprt->xp_next)
862*0Sstevel@tonic-gate 		pool->p_lhead = NULL;
863*0Sstevel@tonic-gate 	else {
864*0Sstevel@tonic-gate 		SVCMASTERXPRT *next = xprt->xp_next;
865*0Sstevel@tonic-gate 		SVCMASTERXPRT *prev = xprt->xp_prev;
866*0Sstevel@tonic-gate 
867*0Sstevel@tonic-gate 		next->xp_prev = prev;
868*0Sstevel@tonic-gate 		prev->xp_next = next;
869*0Sstevel@tonic-gate 
870*0Sstevel@tonic-gate 		if (pool->p_lhead == xprt)
871*0Sstevel@tonic-gate 			pool->p_lhead = next;
872*0Sstevel@tonic-gate 	}
873*0Sstevel@tonic-gate 
874*0Sstevel@tonic-gate 	xprt->xp_next = xprt->xp_prev = NULL;
875*0Sstevel@tonic-gate 
876*0Sstevel@tonic-gate 	/* Decrement list count */
877*0Sstevel@tonic-gate 	pool->p_lcount--;
878*0Sstevel@tonic-gate 
879*0Sstevel@tonic-gate 	rw_exit(&pool->p_lrwlock);
880*0Sstevel@tonic-gate }
881*0Sstevel@tonic-gate 
882*0Sstevel@tonic-gate static void
883*0Sstevel@tonic-gate svc_xprt_qdestroy(SVCPOOL *pool)
884*0Sstevel@tonic-gate {
885*0Sstevel@tonic-gate 	mutex_destroy(&pool->p_qend_lock);
886*0Sstevel@tonic-gate 	kmem_free(pool->p_qbody, pool->p_qsize * sizeof (__SVCXPRT_QNODE));
887*0Sstevel@tonic-gate }
888*0Sstevel@tonic-gate 
889*0Sstevel@tonic-gate /*
890*0Sstevel@tonic-gate  * Initialize an `xprt-ready' queue for a given pool.
891*0Sstevel@tonic-gate  */
892*0Sstevel@tonic-gate static void
893*0Sstevel@tonic-gate svc_xprt_qinit(SVCPOOL *pool, size_t qsize)
894*0Sstevel@tonic-gate {
895*0Sstevel@tonic-gate 	int i;
896*0Sstevel@tonic-gate 
897*0Sstevel@tonic-gate 	pool->p_qsize = qsize;
898*0Sstevel@tonic-gate 	pool->p_qbody = kmem_zalloc(pool->p_qsize * sizeof (__SVCXPRT_QNODE),
899*0Sstevel@tonic-gate 	    KM_SLEEP);
900*0Sstevel@tonic-gate 
901*0Sstevel@tonic-gate 	for (i = 0; i < pool->p_qsize - 1; i++)
902*0Sstevel@tonic-gate 		pool->p_qbody[i].q_next = &(pool->p_qbody[i+1]);
903*0Sstevel@tonic-gate 
904*0Sstevel@tonic-gate 	pool->p_qbody[pool->p_qsize-1].q_next = &(pool->p_qbody[0]);
905*0Sstevel@tonic-gate 	pool->p_qtop = &(pool->p_qbody[0]);
906*0Sstevel@tonic-gate 	pool->p_qend = &(pool->p_qbody[0]);
907*0Sstevel@tonic-gate 
908*0Sstevel@tonic-gate 	mutex_init(&pool->p_qend_lock, NULL, MUTEX_DEFAULT, NULL);
909*0Sstevel@tonic-gate }
910*0Sstevel@tonic-gate 
911*0Sstevel@tonic-gate /*
912*0Sstevel@tonic-gate  * Called from the svc_queuereq() interrupt routine to queue
913*0Sstevel@tonic-gate  * a hint for svc_poll() which transport has a pending request.
914*0Sstevel@tonic-gate  * - insert a pointer to xprt into the xprt-ready queue (FIFO)
915*0Sstevel@tonic-gate  * - if the xprt-ready queue is full turn the overflow flag on.
916*0Sstevel@tonic-gate  *
917*0Sstevel@tonic-gate  * NOTICE: pool->p_qtop is protected by the the pool's request lock
918*0Sstevel@tonic-gate  * and the caller (svc_queuereq()) must hold the lock.
919*0Sstevel@tonic-gate  */
920*0Sstevel@tonic-gate static void
921*0Sstevel@tonic-gate svc_xprt_qput(SVCPOOL *pool, SVCMASTERXPRT *xprt)
922*0Sstevel@tonic-gate {
923*0Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pool->p_req_lock));
924*0Sstevel@tonic-gate 
925*0Sstevel@tonic-gate 	/* If the overflow flag is there is nothing we can do */
926*0Sstevel@tonic-gate 	if (pool->p_qoverflow)
927*0Sstevel@tonic-gate 		return;
928*0Sstevel@tonic-gate 
929*0Sstevel@tonic-gate 	/* If the queue is full turn the overflow flag on and exit */
930*0Sstevel@tonic-gate 	if (pool->p_qtop->q_next == pool->p_qend) {
931*0Sstevel@tonic-gate 		mutex_enter(&pool->p_qend_lock);
932*0Sstevel@tonic-gate 		if (pool->p_qtop->q_next == pool->p_qend) {
933*0Sstevel@tonic-gate 			pool->p_qoverflow = TRUE;
934*0Sstevel@tonic-gate 			mutex_exit(&pool->p_qend_lock);
935*0Sstevel@tonic-gate 			return;
936*0Sstevel@tonic-gate 		}
937*0Sstevel@tonic-gate 		mutex_exit(&pool->p_qend_lock);
938*0Sstevel@tonic-gate 	}
939*0Sstevel@tonic-gate 
940*0Sstevel@tonic-gate 	/* Insert a hint and move pool->p_qtop */
941*0Sstevel@tonic-gate 	pool->p_qtop->q_xprt = xprt;
942*0Sstevel@tonic-gate 	pool->p_qtop = pool->p_qtop->q_next;
943*0Sstevel@tonic-gate }
944*0Sstevel@tonic-gate 
945*0Sstevel@tonic-gate /*
946*0Sstevel@tonic-gate  * Called from svc_poll() to get a hint which transport has a
947*0Sstevel@tonic-gate  * pending request. Returns a pointer to a transport or NULL if the
948*0Sstevel@tonic-gate  * `xprt-ready' queue is empty.
949*0Sstevel@tonic-gate  *
950*0Sstevel@tonic-gate  * Since we do not acquire the pool's request lock while checking if
951*0Sstevel@tonic-gate  * the queue is empty we may miss a request that is just being delivered.
952*0Sstevel@tonic-gate  * However this is ok since svc_poll() will retry again until the
953*0Sstevel@tonic-gate  * count indicates that there are pending requests for this pool.
954*0Sstevel@tonic-gate  */
955*0Sstevel@tonic-gate static SVCMASTERXPRT *
956*0Sstevel@tonic-gate svc_xprt_qget(SVCPOOL *pool)
957*0Sstevel@tonic-gate {
958*0Sstevel@tonic-gate 	SVCMASTERXPRT *xprt;
959*0Sstevel@tonic-gate 
960*0Sstevel@tonic-gate 	mutex_enter(&pool->p_qend_lock);
961*0Sstevel@tonic-gate 	do {
962*0Sstevel@tonic-gate 		/*
963*0Sstevel@tonic-gate 		 * If the queue is empty return NULL.
964*0Sstevel@tonic-gate 		 * Since we do not acquire the pool's request lock which
965*0Sstevel@tonic-gate 		 * protects pool->p_qtop this is not exact check. However,
966*0Sstevel@tonic-gate 		 * this is safe - if we miss a request here svc_poll()
967*0Sstevel@tonic-gate 		 * will retry again.
968*0Sstevel@tonic-gate 		 */
969*0Sstevel@tonic-gate 		if (pool->p_qend == pool->p_qtop) {
970*0Sstevel@tonic-gate 			mutex_exit(&pool->p_qend_lock);
971*0Sstevel@tonic-gate 			return (NULL);
972*0Sstevel@tonic-gate 		}
973*0Sstevel@tonic-gate 
974*0Sstevel@tonic-gate 		/* Get a hint and move pool->p_qend */
975*0Sstevel@tonic-gate 		xprt = pool->p_qend->q_xprt;
976*0Sstevel@tonic-gate 		pool->p_qend = pool->p_qend->q_next;
977*0Sstevel@tonic-gate 
978*0Sstevel@tonic-gate 		/* Skip fields deleted by svc_xprt_qdelete()	 */
979*0Sstevel@tonic-gate 	} while (xprt == NULL);
980*0Sstevel@tonic-gate 	mutex_exit(&pool->p_qend_lock);
981*0Sstevel@tonic-gate 
982*0Sstevel@tonic-gate 	return (xprt);
983*0Sstevel@tonic-gate }
984*0Sstevel@tonic-gate 
985*0Sstevel@tonic-gate /*
986*0Sstevel@tonic-gate  * Reset an overflow in the xprt-ready queue after
987*0Sstevel@tonic-gate  * all the pending requests has been drained.
988*0Sstevel@tonic-gate  * This switches svc_poll back to getting hints from the
989*0Sstevel@tonic-gate  * xprt-ready queue.
990*0Sstevel@tonic-gate  *
991*0Sstevel@tonic-gate  * NOTICE: pool->p_qtop is protected by the the pool's request lock
992*0Sstevel@tonic-gate  * and the caller (svc_poll()) must hold the lock.
993*0Sstevel@tonic-gate  */
994*0Sstevel@tonic-gate static void
995*0Sstevel@tonic-gate svc_xprt_qreset(SVCPOOL *pool)
996*0Sstevel@tonic-gate {
997*0Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pool->p_req_lock));
998*0Sstevel@tonic-gate 
999*0Sstevel@tonic-gate 	pool->p_qend = pool->p_qtop;
1000*0Sstevel@tonic-gate 	pool->p_qoverflow = FALSE;
1001*0Sstevel@tonic-gate }
1002*0Sstevel@tonic-gate 
1003*0Sstevel@tonic-gate /*
1004*0Sstevel@tonic-gate  * Delete all the references to a transport handle that
1005*0Sstevel@tonic-gate  * is being destroyed from the xprt-ready queue.
1006*0Sstevel@tonic-gate  * Deleted pointers are replaced with NULLs.
1007*0Sstevel@tonic-gate  */
1008*0Sstevel@tonic-gate static void
1009*0Sstevel@tonic-gate svc_xprt_qdelete(SVCPOOL *pool, SVCMASTERXPRT *xprt)
1010*0Sstevel@tonic-gate {
1011*0Sstevel@tonic-gate 	__SVCXPRT_QNODE *q = pool->p_qend;
1012*0Sstevel@tonic-gate 	__SVCXPRT_QNODE *qtop = pool->p_qtop;
1013*0Sstevel@tonic-gate 
1014*0Sstevel@tonic-gate 	/*
1015*0Sstevel@tonic-gate 	 * Delete all the references to xprt between the current
1016*0Sstevel@tonic-gate 	 * position of pool->p_qend and current pool->p_qtop.
1017*0Sstevel@tonic-gate 	 */
1018*0Sstevel@tonic-gate 	for (;;) {
1019*0Sstevel@tonic-gate 		if (q->q_xprt == xprt)
1020*0Sstevel@tonic-gate 			q->q_xprt = NULL;
1021*0Sstevel@tonic-gate 		if (q == qtop)
1022*0Sstevel@tonic-gate 			return;
1023*0Sstevel@tonic-gate 		q = q->q_next;
1024*0Sstevel@tonic-gate 	}
1025*0Sstevel@tonic-gate }
1026*0Sstevel@tonic-gate 
1027*0Sstevel@tonic-gate /*
1028*0Sstevel@tonic-gate  * Destructor for a master server transport handle.
1029*0Sstevel@tonic-gate  * - if there are no more non-detached threads linked to this transport
1030*0Sstevel@tonic-gate  *   then, if requested, call xp_closeproc (we don't wait for detached
1031*0Sstevel@tonic-gate  *   threads linked to this transport to complete).
1032*0Sstevel@tonic-gate  * - if there are no more threads linked to this
1033*0Sstevel@tonic-gate  *   transport then
1034*0Sstevel@tonic-gate  *   a) remove references to this transport from the xprt-ready queue
1035*0Sstevel@tonic-gate  *   b) remove a reference to this transport from the pool's transport list
1036*0Sstevel@tonic-gate  *   c) call a transport specific `destroy' function
1037*0Sstevel@tonic-gate  *   d) cancel remaining thread reservations.
1038*0Sstevel@tonic-gate  *
1039*0Sstevel@tonic-gate  * NOTICE: Caller must hold the transport's thread lock.
1040*0Sstevel@tonic-gate  */
1041*0Sstevel@tonic-gate static void
1042*0Sstevel@tonic-gate svc_xprt_cleanup(SVCMASTERXPRT *xprt, bool_t detached)
1043*0Sstevel@tonic-gate {
1044*0Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&xprt->xp_thread_lock));
1045*0Sstevel@tonic-gate 	ASSERT(xprt->xp_wq == NULL);
1046*0Sstevel@tonic-gate 
1047*0Sstevel@tonic-gate 	/*
1048*0Sstevel@tonic-gate 	 * If called from the last non-detached thread
1049*0Sstevel@tonic-gate 	 * it should call the closeproc on this transport.
1050*0Sstevel@tonic-gate 	 */
1051*0Sstevel@tonic-gate 	if (!detached && xprt->xp_threads == 0 && xprt->xp_closeproc) {
1052*0Sstevel@tonic-gate 		(*(xprt->xp_closeproc)) (xprt);
1053*0Sstevel@tonic-gate 	}
1054*0Sstevel@tonic-gate 
1055*0Sstevel@tonic-gate 	if (xprt->xp_threads + xprt->xp_detached_threads > 0)
1056*0Sstevel@tonic-gate 		mutex_exit(&xprt->xp_thread_lock);
1057*0Sstevel@tonic-gate 	else {
1058*0Sstevel@tonic-gate 		/* Remove references to xprt from the `xprt-ready' queue */
1059*0Sstevel@tonic-gate 		svc_xprt_qdelete(xprt->xp_pool, xprt);
1060*0Sstevel@tonic-gate 
1061*0Sstevel@tonic-gate 		/* Unregister xprt from the pool's transport list */
1062*0Sstevel@tonic-gate 		svc_xprt_unregister(xprt);
1063*0Sstevel@tonic-gate 		svc_callout_free(xprt);
1064*0Sstevel@tonic-gate 		SVC_DESTROY(xprt);
1065*0Sstevel@tonic-gate 	}
1066*0Sstevel@tonic-gate }
1067*0Sstevel@tonic-gate 
1068*0Sstevel@tonic-gate /*
1069*0Sstevel@tonic-gate  * Find a dispatch routine for a given prog/vers pair.
1070*0Sstevel@tonic-gate  * This function is called from svc_getreq() to search the callout
1071*0Sstevel@tonic-gate  * table for an entry with a matching RPC program number `prog'
1072*0Sstevel@tonic-gate  * and a version range that covers `vers'.
1073*0Sstevel@tonic-gate  * - if it finds a matching entry it returns pointer to the dispatch routine
1074*0Sstevel@tonic-gate  * - otherwise it returns NULL and, if `minp' or `maxp' are not NULL,
1075*0Sstevel@tonic-gate  *   fills them with, respectively, lowest version and highest version
1076*0Sstevel@tonic-gate  *   supported for the program `prog'
1077*0Sstevel@tonic-gate  */
1078*0Sstevel@tonic-gate static SVC_DISPATCH *
1079*0Sstevel@tonic-gate svc_callout_find(SVCXPRT *xprt, rpcprog_t prog, rpcvers_t vers,
1080*0Sstevel@tonic-gate     rpcvers_t *vers_min, rpcvers_t *vers_max)
1081*0Sstevel@tonic-gate {
1082*0Sstevel@tonic-gate 	SVC_CALLOUT_TABLE *sct = xprt->xp_sct;
1083*0Sstevel@tonic-gate 	int i;
1084*0Sstevel@tonic-gate 
1085*0Sstevel@tonic-gate 	*vers_min = ~(rpcvers_t)0;
1086*0Sstevel@tonic-gate 	*vers_max = 0;
1087*0Sstevel@tonic-gate 
1088*0Sstevel@tonic-gate 	for (i = 0; i < sct->sct_size; i++) {
1089*0Sstevel@tonic-gate 		SVC_CALLOUT *sc = &sct->sct_sc[i];
1090*0Sstevel@tonic-gate 
1091*0Sstevel@tonic-gate 		if (prog == sc->sc_prog) {
1092*0Sstevel@tonic-gate 			if (vers >= sc->sc_versmin && vers <= sc->sc_versmax)
1093*0Sstevel@tonic-gate 				return (sc->sc_dispatch);
1094*0Sstevel@tonic-gate 
1095*0Sstevel@tonic-gate 			if (*vers_max < sc->sc_versmax)
1096*0Sstevel@tonic-gate 				*vers_max = sc->sc_versmax;
1097*0Sstevel@tonic-gate 			if (*vers_min > sc->sc_versmin)
1098*0Sstevel@tonic-gate 				*vers_min = sc->sc_versmin;
1099*0Sstevel@tonic-gate 		}
1100*0Sstevel@tonic-gate 	}
1101*0Sstevel@tonic-gate 
1102*0Sstevel@tonic-gate 	return (NULL);
1103*0Sstevel@tonic-gate }
1104*0Sstevel@tonic-gate 
1105*0Sstevel@tonic-gate /*
1106*0Sstevel@tonic-gate  * Optionally free callout table allocated for this transport by
1107*0Sstevel@tonic-gate  * the service provider.
1108*0Sstevel@tonic-gate  */
1109*0Sstevel@tonic-gate static void
1110*0Sstevel@tonic-gate svc_callout_free(SVCMASTERXPRT *xprt)
1111*0Sstevel@tonic-gate {
1112*0Sstevel@tonic-gate 	SVC_CALLOUT_TABLE *sct = xprt->xp_sct;
1113*0Sstevel@tonic-gate 
1114*0Sstevel@tonic-gate 	if (sct->sct_free) {
1115*0Sstevel@tonic-gate 		kmem_free(sct->sct_sc, sct->sct_size * sizeof (SVC_CALLOUT));
1116*0Sstevel@tonic-gate 		kmem_free(sct, sizeof (SVC_CALLOUT_TABLE));
1117*0Sstevel@tonic-gate 	}
1118*0Sstevel@tonic-gate }
1119*0Sstevel@tonic-gate 
1120*0Sstevel@tonic-gate /*
1121*0Sstevel@tonic-gate  * Send a reply to an RPC request
1122*0Sstevel@tonic-gate  *
1123*0Sstevel@tonic-gate  * PSARC 2003/523 Contract Private Interface
1124*0Sstevel@tonic-gate  * svc_sendreply
1125*0Sstevel@tonic-gate  * Changes must be reviewed by Solaris File Sharing
1126*0Sstevel@tonic-gate  * Changes must be communicated to contract-2003-523@sun.com
1127*0Sstevel@tonic-gate  */
1128*0Sstevel@tonic-gate bool_t
1129*0Sstevel@tonic-gate svc_sendreply(const SVCXPRT *clone_xprt, const xdrproc_t xdr_results,
1130*0Sstevel@tonic-gate     const caddr_t xdr_location)
1131*0Sstevel@tonic-gate {
1132*0Sstevel@tonic-gate 	struct rpc_msg rply;
1133*0Sstevel@tonic-gate 
1134*0Sstevel@tonic-gate 	rply.rm_direction = REPLY;
1135*0Sstevel@tonic-gate 	rply.rm_reply.rp_stat = MSG_ACCEPTED;
1136*0Sstevel@tonic-gate 	rply.acpted_rply.ar_verf = clone_xprt->xp_verf;
1137*0Sstevel@tonic-gate 	rply.acpted_rply.ar_stat = SUCCESS;
1138*0Sstevel@tonic-gate 	rply.acpted_rply.ar_results.where = xdr_location;
1139*0Sstevel@tonic-gate 	rply.acpted_rply.ar_results.proc = xdr_results;
1140*0Sstevel@tonic-gate 
1141*0Sstevel@tonic-gate 	return (SVC_REPLY((SVCXPRT *)clone_xprt, &rply));
1142*0Sstevel@tonic-gate }
1143*0Sstevel@tonic-gate 
1144*0Sstevel@tonic-gate /*
1145*0Sstevel@tonic-gate  * No procedure error reply
1146*0Sstevel@tonic-gate  *
1147*0Sstevel@tonic-gate  * PSARC 2003/523 Contract Private Interface
1148*0Sstevel@tonic-gate  * svcerr_noproc
1149*0Sstevel@tonic-gate  * Changes must be reviewed by Solaris File Sharing
1150*0Sstevel@tonic-gate  * Changes must be communicated to contract-2003-523@sun.com
1151*0Sstevel@tonic-gate  */
1152*0Sstevel@tonic-gate void
1153*0Sstevel@tonic-gate svcerr_noproc(const SVCXPRT *clone_xprt)
1154*0Sstevel@tonic-gate {
1155*0Sstevel@tonic-gate 	struct rpc_msg rply;
1156*0Sstevel@tonic-gate 
1157*0Sstevel@tonic-gate 	rply.rm_direction = REPLY;
1158*0Sstevel@tonic-gate 	rply.rm_reply.rp_stat = MSG_ACCEPTED;
1159*0Sstevel@tonic-gate 	rply.acpted_rply.ar_verf = clone_xprt->xp_verf;
1160*0Sstevel@tonic-gate 	rply.acpted_rply.ar_stat = PROC_UNAVAIL;
1161*0Sstevel@tonic-gate 	SVC_FREERES((SVCXPRT *)clone_xprt);
1162*0Sstevel@tonic-gate 	SVC_REPLY((SVCXPRT *)clone_xprt, &rply);
1163*0Sstevel@tonic-gate }
1164*0Sstevel@tonic-gate 
1165*0Sstevel@tonic-gate /*
1166*0Sstevel@tonic-gate  * Can't decode arguments error reply
1167*0Sstevel@tonic-gate  *
1168*0Sstevel@tonic-gate  * PSARC 2003/523 Contract Private Interface
1169*0Sstevel@tonic-gate  * svcerr_decode
1170*0Sstevel@tonic-gate  * Changes must be reviewed by Solaris File Sharing
1171*0Sstevel@tonic-gate  * Changes must be communicated to contract-2003-523@sun.com
1172*0Sstevel@tonic-gate  */
1173*0Sstevel@tonic-gate void
1174*0Sstevel@tonic-gate svcerr_decode(const SVCXPRT *clone_xprt)
1175*0Sstevel@tonic-gate {
1176*0Sstevel@tonic-gate 	struct rpc_msg rply;
1177*0Sstevel@tonic-gate 
1178*0Sstevel@tonic-gate 	rply.rm_direction = REPLY;
1179*0Sstevel@tonic-gate 	rply.rm_reply.rp_stat = MSG_ACCEPTED;
1180*0Sstevel@tonic-gate 	rply.acpted_rply.ar_verf = clone_xprt->xp_verf;
1181*0Sstevel@tonic-gate 	rply.acpted_rply.ar_stat = GARBAGE_ARGS;
1182*0Sstevel@tonic-gate 	SVC_FREERES((SVCXPRT *)clone_xprt);
1183*0Sstevel@tonic-gate 	SVC_REPLY((SVCXPRT *)clone_xprt, &rply);
1184*0Sstevel@tonic-gate }
1185*0Sstevel@tonic-gate 
1186*0Sstevel@tonic-gate /*
1187*0Sstevel@tonic-gate  * Some system error
1188*0Sstevel@tonic-gate  */
1189*0Sstevel@tonic-gate void
1190*0Sstevel@tonic-gate svcerr_systemerr(const SVCXPRT *clone_xprt)
1191*0Sstevel@tonic-gate {
1192*0Sstevel@tonic-gate 	struct rpc_msg rply;
1193*0Sstevel@tonic-gate 
1194*0Sstevel@tonic-gate 	rply.rm_direction = REPLY;
1195*0Sstevel@tonic-gate 	rply.rm_reply.rp_stat = MSG_ACCEPTED;
1196*0Sstevel@tonic-gate 	rply.acpted_rply.ar_verf = clone_xprt->xp_verf;
1197*0Sstevel@tonic-gate 	rply.acpted_rply.ar_stat = SYSTEM_ERR;
1198*0Sstevel@tonic-gate 	SVC_FREERES((SVCXPRT *)clone_xprt);
1199*0Sstevel@tonic-gate 	SVC_REPLY((SVCXPRT *)clone_xprt, &rply);
1200*0Sstevel@tonic-gate }
1201*0Sstevel@tonic-gate 
1202*0Sstevel@tonic-gate /*
1203*0Sstevel@tonic-gate  * Authentication error reply
1204*0Sstevel@tonic-gate  */
1205*0Sstevel@tonic-gate void
1206*0Sstevel@tonic-gate svcerr_auth(const SVCXPRT *clone_xprt, const enum auth_stat why)
1207*0Sstevel@tonic-gate {
1208*0Sstevel@tonic-gate 	struct rpc_msg rply;
1209*0Sstevel@tonic-gate 
1210*0Sstevel@tonic-gate 	rply.rm_direction = REPLY;
1211*0Sstevel@tonic-gate 	rply.rm_reply.rp_stat = MSG_DENIED;
1212*0Sstevel@tonic-gate 	rply.rjcted_rply.rj_stat = AUTH_ERROR;
1213*0Sstevel@tonic-gate 	rply.rjcted_rply.rj_why = why;
1214*0Sstevel@tonic-gate 	SVC_FREERES((SVCXPRT *)clone_xprt);
1215*0Sstevel@tonic-gate 	SVC_REPLY((SVCXPRT *)clone_xprt, &rply);
1216*0Sstevel@tonic-gate }
1217*0Sstevel@tonic-gate 
1218*0Sstevel@tonic-gate /*
1219*0Sstevel@tonic-gate  * Authentication too weak error reply
1220*0Sstevel@tonic-gate  */
1221*0Sstevel@tonic-gate void
1222*0Sstevel@tonic-gate svcerr_weakauth(const SVCXPRT *clone_xprt)
1223*0Sstevel@tonic-gate {
1224*0Sstevel@tonic-gate 	svcerr_auth((SVCXPRT *)clone_xprt, AUTH_TOOWEAK);
1225*0Sstevel@tonic-gate }
1226*0Sstevel@tonic-gate 
1227*0Sstevel@tonic-gate /*
1228*0Sstevel@tonic-gate  * Program unavailable error reply
1229*0Sstevel@tonic-gate  *
1230*0Sstevel@tonic-gate  * PSARC 2003/523 Contract Private Interface
1231*0Sstevel@tonic-gate  * svcerr_noprog
1232*0Sstevel@tonic-gate  * Changes must be reviewed by Solaris File Sharing
1233*0Sstevel@tonic-gate  * Changes must be communicated to contract-2003-523@sun.com
1234*0Sstevel@tonic-gate  */
1235*0Sstevel@tonic-gate void
1236*0Sstevel@tonic-gate svcerr_noprog(const SVCXPRT *clone_xprt)
1237*0Sstevel@tonic-gate {
1238*0Sstevel@tonic-gate 	struct rpc_msg rply;
1239*0Sstevel@tonic-gate 
1240*0Sstevel@tonic-gate 	rply.rm_direction = REPLY;
1241*0Sstevel@tonic-gate 	rply.rm_reply.rp_stat = MSG_ACCEPTED;
1242*0Sstevel@tonic-gate 	rply.acpted_rply.ar_verf = clone_xprt->xp_verf;
1243*0Sstevel@tonic-gate 	rply.acpted_rply.ar_stat = PROG_UNAVAIL;
1244*0Sstevel@tonic-gate 	SVC_FREERES((SVCXPRT *)clone_xprt);
1245*0Sstevel@tonic-gate 	SVC_REPLY((SVCXPRT *)clone_xprt, &rply);
1246*0Sstevel@tonic-gate }
1247*0Sstevel@tonic-gate 
1248*0Sstevel@tonic-gate /*
1249*0Sstevel@tonic-gate  * Program version mismatch error reply
1250*0Sstevel@tonic-gate  *
1251*0Sstevel@tonic-gate  * PSARC 2003/523 Contract Private Interface
1252*0Sstevel@tonic-gate  * svcerr_progvers
1253*0Sstevel@tonic-gate  * Changes must be reviewed by Solaris File Sharing
1254*0Sstevel@tonic-gate  * Changes must be communicated to contract-2003-523@sun.com
1255*0Sstevel@tonic-gate  */
1256*0Sstevel@tonic-gate void
1257*0Sstevel@tonic-gate svcerr_progvers(const SVCXPRT *clone_xprt,
1258*0Sstevel@tonic-gate     const rpcvers_t low_vers, const rpcvers_t high_vers)
1259*0Sstevel@tonic-gate {
1260*0Sstevel@tonic-gate 	struct rpc_msg rply;
1261*0Sstevel@tonic-gate 
1262*0Sstevel@tonic-gate 	rply.rm_direction = REPLY;
1263*0Sstevel@tonic-gate 	rply.rm_reply.rp_stat = MSG_ACCEPTED;
1264*0Sstevel@tonic-gate 	rply.acpted_rply.ar_verf = clone_xprt->xp_verf;
1265*0Sstevel@tonic-gate 	rply.acpted_rply.ar_stat = PROG_MISMATCH;
1266*0Sstevel@tonic-gate 	rply.acpted_rply.ar_vers.low = low_vers;
1267*0Sstevel@tonic-gate 	rply.acpted_rply.ar_vers.high = high_vers;
1268*0Sstevel@tonic-gate 	SVC_FREERES((SVCXPRT *)clone_xprt);
1269*0Sstevel@tonic-gate 	SVC_REPLY((SVCXPRT *)clone_xprt, &rply);
1270*0Sstevel@tonic-gate }
1271*0Sstevel@tonic-gate 
1272*0Sstevel@tonic-gate /*
1273*0Sstevel@tonic-gate  * Get server side input from some transport.
1274*0Sstevel@tonic-gate  *
1275*0Sstevel@tonic-gate  * Statement of authentication parameters management:
1276*0Sstevel@tonic-gate  * This function owns and manages all authentication parameters, specifically
1277*0Sstevel@tonic-gate  * the "raw" parameters (msg.rm_call.cb_cred and msg.rm_call.cb_verf) and
1278*0Sstevel@tonic-gate  * the "cooked" credentials (rqst->rq_clntcred).
1279*0Sstevel@tonic-gate  * However, this function does not know the structure of the cooked
1280*0Sstevel@tonic-gate  * credentials, so it make the following assumptions:
1281*0Sstevel@tonic-gate  *   a) the structure is contiguous (no pointers), and
1282*0Sstevel@tonic-gate  *   b) the cred structure size does not exceed RQCRED_SIZE bytes.
1283*0Sstevel@tonic-gate  * In all events, all three parameters are freed upon exit from this routine.
1284*0Sstevel@tonic-gate  * The storage is trivially managed on the call stack in user land, but
1285*0Sstevel@tonic-gate  * is malloced in kernel land.
1286*0Sstevel@tonic-gate  *
1287*0Sstevel@tonic-gate  * Note: the xprt's xp_svc_lock is not held while the service's dispatch
1288*0Sstevel@tonic-gate  * routine is running.	If we decide to implement svc_unregister(), we'll
1289*0Sstevel@tonic-gate  * need to decide whether it's okay for a thread to unregister a service
1290*0Sstevel@tonic-gate  * while a request is being processed.	If we decide that this is a
1291*0Sstevel@tonic-gate  * problem, we can probably use some sort of reference counting scheme to
1292*0Sstevel@tonic-gate  * keep the callout entry from going away until the request has completed.
1293*0Sstevel@tonic-gate  */
1294*0Sstevel@tonic-gate static void
1295*0Sstevel@tonic-gate svc_getreq(
1296*0Sstevel@tonic-gate 	SVCXPRT *clone_xprt,	/* clone transport handle */
1297*0Sstevel@tonic-gate 	mblk_t *mp)
1298*0Sstevel@tonic-gate {
1299*0Sstevel@tonic-gate 	struct rpc_msg msg;
1300*0Sstevel@tonic-gate 	struct svc_req r;
1301*0Sstevel@tonic-gate 	char  *cred_area;	/* too big to allocate on call stack */
1302*0Sstevel@tonic-gate 
1303*0Sstevel@tonic-gate 	TRACE_0(TR_FAC_KRPC, TR_SVC_GETREQ_START,
1304*0Sstevel@tonic-gate 	    "svc_getreq_start:");
1305*0Sstevel@tonic-gate 
1306*0Sstevel@tonic-gate 	ASSERT(clone_xprt->xp_master != NULL);
1307*0Sstevel@tonic-gate 
1308*0Sstevel@tonic-gate 	/*
1309*0Sstevel@tonic-gate 	 * Firstly, allocate the authentication parameters' storage
1310*0Sstevel@tonic-gate 	 */
1311*0Sstevel@tonic-gate 	mutex_enter(&rqcred_lock);
1312*0Sstevel@tonic-gate 	if (rqcred_head) {
1313*0Sstevel@tonic-gate 		cred_area = rqcred_head;
1314*0Sstevel@tonic-gate 
1315*0Sstevel@tonic-gate 		/* LINTED pointer alignment */
1316*0Sstevel@tonic-gate 		rqcred_head = *(caddr_t *)rqcred_head;
1317*0Sstevel@tonic-gate 		mutex_exit(&rqcred_lock);
1318*0Sstevel@tonic-gate 	} else {
1319*0Sstevel@tonic-gate 		mutex_exit(&rqcred_lock);
1320*0Sstevel@tonic-gate 		cred_area = kmem_alloc(2 * MAX_AUTH_BYTES + RQCRED_SIZE,
1321*0Sstevel@tonic-gate 		    KM_SLEEP);
1322*0Sstevel@tonic-gate 	}
1323*0Sstevel@tonic-gate 	msg.rm_call.cb_cred.oa_base = cred_area;
1324*0Sstevel@tonic-gate 	msg.rm_call.cb_verf.oa_base = &(cred_area[MAX_AUTH_BYTES]);
1325*0Sstevel@tonic-gate 	r.rq_clntcred = &(cred_area[2 * MAX_AUTH_BYTES]);
1326*0Sstevel@tonic-gate 
1327*0Sstevel@tonic-gate 	/*
1328*0Sstevel@tonic-gate 	 * Now receive a message from the transport.
1329*0Sstevel@tonic-gate 	 */
1330*0Sstevel@tonic-gate 	if (SVC_RECV(clone_xprt, mp, &msg)) {
1331*0Sstevel@tonic-gate 		void (*dispatchroutine) (struct svc_req *, SVCXPRT *);
1332*0Sstevel@tonic-gate 		rpcvers_t vers_min;
1333*0Sstevel@tonic-gate 		rpcvers_t vers_max;
1334*0Sstevel@tonic-gate 		bool_t no_dispatch;
1335*0Sstevel@tonic-gate 		enum auth_stat why;
1336*0Sstevel@tonic-gate 
1337*0Sstevel@tonic-gate 		/*
1338*0Sstevel@tonic-gate 		 * Find the registered program and call its
1339*0Sstevel@tonic-gate 		 * dispatch routine.
1340*0Sstevel@tonic-gate 		 */
1341*0Sstevel@tonic-gate 		r.rq_xprt = clone_xprt;
1342*0Sstevel@tonic-gate 		r.rq_prog = msg.rm_call.cb_prog;
1343*0Sstevel@tonic-gate 		r.rq_vers = msg.rm_call.cb_vers;
1344*0Sstevel@tonic-gate 		r.rq_proc = msg.rm_call.cb_proc;
1345*0Sstevel@tonic-gate 		r.rq_cred = msg.rm_call.cb_cred;
1346*0Sstevel@tonic-gate 
1347*0Sstevel@tonic-gate 		/*
1348*0Sstevel@tonic-gate 		 * First authenticate the message.
1349*0Sstevel@tonic-gate 		 */
1350*0Sstevel@tonic-gate 		TRACE_0(TR_FAC_KRPC, TR_SVC_GETREQ_AUTH_START,
1351*0Sstevel@tonic-gate 		    "svc_getreq_auth_start:");
1352*0Sstevel@tonic-gate 		if ((why = sec_svc_msg(&r, &msg, &no_dispatch)) != AUTH_OK) {
1353*0Sstevel@tonic-gate 			TRACE_1(TR_FAC_KRPC, TR_SVC_GETREQ_AUTH_END,
1354*0Sstevel@tonic-gate 			    "svc_getreq_auth_end:(%S)", "failed");
1355*0Sstevel@tonic-gate 			svcerr_auth(clone_xprt, why);
1356*0Sstevel@tonic-gate 			/*
1357*0Sstevel@tonic-gate 			 * Free the arguments.
1358*0Sstevel@tonic-gate 			 */
1359*0Sstevel@tonic-gate 			(void) SVC_FREEARGS(clone_xprt, NULL, NULL);
1360*0Sstevel@tonic-gate 		} else if (no_dispatch) {
1361*0Sstevel@tonic-gate 			/*
1362*0Sstevel@tonic-gate 			 * XXX - when bug id 4053736 is done, remove
1363*0Sstevel@tonic-gate 			 * the SVC_FREEARGS() call.
1364*0Sstevel@tonic-gate 			 */
1365*0Sstevel@tonic-gate 			(void) SVC_FREEARGS(clone_xprt, NULL, NULL);
1366*0Sstevel@tonic-gate 		} else {
1367*0Sstevel@tonic-gate 			TRACE_1(TR_FAC_KRPC, TR_SVC_GETREQ_AUTH_END,
1368*0Sstevel@tonic-gate 			    "svc_getreq_auth_end:(%S)", "good");
1369*0Sstevel@tonic-gate 
1370*0Sstevel@tonic-gate 			dispatchroutine = svc_callout_find(clone_xprt,
1371*0Sstevel@tonic-gate 			    r.rq_prog, r.rq_vers, &vers_min, &vers_max);
1372*0Sstevel@tonic-gate 
1373*0Sstevel@tonic-gate 			if (dispatchroutine) {
1374*0Sstevel@tonic-gate 				(*dispatchroutine) (&r, clone_xprt);
1375*0Sstevel@tonic-gate 			} else {
1376*0Sstevel@tonic-gate 				/*
1377*0Sstevel@tonic-gate 				 * If we got here, the program or version
1378*0Sstevel@tonic-gate 				 * is not served ...
1379*0Sstevel@tonic-gate 				 */
1380*0Sstevel@tonic-gate 				if (vers_max == 0 ||
1381*0Sstevel@tonic-gate 				    version_keepquiet(clone_xprt))
1382*0Sstevel@tonic-gate 					svcerr_noprog(clone_xprt);
1383*0Sstevel@tonic-gate 				else
1384*0Sstevel@tonic-gate 					svcerr_progvers(clone_xprt, vers_min,
1385*0Sstevel@tonic-gate 					    vers_max);
1386*0Sstevel@tonic-gate 
1387*0Sstevel@tonic-gate 				/*
1388*0Sstevel@tonic-gate 				 * Free the arguments. For successful calls
1389*0Sstevel@tonic-gate 				 * this is done by the dispatch routine.
1390*0Sstevel@tonic-gate 				 */
1391*0Sstevel@tonic-gate 				(void) SVC_FREEARGS(clone_xprt, NULL, NULL);
1392*0Sstevel@tonic-gate 				/* Fall through to ... */
1393*0Sstevel@tonic-gate 			}
1394*0Sstevel@tonic-gate 			/*
1395*0Sstevel@tonic-gate 			 * Call cleanup procedure for RPCSEC_GSS.
1396*0Sstevel@tonic-gate 			 * This is a hack since there is currently no
1397*0Sstevel@tonic-gate 			 * op, such as SVC_CLEANAUTH. rpc_gss_cleanup
1398*0Sstevel@tonic-gate 			 * should only be called for a non null proc.
1399*0Sstevel@tonic-gate 			 * Null procs in RPC GSS are overloaded to
1400*0Sstevel@tonic-gate 			 * provide context setup and control. The main
1401*0Sstevel@tonic-gate 			 * purpose of rpc_gss_cleanup is to decrement the
1402*0Sstevel@tonic-gate 			 * reference count associated with the cached
1403*0Sstevel@tonic-gate 			 * GSS security context. We should never get here
1404*0Sstevel@tonic-gate 			 * for an RPCSEC_GSS null proc since *no_dispatch
1405*0Sstevel@tonic-gate 			 * would have been set to true from sec_svc_msg above.
1406*0Sstevel@tonic-gate 			 */
1407*0Sstevel@tonic-gate 			if (r.rq_cred.oa_flavor == RPCSEC_GSS)
1408*0Sstevel@tonic-gate 				rpc_gss_cleanup(clone_xprt);
1409*0Sstevel@tonic-gate 		}
1410*0Sstevel@tonic-gate 	}
1411*0Sstevel@tonic-gate 
1412*0Sstevel@tonic-gate 	/*
1413*0Sstevel@tonic-gate 	 * Free authentication parameters' storage
1414*0Sstevel@tonic-gate 	 */
1415*0Sstevel@tonic-gate 	mutex_enter(&rqcred_lock);
1416*0Sstevel@tonic-gate 	/* LINTED pointer alignment */
1417*0Sstevel@tonic-gate 	*(caddr_t *)cred_area = rqcred_head;
1418*0Sstevel@tonic-gate 	rqcred_head = cred_area;
1419*0Sstevel@tonic-gate 	mutex_exit(&rqcred_lock);
1420*0Sstevel@tonic-gate }
1421*0Sstevel@tonic-gate 
1422*0Sstevel@tonic-gate /*
1423*0Sstevel@tonic-gate  * Allocate new clone transport handle.
1424*0Sstevel@tonic-gate  */
1425*0Sstevel@tonic-gate static SVCXPRT *
1426*0Sstevel@tonic-gate svc_clone_init(void)
1427*0Sstevel@tonic-gate {
1428*0Sstevel@tonic-gate 	SVCXPRT *clone_xprt;
1429*0Sstevel@tonic-gate 
1430*0Sstevel@tonic-gate 	clone_xprt = kmem_zalloc(sizeof (SVCXPRT), KM_SLEEP);
1431*0Sstevel@tonic-gate 	clone_xprt->xp_cred = crget();
1432*0Sstevel@tonic-gate 	return (clone_xprt);
1433*0Sstevel@tonic-gate }
1434*0Sstevel@tonic-gate 
1435*0Sstevel@tonic-gate /*
1436*0Sstevel@tonic-gate  * Free memory allocated by svc_clone_init.
1437*0Sstevel@tonic-gate  */
1438*0Sstevel@tonic-gate static void
1439*0Sstevel@tonic-gate svc_clone_free(SVCXPRT *clone_xprt)
1440*0Sstevel@tonic-gate {
1441*0Sstevel@tonic-gate 	/* Fre credentials from crget() */
1442*0Sstevel@tonic-gate 	if (clone_xprt->xp_cred)
1443*0Sstevel@tonic-gate 		crfree(clone_xprt->xp_cred);
1444*0Sstevel@tonic-gate 
1445*0Sstevel@tonic-gate 	kmem_free(clone_xprt, sizeof (SVCXPRT));
1446*0Sstevel@tonic-gate }
1447*0Sstevel@tonic-gate 
1448*0Sstevel@tonic-gate /*
1449*0Sstevel@tonic-gate  * Link a per-thread clone transport handle to a master
1450*0Sstevel@tonic-gate  * - increment a thread reference count on the master
1451*0Sstevel@tonic-gate  * - copy some of the master's fields to the clone
1452*0Sstevel@tonic-gate  * - call a transport specific clone routine.
1453*0Sstevel@tonic-gate  */
1454*0Sstevel@tonic-gate static void
1455*0Sstevel@tonic-gate svc_clone_link(SVCMASTERXPRT *xprt, SVCXPRT *clone_xprt)
1456*0Sstevel@tonic-gate {
1457*0Sstevel@tonic-gate 	cred_t *cred = clone_xprt->xp_cred;
1458*0Sstevel@tonic-gate 
1459*0Sstevel@tonic-gate 	ASSERT(cred);
1460*0Sstevel@tonic-gate 
1461*0Sstevel@tonic-gate 	/*
1462*0Sstevel@tonic-gate 	 * Bump up master's thread count.
1463*0Sstevel@tonic-gate 	 * Linking a per-thread clone transport handle to a master
1464*0Sstevel@tonic-gate 	 * associates a service thread with the master.
1465*0Sstevel@tonic-gate 	 */
1466*0Sstevel@tonic-gate 	mutex_enter(&xprt->xp_thread_lock);
1467*0Sstevel@tonic-gate 	xprt->xp_threads++;
1468*0Sstevel@tonic-gate 	mutex_exit(&xprt->xp_thread_lock);
1469*0Sstevel@tonic-gate 
1470*0Sstevel@tonic-gate 	/* Clear everything */
1471*0Sstevel@tonic-gate 	bzero(clone_xprt, sizeof (SVCXPRT));
1472*0Sstevel@tonic-gate 
1473*0Sstevel@tonic-gate 	/* Set pointer to the master transport stucture */
1474*0Sstevel@tonic-gate 	clone_xprt->xp_master = xprt;
1475*0Sstevel@tonic-gate 
1476*0Sstevel@tonic-gate 	/* Structure copy of all the common fields */
1477*0Sstevel@tonic-gate 	clone_xprt->xp_xpc = xprt->xp_xpc;
1478*0Sstevel@tonic-gate 
1479*0Sstevel@tonic-gate 	/* Restore per-thread fields (xp_cred) */
1480*0Sstevel@tonic-gate 	clone_xprt->xp_cred = cred;
1481*0Sstevel@tonic-gate 
1482*0Sstevel@tonic-gate 	/*
1483*0Sstevel@tonic-gate 	 * NOTICE: There is no transport-type specific code now.
1484*0Sstevel@tonic-gate 	 *	   If you want to add a transport-type specific cloning code
1485*0Sstevel@tonic-gate 	 *	   add one more operation (e.g. xp_clone()) to svc_ops,
1486*0Sstevel@tonic-gate 	 *	   implement it for each transport type, and call it here
1487*0Sstevel@tonic-gate 	 *	   through an appropriate macro (e.g. SVC_CLONE()).
1488*0Sstevel@tonic-gate 	 */
1489*0Sstevel@tonic-gate }
1490*0Sstevel@tonic-gate 
1491*0Sstevel@tonic-gate /*
1492*0Sstevel@tonic-gate  * Unlink a non-detached clone transport handle from a master
1493*0Sstevel@tonic-gate  * - decrement a thread reference count on the master
1494*0Sstevel@tonic-gate  * - if the transport is closing (xp_wq is NULL) call svc_xprt_cleanup();
1495*0Sstevel@tonic-gate  *   if this is the last non-detached/absolute thread on this transport
1496*0Sstevel@tonic-gate  *   then it will close/destroy the transport
1497*0Sstevel@tonic-gate  * - call transport specific function to destroy the clone handle
1498*0Sstevel@tonic-gate  * - clear xp_master to avoid recursion.
1499*0Sstevel@tonic-gate  */
1500*0Sstevel@tonic-gate static void
1501*0Sstevel@tonic-gate svc_clone_unlink(SVCXPRT *clone_xprt)
1502*0Sstevel@tonic-gate {
1503*0Sstevel@tonic-gate 	SVCMASTERXPRT *xprt = clone_xprt->xp_master;
1504*0Sstevel@tonic-gate 
1505*0Sstevel@tonic-gate 	/* This cannot be a detached thread */
1506*0Sstevel@tonic-gate 	ASSERT(!clone_xprt->xp_detached);
1507*0Sstevel@tonic-gate 	ASSERT(xprt->xp_threads > 0);
1508*0Sstevel@tonic-gate 
1509*0Sstevel@tonic-gate 	/* Decrement a reference count on the transport */
1510*0Sstevel@tonic-gate 	mutex_enter(&xprt->xp_thread_lock);
1511*0Sstevel@tonic-gate 	xprt->xp_threads--;
1512*0Sstevel@tonic-gate 
1513*0Sstevel@tonic-gate 	/* svc_xprt_cleanup() unlocks xp_thread_lock or destroys xprt */
1514*0Sstevel@tonic-gate 	if (xprt->xp_wq)
1515*0Sstevel@tonic-gate 		mutex_exit(&xprt->xp_thread_lock);
1516*0Sstevel@tonic-gate 	else
1517*0Sstevel@tonic-gate 		svc_xprt_cleanup(xprt, FALSE);
1518*0Sstevel@tonic-gate 
1519*0Sstevel@tonic-gate 	/* Call a transport specific clone `destroy' function */
1520*0Sstevel@tonic-gate 	SVC_CLONE_DESTROY(clone_xprt);
1521*0Sstevel@tonic-gate 
1522*0Sstevel@tonic-gate 	/* Clear xp_master */
1523*0Sstevel@tonic-gate 	clone_xprt->xp_master = NULL;
1524*0Sstevel@tonic-gate }
1525*0Sstevel@tonic-gate 
1526*0Sstevel@tonic-gate /*
1527*0Sstevel@tonic-gate  * Unlink a detached clone transport handle from a master
1528*0Sstevel@tonic-gate  * - decrement the thread count on the master
1529*0Sstevel@tonic-gate  * - if the transport is closing (xp_wq is NULL) call svc_xprt_cleanup();
1530*0Sstevel@tonic-gate  *   if this is the last thread on this transport then it will destroy
1531*0Sstevel@tonic-gate  *   the transport.
1532*0Sstevel@tonic-gate  * - call a transport specific function to destroy the clone handle
1533*0Sstevel@tonic-gate  * - clear xp_master to avoid recursion.
1534*0Sstevel@tonic-gate  */
1535*0Sstevel@tonic-gate static void
1536*0Sstevel@tonic-gate svc_clone_unlinkdetached(SVCXPRT *clone_xprt)
1537*0Sstevel@tonic-gate {
1538*0Sstevel@tonic-gate 	SVCMASTERXPRT *xprt = clone_xprt->xp_master;
1539*0Sstevel@tonic-gate 
1540*0Sstevel@tonic-gate 	/* This must be a detached thread */
1541*0Sstevel@tonic-gate 	ASSERT(clone_xprt->xp_detached);
1542*0Sstevel@tonic-gate 	ASSERT(xprt->xp_detached_threads > 0);
1543*0Sstevel@tonic-gate 	ASSERT(xprt->xp_threads + xprt->xp_detached_threads > 0);
1544*0Sstevel@tonic-gate 
1545*0Sstevel@tonic-gate 	/* Grab xprt->xp_thread_lock and decrement link counts */
1546*0Sstevel@tonic-gate 	mutex_enter(&xprt->xp_thread_lock);
1547*0Sstevel@tonic-gate 	xprt->xp_detached_threads--;
1548*0Sstevel@tonic-gate 
1549*0Sstevel@tonic-gate 	/* svc_xprt_cleanup() unlocks xp_thread_lock or destroys xprt */
1550*0Sstevel@tonic-gate 	if (xprt->xp_wq)
1551*0Sstevel@tonic-gate 		mutex_exit(&xprt->xp_thread_lock);
1552*0Sstevel@tonic-gate 	else
1553*0Sstevel@tonic-gate 		svc_xprt_cleanup(xprt, TRUE);
1554*0Sstevel@tonic-gate 
1555*0Sstevel@tonic-gate 	/* Call transport specific clone `destroy' function */
1556*0Sstevel@tonic-gate 	SVC_CLONE_DESTROY(clone_xprt);
1557*0Sstevel@tonic-gate 
1558*0Sstevel@tonic-gate 	/* Clear xp_master */
1559*0Sstevel@tonic-gate 	clone_xprt->xp_master = NULL;
1560*0Sstevel@tonic-gate }
1561*0Sstevel@tonic-gate 
1562*0Sstevel@tonic-gate /*
1563*0Sstevel@tonic-gate  * Try to exit a non-detached service thread
1564*0Sstevel@tonic-gate  * - check if there are enough threads left
1565*0Sstevel@tonic-gate  * - if this thread (ie its clone transport handle) are linked
1566*0Sstevel@tonic-gate  *   to a master transport then unlink it
1567*0Sstevel@tonic-gate  * - free the clone structure
1568*0Sstevel@tonic-gate  * - return to userland for thread exit
1569*0Sstevel@tonic-gate  *
1570*0Sstevel@tonic-gate  * If this is the last non-detached or the last thread on this
1571*0Sstevel@tonic-gate  * transport then the call to svc_clone_unlink() will, respectively,
1572*0Sstevel@tonic-gate  * close and/or destroy the transport.
1573*0Sstevel@tonic-gate  */
1574*0Sstevel@tonic-gate static void
1575*0Sstevel@tonic-gate svc_thread_exit(SVCPOOL *pool, SVCXPRT *clone_xprt)
1576*0Sstevel@tonic-gate {
1577*0Sstevel@tonic-gate 	if (clone_xprt->xp_master)
1578*0Sstevel@tonic-gate 		svc_clone_unlink(clone_xprt);
1579*0Sstevel@tonic-gate 	svc_clone_free(clone_xprt);
1580*0Sstevel@tonic-gate 
1581*0Sstevel@tonic-gate 	mutex_enter(&pool->p_thread_lock);
1582*0Sstevel@tonic-gate 	pool->p_threads--;
1583*0Sstevel@tonic-gate 	if (pool->p_closing && svc_pool_tryexit(pool))
1584*0Sstevel@tonic-gate 		/* return -  thread exit will be handled at user level */
1585*0Sstevel@tonic-gate 		return;
1586*0Sstevel@tonic-gate 	mutex_exit(&pool->p_thread_lock);
1587*0Sstevel@tonic-gate 
1588*0Sstevel@tonic-gate 	/* return -  thread exit will be handled at user level */
1589*0Sstevel@tonic-gate }
1590*0Sstevel@tonic-gate 
1591*0Sstevel@tonic-gate /*
1592*0Sstevel@tonic-gate  * Exit a detached service thread that returned to svc_run
1593*0Sstevel@tonic-gate  * - decrement the `detached thread' count for the pool
1594*0Sstevel@tonic-gate  * - unlink the detached clone transport handle from the master
1595*0Sstevel@tonic-gate  * - free the clone structure
1596*0Sstevel@tonic-gate  * - return to userland for thread exit
1597*0Sstevel@tonic-gate  *
1598*0Sstevel@tonic-gate  * If this is the last thread on this transport then the call
1599*0Sstevel@tonic-gate  * to svc_clone_unlinkdetached() will destroy the transport.
1600*0Sstevel@tonic-gate  */
1601*0Sstevel@tonic-gate static void
1602*0Sstevel@tonic-gate svc_thread_exitdetached(SVCPOOL *pool, SVCXPRT *clone_xprt)
1603*0Sstevel@tonic-gate {
1604*0Sstevel@tonic-gate 	/* This must be a detached thread */
1605*0Sstevel@tonic-gate 	ASSERT(clone_xprt->xp_master);
1606*0Sstevel@tonic-gate 	ASSERT(clone_xprt->xp_detached);
1607*0Sstevel@tonic-gate 	ASSERT(!MUTEX_HELD(&pool->p_thread_lock));
1608*0Sstevel@tonic-gate 
1609*0Sstevel@tonic-gate 	svc_clone_unlinkdetached(clone_xprt);
1610*0Sstevel@tonic-gate 	svc_clone_free(clone_xprt);
1611*0Sstevel@tonic-gate 
1612*0Sstevel@tonic-gate 	mutex_enter(&pool->p_thread_lock);
1613*0Sstevel@tonic-gate 
1614*0Sstevel@tonic-gate 	ASSERT(pool->p_reserved_threads >= 0);
1615*0Sstevel@tonic-gate 	ASSERT(pool->p_detached_threads > 0);
1616*0Sstevel@tonic-gate 
1617*0Sstevel@tonic-gate 	pool->p_detached_threads--;
1618*0Sstevel@tonic-gate 	if (pool->p_closing && svc_pool_tryexit(pool))
1619*0Sstevel@tonic-gate 		/* return -  thread exit will be handled at user level */
1620*0Sstevel@tonic-gate 		return;
1621*0Sstevel@tonic-gate 	mutex_exit(&pool->p_thread_lock);
1622*0Sstevel@tonic-gate 
1623*0Sstevel@tonic-gate 	/* return -  thread exit will be handled at user level */
1624*0Sstevel@tonic-gate }
1625*0Sstevel@tonic-gate 
1626*0Sstevel@tonic-gate /*
1627*0Sstevel@tonic-gate  * PSARC 2003/523 Contract Private Interface
1628*0Sstevel@tonic-gate  * svc_wait
1629*0Sstevel@tonic-gate  * Changes must be reviewed by Solaris File Sharing
1630*0Sstevel@tonic-gate  * Changes must be communicated to contract-2003-523@sun.com
1631*0Sstevel@tonic-gate  */
1632*0Sstevel@tonic-gate int
1633*0Sstevel@tonic-gate svc_wait(int id)
1634*0Sstevel@tonic-gate {
1635*0Sstevel@tonic-gate 	SVCPOOL *pool;
1636*0Sstevel@tonic-gate 	int	err = 0;
1637*0Sstevel@tonic-gate 	struct svc_globals *svc;
1638*0Sstevel@tonic-gate 
1639*0Sstevel@tonic-gate 	svc = zone_getspecific(svc_zone_key, curproc->p_zone);
1640*0Sstevel@tonic-gate 	mutex_enter(&svc->svc_plock);
1641*0Sstevel@tonic-gate 	pool = svc_pool_find(svc, id);
1642*0Sstevel@tonic-gate 	mutex_exit(&svc->svc_plock);
1643*0Sstevel@tonic-gate 
1644*0Sstevel@tonic-gate 	if (pool == NULL)
1645*0Sstevel@tonic-gate 		return (ENOENT);
1646*0Sstevel@tonic-gate 
1647*0Sstevel@tonic-gate 	mutex_enter(&pool->p_user_lock);
1648*0Sstevel@tonic-gate 
1649*0Sstevel@tonic-gate 	/* Check if there's already a user thread waiting on this pool */
1650*0Sstevel@tonic-gate 	if (pool->p_user_waiting) {
1651*0Sstevel@tonic-gate 		mutex_exit(&pool->p_user_lock);
1652*0Sstevel@tonic-gate 		return (EBUSY);
1653*0Sstevel@tonic-gate 	}
1654*0Sstevel@tonic-gate 
1655*0Sstevel@tonic-gate 	pool->p_user_waiting = TRUE;
1656*0Sstevel@tonic-gate 
1657*0Sstevel@tonic-gate 	/* Go to sleep, waiting for the signaled flag. */
1658*0Sstevel@tonic-gate 	while (!pool->p_signal_create_thread && !pool->p_user_exit) {
1659*0Sstevel@tonic-gate 		if (cv_wait_sig(&pool->p_user_cv, &pool->p_user_lock) == 0) {
1660*0Sstevel@tonic-gate 			/* Interrupted, return to handle exit or signal */
1661*0Sstevel@tonic-gate 			pool->p_user_waiting = FALSE;
1662*0Sstevel@tonic-gate 			pool->p_signal_create_thread = FALSE;
1663*0Sstevel@tonic-gate 			mutex_exit(&pool->p_user_lock);
1664*0Sstevel@tonic-gate 
1665*0Sstevel@tonic-gate 			/*
1666*0Sstevel@tonic-gate 			 * Thread has been interrupted and therefore
1667*0Sstevel@tonic-gate 			 * the service daemon is leaving as well so
1668*0Sstevel@tonic-gate 			 * let's go ahead and remove the service
1669*0Sstevel@tonic-gate 			 * pool at this time.
1670*0Sstevel@tonic-gate 			 */
1671*0Sstevel@tonic-gate 			mutex_enter(&svc->svc_plock);
1672*0Sstevel@tonic-gate 			svc_pool_unregister(svc, pool);
1673*0Sstevel@tonic-gate 			mutex_exit(&svc->svc_plock);
1674*0Sstevel@tonic-gate 
1675*0Sstevel@tonic-gate 			return (EINTR);
1676*0Sstevel@tonic-gate 		}
1677*0Sstevel@tonic-gate 	}
1678*0Sstevel@tonic-gate 
1679*0Sstevel@tonic-gate 	pool->p_signal_create_thread = FALSE;
1680*0Sstevel@tonic-gate 	pool->p_user_waiting = FALSE;
1681*0Sstevel@tonic-gate 
1682*0Sstevel@tonic-gate 	/*
1683*0Sstevel@tonic-gate 	 * About to exit the service pool. Set return value
1684*0Sstevel@tonic-gate 	 * to let the userland code know our intent. Signal
1685*0Sstevel@tonic-gate 	 * svc_thread_creator() so that it can clean up the
1686*0Sstevel@tonic-gate 	 * pool structure.
1687*0Sstevel@tonic-gate 	 */
1688*0Sstevel@tonic-gate 	if (pool->p_user_exit) {
1689*0Sstevel@tonic-gate 		err = ECANCELED;
1690*0Sstevel@tonic-gate 		cv_signal(&pool->p_user_cv);
1691*0Sstevel@tonic-gate 	}
1692*0Sstevel@tonic-gate 
1693*0Sstevel@tonic-gate 	mutex_exit(&pool->p_user_lock);
1694*0Sstevel@tonic-gate 
1695*0Sstevel@tonic-gate 	/* Return to userland with error code, for possible thread creation. */
1696*0Sstevel@tonic-gate 	return (err);
1697*0Sstevel@tonic-gate }
1698*0Sstevel@tonic-gate 
1699*0Sstevel@tonic-gate /*
1700*0Sstevel@tonic-gate  * `Service threads' creator thread.
1701*0Sstevel@tonic-gate  * The creator thread waits for a signal to create new thread.
1702*0Sstevel@tonic-gate  */
1703*0Sstevel@tonic-gate static void
1704*0Sstevel@tonic-gate svc_thread_creator(SVCPOOL *pool)
1705*0Sstevel@tonic-gate {
1706*0Sstevel@tonic-gate 	callb_cpr_t cpr_info;	/* CPR info for the creator thread */
1707*0Sstevel@tonic-gate 
1708*0Sstevel@tonic-gate 	CALLB_CPR_INIT(&cpr_info, &pool->p_creator_lock, callb_generic_cpr,
1709*0Sstevel@tonic-gate 	    "svc_thread_creator");
1710*0Sstevel@tonic-gate 
1711*0Sstevel@tonic-gate 	for (;;) {
1712*0Sstevel@tonic-gate 		mutex_enter(&pool->p_creator_lock);
1713*0Sstevel@tonic-gate 
1714*0Sstevel@tonic-gate 		/* Check if someone set the exit flag */
1715*0Sstevel@tonic-gate 		if (pool->p_creator_exit)
1716*0Sstevel@tonic-gate 			break;
1717*0Sstevel@tonic-gate 
1718*0Sstevel@tonic-gate 		/* Clear the `signaled' flag and go asleep */
1719*0Sstevel@tonic-gate 		pool->p_creator_signaled = FALSE;
1720*0Sstevel@tonic-gate 
1721*0Sstevel@tonic-gate 		CALLB_CPR_SAFE_BEGIN(&cpr_info);
1722*0Sstevel@tonic-gate 		cv_wait(&pool->p_creator_cv, &pool->p_creator_lock);
1723*0Sstevel@tonic-gate 		CALLB_CPR_SAFE_END(&cpr_info, &pool->p_creator_lock);
1724*0Sstevel@tonic-gate 
1725*0Sstevel@tonic-gate 		/* Check if someone signaled to exit */
1726*0Sstevel@tonic-gate 		if (pool->p_creator_exit)
1727*0Sstevel@tonic-gate 			break;
1728*0Sstevel@tonic-gate 
1729*0Sstevel@tonic-gate 		mutex_exit(&pool->p_creator_lock);
1730*0Sstevel@tonic-gate 
1731*0Sstevel@tonic-gate 		mutex_enter(&pool->p_thread_lock);
1732*0Sstevel@tonic-gate 
1733*0Sstevel@tonic-gate 		/*
1734*0Sstevel@tonic-gate 		 * When the pool is in closing state and all the transports
1735*0Sstevel@tonic-gate 		 * are gone the creator should not create any new threads.
1736*0Sstevel@tonic-gate 		 */
1737*0Sstevel@tonic-gate 		if (pool->p_closing) {
1738*0Sstevel@tonic-gate 			rw_enter(&pool->p_lrwlock, RW_READER);
1739*0Sstevel@tonic-gate 			if (pool->p_lcount == 0) {
1740*0Sstevel@tonic-gate 				rw_exit(&pool->p_lrwlock);
1741*0Sstevel@tonic-gate 				mutex_exit(&pool->p_thread_lock);
1742*0Sstevel@tonic-gate 				continue;
1743*0Sstevel@tonic-gate 			}
1744*0Sstevel@tonic-gate 			rw_exit(&pool->p_lrwlock);
1745*0Sstevel@tonic-gate 		}
1746*0Sstevel@tonic-gate 
1747*0Sstevel@tonic-gate 		/*
1748*0Sstevel@tonic-gate 		 * Create a new service thread now.
1749*0Sstevel@tonic-gate 		 */
1750*0Sstevel@tonic-gate 		ASSERT(pool->p_reserved_threads >= 0);
1751*0Sstevel@tonic-gate 		ASSERT(pool->p_detached_threads >= 0);
1752*0Sstevel@tonic-gate 
1753*0Sstevel@tonic-gate 		if (pool->p_threads + pool->p_detached_threads <
1754*0Sstevel@tonic-gate 		    pool->p_maxthreads) {
1755*0Sstevel@tonic-gate 			/*
1756*0Sstevel@tonic-gate 			 * Signal the service pool wait thread
1757*0Sstevel@tonic-gate 			 * only if it hasn't already been signaled.
1758*0Sstevel@tonic-gate 			 */
1759*0Sstevel@tonic-gate 			mutex_enter(&pool->p_user_lock);
1760*0Sstevel@tonic-gate 			if (pool->p_signal_create_thread == FALSE) {
1761*0Sstevel@tonic-gate 				pool->p_signal_create_thread = TRUE;
1762*0Sstevel@tonic-gate 				cv_signal(&pool->p_user_cv);
1763*0Sstevel@tonic-gate 			}
1764*0Sstevel@tonic-gate 			mutex_exit(&pool->p_user_lock);
1765*0Sstevel@tonic-gate 
1766*0Sstevel@tonic-gate 		}
1767*0Sstevel@tonic-gate 
1768*0Sstevel@tonic-gate 		mutex_exit(&pool->p_thread_lock);
1769*0Sstevel@tonic-gate 	}
1770*0Sstevel@tonic-gate 
1771*0Sstevel@tonic-gate 	/*
1772*0Sstevel@tonic-gate 	 * Pool is closed. Cleanup and exit.
1773*0Sstevel@tonic-gate 	 */
1774*0Sstevel@tonic-gate 
1775*0Sstevel@tonic-gate 	/* Signal userland creator thread that it can stop now. */
1776*0Sstevel@tonic-gate 	mutex_enter(&pool->p_user_lock);
1777*0Sstevel@tonic-gate 	pool->p_user_exit = TRUE;
1778*0Sstevel@tonic-gate 	cv_broadcast(&pool->p_user_cv);
1779*0Sstevel@tonic-gate 	mutex_exit(&pool->p_user_lock);
1780*0Sstevel@tonic-gate 
1781*0Sstevel@tonic-gate 	/* Wait for svc_wait() to be done with the pool */
1782*0Sstevel@tonic-gate 	mutex_enter(&pool->p_user_lock);
1783*0Sstevel@tonic-gate 	while (pool->p_user_waiting) {
1784*0Sstevel@tonic-gate 		CALLB_CPR_SAFE_BEGIN(&cpr_info);
1785*0Sstevel@tonic-gate 		cv_wait(&pool->p_user_cv, &pool->p_user_lock);
1786*0Sstevel@tonic-gate 		CALLB_CPR_SAFE_END(&cpr_info, &pool->p_creator_lock);
1787*0Sstevel@tonic-gate 	}
1788*0Sstevel@tonic-gate 	mutex_exit(&pool->p_user_lock);
1789*0Sstevel@tonic-gate 
1790*0Sstevel@tonic-gate 	CALLB_CPR_EXIT(&cpr_info);
1791*0Sstevel@tonic-gate 	svc_pool_cleanup(pool);
1792*0Sstevel@tonic-gate 	zthread_exit();
1793*0Sstevel@tonic-gate }
1794*0Sstevel@tonic-gate 
1795*0Sstevel@tonic-gate /*
1796*0Sstevel@tonic-gate  * If the creator thread  is idle signal it to create
1797*0Sstevel@tonic-gate  * a new service thread.
1798*0Sstevel@tonic-gate  */
1799*0Sstevel@tonic-gate static void
1800*0Sstevel@tonic-gate svc_creator_signal(SVCPOOL *pool)
1801*0Sstevel@tonic-gate {
1802*0Sstevel@tonic-gate 	mutex_enter(&pool->p_creator_lock);
1803*0Sstevel@tonic-gate 	if (pool->p_creator_signaled == FALSE) {
1804*0Sstevel@tonic-gate 		pool->p_creator_signaled = TRUE;
1805*0Sstevel@tonic-gate 		cv_signal(&pool->p_creator_cv);
1806*0Sstevel@tonic-gate 	}
1807*0Sstevel@tonic-gate 	mutex_exit(&pool->p_creator_lock);
1808*0Sstevel@tonic-gate }
1809*0Sstevel@tonic-gate 
1810*0Sstevel@tonic-gate /*
1811*0Sstevel@tonic-gate  * Notify the creator thread to clean up and exit.
1812*0Sstevel@tonic-gate  */
1813*0Sstevel@tonic-gate static void
1814*0Sstevel@tonic-gate svc_creator_signalexit(SVCPOOL *pool)
1815*0Sstevel@tonic-gate {
1816*0Sstevel@tonic-gate 	mutex_enter(&pool->p_creator_lock);
1817*0Sstevel@tonic-gate 	pool->p_creator_exit = TRUE;
1818*0Sstevel@tonic-gate 	cv_signal(&pool->p_creator_cv);
1819*0Sstevel@tonic-gate 	mutex_exit(&pool->p_creator_lock);
1820*0Sstevel@tonic-gate }
1821*0Sstevel@tonic-gate 
1822*0Sstevel@tonic-gate /*
1823*0Sstevel@tonic-gate  * Polling part of the svc_run().
1824*0Sstevel@tonic-gate  * - search for a transport with a pending request
1825*0Sstevel@tonic-gate  * - when one is found then latch the request lock and return to svc_run()
1826*0Sstevel@tonic-gate  * - if there is no request go asleep and wait for a signal
1827*0Sstevel@tonic-gate  * - handle two exceptions:
1828*0Sstevel@tonic-gate  *   a) current transport is closing
1829*0Sstevel@tonic-gate  *   b) timeout waiting for a new request
1830*0Sstevel@tonic-gate  *   in both cases return to svc_run()
1831*0Sstevel@tonic-gate  */
1832*0Sstevel@tonic-gate static SVCMASTERXPRT *
1833*0Sstevel@tonic-gate svc_poll(SVCPOOL *pool, SVCMASTERXPRT *xprt, SVCXPRT *clone_xprt)
1834*0Sstevel@tonic-gate {
1835*0Sstevel@tonic-gate 	/*
1836*0Sstevel@tonic-gate 	 * Main loop iterates until
1837*0Sstevel@tonic-gate 	 * a) we find a pending request,
1838*0Sstevel@tonic-gate 	 * b) detect that the current transport is closing
1839*0Sstevel@tonic-gate 	 * c) time out waiting for a new request.
1840*0Sstevel@tonic-gate 	 */
1841*0Sstevel@tonic-gate 	for (;;) {
1842*0Sstevel@tonic-gate 		SVCMASTERXPRT *next;
1843*0Sstevel@tonic-gate 		clock_t timeleft;
1844*0Sstevel@tonic-gate 
1845*0Sstevel@tonic-gate 		/*
1846*0Sstevel@tonic-gate 		 * Step 1.
1847*0Sstevel@tonic-gate 		 * Check if there is a pending request on the current
1848*0Sstevel@tonic-gate 		 * transport handle so that we can avoid cloning.
1849*0Sstevel@tonic-gate 		 * If so then decrement the `pending-request' count for
1850*0Sstevel@tonic-gate 		 * the pool and return to svc_run().
1851*0Sstevel@tonic-gate 		 *
1852*0Sstevel@tonic-gate 		 * We need to prevent a potential starvation. When
1853*0Sstevel@tonic-gate 		 * a selected transport has all pending requests coming in
1854*0Sstevel@tonic-gate 		 * all the time then the service threads will never switch to
1855*0Sstevel@tonic-gate 		 * another transport. With a limited number of service
1856*0Sstevel@tonic-gate 		 * threads some transports may be never serviced.
1857*0Sstevel@tonic-gate 		 * To prevent such a scenario we pick up at most
1858*0Sstevel@tonic-gate 		 * pool->p_max_same_xprt requests from the same transport
1859*0Sstevel@tonic-gate 		 * and then take a hint from the xprt-ready queue or walk
1860*0Sstevel@tonic-gate 		 * the transport list.
1861*0Sstevel@tonic-gate 		 */
1862*0Sstevel@tonic-gate 		if (xprt && xprt->xp_req_head && (!pool->p_qoverflow ||
1863*0Sstevel@tonic-gate 		    clone_xprt->xp_same_xprt++ < pool->p_max_same_xprt)) {
1864*0Sstevel@tonic-gate 			mutex_enter(&xprt->xp_req_lock);
1865*0Sstevel@tonic-gate 			if (xprt->xp_req_head) {
1866*0Sstevel@tonic-gate 				mutex_enter(&pool->p_req_lock);
1867*0Sstevel@tonic-gate 				pool->p_reqs--;
1868*0Sstevel@tonic-gate 				mutex_exit(&pool->p_req_lock);
1869*0Sstevel@tonic-gate 
1870*0Sstevel@tonic-gate 				return (xprt);
1871*0Sstevel@tonic-gate 			}
1872*0Sstevel@tonic-gate 			mutex_exit(&xprt->xp_req_lock);
1873*0Sstevel@tonic-gate 		}
1874*0Sstevel@tonic-gate 		clone_xprt->xp_same_xprt = 0;
1875*0Sstevel@tonic-gate 
1876*0Sstevel@tonic-gate 		/*
1877*0Sstevel@tonic-gate 		 * Step 2.
1878*0Sstevel@tonic-gate 		 * If there is no request on the current transport try to
1879*0Sstevel@tonic-gate 		 * find another transport with a pending request.
1880*0Sstevel@tonic-gate 		 */
1881*0Sstevel@tonic-gate 		mutex_enter(&pool->p_req_lock);
1882*0Sstevel@tonic-gate 		pool->p_walkers++;
1883*0Sstevel@tonic-gate 		mutex_exit(&pool->p_req_lock);
1884*0Sstevel@tonic-gate 
1885*0Sstevel@tonic-gate 		/*
1886*0Sstevel@tonic-gate 		 * Make sure that transports will not be destroyed just
1887*0Sstevel@tonic-gate 		 * while we are checking them.
1888*0Sstevel@tonic-gate 		 */
1889*0Sstevel@tonic-gate 		rw_enter(&pool->p_lrwlock, RW_READER);
1890*0Sstevel@tonic-gate 
1891*0Sstevel@tonic-gate 		for (;;) {
1892*0Sstevel@tonic-gate 			SVCMASTERXPRT *hint;
1893*0Sstevel@tonic-gate 
1894*0Sstevel@tonic-gate 			/*
1895*0Sstevel@tonic-gate 			 * Get the next transport from the xprt-ready queue.
1896*0Sstevel@tonic-gate 			 * This is a hint. There is no guarantee that the
1897*0Sstevel@tonic-gate 			 * transport still has a pending request since it
1898*0Sstevel@tonic-gate 			 * could be picked up by another thread in step 1.
1899*0Sstevel@tonic-gate 			 *
1900*0Sstevel@tonic-gate 			 * If the transport has a pending request then keep
1901*0Sstevel@tonic-gate 			 * it locked. Decrement the `pending-requests' for
1902*0Sstevel@tonic-gate 			 * the pool and `walking-threads' counts, and return
1903*0Sstevel@tonic-gate 			 * to svc_run().
1904*0Sstevel@tonic-gate 			 */
1905*0Sstevel@tonic-gate 			hint = svc_xprt_qget(pool);
1906*0Sstevel@tonic-gate 
1907*0Sstevel@tonic-gate 			if (hint && hint->xp_req_head) {
1908*0Sstevel@tonic-gate 				mutex_enter(&hint->xp_req_lock);
1909*0Sstevel@tonic-gate 				if (hint->xp_req_head) {
1910*0Sstevel@tonic-gate 					rw_exit(&pool->p_lrwlock);
1911*0Sstevel@tonic-gate 
1912*0Sstevel@tonic-gate 					mutex_enter(&pool->p_req_lock);
1913*0Sstevel@tonic-gate 					pool->p_reqs--;
1914*0Sstevel@tonic-gate 					pool->p_walkers--;
1915*0Sstevel@tonic-gate 					mutex_exit(&pool->p_req_lock);
1916*0Sstevel@tonic-gate 
1917*0Sstevel@tonic-gate 					return (hint);
1918*0Sstevel@tonic-gate 				}
1919*0Sstevel@tonic-gate 				mutex_exit(&hint->xp_req_lock);
1920*0Sstevel@tonic-gate 			}
1921*0Sstevel@tonic-gate 
1922*0Sstevel@tonic-gate 			/*
1923*0Sstevel@tonic-gate 			 * If there was no hint in the xprt-ready queue then
1924*0Sstevel@tonic-gate 			 * - if there is less pending requests than polling
1925*0Sstevel@tonic-gate 			 *   threads go asleep
1926*0Sstevel@tonic-gate 			 * - otherwise check if there was an overflow in the
1927*0Sstevel@tonic-gate 			 *   xprt-ready queue; if so, then we need to break
1928*0Sstevel@tonic-gate 			 *   the `drain' mode
1929*0Sstevel@tonic-gate 			 */
1930*0Sstevel@tonic-gate 			if (hint == NULL) {
1931*0Sstevel@tonic-gate 				if (pool->p_reqs < pool->p_walkers) {
1932*0Sstevel@tonic-gate 					mutex_enter(&pool->p_req_lock);
1933*0Sstevel@tonic-gate 					if (pool->p_reqs < pool->p_walkers)
1934*0Sstevel@tonic-gate 						goto sleep;
1935*0Sstevel@tonic-gate 					mutex_exit(&pool->p_req_lock);
1936*0Sstevel@tonic-gate 				}
1937*0Sstevel@tonic-gate 				if (pool->p_qoverflow) {
1938*0Sstevel@tonic-gate 					break;
1939*0Sstevel@tonic-gate 				}
1940*0Sstevel@tonic-gate 			}
1941*0Sstevel@tonic-gate 		}
1942*0Sstevel@tonic-gate 
1943*0Sstevel@tonic-gate 		/*
1944*0Sstevel@tonic-gate 		 * If there was an overflow in the xprt-ready queue then we
1945*0Sstevel@tonic-gate 		 * need to switch to the `drain' mode, i.e. walk through the
1946*0Sstevel@tonic-gate 		 * pool's transport list and search for a transport with a
1947*0Sstevel@tonic-gate 		 * pending request. If we manage to drain all the pending
1948*0Sstevel@tonic-gate 		 * requests then we can clear the overflow flag. This will
1949*0Sstevel@tonic-gate 		 * switch svc_poll() back to taking hints from the xprt-ready
1950*0Sstevel@tonic-gate 		 * queue (which is generally more efficient).
1951*0Sstevel@tonic-gate 		 *
1952*0Sstevel@tonic-gate 		 * If there are no registered transports simply go asleep.
1953*0Sstevel@tonic-gate 		 */
1954*0Sstevel@tonic-gate 		if (xprt == NULL && pool->p_lhead == NULL) {
1955*0Sstevel@tonic-gate 			mutex_enter(&pool->p_req_lock);
1956*0Sstevel@tonic-gate 			goto sleep;
1957*0Sstevel@tonic-gate 		}
1958*0Sstevel@tonic-gate 
1959*0Sstevel@tonic-gate 		/*
1960*0Sstevel@tonic-gate 		 * `Walk' through the pool's list of master server
1961*0Sstevel@tonic-gate 		 * transport handles. Continue to loop until there are less
1962*0Sstevel@tonic-gate 		 * looping threads then pending requests.
1963*0Sstevel@tonic-gate 		 */
1964*0Sstevel@tonic-gate 		next = xprt ? xprt->xp_next : pool->p_lhead;
1965*0Sstevel@tonic-gate 
1966*0Sstevel@tonic-gate 		for (;;) {
1967*0Sstevel@tonic-gate 			/*
1968*0Sstevel@tonic-gate 			 * Check if there is a request on this transport.
1969*0Sstevel@tonic-gate 			 *
1970*0Sstevel@tonic-gate 			 * Since blocking on a locked mutex is very expensive
1971*0Sstevel@tonic-gate 			 * check for a request without a lock first. If we miss
1972*0Sstevel@tonic-gate 			 * a request that is just being delivered but this will
1973*0Sstevel@tonic-gate 			 * cost at most one full walk through the list.
1974*0Sstevel@tonic-gate 			 */
1975*0Sstevel@tonic-gate 			if (next->xp_req_head) {
1976*0Sstevel@tonic-gate 				/*
1977*0Sstevel@tonic-gate 				 * Check again, now with a lock.
1978*0Sstevel@tonic-gate 				 */
1979*0Sstevel@tonic-gate 				mutex_enter(&next->xp_req_lock);
1980*0Sstevel@tonic-gate 				if (next->xp_req_head) {
1981*0Sstevel@tonic-gate 					rw_exit(&pool->p_lrwlock);
1982*0Sstevel@tonic-gate 
1983*0Sstevel@tonic-gate 					mutex_enter(&pool->p_req_lock);
1984*0Sstevel@tonic-gate 					pool->p_reqs--;
1985*0Sstevel@tonic-gate 					pool->p_walkers--;
1986*0Sstevel@tonic-gate 					mutex_exit(&pool->p_req_lock);
1987*0Sstevel@tonic-gate 
1988*0Sstevel@tonic-gate 					return (next);
1989*0Sstevel@tonic-gate 				}
1990*0Sstevel@tonic-gate 				mutex_exit(&next->xp_req_lock);
1991*0Sstevel@tonic-gate 			}
1992*0Sstevel@tonic-gate 
1993*0Sstevel@tonic-gate 			/*
1994*0Sstevel@tonic-gate 			 * Continue to `walk' through the pool's
1995*0Sstevel@tonic-gate 			 * transport list until there is less requests
1996*0Sstevel@tonic-gate 			 * than walkers. Check this condition without
1997*0Sstevel@tonic-gate 			 * a lock first to avoid contention on a mutex.
1998*0Sstevel@tonic-gate 			 */
1999*0Sstevel@tonic-gate 			if (pool->p_reqs < pool->p_walkers) {
2000*0Sstevel@tonic-gate 				/*
2001*0Sstevel@tonic-gate 				 * Check again, now with the lock.
2002*0Sstevel@tonic-gate 				 * If all the pending requests have been
2003*0Sstevel@tonic-gate 				 * picked up than clear the overflow flag.
2004*0Sstevel@tonic-gate 				 */
2005*0Sstevel@tonic-gate 				mutex_enter(&pool->p_req_lock);
2006*0Sstevel@tonic-gate 				if (pool->p_reqs <= 0)
2007*0Sstevel@tonic-gate 					svc_xprt_qreset(pool);
2008*0Sstevel@tonic-gate 				if (pool->p_reqs < pool->p_walkers)
2009*0Sstevel@tonic-gate 					break;	/* goto sleep */
2010*0Sstevel@tonic-gate 				mutex_exit(&pool->p_req_lock);
2011*0Sstevel@tonic-gate 			}
2012*0Sstevel@tonic-gate 
2013*0Sstevel@tonic-gate 			next = next->xp_next;
2014*0Sstevel@tonic-gate 		}
2015*0Sstevel@tonic-gate 
2016*0Sstevel@tonic-gate 	sleep:
2017*0Sstevel@tonic-gate 		/*
2018*0Sstevel@tonic-gate 		 * No work to do. Stop the `walk' and go asleep.
2019*0Sstevel@tonic-gate 		 * Decrement the `walking-threads' count for the pool.
2020*0Sstevel@tonic-gate 		 */
2021*0Sstevel@tonic-gate 		pool->p_walkers--;
2022*0Sstevel@tonic-gate 		rw_exit(&pool->p_lrwlock);
2023*0Sstevel@tonic-gate 
2024*0Sstevel@tonic-gate 		/*
2025*0Sstevel@tonic-gate 		 * Count us as asleep, mark this thread as safe
2026*0Sstevel@tonic-gate 		 * for suspend and wait for a request.
2027*0Sstevel@tonic-gate 		 */
2028*0Sstevel@tonic-gate 		pool->p_asleep++;
2029*0Sstevel@tonic-gate 		timeleft = cv_timedwait_sig(&pool->p_req_cv, &pool->p_req_lock,
2030*0Sstevel@tonic-gate 		    pool->p_timeout + lbolt);
2031*0Sstevel@tonic-gate 
2032*0Sstevel@tonic-gate 		/*
2033*0Sstevel@tonic-gate 		 * If the drowsy flag is on this means that
2034*0Sstevel@tonic-gate 		 * someone has signaled a wakeup. In such a case
2035*0Sstevel@tonic-gate 		 * the `asleep-threads' count has already updated
2036*0Sstevel@tonic-gate 		 * so just clear the flag.
2037*0Sstevel@tonic-gate 		 *
2038*0Sstevel@tonic-gate 		 * If the drowsy flag is off then we need to update
2039*0Sstevel@tonic-gate 		 * the `asleep-threads' count.
2040*0Sstevel@tonic-gate 		 */
2041*0Sstevel@tonic-gate 		if (pool->p_drowsy) {
2042*0Sstevel@tonic-gate 			pool->p_drowsy = FALSE;
2043*0Sstevel@tonic-gate 			/*
2044*0Sstevel@tonic-gate 			 * If the thread is here because it timedout,
2045*0Sstevel@tonic-gate 			 * instead of returning SVC_ETIMEDOUT, it is
2046*0Sstevel@tonic-gate 			 * time to do some more work.
2047*0Sstevel@tonic-gate 			 */
2048*0Sstevel@tonic-gate 			if (timeleft == -1)
2049*0Sstevel@tonic-gate 				timeleft = 1;
2050*0Sstevel@tonic-gate 		} else {
2051*0Sstevel@tonic-gate 			pool->p_asleep--;
2052*0Sstevel@tonic-gate 		}
2053*0Sstevel@tonic-gate 		mutex_exit(&pool->p_req_lock);
2054*0Sstevel@tonic-gate 
2055*0Sstevel@tonic-gate 		/*
2056*0Sstevel@tonic-gate 		 * If we received a signal while waiting for a
2057*0Sstevel@tonic-gate 		 * request, inform svc_run(), so that we can return
2058*0Sstevel@tonic-gate 		 * to user level and restart the call.
2059*0Sstevel@tonic-gate 		 */
2060*0Sstevel@tonic-gate 		if (timeleft == 0)
2061*0Sstevel@tonic-gate 			return (SVC_EINTR);
2062*0Sstevel@tonic-gate 
2063*0Sstevel@tonic-gate 		/*
2064*0Sstevel@tonic-gate 		 * If the current transport is gone then notify
2065*0Sstevel@tonic-gate 		 * svc_run() to unlink from it.
2066*0Sstevel@tonic-gate 		 */
2067*0Sstevel@tonic-gate 		if (xprt && xprt->xp_wq == NULL)
2068*0Sstevel@tonic-gate 			return (SVC_EXPRTGONE);
2069*0Sstevel@tonic-gate 
2070*0Sstevel@tonic-gate 		/*
2071*0Sstevel@tonic-gate 		 * If we have timed out waiting for a request inform
2072*0Sstevel@tonic-gate 		 * svc_run() that we probably don't need this thread.
2073*0Sstevel@tonic-gate 		 */
2074*0Sstevel@tonic-gate 		if (timeleft == -1)
2075*0Sstevel@tonic-gate 			return (SVC_ETIMEDOUT);
2076*0Sstevel@tonic-gate 	}
2077*0Sstevel@tonic-gate }
2078*0Sstevel@tonic-gate 
2079*0Sstevel@tonic-gate /*
2080*0Sstevel@tonic-gate  * Main loop of the kernel RPC server
2081*0Sstevel@tonic-gate  * - wait for input (find a transport with a pending request).
2082*0Sstevel@tonic-gate  * - dequeue the request
2083*0Sstevel@tonic-gate  * - call a registered server routine to process the requests
2084*0Sstevel@tonic-gate  *
2085*0Sstevel@tonic-gate  * There can many threads running concurrently in this loop
2086*0Sstevel@tonic-gate  * on the same or on different transports.
2087*0Sstevel@tonic-gate  */
2088*0Sstevel@tonic-gate static int
2089*0Sstevel@tonic-gate svc_run(SVCPOOL *pool)
2090*0Sstevel@tonic-gate {
2091*0Sstevel@tonic-gate 	SVCMASTERXPRT *xprt = NULL;	/* master transport handle  */
2092*0Sstevel@tonic-gate 	SVCXPRT *clone_xprt;	/* clone for this thread    */
2093*0Sstevel@tonic-gate 	struct svc_globals *svc;
2094*0Sstevel@tonic-gate 	proc_t *p = ttoproc(curthread);
2095*0Sstevel@tonic-gate 
2096*0Sstevel@tonic-gate 	/* Allocate a clone transport handle for this thread */
2097*0Sstevel@tonic-gate 	clone_xprt = svc_clone_init();
2098*0Sstevel@tonic-gate 
2099*0Sstevel@tonic-gate 	/*
2100*0Sstevel@tonic-gate 	 * The loop iterates until the thread becomes
2101*0Sstevel@tonic-gate 	 * idle too long or the transport is gone.
2102*0Sstevel@tonic-gate 	 */
2103*0Sstevel@tonic-gate 	for (;;) {
2104*0Sstevel@tonic-gate 		SVCMASTERXPRT *next;
2105*0Sstevel@tonic-gate 		mblk_t *mp;
2106*0Sstevel@tonic-gate 
2107*0Sstevel@tonic-gate 		TRACE_0(TR_FAC_KRPC, TR_SVC_RUN, "svc_run");
2108*0Sstevel@tonic-gate 
2109*0Sstevel@tonic-gate 		/*
2110*0Sstevel@tonic-gate 		 * If the process is exiting/killed, return
2111*0Sstevel@tonic-gate 		 * immediately without processing any more
2112*0Sstevel@tonic-gate 		 * requests.
2113*0Sstevel@tonic-gate 		 */
2114*0Sstevel@tonic-gate 		if (p->p_flag & (SEXITLWPS|SKILLED)) {
2115*0Sstevel@tonic-gate 			svc_thread_exit(pool, clone_xprt);
2116*0Sstevel@tonic-gate 
2117*0Sstevel@tonic-gate 			/*
2118*0Sstevel@tonic-gate 			 * Thread has been interrupted and therefore
2119*0Sstevel@tonic-gate 			 * the service daemon is leaving as well so
2120*0Sstevel@tonic-gate 			 * let's go ahead and remove the service
2121*0Sstevel@tonic-gate 			 * pool at this time.
2122*0Sstevel@tonic-gate 			 */
2123*0Sstevel@tonic-gate 			svc = zone_getspecific(svc_zone_key, curproc->p_zone);
2124*0Sstevel@tonic-gate 			mutex_enter(&svc->svc_plock);
2125*0Sstevel@tonic-gate 			svc_pool_unregister(svc, pool);
2126*0Sstevel@tonic-gate 			mutex_exit(&svc->svc_plock);
2127*0Sstevel@tonic-gate 
2128*0Sstevel@tonic-gate 			return (0);
2129*0Sstevel@tonic-gate 		}
2130*0Sstevel@tonic-gate 
2131*0Sstevel@tonic-gate 		/* Find a transport with a pending request */
2132*0Sstevel@tonic-gate 		next = svc_poll(pool, xprt, clone_xprt);
2133*0Sstevel@tonic-gate 
2134*0Sstevel@tonic-gate 		/*
2135*0Sstevel@tonic-gate 		 * If svc_poll() finds a transport with a request
2136*0Sstevel@tonic-gate 		 * it latches xp_req_lock on it. Therefore we need
2137*0Sstevel@tonic-gate 		 * to dequeue the request and release the lock as
2138*0Sstevel@tonic-gate 		 * soon as possible.
2139*0Sstevel@tonic-gate 		 */
2140*0Sstevel@tonic-gate 		ASSERT(next != NULL &&
2141*0Sstevel@tonic-gate 		    (next == SVC_EXPRTGONE ||
2142*0Sstevel@tonic-gate 		    next == SVC_ETIMEDOUT ||
2143*0Sstevel@tonic-gate 		    next == SVC_EINTR ||
2144*0Sstevel@tonic-gate 		    MUTEX_HELD(&next->xp_req_lock)));
2145*0Sstevel@tonic-gate 
2146*0Sstevel@tonic-gate 		/* Ooops! Current transport is closing. Unlink now */
2147*0Sstevel@tonic-gate 		if (next == SVC_EXPRTGONE) {
2148*0Sstevel@tonic-gate 			svc_clone_unlink(clone_xprt);
2149*0Sstevel@tonic-gate 			xprt = NULL;
2150*0Sstevel@tonic-gate 			continue;
2151*0Sstevel@tonic-gate 		}
2152*0Sstevel@tonic-gate 
2153*0Sstevel@tonic-gate 		/* Ooops! Timeout while waiting for a request. Exit */
2154*0Sstevel@tonic-gate 		if (next == SVC_ETIMEDOUT) {
2155*0Sstevel@tonic-gate 			svc_thread_exit(pool, clone_xprt);
2156*0Sstevel@tonic-gate 			return (0);
2157*0Sstevel@tonic-gate 		}
2158*0Sstevel@tonic-gate 
2159*0Sstevel@tonic-gate 		/*
2160*0Sstevel@tonic-gate 		 * Interrupted by a signal while waiting for a
2161*0Sstevel@tonic-gate 		 * request. Return to userspace and restart.
2162*0Sstevel@tonic-gate 		 */
2163*0Sstevel@tonic-gate 		if (next == SVC_EINTR) {
2164*0Sstevel@tonic-gate 			svc_thread_exit(pool, clone_xprt);
2165*0Sstevel@tonic-gate 
2166*0Sstevel@tonic-gate 			/*
2167*0Sstevel@tonic-gate 			 * Thread has been interrupted and therefore
2168*0Sstevel@tonic-gate 			 * the service daemon is leaving as well so
2169*0Sstevel@tonic-gate 			 * let's go ahead and remove the service
2170*0Sstevel@tonic-gate 			 * pool at this time.
2171*0Sstevel@tonic-gate 			 */
2172*0Sstevel@tonic-gate 			svc = zone_getspecific(svc_zone_key, curproc->p_zone);
2173*0Sstevel@tonic-gate 			mutex_enter(&svc->svc_plock);
2174*0Sstevel@tonic-gate 			svc_pool_unregister(svc, pool);
2175*0Sstevel@tonic-gate 			mutex_exit(&svc->svc_plock);
2176*0Sstevel@tonic-gate 
2177*0Sstevel@tonic-gate 			return (EINTR);
2178*0Sstevel@tonic-gate 		}
2179*0Sstevel@tonic-gate 
2180*0Sstevel@tonic-gate 		/*
2181*0Sstevel@tonic-gate 		 * De-queue the request and release the request lock
2182*0Sstevel@tonic-gate 		 * on this transport (latched by svc_poll()).
2183*0Sstevel@tonic-gate 		 */
2184*0Sstevel@tonic-gate 		mp = next->xp_req_head;
2185*0Sstevel@tonic-gate 		next->xp_req_head = mp->b_next;
2186*0Sstevel@tonic-gate 		mp->b_next = (mblk_t *)0;
2187*0Sstevel@tonic-gate 
2188*0Sstevel@tonic-gate 		TRACE_2(TR_FAC_KRPC, TR_NFSFP_QUE_REQ_DEQ,
2189*0Sstevel@tonic-gate 		    "rpc_que_req_deq:pool %p mp %p", pool, mp);
2190*0Sstevel@tonic-gate 		mutex_exit(&next->xp_req_lock);
2191*0Sstevel@tonic-gate 
2192*0Sstevel@tonic-gate 		/*
2193*0Sstevel@tonic-gate 		 * If this is a new request on a current transport then
2194*0Sstevel@tonic-gate 		 * the clone structure is already properly initialized.
2195*0Sstevel@tonic-gate 		 * Otherwise, if the request is on a different transport,
2196*0Sstevel@tonic-gate 		 * unlink from the current master and link to
2197*0Sstevel@tonic-gate 		 * the one we got a request on.
2198*0Sstevel@tonic-gate 		 */
2199*0Sstevel@tonic-gate 		if (next != xprt) {
2200*0Sstevel@tonic-gate 			if (xprt)
2201*0Sstevel@tonic-gate 				svc_clone_unlink(clone_xprt);
2202*0Sstevel@tonic-gate 			svc_clone_link(next, clone_xprt);
2203*0Sstevel@tonic-gate 			xprt = next;
2204*0Sstevel@tonic-gate 		}
2205*0Sstevel@tonic-gate 
2206*0Sstevel@tonic-gate 		/*
2207*0Sstevel@tonic-gate 		 * If there are more requests and req_cv hasn't
2208*0Sstevel@tonic-gate 		 * been signaled yet then wake up one more thread now.
2209*0Sstevel@tonic-gate 		 *
2210*0Sstevel@tonic-gate 		 * We avoid signaling req_cv until the most recently
2211*0Sstevel@tonic-gate 		 * signaled thread wakes up and gets CPU to clear
2212*0Sstevel@tonic-gate 		 * the `drowsy' flag.
2213*0Sstevel@tonic-gate 		 */
2214*0Sstevel@tonic-gate 		if (!(pool->p_drowsy || pool->p_reqs <= pool->p_walkers ||
2215*0Sstevel@tonic-gate 		    pool->p_asleep == 0)) {
2216*0Sstevel@tonic-gate 			mutex_enter(&pool->p_req_lock);
2217*0Sstevel@tonic-gate 
2218*0Sstevel@tonic-gate 			if (pool->p_drowsy || pool->p_reqs <= pool->p_walkers ||
2219*0Sstevel@tonic-gate 			    pool->p_asleep == 0)
2220*0Sstevel@tonic-gate 				mutex_exit(&pool->p_req_lock);
2221*0Sstevel@tonic-gate 			else {
2222*0Sstevel@tonic-gate 				pool->p_asleep--;
2223*0Sstevel@tonic-gate 				pool->p_drowsy = TRUE;
2224*0Sstevel@tonic-gate 
2225*0Sstevel@tonic-gate 				cv_signal(&pool->p_req_cv);
2226*0Sstevel@tonic-gate 				mutex_exit(&pool->p_req_lock);
2227*0Sstevel@tonic-gate 			}
2228*0Sstevel@tonic-gate 		}
2229*0Sstevel@tonic-gate 
2230*0Sstevel@tonic-gate 		/*
2231*0Sstevel@tonic-gate 		 * If there are no asleep/signaled threads, we are
2232*0Sstevel@tonic-gate 		 * still below pool->p_maxthreads limit, and no thread is
2233*0Sstevel@tonic-gate 		 * currently being created then signal the creator
2234*0Sstevel@tonic-gate 		 * for one more service thread.
2235*0Sstevel@tonic-gate 		 *
2236*0Sstevel@tonic-gate 		 * The asleep and drowsy checks are not protected
2237*0Sstevel@tonic-gate 		 * by a lock since it hurts performance and a wrong
2238*0Sstevel@tonic-gate 		 * decision is not essential.
2239*0Sstevel@tonic-gate 		 */
2240*0Sstevel@tonic-gate 		if (pool->p_asleep == 0 && !pool->p_drowsy &&
2241*0Sstevel@tonic-gate 		    pool->p_threads + pool->p_detached_threads <
2242*0Sstevel@tonic-gate 		    pool->p_maxthreads)
2243*0Sstevel@tonic-gate 			svc_creator_signal(pool);
2244*0Sstevel@tonic-gate 
2245*0Sstevel@tonic-gate 		/*
2246*0Sstevel@tonic-gate 		 * Process the request.
2247*0Sstevel@tonic-gate 		 */
2248*0Sstevel@tonic-gate 		svc_getreq(clone_xprt, mp);
2249*0Sstevel@tonic-gate 
2250*0Sstevel@tonic-gate 		/* If thread had a reservation it should have been canceled */
2251*0Sstevel@tonic-gate 		ASSERT(!clone_xprt->xp_reserved);
2252*0Sstevel@tonic-gate 
2253*0Sstevel@tonic-gate 		/*
2254*0Sstevel@tonic-gate 		 * If the clone is marked detached then exit.
2255*0Sstevel@tonic-gate 		 * The rpcmod slot has already been released
2256*0Sstevel@tonic-gate 		 * when we detached this thread.
2257*0Sstevel@tonic-gate 		 */
2258*0Sstevel@tonic-gate 		if (clone_xprt->xp_detached) {
2259*0Sstevel@tonic-gate 			svc_thread_exitdetached(pool, clone_xprt);
2260*0Sstevel@tonic-gate 			return (0);
2261*0Sstevel@tonic-gate 		}
2262*0Sstevel@tonic-gate 
2263*0Sstevel@tonic-gate 		/*
2264*0Sstevel@tonic-gate 		 * Release our reference on the rpcmod
2265*0Sstevel@tonic-gate 		 * slot attached to xp_wq->q_ptr.
2266*0Sstevel@tonic-gate 		 */
2267*0Sstevel@tonic-gate 		(*RELE_PROC(xprt)) (clone_xprt->xp_wq, NULL);
2268*0Sstevel@tonic-gate 	}
2269*0Sstevel@tonic-gate 	/* NOTREACHED */
2270*0Sstevel@tonic-gate }
2271*0Sstevel@tonic-gate 
2272*0Sstevel@tonic-gate /*
2273*0Sstevel@tonic-gate  * Flush any pending requests for the queue and
2274*0Sstevel@tonic-gate  * and free the associated mblks.
2275*0Sstevel@tonic-gate  */
2276*0Sstevel@tonic-gate void
2277*0Sstevel@tonic-gate svc_queueclean(queue_t *q)
2278*0Sstevel@tonic-gate {
2279*0Sstevel@tonic-gate 	SVCMASTERXPRT *xprt = ((void **) q->q_ptr)[0];
2280*0Sstevel@tonic-gate 	mblk_t *mp;
2281*0Sstevel@tonic-gate 
2282*0Sstevel@tonic-gate 	/*
2283*0Sstevel@tonic-gate 	 * clean up the requests
2284*0Sstevel@tonic-gate 	 */
2285*0Sstevel@tonic-gate 	mutex_enter(&xprt->xp_req_lock);
2286*0Sstevel@tonic-gate 	while ((mp = xprt->xp_req_head) != NULL) {
2287*0Sstevel@tonic-gate 		xprt->xp_req_head = mp->b_next;
2288*0Sstevel@tonic-gate 		mp->b_next = (mblk_t *)0;
2289*0Sstevel@tonic-gate 		(*RELE_PROC(xprt)) (xprt->xp_wq, mp);
2290*0Sstevel@tonic-gate 	}
2291*0Sstevel@tonic-gate 	mutex_exit(&xprt->xp_req_lock);
2292*0Sstevel@tonic-gate }
2293*0Sstevel@tonic-gate 
2294*0Sstevel@tonic-gate /*
2295*0Sstevel@tonic-gate  * This routine is called by rpcmod to inform kernel RPC that a
2296*0Sstevel@tonic-gate  * queue is closing. It is called after all the requests have been
2297*0Sstevel@tonic-gate  * picked up (that is after all the slots on the queue have
2298*0Sstevel@tonic-gate  * been released by kernel RPC). It is also guaranteed that no more
2299*0Sstevel@tonic-gate  * request will be delivered on this transport.
2300*0Sstevel@tonic-gate  *
2301*0Sstevel@tonic-gate  * - clear xp_wq to mark the master server transport handle as closing
2302*0Sstevel@tonic-gate  * - if there are no more threads on this transport close/destroy it
2303*0Sstevel@tonic-gate  * - otherwise, broadcast threads sleeping in svc_poll(); the last
2304*0Sstevel@tonic-gate  *   thread will close/destroy the transport.
2305*0Sstevel@tonic-gate  */
2306*0Sstevel@tonic-gate void
2307*0Sstevel@tonic-gate svc_queueclose(queue_t *q)
2308*0Sstevel@tonic-gate {
2309*0Sstevel@tonic-gate 	SVCMASTERXPRT *xprt = ((void **) q->q_ptr)[0];
2310*0Sstevel@tonic-gate 
2311*0Sstevel@tonic-gate 	if (xprt == NULL) {
2312*0Sstevel@tonic-gate 		/*
2313*0Sstevel@tonic-gate 		 * If there is no master xprt associated with this stream,
2314*0Sstevel@tonic-gate 		 * then there is nothing to do.  This happens regularly
2315*0Sstevel@tonic-gate 		 * with connection-oriented listening streams created by
2316*0Sstevel@tonic-gate 		 * nfsd.
2317*0Sstevel@tonic-gate 		 */
2318*0Sstevel@tonic-gate 		return;
2319*0Sstevel@tonic-gate 	}
2320*0Sstevel@tonic-gate 
2321*0Sstevel@tonic-gate 	mutex_enter(&xprt->xp_thread_lock);
2322*0Sstevel@tonic-gate 
2323*0Sstevel@tonic-gate 	ASSERT(xprt->xp_req_head == NULL);
2324*0Sstevel@tonic-gate 	ASSERT(xprt->xp_wq != NULL);
2325*0Sstevel@tonic-gate 
2326*0Sstevel@tonic-gate 	xprt->xp_wq = NULL;
2327*0Sstevel@tonic-gate 
2328*0Sstevel@tonic-gate 	if (xprt->xp_threads == 0) {
2329*0Sstevel@tonic-gate 		SVCPOOL *pool = xprt->xp_pool;
2330*0Sstevel@tonic-gate 
2331*0Sstevel@tonic-gate 		/*
2332*0Sstevel@tonic-gate 		 * svc_xprt_cleanup() destroys the transport
2333*0Sstevel@tonic-gate 		 * or releases the transport thread lock
2334*0Sstevel@tonic-gate 		 */
2335*0Sstevel@tonic-gate 		svc_xprt_cleanup(xprt, FALSE);
2336*0Sstevel@tonic-gate 
2337*0Sstevel@tonic-gate 		mutex_enter(&pool->p_thread_lock);
2338*0Sstevel@tonic-gate 
2339*0Sstevel@tonic-gate 		/*
2340*0Sstevel@tonic-gate 		 * If the pool is in closing state and this was
2341*0Sstevel@tonic-gate 		 * the last transport in the pool then signal the creator
2342*0Sstevel@tonic-gate 		 * thread to clean up and exit.
2343*0Sstevel@tonic-gate 		 */
2344*0Sstevel@tonic-gate 		if (pool->p_closing && svc_pool_tryexit(pool)) {
2345*0Sstevel@tonic-gate 			return;
2346*0Sstevel@tonic-gate 		}
2347*0Sstevel@tonic-gate 		mutex_exit(&pool->p_thread_lock);
2348*0Sstevel@tonic-gate 	} else {
2349*0Sstevel@tonic-gate 		/*
2350*0Sstevel@tonic-gate 		 * Wakeup threads sleeping in svc_poll() so that they
2351*0Sstevel@tonic-gate 		 * unlink from the transport
2352*0Sstevel@tonic-gate 		 */
2353*0Sstevel@tonic-gate 		mutex_enter(&xprt->xp_pool->p_req_lock);
2354*0Sstevel@tonic-gate 		cv_broadcast(&xprt->xp_pool->p_req_cv);
2355*0Sstevel@tonic-gate 		mutex_exit(&xprt->xp_pool->p_req_lock);
2356*0Sstevel@tonic-gate 
2357*0Sstevel@tonic-gate 		/*
2358*0Sstevel@tonic-gate 		 *  NOTICE: No references to the master transport structure
2359*0Sstevel@tonic-gate 		 *	    beyond this point!
2360*0Sstevel@tonic-gate 		 */
2361*0Sstevel@tonic-gate 		mutex_exit(&xprt->xp_thread_lock);
2362*0Sstevel@tonic-gate 	}
2363*0Sstevel@tonic-gate }
2364*0Sstevel@tonic-gate 
2365*0Sstevel@tonic-gate /*
2366*0Sstevel@tonic-gate  * Interrupt `request delivery' routine called from rpcmod
2367*0Sstevel@tonic-gate  * - put a request at the tail of the transport request queue
2368*0Sstevel@tonic-gate  * - insert a hint for svc_poll() into the xprt-ready queue
2369*0Sstevel@tonic-gate  * - increment the `pending-requests' count for the pool
2370*0Sstevel@tonic-gate  * - wake up a thread sleeping in svc_poll() if necessary
2371*0Sstevel@tonic-gate  * - if all the threads are running ask the creator for a new one.
2372*0Sstevel@tonic-gate  */
2373*0Sstevel@tonic-gate void
2374*0Sstevel@tonic-gate svc_queuereq(queue_t *q, mblk_t *mp)
2375*0Sstevel@tonic-gate {
2376*0Sstevel@tonic-gate 	SVCMASTERXPRT *xprt = ((void **) q->q_ptr)[0];
2377*0Sstevel@tonic-gate 	SVCPOOL *pool = xprt->xp_pool;
2378*0Sstevel@tonic-gate 
2379*0Sstevel@tonic-gate 	TRACE_0(TR_FAC_KRPC, TR_SVC_QUEUEREQ_START, "svc_queuereq_start");
2380*0Sstevel@tonic-gate 
2381*0Sstevel@tonic-gate 	/*
2382*0Sstevel@tonic-gate 	 * Step 1.
2383*0Sstevel@tonic-gate 	 * Grab the transport's request lock and put
2384*0Sstevel@tonic-gate 	 * the request at the tail of the transport's
2385*0Sstevel@tonic-gate 	 * request queue.
2386*0Sstevel@tonic-gate 	 */
2387*0Sstevel@tonic-gate 	mutex_enter(&xprt->xp_req_lock);
2388*0Sstevel@tonic-gate 	if (xprt->xp_req_head == NULL)
2389*0Sstevel@tonic-gate 		xprt->xp_req_head = mp;
2390*0Sstevel@tonic-gate 	else
2391*0Sstevel@tonic-gate 		xprt->xp_req_tail->b_next = mp;
2392*0Sstevel@tonic-gate 	xprt->xp_req_tail = mp;
2393*0Sstevel@tonic-gate 
2394*0Sstevel@tonic-gate 	mutex_exit(&xprt->xp_req_lock);
2395*0Sstevel@tonic-gate 
2396*0Sstevel@tonic-gate 	/*
2397*0Sstevel@tonic-gate 	 * Step 2.
2398*0Sstevel@tonic-gate 	 * Grab the pool request lock, insert a hint into
2399*0Sstevel@tonic-gate 	 * the xprt-ready queue, increment `pending-requests'
2400*0Sstevel@tonic-gate 	 * count for the pool, and wake up a thread sleeping
2401*0Sstevel@tonic-gate 	 * in svc_poll() if necessary.
2402*0Sstevel@tonic-gate 	 */
2403*0Sstevel@tonic-gate 	mutex_enter(&pool->p_req_lock);
2404*0Sstevel@tonic-gate 
2405*0Sstevel@tonic-gate 	/* Insert pointer to this transport into the xprt-ready queue */
2406*0Sstevel@tonic-gate 	svc_xprt_qput(pool, xprt);
2407*0Sstevel@tonic-gate 
2408*0Sstevel@tonic-gate 	/* Increment the `pending-requests' count for the pool */
2409*0Sstevel@tonic-gate 	pool->p_reqs++;
2410*0Sstevel@tonic-gate 
2411*0Sstevel@tonic-gate 	TRACE_2(TR_FAC_KRPC, TR_NFSFP_QUE_REQ_ENQ,
2412*0Sstevel@tonic-gate 	    "rpc_que_req_enq:pool %p mp %p", pool, mp);
2413*0Sstevel@tonic-gate 
2414*0Sstevel@tonic-gate 	/*
2415*0Sstevel@tonic-gate 	 * If there are more requests and req_cv hasn't
2416*0Sstevel@tonic-gate 	 * been signaled yet then wake up one more thread now.
2417*0Sstevel@tonic-gate 	 *
2418*0Sstevel@tonic-gate 	 * We avoid signaling req_cv until the most recently
2419*0Sstevel@tonic-gate 	 * signaled thread wakes up and gets CPU to clear
2420*0Sstevel@tonic-gate 	 * the `drowsy' flag.
2421*0Sstevel@tonic-gate 	 */
2422*0Sstevel@tonic-gate 	if (pool->p_drowsy || pool->p_reqs <= pool->p_walkers ||
2423*0Sstevel@tonic-gate 	    pool->p_asleep == 0) {
2424*0Sstevel@tonic-gate 		mutex_exit(&pool->p_req_lock);
2425*0Sstevel@tonic-gate 	} else {
2426*0Sstevel@tonic-gate 		pool->p_drowsy = TRUE;
2427*0Sstevel@tonic-gate 		pool->p_asleep--;
2428*0Sstevel@tonic-gate 
2429*0Sstevel@tonic-gate 		/*
2430*0Sstevel@tonic-gate 		 * Signal wakeup and drop the request lock.
2431*0Sstevel@tonic-gate 		 */
2432*0Sstevel@tonic-gate 		cv_signal(&pool->p_req_cv);
2433*0Sstevel@tonic-gate 		mutex_exit(&pool->p_req_lock);
2434*0Sstevel@tonic-gate 	}
2435*0Sstevel@tonic-gate 
2436*0Sstevel@tonic-gate 	/*
2437*0Sstevel@tonic-gate 	 * Step 3.
2438*0Sstevel@tonic-gate 	 * If there are no asleep/signaled threads, we are
2439*0Sstevel@tonic-gate 	 * still below pool->p_maxthreads limit, and no thread is
2440*0Sstevel@tonic-gate 	 * currently being created then signal the creator
2441*0Sstevel@tonic-gate 	 * for one more service thread.
2442*0Sstevel@tonic-gate 	 *
2443*0Sstevel@tonic-gate 	 * The asleep and drowsy checks are not not protected
2444*0Sstevel@tonic-gate 	 * by a lock since it hurts performance and a wrong
2445*0Sstevel@tonic-gate 	 * decision is not essential.
2446*0Sstevel@tonic-gate 	 */
2447*0Sstevel@tonic-gate 	if (pool->p_asleep == 0 && !pool->p_drowsy &&
2448*0Sstevel@tonic-gate 		pool->p_threads + pool->p_detached_threads < pool->p_maxthreads)
2449*0Sstevel@tonic-gate 		svc_creator_signal(pool);
2450*0Sstevel@tonic-gate 
2451*0Sstevel@tonic-gate 	TRACE_1(TR_FAC_KRPC, TR_SVC_QUEUEREQ_END,
2452*0Sstevel@tonic-gate 	    "svc_queuereq_end:(%S)", "end");
2453*0Sstevel@tonic-gate }
2454*0Sstevel@tonic-gate 
2455*0Sstevel@tonic-gate /*
2456*0Sstevel@tonic-gate  * Reserve a service thread so that it can be detached later.
2457*0Sstevel@tonic-gate  * This reservation is required to make sure that when it tries to
2458*0Sstevel@tonic-gate  * detach itself the total number of detached threads does not exceed
2459*0Sstevel@tonic-gate  * pool->p_maxthreads - pool->p_redline (i.e. that we can have
2460*0Sstevel@tonic-gate  * up to pool->p_redline non-detached threads).
2461*0Sstevel@tonic-gate  *
2462*0Sstevel@tonic-gate  * If the thread does not detach itself later, it should cancel the
2463*0Sstevel@tonic-gate  * reservation before returning to svc_run().
2464*0Sstevel@tonic-gate  *
2465*0Sstevel@tonic-gate  * - check if there is room for more reserved/detached threads
2466*0Sstevel@tonic-gate  * - if so, then increment the `reserved threads' count for the pool
2467*0Sstevel@tonic-gate  * - mark the thread as reserved (setting the flag in the clone transport
2468*0Sstevel@tonic-gate  *   handle for this thread
2469*0Sstevel@tonic-gate  * - returns 1 if the reservation succeeded, 0 if it failed.
2470*0Sstevel@tonic-gate  */
2471*0Sstevel@tonic-gate int
2472*0Sstevel@tonic-gate svc_reserve_thread(SVCXPRT *clone_xprt)
2473*0Sstevel@tonic-gate {
2474*0Sstevel@tonic-gate 	SVCPOOL *pool = clone_xprt->xp_master->xp_pool;
2475*0Sstevel@tonic-gate 
2476*0Sstevel@tonic-gate 	/* Recursive reservations are not allowed */
2477*0Sstevel@tonic-gate 	ASSERT(!clone_xprt->xp_reserved);
2478*0Sstevel@tonic-gate 	ASSERT(!clone_xprt->xp_detached);
2479*0Sstevel@tonic-gate 
2480*0Sstevel@tonic-gate 	/* Check pool counts if there is room for reservation */
2481*0Sstevel@tonic-gate 	mutex_enter(&pool->p_thread_lock);
2482*0Sstevel@tonic-gate 	if (pool->p_reserved_threads + pool->p_detached_threads >=
2483*0Sstevel@tonic-gate 		pool->p_maxthreads - pool->p_redline) {
2484*0Sstevel@tonic-gate 		mutex_exit(&pool->p_thread_lock);
2485*0Sstevel@tonic-gate 		return (0);
2486*0Sstevel@tonic-gate 	}
2487*0Sstevel@tonic-gate 	pool->p_reserved_threads++;
2488*0Sstevel@tonic-gate 	mutex_exit(&pool->p_thread_lock);
2489*0Sstevel@tonic-gate 
2490*0Sstevel@tonic-gate 	/* Mark the thread (clone handle) as reserved */
2491*0Sstevel@tonic-gate 	clone_xprt->xp_reserved = TRUE;
2492*0Sstevel@tonic-gate 
2493*0Sstevel@tonic-gate 	return (1);
2494*0Sstevel@tonic-gate }
2495*0Sstevel@tonic-gate 
2496*0Sstevel@tonic-gate /*
2497*0Sstevel@tonic-gate  * Cancel a reservation for a thread.
2498*0Sstevel@tonic-gate  * - decrement the `reserved threads' count for the pool
2499*0Sstevel@tonic-gate  * - clear the flag in the clone transport handle for this thread.
2500*0Sstevel@tonic-gate  */
2501*0Sstevel@tonic-gate void
2502*0Sstevel@tonic-gate svc_unreserve_thread(SVCXPRT *clone_xprt)
2503*0Sstevel@tonic-gate {
2504*0Sstevel@tonic-gate 	SVCPOOL *pool = clone_xprt->xp_master->xp_pool;
2505*0Sstevel@tonic-gate 
2506*0Sstevel@tonic-gate 	/* Thread must have a reservation */
2507*0Sstevel@tonic-gate 	ASSERT(clone_xprt->xp_reserved);
2508*0Sstevel@tonic-gate 	ASSERT(!clone_xprt->xp_detached);
2509*0Sstevel@tonic-gate 
2510*0Sstevel@tonic-gate 	/* Decrement global count */
2511*0Sstevel@tonic-gate 	mutex_enter(&pool->p_thread_lock);
2512*0Sstevel@tonic-gate 	pool->p_reserved_threads--;
2513*0Sstevel@tonic-gate 	mutex_exit(&pool->p_thread_lock);
2514*0Sstevel@tonic-gate 
2515*0Sstevel@tonic-gate 	/* Clear reservation flag */
2516*0Sstevel@tonic-gate 	clone_xprt->xp_reserved = FALSE;
2517*0Sstevel@tonic-gate }
2518*0Sstevel@tonic-gate 
2519*0Sstevel@tonic-gate /*
2520*0Sstevel@tonic-gate  * Detach a thread from its transport, so that it can block for an
2521*0Sstevel@tonic-gate  * extended time.  Because the transport can be closed after the thread is
2522*0Sstevel@tonic-gate  * detached, the thread should have already sent off a reply if it was
2523*0Sstevel@tonic-gate  * going to send one.
2524*0Sstevel@tonic-gate  *
2525*0Sstevel@tonic-gate  * - decrement `non-detached threads' count and increment `detached threads'
2526*0Sstevel@tonic-gate  *   counts for the transport
2527*0Sstevel@tonic-gate  * - decrement the  `non-detached threads' and `reserved threads'
2528*0Sstevel@tonic-gate  *   counts and increment the `detached threads' count for the pool
2529*0Sstevel@tonic-gate  * - release the rpcmod slot
2530*0Sstevel@tonic-gate  * - mark the clone (thread) as detached.
2531*0Sstevel@tonic-gate  *
2532*0Sstevel@tonic-gate  * No need to return a pointer to the thread's CPR information, since
2533*0Sstevel@tonic-gate  * the thread has a userland identity.
2534*0Sstevel@tonic-gate  *
2535*0Sstevel@tonic-gate  * NOTICE: a thread must not detach itself without making a prior reservation
2536*0Sstevel@tonic-gate  *	   through svc_thread_reserve().
2537*0Sstevel@tonic-gate  */
2538*0Sstevel@tonic-gate callb_cpr_t *
2539*0Sstevel@tonic-gate svc_detach_thread(SVCXPRT *clone_xprt)
2540*0Sstevel@tonic-gate {
2541*0Sstevel@tonic-gate 	SVCMASTERXPRT *xprt = clone_xprt->xp_master;
2542*0Sstevel@tonic-gate 	SVCPOOL *pool = xprt->xp_pool;
2543*0Sstevel@tonic-gate 
2544*0Sstevel@tonic-gate 	/* Thread must have a reservation */
2545*0Sstevel@tonic-gate 	ASSERT(clone_xprt->xp_reserved);
2546*0Sstevel@tonic-gate 	ASSERT(!clone_xprt->xp_detached);
2547*0Sstevel@tonic-gate 
2548*0Sstevel@tonic-gate 	/* Bookkeeping for this transport */
2549*0Sstevel@tonic-gate 	mutex_enter(&xprt->xp_thread_lock);
2550*0Sstevel@tonic-gate 	xprt->xp_threads--;
2551*0Sstevel@tonic-gate 	xprt->xp_detached_threads++;
2552*0Sstevel@tonic-gate 	mutex_exit(&xprt->xp_thread_lock);
2553*0Sstevel@tonic-gate 
2554*0Sstevel@tonic-gate 	/* Bookkeeping for the pool */
2555*0Sstevel@tonic-gate 	mutex_enter(&pool->p_thread_lock);
2556*0Sstevel@tonic-gate 	pool->p_threads--;
2557*0Sstevel@tonic-gate 	pool->p_reserved_threads--;
2558*0Sstevel@tonic-gate 	pool->p_detached_threads++;
2559*0Sstevel@tonic-gate 	mutex_exit(&pool->p_thread_lock);
2560*0Sstevel@tonic-gate 
2561*0Sstevel@tonic-gate 	/* Release an rpcmod slot for this request */
2562*0Sstevel@tonic-gate 	(*RELE_PROC(xprt)) (clone_xprt->xp_wq, NULL);
2563*0Sstevel@tonic-gate 
2564*0Sstevel@tonic-gate 	/* Mark the clone (thread) as detached */
2565*0Sstevel@tonic-gate 	clone_xprt->xp_reserved = FALSE;
2566*0Sstevel@tonic-gate 	clone_xprt->xp_detached = TRUE;
2567*0Sstevel@tonic-gate 
2568*0Sstevel@tonic-gate 	return (NULL);
2569*0Sstevel@tonic-gate }
2570*0Sstevel@tonic-gate 
2571*0Sstevel@tonic-gate /*
2572*0Sstevel@tonic-gate  * This routine is responsible for extracting RDMA plugin master XPRT,
2573*0Sstevel@tonic-gate  * unregister from the SVCPOOL and initiate plugin specific cleanup.
2574*0Sstevel@tonic-gate  * It is passed a list/group of rdma transports as records which are
2575*0Sstevel@tonic-gate  * active in a given registered or unregistered kRPC thread pool. Its shuts
2576*0Sstevel@tonic-gate  * all active rdma transports in that pool. If the thread active on the trasport
2577*0Sstevel@tonic-gate  * happens to be last thread for that pool, it will signal the creater thread
2578*0Sstevel@tonic-gate  * to cleanup the pool and destroy the xprt in svc_queueclose()
2579*0Sstevel@tonic-gate  */
2580*0Sstevel@tonic-gate void
2581*0Sstevel@tonic-gate rdma_stop(rdma_xprt_group_t rdma_xprts)
2582*0Sstevel@tonic-gate {
2583*0Sstevel@tonic-gate 	SVCMASTERXPRT *xprt;
2584*0Sstevel@tonic-gate 	rdma_xprt_record_t *curr_rec;
2585*0Sstevel@tonic-gate 	queue_t *q;
2586*0Sstevel@tonic-gate 	mblk_t *mp;
2587*0Sstevel@tonic-gate 	int i;
2588*0Sstevel@tonic-gate 
2589*0Sstevel@tonic-gate 	if (rdma_xprts.rtg_count == 0)
2590*0Sstevel@tonic-gate 		return;
2591*0Sstevel@tonic-gate 
2592*0Sstevel@tonic-gate 	for (i = 0; i < rdma_xprts.rtg_count; i++) {
2593*0Sstevel@tonic-gate 		curr_rec = rdma_xprts.rtg_listhead;
2594*0Sstevel@tonic-gate 		rdma_xprts.rtg_listhead = curr_rec->rtr_next;
2595*0Sstevel@tonic-gate 		curr_rec->rtr_next = NULL;
2596*0Sstevel@tonic-gate 		xprt = curr_rec->rtr_xprt_ptr;
2597*0Sstevel@tonic-gate 		q = xprt->xp_wq;
2598*0Sstevel@tonic-gate 		svc_rdma_kstop(xprt);
2599*0Sstevel@tonic-gate 
2600*0Sstevel@tonic-gate 		mutex_enter(&xprt->xp_req_lock);
2601*0Sstevel@tonic-gate 		while ((mp = xprt->xp_req_head) != NULL) {
2602*0Sstevel@tonic-gate 			xprt->xp_req_head = mp->b_next;
2603*0Sstevel@tonic-gate 			mp->b_next = (mblk_t *)0;
2604*0Sstevel@tonic-gate 			if (mp)
2605*0Sstevel@tonic-gate 				freemsg(mp);
2606*0Sstevel@tonic-gate 		}
2607*0Sstevel@tonic-gate 		mutex_exit(&xprt->xp_req_lock);
2608*0Sstevel@tonic-gate 		svc_queueclose(q);
2609*0Sstevel@tonic-gate #ifdef	DEBUG
2610*0Sstevel@tonic-gate 		if (rdma_check)
2611*0Sstevel@tonic-gate 			cmn_err(CE_NOTE, "rdma_stop: Exited svc_queueclose\n");
2612*0Sstevel@tonic-gate #endif
2613*0Sstevel@tonic-gate 		/*
2614*0Sstevel@tonic-gate 		 * Free the rdma transport record for the expunged rdma
2615*0Sstevel@tonic-gate 		 * based master transport handle.
2616*0Sstevel@tonic-gate 		 */
2617*0Sstevel@tonic-gate 		kmem_free(curr_rec, sizeof (rdma_xprt_record_t));
2618*0Sstevel@tonic-gate 		if (!rdma_xprts.rtg_listhead)
2619*0Sstevel@tonic-gate 			break;
2620*0Sstevel@tonic-gate 	}
2621*0Sstevel@tonic-gate }
2622