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