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