xref: /titanic_50/usr/src/uts/common/os/strsubr.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
24*7c478bd9Sstevel@tonic-gate 
25*7c478bd9Sstevel@tonic-gate 
26*7c478bd9Sstevel@tonic-gate /*
27*7c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
28*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
29*7c478bd9Sstevel@tonic-gate  */
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
34*7c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
35*7c478bd9Sstevel@tonic-gate #include <sys/param.h>
36*7c478bd9Sstevel@tonic-gate #include <sys/errno.h>
37*7c478bd9Sstevel@tonic-gate #include <sys/signal.h>
38*7c478bd9Sstevel@tonic-gate #include <sys/proc.h>
39*7c478bd9Sstevel@tonic-gate #include <sys/conf.h>
40*7c478bd9Sstevel@tonic-gate #include <sys/cred.h>
41*7c478bd9Sstevel@tonic-gate #include <sys/user.h>
42*7c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
43*7c478bd9Sstevel@tonic-gate #include <sys/file.h>
44*7c478bd9Sstevel@tonic-gate #include <sys/session.h>
45*7c478bd9Sstevel@tonic-gate #include <sys/stream.h>
46*7c478bd9Sstevel@tonic-gate #include <sys/strsubr.h>
47*7c478bd9Sstevel@tonic-gate #include <sys/stropts.h>
48*7c478bd9Sstevel@tonic-gate #include <sys/poll.h>
49*7c478bd9Sstevel@tonic-gate #include <sys/systm.h>
50*7c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
51*7c478bd9Sstevel@tonic-gate #include <sys/uio.h>
52*7c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
53*7c478bd9Sstevel@tonic-gate #include <sys/priocntl.h>
54*7c478bd9Sstevel@tonic-gate #include <sys/procset.h>
55*7c478bd9Sstevel@tonic-gate #include <sys/vmem.h>
56*7c478bd9Sstevel@tonic-gate #include <sys/bitmap.h>
57*7c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
58*7c478bd9Sstevel@tonic-gate #include <sys/siginfo.h>
59*7c478bd9Sstevel@tonic-gate #include <sys/vtrace.h>
60*7c478bd9Sstevel@tonic-gate #include <sys/callb.h>
61*7c478bd9Sstevel@tonic-gate #include <sys/debug.h>
62*7c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
63*7c478bd9Sstevel@tonic-gate #include <sys/vmsystm.h>
64*7c478bd9Sstevel@tonic-gate #include <vm/page.h>
65*7c478bd9Sstevel@tonic-gate #include <sys/atomic.h>
66*7c478bd9Sstevel@tonic-gate #include <sys/suntpi.h>
67*7c478bd9Sstevel@tonic-gate #include <sys/strlog.h>
68*7c478bd9Sstevel@tonic-gate #include <sys/promif.h>
69*7c478bd9Sstevel@tonic-gate #include <sys/project.h>
70*7c478bd9Sstevel@tonic-gate #include <sys/vm.h>
71*7c478bd9Sstevel@tonic-gate #include <sys/taskq.h>
72*7c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
73*7c478bd9Sstevel@tonic-gate #include <sys/sunldi_impl.h>
74*7c478bd9Sstevel@tonic-gate #include <sys/strsun.h>
75*7c478bd9Sstevel@tonic-gate #include <sys/isa_defs.h>
76*7c478bd9Sstevel@tonic-gate #include <sys/multidata.h>
77*7c478bd9Sstevel@tonic-gate #include <sys/pattr.h>
78*7c478bd9Sstevel@tonic-gate #include <sys/strft.h>
79*7c478bd9Sstevel@tonic-gate #include <sys/zone.h>
80*7c478bd9Sstevel@tonic-gate 
81*7c478bd9Sstevel@tonic-gate #define	O_SAMESTR(q)	(((q)->q_next) && \
82*7c478bd9Sstevel@tonic-gate 	(((q)->q_flag & QREADR) == ((q)->q_next->q_flag & QREADR)))
83*7c478bd9Sstevel@tonic-gate 
84*7c478bd9Sstevel@tonic-gate /*
85*7c478bd9Sstevel@tonic-gate  * WARNING:
86*7c478bd9Sstevel@tonic-gate  * The variables and routines in this file are private, belonging
87*7c478bd9Sstevel@tonic-gate  * to the STREAMS subsystem. These should not be used by modules
88*7c478bd9Sstevel@tonic-gate  * or drivers. Compatibility will not be guaranteed.
89*7c478bd9Sstevel@tonic-gate  */
90*7c478bd9Sstevel@tonic-gate 
91*7c478bd9Sstevel@tonic-gate /*
92*7c478bd9Sstevel@tonic-gate  * Id value used to distinguish between different multiplexor links.
93*7c478bd9Sstevel@tonic-gate  */
94*7c478bd9Sstevel@tonic-gate static int32_t lnk_id = 0;
95*7c478bd9Sstevel@tonic-gate 
96*7c478bd9Sstevel@tonic-gate #define	STREAMS_LOPRI MINCLSYSPRI
97*7c478bd9Sstevel@tonic-gate static pri_t streams_lopri = STREAMS_LOPRI;
98*7c478bd9Sstevel@tonic-gate 
99*7c478bd9Sstevel@tonic-gate #define	STRSTAT(x)	(str_statistics.x.value.ui64++)
100*7c478bd9Sstevel@tonic-gate typedef struct str_stat {
101*7c478bd9Sstevel@tonic-gate 	kstat_named_t	sqenables;
102*7c478bd9Sstevel@tonic-gate 	kstat_named_t	stenables;
103*7c478bd9Sstevel@tonic-gate 	kstat_named_t	syncqservice;
104*7c478bd9Sstevel@tonic-gate 	kstat_named_t	freebs;
105*7c478bd9Sstevel@tonic-gate 	kstat_named_t	qwr_outer;
106*7c478bd9Sstevel@tonic-gate 	kstat_named_t	rservice;
107*7c478bd9Sstevel@tonic-gate 	kstat_named_t	strwaits;
108*7c478bd9Sstevel@tonic-gate 	kstat_named_t	taskqfails;
109*7c478bd9Sstevel@tonic-gate 	kstat_named_t	bufcalls;
110*7c478bd9Sstevel@tonic-gate 	kstat_named_t	qhelps;
111*7c478bd9Sstevel@tonic-gate 	kstat_named_t	qremoved;
112*7c478bd9Sstevel@tonic-gate 	kstat_named_t	sqremoved;
113*7c478bd9Sstevel@tonic-gate 	kstat_named_t	bcwaits;
114*7c478bd9Sstevel@tonic-gate 	kstat_named_t	sqtoomany;
115*7c478bd9Sstevel@tonic-gate } str_stat_t;
116*7c478bd9Sstevel@tonic-gate 
117*7c478bd9Sstevel@tonic-gate static str_stat_t str_statistics = {
118*7c478bd9Sstevel@tonic-gate 	{ "sqenables",		KSTAT_DATA_UINT64 },
119*7c478bd9Sstevel@tonic-gate 	{ "stenables",		KSTAT_DATA_UINT64 },
120*7c478bd9Sstevel@tonic-gate 	{ "syncqservice",	KSTAT_DATA_UINT64 },
121*7c478bd9Sstevel@tonic-gate 	{ "freebs",		KSTAT_DATA_UINT64 },
122*7c478bd9Sstevel@tonic-gate 	{ "qwr_outer",		KSTAT_DATA_UINT64 },
123*7c478bd9Sstevel@tonic-gate 	{ "rservice",		KSTAT_DATA_UINT64 },
124*7c478bd9Sstevel@tonic-gate 	{ "strwaits",		KSTAT_DATA_UINT64 },
125*7c478bd9Sstevel@tonic-gate 	{ "taskqfails",		KSTAT_DATA_UINT64 },
126*7c478bd9Sstevel@tonic-gate 	{ "bufcalls",		KSTAT_DATA_UINT64 },
127*7c478bd9Sstevel@tonic-gate 	{ "qhelps",		KSTAT_DATA_UINT64 },
128*7c478bd9Sstevel@tonic-gate 	{ "qremoved",		KSTAT_DATA_UINT64 },
129*7c478bd9Sstevel@tonic-gate 	{ "sqremoved",		KSTAT_DATA_UINT64 },
130*7c478bd9Sstevel@tonic-gate 	{ "bcwaits",		KSTAT_DATA_UINT64 },
131*7c478bd9Sstevel@tonic-gate 	{ "sqtoomany",		KSTAT_DATA_UINT64 },
132*7c478bd9Sstevel@tonic-gate };
133*7c478bd9Sstevel@tonic-gate 
134*7c478bd9Sstevel@tonic-gate static kstat_t *str_kstat;
135*7c478bd9Sstevel@tonic-gate 
136*7c478bd9Sstevel@tonic-gate /*
137*7c478bd9Sstevel@tonic-gate  * qrunflag was used previously to control background scheduling of queues. It
138*7c478bd9Sstevel@tonic-gate  * is not used anymore, but kept here in case some module still wants to access
139*7c478bd9Sstevel@tonic-gate  * it via qready() and setqsched macros.
140*7c478bd9Sstevel@tonic-gate  */
141*7c478bd9Sstevel@tonic-gate char qrunflag;			/*  Unused */
142*7c478bd9Sstevel@tonic-gate 
143*7c478bd9Sstevel@tonic-gate /*
144*7c478bd9Sstevel@tonic-gate  * Most of the streams scheduling is done via task queues. Task queues may fail
145*7c478bd9Sstevel@tonic-gate  * for non-sleep dispatches, so there are two backup threads servicing failed
146*7c478bd9Sstevel@tonic-gate  * requests for queues and syncqs. Both of these threads also service failed
147*7c478bd9Sstevel@tonic-gate  * dispatches freebs requests. Queues are put in the list specified by `qhead'
148*7c478bd9Sstevel@tonic-gate  * and `qtail' pointers, syncqs use `sqhead' and `sqtail' pointers and freebs
149*7c478bd9Sstevel@tonic-gate  * requests are put into `freebs_list' which has no tail pointer. All three
150*7c478bd9Sstevel@tonic-gate  * lists are protected by a single `service_queue' lock and use
151*7c478bd9Sstevel@tonic-gate  * `services_to_run' condition variable for signaling background threads. Use of
152*7c478bd9Sstevel@tonic-gate  * a single lock should not be a problem because it is only used under heavy
153*7c478bd9Sstevel@tonic-gate  * loads when task queues start to fail and at that time it may be a good idea
154*7c478bd9Sstevel@tonic-gate  * to throttle scheduling requests.
155*7c478bd9Sstevel@tonic-gate  *
156*7c478bd9Sstevel@tonic-gate  * NOTE: queues and syncqs should be scheduled by two separate threads because
157*7c478bd9Sstevel@tonic-gate  * queue servicing may be blocked waiting for a syncq which may be also
158*7c478bd9Sstevel@tonic-gate  * scheduled for background execution. This may create a deadlock when only one
159*7c478bd9Sstevel@tonic-gate  * thread is used for both.
160*7c478bd9Sstevel@tonic-gate  */
161*7c478bd9Sstevel@tonic-gate 
162*7c478bd9Sstevel@tonic-gate static taskq_t *streams_taskq;		/* Used for most STREAMS scheduling */
163*7c478bd9Sstevel@tonic-gate 
164*7c478bd9Sstevel@tonic-gate static kmutex_t service_queue;		/* protects all of servicing vars */
165*7c478bd9Sstevel@tonic-gate static kcondvar_t services_to_run;	/* wake up background service thread */
166*7c478bd9Sstevel@tonic-gate static kcondvar_t syncqs_to_run;	/* wake up background service thread */
167*7c478bd9Sstevel@tonic-gate 
168*7c478bd9Sstevel@tonic-gate /*
169*7c478bd9Sstevel@tonic-gate  * List of queues scheduled for background processing dueue to lack of resources
170*7c478bd9Sstevel@tonic-gate  * in the task queues. Protected by service_queue lock;
171*7c478bd9Sstevel@tonic-gate  */
172*7c478bd9Sstevel@tonic-gate static struct queue *qhead;
173*7c478bd9Sstevel@tonic-gate static struct queue *qtail;
174*7c478bd9Sstevel@tonic-gate 
175*7c478bd9Sstevel@tonic-gate /*
176*7c478bd9Sstevel@tonic-gate  * Same list for syncqs
177*7c478bd9Sstevel@tonic-gate  */
178*7c478bd9Sstevel@tonic-gate static syncq_t *sqhead;
179*7c478bd9Sstevel@tonic-gate static syncq_t *sqtail;
180*7c478bd9Sstevel@tonic-gate 
181*7c478bd9Sstevel@tonic-gate static mblk_t *freebs_list;	/* list of buffers to free */
182*7c478bd9Sstevel@tonic-gate 
183*7c478bd9Sstevel@tonic-gate /*
184*7c478bd9Sstevel@tonic-gate  * Backup threads for servicing queues and syncqs
185*7c478bd9Sstevel@tonic-gate  */
186*7c478bd9Sstevel@tonic-gate kthread_t *streams_qbkgrnd_thread;
187*7c478bd9Sstevel@tonic-gate kthread_t *streams_sqbkgrnd_thread;
188*7c478bd9Sstevel@tonic-gate 
189*7c478bd9Sstevel@tonic-gate /*
190*7c478bd9Sstevel@tonic-gate  * Bufcalls related variables.
191*7c478bd9Sstevel@tonic-gate  */
192*7c478bd9Sstevel@tonic-gate struct bclist	strbcalls;	/* list of waiting bufcalls */
193*7c478bd9Sstevel@tonic-gate kmutex_t	strbcall_lock;	/* protects bufcall list (strbcalls) */
194*7c478bd9Sstevel@tonic-gate kcondvar_t	strbcall_cv;	/* Signaling when a bufcall is added */
195*7c478bd9Sstevel@tonic-gate kmutex_t	bcall_monitor;	/* sleep/wakeup style monitor */
196*7c478bd9Sstevel@tonic-gate kcondvar_t	bcall_cv;	/* wait 'till executing bufcall completes */
197*7c478bd9Sstevel@tonic-gate kthread_t	*bc_bkgrnd_thread; /* Thread to service bufcall requests */
198*7c478bd9Sstevel@tonic-gate 
199*7c478bd9Sstevel@tonic-gate kmutex_t	strresources;	/* protects global resources */
200*7c478bd9Sstevel@tonic-gate kmutex_t	muxifier;	/* single-threads multiplexor creation */
201*7c478bd9Sstevel@tonic-gate 
202*7c478bd9Sstevel@tonic-gate extern void	time_to_wait(clock_t *, clock_t);
203*7c478bd9Sstevel@tonic-gate 
204*7c478bd9Sstevel@tonic-gate /*
205*7c478bd9Sstevel@tonic-gate  * run_queues is no longer used, but is kept in case some 3-d party
206*7c478bd9Sstevel@tonic-gate  * module/driver decides to use it.
207*7c478bd9Sstevel@tonic-gate  */
208*7c478bd9Sstevel@tonic-gate int run_queues = 0;
209*7c478bd9Sstevel@tonic-gate 
210*7c478bd9Sstevel@tonic-gate /*
211*7c478bd9Sstevel@tonic-gate  * sq_max_size is the depth of the syncq (in number of messages) before
212*7c478bd9Sstevel@tonic-gate  * qfill_syncq() starts QFULL'ing destination queues. As its primary
213*7c478bd9Sstevel@tonic-gate  * consumer - IP is no longer D_MTPERMOD, but there may be other
214*7c478bd9Sstevel@tonic-gate  * modules/drivers depend on this syncq flow control, we prefer to
215*7c478bd9Sstevel@tonic-gate  * choose a large number as the default value. For potential
216*7c478bd9Sstevel@tonic-gate  * performance gain, this value is tunable in /etc/system.
217*7c478bd9Sstevel@tonic-gate  */
218*7c478bd9Sstevel@tonic-gate int sq_max_size = 10000;
219*7c478bd9Sstevel@tonic-gate 
220*7c478bd9Sstevel@tonic-gate /*
221*7c478bd9Sstevel@tonic-gate  * the number of ciputctrl structures per syncq and stream we create when
222*7c478bd9Sstevel@tonic-gate  * needed.
223*7c478bd9Sstevel@tonic-gate  */
224*7c478bd9Sstevel@tonic-gate int n_ciputctrl;
225*7c478bd9Sstevel@tonic-gate int max_n_ciputctrl = 16;
226*7c478bd9Sstevel@tonic-gate /*
227*7c478bd9Sstevel@tonic-gate  * if n_ciputctrl is < min_n_ciputctrl don't even create ciputctrl_cache.
228*7c478bd9Sstevel@tonic-gate  */
229*7c478bd9Sstevel@tonic-gate int min_n_ciputctrl = 2;
230*7c478bd9Sstevel@tonic-gate 
231*7c478bd9Sstevel@tonic-gate static struct mux_node *mux_nodes;	/* mux info for cycle checking */
232*7c478bd9Sstevel@tonic-gate 
233*7c478bd9Sstevel@tonic-gate /*
234*7c478bd9Sstevel@tonic-gate  * Per-driver/module syncqs
235*7c478bd9Sstevel@tonic-gate  * ========================
236*7c478bd9Sstevel@tonic-gate  *
237*7c478bd9Sstevel@tonic-gate  * For drivers/modules that use PERMOD or outer syncqs we keep a list of
238*7c478bd9Sstevel@tonic-gate  * perdm structures, new entries being added (and new syncqs allocated) when
239*7c478bd9Sstevel@tonic-gate  * setq() encounters a module/driver with a streamtab that it hasn't seen
240*7c478bd9Sstevel@tonic-gate  * before.
241*7c478bd9Sstevel@tonic-gate  * The reason for this mechanism is that some modules and drivers share a
242*7c478bd9Sstevel@tonic-gate  * common streamtab and it is necessary for those modules and drivers to also
243*7c478bd9Sstevel@tonic-gate  * share a common PERMOD syncq.
244*7c478bd9Sstevel@tonic-gate  *
245*7c478bd9Sstevel@tonic-gate  * perdm_list --> dm_str == streamtab_1
246*7c478bd9Sstevel@tonic-gate  *                dm_sq == syncq_1
247*7c478bd9Sstevel@tonic-gate  *                dm_ref
248*7c478bd9Sstevel@tonic-gate  *                dm_next --> dm_str == streamtab_2
249*7c478bd9Sstevel@tonic-gate  *                            dm_sq == syncq_2
250*7c478bd9Sstevel@tonic-gate  *                            dm_ref
251*7c478bd9Sstevel@tonic-gate  *                            dm_next --> ... NULL
252*7c478bd9Sstevel@tonic-gate  *
253*7c478bd9Sstevel@tonic-gate  * The dm_ref field is incremented for each new driver/module that takes
254*7c478bd9Sstevel@tonic-gate  * a reference to the perdm structure and hence shares the syncq.
255*7c478bd9Sstevel@tonic-gate  * References are held in the fmodsw_impl_t structure for each STREAMS module
256*7c478bd9Sstevel@tonic-gate  * or the dev_impl array (indexed by device major number) for each driver.
257*7c478bd9Sstevel@tonic-gate  *
258*7c478bd9Sstevel@tonic-gate  * perdm_list -> [dm_ref == 1] -> [dm_ref == 2] -> [dm_ref == 1] -> NULL
259*7c478bd9Sstevel@tonic-gate  *		     ^                 ^ ^               ^
260*7c478bd9Sstevel@tonic-gate  *                   |  ______________/  |               |
261*7c478bd9Sstevel@tonic-gate  *                   | /                 |               |
262*7c478bd9Sstevel@tonic-gate  * dev_impl:     ...|x|y|...          module A	      module B
263*7c478bd9Sstevel@tonic-gate  *
264*7c478bd9Sstevel@tonic-gate  * When a module/driver is unloaded the reference count is decremented and,
265*7c478bd9Sstevel@tonic-gate  * when it falls to zero, the perdm structure is removed from the list and
266*7c478bd9Sstevel@tonic-gate  * the syncq is freed (see rele_dm()).
267*7c478bd9Sstevel@tonic-gate  */
268*7c478bd9Sstevel@tonic-gate perdm_t *perdm_list = NULL;
269*7c478bd9Sstevel@tonic-gate static krwlock_t perdm_rwlock;
270*7c478bd9Sstevel@tonic-gate cdevsw_impl_t *devimpl;
271*7c478bd9Sstevel@tonic-gate 
272*7c478bd9Sstevel@tonic-gate extern struct qinit strdata;
273*7c478bd9Sstevel@tonic-gate extern struct qinit stwdata;
274*7c478bd9Sstevel@tonic-gate 
275*7c478bd9Sstevel@tonic-gate static void runservice(queue_t *);
276*7c478bd9Sstevel@tonic-gate static void streams_bufcall_service(void);
277*7c478bd9Sstevel@tonic-gate static void streams_qbkgrnd_service(void);
278*7c478bd9Sstevel@tonic-gate static void streams_sqbkgrnd_service(void);
279*7c478bd9Sstevel@tonic-gate static syncq_t *new_syncq(void);
280*7c478bd9Sstevel@tonic-gate static void free_syncq(syncq_t *);
281*7c478bd9Sstevel@tonic-gate static void outer_insert(syncq_t *, syncq_t *);
282*7c478bd9Sstevel@tonic-gate static void outer_remove(syncq_t *, syncq_t *);
283*7c478bd9Sstevel@tonic-gate static void write_now(syncq_t *);
284*7c478bd9Sstevel@tonic-gate static void clr_qfull(queue_t *);
285*7c478bd9Sstevel@tonic-gate static void enable_svc(queue_t *);
286*7c478bd9Sstevel@tonic-gate static void runbufcalls(void);
287*7c478bd9Sstevel@tonic-gate static void sqenable(syncq_t *);
288*7c478bd9Sstevel@tonic-gate static void sqfill_events(syncq_t *, queue_t *, mblk_t *, void (*)());
289*7c478bd9Sstevel@tonic-gate static void wait_q_syncq(queue_t *);
290*7c478bd9Sstevel@tonic-gate 
291*7c478bd9Sstevel@tonic-gate static void queue_service(queue_t *);
292*7c478bd9Sstevel@tonic-gate static void stream_service(stdata_t *);
293*7c478bd9Sstevel@tonic-gate static void syncq_service(syncq_t *);
294*7c478bd9Sstevel@tonic-gate static void qwriter_outer_service(syncq_t *);
295*7c478bd9Sstevel@tonic-gate static void mblk_free(mblk_t *);
296*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
297*7c478bd9Sstevel@tonic-gate static int qprocsareon(queue_t *);
298*7c478bd9Sstevel@tonic-gate #endif
299*7c478bd9Sstevel@tonic-gate 
300*7c478bd9Sstevel@tonic-gate static void set_nfsrv_ptr(queue_t *, queue_t *, queue_t *, queue_t *);
301*7c478bd9Sstevel@tonic-gate static void reset_nfsrv_ptr(queue_t *, queue_t *);
302*7c478bd9Sstevel@tonic-gate 
303*7c478bd9Sstevel@tonic-gate static void sq_run_events(syncq_t *);
304*7c478bd9Sstevel@tonic-gate static int propagate_syncq(queue_t *);
305*7c478bd9Sstevel@tonic-gate 
306*7c478bd9Sstevel@tonic-gate static void	blocksq(syncq_t *, ushort_t, int);
307*7c478bd9Sstevel@tonic-gate static void	unblocksq(syncq_t *, ushort_t, int);
308*7c478bd9Sstevel@tonic-gate static int	dropsq(syncq_t *, uint16_t);
309*7c478bd9Sstevel@tonic-gate static void	emptysq(syncq_t *);
310*7c478bd9Sstevel@tonic-gate static sqlist_t *sqlist_alloc(struct stdata *, int);
311*7c478bd9Sstevel@tonic-gate static void	sqlist_free(sqlist_t *);
312*7c478bd9Sstevel@tonic-gate static sqlist_t	*sqlist_build(queue_t *, struct stdata *, boolean_t);
313*7c478bd9Sstevel@tonic-gate static void	sqlist_insert(sqlist_t *, syncq_t *);
314*7c478bd9Sstevel@tonic-gate static void	sqlist_insertall(sqlist_t *, queue_t *);
315*7c478bd9Sstevel@tonic-gate 
316*7c478bd9Sstevel@tonic-gate static void	strsetuio(stdata_t *);
317*7c478bd9Sstevel@tonic-gate 
318*7c478bd9Sstevel@tonic-gate struct kmem_cache *stream_head_cache;
319*7c478bd9Sstevel@tonic-gate struct kmem_cache *queue_cache;
320*7c478bd9Sstevel@tonic-gate struct kmem_cache *syncq_cache;
321*7c478bd9Sstevel@tonic-gate struct kmem_cache *qband_cache;
322*7c478bd9Sstevel@tonic-gate struct kmem_cache *linkinfo_cache;
323*7c478bd9Sstevel@tonic-gate struct kmem_cache *ciputctrl_cache = NULL;
324*7c478bd9Sstevel@tonic-gate 
325*7c478bd9Sstevel@tonic-gate static linkinfo_t *linkinfo_list;
326*7c478bd9Sstevel@tonic-gate 
327*7c478bd9Sstevel@tonic-gate /*
328*7c478bd9Sstevel@tonic-gate  *  Qinit structure and Module_info structures
329*7c478bd9Sstevel@tonic-gate  *	for passthru read and write queues
330*7c478bd9Sstevel@tonic-gate  */
331*7c478bd9Sstevel@tonic-gate 
332*7c478bd9Sstevel@tonic-gate static void pass_wput(queue_t *, mblk_t *);
333*7c478bd9Sstevel@tonic-gate static queue_t *link_addpassthru(stdata_t *);
334*7c478bd9Sstevel@tonic-gate static void link_rempassthru(queue_t *);
335*7c478bd9Sstevel@tonic-gate 
336*7c478bd9Sstevel@tonic-gate struct  module_info passthru_info = {
337*7c478bd9Sstevel@tonic-gate 	0,
338*7c478bd9Sstevel@tonic-gate 	"passthru",
339*7c478bd9Sstevel@tonic-gate 	0,
340*7c478bd9Sstevel@tonic-gate 	INFPSZ,
341*7c478bd9Sstevel@tonic-gate 	STRHIGH,
342*7c478bd9Sstevel@tonic-gate 	STRLOW
343*7c478bd9Sstevel@tonic-gate };
344*7c478bd9Sstevel@tonic-gate 
345*7c478bd9Sstevel@tonic-gate struct  qinit passthru_rinit = {
346*7c478bd9Sstevel@tonic-gate 	(int (*)())putnext,
347*7c478bd9Sstevel@tonic-gate 	NULL,
348*7c478bd9Sstevel@tonic-gate 	NULL,
349*7c478bd9Sstevel@tonic-gate 	NULL,
350*7c478bd9Sstevel@tonic-gate 	NULL,
351*7c478bd9Sstevel@tonic-gate 	&passthru_info,
352*7c478bd9Sstevel@tonic-gate 	NULL
353*7c478bd9Sstevel@tonic-gate };
354*7c478bd9Sstevel@tonic-gate 
355*7c478bd9Sstevel@tonic-gate struct  qinit passthru_winit = {
356*7c478bd9Sstevel@tonic-gate 	(int (*)()) pass_wput,
357*7c478bd9Sstevel@tonic-gate 	NULL,
358*7c478bd9Sstevel@tonic-gate 	NULL,
359*7c478bd9Sstevel@tonic-gate 	NULL,
360*7c478bd9Sstevel@tonic-gate 	NULL,
361*7c478bd9Sstevel@tonic-gate 	&passthru_info,
362*7c478bd9Sstevel@tonic-gate 	NULL
363*7c478bd9Sstevel@tonic-gate };
364*7c478bd9Sstevel@tonic-gate 
365*7c478bd9Sstevel@tonic-gate /*
366*7c478bd9Sstevel@tonic-gate  * Special form of assertion: verify that X implies Y i.e. when X is true Y
367*7c478bd9Sstevel@tonic-gate  * should also be true.
368*7c478bd9Sstevel@tonic-gate  */
369*7c478bd9Sstevel@tonic-gate #define	IMPLY(X, Y)	ASSERT(!(X) || (Y))
370*7c478bd9Sstevel@tonic-gate 
371*7c478bd9Sstevel@tonic-gate /*
372*7c478bd9Sstevel@tonic-gate  * Logical equivalence. Verify that both X and Y are either TRUE or FALSE.
373*7c478bd9Sstevel@tonic-gate  */
374*7c478bd9Sstevel@tonic-gate #define	EQUIV(X, Y)	{ IMPLY(X, Y); IMPLY(Y, X); }
375*7c478bd9Sstevel@tonic-gate 
376*7c478bd9Sstevel@tonic-gate /*
377*7c478bd9Sstevel@tonic-gate  * Verify correctness of list head/tail pointers.
378*7c478bd9Sstevel@tonic-gate  */
379*7c478bd9Sstevel@tonic-gate #define	LISTCHECK(head, tail, link) {				\
380*7c478bd9Sstevel@tonic-gate 	EQUIV(head, tail);					\
381*7c478bd9Sstevel@tonic-gate 	IMPLY(tail != NULL, tail->link == NULL);		\
382*7c478bd9Sstevel@tonic-gate }
383*7c478bd9Sstevel@tonic-gate 
384*7c478bd9Sstevel@tonic-gate /*
385*7c478bd9Sstevel@tonic-gate  * Enqueue a list element `el' in the end of a list denoted by `head' and `tail'
386*7c478bd9Sstevel@tonic-gate  * using a `link' field.
387*7c478bd9Sstevel@tonic-gate  */
388*7c478bd9Sstevel@tonic-gate #define	ENQUEUE(el, head, tail, link) {				\
389*7c478bd9Sstevel@tonic-gate 	ASSERT(el->link == NULL);				\
390*7c478bd9Sstevel@tonic-gate 	LISTCHECK(head, tail, link);				\
391*7c478bd9Sstevel@tonic-gate 	if (head == NULL)					\
392*7c478bd9Sstevel@tonic-gate 		head = el;					\
393*7c478bd9Sstevel@tonic-gate 	else							\
394*7c478bd9Sstevel@tonic-gate 		tail->link = el;				\
395*7c478bd9Sstevel@tonic-gate 	tail = el;						\
396*7c478bd9Sstevel@tonic-gate }
397*7c478bd9Sstevel@tonic-gate 
398*7c478bd9Sstevel@tonic-gate /*
399*7c478bd9Sstevel@tonic-gate  * Dequeue the first element of the list denoted by `head' and `tail' pointers
400*7c478bd9Sstevel@tonic-gate  * using a `link' field and put result into `el'.
401*7c478bd9Sstevel@tonic-gate  */
402*7c478bd9Sstevel@tonic-gate #define	DQ(el, head, tail, link) {				\
403*7c478bd9Sstevel@tonic-gate 	LISTCHECK(head, tail, link);				\
404*7c478bd9Sstevel@tonic-gate 	el = head;						\
405*7c478bd9Sstevel@tonic-gate 	if (head != NULL) {					\
406*7c478bd9Sstevel@tonic-gate 		head = head->link;				\
407*7c478bd9Sstevel@tonic-gate 		if (head == NULL)				\
408*7c478bd9Sstevel@tonic-gate 			tail = NULL;				\
409*7c478bd9Sstevel@tonic-gate 		el->link = NULL;				\
410*7c478bd9Sstevel@tonic-gate 	}							\
411*7c478bd9Sstevel@tonic-gate }
412*7c478bd9Sstevel@tonic-gate 
413*7c478bd9Sstevel@tonic-gate /*
414*7c478bd9Sstevel@tonic-gate  * Remove `el' from the list using `chase' and `curr' pointers and return result
415*7c478bd9Sstevel@tonic-gate  * in `succeed'.
416*7c478bd9Sstevel@tonic-gate  */
417*7c478bd9Sstevel@tonic-gate #define	RMQ(el, head, tail, link, chase, curr, succeed) {	\
418*7c478bd9Sstevel@tonic-gate 	LISTCHECK(head, tail, link);				\
419*7c478bd9Sstevel@tonic-gate 	chase = NULL;						\
420*7c478bd9Sstevel@tonic-gate 	succeed = 0;						\
421*7c478bd9Sstevel@tonic-gate 	for (curr = head; (curr != el) && (curr != NULL); curr = curr->link) \
422*7c478bd9Sstevel@tonic-gate 		chase = curr;					\
423*7c478bd9Sstevel@tonic-gate 	if (curr != NULL) {					\
424*7c478bd9Sstevel@tonic-gate 		succeed = 1;					\
425*7c478bd9Sstevel@tonic-gate 		ASSERT(curr == el);				\
426*7c478bd9Sstevel@tonic-gate 		if (chase != NULL)				\
427*7c478bd9Sstevel@tonic-gate 			chase->link = curr->link;		\
428*7c478bd9Sstevel@tonic-gate 		else						\
429*7c478bd9Sstevel@tonic-gate 			head = curr->link;			\
430*7c478bd9Sstevel@tonic-gate 		curr->link = NULL;				\
431*7c478bd9Sstevel@tonic-gate 		if (curr == tail)				\
432*7c478bd9Sstevel@tonic-gate 			tail = chase;				\
433*7c478bd9Sstevel@tonic-gate 	}							\
434*7c478bd9Sstevel@tonic-gate 	LISTCHECK(head, tail, link);				\
435*7c478bd9Sstevel@tonic-gate }
436*7c478bd9Sstevel@tonic-gate 
437*7c478bd9Sstevel@tonic-gate /* Handling of delayed messages on the inner syncq. */
438*7c478bd9Sstevel@tonic-gate 
439*7c478bd9Sstevel@tonic-gate /*
440*7c478bd9Sstevel@tonic-gate  * DEBUG versions should use function versions (to simplify tracing) and
441*7c478bd9Sstevel@tonic-gate  * non-DEBUG kernels should use macro versions.
442*7c478bd9Sstevel@tonic-gate  */
443*7c478bd9Sstevel@tonic-gate 
444*7c478bd9Sstevel@tonic-gate /*
445*7c478bd9Sstevel@tonic-gate  * Put a queue on the syncq list of queues.
446*7c478bd9Sstevel@tonic-gate  * Assumes SQLOCK held.
447*7c478bd9Sstevel@tonic-gate  */
448*7c478bd9Sstevel@tonic-gate #define	SQPUT_Q(sq, qp)							\
449*7c478bd9Sstevel@tonic-gate {									\
450*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(SQLOCK(sq)));					\
451*7c478bd9Sstevel@tonic-gate 	if (!(qp->q_sqflags & Q_SQQUEUED)) {				\
452*7c478bd9Sstevel@tonic-gate 		/* The queue should not be linked anywhere */		\
453*7c478bd9Sstevel@tonic-gate 		ASSERT((qp->q_sqprev == NULL) && (qp->q_sqnext == NULL)); \
454*7c478bd9Sstevel@tonic-gate 		/* Head and tail may only be NULL simultaneously */	\
455*7c478bd9Sstevel@tonic-gate 		EQUIV(sq->sq_head, sq->sq_tail);			\
456*7c478bd9Sstevel@tonic-gate 		/* Queue may be only enqueyed on its syncq */		\
457*7c478bd9Sstevel@tonic-gate 		ASSERT(sq == qp->q_syncq);				\
458*7c478bd9Sstevel@tonic-gate 		/* Check the correctness of SQ_MESSAGES flag */		\
459*7c478bd9Sstevel@tonic-gate 		EQUIV(sq->sq_head, (sq->sq_flags & SQ_MESSAGES));	\
460*7c478bd9Sstevel@tonic-gate 		/* Sanity check first/last elements of the list */	\
461*7c478bd9Sstevel@tonic-gate 		IMPLY(sq->sq_head != NULL, sq->sq_head->q_sqprev == NULL);\
462*7c478bd9Sstevel@tonic-gate 		IMPLY(sq->sq_tail != NULL, sq->sq_tail->q_sqnext == NULL);\
463*7c478bd9Sstevel@tonic-gate 		/*							\
464*7c478bd9Sstevel@tonic-gate 		 * Sanity check of priority field: empty queue should	\
465*7c478bd9Sstevel@tonic-gate 		 * have zero priority					\
466*7c478bd9Sstevel@tonic-gate 		 * and nqueues equal to zero.				\
467*7c478bd9Sstevel@tonic-gate 		 */							\
468*7c478bd9Sstevel@tonic-gate 		IMPLY(sq->sq_head == NULL, sq->sq_pri == 0);		\
469*7c478bd9Sstevel@tonic-gate 		/* Sanity check of sq_nqueues field */			\
470*7c478bd9Sstevel@tonic-gate 		EQUIV(sq->sq_head, sq->sq_nqueues);			\
471*7c478bd9Sstevel@tonic-gate 		if (sq->sq_head == NULL) {				\
472*7c478bd9Sstevel@tonic-gate 			sq->sq_head = sq->sq_tail = qp;			\
473*7c478bd9Sstevel@tonic-gate 			sq->sq_flags |= SQ_MESSAGES;			\
474*7c478bd9Sstevel@tonic-gate 		} else if (qp->q_spri == 0) {				\
475*7c478bd9Sstevel@tonic-gate 			qp->q_sqprev = sq->sq_tail;			\
476*7c478bd9Sstevel@tonic-gate 			sq->sq_tail->q_sqnext = qp;			\
477*7c478bd9Sstevel@tonic-gate 			sq->sq_tail = qp;				\
478*7c478bd9Sstevel@tonic-gate 		} else {						\
479*7c478bd9Sstevel@tonic-gate 			/*						\
480*7c478bd9Sstevel@tonic-gate 			 * Put this queue in priority order: higher	\
481*7c478bd9Sstevel@tonic-gate 			 * priority gets closer to the head.		\
482*7c478bd9Sstevel@tonic-gate 			 */						\
483*7c478bd9Sstevel@tonic-gate 			queue_t **qpp = &sq->sq_tail;			\
484*7c478bd9Sstevel@tonic-gate 			queue_t *qnext = NULL;				\
485*7c478bd9Sstevel@tonic-gate 									\
486*7c478bd9Sstevel@tonic-gate 			while (*qpp != NULL && qp->q_spri > (*qpp)->q_spri) { \
487*7c478bd9Sstevel@tonic-gate 				qnext = *qpp;				\
488*7c478bd9Sstevel@tonic-gate 				qpp = &(*qpp)->q_sqprev;		\
489*7c478bd9Sstevel@tonic-gate 			}						\
490*7c478bd9Sstevel@tonic-gate 			qp->q_sqnext = qnext;				\
491*7c478bd9Sstevel@tonic-gate 			qp->q_sqprev = *qpp;				\
492*7c478bd9Sstevel@tonic-gate 			if (*qpp != NULL) {				\
493*7c478bd9Sstevel@tonic-gate 				(*qpp)->q_sqnext = qp;			\
494*7c478bd9Sstevel@tonic-gate 			} else {					\
495*7c478bd9Sstevel@tonic-gate 				sq->sq_head = qp;			\
496*7c478bd9Sstevel@tonic-gate 				sq->sq_pri = sq->sq_head->q_spri;	\
497*7c478bd9Sstevel@tonic-gate 			}						\
498*7c478bd9Sstevel@tonic-gate 			*qpp = qp;					\
499*7c478bd9Sstevel@tonic-gate 		}							\
500*7c478bd9Sstevel@tonic-gate 		qp->q_sqflags |= Q_SQQUEUED;				\
501*7c478bd9Sstevel@tonic-gate 		qp->q_sqtstamp = lbolt;					\
502*7c478bd9Sstevel@tonic-gate 		sq->sq_nqueues++;					\
503*7c478bd9Sstevel@tonic-gate 	}								\
504*7c478bd9Sstevel@tonic-gate }
505*7c478bd9Sstevel@tonic-gate 
506*7c478bd9Sstevel@tonic-gate /*
507*7c478bd9Sstevel@tonic-gate  * Remove a queue from the syncq list
508*7c478bd9Sstevel@tonic-gate  * Assumes SQLOCK held.
509*7c478bd9Sstevel@tonic-gate  */
510*7c478bd9Sstevel@tonic-gate #define	SQRM_Q(sq, qp)							\
511*7c478bd9Sstevel@tonic-gate 	{								\
512*7c478bd9Sstevel@tonic-gate 		ASSERT(MUTEX_HELD(SQLOCK(sq)));				\
513*7c478bd9Sstevel@tonic-gate 		ASSERT(qp->q_sqflags & Q_SQQUEUED);			\
514*7c478bd9Sstevel@tonic-gate 		ASSERT(sq->sq_head != NULL && sq->sq_tail != NULL);	\
515*7c478bd9Sstevel@tonic-gate 		ASSERT((sq->sq_flags & SQ_MESSAGES) != 0);		\
516*7c478bd9Sstevel@tonic-gate 		/* Check that the queue is actually in the list */	\
517*7c478bd9Sstevel@tonic-gate 		ASSERT(qp->q_sqnext != NULL || sq->sq_tail == qp);	\
518*7c478bd9Sstevel@tonic-gate 		ASSERT(qp->q_sqprev != NULL || sq->sq_head == qp);	\
519*7c478bd9Sstevel@tonic-gate 		ASSERT(sq->sq_nqueues != 0);				\
520*7c478bd9Sstevel@tonic-gate 		if (qp->q_sqprev == NULL) {				\
521*7c478bd9Sstevel@tonic-gate 			/* First queue on list, make head q_sqnext */	\
522*7c478bd9Sstevel@tonic-gate 			sq->sq_head = qp->q_sqnext;			\
523*7c478bd9Sstevel@tonic-gate 		} else {						\
524*7c478bd9Sstevel@tonic-gate 			/* Make prev->next == next */			\
525*7c478bd9Sstevel@tonic-gate 			qp->q_sqprev->q_sqnext = qp->q_sqnext;		\
526*7c478bd9Sstevel@tonic-gate 		}							\
527*7c478bd9Sstevel@tonic-gate 		if (qp->q_sqnext == NULL) {				\
528*7c478bd9Sstevel@tonic-gate 			/* Last queue on list, make tail sqprev */	\
529*7c478bd9Sstevel@tonic-gate 			sq->sq_tail = qp->q_sqprev;			\
530*7c478bd9Sstevel@tonic-gate 		} else {						\
531*7c478bd9Sstevel@tonic-gate 			/* Make next->prev == prev */			\
532*7c478bd9Sstevel@tonic-gate 			qp->q_sqnext->q_sqprev = qp->q_sqprev;		\
533*7c478bd9Sstevel@tonic-gate 		}							\
534*7c478bd9Sstevel@tonic-gate 		/* clear out references on this queue */		\
535*7c478bd9Sstevel@tonic-gate 		qp->q_sqprev = qp->q_sqnext = NULL;			\
536*7c478bd9Sstevel@tonic-gate 		qp->q_sqflags &= ~Q_SQQUEUED;				\
537*7c478bd9Sstevel@tonic-gate 		/* If there is nothing queued, clear SQ_MESSAGES */	\
538*7c478bd9Sstevel@tonic-gate 		if (sq->sq_head != NULL) {				\
539*7c478bd9Sstevel@tonic-gate 			sq->sq_pri = sq->sq_head->q_spri;		\
540*7c478bd9Sstevel@tonic-gate 		} else	{						\
541*7c478bd9Sstevel@tonic-gate 			sq->sq_flags &= ~SQ_MESSAGES;			\
542*7c478bd9Sstevel@tonic-gate 			sq->sq_pri = 0;					\
543*7c478bd9Sstevel@tonic-gate 		}							\
544*7c478bd9Sstevel@tonic-gate 		sq->sq_nqueues--;					\
545*7c478bd9Sstevel@tonic-gate 		ASSERT(sq->sq_head != NULL || sq->sq_evhead != NULL ||	\
546*7c478bd9Sstevel@tonic-gate 		    (sq->sq_flags & SQ_QUEUED) == 0);			\
547*7c478bd9Sstevel@tonic-gate 	}
548*7c478bd9Sstevel@tonic-gate 
549*7c478bd9Sstevel@tonic-gate /* Hide the definition from the header file. */
550*7c478bd9Sstevel@tonic-gate #ifdef SQPUT_MP
551*7c478bd9Sstevel@tonic-gate #undef SQPUT_MP
552*7c478bd9Sstevel@tonic-gate #endif
553*7c478bd9Sstevel@tonic-gate 
554*7c478bd9Sstevel@tonic-gate /*
555*7c478bd9Sstevel@tonic-gate  * Put a message on the queue syncq.
556*7c478bd9Sstevel@tonic-gate  * Assumes QLOCK held.
557*7c478bd9Sstevel@tonic-gate  */
558*7c478bd9Sstevel@tonic-gate #define	SQPUT_MP(qp, mp)						\
559*7c478bd9Sstevel@tonic-gate 	{								\
560*7c478bd9Sstevel@tonic-gate 		ASSERT(MUTEX_HELD(QLOCK(qp)));				\
561*7c478bd9Sstevel@tonic-gate 		ASSERT(qp->q_sqhead == NULL ||				\
562*7c478bd9Sstevel@tonic-gate 		    (qp->q_sqtail != NULL &&				\
563*7c478bd9Sstevel@tonic-gate 		    qp->q_sqtail->b_next == NULL));			\
564*7c478bd9Sstevel@tonic-gate 		qp->q_syncqmsgs++;					\
565*7c478bd9Sstevel@tonic-gate 		ASSERT(qp->q_syncqmsgs != 0);	/* Wraparound */	\
566*7c478bd9Sstevel@tonic-gate 		if (qp->q_sqhead == NULL) {				\
567*7c478bd9Sstevel@tonic-gate 			qp->q_sqhead = qp->q_sqtail = mp;		\
568*7c478bd9Sstevel@tonic-gate 		} else {						\
569*7c478bd9Sstevel@tonic-gate 			qp->q_sqtail->b_next = mp;			\
570*7c478bd9Sstevel@tonic-gate 			qp->q_sqtail = mp;				\
571*7c478bd9Sstevel@tonic-gate 		}							\
572*7c478bd9Sstevel@tonic-gate 		ASSERT(qp->q_syncqmsgs > 0);				\
573*7c478bd9Sstevel@tonic-gate 	}
574*7c478bd9Sstevel@tonic-gate 
575*7c478bd9Sstevel@tonic-gate #define	SQ_PUTCOUNT_SETFAST_LOCKED(sq) {				\
576*7c478bd9Sstevel@tonic-gate 		ASSERT(MUTEX_HELD(SQLOCK(sq)));				\
577*7c478bd9Sstevel@tonic-gate 		if ((sq)->sq_ciputctrl != NULL) {			\
578*7c478bd9Sstevel@tonic-gate 			int i;						\
579*7c478bd9Sstevel@tonic-gate 			int nlocks = (sq)->sq_nciputctrl;		\
580*7c478bd9Sstevel@tonic-gate 			ciputctrl_t *cip = (sq)->sq_ciputctrl;		\
581*7c478bd9Sstevel@tonic-gate 			ASSERT((sq)->sq_type & SQ_CIPUT);		\
582*7c478bd9Sstevel@tonic-gate 			for (i = 0; i <= nlocks; i++) {			\
583*7c478bd9Sstevel@tonic-gate 				ASSERT(MUTEX_HELD(&cip[i].ciputctrl_lock)); \
584*7c478bd9Sstevel@tonic-gate 				cip[i].ciputctrl_count |= SQ_FASTPUT;	\
585*7c478bd9Sstevel@tonic-gate 			}						\
586*7c478bd9Sstevel@tonic-gate 		}							\
587*7c478bd9Sstevel@tonic-gate 	}
588*7c478bd9Sstevel@tonic-gate 
589*7c478bd9Sstevel@tonic-gate 
590*7c478bd9Sstevel@tonic-gate #define	SQ_PUTCOUNT_CLRFAST_LOCKED(sq) {				\
591*7c478bd9Sstevel@tonic-gate 		ASSERT(MUTEX_HELD(SQLOCK(sq)));				\
592*7c478bd9Sstevel@tonic-gate 		if ((sq)->sq_ciputctrl != NULL) {			\
593*7c478bd9Sstevel@tonic-gate 			int i;						\
594*7c478bd9Sstevel@tonic-gate 			int nlocks = (sq)->sq_nciputctrl;		\
595*7c478bd9Sstevel@tonic-gate 			ciputctrl_t *cip = (sq)->sq_ciputctrl;		\
596*7c478bd9Sstevel@tonic-gate 			ASSERT((sq)->sq_type & SQ_CIPUT);		\
597*7c478bd9Sstevel@tonic-gate 			for (i = 0; i <= nlocks; i++) {			\
598*7c478bd9Sstevel@tonic-gate 				ASSERT(MUTEX_HELD(&cip[i].ciputctrl_lock)); \
599*7c478bd9Sstevel@tonic-gate 				cip[i].ciputctrl_count &= ~SQ_FASTPUT;	\
600*7c478bd9Sstevel@tonic-gate 			}						\
601*7c478bd9Sstevel@tonic-gate 		}							\
602*7c478bd9Sstevel@tonic-gate 	}
603*7c478bd9Sstevel@tonic-gate 
604*7c478bd9Sstevel@tonic-gate /*
605*7c478bd9Sstevel@tonic-gate  * Run service procedures for all queues in the stream head.
606*7c478bd9Sstevel@tonic-gate  */
607*7c478bd9Sstevel@tonic-gate #define	STR_SERVICE(stp, q) {						\
608*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&stp->sd_qlock));				\
609*7c478bd9Sstevel@tonic-gate 	while (stp->sd_qhead != NULL) {					\
610*7c478bd9Sstevel@tonic-gate 		DQ(q, stp->sd_qhead, stp->sd_qtail, q_link);		\
611*7c478bd9Sstevel@tonic-gate 		ASSERT(stp->sd_nqueues > 0);				\
612*7c478bd9Sstevel@tonic-gate 		stp->sd_nqueues--;					\
613*7c478bd9Sstevel@tonic-gate 		ASSERT(!(q->q_flag & QINSERVICE));			\
614*7c478bd9Sstevel@tonic-gate 		mutex_exit(&stp->sd_qlock);				\
615*7c478bd9Sstevel@tonic-gate 		queue_service(q);					\
616*7c478bd9Sstevel@tonic-gate 		mutex_enter(&stp->sd_qlock);				\
617*7c478bd9Sstevel@tonic-gate 	}								\
618*7c478bd9Sstevel@tonic-gate 	ASSERT(stp->sd_nqueues == 0);					\
619*7c478bd9Sstevel@tonic-gate 	ASSERT((stp->sd_qhead == NULL) && (stp->sd_qtail == NULL));	\
620*7c478bd9Sstevel@tonic-gate }
621*7c478bd9Sstevel@tonic-gate 
622*7c478bd9Sstevel@tonic-gate /*
623*7c478bd9Sstevel@tonic-gate  * constructor/destructor routines for the stream head cache
624*7c478bd9Sstevel@tonic-gate  */
625*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
626*7c478bd9Sstevel@tonic-gate static int
627*7c478bd9Sstevel@tonic-gate stream_head_constructor(void *buf, void *cdrarg, int kmflags)
628*7c478bd9Sstevel@tonic-gate {
629*7c478bd9Sstevel@tonic-gate 	stdata_t *stp = buf;
630*7c478bd9Sstevel@tonic-gate 
631*7c478bd9Sstevel@tonic-gate 	mutex_init(&stp->sd_lock, NULL, MUTEX_DEFAULT, NULL);
632*7c478bd9Sstevel@tonic-gate 	mutex_init(&stp->sd_reflock, NULL, MUTEX_DEFAULT, NULL);
633*7c478bd9Sstevel@tonic-gate 	mutex_init(&stp->sd_qlock, NULL, MUTEX_DEFAULT, NULL);
634*7c478bd9Sstevel@tonic-gate 	cv_init(&stp->sd_monitor, NULL, CV_DEFAULT, NULL);
635*7c478bd9Sstevel@tonic-gate 	cv_init(&stp->sd_iocmonitor, NULL, CV_DEFAULT, NULL);
636*7c478bd9Sstevel@tonic-gate 	cv_init(&stp->sd_qcv, NULL, CV_DEFAULT, NULL);
637*7c478bd9Sstevel@tonic-gate 	cv_init(&stp->sd_zcopy_wait, NULL, CV_DEFAULT, NULL);
638*7c478bd9Sstevel@tonic-gate 	stp->sd_wrq = NULL;
639*7c478bd9Sstevel@tonic-gate 
640*7c478bd9Sstevel@tonic-gate 	return (0);
641*7c478bd9Sstevel@tonic-gate }
642*7c478bd9Sstevel@tonic-gate 
643*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
644*7c478bd9Sstevel@tonic-gate static void
645*7c478bd9Sstevel@tonic-gate stream_head_destructor(void *buf, void *cdrarg)
646*7c478bd9Sstevel@tonic-gate {
647*7c478bd9Sstevel@tonic-gate 	stdata_t *stp = buf;
648*7c478bd9Sstevel@tonic-gate 
649*7c478bd9Sstevel@tonic-gate 	mutex_destroy(&stp->sd_lock);
650*7c478bd9Sstevel@tonic-gate 	mutex_destroy(&stp->sd_reflock);
651*7c478bd9Sstevel@tonic-gate 	mutex_destroy(&stp->sd_qlock);
652*7c478bd9Sstevel@tonic-gate 	cv_destroy(&stp->sd_monitor);
653*7c478bd9Sstevel@tonic-gate 	cv_destroy(&stp->sd_iocmonitor);
654*7c478bd9Sstevel@tonic-gate 	cv_destroy(&stp->sd_qcv);
655*7c478bd9Sstevel@tonic-gate 	cv_destroy(&stp->sd_zcopy_wait);
656*7c478bd9Sstevel@tonic-gate }
657*7c478bd9Sstevel@tonic-gate 
658*7c478bd9Sstevel@tonic-gate /*
659*7c478bd9Sstevel@tonic-gate  * constructor/destructor routines for the queue cache
660*7c478bd9Sstevel@tonic-gate  */
661*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
662*7c478bd9Sstevel@tonic-gate static int
663*7c478bd9Sstevel@tonic-gate queue_constructor(void *buf, void *cdrarg, int kmflags)
664*7c478bd9Sstevel@tonic-gate {
665*7c478bd9Sstevel@tonic-gate 	queinfo_t *qip = buf;
666*7c478bd9Sstevel@tonic-gate 	queue_t *qp = &qip->qu_rqueue;
667*7c478bd9Sstevel@tonic-gate 	queue_t *wqp = &qip->qu_wqueue;
668*7c478bd9Sstevel@tonic-gate 	syncq_t	*sq = &qip->qu_syncq;
669*7c478bd9Sstevel@tonic-gate 
670*7c478bd9Sstevel@tonic-gate 	qp->q_first = NULL;
671*7c478bd9Sstevel@tonic-gate 	qp->q_link = NULL;
672*7c478bd9Sstevel@tonic-gate 	qp->q_count = 0;
673*7c478bd9Sstevel@tonic-gate 	qp->q_mblkcnt = 0;
674*7c478bd9Sstevel@tonic-gate 	qp->q_sqhead = NULL;
675*7c478bd9Sstevel@tonic-gate 	qp->q_sqtail = NULL;
676*7c478bd9Sstevel@tonic-gate 	qp->q_sqnext = NULL;
677*7c478bd9Sstevel@tonic-gate 	qp->q_sqprev = NULL;
678*7c478bd9Sstevel@tonic-gate 	qp->q_sqflags = 0;
679*7c478bd9Sstevel@tonic-gate 	qp->q_rwcnt = 0;
680*7c478bd9Sstevel@tonic-gate 	qp->q_spri = 0;
681*7c478bd9Sstevel@tonic-gate 
682*7c478bd9Sstevel@tonic-gate 	mutex_init(QLOCK(qp), NULL, MUTEX_DEFAULT, NULL);
683*7c478bd9Sstevel@tonic-gate 	cv_init(&qp->q_wait, NULL, CV_DEFAULT, NULL);
684*7c478bd9Sstevel@tonic-gate 
685*7c478bd9Sstevel@tonic-gate 	wqp->q_first = NULL;
686*7c478bd9Sstevel@tonic-gate 	wqp->q_link = NULL;
687*7c478bd9Sstevel@tonic-gate 	wqp->q_count = 0;
688*7c478bd9Sstevel@tonic-gate 	wqp->q_mblkcnt = 0;
689*7c478bd9Sstevel@tonic-gate 	wqp->q_sqhead = NULL;
690*7c478bd9Sstevel@tonic-gate 	wqp->q_sqtail = NULL;
691*7c478bd9Sstevel@tonic-gate 	wqp->q_sqnext = NULL;
692*7c478bd9Sstevel@tonic-gate 	wqp->q_sqprev = NULL;
693*7c478bd9Sstevel@tonic-gate 	wqp->q_sqflags = 0;
694*7c478bd9Sstevel@tonic-gate 	wqp->q_rwcnt = 0;
695*7c478bd9Sstevel@tonic-gate 	wqp->q_spri = 0;
696*7c478bd9Sstevel@tonic-gate 
697*7c478bd9Sstevel@tonic-gate 	mutex_init(QLOCK(wqp), NULL, MUTEX_DEFAULT, NULL);
698*7c478bd9Sstevel@tonic-gate 	cv_init(&wqp->q_wait, NULL, CV_DEFAULT, NULL);
699*7c478bd9Sstevel@tonic-gate 
700*7c478bd9Sstevel@tonic-gate 	sq->sq_head = NULL;
701*7c478bd9Sstevel@tonic-gate 	sq->sq_tail = NULL;
702*7c478bd9Sstevel@tonic-gate 	sq->sq_evhead = NULL;
703*7c478bd9Sstevel@tonic-gate 	sq->sq_evtail = NULL;
704*7c478bd9Sstevel@tonic-gate 	sq->sq_callbpend = NULL;
705*7c478bd9Sstevel@tonic-gate 	sq->sq_outer = NULL;
706*7c478bd9Sstevel@tonic-gate 	sq->sq_onext = NULL;
707*7c478bd9Sstevel@tonic-gate 	sq->sq_oprev = NULL;
708*7c478bd9Sstevel@tonic-gate 	sq->sq_next = NULL;
709*7c478bd9Sstevel@tonic-gate 	sq->sq_svcflags = 0;
710*7c478bd9Sstevel@tonic-gate 	sq->sq_servcount = 0;
711*7c478bd9Sstevel@tonic-gate 	sq->sq_needexcl = 0;
712*7c478bd9Sstevel@tonic-gate 	sq->sq_nqueues = 0;
713*7c478bd9Sstevel@tonic-gate 	sq->sq_pri = 0;
714*7c478bd9Sstevel@tonic-gate 
715*7c478bd9Sstevel@tonic-gate 	mutex_init(&sq->sq_lock, NULL, MUTEX_DEFAULT, NULL);
716*7c478bd9Sstevel@tonic-gate 	cv_init(&sq->sq_wait, NULL, CV_DEFAULT, NULL);
717*7c478bd9Sstevel@tonic-gate 	cv_init(&sq->sq_exitwait, NULL, CV_DEFAULT, NULL);
718*7c478bd9Sstevel@tonic-gate 
719*7c478bd9Sstevel@tonic-gate 	return (0);
720*7c478bd9Sstevel@tonic-gate }
721*7c478bd9Sstevel@tonic-gate 
722*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
723*7c478bd9Sstevel@tonic-gate static void
724*7c478bd9Sstevel@tonic-gate queue_destructor(void *buf, void *cdrarg)
725*7c478bd9Sstevel@tonic-gate {
726*7c478bd9Sstevel@tonic-gate 	queinfo_t *qip = buf;
727*7c478bd9Sstevel@tonic-gate 	queue_t *qp = &qip->qu_rqueue;
728*7c478bd9Sstevel@tonic-gate 	queue_t *wqp = &qip->qu_wqueue;
729*7c478bd9Sstevel@tonic-gate 	syncq_t	*sq = &qip->qu_syncq;
730*7c478bd9Sstevel@tonic-gate 
731*7c478bd9Sstevel@tonic-gate 	ASSERT(qp->q_sqhead == NULL);
732*7c478bd9Sstevel@tonic-gate 	ASSERT(wqp->q_sqhead == NULL);
733*7c478bd9Sstevel@tonic-gate 	ASSERT(qp->q_sqnext == NULL);
734*7c478bd9Sstevel@tonic-gate 	ASSERT(wqp->q_sqnext == NULL);
735*7c478bd9Sstevel@tonic-gate 	ASSERT(qp->q_rwcnt == 0);
736*7c478bd9Sstevel@tonic-gate 	ASSERT(wqp->q_rwcnt == 0);
737*7c478bd9Sstevel@tonic-gate 
738*7c478bd9Sstevel@tonic-gate 	mutex_destroy(&qp->q_lock);
739*7c478bd9Sstevel@tonic-gate 	cv_destroy(&qp->q_wait);
740*7c478bd9Sstevel@tonic-gate 
741*7c478bd9Sstevel@tonic-gate 	mutex_destroy(&wqp->q_lock);
742*7c478bd9Sstevel@tonic-gate 	cv_destroy(&wqp->q_wait);
743*7c478bd9Sstevel@tonic-gate 
744*7c478bd9Sstevel@tonic-gate 	mutex_destroy(&sq->sq_lock);
745*7c478bd9Sstevel@tonic-gate 	cv_destroy(&sq->sq_wait);
746*7c478bd9Sstevel@tonic-gate 	cv_destroy(&sq->sq_exitwait);
747*7c478bd9Sstevel@tonic-gate }
748*7c478bd9Sstevel@tonic-gate 
749*7c478bd9Sstevel@tonic-gate /*
750*7c478bd9Sstevel@tonic-gate  * constructor/destructor routines for the syncq cache
751*7c478bd9Sstevel@tonic-gate  */
752*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
753*7c478bd9Sstevel@tonic-gate static int
754*7c478bd9Sstevel@tonic-gate syncq_constructor(void *buf, void *cdrarg, int kmflags)
755*7c478bd9Sstevel@tonic-gate {
756*7c478bd9Sstevel@tonic-gate 	syncq_t	*sq = buf;
757*7c478bd9Sstevel@tonic-gate 
758*7c478bd9Sstevel@tonic-gate 	bzero(buf, sizeof (syncq_t));
759*7c478bd9Sstevel@tonic-gate 
760*7c478bd9Sstevel@tonic-gate 	mutex_init(&sq->sq_lock, NULL, MUTEX_DEFAULT, NULL);
761*7c478bd9Sstevel@tonic-gate 	cv_init(&sq->sq_wait, NULL, CV_DEFAULT, NULL);
762*7c478bd9Sstevel@tonic-gate 	cv_init(&sq->sq_exitwait, NULL, CV_DEFAULT, NULL);
763*7c478bd9Sstevel@tonic-gate 
764*7c478bd9Sstevel@tonic-gate 	return (0);
765*7c478bd9Sstevel@tonic-gate }
766*7c478bd9Sstevel@tonic-gate 
767*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
768*7c478bd9Sstevel@tonic-gate static void
769*7c478bd9Sstevel@tonic-gate syncq_destructor(void *buf, void *cdrarg)
770*7c478bd9Sstevel@tonic-gate {
771*7c478bd9Sstevel@tonic-gate 	syncq_t	*sq = buf;
772*7c478bd9Sstevel@tonic-gate 
773*7c478bd9Sstevel@tonic-gate 	ASSERT(sq->sq_head == NULL);
774*7c478bd9Sstevel@tonic-gate 	ASSERT(sq->sq_tail == NULL);
775*7c478bd9Sstevel@tonic-gate 	ASSERT(sq->sq_evhead == NULL);
776*7c478bd9Sstevel@tonic-gate 	ASSERT(sq->sq_evtail == NULL);
777*7c478bd9Sstevel@tonic-gate 	ASSERT(sq->sq_callbpend == NULL);
778*7c478bd9Sstevel@tonic-gate 	ASSERT(sq->sq_callbflags == 0);
779*7c478bd9Sstevel@tonic-gate 	ASSERT(sq->sq_outer == NULL);
780*7c478bd9Sstevel@tonic-gate 	ASSERT(sq->sq_onext == NULL);
781*7c478bd9Sstevel@tonic-gate 	ASSERT(sq->sq_oprev == NULL);
782*7c478bd9Sstevel@tonic-gate 	ASSERT(sq->sq_next == NULL);
783*7c478bd9Sstevel@tonic-gate 	ASSERT(sq->sq_needexcl == 0);
784*7c478bd9Sstevel@tonic-gate 	ASSERT(sq->sq_svcflags == 0);
785*7c478bd9Sstevel@tonic-gate 	ASSERT(sq->sq_servcount == 0);
786*7c478bd9Sstevel@tonic-gate 	ASSERT(sq->sq_nqueues == 0);
787*7c478bd9Sstevel@tonic-gate 	ASSERT(sq->sq_pri == 0);
788*7c478bd9Sstevel@tonic-gate 	ASSERT(sq->sq_count == 0);
789*7c478bd9Sstevel@tonic-gate 	ASSERT(sq->sq_rmqcount == 0);
790*7c478bd9Sstevel@tonic-gate 	ASSERT(sq->sq_cancelid == 0);
791*7c478bd9Sstevel@tonic-gate 	ASSERT(sq->sq_ciputctrl == NULL);
792*7c478bd9Sstevel@tonic-gate 	ASSERT(sq->sq_nciputctrl == 0);
793*7c478bd9Sstevel@tonic-gate 	ASSERT(sq->sq_type == 0);
794*7c478bd9Sstevel@tonic-gate 	ASSERT(sq->sq_flags == 0);
795*7c478bd9Sstevel@tonic-gate 
796*7c478bd9Sstevel@tonic-gate 	mutex_destroy(&sq->sq_lock);
797*7c478bd9Sstevel@tonic-gate 	cv_destroy(&sq->sq_wait);
798*7c478bd9Sstevel@tonic-gate 	cv_destroy(&sq->sq_exitwait);
799*7c478bd9Sstevel@tonic-gate }
800*7c478bd9Sstevel@tonic-gate 
801*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
802*7c478bd9Sstevel@tonic-gate static int
803*7c478bd9Sstevel@tonic-gate ciputctrl_constructor(void *buf, void *cdrarg, int kmflags)
804*7c478bd9Sstevel@tonic-gate {
805*7c478bd9Sstevel@tonic-gate 	ciputctrl_t *cip = buf;
806*7c478bd9Sstevel@tonic-gate 	int i;
807*7c478bd9Sstevel@tonic-gate 
808*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < n_ciputctrl; i++) {
809*7c478bd9Sstevel@tonic-gate 		cip[i].ciputctrl_count = SQ_FASTPUT;
810*7c478bd9Sstevel@tonic-gate 		mutex_init(&cip[i].ciputctrl_lock, NULL, MUTEX_DEFAULT, NULL);
811*7c478bd9Sstevel@tonic-gate 	}
812*7c478bd9Sstevel@tonic-gate 
813*7c478bd9Sstevel@tonic-gate 	return (0);
814*7c478bd9Sstevel@tonic-gate }
815*7c478bd9Sstevel@tonic-gate 
816*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
817*7c478bd9Sstevel@tonic-gate static void
818*7c478bd9Sstevel@tonic-gate ciputctrl_destructor(void *buf, void *cdrarg)
819*7c478bd9Sstevel@tonic-gate {
820*7c478bd9Sstevel@tonic-gate 	ciputctrl_t *cip = buf;
821*7c478bd9Sstevel@tonic-gate 	int i;
822*7c478bd9Sstevel@tonic-gate 
823*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < n_ciputctrl; i++) {
824*7c478bd9Sstevel@tonic-gate 		ASSERT(cip[i].ciputctrl_count & SQ_FASTPUT);
825*7c478bd9Sstevel@tonic-gate 		mutex_destroy(&cip[i].ciputctrl_lock);
826*7c478bd9Sstevel@tonic-gate 	}
827*7c478bd9Sstevel@tonic-gate }
828*7c478bd9Sstevel@tonic-gate 
829*7c478bd9Sstevel@tonic-gate /*
830*7c478bd9Sstevel@tonic-gate  * Init routine run from main at boot time.
831*7c478bd9Sstevel@tonic-gate  */
832*7c478bd9Sstevel@tonic-gate void
833*7c478bd9Sstevel@tonic-gate strinit(void)
834*7c478bd9Sstevel@tonic-gate {
835*7c478bd9Sstevel@tonic-gate 	int i;
836*7c478bd9Sstevel@tonic-gate 	int ncpus = ((boot_max_ncpus == -1) ? max_ncpus : boot_max_ncpus);
837*7c478bd9Sstevel@tonic-gate 
838*7c478bd9Sstevel@tonic-gate 	/*
839*7c478bd9Sstevel@tonic-gate 	 * Set up mux_node structures.
840*7c478bd9Sstevel@tonic-gate 	 */
841*7c478bd9Sstevel@tonic-gate 	mux_nodes = kmem_zalloc((sizeof (struct mux_node) * devcnt), KM_SLEEP);
842*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < devcnt; i++)
843*7c478bd9Sstevel@tonic-gate 		mux_nodes[i].mn_imaj = i;
844*7c478bd9Sstevel@tonic-gate 
845*7c478bd9Sstevel@tonic-gate 	stream_head_cache = kmem_cache_create("stream_head_cache",
846*7c478bd9Sstevel@tonic-gate 		sizeof (stdata_t), 0,
847*7c478bd9Sstevel@tonic-gate 		stream_head_constructor, stream_head_destructor, NULL,
848*7c478bd9Sstevel@tonic-gate 		NULL, NULL, 0);
849*7c478bd9Sstevel@tonic-gate 
850*7c478bd9Sstevel@tonic-gate 	queue_cache = kmem_cache_create("queue_cache", sizeof (queinfo_t), 0,
851*7c478bd9Sstevel@tonic-gate 		queue_constructor, queue_destructor, NULL, NULL, NULL, 0);
852*7c478bd9Sstevel@tonic-gate 
853*7c478bd9Sstevel@tonic-gate 	syncq_cache = kmem_cache_create("syncq_cache", sizeof (syncq_t), 0,
854*7c478bd9Sstevel@tonic-gate 		syncq_constructor, syncq_destructor, NULL, NULL, NULL, 0);
855*7c478bd9Sstevel@tonic-gate 
856*7c478bd9Sstevel@tonic-gate 	qband_cache = kmem_cache_create("qband_cache",
857*7c478bd9Sstevel@tonic-gate 		sizeof (qband_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
858*7c478bd9Sstevel@tonic-gate 
859*7c478bd9Sstevel@tonic-gate 	linkinfo_cache = kmem_cache_create("linkinfo_cache",
860*7c478bd9Sstevel@tonic-gate 		sizeof (linkinfo_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
861*7c478bd9Sstevel@tonic-gate 
862*7c478bd9Sstevel@tonic-gate 	n_ciputctrl = ncpus;
863*7c478bd9Sstevel@tonic-gate 	n_ciputctrl = 1 << highbit(n_ciputctrl - 1);
864*7c478bd9Sstevel@tonic-gate 	ASSERT(n_ciputctrl >= 1);
865*7c478bd9Sstevel@tonic-gate 	n_ciputctrl = MIN(n_ciputctrl, max_n_ciputctrl);
866*7c478bd9Sstevel@tonic-gate 	if (n_ciputctrl >= min_n_ciputctrl) {
867*7c478bd9Sstevel@tonic-gate 		ciputctrl_cache = kmem_cache_create("ciputctrl_cache",
868*7c478bd9Sstevel@tonic-gate 			sizeof (ciputctrl_t) * n_ciputctrl,
869*7c478bd9Sstevel@tonic-gate 			sizeof (ciputctrl_t), ciputctrl_constructor,
870*7c478bd9Sstevel@tonic-gate 			ciputctrl_destructor, NULL, NULL, NULL, 0);
871*7c478bd9Sstevel@tonic-gate 	}
872*7c478bd9Sstevel@tonic-gate 
873*7c478bd9Sstevel@tonic-gate 	streams_taskq = system_taskq;
874*7c478bd9Sstevel@tonic-gate 
875*7c478bd9Sstevel@tonic-gate 	if (streams_taskq == NULL)
876*7c478bd9Sstevel@tonic-gate 		panic("strinit: no memory for streams taskq!");
877*7c478bd9Sstevel@tonic-gate 
878*7c478bd9Sstevel@tonic-gate 	bc_bkgrnd_thread = thread_create(NULL, 0,
879*7c478bd9Sstevel@tonic-gate 	    streams_bufcall_service, NULL, 0, &p0, TS_RUN, streams_lopri);
880*7c478bd9Sstevel@tonic-gate 
881*7c478bd9Sstevel@tonic-gate 	streams_qbkgrnd_thread = thread_create(NULL, 0,
882*7c478bd9Sstevel@tonic-gate 	    streams_qbkgrnd_service, NULL, 0, &p0, TS_RUN, streams_lopri);
883*7c478bd9Sstevel@tonic-gate 
884*7c478bd9Sstevel@tonic-gate 	streams_sqbkgrnd_thread = thread_create(NULL, 0,
885*7c478bd9Sstevel@tonic-gate 	    streams_sqbkgrnd_service, NULL, 0, &p0, TS_RUN, streams_lopri);
886*7c478bd9Sstevel@tonic-gate 
887*7c478bd9Sstevel@tonic-gate 	/*
888*7c478bd9Sstevel@tonic-gate 	 * Create STREAMS kstats.
889*7c478bd9Sstevel@tonic-gate 	 */
890*7c478bd9Sstevel@tonic-gate 	str_kstat = kstat_create("streams", 0, "strstat",
891*7c478bd9Sstevel@tonic-gate 	    "net", KSTAT_TYPE_NAMED,
892*7c478bd9Sstevel@tonic-gate 	    sizeof (str_statistics) / sizeof (kstat_named_t),
893*7c478bd9Sstevel@tonic-gate 	    KSTAT_FLAG_VIRTUAL);
894*7c478bd9Sstevel@tonic-gate 
895*7c478bd9Sstevel@tonic-gate 	if (str_kstat != NULL) {
896*7c478bd9Sstevel@tonic-gate 		str_kstat->ks_data = &str_statistics;
897*7c478bd9Sstevel@tonic-gate 		kstat_install(str_kstat);
898*7c478bd9Sstevel@tonic-gate 	}
899*7c478bd9Sstevel@tonic-gate 
900*7c478bd9Sstevel@tonic-gate 	/*
901*7c478bd9Sstevel@tonic-gate 	 * TPI support routine initialisation.
902*7c478bd9Sstevel@tonic-gate 	 */
903*7c478bd9Sstevel@tonic-gate 	tpi_init();
904*7c478bd9Sstevel@tonic-gate }
905*7c478bd9Sstevel@tonic-gate 
906*7c478bd9Sstevel@tonic-gate void
907*7c478bd9Sstevel@tonic-gate str_sendsig(vnode_t *vp, int event, uchar_t band, int error)
908*7c478bd9Sstevel@tonic-gate {
909*7c478bd9Sstevel@tonic-gate 	struct stdata *stp;
910*7c478bd9Sstevel@tonic-gate 
911*7c478bd9Sstevel@tonic-gate 	ASSERT(vp->v_stream);
912*7c478bd9Sstevel@tonic-gate 	stp = vp->v_stream;
913*7c478bd9Sstevel@tonic-gate 	/* Have to hold sd_lock to prevent siglist from changing */
914*7c478bd9Sstevel@tonic-gate 	mutex_enter(&stp->sd_lock);
915*7c478bd9Sstevel@tonic-gate 	if (stp->sd_sigflags & event)
916*7c478bd9Sstevel@tonic-gate 		strsendsig(stp->sd_siglist, event, band, error);
917*7c478bd9Sstevel@tonic-gate 	mutex_exit(&stp->sd_lock);
918*7c478bd9Sstevel@tonic-gate }
919*7c478bd9Sstevel@tonic-gate 
920*7c478bd9Sstevel@tonic-gate /*
921*7c478bd9Sstevel@tonic-gate  * Send the "sevent" set of signals to a process.
922*7c478bd9Sstevel@tonic-gate  * This might send more than one signal if the process is registered
923*7c478bd9Sstevel@tonic-gate  * for multiple events. The caller should pass in an sevent that only
924*7c478bd9Sstevel@tonic-gate  * includes the events for which the process has registered.
925*7c478bd9Sstevel@tonic-gate  */
926*7c478bd9Sstevel@tonic-gate static void
927*7c478bd9Sstevel@tonic-gate dosendsig(proc_t *proc, int events, int sevent, k_siginfo_t *info,
928*7c478bd9Sstevel@tonic-gate 	uchar_t band, int error)
929*7c478bd9Sstevel@tonic-gate {
930*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&proc->p_lock));
931*7c478bd9Sstevel@tonic-gate 
932*7c478bd9Sstevel@tonic-gate 	info->si_band = 0;
933*7c478bd9Sstevel@tonic-gate 	info->si_errno = 0;
934*7c478bd9Sstevel@tonic-gate 
935*7c478bd9Sstevel@tonic-gate 	if (sevent & S_ERROR) {
936*7c478bd9Sstevel@tonic-gate 		sevent &= ~S_ERROR;
937*7c478bd9Sstevel@tonic-gate 		info->si_code = POLL_ERR;
938*7c478bd9Sstevel@tonic-gate 		info->si_errno = error;
939*7c478bd9Sstevel@tonic-gate 		TRACE_2(TR_FAC_STREAMS_FR, TR_STRSENDSIG,
940*7c478bd9Sstevel@tonic-gate 			"strsendsig:proc %p info %p", proc, info);
941*7c478bd9Sstevel@tonic-gate 		sigaddq(proc, NULL, info, KM_NOSLEEP);
942*7c478bd9Sstevel@tonic-gate 		info->si_errno = 0;
943*7c478bd9Sstevel@tonic-gate 	}
944*7c478bd9Sstevel@tonic-gate 	if (sevent & S_HANGUP) {
945*7c478bd9Sstevel@tonic-gate 		sevent &= ~S_HANGUP;
946*7c478bd9Sstevel@tonic-gate 		info->si_code = POLL_HUP;
947*7c478bd9Sstevel@tonic-gate 		TRACE_2(TR_FAC_STREAMS_FR, TR_STRSENDSIG,
948*7c478bd9Sstevel@tonic-gate 			"strsendsig:proc %p info %p", proc, info);
949*7c478bd9Sstevel@tonic-gate 		sigaddq(proc, NULL, info, KM_NOSLEEP);
950*7c478bd9Sstevel@tonic-gate 	}
951*7c478bd9Sstevel@tonic-gate 	if (sevent & S_HIPRI) {
952*7c478bd9Sstevel@tonic-gate 		sevent &= ~S_HIPRI;
953*7c478bd9Sstevel@tonic-gate 		info->si_code = POLL_PRI;
954*7c478bd9Sstevel@tonic-gate 		TRACE_2(TR_FAC_STREAMS_FR, TR_STRSENDSIG,
955*7c478bd9Sstevel@tonic-gate 			"strsendsig:proc %p info %p", proc, info);
956*7c478bd9Sstevel@tonic-gate 		sigaddq(proc, NULL, info, KM_NOSLEEP);
957*7c478bd9Sstevel@tonic-gate 	}
958*7c478bd9Sstevel@tonic-gate 	if (sevent & S_RDBAND) {
959*7c478bd9Sstevel@tonic-gate 		sevent &= ~S_RDBAND;
960*7c478bd9Sstevel@tonic-gate 		if (events & S_BANDURG)
961*7c478bd9Sstevel@tonic-gate 			sigtoproc(proc, NULL, SIGURG);
962*7c478bd9Sstevel@tonic-gate 		else
963*7c478bd9Sstevel@tonic-gate 			sigtoproc(proc, NULL, SIGPOLL);
964*7c478bd9Sstevel@tonic-gate 	}
965*7c478bd9Sstevel@tonic-gate 	if (sevent & S_WRBAND) {
966*7c478bd9Sstevel@tonic-gate 		sevent &= ~S_WRBAND;
967*7c478bd9Sstevel@tonic-gate 		sigtoproc(proc, NULL, SIGPOLL);
968*7c478bd9Sstevel@tonic-gate 	}
969*7c478bd9Sstevel@tonic-gate 	if (sevent & S_INPUT) {
970*7c478bd9Sstevel@tonic-gate 		sevent &= ~S_INPUT;
971*7c478bd9Sstevel@tonic-gate 		info->si_code = POLL_IN;
972*7c478bd9Sstevel@tonic-gate 		info->si_band = band;
973*7c478bd9Sstevel@tonic-gate 		TRACE_2(TR_FAC_STREAMS_FR, TR_STRSENDSIG,
974*7c478bd9Sstevel@tonic-gate 			"strsendsig:proc %p info %p", proc, info);
975*7c478bd9Sstevel@tonic-gate 		sigaddq(proc, NULL, info, KM_NOSLEEP);
976*7c478bd9Sstevel@tonic-gate 		info->si_band = 0;
977*7c478bd9Sstevel@tonic-gate 	}
978*7c478bd9Sstevel@tonic-gate 	if (sevent & S_OUTPUT) {
979*7c478bd9Sstevel@tonic-gate 		sevent &= ~S_OUTPUT;
980*7c478bd9Sstevel@tonic-gate 		info->si_code = POLL_OUT;
981*7c478bd9Sstevel@tonic-gate 		info->si_band = band;
982*7c478bd9Sstevel@tonic-gate 		TRACE_2(TR_FAC_STREAMS_FR, TR_STRSENDSIG,
983*7c478bd9Sstevel@tonic-gate 			"strsendsig:proc %p info %p", proc, info);
984*7c478bd9Sstevel@tonic-gate 		sigaddq(proc, NULL, info, KM_NOSLEEP);
985*7c478bd9Sstevel@tonic-gate 		info->si_band = 0;
986*7c478bd9Sstevel@tonic-gate 	}
987*7c478bd9Sstevel@tonic-gate 	if (sevent & S_MSG) {
988*7c478bd9Sstevel@tonic-gate 		sevent &= ~S_MSG;
989*7c478bd9Sstevel@tonic-gate 		info->si_code = POLL_MSG;
990*7c478bd9Sstevel@tonic-gate 		info->si_band = band;
991*7c478bd9Sstevel@tonic-gate 		TRACE_2(TR_FAC_STREAMS_FR, TR_STRSENDSIG,
992*7c478bd9Sstevel@tonic-gate 			"strsendsig:proc %p info %p", proc, info);
993*7c478bd9Sstevel@tonic-gate 		sigaddq(proc, NULL, info, KM_NOSLEEP);
994*7c478bd9Sstevel@tonic-gate 		info->si_band = 0;
995*7c478bd9Sstevel@tonic-gate 	}
996*7c478bd9Sstevel@tonic-gate 	if (sevent & S_RDNORM) {
997*7c478bd9Sstevel@tonic-gate 		sevent &= ~S_RDNORM;
998*7c478bd9Sstevel@tonic-gate 		sigtoproc(proc, NULL, SIGPOLL);
999*7c478bd9Sstevel@tonic-gate 	}
1000*7c478bd9Sstevel@tonic-gate 	if (sevent != 0) {
1001*7c478bd9Sstevel@tonic-gate 		panic("strsendsig: unknown event(s) %x", sevent);
1002*7c478bd9Sstevel@tonic-gate 	}
1003*7c478bd9Sstevel@tonic-gate }
1004*7c478bd9Sstevel@tonic-gate 
1005*7c478bd9Sstevel@tonic-gate /*
1006*7c478bd9Sstevel@tonic-gate  * Send SIGPOLL/SIGURG signal to all processes and process groups
1007*7c478bd9Sstevel@tonic-gate  * registered on the given signal list that want a signal for at
1008*7c478bd9Sstevel@tonic-gate  * least one of the specified events.
1009*7c478bd9Sstevel@tonic-gate  *
1010*7c478bd9Sstevel@tonic-gate  * Must be called with exclusive access to siglist (caller holding sd_lock).
1011*7c478bd9Sstevel@tonic-gate  *
1012*7c478bd9Sstevel@tonic-gate  * strioctl(I_SETSIG/I_ESETSIG) will only change siglist when holding
1013*7c478bd9Sstevel@tonic-gate  * sd_lock and the ioctl code maintains a PID_HOLD on the pid structure
1014*7c478bd9Sstevel@tonic-gate  * while it is in the siglist.
1015*7c478bd9Sstevel@tonic-gate  *
1016*7c478bd9Sstevel@tonic-gate  * For performance reasons (MP scalability) the code drops pidlock
1017*7c478bd9Sstevel@tonic-gate  * when sending signals to a single process.
1018*7c478bd9Sstevel@tonic-gate  * When sending to a process group the code holds
1019*7c478bd9Sstevel@tonic-gate  * pidlock to prevent the membership in the process group from changing
1020*7c478bd9Sstevel@tonic-gate  * while walking the p_pglink list.
1021*7c478bd9Sstevel@tonic-gate  */
1022*7c478bd9Sstevel@tonic-gate void
1023*7c478bd9Sstevel@tonic-gate strsendsig(strsig_t *siglist, int event, uchar_t band, int error)
1024*7c478bd9Sstevel@tonic-gate {
1025*7c478bd9Sstevel@tonic-gate 	strsig_t *ssp;
1026*7c478bd9Sstevel@tonic-gate 	k_siginfo_t info;
1027*7c478bd9Sstevel@tonic-gate 	struct pid *pidp;
1028*7c478bd9Sstevel@tonic-gate 	proc_t  *proc;
1029*7c478bd9Sstevel@tonic-gate 
1030*7c478bd9Sstevel@tonic-gate 	info.si_signo = SIGPOLL;
1031*7c478bd9Sstevel@tonic-gate 	info.si_errno = 0;
1032*7c478bd9Sstevel@tonic-gate 	for (ssp = siglist; ssp; ssp = ssp->ss_next) {
1033*7c478bd9Sstevel@tonic-gate 		int sevent;
1034*7c478bd9Sstevel@tonic-gate 
1035*7c478bd9Sstevel@tonic-gate 		sevent = ssp->ss_events & event;
1036*7c478bd9Sstevel@tonic-gate 		if (sevent == 0)
1037*7c478bd9Sstevel@tonic-gate 			continue;
1038*7c478bd9Sstevel@tonic-gate 
1039*7c478bd9Sstevel@tonic-gate 		if ((pidp = ssp->ss_pidp) == NULL) {
1040*7c478bd9Sstevel@tonic-gate 			/* pid was released but still on event list */
1041*7c478bd9Sstevel@tonic-gate 			continue;
1042*7c478bd9Sstevel@tonic-gate 		}
1043*7c478bd9Sstevel@tonic-gate 
1044*7c478bd9Sstevel@tonic-gate 
1045*7c478bd9Sstevel@tonic-gate 		if (ssp->ss_pid > 0) {
1046*7c478bd9Sstevel@tonic-gate 			/*
1047*7c478bd9Sstevel@tonic-gate 			 * XXX This unfortunately still generates
1048*7c478bd9Sstevel@tonic-gate 			 * a signal when a fd is closed but
1049*7c478bd9Sstevel@tonic-gate 			 * the proc is active.
1050*7c478bd9Sstevel@tonic-gate 			 */
1051*7c478bd9Sstevel@tonic-gate 			ASSERT(ssp->ss_pid == pidp->pid_id);
1052*7c478bd9Sstevel@tonic-gate 
1053*7c478bd9Sstevel@tonic-gate 			mutex_enter(&pidlock);
1054*7c478bd9Sstevel@tonic-gate 			proc = prfind_zone(pidp->pid_id, ALL_ZONES);
1055*7c478bd9Sstevel@tonic-gate 			if (proc == NULL) {
1056*7c478bd9Sstevel@tonic-gate 				mutex_exit(&pidlock);
1057*7c478bd9Sstevel@tonic-gate 				continue;
1058*7c478bd9Sstevel@tonic-gate 			}
1059*7c478bd9Sstevel@tonic-gate 			mutex_enter(&proc->p_lock);
1060*7c478bd9Sstevel@tonic-gate 			mutex_exit(&pidlock);
1061*7c478bd9Sstevel@tonic-gate 			dosendsig(proc, ssp->ss_events, sevent, &info,
1062*7c478bd9Sstevel@tonic-gate 				band, error);
1063*7c478bd9Sstevel@tonic-gate 			mutex_exit(&proc->p_lock);
1064*7c478bd9Sstevel@tonic-gate 		} else {
1065*7c478bd9Sstevel@tonic-gate 			/*
1066*7c478bd9Sstevel@tonic-gate 			 * Send to process group. Hold pidlock across
1067*7c478bd9Sstevel@tonic-gate 			 * calls to dosendsig().
1068*7c478bd9Sstevel@tonic-gate 			 */
1069*7c478bd9Sstevel@tonic-gate 			pid_t pgrp = -ssp->ss_pid;
1070*7c478bd9Sstevel@tonic-gate 
1071*7c478bd9Sstevel@tonic-gate 			mutex_enter(&pidlock);
1072*7c478bd9Sstevel@tonic-gate 			proc = pgfind_zone(pgrp, ALL_ZONES);
1073*7c478bd9Sstevel@tonic-gate 			while (proc != NULL) {
1074*7c478bd9Sstevel@tonic-gate 				mutex_enter(&proc->p_lock);
1075*7c478bd9Sstevel@tonic-gate 				dosendsig(proc, ssp->ss_events, sevent,
1076*7c478bd9Sstevel@tonic-gate 					&info, band, error);
1077*7c478bd9Sstevel@tonic-gate 				mutex_exit(&proc->p_lock);
1078*7c478bd9Sstevel@tonic-gate 				proc = proc->p_pglink;
1079*7c478bd9Sstevel@tonic-gate 			}
1080*7c478bd9Sstevel@tonic-gate 			mutex_exit(&pidlock);
1081*7c478bd9Sstevel@tonic-gate 		}
1082*7c478bd9Sstevel@tonic-gate 	}
1083*7c478bd9Sstevel@tonic-gate }
1084*7c478bd9Sstevel@tonic-gate 
1085*7c478bd9Sstevel@tonic-gate /*
1086*7c478bd9Sstevel@tonic-gate  * Attach a stream device or module.
1087*7c478bd9Sstevel@tonic-gate  * qp is a read queue; the new queue goes in so its next
1088*7c478bd9Sstevel@tonic-gate  * read ptr is the argument, and the write queue corresponding
1089*7c478bd9Sstevel@tonic-gate  * to the argument points to this queue. Return 0 on success,
1090*7c478bd9Sstevel@tonic-gate  * or a non-zero errno on failure.
1091*7c478bd9Sstevel@tonic-gate  */
1092*7c478bd9Sstevel@tonic-gate int
1093*7c478bd9Sstevel@tonic-gate qattach(queue_t *qp, dev_t *devp, int oflag, cred_t *crp, fmodsw_impl_t *fp,
1094*7c478bd9Sstevel@tonic-gate     boolean_t is_insert)
1095*7c478bd9Sstevel@tonic-gate {
1096*7c478bd9Sstevel@tonic-gate 	major_t			major;
1097*7c478bd9Sstevel@tonic-gate 	cdevsw_impl_t		*dp;
1098*7c478bd9Sstevel@tonic-gate 	struct streamtab	*str;
1099*7c478bd9Sstevel@tonic-gate 	queue_t			*rq;
1100*7c478bd9Sstevel@tonic-gate 	queue_t			*wrq;
1101*7c478bd9Sstevel@tonic-gate 	uint32_t		qflag;
1102*7c478bd9Sstevel@tonic-gate 	uint32_t		sqtype;
1103*7c478bd9Sstevel@tonic-gate 	perdm_t			*dmp;
1104*7c478bd9Sstevel@tonic-gate 	int			error;
1105*7c478bd9Sstevel@tonic-gate 	int			sflag;
1106*7c478bd9Sstevel@tonic-gate 
1107*7c478bd9Sstevel@tonic-gate 	rq = allocq();
1108*7c478bd9Sstevel@tonic-gate 	wrq = _WR(rq);
1109*7c478bd9Sstevel@tonic-gate 	STREAM(rq) = STREAM(wrq) = STREAM(qp);
1110*7c478bd9Sstevel@tonic-gate 
1111*7c478bd9Sstevel@tonic-gate 	if (fp != NULL) {
1112*7c478bd9Sstevel@tonic-gate 		str = fp->f_str;
1113*7c478bd9Sstevel@tonic-gate 		qflag = fp->f_qflag;
1114*7c478bd9Sstevel@tonic-gate 		sqtype = fp->f_sqtype;
1115*7c478bd9Sstevel@tonic-gate 		dmp = fp->f_dmp;
1116*7c478bd9Sstevel@tonic-gate 		IMPLY((qflag & (QPERMOD | QMTOUTPERIM)), dmp != NULL);
1117*7c478bd9Sstevel@tonic-gate 		sflag = MODOPEN;
1118*7c478bd9Sstevel@tonic-gate 
1119*7c478bd9Sstevel@tonic-gate 		/*
1120*7c478bd9Sstevel@tonic-gate 		 * stash away a pointer to the module structure so we can
1121*7c478bd9Sstevel@tonic-gate 		 * unref it in qdetach.
1122*7c478bd9Sstevel@tonic-gate 		 */
1123*7c478bd9Sstevel@tonic-gate 		rq->q_fp = fp;
1124*7c478bd9Sstevel@tonic-gate 	} else {
1125*7c478bd9Sstevel@tonic-gate 		ASSERT(!is_insert);
1126*7c478bd9Sstevel@tonic-gate 
1127*7c478bd9Sstevel@tonic-gate 		major = getmajor(*devp);
1128*7c478bd9Sstevel@tonic-gate 		dp = &devimpl[major];
1129*7c478bd9Sstevel@tonic-gate 
1130*7c478bd9Sstevel@tonic-gate 		str = dp->d_str;
1131*7c478bd9Sstevel@tonic-gate 		ASSERT(str == STREAMSTAB(major));
1132*7c478bd9Sstevel@tonic-gate 
1133*7c478bd9Sstevel@tonic-gate 		qflag = dp->d_qflag;
1134*7c478bd9Sstevel@tonic-gate 		ASSERT(qflag & QISDRV);
1135*7c478bd9Sstevel@tonic-gate 		sqtype = dp->d_sqtype;
1136*7c478bd9Sstevel@tonic-gate 
1137*7c478bd9Sstevel@tonic-gate 		/* create perdm_t if needed */
1138*7c478bd9Sstevel@tonic-gate 		if (NEED_DM(dp->d_dmp, qflag))
1139*7c478bd9Sstevel@tonic-gate 			dp->d_dmp = hold_dm(str, qflag, sqtype);
1140*7c478bd9Sstevel@tonic-gate 
1141*7c478bd9Sstevel@tonic-gate 		dmp = dp->d_dmp;
1142*7c478bd9Sstevel@tonic-gate 		sflag = 0;
1143*7c478bd9Sstevel@tonic-gate 	}
1144*7c478bd9Sstevel@tonic-gate 
1145*7c478bd9Sstevel@tonic-gate 	TRACE_2(TR_FAC_STREAMS_FR, TR_QATTACH_FLAGS,
1146*7c478bd9Sstevel@tonic-gate 	    "qattach:qflag == %X(%X)", qflag, *devp);
1147*7c478bd9Sstevel@tonic-gate 
1148*7c478bd9Sstevel@tonic-gate 	/* setq might sleep in allocator - avoid holding locks. */
1149*7c478bd9Sstevel@tonic-gate 	setq(rq, str->st_rdinit, str->st_wrinit, dmp, qflag, sqtype, B_FALSE);
1150*7c478bd9Sstevel@tonic-gate 
1151*7c478bd9Sstevel@tonic-gate 	/*
1152*7c478bd9Sstevel@tonic-gate 	 * Before calling the module's open routine, set up the q_next
1153*7c478bd9Sstevel@tonic-gate 	 * pointer for inserting a module in the middle of a stream.
1154*7c478bd9Sstevel@tonic-gate 	 *
1155*7c478bd9Sstevel@tonic-gate 	 * Note that we can always set _QINSERTING and set up q_next
1156*7c478bd9Sstevel@tonic-gate 	 * pointer for both inserting and pushing a module.  Then there
1157*7c478bd9Sstevel@tonic-gate 	 * is no need for the is_insert parameter.  In insertq(), called
1158*7c478bd9Sstevel@tonic-gate 	 * by qprocson(), assume that q_next of the new module always points
1159*7c478bd9Sstevel@tonic-gate 	 * to the correct queue and use it for insertion.  Everything should
1160*7c478bd9Sstevel@tonic-gate 	 * work out fine.  But in the first release of _I_INSERT, we
1161*7c478bd9Sstevel@tonic-gate 	 * distinguish between inserting and pushing to make sure that
1162*7c478bd9Sstevel@tonic-gate 	 * pushing a module follows the same code path as before.
1163*7c478bd9Sstevel@tonic-gate 	 */
1164*7c478bd9Sstevel@tonic-gate 	if (is_insert) {
1165*7c478bd9Sstevel@tonic-gate 		rq->q_flag |= _QINSERTING;
1166*7c478bd9Sstevel@tonic-gate 		rq->q_next = qp;
1167*7c478bd9Sstevel@tonic-gate 	}
1168*7c478bd9Sstevel@tonic-gate 
1169*7c478bd9Sstevel@tonic-gate 	/*
1170*7c478bd9Sstevel@tonic-gate 	 * If there is an outer perimeter get exclusive access during
1171*7c478bd9Sstevel@tonic-gate 	 * the open procedure.  Bump up the reference count on the queue.
1172*7c478bd9Sstevel@tonic-gate 	 */
1173*7c478bd9Sstevel@tonic-gate 	entersq(rq->q_syncq, SQ_OPENCLOSE);
1174*7c478bd9Sstevel@tonic-gate 	error = (*rq->q_qinfo->qi_qopen)(rq, devp, oflag, sflag, crp);
1175*7c478bd9Sstevel@tonic-gate 	if (error != 0)
1176*7c478bd9Sstevel@tonic-gate 		goto failed;
1177*7c478bd9Sstevel@tonic-gate 	leavesq(rq->q_syncq, SQ_OPENCLOSE);
1178*7c478bd9Sstevel@tonic-gate 	ASSERT(qprocsareon(rq));
1179*7c478bd9Sstevel@tonic-gate 	return (0);
1180*7c478bd9Sstevel@tonic-gate 
1181*7c478bd9Sstevel@tonic-gate failed:
1182*7c478bd9Sstevel@tonic-gate 	rq->q_flag &= ~_QINSERTING;
1183*7c478bd9Sstevel@tonic-gate 	if (backq(wrq) != NULL && backq(wrq)->q_next == wrq)
1184*7c478bd9Sstevel@tonic-gate 		qprocsoff(rq);
1185*7c478bd9Sstevel@tonic-gate 	leavesq(rq->q_syncq, SQ_OPENCLOSE);
1186*7c478bd9Sstevel@tonic-gate 	rq->q_next = wrq->q_next = NULL;
1187*7c478bd9Sstevel@tonic-gate 	qdetach(rq, 0, 0, crp, B_FALSE);
1188*7c478bd9Sstevel@tonic-gate 	return (error);
1189*7c478bd9Sstevel@tonic-gate }
1190*7c478bd9Sstevel@tonic-gate 
1191*7c478bd9Sstevel@tonic-gate /*
1192*7c478bd9Sstevel@tonic-gate  * Handle second open of stream. For modules, set the
1193*7c478bd9Sstevel@tonic-gate  * last argument to MODOPEN and do not pass any open flags.
1194*7c478bd9Sstevel@tonic-gate  * Ignore dummydev since this is not the first open.
1195*7c478bd9Sstevel@tonic-gate  */
1196*7c478bd9Sstevel@tonic-gate int
1197*7c478bd9Sstevel@tonic-gate qreopen(queue_t *qp, dev_t *devp, int flag, cred_t *crp)
1198*7c478bd9Sstevel@tonic-gate {
1199*7c478bd9Sstevel@tonic-gate 	int	error;
1200*7c478bd9Sstevel@tonic-gate 	dev_t dummydev;
1201*7c478bd9Sstevel@tonic-gate 	queue_t *wqp = _WR(qp);
1202*7c478bd9Sstevel@tonic-gate 
1203*7c478bd9Sstevel@tonic-gate 	ASSERT(qp->q_flag & QREADR);
1204*7c478bd9Sstevel@tonic-gate 	entersq(qp->q_syncq, SQ_OPENCLOSE);
1205*7c478bd9Sstevel@tonic-gate 
1206*7c478bd9Sstevel@tonic-gate 	dummydev = *devp;
1207*7c478bd9Sstevel@tonic-gate 	if (error = ((*qp->q_qinfo->qi_qopen)(qp, &dummydev,
1208*7c478bd9Sstevel@tonic-gate 	    (wqp->q_next ? 0 : flag), (wqp->q_next ? MODOPEN : 0), crp))) {
1209*7c478bd9Sstevel@tonic-gate 		leavesq(qp->q_syncq, SQ_OPENCLOSE);
1210*7c478bd9Sstevel@tonic-gate 		mutex_enter(&STREAM(qp)->sd_lock);
1211*7c478bd9Sstevel@tonic-gate 		qp->q_stream->sd_flag |= STREOPENFAIL;
1212*7c478bd9Sstevel@tonic-gate 		mutex_exit(&STREAM(qp)->sd_lock);
1213*7c478bd9Sstevel@tonic-gate 		return (error);
1214*7c478bd9Sstevel@tonic-gate 	}
1215*7c478bd9Sstevel@tonic-gate 	leavesq(qp->q_syncq, SQ_OPENCLOSE);
1216*7c478bd9Sstevel@tonic-gate 
1217*7c478bd9Sstevel@tonic-gate 	/*
1218*7c478bd9Sstevel@tonic-gate 	 * successful open should have done qprocson()
1219*7c478bd9Sstevel@tonic-gate 	 */
1220*7c478bd9Sstevel@tonic-gate 	ASSERT(qprocsareon(_RD(qp)));
1221*7c478bd9Sstevel@tonic-gate 	return (0);
1222*7c478bd9Sstevel@tonic-gate }
1223*7c478bd9Sstevel@tonic-gate 
1224*7c478bd9Sstevel@tonic-gate /*
1225*7c478bd9Sstevel@tonic-gate  * Detach a stream module or device.
1226*7c478bd9Sstevel@tonic-gate  * If clmode == 1 then the module or driver was opened and its
1227*7c478bd9Sstevel@tonic-gate  * close routine must be called. If clmode == 0, the module
1228*7c478bd9Sstevel@tonic-gate  * or driver was never opened or the open failed, and so its close
1229*7c478bd9Sstevel@tonic-gate  * should not be called.
1230*7c478bd9Sstevel@tonic-gate  */
1231*7c478bd9Sstevel@tonic-gate void
1232*7c478bd9Sstevel@tonic-gate qdetach(queue_t *qp, int clmode, int flag, cred_t *crp, boolean_t is_remove)
1233*7c478bd9Sstevel@tonic-gate {
1234*7c478bd9Sstevel@tonic-gate 	queue_t *wqp = _WR(qp);
1235*7c478bd9Sstevel@tonic-gate 	ASSERT(STREAM(qp)->sd_flag & (STRCLOSE|STWOPEN|STRPLUMB));
1236*7c478bd9Sstevel@tonic-gate 
1237*7c478bd9Sstevel@tonic-gate 	if (STREAM_NEEDSERVICE(STREAM(qp)))
1238*7c478bd9Sstevel@tonic-gate 		stream_runservice(STREAM(qp));
1239*7c478bd9Sstevel@tonic-gate 
1240*7c478bd9Sstevel@tonic-gate 	if (clmode) {
1241*7c478bd9Sstevel@tonic-gate 		/*
1242*7c478bd9Sstevel@tonic-gate 		 * Make sure that all the messages on the write side syncq are
1243*7c478bd9Sstevel@tonic-gate 		 * processed and nothing is left. Since we are closing, no new
1244*7c478bd9Sstevel@tonic-gate 		 * messages may appear there.
1245*7c478bd9Sstevel@tonic-gate 		 */
1246*7c478bd9Sstevel@tonic-gate 		wait_q_syncq(wqp);
1247*7c478bd9Sstevel@tonic-gate 
1248*7c478bd9Sstevel@tonic-gate 		entersq(qp->q_syncq, SQ_OPENCLOSE);
1249*7c478bd9Sstevel@tonic-gate 		if (is_remove) {
1250*7c478bd9Sstevel@tonic-gate 			mutex_enter(QLOCK(qp));
1251*7c478bd9Sstevel@tonic-gate 			qp->q_flag |= _QREMOVING;
1252*7c478bd9Sstevel@tonic-gate 			mutex_exit(QLOCK(qp));
1253*7c478bd9Sstevel@tonic-gate 		}
1254*7c478bd9Sstevel@tonic-gate 		(*qp->q_qinfo->qi_qclose)(qp, flag, crp);
1255*7c478bd9Sstevel@tonic-gate 		/*
1256*7c478bd9Sstevel@tonic-gate 		 * Check that qprocsoff() was actually called.
1257*7c478bd9Sstevel@tonic-gate 		 */
1258*7c478bd9Sstevel@tonic-gate 		ASSERT((qp->q_flag & QWCLOSE) && (wqp->q_flag & QWCLOSE));
1259*7c478bd9Sstevel@tonic-gate 
1260*7c478bd9Sstevel@tonic-gate 		leavesq(qp->q_syncq, SQ_OPENCLOSE);
1261*7c478bd9Sstevel@tonic-gate 	} else {
1262*7c478bd9Sstevel@tonic-gate 		disable_svc(qp);
1263*7c478bd9Sstevel@tonic-gate 	}
1264*7c478bd9Sstevel@tonic-gate 
1265*7c478bd9Sstevel@tonic-gate 	/*
1266*7c478bd9Sstevel@tonic-gate 	 * Allow any threads blocked in entersq to proceed and discover
1267*7c478bd9Sstevel@tonic-gate 	 * the QWCLOSE is set.
1268*7c478bd9Sstevel@tonic-gate 	 * Note: This assumes that all users of entersq check QWCLOSE.
1269*7c478bd9Sstevel@tonic-gate 	 * Currently runservice is the only entersq that can happen
1270*7c478bd9Sstevel@tonic-gate 	 * after removeq has finished.
1271*7c478bd9Sstevel@tonic-gate 	 * Removeq will have discarded all messages destined to the closing
1272*7c478bd9Sstevel@tonic-gate 	 * pair of queues from the syncq.
1273*7c478bd9Sstevel@tonic-gate 	 * NOTE: Calling a function inside an assert is unconventional.
1274*7c478bd9Sstevel@tonic-gate 	 * However, it does not cause any problem since flush_syncq() does
1275*7c478bd9Sstevel@tonic-gate 	 * not change any state except when it returns non-zero i.e.
1276*7c478bd9Sstevel@tonic-gate 	 * when the assert will trigger.
1277*7c478bd9Sstevel@tonic-gate 	 */
1278*7c478bd9Sstevel@tonic-gate 	ASSERT(flush_syncq(qp->q_syncq, qp) == 0);
1279*7c478bd9Sstevel@tonic-gate 	ASSERT(flush_syncq(wqp->q_syncq, wqp) == 0);
1280*7c478bd9Sstevel@tonic-gate 	ASSERT((qp->q_flag & QPERMOD) ||
1281*7c478bd9Sstevel@tonic-gate 		((qp->q_syncq->sq_head == NULL) &&
1282*7c478bd9Sstevel@tonic-gate 		(wqp->q_syncq->sq_head == NULL)));
1283*7c478bd9Sstevel@tonic-gate 
1284*7c478bd9Sstevel@tonic-gate 	/*
1285*7c478bd9Sstevel@tonic-gate 	 * Flush the queues before q_next is set to NULL. This is needed
1286*7c478bd9Sstevel@tonic-gate 	 * in order to backenable any downstream queue before we go away.
1287*7c478bd9Sstevel@tonic-gate 	 * Note: we are already removed from the stream so that the
1288*7c478bd9Sstevel@tonic-gate 	 * backenabling will not cause any messages to be delivered to our
1289*7c478bd9Sstevel@tonic-gate 	 * put procedures.
1290*7c478bd9Sstevel@tonic-gate 	 */
1291*7c478bd9Sstevel@tonic-gate 	flushq(qp, FLUSHALL);
1292*7c478bd9Sstevel@tonic-gate 	flushq(wqp, FLUSHALL);
1293*7c478bd9Sstevel@tonic-gate 
1294*7c478bd9Sstevel@tonic-gate 	/*
1295*7c478bd9Sstevel@tonic-gate 	 * wait for any pending service processing to complete
1296*7c478bd9Sstevel@tonic-gate 	 */
1297*7c478bd9Sstevel@tonic-gate 	wait_svc(qp);
1298*7c478bd9Sstevel@tonic-gate 
1299*7c478bd9Sstevel@tonic-gate 	/* Tidy up - removeq only does a half-remove from stream */
1300*7c478bd9Sstevel@tonic-gate 	qp->q_next = wqp->q_next = NULL;
1301*7c478bd9Sstevel@tonic-gate 	ASSERT(!(qp->q_flag & QENAB));
1302*7c478bd9Sstevel@tonic-gate 	ASSERT(!(wqp->q_flag & QENAB));
1303*7c478bd9Sstevel@tonic-gate 
1304*7c478bd9Sstevel@tonic-gate 	/* release any fmodsw_impl_t structure held on behalf of the queue */
1305*7c478bd9Sstevel@tonic-gate 
1306*7c478bd9Sstevel@tonic-gate 	ASSERT(qp->q_fp != NULL || qp->q_flag & QISDRV);
1307*7c478bd9Sstevel@tonic-gate 	if (qp->q_fp != NULL)
1308*7c478bd9Sstevel@tonic-gate 		fmodsw_rele(qp->q_fp);
1309*7c478bd9Sstevel@tonic-gate 
1310*7c478bd9Sstevel@tonic-gate 	/* freeq removes us from the outer perimeter if any */
1311*7c478bd9Sstevel@tonic-gate 	freeq(qp);
1312*7c478bd9Sstevel@tonic-gate }
1313*7c478bd9Sstevel@tonic-gate 
1314*7c478bd9Sstevel@tonic-gate /* Prevent service procedures from being called */
1315*7c478bd9Sstevel@tonic-gate void
1316*7c478bd9Sstevel@tonic-gate disable_svc(queue_t *qp)
1317*7c478bd9Sstevel@tonic-gate {
1318*7c478bd9Sstevel@tonic-gate 	queue_t *wqp = _WR(qp);
1319*7c478bd9Sstevel@tonic-gate 
1320*7c478bd9Sstevel@tonic-gate 	ASSERT(qp->q_flag & QREADR);
1321*7c478bd9Sstevel@tonic-gate 	mutex_enter(QLOCK(qp));
1322*7c478bd9Sstevel@tonic-gate 	qp->q_flag |= QWCLOSE;
1323*7c478bd9Sstevel@tonic-gate 	mutex_exit(QLOCK(qp));
1324*7c478bd9Sstevel@tonic-gate 	mutex_enter(QLOCK(wqp));
1325*7c478bd9Sstevel@tonic-gate 	wqp->q_flag |= QWCLOSE;
1326*7c478bd9Sstevel@tonic-gate 	mutex_exit(QLOCK(wqp));
1327*7c478bd9Sstevel@tonic-gate }
1328*7c478bd9Sstevel@tonic-gate 
1329*7c478bd9Sstevel@tonic-gate /* allow service procedures to be called again */
1330*7c478bd9Sstevel@tonic-gate void
1331*7c478bd9Sstevel@tonic-gate enable_svc(queue_t *qp)
1332*7c478bd9Sstevel@tonic-gate {
1333*7c478bd9Sstevel@tonic-gate 	queue_t *wqp = _WR(qp);
1334*7c478bd9Sstevel@tonic-gate 
1335*7c478bd9Sstevel@tonic-gate 	ASSERT(qp->q_flag & QREADR);
1336*7c478bd9Sstevel@tonic-gate 	mutex_enter(QLOCK(qp));
1337*7c478bd9Sstevel@tonic-gate 	qp->q_flag &= ~QWCLOSE;
1338*7c478bd9Sstevel@tonic-gate 	mutex_exit(QLOCK(qp));
1339*7c478bd9Sstevel@tonic-gate 	mutex_enter(QLOCK(wqp));
1340*7c478bd9Sstevel@tonic-gate 	wqp->q_flag &= ~QWCLOSE;
1341*7c478bd9Sstevel@tonic-gate 	mutex_exit(QLOCK(wqp));
1342*7c478bd9Sstevel@tonic-gate }
1343*7c478bd9Sstevel@tonic-gate 
1344*7c478bd9Sstevel@tonic-gate /*
1345*7c478bd9Sstevel@tonic-gate  * Remove queue from qhead/qtail if it is enabled.
1346*7c478bd9Sstevel@tonic-gate  * Only reset QENAB if the queue was removed from the runlist.
1347*7c478bd9Sstevel@tonic-gate  * A queue goes through 3 stages:
1348*7c478bd9Sstevel@tonic-gate  *	It is on the service list and QENAB is set.
1349*7c478bd9Sstevel@tonic-gate  *	It is removed from the service list but QENAB is still set.
1350*7c478bd9Sstevel@tonic-gate  *	QENAB gets changed to QINSERVICE.
1351*7c478bd9Sstevel@tonic-gate  *	QINSERVICE is reset (when the service procedure is done)
1352*7c478bd9Sstevel@tonic-gate  * Thus we can not reset QENAB unless we actually removed it from the service
1353*7c478bd9Sstevel@tonic-gate  * queue.
1354*7c478bd9Sstevel@tonic-gate  */
1355*7c478bd9Sstevel@tonic-gate void
1356*7c478bd9Sstevel@tonic-gate remove_runlist(queue_t *qp)
1357*7c478bd9Sstevel@tonic-gate {
1358*7c478bd9Sstevel@tonic-gate 	if (qp->q_flag & QENAB && qhead != NULL) {
1359*7c478bd9Sstevel@tonic-gate 		queue_t *q_chase;
1360*7c478bd9Sstevel@tonic-gate 		queue_t *q_curr;
1361*7c478bd9Sstevel@tonic-gate 		int removed;
1362*7c478bd9Sstevel@tonic-gate 
1363*7c478bd9Sstevel@tonic-gate 		mutex_enter(&service_queue);
1364*7c478bd9Sstevel@tonic-gate 		RMQ(qp, qhead, qtail, q_link, q_chase, q_curr, removed);
1365*7c478bd9Sstevel@tonic-gate 		mutex_exit(&service_queue);
1366*7c478bd9Sstevel@tonic-gate 		if (removed) {
1367*7c478bd9Sstevel@tonic-gate 			STRSTAT(qremoved);
1368*7c478bd9Sstevel@tonic-gate 			qp->q_flag &= ~QENAB;
1369*7c478bd9Sstevel@tonic-gate 		}
1370*7c478bd9Sstevel@tonic-gate 	}
1371*7c478bd9Sstevel@tonic-gate }
1372*7c478bd9Sstevel@tonic-gate 
1373*7c478bd9Sstevel@tonic-gate 
1374*7c478bd9Sstevel@tonic-gate /*
1375*7c478bd9Sstevel@tonic-gate  * wait for any pending service processing to complete.
1376*7c478bd9Sstevel@tonic-gate  * The removal of queues from the runlist is not atomic with the
1377*7c478bd9Sstevel@tonic-gate  * clearing of the QENABLED flag and setting the INSERVICE flag.
1378*7c478bd9Sstevel@tonic-gate  * consequently it is possible for remove_runlist in strclose
1379*7c478bd9Sstevel@tonic-gate  * to not find the queue on the runlist but for it to be QENABLED
1380*7c478bd9Sstevel@tonic-gate  * and not yet INSERVICE -> hence wait_svc needs to check QENABLED
1381*7c478bd9Sstevel@tonic-gate  * as well as INSERVICE.
1382*7c478bd9Sstevel@tonic-gate  */
1383*7c478bd9Sstevel@tonic-gate void
1384*7c478bd9Sstevel@tonic-gate wait_svc(queue_t *qp)
1385*7c478bd9Sstevel@tonic-gate {
1386*7c478bd9Sstevel@tonic-gate 	queue_t *wqp = _WR(qp);
1387*7c478bd9Sstevel@tonic-gate 
1388*7c478bd9Sstevel@tonic-gate 	ASSERT(qp->q_flag & QREADR);
1389*7c478bd9Sstevel@tonic-gate 
1390*7c478bd9Sstevel@tonic-gate 	/*
1391*7c478bd9Sstevel@tonic-gate 	 * Try to remove queues from qhead/qtail list.
1392*7c478bd9Sstevel@tonic-gate 	 */
1393*7c478bd9Sstevel@tonic-gate 	if (qhead != NULL) {
1394*7c478bd9Sstevel@tonic-gate 		remove_runlist(qp);
1395*7c478bd9Sstevel@tonic-gate 		remove_runlist(wqp);
1396*7c478bd9Sstevel@tonic-gate 	}
1397*7c478bd9Sstevel@tonic-gate 	/*
1398*7c478bd9Sstevel@tonic-gate 	 * Wait till the syncqs associated with the queue
1399*7c478bd9Sstevel@tonic-gate 	 * will dissapear from background processing list.
1400*7c478bd9Sstevel@tonic-gate 	 * This only needs to be done for non-PERMOD perimeters since
1401*7c478bd9Sstevel@tonic-gate 	 * for PERMOD perimeters the syncq may be shared and will only be freed
1402*7c478bd9Sstevel@tonic-gate 	 * when the last module/driver is unloaded.
1403*7c478bd9Sstevel@tonic-gate 	 * If for PERMOD perimeters queue was on the syncq list, removeq()
1404*7c478bd9Sstevel@tonic-gate 	 * should call propagate_syncq() or drain_syncq() for it. Both of these
1405*7c478bd9Sstevel@tonic-gate 	 * function remove the queue from its syncq list, so sqthread will not
1406*7c478bd9Sstevel@tonic-gate 	 * try to access the queue.
1407*7c478bd9Sstevel@tonic-gate 	 */
1408*7c478bd9Sstevel@tonic-gate 	if (!(qp->q_flag & QPERMOD)) {
1409*7c478bd9Sstevel@tonic-gate 		syncq_t *rsq = qp->q_syncq;
1410*7c478bd9Sstevel@tonic-gate 		syncq_t *wsq = wqp->q_syncq;
1411*7c478bd9Sstevel@tonic-gate 
1412*7c478bd9Sstevel@tonic-gate 		/*
1413*7c478bd9Sstevel@tonic-gate 		 * Disable rsq and wsq and wait for any background processing of
1414*7c478bd9Sstevel@tonic-gate 		 * syncq to complete.
1415*7c478bd9Sstevel@tonic-gate 		 */
1416*7c478bd9Sstevel@tonic-gate 		wait_sq_svc(rsq);
1417*7c478bd9Sstevel@tonic-gate 		if (wsq != rsq)
1418*7c478bd9Sstevel@tonic-gate 			wait_sq_svc(wsq);
1419*7c478bd9Sstevel@tonic-gate 	}
1420*7c478bd9Sstevel@tonic-gate 
1421*7c478bd9Sstevel@tonic-gate 	mutex_enter(QLOCK(qp));
1422*7c478bd9Sstevel@tonic-gate 	while (qp->q_flag & (QINSERVICE|QENAB))
1423*7c478bd9Sstevel@tonic-gate 		cv_wait(&qp->q_wait, QLOCK(qp));
1424*7c478bd9Sstevel@tonic-gate 	mutex_exit(QLOCK(qp));
1425*7c478bd9Sstevel@tonic-gate 	mutex_enter(QLOCK(wqp));
1426*7c478bd9Sstevel@tonic-gate 	while (wqp->q_flag & (QINSERVICE|QENAB))
1427*7c478bd9Sstevel@tonic-gate 		cv_wait(&wqp->q_wait, QLOCK(wqp));
1428*7c478bd9Sstevel@tonic-gate 	mutex_exit(QLOCK(wqp));
1429*7c478bd9Sstevel@tonic-gate }
1430*7c478bd9Sstevel@tonic-gate 
1431*7c478bd9Sstevel@tonic-gate /*
1432*7c478bd9Sstevel@tonic-gate  * Put ioctl data from userland buffer `arg' into the mblk chain `bp'.
1433*7c478bd9Sstevel@tonic-gate  * `flag' must always contain either K_TO_K or U_TO_K; STR_NOSIG may
1434*7c478bd9Sstevel@tonic-gate  * also be set, and is passed through to allocb_cred_wait().
1435*7c478bd9Sstevel@tonic-gate  *
1436*7c478bd9Sstevel@tonic-gate  * Returns errno on failure, zero on success.
1437*7c478bd9Sstevel@tonic-gate  */
1438*7c478bd9Sstevel@tonic-gate int
1439*7c478bd9Sstevel@tonic-gate putiocd(mblk_t *bp, char *arg, int flag, cred_t *cr)
1440*7c478bd9Sstevel@tonic-gate {
1441*7c478bd9Sstevel@tonic-gate 	mblk_t *tmp;
1442*7c478bd9Sstevel@tonic-gate 	ssize_t  count;
1443*7c478bd9Sstevel@tonic-gate 	size_t n;
1444*7c478bd9Sstevel@tonic-gate 	int error = 0;
1445*7c478bd9Sstevel@tonic-gate 
1446*7c478bd9Sstevel@tonic-gate 	ASSERT((flag & (U_TO_K | K_TO_K)) == U_TO_K ||
1447*7c478bd9Sstevel@tonic-gate 		(flag & (U_TO_K | K_TO_K)) == K_TO_K);
1448*7c478bd9Sstevel@tonic-gate 
1449*7c478bd9Sstevel@tonic-gate 	if (bp->b_datap->db_type == M_IOCTL) {
1450*7c478bd9Sstevel@tonic-gate 		count = ((struct iocblk *)bp->b_rptr)->ioc_count;
1451*7c478bd9Sstevel@tonic-gate 	} else {
1452*7c478bd9Sstevel@tonic-gate 		ASSERT(bp->b_datap->db_type == M_COPYIN);
1453*7c478bd9Sstevel@tonic-gate 		count = ((struct copyreq *)bp->b_rptr)->cq_size;
1454*7c478bd9Sstevel@tonic-gate 	}
1455*7c478bd9Sstevel@tonic-gate 	/*
1456*7c478bd9Sstevel@tonic-gate 	 * strdoioctl validates ioc_count, so if this assert fails it
1457*7c478bd9Sstevel@tonic-gate 	 * cannot be due to user error.
1458*7c478bd9Sstevel@tonic-gate 	 */
1459*7c478bd9Sstevel@tonic-gate 	ASSERT(count >= 0);
1460*7c478bd9Sstevel@tonic-gate 
1461*7c478bd9Sstevel@tonic-gate 	while (count > 0) {
1462*7c478bd9Sstevel@tonic-gate 		n = MIN(MAXIOCBSZ, count);
1463*7c478bd9Sstevel@tonic-gate 		if ((tmp = allocb_cred_wait(n, (flag & STR_NOSIG), &error,
1464*7c478bd9Sstevel@tonic-gate 		    cr)) == NULL) {
1465*7c478bd9Sstevel@tonic-gate 			return (error);
1466*7c478bd9Sstevel@tonic-gate 		}
1467*7c478bd9Sstevel@tonic-gate 		error = strcopyin(arg, tmp->b_wptr, n, flag & (U_TO_K|K_TO_K));
1468*7c478bd9Sstevel@tonic-gate 		if (error != 0) {
1469*7c478bd9Sstevel@tonic-gate 			freeb(tmp);
1470*7c478bd9Sstevel@tonic-gate 			return (error);
1471*7c478bd9Sstevel@tonic-gate 		}
1472*7c478bd9Sstevel@tonic-gate 		arg += n;
1473*7c478bd9Sstevel@tonic-gate 		DB_CPID(tmp) = curproc->p_pid;
1474*7c478bd9Sstevel@tonic-gate 		tmp->b_wptr += n;
1475*7c478bd9Sstevel@tonic-gate 		count -= n;
1476*7c478bd9Sstevel@tonic-gate 		bp = (bp->b_cont = tmp);
1477*7c478bd9Sstevel@tonic-gate 	}
1478*7c478bd9Sstevel@tonic-gate 
1479*7c478bd9Sstevel@tonic-gate 	return (0);
1480*7c478bd9Sstevel@tonic-gate }
1481*7c478bd9Sstevel@tonic-gate 
1482*7c478bd9Sstevel@tonic-gate /*
1483*7c478bd9Sstevel@tonic-gate  * Copy ioctl data to user-land. Return non-zero errno on failure,
1484*7c478bd9Sstevel@tonic-gate  * 0 for success.
1485*7c478bd9Sstevel@tonic-gate  */
1486*7c478bd9Sstevel@tonic-gate int
1487*7c478bd9Sstevel@tonic-gate getiocd(mblk_t *bp, char *arg, int copymode)
1488*7c478bd9Sstevel@tonic-gate {
1489*7c478bd9Sstevel@tonic-gate 	ssize_t count;
1490*7c478bd9Sstevel@tonic-gate 	size_t  n;
1491*7c478bd9Sstevel@tonic-gate 	int	error;
1492*7c478bd9Sstevel@tonic-gate 
1493*7c478bd9Sstevel@tonic-gate 	if (bp->b_datap->db_type == M_IOCACK)
1494*7c478bd9Sstevel@tonic-gate 		count = ((struct iocblk *)bp->b_rptr)->ioc_count;
1495*7c478bd9Sstevel@tonic-gate 	else {
1496*7c478bd9Sstevel@tonic-gate 		ASSERT(bp->b_datap->db_type == M_COPYOUT);
1497*7c478bd9Sstevel@tonic-gate 		count = ((struct copyreq *)bp->b_rptr)->cq_size;
1498*7c478bd9Sstevel@tonic-gate 	}
1499*7c478bd9Sstevel@tonic-gate 	ASSERT(count >= 0);
1500*7c478bd9Sstevel@tonic-gate 
1501*7c478bd9Sstevel@tonic-gate 	for (bp = bp->b_cont; bp && count;
1502*7c478bd9Sstevel@tonic-gate 	    count -= n, bp = bp->b_cont, arg += n) {
1503*7c478bd9Sstevel@tonic-gate 		n = MIN(count, bp->b_wptr - bp->b_rptr);
1504*7c478bd9Sstevel@tonic-gate 		error = strcopyout(bp->b_rptr, arg, n, copymode);
1505*7c478bd9Sstevel@tonic-gate 		if (error)
1506*7c478bd9Sstevel@tonic-gate 			return (error);
1507*7c478bd9Sstevel@tonic-gate 	}
1508*7c478bd9Sstevel@tonic-gate 	ASSERT(count == 0);
1509*7c478bd9Sstevel@tonic-gate 	return (0);
1510*7c478bd9Sstevel@tonic-gate }
1511*7c478bd9Sstevel@tonic-gate 
1512*7c478bd9Sstevel@tonic-gate /*
1513*7c478bd9Sstevel@tonic-gate  * Allocate a linkinfo entry given the write queue of the
1514*7c478bd9Sstevel@tonic-gate  * bottom module of the top stream and the write queue of the
1515*7c478bd9Sstevel@tonic-gate  * stream head of the bottom stream.
1516*7c478bd9Sstevel@tonic-gate  */
1517*7c478bd9Sstevel@tonic-gate linkinfo_t *
1518*7c478bd9Sstevel@tonic-gate alloclink(queue_t *qup, queue_t *qdown, file_t *fpdown)
1519*7c478bd9Sstevel@tonic-gate {
1520*7c478bd9Sstevel@tonic-gate 	linkinfo_t *linkp;
1521*7c478bd9Sstevel@tonic-gate 
1522*7c478bd9Sstevel@tonic-gate 	linkp = kmem_cache_alloc(linkinfo_cache, KM_SLEEP);
1523*7c478bd9Sstevel@tonic-gate 
1524*7c478bd9Sstevel@tonic-gate 	linkp->li_lblk.l_qtop = qup;
1525*7c478bd9Sstevel@tonic-gate 	linkp->li_lblk.l_qbot = qdown;
1526*7c478bd9Sstevel@tonic-gate 	linkp->li_fpdown = fpdown;
1527*7c478bd9Sstevel@tonic-gate 
1528*7c478bd9Sstevel@tonic-gate 	mutex_enter(&strresources);
1529*7c478bd9Sstevel@tonic-gate 	linkp->li_next = linkinfo_list;
1530*7c478bd9Sstevel@tonic-gate 	linkp->li_prev = NULL;
1531*7c478bd9Sstevel@tonic-gate 	if (linkp->li_next)
1532*7c478bd9Sstevel@tonic-gate 		linkp->li_next->li_prev = linkp;
1533*7c478bd9Sstevel@tonic-gate 	linkinfo_list = linkp;
1534*7c478bd9Sstevel@tonic-gate 	linkp->li_lblk.l_index = ++lnk_id;
1535*7c478bd9Sstevel@tonic-gate 	ASSERT(lnk_id != 0);	/* this should never wrap in practice */
1536*7c478bd9Sstevel@tonic-gate 	mutex_exit(&strresources);
1537*7c478bd9Sstevel@tonic-gate 
1538*7c478bd9Sstevel@tonic-gate 	return (linkp);
1539*7c478bd9Sstevel@tonic-gate }
1540*7c478bd9Sstevel@tonic-gate 
1541*7c478bd9Sstevel@tonic-gate /*
1542*7c478bd9Sstevel@tonic-gate  * Free a linkinfo entry.
1543*7c478bd9Sstevel@tonic-gate  */
1544*7c478bd9Sstevel@tonic-gate void
1545*7c478bd9Sstevel@tonic-gate lbfree(linkinfo_t *linkp)
1546*7c478bd9Sstevel@tonic-gate {
1547*7c478bd9Sstevel@tonic-gate 	mutex_enter(&strresources);
1548*7c478bd9Sstevel@tonic-gate 	if (linkp->li_next)
1549*7c478bd9Sstevel@tonic-gate 		linkp->li_next->li_prev = linkp->li_prev;
1550*7c478bd9Sstevel@tonic-gate 	if (linkp->li_prev)
1551*7c478bd9Sstevel@tonic-gate 		linkp->li_prev->li_next = linkp->li_next;
1552*7c478bd9Sstevel@tonic-gate 	else
1553*7c478bd9Sstevel@tonic-gate 		linkinfo_list = linkp->li_next;
1554*7c478bd9Sstevel@tonic-gate 	mutex_exit(&strresources);
1555*7c478bd9Sstevel@tonic-gate 
1556*7c478bd9Sstevel@tonic-gate 	kmem_cache_free(linkinfo_cache, linkp);
1557*7c478bd9Sstevel@tonic-gate }
1558*7c478bd9Sstevel@tonic-gate 
1559*7c478bd9Sstevel@tonic-gate /*
1560*7c478bd9Sstevel@tonic-gate  * Check for a potential linking cycle.
1561*7c478bd9Sstevel@tonic-gate  * Return 1 if a link will result in a cycle,
1562*7c478bd9Sstevel@tonic-gate  * and 0 otherwise.
1563*7c478bd9Sstevel@tonic-gate  */
1564*7c478bd9Sstevel@tonic-gate int
1565*7c478bd9Sstevel@tonic-gate linkcycle(stdata_t *upstp, stdata_t *lostp)
1566*7c478bd9Sstevel@tonic-gate {
1567*7c478bd9Sstevel@tonic-gate 	struct mux_node *np;
1568*7c478bd9Sstevel@tonic-gate 	struct mux_edge *ep;
1569*7c478bd9Sstevel@tonic-gate 	int i;
1570*7c478bd9Sstevel@tonic-gate 	major_t lomaj;
1571*7c478bd9Sstevel@tonic-gate 	major_t upmaj;
1572*7c478bd9Sstevel@tonic-gate 	/*
1573*7c478bd9Sstevel@tonic-gate 	 * if the lower stream is a pipe/FIFO, return, since link
1574*7c478bd9Sstevel@tonic-gate 	 * cycles can not happen on pipes/FIFOs
1575*7c478bd9Sstevel@tonic-gate 	 */
1576*7c478bd9Sstevel@tonic-gate 	if (lostp->sd_vnode->v_type == VFIFO)
1577*7c478bd9Sstevel@tonic-gate 		return (0);
1578*7c478bd9Sstevel@tonic-gate 
1579*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < devcnt; i++) {
1580*7c478bd9Sstevel@tonic-gate 		np = &mux_nodes[i];
1581*7c478bd9Sstevel@tonic-gate 		MUX_CLEAR(np);
1582*7c478bd9Sstevel@tonic-gate 	}
1583*7c478bd9Sstevel@tonic-gate 	lomaj = getmajor(lostp->sd_vnode->v_rdev);
1584*7c478bd9Sstevel@tonic-gate 	upmaj = getmajor(upstp->sd_vnode->v_rdev);
1585*7c478bd9Sstevel@tonic-gate 	np = &mux_nodes[lomaj];
1586*7c478bd9Sstevel@tonic-gate 	for (;;) {
1587*7c478bd9Sstevel@tonic-gate 		if (!MUX_DIDVISIT(np)) {
1588*7c478bd9Sstevel@tonic-gate 			if (np->mn_imaj == upmaj)
1589*7c478bd9Sstevel@tonic-gate 				return (1);
1590*7c478bd9Sstevel@tonic-gate 			if (np->mn_outp == NULL) {
1591*7c478bd9Sstevel@tonic-gate 				MUX_VISIT(np);
1592*7c478bd9Sstevel@tonic-gate 				if (np->mn_originp == NULL)
1593*7c478bd9Sstevel@tonic-gate 					return (0);
1594*7c478bd9Sstevel@tonic-gate 				np = np->mn_originp;
1595*7c478bd9Sstevel@tonic-gate 				continue;
1596*7c478bd9Sstevel@tonic-gate 			}
1597*7c478bd9Sstevel@tonic-gate 			MUX_VISIT(np);
1598*7c478bd9Sstevel@tonic-gate 			np->mn_startp = np->mn_outp;
1599*7c478bd9Sstevel@tonic-gate 		} else {
1600*7c478bd9Sstevel@tonic-gate 			if (np->mn_startp == NULL) {
1601*7c478bd9Sstevel@tonic-gate 				if (np->mn_originp == NULL)
1602*7c478bd9Sstevel@tonic-gate 					return (0);
1603*7c478bd9Sstevel@tonic-gate 				else {
1604*7c478bd9Sstevel@tonic-gate 					np = np->mn_originp;
1605*7c478bd9Sstevel@tonic-gate 					continue;
1606*7c478bd9Sstevel@tonic-gate 				}
1607*7c478bd9Sstevel@tonic-gate 			}
1608*7c478bd9Sstevel@tonic-gate 			/*
1609*7c478bd9Sstevel@tonic-gate 			 * If ep->me_nodep is a FIFO (me_nodep == NULL),
1610*7c478bd9Sstevel@tonic-gate 			 * ignore the edge and move on. ep->me_nodep gets
1611*7c478bd9Sstevel@tonic-gate 			 * set to NULL in mux_addedge() if it is a FIFO.
1612*7c478bd9Sstevel@tonic-gate 			 *
1613*7c478bd9Sstevel@tonic-gate 			 */
1614*7c478bd9Sstevel@tonic-gate 			ep = np->mn_startp;
1615*7c478bd9Sstevel@tonic-gate 			np->mn_startp = ep->me_nextp;
1616*7c478bd9Sstevel@tonic-gate 			if (ep->me_nodep == NULL)
1617*7c478bd9Sstevel@tonic-gate 				continue;
1618*7c478bd9Sstevel@tonic-gate 			ep->me_nodep->mn_originp = np;
1619*7c478bd9Sstevel@tonic-gate 			np = ep->me_nodep;
1620*7c478bd9Sstevel@tonic-gate 		}
1621*7c478bd9Sstevel@tonic-gate 	}
1622*7c478bd9Sstevel@tonic-gate }
1623*7c478bd9Sstevel@tonic-gate 
1624*7c478bd9Sstevel@tonic-gate /*
1625*7c478bd9Sstevel@tonic-gate  * Find linkinfo entry corresponding to the parameters.
1626*7c478bd9Sstevel@tonic-gate  */
1627*7c478bd9Sstevel@tonic-gate linkinfo_t *
1628*7c478bd9Sstevel@tonic-gate findlinks(stdata_t *stp, int index, int type)
1629*7c478bd9Sstevel@tonic-gate {
1630*7c478bd9Sstevel@tonic-gate 	linkinfo_t *linkp;
1631*7c478bd9Sstevel@tonic-gate 	struct mux_edge *mep;
1632*7c478bd9Sstevel@tonic-gate 	struct mux_node *mnp;
1633*7c478bd9Sstevel@tonic-gate 	queue_t *qup;
1634*7c478bd9Sstevel@tonic-gate 
1635*7c478bd9Sstevel@tonic-gate 	mutex_enter(&strresources);
1636*7c478bd9Sstevel@tonic-gate 	if ((type & LINKTYPEMASK) == LINKNORMAL) {
1637*7c478bd9Sstevel@tonic-gate 		qup = getendq(stp->sd_wrq);
1638*7c478bd9Sstevel@tonic-gate 		for (linkp = linkinfo_list; linkp; linkp = linkp->li_next) {
1639*7c478bd9Sstevel@tonic-gate 			if ((qup == linkp->li_lblk.l_qtop) &&
1640*7c478bd9Sstevel@tonic-gate 			    (!index || (index == linkp->li_lblk.l_index))) {
1641*7c478bd9Sstevel@tonic-gate 				mutex_exit(&strresources);
1642*7c478bd9Sstevel@tonic-gate 				return (linkp);
1643*7c478bd9Sstevel@tonic-gate 			}
1644*7c478bd9Sstevel@tonic-gate 		}
1645*7c478bd9Sstevel@tonic-gate 	} else {
1646*7c478bd9Sstevel@tonic-gate 		ASSERT((type & LINKTYPEMASK) == LINKPERSIST);
1647*7c478bd9Sstevel@tonic-gate 		mnp = &mux_nodes[getmajor(stp->sd_vnode->v_rdev)];
1648*7c478bd9Sstevel@tonic-gate 		mep = mnp->mn_outp;
1649*7c478bd9Sstevel@tonic-gate 		while (mep) {
1650*7c478bd9Sstevel@tonic-gate 			if ((index == 0) || (index == mep->me_muxid))
1651*7c478bd9Sstevel@tonic-gate 				break;
1652*7c478bd9Sstevel@tonic-gate 			mep = mep->me_nextp;
1653*7c478bd9Sstevel@tonic-gate 		}
1654*7c478bd9Sstevel@tonic-gate 		if (!mep) {
1655*7c478bd9Sstevel@tonic-gate 			mutex_exit(&strresources);
1656*7c478bd9Sstevel@tonic-gate 			return (NULL);
1657*7c478bd9Sstevel@tonic-gate 		}
1658*7c478bd9Sstevel@tonic-gate 		for (linkp = linkinfo_list; linkp; linkp = linkp->li_next) {
1659*7c478bd9Sstevel@tonic-gate 			if ((!linkp->li_lblk.l_qtop) &&
1660*7c478bd9Sstevel@tonic-gate 			    (mep->me_muxid == linkp->li_lblk.l_index)) {
1661*7c478bd9Sstevel@tonic-gate 				mutex_exit(&strresources);
1662*7c478bd9Sstevel@tonic-gate 				return (linkp);
1663*7c478bd9Sstevel@tonic-gate 			}
1664*7c478bd9Sstevel@tonic-gate 		}
1665*7c478bd9Sstevel@tonic-gate 	}
1666*7c478bd9Sstevel@tonic-gate 	mutex_exit(&strresources);
1667*7c478bd9Sstevel@tonic-gate 	return (NULL);
1668*7c478bd9Sstevel@tonic-gate }
1669*7c478bd9Sstevel@tonic-gate 
1670*7c478bd9Sstevel@tonic-gate /*
1671*7c478bd9Sstevel@tonic-gate  * Given a queue ptr, follow the chain of q_next pointers until you reach the
1672*7c478bd9Sstevel@tonic-gate  * last queue on the chain and return it.
1673*7c478bd9Sstevel@tonic-gate  */
1674*7c478bd9Sstevel@tonic-gate queue_t *
1675*7c478bd9Sstevel@tonic-gate getendq(queue_t *q)
1676*7c478bd9Sstevel@tonic-gate {
1677*7c478bd9Sstevel@tonic-gate 	ASSERT(q != NULL);
1678*7c478bd9Sstevel@tonic-gate 	while (_SAMESTR(q))
1679*7c478bd9Sstevel@tonic-gate 		q = q->q_next;
1680*7c478bd9Sstevel@tonic-gate 	return (q);
1681*7c478bd9Sstevel@tonic-gate }
1682*7c478bd9Sstevel@tonic-gate 
1683*7c478bd9Sstevel@tonic-gate /*
1684*7c478bd9Sstevel@tonic-gate  * wait for the syncq count to drop to zero.
1685*7c478bd9Sstevel@tonic-gate  * sq could be either outer or inner.
1686*7c478bd9Sstevel@tonic-gate  */
1687*7c478bd9Sstevel@tonic-gate 
1688*7c478bd9Sstevel@tonic-gate static void
1689*7c478bd9Sstevel@tonic-gate wait_syncq(syncq_t *sq)
1690*7c478bd9Sstevel@tonic-gate {
1691*7c478bd9Sstevel@tonic-gate 	uint16_t count;
1692*7c478bd9Sstevel@tonic-gate 
1693*7c478bd9Sstevel@tonic-gate 	mutex_enter(SQLOCK(sq));
1694*7c478bd9Sstevel@tonic-gate 	count = sq->sq_count;
1695*7c478bd9Sstevel@tonic-gate 	SQ_PUTLOCKS_ENTER(sq);
1696*7c478bd9Sstevel@tonic-gate 	SUM_SQ_PUTCOUNTS(sq, count);
1697*7c478bd9Sstevel@tonic-gate 	while (count != 0) {
1698*7c478bd9Sstevel@tonic-gate 		sq->sq_flags |= SQ_WANTWAKEUP;
1699*7c478bd9Sstevel@tonic-gate 		SQ_PUTLOCKS_EXIT(sq);
1700*7c478bd9Sstevel@tonic-gate 		cv_wait(&sq->sq_wait, SQLOCK(sq));
1701*7c478bd9Sstevel@tonic-gate 		count = sq->sq_count;
1702*7c478bd9Sstevel@tonic-gate 		SQ_PUTLOCKS_ENTER(sq);
1703*7c478bd9Sstevel@tonic-gate 		SUM_SQ_PUTCOUNTS(sq, count);
1704*7c478bd9Sstevel@tonic-gate 	}
1705*7c478bd9Sstevel@tonic-gate 	SQ_PUTLOCKS_EXIT(sq);
1706*7c478bd9Sstevel@tonic-gate 	mutex_exit(SQLOCK(sq));
1707*7c478bd9Sstevel@tonic-gate }
1708*7c478bd9Sstevel@tonic-gate 
1709*7c478bd9Sstevel@tonic-gate /*
1710*7c478bd9Sstevel@tonic-gate  * Wait while there are any messages for the queue in its syncq.
1711*7c478bd9Sstevel@tonic-gate  */
1712*7c478bd9Sstevel@tonic-gate static void
1713*7c478bd9Sstevel@tonic-gate wait_q_syncq(queue_t *q)
1714*7c478bd9Sstevel@tonic-gate {
1715*7c478bd9Sstevel@tonic-gate 	if ((q->q_sqflags & Q_SQQUEUED) || (q->q_syncqmsgs > 0)) {
1716*7c478bd9Sstevel@tonic-gate 		syncq_t *sq = q->q_syncq;
1717*7c478bd9Sstevel@tonic-gate 
1718*7c478bd9Sstevel@tonic-gate 		mutex_enter(SQLOCK(sq));
1719*7c478bd9Sstevel@tonic-gate 		while ((q->q_sqflags & Q_SQQUEUED) || (q->q_syncqmsgs > 0)) {
1720*7c478bd9Sstevel@tonic-gate 			sq->sq_flags |= SQ_WANTWAKEUP;
1721*7c478bd9Sstevel@tonic-gate 			cv_wait(&sq->sq_wait, SQLOCK(sq));
1722*7c478bd9Sstevel@tonic-gate 		}
1723*7c478bd9Sstevel@tonic-gate 		mutex_exit(SQLOCK(sq));
1724*7c478bd9Sstevel@tonic-gate 	}
1725*7c478bd9Sstevel@tonic-gate }
1726*7c478bd9Sstevel@tonic-gate 
1727*7c478bd9Sstevel@tonic-gate 
1728*7c478bd9Sstevel@tonic-gate int
1729*7c478bd9Sstevel@tonic-gate mlink_file(vnode_t *vp, int cmd, struct file *fpdown, cred_t *crp, int *rvalp,
1730*7c478bd9Sstevel@tonic-gate     int lhlink)
1731*7c478bd9Sstevel@tonic-gate {
1732*7c478bd9Sstevel@tonic-gate 	struct stdata *stp;
1733*7c478bd9Sstevel@tonic-gate 	struct strioctl strioc;
1734*7c478bd9Sstevel@tonic-gate 	struct linkinfo *linkp;
1735*7c478bd9Sstevel@tonic-gate 	struct stdata *stpdown;
1736*7c478bd9Sstevel@tonic-gate 	struct streamtab *str;
1737*7c478bd9Sstevel@tonic-gate 	queue_t *passq;
1738*7c478bd9Sstevel@tonic-gate 	syncq_t *passyncq;
1739*7c478bd9Sstevel@tonic-gate 	queue_t *rq;
1740*7c478bd9Sstevel@tonic-gate 	cdevsw_impl_t *dp;
1741*7c478bd9Sstevel@tonic-gate 	uint32_t qflag;
1742*7c478bd9Sstevel@tonic-gate 	uint32_t sqtype;
1743*7c478bd9Sstevel@tonic-gate 	perdm_t *dmp;
1744*7c478bd9Sstevel@tonic-gate 	int error = 0;
1745*7c478bd9Sstevel@tonic-gate 
1746*7c478bd9Sstevel@tonic-gate 	stp = vp->v_stream;
1747*7c478bd9Sstevel@tonic-gate 	TRACE_1(TR_FAC_STREAMS_FR,
1748*7c478bd9Sstevel@tonic-gate 		TR_I_LINK, "I_LINK/I_PLINK:stp %p", stp);
1749*7c478bd9Sstevel@tonic-gate 	/*
1750*7c478bd9Sstevel@tonic-gate 	 * Test for invalid upper stream
1751*7c478bd9Sstevel@tonic-gate 	 */
1752*7c478bd9Sstevel@tonic-gate 	if (stp->sd_flag & STRHUP) {
1753*7c478bd9Sstevel@tonic-gate 		return (ENXIO);
1754*7c478bd9Sstevel@tonic-gate 	}
1755*7c478bd9Sstevel@tonic-gate 	if (vp->v_type == VFIFO) {
1756*7c478bd9Sstevel@tonic-gate 		return (EINVAL);
1757*7c478bd9Sstevel@tonic-gate 	}
1758*7c478bd9Sstevel@tonic-gate 	if (stp->sd_strtab == NULL) {
1759*7c478bd9Sstevel@tonic-gate 		return (EINVAL);
1760*7c478bd9Sstevel@tonic-gate 	}
1761*7c478bd9Sstevel@tonic-gate 	if (!stp->sd_strtab->st_muxwinit) {
1762*7c478bd9Sstevel@tonic-gate 		return (EINVAL);
1763*7c478bd9Sstevel@tonic-gate 	}
1764*7c478bd9Sstevel@tonic-gate 	if (fpdown == NULL) {
1765*7c478bd9Sstevel@tonic-gate 		return (EBADF);
1766*7c478bd9Sstevel@tonic-gate 	}
1767*7c478bd9Sstevel@tonic-gate 	if (getmajor(stp->sd_vnode->v_rdev) >= devcnt) {
1768*7c478bd9Sstevel@tonic-gate 		return (EINVAL);
1769*7c478bd9Sstevel@tonic-gate 	}
1770*7c478bd9Sstevel@tonic-gate 	mutex_enter(&muxifier);
1771*7c478bd9Sstevel@tonic-gate 	if (stp->sd_flag & STPLEX) {
1772*7c478bd9Sstevel@tonic-gate 		mutex_exit(&muxifier);
1773*7c478bd9Sstevel@tonic-gate 		return (ENXIO);
1774*7c478bd9Sstevel@tonic-gate 	}
1775*7c478bd9Sstevel@tonic-gate 
1776*7c478bd9Sstevel@tonic-gate 	/*
1777*7c478bd9Sstevel@tonic-gate 	 * Test for invalid lower stream.
1778*7c478bd9Sstevel@tonic-gate 	 * The check for the v_type != VFIFO and having a major
1779*7c478bd9Sstevel@tonic-gate 	 * number not >= devcnt is done to avoid problems with
1780*7c478bd9Sstevel@tonic-gate 	 * adding mux_node entry past the end of mux_nodes[].
1781*7c478bd9Sstevel@tonic-gate 	 * For FIFO's we don't add an entry so this isn't a
1782*7c478bd9Sstevel@tonic-gate 	 * problem.
1783*7c478bd9Sstevel@tonic-gate 	 */
1784*7c478bd9Sstevel@tonic-gate 	if (((stpdown = fpdown->f_vnode->v_stream) == NULL) ||
1785*7c478bd9Sstevel@tonic-gate 	    (stpdown == stp) || (stpdown->sd_flag &
1786*7c478bd9Sstevel@tonic-gate 	    (STPLEX|STRHUP|STRDERR|STWRERR|IOCWAIT|STRPLUMB)) ||
1787*7c478bd9Sstevel@tonic-gate 	    ((stpdown->sd_vnode->v_type != VFIFO) &&
1788*7c478bd9Sstevel@tonic-gate 	    (getmajor(stpdown->sd_vnode->v_rdev) >= devcnt)) ||
1789*7c478bd9Sstevel@tonic-gate 	    linkcycle(stp, stpdown)) {
1790*7c478bd9Sstevel@tonic-gate 		mutex_exit(&muxifier);
1791*7c478bd9Sstevel@tonic-gate 		return (EINVAL);
1792*7c478bd9Sstevel@tonic-gate 	}
1793*7c478bd9Sstevel@tonic-gate 	TRACE_1(TR_FAC_STREAMS_FR,
1794*7c478bd9Sstevel@tonic-gate 		TR_STPDOWN, "stpdown:%p", stpdown);
1795*7c478bd9Sstevel@tonic-gate 	rq = getendq(stp->sd_wrq);
1796*7c478bd9Sstevel@tonic-gate 	if (cmd == I_PLINK)
1797*7c478bd9Sstevel@tonic-gate 		rq = NULL;
1798*7c478bd9Sstevel@tonic-gate 
1799*7c478bd9Sstevel@tonic-gate 	linkp = alloclink(rq, stpdown->sd_wrq, fpdown);
1800*7c478bd9Sstevel@tonic-gate 
1801*7c478bd9Sstevel@tonic-gate 	strioc.ic_cmd = cmd;
1802*7c478bd9Sstevel@tonic-gate 	strioc.ic_timout = INFTIM;
1803*7c478bd9Sstevel@tonic-gate 	strioc.ic_len = sizeof (struct linkblk);
1804*7c478bd9Sstevel@tonic-gate 	strioc.ic_dp = (char *)&linkp->li_lblk;
1805*7c478bd9Sstevel@tonic-gate 
1806*7c478bd9Sstevel@tonic-gate 	/*
1807*7c478bd9Sstevel@tonic-gate 	 * STRPLUMB protects plumbing changes and should be set before
1808*7c478bd9Sstevel@tonic-gate 	 * link_addpassthru()/link_rempassthru() are called, so it is set here
1809*7c478bd9Sstevel@tonic-gate 	 * and cleared in the end of mlink when passthru queue is removed.
1810*7c478bd9Sstevel@tonic-gate 	 * Setting of STRPLUMB prevents reopens of the stream while passthru
1811*7c478bd9Sstevel@tonic-gate 	 * queue is in-place (it is not a proper module and doesn't have open
1812*7c478bd9Sstevel@tonic-gate 	 * entry point).
1813*7c478bd9Sstevel@tonic-gate 	 *
1814*7c478bd9Sstevel@tonic-gate 	 * STPLEX prevents any threads from entering the stream from above. It
1815*7c478bd9Sstevel@tonic-gate 	 * can't be set before the call to link_addpassthru() because putnext
1816*7c478bd9Sstevel@tonic-gate 	 * from below may cause stream head I/O routines to be called and these
1817*7c478bd9Sstevel@tonic-gate 	 * routines assert that STPLEX is not set. After link_addpassthru()
1818*7c478bd9Sstevel@tonic-gate 	 * nothing may come from below since the pass queue syncq is blocked.
1819*7c478bd9Sstevel@tonic-gate 	 * Note also that STPLEX should be cleared before the call to
1820*7c478bd9Sstevel@tonic-gate 	 * link_remmpassthru() since when messages start flowing to the stream
1821*7c478bd9Sstevel@tonic-gate 	 * head (e.g. because of message propagation from the pass queue) stream
1822*7c478bd9Sstevel@tonic-gate 	 * head I/O routines may be called with STPLEX flag set.
1823*7c478bd9Sstevel@tonic-gate 	 *
1824*7c478bd9Sstevel@tonic-gate 	 * When STPLEX is set, nothing may come into the stream from above and
1825*7c478bd9Sstevel@tonic-gate 	 * it is safe to do a setq which will change stream head. So, the
1826*7c478bd9Sstevel@tonic-gate 	 * correct sequence of actions is:
1827*7c478bd9Sstevel@tonic-gate 	 *
1828*7c478bd9Sstevel@tonic-gate 	 * 1) Set STRPLUMB
1829*7c478bd9Sstevel@tonic-gate 	 * 2) Call link_addpassthru()
1830*7c478bd9Sstevel@tonic-gate 	 * 3) Set STPLEX
1831*7c478bd9Sstevel@tonic-gate 	 * 4) Call setq and update the stream state
1832*7c478bd9Sstevel@tonic-gate 	 * 5) Clear STPLEX
1833*7c478bd9Sstevel@tonic-gate 	 * 6) Call link_rempassthru()
1834*7c478bd9Sstevel@tonic-gate 	 * 7) Clear STRPLUMB
1835*7c478bd9Sstevel@tonic-gate 	 *
1836*7c478bd9Sstevel@tonic-gate 	 * The same sequence applies to munlink() code.
1837*7c478bd9Sstevel@tonic-gate 	 */
1838*7c478bd9Sstevel@tonic-gate 	mutex_enter(&stpdown->sd_lock);
1839*7c478bd9Sstevel@tonic-gate 	stpdown->sd_flag |= STRPLUMB;
1840*7c478bd9Sstevel@tonic-gate 	mutex_exit(&stpdown->sd_lock);
1841*7c478bd9Sstevel@tonic-gate 	/*
1842*7c478bd9Sstevel@tonic-gate 	 * Add passthru queue below lower mux. This will block
1843*7c478bd9Sstevel@tonic-gate 	 * syncqs of lower muxs read queue during I_LINK/I_UNLINK.
1844*7c478bd9Sstevel@tonic-gate 	 */
1845*7c478bd9Sstevel@tonic-gate 	passq = link_addpassthru(stpdown);
1846*7c478bd9Sstevel@tonic-gate 
1847*7c478bd9Sstevel@tonic-gate 	mutex_enter(&stpdown->sd_lock);
1848*7c478bd9Sstevel@tonic-gate 	stpdown->sd_flag |= STPLEX;
1849*7c478bd9Sstevel@tonic-gate 	mutex_exit(&stpdown->sd_lock);
1850*7c478bd9Sstevel@tonic-gate 
1851*7c478bd9Sstevel@tonic-gate 	rq = _RD(stpdown->sd_wrq);
1852*7c478bd9Sstevel@tonic-gate 	/*
1853*7c478bd9Sstevel@tonic-gate 	 * There may be messages in the streamhead's syncq due to messages
1854*7c478bd9Sstevel@tonic-gate 	 * that arrived before link_addpassthru() was done. To avoid
1855*7c478bd9Sstevel@tonic-gate 	 * background processing of the syncq happening simultaneous with
1856*7c478bd9Sstevel@tonic-gate 	 * setq processing, we disable the streamhead syncq and wait until
1857*7c478bd9Sstevel@tonic-gate 	 * existing background thread finishes working on it.
1858*7c478bd9Sstevel@tonic-gate 	 */
1859*7c478bd9Sstevel@tonic-gate 	wait_sq_svc(rq->q_syncq);
1860*7c478bd9Sstevel@tonic-gate 	passyncq = passq->q_syncq;
1861*7c478bd9Sstevel@tonic-gate 	if (!(passyncq->sq_flags & SQ_BLOCKED))
1862*7c478bd9Sstevel@tonic-gate 		blocksq(passyncq, SQ_BLOCKED, 0);
1863*7c478bd9Sstevel@tonic-gate 
1864*7c478bd9Sstevel@tonic-gate 	ASSERT((rq->q_flag & QMT_TYPEMASK) == QMTSAFE);
1865*7c478bd9Sstevel@tonic-gate 	ASSERT(rq->q_syncq == SQ(rq) && _WR(rq)->q_syncq == SQ(rq));
1866*7c478bd9Sstevel@tonic-gate 	rq->q_ptr = _WR(rq)->q_ptr = NULL;
1867*7c478bd9Sstevel@tonic-gate 
1868*7c478bd9Sstevel@tonic-gate 	/* setq might sleep in allocator - avoid holding locks. */
1869*7c478bd9Sstevel@tonic-gate 	/* Note: we are holding muxifier here. */
1870*7c478bd9Sstevel@tonic-gate 
1871*7c478bd9Sstevel@tonic-gate 	str = stp->sd_strtab;
1872*7c478bd9Sstevel@tonic-gate 	dp = &devimpl[getmajor(vp->v_rdev)];
1873*7c478bd9Sstevel@tonic-gate 	ASSERT(dp->d_str == str);
1874*7c478bd9Sstevel@tonic-gate 
1875*7c478bd9Sstevel@tonic-gate 	qflag = dp->d_qflag;
1876*7c478bd9Sstevel@tonic-gate 	sqtype = dp->d_sqtype;
1877*7c478bd9Sstevel@tonic-gate 
1878*7c478bd9Sstevel@tonic-gate 	/* create perdm_t if needed */
1879*7c478bd9Sstevel@tonic-gate 	if (NEED_DM(dp->d_dmp, qflag))
1880*7c478bd9Sstevel@tonic-gate 		dp->d_dmp = hold_dm(str, qflag, sqtype);
1881*7c478bd9Sstevel@tonic-gate 
1882*7c478bd9Sstevel@tonic-gate 	dmp = dp->d_dmp;
1883*7c478bd9Sstevel@tonic-gate 
1884*7c478bd9Sstevel@tonic-gate 	setq(rq, str->st_muxrinit, str->st_muxwinit, dmp, qflag, sqtype,
1885*7c478bd9Sstevel@tonic-gate 	    B_TRUE);
1886*7c478bd9Sstevel@tonic-gate 
1887*7c478bd9Sstevel@tonic-gate 	/*
1888*7c478bd9Sstevel@tonic-gate 	 * XXX Remove any "odd" messages from the queue.
1889*7c478bd9Sstevel@tonic-gate 	 * Keep only M_DATA, M_PROTO, M_PCPROTO.
1890*7c478bd9Sstevel@tonic-gate 	 */
1891*7c478bd9Sstevel@tonic-gate 	error = strdoioctl(stp, &strioc, FNATIVE,
1892*7c478bd9Sstevel@tonic-gate 	    K_TO_K | STR_NOERROR | STR_NOSIG, crp, rvalp);
1893*7c478bd9Sstevel@tonic-gate 	if (error != 0) {
1894*7c478bd9Sstevel@tonic-gate 		lbfree(linkp);
1895*7c478bd9Sstevel@tonic-gate 
1896*7c478bd9Sstevel@tonic-gate 		if (!(passyncq->sq_flags & SQ_BLOCKED))
1897*7c478bd9Sstevel@tonic-gate 			blocksq(passyncq, SQ_BLOCKED, 0);
1898*7c478bd9Sstevel@tonic-gate 		/*
1899*7c478bd9Sstevel@tonic-gate 		 * Restore the stream head queue and then remove
1900*7c478bd9Sstevel@tonic-gate 		 * the passq. Turn off STPLEX before we turn on
1901*7c478bd9Sstevel@tonic-gate 		 * the stream by removing the passq.
1902*7c478bd9Sstevel@tonic-gate 		 */
1903*7c478bd9Sstevel@tonic-gate 		rq->q_ptr = _WR(rq)->q_ptr = stpdown;
1904*7c478bd9Sstevel@tonic-gate 		setq(rq, &strdata, &stwdata, NULL, QMTSAFE, SQ_CI|SQ_CO,
1905*7c478bd9Sstevel@tonic-gate 		    B_TRUE);
1906*7c478bd9Sstevel@tonic-gate 
1907*7c478bd9Sstevel@tonic-gate 		mutex_enter(&stpdown->sd_lock);
1908*7c478bd9Sstevel@tonic-gate 		stpdown->sd_flag &= ~STPLEX;
1909*7c478bd9Sstevel@tonic-gate 		mutex_exit(&stpdown->sd_lock);
1910*7c478bd9Sstevel@tonic-gate 
1911*7c478bd9Sstevel@tonic-gate 		link_rempassthru(passq);
1912*7c478bd9Sstevel@tonic-gate 
1913*7c478bd9Sstevel@tonic-gate 		mutex_enter(&stpdown->sd_lock);
1914*7c478bd9Sstevel@tonic-gate 		stpdown->sd_flag &= ~STRPLUMB;
1915*7c478bd9Sstevel@tonic-gate 		/* Wakeup anyone waiting for STRPLUMB to clear. */
1916*7c478bd9Sstevel@tonic-gate 		cv_broadcast(&stpdown->sd_monitor);
1917*7c478bd9Sstevel@tonic-gate 		mutex_exit(&stpdown->sd_lock);
1918*7c478bd9Sstevel@tonic-gate 
1919*7c478bd9Sstevel@tonic-gate 		mutex_exit(&muxifier);
1920*7c478bd9Sstevel@tonic-gate 		return (error);
1921*7c478bd9Sstevel@tonic-gate 	}
1922*7c478bd9Sstevel@tonic-gate 	mutex_enter(&fpdown->f_tlock);
1923*7c478bd9Sstevel@tonic-gate 	fpdown->f_count++;
1924*7c478bd9Sstevel@tonic-gate 	mutex_exit(&fpdown->f_tlock);
1925*7c478bd9Sstevel@tonic-gate 
1926*7c478bd9Sstevel@tonic-gate 	/*
1927*7c478bd9Sstevel@tonic-gate 	 * if we've made it here the linkage is all set up so we should also
1928*7c478bd9Sstevel@tonic-gate 	 * set up the layered driver linkages
1929*7c478bd9Sstevel@tonic-gate 	 */
1930*7c478bd9Sstevel@tonic-gate 
1931*7c478bd9Sstevel@tonic-gate 	ASSERT((cmd == I_LINK) || (cmd == I_PLINK));
1932*7c478bd9Sstevel@tonic-gate 	if (cmd == I_LINK) {
1933*7c478bd9Sstevel@tonic-gate 		ldi_mlink_fp(stp, fpdown, lhlink, LINKNORMAL);
1934*7c478bd9Sstevel@tonic-gate 	} else {
1935*7c478bd9Sstevel@tonic-gate 		ldi_mlink_fp(stp, fpdown, lhlink, LINKPERSIST);
1936*7c478bd9Sstevel@tonic-gate 	}
1937*7c478bd9Sstevel@tonic-gate 
1938*7c478bd9Sstevel@tonic-gate 	link_rempassthru(passq);
1939*7c478bd9Sstevel@tonic-gate 
1940*7c478bd9Sstevel@tonic-gate 	mux_addedge(stp, stpdown, linkp->li_lblk.l_index);
1941*7c478bd9Sstevel@tonic-gate 
1942*7c478bd9Sstevel@tonic-gate 	/*
1943*7c478bd9Sstevel@tonic-gate 	 * Mark the upper stream as having dependent links
1944*7c478bd9Sstevel@tonic-gate 	 * so that strclose can clean it up.
1945*7c478bd9Sstevel@tonic-gate 	 */
1946*7c478bd9Sstevel@tonic-gate 	if (cmd == I_LINK) {
1947*7c478bd9Sstevel@tonic-gate 		mutex_enter(&stp->sd_lock);
1948*7c478bd9Sstevel@tonic-gate 		stp->sd_flag |= STRHASLINKS;
1949*7c478bd9Sstevel@tonic-gate 		mutex_exit(&stp->sd_lock);
1950*7c478bd9Sstevel@tonic-gate 	}
1951*7c478bd9Sstevel@tonic-gate 	/*
1952*7c478bd9Sstevel@tonic-gate 	 * Wake up any other processes that may have been
1953*7c478bd9Sstevel@tonic-gate 	 * waiting on the lower stream. These will all
1954*7c478bd9Sstevel@tonic-gate 	 * error out.
1955*7c478bd9Sstevel@tonic-gate 	 */
1956*7c478bd9Sstevel@tonic-gate 	mutex_enter(&stpdown->sd_lock);
1957*7c478bd9Sstevel@tonic-gate 	/* The passthru module is removed so we may release STRPLUMB */
1958*7c478bd9Sstevel@tonic-gate 	stpdown->sd_flag &= ~STRPLUMB;
1959*7c478bd9Sstevel@tonic-gate 	cv_broadcast(&rq->q_wait);
1960*7c478bd9Sstevel@tonic-gate 	cv_broadcast(&_WR(rq)->q_wait);
1961*7c478bd9Sstevel@tonic-gate 	cv_broadcast(&stpdown->sd_monitor);
1962*7c478bd9Sstevel@tonic-gate 	mutex_exit(&stpdown->sd_lock);
1963*7c478bd9Sstevel@tonic-gate 	mutex_exit(&muxifier);
1964*7c478bd9Sstevel@tonic-gate 	*rvalp = linkp->li_lblk.l_index;
1965*7c478bd9Sstevel@tonic-gate 	return (0);
1966*7c478bd9Sstevel@tonic-gate }
1967*7c478bd9Sstevel@tonic-gate 
1968*7c478bd9Sstevel@tonic-gate int
1969*7c478bd9Sstevel@tonic-gate mlink(vnode_t *vp, int cmd, int arg, cred_t *crp, int *rvalp, int lhlink)
1970*7c478bd9Sstevel@tonic-gate {
1971*7c478bd9Sstevel@tonic-gate 	int		ret;
1972*7c478bd9Sstevel@tonic-gate 	struct file	*fpdown;
1973*7c478bd9Sstevel@tonic-gate 
1974*7c478bd9Sstevel@tonic-gate 	fpdown = getf(arg);
1975*7c478bd9Sstevel@tonic-gate 	ret = mlink_file(vp, cmd, fpdown, crp, rvalp, lhlink);
1976*7c478bd9Sstevel@tonic-gate 	if (fpdown != NULL)
1977*7c478bd9Sstevel@tonic-gate 		releasef(arg);
1978*7c478bd9Sstevel@tonic-gate 	return (ret);
1979*7c478bd9Sstevel@tonic-gate }
1980*7c478bd9Sstevel@tonic-gate 
1981*7c478bd9Sstevel@tonic-gate /*
1982*7c478bd9Sstevel@tonic-gate  * Unlink a multiplexor link. Stp is the controlling stream for the
1983*7c478bd9Sstevel@tonic-gate  * link, and linkp points to the link's entry in the linkinfo list.
1984*7c478bd9Sstevel@tonic-gate  * The muxifier lock must be held on entry and is dropped on exit.
1985*7c478bd9Sstevel@tonic-gate  *
1986*7c478bd9Sstevel@tonic-gate  * NOTE : Currently it is assumed that mux would process all the messages
1987*7c478bd9Sstevel@tonic-gate  * sitting on it's queue before ACKing the UNLINK. It is the responsibility
1988*7c478bd9Sstevel@tonic-gate  * of the mux to handle all the messages that arrive before UNLINK.
1989*7c478bd9Sstevel@tonic-gate  * If the mux has to send down messages on its lower stream before
1990*7c478bd9Sstevel@tonic-gate  * ACKing I_UNLINK, then it *should* know to handle messages even
1991*7c478bd9Sstevel@tonic-gate  * after the UNLINK is acked (actually it should be able to handle till we
1992*7c478bd9Sstevel@tonic-gate  * re-block the read side of the pass queue here). If the mux does not
1993*7c478bd9Sstevel@tonic-gate  * open up the lower stream, any messages that arrive during UNLINK
1994*7c478bd9Sstevel@tonic-gate  * will be put in the stream head. In the case of lower stream opening
1995*7c478bd9Sstevel@tonic-gate  * up, some messages might land in the stream head depending on when
1996*7c478bd9Sstevel@tonic-gate  * the message arrived and when the read side of the pass queue was
1997*7c478bd9Sstevel@tonic-gate  * re-blocked.
1998*7c478bd9Sstevel@tonic-gate  */
1999*7c478bd9Sstevel@tonic-gate int
2000*7c478bd9Sstevel@tonic-gate munlink(stdata_t *stp, linkinfo_t *linkp, int flag, cred_t *crp, int *rvalp)
2001*7c478bd9Sstevel@tonic-gate {
2002*7c478bd9Sstevel@tonic-gate 	struct strioctl strioc;
2003*7c478bd9Sstevel@tonic-gate 	struct stdata *stpdown;
2004*7c478bd9Sstevel@tonic-gate 	queue_t *rq, *wrq;
2005*7c478bd9Sstevel@tonic-gate 	queue_t	*passq;
2006*7c478bd9Sstevel@tonic-gate 	syncq_t *passyncq;
2007*7c478bd9Sstevel@tonic-gate 	int error = 0;
2008*7c478bd9Sstevel@tonic-gate 	file_t *fpdown;
2009*7c478bd9Sstevel@tonic-gate 
2010*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&muxifier));
2011*7c478bd9Sstevel@tonic-gate 
2012*7c478bd9Sstevel@tonic-gate 	stpdown = linkp->li_fpdown->f_vnode->v_stream;
2013*7c478bd9Sstevel@tonic-gate 
2014*7c478bd9Sstevel@tonic-gate 	/*
2015*7c478bd9Sstevel@tonic-gate 	 * See the comment in mlink() concerning STRPLUMB/STPLEX flags.
2016*7c478bd9Sstevel@tonic-gate 	 */
2017*7c478bd9Sstevel@tonic-gate 	mutex_enter(&stpdown->sd_lock);
2018*7c478bd9Sstevel@tonic-gate 	stpdown->sd_flag |= STRPLUMB;
2019*7c478bd9Sstevel@tonic-gate 	mutex_exit(&stpdown->sd_lock);
2020*7c478bd9Sstevel@tonic-gate 
2021*7c478bd9Sstevel@tonic-gate 	/*
2022*7c478bd9Sstevel@tonic-gate 	 * Add passthru queue below lower mux. This will block
2023*7c478bd9Sstevel@tonic-gate 	 * syncqs of lower muxs read queue during I_LINK/I_UNLINK.
2024*7c478bd9Sstevel@tonic-gate 	 */
2025*7c478bd9Sstevel@tonic-gate 	passq = link_addpassthru(stpdown);
2026*7c478bd9Sstevel@tonic-gate 
2027*7c478bd9Sstevel@tonic-gate 	if ((flag & LINKTYPEMASK) == LINKNORMAL)
2028*7c478bd9Sstevel@tonic-gate 		strioc.ic_cmd = I_UNLINK;
2029*7c478bd9Sstevel@tonic-gate 	else
2030*7c478bd9Sstevel@tonic-gate 		strioc.ic_cmd = I_PUNLINK;
2031*7c478bd9Sstevel@tonic-gate 	strioc.ic_timout = INFTIM;
2032*7c478bd9Sstevel@tonic-gate 	strioc.ic_len = sizeof (struct linkblk);
2033*7c478bd9Sstevel@tonic-gate 	strioc.ic_dp = (char *)&linkp->li_lblk;
2034*7c478bd9Sstevel@tonic-gate 
2035*7c478bd9Sstevel@tonic-gate 	error = strdoioctl(stp, &strioc, FNATIVE,
2036*7c478bd9Sstevel@tonic-gate 	    K_TO_K | STR_NOERROR | STR_NOSIG, crp, rvalp);
2037*7c478bd9Sstevel@tonic-gate 
2038*7c478bd9Sstevel@tonic-gate 	/*
2039*7c478bd9Sstevel@tonic-gate 	 * If there was an error and this is not called via strclose,
2040*7c478bd9Sstevel@tonic-gate 	 * return to the user. Otherwise, pretend there was no error
2041*7c478bd9Sstevel@tonic-gate 	 * and close the link.
2042*7c478bd9Sstevel@tonic-gate 	 */
2043*7c478bd9Sstevel@tonic-gate 	if (error) {
2044*7c478bd9Sstevel@tonic-gate 		if (flag & LINKCLOSE) {
2045*7c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "KERNEL: munlink: could not perform "
2046*7c478bd9Sstevel@tonic-gate 			    "unlink ioctl, closing anyway (%d)\n", error);
2047*7c478bd9Sstevel@tonic-gate 		} else {
2048*7c478bd9Sstevel@tonic-gate 			link_rempassthru(passq);
2049*7c478bd9Sstevel@tonic-gate 			mutex_enter(&stpdown->sd_lock);
2050*7c478bd9Sstevel@tonic-gate 			stpdown->sd_flag &= ~STRPLUMB;
2051*7c478bd9Sstevel@tonic-gate 			cv_broadcast(&stpdown->sd_monitor);
2052*7c478bd9Sstevel@tonic-gate 			mutex_exit(&stpdown->sd_lock);
2053*7c478bd9Sstevel@tonic-gate 			mutex_exit(&muxifier);
2054*7c478bd9Sstevel@tonic-gate 			return (error);
2055*7c478bd9Sstevel@tonic-gate 		}
2056*7c478bd9Sstevel@tonic-gate 	}
2057*7c478bd9Sstevel@tonic-gate 
2058*7c478bd9Sstevel@tonic-gate 	mux_rmvedge(stp, linkp->li_lblk.l_index);
2059*7c478bd9Sstevel@tonic-gate 	fpdown = linkp->li_fpdown;
2060*7c478bd9Sstevel@tonic-gate 	lbfree(linkp);
2061*7c478bd9Sstevel@tonic-gate 
2062*7c478bd9Sstevel@tonic-gate 	/*
2063*7c478bd9Sstevel@tonic-gate 	 * We go ahead and drop muxifier here--it's a nasty global lock that
2064*7c478bd9Sstevel@tonic-gate 	 * can slow others down. It's okay to since attempts to mlink() this
2065*7c478bd9Sstevel@tonic-gate 	 * stream will be stopped because STPLEX is still set in the stdata
2066*7c478bd9Sstevel@tonic-gate 	 * structure, and munlink() is stopped because mux_rmvedge() and
2067*7c478bd9Sstevel@tonic-gate 	 * lbfree() have removed it from mux_nodes[] and linkinfo_list,
2068*7c478bd9Sstevel@tonic-gate 	 * respectively.  Note that we defer the closef() of fpdown until
2069*7c478bd9Sstevel@tonic-gate 	 * after we drop muxifier since strclose() can call munlinkall().
2070*7c478bd9Sstevel@tonic-gate 	 */
2071*7c478bd9Sstevel@tonic-gate 	mutex_exit(&muxifier);
2072*7c478bd9Sstevel@tonic-gate 
2073*7c478bd9Sstevel@tonic-gate 	wrq = stpdown->sd_wrq;
2074*7c478bd9Sstevel@tonic-gate 	rq = _RD(wrq);
2075*7c478bd9Sstevel@tonic-gate 
2076*7c478bd9Sstevel@tonic-gate 	/*
2077*7c478bd9Sstevel@tonic-gate 	 * Get rid of outstanding service procedure runs, before we make
2078*7c478bd9Sstevel@tonic-gate 	 * it a stream head, since a stream head doesn't have any service
2079*7c478bd9Sstevel@tonic-gate 	 * procedure.
2080*7c478bd9Sstevel@tonic-gate 	 */
2081*7c478bd9Sstevel@tonic-gate 	disable_svc(rq);
2082*7c478bd9Sstevel@tonic-gate 	wait_svc(rq);
2083*7c478bd9Sstevel@tonic-gate 
2084*7c478bd9Sstevel@tonic-gate 	/*
2085*7c478bd9Sstevel@tonic-gate 	 * Since we don't disable the syncq for QPERMOD, we wait for whatever
2086*7c478bd9Sstevel@tonic-gate 	 * is queued up to be finished. mux should take care that nothing is
2087*7c478bd9Sstevel@tonic-gate 	 * send down to this queue. We should do it now as we're going to block
2088*7c478bd9Sstevel@tonic-gate 	 * passyncq if it was unblocked.
2089*7c478bd9Sstevel@tonic-gate 	 */
2090*7c478bd9Sstevel@tonic-gate 	if (wrq->q_flag & QPERMOD) {
2091*7c478bd9Sstevel@tonic-gate 		syncq_t	*sq = wrq->q_syncq;
2092*7c478bd9Sstevel@tonic-gate 
2093*7c478bd9Sstevel@tonic-gate 		mutex_enter(SQLOCK(sq));
2094*7c478bd9Sstevel@tonic-gate 		while (wrq->q_sqflags & Q_SQQUEUED) {
2095*7c478bd9Sstevel@tonic-gate 			sq->sq_flags |= SQ_WANTWAKEUP;
2096*7c478bd9Sstevel@tonic-gate 			cv_wait(&sq->sq_wait, SQLOCK(sq));
2097*7c478bd9Sstevel@tonic-gate 		}
2098*7c478bd9Sstevel@tonic-gate 		mutex_exit(SQLOCK(sq));
2099*7c478bd9Sstevel@tonic-gate 	}
2100*7c478bd9Sstevel@tonic-gate 	passyncq = passq->q_syncq;
2101*7c478bd9Sstevel@tonic-gate 	if (!(passyncq->sq_flags & SQ_BLOCKED)) {
2102*7c478bd9Sstevel@tonic-gate 
2103*7c478bd9Sstevel@tonic-gate 		syncq_t *sq, *outer;
2104*7c478bd9Sstevel@tonic-gate 
2105*7c478bd9Sstevel@tonic-gate 		/*
2106*7c478bd9Sstevel@tonic-gate 		 * Messages could be flowing from underneath. We will
2107*7c478bd9Sstevel@tonic-gate 		 * block the read side of the passq. This would be
2108*7c478bd9Sstevel@tonic-gate 		 * sufficient for QPAIR and QPERQ muxes to ensure
2109*7c478bd9Sstevel@tonic-gate 		 * that no data is flowing up into this queue
2110*7c478bd9Sstevel@tonic-gate 		 * and hence no thread active in this instance of
2111*7c478bd9Sstevel@tonic-gate 		 * lower mux. But for QPERMOD and QMTOUTPERIM there
2112*7c478bd9Sstevel@tonic-gate 		 * could be messages on the inner and outer/inner
2113*7c478bd9Sstevel@tonic-gate 		 * syncqs respectively. We will wait for them to drain.
2114*7c478bd9Sstevel@tonic-gate 		 * Because passq is blocked messages end up in the syncq
2115*7c478bd9Sstevel@tonic-gate 		 * And qfill_syncq could possibly end up setting QFULL
2116*7c478bd9Sstevel@tonic-gate 		 * which will access the rq->q_flag. Hence, we have to
2117*7c478bd9Sstevel@tonic-gate 		 * acquire the QLOCK in setq.
2118*7c478bd9Sstevel@tonic-gate 		 *
2119*7c478bd9Sstevel@tonic-gate 		 * XXX Messages can also flow from top into this
2120*7c478bd9Sstevel@tonic-gate 		 * queue though the unlink is over (Ex. some instance
2121*7c478bd9Sstevel@tonic-gate 		 * in putnext() called from top that has still not
2122*7c478bd9Sstevel@tonic-gate 		 * accessed this queue. And also putq(lowerq) ?).
2123*7c478bd9Sstevel@tonic-gate 		 * Solution : How about blocking the l_qtop queue ?
2124*7c478bd9Sstevel@tonic-gate 		 * Do we really care about such pure D_MP muxes ?
2125*7c478bd9Sstevel@tonic-gate 		 */
2126*7c478bd9Sstevel@tonic-gate 
2127*7c478bd9Sstevel@tonic-gate 		blocksq(passyncq, SQ_BLOCKED, 0);
2128*7c478bd9Sstevel@tonic-gate 
2129*7c478bd9Sstevel@tonic-gate 		sq = rq->q_syncq;
2130*7c478bd9Sstevel@tonic-gate 		if ((outer = sq->sq_outer) != NULL) {
2131*7c478bd9Sstevel@tonic-gate 
2132*7c478bd9Sstevel@tonic-gate 			/*
2133*7c478bd9Sstevel@tonic-gate 			 * We have to just wait for the outer sq_count
2134*7c478bd9Sstevel@tonic-gate 			 * drop to zero. As this does not prevent new
2135*7c478bd9Sstevel@tonic-gate 			 * messages to enter the outer perimeter, this
2136*7c478bd9Sstevel@tonic-gate 			 * is subject to starvation.
2137*7c478bd9Sstevel@tonic-gate 			 *
2138*7c478bd9Sstevel@tonic-gate 			 * NOTE :Because of blocksq above, messages could
2139*7c478bd9Sstevel@tonic-gate 			 * be in the inner syncq only because of some
2140*7c478bd9Sstevel@tonic-gate 			 * thread holding the outer perimeter exclusively.
2141*7c478bd9Sstevel@tonic-gate 			 * Hence it would be sufficient to wait for the
2142*7c478bd9Sstevel@tonic-gate 			 * exclusive holder of the outer perimeter to drain
2143*7c478bd9Sstevel@tonic-gate 			 * the inner and outer syncqs. But we will not depend
2144*7c478bd9Sstevel@tonic-gate 			 * on this feature and hence check the inner syncqs
2145*7c478bd9Sstevel@tonic-gate 			 * separately.
2146*7c478bd9Sstevel@tonic-gate 			 */
2147*7c478bd9Sstevel@tonic-gate 			wait_syncq(outer);
2148*7c478bd9Sstevel@tonic-gate 		}
2149*7c478bd9Sstevel@tonic-gate 
2150*7c478bd9Sstevel@tonic-gate 
2151*7c478bd9Sstevel@tonic-gate 		/*
2152*7c478bd9Sstevel@tonic-gate 		 * There could be messages destined for
2153*7c478bd9Sstevel@tonic-gate 		 * this queue. Let the exclusive holder
2154*7c478bd9Sstevel@tonic-gate 		 * drain it.
2155*7c478bd9Sstevel@tonic-gate 		 */
2156*7c478bd9Sstevel@tonic-gate 
2157*7c478bd9Sstevel@tonic-gate 		wait_syncq(sq);
2158*7c478bd9Sstevel@tonic-gate 		ASSERT((rq->q_flag & QPERMOD) ||
2159*7c478bd9Sstevel@tonic-gate 			((rq->q_syncq->sq_head == NULL) &&
2160*7c478bd9Sstevel@tonic-gate 			(_WR(rq)->q_syncq->sq_head == NULL)));
2161*7c478bd9Sstevel@tonic-gate 	}
2162*7c478bd9Sstevel@tonic-gate 
2163*7c478bd9Sstevel@tonic-gate 	/*
2164*7c478bd9Sstevel@tonic-gate 	 * We haven't taken care of QPERMOD case yet. QPERMOD is a special
2165*7c478bd9Sstevel@tonic-gate 	 * case as we don't disable its syncq or remove it off the syncq
2166*7c478bd9Sstevel@tonic-gate 	 * service list.
2167*7c478bd9Sstevel@tonic-gate 	 */
2168*7c478bd9Sstevel@tonic-gate 	if (rq->q_flag & QPERMOD) {
2169*7c478bd9Sstevel@tonic-gate 		syncq_t	*sq = rq->q_syncq;
2170*7c478bd9Sstevel@tonic-gate 
2171*7c478bd9Sstevel@tonic-gate 		mutex_enter(SQLOCK(sq));
2172*7c478bd9Sstevel@tonic-gate 		while (rq->q_sqflags & Q_SQQUEUED) {
2173*7c478bd9Sstevel@tonic-gate 			sq->sq_flags |= SQ_WANTWAKEUP;
2174*7c478bd9Sstevel@tonic-gate 			cv_wait(&sq->sq_wait, SQLOCK(sq));
2175*7c478bd9Sstevel@tonic-gate 		}
2176*7c478bd9Sstevel@tonic-gate 		mutex_exit(SQLOCK(sq));
2177*7c478bd9Sstevel@tonic-gate 	}
2178*7c478bd9Sstevel@tonic-gate 
2179*7c478bd9Sstevel@tonic-gate 	/*
2180*7c478bd9Sstevel@tonic-gate 	 * flush_syncq changes states only when there is some messages to
2181*7c478bd9Sstevel@tonic-gate 	 * free. ie when it returns non-zero value to return.
2182*7c478bd9Sstevel@tonic-gate 	 */
2183*7c478bd9Sstevel@tonic-gate 	ASSERT(flush_syncq(rq->q_syncq, rq) == 0);
2184*7c478bd9Sstevel@tonic-gate 	ASSERT(flush_syncq(wrq->q_syncq, wrq) == 0);
2185*7c478bd9Sstevel@tonic-gate 
2186*7c478bd9Sstevel@tonic-gate 	/*
2187*7c478bd9Sstevel@tonic-gate 	 * No body else should know about this queue now.
2188*7c478bd9Sstevel@tonic-gate 	 * If the mux did not process the messages before
2189*7c478bd9Sstevel@tonic-gate 	 * acking the I_UNLINK, free them now.
2190*7c478bd9Sstevel@tonic-gate 	 */
2191*7c478bd9Sstevel@tonic-gate 
2192*7c478bd9Sstevel@tonic-gate 	flushq(rq, FLUSHALL);
2193*7c478bd9Sstevel@tonic-gate 	flushq(_WR(rq), FLUSHALL);
2194*7c478bd9Sstevel@tonic-gate 
2195*7c478bd9Sstevel@tonic-gate 	/*
2196*7c478bd9Sstevel@tonic-gate 	 * Convert the mux lower queue into a stream head queue.
2197*7c478bd9Sstevel@tonic-gate 	 * Turn off STPLEX before we turn on the stream by removing the passq.
2198*7c478bd9Sstevel@tonic-gate 	 */
2199*7c478bd9Sstevel@tonic-gate 	rq->q_ptr = wrq->q_ptr = stpdown;
2200*7c478bd9Sstevel@tonic-gate 	setq(rq, &strdata, &stwdata, NULL, QMTSAFE, SQ_CI|SQ_CO, B_TRUE);
2201*7c478bd9Sstevel@tonic-gate 
2202*7c478bd9Sstevel@tonic-gate 	ASSERT((rq->q_flag & QMT_TYPEMASK) == QMTSAFE);
2203*7c478bd9Sstevel@tonic-gate 	ASSERT(rq->q_syncq == SQ(rq) && _WR(rq)->q_syncq == SQ(rq));
2204*7c478bd9Sstevel@tonic-gate 
2205*7c478bd9Sstevel@tonic-gate 	enable_svc(rq);
2206*7c478bd9Sstevel@tonic-gate 
2207*7c478bd9Sstevel@tonic-gate 	/*
2208*7c478bd9Sstevel@tonic-gate 	 * Now it is a proper stream, so STPLEX is cleared. But STRPLUMB still
2209*7c478bd9Sstevel@tonic-gate 	 * needs to be set to prevent reopen() of the stream - such reopen may
2210*7c478bd9Sstevel@tonic-gate 	 * try to call non-existent pass queue open routine and panic.
2211*7c478bd9Sstevel@tonic-gate 	 */
2212*7c478bd9Sstevel@tonic-gate 	mutex_enter(&stpdown->sd_lock);
2213*7c478bd9Sstevel@tonic-gate 	stpdown->sd_flag &= ~STPLEX;
2214*7c478bd9Sstevel@tonic-gate 	mutex_exit(&stpdown->sd_lock);
2215*7c478bd9Sstevel@tonic-gate 
2216*7c478bd9Sstevel@tonic-gate 	ASSERT(((flag & LINKTYPEMASK) == LINKNORMAL) ||
2217*7c478bd9Sstevel@tonic-gate 	    ((flag & LINKTYPEMASK) == LINKPERSIST));
2218*7c478bd9Sstevel@tonic-gate 
2219*7c478bd9Sstevel@tonic-gate 	/* clean up the layered driver linkages */
2220*7c478bd9Sstevel@tonic-gate 	if ((flag & LINKTYPEMASK) == LINKNORMAL) {
2221*7c478bd9Sstevel@tonic-gate 		ldi_munlink_fp(stp, fpdown, LINKNORMAL);
2222*7c478bd9Sstevel@tonic-gate 	} else {
2223*7c478bd9Sstevel@tonic-gate 		ldi_munlink_fp(stp, fpdown, LINKPERSIST);
2224*7c478bd9Sstevel@tonic-gate 	}
2225*7c478bd9Sstevel@tonic-gate 
2226*7c478bd9Sstevel@tonic-gate 	link_rempassthru(passq);
2227*7c478bd9Sstevel@tonic-gate 
2228*7c478bd9Sstevel@tonic-gate 	/*
2229*7c478bd9Sstevel@tonic-gate 	 * Now all plumbing changes are finished and STRPLUMB is no
2230*7c478bd9Sstevel@tonic-gate 	 * longer needed.
2231*7c478bd9Sstevel@tonic-gate 	 */
2232*7c478bd9Sstevel@tonic-gate 	mutex_enter(&stpdown->sd_lock);
2233*7c478bd9Sstevel@tonic-gate 	stpdown->sd_flag &= ~STRPLUMB;
2234*7c478bd9Sstevel@tonic-gate 	cv_broadcast(&stpdown->sd_monitor);
2235*7c478bd9Sstevel@tonic-gate 	mutex_exit(&stpdown->sd_lock);
2236*7c478bd9Sstevel@tonic-gate 
2237*7c478bd9Sstevel@tonic-gate 	(void) closef(fpdown);
2238*7c478bd9Sstevel@tonic-gate 	return (0);
2239*7c478bd9Sstevel@tonic-gate }
2240*7c478bd9Sstevel@tonic-gate 
2241*7c478bd9Sstevel@tonic-gate /*
2242*7c478bd9Sstevel@tonic-gate  * Unlink all multiplexor links for which stp is the controlling stream.
2243*7c478bd9Sstevel@tonic-gate  * Return 0, or a non-zero errno on failure.
2244*7c478bd9Sstevel@tonic-gate  */
2245*7c478bd9Sstevel@tonic-gate int
2246*7c478bd9Sstevel@tonic-gate munlinkall(stdata_t *stp, int flag, cred_t *crp, int *rvalp)
2247*7c478bd9Sstevel@tonic-gate {
2248*7c478bd9Sstevel@tonic-gate 	linkinfo_t *linkp;
2249*7c478bd9Sstevel@tonic-gate 	int error = 0;
2250*7c478bd9Sstevel@tonic-gate 
2251*7c478bd9Sstevel@tonic-gate 	mutex_enter(&muxifier);
2252*7c478bd9Sstevel@tonic-gate 	while (linkp = findlinks(stp, 0, flag)) {
2253*7c478bd9Sstevel@tonic-gate 		/*
2254*7c478bd9Sstevel@tonic-gate 		 * munlink() releases the muxifier lock.
2255*7c478bd9Sstevel@tonic-gate 		 */
2256*7c478bd9Sstevel@tonic-gate 		if (error = munlink(stp, linkp, flag, crp, rvalp))
2257*7c478bd9Sstevel@tonic-gate 			return (error);
2258*7c478bd9Sstevel@tonic-gate 		mutex_enter(&muxifier);
2259*7c478bd9Sstevel@tonic-gate 	}
2260*7c478bd9Sstevel@tonic-gate 	mutex_exit(&muxifier);
2261*7c478bd9Sstevel@tonic-gate 	return (0);
2262*7c478bd9Sstevel@tonic-gate }
2263*7c478bd9Sstevel@tonic-gate 
2264*7c478bd9Sstevel@tonic-gate /*
2265*7c478bd9Sstevel@tonic-gate  * A multiplexor link has been made. Add an
2266*7c478bd9Sstevel@tonic-gate  * edge to the directed graph.
2267*7c478bd9Sstevel@tonic-gate  */
2268*7c478bd9Sstevel@tonic-gate void
2269*7c478bd9Sstevel@tonic-gate mux_addedge(stdata_t *upstp, stdata_t *lostp, int muxid)
2270*7c478bd9Sstevel@tonic-gate {
2271*7c478bd9Sstevel@tonic-gate 	struct mux_node *np;
2272*7c478bd9Sstevel@tonic-gate 	struct mux_edge *ep;
2273*7c478bd9Sstevel@tonic-gate 	major_t upmaj;
2274*7c478bd9Sstevel@tonic-gate 	major_t lomaj;
2275*7c478bd9Sstevel@tonic-gate 
2276*7c478bd9Sstevel@tonic-gate 	upmaj = getmajor(upstp->sd_vnode->v_rdev);
2277*7c478bd9Sstevel@tonic-gate 	lomaj = getmajor(lostp->sd_vnode->v_rdev);
2278*7c478bd9Sstevel@tonic-gate 	np = &mux_nodes[upmaj];
2279*7c478bd9Sstevel@tonic-gate 	if (np->mn_outp) {
2280*7c478bd9Sstevel@tonic-gate 		ep = np->mn_outp;
2281*7c478bd9Sstevel@tonic-gate 		while (ep->me_nextp)
2282*7c478bd9Sstevel@tonic-gate 			ep = ep->me_nextp;
2283*7c478bd9Sstevel@tonic-gate 		ep->me_nextp = kmem_alloc(sizeof (struct mux_edge), KM_SLEEP);
2284*7c478bd9Sstevel@tonic-gate 		ep = ep->me_nextp;
2285*7c478bd9Sstevel@tonic-gate 	} else {
2286*7c478bd9Sstevel@tonic-gate 		np->mn_outp = kmem_alloc(sizeof (struct mux_edge), KM_SLEEP);
2287*7c478bd9Sstevel@tonic-gate 		ep = np->mn_outp;
2288*7c478bd9Sstevel@tonic-gate 	}
2289*7c478bd9Sstevel@tonic-gate 	ep->me_nextp = NULL;
2290*7c478bd9Sstevel@tonic-gate 	ep->me_muxid = muxid;
2291*7c478bd9Sstevel@tonic-gate 	if (lostp->sd_vnode->v_type == VFIFO)
2292*7c478bd9Sstevel@tonic-gate 		ep->me_nodep = NULL;
2293*7c478bd9Sstevel@tonic-gate 	else
2294*7c478bd9Sstevel@tonic-gate 		ep->me_nodep = &mux_nodes[lomaj];
2295*7c478bd9Sstevel@tonic-gate }
2296*7c478bd9Sstevel@tonic-gate 
2297*7c478bd9Sstevel@tonic-gate /*
2298*7c478bd9Sstevel@tonic-gate  * A multiplexor link has been removed. Remove the
2299*7c478bd9Sstevel@tonic-gate  * edge in the directed graph.
2300*7c478bd9Sstevel@tonic-gate  */
2301*7c478bd9Sstevel@tonic-gate void
2302*7c478bd9Sstevel@tonic-gate mux_rmvedge(stdata_t *upstp, int muxid)
2303*7c478bd9Sstevel@tonic-gate {
2304*7c478bd9Sstevel@tonic-gate 	struct mux_node *np;
2305*7c478bd9Sstevel@tonic-gate 	struct mux_edge *ep;
2306*7c478bd9Sstevel@tonic-gate 	struct mux_edge *pep = NULL;
2307*7c478bd9Sstevel@tonic-gate 	major_t upmaj;
2308*7c478bd9Sstevel@tonic-gate 
2309*7c478bd9Sstevel@tonic-gate 	upmaj = getmajor(upstp->sd_vnode->v_rdev);
2310*7c478bd9Sstevel@tonic-gate 	np = &mux_nodes[upmaj];
2311*7c478bd9Sstevel@tonic-gate 	ASSERT(np->mn_outp != NULL);
2312*7c478bd9Sstevel@tonic-gate 	ep = np->mn_outp;
2313*7c478bd9Sstevel@tonic-gate 	while (ep) {
2314*7c478bd9Sstevel@tonic-gate 		if (ep->me_muxid == muxid) {
2315*7c478bd9Sstevel@tonic-gate 			if (pep)
2316*7c478bd9Sstevel@tonic-gate 				pep->me_nextp = ep->me_nextp;
2317*7c478bd9Sstevel@tonic-gate 			else
2318*7c478bd9Sstevel@tonic-gate 				np->mn_outp = ep->me_nextp;
2319*7c478bd9Sstevel@tonic-gate 			kmem_free(ep, sizeof (struct mux_edge));
2320*7c478bd9Sstevel@tonic-gate 			return;
2321*7c478bd9Sstevel@tonic-gate 		}
2322*7c478bd9Sstevel@tonic-gate 		pep = ep;
2323*7c478bd9Sstevel@tonic-gate 		ep = ep->me_nextp;
2324*7c478bd9Sstevel@tonic-gate 	}
2325*7c478bd9Sstevel@tonic-gate 	ASSERT(0);	/* should not reach here */
2326*7c478bd9Sstevel@tonic-gate }
2327*7c478bd9Sstevel@tonic-gate 
2328*7c478bd9Sstevel@tonic-gate /*
2329*7c478bd9Sstevel@tonic-gate  * Translate the device flags (from conf.h) to the corresponding
2330*7c478bd9Sstevel@tonic-gate  * qflag and sq_flag (type) values.
2331*7c478bd9Sstevel@tonic-gate  */
2332*7c478bd9Sstevel@tonic-gate int
2333*7c478bd9Sstevel@tonic-gate devflg_to_qflag(struct streamtab *stp, uint32_t devflag, uint32_t *qflagp,
2334*7c478bd9Sstevel@tonic-gate 	uint32_t *sqtypep)
2335*7c478bd9Sstevel@tonic-gate {
2336*7c478bd9Sstevel@tonic-gate 	uint32_t qflag = 0;
2337*7c478bd9Sstevel@tonic-gate 	uint32_t sqtype = 0;
2338*7c478bd9Sstevel@tonic-gate 
2339*7c478bd9Sstevel@tonic-gate 	if (devflag & _D_OLD)
2340*7c478bd9Sstevel@tonic-gate 		goto bad;
2341*7c478bd9Sstevel@tonic-gate 
2342*7c478bd9Sstevel@tonic-gate 	/* Inner perimeter presence and scope */
2343*7c478bd9Sstevel@tonic-gate 	switch (devflag & D_MTINNER_MASK) {
2344*7c478bd9Sstevel@tonic-gate 	case D_MP:
2345*7c478bd9Sstevel@tonic-gate 		qflag |= QMTSAFE;
2346*7c478bd9Sstevel@tonic-gate 		sqtype |= SQ_CI;
2347*7c478bd9Sstevel@tonic-gate 		break;
2348*7c478bd9Sstevel@tonic-gate 	case D_MTPERQ|D_MP:
2349*7c478bd9Sstevel@tonic-gate 		qflag |= QPERQ;
2350*7c478bd9Sstevel@tonic-gate 		break;
2351*7c478bd9Sstevel@tonic-gate 	case D_MTQPAIR|D_MP:
2352*7c478bd9Sstevel@tonic-gate 		qflag |= QPAIR;
2353*7c478bd9Sstevel@tonic-gate 		break;
2354*7c478bd9Sstevel@tonic-gate 	case D_MTPERMOD|D_MP:
2355*7c478bd9Sstevel@tonic-gate 		qflag |= QPERMOD;
2356*7c478bd9Sstevel@tonic-gate 		break;
2357*7c478bd9Sstevel@tonic-gate 	default:
2358*7c478bd9Sstevel@tonic-gate 		goto bad;
2359*7c478bd9Sstevel@tonic-gate 	}
2360*7c478bd9Sstevel@tonic-gate 
2361*7c478bd9Sstevel@tonic-gate 	/* Outer perimeter */
2362*7c478bd9Sstevel@tonic-gate 	if (devflag & D_MTOUTPERIM) {
2363*7c478bd9Sstevel@tonic-gate 		switch (devflag & D_MTINNER_MASK) {
2364*7c478bd9Sstevel@tonic-gate 		case D_MP:
2365*7c478bd9Sstevel@tonic-gate 		case D_MTPERQ|D_MP:
2366*7c478bd9Sstevel@tonic-gate 		case D_MTQPAIR|D_MP:
2367*7c478bd9Sstevel@tonic-gate 			break;
2368*7c478bd9Sstevel@tonic-gate 		default:
2369*7c478bd9Sstevel@tonic-gate 			goto bad;
2370*7c478bd9Sstevel@tonic-gate 		}
2371*7c478bd9Sstevel@tonic-gate 		qflag |= QMTOUTPERIM;
2372*7c478bd9Sstevel@tonic-gate 	}
2373*7c478bd9Sstevel@tonic-gate 
2374*7c478bd9Sstevel@tonic-gate 	/* Inner perimeter modifiers */
2375*7c478bd9Sstevel@tonic-gate 	if (devflag & D_MTINNER_MOD) {
2376*7c478bd9Sstevel@tonic-gate 		switch (devflag & D_MTINNER_MASK) {
2377*7c478bd9Sstevel@tonic-gate 		case D_MP:
2378*7c478bd9Sstevel@tonic-gate 			goto bad;
2379*7c478bd9Sstevel@tonic-gate 		default:
2380*7c478bd9Sstevel@tonic-gate 			break;
2381*7c478bd9Sstevel@tonic-gate 		}
2382*7c478bd9Sstevel@tonic-gate 		if (devflag & D_MTPUTSHARED)
2383*7c478bd9Sstevel@tonic-gate 			sqtype |= SQ_CIPUT;
2384*7c478bd9Sstevel@tonic-gate 		if (devflag & _D_MTOCSHARED) {
2385*7c478bd9Sstevel@tonic-gate 			/*
2386*7c478bd9Sstevel@tonic-gate 			 * The code in putnext assumes that it has the
2387*7c478bd9Sstevel@tonic-gate 			 * highest concurrency by not checking sq_count.
2388*7c478bd9Sstevel@tonic-gate 			 * Thus _D_MTOCSHARED can only be supported when
2389*7c478bd9Sstevel@tonic-gate 			 * D_MTPUTSHARED is set.
2390*7c478bd9Sstevel@tonic-gate 			 */
2391*7c478bd9Sstevel@tonic-gate 			if (!(devflag & D_MTPUTSHARED))
2392*7c478bd9Sstevel@tonic-gate 				goto bad;
2393*7c478bd9Sstevel@tonic-gate 			sqtype |= SQ_CIOC;
2394*7c478bd9Sstevel@tonic-gate 		}
2395*7c478bd9Sstevel@tonic-gate 		if (devflag & _D_MTCBSHARED) {
2396*7c478bd9Sstevel@tonic-gate 			/*
2397*7c478bd9Sstevel@tonic-gate 			 * The code in putnext assumes that it has the
2398*7c478bd9Sstevel@tonic-gate 			 * highest concurrency by not checking sq_count.
2399*7c478bd9Sstevel@tonic-gate 			 * Thus _D_MTCBSHARED can only be supported when
2400*7c478bd9Sstevel@tonic-gate 			 * D_MTPUTSHARED is set.
2401*7c478bd9Sstevel@tonic-gate 			 */
2402*7c478bd9Sstevel@tonic-gate 			if (!(devflag & D_MTPUTSHARED))
2403*7c478bd9Sstevel@tonic-gate 				goto bad;
2404*7c478bd9Sstevel@tonic-gate 			sqtype |= SQ_CICB;
2405*7c478bd9Sstevel@tonic-gate 		}
2406*7c478bd9Sstevel@tonic-gate 		if (devflag & _D_MTSVCSHARED) {
2407*7c478bd9Sstevel@tonic-gate 			/*
2408*7c478bd9Sstevel@tonic-gate 			 * The code in putnext assumes that it has the
2409*7c478bd9Sstevel@tonic-gate 			 * highest concurrency by not checking sq_count.
2410*7c478bd9Sstevel@tonic-gate 			 * Thus _D_MTSVCSHARED can only be supported when
2411*7c478bd9Sstevel@tonic-gate 			 * D_MTPUTSHARED is set. Also _D_MTSVCSHARED is
2412*7c478bd9Sstevel@tonic-gate 			 * supported only for QPERMOD.
2413*7c478bd9Sstevel@tonic-gate 			 */
2414*7c478bd9Sstevel@tonic-gate 			if (!(devflag & D_MTPUTSHARED) || !(qflag & QPERMOD))
2415*7c478bd9Sstevel@tonic-gate 				goto bad;
2416*7c478bd9Sstevel@tonic-gate 			sqtype |= SQ_CISVC;
2417*7c478bd9Sstevel@tonic-gate 		}
2418*7c478bd9Sstevel@tonic-gate 	}
2419*7c478bd9Sstevel@tonic-gate 
2420*7c478bd9Sstevel@tonic-gate 	/* Default outer perimeter concurrency */
2421*7c478bd9Sstevel@tonic-gate 	sqtype |= SQ_CO;
2422*7c478bd9Sstevel@tonic-gate 
2423*7c478bd9Sstevel@tonic-gate 	/* Outer perimeter modifiers */
2424*7c478bd9Sstevel@tonic-gate 	if (devflag & D_MTOCEXCL) {
2425*7c478bd9Sstevel@tonic-gate 		if (!(devflag & D_MTOUTPERIM)) {
2426*7c478bd9Sstevel@tonic-gate 			/* No outer perimeter */
2427*7c478bd9Sstevel@tonic-gate 			goto bad;
2428*7c478bd9Sstevel@tonic-gate 		}
2429*7c478bd9Sstevel@tonic-gate 		sqtype &= ~SQ_COOC;
2430*7c478bd9Sstevel@tonic-gate 	}
2431*7c478bd9Sstevel@tonic-gate 
2432*7c478bd9Sstevel@tonic-gate 	/* Synchronous Streams extended qinit structure */
2433*7c478bd9Sstevel@tonic-gate 	if (devflag & D_SYNCSTR)
2434*7c478bd9Sstevel@tonic-gate 		qflag |= QSYNCSTR;
2435*7c478bd9Sstevel@tonic-gate 
2436*7c478bd9Sstevel@tonic-gate 	*qflagp = qflag;
2437*7c478bd9Sstevel@tonic-gate 	*sqtypep = sqtype;
2438*7c478bd9Sstevel@tonic-gate 	return (0);
2439*7c478bd9Sstevel@tonic-gate 
2440*7c478bd9Sstevel@tonic-gate bad:
2441*7c478bd9Sstevel@tonic-gate 	cmn_err(CE_WARN,
2442*7c478bd9Sstevel@tonic-gate 	    "stropen: bad MT flags (0x%x) in driver '%s'",
2443*7c478bd9Sstevel@tonic-gate 	    (int)(qflag & D_MTSAFETY_MASK),
2444*7c478bd9Sstevel@tonic-gate 	    stp->st_rdinit->qi_minfo->mi_idname);
2445*7c478bd9Sstevel@tonic-gate 
2446*7c478bd9Sstevel@tonic-gate 	return (EINVAL);
2447*7c478bd9Sstevel@tonic-gate }
2448*7c478bd9Sstevel@tonic-gate 
2449*7c478bd9Sstevel@tonic-gate /*
2450*7c478bd9Sstevel@tonic-gate  * Set the interface values for a pair of queues (qinit structure,
2451*7c478bd9Sstevel@tonic-gate  * packet sizes, water marks).
2452*7c478bd9Sstevel@tonic-gate  * setq assumes that the caller does not have a claim (entersq or claimq)
2453*7c478bd9Sstevel@tonic-gate  * on the queue.
2454*7c478bd9Sstevel@tonic-gate  */
2455*7c478bd9Sstevel@tonic-gate void
2456*7c478bd9Sstevel@tonic-gate setq(queue_t *rq, struct qinit *rinit, struct qinit *winit,
2457*7c478bd9Sstevel@tonic-gate     perdm_t *dmp, uint32_t qflag, uint32_t sqtype, boolean_t lock_needed)
2458*7c478bd9Sstevel@tonic-gate {
2459*7c478bd9Sstevel@tonic-gate 	queue_t *wq;
2460*7c478bd9Sstevel@tonic-gate 	syncq_t	*sq, *outer;
2461*7c478bd9Sstevel@tonic-gate 
2462*7c478bd9Sstevel@tonic-gate 	ASSERT(rq->q_flag & QREADR);
2463*7c478bd9Sstevel@tonic-gate 	ASSERT((qflag & QMT_TYPEMASK) != 0);
2464*7c478bd9Sstevel@tonic-gate 	IMPLY((qflag & (QPERMOD | QMTOUTPERIM)), dmp != NULL);
2465*7c478bd9Sstevel@tonic-gate 
2466*7c478bd9Sstevel@tonic-gate 	wq = _WR(rq);
2467*7c478bd9Sstevel@tonic-gate 	rq->q_qinfo = rinit;
2468*7c478bd9Sstevel@tonic-gate 	rq->q_hiwat = rinit->qi_minfo->mi_hiwat;
2469*7c478bd9Sstevel@tonic-gate 	rq->q_lowat = rinit->qi_minfo->mi_lowat;
2470*7c478bd9Sstevel@tonic-gate 	rq->q_minpsz = rinit->qi_minfo->mi_minpsz;
2471*7c478bd9Sstevel@tonic-gate 	rq->q_maxpsz = rinit->qi_minfo->mi_maxpsz;
2472*7c478bd9Sstevel@tonic-gate 	wq->q_qinfo = winit;
2473*7c478bd9Sstevel@tonic-gate 	wq->q_hiwat = winit->qi_minfo->mi_hiwat;
2474*7c478bd9Sstevel@tonic-gate 	wq->q_lowat = winit->qi_minfo->mi_lowat;
2475*7c478bd9Sstevel@tonic-gate 	wq->q_minpsz = winit->qi_minfo->mi_minpsz;
2476*7c478bd9Sstevel@tonic-gate 	wq->q_maxpsz = winit->qi_minfo->mi_maxpsz;
2477*7c478bd9Sstevel@tonic-gate 
2478*7c478bd9Sstevel@tonic-gate 	/* Remove old syncqs */
2479*7c478bd9Sstevel@tonic-gate 	sq = rq->q_syncq;
2480*7c478bd9Sstevel@tonic-gate 	outer = sq->sq_outer;
2481*7c478bd9Sstevel@tonic-gate 	if (outer != NULL) {
2482*7c478bd9Sstevel@tonic-gate 		ASSERT(wq->q_syncq->sq_outer == outer);
2483*7c478bd9Sstevel@tonic-gate 		outer_remove(outer, rq->q_syncq);
2484*7c478bd9Sstevel@tonic-gate 		if (wq->q_syncq != rq->q_syncq)
2485*7c478bd9Sstevel@tonic-gate 			outer_remove(outer, wq->q_syncq);
2486*7c478bd9Sstevel@tonic-gate 	}
2487*7c478bd9Sstevel@tonic-gate 	ASSERT(sq->sq_outer == NULL);
2488*7c478bd9Sstevel@tonic-gate 	ASSERT(sq->sq_onext == NULL && sq->sq_oprev == NULL);
2489*7c478bd9Sstevel@tonic-gate 
2490*7c478bd9Sstevel@tonic-gate 	if (sq != SQ(rq)) {
2491*7c478bd9Sstevel@tonic-gate 		if (!(rq->q_flag & QPERMOD))
2492*7c478bd9Sstevel@tonic-gate 			free_syncq(sq);
2493*7c478bd9Sstevel@tonic-gate 		if (wq->q_syncq == rq->q_syncq)
2494*7c478bd9Sstevel@tonic-gate 			wq->q_syncq = NULL;
2495*7c478bd9Sstevel@tonic-gate 		rq->q_syncq = NULL;
2496*7c478bd9Sstevel@tonic-gate 	}
2497*7c478bd9Sstevel@tonic-gate 	if (wq->q_syncq != NULL && wq->q_syncq != sq &&
2498*7c478bd9Sstevel@tonic-gate 	    wq->q_syncq != SQ(rq)) {
2499*7c478bd9Sstevel@tonic-gate 		free_syncq(wq->q_syncq);
2500*7c478bd9Sstevel@tonic-gate 		wq->q_syncq = NULL;
2501*7c478bd9Sstevel@tonic-gate 	}
2502*7c478bd9Sstevel@tonic-gate 	ASSERT(rq->q_syncq == NULL || (rq->q_syncq->sq_head == NULL &&
2503*7c478bd9Sstevel@tonic-gate 				rq->q_syncq->sq_tail == NULL));
2504*7c478bd9Sstevel@tonic-gate 	ASSERT(wq->q_syncq == NULL || (wq->q_syncq->sq_head == NULL &&
2505*7c478bd9Sstevel@tonic-gate 				wq->q_syncq->sq_tail == NULL));
2506*7c478bd9Sstevel@tonic-gate 
2507*7c478bd9Sstevel@tonic-gate 	if (!(rq->q_flag & QPERMOD) &&
2508*7c478bd9Sstevel@tonic-gate 	    rq->q_syncq != NULL && rq->q_syncq->sq_ciputctrl != NULL) {
2509*7c478bd9Sstevel@tonic-gate 		ASSERT(rq->q_syncq->sq_nciputctrl == n_ciputctrl - 1);
2510*7c478bd9Sstevel@tonic-gate 		SUMCHECK_CIPUTCTRL_COUNTS(rq->q_syncq->sq_ciputctrl,
2511*7c478bd9Sstevel@tonic-gate 		    rq->q_syncq->sq_nciputctrl, 0);
2512*7c478bd9Sstevel@tonic-gate 		ASSERT(ciputctrl_cache != NULL);
2513*7c478bd9Sstevel@tonic-gate 		kmem_cache_free(ciputctrl_cache, rq->q_syncq->sq_ciputctrl);
2514*7c478bd9Sstevel@tonic-gate 		rq->q_syncq->sq_ciputctrl = NULL;
2515*7c478bd9Sstevel@tonic-gate 		rq->q_syncq->sq_nciputctrl = 0;
2516*7c478bd9Sstevel@tonic-gate 	}
2517*7c478bd9Sstevel@tonic-gate 
2518*7c478bd9Sstevel@tonic-gate 	if (!(wq->q_flag & QPERMOD) &&
2519*7c478bd9Sstevel@tonic-gate 	    wq->q_syncq != NULL && wq->q_syncq->sq_ciputctrl != NULL) {
2520*7c478bd9Sstevel@tonic-gate 		ASSERT(wq->q_syncq->sq_nciputctrl == n_ciputctrl - 1);
2521*7c478bd9Sstevel@tonic-gate 		SUMCHECK_CIPUTCTRL_COUNTS(wq->q_syncq->sq_ciputctrl,
2522*7c478bd9Sstevel@tonic-gate 		    wq->q_syncq->sq_nciputctrl, 0);
2523*7c478bd9Sstevel@tonic-gate 		ASSERT(ciputctrl_cache != NULL);
2524*7c478bd9Sstevel@tonic-gate 		kmem_cache_free(ciputctrl_cache, wq->q_syncq->sq_ciputctrl);
2525*7c478bd9Sstevel@tonic-gate 		wq->q_syncq->sq_ciputctrl = NULL;
2526*7c478bd9Sstevel@tonic-gate 		wq->q_syncq->sq_nciputctrl = 0;
2527*7c478bd9Sstevel@tonic-gate 	}
2528*7c478bd9Sstevel@tonic-gate 
2529*7c478bd9Sstevel@tonic-gate 	sq = SQ(rq);
2530*7c478bd9Sstevel@tonic-gate 	ASSERT(sq->sq_head == NULL && sq->sq_tail == NULL);
2531*7c478bd9Sstevel@tonic-gate 	ASSERT(sq->sq_outer == NULL);
2532*7c478bd9Sstevel@tonic-gate 	ASSERT(sq->sq_onext == NULL && sq->sq_oprev == NULL);
2533*7c478bd9Sstevel@tonic-gate 
2534*7c478bd9Sstevel@tonic-gate 	/*
2535*7c478bd9Sstevel@tonic-gate 	 * Create syncqs based on qflag and sqtype. Set the SQ_TYPES_IN_FLAGS
2536*7c478bd9Sstevel@tonic-gate 	 * bits in sq_flag based on the sqtype.
2537*7c478bd9Sstevel@tonic-gate 	 */
2538*7c478bd9Sstevel@tonic-gate 	ASSERT((sq->sq_flags & ~SQ_TYPES_IN_FLAGS) == 0);
2539*7c478bd9Sstevel@tonic-gate 
2540*7c478bd9Sstevel@tonic-gate 	rq->q_syncq = wq->q_syncq = sq;
2541*7c478bd9Sstevel@tonic-gate 	sq->sq_type = sqtype;
2542*7c478bd9Sstevel@tonic-gate 	sq->sq_flags = (sqtype & SQ_TYPES_IN_FLAGS);
2543*7c478bd9Sstevel@tonic-gate 
2544*7c478bd9Sstevel@tonic-gate 	/*
2545*7c478bd9Sstevel@tonic-gate 	 *  We are making sq_svcflags zero,
2546*7c478bd9Sstevel@tonic-gate 	 *  resetting SQ_DISABLED in case it was set by
2547*7c478bd9Sstevel@tonic-gate 	 *  wait_svc() in the munlink path.
2548*7c478bd9Sstevel@tonic-gate 	 *
2549*7c478bd9Sstevel@tonic-gate 	 */
2550*7c478bd9Sstevel@tonic-gate 	ASSERT((sq->sq_svcflags & SQ_SERVICE) == 0);
2551*7c478bd9Sstevel@tonic-gate 	sq->sq_svcflags = 0;
2552*7c478bd9Sstevel@tonic-gate 
2553*7c478bd9Sstevel@tonic-gate 	/*
2554*7c478bd9Sstevel@tonic-gate 	 * We need to acquire the lock here for the mlink and munlink case,
2555*7c478bd9Sstevel@tonic-gate 	 * where canputnext, backenable, etc can access the q_flag.
2556*7c478bd9Sstevel@tonic-gate 	 */
2557*7c478bd9Sstevel@tonic-gate 	if (lock_needed) {
2558*7c478bd9Sstevel@tonic-gate 		mutex_enter(QLOCK(rq));
2559*7c478bd9Sstevel@tonic-gate 		rq->q_flag = (rq->q_flag & ~QMT_TYPEMASK) | QWANTR | qflag;
2560*7c478bd9Sstevel@tonic-gate 		mutex_exit(QLOCK(rq));
2561*7c478bd9Sstevel@tonic-gate 		mutex_enter(QLOCK(wq));
2562*7c478bd9Sstevel@tonic-gate 		wq->q_flag = (wq->q_flag & ~QMT_TYPEMASK) | QWANTR | qflag;
2563*7c478bd9Sstevel@tonic-gate 		mutex_exit(QLOCK(wq));
2564*7c478bd9Sstevel@tonic-gate 	} else {
2565*7c478bd9Sstevel@tonic-gate 		rq->q_flag = (rq->q_flag & ~QMT_TYPEMASK) | QWANTR | qflag;
2566*7c478bd9Sstevel@tonic-gate 		wq->q_flag = (wq->q_flag & ~QMT_TYPEMASK) | QWANTR | qflag;
2567*7c478bd9Sstevel@tonic-gate 	}
2568*7c478bd9Sstevel@tonic-gate 
2569*7c478bd9Sstevel@tonic-gate 	if (qflag & QPERQ) {
2570*7c478bd9Sstevel@tonic-gate 		/* Allocate a separate syncq for the write side */
2571*7c478bd9Sstevel@tonic-gate 		sq = new_syncq();
2572*7c478bd9Sstevel@tonic-gate 		sq->sq_type = rq->q_syncq->sq_type;
2573*7c478bd9Sstevel@tonic-gate 		sq->sq_flags = rq->q_syncq->sq_flags;
2574*7c478bd9Sstevel@tonic-gate 		ASSERT(sq->sq_outer == NULL && sq->sq_onext == NULL &&
2575*7c478bd9Sstevel@tonic-gate 		    sq->sq_oprev == NULL);
2576*7c478bd9Sstevel@tonic-gate 		wq->q_syncq = sq;
2577*7c478bd9Sstevel@tonic-gate 	}
2578*7c478bd9Sstevel@tonic-gate 	if (qflag & QPERMOD) {
2579*7c478bd9Sstevel@tonic-gate 		sq = dmp->dm_sq;
2580*7c478bd9Sstevel@tonic-gate 
2581*7c478bd9Sstevel@tonic-gate 		/*
2582*7c478bd9Sstevel@tonic-gate 		 * Assert that we do have an inner perimeter syncq and that it
2583*7c478bd9Sstevel@tonic-gate 		 * does not have an outer perimeter associated with it.
2584*7c478bd9Sstevel@tonic-gate 		 */
2585*7c478bd9Sstevel@tonic-gate 		ASSERT(sq->sq_outer == NULL && sq->sq_onext == NULL &&
2586*7c478bd9Sstevel@tonic-gate 		    sq->sq_oprev == NULL);
2587*7c478bd9Sstevel@tonic-gate 		rq->q_syncq = wq->q_syncq = sq;
2588*7c478bd9Sstevel@tonic-gate 	}
2589*7c478bd9Sstevel@tonic-gate 	if (qflag & QMTOUTPERIM) {
2590*7c478bd9Sstevel@tonic-gate 		outer = dmp->dm_sq;
2591*7c478bd9Sstevel@tonic-gate 
2592*7c478bd9Sstevel@tonic-gate 		ASSERT(outer->sq_outer == NULL);
2593*7c478bd9Sstevel@tonic-gate 		outer_insert(outer, rq->q_syncq);
2594*7c478bd9Sstevel@tonic-gate 		if (wq->q_syncq != rq->q_syncq)
2595*7c478bd9Sstevel@tonic-gate 			outer_insert(outer, wq->q_syncq);
2596*7c478bd9Sstevel@tonic-gate 	}
2597*7c478bd9Sstevel@tonic-gate 	ASSERT((rq->q_syncq->sq_flags & SQ_TYPES_IN_FLAGS) ==
2598*7c478bd9Sstevel@tonic-gate 		(rq->q_syncq->sq_type & SQ_TYPES_IN_FLAGS));
2599*7c478bd9Sstevel@tonic-gate 	ASSERT((wq->q_syncq->sq_flags & SQ_TYPES_IN_FLAGS) ==
2600*7c478bd9Sstevel@tonic-gate 		(wq->q_syncq->sq_type & SQ_TYPES_IN_FLAGS));
2601*7c478bd9Sstevel@tonic-gate 	ASSERT((rq->q_flag & QMT_TYPEMASK) == (qflag & QMT_TYPEMASK));
2602*7c478bd9Sstevel@tonic-gate 
2603*7c478bd9Sstevel@tonic-gate 	/*
2604*7c478bd9Sstevel@tonic-gate 	 * Initialize struio() types.
2605*7c478bd9Sstevel@tonic-gate 	 */
2606*7c478bd9Sstevel@tonic-gate 	rq->q_struiot =
2607*7c478bd9Sstevel@tonic-gate 	    (rq->q_flag & QSYNCSTR) ? rinit->qi_struiot : STRUIOT_NONE;
2608*7c478bd9Sstevel@tonic-gate 	wq->q_struiot =
2609*7c478bd9Sstevel@tonic-gate 	    (wq->q_flag & QSYNCSTR) ? winit->qi_struiot : STRUIOT_NONE;
2610*7c478bd9Sstevel@tonic-gate }
2611*7c478bd9Sstevel@tonic-gate 
2612*7c478bd9Sstevel@tonic-gate perdm_t *
2613*7c478bd9Sstevel@tonic-gate hold_dm(struct streamtab *str, uint32_t qflag, uint32_t sqtype)
2614*7c478bd9Sstevel@tonic-gate {
2615*7c478bd9Sstevel@tonic-gate 	syncq_t	*sq;
2616*7c478bd9Sstevel@tonic-gate 	perdm_t	**pp;
2617*7c478bd9Sstevel@tonic-gate 	perdm_t	*p;
2618*7c478bd9Sstevel@tonic-gate 	perdm_t	*dmp;
2619*7c478bd9Sstevel@tonic-gate 
2620*7c478bd9Sstevel@tonic-gate 	ASSERT(str != NULL);
2621*7c478bd9Sstevel@tonic-gate 	ASSERT(qflag & (QPERMOD | QMTOUTPERIM));
2622*7c478bd9Sstevel@tonic-gate 
2623*7c478bd9Sstevel@tonic-gate 	rw_enter(&perdm_rwlock, RW_READER);
2624*7c478bd9Sstevel@tonic-gate 	for (p = perdm_list; p != NULL; p = p->dm_next) {
2625*7c478bd9Sstevel@tonic-gate 		if (p->dm_str == str) {	/* found one */
2626*7c478bd9Sstevel@tonic-gate 			atomic_add_32(&(p->dm_ref), 1);
2627*7c478bd9Sstevel@tonic-gate 			rw_exit(&perdm_rwlock);
2628*7c478bd9Sstevel@tonic-gate 			return (p);
2629*7c478bd9Sstevel@tonic-gate 		}
2630*7c478bd9Sstevel@tonic-gate 	}
2631*7c478bd9Sstevel@tonic-gate 	rw_exit(&perdm_rwlock);
2632*7c478bd9Sstevel@tonic-gate 
2633*7c478bd9Sstevel@tonic-gate 	sq = new_syncq();
2634*7c478bd9Sstevel@tonic-gate 	if (qflag & QPERMOD) {
2635*7c478bd9Sstevel@tonic-gate 		sq->sq_type = sqtype | SQ_PERMOD;
2636*7c478bd9Sstevel@tonic-gate 		sq->sq_flags = sqtype & SQ_TYPES_IN_FLAGS;
2637*7c478bd9Sstevel@tonic-gate 	} else {
2638*7c478bd9Sstevel@tonic-gate 		ASSERT(qflag & QMTOUTPERIM);
2639*7c478bd9Sstevel@tonic-gate 		sq->sq_onext = sq->sq_oprev = sq;
2640*7c478bd9Sstevel@tonic-gate 	}
2641*7c478bd9Sstevel@tonic-gate 
2642*7c478bd9Sstevel@tonic-gate 	dmp = kmem_alloc(sizeof (perdm_t), KM_SLEEP);
2643*7c478bd9Sstevel@tonic-gate 	dmp->dm_sq = sq;
2644*7c478bd9Sstevel@tonic-gate 	dmp->dm_str = str;
2645*7c478bd9Sstevel@tonic-gate 	dmp->dm_ref = 1;
2646*7c478bd9Sstevel@tonic-gate 	dmp->dm_next = NULL;
2647*7c478bd9Sstevel@tonic-gate 
2648*7c478bd9Sstevel@tonic-gate 	rw_enter(&perdm_rwlock, RW_WRITER);
2649*7c478bd9Sstevel@tonic-gate 	for (pp = &perdm_list; (p = *pp) != NULL; pp = &(p->dm_next)) {
2650*7c478bd9Sstevel@tonic-gate 		if (p->dm_str == str) {	/* already present */
2651*7c478bd9Sstevel@tonic-gate 			p->dm_ref++;
2652*7c478bd9Sstevel@tonic-gate 			rw_exit(&perdm_rwlock);
2653*7c478bd9Sstevel@tonic-gate 			free_syncq(sq);
2654*7c478bd9Sstevel@tonic-gate 			kmem_free(dmp, sizeof (perdm_t));
2655*7c478bd9Sstevel@tonic-gate 			return (p);
2656*7c478bd9Sstevel@tonic-gate 		}
2657*7c478bd9Sstevel@tonic-gate 	}
2658*7c478bd9Sstevel@tonic-gate 
2659*7c478bd9Sstevel@tonic-gate 	*pp = dmp;
2660*7c478bd9Sstevel@tonic-gate 	rw_exit(&perdm_rwlock);
2661*7c478bd9Sstevel@tonic-gate 	return (dmp);
2662*7c478bd9Sstevel@tonic-gate }
2663*7c478bd9Sstevel@tonic-gate 
2664*7c478bd9Sstevel@tonic-gate void
2665*7c478bd9Sstevel@tonic-gate rele_dm(perdm_t *dmp)
2666*7c478bd9Sstevel@tonic-gate {
2667*7c478bd9Sstevel@tonic-gate 	perdm_t **pp;
2668*7c478bd9Sstevel@tonic-gate 	perdm_t *p;
2669*7c478bd9Sstevel@tonic-gate 
2670*7c478bd9Sstevel@tonic-gate 	rw_enter(&perdm_rwlock, RW_WRITER);
2671*7c478bd9Sstevel@tonic-gate 	ASSERT(dmp->dm_ref > 0);
2672*7c478bd9Sstevel@tonic-gate 
2673*7c478bd9Sstevel@tonic-gate 	if (--dmp->dm_ref > 0) {
2674*7c478bd9Sstevel@tonic-gate 		rw_exit(&perdm_rwlock);
2675*7c478bd9Sstevel@tonic-gate 		return;
2676*7c478bd9Sstevel@tonic-gate 	}
2677*7c478bd9Sstevel@tonic-gate 
2678*7c478bd9Sstevel@tonic-gate 	for (pp = &perdm_list; (p = *pp) != NULL; pp = &(p->dm_next))
2679*7c478bd9Sstevel@tonic-gate 		if (p == dmp)
2680*7c478bd9Sstevel@tonic-gate 			break;
2681*7c478bd9Sstevel@tonic-gate 	ASSERT(p == dmp);
2682*7c478bd9Sstevel@tonic-gate 	*pp = p->dm_next;
2683*7c478bd9Sstevel@tonic-gate 	rw_exit(&perdm_rwlock);
2684*7c478bd9Sstevel@tonic-gate 
2685*7c478bd9Sstevel@tonic-gate 	/*
2686*7c478bd9Sstevel@tonic-gate 	 * Wait for any background processing that relies on the
2687*7c478bd9Sstevel@tonic-gate 	 * syncq to complete before it is freed.
2688*7c478bd9Sstevel@tonic-gate 	 */
2689*7c478bd9Sstevel@tonic-gate 	wait_sq_svc(p->dm_sq);
2690*7c478bd9Sstevel@tonic-gate 	free_syncq(p->dm_sq);
2691*7c478bd9Sstevel@tonic-gate 	kmem_free(p, sizeof (perdm_t));
2692*7c478bd9Sstevel@tonic-gate }
2693*7c478bd9Sstevel@tonic-gate 
2694*7c478bd9Sstevel@tonic-gate /*
2695*7c478bd9Sstevel@tonic-gate  * Make a protocol message given control and data buffers.
2696*7c478bd9Sstevel@tonic-gate  * n.b., this can block; be careful of what locks you hold when calling it.
2697*7c478bd9Sstevel@tonic-gate  *
2698*7c478bd9Sstevel@tonic-gate  * If sd_maxblk is less than *iosize this routine can fail part way through
2699*7c478bd9Sstevel@tonic-gate  * (due to an allocation failure). In this case on return *iosize will contain
2700*7c478bd9Sstevel@tonic-gate  * the amount that was consumed. Otherwise *iosize will not be modified
2701*7c478bd9Sstevel@tonic-gate  * i.e. it will contain the amount that was consumed.
2702*7c478bd9Sstevel@tonic-gate  */
2703*7c478bd9Sstevel@tonic-gate int
2704*7c478bd9Sstevel@tonic-gate strmakemsg(
2705*7c478bd9Sstevel@tonic-gate 	struct strbuf *mctl,
2706*7c478bd9Sstevel@tonic-gate 	ssize_t *iosize,
2707*7c478bd9Sstevel@tonic-gate 	struct uio *uiop,
2708*7c478bd9Sstevel@tonic-gate 	stdata_t *stp,
2709*7c478bd9Sstevel@tonic-gate 	int32_t flag,
2710*7c478bd9Sstevel@tonic-gate 	mblk_t **mpp)
2711*7c478bd9Sstevel@tonic-gate {
2712*7c478bd9Sstevel@tonic-gate 	mblk_t *mpctl = NULL;
2713*7c478bd9Sstevel@tonic-gate 	mblk_t *mpdata = NULL;
2714*7c478bd9Sstevel@tonic-gate 	int error;
2715*7c478bd9Sstevel@tonic-gate 
2716*7c478bd9Sstevel@tonic-gate 	ASSERT(uiop != NULL);
2717*7c478bd9Sstevel@tonic-gate 
2718*7c478bd9Sstevel@tonic-gate 	*mpp = NULL;
2719*7c478bd9Sstevel@tonic-gate 	/* Create control part, if any */
2720*7c478bd9Sstevel@tonic-gate 	if ((mctl != NULL) && (mctl->len >= 0)) {
2721*7c478bd9Sstevel@tonic-gate 		error = strmakectl(mctl, flag, uiop->uio_fmode, &mpctl);
2722*7c478bd9Sstevel@tonic-gate 		if (error)
2723*7c478bd9Sstevel@tonic-gate 			return (error);
2724*7c478bd9Sstevel@tonic-gate 	}
2725*7c478bd9Sstevel@tonic-gate 	/* Create data part, if any */
2726*7c478bd9Sstevel@tonic-gate 	if (*iosize >= 0) {
2727*7c478bd9Sstevel@tonic-gate 		error = strmakedata(iosize, uiop, stp, flag, &mpdata);
2728*7c478bd9Sstevel@tonic-gate 		if (error) {
2729*7c478bd9Sstevel@tonic-gate 			freemsg(mpctl);
2730*7c478bd9Sstevel@tonic-gate 			return (error);
2731*7c478bd9Sstevel@tonic-gate 		}
2732*7c478bd9Sstevel@tonic-gate 	}
2733*7c478bd9Sstevel@tonic-gate 	if (mpctl != NULL) {
2734*7c478bd9Sstevel@tonic-gate 		if (mpdata != NULL)
2735*7c478bd9Sstevel@tonic-gate 			linkb(mpctl, mpdata);
2736*7c478bd9Sstevel@tonic-gate 		*mpp = mpctl;
2737*7c478bd9Sstevel@tonic-gate 	} else {
2738*7c478bd9Sstevel@tonic-gate 		*mpp = mpdata;
2739*7c478bd9Sstevel@tonic-gate 	}
2740*7c478bd9Sstevel@tonic-gate 	return (0);
2741*7c478bd9Sstevel@tonic-gate }
2742*7c478bd9Sstevel@tonic-gate 
2743*7c478bd9Sstevel@tonic-gate /*
2744*7c478bd9Sstevel@tonic-gate  * Make the control part of a protocol message given a control buffer.
2745*7c478bd9Sstevel@tonic-gate  * n.b., this can block; be careful of what locks you hold when calling it.
2746*7c478bd9Sstevel@tonic-gate  */
2747*7c478bd9Sstevel@tonic-gate int
2748*7c478bd9Sstevel@tonic-gate strmakectl(
2749*7c478bd9Sstevel@tonic-gate 	struct strbuf *mctl,
2750*7c478bd9Sstevel@tonic-gate 	int32_t flag,
2751*7c478bd9Sstevel@tonic-gate 	int32_t fflag,
2752*7c478bd9Sstevel@tonic-gate 	mblk_t **mpp)
2753*7c478bd9Sstevel@tonic-gate {
2754*7c478bd9Sstevel@tonic-gate 	mblk_t *bp = NULL;
2755*7c478bd9Sstevel@tonic-gate 	unsigned char msgtype;
2756*7c478bd9Sstevel@tonic-gate 	int error = 0;
2757*7c478bd9Sstevel@tonic-gate 
2758*7c478bd9Sstevel@tonic-gate 	*mpp = NULL;
2759*7c478bd9Sstevel@tonic-gate 	/*
2760*7c478bd9Sstevel@tonic-gate 	 * Create control part of message, if any.
2761*7c478bd9Sstevel@tonic-gate 	 */
2762*7c478bd9Sstevel@tonic-gate 	if ((mctl != NULL) && (mctl->len >= 0)) {
2763*7c478bd9Sstevel@tonic-gate 		caddr_t base;
2764*7c478bd9Sstevel@tonic-gate 		int ctlcount;
2765*7c478bd9Sstevel@tonic-gate 		int allocsz;
2766*7c478bd9Sstevel@tonic-gate 
2767*7c478bd9Sstevel@tonic-gate 		if (flag & RS_HIPRI)
2768*7c478bd9Sstevel@tonic-gate 			msgtype = M_PCPROTO;
2769*7c478bd9Sstevel@tonic-gate 		else
2770*7c478bd9Sstevel@tonic-gate 			msgtype = M_PROTO;
2771*7c478bd9Sstevel@tonic-gate 
2772*7c478bd9Sstevel@tonic-gate 		ctlcount = mctl->len;
2773*7c478bd9Sstevel@tonic-gate 		base = mctl->buf;
2774*7c478bd9Sstevel@tonic-gate 
2775*7c478bd9Sstevel@tonic-gate 		/*
2776*7c478bd9Sstevel@tonic-gate 		 * Give modules a better chance to reuse M_PROTO/M_PCPROTO
2777*7c478bd9Sstevel@tonic-gate 		 * blocks by increasing the size to something more usable.
2778*7c478bd9Sstevel@tonic-gate 		 */
2779*7c478bd9Sstevel@tonic-gate 		allocsz = MAX(ctlcount, 64);
2780*7c478bd9Sstevel@tonic-gate 
2781*7c478bd9Sstevel@tonic-gate 		/*
2782*7c478bd9Sstevel@tonic-gate 		 * Range checking has already been done; simply try
2783*7c478bd9Sstevel@tonic-gate 		 * to allocate a message block for the ctl part.
2784*7c478bd9Sstevel@tonic-gate 		 */
2785*7c478bd9Sstevel@tonic-gate 		while (!(bp = allocb(allocsz, BPRI_MED))) {
2786*7c478bd9Sstevel@tonic-gate 			if (fflag & (FNDELAY|FNONBLOCK))
2787*7c478bd9Sstevel@tonic-gate 				return (EAGAIN);
2788*7c478bd9Sstevel@tonic-gate 			if (error = strwaitbuf(allocsz, BPRI_MED))
2789*7c478bd9Sstevel@tonic-gate 				return (error);
2790*7c478bd9Sstevel@tonic-gate 		}
2791*7c478bd9Sstevel@tonic-gate 
2792*7c478bd9Sstevel@tonic-gate 		bp->b_datap->db_type = msgtype;
2793*7c478bd9Sstevel@tonic-gate 		if (copyin(base, bp->b_wptr, ctlcount)) {
2794*7c478bd9Sstevel@tonic-gate 			freeb(bp);
2795*7c478bd9Sstevel@tonic-gate 			return (EFAULT);
2796*7c478bd9Sstevel@tonic-gate 		}
2797*7c478bd9Sstevel@tonic-gate 		bp->b_wptr += ctlcount;
2798*7c478bd9Sstevel@tonic-gate 	}
2799*7c478bd9Sstevel@tonic-gate 	*mpp = bp;
2800*7c478bd9Sstevel@tonic-gate 	return (0);
2801*7c478bd9Sstevel@tonic-gate }
2802*7c478bd9Sstevel@tonic-gate 
2803*7c478bd9Sstevel@tonic-gate /*
2804*7c478bd9Sstevel@tonic-gate  * Make a protocol message given data buffers.
2805*7c478bd9Sstevel@tonic-gate  * n.b., this can block; be careful of what locks you hold when calling it.
2806*7c478bd9Sstevel@tonic-gate  *
2807*7c478bd9Sstevel@tonic-gate  * If sd_maxblk is less than *iosize this routine can fail part way through
2808*7c478bd9Sstevel@tonic-gate  * (due to an allocation failure). In this case on return *iosize will contain
2809*7c478bd9Sstevel@tonic-gate  * the amount that was consumed. Otherwise *iosize will not be modified
2810*7c478bd9Sstevel@tonic-gate  * i.e. it will contain the amount that was consumed.
2811*7c478bd9Sstevel@tonic-gate  */
2812*7c478bd9Sstevel@tonic-gate int
2813*7c478bd9Sstevel@tonic-gate strmakedata(
2814*7c478bd9Sstevel@tonic-gate 	ssize_t   *iosize,
2815*7c478bd9Sstevel@tonic-gate 	struct uio *uiop,
2816*7c478bd9Sstevel@tonic-gate 	stdata_t *stp,
2817*7c478bd9Sstevel@tonic-gate 	int32_t flag,
2818*7c478bd9Sstevel@tonic-gate 	mblk_t **mpp)
2819*7c478bd9Sstevel@tonic-gate {
2820*7c478bd9Sstevel@tonic-gate 	mblk_t *mp = NULL;
2821*7c478bd9Sstevel@tonic-gate 	mblk_t *bp;
2822*7c478bd9Sstevel@tonic-gate 	int wroff = (int)stp->sd_wroff;
2823*7c478bd9Sstevel@tonic-gate 	int error = 0;
2824*7c478bd9Sstevel@tonic-gate 	ssize_t maxblk;
2825*7c478bd9Sstevel@tonic-gate 	ssize_t count = *iosize;
2826*7c478bd9Sstevel@tonic-gate 	cred_t *cr = CRED();
2827*7c478bd9Sstevel@tonic-gate 
2828*7c478bd9Sstevel@tonic-gate 	*mpp = NULL;
2829*7c478bd9Sstevel@tonic-gate 	if (count < 0)
2830*7c478bd9Sstevel@tonic-gate 		return (0);
2831*7c478bd9Sstevel@tonic-gate 
2832*7c478bd9Sstevel@tonic-gate 	maxblk = stp->sd_maxblk;
2833*7c478bd9Sstevel@tonic-gate 	if (maxblk == INFPSZ)
2834*7c478bd9Sstevel@tonic-gate 		maxblk = count;
2835*7c478bd9Sstevel@tonic-gate 
2836*7c478bd9Sstevel@tonic-gate 	/*
2837*7c478bd9Sstevel@tonic-gate 	 * Create data part of message, if any.
2838*7c478bd9Sstevel@tonic-gate 	 */
2839*7c478bd9Sstevel@tonic-gate 	do {
2840*7c478bd9Sstevel@tonic-gate 		ssize_t size;
2841*7c478bd9Sstevel@tonic-gate 		dblk_t  *dp;
2842*7c478bd9Sstevel@tonic-gate 
2843*7c478bd9Sstevel@tonic-gate 		ASSERT(uiop);
2844*7c478bd9Sstevel@tonic-gate 
2845*7c478bd9Sstevel@tonic-gate 		size = MIN(count, maxblk);
2846*7c478bd9Sstevel@tonic-gate 
2847*7c478bd9Sstevel@tonic-gate 		while ((bp = allocb_cred(size + wroff, cr)) == NULL) {
2848*7c478bd9Sstevel@tonic-gate 			error = EAGAIN;
2849*7c478bd9Sstevel@tonic-gate 			if ((uiop->uio_fmode & (FNDELAY|FNONBLOCK)) ||
2850*7c478bd9Sstevel@tonic-gate 			    (error = strwaitbuf(size + wroff, BPRI_MED)) != 0) {
2851*7c478bd9Sstevel@tonic-gate 				if (count == *iosize) {
2852*7c478bd9Sstevel@tonic-gate 					freemsg(mp);
2853*7c478bd9Sstevel@tonic-gate 					return (error);
2854*7c478bd9Sstevel@tonic-gate 				} else {
2855*7c478bd9Sstevel@tonic-gate 					*iosize -= count;
2856*7c478bd9Sstevel@tonic-gate 					*mpp = mp;
2857*7c478bd9Sstevel@tonic-gate 					return (0);
2858*7c478bd9Sstevel@tonic-gate 				}
2859*7c478bd9Sstevel@tonic-gate 			}
2860*7c478bd9Sstevel@tonic-gate 		}
2861*7c478bd9Sstevel@tonic-gate 		dp = bp->b_datap;
2862*7c478bd9Sstevel@tonic-gate 		dp->db_cpid = curproc->p_pid;
2863*7c478bd9Sstevel@tonic-gate 		ASSERT(wroff <= dp->db_lim - bp->b_wptr);
2864*7c478bd9Sstevel@tonic-gate 		bp->b_wptr = bp->b_rptr = bp->b_rptr + wroff;
2865*7c478bd9Sstevel@tonic-gate 
2866*7c478bd9Sstevel@tonic-gate 		if (flag & STRUIO_POSTPONE) {
2867*7c478bd9Sstevel@tonic-gate 			/*
2868*7c478bd9Sstevel@tonic-gate 			 * Setup the stream uio portion of the
2869*7c478bd9Sstevel@tonic-gate 			 * dblk for subsequent use by struioget().
2870*7c478bd9Sstevel@tonic-gate 			 */
2871*7c478bd9Sstevel@tonic-gate 			dp->db_struioflag = STRUIO_SPEC;
2872*7c478bd9Sstevel@tonic-gate 			dp->db_cksumstart = 0;
2873*7c478bd9Sstevel@tonic-gate 			dp->db_cksumstuff = 0;
2874*7c478bd9Sstevel@tonic-gate 			dp->db_cksumend = size;
2875*7c478bd9Sstevel@tonic-gate 			*(long long *)dp->db_struioun.data = 0ll;
2876*7c478bd9Sstevel@tonic-gate 		} else {
2877*7c478bd9Sstevel@tonic-gate 			if (stp->sd_copyflag & STRCOPYCACHED)
2878*7c478bd9Sstevel@tonic-gate 				uiop->uio_extflg |= UIO_COPY_CACHED;
2879*7c478bd9Sstevel@tonic-gate 
2880*7c478bd9Sstevel@tonic-gate 			if (size != 0) {
2881*7c478bd9Sstevel@tonic-gate 				error = uiomove(bp->b_wptr, size, UIO_WRITE,
2882*7c478bd9Sstevel@tonic-gate 				    uiop);
2883*7c478bd9Sstevel@tonic-gate 				if (error != 0) {
2884*7c478bd9Sstevel@tonic-gate 					freeb(bp);
2885*7c478bd9Sstevel@tonic-gate 					freemsg(mp);
2886*7c478bd9Sstevel@tonic-gate 					return (error);
2887*7c478bd9Sstevel@tonic-gate 				}
2888*7c478bd9Sstevel@tonic-gate 			}
2889*7c478bd9Sstevel@tonic-gate 		}
2890*7c478bd9Sstevel@tonic-gate 
2891*7c478bd9Sstevel@tonic-gate 		bp->b_wptr += size;
2892*7c478bd9Sstevel@tonic-gate 		count -= size;
2893*7c478bd9Sstevel@tonic-gate 
2894*7c478bd9Sstevel@tonic-gate 		if (mp == NULL)
2895*7c478bd9Sstevel@tonic-gate 			mp = bp;
2896*7c478bd9Sstevel@tonic-gate 		else
2897*7c478bd9Sstevel@tonic-gate 			linkb(mp, bp);
2898*7c478bd9Sstevel@tonic-gate 	} while (count > 0);
2899*7c478bd9Sstevel@tonic-gate 
2900*7c478bd9Sstevel@tonic-gate 	*mpp = mp;
2901*7c478bd9Sstevel@tonic-gate 	return (0);
2902*7c478bd9Sstevel@tonic-gate }
2903*7c478bd9Sstevel@tonic-gate 
2904*7c478bd9Sstevel@tonic-gate /*
2905*7c478bd9Sstevel@tonic-gate  * Wait for a buffer to become available. Return non-zero errno
2906*7c478bd9Sstevel@tonic-gate  * if not able to wait, 0 if buffer is probably there.
2907*7c478bd9Sstevel@tonic-gate  */
2908*7c478bd9Sstevel@tonic-gate int
2909*7c478bd9Sstevel@tonic-gate strwaitbuf(size_t size, int pri)
2910*7c478bd9Sstevel@tonic-gate {
2911*7c478bd9Sstevel@tonic-gate 	bufcall_id_t id;
2912*7c478bd9Sstevel@tonic-gate 
2913*7c478bd9Sstevel@tonic-gate 	mutex_enter(&bcall_monitor);
2914*7c478bd9Sstevel@tonic-gate 	if ((id = bufcall(size, pri, (void (*)(void *))cv_broadcast,
2915*7c478bd9Sstevel@tonic-gate 	    &ttoproc(curthread)->p_flag_cv)) == 0) {
2916*7c478bd9Sstevel@tonic-gate 		mutex_exit(&bcall_monitor);
2917*7c478bd9Sstevel@tonic-gate 		return (ENOSR);
2918*7c478bd9Sstevel@tonic-gate 	}
2919*7c478bd9Sstevel@tonic-gate 	if (!cv_wait_sig(&(ttoproc(curthread)->p_flag_cv), &bcall_monitor)) {
2920*7c478bd9Sstevel@tonic-gate 		unbufcall(id);
2921*7c478bd9Sstevel@tonic-gate 		mutex_exit(&bcall_monitor);
2922*7c478bd9Sstevel@tonic-gate 		return (EINTR);
2923*7c478bd9Sstevel@tonic-gate 	}
2924*7c478bd9Sstevel@tonic-gate 	unbufcall(id);
2925*7c478bd9Sstevel@tonic-gate 	mutex_exit(&bcall_monitor);
2926*7c478bd9Sstevel@tonic-gate 	return (0);
2927*7c478bd9Sstevel@tonic-gate }
2928*7c478bd9Sstevel@tonic-gate 
2929*7c478bd9Sstevel@tonic-gate /*
2930*7c478bd9Sstevel@tonic-gate  * This function waits for a read or write event to happen on a stream.
2931*7c478bd9Sstevel@tonic-gate  * fmode can specify FNDELAY and/or FNONBLOCK.
2932*7c478bd9Sstevel@tonic-gate  * The timeout is in ms with -1 meaning infinite.
2933*7c478bd9Sstevel@tonic-gate  * The flag values work as follows:
2934*7c478bd9Sstevel@tonic-gate  *	READWAIT	Check for read side errors, send M_READ
2935*7c478bd9Sstevel@tonic-gate  *	GETWAIT		Check for read side errors, no M_READ
2936*7c478bd9Sstevel@tonic-gate  *	WRITEWAIT	Check for write side errors.
2937*7c478bd9Sstevel@tonic-gate  *	NOINTR		Do not return error if nonblocking or timeout.
2938*7c478bd9Sstevel@tonic-gate  * 	STR_NOERROR	Ignore all errors except STPLEX.
2939*7c478bd9Sstevel@tonic-gate  *	STR_NOSIG	Ignore/hold signals during the duration of the call.
2940*7c478bd9Sstevel@tonic-gate  *	STR_PEEK	Pass through the strgeterr().
2941*7c478bd9Sstevel@tonic-gate  */
2942*7c478bd9Sstevel@tonic-gate int
2943*7c478bd9Sstevel@tonic-gate strwaitq(stdata_t *stp, int flag, ssize_t count, int fmode, clock_t timout,
2944*7c478bd9Sstevel@tonic-gate     int *done)
2945*7c478bd9Sstevel@tonic-gate {
2946*7c478bd9Sstevel@tonic-gate 	int slpflg, errs;
2947*7c478bd9Sstevel@tonic-gate 	int error;
2948*7c478bd9Sstevel@tonic-gate 	kcondvar_t *sleepon;
2949*7c478bd9Sstevel@tonic-gate 	mblk_t *mp;
2950*7c478bd9Sstevel@tonic-gate 	ssize_t *rd_count;
2951*7c478bd9Sstevel@tonic-gate 	clock_t rval;
2952*7c478bd9Sstevel@tonic-gate 
2953*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&stp->sd_lock));
2954*7c478bd9Sstevel@tonic-gate 	if ((flag & READWAIT) || (flag & GETWAIT)) {
2955*7c478bd9Sstevel@tonic-gate 		slpflg = RSLEEP;
2956*7c478bd9Sstevel@tonic-gate 		sleepon = &_RD(stp->sd_wrq)->q_wait;
2957*7c478bd9Sstevel@tonic-gate 		errs = STRDERR|STPLEX;
2958*7c478bd9Sstevel@tonic-gate 	} else {
2959*7c478bd9Sstevel@tonic-gate 		slpflg = WSLEEP;
2960*7c478bd9Sstevel@tonic-gate 		sleepon = &stp->sd_wrq->q_wait;
2961*7c478bd9Sstevel@tonic-gate 		errs = STWRERR|STRHUP|STPLEX;
2962*7c478bd9Sstevel@tonic-gate 	}
2963*7c478bd9Sstevel@tonic-gate 	if (flag & STR_NOERROR)
2964*7c478bd9Sstevel@tonic-gate 		errs = STPLEX;
2965*7c478bd9Sstevel@tonic-gate 
2966*7c478bd9Sstevel@tonic-gate 	if (stp->sd_wakeq & slpflg) {
2967*7c478bd9Sstevel@tonic-gate 		/*
2968*7c478bd9Sstevel@tonic-gate 		 * A strwakeq() is pending, no need to sleep.
2969*7c478bd9Sstevel@tonic-gate 		 */
2970*7c478bd9Sstevel@tonic-gate 		stp->sd_wakeq &= ~slpflg;
2971*7c478bd9Sstevel@tonic-gate 		*done = 0;
2972*7c478bd9Sstevel@tonic-gate 		return (0);
2973*7c478bd9Sstevel@tonic-gate 	}
2974*7c478bd9Sstevel@tonic-gate 
2975*7c478bd9Sstevel@tonic-gate 	if (fmode & (FNDELAY|FNONBLOCK)) {
2976*7c478bd9Sstevel@tonic-gate 		if (!(flag & NOINTR))
2977*7c478bd9Sstevel@tonic-gate 			error = EAGAIN;
2978*7c478bd9Sstevel@tonic-gate 		else
2979*7c478bd9Sstevel@tonic-gate 			error = 0;
2980*7c478bd9Sstevel@tonic-gate 		*done = 1;
2981*7c478bd9Sstevel@tonic-gate 		return (error);
2982*7c478bd9Sstevel@tonic-gate 	}
2983*7c478bd9Sstevel@tonic-gate 
2984*7c478bd9Sstevel@tonic-gate 	if (stp->sd_flag & errs) {
2985*7c478bd9Sstevel@tonic-gate 		/*
2986*7c478bd9Sstevel@tonic-gate 		 * Check for errors before going to sleep since the
2987*7c478bd9Sstevel@tonic-gate 		 * caller might not have checked this while holding
2988*7c478bd9Sstevel@tonic-gate 		 * sd_lock.
2989*7c478bd9Sstevel@tonic-gate 		 */
2990*7c478bd9Sstevel@tonic-gate 		error = strgeterr(stp, errs, (flag & STR_PEEK));
2991*7c478bd9Sstevel@tonic-gate 		if (error != 0) {
2992*7c478bd9Sstevel@tonic-gate 			*done = 1;
2993*7c478bd9Sstevel@tonic-gate 			return (error);
2994*7c478bd9Sstevel@tonic-gate 		}
2995*7c478bd9Sstevel@tonic-gate 	}
2996*7c478bd9Sstevel@tonic-gate 
2997*7c478bd9Sstevel@tonic-gate 	/*
2998*7c478bd9Sstevel@tonic-gate 	 * If any module downstream has requested read notification
2999*7c478bd9Sstevel@tonic-gate 	 * by setting SNDMREAD flag using M_SETOPTS, send a message
3000*7c478bd9Sstevel@tonic-gate 	 * down stream.
3001*7c478bd9Sstevel@tonic-gate 	 */
3002*7c478bd9Sstevel@tonic-gate 	if ((flag & READWAIT) && (stp->sd_flag & SNDMREAD)) {
3003*7c478bd9Sstevel@tonic-gate 		mutex_exit(&stp->sd_lock);
3004*7c478bd9Sstevel@tonic-gate 		if (!(mp = allocb_wait(sizeof (ssize_t), BPRI_MED,
3005*7c478bd9Sstevel@tonic-gate 		    (flag & STR_NOSIG), &error))) {
3006*7c478bd9Sstevel@tonic-gate 			mutex_enter(&stp->sd_lock);
3007*7c478bd9Sstevel@tonic-gate 			*done = 1;
3008*7c478bd9Sstevel@tonic-gate 			return (error);
3009*7c478bd9Sstevel@tonic-gate 		}
3010*7c478bd9Sstevel@tonic-gate 		mp->b_datap->db_type = M_READ;
3011*7c478bd9Sstevel@tonic-gate 		rd_count = (ssize_t *)mp->b_wptr;
3012*7c478bd9Sstevel@tonic-gate 		*rd_count = count;
3013*7c478bd9Sstevel@tonic-gate 		mp->b_wptr += sizeof (ssize_t);
3014*7c478bd9Sstevel@tonic-gate 		/*
3015*7c478bd9Sstevel@tonic-gate 		 * Send the number of bytes requested by the
3016*7c478bd9Sstevel@tonic-gate 		 * read as the argument to M_READ.
3017*7c478bd9Sstevel@tonic-gate 		 */
3018*7c478bd9Sstevel@tonic-gate 		stream_willservice(stp);
3019*7c478bd9Sstevel@tonic-gate 		putnext(stp->sd_wrq, mp);
3020*7c478bd9Sstevel@tonic-gate 		stream_runservice(stp);
3021*7c478bd9Sstevel@tonic-gate 		mutex_enter(&stp->sd_lock);
3022*7c478bd9Sstevel@tonic-gate 
3023*7c478bd9Sstevel@tonic-gate 		/*
3024*7c478bd9Sstevel@tonic-gate 		 * If any data arrived due to inline processing
3025*7c478bd9Sstevel@tonic-gate 		 * of putnext(), don't sleep.
3026*7c478bd9Sstevel@tonic-gate 		 */
3027*7c478bd9Sstevel@tonic-gate 		if (_RD(stp->sd_wrq)->q_first != NULL) {
3028*7c478bd9Sstevel@tonic-gate 			*done = 0;
3029*7c478bd9Sstevel@tonic-gate 			return (0);
3030*7c478bd9Sstevel@tonic-gate 		}
3031*7c478bd9Sstevel@tonic-gate 	}
3032*7c478bd9Sstevel@tonic-gate 
3033*7c478bd9Sstevel@tonic-gate 	stp->sd_flag |= slpflg;
3034*7c478bd9Sstevel@tonic-gate 	TRACE_5(TR_FAC_STREAMS_FR, TR_STRWAITQ_WAIT2,
3035*7c478bd9Sstevel@tonic-gate 		"strwaitq sleeps (2):%p, %X, %lX, %X, %p",
3036*7c478bd9Sstevel@tonic-gate 		stp, flag, count, fmode, done);
3037*7c478bd9Sstevel@tonic-gate 
3038*7c478bd9Sstevel@tonic-gate 	rval = str_cv_wait(sleepon, &stp->sd_lock, timout, flag & STR_NOSIG);
3039*7c478bd9Sstevel@tonic-gate 	if (rval > 0) {
3040*7c478bd9Sstevel@tonic-gate 		/* EMPTY */
3041*7c478bd9Sstevel@tonic-gate 		TRACE_5(TR_FAC_STREAMS_FR, TR_STRWAITQ_WAKE2,
3042*7c478bd9Sstevel@tonic-gate 			"strwaitq awakes(2):%X, %X, %X, %X, %X",
3043*7c478bd9Sstevel@tonic-gate 			stp, flag, count, fmode, done);
3044*7c478bd9Sstevel@tonic-gate 	} else if (rval == 0) {
3045*7c478bd9Sstevel@tonic-gate 		TRACE_5(TR_FAC_STREAMS_FR, TR_STRWAITQ_INTR2,
3046*7c478bd9Sstevel@tonic-gate 			"strwaitq interrupt #2:%p, %X, %lX, %X, %p",
3047*7c478bd9Sstevel@tonic-gate 			stp, flag, count, fmode, done);
3048*7c478bd9Sstevel@tonic-gate 		stp->sd_flag &= ~slpflg;
3049*7c478bd9Sstevel@tonic-gate 		cv_broadcast(sleepon);
3050*7c478bd9Sstevel@tonic-gate 		if (!(flag & NOINTR))
3051*7c478bd9Sstevel@tonic-gate 			error = EINTR;
3052*7c478bd9Sstevel@tonic-gate 		else
3053*7c478bd9Sstevel@tonic-gate 			error = 0;
3054*7c478bd9Sstevel@tonic-gate 		*done = 1;
3055*7c478bd9Sstevel@tonic-gate 		return (error);
3056*7c478bd9Sstevel@tonic-gate 	} else {
3057*7c478bd9Sstevel@tonic-gate 		/* timeout */
3058*7c478bd9Sstevel@tonic-gate 		TRACE_5(TR_FAC_STREAMS_FR, TR_STRWAITQ_TIME,
3059*7c478bd9Sstevel@tonic-gate 			"strwaitq timeout:%p, %X, %lX, %X, %p",
3060*7c478bd9Sstevel@tonic-gate 			stp, flag, count, fmode, done);
3061*7c478bd9Sstevel@tonic-gate 		*done = 1;
3062*7c478bd9Sstevel@tonic-gate 		if (!(flag & NOINTR))
3063*7c478bd9Sstevel@tonic-gate 			return (ETIME);
3064*7c478bd9Sstevel@tonic-gate 		else
3065*7c478bd9Sstevel@tonic-gate 			return (0);
3066*7c478bd9Sstevel@tonic-gate 	}
3067*7c478bd9Sstevel@tonic-gate 	/*
3068*7c478bd9Sstevel@tonic-gate 	 * If the caller implements delayed errors (i.e. queued after data)
3069*7c478bd9Sstevel@tonic-gate 	 * we can not check for errors here since data as well as an
3070*7c478bd9Sstevel@tonic-gate 	 * error might have arrived at the stream head. We return to
3071*7c478bd9Sstevel@tonic-gate 	 * have the caller check the read queue before checking for errors.
3072*7c478bd9Sstevel@tonic-gate 	 */
3073*7c478bd9Sstevel@tonic-gate 	if ((stp->sd_flag & errs) && !(flag & STR_DELAYERR)) {
3074*7c478bd9Sstevel@tonic-gate 		error = strgeterr(stp, errs, (flag & STR_PEEK));
3075*7c478bd9Sstevel@tonic-gate 		if (error != 0) {
3076*7c478bd9Sstevel@tonic-gate 			*done = 1;
3077*7c478bd9Sstevel@tonic-gate 			return (error);
3078*7c478bd9Sstevel@tonic-gate 		}
3079*7c478bd9Sstevel@tonic-gate 	}
3080*7c478bd9Sstevel@tonic-gate 	*done = 0;
3081*7c478bd9Sstevel@tonic-gate 	return (0);
3082*7c478bd9Sstevel@tonic-gate }
3083*7c478bd9Sstevel@tonic-gate 
3084*7c478bd9Sstevel@tonic-gate /*
3085*7c478bd9Sstevel@tonic-gate  * Perform job control discipline access checks.
3086*7c478bd9Sstevel@tonic-gate  * Return 0 for success and the errno for failure.
3087*7c478bd9Sstevel@tonic-gate  */
3088*7c478bd9Sstevel@tonic-gate 
3089*7c478bd9Sstevel@tonic-gate #define	cantsend(p, t, sig) \
3090*7c478bd9Sstevel@tonic-gate 	(sigismember(&(p)->p_ignore, sig) || signal_is_blocked((t), sig))
3091*7c478bd9Sstevel@tonic-gate 
3092*7c478bd9Sstevel@tonic-gate int
3093*7c478bd9Sstevel@tonic-gate straccess(struct stdata *stp, enum jcaccess mode)
3094*7c478bd9Sstevel@tonic-gate {
3095*7c478bd9Sstevel@tonic-gate 	extern kcondvar_t lbolt_cv;	/* XXX: should be in a header file */
3096*7c478bd9Sstevel@tonic-gate 	kthread_t *t = curthread;
3097*7c478bd9Sstevel@tonic-gate 	proc_t *p = ttoproc(t);
3098*7c478bd9Sstevel@tonic-gate 	sess_t *sp;
3099*7c478bd9Sstevel@tonic-gate 
3100*7c478bd9Sstevel@tonic-gate 	if (stp->sd_sidp == NULL || stp->sd_vnode->v_type == VFIFO)
3101*7c478bd9Sstevel@tonic-gate 		return (0);
3102*7c478bd9Sstevel@tonic-gate 
3103*7c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
3104*7c478bd9Sstevel@tonic-gate 	sp = p->p_sessp;
3105*7c478bd9Sstevel@tonic-gate 
3106*7c478bd9Sstevel@tonic-gate 	for (;;) {
3107*7c478bd9Sstevel@tonic-gate 		/*
3108*7c478bd9Sstevel@tonic-gate 		 * If this is not the calling process's controlling terminal
3109*7c478bd9Sstevel@tonic-gate 		 * or if the calling process is already in the foreground
3110*7c478bd9Sstevel@tonic-gate 		 * then allow access.
3111*7c478bd9Sstevel@tonic-gate 		 */
3112*7c478bd9Sstevel@tonic-gate 		if (sp->s_dev != stp->sd_vnode->v_rdev ||
3113*7c478bd9Sstevel@tonic-gate 		    p->p_pgidp == stp->sd_pgidp) {
3114*7c478bd9Sstevel@tonic-gate 			mutex_exit(&p->p_lock);
3115*7c478bd9Sstevel@tonic-gate 			return (0);
3116*7c478bd9Sstevel@tonic-gate 		}
3117*7c478bd9Sstevel@tonic-gate 
3118*7c478bd9Sstevel@tonic-gate 		/*
3119*7c478bd9Sstevel@tonic-gate 		 * Check to see if controlling terminal has been deallocated.
3120*7c478bd9Sstevel@tonic-gate 		 */
3121*7c478bd9Sstevel@tonic-gate 		if (sp->s_vp == NULL) {
3122*7c478bd9Sstevel@tonic-gate 			if (!cantsend(p, t, SIGHUP))
3123*7c478bd9Sstevel@tonic-gate 				sigtoproc(p, t, SIGHUP);
3124*7c478bd9Sstevel@tonic-gate 			mutex_exit(&p->p_lock);
3125*7c478bd9Sstevel@tonic-gate 			return (EIO);
3126*7c478bd9Sstevel@tonic-gate 		}
3127*7c478bd9Sstevel@tonic-gate 
3128*7c478bd9Sstevel@tonic-gate 		if (mode == JCGETP) {
3129*7c478bd9Sstevel@tonic-gate 			mutex_exit(&p->p_lock);
3130*7c478bd9Sstevel@tonic-gate 			return (0);
3131*7c478bd9Sstevel@tonic-gate 		}
3132*7c478bd9Sstevel@tonic-gate 
3133*7c478bd9Sstevel@tonic-gate 		if (mode == JCREAD) {
3134*7c478bd9Sstevel@tonic-gate 			if (p->p_detached || cantsend(p, t, SIGTTIN)) {
3135*7c478bd9Sstevel@tonic-gate 				mutex_exit(&p->p_lock);
3136*7c478bd9Sstevel@tonic-gate 				return (EIO);
3137*7c478bd9Sstevel@tonic-gate 			}
3138*7c478bd9Sstevel@tonic-gate 			mutex_exit(&p->p_lock);
3139*7c478bd9Sstevel@tonic-gate 			pgsignal(p->p_pgidp, SIGTTIN);
3140*7c478bd9Sstevel@tonic-gate 			mutex_enter(&p->p_lock);
3141*7c478bd9Sstevel@tonic-gate 		} else {  /* mode == JCWRITE or JCSETP */
3142*7c478bd9Sstevel@tonic-gate 			if ((mode == JCWRITE && !(stp->sd_flag & STRTOSTOP)) ||
3143*7c478bd9Sstevel@tonic-gate 			    cantsend(p, t, SIGTTOU)) {
3144*7c478bd9Sstevel@tonic-gate 				mutex_exit(&p->p_lock);
3145*7c478bd9Sstevel@tonic-gate 				return (0);
3146*7c478bd9Sstevel@tonic-gate 			}
3147*7c478bd9Sstevel@tonic-gate 			if (p->p_detached) {
3148*7c478bd9Sstevel@tonic-gate 				mutex_exit(&p->p_lock);
3149*7c478bd9Sstevel@tonic-gate 				return (EIO);
3150*7c478bd9Sstevel@tonic-gate 			}
3151*7c478bd9Sstevel@tonic-gate 			mutex_exit(&p->p_lock);
3152*7c478bd9Sstevel@tonic-gate 			pgsignal(p->p_pgidp, SIGTTOU);
3153*7c478bd9Sstevel@tonic-gate 			mutex_enter(&p->p_lock);
3154*7c478bd9Sstevel@tonic-gate 		}
3155*7c478bd9Sstevel@tonic-gate 
3156*7c478bd9Sstevel@tonic-gate 		/*
3157*7c478bd9Sstevel@tonic-gate 		 * We call cv_wait_sig_swap() to cause the appropriate
3158*7c478bd9Sstevel@tonic-gate 		 * action for the jobcontrol signal to take place.
3159*7c478bd9Sstevel@tonic-gate 		 * If the signal is being caught, we will take the
3160*7c478bd9Sstevel@tonic-gate 		 * EINTR error return.  Otherwise, the default action
3161*7c478bd9Sstevel@tonic-gate 		 * of causing the process to stop will take place.
3162*7c478bd9Sstevel@tonic-gate 		 * In this case, we rely on the periodic cv_broadcast() on
3163*7c478bd9Sstevel@tonic-gate 		 * &lbolt_cv to wake us up to loop around and test again.
3164*7c478bd9Sstevel@tonic-gate 		 * We can't get here if the signal is ignored or
3165*7c478bd9Sstevel@tonic-gate 		 * if the current thread is blocking the signal.
3166*7c478bd9Sstevel@tonic-gate 		 */
3167*7c478bd9Sstevel@tonic-gate 		if (!cv_wait_sig_swap(&lbolt_cv, &p->p_lock)) {
3168*7c478bd9Sstevel@tonic-gate 			mutex_exit(&p->p_lock);
3169*7c478bd9Sstevel@tonic-gate 			return (EINTR);
3170*7c478bd9Sstevel@tonic-gate 		}
3171*7c478bd9Sstevel@tonic-gate 	}
3172*7c478bd9Sstevel@tonic-gate }
3173*7c478bd9Sstevel@tonic-gate 
3174*7c478bd9Sstevel@tonic-gate /*
3175*7c478bd9Sstevel@tonic-gate  * Return size of message of block type (bp->b_datap->db_type)
3176*7c478bd9Sstevel@tonic-gate  */
3177*7c478bd9Sstevel@tonic-gate size_t
3178*7c478bd9Sstevel@tonic-gate xmsgsize(mblk_t *bp)
3179*7c478bd9Sstevel@tonic-gate {
3180*7c478bd9Sstevel@tonic-gate 	unsigned char type;
3181*7c478bd9Sstevel@tonic-gate 	size_t count = 0;
3182*7c478bd9Sstevel@tonic-gate 
3183*7c478bd9Sstevel@tonic-gate 	type = bp->b_datap->db_type;
3184*7c478bd9Sstevel@tonic-gate 
3185*7c478bd9Sstevel@tonic-gate 	for (; bp; bp = bp->b_cont) {
3186*7c478bd9Sstevel@tonic-gate 		if (type != bp->b_datap->db_type)
3187*7c478bd9Sstevel@tonic-gate 			break;
3188*7c478bd9Sstevel@tonic-gate 		ASSERT(bp->b_wptr >= bp->b_rptr);
3189*7c478bd9Sstevel@tonic-gate 		count += bp->b_wptr - bp->b_rptr;
3190*7c478bd9Sstevel@tonic-gate 	}
3191*7c478bd9Sstevel@tonic-gate 	return (count);
3192*7c478bd9Sstevel@tonic-gate }
3193*7c478bd9Sstevel@tonic-gate 
3194*7c478bd9Sstevel@tonic-gate /*
3195*7c478bd9Sstevel@tonic-gate  * Allocate a stream head.
3196*7c478bd9Sstevel@tonic-gate  */
3197*7c478bd9Sstevel@tonic-gate struct stdata *
3198*7c478bd9Sstevel@tonic-gate shalloc(queue_t *qp)
3199*7c478bd9Sstevel@tonic-gate {
3200*7c478bd9Sstevel@tonic-gate 	stdata_t *stp;
3201*7c478bd9Sstevel@tonic-gate 
3202*7c478bd9Sstevel@tonic-gate 	stp = kmem_cache_alloc(stream_head_cache, KM_SLEEP);
3203*7c478bd9Sstevel@tonic-gate 
3204*7c478bd9Sstevel@tonic-gate 	stp->sd_wrq = _WR(qp);
3205*7c478bd9Sstevel@tonic-gate 	stp->sd_strtab = NULL;
3206*7c478bd9Sstevel@tonic-gate 	stp->sd_iocid = 0;
3207*7c478bd9Sstevel@tonic-gate 	stp->sd_mate = NULL;
3208*7c478bd9Sstevel@tonic-gate 	stp->sd_freezer = NULL;
3209*7c478bd9Sstevel@tonic-gate 	stp->sd_refcnt = 0;
3210*7c478bd9Sstevel@tonic-gate 	stp->sd_wakeq = 0;
3211*7c478bd9Sstevel@tonic-gate 	stp->sd_anchor = 0;
3212*7c478bd9Sstevel@tonic-gate 	stp->sd_struiowrq = NULL;
3213*7c478bd9Sstevel@tonic-gate 	stp->sd_struiordq = NULL;
3214*7c478bd9Sstevel@tonic-gate 	stp->sd_struiodnak = 0;
3215*7c478bd9Sstevel@tonic-gate 	stp->sd_struionak = NULL;
3216*7c478bd9Sstevel@tonic-gate #ifdef C2_AUDIT
3217*7c478bd9Sstevel@tonic-gate 	stp->sd_t_audit_data = NULL;
3218*7c478bd9Sstevel@tonic-gate #endif
3219*7c478bd9Sstevel@tonic-gate 	stp->sd_rput_opt = 0;
3220*7c478bd9Sstevel@tonic-gate 	stp->sd_wput_opt = 0;
3221*7c478bd9Sstevel@tonic-gate 	stp->sd_read_opt = 0;
3222*7c478bd9Sstevel@tonic-gate 	stp->sd_rprotofunc = strrput_proto;
3223*7c478bd9Sstevel@tonic-gate 	stp->sd_rmiscfunc = strrput_misc;
3224*7c478bd9Sstevel@tonic-gate 	stp->sd_rderrfunc = stp->sd_wrerrfunc = NULL;
3225*7c478bd9Sstevel@tonic-gate 	stp->sd_ciputctrl = NULL;
3226*7c478bd9Sstevel@tonic-gate 	stp->sd_nciputctrl = 0;
3227*7c478bd9Sstevel@tonic-gate 	stp->sd_qhead = NULL;
3228*7c478bd9Sstevel@tonic-gate 	stp->sd_qtail = NULL;
3229*7c478bd9Sstevel@tonic-gate 	stp->sd_servid = NULL;
3230*7c478bd9Sstevel@tonic-gate 	stp->sd_nqueues = 0;
3231*7c478bd9Sstevel@tonic-gate 	stp->sd_svcflags = 0;
3232*7c478bd9Sstevel@tonic-gate 	stp->sd_copyflag = 0;
3233*7c478bd9Sstevel@tonic-gate 	return (stp);
3234*7c478bd9Sstevel@tonic-gate }
3235*7c478bd9Sstevel@tonic-gate 
3236*7c478bd9Sstevel@tonic-gate /*
3237*7c478bd9Sstevel@tonic-gate  * Free a stream head.
3238*7c478bd9Sstevel@tonic-gate  */
3239*7c478bd9Sstevel@tonic-gate void
3240*7c478bd9Sstevel@tonic-gate shfree(stdata_t *stp)
3241*7c478bd9Sstevel@tonic-gate {
3242*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&stp->sd_lock));
3243*7c478bd9Sstevel@tonic-gate 
3244*7c478bd9Sstevel@tonic-gate 	stp->sd_wrq = NULL;
3245*7c478bd9Sstevel@tonic-gate 
3246*7c478bd9Sstevel@tonic-gate 	mutex_enter(&stp->sd_qlock);
3247*7c478bd9Sstevel@tonic-gate 	while (stp->sd_svcflags & STRS_SCHEDULED) {
3248*7c478bd9Sstevel@tonic-gate 		STRSTAT(strwaits);
3249*7c478bd9Sstevel@tonic-gate 		cv_wait(&stp->sd_qcv, &stp->sd_qlock);
3250*7c478bd9Sstevel@tonic-gate 	}
3251*7c478bd9Sstevel@tonic-gate 	mutex_exit(&stp->sd_qlock);
3252*7c478bd9Sstevel@tonic-gate 
3253*7c478bd9Sstevel@tonic-gate 	if (stp->sd_ciputctrl != NULL) {
3254*7c478bd9Sstevel@tonic-gate 		ASSERT(stp->sd_nciputctrl == n_ciputctrl - 1);
3255*7c478bd9Sstevel@tonic-gate 		SUMCHECK_CIPUTCTRL_COUNTS(stp->sd_ciputctrl,
3256*7c478bd9Sstevel@tonic-gate 		    stp->sd_nciputctrl, 0);
3257*7c478bd9Sstevel@tonic-gate 		ASSERT(ciputctrl_cache != NULL);
3258*7c478bd9Sstevel@tonic-gate 		kmem_cache_free(ciputctrl_cache, stp->sd_ciputctrl);
3259*7c478bd9Sstevel@tonic-gate 		stp->sd_ciputctrl = NULL;
3260*7c478bd9Sstevel@tonic-gate 		stp->sd_nciputctrl = 0;
3261*7c478bd9Sstevel@tonic-gate 	}
3262*7c478bd9Sstevel@tonic-gate 	ASSERT(stp->sd_qhead == NULL);
3263*7c478bd9Sstevel@tonic-gate 	ASSERT(stp->sd_qtail == NULL);
3264*7c478bd9Sstevel@tonic-gate 	ASSERT(stp->sd_nqueues == 0);
3265*7c478bd9Sstevel@tonic-gate 	kmem_cache_free(stream_head_cache, stp);
3266*7c478bd9Sstevel@tonic-gate }
3267*7c478bd9Sstevel@tonic-gate 
3268*7c478bd9Sstevel@tonic-gate /*
3269*7c478bd9Sstevel@tonic-gate  * Allocate a pair of queues and a syncq for the pair
3270*7c478bd9Sstevel@tonic-gate  */
3271*7c478bd9Sstevel@tonic-gate queue_t *
3272*7c478bd9Sstevel@tonic-gate allocq(void)
3273*7c478bd9Sstevel@tonic-gate {
3274*7c478bd9Sstevel@tonic-gate 	queinfo_t *qip;
3275*7c478bd9Sstevel@tonic-gate 	queue_t *qp, *wqp;
3276*7c478bd9Sstevel@tonic-gate 	syncq_t	*sq;
3277*7c478bd9Sstevel@tonic-gate 
3278*7c478bd9Sstevel@tonic-gate 	qip = kmem_cache_alloc(queue_cache, KM_SLEEP);
3279*7c478bd9Sstevel@tonic-gate 
3280*7c478bd9Sstevel@tonic-gate 	qp = &qip->qu_rqueue;
3281*7c478bd9Sstevel@tonic-gate 	wqp = &qip->qu_wqueue;
3282*7c478bd9Sstevel@tonic-gate 	sq = &qip->qu_syncq;
3283*7c478bd9Sstevel@tonic-gate 
3284*7c478bd9Sstevel@tonic-gate 	qp->q_last	= NULL;
3285*7c478bd9Sstevel@tonic-gate 	qp->q_next	= NULL;
3286*7c478bd9Sstevel@tonic-gate 	qp->q_ptr	= NULL;
3287*7c478bd9Sstevel@tonic-gate 	qp->q_flag	= QUSE | QREADR;
3288*7c478bd9Sstevel@tonic-gate 	qp->q_bandp	= NULL;
3289*7c478bd9Sstevel@tonic-gate 	qp->q_stream	= NULL;
3290*7c478bd9Sstevel@tonic-gate 	qp->q_syncq	= sq;
3291*7c478bd9Sstevel@tonic-gate 	qp->q_nband	= 0;
3292*7c478bd9Sstevel@tonic-gate 	qp->q_nfsrv	= NULL;
3293*7c478bd9Sstevel@tonic-gate 	qp->q_draining	= 0;
3294*7c478bd9Sstevel@tonic-gate 	qp->q_syncqmsgs	= 0;
3295*7c478bd9Sstevel@tonic-gate 	qp->q_spri	= 0;
3296*7c478bd9Sstevel@tonic-gate 	qp->q_qtstamp	= 0;
3297*7c478bd9Sstevel@tonic-gate 	qp->q_sqtstamp	= 0;
3298*7c478bd9Sstevel@tonic-gate 	qp->q_fp	= NULL;
3299*7c478bd9Sstevel@tonic-gate 
3300*7c478bd9Sstevel@tonic-gate 	wqp->q_last	= NULL;
3301*7c478bd9Sstevel@tonic-gate 	wqp->q_next	= NULL;
3302*7c478bd9Sstevel@tonic-gate 	wqp->q_ptr	= NULL;
3303*7c478bd9Sstevel@tonic-gate 	wqp->q_flag	= QUSE;
3304*7c478bd9Sstevel@tonic-gate 	wqp->q_bandp	= NULL;
3305*7c478bd9Sstevel@tonic-gate 	wqp->q_stream	= NULL;
3306*7c478bd9Sstevel@tonic-gate 	wqp->q_syncq	= sq;
3307*7c478bd9Sstevel@tonic-gate 	wqp->q_nband	= 0;
3308*7c478bd9Sstevel@tonic-gate 	wqp->q_nfsrv	= NULL;
3309*7c478bd9Sstevel@tonic-gate 	wqp->q_draining	= 0;
3310*7c478bd9Sstevel@tonic-gate 	wqp->q_syncqmsgs = 0;
3311*7c478bd9Sstevel@tonic-gate 	wqp->q_qtstamp	= 0;
3312*7c478bd9Sstevel@tonic-gate 	wqp->q_sqtstamp	= 0;
3313*7c478bd9Sstevel@tonic-gate 	wqp->q_spri	= 0;
3314*7c478bd9Sstevel@tonic-gate 
3315*7c478bd9Sstevel@tonic-gate 	sq->sq_count	= 0;
3316*7c478bd9Sstevel@tonic-gate 	sq->sq_rmqcount	= 0;
3317*7c478bd9Sstevel@tonic-gate 	sq->sq_flags	= 0;
3318*7c478bd9Sstevel@tonic-gate 	sq->sq_type	= 0;
3319*7c478bd9Sstevel@tonic-gate 	sq->sq_callbflags = 0;
3320*7c478bd9Sstevel@tonic-gate 	sq->sq_cancelid	= 0;
3321*7c478bd9Sstevel@tonic-gate 	sq->sq_ciputctrl = NULL;
3322*7c478bd9Sstevel@tonic-gate 	sq->sq_nciputctrl = 0;
3323*7c478bd9Sstevel@tonic-gate 	sq->sq_needexcl = 0;
3324*7c478bd9Sstevel@tonic-gate 	sq->sq_svcflags = 0;
3325*7c478bd9Sstevel@tonic-gate 
3326*7c478bd9Sstevel@tonic-gate 	return (qp);
3327*7c478bd9Sstevel@tonic-gate }
3328*7c478bd9Sstevel@tonic-gate 
3329*7c478bd9Sstevel@tonic-gate /*
3330*7c478bd9Sstevel@tonic-gate  * Free a pair of queues and the "attached" syncq.
3331*7c478bd9Sstevel@tonic-gate  * Discard any messages left on the syncq(s), remove the syncq(s) from the
3332*7c478bd9Sstevel@tonic-gate  * outer perimeter, and free the syncq(s) if they are not the "attached" syncq.
3333*7c478bd9Sstevel@tonic-gate  */
3334*7c478bd9Sstevel@tonic-gate void
3335*7c478bd9Sstevel@tonic-gate freeq(queue_t *qp)
3336*7c478bd9Sstevel@tonic-gate {
3337*7c478bd9Sstevel@tonic-gate 	qband_t *qbp, *nqbp;
3338*7c478bd9Sstevel@tonic-gate 	syncq_t *sq, *outer;
3339*7c478bd9Sstevel@tonic-gate 	queue_t *wqp = _WR(qp);
3340*7c478bd9Sstevel@tonic-gate 
3341*7c478bd9Sstevel@tonic-gate 	ASSERT(qp->q_flag & QREADR);
3342*7c478bd9Sstevel@tonic-gate 
3343*7c478bd9Sstevel@tonic-gate 	(void) flush_syncq(qp->q_syncq, qp);
3344*7c478bd9Sstevel@tonic-gate 	(void) flush_syncq(wqp->q_syncq, wqp);
3345*7c478bd9Sstevel@tonic-gate 	ASSERT(qp->q_syncqmsgs == 0 && wqp->q_syncqmsgs == 0);
3346*7c478bd9Sstevel@tonic-gate 
3347*7c478bd9Sstevel@tonic-gate 	outer = qp->q_syncq->sq_outer;
3348*7c478bd9Sstevel@tonic-gate 	if (outer != NULL) {
3349*7c478bd9Sstevel@tonic-gate 		outer_remove(outer, qp->q_syncq);
3350*7c478bd9Sstevel@tonic-gate 		if (wqp->q_syncq != qp->q_syncq)
3351*7c478bd9Sstevel@tonic-gate 			outer_remove(outer, wqp->q_syncq);
3352*7c478bd9Sstevel@tonic-gate 	}
3353*7c478bd9Sstevel@tonic-gate 	/*
3354*7c478bd9Sstevel@tonic-gate 	 * Free any syncqs that are outside what allocq returned.
3355*7c478bd9Sstevel@tonic-gate 	 */
3356*7c478bd9Sstevel@tonic-gate 	if (qp->q_syncq != SQ(qp) && !(qp->q_flag & QPERMOD))
3357*7c478bd9Sstevel@tonic-gate 		free_syncq(qp->q_syncq);
3358*7c478bd9Sstevel@tonic-gate 	if (qp->q_syncq != wqp->q_syncq && wqp->q_syncq != SQ(qp))
3359*7c478bd9Sstevel@tonic-gate 		free_syncq(wqp->q_syncq);
3360*7c478bd9Sstevel@tonic-gate 
3361*7c478bd9Sstevel@tonic-gate 	ASSERT((qp->q_sqflags & (Q_SQQUEUED | Q_SQDRAINING)) == 0);
3362*7c478bd9Sstevel@tonic-gate 	ASSERT((wqp->q_sqflags & (Q_SQQUEUED | Q_SQDRAINING)) == 0);
3363*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(QLOCK(qp)));
3364*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(QLOCK(wqp)));
3365*7c478bd9Sstevel@tonic-gate 	sq = SQ(qp);
3366*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(SQLOCK(sq)));
3367*7c478bd9Sstevel@tonic-gate 	ASSERT(sq->sq_head == NULL && sq->sq_tail == NULL);
3368*7c478bd9Sstevel@tonic-gate 	ASSERT(sq->sq_outer == NULL);
3369*7c478bd9Sstevel@tonic-gate 	ASSERT(sq->sq_onext == NULL && sq->sq_oprev == NULL);
3370*7c478bd9Sstevel@tonic-gate 	ASSERT(sq->sq_callbpend == NULL);
3371*7c478bd9Sstevel@tonic-gate 	ASSERT(sq->sq_needexcl == 0);
3372*7c478bd9Sstevel@tonic-gate 
3373*7c478bd9Sstevel@tonic-gate 	if (sq->sq_ciputctrl != NULL) {
3374*7c478bd9Sstevel@tonic-gate 		ASSERT(sq->sq_nciputctrl == n_ciputctrl - 1);
3375*7c478bd9Sstevel@tonic-gate 		SUMCHECK_CIPUTCTRL_COUNTS(sq->sq_ciputctrl,
3376*7c478bd9Sstevel@tonic-gate 		    sq->sq_nciputctrl, 0);
3377*7c478bd9Sstevel@tonic-gate 		ASSERT(ciputctrl_cache != NULL);
3378*7c478bd9Sstevel@tonic-gate 		kmem_cache_free(ciputctrl_cache, sq->sq_ciputctrl);
3379*7c478bd9Sstevel@tonic-gate 		sq->sq_ciputctrl = NULL;
3380*7c478bd9Sstevel@tonic-gate 		sq->sq_nciputctrl = 0;
3381*7c478bd9Sstevel@tonic-gate 	}
3382*7c478bd9Sstevel@tonic-gate 
3383*7c478bd9Sstevel@tonic-gate 	ASSERT(qp->q_first == NULL && wqp->q_first == NULL);
3384*7c478bd9Sstevel@tonic-gate 	ASSERT(qp->q_count == 0 && wqp->q_count == 0);
3385*7c478bd9Sstevel@tonic-gate 	ASSERT(qp->q_mblkcnt == 0 && wqp->q_mblkcnt == 0);
3386*7c478bd9Sstevel@tonic-gate 
3387*7c478bd9Sstevel@tonic-gate 	qp->q_flag &= ~QUSE;
3388*7c478bd9Sstevel@tonic-gate 	wqp->q_flag &= ~QUSE;
3389*7c478bd9Sstevel@tonic-gate 
3390*7c478bd9Sstevel@tonic-gate 	/* NOTE: Uncomment the assert below once bugid 1159635 is fixed. */
3391*7c478bd9Sstevel@tonic-gate 	/* ASSERT((qp->q_flag & QWANTW) == 0 && (wqp->q_flag & QWANTW) == 0); */
3392*7c478bd9Sstevel@tonic-gate 
3393*7c478bd9Sstevel@tonic-gate 	qbp = qp->q_bandp;
3394*7c478bd9Sstevel@tonic-gate 	while (qbp) {
3395*7c478bd9Sstevel@tonic-gate 		nqbp = qbp->qb_next;
3396*7c478bd9Sstevel@tonic-gate 		freeband(qbp);
3397*7c478bd9Sstevel@tonic-gate 		qbp = nqbp;
3398*7c478bd9Sstevel@tonic-gate 	}
3399*7c478bd9Sstevel@tonic-gate 	qbp = wqp->q_bandp;
3400*7c478bd9Sstevel@tonic-gate 	while (qbp) {
3401*7c478bd9Sstevel@tonic-gate 		nqbp = qbp->qb_next;
3402*7c478bd9Sstevel@tonic-gate 		freeband(qbp);
3403*7c478bd9Sstevel@tonic-gate 		qbp = nqbp;
3404*7c478bd9Sstevel@tonic-gate 	}
3405*7c478bd9Sstevel@tonic-gate 	kmem_cache_free(queue_cache, qp);
3406*7c478bd9Sstevel@tonic-gate }
3407*7c478bd9Sstevel@tonic-gate 
3408*7c478bd9Sstevel@tonic-gate /*
3409*7c478bd9Sstevel@tonic-gate  * Allocate a qband structure.
3410*7c478bd9Sstevel@tonic-gate  */
3411*7c478bd9Sstevel@tonic-gate qband_t *
3412*7c478bd9Sstevel@tonic-gate allocband(void)
3413*7c478bd9Sstevel@tonic-gate {
3414*7c478bd9Sstevel@tonic-gate 	qband_t *qbp;
3415*7c478bd9Sstevel@tonic-gate 
3416*7c478bd9Sstevel@tonic-gate 	qbp = kmem_cache_alloc(qband_cache, KM_NOSLEEP);
3417*7c478bd9Sstevel@tonic-gate 	if (qbp == NULL)
3418*7c478bd9Sstevel@tonic-gate 		return (NULL);
3419*7c478bd9Sstevel@tonic-gate 
3420*7c478bd9Sstevel@tonic-gate 	qbp->qb_next	= NULL;
3421*7c478bd9Sstevel@tonic-gate 	qbp->qb_count	= 0;
3422*7c478bd9Sstevel@tonic-gate 	qbp->qb_mblkcnt	= 0;
3423*7c478bd9Sstevel@tonic-gate 	qbp->qb_first	= NULL;
3424*7c478bd9Sstevel@tonic-gate 	qbp->qb_last	= NULL;
3425*7c478bd9Sstevel@tonic-gate 	qbp->qb_flag	= 0;
3426*7c478bd9Sstevel@tonic-gate 
3427*7c478bd9Sstevel@tonic-gate 	return (qbp);
3428*7c478bd9Sstevel@tonic-gate }
3429*7c478bd9Sstevel@tonic-gate 
3430*7c478bd9Sstevel@tonic-gate /*
3431*7c478bd9Sstevel@tonic-gate  * Free a qband structure.
3432*7c478bd9Sstevel@tonic-gate  */
3433*7c478bd9Sstevel@tonic-gate void
3434*7c478bd9Sstevel@tonic-gate freeband(qband_t *qbp)
3435*7c478bd9Sstevel@tonic-gate {
3436*7c478bd9Sstevel@tonic-gate 	kmem_cache_free(qband_cache, qbp);
3437*7c478bd9Sstevel@tonic-gate }
3438*7c478bd9Sstevel@tonic-gate 
3439*7c478bd9Sstevel@tonic-gate /*
3440*7c478bd9Sstevel@tonic-gate  * Just like putnextctl(9F), except that allocb_wait() is used.
3441*7c478bd9Sstevel@tonic-gate  *
3442*7c478bd9Sstevel@tonic-gate  * Consolidation Private, and of course only callable from the stream head or
3443*7c478bd9Sstevel@tonic-gate  * routines that may block.
3444*7c478bd9Sstevel@tonic-gate  */
3445*7c478bd9Sstevel@tonic-gate int
3446*7c478bd9Sstevel@tonic-gate putnextctl_wait(queue_t *q, int type)
3447*7c478bd9Sstevel@tonic-gate {
3448*7c478bd9Sstevel@tonic-gate 	mblk_t *bp;
3449*7c478bd9Sstevel@tonic-gate 	int error;
3450*7c478bd9Sstevel@tonic-gate 
3451*7c478bd9Sstevel@tonic-gate 	if ((datamsg(type) && (type != M_DELAY)) ||
3452*7c478bd9Sstevel@tonic-gate 	    (bp = allocb_wait(0, BPRI_HI, 0, &error)) == NULL)
3453*7c478bd9Sstevel@tonic-gate 		return (0);
3454*7c478bd9Sstevel@tonic-gate 
3455*7c478bd9Sstevel@tonic-gate 	bp->b_datap->db_type = (unsigned char)type;
3456*7c478bd9Sstevel@tonic-gate 	putnext(q, bp);
3457*7c478bd9Sstevel@tonic-gate 	return (1);
3458*7c478bd9Sstevel@tonic-gate }
3459*7c478bd9Sstevel@tonic-gate 
3460*7c478bd9Sstevel@tonic-gate /*
3461*7c478bd9Sstevel@tonic-gate  * run any possible bufcalls.
3462*7c478bd9Sstevel@tonic-gate  */
3463*7c478bd9Sstevel@tonic-gate void
3464*7c478bd9Sstevel@tonic-gate runbufcalls(void)
3465*7c478bd9Sstevel@tonic-gate {
3466*7c478bd9Sstevel@tonic-gate 	strbufcall_t *bcp;
3467*7c478bd9Sstevel@tonic-gate 
3468*7c478bd9Sstevel@tonic-gate 	mutex_enter(&bcall_monitor);
3469*7c478bd9Sstevel@tonic-gate 	mutex_enter(&strbcall_lock);
3470*7c478bd9Sstevel@tonic-gate 
3471*7c478bd9Sstevel@tonic-gate 	if (strbcalls.bc_head) {
3472*7c478bd9Sstevel@tonic-gate 		size_t count;
3473*7c478bd9Sstevel@tonic-gate 		int nevent;
3474*7c478bd9Sstevel@tonic-gate 
3475*7c478bd9Sstevel@tonic-gate 		/*
3476*7c478bd9Sstevel@tonic-gate 		 * count how many events are on the list
3477*7c478bd9Sstevel@tonic-gate 		 * now so we can check to avoid looping
3478*7c478bd9Sstevel@tonic-gate 		 * in low memory situations
3479*7c478bd9Sstevel@tonic-gate 		 */
3480*7c478bd9Sstevel@tonic-gate 		nevent = 0;
3481*7c478bd9Sstevel@tonic-gate 		for (bcp = strbcalls.bc_head; bcp; bcp = bcp->bc_next)
3482*7c478bd9Sstevel@tonic-gate 			nevent++;
3483*7c478bd9Sstevel@tonic-gate 
3484*7c478bd9Sstevel@tonic-gate 		/*
3485*7c478bd9Sstevel@tonic-gate 		 * get estimate of available memory from kmem_avail().
3486*7c478bd9Sstevel@tonic-gate 		 * awake all bufcall functions waiting for
3487*7c478bd9Sstevel@tonic-gate 		 * memory whose request could be satisfied
3488*7c478bd9Sstevel@tonic-gate 		 * by 'count' memory and let 'em fight for it.
3489*7c478bd9Sstevel@tonic-gate 		 */
3490*7c478bd9Sstevel@tonic-gate 		count = kmem_avail();
3491*7c478bd9Sstevel@tonic-gate 		while ((bcp = strbcalls.bc_head) != NULL && nevent) {
3492*7c478bd9Sstevel@tonic-gate 			STRSTAT(bufcalls);
3493*7c478bd9Sstevel@tonic-gate 			--nevent;
3494*7c478bd9Sstevel@tonic-gate 			if (bcp->bc_size <= count) {
3495*7c478bd9Sstevel@tonic-gate 				bcp->bc_executor = curthread;
3496*7c478bd9Sstevel@tonic-gate 				mutex_exit(&strbcall_lock);
3497*7c478bd9Sstevel@tonic-gate 				(*bcp->bc_func)(bcp->bc_arg);
3498*7c478bd9Sstevel@tonic-gate 				mutex_enter(&strbcall_lock);
3499*7c478bd9Sstevel@tonic-gate 				bcp->bc_executor = NULL;
3500*7c478bd9Sstevel@tonic-gate 				cv_broadcast(&bcall_cv);
3501*7c478bd9Sstevel@tonic-gate 				strbcalls.bc_head = bcp->bc_next;
3502*7c478bd9Sstevel@tonic-gate 				kmem_free(bcp, sizeof (strbufcall_t));
3503*7c478bd9Sstevel@tonic-gate 			} else {
3504*7c478bd9Sstevel@tonic-gate 				/*
3505*7c478bd9Sstevel@tonic-gate 				 * too big, try again later - note
3506*7c478bd9Sstevel@tonic-gate 				 * that nevent was decremented above
3507*7c478bd9Sstevel@tonic-gate 				 * so we won't retry this one on this
3508*7c478bd9Sstevel@tonic-gate 				 * iteration of the loop
3509*7c478bd9Sstevel@tonic-gate 				 */
3510*7c478bd9Sstevel@tonic-gate 				if (bcp->bc_next != NULL) {
3511*7c478bd9Sstevel@tonic-gate 					strbcalls.bc_head = bcp->bc_next;
3512*7c478bd9Sstevel@tonic-gate 					bcp->bc_next = NULL;
3513*7c478bd9Sstevel@tonic-gate 					strbcalls.bc_tail->bc_next = bcp;
3514*7c478bd9Sstevel@tonic-gate 					strbcalls.bc_tail = bcp;
3515*7c478bd9Sstevel@tonic-gate 				}
3516*7c478bd9Sstevel@tonic-gate 			}
3517*7c478bd9Sstevel@tonic-gate 		}
3518*7c478bd9Sstevel@tonic-gate 		if (strbcalls.bc_head == NULL)
3519*7c478bd9Sstevel@tonic-gate 			strbcalls.bc_tail = NULL;
3520*7c478bd9Sstevel@tonic-gate 	}
3521*7c478bd9Sstevel@tonic-gate 
3522*7c478bd9Sstevel@tonic-gate 	mutex_exit(&strbcall_lock);
3523*7c478bd9Sstevel@tonic-gate 	mutex_exit(&bcall_monitor);
3524*7c478bd9Sstevel@tonic-gate }
3525*7c478bd9Sstevel@tonic-gate 
3526*7c478bd9Sstevel@tonic-gate 
3527*7c478bd9Sstevel@tonic-gate /*
3528*7c478bd9Sstevel@tonic-gate  * actually run queue's service routine.
3529*7c478bd9Sstevel@tonic-gate  */
3530*7c478bd9Sstevel@tonic-gate static void
3531*7c478bd9Sstevel@tonic-gate runservice(queue_t *q)
3532*7c478bd9Sstevel@tonic-gate {
3533*7c478bd9Sstevel@tonic-gate 	qband_t *qbp;
3534*7c478bd9Sstevel@tonic-gate 
3535*7c478bd9Sstevel@tonic-gate 	ASSERT(q->q_qinfo->qi_srvp);
3536*7c478bd9Sstevel@tonic-gate again:
3537*7c478bd9Sstevel@tonic-gate 	entersq(q->q_syncq, SQ_SVC);
3538*7c478bd9Sstevel@tonic-gate 	TRACE_1(TR_FAC_STREAMS_FR, TR_QRUNSERVICE_START,
3539*7c478bd9Sstevel@tonic-gate 		"runservice starts:%p", q);
3540*7c478bd9Sstevel@tonic-gate 
3541*7c478bd9Sstevel@tonic-gate 	if (!(q->q_flag & QWCLOSE))
3542*7c478bd9Sstevel@tonic-gate 		(*q->q_qinfo->qi_srvp)(q);
3543*7c478bd9Sstevel@tonic-gate 
3544*7c478bd9Sstevel@tonic-gate 	TRACE_1(TR_FAC_STREAMS_FR, TR_QRUNSERVICE_END,
3545*7c478bd9Sstevel@tonic-gate 		"runservice ends:(%p)", q);
3546*7c478bd9Sstevel@tonic-gate 
3547*7c478bd9Sstevel@tonic-gate 	leavesq(q->q_syncq, SQ_SVC);
3548*7c478bd9Sstevel@tonic-gate 
3549*7c478bd9Sstevel@tonic-gate 	mutex_enter(QLOCK(q));
3550*7c478bd9Sstevel@tonic-gate 	if (q->q_flag & QENAB) {
3551*7c478bd9Sstevel@tonic-gate 		q->q_flag &= ~QENAB;
3552*7c478bd9Sstevel@tonic-gate 		mutex_exit(QLOCK(q));
3553*7c478bd9Sstevel@tonic-gate 		goto again;
3554*7c478bd9Sstevel@tonic-gate 	}
3555*7c478bd9Sstevel@tonic-gate 	q->q_flag &= ~QINSERVICE;
3556*7c478bd9Sstevel@tonic-gate 	q->q_flag &= ~QBACK;
3557*7c478bd9Sstevel@tonic-gate 	for (qbp = q->q_bandp; qbp; qbp = qbp->qb_next)
3558*7c478bd9Sstevel@tonic-gate 		qbp->qb_flag &= ~QB_BACK;
3559*7c478bd9Sstevel@tonic-gate 	/*
3560*7c478bd9Sstevel@tonic-gate 	 * Wakeup thread waiting for the service procedure
3561*7c478bd9Sstevel@tonic-gate 	 * to be run (strclose and qdetach).
3562*7c478bd9Sstevel@tonic-gate 	 */
3563*7c478bd9Sstevel@tonic-gate 	cv_broadcast(&q->q_wait);
3564*7c478bd9Sstevel@tonic-gate 
3565*7c478bd9Sstevel@tonic-gate 	mutex_exit(QLOCK(q));
3566*7c478bd9Sstevel@tonic-gate }
3567*7c478bd9Sstevel@tonic-gate 
3568*7c478bd9Sstevel@tonic-gate /*
3569*7c478bd9Sstevel@tonic-gate  * Background processing of bufcalls.
3570*7c478bd9Sstevel@tonic-gate  */
3571*7c478bd9Sstevel@tonic-gate void
3572*7c478bd9Sstevel@tonic-gate streams_bufcall_service(void)
3573*7c478bd9Sstevel@tonic-gate {
3574*7c478bd9Sstevel@tonic-gate 	callb_cpr_t	cprinfo;
3575*7c478bd9Sstevel@tonic-gate 
3576*7c478bd9Sstevel@tonic-gate 	CALLB_CPR_INIT(&cprinfo, &strbcall_lock, callb_generic_cpr,
3577*7c478bd9Sstevel@tonic-gate 	    "streams_bufcall_service");
3578*7c478bd9Sstevel@tonic-gate 
3579*7c478bd9Sstevel@tonic-gate 	mutex_enter(&strbcall_lock);
3580*7c478bd9Sstevel@tonic-gate 
3581*7c478bd9Sstevel@tonic-gate 	for (;;) {
3582*7c478bd9Sstevel@tonic-gate 		if (strbcalls.bc_head != NULL && kmem_avail() > 0) {
3583*7c478bd9Sstevel@tonic-gate 			mutex_exit(&strbcall_lock);
3584*7c478bd9Sstevel@tonic-gate 			runbufcalls();
3585*7c478bd9Sstevel@tonic-gate 			mutex_enter(&strbcall_lock);
3586*7c478bd9Sstevel@tonic-gate 		}
3587*7c478bd9Sstevel@tonic-gate 		if (strbcalls.bc_head != NULL) {
3588*7c478bd9Sstevel@tonic-gate 			clock_t wt, tick;
3589*7c478bd9Sstevel@tonic-gate 
3590*7c478bd9Sstevel@tonic-gate 			STRSTAT(bcwaits);
3591*7c478bd9Sstevel@tonic-gate 			/* Wait for memory to become available */
3592*7c478bd9Sstevel@tonic-gate 			CALLB_CPR_SAFE_BEGIN(&cprinfo);
3593*7c478bd9Sstevel@tonic-gate 			tick = SEC_TO_TICK(60);
3594*7c478bd9Sstevel@tonic-gate 			time_to_wait(&wt, tick);
3595*7c478bd9Sstevel@tonic-gate 			(void) cv_timedwait(&memavail_cv, &strbcall_lock, wt);
3596*7c478bd9Sstevel@tonic-gate 			CALLB_CPR_SAFE_END(&cprinfo, &strbcall_lock);
3597*7c478bd9Sstevel@tonic-gate 		}
3598*7c478bd9Sstevel@tonic-gate 
3599*7c478bd9Sstevel@tonic-gate 		/* Wait for new work to arrive */
3600*7c478bd9Sstevel@tonic-gate 		if (strbcalls.bc_head == NULL) {
3601*7c478bd9Sstevel@tonic-gate 			CALLB_CPR_SAFE_BEGIN(&cprinfo);
3602*7c478bd9Sstevel@tonic-gate 			cv_wait(&strbcall_cv, &strbcall_lock);
3603*7c478bd9Sstevel@tonic-gate 			CALLB_CPR_SAFE_END(&cprinfo, &strbcall_lock);
3604*7c478bd9Sstevel@tonic-gate 		}
3605*7c478bd9Sstevel@tonic-gate 	}
3606*7c478bd9Sstevel@tonic-gate }
3607*7c478bd9Sstevel@tonic-gate 
3608*7c478bd9Sstevel@tonic-gate /*
3609*7c478bd9Sstevel@tonic-gate  * Background processing of streams background tasks which failed
3610*7c478bd9Sstevel@tonic-gate  * taskq_dispatch.
3611*7c478bd9Sstevel@tonic-gate  */
3612*7c478bd9Sstevel@tonic-gate static void
3613*7c478bd9Sstevel@tonic-gate streams_qbkgrnd_service(void)
3614*7c478bd9Sstevel@tonic-gate {
3615*7c478bd9Sstevel@tonic-gate 	callb_cpr_t cprinfo;
3616*7c478bd9Sstevel@tonic-gate 	queue_t *q;
3617*7c478bd9Sstevel@tonic-gate 
3618*7c478bd9Sstevel@tonic-gate 	CALLB_CPR_INIT(&cprinfo, &service_queue, callb_generic_cpr,
3619*7c478bd9Sstevel@tonic-gate 	    "streams_bkgrnd_service");
3620*7c478bd9Sstevel@tonic-gate 
3621*7c478bd9Sstevel@tonic-gate 	mutex_enter(&service_queue);
3622*7c478bd9Sstevel@tonic-gate 
3623*7c478bd9Sstevel@tonic-gate 	for (;;) {
3624*7c478bd9Sstevel@tonic-gate 		/*
3625*7c478bd9Sstevel@tonic-gate 		 * Wait for work to arrive.
3626*7c478bd9Sstevel@tonic-gate 		 */
3627*7c478bd9Sstevel@tonic-gate 		while ((freebs_list == NULL) && (qhead == NULL)) {
3628*7c478bd9Sstevel@tonic-gate 			CALLB_CPR_SAFE_BEGIN(&cprinfo);
3629*7c478bd9Sstevel@tonic-gate 			cv_wait(&services_to_run, &service_queue);
3630*7c478bd9Sstevel@tonic-gate 			CALLB_CPR_SAFE_END(&cprinfo, &service_queue);
3631*7c478bd9Sstevel@tonic-gate 		}
3632*7c478bd9Sstevel@tonic-gate 		/*
3633*7c478bd9Sstevel@tonic-gate 		 * Handle all pending freebs requests to free memory.
3634*7c478bd9Sstevel@tonic-gate 		 */
3635*7c478bd9Sstevel@tonic-gate 		while (freebs_list != NULL) {
3636*7c478bd9Sstevel@tonic-gate 			mblk_t *mp = freebs_list;
3637*7c478bd9Sstevel@tonic-gate 			freebs_list = mp->b_next;
3638*7c478bd9Sstevel@tonic-gate 			mutex_exit(&service_queue);
3639*7c478bd9Sstevel@tonic-gate 			mblk_free(mp);
3640*7c478bd9Sstevel@tonic-gate 			mutex_enter(&service_queue);
3641*7c478bd9Sstevel@tonic-gate 		}
3642*7c478bd9Sstevel@tonic-gate 		/*
3643*7c478bd9Sstevel@tonic-gate 		 * Run pending queues.
3644*7c478bd9Sstevel@tonic-gate 		 */
3645*7c478bd9Sstevel@tonic-gate 		while (qhead != NULL) {
3646*7c478bd9Sstevel@tonic-gate 			DQ(q, qhead, qtail, q_link);
3647*7c478bd9Sstevel@tonic-gate 			ASSERT(q != NULL);
3648*7c478bd9Sstevel@tonic-gate 			mutex_exit(&service_queue);
3649*7c478bd9Sstevel@tonic-gate 			queue_service(q);
3650*7c478bd9Sstevel@tonic-gate 			mutex_enter(&service_queue);
3651*7c478bd9Sstevel@tonic-gate 		}
3652*7c478bd9Sstevel@tonic-gate 		ASSERT(qhead == NULL && qtail == NULL);
3653*7c478bd9Sstevel@tonic-gate 	}
3654*7c478bd9Sstevel@tonic-gate }
3655*7c478bd9Sstevel@tonic-gate 
3656*7c478bd9Sstevel@tonic-gate /*
3657*7c478bd9Sstevel@tonic-gate  * Background processing of streams background tasks which failed
3658*7c478bd9Sstevel@tonic-gate  * taskq_dispatch.
3659*7c478bd9Sstevel@tonic-gate  */
3660*7c478bd9Sstevel@tonic-gate static void
3661*7c478bd9Sstevel@tonic-gate streams_sqbkgrnd_service(void)
3662*7c478bd9Sstevel@tonic-gate {
3663*7c478bd9Sstevel@tonic-gate 	callb_cpr_t cprinfo;
3664*7c478bd9Sstevel@tonic-gate 	syncq_t *sq;
3665*7c478bd9Sstevel@tonic-gate 
3666*7c478bd9Sstevel@tonic-gate 	CALLB_CPR_INIT(&cprinfo, &service_queue, callb_generic_cpr,
3667*7c478bd9Sstevel@tonic-gate 	    "streams_sqbkgrnd_service");
3668*7c478bd9Sstevel@tonic-gate 
3669*7c478bd9Sstevel@tonic-gate 	mutex_enter(&service_queue);
3670*7c478bd9Sstevel@tonic-gate 
3671*7c478bd9Sstevel@tonic-gate 	for (;;) {
3672*7c478bd9Sstevel@tonic-gate 		/*
3673*7c478bd9Sstevel@tonic-gate 		 * Wait for work to arrive.
3674*7c478bd9Sstevel@tonic-gate 		 */
3675*7c478bd9Sstevel@tonic-gate 		while (sqhead == NULL) {
3676*7c478bd9Sstevel@tonic-gate 			CALLB_CPR_SAFE_BEGIN(&cprinfo);
3677*7c478bd9Sstevel@tonic-gate 			cv_wait(&syncqs_to_run, &service_queue);
3678*7c478bd9Sstevel@tonic-gate 			CALLB_CPR_SAFE_END(&cprinfo, &service_queue);
3679*7c478bd9Sstevel@tonic-gate 		}
3680*7c478bd9Sstevel@tonic-gate 
3681*7c478bd9Sstevel@tonic-gate 		/*
3682*7c478bd9Sstevel@tonic-gate 		 * Run pending syncqs.
3683*7c478bd9Sstevel@tonic-gate 		 */
3684*7c478bd9Sstevel@tonic-gate 		while (sqhead != NULL) {
3685*7c478bd9Sstevel@tonic-gate 			DQ(sq, sqhead, sqtail, sq_next);
3686*7c478bd9Sstevel@tonic-gate 			ASSERT(sq != NULL);
3687*7c478bd9Sstevel@tonic-gate 			ASSERT(sq->sq_svcflags & SQ_BGTHREAD);
3688*7c478bd9Sstevel@tonic-gate 			mutex_exit(&service_queue);
3689*7c478bd9Sstevel@tonic-gate 			syncq_service(sq);
3690*7c478bd9Sstevel@tonic-gate 			mutex_enter(&service_queue);
3691*7c478bd9Sstevel@tonic-gate 		}
3692*7c478bd9Sstevel@tonic-gate 	}
3693*7c478bd9Sstevel@tonic-gate }
3694*7c478bd9Sstevel@tonic-gate 
3695*7c478bd9Sstevel@tonic-gate /*
3696*7c478bd9Sstevel@tonic-gate  * Disable the syncq and wait for background syncq processing to complete.
3697*7c478bd9Sstevel@tonic-gate  * If the syncq is placed on the sqhead/sqtail queue, try to remove it from the
3698*7c478bd9Sstevel@tonic-gate  * list.
3699*7c478bd9Sstevel@tonic-gate  */
3700*7c478bd9Sstevel@tonic-gate void
3701*7c478bd9Sstevel@tonic-gate wait_sq_svc(syncq_t *sq)
3702*7c478bd9Sstevel@tonic-gate {
3703*7c478bd9Sstevel@tonic-gate 	mutex_enter(SQLOCK(sq));
3704*7c478bd9Sstevel@tonic-gate 	sq->sq_svcflags |= SQ_DISABLED;
3705*7c478bd9Sstevel@tonic-gate 	if (sq->sq_svcflags & SQ_BGTHREAD) {
3706*7c478bd9Sstevel@tonic-gate 		syncq_t *sq_chase;
3707*7c478bd9Sstevel@tonic-gate 		syncq_t *sq_curr;
3708*7c478bd9Sstevel@tonic-gate 		int removed;
3709*7c478bd9Sstevel@tonic-gate 
3710*7c478bd9Sstevel@tonic-gate 		ASSERT(sq->sq_servcount == 1);
3711*7c478bd9Sstevel@tonic-gate 		mutex_enter(&service_queue);
3712*7c478bd9Sstevel@tonic-gate 		RMQ(sq, sqhead, sqtail, sq_next, sq_chase, sq_curr, removed);
3713*7c478bd9Sstevel@tonic-gate 		mutex_exit(&service_queue);
3714*7c478bd9Sstevel@tonic-gate 		if (removed) {
3715*7c478bd9Sstevel@tonic-gate 			sq->sq_svcflags &= ~SQ_BGTHREAD;
3716*7c478bd9Sstevel@tonic-gate 			sq->sq_servcount = 0;
3717*7c478bd9Sstevel@tonic-gate 			STRSTAT(sqremoved);
3718*7c478bd9Sstevel@tonic-gate 			goto done;
3719*7c478bd9Sstevel@tonic-gate 		}
3720*7c478bd9Sstevel@tonic-gate 	}
3721*7c478bd9Sstevel@tonic-gate 	while (sq->sq_servcount != 0) {
3722*7c478bd9Sstevel@tonic-gate 		sq->sq_flags |= SQ_WANTWAKEUP;
3723*7c478bd9Sstevel@tonic-gate 		cv_wait(&sq->sq_wait, SQLOCK(sq));
3724*7c478bd9Sstevel@tonic-gate 	}
3725*7c478bd9Sstevel@tonic-gate done:
3726*7c478bd9Sstevel@tonic-gate 	mutex_exit(SQLOCK(sq));
3727*7c478bd9Sstevel@tonic-gate }
3728*7c478bd9Sstevel@tonic-gate 
3729*7c478bd9Sstevel@tonic-gate /*
3730*7c478bd9Sstevel@tonic-gate  * Put a syncq on the list of syncq's to be serviced by the sqthread.
3731*7c478bd9Sstevel@tonic-gate  * Add the argument to the end of the sqhead list and set the flag
3732*7c478bd9Sstevel@tonic-gate  * indicating this syncq has been enabled.  If it has already been
3733*7c478bd9Sstevel@tonic-gate  * enabled, don't do anything.
3734*7c478bd9Sstevel@tonic-gate  * This routine assumes that SQLOCK is held.
3735*7c478bd9Sstevel@tonic-gate  * NOTE that the lock order is to have the SQLOCK first,
3736*7c478bd9Sstevel@tonic-gate  * so if the service_syncq lock is held, we need to release it
3737*7c478bd9Sstevel@tonic-gate  * before aquiring the SQLOCK (mostly relevant for the background
3738*7c478bd9Sstevel@tonic-gate  * thread, and this seems to be common among the STREAMS global locks).
3739*7c478bd9Sstevel@tonic-gate  * Note the the sq_svcflags are protected by the SQLOCK.
3740*7c478bd9Sstevel@tonic-gate  */
3741*7c478bd9Sstevel@tonic-gate void
3742*7c478bd9Sstevel@tonic-gate sqenable(syncq_t *sq)
3743*7c478bd9Sstevel@tonic-gate {
3744*7c478bd9Sstevel@tonic-gate 	/*
3745*7c478bd9Sstevel@tonic-gate 	 * This is probably not important except for where I believe it
3746*7c478bd9Sstevel@tonic-gate 	 * is being called.  At that point, it should be held (and it
3747*7c478bd9Sstevel@tonic-gate 	 * is a pain to release it just for this routine, so don't do
3748*7c478bd9Sstevel@tonic-gate 	 * it).
3749*7c478bd9Sstevel@tonic-gate 	 */
3750*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(SQLOCK(sq)));
3751*7c478bd9Sstevel@tonic-gate 
3752*7c478bd9Sstevel@tonic-gate 	IMPLY(sq->sq_servcount == 0, sq->sq_next == NULL);
3753*7c478bd9Sstevel@tonic-gate 	IMPLY(sq->sq_next != NULL, sq->sq_svcflags & SQ_BGTHREAD);
3754*7c478bd9Sstevel@tonic-gate 
3755*7c478bd9Sstevel@tonic-gate 	/*
3756*7c478bd9Sstevel@tonic-gate 	 * Do not put on list if background thread is scheduled or
3757*7c478bd9Sstevel@tonic-gate 	 * syncq is disabled.
3758*7c478bd9Sstevel@tonic-gate 	 */
3759*7c478bd9Sstevel@tonic-gate 	if (sq->sq_svcflags & (SQ_DISABLED | SQ_BGTHREAD))
3760*7c478bd9Sstevel@tonic-gate 		return;
3761*7c478bd9Sstevel@tonic-gate 
3762*7c478bd9Sstevel@tonic-gate 	/*
3763*7c478bd9Sstevel@tonic-gate 	 * Check whether we should enable sq at all.
3764*7c478bd9Sstevel@tonic-gate 	 * Non PERMOD syncqs may be drained by at most one thread.
3765*7c478bd9Sstevel@tonic-gate 	 * PERMOD syncqs may be drained by several threads but we limit the
3766*7c478bd9Sstevel@tonic-gate 	 * total amount to the lesser of
3767*7c478bd9Sstevel@tonic-gate 	 *	Number of queues on the squeue and
3768*7c478bd9Sstevel@tonic-gate 	 *	Number of CPUs.
3769*7c478bd9Sstevel@tonic-gate 	 */
3770*7c478bd9Sstevel@tonic-gate 	if (sq->sq_servcount != 0) {
3771*7c478bd9Sstevel@tonic-gate 		if (((sq->sq_type & SQ_PERMOD) == 0) ||
3772*7c478bd9Sstevel@tonic-gate 		    (sq->sq_servcount >= MIN(sq->sq_nqueues, ncpus_online))) {
3773*7c478bd9Sstevel@tonic-gate 			STRSTAT(sqtoomany);
3774*7c478bd9Sstevel@tonic-gate 			return;
3775*7c478bd9Sstevel@tonic-gate 		}
3776*7c478bd9Sstevel@tonic-gate 	}
3777*7c478bd9Sstevel@tonic-gate 
3778*7c478bd9Sstevel@tonic-gate 	sq->sq_tstamp = lbolt;
3779*7c478bd9Sstevel@tonic-gate 	STRSTAT(sqenables);
3780*7c478bd9Sstevel@tonic-gate 
3781*7c478bd9Sstevel@tonic-gate 	/* Attempt a taskq dispatch */
3782*7c478bd9Sstevel@tonic-gate 	sq->sq_servid = (void *)taskq_dispatch(streams_taskq,
3783*7c478bd9Sstevel@tonic-gate 	    (task_func_t *)syncq_service, sq, TQ_NOSLEEP | TQ_NOQUEUE);
3784*7c478bd9Sstevel@tonic-gate 	if (sq->sq_servid != NULL) {
3785*7c478bd9Sstevel@tonic-gate 		sq->sq_servcount++;
3786*7c478bd9Sstevel@tonic-gate 		return;
3787*7c478bd9Sstevel@tonic-gate 	}
3788*7c478bd9Sstevel@tonic-gate 
3789*7c478bd9Sstevel@tonic-gate 	/*
3790*7c478bd9Sstevel@tonic-gate 	 * This taskq dispatch failed, but a previous one may have succeeded.
3791*7c478bd9Sstevel@tonic-gate 	 * Don't try to schedule on the background thread whilst there is
3792*7c478bd9Sstevel@tonic-gate 	 * outstanding taskq processing.
3793*7c478bd9Sstevel@tonic-gate 	 */
3794*7c478bd9Sstevel@tonic-gate 	if (sq->sq_servcount != 0)
3795*7c478bd9Sstevel@tonic-gate 		return;
3796*7c478bd9Sstevel@tonic-gate 
3797*7c478bd9Sstevel@tonic-gate 	/*
3798*7c478bd9Sstevel@tonic-gate 	 * System is low on resources and can't perform a non-sleeping
3799*7c478bd9Sstevel@tonic-gate 	 * dispatch. Schedule the syncq for a background thread and mark the
3800*7c478bd9Sstevel@tonic-gate 	 * syncq to avoid any further taskq dispatch attempts.
3801*7c478bd9Sstevel@tonic-gate 	 */
3802*7c478bd9Sstevel@tonic-gate 	mutex_enter(&service_queue);
3803*7c478bd9Sstevel@tonic-gate 	STRSTAT(taskqfails);
3804*7c478bd9Sstevel@tonic-gate 	ENQUEUE(sq, sqhead, sqtail, sq_next);
3805*7c478bd9Sstevel@tonic-gate 	sq->sq_svcflags |= SQ_BGTHREAD;
3806*7c478bd9Sstevel@tonic-gate 	sq->sq_servcount = 1;
3807*7c478bd9Sstevel@tonic-gate 	cv_signal(&syncqs_to_run);
3808*7c478bd9Sstevel@tonic-gate 	mutex_exit(&service_queue);
3809*7c478bd9Sstevel@tonic-gate }
3810*7c478bd9Sstevel@tonic-gate 
3811*7c478bd9Sstevel@tonic-gate /*
3812*7c478bd9Sstevel@tonic-gate  * Note: fifo_close() depends on the mblk_t on the queue being freed
3813*7c478bd9Sstevel@tonic-gate  * asynchronously. The asynchronous freeing of messages breaks the
3814*7c478bd9Sstevel@tonic-gate  * recursive call chain of fifo_close() while there are I_SENDFD type of
3815*7c478bd9Sstevel@tonic-gate  * messages refering other file pointers on the queue. Then when
3816*7c478bd9Sstevel@tonic-gate  * closing pipes it can avoid stack overflow in case of daisy-chained
3817*7c478bd9Sstevel@tonic-gate  * pipes, and also avoid deadlock in case of fifonode_t pairs (which
3818*7c478bd9Sstevel@tonic-gate  * share the same fifolock_t).
3819*7c478bd9Sstevel@tonic-gate  */
3820*7c478bd9Sstevel@tonic-gate 
3821*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
3822*7c478bd9Sstevel@tonic-gate void
3823*7c478bd9Sstevel@tonic-gate freebs_enqueue(mblk_t *mp, dblk_t *dbp)
3824*7c478bd9Sstevel@tonic-gate {
3825*7c478bd9Sstevel@tonic-gate 	ASSERT(dbp->db_mblk == mp);
3826*7c478bd9Sstevel@tonic-gate 
3827*7c478bd9Sstevel@tonic-gate 	/*
3828*7c478bd9Sstevel@tonic-gate 	 * Check data sanity. The dblock should have non-empty free function.
3829*7c478bd9Sstevel@tonic-gate 	 * It is better to panic here then later when the dblock is freed
3830*7c478bd9Sstevel@tonic-gate 	 * asynchronously when the context is lost.
3831*7c478bd9Sstevel@tonic-gate 	 */
3832*7c478bd9Sstevel@tonic-gate 	if (dbp->db_frtnp->free_func == NULL) {
3833*7c478bd9Sstevel@tonic-gate 		panic("freebs_enqueue: dblock %p has a NULL free callback",
3834*7c478bd9Sstevel@tonic-gate 		    (void *) dbp);
3835*7c478bd9Sstevel@tonic-gate 	}
3836*7c478bd9Sstevel@tonic-gate 
3837*7c478bd9Sstevel@tonic-gate 	STRSTAT(freebs);
3838*7c478bd9Sstevel@tonic-gate 	if (taskq_dispatch(streams_taskq, (task_func_t *)mblk_free, mp,
3839*7c478bd9Sstevel@tonic-gate 	    TQ_NOSLEEP) == NULL) {
3840*7c478bd9Sstevel@tonic-gate 		/*
3841*7c478bd9Sstevel@tonic-gate 		 * System is low on resources and can't perform a non-sleeping
3842*7c478bd9Sstevel@tonic-gate 		 * dispatch. Schedule for a background thread.
3843*7c478bd9Sstevel@tonic-gate 		 */
3844*7c478bd9Sstevel@tonic-gate 		mutex_enter(&service_queue);
3845*7c478bd9Sstevel@tonic-gate 		STRSTAT(taskqfails);
3846*7c478bd9Sstevel@tonic-gate 		mp->b_next = freebs_list;
3847*7c478bd9Sstevel@tonic-gate 		freebs_list = mp;
3848*7c478bd9Sstevel@tonic-gate 		cv_signal(&services_to_run);
3849*7c478bd9Sstevel@tonic-gate 		mutex_exit(&service_queue);
3850*7c478bd9Sstevel@tonic-gate 	}
3851*7c478bd9Sstevel@tonic-gate }
3852*7c478bd9Sstevel@tonic-gate 
3853*7c478bd9Sstevel@tonic-gate /*
3854*7c478bd9Sstevel@tonic-gate  * Set the QBACK or QB_BACK flag in the given queue for
3855*7c478bd9Sstevel@tonic-gate  * the given priority band.
3856*7c478bd9Sstevel@tonic-gate  */
3857*7c478bd9Sstevel@tonic-gate void
3858*7c478bd9Sstevel@tonic-gate setqback(queue_t *q, unsigned char pri)
3859*7c478bd9Sstevel@tonic-gate {
3860*7c478bd9Sstevel@tonic-gate 	int i;
3861*7c478bd9Sstevel@tonic-gate 	qband_t *qbp;
3862*7c478bd9Sstevel@tonic-gate 	qband_t **qbpp;
3863*7c478bd9Sstevel@tonic-gate 
3864*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(QLOCK(q)));
3865*7c478bd9Sstevel@tonic-gate 	if (pri != 0) {
3866*7c478bd9Sstevel@tonic-gate 		if (pri > q->q_nband) {
3867*7c478bd9Sstevel@tonic-gate 			qbpp = &q->q_bandp;
3868*7c478bd9Sstevel@tonic-gate 			while (*qbpp)
3869*7c478bd9Sstevel@tonic-gate 				qbpp = &(*qbpp)->qb_next;
3870*7c478bd9Sstevel@tonic-gate 			while (pri > q->q_nband) {
3871*7c478bd9Sstevel@tonic-gate 				if ((*qbpp = allocband()) == NULL) {
3872*7c478bd9Sstevel@tonic-gate 					cmn_err(CE_WARN,
3873*7c478bd9Sstevel@tonic-gate 					    "setqback: can't allocate qband\n");
3874*7c478bd9Sstevel@tonic-gate 					return;
3875*7c478bd9Sstevel@tonic-gate 				}
3876*7c478bd9Sstevel@tonic-gate 				(*qbpp)->qb_hiwat = q->q_hiwat;
3877*7c478bd9Sstevel@tonic-gate 				(*qbpp)->qb_lowat = q->q_lowat;
3878*7c478bd9Sstevel@tonic-gate 				q->q_nband++;
3879*7c478bd9Sstevel@tonic-gate 				qbpp = &(*qbpp)->qb_next;
3880*7c478bd9Sstevel@tonic-gate 			}
3881*7c478bd9Sstevel@tonic-gate 		}
3882*7c478bd9Sstevel@tonic-gate 		qbp = q->q_bandp;
3883*7c478bd9Sstevel@tonic-gate 		i = pri;
3884*7c478bd9Sstevel@tonic-gate 		while (--i)
3885*7c478bd9Sstevel@tonic-gate 			qbp = qbp->qb_next;
3886*7c478bd9Sstevel@tonic-gate 		qbp->qb_flag |= QB_BACK;
3887*7c478bd9Sstevel@tonic-gate 	} else {
3888*7c478bd9Sstevel@tonic-gate 		q->q_flag |= QBACK;
3889*7c478bd9Sstevel@tonic-gate 	}
3890*7c478bd9Sstevel@tonic-gate }
3891*7c478bd9Sstevel@tonic-gate 
3892*7c478bd9Sstevel@tonic-gate int
3893*7c478bd9Sstevel@tonic-gate strcopyin(void *from, void *to, size_t len, int copyflag)
3894*7c478bd9Sstevel@tonic-gate {
3895*7c478bd9Sstevel@tonic-gate 	if (copyflag & U_TO_K) {
3896*7c478bd9Sstevel@tonic-gate 		ASSERT((copyflag & K_TO_K) == 0);
3897*7c478bd9Sstevel@tonic-gate 		if (copyin(from, to, len))
3898*7c478bd9Sstevel@tonic-gate 			return (EFAULT);
3899*7c478bd9Sstevel@tonic-gate 	} else {
3900*7c478bd9Sstevel@tonic-gate 		ASSERT(copyflag & K_TO_K);
3901*7c478bd9Sstevel@tonic-gate 		bcopy(from, to, len);
3902*7c478bd9Sstevel@tonic-gate 	}
3903*7c478bd9Sstevel@tonic-gate 	return (0);
3904*7c478bd9Sstevel@tonic-gate }
3905*7c478bd9Sstevel@tonic-gate 
3906*7c478bd9Sstevel@tonic-gate int
3907*7c478bd9Sstevel@tonic-gate strcopyout(void *from, void *to, size_t len, int copyflag)
3908*7c478bd9Sstevel@tonic-gate {
3909*7c478bd9Sstevel@tonic-gate 	if (copyflag & U_TO_K) {
3910*7c478bd9Sstevel@tonic-gate 		if (copyout(from, to, len))
3911*7c478bd9Sstevel@tonic-gate 			return (EFAULT);
3912*7c478bd9Sstevel@tonic-gate 	} else {
3913*7c478bd9Sstevel@tonic-gate 		ASSERT(copyflag & K_TO_K);
3914*7c478bd9Sstevel@tonic-gate 		bcopy(from, to, len);
3915*7c478bd9Sstevel@tonic-gate 	}
3916*7c478bd9Sstevel@tonic-gate 	return (0);
3917*7c478bd9Sstevel@tonic-gate }
3918*7c478bd9Sstevel@tonic-gate 
3919*7c478bd9Sstevel@tonic-gate /*
3920*7c478bd9Sstevel@tonic-gate  * strsignal_nolock() posts a signal to the process(es) at the stream head.
3921*7c478bd9Sstevel@tonic-gate  * It assumes that the stream head lock is already held, whereas strsignal()
3922*7c478bd9Sstevel@tonic-gate  * acquires the lock first.  This routine was created because a few callers
3923*7c478bd9Sstevel@tonic-gate  * release the stream head lock before calling only to re-acquire it after
3924*7c478bd9Sstevel@tonic-gate  * it returns.
3925*7c478bd9Sstevel@tonic-gate  */
3926*7c478bd9Sstevel@tonic-gate void
3927*7c478bd9Sstevel@tonic-gate strsignal_nolock(stdata_t *stp, int sig, int32_t band)
3928*7c478bd9Sstevel@tonic-gate {
3929*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&stp->sd_lock));
3930*7c478bd9Sstevel@tonic-gate 	switch (sig) {
3931*7c478bd9Sstevel@tonic-gate 	case SIGPOLL:
3932*7c478bd9Sstevel@tonic-gate 		if (stp->sd_sigflags & S_MSG)
3933*7c478bd9Sstevel@tonic-gate 			strsendsig(stp->sd_siglist, S_MSG, (uchar_t)band, 0);
3934*7c478bd9Sstevel@tonic-gate 		break;
3935*7c478bd9Sstevel@tonic-gate 
3936*7c478bd9Sstevel@tonic-gate 	default:
3937*7c478bd9Sstevel@tonic-gate 		if (stp->sd_pgidp) {
3938*7c478bd9Sstevel@tonic-gate 			pgsignal(stp->sd_pgidp, sig);
3939*7c478bd9Sstevel@tonic-gate 		}
3940*7c478bd9Sstevel@tonic-gate 		break;
3941*7c478bd9Sstevel@tonic-gate 	}
3942*7c478bd9Sstevel@tonic-gate }
3943*7c478bd9Sstevel@tonic-gate 
3944*7c478bd9Sstevel@tonic-gate void
3945*7c478bd9Sstevel@tonic-gate strsignal(stdata_t *stp, int sig, int32_t band)
3946*7c478bd9Sstevel@tonic-gate {
3947*7c478bd9Sstevel@tonic-gate 	TRACE_3(TR_FAC_STREAMS_FR, TR_SENDSIG,
3948*7c478bd9Sstevel@tonic-gate 		"strsignal:%p, %X, %X", stp, sig, band);
3949*7c478bd9Sstevel@tonic-gate 
3950*7c478bd9Sstevel@tonic-gate 	mutex_enter(&stp->sd_lock);
3951*7c478bd9Sstevel@tonic-gate 	switch (sig) {
3952*7c478bd9Sstevel@tonic-gate 	case SIGPOLL:
3953*7c478bd9Sstevel@tonic-gate 		if (stp->sd_sigflags & S_MSG)
3954*7c478bd9Sstevel@tonic-gate 			strsendsig(stp->sd_siglist, S_MSG, (uchar_t)band, 0);
3955*7c478bd9Sstevel@tonic-gate 		break;
3956*7c478bd9Sstevel@tonic-gate 
3957*7c478bd9Sstevel@tonic-gate 	default:
3958*7c478bd9Sstevel@tonic-gate 		if (stp->sd_pgidp) {
3959*7c478bd9Sstevel@tonic-gate 			pgsignal(stp->sd_pgidp, sig);
3960*7c478bd9Sstevel@tonic-gate 		}
3961*7c478bd9Sstevel@tonic-gate 		break;
3962*7c478bd9Sstevel@tonic-gate 	}
3963*7c478bd9Sstevel@tonic-gate 	mutex_exit(&stp->sd_lock);
3964*7c478bd9Sstevel@tonic-gate }
3965*7c478bd9Sstevel@tonic-gate 
3966*7c478bd9Sstevel@tonic-gate void
3967*7c478bd9Sstevel@tonic-gate strhup(stdata_t *stp)
3968*7c478bd9Sstevel@tonic-gate {
3969*7c478bd9Sstevel@tonic-gate 	pollwakeup(&stp->sd_pollist, POLLHUP);
3970*7c478bd9Sstevel@tonic-gate 	mutex_enter(&stp->sd_lock);
3971*7c478bd9Sstevel@tonic-gate 	if (stp->sd_sigflags & S_HANGUP)
3972*7c478bd9Sstevel@tonic-gate 		strsendsig(stp->sd_siglist, S_HANGUP, 0, 0);
3973*7c478bd9Sstevel@tonic-gate 	mutex_exit(&stp->sd_lock);
3974*7c478bd9Sstevel@tonic-gate }
3975*7c478bd9Sstevel@tonic-gate 
3976*7c478bd9Sstevel@tonic-gate void
3977*7c478bd9Sstevel@tonic-gate stralloctty(sess_t *sp, stdata_t *stp)
3978*7c478bd9Sstevel@tonic-gate {
3979*7c478bd9Sstevel@tonic-gate 	mutex_enter(&stp->sd_lock);
3980*7c478bd9Sstevel@tonic-gate 	mutex_enter(&pidlock);
3981*7c478bd9Sstevel@tonic-gate 	stp->sd_sidp = sp->s_sidp;
3982*7c478bd9Sstevel@tonic-gate 	stp->sd_pgidp = sp->s_sidp;
3983*7c478bd9Sstevel@tonic-gate 	PID_HOLD(stp->sd_pgidp);
3984*7c478bd9Sstevel@tonic-gate 	PID_HOLD(stp->sd_sidp);
3985*7c478bd9Sstevel@tonic-gate 	mutex_exit(&pidlock);
3986*7c478bd9Sstevel@tonic-gate 	mutex_exit(&stp->sd_lock);
3987*7c478bd9Sstevel@tonic-gate }
3988*7c478bd9Sstevel@tonic-gate 
3989*7c478bd9Sstevel@tonic-gate void
3990*7c478bd9Sstevel@tonic-gate strfreectty(stdata_t *stp)
3991*7c478bd9Sstevel@tonic-gate {
3992*7c478bd9Sstevel@tonic-gate 	mutex_enter(&stp->sd_lock);
3993*7c478bd9Sstevel@tonic-gate 	pgsignal(stp->sd_pgidp, SIGHUP);
3994*7c478bd9Sstevel@tonic-gate 	mutex_enter(&pidlock);
3995*7c478bd9Sstevel@tonic-gate 	PID_RELE(stp->sd_pgidp);
3996*7c478bd9Sstevel@tonic-gate 	PID_RELE(stp->sd_sidp);
3997*7c478bd9Sstevel@tonic-gate 	stp->sd_pgidp = NULL;
3998*7c478bd9Sstevel@tonic-gate 	stp->sd_sidp = NULL;
3999*7c478bd9Sstevel@tonic-gate 	mutex_exit(&pidlock);
4000*7c478bd9Sstevel@tonic-gate 	mutex_exit(&stp->sd_lock);
4001*7c478bd9Sstevel@tonic-gate 	if (!(stp->sd_flag & STRHUP))
4002*7c478bd9Sstevel@tonic-gate 		strhup(stp);
4003*7c478bd9Sstevel@tonic-gate }
4004*7c478bd9Sstevel@tonic-gate 
4005*7c478bd9Sstevel@tonic-gate void
4006*7c478bd9Sstevel@tonic-gate strctty(stdata_t *stp)
4007*7c478bd9Sstevel@tonic-gate {
4008*7c478bd9Sstevel@tonic-gate 	extern vnode_t *makectty();
4009*7c478bd9Sstevel@tonic-gate 	proc_t *p = curproc;
4010*7c478bd9Sstevel@tonic-gate 	sess_t *sp = p->p_sessp;
4011*7c478bd9Sstevel@tonic-gate 
4012*7c478bd9Sstevel@tonic-gate 	mutex_enter(&stp->sd_lock);
4013*7c478bd9Sstevel@tonic-gate 	/*
4014*7c478bd9Sstevel@tonic-gate 	 * No need to hold the session lock or do a TTYHOLD,
4015*7c478bd9Sstevel@tonic-gate 	 * because this is the only thread that can be the
4016*7c478bd9Sstevel@tonic-gate 	 * session leader and not have a controlling tty.
4017*7c478bd9Sstevel@tonic-gate 	 */
4018*7c478bd9Sstevel@tonic-gate 	if ((stp->sd_flag & (STRHUP|STRDERR|STWRERR|STPLEX)) == 0 &&
4019*7c478bd9Sstevel@tonic-gate 	    stp->sd_sidp == NULL &&		/* not allocated as ctty */
4020*7c478bd9Sstevel@tonic-gate 	    sp->s_sidp == p->p_pidp &&		/* session leader */
4021*7c478bd9Sstevel@tonic-gate 	    sp->s_flag != SESS_CLOSE &&		/* session is not closing */
4022*7c478bd9Sstevel@tonic-gate 	    sp->s_vp == NULL) {			/* without ctty */
4023*7c478bd9Sstevel@tonic-gate 		mutex_exit(&stp->sd_lock);
4024*7c478bd9Sstevel@tonic-gate 		ASSERT(stp->sd_pgidp == NULL);
4025*7c478bd9Sstevel@tonic-gate 		alloctty(p, makectty(stp->sd_vnode));
4026*7c478bd9Sstevel@tonic-gate 		stralloctty(sp, stp);
4027*7c478bd9Sstevel@tonic-gate 		mutex_enter(&stp->sd_lock);
4028*7c478bd9Sstevel@tonic-gate 		stp->sd_flag |= STRISTTY;	/* just to be sure */
4029*7c478bd9Sstevel@tonic-gate 	}
4030*7c478bd9Sstevel@tonic-gate 	mutex_exit(&stp->sd_lock);
4031*7c478bd9Sstevel@tonic-gate }
4032*7c478bd9Sstevel@tonic-gate 
4033*7c478bd9Sstevel@tonic-gate /*
4034*7c478bd9Sstevel@tonic-gate  * enable first back queue with svc procedure.
4035*7c478bd9Sstevel@tonic-gate  * Use pri == -1 to avoid the setqback
4036*7c478bd9Sstevel@tonic-gate  */
4037*7c478bd9Sstevel@tonic-gate void
4038*7c478bd9Sstevel@tonic-gate backenable(queue_t *q, int pri)
4039*7c478bd9Sstevel@tonic-gate {
4040*7c478bd9Sstevel@tonic-gate 	queue_t	*nq;
4041*7c478bd9Sstevel@tonic-gate 
4042*7c478bd9Sstevel@tonic-gate 	/*
4043*7c478bd9Sstevel@tonic-gate 	 * our presence might not prevent other modules in our own
4044*7c478bd9Sstevel@tonic-gate 	 * stream from popping/pushing since the caller of getq might not
4045*7c478bd9Sstevel@tonic-gate 	 * have a claim on the queue (some drivers do a getq on somebody
4046*7c478bd9Sstevel@tonic-gate 	 * else's queue - they know that the queue itself is not going away
4047*7c478bd9Sstevel@tonic-gate 	 * but the framework has to guarantee q_next in that stream.)
4048*7c478bd9Sstevel@tonic-gate 	 */
4049*7c478bd9Sstevel@tonic-gate 	claimstr(q);
4050*7c478bd9Sstevel@tonic-gate 
4051*7c478bd9Sstevel@tonic-gate 	/* find nearest back queue with service proc */
4052*7c478bd9Sstevel@tonic-gate 	for (nq = backq(q); nq && !nq->q_qinfo->qi_srvp; nq = backq(nq)) {
4053*7c478bd9Sstevel@tonic-gate 		ASSERT(STRMATED(q->q_stream) || STREAM(q) == STREAM(nq));
4054*7c478bd9Sstevel@tonic-gate 	}
4055*7c478bd9Sstevel@tonic-gate 
4056*7c478bd9Sstevel@tonic-gate 	if (nq) {
4057*7c478bd9Sstevel@tonic-gate 		kthread_t *freezer;
4058*7c478bd9Sstevel@tonic-gate 		/*
4059*7c478bd9Sstevel@tonic-gate 		 * backenable can be called either with no locks held
4060*7c478bd9Sstevel@tonic-gate 		 * or with the stream frozen (the latter occurs when a module
4061*7c478bd9Sstevel@tonic-gate 		 * calls rmvq with the stream frozen.) If the stream is frozen
4062*7c478bd9Sstevel@tonic-gate 		 * by the caller the caller will hold all qlocks in the stream.
4063*7c478bd9Sstevel@tonic-gate 		 */
4064*7c478bd9Sstevel@tonic-gate 		freezer = STREAM(q)->sd_freezer;
4065*7c478bd9Sstevel@tonic-gate 		if (freezer != curthread) {
4066*7c478bd9Sstevel@tonic-gate 			mutex_enter(QLOCK(nq));
4067*7c478bd9Sstevel@tonic-gate 		}
4068*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
4069*7c478bd9Sstevel@tonic-gate 		else {
4070*7c478bd9Sstevel@tonic-gate 			ASSERT(frozenstr(q));
4071*7c478bd9Sstevel@tonic-gate 			ASSERT(MUTEX_HELD(QLOCK(q)));
4072*7c478bd9Sstevel@tonic-gate 			ASSERT(MUTEX_HELD(QLOCK(nq)));
4073*7c478bd9Sstevel@tonic-gate 		}
4074*7c478bd9Sstevel@tonic-gate #endif
4075*7c478bd9Sstevel@tonic-gate 		if (pri != -1)
4076*7c478bd9Sstevel@tonic-gate 			setqback(nq, pri);
4077*7c478bd9Sstevel@tonic-gate 		qenable_locked(nq);
4078*7c478bd9Sstevel@tonic-gate 		if (freezer != curthread)
4079*7c478bd9Sstevel@tonic-gate 			mutex_exit(QLOCK(nq));
4080*7c478bd9Sstevel@tonic-gate 	}
4081*7c478bd9Sstevel@tonic-gate 	releasestr(q);
4082*7c478bd9Sstevel@tonic-gate }
4083*7c478bd9Sstevel@tonic-gate 
4084*7c478bd9Sstevel@tonic-gate /*
4085*7c478bd9Sstevel@tonic-gate  * Return the appropriate errno when one of flags_to_check is set
4086*7c478bd9Sstevel@tonic-gate  * in sd_flags. Uses the exported error routines if they are set.
4087*7c478bd9Sstevel@tonic-gate  * Will return 0 if non error is set (or if the exported error routines
4088*7c478bd9Sstevel@tonic-gate  * do not return an error).
4089*7c478bd9Sstevel@tonic-gate  *
4090*7c478bd9Sstevel@tonic-gate  * If there is both a read and write error to check we prefer the read error.
4091*7c478bd9Sstevel@tonic-gate  * Also, give preference to recorded errno's over the error functions.
4092*7c478bd9Sstevel@tonic-gate  * The flags that are handled are:
4093*7c478bd9Sstevel@tonic-gate  *	STPLEX		return EINVAL
4094*7c478bd9Sstevel@tonic-gate  *	STRDERR		return sd_rerror (and clear if STRDERRNONPERSIST)
4095*7c478bd9Sstevel@tonic-gate  *	STWRERR		return sd_werror (and clear if STWRERRNONPERSIST)
4096*7c478bd9Sstevel@tonic-gate  *	STRHUP		return sd_werror
4097*7c478bd9Sstevel@tonic-gate  *
4098*7c478bd9Sstevel@tonic-gate  * If the caller indicates that the operation is a peek a nonpersistent error
4099*7c478bd9Sstevel@tonic-gate  * is not cleared.
4100*7c478bd9Sstevel@tonic-gate  */
4101*7c478bd9Sstevel@tonic-gate int
4102*7c478bd9Sstevel@tonic-gate strgeterr(stdata_t *stp, int32_t flags_to_check, int ispeek)
4103*7c478bd9Sstevel@tonic-gate {
4104*7c478bd9Sstevel@tonic-gate 	int32_t sd_flag = stp->sd_flag & flags_to_check;
4105*7c478bd9Sstevel@tonic-gate 	int error = 0;
4106*7c478bd9Sstevel@tonic-gate 
4107*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&stp->sd_lock));
4108*7c478bd9Sstevel@tonic-gate 	ASSERT((flags_to_check & ~(STRDERR|STWRERR|STRHUP|STPLEX)) == 0);
4109*7c478bd9Sstevel@tonic-gate 	if (sd_flag & STPLEX)
4110*7c478bd9Sstevel@tonic-gate 		error = EINVAL;
4111*7c478bd9Sstevel@tonic-gate 	else if (sd_flag & STRDERR) {
4112*7c478bd9Sstevel@tonic-gate 		error = stp->sd_rerror;
4113*7c478bd9Sstevel@tonic-gate 		if ((stp->sd_flag & STRDERRNONPERSIST) && !ispeek) {
4114*7c478bd9Sstevel@tonic-gate 			/*
4115*7c478bd9Sstevel@tonic-gate 			 * Read errors are non-persistent i.e. discarded once
4116*7c478bd9Sstevel@tonic-gate 			 * returned to a non-peeking caller,
4117*7c478bd9Sstevel@tonic-gate 			 */
4118*7c478bd9Sstevel@tonic-gate 			stp->sd_rerror = 0;
4119*7c478bd9Sstevel@tonic-gate 			stp->sd_flag &= ~STRDERR;
4120*7c478bd9Sstevel@tonic-gate 		}
4121*7c478bd9Sstevel@tonic-gate 		if (error == 0 && stp->sd_rderrfunc != NULL) {
4122*7c478bd9Sstevel@tonic-gate 			int clearerr = 0;
4123*7c478bd9Sstevel@tonic-gate 
4124*7c478bd9Sstevel@tonic-gate 			error = (*stp->sd_rderrfunc)(stp->sd_vnode, ispeek,
4125*7c478bd9Sstevel@tonic-gate 						&clearerr);
4126*7c478bd9Sstevel@tonic-gate 			if (clearerr) {
4127*7c478bd9Sstevel@tonic-gate 				stp->sd_flag &= ~STRDERR;
4128*7c478bd9Sstevel@tonic-gate 				stp->sd_rderrfunc = NULL;
4129*7c478bd9Sstevel@tonic-gate 			}
4130*7c478bd9Sstevel@tonic-gate 		}
4131*7c478bd9Sstevel@tonic-gate 	} else if (sd_flag & STWRERR) {
4132*7c478bd9Sstevel@tonic-gate 		error = stp->sd_werror;
4133*7c478bd9Sstevel@tonic-gate 		if ((stp->sd_flag & STWRERRNONPERSIST) && !ispeek) {
4134*7c478bd9Sstevel@tonic-gate 			/*
4135*7c478bd9Sstevel@tonic-gate 			 * Write errors are non-persistent i.e. discarded once
4136*7c478bd9Sstevel@tonic-gate 			 * returned to a non-peeking caller,
4137*7c478bd9Sstevel@tonic-gate 			 */
4138*7c478bd9Sstevel@tonic-gate 			stp->sd_werror = 0;
4139*7c478bd9Sstevel@tonic-gate 			stp->sd_flag &= ~STWRERR;
4140*7c478bd9Sstevel@tonic-gate 		}
4141*7c478bd9Sstevel@tonic-gate 		if (error == 0 && stp->sd_wrerrfunc != NULL) {
4142*7c478bd9Sstevel@tonic-gate 			int clearerr = 0;
4143*7c478bd9Sstevel@tonic-gate 
4144*7c478bd9Sstevel@tonic-gate 			error = (*stp->sd_wrerrfunc)(stp->sd_vnode, ispeek,
4145*7c478bd9Sstevel@tonic-gate 						&clearerr);
4146*7c478bd9Sstevel@tonic-gate 			if (clearerr) {
4147*7c478bd9Sstevel@tonic-gate 				stp->sd_flag &= ~STWRERR;
4148*7c478bd9Sstevel@tonic-gate 				stp->sd_wrerrfunc = NULL;
4149*7c478bd9Sstevel@tonic-gate 			}
4150*7c478bd9Sstevel@tonic-gate 		}
4151*7c478bd9Sstevel@tonic-gate 	} else if (sd_flag & STRHUP) {
4152*7c478bd9Sstevel@tonic-gate 		/* sd_werror set when STRHUP */
4153*7c478bd9Sstevel@tonic-gate 		error = stp->sd_werror;
4154*7c478bd9Sstevel@tonic-gate 	}
4155*7c478bd9Sstevel@tonic-gate 	return (error);
4156*7c478bd9Sstevel@tonic-gate }
4157*7c478bd9Sstevel@tonic-gate 
4158*7c478bd9Sstevel@tonic-gate 
4159*7c478bd9Sstevel@tonic-gate /*
4160*7c478bd9Sstevel@tonic-gate  * single-thread open/close/push/pop
4161*7c478bd9Sstevel@tonic-gate  * for twisted streams also
4162*7c478bd9Sstevel@tonic-gate  */
4163*7c478bd9Sstevel@tonic-gate int
4164*7c478bd9Sstevel@tonic-gate strstartplumb(stdata_t *stp, int flag, int cmd)
4165*7c478bd9Sstevel@tonic-gate {
4166*7c478bd9Sstevel@tonic-gate 	int waited = 1;
4167*7c478bd9Sstevel@tonic-gate 	int error = 0;
4168*7c478bd9Sstevel@tonic-gate 
4169*7c478bd9Sstevel@tonic-gate 	if (STRMATED(stp)) {
4170*7c478bd9Sstevel@tonic-gate 		struct stdata *stmatep = stp->sd_mate;
4171*7c478bd9Sstevel@tonic-gate 
4172*7c478bd9Sstevel@tonic-gate 		STRLOCKMATES(stp);
4173*7c478bd9Sstevel@tonic-gate 		while (waited) {
4174*7c478bd9Sstevel@tonic-gate 			waited = 0;
4175*7c478bd9Sstevel@tonic-gate 			while (stmatep->sd_flag & (STWOPEN|STRCLOSE|STRPLUMB)) {
4176*7c478bd9Sstevel@tonic-gate 				if ((cmd == I_POP) &&
4177*7c478bd9Sstevel@tonic-gate 				    (flag & (FNDELAY|FNONBLOCK))) {
4178*7c478bd9Sstevel@tonic-gate 					STRUNLOCKMATES(stp);
4179*7c478bd9Sstevel@tonic-gate 					return (EAGAIN);
4180*7c478bd9Sstevel@tonic-gate 				}
4181*7c478bd9Sstevel@tonic-gate 				waited = 1;
4182*7c478bd9Sstevel@tonic-gate 				mutex_exit(&stp->sd_lock);
4183*7c478bd9Sstevel@tonic-gate 				if (!cv_wait_sig(&stmatep->sd_monitor,
4184*7c478bd9Sstevel@tonic-gate 				    &stmatep->sd_lock)) {
4185*7c478bd9Sstevel@tonic-gate 					mutex_exit(&stmatep->sd_lock);
4186*7c478bd9Sstevel@tonic-gate 					return (EINTR);
4187*7c478bd9Sstevel@tonic-gate 				}
4188*7c478bd9Sstevel@tonic-gate 				mutex_exit(&stmatep->sd_lock);
4189*7c478bd9Sstevel@tonic-gate 				STRLOCKMATES(stp);
4190*7c478bd9Sstevel@tonic-gate 			}
4191*7c478bd9Sstevel@tonic-gate 			while (stp->sd_flag & (STWOPEN|STRCLOSE|STRPLUMB)) {
4192*7c478bd9Sstevel@tonic-gate 				if ((cmd == I_POP) &&
4193*7c478bd9Sstevel@tonic-gate 					(flag & (FNDELAY|FNONBLOCK))) {
4194*7c478bd9Sstevel@tonic-gate 					STRUNLOCKMATES(stp);
4195*7c478bd9Sstevel@tonic-gate 					return (EAGAIN);
4196*7c478bd9Sstevel@tonic-gate 				}
4197*7c478bd9Sstevel@tonic-gate 				waited = 1;
4198*7c478bd9Sstevel@tonic-gate 				mutex_exit(&stmatep->sd_lock);
4199*7c478bd9Sstevel@tonic-gate 				if (!cv_wait_sig(&stp->sd_monitor,
4200*7c478bd9Sstevel@tonic-gate 				    &stp->sd_lock)) {
4201*7c478bd9Sstevel@tonic-gate 					mutex_exit(&stp->sd_lock);
4202*7c478bd9Sstevel@tonic-gate 					return (EINTR);
4203*7c478bd9Sstevel@tonic-gate 				}
4204*7c478bd9Sstevel@tonic-gate 				mutex_exit(&stp->sd_lock);
4205*7c478bd9Sstevel@tonic-gate 				STRLOCKMATES(stp);
4206*7c478bd9Sstevel@tonic-gate 			}
4207*7c478bd9Sstevel@tonic-gate 			if (stp->sd_flag & (STRDERR|STWRERR|STRHUP|STPLEX)) {
4208*7c478bd9Sstevel@tonic-gate 				error = strgeterr(stp,
4209*7c478bd9Sstevel@tonic-gate 					STRDERR|STWRERR|STRHUP|STPLEX, 0);
4210*7c478bd9Sstevel@tonic-gate 				if (error != 0) {
4211*7c478bd9Sstevel@tonic-gate 					STRUNLOCKMATES(stp);
4212*7c478bd9Sstevel@tonic-gate 					return (error);
4213*7c478bd9Sstevel@tonic-gate 				}
4214*7c478bd9Sstevel@tonic-gate 			}
4215*7c478bd9Sstevel@tonic-gate 		}
4216*7c478bd9Sstevel@tonic-gate 		stp->sd_flag |= STRPLUMB;
4217*7c478bd9Sstevel@tonic-gate 		STRUNLOCKMATES(stp);
4218*7c478bd9Sstevel@tonic-gate 	} else {
4219*7c478bd9Sstevel@tonic-gate 		mutex_enter(&stp->sd_lock);
4220*7c478bd9Sstevel@tonic-gate 		while (stp->sd_flag & (STWOPEN|STRCLOSE|STRPLUMB)) {
4221*7c478bd9Sstevel@tonic-gate 			if (((cmd == I_POP) || (cmd == _I_REMOVE)) &&
4222*7c478bd9Sstevel@tonic-gate 			    (flag & (FNDELAY|FNONBLOCK))) {
4223*7c478bd9Sstevel@tonic-gate 				mutex_exit(&stp->sd_lock);
4224*7c478bd9Sstevel@tonic-gate 				return (EAGAIN);
4225*7c478bd9Sstevel@tonic-gate 			}
4226*7c478bd9Sstevel@tonic-gate 			if (!cv_wait_sig(&stp->sd_monitor, &stp->sd_lock)) {
4227*7c478bd9Sstevel@tonic-gate 				mutex_exit(&stp->sd_lock);
4228*7c478bd9Sstevel@tonic-gate 				return (EINTR);
4229*7c478bd9Sstevel@tonic-gate 			}
4230*7c478bd9Sstevel@tonic-gate 			if (stp->sd_flag & (STRDERR|STWRERR|STRHUP|STPLEX)) {
4231*7c478bd9Sstevel@tonic-gate 				error = strgeterr(stp,
4232*7c478bd9Sstevel@tonic-gate 					STRDERR|STWRERR|STRHUP|STPLEX, 0);
4233*7c478bd9Sstevel@tonic-gate 				if (error != 0) {
4234*7c478bd9Sstevel@tonic-gate 					mutex_exit(&stp->sd_lock);
4235*7c478bd9Sstevel@tonic-gate 					return (error);
4236*7c478bd9Sstevel@tonic-gate 				}
4237*7c478bd9Sstevel@tonic-gate 			}
4238*7c478bd9Sstevel@tonic-gate 		}
4239*7c478bd9Sstevel@tonic-gate 		stp->sd_flag |= STRPLUMB;
4240*7c478bd9Sstevel@tonic-gate 		mutex_exit(&stp->sd_lock);
4241*7c478bd9Sstevel@tonic-gate 	}
4242*7c478bd9Sstevel@tonic-gate 	return (0);
4243*7c478bd9Sstevel@tonic-gate }
4244*7c478bd9Sstevel@tonic-gate 
4245*7c478bd9Sstevel@tonic-gate /*
4246*7c478bd9Sstevel@tonic-gate  * Complete the plumbing operation associated with stream `stp'.
4247*7c478bd9Sstevel@tonic-gate  */
4248*7c478bd9Sstevel@tonic-gate void
4249*7c478bd9Sstevel@tonic-gate strendplumb(stdata_t *stp)
4250*7c478bd9Sstevel@tonic-gate {
4251*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&stp->sd_lock));
4252*7c478bd9Sstevel@tonic-gate 	ASSERT(stp->sd_flag & STRPLUMB);
4253*7c478bd9Sstevel@tonic-gate 	stp->sd_flag &= ~STRPLUMB;
4254*7c478bd9Sstevel@tonic-gate 	cv_broadcast(&stp->sd_monitor);
4255*7c478bd9Sstevel@tonic-gate }
4256*7c478bd9Sstevel@tonic-gate 
4257*7c478bd9Sstevel@tonic-gate /*
4258*7c478bd9Sstevel@tonic-gate  * This describes how the STREAMS framework handles synchronization
4259*7c478bd9Sstevel@tonic-gate  * during open/push and close/pop.
4260*7c478bd9Sstevel@tonic-gate  * The key interfaces for open and close are qprocson and qprocsoff,
4261*7c478bd9Sstevel@tonic-gate  * respectively. While the close case in general is harder both open
4262*7c478bd9Sstevel@tonic-gate  * have close have significant similarities.
4263*7c478bd9Sstevel@tonic-gate  *
4264*7c478bd9Sstevel@tonic-gate  * During close the STREAMS framework has to both ensure that there
4265*7c478bd9Sstevel@tonic-gate  * are no stale references to the queue pair (and syncq) that
4266*7c478bd9Sstevel@tonic-gate  * are being closed and also provide the guarantees that are documented
4267*7c478bd9Sstevel@tonic-gate  * in qprocsoff(9F).
4268*7c478bd9Sstevel@tonic-gate  * If there are stale references to the queue that is closing it can
4269*7c478bd9Sstevel@tonic-gate  * result in kernel memory corruption or kernel panics.
4270*7c478bd9Sstevel@tonic-gate  *
4271*7c478bd9Sstevel@tonic-gate  * Note that is it up to the module/driver to ensure that it itself
4272*7c478bd9Sstevel@tonic-gate  * does not have any stale references to the closing queues once its close
4273*7c478bd9Sstevel@tonic-gate  * routine returns. This includes:
4274*7c478bd9Sstevel@tonic-gate  *  - Cancelling any timeout/bufcall/qtimeout/qbufcall callback routines
4275*7c478bd9Sstevel@tonic-gate  *    associated with the queues. For timeout and bufcall callbacks the
4276*7c478bd9Sstevel@tonic-gate  *    module/driver also has to ensure (or wait for) any callbacks that
4277*7c478bd9Sstevel@tonic-gate  *    are in progress.
4278*7c478bd9Sstevel@tonic-gate  *  - If the module/driver is using esballoc it has to ensure that any
4279*7c478bd9Sstevel@tonic-gate  *    esballoc free functions do not refer to a queue that has closed.
4280*7c478bd9Sstevel@tonic-gate  *    (Note that in general the close routine can not wait for the esballoc'ed
4281*7c478bd9Sstevel@tonic-gate  *    messages to be freed since that can cause a deadlock.)
4282*7c478bd9Sstevel@tonic-gate  *  - Cancelling any interrupts that refer to the closing queues and
4283*7c478bd9Sstevel@tonic-gate  *    also ensuring that there are no interrupts in progress that will
4284*7c478bd9Sstevel@tonic-gate  *    refer to the closing queues once the close routine returns.
4285*7c478bd9Sstevel@tonic-gate  *  - For multiplexors removing any driver global state that refers to
4286*7c478bd9Sstevel@tonic-gate  *    the closing queue and also ensuring that there are no threads in
4287*7c478bd9Sstevel@tonic-gate  *    the multiplexor that has picked up a queue pointer but not yet
4288*7c478bd9Sstevel@tonic-gate  *    finished using it.
4289*7c478bd9Sstevel@tonic-gate  *
4290*7c478bd9Sstevel@tonic-gate  * In addition, a driver/module can only reference the q_next pointer
4291*7c478bd9Sstevel@tonic-gate  * in its open, close, put, or service procedures or in a
4292*7c478bd9Sstevel@tonic-gate  * qtimeout/qbufcall callback procedure executing "on" the correct
4293*7c478bd9Sstevel@tonic-gate  * stream. Thus it can not reference the q_next pointer in an interrupt
4294*7c478bd9Sstevel@tonic-gate  * routine or a timeout, bufcall or esballoc callback routine. Likewise
4295*7c478bd9Sstevel@tonic-gate  * it can not reference q_next of a different queue e.g. in a mux that
4296*7c478bd9Sstevel@tonic-gate  * passes messages from one queues put/service procedure to another queue.
4297*7c478bd9Sstevel@tonic-gate  * In all the cases when the driver/module can not access the q_next
4298*7c478bd9Sstevel@tonic-gate  * field it must use the *next* versions e.g. canputnext instead of
4299*7c478bd9Sstevel@tonic-gate  * canput(q->q_next) and putnextctl instead of putctl(q->q_next, ...).
4300*7c478bd9Sstevel@tonic-gate  *
4301*7c478bd9Sstevel@tonic-gate  *
4302*7c478bd9Sstevel@tonic-gate  * Assuming that the driver/module conforms to the above constraints
4303*7c478bd9Sstevel@tonic-gate  * the STREAMS framework has to avoid stale references to q_next for all
4304*7c478bd9Sstevel@tonic-gate  * the framework internal cases which include (but are not limited to):
4305*7c478bd9Sstevel@tonic-gate  *  - Threads in canput/canputnext/backenable and elsewhere that are
4306*7c478bd9Sstevel@tonic-gate  *    walking q_next.
4307*7c478bd9Sstevel@tonic-gate  *  - Messages on a syncq that have a reference to the queue through b_queue.
4308*7c478bd9Sstevel@tonic-gate  *  - Messages on an outer perimeter (syncq) that have a reference to the
4309*7c478bd9Sstevel@tonic-gate  *    queue through b_queue.
4310*7c478bd9Sstevel@tonic-gate  *  - Threads that use q_nfsrv (e.g. canput) to find a queue.
4311*7c478bd9Sstevel@tonic-gate  *    Note that only canput and bcanput use q_nfsrv without any locking.
4312*7c478bd9Sstevel@tonic-gate  *
4313*7c478bd9Sstevel@tonic-gate  * The STREAMS framework providing the qprocsoff(9F) guarantees means that
4314*7c478bd9Sstevel@tonic-gate  * after qprocsoff returns, the framework has to ensure that no threads can
4315*7c478bd9Sstevel@tonic-gate  * enter the put or service routines for the closing read or write-side queue.
4316*7c478bd9Sstevel@tonic-gate  * In addition to preventing "direct" entry into the put procedures
4317*7c478bd9Sstevel@tonic-gate  * the framework also has to prevent messages being drained from
4318*7c478bd9Sstevel@tonic-gate  * the syncq or the outer perimeter.
4319*7c478bd9Sstevel@tonic-gate  * XXX Note that currently qdetach does relies on D_MTOCEXCL as the only
4320*7c478bd9Sstevel@tonic-gate  * mechanism to prevent qwriter(PERIM_OUTER) from running after
4321*7c478bd9Sstevel@tonic-gate  * qprocsoff has returned.
4322*7c478bd9Sstevel@tonic-gate  * Note that if a module/driver uses put(9F) on one of its own queues
4323*7c478bd9Sstevel@tonic-gate  * it is up to the module/driver to ensure that the put() doesn't
4324*7c478bd9Sstevel@tonic-gate  * get called when the queue is closing.
4325*7c478bd9Sstevel@tonic-gate  *
4326*7c478bd9Sstevel@tonic-gate  *
4327*7c478bd9Sstevel@tonic-gate  * The framework aspects of the above "contract" is implemented by
4328*7c478bd9Sstevel@tonic-gate  * qprocsoff, removeq, and strlock:
4329*7c478bd9Sstevel@tonic-gate  *  - qprocsoff (disable_svc) sets QWCLOSE to prevent runservice from
4330*7c478bd9Sstevel@tonic-gate  *    entering the service procedures.
4331*7c478bd9Sstevel@tonic-gate  *  - strlock acquires the sd_lock and sd_reflock to prevent putnext,
4332*7c478bd9Sstevel@tonic-gate  *    canputnext, backenable etc from dereferencing the q_next that will
4333*7c478bd9Sstevel@tonic-gate  *    soon change.
4334*7c478bd9Sstevel@tonic-gate  *  - strlock waits for sd_refcnt to be zero to wait for e.g. any canputnext
4335*7c478bd9Sstevel@tonic-gate  *    or other q_next walker that uses claimstr/releasestr to finish.
4336*7c478bd9Sstevel@tonic-gate  *  - optionally for every syncq in the stream strlock acquires all the
4337*7c478bd9Sstevel@tonic-gate  *    sq_lock's and waits for all sq_counts to drop to a value that indicates
4338*7c478bd9Sstevel@tonic-gate  *    that no thread executes in the put or service procedures and that no
4339*7c478bd9Sstevel@tonic-gate  *    thread is draining into the module/driver. This ensures that no
4340*7c478bd9Sstevel@tonic-gate  *    open, close, put, service, or qtimeout/qbufcall callback procedure is
4341*7c478bd9Sstevel@tonic-gate  *    currently executing hence no such thread can end up with the old stale
4342*7c478bd9Sstevel@tonic-gate  *    q_next value and no canput/backenable can have the old stale
4343*7c478bd9Sstevel@tonic-gate  *    q_nfsrv/q_next.
4344*7c478bd9Sstevel@tonic-gate  *  - qdetach (wait_svc) makes sure that any scheduled or running threads
4345*7c478bd9Sstevel@tonic-gate  *    have either finished or observed the QWCLOSE flag and gone away.
4346*7c478bd9Sstevel@tonic-gate  */
4347*7c478bd9Sstevel@tonic-gate 
4348*7c478bd9Sstevel@tonic-gate 
4349*7c478bd9Sstevel@tonic-gate /*
4350*7c478bd9Sstevel@tonic-gate  * Get all the locks necessary to change q_next.
4351*7c478bd9Sstevel@tonic-gate  *
4352*7c478bd9Sstevel@tonic-gate  * Wait for sd_refcnt to reach 0 and, if sqlist is present, wait for  the
4353*7c478bd9Sstevel@tonic-gate  * sq_count of each syncq in the list to drop to sq_rmqcount, indicating that
4354*7c478bd9Sstevel@tonic-gate  * the only threads inside the sqncq are threads currently calling removeq().
4355*7c478bd9Sstevel@tonic-gate  * Since threads calling removeq() are in the process of removing their queues
4356*7c478bd9Sstevel@tonic-gate  * from the stream, we do not need to worry about them accessing a stale q_next
4357*7c478bd9Sstevel@tonic-gate  * pointer and thus we do not need to wait for them to exit (in fact, waiting
4358*7c478bd9Sstevel@tonic-gate  * for them can cause deadlock).
4359*7c478bd9Sstevel@tonic-gate  *
4360*7c478bd9Sstevel@tonic-gate  * This routine is subject to starvation since it does not set any flag to
4361*7c478bd9Sstevel@tonic-gate  * prevent threads from entering a module in the stream(i.e. sq_count can
4362*7c478bd9Sstevel@tonic-gate  * increase on some syncq while it is waiting on some other syncq.)
4363*7c478bd9Sstevel@tonic-gate  *
4364*7c478bd9Sstevel@tonic-gate  * Assumes that only one thread attempts to call strlock for a given
4365*7c478bd9Sstevel@tonic-gate  * stream. If this is not the case the two threads would deadlock.
4366*7c478bd9Sstevel@tonic-gate  * This assumption is guaranteed since strlock is only called by insertq
4367*7c478bd9Sstevel@tonic-gate  * and removeq and streams plumbing changes are single-threaded for
4368*7c478bd9Sstevel@tonic-gate  * a given stream using the STWOPEN, STRCLOSE, and STRPLUMB flags.
4369*7c478bd9Sstevel@tonic-gate  *
4370*7c478bd9Sstevel@tonic-gate  * For pipes, it is not difficult to atomically designate a pair of streams
4371*7c478bd9Sstevel@tonic-gate  * to be mated. Once mated atomically by the framework the twisted pair remain
4372*7c478bd9Sstevel@tonic-gate  * configured that way until dismantled atomically by the framework.
4373*7c478bd9Sstevel@tonic-gate  * When plumbing takes place on a twisted stream it is necessary to ensure that
4374*7c478bd9Sstevel@tonic-gate  * this operation is done exclusively on the twisted stream since two such
4375*7c478bd9Sstevel@tonic-gate  * operations, each initiated on different ends of the pipe will deadlock
4376*7c478bd9Sstevel@tonic-gate  * waiting for each other to complete.
4377*7c478bd9Sstevel@tonic-gate  *
4378*7c478bd9Sstevel@tonic-gate  * On entry, no locks should be held.
4379*7c478bd9Sstevel@tonic-gate  * The locks acquired and held by strlock depends on a few factors.
4380*7c478bd9Sstevel@tonic-gate  * - If sqlist is non-NULL all the syncq locks in the sqlist will be acquired
4381*7c478bd9Sstevel@tonic-gate  *   and held on exit and all sq_count are at an acceptable level.
4382*7c478bd9Sstevel@tonic-gate  * - In all cases, sd_lock and sd_reflock are acquired and held on exit with
4383*7c478bd9Sstevel@tonic-gate  *   sd_refcnt being zero.
4384*7c478bd9Sstevel@tonic-gate  */
4385*7c478bd9Sstevel@tonic-gate 
4386*7c478bd9Sstevel@tonic-gate static void
4387*7c478bd9Sstevel@tonic-gate strlock(struct stdata *stp, sqlist_t *sqlist)
4388*7c478bd9Sstevel@tonic-gate {
4389*7c478bd9Sstevel@tonic-gate 	syncql_t *sql, *sql2;
4390*7c478bd9Sstevel@tonic-gate retry:
4391*7c478bd9Sstevel@tonic-gate 	/*
4392*7c478bd9Sstevel@tonic-gate 	 * Wait for any claimstr to go away.
4393*7c478bd9Sstevel@tonic-gate 	 */
4394*7c478bd9Sstevel@tonic-gate 	if (STRMATED(stp)) {
4395*7c478bd9Sstevel@tonic-gate 		struct stdata *stp1, *stp2;
4396*7c478bd9Sstevel@tonic-gate 
4397*7c478bd9Sstevel@tonic-gate 		STRLOCKMATES(stp);
4398*7c478bd9Sstevel@tonic-gate 		/*
4399*7c478bd9Sstevel@tonic-gate 		 * Note that the selection of locking order is not
4400*7c478bd9Sstevel@tonic-gate 		 * important, just that they are always aquired in
4401*7c478bd9Sstevel@tonic-gate 		 * the same order.  To assure this, we choose this
4402*7c478bd9Sstevel@tonic-gate 		 * order based on the value of the pointer, and since
4403*7c478bd9Sstevel@tonic-gate 		 * the pointer will not change for the life of this
4404*7c478bd9Sstevel@tonic-gate 		 * pair, we will always grab the locks in the same
4405*7c478bd9Sstevel@tonic-gate 		 * order (and hence, prevent deadlocks).
4406*7c478bd9Sstevel@tonic-gate 		 */
4407*7c478bd9Sstevel@tonic-gate 		if (&(stp->sd_lock) > &((stp->sd_mate)->sd_lock)) {
4408*7c478bd9Sstevel@tonic-gate 			stp1 = stp;
4409*7c478bd9Sstevel@tonic-gate 			stp2 = stp->sd_mate;
4410*7c478bd9Sstevel@tonic-gate 		} else {
4411*7c478bd9Sstevel@tonic-gate 			stp2 = stp;
4412*7c478bd9Sstevel@tonic-gate 			stp1 = stp->sd_mate;
4413*7c478bd9Sstevel@tonic-gate 		}
4414*7c478bd9Sstevel@tonic-gate 		mutex_enter(&stp1->sd_reflock);
4415*7c478bd9Sstevel@tonic-gate 		if (stp1->sd_refcnt > 0) {
4416*7c478bd9Sstevel@tonic-gate 			STRUNLOCKMATES(stp);
4417*7c478bd9Sstevel@tonic-gate 			cv_wait(&stp1->sd_monitor, &stp1->sd_reflock);
4418*7c478bd9Sstevel@tonic-gate 			mutex_exit(&stp1->sd_reflock);
4419*7c478bd9Sstevel@tonic-gate 			goto retry;
4420*7c478bd9Sstevel@tonic-gate 		}
4421*7c478bd9Sstevel@tonic-gate 		mutex_enter(&stp2->sd_reflock);
4422*7c478bd9Sstevel@tonic-gate 		if (stp2->sd_refcnt > 0) {
4423*7c478bd9Sstevel@tonic-gate 			STRUNLOCKMATES(stp);
4424*7c478bd9Sstevel@tonic-gate 			mutex_exit(&stp1->sd_reflock);
4425*7c478bd9Sstevel@tonic-gate 			cv_wait(&stp2->sd_monitor, &stp2->sd_reflock);
4426*7c478bd9Sstevel@tonic-gate 			mutex_exit(&stp2->sd_reflock);
4427*7c478bd9Sstevel@tonic-gate 			goto retry;
4428*7c478bd9Sstevel@tonic-gate 		}
4429*7c478bd9Sstevel@tonic-gate 		STREAM_PUTLOCKS_ENTER(stp1);
4430*7c478bd9Sstevel@tonic-gate 		STREAM_PUTLOCKS_ENTER(stp2);
4431*7c478bd9Sstevel@tonic-gate 	} else {
4432*7c478bd9Sstevel@tonic-gate 		mutex_enter(&stp->sd_lock);
4433*7c478bd9Sstevel@tonic-gate 		mutex_enter(&stp->sd_reflock);
4434*7c478bd9Sstevel@tonic-gate 		while (stp->sd_refcnt > 0) {
4435*7c478bd9Sstevel@tonic-gate 			mutex_exit(&stp->sd_lock);
4436*7c478bd9Sstevel@tonic-gate 			cv_wait(&stp->sd_monitor, &stp->sd_reflock);
4437*7c478bd9Sstevel@tonic-gate 			if (mutex_tryenter(&stp->sd_lock) == 0) {
4438*7c478bd9Sstevel@tonic-gate 				mutex_exit(&stp->sd_reflock);
4439*7c478bd9Sstevel@tonic-gate 				mutex_enter(&stp->sd_lock);
4440*7c478bd9Sstevel@tonic-gate 				mutex_enter(&stp->sd_reflock);
4441*7c478bd9Sstevel@tonic-gate 			}
4442*7c478bd9Sstevel@tonic-gate 		}
4443*7c478bd9Sstevel@tonic-gate 		STREAM_PUTLOCKS_ENTER(stp);
4444*7c478bd9Sstevel@tonic-gate 	}
4445*7c478bd9Sstevel@tonic-gate 
4446*7c478bd9Sstevel@tonic-gate 	if (sqlist == NULL)
4447*7c478bd9Sstevel@tonic-gate 		return;
4448*7c478bd9Sstevel@tonic-gate 
4449*7c478bd9Sstevel@tonic-gate 	for (sql = sqlist->sqlist_head; sql; sql = sql->sql_next) {
4450*7c478bd9Sstevel@tonic-gate 		syncq_t *sq = sql->sql_sq;
4451*7c478bd9Sstevel@tonic-gate 		uint16_t count;
4452*7c478bd9Sstevel@tonic-gate 
4453*7c478bd9Sstevel@tonic-gate 		mutex_enter(SQLOCK(sq));
4454*7c478bd9Sstevel@tonic-gate 		count = sq->sq_count;
4455*7c478bd9Sstevel@tonic-gate 		ASSERT(sq->sq_rmqcount <= count);
4456*7c478bd9Sstevel@tonic-gate 		SQ_PUTLOCKS_ENTER(sq);
4457*7c478bd9Sstevel@tonic-gate 		SUM_SQ_PUTCOUNTS(sq, count);
4458*7c478bd9Sstevel@tonic-gate 		if (count == sq->sq_rmqcount)
4459*7c478bd9Sstevel@tonic-gate 			continue;
4460*7c478bd9Sstevel@tonic-gate 
4461*7c478bd9Sstevel@tonic-gate 		/* Failed - drop all locks that we have acquired so far */
4462*7c478bd9Sstevel@tonic-gate 		if (STRMATED(stp)) {
4463*7c478bd9Sstevel@tonic-gate 			STREAM_PUTLOCKS_EXIT(stp);
4464*7c478bd9Sstevel@tonic-gate 			STREAM_PUTLOCKS_EXIT(stp->sd_mate);
4465*7c478bd9Sstevel@tonic-gate 			STRUNLOCKMATES(stp);
4466*7c478bd9Sstevel@tonic-gate 			mutex_exit(&stp->sd_reflock);
4467*7c478bd9Sstevel@tonic-gate 			mutex_exit(&stp->sd_mate->sd_reflock);
4468*7c478bd9Sstevel@tonic-gate 		} else {
4469*7c478bd9Sstevel@tonic-gate 			STREAM_PUTLOCKS_EXIT(stp);
4470*7c478bd9Sstevel@tonic-gate 			mutex_exit(&stp->sd_lock);
4471*7c478bd9Sstevel@tonic-gate 			mutex_exit(&stp->sd_reflock);
4472*7c478bd9Sstevel@tonic-gate 		}
4473*7c478bd9Sstevel@tonic-gate 		for (sql2 = sqlist->sqlist_head; sql2 != sql;
4474*7c478bd9Sstevel@tonic-gate 		    sql2 = sql2->sql_next) {
4475*7c478bd9Sstevel@tonic-gate 			SQ_PUTLOCKS_EXIT(sql2->sql_sq);
4476*7c478bd9Sstevel@tonic-gate 			mutex_exit(SQLOCK(sql2->sql_sq));
4477*7c478bd9Sstevel@tonic-gate 		}
4478*7c478bd9Sstevel@tonic-gate 
4479*7c478bd9Sstevel@tonic-gate 		/*
4480*7c478bd9Sstevel@tonic-gate 		 * The wait loop below may starve when there are many threads
4481*7c478bd9Sstevel@tonic-gate 		 * claiming the syncq. This is especially a problem with permod
4482*7c478bd9Sstevel@tonic-gate 		 * syncqs (IP). To lessen the impact of the problem we increment
4483*7c478bd9Sstevel@tonic-gate 		 * sq_needexcl and clear fastbits so that putnexts will slow
4484*7c478bd9Sstevel@tonic-gate 		 * down and call sqenable instead of draining right away.
4485*7c478bd9Sstevel@tonic-gate 		 */
4486*7c478bd9Sstevel@tonic-gate 		sq->sq_needexcl++;
4487*7c478bd9Sstevel@tonic-gate 		SQ_PUTCOUNT_CLRFAST_LOCKED(sq);
4488*7c478bd9Sstevel@tonic-gate 		while (count > sq->sq_rmqcount) {
4489*7c478bd9Sstevel@tonic-gate 			sq->sq_flags |= SQ_WANTWAKEUP;
4490*7c478bd9Sstevel@tonic-gate 			SQ_PUTLOCKS_EXIT(sq);
4491*7c478bd9Sstevel@tonic-gate 			cv_wait(&sq->sq_wait, SQLOCK(sq));
4492*7c478bd9Sstevel@tonic-gate 			count = sq->sq_count;
4493*7c478bd9Sstevel@tonic-gate 			SQ_PUTLOCKS_ENTER(sq);
4494*7c478bd9Sstevel@tonic-gate 			SUM_SQ_PUTCOUNTS(sq, count);
4495*7c478bd9Sstevel@tonic-gate 		}
4496*7c478bd9Sstevel@tonic-gate 		sq->sq_needexcl--;
4497*7c478bd9Sstevel@tonic-gate 		if (sq->sq_needexcl == 0)
4498*7c478bd9Sstevel@tonic-gate 			SQ_PUTCOUNT_SETFAST_LOCKED(sq);
4499*7c478bd9Sstevel@tonic-gate 		SQ_PUTLOCKS_EXIT(sq);
4500*7c478bd9Sstevel@tonic-gate 		ASSERT(count == sq->sq_rmqcount);
4501*7c478bd9Sstevel@tonic-gate 		mutex_exit(SQLOCK(sq));
4502*7c478bd9Sstevel@tonic-gate 		goto retry;
4503*7c478bd9Sstevel@tonic-gate 	}
4504*7c478bd9Sstevel@tonic-gate }
4505*7c478bd9Sstevel@tonic-gate 
4506*7c478bd9Sstevel@tonic-gate /*
4507*7c478bd9Sstevel@tonic-gate  * Drop all the locks that strlock acquired.
4508*7c478bd9Sstevel@tonic-gate  */
4509*7c478bd9Sstevel@tonic-gate static void
4510*7c478bd9Sstevel@tonic-gate strunlock(struct stdata *stp, sqlist_t *sqlist)
4511*7c478bd9Sstevel@tonic-gate {
4512*7c478bd9Sstevel@tonic-gate 	syncql_t *sql;
4513*7c478bd9Sstevel@tonic-gate 
4514*7c478bd9Sstevel@tonic-gate 	if (STRMATED(stp)) {
4515*7c478bd9Sstevel@tonic-gate 		STREAM_PUTLOCKS_EXIT(stp);
4516*7c478bd9Sstevel@tonic-gate 		STREAM_PUTLOCKS_EXIT(stp->sd_mate);
4517*7c478bd9Sstevel@tonic-gate 		STRUNLOCKMATES(stp);
4518*7c478bd9Sstevel@tonic-gate 		mutex_exit(&stp->sd_reflock);
4519*7c478bd9Sstevel@tonic-gate 		mutex_exit(&stp->sd_mate->sd_reflock);
4520*7c478bd9Sstevel@tonic-gate 	} else {
4521*7c478bd9Sstevel@tonic-gate 		STREAM_PUTLOCKS_EXIT(stp);
4522*7c478bd9Sstevel@tonic-gate 		mutex_exit(&stp->sd_lock);
4523*7c478bd9Sstevel@tonic-gate 		mutex_exit(&stp->sd_reflock);
4524*7c478bd9Sstevel@tonic-gate 	}
4525*7c478bd9Sstevel@tonic-gate 
4526*7c478bd9Sstevel@tonic-gate 	if (sqlist == NULL)
4527*7c478bd9Sstevel@tonic-gate 		return;
4528*7c478bd9Sstevel@tonic-gate 
4529*7c478bd9Sstevel@tonic-gate 	for (sql = sqlist->sqlist_head; sql; sql = sql->sql_next) {
4530*7c478bd9Sstevel@tonic-gate 		SQ_PUTLOCKS_EXIT(sql->sql_sq);
4531*7c478bd9Sstevel@tonic-gate 		mutex_exit(SQLOCK(sql->sql_sq));
4532*7c478bd9Sstevel@tonic-gate 	}
4533*7c478bd9Sstevel@tonic-gate }
4534*7c478bd9Sstevel@tonic-gate 
4535*7c478bd9Sstevel@tonic-gate 
4536*7c478bd9Sstevel@tonic-gate /*
4537*7c478bd9Sstevel@tonic-gate  * Given two read queues, insert a new single one after another.
4538*7c478bd9Sstevel@tonic-gate  *
4539*7c478bd9Sstevel@tonic-gate  * This routine acquires all the necessary locks in order to change
4540*7c478bd9Sstevel@tonic-gate  * q_next and related pointer using strlock().
4541*7c478bd9Sstevel@tonic-gate  * It depends on the stream head ensuring that there are no concurrent
4542*7c478bd9Sstevel@tonic-gate  * insertq or removeq on the same stream. The stream head ensures this
4543*7c478bd9Sstevel@tonic-gate  * using the flags STWOPEN, STRCLOSE, and STRPLUMB.
4544*7c478bd9Sstevel@tonic-gate  *
4545*7c478bd9Sstevel@tonic-gate  * Note that no syncq locks are held during the q_next change. This is
4546*7c478bd9Sstevel@tonic-gate  * applied to all streams since, unlike removeq, there is no problem of stale
4547*7c478bd9Sstevel@tonic-gate  * pointers when adding a module to the stream. Thus drivers/modules that do a
4548*7c478bd9Sstevel@tonic-gate  * canput(rq->q_next) would never get a closed/freed queue pointer even if we
4549*7c478bd9Sstevel@tonic-gate  * applied this optimization to all streams.
4550*7c478bd9Sstevel@tonic-gate  */
4551*7c478bd9Sstevel@tonic-gate void
4552*7c478bd9Sstevel@tonic-gate insertq(struct stdata *stp, queue_t *new)
4553*7c478bd9Sstevel@tonic-gate {
4554*7c478bd9Sstevel@tonic-gate 	queue_t	*after;
4555*7c478bd9Sstevel@tonic-gate 	queue_t *wafter;
4556*7c478bd9Sstevel@tonic-gate 	queue_t *wnew = _WR(new);
4557*7c478bd9Sstevel@tonic-gate 	boolean_t have_fifo = B_FALSE;
4558*7c478bd9Sstevel@tonic-gate 
4559*7c478bd9Sstevel@tonic-gate 	if (new->q_flag & _QINSERTING) {
4560*7c478bd9Sstevel@tonic-gate 		ASSERT(stp->sd_vnode->v_type != VFIFO);
4561*7c478bd9Sstevel@tonic-gate 		after = new->q_next;
4562*7c478bd9Sstevel@tonic-gate 		wafter = _WR(new->q_next);
4563*7c478bd9Sstevel@tonic-gate 	} else {
4564*7c478bd9Sstevel@tonic-gate 		after = _RD(stp->sd_wrq);
4565*7c478bd9Sstevel@tonic-gate 		wafter = stp->sd_wrq;
4566*7c478bd9Sstevel@tonic-gate 	}
4567*7c478bd9Sstevel@tonic-gate 
4568*7c478bd9Sstevel@tonic-gate 	TRACE_2(TR_FAC_STREAMS_FR, TR_INSERTQ,
4569*7c478bd9Sstevel@tonic-gate 		"insertq:%p, %p", after, new);
4570*7c478bd9Sstevel@tonic-gate 	ASSERT(after->q_flag & QREADR);
4571*7c478bd9Sstevel@tonic-gate 	ASSERT(new->q_flag & QREADR);
4572*7c478bd9Sstevel@tonic-gate 
4573*7c478bd9Sstevel@tonic-gate 	strlock(stp, NULL);
4574*7c478bd9Sstevel@tonic-gate 
4575*7c478bd9Sstevel@tonic-gate 	/* Do we have a FIFO? */
4576*7c478bd9Sstevel@tonic-gate 	if (wafter->q_next == after) {
4577*7c478bd9Sstevel@tonic-gate 		have_fifo = B_TRUE;
4578*7c478bd9Sstevel@tonic-gate 		wnew->q_next = new;
4579*7c478bd9Sstevel@tonic-gate 	} else {
4580*7c478bd9Sstevel@tonic-gate 		wnew->q_next = wafter->q_next;
4581*7c478bd9Sstevel@tonic-gate 	}
4582*7c478bd9Sstevel@tonic-gate 	new->q_next = after;
4583*7c478bd9Sstevel@tonic-gate 
4584*7c478bd9Sstevel@tonic-gate 	set_nfsrv_ptr(new, wnew, after, wafter);
4585*7c478bd9Sstevel@tonic-gate 	/*
4586*7c478bd9Sstevel@tonic-gate 	 * set_nfsrv_ptr() needs to know if this is an insertion or not,
4587*7c478bd9Sstevel@tonic-gate 	 * so only reset this flag after calling it.
4588*7c478bd9Sstevel@tonic-gate 	 */
4589*7c478bd9Sstevel@tonic-gate 	new->q_flag &= ~_QINSERTING;
4590*7c478bd9Sstevel@tonic-gate 
4591*7c478bd9Sstevel@tonic-gate 	if (have_fifo) {
4592*7c478bd9Sstevel@tonic-gate 		wafter->q_next = wnew;
4593*7c478bd9Sstevel@tonic-gate 	} else {
4594*7c478bd9Sstevel@tonic-gate 		if (wafter->q_next)
4595*7c478bd9Sstevel@tonic-gate 			_OTHERQ(wafter->q_next)->q_next = new;
4596*7c478bd9Sstevel@tonic-gate 		wafter->q_next = wnew;
4597*7c478bd9Sstevel@tonic-gate 	}
4598*7c478bd9Sstevel@tonic-gate 
4599*7c478bd9Sstevel@tonic-gate 	set_qend(new);
4600*7c478bd9Sstevel@tonic-gate 	/* The QEND flag might have to be updated for the upstream guy */
4601*7c478bd9Sstevel@tonic-gate 	set_qend(after);
4602*7c478bd9Sstevel@tonic-gate 
4603*7c478bd9Sstevel@tonic-gate 	ASSERT(_SAMESTR(new) == O_SAMESTR(new));
4604*7c478bd9Sstevel@tonic-gate 	ASSERT(_SAMESTR(wnew) == O_SAMESTR(wnew));
4605*7c478bd9Sstevel@tonic-gate 	ASSERT(_SAMESTR(after) == O_SAMESTR(after));
4606*7c478bd9Sstevel@tonic-gate 	ASSERT(_SAMESTR(wafter) == O_SAMESTR(wafter));
4607*7c478bd9Sstevel@tonic-gate 	strsetuio(stp);
4608*7c478bd9Sstevel@tonic-gate 
4609*7c478bd9Sstevel@tonic-gate 	/*
4610*7c478bd9Sstevel@tonic-gate 	 * If this was a module insertion, bump the push count.
4611*7c478bd9Sstevel@tonic-gate 	 */
4612*7c478bd9Sstevel@tonic-gate 	if (!(new->q_flag & QISDRV))
4613*7c478bd9Sstevel@tonic-gate 		stp->sd_pushcnt++;
4614*7c478bd9Sstevel@tonic-gate 
4615*7c478bd9Sstevel@tonic-gate 	strunlock(stp, NULL);
4616*7c478bd9Sstevel@tonic-gate }
4617*7c478bd9Sstevel@tonic-gate 
4618*7c478bd9Sstevel@tonic-gate /*
4619*7c478bd9Sstevel@tonic-gate  * Given a read queue, unlink it from any neighbors.
4620*7c478bd9Sstevel@tonic-gate  *
4621*7c478bd9Sstevel@tonic-gate  * This routine acquires all the necessary locks in order to
4622*7c478bd9Sstevel@tonic-gate  * change q_next and related pointers and also guard against
4623*7c478bd9Sstevel@tonic-gate  * stale references (e.g. through q_next) to the queue that
4624*7c478bd9Sstevel@tonic-gate  * is being removed. It also plays part of the role in ensuring
4625*7c478bd9Sstevel@tonic-gate  * that the module's/driver's put procedure doesn't get called
4626*7c478bd9Sstevel@tonic-gate  * after qprocsoff returns.
4627*7c478bd9Sstevel@tonic-gate  *
4628*7c478bd9Sstevel@tonic-gate  * Removeq depends on the stream head ensuring that there are
4629*7c478bd9Sstevel@tonic-gate  * no concurrent insertq or removeq on the same stream. The
4630*7c478bd9Sstevel@tonic-gate  * stream head ensures this using the flags STWOPEN, STRCLOSE and
4631*7c478bd9Sstevel@tonic-gate  * STRPLUMB.
4632*7c478bd9Sstevel@tonic-gate  *
4633*7c478bd9Sstevel@tonic-gate  * The set of locks needed to remove the queue is different in
4634*7c478bd9Sstevel@tonic-gate  * different cases:
4635*7c478bd9Sstevel@tonic-gate  *
4636*7c478bd9Sstevel@tonic-gate  * Acquire sd_lock, sd_reflock, and all the syncq locks in the stream after
4637*7c478bd9Sstevel@tonic-gate  * waiting for the syncq reference count to drop to 0 indicating that no
4638*7c478bd9Sstevel@tonic-gate  * non-close threads are present anywhere in the stream. This ensures that any
4639*7c478bd9Sstevel@tonic-gate  * module/driver can reference q_next in its open, close, put, or service
4640*7c478bd9Sstevel@tonic-gate  * procedures.
4641*7c478bd9Sstevel@tonic-gate  *
4642*7c478bd9Sstevel@tonic-gate  * The sq_rmqcount counter tracks the number of threads inside removeq().
4643*7c478bd9Sstevel@tonic-gate  * strlock() ensures that there is either no threads executing inside perimeter
4644*7c478bd9Sstevel@tonic-gate  * or there is only a thread calling qprocsoff().
4645*7c478bd9Sstevel@tonic-gate  *
4646*7c478bd9Sstevel@tonic-gate  * strlock() compares the value of sq_count with the number of threads inside
4647*7c478bd9Sstevel@tonic-gate  * removeq() and waits until sq_count is equal to sq_rmqcount. We need to wakeup
4648*7c478bd9Sstevel@tonic-gate  * any threads waiting in strlock() when the sq_rmqcount increases.
4649*7c478bd9Sstevel@tonic-gate  */
4650*7c478bd9Sstevel@tonic-gate 
4651*7c478bd9Sstevel@tonic-gate void
4652*7c478bd9Sstevel@tonic-gate removeq(queue_t *qp)
4653*7c478bd9Sstevel@tonic-gate {
4654*7c478bd9Sstevel@tonic-gate 	queue_t *wqp = _WR(qp);
4655*7c478bd9Sstevel@tonic-gate 	struct stdata *stp = STREAM(qp);
4656*7c478bd9Sstevel@tonic-gate 	sqlist_t *sqlist = NULL;
4657*7c478bd9Sstevel@tonic-gate 	boolean_t isdriver;
4658*7c478bd9Sstevel@tonic-gate 	int moved;
4659*7c478bd9Sstevel@tonic-gate 	syncq_t *sq = qp->q_syncq;
4660*7c478bd9Sstevel@tonic-gate 	syncq_t *wsq = wqp->q_syncq;
4661*7c478bd9Sstevel@tonic-gate 
4662*7c478bd9Sstevel@tonic-gate 	ASSERT(stp);
4663*7c478bd9Sstevel@tonic-gate 
4664*7c478bd9Sstevel@tonic-gate 	TRACE_2(TR_FAC_STREAMS_FR, TR_REMOVEQ,
4665*7c478bd9Sstevel@tonic-gate 		"removeq:%p %p", qp, wqp);
4666*7c478bd9Sstevel@tonic-gate 	ASSERT(qp->q_flag&QREADR);
4667*7c478bd9Sstevel@tonic-gate 
4668*7c478bd9Sstevel@tonic-gate 	/*
4669*7c478bd9Sstevel@tonic-gate 	 * For queues using Synchronous streams, we must wait for all threads in
4670*7c478bd9Sstevel@tonic-gate 	 * rwnext() to drain out before proceeding.
4671*7c478bd9Sstevel@tonic-gate 	 */
4672*7c478bd9Sstevel@tonic-gate 	if (qp->q_flag & QSYNCSTR) {
4673*7c478bd9Sstevel@tonic-gate 		/* First, we need wakeup any threads blocked in rwnext() */
4674*7c478bd9Sstevel@tonic-gate 		mutex_enter(SQLOCK(sq));
4675*7c478bd9Sstevel@tonic-gate 		if (sq->sq_flags & SQ_WANTWAKEUP) {
4676*7c478bd9Sstevel@tonic-gate 			sq->sq_flags &= ~SQ_WANTWAKEUP;
4677*7c478bd9Sstevel@tonic-gate 			cv_broadcast(&sq->sq_wait);
4678*7c478bd9Sstevel@tonic-gate 		}
4679*7c478bd9Sstevel@tonic-gate 		mutex_exit(SQLOCK(sq));
4680*7c478bd9Sstevel@tonic-gate 
4681*7c478bd9Sstevel@tonic-gate 		if (wsq != sq) {
4682*7c478bd9Sstevel@tonic-gate 			mutex_enter(SQLOCK(wsq));
4683*7c478bd9Sstevel@tonic-gate 			if (wsq->sq_flags & SQ_WANTWAKEUP) {
4684*7c478bd9Sstevel@tonic-gate 				wsq->sq_flags &= ~SQ_WANTWAKEUP;
4685*7c478bd9Sstevel@tonic-gate 				cv_broadcast(&wsq->sq_wait);
4686*7c478bd9Sstevel@tonic-gate 			}
4687*7c478bd9Sstevel@tonic-gate 			mutex_exit(SQLOCK(wsq));
4688*7c478bd9Sstevel@tonic-gate 		}
4689*7c478bd9Sstevel@tonic-gate 
4690*7c478bd9Sstevel@tonic-gate 		mutex_enter(QLOCK(qp));
4691*7c478bd9Sstevel@tonic-gate 		while (qp->q_rwcnt > 0) {
4692*7c478bd9Sstevel@tonic-gate 			qp->q_flag |= QWANTRMQSYNC;
4693*7c478bd9Sstevel@tonic-gate 			cv_wait(&qp->q_wait, QLOCK(qp));
4694*7c478bd9Sstevel@tonic-gate 		}
4695*7c478bd9Sstevel@tonic-gate 		mutex_exit(QLOCK(qp));
4696*7c478bd9Sstevel@tonic-gate 
4697*7c478bd9Sstevel@tonic-gate 		mutex_enter(QLOCK(wqp));
4698*7c478bd9Sstevel@tonic-gate 		while (wqp->q_rwcnt > 0) {
4699*7c478bd9Sstevel@tonic-gate 			wqp->q_flag |= QWANTRMQSYNC;
4700*7c478bd9Sstevel@tonic-gate 			cv_wait(&wqp->q_wait, QLOCK(wqp));
4701*7c478bd9Sstevel@tonic-gate 		}
4702*7c478bd9Sstevel@tonic-gate 		mutex_exit(QLOCK(wqp));
4703*7c478bd9Sstevel@tonic-gate 	}
4704*7c478bd9Sstevel@tonic-gate 
4705*7c478bd9Sstevel@tonic-gate 	mutex_enter(SQLOCK(sq));
4706*7c478bd9Sstevel@tonic-gate 	sq->sq_rmqcount++;
4707*7c478bd9Sstevel@tonic-gate 	if (sq->sq_flags & SQ_WANTWAKEUP) {
4708*7c478bd9Sstevel@tonic-gate 		sq->sq_flags &= ~SQ_WANTWAKEUP;
4709*7c478bd9Sstevel@tonic-gate 		cv_broadcast(&sq->sq_wait);
4710*7c478bd9Sstevel@tonic-gate 	}
4711*7c478bd9Sstevel@tonic-gate 	mutex_exit(SQLOCK(sq));
4712*7c478bd9Sstevel@tonic-gate 
4713*7c478bd9Sstevel@tonic-gate 	isdriver = (qp->q_flag & QISDRV);
4714*7c478bd9Sstevel@tonic-gate 
4715*7c478bd9Sstevel@tonic-gate 	sqlist = sqlist_build(qp, stp, STRMATED(stp));
4716*7c478bd9Sstevel@tonic-gate 	strlock(stp, sqlist);
4717*7c478bd9Sstevel@tonic-gate 
4718*7c478bd9Sstevel@tonic-gate 	reset_nfsrv_ptr(qp, wqp);
4719*7c478bd9Sstevel@tonic-gate 
4720*7c478bd9Sstevel@tonic-gate 	ASSERT(wqp->q_next == NULL || backq(qp)->q_next == qp);
4721*7c478bd9Sstevel@tonic-gate 	ASSERT(qp->q_next == NULL || backq(wqp)->q_next == wqp);
4722*7c478bd9Sstevel@tonic-gate 	/* Do we have a FIFO? */
4723*7c478bd9Sstevel@tonic-gate 	if (wqp->q_next == qp) {
4724*7c478bd9Sstevel@tonic-gate 		stp->sd_wrq->q_next = _RD(stp->sd_wrq);
4725*7c478bd9Sstevel@tonic-gate 	} else {
4726*7c478bd9Sstevel@tonic-gate 		if (wqp->q_next)
4727*7c478bd9Sstevel@tonic-gate 			backq(qp)->q_next = qp->q_next;
4728*7c478bd9Sstevel@tonic-gate 		if (qp->q_next)
4729*7c478bd9Sstevel@tonic-gate 			backq(wqp)->q_next = wqp->q_next;
4730*7c478bd9Sstevel@tonic-gate 	}
4731*7c478bd9Sstevel@tonic-gate 
4732*7c478bd9Sstevel@tonic-gate 	/* The QEND flag might have to be updated for the upstream guy */
4733*7c478bd9Sstevel@tonic-gate 	if (qp->q_next)
4734*7c478bd9Sstevel@tonic-gate 		set_qend(qp->q_next);
4735*7c478bd9Sstevel@tonic-gate 
4736*7c478bd9Sstevel@tonic-gate 	ASSERT(_SAMESTR(stp->sd_wrq) == O_SAMESTR(stp->sd_wrq));
4737*7c478bd9Sstevel@tonic-gate 	ASSERT(_SAMESTR(_RD(stp->sd_wrq)) == O_SAMESTR(_RD(stp->sd_wrq)));
4738*7c478bd9Sstevel@tonic-gate 
4739*7c478bd9Sstevel@tonic-gate 	/*
4740*7c478bd9Sstevel@tonic-gate 	 * Move any messages destined for the put procedures to the next
4741*7c478bd9Sstevel@tonic-gate 	 * syncq in line. Otherwise free them.
4742*7c478bd9Sstevel@tonic-gate 	 */
4743*7c478bd9Sstevel@tonic-gate 	moved = 0;
4744*7c478bd9Sstevel@tonic-gate 	/*
4745*7c478bd9Sstevel@tonic-gate 	 * Quick check to see whether there are any messages or events.
4746*7c478bd9Sstevel@tonic-gate 	 */
4747*7c478bd9Sstevel@tonic-gate 	if (qp->q_syncqmsgs != 0 || (qp->q_syncq->sq_flags & SQ_EVENTS))
4748*7c478bd9Sstevel@tonic-gate 		moved += propagate_syncq(qp);
4749*7c478bd9Sstevel@tonic-gate 	if (wqp->q_syncqmsgs != 0 ||
4750*7c478bd9Sstevel@tonic-gate 	    (wqp->q_syncq->sq_flags & SQ_EVENTS))
4751*7c478bd9Sstevel@tonic-gate 		moved += propagate_syncq(wqp);
4752*7c478bd9Sstevel@tonic-gate 
4753*7c478bd9Sstevel@tonic-gate 	strsetuio(stp);
4754*7c478bd9Sstevel@tonic-gate 
4755*7c478bd9Sstevel@tonic-gate 	/*
4756*7c478bd9Sstevel@tonic-gate 	 * If this was a module removal, decrement the push count.
4757*7c478bd9Sstevel@tonic-gate 	 */
4758*7c478bd9Sstevel@tonic-gate 	if (!isdriver)
4759*7c478bd9Sstevel@tonic-gate 		stp->sd_pushcnt--;
4760*7c478bd9Sstevel@tonic-gate 
4761*7c478bd9Sstevel@tonic-gate 	strunlock(stp, sqlist);
4762*7c478bd9Sstevel@tonic-gate 	sqlist_free(sqlist);
4763*7c478bd9Sstevel@tonic-gate 
4764*7c478bd9Sstevel@tonic-gate 	/*
4765*7c478bd9Sstevel@tonic-gate 	 * Make sure any messages that were propagated are drained.
4766*7c478bd9Sstevel@tonic-gate 	 * Also clear any QFULL bit caused by messages that were propagated.
4767*7c478bd9Sstevel@tonic-gate 	 */
4768*7c478bd9Sstevel@tonic-gate 
4769*7c478bd9Sstevel@tonic-gate 	if (qp->q_next != NULL) {
4770*7c478bd9Sstevel@tonic-gate 		clr_qfull(qp);
4771*7c478bd9Sstevel@tonic-gate 		/*
4772*7c478bd9Sstevel@tonic-gate 		 * For the driver calling qprocsoff, propagate_syncq
4773*7c478bd9Sstevel@tonic-gate 		 * frees all the messages instead of putting it in
4774*7c478bd9Sstevel@tonic-gate 		 * the stream head
4775*7c478bd9Sstevel@tonic-gate 		 */
4776*7c478bd9Sstevel@tonic-gate 		if (!isdriver && (moved > 0))
4777*7c478bd9Sstevel@tonic-gate 			emptysq(qp->q_next->q_syncq);
4778*7c478bd9Sstevel@tonic-gate 	}
4779*7c478bd9Sstevel@tonic-gate 	if (wqp->q_next != NULL) {
4780*7c478bd9Sstevel@tonic-gate 		clr_qfull(wqp);
4781*7c478bd9Sstevel@tonic-gate 		/*
4782*7c478bd9Sstevel@tonic-gate 		 * We come here for any pop of a module except for the
4783*7c478bd9Sstevel@tonic-gate 		 * case of driver being removed. We don't call emptysq
4784*7c478bd9Sstevel@tonic-gate 		 * if we did not move any messages. This will avoid holding
4785*7c478bd9Sstevel@tonic-gate 		 * PERMOD syncq locks in emptysq
4786*7c478bd9Sstevel@tonic-gate 		 */
4787*7c478bd9Sstevel@tonic-gate 		if (moved > 0)
4788*7c478bd9Sstevel@tonic-gate 			emptysq(wqp->q_next->q_syncq);
4789*7c478bd9Sstevel@tonic-gate 	}
4790*7c478bd9Sstevel@tonic-gate 
4791*7c478bd9Sstevel@tonic-gate 	mutex_enter(SQLOCK(sq));
4792*7c478bd9Sstevel@tonic-gate 	sq->sq_rmqcount--;
4793*7c478bd9Sstevel@tonic-gate 	mutex_exit(SQLOCK(sq));
4794*7c478bd9Sstevel@tonic-gate }
4795*7c478bd9Sstevel@tonic-gate 
4796*7c478bd9Sstevel@tonic-gate /*
4797*7c478bd9Sstevel@tonic-gate  * Prevent further entry by setting a flag (like SQ_FROZEN, SQ_BLOCKED or
4798*7c478bd9Sstevel@tonic-gate  * SQ_WRITER) on a syncq.
4799*7c478bd9Sstevel@tonic-gate  * If maxcnt is not -1 it assumes that caller has "maxcnt" claim(s) on the
4800*7c478bd9Sstevel@tonic-gate  * sync queue and waits until sq_count reaches maxcnt.
4801*7c478bd9Sstevel@tonic-gate  *
4802*7c478bd9Sstevel@tonic-gate  * if maxcnt is -1 there's no need to grab sq_putlocks since the caller
4803*7c478bd9Sstevel@tonic-gate  * does not care about putnext threads that are in the middle of calling put
4804*7c478bd9Sstevel@tonic-gate  * entry points.
4805*7c478bd9Sstevel@tonic-gate  *
4806*7c478bd9Sstevel@tonic-gate  * This routine is used for both inner and outer syncqs.
4807*7c478bd9Sstevel@tonic-gate  */
4808*7c478bd9Sstevel@tonic-gate static void
4809*7c478bd9Sstevel@tonic-gate blocksq(syncq_t *sq, ushort_t flag, int maxcnt)
4810*7c478bd9Sstevel@tonic-gate {
4811*7c478bd9Sstevel@tonic-gate 	uint16_t count = 0;
4812*7c478bd9Sstevel@tonic-gate 
4813*7c478bd9Sstevel@tonic-gate 	mutex_enter(SQLOCK(sq));
4814*7c478bd9Sstevel@tonic-gate 	/*
4815*7c478bd9Sstevel@tonic-gate 	 * Wait for SQ_FROZEN/SQ_BLOCKED to be reset.
4816*7c478bd9Sstevel@tonic-gate 	 * SQ_FROZEN will be set if there is a frozen stream that has a
4817*7c478bd9Sstevel@tonic-gate 	 * queue which also refers to this "shared" syncq.
4818*7c478bd9Sstevel@tonic-gate 	 * SQ_BLOCKED will be set if there is "off" queue which also
4819*7c478bd9Sstevel@tonic-gate 	 * refers to this "shared" syncq.
4820*7c478bd9Sstevel@tonic-gate 	 */
4821*7c478bd9Sstevel@tonic-gate 	if (maxcnt != -1) {
4822*7c478bd9Sstevel@tonic-gate 		count = sq->sq_count;
4823*7c478bd9Sstevel@tonic-gate 		SQ_PUTLOCKS_ENTER(sq);
4824*7c478bd9Sstevel@tonic-gate 		SQ_PUTCOUNT_CLRFAST_LOCKED(sq);
4825*7c478bd9Sstevel@tonic-gate 		SUM_SQ_PUTCOUNTS(sq, count);
4826*7c478bd9Sstevel@tonic-gate 	}
4827*7c478bd9Sstevel@tonic-gate 	sq->sq_needexcl++;
4828*7c478bd9Sstevel@tonic-gate 	ASSERT(sq->sq_needexcl != 0);	/* wraparound */
4829*7c478bd9Sstevel@tonic-gate 
4830*7c478bd9Sstevel@tonic-gate 	while ((sq->sq_flags & flag) ||
4831*7c478bd9Sstevel@tonic-gate 	    (maxcnt != -1 && count > (unsigned)maxcnt)) {
4832*7c478bd9Sstevel@tonic-gate 		sq->sq_flags |= SQ_WANTWAKEUP;
4833*7c478bd9Sstevel@tonic-gate 		if (maxcnt != -1) {
4834*7c478bd9Sstevel@tonic-gate 			SQ_PUTLOCKS_EXIT(sq);
4835*7c478bd9Sstevel@tonic-gate 		}
4836*7c478bd9Sstevel@tonic-gate 		cv_wait(&sq->sq_wait, SQLOCK(sq));
4837*7c478bd9Sstevel@tonic-gate 		if (maxcnt != -1) {
4838*7c478bd9Sstevel@tonic-gate 			count = sq->sq_count;
4839*7c478bd9Sstevel@tonic-gate 			SQ_PUTLOCKS_ENTER(sq);
4840*7c478bd9Sstevel@tonic-gate 			SUM_SQ_PUTCOUNTS(sq, count);
4841*7c478bd9Sstevel@tonic-gate 		}
4842*7c478bd9Sstevel@tonic-gate 	}
4843*7c478bd9Sstevel@tonic-gate 	sq->sq_needexcl--;
4844*7c478bd9Sstevel@tonic-gate 	sq->sq_flags |= flag;
4845*7c478bd9Sstevel@tonic-gate 	ASSERT(maxcnt == -1 || count == maxcnt);
4846*7c478bd9Sstevel@tonic-gate 	if (maxcnt != -1) {
4847*7c478bd9Sstevel@tonic-gate 		if (sq->sq_needexcl == 0) {
4848*7c478bd9Sstevel@tonic-gate 			SQ_PUTCOUNT_SETFAST_LOCKED(sq);
4849*7c478bd9Sstevel@tonic-gate 		}
4850*7c478bd9Sstevel@tonic-gate 		SQ_PUTLOCKS_EXIT(sq);
4851*7c478bd9Sstevel@tonic-gate 	} else if (sq->sq_needexcl == 0) {
4852*7c478bd9Sstevel@tonic-gate 		SQ_PUTCOUNT_SETFAST(sq);
4853*7c478bd9Sstevel@tonic-gate 	}
4854*7c478bd9Sstevel@tonic-gate 
4855*7c478bd9Sstevel@tonic-gate 	mutex_exit(SQLOCK(sq));
4856*7c478bd9Sstevel@tonic-gate }
4857*7c478bd9Sstevel@tonic-gate 
4858*7c478bd9Sstevel@tonic-gate /*
4859*7c478bd9Sstevel@tonic-gate  * Reset a flag that was set with blocksq.
4860*7c478bd9Sstevel@tonic-gate  *
4861*7c478bd9Sstevel@tonic-gate  * Can not use this routine to reset SQ_WRITER.
4862*7c478bd9Sstevel@tonic-gate  *
4863*7c478bd9Sstevel@tonic-gate  * If "isouter" is set then the syncq is assumed to be an outer perimeter
4864*7c478bd9Sstevel@tonic-gate  * and drain_syncq is not called. Instead we rely on the qwriter_outer thread
4865*7c478bd9Sstevel@tonic-gate  * to handle the queued qwriter operations.
4866*7c478bd9Sstevel@tonic-gate  *
4867*7c478bd9Sstevel@tonic-gate  * no need to grab sq_putlocks here. See comment in strsubr.h that explains when
4868*7c478bd9Sstevel@tonic-gate  * sq_putlocks are used.
4869*7c478bd9Sstevel@tonic-gate  */
4870*7c478bd9Sstevel@tonic-gate static void
4871*7c478bd9Sstevel@tonic-gate unblocksq(syncq_t *sq, uint16_t resetflag, int isouter)
4872*7c478bd9Sstevel@tonic-gate {
4873*7c478bd9Sstevel@tonic-gate 	uint16_t flags;
4874*7c478bd9Sstevel@tonic-gate 
4875*7c478bd9Sstevel@tonic-gate 	mutex_enter(SQLOCK(sq));
4876*7c478bd9Sstevel@tonic-gate 	ASSERT(resetflag != SQ_WRITER);
4877*7c478bd9Sstevel@tonic-gate 	ASSERT(sq->sq_flags & resetflag);
4878*7c478bd9Sstevel@tonic-gate 	flags = sq->sq_flags & ~resetflag;
4879*7c478bd9Sstevel@tonic-gate 	sq->sq_flags = flags;
4880*7c478bd9Sstevel@tonic-gate 	if (flags & (SQ_QUEUED | SQ_WANTWAKEUP)) {
4881*7c478bd9Sstevel@tonic-gate 		if (flags & SQ_WANTWAKEUP) {
4882*7c478bd9Sstevel@tonic-gate 			flags &= ~SQ_WANTWAKEUP;
4883*7c478bd9Sstevel@tonic-gate 			cv_broadcast(&sq->sq_wait);
4884*7c478bd9Sstevel@tonic-gate 		}
4885*7c478bd9Sstevel@tonic-gate 		sq->sq_flags = flags;
4886*7c478bd9Sstevel@tonic-gate 		if ((flags & SQ_QUEUED) && !(flags & (SQ_STAYAWAY|SQ_EXCL))) {
4887*7c478bd9Sstevel@tonic-gate 			if (!isouter) {
4888*7c478bd9Sstevel@tonic-gate 				/* drain_syncq drops SQLOCK */
4889*7c478bd9Sstevel@tonic-gate 				drain_syncq(sq);
4890*7c478bd9Sstevel@tonic-gate 				return;
4891*7c478bd9Sstevel@tonic-gate 			}
4892*7c478bd9Sstevel@tonic-gate 		}
4893*7c478bd9Sstevel@tonic-gate 	}
4894*7c478bd9Sstevel@tonic-gate 	mutex_exit(SQLOCK(sq));
4895*7c478bd9Sstevel@tonic-gate }
4896*7c478bd9Sstevel@tonic-gate 
4897*7c478bd9Sstevel@tonic-gate /*
4898*7c478bd9Sstevel@tonic-gate  * Reset a flag that was set with blocksq.
4899*7c478bd9Sstevel@tonic-gate  * Does not drain the syncq. Use emptysq() for that.
4900*7c478bd9Sstevel@tonic-gate  * Returns 1 if SQ_QUEUED is set. Otherwise 0.
4901*7c478bd9Sstevel@tonic-gate  *
4902*7c478bd9Sstevel@tonic-gate  * no need to grab sq_putlocks here. See comment in strsubr.h that explains when
4903*7c478bd9Sstevel@tonic-gate  * sq_putlocks are used.
4904*7c478bd9Sstevel@tonic-gate  */
4905*7c478bd9Sstevel@tonic-gate static int
4906*7c478bd9Sstevel@tonic-gate dropsq(syncq_t *sq, uint16_t resetflag)
4907*7c478bd9Sstevel@tonic-gate {
4908*7c478bd9Sstevel@tonic-gate 	uint16_t flags;
4909*7c478bd9Sstevel@tonic-gate 
4910*7c478bd9Sstevel@tonic-gate 	mutex_enter(SQLOCK(sq));
4911*7c478bd9Sstevel@tonic-gate 	ASSERT(sq->sq_flags & resetflag);
4912*7c478bd9Sstevel@tonic-gate 	flags = sq->sq_flags & ~resetflag;
4913*7c478bd9Sstevel@tonic-gate 	if (flags & SQ_WANTWAKEUP) {
4914*7c478bd9Sstevel@tonic-gate 		flags &= ~SQ_WANTWAKEUP;
4915*7c478bd9Sstevel@tonic-gate 		cv_broadcast(&sq->sq_wait);
4916*7c478bd9Sstevel@tonic-gate 	}
4917*7c478bd9Sstevel@tonic-gate 	sq->sq_flags = flags;
4918*7c478bd9Sstevel@tonic-gate 	mutex_exit(SQLOCK(sq));
4919*7c478bd9Sstevel@tonic-gate 	if (flags & SQ_QUEUED)
4920*7c478bd9Sstevel@tonic-gate 		return (1);
4921*7c478bd9Sstevel@tonic-gate 	return (0);
4922*7c478bd9Sstevel@tonic-gate }
4923*7c478bd9Sstevel@tonic-gate 
4924*7c478bd9Sstevel@tonic-gate /*
4925*7c478bd9Sstevel@tonic-gate  * Empty all the messages on a syncq.
4926*7c478bd9Sstevel@tonic-gate  *
4927*7c478bd9Sstevel@tonic-gate  * no need to grab sq_putlocks here. See comment in strsubr.h that explains when
4928*7c478bd9Sstevel@tonic-gate  * sq_putlocks are used.
4929*7c478bd9Sstevel@tonic-gate  */
4930*7c478bd9Sstevel@tonic-gate static void
4931*7c478bd9Sstevel@tonic-gate emptysq(syncq_t *sq)
4932*7c478bd9Sstevel@tonic-gate {
4933*7c478bd9Sstevel@tonic-gate 	uint16_t flags;
4934*7c478bd9Sstevel@tonic-gate 
4935*7c478bd9Sstevel@tonic-gate 	mutex_enter(SQLOCK(sq));
4936*7c478bd9Sstevel@tonic-gate 	flags = sq->sq_flags;
4937*7c478bd9Sstevel@tonic-gate 	if ((flags & SQ_QUEUED) && !(flags & (SQ_STAYAWAY|SQ_EXCL))) {
4938*7c478bd9Sstevel@tonic-gate 		/*
4939*7c478bd9Sstevel@tonic-gate 		 * To prevent potential recursive invocation of drain_syncq we
4940*7c478bd9Sstevel@tonic-gate 		 * do not call drain_syncq if count is non-zero.
4941*7c478bd9Sstevel@tonic-gate 		 */
4942*7c478bd9Sstevel@tonic-gate 		if (sq->sq_count == 0) {
4943*7c478bd9Sstevel@tonic-gate 			/* drain_syncq() drops SQLOCK */
4944*7c478bd9Sstevel@tonic-gate 			drain_syncq(sq);
4945*7c478bd9Sstevel@tonic-gate 			return;
4946*7c478bd9Sstevel@tonic-gate 		} else
4947*7c478bd9Sstevel@tonic-gate 			sqenable(sq);
4948*7c478bd9Sstevel@tonic-gate 	}
4949*7c478bd9Sstevel@tonic-gate 	mutex_exit(SQLOCK(sq));
4950*7c478bd9Sstevel@tonic-gate }
4951*7c478bd9Sstevel@tonic-gate 
4952*7c478bd9Sstevel@tonic-gate /*
4953*7c478bd9Sstevel@tonic-gate  * Ordered insert while removing duplicates.
4954*7c478bd9Sstevel@tonic-gate  */
4955*7c478bd9Sstevel@tonic-gate static void
4956*7c478bd9Sstevel@tonic-gate sqlist_insert(sqlist_t *sqlist, syncq_t *sqp)
4957*7c478bd9Sstevel@tonic-gate {
4958*7c478bd9Sstevel@tonic-gate 	syncql_t *sqlp, **prev_sqlpp, *new_sqlp;
4959*7c478bd9Sstevel@tonic-gate 
4960*7c478bd9Sstevel@tonic-gate 	prev_sqlpp = &sqlist->sqlist_head;
4961*7c478bd9Sstevel@tonic-gate 	while ((sqlp = *prev_sqlpp) != NULL) {
4962*7c478bd9Sstevel@tonic-gate 		if (sqlp->sql_sq >= sqp) {
4963*7c478bd9Sstevel@tonic-gate 			if (sqlp->sql_sq == sqp)	/* duplicate */
4964*7c478bd9Sstevel@tonic-gate 				return;
4965*7c478bd9Sstevel@tonic-gate 			break;
4966*7c478bd9Sstevel@tonic-gate 		}
4967*7c478bd9Sstevel@tonic-gate 		prev_sqlpp = &sqlp->sql_next;
4968*7c478bd9Sstevel@tonic-gate 	}
4969*7c478bd9Sstevel@tonic-gate 	new_sqlp = &sqlist->sqlist_array[sqlist->sqlist_index++];
4970*7c478bd9Sstevel@tonic-gate 	ASSERT((char *)new_sqlp < (char *)sqlist + sqlist->sqlist_size);
4971*7c478bd9Sstevel@tonic-gate 	new_sqlp->sql_next = sqlp;
4972*7c478bd9Sstevel@tonic-gate 	new_sqlp->sql_sq = sqp;
4973*7c478bd9Sstevel@tonic-gate 	*prev_sqlpp = new_sqlp;
4974*7c478bd9Sstevel@tonic-gate }
4975*7c478bd9Sstevel@tonic-gate 
4976*7c478bd9Sstevel@tonic-gate /*
4977*7c478bd9Sstevel@tonic-gate  * Walk the write side queues until we hit either the driver
4978*7c478bd9Sstevel@tonic-gate  * or a twist in the stream (_SAMESTR will return false in both
4979*7c478bd9Sstevel@tonic-gate  * these cases) then turn around and walk the read side queues
4980*7c478bd9Sstevel@tonic-gate  * back up to the stream head.
4981*7c478bd9Sstevel@tonic-gate  */
4982*7c478bd9Sstevel@tonic-gate static void
4983*7c478bd9Sstevel@tonic-gate sqlist_insertall(sqlist_t *sqlist, queue_t *q)
4984*7c478bd9Sstevel@tonic-gate {
4985*7c478bd9Sstevel@tonic-gate 	while (q != NULL) {
4986*7c478bd9Sstevel@tonic-gate 		sqlist_insert(sqlist, q->q_syncq);
4987*7c478bd9Sstevel@tonic-gate 
4988*7c478bd9Sstevel@tonic-gate 		if (_SAMESTR(q))
4989*7c478bd9Sstevel@tonic-gate 			q = q->q_next;
4990*7c478bd9Sstevel@tonic-gate 		else if (!(q->q_flag & QREADR))
4991*7c478bd9Sstevel@tonic-gate 			q = _RD(q);
4992*7c478bd9Sstevel@tonic-gate 		else
4993*7c478bd9Sstevel@tonic-gate 			q = NULL;
4994*7c478bd9Sstevel@tonic-gate 	}
4995*7c478bd9Sstevel@tonic-gate }
4996*7c478bd9Sstevel@tonic-gate 
4997*7c478bd9Sstevel@tonic-gate /*
4998*7c478bd9Sstevel@tonic-gate  * Allocate and build a list of all syncqs in a stream and the syncq(s)
4999*7c478bd9Sstevel@tonic-gate  * associated with the "q" parameter. The resulting list is sorted in a
5000*7c478bd9Sstevel@tonic-gate  * canonical order and is free of duplicates.
5001*7c478bd9Sstevel@tonic-gate  * Assumes the passed queue is a _RD(q).
5002*7c478bd9Sstevel@tonic-gate  */
5003*7c478bd9Sstevel@tonic-gate static sqlist_t *
5004*7c478bd9Sstevel@tonic-gate sqlist_build(queue_t *q, struct stdata *stp, boolean_t do_twist)
5005*7c478bd9Sstevel@tonic-gate {
5006*7c478bd9Sstevel@tonic-gate 	sqlist_t *sqlist = sqlist_alloc(stp, KM_SLEEP);
5007*7c478bd9Sstevel@tonic-gate 
5008*7c478bd9Sstevel@tonic-gate 	/*
5009*7c478bd9Sstevel@tonic-gate 	 * start with the current queue/qpair
5010*7c478bd9Sstevel@tonic-gate 	 */
5011*7c478bd9Sstevel@tonic-gate 	ASSERT(q->q_flag & QREADR);
5012*7c478bd9Sstevel@tonic-gate 
5013*7c478bd9Sstevel@tonic-gate 	sqlist_insert(sqlist, q->q_syncq);
5014*7c478bd9Sstevel@tonic-gate 	sqlist_insert(sqlist, _WR(q)->q_syncq);
5015*7c478bd9Sstevel@tonic-gate 
5016*7c478bd9Sstevel@tonic-gate 	sqlist_insertall(sqlist, stp->sd_wrq);
5017*7c478bd9Sstevel@tonic-gate 	if (do_twist)
5018*7c478bd9Sstevel@tonic-gate 		sqlist_insertall(sqlist, stp->sd_mate->sd_wrq);
5019*7c478bd9Sstevel@tonic-gate 
5020*7c478bd9Sstevel@tonic-gate 	return (sqlist);
5021*7c478bd9Sstevel@tonic-gate }
5022*7c478bd9Sstevel@tonic-gate 
5023*7c478bd9Sstevel@tonic-gate static sqlist_t *
5024*7c478bd9Sstevel@tonic-gate sqlist_alloc(struct stdata *stp, int kmflag)
5025*7c478bd9Sstevel@tonic-gate {
5026*7c478bd9Sstevel@tonic-gate 	size_t sqlist_size;
5027*7c478bd9Sstevel@tonic-gate 	sqlist_t *sqlist;
5028*7c478bd9Sstevel@tonic-gate 
5029*7c478bd9Sstevel@tonic-gate 	/*
5030*7c478bd9Sstevel@tonic-gate 	 * Allocate 2 syncql_t's for each pushed module. Note that
5031*7c478bd9Sstevel@tonic-gate 	 * the sqlist_t structure already has 4 syncql_t's built in:
5032*7c478bd9Sstevel@tonic-gate 	 * 2 for the stream head, and 2 for the driver/other stream head.
5033*7c478bd9Sstevel@tonic-gate 	 */
5034*7c478bd9Sstevel@tonic-gate 	sqlist_size = 2 * sizeof (syncql_t) * stp->sd_pushcnt +
5035*7c478bd9Sstevel@tonic-gate 		sizeof (sqlist_t);
5036*7c478bd9Sstevel@tonic-gate 	if (STRMATED(stp))
5037*7c478bd9Sstevel@tonic-gate 		sqlist_size += 2 * sizeof (syncql_t) * stp->sd_mate->sd_pushcnt;
5038*7c478bd9Sstevel@tonic-gate 	sqlist = kmem_alloc(sqlist_size, kmflag);
5039*7c478bd9Sstevel@tonic-gate 
5040*7c478bd9Sstevel@tonic-gate 	sqlist->sqlist_head = NULL;
5041*7c478bd9Sstevel@tonic-gate 	sqlist->sqlist_size = sqlist_size;
5042*7c478bd9Sstevel@tonic-gate 	sqlist->sqlist_index = 0;
5043*7c478bd9Sstevel@tonic-gate 
5044*7c478bd9Sstevel@tonic-gate 	return (sqlist);
5045*7c478bd9Sstevel@tonic-gate }
5046*7c478bd9Sstevel@tonic-gate 
5047*7c478bd9Sstevel@tonic-gate /*
5048*7c478bd9Sstevel@tonic-gate  * Free the list created by sqlist_alloc()
5049*7c478bd9Sstevel@tonic-gate  */
5050*7c478bd9Sstevel@tonic-gate static void
5051*7c478bd9Sstevel@tonic-gate sqlist_free(sqlist_t *sqlist)
5052*7c478bd9Sstevel@tonic-gate {
5053*7c478bd9Sstevel@tonic-gate 	kmem_free(sqlist, sqlist->sqlist_size);
5054*7c478bd9Sstevel@tonic-gate }
5055*7c478bd9Sstevel@tonic-gate 
5056*7c478bd9Sstevel@tonic-gate /*
5057*7c478bd9Sstevel@tonic-gate  * Prevent any new entries into any syncq in this stream.
5058*7c478bd9Sstevel@tonic-gate  * Used by freezestr.
5059*7c478bd9Sstevel@tonic-gate  */
5060*7c478bd9Sstevel@tonic-gate void
5061*7c478bd9Sstevel@tonic-gate strblock(queue_t *q)
5062*7c478bd9Sstevel@tonic-gate {
5063*7c478bd9Sstevel@tonic-gate 	struct stdata	*stp;
5064*7c478bd9Sstevel@tonic-gate 	syncql_t	*sql;
5065*7c478bd9Sstevel@tonic-gate 	sqlist_t	*sqlist;
5066*7c478bd9Sstevel@tonic-gate 
5067*7c478bd9Sstevel@tonic-gate 	q = _RD(q);
5068*7c478bd9Sstevel@tonic-gate 
5069*7c478bd9Sstevel@tonic-gate 	stp = STREAM(q);
5070*7c478bd9Sstevel@tonic-gate 	ASSERT(stp != NULL);
5071*7c478bd9Sstevel@tonic-gate 
5072*7c478bd9Sstevel@tonic-gate 	/*
5073*7c478bd9Sstevel@tonic-gate 	 * Get a sorted list with all the duplicates removed containing
5074*7c478bd9Sstevel@tonic-gate 	 * all the syncqs referenced by this stream.
5075*7c478bd9Sstevel@tonic-gate 	 */
5076*7c478bd9Sstevel@tonic-gate 	sqlist = sqlist_build(q, stp, B_FALSE);
5077*7c478bd9Sstevel@tonic-gate 	for (sql = sqlist->sqlist_head; sql != NULL; sql = sql->sql_next)
5078*7c478bd9Sstevel@tonic-gate 		blocksq(sql->sql_sq, SQ_FROZEN, -1);
5079*7c478bd9Sstevel@tonic-gate 	sqlist_free(sqlist);
5080*7c478bd9Sstevel@tonic-gate }
5081*7c478bd9Sstevel@tonic-gate 
5082*7c478bd9Sstevel@tonic-gate /*
5083*7c478bd9Sstevel@tonic-gate  * Release the block on new entries into this stream
5084*7c478bd9Sstevel@tonic-gate  */
5085*7c478bd9Sstevel@tonic-gate void
5086*7c478bd9Sstevel@tonic-gate strunblock(queue_t *q)
5087*7c478bd9Sstevel@tonic-gate {
5088*7c478bd9Sstevel@tonic-gate 	struct stdata	*stp;
5089*7c478bd9Sstevel@tonic-gate 	syncql_t	*sql;
5090*7c478bd9Sstevel@tonic-gate 	sqlist_t	*sqlist;
5091*7c478bd9Sstevel@tonic-gate 	int		drain_needed;
5092*7c478bd9Sstevel@tonic-gate 
5093*7c478bd9Sstevel@tonic-gate 	q = _RD(q);
5094*7c478bd9Sstevel@tonic-gate 
5095*7c478bd9Sstevel@tonic-gate 	/*
5096*7c478bd9Sstevel@tonic-gate 	 * Get a sorted list with all the duplicates removed containing
5097*7c478bd9Sstevel@tonic-gate 	 * all the syncqs referenced by this stream.
5098*7c478bd9Sstevel@tonic-gate 	 * Have to drop the SQ_FROZEN flag on all the syncqs before
5099*7c478bd9Sstevel@tonic-gate 	 * starting to drain them; otherwise the draining might
5100*7c478bd9Sstevel@tonic-gate 	 * cause a freezestr in some module on the stream (which
5101*7c478bd9Sstevel@tonic-gate 	 * would deadlock.)
5102*7c478bd9Sstevel@tonic-gate 	 */
5103*7c478bd9Sstevel@tonic-gate 	stp = STREAM(q);
5104*7c478bd9Sstevel@tonic-gate 	ASSERT(stp != NULL);
5105*7c478bd9Sstevel@tonic-gate 	sqlist = sqlist_build(q, stp, B_FALSE);
5106*7c478bd9Sstevel@tonic-gate 	drain_needed = 0;
5107*7c478bd9Sstevel@tonic-gate 	for (sql = sqlist->sqlist_head; sql != NULL; sql = sql->sql_next)
5108*7c478bd9Sstevel@tonic-gate 		drain_needed += dropsq(sql->sql_sq, SQ_FROZEN);
5109*7c478bd9Sstevel@tonic-gate 	if (drain_needed) {
5110*7c478bd9Sstevel@tonic-gate 		for (sql = sqlist->sqlist_head; sql != NULL;
5111*7c478bd9Sstevel@tonic-gate 		    sql = sql->sql_next)
5112*7c478bd9Sstevel@tonic-gate 			emptysq(sql->sql_sq);
5113*7c478bd9Sstevel@tonic-gate 	}
5114*7c478bd9Sstevel@tonic-gate 	sqlist_free(sqlist);
5115*7c478bd9Sstevel@tonic-gate }
5116*7c478bd9Sstevel@tonic-gate 
5117*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
5118*7c478bd9Sstevel@tonic-gate static int
5119*7c478bd9Sstevel@tonic-gate qprocsareon(queue_t *rq)
5120*7c478bd9Sstevel@tonic-gate {
5121*7c478bd9Sstevel@tonic-gate 	if (rq->q_next == NULL)
5122*7c478bd9Sstevel@tonic-gate 		return (0);
5123*7c478bd9Sstevel@tonic-gate 	return (_WR(rq->q_next)->q_next == _WR(rq));
5124*7c478bd9Sstevel@tonic-gate }
5125*7c478bd9Sstevel@tonic-gate 
5126*7c478bd9Sstevel@tonic-gate int
5127*7c478bd9Sstevel@tonic-gate qclaimed(queue_t *q)
5128*7c478bd9Sstevel@tonic-gate {
5129*7c478bd9Sstevel@tonic-gate 	uint_t count;
5130*7c478bd9Sstevel@tonic-gate 
5131*7c478bd9Sstevel@tonic-gate 	count = q->q_syncq->sq_count;
5132*7c478bd9Sstevel@tonic-gate 	SUM_SQ_PUTCOUNTS(q->q_syncq, count);
5133*7c478bd9Sstevel@tonic-gate 	return (count != 0);
5134*7c478bd9Sstevel@tonic-gate }
5135*7c478bd9Sstevel@tonic-gate 
5136*7c478bd9Sstevel@tonic-gate /*
5137*7c478bd9Sstevel@tonic-gate  * Check if anyone has frozen this stream with freezestr
5138*7c478bd9Sstevel@tonic-gate  */
5139*7c478bd9Sstevel@tonic-gate int
5140*7c478bd9Sstevel@tonic-gate frozenstr(queue_t *q)
5141*7c478bd9Sstevel@tonic-gate {
5142*7c478bd9Sstevel@tonic-gate 	return ((q->q_syncq->sq_flags & SQ_FROZEN) != 0);
5143*7c478bd9Sstevel@tonic-gate }
5144*7c478bd9Sstevel@tonic-gate #endif /* DEBUG */
5145*7c478bd9Sstevel@tonic-gate 
5146*7c478bd9Sstevel@tonic-gate /*
5147*7c478bd9Sstevel@tonic-gate  * Enter a queue.
5148*7c478bd9Sstevel@tonic-gate  * Obsoleted interface. Should not be used.
5149*7c478bd9Sstevel@tonic-gate  */
5150*7c478bd9Sstevel@tonic-gate void
5151*7c478bd9Sstevel@tonic-gate enterq(queue_t *q)
5152*7c478bd9Sstevel@tonic-gate {
5153*7c478bd9Sstevel@tonic-gate 	entersq(q->q_syncq, SQ_CALLBACK);
5154*7c478bd9Sstevel@tonic-gate }
5155*7c478bd9Sstevel@tonic-gate 
5156*7c478bd9Sstevel@tonic-gate void
5157*7c478bd9Sstevel@tonic-gate leaveq(queue_t *q)
5158*7c478bd9Sstevel@tonic-gate {
5159*7c478bd9Sstevel@tonic-gate 	leavesq(q->q_syncq, SQ_CALLBACK);
5160*7c478bd9Sstevel@tonic-gate }
5161*7c478bd9Sstevel@tonic-gate 
5162*7c478bd9Sstevel@tonic-gate /*
5163*7c478bd9Sstevel@tonic-gate  * Enter a perimeter. c_inner and c_outer specifies which concurrency bits
5164*7c478bd9Sstevel@tonic-gate  * to check.
5165*7c478bd9Sstevel@tonic-gate  * Wait if SQ_QUEUED is set to preserve ordering between messages and qwriter
5166*7c478bd9Sstevel@tonic-gate  * calls and the running of open, close and service procedures.
5167*7c478bd9Sstevel@tonic-gate  *
5168*7c478bd9Sstevel@tonic-gate  * if c_inner bit is set no need to grab sq_putlocks since we don't care
5169*7c478bd9Sstevel@tonic-gate  * if other threads have entered or are entering put entry point.
5170*7c478bd9Sstevel@tonic-gate  *
5171*7c478bd9Sstevel@tonic-gate  * if c_inner bit is set it might have been posible to use
5172*7c478bd9Sstevel@tonic-gate  * sq_putlocks/sq_putcounts instead of SQLOCK/sq_count (e.g. to optimize
5173*7c478bd9Sstevel@tonic-gate  * open/close path for IP) but since the count may need to be decremented in
5174*7c478bd9Sstevel@tonic-gate  * qwait() we wouldn't know which counter to decrement. Currently counter is
5175*7c478bd9Sstevel@tonic-gate  * selected by current cpu_seqid and current CPU can change at any moment. XXX
5176*7c478bd9Sstevel@tonic-gate  * in the future we might use curthread id bits to select the counter and this
5177*7c478bd9Sstevel@tonic-gate  * would stay constant across routine calls.
5178*7c478bd9Sstevel@tonic-gate  */
5179*7c478bd9Sstevel@tonic-gate void
5180*7c478bd9Sstevel@tonic-gate entersq(syncq_t *sq, int entrypoint)
5181*7c478bd9Sstevel@tonic-gate {
5182*7c478bd9Sstevel@tonic-gate 	uint16_t	count = 0;
5183*7c478bd9Sstevel@tonic-gate 	uint16_t	flags;
5184*7c478bd9Sstevel@tonic-gate 	uint16_t	waitflags = SQ_STAYAWAY | SQ_EVENTS | SQ_EXCL;
5185*7c478bd9Sstevel@tonic-gate 	uint16_t	type;
5186*7c478bd9Sstevel@tonic-gate 	uint_t		c_inner = entrypoint & SQ_CI;
5187*7c478bd9Sstevel@tonic-gate 	uint_t		c_outer = entrypoint & SQ_CO;
5188*7c478bd9Sstevel@tonic-gate 
5189*7c478bd9Sstevel@tonic-gate 	/*
5190*7c478bd9Sstevel@tonic-gate 	 * Increment ref count to keep closes out of this queue.
5191*7c478bd9Sstevel@tonic-gate 	 */
5192*7c478bd9Sstevel@tonic-gate 	ASSERT(sq);
5193*7c478bd9Sstevel@tonic-gate 	ASSERT(c_inner && c_outer);
5194*7c478bd9Sstevel@tonic-gate 	mutex_enter(SQLOCK(sq));
5195*7c478bd9Sstevel@tonic-gate 	flags = sq->sq_flags;
5196*7c478bd9Sstevel@tonic-gate 	type = sq->sq_type;
5197*7c478bd9Sstevel@tonic-gate 	if (!(type & c_inner)) {
5198*7c478bd9Sstevel@tonic-gate 		/* Make sure all putcounts now use slowlock. */
5199*7c478bd9Sstevel@tonic-gate 		count = sq->sq_count;
5200*7c478bd9Sstevel@tonic-gate 		SQ_PUTLOCKS_ENTER(sq);
5201*7c478bd9Sstevel@tonic-gate 		SQ_PUTCOUNT_CLRFAST_LOCKED(sq);
5202*7c478bd9Sstevel@tonic-gate 		SUM_SQ_PUTCOUNTS(sq, count);
5203*7c478bd9Sstevel@tonic-gate 		sq->sq_needexcl++;
5204*7c478bd9Sstevel@tonic-gate 		ASSERT(sq->sq_needexcl != 0);	/* wraparound */
5205*7c478bd9Sstevel@tonic-gate 		waitflags |= SQ_MESSAGES;
5206*7c478bd9Sstevel@tonic-gate 	}
5207*7c478bd9Sstevel@tonic-gate 	/*
5208*7c478bd9Sstevel@tonic-gate 	 * Wait until we can enter the inner perimeter.
5209*7c478bd9Sstevel@tonic-gate 	 * If we want exclusive access we wait until sq_count is 0.
5210*7c478bd9Sstevel@tonic-gate 	 * We have to do this before entering the outer perimeter in order
5211*7c478bd9Sstevel@tonic-gate 	 * to preserve put/close message ordering.
5212*7c478bd9Sstevel@tonic-gate 	 */
5213*7c478bd9Sstevel@tonic-gate 	while ((flags & waitflags) || (!(type & c_inner) && count != 0)) {
5214*7c478bd9Sstevel@tonic-gate 		sq->sq_flags = flags | SQ_WANTWAKEUP;
5215*7c478bd9Sstevel@tonic-gate 		if (!(type & c_inner)) {
5216*7c478bd9Sstevel@tonic-gate 			SQ_PUTLOCKS_EXIT(sq);
5217*7c478bd9Sstevel@tonic-gate 		}
5218*7c478bd9Sstevel@tonic-gate 		cv_wait(&sq->sq_wait, SQLOCK(sq));
5219*7c478bd9Sstevel@tonic-gate 		if (!(type & c_inner)) {
5220*7c478bd9Sstevel@tonic-gate 			count = sq->sq_count;
5221*7c478bd9Sstevel@tonic-gate 			SQ_PUTLOCKS_ENTER(sq);
5222*7c478bd9Sstevel@tonic-gate 			SUM_SQ_PUTCOUNTS(sq, count);
5223*7c478bd9Sstevel@tonic-gate 		}
5224*7c478bd9Sstevel@tonic-gate 		flags = sq->sq_flags;
5225*7c478bd9Sstevel@tonic-gate 	}
5226*7c478bd9Sstevel@tonic-gate 
5227*7c478bd9Sstevel@tonic-gate 	if (!(type & c_inner)) {
5228*7c478bd9Sstevel@tonic-gate 		ASSERT(sq->sq_needexcl > 0);
5229*7c478bd9Sstevel@tonic-gate 		sq->sq_needexcl--;
5230*7c478bd9Sstevel@tonic-gate 		if (sq->sq_needexcl == 0) {
5231*7c478bd9Sstevel@tonic-gate 			SQ_PUTCOUNT_SETFAST_LOCKED(sq);
5232*7c478bd9Sstevel@tonic-gate 		}
5233*7c478bd9Sstevel@tonic-gate 	}
5234*7c478bd9Sstevel@tonic-gate 
5235*7c478bd9Sstevel@tonic-gate 	/* Check if we need to enter the outer perimeter */
5236*7c478bd9Sstevel@tonic-gate 	if (!(type & c_outer)) {
5237*7c478bd9Sstevel@tonic-gate 		/*
5238*7c478bd9Sstevel@tonic-gate 		 * We have to enter the outer perimeter exclusively before
5239*7c478bd9Sstevel@tonic-gate 		 * we can increment sq_count to avoid deadlock. This implies
5240*7c478bd9Sstevel@tonic-gate 		 * that we have to re-check sq_flags and sq_count.
5241*7c478bd9Sstevel@tonic-gate 		 *
5242*7c478bd9Sstevel@tonic-gate 		 * is it possible to have c_inner set when c_outer is not set?
5243*7c478bd9Sstevel@tonic-gate 		 */
5244*7c478bd9Sstevel@tonic-gate 		if (!(type & c_inner)) {
5245*7c478bd9Sstevel@tonic-gate 			SQ_PUTLOCKS_EXIT(sq);
5246*7c478bd9Sstevel@tonic-gate 		}
5247*7c478bd9Sstevel@tonic-gate 		mutex_exit(SQLOCK(sq));
5248*7c478bd9Sstevel@tonic-gate 		outer_enter(sq->sq_outer, SQ_GOAWAY);
5249*7c478bd9Sstevel@tonic-gate 		mutex_enter(SQLOCK(sq));
5250*7c478bd9Sstevel@tonic-gate 		flags = sq->sq_flags;
5251*7c478bd9Sstevel@tonic-gate 		/*
5252*7c478bd9Sstevel@tonic-gate 		 * there should be no need to recheck sq_putcounts
5253*7c478bd9Sstevel@tonic-gate 		 * because outer_enter() has already waited for them to clear
5254*7c478bd9Sstevel@tonic-gate 		 * after setting SQ_WRITER.
5255*7c478bd9Sstevel@tonic-gate 		 */
5256*7c478bd9Sstevel@tonic-gate 		count = sq->sq_count;
5257*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
5258*7c478bd9Sstevel@tonic-gate 		/*
5259*7c478bd9Sstevel@tonic-gate 		 * SUMCHECK_SQ_PUTCOUNTS should return the sum instead
5260*7c478bd9Sstevel@tonic-gate 		 * of doing an ASSERT internally. Others should do
5261*7c478bd9Sstevel@tonic-gate 		 * something like
5262*7c478bd9Sstevel@tonic-gate 		 *	 ASSERT(SUMCHECK_SQ_PUTCOUNTS(sq) == 0);
5263*7c478bd9Sstevel@tonic-gate 		 * without the need to #ifdef DEBUG it.
5264*7c478bd9Sstevel@tonic-gate 		 */
5265*7c478bd9Sstevel@tonic-gate 		SUMCHECK_SQ_PUTCOUNTS(sq, 0);
5266*7c478bd9Sstevel@tonic-gate #endif
5267*7c478bd9Sstevel@tonic-gate 		while ((flags & (SQ_EXCL|SQ_BLOCKED|SQ_FROZEN)) ||
5268*7c478bd9Sstevel@tonic-gate 		    (!(type & c_inner) && count != 0)) {
5269*7c478bd9Sstevel@tonic-gate 			sq->sq_flags = flags | SQ_WANTWAKEUP;
5270*7c478bd9Sstevel@tonic-gate 			cv_wait(&sq->sq_wait, SQLOCK(sq));
5271*7c478bd9Sstevel@tonic-gate 			count = sq->sq_count;
5272*7c478bd9Sstevel@tonic-gate 			flags = sq->sq_flags;
5273*7c478bd9Sstevel@tonic-gate 		}
5274*7c478bd9Sstevel@tonic-gate 	}
5275*7c478bd9Sstevel@tonic-gate 
5276*7c478bd9Sstevel@tonic-gate 	sq->sq_count++;
5277*7c478bd9Sstevel@tonic-gate 	ASSERT(sq->sq_count != 0);	/* Wraparound */
5278*7c478bd9Sstevel@tonic-gate 	if (!(type & c_inner)) {
5279*7c478bd9Sstevel@tonic-gate 		/* Exclusive entry */
5280*7c478bd9Sstevel@tonic-gate 		ASSERT(sq->sq_count == 1);
5281*7c478bd9Sstevel@tonic-gate 		sq->sq_flags |= SQ_EXCL;
5282*7c478bd9Sstevel@tonic-gate 		if (type & c_outer) {
5283*7c478bd9Sstevel@tonic-gate 			SQ_PUTLOCKS_EXIT(sq);
5284*7c478bd9Sstevel@tonic-gate 		}
5285*7c478bd9Sstevel@tonic-gate 	}
5286*7c478bd9Sstevel@tonic-gate 	mutex_exit(SQLOCK(sq));
5287*7c478bd9Sstevel@tonic-gate }
5288*7c478bd9Sstevel@tonic-gate 
5289*7c478bd9Sstevel@tonic-gate /*
5290*7c478bd9Sstevel@tonic-gate  * leave a syncq. announce to framework that closes may proceed.
5291*7c478bd9Sstevel@tonic-gate  * c_inner and c_outer specifies which concurrency bits
5292*7c478bd9Sstevel@tonic-gate  * to check.
5293*7c478bd9Sstevel@tonic-gate  *
5294*7c478bd9Sstevel@tonic-gate  * must never be called from driver or module put entry point.
5295*7c478bd9Sstevel@tonic-gate  *
5296*7c478bd9Sstevel@tonic-gate  * no need to grab sq_putlocks here. See comment in strsubr.h that explains when
5297*7c478bd9Sstevel@tonic-gate  * sq_putlocks are used.
5298*7c478bd9Sstevel@tonic-gate  */
5299*7c478bd9Sstevel@tonic-gate void
5300*7c478bd9Sstevel@tonic-gate leavesq(syncq_t *sq, int entrypoint)
5301*7c478bd9Sstevel@tonic-gate {
5302*7c478bd9Sstevel@tonic-gate 	uint16_t	flags;
5303*7c478bd9Sstevel@tonic-gate 	uint16_t	type;
5304*7c478bd9Sstevel@tonic-gate 	uint_t		c_outer = entrypoint & SQ_CO;
5305*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
5306*7c478bd9Sstevel@tonic-gate 	uint_t		c_inner = entrypoint & SQ_CI;
5307*7c478bd9Sstevel@tonic-gate #endif
5308*7c478bd9Sstevel@tonic-gate 
5309*7c478bd9Sstevel@tonic-gate 	/*
5310*7c478bd9Sstevel@tonic-gate 	 * decrement ref count, drain the syncq if possible, and wake up
5311*7c478bd9Sstevel@tonic-gate 	 * any waiting close.
5312*7c478bd9Sstevel@tonic-gate 	 */
5313*7c478bd9Sstevel@tonic-gate 	ASSERT(sq);
5314*7c478bd9Sstevel@tonic-gate 	ASSERT(c_inner && c_outer);
5315*7c478bd9Sstevel@tonic-gate 	mutex_enter(SQLOCK(sq));
5316*7c478bd9Sstevel@tonic-gate 	flags = sq->sq_flags;
5317*7c478bd9Sstevel@tonic-gate 	type = sq->sq_type;
5318*7c478bd9Sstevel@tonic-gate 	if (flags & (SQ_QUEUED|SQ_WANTWAKEUP|SQ_WANTEXWAKEUP)) {
5319*7c478bd9Sstevel@tonic-gate 
5320*7c478bd9Sstevel@tonic-gate 		if (flags & SQ_WANTWAKEUP) {
5321*7c478bd9Sstevel@tonic-gate 			flags &= ~SQ_WANTWAKEUP;
5322*7c478bd9Sstevel@tonic-gate 			cv_broadcast(&sq->sq_wait);
5323*7c478bd9Sstevel@tonic-gate 		}
5324*7c478bd9Sstevel@tonic-gate 		if (flags & SQ_WANTEXWAKEUP) {
5325*7c478bd9Sstevel@tonic-gate 			flags &= ~SQ_WANTEXWAKEUP;
5326*7c478bd9Sstevel@tonic-gate 			cv_broadcast(&sq->sq_exitwait);
5327*7c478bd9Sstevel@tonic-gate 		}
5328*7c478bd9Sstevel@tonic-gate 
5329*7c478bd9Sstevel@tonic-gate 		if ((flags & SQ_QUEUED) && !(flags & SQ_STAYAWAY)) {
5330*7c478bd9Sstevel@tonic-gate 			/*
5331*7c478bd9Sstevel@tonic-gate 			 * The syncq needs to be drained. "Exit" the syncq
5332*7c478bd9Sstevel@tonic-gate 			 * before calling drain_syncq.
5333*7c478bd9Sstevel@tonic-gate 			 */
5334*7c478bd9Sstevel@tonic-gate 			ASSERT(sq->sq_count != 0);
5335*7c478bd9Sstevel@tonic-gate 			sq->sq_count--;
5336*7c478bd9Sstevel@tonic-gate 			ASSERT((flags & SQ_EXCL) || (type & c_inner));
5337*7c478bd9Sstevel@tonic-gate 			sq->sq_flags = flags & ~SQ_EXCL;
5338*7c478bd9Sstevel@tonic-gate 			drain_syncq(sq);
5339*7c478bd9Sstevel@tonic-gate 			ASSERT(MUTEX_NOT_HELD(SQLOCK(sq)));
5340*7c478bd9Sstevel@tonic-gate 			/* Check if we need to exit the outer perimeter */
5341*7c478bd9Sstevel@tonic-gate 			/* XXX will this ever be true? */
5342*7c478bd9Sstevel@tonic-gate 			if (!(type & c_outer))
5343*7c478bd9Sstevel@tonic-gate 				outer_exit(sq->sq_outer);
5344*7c478bd9Sstevel@tonic-gate 			return;
5345*7c478bd9Sstevel@tonic-gate 		}
5346*7c478bd9Sstevel@tonic-gate 	}
5347*7c478bd9Sstevel@tonic-gate 	ASSERT(sq->sq_count != 0);
5348*7c478bd9Sstevel@tonic-gate 	sq->sq_count--;
5349*7c478bd9Sstevel@tonic-gate 	ASSERT((flags & SQ_EXCL) || (type & c_inner));
5350*7c478bd9Sstevel@tonic-gate 	sq->sq_flags = flags & ~SQ_EXCL;
5351*7c478bd9Sstevel@tonic-gate 	mutex_exit(SQLOCK(sq));
5352*7c478bd9Sstevel@tonic-gate 
5353*7c478bd9Sstevel@tonic-gate 	/* Check if we need to exit the outer perimeter */
5354*7c478bd9Sstevel@tonic-gate 	if (!(sq->sq_type & c_outer))
5355*7c478bd9Sstevel@tonic-gate 		outer_exit(sq->sq_outer);
5356*7c478bd9Sstevel@tonic-gate }
5357*7c478bd9Sstevel@tonic-gate 
5358*7c478bd9Sstevel@tonic-gate /*
5359*7c478bd9Sstevel@tonic-gate  * Prevent q_next from changing in this stream by incrementing sq_count.
5360*7c478bd9Sstevel@tonic-gate  *
5361*7c478bd9Sstevel@tonic-gate  * no need to grab sq_putlocks here. See comment in strsubr.h that explains when
5362*7c478bd9Sstevel@tonic-gate  * sq_putlocks are used.
5363*7c478bd9Sstevel@tonic-gate  */
5364*7c478bd9Sstevel@tonic-gate void
5365*7c478bd9Sstevel@tonic-gate claimq(queue_t *qp)
5366*7c478bd9Sstevel@tonic-gate {
5367*7c478bd9Sstevel@tonic-gate 	syncq_t	*sq = qp->q_syncq;
5368*7c478bd9Sstevel@tonic-gate 
5369*7c478bd9Sstevel@tonic-gate 	mutex_enter(SQLOCK(sq));
5370*7c478bd9Sstevel@tonic-gate 	sq->sq_count++;
5371*7c478bd9Sstevel@tonic-gate 	ASSERT(sq->sq_count != 0);	/* Wraparound */
5372*7c478bd9Sstevel@tonic-gate 	mutex_exit(SQLOCK(sq));
5373*7c478bd9Sstevel@tonic-gate }
5374*7c478bd9Sstevel@tonic-gate 
5375*7c478bd9Sstevel@tonic-gate /*
5376*7c478bd9Sstevel@tonic-gate  * Undo claimq.
5377*7c478bd9Sstevel@tonic-gate  *
5378*7c478bd9Sstevel@tonic-gate  * no need to grab sq_putlocks here. See comment in strsubr.h that explains when
5379*7c478bd9Sstevel@tonic-gate  * sq_putlocks are used.
5380*7c478bd9Sstevel@tonic-gate  */
5381*7c478bd9Sstevel@tonic-gate void
5382*7c478bd9Sstevel@tonic-gate releaseq(queue_t *qp)
5383*7c478bd9Sstevel@tonic-gate {
5384*7c478bd9Sstevel@tonic-gate 	syncq_t	*sq = qp->q_syncq;
5385*7c478bd9Sstevel@tonic-gate 	uint16_t flags;
5386*7c478bd9Sstevel@tonic-gate 
5387*7c478bd9Sstevel@tonic-gate 	mutex_enter(SQLOCK(sq));
5388*7c478bd9Sstevel@tonic-gate 	ASSERT(sq->sq_count > 0);
5389*7c478bd9Sstevel@tonic-gate 	sq->sq_count--;
5390*7c478bd9Sstevel@tonic-gate 
5391*7c478bd9Sstevel@tonic-gate 	flags = sq->sq_flags;
5392*7c478bd9Sstevel@tonic-gate 	if (flags & (SQ_WANTWAKEUP|SQ_QUEUED)) {
5393*7c478bd9Sstevel@tonic-gate 		if (flags & SQ_WANTWAKEUP) {
5394*7c478bd9Sstevel@tonic-gate 			flags &= ~SQ_WANTWAKEUP;
5395*7c478bd9Sstevel@tonic-gate 			cv_broadcast(&sq->sq_wait);
5396*7c478bd9Sstevel@tonic-gate 		}
5397*7c478bd9Sstevel@tonic-gate 		sq->sq_flags = flags;
5398*7c478bd9Sstevel@tonic-gate 		if ((flags & SQ_QUEUED) && !(flags & (SQ_STAYAWAY|SQ_EXCL))) {
5399*7c478bd9Sstevel@tonic-gate 			/*
5400*7c478bd9Sstevel@tonic-gate 			 * To prevent potential recursive invocation of
5401*7c478bd9Sstevel@tonic-gate 			 * drain_syncq we do not call drain_syncq if count is
5402*7c478bd9Sstevel@tonic-gate 			 * non-zero.
5403*7c478bd9Sstevel@tonic-gate 			 */
5404*7c478bd9Sstevel@tonic-gate 			if (sq->sq_count == 0) {
5405*7c478bd9Sstevel@tonic-gate 				drain_syncq(sq);
5406*7c478bd9Sstevel@tonic-gate 				return;
5407*7c478bd9Sstevel@tonic-gate 			} else
5408*7c478bd9Sstevel@tonic-gate 				sqenable(sq);
5409*7c478bd9Sstevel@tonic-gate 		}
5410*7c478bd9Sstevel@tonic-gate 	}
5411*7c478bd9Sstevel@tonic-gate 	mutex_exit(SQLOCK(sq));
5412*7c478bd9Sstevel@tonic-gate }
5413*7c478bd9Sstevel@tonic-gate 
5414*7c478bd9Sstevel@tonic-gate /*
5415*7c478bd9Sstevel@tonic-gate  * Prevent q_next from changing in this stream by incrementing sd_refcnt.
5416*7c478bd9Sstevel@tonic-gate  */
5417*7c478bd9Sstevel@tonic-gate void
5418*7c478bd9Sstevel@tonic-gate claimstr(queue_t *qp)
5419*7c478bd9Sstevel@tonic-gate {
5420*7c478bd9Sstevel@tonic-gate 	struct stdata *stp = STREAM(qp);
5421*7c478bd9Sstevel@tonic-gate 
5422*7c478bd9Sstevel@tonic-gate 	mutex_enter(&stp->sd_reflock);
5423*7c478bd9Sstevel@tonic-gate 	stp->sd_refcnt++;
5424*7c478bd9Sstevel@tonic-gate 	ASSERT(stp->sd_refcnt != 0);	/* Wraparound */
5425*7c478bd9Sstevel@tonic-gate 	mutex_exit(&stp->sd_reflock);
5426*7c478bd9Sstevel@tonic-gate }
5427*7c478bd9Sstevel@tonic-gate 
5428*7c478bd9Sstevel@tonic-gate /*
5429*7c478bd9Sstevel@tonic-gate  * Undo claimstr.
5430*7c478bd9Sstevel@tonic-gate  */
5431*7c478bd9Sstevel@tonic-gate void
5432*7c478bd9Sstevel@tonic-gate releasestr(queue_t *qp)
5433*7c478bd9Sstevel@tonic-gate {
5434*7c478bd9Sstevel@tonic-gate 	struct stdata *stp = STREAM(qp);
5435*7c478bd9Sstevel@tonic-gate 
5436*7c478bd9Sstevel@tonic-gate 	mutex_enter(&stp->sd_reflock);
5437*7c478bd9Sstevel@tonic-gate 	ASSERT(stp->sd_refcnt != 0);
5438*7c478bd9Sstevel@tonic-gate 	stp->sd_refcnt--;
5439*7c478bd9Sstevel@tonic-gate 	cv_broadcast(&stp->sd_monitor);
5440*7c478bd9Sstevel@tonic-gate 	mutex_exit(&stp->sd_reflock);
5441*7c478bd9Sstevel@tonic-gate }
5442*7c478bd9Sstevel@tonic-gate 
5443*7c478bd9Sstevel@tonic-gate static syncq_t *
5444*7c478bd9Sstevel@tonic-gate new_syncq(void)
5445*7c478bd9Sstevel@tonic-gate {
5446*7c478bd9Sstevel@tonic-gate 	return (kmem_cache_alloc(syncq_cache, KM_SLEEP));
5447*7c478bd9Sstevel@tonic-gate }
5448*7c478bd9Sstevel@tonic-gate 
5449*7c478bd9Sstevel@tonic-gate static void
5450*7c478bd9Sstevel@tonic-gate free_syncq(syncq_t *sq)
5451*7c478bd9Sstevel@tonic-gate {
5452*7c478bd9Sstevel@tonic-gate 	ASSERT(sq->sq_head == NULL);
5453*7c478bd9Sstevel@tonic-gate 	ASSERT(sq->sq_outer == NULL);
5454*7c478bd9Sstevel@tonic-gate 	ASSERT(sq->sq_callbpend == NULL);
5455*7c478bd9Sstevel@tonic-gate 	ASSERT((sq->sq_onext == NULL && sq->sq_oprev == NULL) ||
5456*7c478bd9Sstevel@tonic-gate 	    (sq->sq_onext == sq && sq->sq_oprev == sq));
5457*7c478bd9Sstevel@tonic-gate 
5458*7c478bd9Sstevel@tonic-gate 	if (sq->sq_ciputctrl != NULL) {
5459*7c478bd9Sstevel@tonic-gate 		ASSERT(sq->sq_nciputctrl == n_ciputctrl - 1);
5460*7c478bd9Sstevel@tonic-gate 		SUMCHECK_CIPUTCTRL_COUNTS(sq->sq_ciputctrl,
5461*7c478bd9Sstevel@tonic-gate 		    sq->sq_nciputctrl, 0);
5462*7c478bd9Sstevel@tonic-gate 		ASSERT(ciputctrl_cache != NULL);
5463*7c478bd9Sstevel@tonic-gate 		kmem_cache_free(ciputctrl_cache, sq->sq_ciputctrl);
5464*7c478bd9Sstevel@tonic-gate 	}
5465*7c478bd9Sstevel@tonic-gate 
5466*7c478bd9Sstevel@tonic-gate 	sq->sq_tail = NULL;
5467*7c478bd9Sstevel@tonic-gate 	sq->sq_evhead = NULL;
5468*7c478bd9Sstevel@tonic-gate 	sq->sq_evtail = NULL;
5469*7c478bd9Sstevel@tonic-gate 	sq->sq_ciputctrl = NULL;
5470*7c478bd9Sstevel@tonic-gate 	sq->sq_nciputctrl = 0;
5471*7c478bd9Sstevel@tonic-gate 	sq->sq_count = 0;
5472*7c478bd9Sstevel@tonic-gate 	sq->sq_rmqcount = 0;
5473*7c478bd9Sstevel@tonic-gate 	sq->sq_callbflags = 0;
5474*7c478bd9Sstevel@tonic-gate 	sq->sq_cancelid = 0;
5475*7c478bd9Sstevel@tonic-gate 	sq->sq_next = NULL;
5476*7c478bd9Sstevel@tonic-gate 	sq->sq_needexcl = 0;
5477*7c478bd9Sstevel@tonic-gate 	sq->sq_svcflags = 0;
5478*7c478bd9Sstevel@tonic-gate 	sq->sq_nqueues = 0;
5479*7c478bd9Sstevel@tonic-gate 	sq->sq_pri = 0;
5480*7c478bd9Sstevel@tonic-gate 	sq->sq_onext = NULL;
5481*7c478bd9Sstevel@tonic-gate 	sq->sq_oprev = NULL;
5482*7c478bd9Sstevel@tonic-gate 	sq->sq_flags = 0;
5483*7c478bd9Sstevel@tonic-gate 	sq->sq_type = 0;
5484*7c478bd9Sstevel@tonic-gate 	sq->sq_servcount = 0;
5485*7c478bd9Sstevel@tonic-gate 
5486*7c478bd9Sstevel@tonic-gate 	kmem_cache_free(syncq_cache, sq);
5487*7c478bd9Sstevel@tonic-gate }
5488*7c478bd9Sstevel@tonic-gate 
5489*7c478bd9Sstevel@tonic-gate /* Outer perimeter code */
5490*7c478bd9Sstevel@tonic-gate 
5491*7c478bd9Sstevel@tonic-gate /*
5492*7c478bd9Sstevel@tonic-gate  * The outer syncq uses the fields and flags in the syncq slightly
5493*7c478bd9Sstevel@tonic-gate  * differently from the inner syncqs.
5494*7c478bd9Sstevel@tonic-gate  *	sq_count	Incremented when there are pending or running
5495*7c478bd9Sstevel@tonic-gate  *			writers at the outer perimeter to prevent the set of
5496*7c478bd9Sstevel@tonic-gate  *			inner syncqs that belong to the outer perimeter from
5497*7c478bd9Sstevel@tonic-gate  *			changing.
5498*7c478bd9Sstevel@tonic-gate  *	sq_head/tail	List of deferred qwriter(OUTER) operations.
5499*7c478bd9Sstevel@tonic-gate  *
5500*7c478bd9Sstevel@tonic-gate  *	SQ_BLOCKED	Set to prevent traversing of sq_next,sq_prev while
5501*7c478bd9Sstevel@tonic-gate  *			inner syncqs are added to or removed from the
5502*7c478bd9Sstevel@tonic-gate  *			outer perimeter.
5503*7c478bd9Sstevel@tonic-gate  *	SQ_QUEUED	sq_head/tail has messages or eventsqueued.
5504*7c478bd9Sstevel@tonic-gate  *
5505*7c478bd9Sstevel@tonic-gate  *	SQ_WRITER	A thread is currently traversing all the inner syncqs
5506*7c478bd9Sstevel@tonic-gate  *			setting the SQ_WRITER flag.
5507*7c478bd9Sstevel@tonic-gate  */
5508*7c478bd9Sstevel@tonic-gate 
5509*7c478bd9Sstevel@tonic-gate /*
5510*7c478bd9Sstevel@tonic-gate  * Get write access at the outer perimeter.
5511*7c478bd9Sstevel@tonic-gate  * Note that read access is done by entersq, putnext, and put by simply
5512*7c478bd9Sstevel@tonic-gate  * incrementing sq_count in the inner syncq.
5513*7c478bd9Sstevel@tonic-gate  *
5514*7c478bd9Sstevel@tonic-gate  * Waits until "flags" is no longer set in the outer to prevent multiple
5515*7c478bd9Sstevel@tonic-gate  * threads from having write access at the same time. SQ_WRITER has to be part
5516*7c478bd9Sstevel@tonic-gate  * of "flags".
5517*7c478bd9Sstevel@tonic-gate  *
5518*7c478bd9Sstevel@tonic-gate  * Increases sq_count on the outer syncq to keep away outer_insert/remove
5519*7c478bd9Sstevel@tonic-gate  * until the outer_exit is finished.
5520*7c478bd9Sstevel@tonic-gate  *
5521*7c478bd9Sstevel@tonic-gate  * outer_enter is vulnerable to starvation since it does not prevent new
5522*7c478bd9Sstevel@tonic-gate  * threads from entering the inner syncqs while it is waiting for sq_count to
5523*7c478bd9Sstevel@tonic-gate  * go to zero.
5524*7c478bd9Sstevel@tonic-gate  */
5525*7c478bd9Sstevel@tonic-gate void
5526*7c478bd9Sstevel@tonic-gate outer_enter(syncq_t *outer, uint16_t flags)
5527*7c478bd9Sstevel@tonic-gate {
5528*7c478bd9Sstevel@tonic-gate 	syncq_t	*sq;
5529*7c478bd9Sstevel@tonic-gate 	int	wait_needed;
5530*7c478bd9Sstevel@tonic-gate 	uint16_t	count;
5531*7c478bd9Sstevel@tonic-gate 
5532*7c478bd9Sstevel@tonic-gate 	ASSERT(outer->sq_outer == NULL && outer->sq_onext != NULL &&
5533*7c478bd9Sstevel@tonic-gate 	    outer->sq_oprev != NULL);
5534*7c478bd9Sstevel@tonic-gate 	ASSERT(flags & SQ_WRITER);
5535*7c478bd9Sstevel@tonic-gate 
5536*7c478bd9Sstevel@tonic-gate retry:
5537*7c478bd9Sstevel@tonic-gate 	mutex_enter(SQLOCK(outer));
5538*7c478bd9Sstevel@tonic-gate 	while (outer->sq_flags & flags) {
5539*7c478bd9Sstevel@tonic-gate 		outer->sq_flags |= SQ_WANTWAKEUP;
5540*7c478bd9Sstevel@tonic-gate 		cv_wait(&outer->sq_wait, SQLOCK(outer));
5541*7c478bd9Sstevel@tonic-gate 	}
5542*7c478bd9Sstevel@tonic-gate 
5543*7c478bd9Sstevel@tonic-gate 	ASSERT(!(outer->sq_flags & SQ_WRITER));
5544*7c478bd9Sstevel@tonic-gate 	outer->sq_flags |= SQ_WRITER;
5545*7c478bd9Sstevel@tonic-gate 	outer->sq_count++;
5546*7c478bd9Sstevel@tonic-gate 	ASSERT(outer->sq_count != 0);	/* wraparound */
5547*7c478bd9Sstevel@tonic-gate 	wait_needed = 0;
5548*7c478bd9Sstevel@tonic-gate 	/*
5549*7c478bd9Sstevel@tonic-gate 	 * Set SQ_WRITER on all the inner syncqs while holding
5550*7c478bd9Sstevel@tonic-gate 	 * the SQLOCK on the outer syncq. This ensures that the changing
5551*7c478bd9Sstevel@tonic-gate 	 * of SQ_WRITER is atomic under the outer SQLOCK.
5552*7c478bd9Sstevel@tonic-gate 	 */
5553*7c478bd9Sstevel@tonic-gate 	for (sq = outer->sq_onext; sq != outer; sq = sq->sq_onext) {
5554*7c478bd9Sstevel@tonic-gate 		mutex_enter(SQLOCK(sq));
5555*7c478bd9Sstevel@tonic-gate 		count = sq->sq_count;
5556*7c478bd9Sstevel@tonic-gate 		SQ_PUTLOCKS_ENTER(sq);
5557*7c478bd9Sstevel@tonic-gate 		sq->sq_flags |= SQ_WRITER;
5558*7c478bd9Sstevel@tonic-gate 		SUM_SQ_PUTCOUNTS(sq, count);
5559*7c478bd9Sstevel@tonic-gate 		if (count != 0)
5560*7c478bd9Sstevel@tonic-gate 			wait_needed = 1;
5561*7c478bd9Sstevel@tonic-gate 		SQ_PUTLOCKS_EXIT(sq);
5562*7c478bd9Sstevel@tonic-gate 		mutex_exit(SQLOCK(sq));
5563*7c478bd9Sstevel@tonic-gate 	}
5564*7c478bd9Sstevel@tonic-gate 	mutex_exit(SQLOCK(outer));
5565*7c478bd9Sstevel@tonic-gate 
5566*7c478bd9Sstevel@tonic-gate 	/*
5567*7c478bd9Sstevel@tonic-gate 	 * Get everybody out of the syncqs sequentially.
5568*7c478bd9Sstevel@tonic-gate 	 * Note that we don't actually need to aqiure the PUTLOCKS, since
5569*7c478bd9Sstevel@tonic-gate 	 * we have already cleared the fastbit, and set QWRITER.  By
5570*7c478bd9Sstevel@tonic-gate 	 * definition, the count can not increase since putnext will
5571*7c478bd9Sstevel@tonic-gate 	 * take the slowlock path (and the purpose of aquiring the
5572*7c478bd9Sstevel@tonic-gate 	 * putlocks was to make sure it didn't increase while we were
5573*7c478bd9Sstevel@tonic-gate 	 * waiting).
5574*7c478bd9Sstevel@tonic-gate 	 *
5575*7c478bd9Sstevel@tonic-gate 	 * Note that we still aquire the PUTLOCKS to be safe.
5576*7c478bd9Sstevel@tonic-gate 	 */
5577*7c478bd9Sstevel@tonic-gate 	if (wait_needed) {
5578*7c478bd9Sstevel@tonic-gate 		for (sq = outer->sq_onext; sq != outer; sq = sq->sq_onext) {
5579*7c478bd9Sstevel@tonic-gate 			mutex_enter(SQLOCK(sq));
5580*7c478bd9Sstevel@tonic-gate 			count = sq->sq_count;
5581*7c478bd9Sstevel@tonic-gate 			SQ_PUTLOCKS_ENTER(sq);
5582*7c478bd9Sstevel@tonic-gate 			SUM_SQ_PUTCOUNTS(sq, count);
5583*7c478bd9Sstevel@tonic-gate 			while (count != 0) {
5584*7c478bd9Sstevel@tonic-gate 				sq->sq_flags |= SQ_WANTWAKEUP;
5585*7c478bd9Sstevel@tonic-gate 				SQ_PUTLOCKS_EXIT(sq);
5586*7c478bd9Sstevel@tonic-gate 				cv_wait(&sq->sq_wait, SQLOCK(sq));
5587*7c478bd9Sstevel@tonic-gate 				count = sq->sq_count;
5588*7c478bd9Sstevel@tonic-gate 				SQ_PUTLOCKS_ENTER(sq);
5589*7c478bd9Sstevel@tonic-gate 				SUM_SQ_PUTCOUNTS(sq, count);
5590*7c478bd9Sstevel@tonic-gate 			}
5591*7c478bd9Sstevel@tonic-gate 			SQ_PUTLOCKS_EXIT(sq);
5592*7c478bd9Sstevel@tonic-gate 			mutex_exit(SQLOCK(sq));
5593*7c478bd9Sstevel@tonic-gate 		}
5594*7c478bd9Sstevel@tonic-gate 		/*
5595*7c478bd9Sstevel@tonic-gate 		 * Verify that none of the flags got set while we
5596*7c478bd9Sstevel@tonic-gate 		 * were waiting for the sq_counts to drop.
5597*7c478bd9Sstevel@tonic-gate 		 * If this happens we exit and retry entering the
5598*7c478bd9Sstevel@tonic-gate 		 * outer perimeter.
5599*7c478bd9Sstevel@tonic-gate 		 */
5600*7c478bd9Sstevel@tonic-gate 		mutex_enter(SQLOCK(outer));
5601*7c478bd9Sstevel@tonic-gate 		if (outer->sq_flags & (flags & ~SQ_WRITER)) {
5602*7c478bd9Sstevel@tonic-gate 			mutex_exit(SQLOCK(outer));
5603*7c478bd9Sstevel@tonic-gate 			outer_exit(outer);
5604*7c478bd9Sstevel@tonic-gate 			goto retry;
5605*7c478bd9Sstevel@tonic-gate 		}
5606*7c478bd9Sstevel@tonic-gate 		mutex_exit(SQLOCK(outer));
5607*7c478bd9Sstevel@tonic-gate 	}
5608*7c478bd9Sstevel@tonic-gate }
5609*7c478bd9Sstevel@tonic-gate 
5610*7c478bd9Sstevel@tonic-gate /*
5611*7c478bd9Sstevel@tonic-gate  * Drop the write access at the outer perimeter.
5612*7c478bd9Sstevel@tonic-gate  * Read access is dropped implicitly (by putnext, put, and leavesq) by
5613*7c478bd9Sstevel@tonic-gate  * decrementing sq_count.
5614*7c478bd9Sstevel@tonic-gate  */
5615*7c478bd9Sstevel@tonic-gate void
5616*7c478bd9Sstevel@tonic-gate outer_exit(syncq_t *outer)
5617*7c478bd9Sstevel@tonic-gate {
5618*7c478bd9Sstevel@tonic-gate 	syncq_t	*sq;
5619*7c478bd9Sstevel@tonic-gate 	int	 drain_needed;
5620*7c478bd9Sstevel@tonic-gate 	uint16_t flags;
5621*7c478bd9Sstevel@tonic-gate 
5622*7c478bd9Sstevel@tonic-gate 	ASSERT(outer->sq_outer == NULL && outer->sq_onext != NULL &&
5623*7c478bd9Sstevel@tonic-gate 	    outer->sq_oprev != NULL);
5624*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(SQLOCK(outer)));
5625*7c478bd9Sstevel@tonic-gate 
5626*7c478bd9Sstevel@tonic-gate 	/*
5627*7c478bd9Sstevel@tonic-gate 	 * Atomically (from the perspective of threads calling become_writer)
5628*7c478bd9Sstevel@tonic-gate 	 * drop the write access at the outer perimeter by holding
5629*7c478bd9Sstevel@tonic-gate 	 * SQLOCK(outer) across all the dropsq calls and the resetting of
5630*7c478bd9Sstevel@tonic-gate 	 * SQ_WRITER.
5631*7c478bd9Sstevel@tonic-gate 	 * This defines a locking order between the outer perimeter
5632*7c478bd9Sstevel@tonic-gate 	 * SQLOCK and the inner perimeter SQLOCKs.
5633*7c478bd9Sstevel@tonic-gate 	 */
5634*7c478bd9Sstevel@tonic-gate 	mutex_enter(SQLOCK(outer));
5635*7c478bd9Sstevel@tonic-gate 	flags = outer->sq_flags;
5636*7c478bd9Sstevel@tonic-gate 	ASSERT(outer->sq_flags & SQ_WRITER);
5637*7c478bd9Sstevel@tonic-gate 	if (flags & SQ_QUEUED) {
5638*7c478bd9Sstevel@tonic-gate 		write_now(outer);
5639*7c478bd9Sstevel@tonic-gate 		flags = outer->sq_flags;
5640*7c478bd9Sstevel@tonic-gate 	}
5641*7c478bd9Sstevel@tonic-gate 
5642*7c478bd9Sstevel@tonic-gate 	/*
5643*7c478bd9Sstevel@tonic-gate 	 * sq_onext is stable since sq_count has not yet been decreased.
5644*7c478bd9Sstevel@tonic-gate 	 * Reset the SQ_WRITER flags in all syncqs.
5645*7c478bd9Sstevel@tonic-gate 	 * After dropping SQ_WRITER on the outer syncq we empty all the
5646*7c478bd9Sstevel@tonic-gate 	 * inner syncqs.
5647*7c478bd9Sstevel@tonic-gate 	 */
5648*7c478bd9Sstevel@tonic-gate 	drain_needed = 0;
5649*7c478bd9Sstevel@tonic-gate 	for (sq = outer->sq_onext; sq != outer; sq = sq->sq_onext)
5650*7c478bd9Sstevel@tonic-gate 		drain_needed += dropsq(sq, SQ_WRITER);
5651*7c478bd9Sstevel@tonic-gate 	ASSERT(!(outer->sq_flags & SQ_QUEUED));
5652*7c478bd9Sstevel@tonic-gate 	flags &= ~SQ_WRITER;
5653*7c478bd9Sstevel@tonic-gate 	if (drain_needed) {
5654*7c478bd9Sstevel@tonic-gate 		outer->sq_flags = flags;
5655*7c478bd9Sstevel@tonic-gate 		mutex_exit(SQLOCK(outer));
5656*7c478bd9Sstevel@tonic-gate 		for (sq = outer->sq_onext; sq != outer; sq = sq->sq_onext)
5657*7c478bd9Sstevel@tonic-gate 			emptysq(sq);
5658*7c478bd9Sstevel@tonic-gate 		mutex_enter(SQLOCK(outer));
5659*7c478bd9Sstevel@tonic-gate 		flags = outer->sq_flags;
5660*7c478bd9Sstevel@tonic-gate 	}
5661*7c478bd9Sstevel@tonic-gate 	if (flags & SQ_WANTWAKEUP) {
5662*7c478bd9Sstevel@tonic-gate 		flags &= ~SQ_WANTWAKEUP;
5663*7c478bd9Sstevel@tonic-gate 		cv_broadcast(&outer->sq_wait);
5664*7c478bd9Sstevel@tonic-gate 	}
5665*7c478bd9Sstevel@tonic-gate 	outer->sq_flags = flags;
5666*7c478bd9Sstevel@tonic-gate 	ASSERT(outer->sq_count > 0);
5667*7c478bd9Sstevel@tonic-gate 	outer->sq_count--;
5668*7c478bd9Sstevel@tonic-gate 	mutex_exit(SQLOCK(outer));
5669*7c478bd9Sstevel@tonic-gate }
5670*7c478bd9Sstevel@tonic-gate 
5671*7c478bd9Sstevel@tonic-gate /*
5672*7c478bd9Sstevel@tonic-gate  * Add another syncq to an outer perimeter.
5673*7c478bd9Sstevel@tonic-gate  * Block out all other access to the outer perimeter while it is being
5674*7c478bd9Sstevel@tonic-gate  * changed using blocksq.
5675*7c478bd9Sstevel@tonic-gate  * Assumes that the caller has *not* done an outer_enter.
5676*7c478bd9Sstevel@tonic-gate  *
5677*7c478bd9Sstevel@tonic-gate  * Vulnerable to starvation in blocksq.
5678*7c478bd9Sstevel@tonic-gate  */
5679*7c478bd9Sstevel@tonic-gate static void
5680*7c478bd9Sstevel@tonic-gate outer_insert(syncq_t *outer, syncq_t *sq)
5681*7c478bd9Sstevel@tonic-gate {
5682*7c478bd9Sstevel@tonic-gate 	ASSERT(outer->sq_outer == NULL && outer->sq_onext != NULL &&
5683*7c478bd9Sstevel@tonic-gate 	    outer->sq_oprev != NULL);
5684*7c478bd9Sstevel@tonic-gate 	ASSERT(sq->sq_outer == NULL && sq->sq_onext == NULL &&
5685*7c478bd9Sstevel@tonic-gate 	    sq->sq_oprev == NULL);	/* Can't be in an outer perimeter */
5686*7c478bd9Sstevel@tonic-gate 
5687*7c478bd9Sstevel@tonic-gate 	/* Get exclusive access to the outer perimeter list */
5688*7c478bd9Sstevel@tonic-gate 	blocksq(outer, SQ_BLOCKED, 0);
5689*7c478bd9Sstevel@tonic-gate 	ASSERT(outer->sq_flags & SQ_BLOCKED);
5690*7c478bd9Sstevel@tonic-gate 	ASSERT(!(outer->sq_flags & SQ_WRITER));
5691*7c478bd9Sstevel@tonic-gate 
5692*7c478bd9Sstevel@tonic-gate 	mutex_enter(SQLOCK(sq));
5693*7c478bd9Sstevel@tonic-gate 	sq->sq_outer = outer;
5694*7c478bd9Sstevel@tonic-gate 	outer->sq_onext->sq_oprev = sq;
5695*7c478bd9Sstevel@tonic-gate 	sq->sq_onext = outer->sq_onext;
5696*7c478bd9Sstevel@tonic-gate 	outer->sq_onext = sq;
5697*7c478bd9Sstevel@tonic-gate 	sq->sq_oprev = outer;
5698*7c478bd9Sstevel@tonic-gate 	mutex_exit(SQLOCK(sq));
5699*7c478bd9Sstevel@tonic-gate 	unblocksq(outer, SQ_BLOCKED, 1);
5700*7c478bd9Sstevel@tonic-gate }
5701*7c478bd9Sstevel@tonic-gate 
5702*7c478bd9Sstevel@tonic-gate /*
5703*7c478bd9Sstevel@tonic-gate  * Remove a syncq from an outer perimeter.
5704*7c478bd9Sstevel@tonic-gate  * Block out all other access to the outer perimeter while it is being
5705*7c478bd9Sstevel@tonic-gate  * changed using blocksq.
5706*7c478bd9Sstevel@tonic-gate  * Assumes that the caller has *not* done an outer_enter.
5707*7c478bd9Sstevel@tonic-gate  *
5708*7c478bd9Sstevel@tonic-gate  * Vulnerable to starvation in blocksq.
5709*7c478bd9Sstevel@tonic-gate  */
5710*7c478bd9Sstevel@tonic-gate static void
5711*7c478bd9Sstevel@tonic-gate outer_remove(syncq_t *outer, syncq_t *sq)
5712*7c478bd9Sstevel@tonic-gate {
5713*7c478bd9Sstevel@tonic-gate 	ASSERT(outer->sq_outer == NULL && outer->sq_onext != NULL &&
5714*7c478bd9Sstevel@tonic-gate 	    outer->sq_oprev != NULL);
5715*7c478bd9Sstevel@tonic-gate 	ASSERT(sq->sq_outer == outer);
5716*7c478bd9Sstevel@tonic-gate 
5717*7c478bd9Sstevel@tonic-gate 	/* Get exclusive access to the outer perimeter list */
5718*7c478bd9Sstevel@tonic-gate 	blocksq(outer, SQ_BLOCKED, 0);
5719*7c478bd9Sstevel@tonic-gate 	ASSERT(outer->sq_flags & SQ_BLOCKED);
5720*7c478bd9Sstevel@tonic-gate 	ASSERT(!(outer->sq_flags & SQ_WRITER));
5721*7c478bd9Sstevel@tonic-gate 
5722*7c478bd9Sstevel@tonic-gate 	mutex_enter(SQLOCK(sq));
5723*7c478bd9Sstevel@tonic-gate 	sq->sq_outer = NULL;
5724*7c478bd9Sstevel@tonic-gate 	sq->sq_onext->sq_oprev = sq->sq_oprev;
5725*7c478bd9Sstevel@tonic-gate 	sq->sq_oprev->sq_onext = sq->sq_onext;
5726*7c478bd9Sstevel@tonic-gate 	sq->sq_oprev = sq->sq_onext = NULL;
5727*7c478bd9Sstevel@tonic-gate 	mutex_exit(SQLOCK(sq));
5728*7c478bd9Sstevel@tonic-gate 	unblocksq(outer, SQ_BLOCKED, 1);
5729*7c478bd9Sstevel@tonic-gate }
5730*7c478bd9Sstevel@tonic-gate 
5731*7c478bd9Sstevel@tonic-gate /*
5732*7c478bd9Sstevel@tonic-gate  * Queue a deferred qwriter(OUTER) callback for this outer perimeter.
5733*7c478bd9Sstevel@tonic-gate  * If this is the first callback for this outer perimeter then add
5734*7c478bd9Sstevel@tonic-gate  * this outer perimeter to the list of outer perimeters that
5735*7c478bd9Sstevel@tonic-gate  * the qwriter_outer_thread will process.
5736*7c478bd9Sstevel@tonic-gate  *
5737*7c478bd9Sstevel@tonic-gate  * Increments sq_count in the outer syncq to prevent the membership
5738*7c478bd9Sstevel@tonic-gate  * of the outer perimeter (in terms of inner syncqs) to change while
5739*7c478bd9Sstevel@tonic-gate  * the callback is pending.
5740*7c478bd9Sstevel@tonic-gate  */
5741*7c478bd9Sstevel@tonic-gate static void
5742*7c478bd9Sstevel@tonic-gate queue_writer(syncq_t *outer, void (*func)(), queue_t *q, mblk_t *mp)
5743*7c478bd9Sstevel@tonic-gate {
5744*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(SQLOCK(outer)));
5745*7c478bd9Sstevel@tonic-gate 
5746*7c478bd9Sstevel@tonic-gate 	mp->b_prev = (mblk_t *)func;
5747*7c478bd9Sstevel@tonic-gate 	mp->b_queue = q;
5748*7c478bd9Sstevel@tonic-gate 	mp->b_next = NULL;
5749*7c478bd9Sstevel@tonic-gate 	outer->sq_count++;	/* Decremented when dequeued */
5750*7c478bd9Sstevel@tonic-gate 	ASSERT(outer->sq_count != 0);	/* Wraparound */
5751*7c478bd9Sstevel@tonic-gate 	if (outer->sq_evhead == NULL) {
5752*7c478bd9Sstevel@tonic-gate 		/* First message. */
5753*7c478bd9Sstevel@tonic-gate 		outer->sq_evhead = outer->sq_evtail = mp;
5754*7c478bd9Sstevel@tonic-gate 		outer->sq_flags |= SQ_EVENTS;
5755*7c478bd9Sstevel@tonic-gate 		mutex_exit(SQLOCK(outer));
5756*7c478bd9Sstevel@tonic-gate 		STRSTAT(qwr_outer);
5757*7c478bd9Sstevel@tonic-gate 		(void) taskq_dispatch(streams_taskq,
5758*7c478bd9Sstevel@tonic-gate 		    (task_func_t *)qwriter_outer_service, outer, TQ_SLEEP);
5759*7c478bd9Sstevel@tonic-gate 	} else {
5760*7c478bd9Sstevel@tonic-gate 		ASSERT(outer->sq_flags & SQ_EVENTS);
5761*7c478bd9Sstevel@tonic-gate 		outer->sq_evtail->b_next = mp;
5762*7c478bd9Sstevel@tonic-gate 		outer->sq_evtail = mp;
5763*7c478bd9Sstevel@tonic-gate 		mutex_exit(SQLOCK(outer));
5764*7c478bd9Sstevel@tonic-gate 	}
5765*7c478bd9Sstevel@tonic-gate }
5766*7c478bd9Sstevel@tonic-gate 
5767*7c478bd9Sstevel@tonic-gate /*
5768*7c478bd9Sstevel@tonic-gate  * Try and upgrade to write access at the outer perimeter. If this can
5769*7c478bd9Sstevel@tonic-gate  * not be done without blocking then queue the callback to be done
5770*7c478bd9Sstevel@tonic-gate  * by the qwriter_outer_thread.
5771*7c478bd9Sstevel@tonic-gate  *
5772*7c478bd9Sstevel@tonic-gate  * This routine can only be called from put or service procedures plus
5773*7c478bd9Sstevel@tonic-gate  * asynchronous callback routines that have properly entered to
5774*7c478bd9Sstevel@tonic-gate  * queue (with entersq.) Thus qwriter(OUTER) assumes the caller has one claim
5775*7c478bd9Sstevel@tonic-gate  * on the syncq associated with q.
5776*7c478bd9Sstevel@tonic-gate  */
5777*7c478bd9Sstevel@tonic-gate void
5778*7c478bd9Sstevel@tonic-gate qwriter_outer(queue_t *q, mblk_t *mp, void (*func)())
5779*7c478bd9Sstevel@tonic-gate {
5780*7c478bd9Sstevel@tonic-gate 	syncq_t	*osq, *sq, *outer;
5781*7c478bd9Sstevel@tonic-gate 	int	failed;
5782*7c478bd9Sstevel@tonic-gate 	uint16_t flags;
5783*7c478bd9Sstevel@tonic-gate 
5784*7c478bd9Sstevel@tonic-gate 	osq = q->q_syncq;
5785*7c478bd9Sstevel@tonic-gate 	outer = osq->sq_outer;
5786*7c478bd9Sstevel@tonic-gate 	if (outer == NULL)
5787*7c478bd9Sstevel@tonic-gate 		panic("qwriter(PERIM_OUTER): no outer perimeter");
5788*7c478bd9Sstevel@tonic-gate 	ASSERT(outer->sq_outer == NULL && outer->sq_onext != NULL &&
5789*7c478bd9Sstevel@tonic-gate 	    outer->sq_oprev != NULL);
5790*7c478bd9Sstevel@tonic-gate 
5791*7c478bd9Sstevel@tonic-gate 	mutex_enter(SQLOCK(outer));
5792*7c478bd9Sstevel@tonic-gate 	flags = outer->sq_flags;
5793*7c478bd9Sstevel@tonic-gate 	/*
5794*7c478bd9Sstevel@tonic-gate 	 * If some thread is traversing sq_next, or if we are blocked by
5795*7c478bd9Sstevel@tonic-gate 	 * outer_insert or outer_remove, or if the we already have queued
5796*7c478bd9Sstevel@tonic-gate 	 * callbacks, then queue this callback for later processing.
5797*7c478bd9Sstevel@tonic-gate 	 *
5798*7c478bd9Sstevel@tonic-gate 	 * Also queue the qwriter for an interrupt thread in order
5799*7c478bd9Sstevel@tonic-gate 	 * to reduce the time spent running at high IPL.
5800*7c478bd9Sstevel@tonic-gate 	 * to identify there are events.
5801*7c478bd9Sstevel@tonic-gate 	 */
5802*7c478bd9Sstevel@tonic-gate 	if ((flags & SQ_GOAWAY) || (curthread->t_pri >= kpreemptpri)) {
5803*7c478bd9Sstevel@tonic-gate 		/*
5804*7c478bd9Sstevel@tonic-gate 		 * Queue the become_writer request.
5805*7c478bd9Sstevel@tonic-gate 		 * The queueing is atomic under SQLOCK(outer) in order
5806*7c478bd9Sstevel@tonic-gate 		 * to synchronize with outer_exit.
5807*7c478bd9Sstevel@tonic-gate 		 * queue_writer will drop the outer SQLOCK
5808*7c478bd9Sstevel@tonic-gate 		 */
5809*7c478bd9Sstevel@tonic-gate 		if (flags & SQ_BLOCKED) {
5810*7c478bd9Sstevel@tonic-gate 			/* Must set SQ_WRITER on inner perimeter */
5811*7c478bd9Sstevel@tonic-gate 			mutex_enter(SQLOCK(osq));
5812*7c478bd9Sstevel@tonic-gate 			osq->sq_flags |= SQ_WRITER;
5813*7c478bd9Sstevel@tonic-gate 			mutex_exit(SQLOCK(osq));
5814*7c478bd9Sstevel@tonic-gate 		} else {
5815*7c478bd9Sstevel@tonic-gate 			if (!(flags & SQ_WRITER)) {
5816*7c478bd9Sstevel@tonic-gate 				/*
5817*7c478bd9Sstevel@tonic-gate 				 * The outer could have been SQ_BLOCKED thus
5818*7c478bd9Sstevel@tonic-gate 				 * SQ_WRITER might not be set on the inner.
5819*7c478bd9Sstevel@tonic-gate 				 */
5820*7c478bd9Sstevel@tonic-gate 				mutex_enter(SQLOCK(osq));
5821*7c478bd9Sstevel@tonic-gate 				osq->sq_flags |= SQ_WRITER;
5822*7c478bd9Sstevel@tonic-gate 				mutex_exit(SQLOCK(osq));
5823*7c478bd9Sstevel@tonic-gate 			}
5824*7c478bd9Sstevel@tonic-gate 			ASSERT(osq->sq_flags & SQ_WRITER);
5825*7c478bd9Sstevel@tonic-gate 		}
5826*7c478bd9Sstevel@tonic-gate 		queue_writer(outer, func, q, mp);
5827*7c478bd9Sstevel@tonic-gate 		return;
5828*7c478bd9Sstevel@tonic-gate 	}
5829*7c478bd9Sstevel@tonic-gate 	/*
5830*7c478bd9Sstevel@tonic-gate 	 * We are half-way to exclusive access to the outer perimeter.
5831*7c478bd9Sstevel@tonic-gate 	 * Prevent any outer_enter, qwriter(OUTER), or outer_insert/remove
5832*7c478bd9Sstevel@tonic-gate 	 * while the inner syncqs are traversed.
5833*7c478bd9Sstevel@tonic-gate 	 */
5834*7c478bd9Sstevel@tonic-gate 	outer->sq_count++;
5835*7c478bd9Sstevel@tonic-gate 	ASSERT(outer->sq_count != 0);	/* wraparound */
5836*7c478bd9Sstevel@tonic-gate 	flags |= SQ_WRITER;
5837*7c478bd9Sstevel@tonic-gate 	/*
5838*7c478bd9Sstevel@tonic-gate 	 * Check if we can run the function immediately. Mark all
5839*7c478bd9Sstevel@tonic-gate 	 * syncqs with the writer flag to prevent new entries into
5840*7c478bd9Sstevel@tonic-gate 	 * put and service procedures.
5841*7c478bd9Sstevel@tonic-gate 	 *
5842*7c478bd9Sstevel@tonic-gate 	 * Set SQ_WRITER on all the inner syncqs while holding
5843*7c478bd9Sstevel@tonic-gate 	 * the SQLOCK on the outer syncq. This ensures that the changing
5844*7c478bd9Sstevel@tonic-gate 	 * of SQ_WRITER is atomic under the outer SQLOCK.
5845*7c478bd9Sstevel@tonic-gate 	 */
5846*7c478bd9Sstevel@tonic-gate 	failed = 0;
5847*7c478bd9Sstevel@tonic-gate 	for (sq = outer->sq_onext; sq != outer; sq = sq->sq_onext) {
5848*7c478bd9Sstevel@tonic-gate 		uint16_t count;
5849*7c478bd9Sstevel@tonic-gate 		uint_t	maxcnt = (sq == osq) ? 1 : 0;
5850*7c478bd9Sstevel@tonic-gate 
5851*7c478bd9Sstevel@tonic-gate 		mutex_enter(SQLOCK(sq));
5852*7c478bd9Sstevel@tonic-gate 		count = sq->sq_count;
5853*7c478bd9Sstevel@tonic-gate 		SQ_PUTLOCKS_ENTER(sq);
5854*7c478bd9Sstevel@tonic-gate 		SUM_SQ_PUTCOUNTS(sq, count);
5855*7c478bd9Sstevel@tonic-gate 		if (sq->sq_count > maxcnt)
5856*7c478bd9Sstevel@tonic-gate 			failed = 1;
5857*7c478bd9Sstevel@tonic-gate 		sq->sq_flags |= SQ_WRITER;
5858*7c478bd9Sstevel@tonic-gate 		SQ_PUTLOCKS_EXIT(sq);
5859*7c478bd9Sstevel@tonic-gate 		mutex_exit(SQLOCK(sq));
5860*7c478bd9Sstevel@tonic-gate 	}
5861*7c478bd9Sstevel@tonic-gate 	if (failed) {
5862*7c478bd9Sstevel@tonic-gate 		/*
5863*7c478bd9Sstevel@tonic-gate 		 * Some other thread has a read claim on the outer perimeter.
5864*7c478bd9Sstevel@tonic-gate 		 * Queue the callback for deferred processing.
5865*7c478bd9Sstevel@tonic-gate 		 *
5866*7c478bd9Sstevel@tonic-gate 		 * queue_writer will set SQ_QUEUED before we drop SQ_WRITER
5867*7c478bd9Sstevel@tonic-gate 		 * so that other qwriter(OUTER) calls will queue their
5868*7c478bd9Sstevel@tonic-gate 		 * callbacks as well. queue_writer increments sq_count so we
5869*7c478bd9Sstevel@tonic-gate 		 * decrement to compensate for the our increment.
5870*7c478bd9Sstevel@tonic-gate 		 *
5871*7c478bd9Sstevel@tonic-gate 		 * Dropping SQ_WRITER enables the writer thread to work
5872*7c478bd9Sstevel@tonic-gate 		 * on this outer perimeter.
5873*7c478bd9Sstevel@tonic-gate 		 */
5874*7c478bd9Sstevel@tonic-gate 		outer->sq_flags = flags;
5875*7c478bd9Sstevel@tonic-gate 		queue_writer(outer, func, q, mp);
5876*7c478bd9Sstevel@tonic-gate 		/* queue_writer dropper the lock */
5877*7c478bd9Sstevel@tonic-gate 		mutex_enter(SQLOCK(outer));
5878*7c478bd9Sstevel@tonic-gate 		ASSERT(outer->sq_count > 0);
5879*7c478bd9Sstevel@tonic-gate 		outer->sq_count--;
5880*7c478bd9Sstevel@tonic-gate 		ASSERT(outer->sq_flags & SQ_WRITER);
5881*7c478bd9Sstevel@tonic-gate 		flags = outer->sq_flags;
5882*7c478bd9Sstevel@tonic-gate 		flags &= ~SQ_WRITER;
5883*7c478bd9Sstevel@tonic-gate 		if (flags & SQ_WANTWAKEUP) {
5884*7c478bd9Sstevel@tonic-gate 			flags &= ~SQ_WANTWAKEUP;
5885*7c478bd9Sstevel@tonic-gate 			cv_broadcast(&outer->sq_wait);
5886*7c478bd9Sstevel@tonic-gate 		}
5887*7c478bd9Sstevel@tonic-gate 		outer->sq_flags = flags;
5888*7c478bd9Sstevel@tonic-gate 		mutex_exit(SQLOCK(outer));
5889*7c478bd9Sstevel@tonic-gate 		return;
5890*7c478bd9Sstevel@tonic-gate 	} else {
5891*7c478bd9Sstevel@tonic-gate 		outer->sq_flags = flags;
5892*7c478bd9Sstevel@tonic-gate 		mutex_exit(SQLOCK(outer));
5893*7c478bd9Sstevel@tonic-gate 	}
5894*7c478bd9Sstevel@tonic-gate 
5895*7c478bd9Sstevel@tonic-gate 	/* Can run it immediately */
5896*7c478bd9Sstevel@tonic-gate 	(*func)(q, mp);
5897*7c478bd9Sstevel@tonic-gate 
5898*7c478bd9Sstevel@tonic-gate 	outer_exit(outer);
5899*7c478bd9Sstevel@tonic-gate }
5900*7c478bd9Sstevel@tonic-gate 
5901*7c478bd9Sstevel@tonic-gate /*
5902*7c478bd9Sstevel@tonic-gate  * Dequeue all writer callbacks from the outer perimeter and run them.
5903*7c478bd9Sstevel@tonic-gate  */
5904*7c478bd9Sstevel@tonic-gate static void
5905*7c478bd9Sstevel@tonic-gate write_now(syncq_t *outer)
5906*7c478bd9Sstevel@tonic-gate {
5907*7c478bd9Sstevel@tonic-gate 	mblk_t		*mp;
5908*7c478bd9Sstevel@tonic-gate 	queue_t		*q;
5909*7c478bd9Sstevel@tonic-gate 	void	(*func)();
5910*7c478bd9Sstevel@tonic-gate 
5911*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(SQLOCK(outer)));
5912*7c478bd9Sstevel@tonic-gate 	ASSERT(outer->sq_outer == NULL && outer->sq_onext != NULL &&
5913*7c478bd9Sstevel@tonic-gate 	    outer->sq_oprev != NULL);
5914*7c478bd9Sstevel@tonic-gate 	while ((mp = outer->sq_evhead) != NULL) {
5915*7c478bd9Sstevel@tonic-gate 		/*
5916*7c478bd9Sstevel@tonic-gate 		 * queues cannot be placed on the queuelist on the outer
5917*7c478bd9Sstevel@tonic-gate 		 * perimiter.
5918*7c478bd9Sstevel@tonic-gate 		 */
5919*7c478bd9Sstevel@tonic-gate 		ASSERT(!(outer->sq_flags & SQ_MESSAGES));
5920*7c478bd9Sstevel@tonic-gate 		ASSERT((outer->sq_flags & SQ_EVENTS));
5921*7c478bd9Sstevel@tonic-gate 
5922*7c478bd9Sstevel@tonic-gate 		outer->sq_evhead = mp->b_next;
5923*7c478bd9Sstevel@tonic-gate 		if (outer->sq_evhead == NULL) {
5924*7c478bd9Sstevel@tonic-gate 			outer->sq_evtail = NULL;
5925*7c478bd9Sstevel@tonic-gate 			outer->sq_flags &= ~SQ_EVENTS;
5926*7c478bd9Sstevel@tonic-gate 		}
5927*7c478bd9Sstevel@tonic-gate 		ASSERT(outer->sq_count != 0);
5928*7c478bd9Sstevel@tonic-gate 		outer->sq_count--;	/* Incremented when enqueued. */
5929*7c478bd9Sstevel@tonic-gate 		mutex_exit(SQLOCK(outer));
5930*7c478bd9Sstevel@tonic-gate 		/*
5931*7c478bd9Sstevel@tonic-gate 		 * Drop the message if the queue is closing.
5932*7c478bd9Sstevel@tonic-gate 		 * Make sure that the queue is "claimed" when the callback
5933*7c478bd9Sstevel@tonic-gate 		 * is run in order to satisfy various ASSERTs.
5934*7c478bd9Sstevel@tonic-gate 		 */
5935*7c478bd9Sstevel@tonic-gate 		q = mp->b_queue;
5936*7c478bd9Sstevel@tonic-gate 		func = (void (*)())mp->b_prev;
5937*7c478bd9Sstevel@tonic-gate 		ASSERT(func != NULL);
5938*7c478bd9Sstevel@tonic-gate 		mp->b_next = mp->b_prev = NULL;
5939*7c478bd9Sstevel@tonic-gate 		if (q->q_flag & QWCLOSE) {
5940*7c478bd9Sstevel@tonic-gate 			freemsg(mp);
5941*7c478bd9Sstevel@tonic-gate 		} else {
5942*7c478bd9Sstevel@tonic-gate 			claimq(q);
5943*7c478bd9Sstevel@tonic-gate 			(*func)(q, mp);
5944*7c478bd9Sstevel@tonic-gate 			releaseq(q);
5945*7c478bd9Sstevel@tonic-gate 		}
5946*7c478bd9Sstevel@tonic-gate 		mutex_enter(SQLOCK(outer));
5947*7c478bd9Sstevel@tonic-gate 	}
5948*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(SQLOCK(outer)));
5949*7c478bd9Sstevel@tonic-gate }
5950*7c478bd9Sstevel@tonic-gate 
5951*7c478bd9Sstevel@tonic-gate /*
5952*7c478bd9Sstevel@tonic-gate  * The list of messages on the inner syncq is effectively hashed
5953*7c478bd9Sstevel@tonic-gate  * by destination queue.  These destination queues are doubly
5954*7c478bd9Sstevel@tonic-gate  * linked lists (hopefully) in priority order.  Messages are then
5955*7c478bd9Sstevel@tonic-gate  * put on the queue referenced by the q_sqhead/q_sqtail elements.
5956*7c478bd9Sstevel@tonic-gate  * Additional messages are linked together by the b_next/b_prev
5957*7c478bd9Sstevel@tonic-gate  * elements in the mblk, with (similar to putq()) the first message
5958*7c478bd9Sstevel@tonic-gate  * having a NULL b_prev and the last message having a NULL b_next.
5959*7c478bd9Sstevel@tonic-gate  *
5960*7c478bd9Sstevel@tonic-gate  * Events, such as qwriter callbacks, are put onto a list in FIFO
5961*7c478bd9Sstevel@tonic-gate  * order referenced by sq_evhead, and sq_evtail.  This is a singly
5962*7c478bd9Sstevel@tonic-gate  * linked list, and messages here MUST be processed in the order queued.
5963*7c478bd9Sstevel@tonic-gate  */
5964*7c478bd9Sstevel@tonic-gate 
5965*7c478bd9Sstevel@tonic-gate /*
5966*7c478bd9Sstevel@tonic-gate  * Run the events on the syncq event list (sq_evhead).
5967*7c478bd9Sstevel@tonic-gate  * Assumes there is only one claim on the syncq, it is
5968*7c478bd9Sstevel@tonic-gate  * already exclusive (SQ_EXCL set), and the SQLOCK held.
5969*7c478bd9Sstevel@tonic-gate  * Messages here are processed in order, with the SQ_EXCL bit
5970*7c478bd9Sstevel@tonic-gate  * held all the way through till the last message is processed.
5971*7c478bd9Sstevel@tonic-gate  */
5972*7c478bd9Sstevel@tonic-gate void
5973*7c478bd9Sstevel@tonic-gate sq_run_events(syncq_t *sq)
5974*7c478bd9Sstevel@tonic-gate {
5975*7c478bd9Sstevel@tonic-gate 	mblk_t		*bp;
5976*7c478bd9Sstevel@tonic-gate 	queue_t		*qp;
5977*7c478bd9Sstevel@tonic-gate 	uint16_t	flags = sq->sq_flags;
5978*7c478bd9Sstevel@tonic-gate 	void		(*func)();
5979*7c478bd9Sstevel@tonic-gate 
5980*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(SQLOCK(sq)));
5981*7c478bd9Sstevel@tonic-gate 	ASSERT((sq->sq_outer == NULL && sq->sq_onext == NULL &&
5982*7c478bd9Sstevel@tonic-gate 		sq->sq_oprev == NULL) ||
5983*7c478bd9Sstevel@tonic-gate 		(sq->sq_outer != NULL && sq->sq_onext != NULL &&
5984*7c478bd9Sstevel@tonic-gate 		sq->sq_oprev != NULL));
5985*7c478bd9Sstevel@tonic-gate 
5986*7c478bd9Sstevel@tonic-gate 	ASSERT(flags & SQ_EXCL);
5987*7c478bd9Sstevel@tonic-gate 	ASSERT(sq->sq_count == 1);
5988*7c478bd9Sstevel@tonic-gate 
5989*7c478bd9Sstevel@tonic-gate 	/*
5990*7c478bd9Sstevel@tonic-gate 	 * We need to process all of the events on this list.  It
5991*7c478bd9Sstevel@tonic-gate 	 * is possible that new events will be added while we are
5992*7c478bd9Sstevel@tonic-gate 	 * away processing a callback, so on every loop, we start
5993*7c478bd9Sstevel@tonic-gate 	 * back at the beginning of the list.
5994*7c478bd9Sstevel@tonic-gate 	 */
5995*7c478bd9Sstevel@tonic-gate 	/*
5996*7c478bd9Sstevel@tonic-gate 	 * We have to reaccess sq_evhead since there is a
5997*7c478bd9Sstevel@tonic-gate 	 * possibility of a new entry while we were running
5998*7c478bd9Sstevel@tonic-gate 	 * the callback.
5999*7c478bd9Sstevel@tonic-gate 	 */
6000*7c478bd9Sstevel@tonic-gate 	for (bp = sq->sq_evhead; bp != NULL; bp = sq->sq_evhead) {
6001*7c478bd9Sstevel@tonic-gate 		ASSERT(bp->b_queue->q_syncq == sq);
6002*7c478bd9Sstevel@tonic-gate 		ASSERT(sq->sq_flags & SQ_EVENTS);
6003*7c478bd9Sstevel@tonic-gate 
6004*7c478bd9Sstevel@tonic-gate 		qp = bp->b_queue;
6005*7c478bd9Sstevel@tonic-gate 		func = (void (*)())bp->b_prev;
6006*7c478bd9Sstevel@tonic-gate 		ASSERT(func != NULL);
6007*7c478bd9Sstevel@tonic-gate 
6008*7c478bd9Sstevel@tonic-gate 		/*
6009*7c478bd9Sstevel@tonic-gate 		 * Messages from the event queue must be taken off in
6010*7c478bd9Sstevel@tonic-gate 		 * FIFO order.
6011*7c478bd9Sstevel@tonic-gate 		 */
6012*7c478bd9Sstevel@tonic-gate 		ASSERT(sq->sq_evhead == bp);
6013*7c478bd9Sstevel@tonic-gate 		sq->sq_evhead = bp->b_next;
6014*7c478bd9Sstevel@tonic-gate 
6015*7c478bd9Sstevel@tonic-gate 		if (bp->b_next == NULL) {
6016*7c478bd9Sstevel@tonic-gate 			/* Deleting last */
6017*7c478bd9Sstevel@tonic-gate 			ASSERT(sq->sq_evtail == bp);
6018*7c478bd9Sstevel@tonic-gate 			sq->sq_evtail = NULL;
6019*7c478bd9Sstevel@tonic-gate 			sq->sq_flags &= ~SQ_EVENTS;
6020*7c478bd9Sstevel@tonic-gate 		}
6021*7c478bd9Sstevel@tonic-gate 		bp->b_prev = bp->b_next = NULL;
6022*7c478bd9Sstevel@tonic-gate 		ASSERT(bp->b_datap->db_ref != 0);
6023*7c478bd9Sstevel@tonic-gate 
6024*7c478bd9Sstevel@tonic-gate 		mutex_exit(SQLOCK(sq));
6025*7c478bd9Sstevel@tonic-gate 
6026*7c478bd9Sstevel@tonic-gate 		(*func)(qp, bp);
6027*7c478bd9Sstevel@tonic-gate 
6028*7c478bd9Sstevel@tonic-gate 		mutex_enter(SQLOCK(sq));
6029*7c478bd9Sstevel@tonic-gate 		/*
6030*7c478bd9Sstevel@tonic-gate 		 * re-read the flags, since they could have changed.
6031*7c478bd9Sstevel@tonic-gate 		 */
6032*7c478bd9Sstevel@tonic-gate 		flags = sq->sq_flags;
6033*7c478bd9Sstevel@tonic-gate 		ASSERT(flags & SQ_EXCL);
6034*7c478bd9Sstevel@tonic-gate 	}
6035*7c478bd9Sstevel@tonic-gate 	ASSERT(sq->sq_evhead == NULL && sq->sq_evtail == NULL);
6036*7c478bd9Sstevel@tonic-gate 	ASSERT(!(sq->sq_flags & SQ_EVENTS));
6037*7c478bd9Sstevel@tonic-gate 
6038*7c478bd9Sstevel@tonic-gate 	if (flags & SQ_WANTWAKEUP) {
6039*7c478bd9Sstevel@tonic-gate 		flags &= ~SQ_WANTWAKEUP;
6040*7c478bd9Sstevel@tonic-gate 		cv_broadcast(&sq->sq_wait);
6041*7c478bd9Sstevel@tonic-gate 	}
6042*7c478bd9Sstevel@tonic-gate 	if (flags & SQ_WANTEXWAKEUP) {
6043*7c478bd9Sstevel@tonic-gate 		flags &= ~SQ_WANTEXWAKEUP;
6044*7c478bd9Sstevel@tonic-gate 		cv_broadcast(&sq->sq_exitwait);
6045*7c478bd9Sstevel@tonic-gate 	}
6046*7c478bd9Sstevel@tonic-gate 	sq->sq_flags = flags;
6047*7c478bd9Sstevel@tonic-gate }
6048*7c478bd9Sstevel@tonic-gate 
6049*7c478bd9Sstevel@tonic-gate /*
6050*7c478bd9Sstevel@tonic-gate  * Put messages on the event list.
6051*7c478bd9Sstevel@tonic-gate  * If we can go exclusive now, do so and process the event list, otherwise
6052*7c478bd9Sstevel@tonic-gate  * let the last claim service this list (or wake the sqthread).
6053*7c478bd9Sstevel@tonic-gate  * This procedure assumes SQLOCK is held.  To run the event list, it
6054*7c478bd9Sstevel@tonic-gate  * must be called with no claims.
6055*7c478bd9Sstevel@tonic-gate  */
6056*7c478bd9Sstevel@tonic-gate static void
6057*7c478bd9Sstevel@tonic-gate sqfill_events(syncq_t *sq, queue_t *q, mblk_t *mp, void (*func)())
6058*7c478bd9Sstevel@tonic-gate {
6059*7c478bd9Sstevel@tonic-gate 	uint16_t count;
6060*7c478bd9Sstevel@tonic-gate 
6061*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(SQLOCK(sq)));
6062*7c478bd9Sstevel@tonic-gate 	ASSERT(func != NULL);
6063*7c478bd9Sstevel@tonic-gate 
6064*7c478bd9Sstevel@tonic-gate 	/*
6065*7c478bd9Sstevel@tonic-gate 	 * This is a callback.  Add it to the list of callbacks
6066*7c478bd9Sstevel@tonic-gate 	 * and see about upgrading.
6067*7c478bd9Sstevel@tonic-gate 	 */
6068*7c478bd9Sstevel@tonic-gate 	mp->b_prev = (mblk_t *)func;
6069*7c478bd9Sstevel@tonic-gate 	mp->b_queue = q;
6070*7c478bd9Sstevel@tonic-gate 	mp->b_next = NULL;
6071*7c478bd9Sstevel@tonic-gate 	if (sq->sq_evhead == NULL) {
6072*7c478bd9Sstevel@tonic-gate 		sq->sq_evhead = sq->sq_evtail = mp;
6073*7c478bd9Sstevel@tonic-gate 		sq->sq_flags |= SQ_EVENTS;
6074*7c478bd9Sstevel@tonic-gate 	} else {
6075*7c478bd9Sstevel@tonic-gate 		ASSERT(sq->sq_evtail != NULL);
6076*7c478bd9Sstevel@tonic-gate 		ASSERT(sq->sq_evtail->b_next == NULL);
6077*7c478bd9Sstevel@tonic-gate 		ASSERT(sq->sq_flags & SQ_EVENTS);
6078*7c478bd9Sstevel@tonic-gate 		sq->sq_evtail->b_next = mp;
6079*7c478bd9Sstevel@tonic-gate 		sq->sq_evtail = mp;
6080*7c478bd9Sstevel@tonic-gate 	}
6081*7c478bd9Sstevel@tonic-gate 	/*
6082*7c478bd9Sstevel@tonic-gate 	 * We have set SQ_EVENTS, so threads will have to
6083*7c478bd9Sstevel@tonic-gate 	 * unwind out of the perimiter, and new entries will
6084*7c478bd9Sstevel@tonic-gate 	 * not grab a putlock.  But we still need to know
6085*7c478bd9Sstevel@tonic-gate 	 * how many threads have already made a claim to the
6086*7c478bd9Sstevel@tonic-gate 	 * syncq, so grab the putlocks, and sum the counts.
6087*7c478bd9Sstevel@tonic-gate 	 * If there are no claims on the syncq, we can upgrade
6088*7c478bd9Sstevel@tonic-gate 	 * to exclusive, and run the event list.
6089*7c478bd9Sstevel@tonic-gate 	 * NOTE: We hold the SQLOCK, so we can just grab the
6090*7c478bd9Sstevel@tonic-gate 	 * putlocks.
6091*7c478bd9Sstevel@tonic-gate 	 */
6092*7c478bd9Sstevel@tonic-gate 	count = sq->sq_count;
6093*7c478bd9Sstevel@tonic-gate 	SQ_PUTLOCKS_ENTER(sq);
6094*7c478bd9Sstevel@tonic-gate 	SUM_SQ_PUTCOUNTS(sq, count);
6095*7c478bd9Sstevel@tonic-gate 	/*
6096*7c478bd9Sstevel@tonic-gate 	 * We have no claim, so we need to check if there
6097*7c478bd9Sstevel@tonic-gate 	 * are no others, then we can upgrade.
6098*7c478bd9Sstevel@tonic-gate 	 */
6099*7c478bd9Sstevel@tonic-gate 	/*
6100*7c478bd9Sstevel@tonic-gate 	 * There are currently no claims on
6101*7c478bd9Sstevel@tonic-gate 	 * the syncq by this thread (at least on this entry). The thread who has
6102*7c478bd9Sstevel@tonic-gate 	 * the claim should drain syncq.
6103*7c478bd9Sstevel@tonic-gate 	 */
6104*7c478bd9Sstevel@tonic-gate 	if (count > 0) {
6105*7c478bd9Sstevel@tonic-gate 		/*
6106*7c478bd9Sstevel@tonic-gate 		 * Can't upgrade - other threads inside.
6107*7c478bd9Sstevel@tonic-gate 		 */
6108*7c478bd9Sstevel@tonic-gate 		SQ_PUTLOCKS_EXIT(sq);
6109*7c478bd9Sstevel@tonic-gate 		mutex_exit(SQLOCK(sq));
6110*7c478bd9Sstevel@tonic-gate 		return;
6111*7c478bd9Sstevel@tonic-gate 	}
6112*7c478bd9Sstevel@tonic-gate 	/*
6113*7c478bd9Sstevel@tonic-gate 	 * Need to set SQ_EXCL and make a claim on the syncq.
6114*7c478bd9Sstevel@tonic-gate 	 */
6115*7c478bd9Sstevel@tonic-gate 	ASSERT((sq->sq_flags & SQ_EXCL) == 0);
6116*7c478bd9Sstevel@tonic-gate 	sq->sq_flags |= SQ_EXCL;
6117*7c478bd9Sstevel@tonic-gate 	ASSERT(sq->sq_count == 0);
6118*7c478bd9Sstevel@tonic-gate 	sq->sq_count++;
6119*7c478bd9Sstevel@tonic-gate 	SQ_PUTLOCKS_EXIT(sq);
6120*7c478bd9Sstevel@tonic-gate 
6121*7c478bd9Sstevel@tonic-gate 	/* Process the events list */
6122*7c478bd9Sstevel@tonic-gate 	sq_run_events(sq);
6123*7c478bd9Sstevel@tonic-gate 
6124*7c478bd9Sstevel@tonic-gate 	/*
6125*7c478bd9Sstevel@tonic-gate 	 * Release our claim...
6126*7c478bd9Sstevel@tonic-gate 	 */
6127*7c478bd9Sstevel@tonic-gate 	sq->sq_count--;
6128*7c478bd9Sstevel@tonic-gate 
6129*7c478bd9Sstevel@tonic-gate 	/*
6130*7c478bd9Sstevel@tonic-gate 	 * And release SQ_EXCL.
6131*7c478bd9Sstevel@tonic-gate 	 * We don't need to acquire the putlocks to release
6132*7c478bd9Sstevel@tonic-gate 	 * SQ_EXCL, since we are exclusive, and hold the SQLOCK.
6133*7c478bd9Sstevel@tonic-gate 	 */
6134*7c478bd9Sstevel@tonic-gate 	sq->sq_flags &= ~SQ_EXCL;
6135*7c478bd9Sstevel@tonic-gate 
6136*7c478bd9Sstevel@tonic-gate 	/*
6137*7c478bd9Sstevel@tonic-gate 	 * sq_run_events should have released SQ_EXCL
6138*7c478bd9Sstevel@tonic-gate 	 */
6139*7c478bd9Sstevel@tonic-gate 	ASSERT(!(sq->sq_flags & SQ_EXCL));
6140*7c478bd9Sstevel@tonic-gate 
6141*7c478bd9Sstevel@tonic-gate 	/*
6142*7c478bd9Sstevel@tonic-gate 	 * If anything happened while we were running the
6143*7c478bd9Sstevel@tonic-gate 	 * events (or was there before), we need to process
6144*7c478bd9Sstevel@tonic-gate 	 * them now.  We shouldn't be exclusive sine we
6145*7c478bd9Sstevel@tonic-gate 	 * released the perimiter above (plus, we asserted
6146*7c478bd9Sstevel@tonic-gate 	 * for it).
6147*7c478bd9Sstevel@tonic-gate 	 */
6148*7c478bd9Sstevel@tonic-gate 	if (!(sq->sq_flags & SQ_STAYAWAY) && (sq->sq_flags & SQ_QUEUED))
6149*7c478bd9Sstevel@tonic-gate 		drain_syncq(sq);
6150*7c478bd9Sstevel@tonic-gate 	else
6151*7c478bd9Sstevel@tonic-gate 		mutex_exit(SQLOCK(sq));
6152*7c478bd9Sstevel@tonic-gate }
6153*7c478bd9Sstevel@tonic-gate 
6154*7c478bd9Sstevel@tonic-gate /*
6155*7c478bd9Sstevel@tonic-gate  * Perform delayed processing. The caller has to make sure that it is safe
6156*7c478bd9Sstevel@tonic-gate  * to enter the syncq (e.g. by checking that none of the SQ_STAYAWAY bits are
6157*7c478bd9Sstevel@tonic-gate  * set.)
6158*7c478bd9Sstevel@tonic-gate  *
6159*7c478bd9Sstevel@tonic-gate  * Assume that the caller has NO claims on the syncq.  However, a claim
6160*7c478bd9Sstevel@tonic-gate  * on the syncq does not indicate that a thread is draining the syncq.
6161*7c478bd9Sstevel@tonic-gate  * There may be more claims on the syncq than there are threads draining
6162*7c478bd9Sstevel@tonic-gate  * (i.e.  #_threads_draining <= sq_count)
6163*7c478bd9Sstevel@tonic-gate  *
6164*7c478bd9Sstevel@tonic-gate  * drain_syncq has to terminate when one of the SQ_STAYAWAY bits gets set
6165*7c478bd9Sstevel@tonic-gate  * in order to preserve qwriter(OUTER) ordering constraints.
6166*7c478bd9Sstevel@tonic-gate  *
6167*7c478bd9Sstevel@tonic-gate  * sq_putcount only needs to be checked when dispatching the queued
6168*7c478bd9Sstevel@tonic-gate  * writer call for CIPUT sync queue, but this is handled in sq_run_events.
6169*7c478bd9Sstevel@tonic-gate  */
6170*7c478bd9Sstevel@tonic-gate void
6171*7c478bd9Sstevel@tonic-gate drain_syncq(syncq_t *sq)
6172*7c478bd9Sstevel@tonic-gate {
6173*7c478bd9Sstevel@tonic-gate 	queue_t		*qp;
6174*7c478bd9Sstevel@tonic-gate 	uint16_t	count;
6175*7c478bd9Sstevel@tonic-gate 	uint16_t	type = sq->sq_type;
6176*7c478bd9Sstevel@tonic-gate 	uint16_t	flags = sq->sq_flags;
6177*7c478bd9Sstevel@tonic-gate 	boolean_t	bg_service = sq->sq_svcflags & SQ_SERVICE;
6178*7c478bd9Sstevel@tonic-gate 
6179*7c478bd9Sstevel@tonic-gate 	TRACE_1(TR_FAC_STREAMS_FR, TR_DRAIN_SYNCQ_START,
6180*7c478bd9Sstevel@tonic-gate 		"drain_syncq start:%p", sq);
6181*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(SQLOCK(sq)));
6182*7c478bd9Sstevel@tonic-gate 	ASSERT((sq->sq_outer == NULL && sq->sq_onext == NULL &&
6183*7c478bd9Sstevel@tonic-gate 		sq->sq_oprev == NULL) ||
6184*7c478bd9Sstevel@tonic-gate 		(sq->sq_outer != NULL && sq->sq_onext != NULL &&
6185*7c478bd9Sstevel@tonic-gate 		sq->sq_oprev != NULL));
6186*7c478bd9Sstevel@tonic-gate 
6187*7c478bd9Sstevel@tonic-gate 	/*
6188*7c478bd9Sstevel@tonic-gate 	 * Drop SQ_SERVICE flag.
6189*7c478bd9Sstevel@tonic-gate 	 */
6190*7c478bd9Sstevel@tonic-gate 	if (bg_service)
6191*7c478bd9Sstevel@tonic-gate 		sq->sq_svcflags &= ~SQ_SERVICE;
6192*7c478bd9Sstevel@tonic-gate 
6193*7c478bd9Sstevel@tonic-gate 	/*
6194*7c478bd9Sstevel@tonic-gate 	 * If SQ_EXCL is set, someone else is processing this syncq - let him
6195*7c478bd9Sstevel@tonic-gate 	 * finish the job.
6196*7c478bd9Sstevel@tonic-gate 	 */
6197*7c478bd9Sstevel@tonic-gate 	if (flags & SQ_EXCL) {
6198*7c478bd9Sstevel@tonic-gate 		if (bg_service) {
6199*7c478bd9Sstevel@tonic-gate 			ASSERT(sq->sq_servcount != 0);
6200*7c478bd9Sstevel@tonic-gate 			sq->sq_servcount--;
6201*7c478bd9Sstevel@tonic-gate 		}
6202*7c478bd9Sstevel@tonic-gate 		mutex_exit(SQLOCK(sq));
6203*7c478bd9Sstevel@tonic-gate 		return;
6204*7c478bd9Sstevel@tonic-gate 	}
6205*7c478bd9Sstevel@tonic-gate 
6206*7c478bd9Sstevel@tonic-gate 	/*
6207*7c478bd9Sstevel@tonic-gate 	 * This routine can be called by a background thread if
6208*7c478bd9Sstevel@tonic-gate 	 * it was scheduled by a hi-priority thread.  SO, if there are
6209*7c478bd9Sstevel@tonic-gate 	 * NOT messages queued, return (remember, we have the SQLOCK,
6210*7c478bd9Sstevel@tonic-gate 	 * and it cannot change until we release it). Wakeup any waiters also.
6211*7c478bd9Sstevel@tonic-gate 	 */
6212*7c478bd9Sstevel@tonic-gate 	if (!(flags & SQ_QUEUED)) {
6213*7c478bd9Sstevel@tonic-gate 		if (flags & SQ_WANTWAKEUP) {
6214*7c478bd9Sstevel@tonic-gate 			flags &= ~SQ_WANTWAKEUP;
6215*7c478bd9Sstevel@tonic-gate 			cv_broadcast(&sq->sq_wait);
6216*7c478bd9Sstevel@tonic-gate 		}
6217*7c478bd9Sstevel@tonic-gate 		if (flags & SQ_WANTEXWAKEUP) {
6218*7c478bd9Sstevel@tonic-gate 			flags &= ~SQ_WANTEXWAKEUP;
6219*7c478bd9Sstevel@tonic-gate 			cv_broadcast(&sq->sq_exitwait);
6220*7c478bd9Sstevel@tonic-gate 		}
6221*7c478bd9Sstevel@tonic-gate 		sq->sq_flags = flags;
6222*7c478bd9Sstevel@tonic-gate 		if (bg_service) {
6223*7c478bd9Sstevel@tonic-gate 			ASSERT(sq->sq_servcount != 0);
6224*7c478bd9Sstevel@tonic-gate 			sq->sq_servcount--;
6225*7c478bd9Sstevel@tonic-gate 		}
6226*7c478bd9Sstevel@tonic-gate 		mutex_exit(SQLOCK(sq));
6227*7c478bd9Sstevel@tonic-gate 		return;
6228*7c478bd9Sstevel@tonic-gate 	}
6229*7c478bd9Sstevel@tonic-gate 
6230*7c478bd9Sstevel@tonic-gate 	/*
6231*7c478bd9Sstevel@tonic-gate 	 * If this is not a concurrent put perimiter, we need to
6232*7c478bd9Sstevel@tonic-gate 	 * become exclusive to drain.  Also, if not CIPUT, we would
6233*7c478bd9Sstevel@tonic-gate 	 * not have acquired a putlock, so we don't need to check
6234*7c478bd9Sstevel@tonic-gate 	 * the putcounts.  If not entering with a claim, we test
6235*7c478bd9Sstevel@tonic-gate 	 * for sq_count == 0.
6236*7c478bd9Sstevel@tonic-gate 	 */
6237*7c478bd9Sstevel@tonic-gate 	type = sq->sq_type;
6238*7c478bd9Sstevel@tonic-gate 	if (!(type & SQ_CIPUT)) {
6239*7c478bd9Sstevel@tonic-gate 		if (sq->sq_count > 1) {
6240*7c478bd9Sstevel@tonic-gate 			if (bg_service) {
6241*7c478bd9Sstevel@tonic-gate 				ASSERT(sq->sq_servcount != 0);
6242*7c478bd9Sstevel@tonic-gate 				sq->sq_servcount--;
6243*7c478bd9Sstevel@tonic-gate 			}
6244*7c478bd9Sstevel@tonic-gate 			mutex_exit(SQLOCK(sq));
6245*7c478bd9Sstevel@tonic-gate 			return;
6246*7c478bd9Sstevel@tonic-gate 		}
6247*7c478bd9Sstevel@tonic-gate 		sq->sq_flags |= SQ_EXCL;
6248*7c478bd9Sstevel@tonic-gate 	}
6249*7c478bd9Sstevel@tonic-gate 
6250*7c478bd9Sstevel@tonic-gate 	/*
6251*7c478bd9Sstevel@tonic-gate 	 * This is where we make a claim to the syncq.
6252*7c478bd9Sstevel@tonic-gate 	 * This can either be done by incrementing a putlock, or
6253*7c478bd9Sstevel@tonic-gate 	 * the sq_count.  But since we already have the SQLOCK
6254*7c478bd9Sstevel@tonic-gate 	 * here, we just bump the sq_count.
6255*7c478bd9Sstevel@tonic-gate 	 *
6256*7c478bd9Sstevel@tonic-gate 	 * Note that after we make a claim, we need to let the code
6257*7c478bd9Sstevel@tonic-gate 	 * fall through to the end of this routine to clean itself
6258*7c478bd9Sstevel@tonic-gate 	 * up.  A return in the while loop will put the syncq in a
6259*7c478bd9Sstevel@tonic-gate 	 * very bad state.
6260*7c478bd9Sstevel@tonic-gate 	 */
6261*7c478bd9Sstevel@tonic-gate 	sq->sq_count++;
6262*7c478bd9Sstevel@tonic-gate 	ASSERT(sq->sq_count != 0);	/* wraparound */
6263*7c478bd9Sstevel@tonic-gate 
6264*7c478bd9Sstevel@tonic-gate 	while ((flags = sq->sq_flags) & SQ_QUEUED) {
6265*7c478bd9Sstevel@tonic-gate 		/*
6266*7c478bd9Sstevel@tonic-gate 		 * If we are told to stayaway or went exclusive,
6267*7c478bd9Sstevel@tonic-gate 		 * we are done.
6268*7c478bd9Sstevel@tonic-gate 		 */
6269*7c478bd9Sstevel@tonic-gate 		if (flags & (SQ_STAYAWAY)) {
6270*7c478bd9Sstevel@tonic-gate 			break;
6271*7c478bd9Sstevel@tonic-gate 		}
6272*7c478bd9Sstevel@tonic-gate 
6273*7c478bd9Sstevel@tonic-gate 		/*
6274*7c478bd9Sstevel@tonic-gate 		 * If there are events to run, do so.
6275*7c478bd9Sstevel@tonic-gate 		 * We have one claim to the syncq, so if there are
6276*7c478bd9Sstevel@tonic-gate 		 * more than one, other threads are running.
6277*7c478bd9Sstevel@tonic-gate 		 */
6278*7c478bd9Sstevel@tonic-gate 		if (sq->sq_evhead != NULL) {
6279*7c478bd9Sstevel@tonic-gate 			ASSERT(sq->sq_flags & SQ_EVENTS);
6280*7c478bd9Sstevel@tonic-gate 
6281*7c478bd9Sstevel@tonic-gate 			count = sq->sq_count;
6282*7c478bd9Sstevel@tonic-gate 			SQ_PUTLOCKS_ENTER(sq);
6283*7c478bd9Sstevel@tonic-gate 			SUM_SQ_PUTCOUNTS(sq, count);
6284*7c478bd9Sstevel@tonic-gate 			if (count > 1) {
6285*7c478bd9Sstevel@tonic-gate 				SQ_PUTLOCKS_EXIT(sq);
6286*7c478bd9Sstevel@tonic-gate 				/* Can't upgrade - other threads inside */
6287*7c478bd9Sstevel@tonic-gate 				break;
6288*7c478bd9Sstevel@tonic-gate 			}
6289*7c478bd9Sstevel@tonic-gate 			ASSERT((flags & SQ_EXCL) == 0);
6290*7c478bd9Sstevel@tonic-gate 			sq->sq_flags = flags | SQ_EXCL;
6291*7c478bd9Sstevel@tonic-gate 			SQ_PUTLOCKS_EXIT(sq);
6292*7c478bd9Sstevel@tonic-gate 			/*
6293*7c478bd9Sstevel@tonic-gate 			 * we have the only claim, run the events,
6294*7c478bd9Sstevel@tonic-gate 			 * sq_run_events will clear the SQ_EXCL flag.
6295*7c478bd9Sstevel@tonic-gate 			 */
6296*7c478bd9Sstevel@tonic-gate 			sq_run_events(sq);
6297*7c478bd9Sstevel@tonic-gate 
6298*7c478bd9Sstevel@tonic-gate 			/*
6299*7c478bd9Sstevel@tonic-gate 			 * If this is a CIPUT perimiter, we need
6300*7c478bd9Sstevel@tonic-gate 			 * to drop the SQ_EXCL flag so we can properly
6301*7c478bd9Sstevel@tonic-gate 			 * continue draining the syncq.
6302*7c478bd9Sstevel@tonic-gate 			 */
6303*7c478bd9Sstevel@tonic-gate 			if (type & SQ_CIPUT) {
6304*7c478bd9Sstevel@tonic-gate 				ASSERT(sq->sq_flags & SQ_EXCL);
6305*7c478bd9Sstevel@tonic-gate 				sq->sq_flags &= ~SQ_EXCL;
6306*7c478bd9Sstevel@tonic-gate 			}
6307*7c478bd9Sstevel@tonic-gate 
6308*7c478bd9Sstevel@tonic-gate 			/*
6309*7c478bd9Sstevel@tonic-gate 			 * And go back to the beginning just in case
6310*7c478bd9Sstevel@tonic-gate 			 * anything changed while we were away.
6311*7c478bd9Sstevel@tonic-gate 			 */
6312*7c478bd9Sstevel@tonic-gate 			ASSERT((sq->sq_flags & SQ_EXCL) || (type & SQ_CIPUT));
6313*7c478bd9Sstevel@tonic-gate 			continue;
6314*7c478bd9Sstevel@tonic-gate 		}
6315*7c478bd9Sstevel@tonic-gate 
6316*7c478bd9Sstevel@tonic-gate 		ASSERT(sq->sq_evhead == NULL);
6317*7c478bd9Sstevel@tonic-gate 		ASSERT(!(sq->sq_flags & SQ_EVENTS));
6318*7c478bd9Sstevel@tonic-gate 
6319*7c478bd9Sstevel@tonic-gate 		/*
6320*7c478bd9Sstevel@tonic-gate 		 * Find the queue that is not draining.
6321*7c478bd9Sstevel@tonic-gate 		 *
6322*7c478bd9Sstevel@tonic-gate 		 * q_draining is protected by QLOCK which we do not hold.
6323*7c478bd9Sstevel@tonic-gate 		 * But if it was set, then a thread was draining, and if it gets
6324*7c478bd9Sstevel@tonic-gate 		 * cleared, then it was because the thread has successfully
6325*7c478bd9Sstevel@tonic-gate 		 * drained the syncq, or a GOAWAY state occured. For the GOAWAY
6326*7c478bd9Sstevel@tonic-gate 		 * state to happen, a thread needs the SQLOCK which we hold, and
6327*7c478bd9Sstevel@tonic-gate 		 * if there was such a flag, we whould have already seen it.
6328*7c478bd9Sstevel@tonic-gate 		 */
6329*7c478bd9Sstevel@tonic-gate 
6330*7c478bd9Sstevel@tonic-gate 		for (qp = sq->sq_head;
6331*7c478bd9Sstevel@tonic-gate 		    qp != NULL && (qp->q_draining ||
6332*7c478bd9Sstevel@tonic-gate 			(qp->q_sqflags & Q_SQDRAINING));
6333*7c478bd9Sstevel@tonic-gate 		    qp = qp->q_sqnext)
6334*7c478bd9Sstevel@tonic-gate 			;
6335*7c478bd9Sstevel@tonic-gate 
6336*7c478bd9Sstevel@tonic-gate 		if (qp == NULL)
6337*7c478bd9Sstevel@tonic-gate 			break;
6338*7c478bd9Sstevel@tonic-gate 
6339*7c478bd9Sstevel@tonic-gate 		/*
6340*7c478bd9Sstevel@tonic-gate 		 * We have a queue to work on, and we hold the
6341*7c478bd9Sstevel@tonic-gate 		 * SQLOCK and one claim, call qdrain_syncq.
6342*7c478bd9Sstevel@tonic-gate 		 * This means we need to release the SQLOCK and
6343*7c478bd9Sstevel@tonic-gate 		 * aquire the QLOCK (OK since we have a claim).
6344*7c478bd9Sstevel@tonic-gate 		 * Note that qdrain_syncq will actually dequeue
6345*7c478bd9Sstevel@tonic-gate 		 * this queue from the sq_head list when it is
6346*7c478bd9Sstevel@tonic-gate 		 * convinced all the work is done and release
6347*7c478bd9Sstevel@tonic-gate 		 * the QLOCK before returning.
6348*7c478bd9Sstevel@tonic-gate 		 */
6349*7c478bd9Sstevel@tonic-gate 		qp->q_sqflags |= Q_SQDRAINING;
6350*7c478bd9Sstevel@tonic-gate 		mutex_exit(SQLOCK(sq));
6351*7c478bd9Sstevel@tonic-gate 		mutex_enter(QLOCK(qp));
6352*7c478bd9Sstevel@tonic-gate 		qdrain_syncq(sq, qp);
6353*7c478bd9Sstevel@tonic-gate 		mutex_enter(SQLOCK(sq));
6354*7c478bd9Sstevel@tonic-gate 
6355*7c478bd9Sstevel@tonic-gate 		/* The queue is drained */
6356*7c478bd9Sstevel@tonic-gate 		ASSERT(qp->q_sqflags & Q_SQDRAINING);
6357*7c478bd9Sstevel@tonic-gate 		qp->q_sqflags &= ~Q_SQDRAINING;
6358*7c478bd9Sstevel@tonic-gate 		/*
6359*7c478bd9Sstevel@tonic-gate 		 * NOTE: After this point qp should not be used since it may be
6360*7c478bd9Sstevel@tonic-gate 		 * closed.
6361*7c478bd9Sstevel@tonic-gate 		 */
6362*7c478bd9Sstevel@tonic-gate 	}
6363*7c478bd9Sstevel@tonic-gate 
6364*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(SQLOCK(sq)));
6365*7c478bd9Sstevel@tonic-gate 	flags = sq->sq_flags;
6366*7c478bd9Sstevel@tonic-gate 
6367*7c478bd9Sstevel@tonic-gate 	/*
6368*7c478bd9Sstevel@tonic-gate 	 * sq->sq_head cannot change because we hold the
6369*7c478bd9Sstevel@tonic-gate 	 * sqlock. However, a thread CAN decide that it is no longer
6370*7c478bd9Sstevel@tonic-gate 	 * going to drain that queue.  However, this should be due to
6371*7c478bd9Sstevel@tonic-gate 	 * a GOAWAY state, and we should see that here.
6372*7c478bd9Sstevel@tonic-gate 	 *
6373*7c478bd9Sstevel@tonic-gate 	 * This loop is not very efficient. One solution may be adding a second
6374*7c478bd9Sstevel@tonic-gate 	 * pointer to the "draining" queue, but it is difficult to do when
6375*7c478bd9Sstevel@tonic-gate 	 * queues are inserted in the middle due to priority ordering. Another
6376*7c478bd9Sstevel@tonic-gate 	 * possibility is to yank the queue out of the sq list and put it onto
6377*7c478bd9Sstevel@tonic-gate 	 * the "draining list" and then put it back if it can't be drained.
6378*7c478bd9Sstevel@tonic-gate 	 */
6379*7c478bd9Sstevel@tonic-gate 
6380*7c478bd9Sstevel@tonic-gate 	ASSERT((sq->sq_head == NULL) || (flags & SQ_GOAWAY) ||
6381*7c478bd9Sstevel@tonic-gate 		(type & SQ_CI) || sq->sq_head->q_draining);
6382*7c478bd9Sstevel@tonic-gate 
6383*7c478bd9Sstevel@tonic-gate 	/* Drop SQ_EXCL for non-CIPUT perimiters */
6384*7c478bd9Sstevel@tonic-gate 	if (!(type & SQ_CIPUT))
6385*7c478bd9Sstevel@tonic-gate 		flags &= ~SQ_EXCL;
6386*7c478bd9Sstevel@tonic-gate 	ASSERT((flags & SQ_EXCL) == 0);
6387*7c478bd9Sstevel@tonic-gate 
6388*7c478bd9Sstevel@tonic-gate 	/* Wake up any waiters. */
6389*7c478bd9Sstevel@tonic-gate 	if (flags & SQ_WANTWAKEUP) {
6390*7c478bd9Sstevel@tonic-gate 		flags &= ~SQ_WANTWAKEUP;
6391*7c478bd9Sstevel@tonic-gate 		cv_broadcast(&sq->sq_wait);
6392*7c478bd9Sstevel@tonic-gate 	}
6393*7c478bd9Sstevel@tonic-gate 	if (flags & SQ_WANTEXWAKEUP) {
6394*7c478bd9Sstevel@tonic-gate 		flags &= ~SQ_WANTEXWAKEUP;
6395*7c478bd9Sstevel@tonic-gate 		cv_broadcast(&sq->sq_exitwait);
6396*7c478bd9Sstevel@tonic-gate 	}
6397*7c478bd9Sstevel@tonic-gate 	sq->sq_flags = flags;
6398*7c478bd9Sstevel@tonic-gate 
6399*7c478bd9Sstevel@tonic-gate 	ASSERT(sq->sq_count != 0);
6400*7c478bd9Sstevel@tonic-gate 	/* Release our claim. */
6401*7c478bd9Sstevel@tonic-gate 	sq->sq_count--;
6402*7c478bd9Sstevel@tonic-gate 
6403*7c478bd9Sstevel@tonic-gate 	if (bg_service) {
6404*7c478bd9Sstevel@tonic-gate 		ASSERT(sq->sq_servcount != 0);
6405*7c478bd9Sstevel@tonic-gate 		sq->sq_servcount--;
6406*7c478bd9Sstevel@tonic-gate 	}
6407*7c478bd9Sstevel@tonic-gate 
6408*7c478bd9Sstevel@tonic-gate 	mutex_exit(SQLOCK(sq));
6409*7c478bd9Sstevel@tonic-gate 
6410*7c478bd9Sstevel@tonic-gate 	TRACE_1(TR_FAC_STREAMS_FR, TR_DRAIN_SYNCQ_END,
6411*7c478bd9Sstevel@tonic-gate 		"drain_syncq end:%p", sq);
6412*7c478bd9Sstevel@tonic-gate }
6413*7c478bd9Sstevel@tonic-gate 
6414*7c478bd9Sstevel@tonic-gate 
6415*7c478bd9Sstevel@tonic-gate /*
6416*7c478bd9Sstevel@tonic-gate  *
6417*7c478bd9Sstevel@tonic-gate  * qdrain_syncq can be called (currently) from only one of two places:
6418*7c478bd9Sstevel@tonic-gate  *	drain_syncq
6419*7c478bd9Sstevel@tonic-gate  * 	putnext  (or some variation of it).
6420*7c478bd9Sstevel@tonic-gate  * and eventually
6421*7c478bd9Sstevel@tonic-gate  * 	qwait(_sig)
6422*7c478bd9Sstevel@tonic-gate  *
6423*7c478bd9Sstevel@tonic-gate  * If called from drain_syncq, we found it in the list
6424*7c478bd9Sstevel@tonic-gate  * of queue's needing service, so there is work to be done (or it
6425*7c478bd9Sstevel@tonic-gate  * wouldn't be on the list).
6426*7c478bd9Sstevel@tonic-gate  *
6427*7c478bd9Sstevel@tonic-gate  * If called from some putnext variation, it was because the
6428*7c478bd9Sstevel@tonic-gate  * perimiter is open, but messages are blocking a putnext and
6429*7c478bd9Sstevel@tonic-gate  * there is not a thread working on it.  Now a thread could start
6430*7c478bd9Sstevel@tonic-gate  * working on it while we are getting ready to do so ourself, but
6431*7c478bd9Sstevel@tonic-gate  * the thread would set the q_draining flag, and we can spin out.
6432*7c478bd9Sstevel@tonic-gate  *
6433*7c478bd9Sstevel@tonic-gate  * As for qwait(_sig), I think I shall let it continue to call
6434*7c478bd9Sstevel@tonic-gate  * drain_syncq directly (after all, it will get here eventually).
6435*7c478bd9Sstevel@tonic-gate  *
6436*7c478bd9Sstevel@tonic-gate  * qdrain_syncq has to terminate when:
6437*7c478bd9Sstevel@tonic-gate  * - one of the SQ_STAYAWAY bits gets set to preserve qwriter(OUTER) ordering
6438*7c478bd9Sstevel@tonic-gate  * - SQ_EVENTS gets set to preserve qwriter(INNER) ordering
6439*7c478bd9Sstevel@tonic-gate  *
6440*7c478bd9Sstevel@tonic-gate  * ASSUMES:
6441*7c478bd9Sstevel@tonic-gate  *	One claim
6442*7c478bd9Sstevel@tonic-gate  * 	QLOCK held
6443*7c478bd9Sstevel@tonic-gate  * 	SQLOCK not held
6444*7c478bd9Sstevel@tonic-gate  *	Will release QLOCK before returning
6445*7c478bd9Sstevel@tonic-gate  */
6446*7c478bd9Sstevel@tonic-gate void
6447*7c478bd9Sstevel@tonic-gate qdrain_syncq(syncq_t *sq, queue_t *q)
6448*7c478bd9Sstevel@tonic-gate {
6449*7c478bd9Sstevel@tonic-gate 	mblk_t		*bp;
6450*7c478bd9Sstevel@tonic-gate 	boolean_t	do_clr;
6451*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
6452*7c478bd9Sstevel@tonic-gate 	uint16_t	count;
6453*7c478bd9Sstevel@tonic-gate #endif
6454*7c478bd9Sstevel@tonic-gate 
6455*7c478bd9Sstevel@tonic-gate 	TRACE_1(TR_FAC_STREAMS_FR, TR_DRAIN_SYNCQ_START,
6456*7c478bd9Sstevel@tonic-gate 		"drain_syncq start:%p", sq);
6457*7c478bd9Sstevel@tonic-gate 	ASSERT(q->q_syncq == sq);
6458*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(QLOCK(q)));
6459*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(SQLOCK(sq)));
6460*7c478bd9Sstevel@tonic-gate 	/*
6461*7c478bd9Sstevel@tonic-gate 	 * For non-CIPUT perimiters, we should be called with the
6462*7c478bd9Sstevel@tonic-gate 	 * exclusive bit set already.  For non-CIPUT perimiters we
6463*7c478bd9Sstevel@tonic-gate 	 * will be doing a concurrent drain, so it better not be set.
6464*7c478bd9Sstevel@tonic-gate 	 */
6465*7c478bd9Sstevel@tonic-gate 	ASSERT((sq->sq_flags & (SQ_EXCL|SQ_CIPUT)));
6466*7c478bd9Sstevel@tonic-gate 	ASSERT(!((sq->sq_type & SQ_CIPUT) && (sq->sq_flags & SQ_EXCL)));
6467*7c478bd9Sstevel@tonic-gate 	ASSERT((sq->sq_type & SQ_CIPUT) || (sq->sq_flags & SQ_EXCL));
6468*7c478bd9Sstevel@tonic-gate 	/*
6469*7c478bd9Sstevel@tonic-gate 	 * All outer pointers are set, or none of them are
6470*7c478bd9Sstevel@tonic-gate 	 */
6471*7c478bd9Sstevel@tonic-gate 	ASSERT((sq->sq_outer == NULL && sq->sq_onext == NULL &&
6472*7c478bd9Sstevel@tonic-gate 		sq->sq_oprev == NULL) ||
6473*7c478bd9Sstevel@tonic-gate 		(sq->sq_outer != NULL && sq->sq_onext != NULL &&
6474*7c478bd9Sstevel@tonic-gate 		sq->sq_oprev != NULL));
6475*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
6476*7c478bd9Sstevel@tonic-gate 	count = sq->sq_count;
6477*7c478bd9Sstevel@tonic-gate 	/*
6478*7c478bd9Sstevel@tonic-gate 	 * This is OK without the putlocks, because we have one
6479*7c478bd9Sstevel@tonic-gate 	 * claim either from the sq_count, or a putcount.  We could
6480*7c478bd9Sstevel@tonic-gate 	 * get an erroneous value from other counts, but ours won't
6481*7c478bd9Sstevel@tonic-gate 	 * change, so one way or another, we will have at least a
6482*7c478bd9Sstevel@tonic-gate 	 * value of one.
6483*7c478bd9Sstevel@tonic-gate 	 */
6484*7c478bd9Sstevel@tonic-gate 	SUM_SQ_PUTCOUNTS(sq, count);
6485*7c478bd9Sstevel@tonic-gate 	ASSERT(count >= 1);
6486*7c478bd9Sstevel@tonic-gate #endif /* DEBUG */
6487*7c478bd9Sstevel@tonic-gate 
6488*7c478bd9Sstevel@tonic-gate 	/*
6489*7c478bd9Sstevel@tonic-gate 	 * The first thing to do here, is find out if a thread is already
6490*7c478bd9Sstevel@tonic-gate 	 * draining this queue or the queue is closing. If so, we are done,
6491*7c478bd9Sstevel@tonic-gate 	 * just return. Also, if there are no messages, we are done as well.
6492*7c478bd9Sstevel@tonic-gate 	 * Note that we check the q_sqhead since there is s window of
6493*7c478bd9Sstevel@tonic-gate 	 * opportunity for us to enter here because Q_SQQUEUED was set, but is
6494*7c478bd9Sstevel@tonic-gate 	 * not anymore.
6495*7c478bd9Sstevel@tonic-gate 	 */
6496*7c478bd9Sstevel@tonic-gate 	if (q->q_draining || (q->q_sqhead == NULL)) {
6497*7c478bd9Sstevel@tonic-gate 		mutex_exit(QLOCK(q));
6498*7c478bd9Sstevel@tonic-gate 		return;
6499*7c478bd9Sstevel@tonic-gate 	}
6500*7c478bd9Sstevel@tonic-gate 
6501*7c478bd9Sstevel@tonic-gate 	/*
6502*7c478bd9Sstevel@tonic-gate 	 * If the perimiter is exclusive, there is nothing we can
6503*7c478bd9Sstevel@tonic-gate 	 * do right now, go away.
6504*7c478bd9Sstevel@tonic-gate 	 * Note that there is nothing to prevent this case from changing
6505*7c478bd9Sstevel@tonic-gate 	 * right after this check, but the spin-out will catch it.
6506*7c478bd9Sstevel@tonic-gate 	 */
6507*7c478bd9Sstevel@tonic-gate 
6508*7c478bd9Sstevel@tonic-gate 	/* Tell other threads that we are draining this queue */
6509*7c478bd9Sstevel@tonic-gate 	q->q_draining = 1;	/* Protected by QLOCK */
6510*7c478bd9Sstevel@tonic-gate 
6511*7c478bd9Sstevel@tonic-gate 	for (bp = q->q_sqhead; bp != NULL; bp = q->q_sqhead) {
6512*7c478bd9Sstevel@tonic-gate 
6513*7c478bd9Sstevel@tonic-gate 		/*
6514*7c478bd9Sstevel@tonic-gate 		 * Because we can enter this routine just because
6515*7c478bd9Sstevel@tonic-gate 		 * a putnext is blocked, we need to spin out if
6516*7c478bd9Sstevel@tonic-gate 		 * the perimiter wants to go exclusive as well
6517*7c478bd9Sstevel@tonic-gate 		 * as just blocked. We need to spin out also if
6518*7c478bd9Sstevel@tonic-gate 		 * events are queued on the syncq.
6519*7c478bd9Sstevel@tonic-gate 		 * Don't check for SQ_EXCL, because non-CIPUT
6520*7c478bd9Sstevel@tonic-gate 		 * perimiters would set it, and it can't become
6521*7c478bd9Sstevel@tonic-gate 		 * exclusive while we hold a claim.
6522*7c478bd9Sstevel@tonic-gate 		 */
6523*7c478bd9Sstevel@tonic-gate 		if (sq->sq_flags & (SQ_STAYAWAY | SQ_EVENTS)) {
6524*7c478bd9Sstevel@tonic-gate 			break;
6525*7c478bd9Sstevel@tonic-gate 		}
6526*7c478bd9Sstevel@tonic-gate 
6527*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
6528*7c478bd9Sstevel@tonic-gate 		/*
6529*7c478bd9Sstevel@tonic-gate 		 * Since we are in qdrain_syncq, we already know the queue,
6530*7c478bd9Sstevel@tonic-gate 		 * but for sanity, we want to check this against the qp that
6531*7c478bd9Sstevel@tonic-gate 		 * was passed in by bp->b_queue.
6532*7c478bd9Sstevel@tonic-gate 		 */
6533*7c478bd9Sstevel@tonic-gate 
6534*7c478bd9Sstevel@tonic-gate 		ASSERT(bp->b_queue == q);
6535*7c478bd9Sstevel@tonic-gate 		ASSERT(bp->b_queue->q_syncq == sq);
6536*7c478bd9Sstevel@tonic-gate 		bp->b_queue = NULL;
6537*7c478bd9Sstevel@tonic-gate 
6538*7c478bd9Sstevel@tonic-gate 		/*
6539*7c478bd9Sstevel@tonic-gate 		 * We would have the following check in the DEBUG code:
6540*7c478bd9Sstevel@tonic-gate 		 *
6541*7c478bd9Sstevel@tonic-gate 		 * if (bp->b_prev != NULL)  {
6542*7c478bd9Sstevel@tonic-gate 		 *	ASSERT(bp->b_prev == (void (*)())q->q_qinfo->qi_putp);
6543*7c478bd9Sstevel@tonic-gate 		 * }
6544*7c478bd9Sstevel@tonic-gate 		 *
6545*7c478bd9Sstevel@tonic-gate 		 * This can't be done, however, since IP modifies qinfo
6546*7c478bd9Sstevel@tonic-gate 		 * structure at run-time (switching between IPv4 qinfo and IPv6
6547*7c478bd9Sstevel@tonic-gate 		 * qinfo), invalidating the check.
6548*7c478bd9Sstevel@tonic-gate 		 * So the assignment to func is left here, but the ASSERT itself
6549*7c478bd9Sstevel@tonic-gate 		 * is removed until the whole issue is resolved.
6550*7c478bd9Sstevel@tonic-gate 		 */
6551*7c478bd9Sstevel@tonic-gate #endif
6552*7c478bd9Sstevel@tonic-gate 		ASSERT(q->q_sqhead == bp);
6553*7c478bd9Sstevel@tonic-gate 		q->q_sqhead = bp->b_next;
6554*7c478bd9Sstevel@tonic-gate 		bp->b_prev = bp->b_next = NULL;
6555*7c478bd9Sstevel@tonic-gate 		ASSERT(q->q_syncqmsgs > 0);
6556*7c478bd9Sstevel@tonic-gate 		mutex_exit(QLOCK(q));
6557*7c478bd9Sstevel@tonic-gate 
6558*7c478bd9Sstevel@tonic-gate 		ASSERT(bp->b_datap->db_ref != 0);
6559*7c478bd9Sstevel@tonic-gate 
6560*7c478bd9Sstevel@tonic-gate 		(void) (*q->q_qinfo->qi_putp)(q, bp);
6561*7c478bd9Sstevel@tonic-gate 
6562*7c478bd9Sstevel@tonic-gate 		mutex_enter(QLOCK(q));
6563*7c478bd9Sstevel@tonic-gate 		/*
6564*7c478bd9Sstevel@tonic-gate 		 * We should decrement q_syncqmsgs only after executing the
6565*7c478bd9Sstevel@tonic-gate 		 * put procedure to avoid a possible race with putnext().
6566*7c478bd9Sstevel@tonic-gate 		 * In putnext() though it sees Q_SQQUEUED is set, there is
6567*7c478bd9Sstevel@tonic-gate 		 * an optimization which allows putnext to call the put
6568*7c478bd9Sstevel@tonic-gate 		 * procedure directly if (q_syncqmsgs == 0) and thus
6569*7c478bd9Sstevel@tonic-gate 		 * a message reodering could otherwise occur.
6570*7c478bd9Sstevel@tonic-gate 		 */
6571*7c478bd9Sstevel@tonic-gate 		q->q_syncqmsgs--;
6572*7c478bd9Sstevel@tonic-gate 
6573*7c478bd9Sstevel@tonic-gate 		/*
6574*7c478bd9Sstevel@tonic-gate 		 * Clear QFULL in the next service procedure queue if
6575*7c478bd9Sstevel@tonic-gate 		 * this is the last message destined to that queue.
6576*7c478bd9Sstevel@tonic-gate 		 *
6577*7c478bd9Sstevel@tonic-gate 		 * It would make better sense to have some sort of
6578*7c478bd9Sstevel@tonic-gate 		 * tunable for the low water mark, but these symantics
6579*7c478bd9Sstevel@tonic-gate 		 * are not yet defined.  So, alas, we use a constant.
6580*7c478bd9Sstevel@tonic-gate 		 */
6581*7c478bd9Sstevel@tonic-gate 		do_clr = (q->q_syncqmsgs == 0);
6582*7c478bd9Sstevel@tonic-gate 		mutex_exit(QLOCK(q));
6583*7c478bd9Sstevel@tonic-gate 
6584*7c478bd9Sstevel@tonic-gate 		if (do_clr)
6585*7c478bd9Sstevel@tonic-gate 			clr_qfull(q);
6586*7c478bd9Sstevel@tonic-gate 
6587*7c478bd9Sstevel@tonic-gate 		mutex_enter(QLOCK(q));
6588*7c478bd9Sstevel@tonic-gate 		/*
6589*7c478bd9Sstevel@tonic-gate 		 * Always clear SQ_EXCL when CIPUT in order to handle
6590*7c478bd9Sstevel@tonic-gate 		 * qwriter(INNER).
6591*7c478bd9Sstevel@tonic-gate 		 */
6592*7c478bd9Sstevel@tonic-gate 		/*
6593*7c478bd9Sstevel@tonic-gate 		 * The putp() can call qwriter and get exclusive access
6594*7c478bd9Sstevel@tonic-gate 		 * IFF this is the only claim.  So, we need to test for
6595*7c478bd9Sstevel@tonic-gate 		 * this possibility so we can aquire the mutex and clear
6596*7c478bd9Sstevel@tonic-gate 		 * the bit.
6597*7c478bd9Sstevel@tonic-gate 		 */
6598*7c478bd9Sstevel@tonic-gate 		if ((sq->sq_type & SQ_CIPUT) && (sq->sq_flags & SQ_EXCL)) {
6599*7c478bd9Sstevel@tonic-gate 			mutex_enter(SQLOCK(sq));
6600*7c478bd9Sstevel@tonic-gate 			sq->sq_flags &= ~SQ_EXCL;
6601*7c478bd9Sstevel@tonic-gate 			mutex_exit(SQLOCK(sq));
6602*7c478bd9Sstevel@tonic-gate 		}
6603*7c478bd9Sstevel@tonic-gate 	}
6604*7c478bd9Sstevel@tonic-gate 
6605*7c478bd9Sstevel@tonic-gate 	/*
6606*7c478bd9Sstevel@tonic-gate 	 * We should either have no queues on the syncq, or we were
6607*7c478bd9Sstevel@tonic-gate 	 * told to goaway by a waiter (which we will wake up at the
6608*7c478bd9Sstevel@tonic-gate 	 * end of this function).
6609*7c478bd9Sstevel@tonic-gate 	 */
6610*7c478bd9Sstevel@tonic-gate 	ASSERT((q->q_sqhead == NULL) ||
6611*7c478bd9Sstevel@tonic-gate 	    (sq->sq_flags & (SQ_STAYAWAY | SQ_EVENTS)));
6612*7c478bd9Sstevel@tonic-gate 
6613*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(QLOCK(q)));
6614*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(SQLOCK(sq)));
6615*7c478bd9Sstevel@tonic-gate 
6616*7c478bd9Sstevel@tonic-gate 	/*
6617*7c478bd9Sstevel@tonic-gate 	 * Remove the q from the syncq list if all the messages are
6618*7c478bd9Sstevel@tonic-gate 	 * drained.
6619*7c478bd9Sstevel@tonic-gate 	 */
6620*7c478bd9Sstevel@tonic-gate 	if (q->q_sqhead == NULL) {
6621*7c478bd9Sstevel@tonic-gate 		mutex_enter(SQLOCK(sq));
6622*7c478bd9Sstevel@tonic-gate 		if (q->q_sqflags & Q_SQQUEUED)
6623*7c478bd9Sstevel@tonic-gate 			SQRM_Q(sq, q);
6624*7c478bd9Sstevel@tonic-gate 		mutex_exit(SQLOCK(sq));
6625*7c478bd9Sstevel@tonic-gate 		/*
6626*7c478bd9Sstevel@tonic-gate 		 * Since the queue is removed from the list, reset its priority.
6627*7c478bd9Sstevel@tonic-gate 		 */
6628*7c478bd9Sstevel@tonic-gate 		q->q_spri = 0;
6629*7c478bd9Sstevel@tonic-gate 	}
6630*7c478bd9Sstevel@tonic-gate 
6631*7c478bd9Sstevel@tonic-gate 	/*
6632*7c478bd9Sstevel@tonic-gate 	 * Remember, the q_draining flag is used to let another
6633*7c478bd9Sstevel@tonic-gate 	 * thread know that there is a thread currently draining
6634*7c478bd9Sstevel@tonic-gate 	 * the messages for a queue.  Since we are now done with
6635*7c478bd9Sstevel@tonic-gate 	 * this queue (even if there may be messages still there),
6636*7c478bd9Sstevel@tonic-gate 	 * we need to clear this flag so some thread will work
6637*7c478bd9Sstevel@tonic-gate 	 * on it if needed.
6638*7c478bd9Sstevel@tonic-gate 	 */
6639*7c478bd9Sstevel@tonic-gate 	ASSERT(q->q_draining);
6640*7c478bd9Sstevel@tonic-gate 	q->q_draining = 0;
6641*7c478bd9Sstevel@tonic-gate 
6642*7c478bd9Sstevel@tonic-gate 	/* called with a claim, so OK to drop all locks. */
6643*7c478bd9Sstevel@tonic-gate 	mutex_exit(QLOCK(q));
6644*7c478bd9Sstevel@tonic-gate 
6645*7c478bd9Sstevel@tonic-gate 	TRACE_1(TR_FAC_STREAMS_FR, TR_DRAIN_SYNCQ_END,
6646*7c478bd9Sstevel@tonic-gate 		"drain_syncq end:%p", sq);
6647*7c478bd9Sstevel@tonic-gate }
6648*7c478bd9Sstevel@tonic-gate /* END OF QDRAIN_SYNCQ  */
6649*7c478bd9Sstevel@tonic-gate 
6650*7c478bd9Sstevel@tonic-gate 
6651*7c478bd9Sstevel@tonic-gate /*
6652*7c478bd9Sstevel@tonic-gate  * This is the mate to qdrain_syncq, except that it is putting the
6653*7c478bd9Sstevel@tonic-gate  * message onto the the queue instead draining.  Since the
6654*7c478bd9Sstevel@tonic-gate  * message is destined for the queue that is selected, there is
6655*7c478bd9Sstevel@tonic-gate  * no need to identify the function because the message is
6656*7c478bd9Sstevel@tonic-gate  * intended for the put routine for the queue.  But this
6657*7c478bd9Sstevel@tonic-gate  * routine will do it anyway just in case (but only for debug kernels).
6658*7c478bd9Sstevel@tonic-gate  *
6659*7c478bd9Sstevel@tonic-gate  * After the message is enqueued on the syncq, it calls putnext_tail()
6660*7c478bd9Sstevel@tonic-gate  * which will schedule a background thread to actually process the message.
6661*7c478bd9Sstevel@tonic-gate  *
6662*7c478bd9Sstevel@tonic-gate  * Assumes that there is a claim on the syncq (sq->sq_count > 0) and
6663*7c478bd9Sstevel@tonic-gate  * SQLOCK(sq) and QLOCK(q) are not held.
6664*7c478bd9Sstevel@tonic-gate  */
6665*7c478bd9Sstevel@tonic-gate void
6666*7c478bd9Sstevel@tonic-gate qfill_syncq(syncq_t *sq, queue_t *q, mblk_t *mp)
6667*7c478bd9Sstevel@tonic-gate {
6668*7c478bd9Sstevel@tonic-gate 	queue_t		*fq = NULL;
6669*7c478bd9Sstevel@tonic-gate 
6670*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(SQLOCK(sq)));
6671*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(QLOCK(q)));
6672*7c478bd9Sstevel@tonic-gate 	ASSERT(sq->sq_count > 0);
6673*7c478bd9Sstevel@tonic-gate 	ASSERT(q->q_syncq == sq);
6674*7c478bd9Sstevel@tonic-gate 	ASSERT((sq->sq_outer == NULL && sq->sq_onext == NULL &&
6675*7c478bd9Sstevel@tonic-gate 		sq->sq_oprev == NULL) ||
6676*7c478bd9Sstevel@tonic-gate 		(sq->sq_outer != NULL && sq->sq_onext != NULL &&
6677*7c478bd9Sstevel@tonic-gate 		sq->sq_oprev != NULL));
6678*7c478bd9Sstevel@tonic-gate 
6679*7c478bd9Sstevel@tonic-gate 	mutex_enter(QLOCK(q));
6680*7c478bd9Sstevel@tonic-gate 
6681*7c478bd9Sstevel@tonic-gate 	/*
6682*7c478bd9Sstevel@tonic-gate 	 * Set QFULL in next service procedure queue (that cares) if not
6683*7c478bd9Sstevel@tonic-gate 	 * already set and if there are already more messages on the syncq
6684*7c478bd9Sstevel@tonic-gate 	 * than sq_max_size.  If sq_max_size is 0, no flow control will be
6685*7c478bd9Sstevel@tonic-gate 	 * asserted on any syncq.
6686*7c478bd9Sstevel@tonic-gate 	 *
6687*7c478bd9Sstevel@tonic-gate 	 * The fq here is the next queue with a service procedure.
6688*7c478bd9Sstevel@tonic-gate 	 * This is where we would fail canputnext, so this is where we
6689*7c478bd9Sstevel@tonic-gate 	 * need to set QFULL.
6690*7c478bd9Sstevel@tonic-gate 	 *
6691*7c478bd9Sstevel@tonic-gate 	 * LOCKING HIERARCHY: In the case when fq != q we need to
6692*7c478bd9Sstevel@tonic-gate 	 *  a) Take QLOCK(fq) to set QFULL flag and
6693*7c478bd9Sstevel@tonic-gate 	 *  b) Take sd_reflock in the case of the hot stream to update
6694*7c478bd9Sstevel@tonic-gate 	 *  	sd_refcnt.
6695*7c478bd9Sstevel@tonic-gate 	 * We already have QLOCK at this point. To avoid cross-locks with
6696*7c478bd9Sstevel@tonic-gate 	 * freezestr() which grabs all QLOCKs and with strlock() which grabs
6697*7c478bd9Sstevel@tonic-gate 	 * both SQLOCK and sd_reflock, we need to drop respective locks first.
6698*7c478bd9Sstevel@tonic-gate 	 */
6699*7c478bd9Sstevel@tonic-gate 	if ((sq_max_size != 0) && (!(q->q_nfsrv->q_flag & QFULL)) &&
6700*7c478bd9Sstevel@tonic-gate 	    (q->q_syncqmsgs > sq_max_size)) {
6701*7c478bd9Sstevel@tonic-gate 		if ((fq = q->q_nfsrv) == q) {
6702*7c478bd9Sstevel@tonic-gate 			fq->q_flag |= QFULL;
6703*7c478bd9Sstevel@tonic-gate 		} else {
6704*7c478bd9Sstevel@tonic-gate 			mutex_exit(QLOCK(q));
6705*7c478bd9Sstevel@tonic-gate 			mutex_enter(QLOCK(fq));
6706*7c478bd9Sstevel@tonic-gate 			fq->q_flag |= QFULL;
6707*7c478bd9Sstevel@tonic-gate 			mutex_exit(QLOCK(fq));
6708*7c478bd9Sstevel@tonic-gate 			mutex_enter(QLOCK(q));
6709*7c478bd9Sstevel@tonic-gate 		}
6710*7c478bd9Sstevel@tonic-gate 	}
6711*7c478bd9Sstevel@tonic-gate 
6712*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
6713*7c478bd9Sstevel@tonic-gate 	/*
6714*7c478bd9Sstevel@tonic-gate 	 * This is used for debug in the qfill_syncq/qdrain_syncq case
6715*7c478bd9Sstevel@tonic-gate 	 * to trace the queue that the message is intended for.  Note
6716*7c478bd9Sstevel@tonic-gate 	 * that the original use was to identify the queue and function
6717*7c478bd9Sstevel@tonic-gate 	 * to call on the drain.  In the new syncq, we have the context
6718*7c478bd9Sstevel@tonic-gate 	 * of the queue that we are draining, so call it's putproc and
6719*7c478bd9Sstevel@tonic-gate 	 * don't rely on the saved values.  But for debug this is still
6720*7c478bd9Sstevel@tonic-gate 	 * usefull information.
6721*7c478bd9Sstevel@tonic-gate 	 */
6722*7c478bd9Sstevel@tonic-gate 	mp->b_prev = (mblk_t *)q->q_qinfo->qi_putp;
6723*7c478bd9Sstevel@tonic-gate 	mp->b_queue = q;
6724*7c478bd9Sstevel@tonic-gate 	mp->b_next = NULL;
6725*7c478bd9Sstevel@tonic-gate #endif
6726*7c478bd9Sstevel@tonic-gate 	ASSERT(q->q_syncq == sq);
6727*7c478bd9Sstevel@tonic-gate 	/*
6728*7c478bd9Sstevel@tonic-gate 	 * Enqueue the message on the list.
6729*7c478bd9Sstevel@tonic-gate 	 * SQPUT_MP() accesses q_syncqmsgs.  We are already holding QLOCK to
6730*7c478bd9Sstevel@tonic-gate 	 * protect it.  So its ok to acquire SQLOCK after SQPUT_MP().
6731*7c478bd9Sstevel@tonic-gate 	 */
6732*7c478bd9Sstevel@tonic-gate 	SQPUT_MP(q, mp);
6733*7c478bd9Sstevel@tonic-gate 	mutex_enter(SQLOCK(sq));
6734*7c478bd9Sstevel@tonic-gate 
6735*7c478bd9Sstevel@tonic-gate 	/*
6736*7c478bd9Sstevel@tonic-gate 	 * And queue on syncq for scheduling, if not already queued.
6737*7c478bd9Sstevel@tonic-gate 	 * Note that we need the SQLOCK for this, and for testing flags
6738*7c478bd9Sstevel@tonic-gate 	 * at the end to see if we will drain.  So grab it now, and
6739*7c478bd9Sstevel@tonic-gate 	 * release it before we call qdrain_syncq or return.
6740*7c478bd9Sstevel@tonic-gate 	 */
6741*7c478bd9Sstevel@tonic-gate 	if (!(q->q_sqflags & Q_SQQUEUED)) {
6742*7c478bd9Sstevel@tonic-gate 		q->q_spri = curthread->t_pri;
6743*7c478bd9Sstevel@tonic-gate 		SQPUT_Q(sq, q);
6744*7c478bd9Sstevel@tonic-gate 	}
6745*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
6746*7c478bd9Sstevel@tonic-gate 	else {
6747*7c478bd9Sstevel@tonic-gate 		/*
6748*7c478bd9Sstevel@tonic-gate 		 * All of these conditions MUST be true!
6749*7c478bd9Sstevel@tonic-gate 		 */
6750*7c478bd9Sstevel@tonic-gate 		ASSERT(sq->sq_tail != NULL);
6751*7c478bd9Sstevel@tonic-gate 		if (sq->sq_tail == sq->sq_head) {
6752*7c478bd9Sstevel@tonic-gate 			ASSERT((q->q_sqprev == NULL) &&
6753*7c478bd9Sstevel@tonic-gate 			    (q->q_sqnext == NULL));
6754*7c478bd9Sstevel@tonic-gate 		} else {
6755*7c478bd9Sstevel@tonic-gate 			ASSERT((q->q_sqprev != NULL) ||
6756*7c478bd9Sstevel@tonic-gate 			    (q->q_sqnext != NULL));
6757*7c478bd9Sstevel@tonic-gate 		}
6758*7c478bd9Sstevel@tonic-gate 		ASSERT(sq->sq_flags & SQ_QUEUED);
6759*7c478bd9Sstevel@tonic-gate 		ASSERT(q->q_syncqmsgs != 0);
6760*7c478bd9Sstevel@tonic-gate 		ASSERT(q->q_sqflags & Q_SQQUEUED);
6761*7c478bd9Sstevel@tonic-gate 	}
6762*7c478bd9Sstevel@tonic-gate #endif
6763*7c478bd9Sstevel@tonic-gate 	mutex_exit(QLOCK(q));
6764*7c478bd9Sstevel@tonic-gate 	/*
6765*7c478bd9Sstevel@tonic-gate 	 * SQLOCK is still held, so sq_count can be safely decremented.
6766*7c478bd9Sstevel@tonic-gate 	 */
6767*7c478bd9Sstevel@tonic-gate 	sq->sq_count--;
6768*7c478bd9Sstevel@tonic-gate 
6769*7c478bd9Sstevel@tonic-gate 	putnext_tail(sq, q, 0);
6770*7c478bd9Sstevel@tonic-gate 	/* Should not reference sq or q after this point. */
6771*7c478bd9Sstevel@tonic-gate }
6772*7c478bd9Sstevel@tonic-gate 
6773*7c478bd9Sstevel@tonic-gate /*  End of qfill_syncq  */
6774*7c478bd9Sstevel@tonic-gate 
6775*7c478bd9Sstevel@tonic-gate /*
6776*7c478bd9Sstevel@tonic-gate  * Remove all messages from a syncq (if qp is NULL) or remove all messages
6777*7c478bd9Sstevel@tonic-gate  * that would be put into qp by drain_syncq.
6778*7c478bd9Sstevel@tonic-gate  * Used when deleting the syncq (qp == NULL) or when detaching
6779*7c478bd9Sstevel@tonic-gate  * a queue (qp != NULL).
6780*7c478bd9Sstevel@tonic-gate  * Return non-zero if one or more messages were freed.
6781*7c478bd9Sstevel@tonic-gate  *
6782*7c478bd9Sstevel@tonic-gate  * no need to grab sq_putlocks here. See comment in strsubr.h that explains when
6783*7c478bd9Sstevel@tonic-gate  * sq_putlocks are used.
6784*7c478bd9Sstevel@tonic-gate  *
6785*7c478bd9Sstevel@tonic-gate  * NOTE: This function assumes that it is called from the close() context and
6786*7c478bd9Sstevel@tonic-gate  * that all the queues in the syncq are going aay. For this reason it doesn't
6787*7c478bd9Sstevel@tonic-gate  * acquire QLOCK for modifying q_sqhead/q_sqtail fields. This assumption is
6788*7c478bd9Sstevel@tonic-gate  * currently valid, but it is useful to rethink this function to behave properly
6789*7c478bd9Sstevel@tonic-gate  * in other cases.
6790*7c478bd9Sstevel@tonic-gate  */
6791*7c478bd9Sstevel@tonic-gate int
6792*7c478bd9Sstevel@tonic-gate flush_syncq(syncq_t *sq, queue_t *qp)
6793*7c478bd9Sstevel@tonic-gate {
6794*7c478bd9Sstevel@tonic-gate 	mblk_t		*bp, *mp_head, *mp_next, *mp_prev;
6795*7c478bd9Sstevel@tonic-gate 	queue_t		*q;
6796*7c478bd9Sstevel@tonic-gate 	int		ret = 0;
6797*7c478bd9Sstevel@tonic-gate 
6798*7c478bd9Sstevel@tonic-gate 	mutex_enter(SQLOCK(sq));
6799*7c478bd9Sstevel@tonic-gate 
6800*7c478bd9Sstevel@tonic-gate 	/*
6801*7c478bd9Sstevel@tonic-gate 	 * Before we leave, we need to make sure there are no
6802*7c478bd9Sstevel@tonic-gate 	 * events listed for this queue.  All events for this queue
6803*7c478bd9Sstevel@tonic-gate 	 * will just be freed.
6804*7c478bd9Sstevel@tonic-gate 	 */
6805*7c478bd9Sstevel@tonic-gate 	if (qp != NULL && sq->sq_evhead != NULL) {
6806*7c478bd9Sstevel@tonic-gate 		ASSERT(sq->sq_flags & SQ_EVENTS);
6807*7c478bd9Sstevel@tonic-gate 
6808*7c478bd9Sstevel@tonic-gate 		mp_prev = NULL;
6809*7c478bd9Sstevel@tonic-gate 		for (bp = sq->sq_evhead; bp != NULL; bp = mp_next) {
6810*7c478bd9Sstevel@tonic-gate 			mp_next = bp->b_next;
6811*7c478bd9Sstevel@tonic-gate 			if (bp->b_queue == qp) {
6812*7c478bd9Sstevel@tonic-gate 				/* Delete this message */
6813*7c478bd9Sstevel@tonic-gate 				if (mp_prev != NULL) {
6814*7c478bd9Sstevel@tonic-gate 					mp_prev->b_next = mp_next;
6815*7c478bd9Sstevel@tonic-gate 					/*
6816*7c478bd9Sstevel@tonic-gate 					 * Update sq_evtail if the last element
6817*7c478bd9Sstevel@tonic-gate 					 * is removed.
6818*7c478bd9Sstevel@tonic-gate 					 */
6819*7c478bd9Sstevel@tonic-gate 					if (bp == sq->sq_evtail) {
6820*7c478bd9Sstevel@tonic-gate 						ASSERT(mp_next == NULL);
6821*7c478bd9Sstevel@tonic-gate 						sq->sq_evtail = mp_prev;
6822*7c478bd9Sstevel@tonic-gate 					}
6823*7c478bd9Sstevel@tonic-gate 				} else
6824*7c478bd9Sstevel@tonic-gate 					sq->sq_evhead = mp_next;
6825*7c478bd9Sstevel@tonic-gate 				if (sq->sq_evhead == NULL)
6826*7c478bd9Sstevel@tonic-gate 					sq->sq_flags &= ~SQ_EVENTS;
6827*7c478bd9Sstevel@tonic-gate 				bp->b_prev = bp->b_next = NULL;
6828*7c478bd9Sstevel@tonic-gate 				freemsg(bp);
6829*7c478bd9Sstevel@tonic-gate 				ret++;
6830*7c478bd9Sstevel@tonic-gate 			} else {
6831*7c478bd9Sstevel@tonic-gate 				mp_prev = bp;
6832*7c478bd9Sstevel@tonic-gate 			}
6833*7c478bd9Sstevel@tonic-gate 		}
6834*7c478bd9Sstevel@tonic-gate 	}
6835*7c478bd9Sstevel@tonic-gate 
6836*7c478bd9Sstevel@tonic-gate 	/*
6837*7c478bd9Sstevel@tonic-gate 	 * Walk sq_head and:
6838*7c478bd9Sstevel@tonic-gate 	 *	- match qp if qp is set, remove it's messages
6839*7c478bd9Sstevel@tonic-gate 	 *	- all if qp is not set
6840*7c478bd9Sstevel@tonic-gate 	 */
6841*7c478bd9Sstevel@tonic-gate 	q = sq->sq_head;
6842*7c478bd9Sstevel@tonic-gate 	while (q != NULL) {
6843*7c478bd9Sstevel@tonic-gate 		ASSERT(q->q_syncq == sq);
6844*7c478bd9Sstevel@tonic-gate 		if ((qp == NULL) || (qp == q)) {
6845*7c478bd9Sstevel@tonic-gate 			/*
6846*7c478bd9Sstevel@tonic-gate 			 * Yank the messages as a list off the queue
6847*7c478bd9Sstevel@tonic-gate 			 */
6848*7c478bd9Sstevel@tonic-gate 			mp_head = q->q_sqhead;
6849*7c478bd9Sstevel@tonic-gate 			/*
6850*7c478bd9Sstevel@tonic-gate 			 * We do not have QLOCK(q) here (which is safe due to
6851*7c478bd9Sstevel@tonic-gate 			 * assumptions mentioned above). To obtain the lock we
6852*7c478bd9Sstevel@tonic-gate 			 * need to release SQLOCK which may allow lots of things
6853*7c478bd9Sstevel@tonic-gate 			 * to change upon us. This place requires more analysis.
6854*7c478bd9Sstevel@tonic-gate 			 */
6855*7c478bd9Sstevel@tonic-gate 			q->q_sqhead = q->q_sqtail = NULL;
6856*7c478bd9Sstevel@tonic-gate 			ASSERT(mp_head->b_queue &&
6857*7c478bd9Sstevel@tonic-gate 			    mp_head->b_queue->q_syncq == sq);
6858*7c478bd9Sstevel@tonic-gate 
6859*7c478bd9Sstevel@tonic-gate 			/*
6860*7c478bd9Sstevel@tonic-gate 			 * Free each of the messages.
6861*7c478bd9Sstevel@tonic-gate 			 */
6862*7c478bd9Sstevel@tonic-gate 			for (bp = mp_head; bp != NULL; bp = mp_next) {
6863*7c478bd9Sstevel@tonic-gate 				mp_next = bp->b_next;
6864*7c478bd9Sstevel@tonic-gate 				bp->b_prev = bp->b_next = NULL;
6865*7c478bd9Sstevel@tonic-gate 				freemsg(bp);
6866*7c478bd9Sstevel@tonic-gate 				ret++;
6867*7c478bd9Sstevel@tonic-gate 			}
6868*7c478bd9Sstevel@tonic-gate 			/*
6869*7c478bd9Sstevel@tonic-gate 			 * Now remove the queue from the syncq.
6870*7c478bd9Sstevel@tonic-gate 			 */
6871*7c478bd9Sstevel@tonic-gate 			ASSERT(q->q_sqflags & Q_SQQUEUED);
6872*7c478bd9Sstevel@tonic-gate 			SQRM_Q(sq, q);
6873*7c478bd9Sstevel@tonic-gate 			q->q_spri = 0;
6874*7c478bd9Sstevel@tonic-gate 			q->q_syncqmsgs = 0;
6875*7c478bd9Sstevel@tonic-gate 
6876*7c478bd9Sstevel@tonic-gate 			/*
6877*7c478bd9Sstevel@tonic-gate 			 * If qp was specified, we are done with it and are
6878*7c478bd9Sstevel@tonic-gate 			 * going to drop SQLOCK(sq) and return. We wakeup syncq
6879*7c478bd9Sstevel@tonic-gate 			 * waiters while we still have the SQLOCK.
6880*7c478bd9Sstevel@tonic-gate 			 */
6881*7c478bd9Sstevel@tonic-gate 			if ((qp != NULL) && (sq->sq_flags & SQ_WANTWAKEUP)) {
6882*7c478bd9Sstevel@tonic-gate 				sq->sq_flags &= ~SQ_WANTWAKEUP;
6883*7c478bd9Sstevel@tonic-gate 				cv_broadcast(&sq->sq_wait);
6884*7c478bd9Sstevel@tonic-gate 			}
6885*7c478bd9Sstevel@tonic-gate 			/* Drop SQLOCK across clr_qfull */
6886*7c478bd9Sstevel@tonic-gate 			mutex_exit(SQLOCK(sq));
6887*7c478bd9Sstevel@tonic-gate 
6888*7c478bd9Sstevel@tonic-gate 			/*
6889*7c478bd9Sstevel@tonic-gate 			 * We avoid doing the test that drain_syncq does and
6890*7c478bd9Sstevel@tonic-gate 			 * unconditionally clear qfull for every flushed
6891*7c478bd9Sstevel@tonic-gate 			 * message. Since flush_syncq is only called during
6892*7c478bd9Sstevel@tonic-gate 			 * close this should not be a problem.
6893*7c478bd9Sstevel@tonic-gate 			 */
6894*7c478bd9Sstevel@tonic-gate 			clr_qfull(q);
6895*7c478bd9Sstevel@tonic-gate 			if (qp != NULL) {
6896*7c478bd9Sstevel@tonic-gate 				return (ret);
6897*7c478bd9Sstevel@tonic-gate 			} else {
6898*7c478bd9Sstevel@tonic-gate 				mutex_enter(SQLOCK(sq));
6899*7c478bd9Sstevel@tonic-gate 				/*
6900*7c478bd9Sstevel@tonic-gate 				 * The head was removed by SQRM_Q above.
6901*7c478bd9Sstevel@tonic-gate 				 * reread the new head and flush it.
6902*7c478bd9Sstevel@tonic-gate 				 */
6903*7c478bd9Sstevel@tonic-gate 				q = sq->sq_head;
6904*7c478bd9Sstevel@tonic-gate 			}
6905*7c478bd9Sstevel@tonic-gate 		} else {
6906*7c478bd9Sstevel@tonic-gate 			q = q->q_sqnext;
6907*7c478bd9Sstevel@tonic-gate 		}
6908*7c478bd9Sstevel@tonic-gate 		ASSERT(MUTEX_HELD(SQLOCK(sq)));
6909*7c478bd9Sstevel@tonic-gate 	}
6910*7c478bd9Sstevel@tonic-gate 
6911*7c478bd9Sstevel@tonic-gate 	if (sq->sq_flags & SQ_WANTWAKEUP) {
6912*7c478bd9Sstevel@tonic-gate 		sq->sq_flags &= ~SQ_WANTWAKEUP;
6913*7c478bd9Sstevel@tonic-gate 		cv_broadcast(&sq->sq_wait);
6914*7c478bd9Sstevel@tonic-gate 	}
6915*7c478bd9Sstevel@tonic-gate 
6916*7c478bd9Sstevel@tonic-gate 	mutex_exit(SQLOCK(sq));
6917*7c478bd9Sstevel@tonic-gate 	return (ret);
6918*7c478bd9Sstevel@tonic-gate }
6919*7c478bd9Sstevel@tonic-gate 
6920*7c478bd9Sstevel@tonic-gate /*
6921*7c478bd9Sstevel@tonic-gate  * Propagate all messages from a syncq to the next syncq that are associated
6922*7c478bd9Sstevel@tonic-gate  * with the specified queue. If the queue is attached to a driver or if the
6923*7c478bd9Sstevel@tonic-gate  * messages have been added due to a qwriter(PERIM_INNER), free the messages.
6924*7c478bd9Sstevel@tonic-gate  *
6925*7c478bd9Sstevel@tonic-gate  * Assumes that the stream is strlock()'ed. We don't come here if there
6926*7c478bd9Sstevel@tonic-gate  * are no messages to propagate.
6927*7c478bd9Sstevel@tonic-gate  *
6928*7c478bd9Sstevel@tonic-gate  * NOTE : If the queue is attached to a driver, all the messages are freed
6929*7c478bd9Sstevel@tonic-gate  * as there is no point in propagating the messages from the driver syncq
6930*7c478bd9Sstevel@tonic-gate  * to the closing stream head which will in turn get freed later.
6931*7c478bd9Sstevel@tonic-gate  */
6932*7c478bd9Sstevel@tonic-gate static int
6933*7c478bd9Sstevel@tonic-gate propagate_syncq(queue_t *qp)
6934*7c478bd9Sstevel@tonic-gate {
6935*7c478bd9Sstevel@tonic-gate 	mblk_t		*bp, *head, *tail, *prev, *next;
6936*7c478bd9Sstevel@tonic-gate 	syncq_t 	*sq;
6937*7c478bd9Sstevel@tonic-gate 	queue_t		*nqp;
6938*7c478bd9Sstevel@tonic-gate 	syncq_t		*nsq;
6939*7c478bd9Sstevel@tonic-gate 	boolean_t	isdriver;
6940*7c478bd9Sstevel@tonic-gate 	int 		moved = 0;
6941*7c478bd9Sstevel@tonic-gate 	uint16_t	flags;
6942*7c478bd9Sstevel@tonic-gate 	pri_t		priority = curthread->t_pri;
6943*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
6944*7c478bd9Sstevel@tonic-gate 	void		(*func)();
6945*7c478bd9Sstevel@tonic-gate #endif
6946*7c478bd9Sstevel@tonic-gate 
6947*7c478bd9Sstevel@tonic-gate 	sq = qp->q_syncq;
6948*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(SQLOCK(sq)));
6949*7c478bd9Sstevel@tonic-gate 	/* debug macro */
6950*7c478bd9Sstevel@tonic-gate 	SQ_PUTLOCKS_HELD(sq);
6951*7c478bd9Sstevel@tonic-gate 	/*
6952*7c478bd9Sstevel@tonic-gate 	 * As entersq() does not increment the sq_count for
6953*7c478bd9Sstevel@tonic-gate 	 * the write side, check sq_count for non-QPERQ
6954*7c478bd9Sstevel@tonic-gate 	 * perimeters alone.
6955*7c478bd9Sstevel@tonic-gate 	 */
6956*7c478bd9Sstevel@tonic-gate 	ASSERT((qp->q_flag & QPERQ) || (sq->sq_count >= 1));
6957*7c478bd9Sstevel@tonic-gate 
6958*7c478bd9Sstevel@tonic-gate 	/*
6959*7c478bd9Sstevel@tonic-gate 	 * propagate_syncq() can be called because of either messages on the
6960*7c478bd9Sstevel@tonic-gate 	 * queue syncq or because on events on the queue syncq. Do actual
6961*7c478bd9Sstevel@tonic-gate 	 * message propagations if there are any messages.
6962*7c478bd9Sstevel@tonic-gate 	 */
6963*7c478bd9Sstevel@tonic-gate 	if (qp->q_syncqmsgs) {
6964*7c478bd9Sstevel@tonic-gate 		isdriver = (qp->q_flag & QISDRV);
6965*7c478bd9Sstevel@tonic-gate 
6966*7c478bd9Sstevel@tonic-gate 		if (!isdriver) {
6967*7c478bd9Sstevel@tonic-gate 			nqp = qp->q_next;
6968*7c478bd9Sstevel@tonic-gate 			nsq = nqp->q_syncq;
6969*7c478bd9Sstevel@tonic-gate 			ASSERT(MUTEX_HELD(SQLOCK(nsq)));
6970*7c478bd9Sstevel@tonic-gate 			/* debug macro */
6971*7c478bd9Sstevel@tonic-gate 			SQ_PUTLOCKS_HELD(nsq);
6972*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
6973*7c478bd9Sstevel@tonic-gate 			func = (void (*)())nqp->q_qinfo->qi_putp;
6974*7c478bd9Sstevel@tonic-gate #endif
6975*7c478bd9Sstevel@tonic-gate 		}
6976*7c478bd9Sstevel@tonic-gate 
6977*7c478bd9Sstevel@tonic-gate 		SQRM_Q(sq, qp);
6978*7c478bd9Sstevel@tonic-gate 		priority = MAX(qp->q_spri, priority);
6979*7c478bd9Sstevel@tonic-gate 		qp->q_spri = 0;
6980*7c478bd9Sstevel@tonic-gate 		head = qp->q_sqhead;
6981*7c478bd9Sstevel@tonic-gate 		tail = qp->q_sqtail;
6982*7c478bd9Sstevel@tonic-gate 		qp->q_sqhead = qp->q_sqtail = NULL;
6983*7c478bd9Sstevel@tonic-gate 		qp->q_syncqmsgs = 0;
6984*7c478bd9Sstevel@tonic-gate 
6985*7c478bd9Sstevel@tonic-gate 		/*
6986*7c478bd9Sstevel@tonic-gate 		 * Walk the list of messages, and free them if this is a driver,
6987*7c478bd9Sstevel@tonic-gate 		 * otherwise reset the b_prev and b_queue value to the new putp.
6988*7c478bd9Sstevel@tonic-gate 		 * Afterward, we will just add the head to the end of the next
6989*7c478bd9Sstevel@tonic-gate 		 * syncq, and point the tail to the end of this one.
6990*7c478bd9Sstevel@tonic-gate 		 */
6991*7c478bd9Sstevel@tonic-gate 
6992*7c478bd9Sstevel@tonic-gate 		for (bp = head; bp != NULL; bp = next) {
6993*7c478bd9Sstevel@tonic-gate 			next = bp->b_next;
6994*7c478bd9Sstevel@tonic-gate 			if (isdriver) {
6995*7c478bd9Sstevel@tonic-gate 				bp->b_prev = bp->b_next = NULL;
6996*7c478bd9Sstevel@tonic-gate 				freemsg(bp);
6997*7c478bd9Sstevel@tonic-gate 				continue;
6998*7c478bd9Sstevel@tonic-gate 			}
6999*7c478bd9Sstevel@tonic-gate 			/* Change the q values for this message */
7000*7c478bd9Sstevel@tonic-gate 			bp->b_queue = nqp;
7001*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
7002*7c478bd9Sstevel@tonic-gate 			bp->b_prev = (mblk_t *)func;
7003*7c478bd9Sstevel@tonic-gate #endif
7004*7c478bd9Sstevel@tonic-gate 			moved++;
7005*7c478bd9Sstevel@tonic-gate 		}
7006*7c478bd9Sstevel@tonic-gate 		/*
7007*7c478bd9Sstevel@tonic-gate 		 * Attach list of messages to the end of the new queue (if there
7008*7c478bd9Sstevel@tonic-gate 		 * is a list of messages).
7009*7c478bd9Sstevel@tonic-gate 		 */
7010*7c478bd9Sstevel@tonic-gate 
7011*7c478bd9Sstevel@tonic-gate 		if (!isdriver && head != NULL) {
7012*7c478bd9Sstevel@tonic-gate 			ASSERT(tail != NULL);
7013*7c478bd9Sstevel@tonic-gate 			if (nqp->q_sqhead == NULL) {
7014*7c478bd9Sstevel@tonic-gate 				nqp->q_sqhead = head;
7015*7c478bd9Sstevel@tonic-gate 			} else {
7016*7c478bd9Sstevel@tonic-gate 				ASSERT(nqp->q_sqtail != NULL);
7017*7c478bd9Sstevel@tonic-gate 				nqp->q_sqtail->b_next = head;
7018*7c478bd9Sstevel@tonic-gate 			}
7019*7c478bd9Sstevel@tonic-gate 			nqp->q_sqtail = tail;
7020*7c478bd9Sstevel@tonic-gate 			/*
7021*7c478bd9Sstevel@tonic-gate 			 * When messages are moved from high priority queue to
7022*7c478bd9Sstevel@tonic-gate 			 * another queue, the destination queue priority is
7023*7c478bd9Sstevel@tonic-gate 			 * upgraded.
7024*7c478bd9Sstevel@tonic-gate 			 */
7025*7c478bd9Sstevel@tonic-gate 
7026*7c478bd9Sstevel@tonic-gate 			if (priority > nqp->q_spri)
7027*7c478bd9Sstevel@tonic-gate 				nqp->q_spri = priority;
7028*7c478bd9Sstevel@tonic-gate 
7029*7c478bd9Sstevel@tonic-gate 			SQPUT_Q(nsq, nqp);
7030*7c478bd9Sstevel@tonic-gate 
7031*7c478bd9Sstevel@tonic-gate 			nqp->q_syncqmsgs += moved;
7032*7c478bd9Sstevel@tonic-gate 			ASSERT(nqp->q_syncqmsgs != 0);
7033*7c478bd9Sstevel@tonic-gate 		}
7034*7c478bd9Sstevel@tonic-gate 	}
7035*7c478bd9Sstevel@tonic-gate 
7036*7c478bd9Sstevel@tonic-gate 	/*
7037*7c478bd9Sstevel@tonic-gate 	 * Before we leave, we need to make sure there are no
7038*7c478bd9Sstevel@tonic-gate 	 * events listed for this queue.  All events for this queue
7039*7c478bd9Sstevel@tonic-gate 	 * will just be freed.
7040*7c478bd9Sstevel@tonic-gate 	 */
7041*7c478bd9Sstevel@tonic-gate 	if (sq->sq_evhead != NULL) {
7042*7c478bd9Sstevel@tonic-gate 		ASSERT(sq->sq_flags & SQ_EVENTS);
7043*7c478bd9Sstevel@tonic-gate 		prev = NULL;
7044*7c478bd9Sstevel@tonic-gate 		for (bp = sq->sq_evhead; bp != NULL; bp = next) {
7045*7c478bd9Sstevel@tonic-gate 			next = bp->b_next;
7046*7c478bd9Sstevel@tonic-gate 			if (bp->b_queue == qp) {
7047*7c478bd9Sstevel@tonic-gate 				/* Delete this message */
7048*7c478bd9Sstevel@tonic-gate 				if (prev != NULL) {
7049*7c478bd9Sstevel@tonic-gate 					prev->b_next = next;
7050*7c478bd9Sstevel@tonic-gate 					/*
7051*7c478bd9Sstevel@tonic-gate 					 * Update sq_evtail if the last element
7052*7c478bd9Sstevel@tonic-gate 					 * is removed.
7053*7c478bd9Sstevel@tonic-gate 					 */
7054*7c478bd9Sstevel@tonic-gate 					if (bp == sq->sq_evtail) {
7055*7c478bd9Sstevel@tonic-gate 						ASSERT(next == NULL);
7056*7c478bd9Sstevel@tonic-gate 						sq->sq_evtail = prev;
7057*7c478bd9Sstevel@tonic-gate 					}
7058*7c478bd9Sstevel@tonic-gate 				} else
7059*7c478bd9Sstevel@tonic-gate 					sq->sq_evhead = next;
7060*7c478bd9Sstevel@tonic-gate 				if (sq->sq_evhead == NULL)
7061*7c478bd9Sstevel@tonic-gate 					sq->sq_flags &= ~SQ_EVENTS;
7062*7c478bd9Sstevel@tonic-gate 				bp->b_prev = bp->b_next = NULL;
7063*7c478bd9Sstevel@tonic-gate 				freemsg(bp);
7064*7c478bd9Sstevel@tonic-gate 			} else {
7065*7c478bd9Sstevel@tonic-gate 				prev = bp;
7066*7c478bd9Sstevel@tonic-gate 			}
7067*7c478bd9Sstevel@tonic-gate 		}
7068*7c478bd9Sstevel@tonic-gate 	}
7069*7c478bd9Sstevel@tonic-gate 
7070*7c478bd9Sstevel@tonic-gate 	flags = sq->sq_flags;
7071*7c478bd9Sstevel@tonic-gate 
7072*7c478bd9Sstevel@tonic-gate 	/* Wake up any waiter before leaving. */
7073*7c478bd9Sstevel@tonic-gate 	if (flags & SQ_WANTWAKEUP) {
7074*7c478bd9Sstevel@tonic-gate 		flags &= ~SQ_WANTWAKEUP;
7075*7c478bd9Sstevel@tonic-gate 		cv_broadcast(&sq->sq_wait);
7076*7c478bd9Sstevel@tonic-gate 	}
7077*7c478bd9Sstevel@tonic-gate 	sq->sq_flags = flags;
7078*7c478bd9Sstevel@tonic-gate 
7079*7c478bd9Sstevel@tonic-gate 	return (moved);
7080*7c478bd9Sstevel@tonic-gate }
7081*7c478bd9Sstevel@tonic-gate 
7082*7c478bd9Sstevel@tonic-gate /*
7083*7c478bd9Sstevel@tonic-gate  * Try and upgrade to exclusive access at the inner perimeter. If this can
7084*7c478bd9Sstevel@tonic-gate  * not be done without blocking then request will be queued on the syncq
7085*7c478bd9Sstevel@tonic-gate  * and drain_syncq will run it later.
7086*7c478bd9Sstevel@tonic-gate  *
7087*7c478bd9Sstevel@tonic-gate  * This routine can only be called from put or service procedures plus
7088*7c478bd9Sstevel@tonic-gate  * asynchronous callback routines that have properly entered to
7089*7c478bd9Sstevel@tonic-gate  * queue (with entersq.) Thus qwriter_inner assumes the caller has one claim
7090*7c478bd9Sstevel@tonic-gate  * on the syncq associated with q.
7091*7c478bd9Sstevel@tonic-gate  */
7092*7c478bd9Sstevel@tonic-gate void
7093*7c478bd9Sstevel@tonic-gate qwriter_inner(queue_t *q, mblk_t *mp, void (*func)())
7094*7c478bd9Sstevel@tonic-gate {
7095*7c478bd9Sstevel@tonic-gate 	syncq_t	*sq = q->q_syncq;
7096*7c478bd9Sstevel@tonic-gate 	uint16_t count;
7097*7c478bd9Sstevel@tonic-gate 
7098*7c478bd9Sstevel@tonic-gate 	mutex_enter(SQLOCK(sq));
7099*7c478bd9Sstevel@tonic-gate 	count = sq->sq_count;
7100*7c478bd9Sstevel@tonic-gate 	SQ_PUTLOCKS_ENTER(sq);
7101*7c478bd9Sstevel@tonic-gate 	SUM_SQ_PUTCOUNTS(sq, count);
7102*7c478bd9Sstevel@tonic-gate 	ASSERT(count >= 1);
7103*7c478bd9Sstevel@tonic-gate 	ASSERT(sq->sq_type & (SQ_CIPUT|SQ_CISVC));
7104*7c478bd9Sstevel@tonic-gate 
7105*7c478bd9Sstevel@tonic-gate 	if (count == 1) {
7106*7c478bd9Sstevel@tonic-gate 		/*
7107*7c478bd9Sstevel@tonic-gate 		 * Can upgrade. This case also handles nested qwriter calls
7108*7c478bd9Sstevel@tonic-gate 		 * (when the qwriter callback function calls qwriter). In that
7109*7c478bd9Sstevel@tonic-gate 		 * case SQ_EXCL is already set.
7110*7c478bd9Sstevel@tonic-gate 		 */
7111*7c478bd9Sstevel@tonic-gate 		sq->sq_flags |= SQ_EXCL;
7112*7c478bd9Sstevel@tonic-gate 		SQ_PUTLOCKS_EXIT(sq);
7113*7c478bd9Sstevel@tonic-gate 		mutex_exit(SQLOCK(sq));
7114*7c478bd9Sstevel@tonic-gate 		(*func)(q, mp);
7115*7c478bd9Sstevel@tonic-gate 		/*
7116*7c478bd9Sstevel@tonic-gate 		 * Assumes that leavesq, putnext, and drain_syncq will reset
7117*7c478bd9Sstevel@tonic-gate 		 * SQ_EXCL for SQ_CIPUT/SQ_CISVC queues. We leave SQ_EXCL on
7118*7c478bd9Sstevel@tonic-gate 		 * until putnext, leavesq, or drain_syncq drops it.
7119*7c478bd9Sstevel@tonic-gate 		 * That way we handle nested qwriter(INNER) without dropping
7120*7c478bd9Sstevel@tonic-gate 		 * SQ_EXCL until the outermost qwriter callback routine is
7121*7c478bd9Sstevel@tonic-gate 		 * done.
7122*7c478bd9Sstevel@tonic-gate 		 */
7123*7c478bd9Sstevel@tonic-gate 		return;
7124*7c478bd9Sstevel@tonic-gate 	}
7125*7c478bd9Sstevel@tonic-gate 	SQ_PUTLOCKS_EXIT(sq);
7126*7c478bd9Sstevel@tonic-gate 	sqfill_events(sq, q, mp, func);
7127*7c478bd9Sstevel@tonic-gate }
7128*7c478bd9Sstevel@tonic-gate 
7129*7c478bd9Sstevel@tonic-gate /*
7130*7c478bd9Sstevel@tonic-gate  * Synchronous callback support functions
7131*7c478bd9Sstevel@tonic-gate  */
7132*7c478bd9Sstevel@tonic-gate 
7133*7c478bd9Sstevel@tonic-gate /*
7134*7c478bd9Sstevel@tonic-gate  * Allocate a callback parameter structure.
7135*7c478bd9Sstevel@tonic-gate  * Assumes that caller initializes the flags and the id.
7136*7c478bd9Sstevel@tonic-gate  * Acquires SQLOCK(sq) if non-NULL is returned.
7137*7c478bd9Sstevel@tonic-gate  */
7138*7c478bd9Sstevel@tonic-gate callbparams_t *
7139*7c478bd9Sstevel@tonic-gate callbparams_alloc(syncq_t *sq, void (*func)(void *), void *arg, int kmflags)
7140*7c478bd9Sstevel@tonic-gate {
7141*7c478bd9Sstevel@tonic-gate 	callbparams_t *cbp;
7142*7c478bd9Sstevel@tonic-gate 	size_t size = sizeof (callbparams_t);
7143*7c478bd9Sstevel@tonic-gate 
7144*7c478bd9Sstevel@tonic-gate 	cbp = kmem_alloc(size, kmflags & ~KM_PANIC);
7145*7c478bd9Sstevel@tonic-gate 
7146*7c478bd9Sstevel@tonic-gate 	/*
7147*7c478bd9Sstevel@tonic-gate 	 * Only try tryhard allocation if the caller is ready to panic.
7148*7c478bd9Sstevel@tonic-gate 	 * Otherwise just fail.
7149*7c478bd9Sstevel@tonic-gate 	 */
7150*7c478bd9Sstevel@tonic-gate 	if (cbp == NULL) {
7151*7c478bd9Sstevel@tonic-gate 		if (kmflags & KM_PANIC)
7152*7c478bd9Sstevel@tonic-gate 			cbp = kmem_alloc_tryhard(sizeof (callbparams_t),
7153*7c478bd9Sstevel@tonic-gate 			    &size, kmflags);
7154*7c478bd9Sstevel@tonic-gate 		else
7155*7c478bd9Sstevel@tonic-gate 			return (NULL);
7156*7c478bd9Sstevel@tonic-gate 	}
7157*7c478bd9Sstevel@tonic-gate 
7158*7c478bd9Sstevel@tonic-gate 	ASSERT(size >= sizeof (callbparams_t));
7159*7c478bd9Sstevel@tonic-gate 	cbp->cbp_size = size;
7160*7c478bd9Sstevel@tonic-gate 	cbp->cbp_sq = sq;
7161*7c478bd9Sstevel@tonic-gate 	cbp->cbp_func = func;
7162*7c478bd9Sstevel@tonic-gate 	cbp->cbp_arg = arg;
7163*7c478bd9Sstevel@tonic-gate 	mutex_enter(SQLOCK(sq));
7164*7c478bd9Sstevel@tonic-gate 	cbp->cbp_next = sq->sq_callbpend;
7165*7c478bd9Sstevel@tonic-gate 	sq->sq_callbpend = cbp;
7166*7c478bd9Sstevel@tonic-gate 	return (cbp);
7167*7c478bd9Sstevel@tonic-gate }
7168*7c478bd9Sstevel@tonic-gate 
7169*7c478bd9Sstevel@tonic-gate void
7170*7c478bd9Sstevel@tonic-gate callbparams_free(syncq_t *sq, callbparams_t *cbp)
7171*7c478bd9Sstevel@tonic-gate {
7172*7c478bd9Sstevel@tonic-gate 	callbparams_t **pp, *p;
7173*7c478bd9Sstevel@tonic-gate 
7174*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(SQLOCK(sq)));
7175*7c478bd9Sstevel@tonic-gate 
7176*7c478bd9Sstevel@tonic-gate 	for (pp = &sq->sq_callbpend; (p = *pp) != NULL; pp = &p->cbp_next) {
7177*7c478bd9Sstevel@tonic-gate 		if (p == cbp) {
7178*7c478bd9Sstevel@tonic-gate 			*pp = p->cbp_next;
7179*7c478bd9Sstevel@tonic-gate 			kmem_free(p, p->cbp_size);
7180*7c478bd9Sstevel@tonic-gate 			return;
7181*7c478bd9Sstevel@tonic-gate 		}
7182*7c478bd9Sstevel@tonic-gate 	}
7183*7c478bd9Sstevel@tonic-gate 	(void) (STRLOG(0, 0, 0, SL_CONSOLE,
7184*7c478bd9Sstevel@tonic-gate 	    "callbparams_free: not found\n"));
7185*7c478bd9Sstevel@tonic-gate }
7186*7c478bd9Sstevel@tonic-gate 
7187*7c478bd9Sstevel@tonic-gate void
7188*7c478bd9Sstevel@tonic-gate callbparams_free_id(syncq_t *sq, callbparams_id_t id, int32_t flag)
7189*7c478bd9Sstevel@tonic-gate {
7190*7c478bd9Sstevel@tonic-gate 	callbparams_t **pp, *p;
7191*7c478bd9Sstevel@tonic-gate 
7192*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(SQLOCK(sq)));
7193*7c478bd9Sstevel@tonic-gate 
7194*7c478bd9Sstevel@tonic-gate 	for (pp = &sq->sq_callbpend; (p = *pp) != NULL; pp = &p->cbp_next) {
7195*7c478bd9Sstevel@tonic-gate 		if (p->cbp_id == id && p->cbp_flags == flag) {
7196*7c478bd9Sstevel@tonic-gate 			*pp = p->cbp_next;
7197*7c478bd9Sstevel@tonic-gate 			kmem_free(p, p->cbp_size);
7198*7c478bd9Sstevel@tonic-gate 			return;
7199*7c478bd9Sstevel@tonic-gate 		}
7200*7c478bd9Sstevel@tonic-gate 	}
7201*7c478bd9Sstevel@tonic-gate 	(void) (STRLOG(0, 0, 0, SL_CONSOLE,
7202*7c478bd9Sstevel@tonic-gate 	    "callbparams_free_id: not found\n"));
7203*7c478bd9Sstevel@tonic-gate }
7204*7c478bd9Sstevel@tonic-gate 
7205*7c478bd9Sstevel@tonic-gate /*
7206*7c478bd9Sstevel@tonic-gate  * Callback wrapper function used by once-only callbacks that can be
7207*7c478bd9Sstevel@tonic-gate  * cancelled (qtimeout and qbufcall)
7208*7c478bd9Sstevel@tonic-gate  * Contains inline version of entersq(sq, SQ_CALLBACK) that can be
7209*7c478bd9Sstevel@tonic-gate  * cancelled by the qun* functions.
7210*7c478bd9Sstevel@tonic-gate  */
7211*7c478bd9Sstevel@tonic-gate void
7212*7c478bd9Sstevel@tonic-gate qcallbwrapper(void *arg)
7213*7c478bd9Sstevel@tonic-gate {
7214*7c478bd9Sstevel@tonic-gate 	callbparams_t *cbp = arg;
7215*7c478bd9Sstevel@tonic-gate 	syncq_t	*sq;
7216*7c478bd9Sstevel@tonic-gate 	uint16_t count = 0;
7217*7c478bd9Sstevel@tonic-gate 	uint16_t waitflags = SQ_STAYAWAY | SQ_EVENTS | SQ_EXCL;
7218*7c478bd9Sstevel@tonic-gate 	uint16_t type;
7219*7c478bd9Sstevel@tonic-gate 
7220*7c478bd9Sstevel@tonic-gate 	sq = cbp->cbp_sq;
7221*7c478bd9Sstevel@tonic-gate 	mutex_enter(SQLOCK(sq));
7222*7c478bd9Sstevel@tonic-gate 	type = sq->sq_type;
7223*7c478bd9Sstevel@tonic-gate 	if (!(type & SQ_CICB)) {
7224*7c478bd9Sstevel@tonic-gate 		count = sq->sq_count;
7225*7c478bd9Sstevel@tonic-gate 		SQ_PUTLOCKS_ENTER(sq);
7226*7c478bd9Sstevel@tonic-gate 		SQ_PUTCOUNT_CLRFAST_LOCKED(sq);
7227*7c478bd9Sstevel@tonic-gate 		SUM_SQ_PUTCOUNTS(sq, count);
7228*7c478bd9Sstevel@tonic-gate 		sq->sq_needexcl++;
7229*7c478bd9Sstevel@tonic-gate 		ASSERT(sq->sq_needexcl != 0);	/* wraparound */
7230*7c478bd9Sstevel@tonic-gate 		waitflags |= SQ_MESSAGES;
7231*7c478bd9Sstevel@tonic-gate 	}
7232*7c478bd9Sstevel@tonic-gate 	/* Can not handle exlusive entry at outer perimeter */
7233*7c478bd9Sstevel@tonic-gate 	ASSERT(type & SQ_COCB);
7234*7c478bd9Sstevel@tonic-gate 
7235*7c478bd9Sstevel@tonic-gate 	while ((sq->sq_flags & waitflags) || (!(type & SQ_CICB) &&count != 0)) {
7236*7c478bd9Sstevel@tonic-gate 		if ((sq->sq_callbflags & cbp->cbp_flags) &&
7237*7c478bd9Sstevel@tonic-gate 		    (sq->sq_cancelid == cbp->cbp_id)) {
7238*7c478bd9Sstevel@tonic-gate 			/* timeout has been cancelled */
7239*7c478bd9Sstevel@tonic-gate 			sq->sq_callbflags |= SQ_CALLB_BYPASSED;
7240*7c478bd9Sstevel@tonic-gate 			callbparams_free(sq, cbp);
7241*7c478bd9Sstevel@tonic-gate 			if (!(type & SQ_CICB)) {
7242*7c478bd9Sstevel@tonic-gate 				ASSERT(sq->sq_needexcl > 0);
7243*7c478bd9Sstevel@tonic-gate 				sq->sq_needexcl--;
7244*7c478bd9Sstevel@tonic-gate 				if (sq->sq_needexcl == 0) {
7245*7c478bd9Sstevel@tonic-gate 					SQ_PUTCOUNT_SETFAST_LOCKED(sq);
7246*7c478bd9Sstevel@tonic-gate 				}
7247*7c478bd9Sstevel@tonic-gate 				SQ_PUTLOCKS_EXIT(sq);
7248*7c478bd9Sstevel@tonic-gate 			}
7249*7c478bd9Sstevel@tonic-gate 			mutex_exit(SQLOCK(sq));
7250*7c478bd9Sstevel@tonic-gate 			return;
7251*7c478bd9Sstevel@tonic-gate 		}
7252*7c478bd9Sstevel@tonic-gate 		sq->sq_flags |= SQ_WANTWAKEUP;
7253*7c478bd9Sstevel@tonic-gate 		if (!(type & SQ_CICB)) {
7254*7c478bd9Sstevel@tonic-gate 			SQ_PUTLOCKS_EXIT(sq);
7255*7c478bd9Sstevel@tonic-gate 		}
7256*7c478bd9Sstevel@tonic-gate 		cv_wait(&sq->sq_wait, SQLOCK(sq));
7257*7c478bd9Sstevel@tonic-gate 		if (!(type & SQ_CICB)) {
7258*7c478bd9Sstevel@tonic-gate 			count = sq->sq_count;
7259*7c478bd9Sstevel@tonic-gate 			SQ_PUTLOCKS_ENTER(sq);
7260*7c478bd9Sstevel@tonic-gate 			SUM_SQ_PUTCOUNTS(sq, count);
7261*7c478bd9Sstevel@tonic-gate 		}
7262*7c478bd9Sstevel@tonic-gate 	}
7263*7c478bd9Sstevel@tonic-gate 
7264*7c478bd9Sstevel@tonic-gate 	sq->sq_count++;
7265*7c478bd9Sstevel@tonic-gate 	ASSERT(sq->sq_count != 0);	/* Wraparound */
7266*7c478bd9Sstevel@tonic-gate 	if (!(type & SQ_CICB)) {
7267*7c478bd9Sstevel@tonic-gate 		ASSERT(count == 0);
7268*7c478bd9Sstevel@tonic-gate 		sq->sq_flags |= SQ_EXCL;
7269*7c478bd9Sstevel@tonic-gate 		ASSERT(sq->sq_needexcl > 0);
7270*7c478bd9Sstevel@tonic-gate 		sq->sq_needexcl--;
7271*7c478bd9Sstevel@tonic-gate 		if (sq->sq_needexcl == 0) {
7272*7c478bd9Sstevel@tonic-gate 			SQ_PUTCOUNT_SETFAST_LOCKED(sq);
7273*7c478bd9Sstevel@tonic-gate 		}
7274*7c478bd9Sstevel@tonic-gate 		SQ_PUTLOCKS_EXIT(sq);
7275*7c478bd9Sstevel@tonic-gate 	}
7276*7c478bd9Sstevel@tonic-gate 
7277*7c478bd9Sstevel@tonic-gate 	mutex_exit(SQLOCK(sq));
7278*7c478bd9Sstevel@tonic-gate 
7279*7c478bd9Sstevel@tonic-gate 	cbp->cbp_func(cbp->cbp_arg);
7280*7c478bd9Sstevel@tonic-gate 
7281*7c478bd9Sstevel@tonic-gate 	/*
7282*7c478bd9Sstevel@tonic-gate 	 * We drop the lock only for leavesq to re-acquire it.
7283*7c478bd9Sstevel@tonic-gate 	 * Possible optimization is inline of leavesq.
7284*7c478bd9Sstevel@tonic-gate 	 */
7285*7c478bd9Sstevel@tonic-gate 	mutex_enter(SQLOCK(sq));
7286*7c478bd9Sstevel@tonic-gate 	callbparams_free(sq, cbp);
7287*7c478bd9Sstevel@tonic-gate 	mutex_exit(SQLOCK(sq));
7288*7c478bd9Sstevel@tonic-gate 	leavesq(sq, SQ_CALLBACK);
7289*7c478bd9Sstevel@tonic-gate }
7290*7c478bd9Sstevel@tonic-gate 
7291*7c478bd9Sstevel@tonic-gate /*
7292*7c478bd9Sstevel@tonic-gate  * no need to grab sq_putlocks here. See comment in strsubr.h that
7293*7c478bd9Sstevel@tonic-gate  * explains when sq_putlocks are used.
7294*7c478bd9Sstevel@tonic-gate  *
7295*7c478bd9Sstevel@tonic-gate  * sq_count (or one of the sq_putcounts) has already been
7296*7c478bd9Sstevel@tonic-gate  * decremented by the caller, and if SQ_QUEUED, we need to call
7297*7c478bd9Sstevel@tonic-gate  * drain_syncq (the global syncq drain).
7298*7c478bd9Sstevel@tonic-gate  * If putnext_tail is called with the SQ_EXCL bit set, we are in
7299*7c478bd9Sstevel@tonic-gate  * one of two states, non-CIPUT perimiter, and we need to clear
7300*7c478bd9Sstevel@tonic-gate  * it, or we went exclusive in the put procedure.  In any case,
7301*7c478bd9Sstevel@tonic-gate  * we want to clear the bit now, and it is probably easier to do
7302*7c478bd9Sstevel@tonic-gate  * this at the beginning of this function (remember, we hold
7303*7c478bd9Sstevel@tonic-gate  * the SQLOCK).  Lastly, if there are other messages queued
7304*7c478bd9Sstevel@tonic-gate  * on the syncq (and not for our destination), enable the syncq
7305*7c478bd9Sstevel@tonic-gate  * for background work.
7306*7c478bd9Sstevel@tonic-gate  */
7307*7c478bd9Sstevel@tonic-gate 
7308*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
7309*7c478bd9Sstevel@tonic-gate void
7310*7c478bd9Sstevel@tonic-gate putnext_tail(syncq_t *sq, queue_t *qp, uint32_t passflags)
7311*7c478bd9Sstevel@tonic-gate {
7312*7c478bd9Sstevel@tonic-gate 	uint16_t	flags = sq->sq_flags;
7313*7c478bd9Sstevel@tonic-gate 
7314*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(SQLOCK(sq)));
7315*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(QLOCK(qp)));
7316*7c478bd9Sstevel@tonic-gate 
7317*7c478bd9Sstevel@tonic-gate 	/* Clear SQ_EXCL if set in passflags */
7318*7c478bd9Sstevel@tonic-gate 	if (passflags & SQ_EXCL) {
7319*7c478bd9Sstevel@tonic-gate 		flags &= ~SQ_EXCL;
7320*7c478bd9Sstevel@tonic-gate 	}
7321*7c478bd9Sstevel@tonic-gate 	if (flags & SQ_WANTWAKEUP) {
7322*7c478bd9Sstevel@tonic-gate 		flags &= ~SQ_WANTWAKEUP;
7323*7c478bd9Sstevel@tonic-gate 		cv_broadcast(&sq->sq_wait);
7324*7c478bd9Sstevel@tonic-gate 	}
7325*7c478bd9Sstevel@tonic-gate 	if (flags & SQ_WANTEXWAKEUP) {
7326*7c478bd9Sstevel@tonic-gate 		flags &= ~SQ_WANTEXWAKEUP;
7327*7c478bd9Sstevel@tonic-gate 		cv_broadcast(&sq->sq_exitwait);
7328*7c478bd9Sstevel@tonic-gate 	}
7329*7c478bd9Sstevel@tonic-gate 	sq->sq_flags = flags;
7330*7c478bd9Sstevel@tonic-gate 
7331*7c478bd9Sstevel@tonic-gate 	/*
7332*7c478bd9Sstevel@tonic-gate 	 * We have cleared SQ_EXCL if we were asked to, and started
7333*7c478bd9Sstevel@tonic-gate 	 * the wakeup process for waiters.  If there are no writers
7334*7c478bd9Sstevel@tonic-gate 	 * then we need to drain the syncq if we were told to, or
7335*7c478bd9Sstevel@tonic-gate 	 * enable the background thread to do it.
7336*7c478bd9Sstevel@tonic-gate 	 */
7337*7c478bd9Sstevel@tonic-gate 	if (!(flags & (SQ_STAYAWAY|SQ_EXCL))) {
7338*7c478bd9Sstevel@tonic-gate 		if ((passflags & SQ_QUEUED) ||
7339*7c478bd9Sstevel@tonic-gate 		    (sq->sq_svcflags & SQ_DISABLED)) {
7340*7c478bd9Sstevel@tonic-gate 			/* drain_syncq will take care of events in the list */
7341*7c478bd9Sstevel@tonic-gate 			drain_syncq(sq);
7342*7c478bd9Sstevel@tonic-gate 			return;
7343*7c478bd9Sstevel@tonic-gate 		} else if (flags & SQ_QUEUED) {
7344*7c478bd9Sstevel@tonic-gate 			sqenable(sq);
7345*7c478bd9Sstevel@tonic-gate 		}
7346*7c478bd9Sstevel@tonic-gate 	}
7347*7c478bd9Sstevel@tonic-gate 	/* Drop the SQLOCK on exit */
7348*7c478bd9Sstevel@tonic-gate 	mutex_exit(SQLOCK(sq));
7349*7c478bd9Sstevel@tonic-gate 	TRACE_3(TR_FAC_STREAMS_FR, TR_PUTNEXT_END,
7350*7c478bd9Sstevel@tonic-gate 		"putnext_end:(%p, %p, %p) done", NULL, qp, sq);
7351*7c478bd9Sstevel@tonic-gate }
7352*7c478bd9Sstevel@tonic-gate 
7353*7c478bd9Sstevel@tonic-gate void
7354*7c478bd9Sstevel@tonic-gate set_qend(queue_t *q)
7355*7c478bd9Sstevel@tonic-gate {
7356*7c478bd9Sstevel@tonic-gate 	mutex_enter(QLOCK(q));
7357*7c478bd9Sstevel@tonic-gate 	if (!O_SAMESTR(q))
7358*7c478bd9Sstevel@tonic-gate 		q->q_flag |= QEND;
7359*7c478bd9Sstevel@tonic-gate 	else
7360*7c478bd9Sstevel@tonic-gate 		q->q_flag &= ~QEND;
7361*7c478bd9Sstevel@tonic-gate 	mutex_exit(QLOCK(q));
7362*7c478bd9Sstevel@tonic-gate 	q = _OTHERQ(q);
7363*7c478bd9Sstevel@tonic-gate 	mutex_enter(QLOCK(q));
7364*7c478bd9Sstevel@tonic-gate 	if (!O_SAMESTR(q))
7365*7c478bd9Sstevel@tonic-gate 		q->q_flag |= QEND;
7366*7c478bd9Sstevel@tonic-gate 	else
7367*7c478bd9Sstevel@tonic-gate 		q->q_flag &= ~QEND;
7368*7c478bd9Sstevel@tonic-gate 	mutex_exit(QLOCK(q));
7369*7c478bd9Sstevel@tonic-gate }
7370*7c478bd9Sstevel@tonic-gate 
7371*7c478bd9Sstevel@tonic-gate 
7372*7c478bd9Sstevel@tonic-gate void
7373*7c478bd9Sstevel@tonic-gate clr_qfull(queue_t *q)
7374*7c478bd9Sstevel@tonic-gate {
7375*7c478bd9Sstevel@tonic-gate 	queue_t	*oq = q;
7376*7c478bd9Sstevel@tonic-gate 
7377*7c478bd9Sstevel@tonic-gate 	q = q->q_nfsrv;
7378*7c478bd9Sstevel@tonic-gate 	/* Fast check if there is any work to do before getting the lock. */
7379*7c478bd9Sstevel@tonic-gate 	if ((q->q_flag & (QFULL|QWANTW)) == 0) {
7380*7c478bd9Sstevel@tonic-gate 		return;
7381*7c478bd9Sstevel@tonic-gate 	}
7382*7c478bd9Sstevel@tonic-gate 
7383*7c478bd9Sstevel@tonic-gate 	/*
7384*7c478bd9Sstevel@tonic-gate 	 * Do not reset QFULL (and backenable) if the q_count is the reason
7385*7c478bd9Sstevel@tonic-gate 	 * for QFULL being set.
7386*7c478bd9Sstevel@tonic-gate 	 */
7387*7c478bd9Sstevel@tonic-gate 	mutex_enter(QLOCK(q));
7388*7c478bd9Sstevel@tonic-gate 	/*
7389*7c478bd9Sstevel@tonic-gate 	 * If both q_count and q_mblkcnt are less than the hiwat mark
7390*7c478bd9Sstevel@tonic-gate 	 */
7391*7c478bd9Sstevel@tonic-gate 	if ((q->q_count < q->q_hiwat) && (q->q_mblkcnt < q->q_hiwat)) {
7392*7c478bd9Sstevel@tonic-gate 		q->q_flag &= ~QFULL;
7393*7c478bd9Sstevel@tonic-gate 		/*
7394*7c478bd9Sstevel@tonic-gate 		 * A little more confusing, how about this way:
7395*7c478bd9Sstevel@tonic-gate 		 * if someone wants to write,
7396*7c478bd9Sstevel@tonic-gate 		 * AND
7397*7c478bd9Sstevel@tonic-gate 		 *    both counts are less than the lowat mark
7398*7c478bd9Sstevel@tonic-gate 		 *    OR
7399*7c478bd9Sstevel@tonic-gate 		 *    the lowat mark is zero
7400*7c478bd9Sstevel@tonic-gate 		 * THEN
7401*7c478bd9Sstevel@tonic-gate 		 * backenable
7402*7c478bd9Sstevel@tonic-gate 		 */
7403*7c478bd9Sstevel@tonic-gate 		if ((q->q_flag & QWANTW) &&
7404*7c478bd9Sstevel@tonic-gate 		    (((q->q_count < q->q_lowat) &&
7405*7c478bd9Sstevel@tonic-gate 		    (q->q_mblkcnt < q->q_lowat)) || q->q_lowat == 0)) {
7406*7c478bd9Sstevel@tonic-gate 			q->q_flag &= ~QWANTW;
7407*7c478bd9Sstevel@tonic-gate 			mutex_exit(QLOCK(q));
7408*7c478bd9Sstevel@tonic-gate 			backenable(oq, 0);
7409*7c478bd9Sstevel@tonic-gate 		} else
7410*7c478bd9Sstevel@tonic-gate 			mutex_exit(QLOCK(q));
7411*7c478bd9Sstevel@tonic-gate 	} else
7412*7c478bd9Sstevel@tonic-gate 		mutex_exit(QLOCK(q));
7413*7c478bd9Sstevel@tonic-gate }
7414*7c478bd9Sstevel@tonic-gate 
7415*7c478bd9Sstevel@tonic-gate /*
7416*7c478bd9Sstevel@tonic-gate  * Set the forward service procedure pointer.
7417*7c478bd9Sstevel@tonic-gate  *
7418*7c478bd9Sstevel@tonic-gate  * Called at insert-time to cache a queue's next forward service procedure in
7419*7c478bd9Sstevel@tonic-gate  * q_nfsrv; used by canput() and canputnext().  If the queue to be inserted
7420*7c478bd9Sstevel@tonic-gate  * has a service procedure then q_nfsrv points to itself.  If the queue to be
7421*7c478bd9Sstevel@tonic-gate  * inserted does not have a service procedure, then q_nfsrv points to the next
7422*7c478bd9Sstevel@tonic-gate  * queue forward that has a service procedure.  If the queue is at the logical
7423*7c478bd9Sstevel@tonic-gate  * end of the stream (driver for write side, stream head for the read side)
7424*7c478bd9Sstevel@tonic-gate  * and does not have a service procedure, then q_nfsrv also points to itself.
7425*7c478bd9Sstevel@tonic-gate  */
7426*7c478bd9Sstevel@tonic-gate void
7427*7c478bd9Sstevel@tonic-gate set_nfsrv_ptr(
7428*7c478bd9Sstevel@tonic-gate 	queue_t  *rnew,		/* read queue pointer to new module */
7429*7c478bd9Sstevel@tonic-gate 	queue_t  *wnew,		/* write queue pointer to new module */
7430*7c478bd9Sstevel@tonic-gate 	queue_t  *prev_rq,	/* read queue pointer to the module above */
7431*7c478bd9Sstevel@tonic-gate 	queue_t  *prev_wq)	/* write queue pointer to the module above */
7432*7c478bd9Sstevel@tonic-gate {
7433*7c478bd9Sstevel@tonic-gate 	queue_t *qp;
7434*7c478bd9Sstevel@tonic-gate 
7435*7c478bd9Sstevel@tonic-gate 	if (prev_wq->q_next == NULL) {
7436*7c478bd9Sstevel@tonic-gate 		/*
7437*7c478bd9Sstevel@tonic-gate 		 * Insert the driver, initialize the driver and stream head.
7438*7c478bd9Sstevel@tonic-gate 		 * In this case, prev_rq/prev_wq should be the stream head.
7439*7c478bd9Sstevel@tonic-gate 		 * _I_INSERT does not allow inserting a driver.  Make sure
7440*7c478bd9Sstevel@tonic-gate 		 * that it is not an insertion.
7441*7c478bd9Sstevel@tonic-gate 		 */
7442*7c478bd9Sstevel@tonic-gate 		ASSERT(!(rnew->q_flag & _QINSERTING));
7443*7c478bd9Sstevel@tonic-gate 		wnew->q_nfsrv = wnew;
7444*7c478bd9Sstevel@tonic-gate 		if (rnew->q_qinfo->qi_srvp)
7445*7c478bd9Sstevel@tonic-gate 			rnew->q_nfsrv = rnew;
7446*7c478bd9Sstevel@tonic-gate 		else
7447*7c478bd9Sstevel@tonic-gate 			rnew->q_nfsrv = prev_rq;
7448*7c478bd9Sstevel@tonic-gate 		prev_rq->q_nfsrv = prev_rq;
7449*7c478bd9Sstevel@tonic-gate 		prev_wq->q_nfsrv = prev_wq;
7450*7c478bd9Sstevel@tonic-gate 	} else {
7451*7c478bd9Sstevel@tonic-gate 		/*
7452*7c478bd9Sstevel@tonic-gate 		 * set up read side q_nfsrv pointer.  This MUST be done
7453*7c478bd9Sstevel@tonic-gate 		 * before setting the write side, because the setting of
7454*7c478bd9Sstevel@tonic-gate 		 * the write side for a fifo may depend on it.
7455*7c478bd9Sstevel@tonic-gate 		 *
7456*7c478bd9Sstevel@tonic-gate 		 * Suppose we have a fifo that only has pipemod pushed.
7457*7c478bd9Sstevel@tonic-gate 		 * pipemod has no read or write service procedures, so
7458*7c478bd9Sstevel@tonic-gate 		 * nfsrv for both pipemod queues points to prev_rq (the
7459*7c478bd9Sstevel@tonic-gate 		 * stream read head).  Now push bufmod (which has only a
7460*7c478bd9Sstevel@tonic-gate 		 * read service procedure).  Doing the write side first,
7461*7c478bd9Sstevel@tonic-gate 		 * wnew->q_nfsrv is set to pipemod's writeq nfsrv, which
7462*7c478bd9Sstevel@tonic-gate 		 * is WRONG; the next queue forward from wnew with a
7463*7c478bd9Sstevel@tonic-gate 		 * service procedure will be rnew, not the stream read head.
7464*7c478bd9Sstevel@tonic-gate 		 * Since the downstream queue (which in the case of a fifo
7465*7c478bd9Sstevel@tonic-gate 		 * is the read queue rnew) can affect upstream queues, it
7466*7c478bd9Sstevel@tonic-gate 		 * needs to be done first.  Setting up the read side first
7467*7c478bd9Sstevel@tonic-gate 		 * sets nfsrv for both pipemod queues to rnew and then
7468*7c478bd9Sstevel@tonic-gate 		 * when the write side is set up, wnew-q_nfsrv will also
7469*7c478bd9Sstevel@tonic-gate 		 * point to rnew.
7470*7c478bd9Sstevel@tonic-gate 		 */
7471*7c478bd9Sstevel@tonic-gate 		if (rnew->q_qinfo->qi_srvp) {
7472*7c478bd9Sstevel@tonic-gate 			/*
7473*7c478bd9Sstevel@tonic-gate 			 * use _OTHERQ() because, if this is a pipe, next
7474*7c478bd9Sstevel@tonic-gate 			 * module may have been pushed from other end and
7475*7c478bd9Sstevel@tonic-gate 			 * q_next could be a read queue.
7476*7c478bd9Sstevel@tonic-gate 			 */
7477*7c478bd9Sstevel@tonic-gate 			qp = _OTHERQ(prev_wq->q_next);
7478*7c478bd9Sstevel@tonic-gate 			while (qp && qp->q_nfsrv != qp) {
7479*7c478bd9Sstevel@tonic-gate 				qp->q_nfsrv = rnew;
7480*7c478bd9Sstevel@tonic-gate 				qp = backq(qp);
7481*7c478bd9Sstevel@tonic-gate 			}
7482*7c478bd9Sstevel@tonic-gate 			rnew->q_nfsrv = rnew;
7483*7c478bd9Sstevel@tonic-gate 		} else
7484*7c478bd9Sstevel@tonic-gate 			rnew->q_nfsrv = prev_rq->q_nfsrv;
7485*7c478bd9Sstevel@tonic-gate 
7486*7c478bd9Sstevel@tonic-gate 		/* set up write side q_nfsrv pointer */
7487*7c478bd9Sstevel@tonic-gate 		if (wnew->q_qinfo->qi_srvp) {
7488*7c478bd9Sstevel@tonic-gate 			wnew->q_nfsrv = wnew;
7489*7c478bd9Sstevel@tonic-gate 
7490*7c478bd9Sstevel@tonic-gate 			/*
7491*7c478bd9Sstevel@tonic-gate 			 * For insertion, need to update nfsrv of the modules
7492*7c478bd9Sstevel@tonic-gate 			 * above which do not have a service routine.
7493*7c478bd9Sstevel@tonic-gate 			 */
7494*7c478bd9Sstevel@tonic-gate 			if (rnew->q_flag & _QINSERTING) {
7495*7c478bd9Sstevel@tonic-gate 				for (qp = prev_wq;
7496*7c478bd9Sstevel@tonic-gate 				    qp != NULL && qp->q_nfsrv != qp;
7497*7c478bd9Sstevel@tonic-gate 				    qp = backq(qp)) {
7498*7c478bd9Sstevel@tonic-gate 					qp->q_nfsrv = wnew->q_nfsrv;
7499*7c478bd9Sstevel@tonic-gate 				}
7500*7c478bd9Sstevel@tonic-gate 			}
7501*7c478bd9Sstevel@tonic-gate 		} else {
7502*7c478bd9Sstevel@tonic-gate 			if (prev_wq->q_next == prev_rq)
7503*7c478bd9Sstevel@tonic-gate 				/*
7504*7c478bd9Sstevel@tonic-gate 				 * Since prev_wq/prev_rq are the middle of a
7505*7c478bd9Sstevel@tonic-gate 				 * fifo, wnew/rnew will also be the middle of
7506*7c478bd9Sstevel@tonic-gate 				 * a fifo and wnew's nfsrv is same as rnew's.
7507*7c478bd9Sstevel@tonic-gate 				 */
7508*7c478bd9Sstevel@tonic-gate 				wnew->q_nfsrv = rnew->q_nfsrv;
7509*7c478bd9Sstevel@tonic-gate 			else
7510*7c478bd9Sstevel@tonic-gate 				wnew->q_nfsrv = prev_wq->q_next->q_nfsrv;
7511*7c478bd9Sstevel@tonic-gate 		}
7512*7c478bd9Sstevel@tonic-gate 	}
7513*7c478bd9Sstevel@tonic-gate }
7514*7c478bd9Sstevel@tonic-gate 
7515*7c478bd9Sstevel@tonic-gate /*
7516*7c478bd9Sstevel@tonic-gate  * Reset the forward service procedure pointer; called at remove-time.
7517*7c478bd9Sstevel@tonic-gate  */
7518*7c478bd9Sstevel@tonic-gate void
7519*7c478bd9Sstevel@tonic-gate reset_nfsrv_ptr(queue_t *rqp, queue_t *wqp)
7520*7c478bd9Sstevel@tonic-gate {
7521*7c478bd9Sstevel@tonic-gate 	queue_t *tmp_qp;
7522*7c478bd9Sstevel@tonic-gate 
7523*7c478bd9Sstevel@tonic-gate 	/* Reset the write side q_nfsrv pointer for _I_REMOVE */
7524*7c478bd9Sstevel@tonic-gate 	if ((rqp->q_flag & _QREMOVING) && (wqp->q_qinfo->qi_srvp != NULL)) {
7525*7c478bd9Sstevel@tonic-gate 		for (tmp_qp = backq(wqp);
7526*7c478bd9Sstevel@tonic-gate 		    tmp_qp != NULL && tmp_qp->q_nfsrv == wqp;
7527*7c478bd9Sstevel@tonic-gate 		    tmp_qp = backq(tmp_qp)) {
7528*7c478bd9Sstevel@tonic-gate 			tmp_qp->q_nfsrv = wqp->q_nfsrv;
7529*7c478bd9Sstevel@tonic-gate 		}
7530*7c478bd9Sstevel@tonic-gate 	}
7531*7c478bd9Sstevel@tonic-gate 
7532*7c478bd9Sstevel@tonic-gate 	/* reset the read side q_nfsrv pointer */
7533*7c478bd9Sstevel@tonic-gate 	if (rqp->q_qinfo->qi_srvp) {
7534*7c478bd9Sstevel@tonic-gate 		if (wqp->q_next) {	/* non-driver case */
7535*7c478bd9Sstevel@tonic-gate 			tmp_qp = _OTHERQ(wqp->q_next);
7536*7c478bd9Sstevel@tonic-gate 			while (tmp_qp && tmp_qp->q_nfsrv == rqp) {
7537*7c478bd9Sstevel@tonic-gate 				/* Note that rqp->q_next cannot be NULL */
7538*7c478bd9Sstevel@tonic-gate 				ASSERT(rqp->q_next != NULL);
7539*7c478bd9Sstevel@tonic-gate 				tmp_qp->q_nfsrv = rqp->q_next->q_nfsrv;
7540*7c478bd9Sstevel@tonic-gate 				tmp_qp = backq(tmp_qp);
7541*7c478bd9Sstevel@tonic-gate 			}
7542*7c478bd9Sstevel@tonic-gate 		}
7543*7c478bd9Sstevel@tonic-gate 	}
7544*7c478bd9Sstevel@tonic-gate }
7545*7c478bd9Sstevel@tonic-gate 
7546*7c478bd9Sstevel@tonic-gate /*
7547*7c478bd9Sstevel@tonic-gate  * This routine should be called after all stream geometry changes to update
7548*7c478bd9Sstevel@tonic-gate  * the stream head cached struio() rd/wr queue pointers. Note must be called
7549*7c478bd9Sstevel@tonic-gate  * with the streamlock()ed.
7550*7c478bd9Sstevel@tonic-gate  *
7551*7c478bd9Sstevel@tonic-gate  * Note: only enables Synchronous STREAMS for a side of a Stream which has
7552*7c478bd9Sstevel@tonic-gate  *	 an explicit synchronous barrier module queue. That is, a queue that
7553*7c478bd9Sstevel@tonic-gate  *	 has specified a struio() type.
7554*7c478bd9Sstevel@tonic-gate  */
7555*7c478bd9Sstevel@tonic-gate static void
7556*7c478bd9Sstevel@tonic-gate strsetuio(stdata_t *stp)
7557*7c478bd9Sstevel@tonic-gate {
7558*7c478bd9Sstevel@tonic-gate 	queue_t *wrq;
7559*7c478bd9Sstevel@tonic-gate 
7560*7c478bd9Sstevel@tonic-gate 	if (stp->sd_flag & STPLEX) {
7561*7c478bd9Sstevel@tonic-gate 		/*
7562*7c478bd9Sstevel@tonic-gate 		 * Not stremahead, but a mux, so no Synchronous STREAMS.
7563*7c478bd9Sstevel@tonic-gate 		 */
7564*7c478bd9Sstevel@tonic-gate 		stp->sd_struiowrq = NULL;
7565*7c478bd9Sstevel@tonic-gate 		stp->sd_struiordq = NULL;
7566*7c478bd9Sstevel@tonic-gate 		return;
7567*7c478bd9Sstevel@tonic-gate 	}
7568*7c478bd9Sstevel@tonic-gate 	/*
7569*7c478bd9Sstevel@tonic-gate 	 * Scan the write queue(s) while synchronous
7570*7c478bd9Sstevel@tonic-gate 	 * until we find a qinfo uio type specified.
7571*7c478bd9Sstevel@tonic-gate 	 */
7572*7c478bd9Sstevel@tonic-gate 	wrq = stp->sd_wrq->q_next;
7573*7c478bd9Sstevel@tonic-gate 	while (wrq) {
7574*7c478bd9Sstevel@tonic-gate 		if (wrq->q_struiot == STRUIOT_NONE) {
7575*7c478bd9Sstevel@tonic-gate 			wrq = 0;
7576*7c478bd9Sstevel@tonic-gate 			break;
7577*7c478bd9Sstevel@tonic-gate 		}
7578*7c478bd9Sstevel@tonic-gate 		if (wrq->q_struiot != STRUIOT_DONTCARE)
7579*7c478bd9Sstevel@tonic-gate 			break;
7580*7c478bd9Sstevel@tonic-gate 		if (! _SAMESTR(wrq)) {
7581*7c478bd9Sstevel@tonic-gate 			wrq = 0;
7582*7c478bd9Sstevel@tonic-gate 			break;
7583*7c478bd9Sstevel@tonic-gate 		}
7584*7c478bd9Sstevel@tonic-gate 		wrq = wrq->q_next;
7585*7c478bd9Sstevel@tonic-gate 	}
7586*7c478bd9Sstevel@tonic-gate 	stp->sd_struiowrq = wrq;
7587*7c478bd9Sstevel@tonic-gate 	/*
7588*7c478bd9Sstevel@tonic-gate 	 * Scan the read queue(s) while synchronous
7589*7c478bd9Sstevel@tonic-gate 	 * until we find a qinfo uio type specified.
7590*7c478bd9Sstevel@tonic-gate 	 */
7591*7c478bd9Sstevel@tonic-gate 	wrq = stp->sd_wrq->q_next;
7592*7c478bd9Sstevel@tonic-gate 	while (wrq) {
7593*7c478bd9Sstevel@tonic-gate 		if (_RD(wrq)->q_struiot == STRUIOT_NONE) {
7594*7c478bd9Sstevel@tonic-gate 			wrq = 0;
7595*7c478bd9Sstevel@tonic-gate 			break;
7596*7c478bd9Sstevel@tonic-gate 		}
7597*7c478bd9Sstevel@tonic-gate 		if (_RD(wrq)->q_struiot != STRUIOT_DONTCARE)
7598*7c478bd9Sstevel@tonic-gate 			break;
7599*7c478bd9Sstevel@tonic-gate 		if (! _SAMESTR(wrq)) {
7600*7c478bd9Sstevel@tonic-gate 			wrq = 0;
7601*7c478bd9Sstevel@tonic-gate 			break;
7602*7c478bd9Sstevel@tonic-gate 		}
7603*7c478bd9Sstevel@tonic-gate 		wrq = wrq->q_next;
7604*7c478bd9Sstevel@tonic-gate 	}
7605*7c478bd9Sstevel@tonic-gate 	stp->sd_struiordq = wrq ? _RD(wrq) : 0;
7606*7c478bd9Sstevel@tonic-gate }
7607*7c478bd9Sstevel@tonic-gate 
7608*7c478bd9Sstevel@tonic-gate /*
7609*7c478bd9Sstevel@tonic-gate  * pass_wput, unblocks the passthru queues, so that
7610*7c478bd9Sstevel@tonic-gate  * messages can arrive at muxs lower read queue, before
7611*7c478bd9Sstevel@tonic-gate  * I_LINK/I_UNLINK is acked/nacked.
7612*7c478bd9Sstevel@tonic-gate  */
7613*7c478bd9Sstevel@tonic-gate static void
7614*7c478bd9Sstevel@tonic-gate pass_wput(queue_t *q, mblk_t *mp)
7615*7c478bd9Sstevel@tonic-gate {
7616*7c478bd9Sstevel@tonic-gate 	syncq_t *sq;
7617*7c478bd9Sstevel@tonic-gate 
7618*7c478bd9Sstevel@tonic-gate 	sq = _RD(q)->q_syncq;
7619*7c478bd9Sstevel@tonic-gate 	if (sq->sq_flags & SQ_BLOCKED)
7620*7c478bd9Sstevel@tonic-gate 		unblocksq(sq, SQ_BLOCKED, 0);
7621*7c478bd9Sstevel@tonic-gate 	putnext(q, mp);
7622*7c478bd9Sstevel@tonic-gate }
7623*7c478bd9Sstevel@tonic-gate 
7624*7c478bd9Sstevel@tonic-gate /*
7625*7c478bd9Sstevel@tonic-gate  * Set up queues for the link/unlink.
7626*7c478bd9Sstevel@tonic-gate  * Create a new queue and block it and then insert it
7627*7c478bd9Sstevel@tonic-gate  * below the stream head on the lower stream.
7628*7c478bd9Sstevel@tonic-gate  * This prevents any messages from arriving during the setq
7629*7c478bd9Sstevel@tonic-gate  * as well as while the mux is processing the LINK/I_UNLINK.
7630*7c478bd9Sstevel@tonic-gate  * The blocked passq is unblocked once the LINK/I_UNLINK has
7631*7c478bd9Sstevel@tonic-gate  * been acked or nacked or if a message is generated and sent
7632*7c478bd9Sstevel@tonic-gate  * down muxs write put procedure.
7633*7c478bd9Sstevel@tonic-gate  * see pass_wput().
7634*7c478bd9Sstevel@tonic-gate  *
7635*7c478bd9Sstevel@tonic-gate  * After the new queue is inserted, all messages coming from below are
7636*7c478bd9Sstevel@tonic-gate  * blocked. The call to strlock will ensure that all activity in the stream head
7637*7c478bd9Sstevel@tonic-gate  * read queue syncq is stopped (sq_count drops to zero).
7638*7c478bd9Sstevel@tonic-gate  */
7639*7c478bd9Sstevel@tonic-gate static queue_t *
7640*7c478bd9Sstevel@tonic-gate link_addpassthru(stdata_t *stpdown)
7641*7c478bd9Sstevel@tonic-gate {
7642*7c478bd9Sstevel@tonic-gate 	queue_t *passq;
7643*7c478bd9Sstevel@tonic-gate 	sqlist_t sqlist;
7644*7c478bd9Sstevel@tonic-gate 
7645*7c478bd9Sstevel@tonic-gate 	passq = allocq();
7646*7c478bd9Sstevel@tonic-gate 	STREAM(passq) = STREAM(_WR(passq)) = stpdown;
7647*7c478bd9Sstevel@tonic-gate 	/* setq might sleep in allocator - avoid holding locks. */
7648*7c478bd9Sstevel@tonic-gate 	setq(passq, &passthru_rinit, &passthru_winit, NULL, QPERQ,
7649*7c478bd9Sstevel@tonic-gate 	    SQ_CI|SQ_CO, B_FALSE);
7650*7c478bd9Sstevel@tonic-gate 	claimq(passq);
7651*7c478bd9Sstevel@tonic-gate 	blocksq(passq->q_syncq, SQ_BLOCKED, 1);
7652*7c478bd9Sstevel@tonic-gate 	insertq(STREAM(passq), passq);
7653*7c478bd9Sstevel@tonic-gate 
7654*7c478bd9Sstevel@tonic-gate 	/*
7655*7c478bd9Sstevel@tonic-gate 	 * Use strlock() to wait for the stream head sq_count to drop to zero
7656*7c478bd9Sstevel@tonic-gate 	 * since we are going to change q_ptr in the stream head.  Note that
7657*7c478bd9Sstevel@tonic-gate 	 * insertq() doesn't wait for any syncq counts to drop to zero.
7658*7c478bd9Sstevel@tonic-gate 	 */
7659*7c478bd9Sstevel@tonic-gate 	sqlist.sqlist_head = NULL;
7660*7c478bd9Sstevel@tonic-gate 	sqlist.sqlist_index = 0;
7661*7c478bd9Sstevel@tonic-gate 	sqlist.sqlist_size = sizeof (sqlist_t);
7662*7c478bd9Sstevel@tonic-gate 	sqlist_insert(&sqlist, _RD(stpdown->sd_wrq)->q_syncq);
7663*7c478bd9Sstevel@tonic-gate 	strlock(stpdown, &sqlist);
7664*7c478bd9Sstevel@tonic-gate 	strunlock(stpdown, &sqlist);
7665*7c478bd9Sstevel@tonic-gate 
7666*7c478bd9Sstevel@tonic-gate 	releaseq(passq);
7667*7c478bd9Sstevel@tonic-gate 	return (passq);
7668*7c478bd9Sstevel@tonic-gate }
7669*7c478bd9Sstevel@tonic-gate 
7670*7c478bd9Sstevel@tonic-gate /*
7671*7c478bd9Sstevel@tonic-gate  * Let messages flow up into the mux by removing
7672*7c478bd9Sstevel@tonic-gate  * the passq.
7673*7c478bd9Sstevel@tonic-gate  */
7674*7c478bd9Sstevel@tonic-gate static void
7675*7c478bd9Sstevel@tonic-gate link_rempassthru(queue_t *passq)
7676*7c478bd9Sstevel@tonic-gate {
7677*7c478bd9Sstevel@tonic-gate 	claimq(passq);
7678*7c478bd9Sstevel@tonic-gate 	removeq(passq);
7679*7c478bd9Sstevel@tonic-gate 	releaseq(passq);
7680*7c478bd9Sstevel@tonic-gate 	freeq(passq);
7681*7c478bd9Sstevel@tonic-gate }
7682*7c478bd9Sstevel@tonic-gate 
7683*7c478bd9Sstevel@tonic-gate /*
7684*7c478bd9Sstevel@tonic-gate  * wait for an event with optional timeout and optional return if
7685*7c478bd9Sstevel@tonic-gate  * a signal is sent to the thread
7686*7c478bd9Sstevel@tonic-gate  * tim:  -1 : no timeout
7687*7c478bd9Sstevel@tonic-gate  *       otherwise the value is relative time in milliseconds to wait
7688*7c478bd9Sstevel@tonic-gate  * nosig:  if 0 then signals will be ignored, otherwise signals
7689*7c478bd9Sstevel@tonic-gate  *       will terminate wait
7690*7c478bd9Sstevel@tonic-gate  * returns >0 on success, 0 if signal was encountered, -1 if timeout
7691*7c478bd9Sstevel@tonic-gate  * was reached.
7692*7c478bd9Sstevel@tonic-gate  */
7693*7c478bd9Sstevel@tonic-gate clock_t
7694*7c478bd9Sstevel@tonic-gate str_cv_wait(kcondvar_t *cvp, kmutex_t *mp, clock_t tim, int nosigs)
7695*7c478bd9Sstevel@tonic-gate {
7696*7c478bd9Sstevel@tonic-gate 	clock_t ret, now, tick;
7697*7c478bd9Sstevel@tonic-gate 
7698*7c478bd9Sstevel@tonic-gate 	if (tim < 0) {
7699*7c478bd9Sstevel@tonic-gate 		if (nosigs) {
7700*7c478bd9Sstevel@tonic-gate 			cv_wait(cvp, mp);
7701*7c478bd9Sstevel@tonic-gate 			ret = 1;
7702*7c478bd9Sstevel@tonic-gate 		} else {
7703*7c478bd9Sstevel@tonic-gate 			ret = cv_wait_sig(cvp, mp);
7704*7c478bd9Sstevel@tonic-gate 		}
7705*7c478bd9Sstevel@tonic-gate 	} else if (tim > 0) {
7706*7c478bd9Sstevel@tonic-gate 		/*
7707*7c478bd9Sstevel@tonic-gate 		 * convert milliseconds to clock ticks
7708*7c478bd9Sstevel@tonic-gate 		 */
7709*7c478bd9Sstevel@tonic-gate 		tick = MSEC_TO_TICK_ROUNDUP(tim);
7710*7c478bd9Sstevel@tonic-gate 		time_to_wait(&now, tick);
7711*7c478bd9Sstevel@tonic-gate 		if (nosigs) {
7712*7c478bd9Sstevel@tonic-gate 			ret = cv_timedwait(cvp, mp, now);
7713*7c478bd9Sstevel@tonic-gate 		} else {
7714*7c478bd9Sstevel@tonic-gate 			ret = cv_timedwait_sig(cvp, mp, now);
7715*7c478bd9Sstevel@tonic-gate 		}
7716*7c478bd9Sstevel@tonic-gate 	} else {
7717*7c478bd9Sstevel@tonic-gate 		ret = -1;
7718*7c478bd9Sstevel@tonic-gate 	}
7719*7c478bd9Sstevel@tonic-gate 	return (ret);
7720*7c478bd9Sstevel@tonic-gate }
7721*7c478bd9Sstevel@tonic-gate 
7722*7c478bd9Sstevel@tonic-gate /*
7723*7c478bd9Sstevel@tonic-gate  * Wait until the stream head can determine if it is at the mark but
7724*7c478bd9Sstevel@tonic-gate  * don't wait forever to prevent a race condition between the "mark" state
7725*7c478bd9Sstevel@tonic-gate  * in the stream head and any mark state in the caller/user of this routine.
7726*7c478bd9Sstevel@tonic-gate  *
7727*7c478bd9Sstevel@tonic-gate  * This is used by sockets and for a socket it would be incorrect
7728*7c478bd9Sstevel@tonic-gate  * to return a failure for SIOCATMARK when there is no data in the receive
7729*7c478bd9Sstevel@tonic-gate  * queue and the marked urgent data is traveling up the stream.
7730*7c478bd9Sstevel@tonic-gate  *
7731*7c478bd9Sstevel@tonic-gate  * This routine waits until the mark is known by waiting for one of these
7732*7c478bd9Sstevel@tonic-gate  * three events:
7733*7c478bd9Sstevel@tonic-gate  *	The stream head read queue becoming non-empty (including an EOF)
7734*7c478bd9Sstevel@tonic-gate  *	The STRATMARK flag being set. (Due to a MSGMARKNEXT message.)
7735*7c478bd9Sstevel@tonic-gate  *	The STRNOTATMARK flag being set (which indicates that the transport
7736*7c478bd9Sstevel@tonic-gate  *	has sent a MSGNOTMARKNEXT message to indicate that it is not at
7737*7c478bd9Sstevel@tonic-gate  *	the mark).
7738*7c478bd9Sstevel@tonic-gate  *
7739*7c478bd9Sstevel@tonic-gate  * The routine returns 1 if the stream is at the mark; 0 if it can
7740*7c478bd9Sstevel@tonic-gate  * be determined that the stream is not at the mark.
7741*7c478bd9Sstevel@tonic-gate  * If the wait times out and it can't determine
7742*7c478bd9Sstevel@tonic-gate  * whether or not the stream might be at the mark the routine will return -1.
7743*7c478bd9Sstevel@tonic-gate  *
7744*7c478bd9Sstevel@tonic-gate  * Note: This routine should only be used when a mark is pending i.e.,
7745*7c478bd9Sstevel@tonic-gate  * in the socket case the SIGURG has been posted.
7746*7c478bd9Sstevel@tonic-gate  * Note2: This can not wakeup just because synchronous streams indicate
7747*7c478bd9Sstevel@tonic-gate  * that data is available since it is not possible to use the synchronous
7748*7c478bd9Sstevel@tonic-gate  * streams interfaces to determine the b_flag value for the data queued below
7749*7c478bd9Sstevel@tonic-gate  * the stream head.
7750*7c478bd9Sstevel@tonic-gate  */
7751*7c478bd9Sstevel@tonic-gate int
7752*7c478bd9Sstevel@tonic-gate strwaitmark(vnode_t *vp)
7753*7c478bd9Sstevel@tonic-gate {
7754*7c478bd9Sstevel@tonic-gate 	struct stdata *stp = vp->v_stream;
7755*7c478bd9Sstevel@tonic-gate 	queue_t *rq = _RD(stp->sd_wrq);
7756*7c478bd9Sstevel@tonic-gate 	int mark;
7757*7c478bd9Sstevel@tonic-gate 
7758*7c478bd9Sstevel@tonic-gate 	mutex_enter(&stp->sd_lock);
7759*7c478bd9Sstevel@tonic-gate 	while (rq->q_first == NULL &&
7760*7c478bd9Sstevel@tonic-gate 	    !(stp->sd_flag & (STRATMARK|STRNOTATMARK|STREOF))) {
7761*7c478bd9Sstevel@tonic-gate 		stp->sd_flag |= RSLEEP;
7762*7c478bd9Sstevel@tonic-gate 
7763*7c478bd9Sstevel@tonic-gate 		/* Wait for 100 milliseconds for any state change. */
7764*7c478bd9Sstevel@tonic-gate 		if (str_cv_wait(&rq->q_wait, &stp->sd_lock, 100, 1) == -1) {
7765*7c478bd9Sstevel@tonic-gate 			mutex_exit(&stp->sd_lock);
7766*7c478bd9Sstevel@tonic-gate 			return (-1);
7767*7c478bd9Sstevel@tonic-gate 		}
7768*7c478bd9Sstevel@tonic-gate 	}
7769*7c478bd9Sstevel@tonic-gate 	if (stp->sd_flag & STRATMARK)
7770*7c478bd9Sstevel@tonic-gate 		mark = 1;
7771*7c478bd9Sstevel@tonic-gate 	else if (rq->q_first != NULL && (rq->q_first->b_flag & MSGMARK))
7772*7c478bd9Sstevel@tonic-gate 		mark = 1;
7773*7c478bd9Sstevel@tonic-gate 	else
7774*7c478bd9Sstevel@tonic-gate 		mark = 0;
7775*7c478bd9Sstevel@tonic-gate 
7776*7c478bd9Sstevel@tonic-gate 	mutex_exit(&stp->sd_lock);
7777*7c478bd9Sstevel@tonic-gate 	return (mark);
7778*7c478bd9Sstevel@tonic-gate }
7779*7c478bd9Sstevel@tonic-gate 
7780*7c478bd9Sstevel@tonic-gate /*
7781*7c478bd9Sstevel@tonic-gate  * Set a read side error. If persist is set change the socket error
7782*7c478bd9Sstevel@tonic-gate  * to persistent. If errfunc is set install the function as the exported
7783*7c478bd9Sstevel@tonic-gate  * error handler.
7784*7c478bd9Sstevel@tonic-gate  */
7785*7c478bd9Sstevel@tonic-gate void
7786*7c478bd9Sstevel@tonic-gate strsetrerror(vnode_t *vp, int error, int persist, errfunc_t errfunc)
7787*7c478bd9Sstevel@tonic-gate {
7788*7c478bd9Sstevel@tonic-gate 	struct stdata *stp = vp->v_stream;
7789*7c478bd9Sstevel@tonic-gate 
7790*7c478bd9Sstevel@tonic-gate 	mutex_enter(&stp->sd_lock);
7791*7c478bd9Sstevel@tonic-gate 	stp->sd_rerror = error;
7792*7c478bd9Sstevel@tonic-gate 	if (error == 0 && errfunc == NULL)
7793*7c478bd9Sstevel@tonic-gate 		stp->sd_flag &= ~STRDERR;
7794*7c478bd9Sstevel@tonic-gate 	else
7795*7c478bd9Sstevel@tonic-gate 		stp->sd_flag |= STRDERR;
7796*7c478bd9Sstevel@tonic-gate 	if (persist) {
7797*7c478bd9Sstevel@tonic-gate 		stp->sd_flag &= ~STRDERRNONPERSIST;
7798*7c478bd9Sstevel@tonic-gate 	} else {
7799*7c478bd9Sstevel@tonic-gate 		stp->sd_flag |= STRDERRNONPERSIST;
7800*7c478bd9Sstevel@tonic-gate 	}
7801*7c478bd9Sstevel@tonic-gate 	stp->sd_rderrfunc = errfunc;
7802*7c478bd9Sstevel@tonic-gate 	if (error != 0 || errfunc != NULL) {
7803*7c478bd9Sstevel@tonic-gate 		cv_broadcast(&_RD(stp->sd_wrq)->q_wait);	/* readers */
7804*7c478bd9Sstevel@tonic-gate 		cv_broadcast(&stp->sd_wrq->q_wait);		/* writers */
7805*7c478bd9Sstevel@tonic-gate 		cv_broadcast(&stp->sd_monitor);			/* ioctllers */
7806*7c478bd9Sstevel@tonic-gate 
7807*7c478bd9Sstevel@tonic-gate 		mutex_exit(&stp->sd_lock);
7808*7c478bd9Sstevel@tonic-gate 		pollwakeup(&stp->sd_pollist, POLLERR);
7809*7c478bd9Sstevel@tonic-gate 		mutex_enter(&stp->sd_lock);
7810*7c478bd9Sstevel@tonic-gate 
7811*7c478bd9Sstevel@tonic-gate 		if (stp->sd_sigflags & S_ERROR)
7812*7c478bd9Sstevel@tonic-gate 			strsendsig(stp->sd_siglist, S_ERROR, 0, error);
7813*7c478bd9Sstevel@tonic-gate 	}
7814*7c478bd9Sstevel@tonic-gate 	mutex_exit(&stp->sd_lock);
7815*7c478bd9Sstevel@tonic-gate }
7816*7c478bd9Sstevel@tonic-gate 
7817*7c478bd9Sstevel@tonic-gate /*
7818*7c478bd9Sstevel@tonic-gate  * Set a write side error. If persist is set change the socket error
7819*7c478bd9Sstevel@tonic-gate  * to persistent.
7820*7c478bd9Sstevel@tonic-gate  */
7821*7c478bd9Sstevel@tonic-gate void
7822*7c478bd9Sstevel@tonic-gate strsetwerror(vnode_t *vp, int error, int persist, errfunc_t errfunc)
7823*7c478bd9Sstevel@tonic-gate {
7824*7c478bd9Sstevel@tonic-gate 	struct stdata *stp = vp->v_stream;
7825*7c478bd9Sstevel@tonic-gate 
7826*7c478bd9Sstevel@tonic-gate 	mutex_enter(&stp->sd_lock);
7827*7c478bd9Sstevel@tonic-gate 	stp->sd_werror = error;
7828*7c478bd9Sstevel@tonic-gate 	if (error == 0 && errfunc == NULL)
7829*7c478bd9Sstevel@tonic-gate 		stp->sd_flag &= ~STWRERR;
7830*7c478bd9Sstevel@tonic-gate 	else
7831*7c478bd9Sstevel@tonic-gate 		stp->sd_flag |= STWRERR;
7832*7c478bd9Sstevel@tonic-gate 	if (persist) {
7833*7c478bd9Sstevel@tonic-gate 		stp->sd_flag &= ~STWRERRNONPERSIST;
7834*7c478bd9Sstevel@tonic-gate 	} else {
7835*7c478bd9Sstevel@tonic-gate 		stp->sd_flag |= STWRERRNONPERSIST;
7836*7c478bd9Sstevel@tonic-gate 	}
7837*7c478bd9Sstevel@tonic-gate 	stp->sd_wrerrfunc = errfunc;
7838*7c478bd9Sstevel@tonic-gate 	if (error != 0 || errfunc != NULL) {
7839*7c478bd9Sstevel@tonic-gate 		cv_broadcast(&_RD(stp->sd_wrq)->q_wait);	/* readers */
7840*7c478bd9Sstevel@tonic-gate 		cv_broadcast(&stp->sd_wrq->q_wait);		/* writers */
7841*7c478bd9Sstevel@tonic-gate 		cv_broadcast(&stp->sd_monitor);			/* ioctllers */
7842*7c478bd9Sstevel@tonic-gate 
7843*7c478bd9Sstevel@tonic-gate 		mutex_exit(&stp->sd_lock);
7844*7c478bd9Sstevel@tonic-gate 		pollwakeup(&stp->sd_pollist, POLLERR);
7845*7c478bd9Sstevel@tonic-gate 		mutex_enter(&stp->sd_lock);
7846*7c478bd9Sstevel@tonic-gate 
7847*7c478bd9Sstevel@tonic-gate 		if (stp->sd_sigflags & S_ERROR)
7848*7c478bd9Sstevel@tonic-gate 			strsendsig(stp->sd_siglist, S_ERROR, 0, error);
7849*7c478bd9Sstevel@tonic-gate 	}
7850*7c478bd9Sstevel@tonic-gate 	mutex_exit(&stp->sd_lock);
7851*7c478bd9Sstevel@tonic-gate }
7852*7c478bd9Sstevel@tonic-gate 
7853*7c478bd9Sstevel@tonic-gate /*
7854*7c478bd9Sstevel@tonic-gate  * Make the stream return 0 (EOF) when all data has been read.
7855*7c478bd9Sstevel@tonic-gate  * No effect on write side.
7856*7c478bd9Sstevel@tonic-gate  */
7857*7c478bd9Sstevel@tonic-gate void
7858*7c478bd9Sstevel@tonic-gate strseteof(vnode_t *vp, int eof)
7859*7c478bd9Sstevel@tonic-gate {
7860*7c478bd9Sstevel@tonic-gate 	struct stdata *stp = vp->v_stream;
7861*7c478bd9Sstevel@tonic-gate 
7862*7c478bd9Sstevel@tonic-gate 	mutex_enter(&stp->sd_lock);
7863*7c478bd9Sstevel@tonic-gate 	if (!eof) {
7864*7c478bd9Sstevel@tonic-gate 		stp->sd_flag &= ~STREOF;
7865*7c478bd9Sstevel@tonic-gate 		mutex_exit(&stp->sd_lock);
7866*7c478bd9Sstevel@tonic-gate 		return;
7867*7c478bd9Sstevel@tonic-gate 	}
7868*7c478bd9Sstevel@tonic-gate 	stp->sd_flag |= STREOF;
7869*7c478bd9Sstevel@tonic-gate 	if (stp->sd_flag & RSLEEP) {
7870*7c478bd9Sstevel@tonic-gate 		stp->sd_flag &= ~RSLEEP;
7871*7c478bd9Sstevel@tonic-gate 		cv_broadcast(&_RD(stp->sd_wrq)->q_wait);
7872*7c478bd9Sstevel@tonic-gate 	}
7873*7c478bd9Sstevel@tonic-gate 
7874*7c478bd9Sstevel@tonic-gate 	mutex_exit(&stp->sd_lock);
7875*7c478bd9Sstevel@tonic-gate 	pollwakeup(&stp->sd_pollist, POLLIN|POLLRDNORM);
7876*7c478bd9Sstevel@tonic-gate 	mutex_enter(&stp->sd_lock);
7877*7c478bd9Sstevel@tonic-gate 
7878*7c478bd9Sstevel@tonic-gate 	if (stp->sd_sigflags & (S_INPUT|S_RDNORM))
7879*7c478bd9Sstevel@tonic-gate 		strsendsig(stp->sd_siglist, S_INPUT|S_RDNORM, 0, 0);
7880*7c478bd9Sstevel@tonic-gate 	mutex_exit(&stp->sd_lock);
7881*7c478bd9Sstevel@tonic-gate }
7882*7c478bd9Sstevel@tonic-gate 
7883*7c478bd9Sstevel@tonic-gate void
7884*7c478bd9Sstevel@tonic-gate strflushrq(vnode_t *vp, int flag)
7885*7c478bd9Sstevel@tonic-gate {
7886*7c478bd9Sstevel@tonic-gate 	struct stdata *stp = vp->v_stream;
7887*7c478bd9Sstevel@tonic-gate 
7888*7c478bd9Sstevel@tonic-gate 	mutex_enter(&stp->sd_lock);
7889*7c478bd9Sstevel@tonic-gate 	flushq(_RD(stp->sd_wrq), flag);
7890*7c478bd9Sstevel@tonic-gate 	mutex_exit(&stp->sd_lock);
7891*7c478bd9Sstevel@tonic-gate }
7892*7c478bd9Sstevel@tonic-gate 
7893*7c478bd9Sstevel@tonic-gate void
7894*7c478bd9Sstevel@tonic-gate strsetrputhooks(vnode_t *vp, uint_t flags,
7895*7c478bd9Sstevel@tonic-gate 		msgfunc_t protofunc, msgfunc_t miscfunc)
7896*7c478bd9Sstevel@tonic-gate {
7897*7c478bd9Sstevel@tonic-gate 	struct stdata *stp = vp->v_stream;
7898*7c478bd9Sstevel@tonic-gate 
7899*7c478bd9Sstevel@tonic-gate 	mutex_enter(&stp->sd_lock);
7900*7c478bd9Sstevel@tonic-gate 
7901*7c478bd9Sstevel@tonic-gate 	if (protofunc == NULL)
7902*7c478bd9Sstevel@tonic-gate 		stp->sd_rprotofunc = strrput_proto;
7903*7c478bd9Sstevel@tonic-gate 	else
7904*7c478bd9Sstevel@tonic-gate 		stp->sd_rprotofunc = protofunc;
7905*7c478bd9Sstevel@tonic-gate 
7906*7c478bd9Sstevel@tonic-gate 	if (miscfunc == NULL)
7907*7c478bd9Sstevel@tonic-gate 		stp->sd_rmiscfunc = strrput_misc;
7908*7c478bd9Sstevel@tonic-gate 	else
7909*7c478bd9Sstevel@tonic-gate 		stp->sd_rmiscfunc = miscfunc;
7910*7c478bd9Sstevel@tonic-gate 
7911*7c478bd9Sstevel@tonic-gate 	if (flags & SH_CONSOL_DATA)
7912*7c478bd9Sstevel@tonic-gate 		stp->sd_rput_opt |= SR_CONSOL_DATA;
7913*7c478bd9Sstevel@tonic-gate 	else
7914*7c478bd9Sstevel@tonic-gate 		stp->sd_rput_opt &= ~SR_CONSOL_DATA;
7915*7c478bd9Sstevel@tonic-gate 
7916*7c478bd9Sstevel@tonic-gate 	if (flags & SH_SIGALLDATA)
7917*7c478bd9Sstevel@tonic-gate 		stp->sd_rput_opt |= SR_SIGALLDATA;
7918*7c478bd9Sstevel@tonic-gate 	else
7919*7c478bd9Sstevel@tonic-gate 		stp->sd_rput_opt &= ~SR_SIGALLDATA;
7920*7c478bd9Sstevel@tonic-gate 
7921*7c478bd9Sstevel@tonic-gate 	if (flags & SH_IGN_ZEROLEN)
7922*7c478bd9Sstevel@tonic-gate 		stp->sd_rput_opt |= SR_IGN_ZEROLEN;
7923*7c478bd9Sstevel@tonic-gate 	else
7924*7c478bd9Sstevel@tonic-gate 		stp->sd_rput_opt &= ~SR_IGN_ZEROLEN;
7925*7c478bd9Sstevel@tonic-gate 
7926*7c478bd9Sstevel@tonic-gate 	mutex_exit(&stp->sd_lock);
7927*7c478bd9Sstevel@tonic-gate }
7928*7c478bd9Sstevel@tonic-gate 
7929*7c478bd9Sstevel@tonic-gate void
7930*7c478bd9Sstevel@tonic-gate strsetwputhooks(vnode_t *vp, uint_t flags, clock_t closetime)
7931*7c478bd9Sstevel@tonic-gate {
7932*7c478bd9Sstevel@tonic-gate 	struct stdata *stp = vp->v_stream;
7933*7c478bd9Sstevel@tonic-gate 
7934*7c478bd9Sstevel@tonic-gate 	mutex_enter(&stp->sd_lock);
7935*7c478bd9Sstevel@tonic-gate 	stp->sd_closetime = closetime;
7936*7c478bd9Sstevel@tonic-gate 
7937*7c478bd9Sstevel@tonic-gate 	if (flags & SH_SIGPIPE)
7938*7c478bd9Sstevel@tonic-gate 		stp->sd_wput_opt |= SW_SIGPIPE;
7939*7c478bd9Sstevel@tonic-gate 	else
7940*7c478bd9Sstevel@tonic-gate 		stp->sd_wput_opt &= ~SW_SIGPIPE;
7941*7c478bd9Sstevel@tonic-gate 	if (flags & SH_RECHECK_ERR)
7942*7c478bd9Sstevel@tonic-gate 		stp->sd_wput_opt |= SW_RECHECK_ERR;
7943*7c478bd9Sstevel@tonic-gate 	else
7944*7c478bd9Sstevel@tonic-gate 		stp->sd_wput_opt &= ~SW_RECHECK_ERR;
7945*7c478bd9Sstevel@tonic-gate 
7946*7c478bd9Sstevel@tonic-gate 	mutex_exit(&stp->sd_lock);
7947*7c478bd9Sstevel@tonic-gate }
7948*7c478bd9Sstevel@tonic-gate 
7949*7c478bd9Sstevel@tonic-gate /* Used within framework when the queue is already locked */
7950*7c478bd9Sstevel@tonic-gate void
7951*7c478bd9Sstevel@tonic-gate qenable_locked(queue_t *q)
7952*7c478bd9Sstevel@tonic-gate {
7953*7c478bd9Sstevel@tonic-gate 	stdata_t *stp = STREAM(q);
7954*7c478bd9Sstevel@tonic-gate 
7955*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(QLOCK(q)));
7956*7c478bd9Sstevel@tonic-gate 
7957*7c478bd9Sstevel@tonic-gate 	if (!q->q_qinfo->qi_srvp)
7958*7c478bd9Sstevel@tonic-gate 		return;
7959*7c478bd9Sstevel@tonic-gate 
7960*7c478bd9Sstevel@tonic-gate 	/*
7961*7c478bd9Sstevel@tonic-gate 	 * Do not place on run queue if already enabled or closing.
7962*7c478bd9Sstevel@tonic-gate 	 */
7963*7c478bd9Sstevel@tonic-gate 	if (q->q_flag & (QWCLOSE|QENAB))
7964*7c478bd9Sstevel@tonic-gate 		return;
7965*7c478bd9Sstevel@tonic-gate 
7966*7c478bd9Sstevel@tonic-gate 	/*
7967*7c478bd9Sstevel@tonic-gate 	 * mark queue enabled and place on run list if it is not already being
7968*7c478bd9Sstevel@tonic-gate 	 * serviced. If it is serviced, the runservice() function will detect
7969*7c478bd9Sstevel@tonic-gate 	 * that QENAB is set and call service procedure before clearing
7970*7c478bd9Sstevel@tonic-gate 	 * QINSERVICE flag.
7971*7c478bd9Sstevel@tonic-gate 	 */
7972*7c478bd9Sstevel@tonic-gate 	q->q_flag |= QENAB;
7973*7c478bd9Sstevel@tonic-gate 	if (q->q_flag & QINSERVICE)
7974*7c478bd9Sstevel@tonic-gate 		return;
7975*7c478bd9Sstevel@tonic-gate 
7976*7c478bd9Sstevel@tonic-gate 	/* Record the time of qenable */
7977*7c478bd9Sstevel@tonic-gate 	q->q_qtstamp = lbolt;
7978*7c478bd9Sstevel@tonic-gate 
7979*7c478bd9Sstevel@tonic-gate 	/*
7980*7c478bd9Sstevel@tonic-gate 	 * Put the queue in the stp list and schedule it for background
7981*7c478bd9Sstevel@tonic-gate 	 * processing if it is not already scheduled or if stream head does not
7982*7c478bd9Sstevel@tonic-gate 	 * intent to process it in the foreground later by setting
7983*7c478bd9Sstevel@tonic-gate 	 * STRS_WILLSERVICE flag.
7984*7c478bd9Sstevel@tonic-gate 	 */
7985*7c478bd9Sstevel@tonic-gate 	mutex_enter(&stp->sd_qlock);
7986*7c478bd9Sstevel@tonic-gate 	/*
7987*7c478bd9Sstevel@tonic-gate 	 * If there are already something on the list, stp flags should show
7988*7c478bd9Sstevel@tonic-gate 	 * intention to drain it.
7989*7c478bd9Sstevel@tonic-gate 	 */
7990*7c478bd9Sstevel@tonic-gate 	IMPLY(STREAM_NEEDSERVICE(stp),
7991*7c478bd9Sstevel@tonic-gate 	    (stp->sd_svcflags & (STRS_WILLSERVICE | STRS_SCHEDULED)));
7992*7c478bd9Sstevel@tonic-gate 
7993*7c478bd9Sstevel@tonic-gate 	ENQUEUE(q, stp->sd_qhead, stp->sd_qtail, q_link);
7994*7c478bd9Sstevel@tonic-gate 	stp->sd_nqueues++;
7995*7c478bd9Sstevel@tonic-gate 
7996*7c478bd9Sstevel@tonic-gate 	/*
7997*7c478bd9Sstevel@tonic-gate 	 * If no one will drain this stream we are the first producer and
7998*7c478bd9Sstevel@tonic-gate 	 * need to schedule it for background thread.
7999*7c478bd9Sstevel@tonic-gate 	 */
8000*7c478bd9Sstevel@tonic-gate 	if (!(stp->sd_svcflags & (STRS_WILLSERVICE | STRS_SCHEDULED))) {
8001*7c478bd9Sstevel@tonic-gate 		/*
8002*7c478bd9Sstevel@tonic-gate 		 * No one will service this stream later, so we have to
8003*7c478bd9Sstevel@tonic-gate 		 * schedule it now.
8004*7c478bd9Sstevel@tonic-gate 		 */
8005*7c478bd9Sstevel@tonic-gate 		STRSTAT(stenables);
8006*7c478bd9Sstevel@tonic-gate 		stp->sd_svcflags |= STRS_SCHEDULED;
8007*7c478bd9Sstevel@tonic-gate 		stp->sd_servid = (void *)taskq_dispatch(streams_taskq,
8008*7c478bd9Sstevel@tonic-gate 		    (task_func_t *)stream_service, stp, TQ_NOSLEEP|TQ_NOQUEUE);
8009*7c478bd9Sstevel@tonic-gate 
8010*7c478bd9Sstevel@tonic-gate 		if (stp->sd_servid == NULL) {
8011*7c478bd9Sstevel@tonic-gate 			/*
8012*7c478bd9Sstevel@tonic-gate 			 * Task queue failed so fail over to the backup
8013*7c478bd9Sstevel@tonic-gate 			 * servicing thread.
8014*7c478bd9Sstevel@tonic-gate 			 */
8015*7c478bd9Sstevel@tonic-gate 			STRSTAT(taskqfails);
8016*7c478bd9Sstevel@tonic-gate 			/*
8017*7c478bd9Sstevel@tonic-gate 			 * It is safe to clear STRS_SCHEDULED flag because it
8018*7c478bd9Sstevel@tonic-gate 			 * was set by this thread above.
8019*7c478bd9Sstevel@tonic-gate 			 */
8020*7c478bd9Sstevel@tonic-gate 			stp->sd_svcflags &= ~STRS_SCHEDULED;
8021*7c478bd9Sstevel@tonic-gate 
8022*7c478bd9Sstevel@tonic-gate 			/*
8023*7c478bd9Sstevel@tonic-gate 			 * Failover scheduling is protected by service_queue
8024*7c478bd9Sstevel@tonic-gate 			 * lock.
8025*7c478bd9Sstevel@tonic-gate 			 */
8026*7c478bd9Sstevel@tonic-gate 			mutex_enter(&service_queue);
8027*7c478bd9Sstevel@tonic-gate 			ASSERT((stp->sd_qhead == q) && (stp->sd_qtail == q));
8028*7c478bd9Sstevel@tonic-gate 			ASSERT(q->q_link == NULL);
8029*7c478bd9Sstevel@tonic-gate 			/*
8030*7c478bd9Sstevel@tonic-gate 			 * Append the queue to qhead/qtail list.
8031*7c478bd9Sstevel@tonic-gate 			 */
8032*7c478bd9Sstevel@tonic-gate 			if (qhead == NULL)
8033*7c478bd9Sstevel@tonic-gate 				qhead = q;
8034*7c478bd9Sstevel@tonic-gate 			else
8035*7c478bd9Sstevel@tonic-gate 				qtail->q_link = q;
8036*7c478bd9Sstevel@tonic-gate 			qtail = q;
8037*7c478bd9Sstevel@tonic-gate 			/*
8038*7c478bd9Sstevel@tonic-gate 			 * Clear stp queue list.
8039*7c478bd9Sstevel@tonic-gate 			 */
8040*7c478bd9Sstevel@tonic-gate 			stp->sd_qhead = stp->sd_qtail = NULL;
8041*7c478bd9Sstevel@tonic-gate 			stp->sd_nqueues = 0;
8042*7c478bd9Sstevel@tonic-gate 			/*
8043*7c478bd9Sstevel@tonic-gate 			 * Wakeup background queue processing thread.
8044*7c478bd9Sstevel@tonic-gate 			 */
8045*7c478bd9Sstevel@tonic-gate 			cv_signal(&services_to_run);
8046*7c478bd9Sstevel@tonic-gate 			mutex_exit(&service_queue);
8047*7c478bd9Sstevel@tonic-gate 		}
8048*7c478bd9Sstevel@tonic-gate 	}
8049*7c478bd9Sstevel@tonic-gate 	mutex_exit(&stp->sd_qlock);
8050*7c478bd9Sstevel@tonic-gate }
8051*7c478bd9Sstevel@tonic-gate 
8052*7c478bd9Sstevel@tonic-gate static void
8053*7c478bd9Sstevel@tonic-gate queue_service(queue_t *q)
8054*7c478bd9Sstevel@tonic-gate {
8055*7c478bd9Sstevel@tonic-gate 	/*
8056*7c478bd9Sstevel@tonic-gate 	 * The queue in the list should have
8057*7c478bd9Sstevel@tonic-gate 	 * QENAB flag set and should not have
8058*7c478bd9Sstevel@tonic-gate 	 * QINSERVICE flag set. QINSERVICE is
8059*7c478bd9Sstevel@tonic-gate 	 * set when the queue is dequeued and
8060*7c478bd9Sstevel@tonic-gate 	 * qenable_locked doesn't enqueue a
8061*7c478bd9Sstevel@tonic-gate 	 * queue with QINSERVICE set.
8062*7c478bd9Sstevel@tonic-gate 	 */
8063*7c478bd9Sstevel@tonic-gate 
8064*7c478bd9Sstevel@tonic-gate 	ASSERT(!(q->q_flag & QINSERVICE));
8065*7c478bd9Sstevel@tonic-gate 	ASSERT((q->q_flag & QENAB));
8066*7c478bd9Sstevel@tonic-gate 	mutex_enter(QLOCK(q));
8067*7c478bd9Sstevel@tonic-gate 	q->q_flag &= ~QENAB;
8068*7c478bd9Sstevel@tonic-gate 	q->q_flag |= QINSERVICE;
8069*7c478bd9Sstevel@tonic-gate 	mutex_exit(QLOCK(q));
8070*7c478bd9Sstevel@tonic-gate 	runservice(q);
8071*7c478bd9Sstevel@tonic-gate }
8072*7c478bd9Sstevel@tonic-gate 
8073*7c478bd9Sstevel@tonic-gate static void
8074*7c478bd9Sstevel@tonic-gate syncq_service(syncq_t *sq)
8075*7c478bd9Sstevel@tonic-gate {
8076*7c478bd9Sstevel@tonic-gate 	STRSTAT(syncqservice);
8077*7c478bd9Sstevel@tonic-gate 	mutex_enter(SQLOCK(sq));
8078*7c478bd9Sstevel@tonic-gate 	ASSERT(!(sq->sq_svcflags & SQ_SERVICE));
8079*7c478bd9Sstevel@tonic-gate 	ASSERT(sq->sq_servcount != 0);
8080*7c478bd9Sstevel@tonic-gate 	ASSERT(sq->sq_next == NULL);
8081*7c478bd9Sstevel@tonic-gate 
8082*7c478bd9Sstevel@tonic-gate 	/* if we came here from the background thread, clear the flag */
8083*7c478bd9Sstevel@tonic-gate 	if (sq->sq_svcflags & SQ_BGTHREAD)
8084*7c478bd9Sstevel@tonic-gate 		sq->sq_svcflags &= ~SQ_BGTHREAD;
8085*7c478bd9Sstevel@tonic-gate 
8086*7c478bd9Sstevel@tonic-gate 	/* let drain_syncq know that it's being called in the background */
8087*7c478bd9Sstevel@tonic-gate 	sq->sq_svcflags |= SQ_SERVICE;
8088*7c478bd9Sstevel@tonic-gate 	drain_syncq(sq);
8089*7c478bd9Sstevel@tonic-gate }
8090*7c478bd9Sstevel@tonic-gate 
8091*7c478bd9Sstevel@tonic-gate static void
8092*7c478bd9Sstevel@tonic-gate qwriter_outer_service(syncq_t *outer)
8093*7c478bd9Sstevel@tonic-gate {
8094*7c478bd9Sstevel@tonic-gate 	/*
8095*7c478bd9Sstevel@tonic-gate 	 * Note that SQ_WRITER is used on the outer perimeter
8096*7c478bd9Sstevel@tonic-gate 	 * to signal that a qwriter(OUTER) is either investigating
8097*7c478bd9Sstevel@tonic-gate 	 * running or that it is actually running a function.
8098*7c478bd9Sstevel@tonic-gate 	 */
8099*7c478bd9Sstevel@tonic-gate 	outer_enter(outer, SQ_BLOCKED|SQ_WRITER);
8100*7c478bd9Sstevel@tonic-gate 
8101*7c478bd9Sstevel@tonic-gate 	/*
8102*7c478bd9Sstevel@tonic-gate 	 * All inner syncq are empty and have SQ_WRITER set
8103*7c478bd9Sstevel@tonic-gate 	 * to block entering the outer perimeter.
8104*7c478bd9Sstevel@tonic-gate 	 *
8105*7c478bd9Sstevel@tonic-gate 	 * We do not need to explicitly call write_now since
8106*7c478bd9Sstevel@tonic-gate 	 * outer_exit does it for us.
8107*7c478bd9Sstevel@tonic-gate 	 */
8108*7c478bd9Sstevel@tonic-gate 	outer_exit(outer);
8109*7c478bd9Sstevel@tonic-gate }
8110*7c478bd9Sstevel@tonic-gate 
8111*7c478bd9Sstevel@tonic-gate static void
8112*7c478bd9Sstevel@tonic-gate mblk_free(mblk_t *mp)
8113*7c478bd9Sstevel@tonic-gate {
8114*7c478bd9Sstevel@tonic-gate 	dblk_t *dbp = mp->b_datap;
8115*7c478bd9Sstevel@tonic-gate 	frtn_t *frp = dbp->db_frtnp;
8116*7c478bd9Sstevel@tonic-gate 
8117*7c478bd9Sstevel@tonic-gate 	mp->b_next = NULL;
8118*7c478bd9Sstevel@tonic-gate 	if (dbp->db_fthdr != NULL)
8119*7c478bd9Sstevel@tonic-gate 		str_ftfree(dbp);
8120*7c478bd9Sstevel@tonic-gate 
8121*7c478bd9Sstevel@tonic-gate 	ASSERT(dbp->db_fthdr == NULL);
8122*7c478bd9Sstevel@tonic-gate 	frp->free_func(frp->free_arg);
8123*7c478bd9Sstevel@tonic-gate 	ASSERT(dbp->db_mblk == mp);
8124*7c478bd9Sstevel@tonic-gate 
8125*7c478bd9Sstevel@tonic-gate 	if (dbp->db_credp != NULL) {
8126*7c478bd9Sstevel@tonic-gate 		crfree(dbp->db_credp);
8127*7c478bd9Sstevel@tonic-gate 		dbp->db_credp = NULL;
8128*7c478bd9Sstevel@tonic-gate 	}
8129*7c478bd9Sstevel@tonic-gate 	dbp->db_cpid = -1;
8130*7c478bd9Sstevel@tonic-gate 	dbp->db_struioflag = 0;
8131*7c478bd9Sstevel@tonic-gate 	dbp->db_struioun.cksum.flags = 0;
8132*7c478bd9Sstevel@tonic-gate 
8133*7c478bd9Sstevel@tonic-gate 	kmem_cache_free(dbp->db_cache, dbp);
8134*7c478bd9Sstevel@tonic-gate }
8135*7c478bd9Sstevel@tonic-gate 
8136*7c478bd9Sstevel@tonic-gate /*
8137*7c478bd9Sstevel@tonic-gate  * Background processing of the stream queue list.
8138*7c478bd9Sstevel@tonic-gate  */
8139*7c478bd9Sstevel@tonic-gate static void
8140*7c478bd9Sstevel@tonic-gate stream_service(stdata_t *stp)
8141*7c478bd9Sstevel@tonic-gate {
8142*7c478bd9Sstevel@tonic-gate 	queue_t *q;
8143*7c478bd9Sstevel@tonic-gate 
8144*7c478bd9Sstevel@tonic-gate 	mutex_enter(&stp->sd_qlock);
8145*7c478bd9Sstevel@tonic-gate 
8146*7c478bd9Sstevel@tonic-gate 	STR_SERVICE(stp, q);
8147*7c478bd9Sstevel@tonic-gate 
8148*7c478bd9Sstevel@tonic-gate 	stp->sd_svcflags &= ~STRS_SCHEDULED;
8149*7c478bd9Sstevel@tonic-gate 	stp->sd_servid = NULL;
8150*7c478bd9Sstevel@tonic-gate 	cv_signal(&stp->sd_qcv);
8151*7c478bd9Sstevel@tonic-gate 	mutex_exit(&stp->sd_qlock);
8152*7c478bd9Sstevel@tonic-gate }
8153*7c478bd9Sstevel@tonic-gate 
8154*7c478bd9Sstevel@tonic-gate /*
8155*7c478bd9Sstevel@tonic-gate  * Foreground processing of the stream queue list.
8156*7c478bd9Sstevel@tonic-gate  */
8157*7c478bd9Sstevel@tonic-gate void
8158*7c478bd9Sstevel@tonic-gate stream_runservice(stdata_t *stp)
8159*7c478bd9Sstevel@tonic-gate {
8160*7c478bd9Sstevel@tonic-gate 	queue_t *q;
8161*7c478bd9Sstevel@tonic-gate 
8162*7c478bd9Sstevel@tonic-gate 	mutex_enter(&stp->sd_qlock);
8163*7c478bd9Sstevel@tonic-gate 	STRSTAT(rservice);
8164*7c478bd9Sstevel@tonic-gate 	/*
8165*7c478bd9Sstevel@tonic-gate 	 * We are going to drain this stream queue list, so qenable_locked will
8166*7c478bd9Sstevel@tonic-gate 	 * not schedule it until we finish.
8167*7c478bd9Sstevel@tonic-gate 	 */
8168*7c478bd9Sstevel@tonic-gate 	stp->sd_svcflags |= STRS_WILLSERVICE;
8169*7c478bd9Sstevel@tonic-gate 
8170*7c478bd9Sstevel@tonic-gate 	STR_SERVICE(stp, q);
8171*7c478bd9Sstevel@tonic-gate 
8172*7c478bd9Sstevel@tonic-gate 	stp->sd_svcflags &= ~STRS_WILLSERVICE;
8173*7c478bd9Sstevel@tonic-gate 	mutex_exit(&stp->sd_qlock);
8174*7c478bd9Sstevel@tonic-gate 	/*
8175*7c478bd9Sstevel@tonic-gate 	 * Help backup background thread to drain the qhead/qtail list.
8176*7c478bd9Sstevel@tonic-gate 	 */
8177*7c478bd9Sstevel@tonic-gate 	while (qhead != NULL) {
8178*7c478bd9Sstevel@tonic-gate 		STRSTAT(qhelps);
8179*7c478bd9Sstevel@tonic-gate 		mutex_enter(&service_queue);
8180*7c478bd9Sstevel@tonic-gate 		DQ(q, qhead, qtail, q_link);
8181*7c478bd9Sstevel@tonic-gate 		mutex_exit(&service_queue);
8182*7c478bd9Sstevel@tonic-gate 		if (q != NULL)
8183*7c478bd9Sstevel@tonic-gate 			queue_service(q);
8184*7c478bd9Sstevel@tonic-gate 	}
8185*7c478bd9Sstevel@tonic-gate }
8186*7c478bd9Sstevel@tonic-gate 
8187*7c478bd9Sstevel@tonic-gate void
8188*7c478bd9Sstevel@tonic-gate stream_willservice(stdata_t *stp)
8189*7c478bd9Sstevel@tonic-gate {
8190*7c478bd9Sstevel@tonic-gate 	mutex_enter(&stp->sd_qlock);
8191*7c478bd9Sstevel@tonic-gate 	stp->sd_svcflags |= STRS_WILLSERVICE;
8192*7c478bd9Sstevel@tonic-gate 	mutex_exit(&stp->sd_qlock);
8193*7c478bd9Sstevel@tonic-gate }
8194*7c478bd9Sstevel@tonic-gate 
8195*7c478bd9Sstevel@tonic-gate /*
8196*7c478bd9Sstevel@tonic-gate  * Replace the cred currently in the mblk with a different one.
8197*7c478bd9Sstevel@tonic-gate  */
8198*7c478bd9Sstevel@tonic-gate void
8199*7c478bd9Sstevel@tonic-gate mblk_setcred(mblk_t *mp, cred_t *cr)
8200*7c478bd9Sstevel@tonic-gate {
8201*7c478bd9Sstevel@tonic-gate 	cred_t *ocr = DB_CRED(mp);
8202*7c478bd9Sstevel@tonic-gate 
8203*7c478bd9Sstevel@tonic-gate 	ASSERT(cr != NULL);
8204*7c478bd9Sstevel@tonic-gate 
8205*7c478bd9Sstevel@tonic-gate 	if (cr != ocr) {
8206*7c478bd9Sstevel@tonic-gate 		crhold(mp->b_datap->db_credp = cr);
8207*7c478bd9Sstevel@tonic-gate 		if (ocr != NULL)
8208*7c478bd9Sstevel@tonic-gate 			crfree(ocr);
8209*7c478bd9Sstevel@tonic-gate 	}
8210*7c478bd9Sstevel@tonic-gate }
8211*7c478bd9Sstevel@tonic-gate 
8212*7c478bd9Sstevel@tonic-gate int
8213*7c478bd9Sstevel@tonic-gate hcksum_assoc(mblk_t *mp,  multidata_t *mmd, pdesc_t *pd,
8214*7c478bd9Sstevel@tonic-gate     uint32_t start, uint32_t stuff, uint32_t end, uint32_t value,
8215*7c478bd9Sstevel@tonic-gate     uint32_t flags, int km_flags)
8216*7c478bd9Sstevel@tonic-gate {
8217*7c478bd9Sstevel@tonic-gate 	int rc = 0;
8218*7c478bd9Sstevel@tonic-gate 
8219*7c478bd9Sstevel@tonic-gate 	ASSERT(DB_TYPE(mp) == M_DATA || DB_TYPE(mp) == M_MULTIDATA);
8220*7c478bd9Sstevel@tonic-gate 	if (mp->b_datap->db_type == M_DATA) {
8221*7c478bd9Sstevel@tonic-gate 		/* Associate values for M_DATA type */
8222*7c478bd9Sstevel@tonic-gate 		mp->b_datap->db_cksumstart = (intptr_t)start;
8223*7c478bd9Sstevel@tonic-gate 		mp->b_datap->db_cksumstuff = (intptr_t)stuff;
8224*7c478bd9Sstevel@tonic-gate 		mp->b_datap->db_cksumend = (intptr_t)end;
8225*7c478bd9Sstevel@tonic-gate 		mp->b_datap->db_struioun.cksum.flags = flags;
8226*7c478bd9Sstevel@tonic-gate 		mp->b_datap->db_cksum16 = (uint16_t)value;
8227*7c478bd9Sstevel@tonic-gate 
8228*7c478bd9Sstevel@tonic-gate 	} else {
8229*7c478bd9Sstevel@tonic-gate 		pattrinfo_t pa_info;
8230*7c478bd9Sstevel@tonic-gate 
8231*7c478bd9Sstevel@tonic-gate 		ASSERT(mmd != NULL);
8232*7c478bd9Sstevel@tonic-gate 
8233*7c478bd9Sstevel@tonic-gate 		pa_info.type = PATTR_HCKSUM;
8234*7c478bd9Sstevel@tonic-gate 		pa_info.len = sizeof (pattr_hcksum_t);
8235*7c478bd9Sstevel@tonic-gate 
8236*7c478bd9Sstevel@tonic-gate 		if (mmd_addpattr(mmd, pd, &pa_info, B_TRUE, km_flags) != NULL) {
8237*7c478bd9Sstevel@tonic-gate 			pattr_hcksum_t *hck = (pattr_hcksum_t *)pa_info.buf;
8238*7c478bd9Sstevel@tonic-gate 
8239*7c478bd9Sstevel@tonic-gate 			hck->hcksum_start_offset = start;
8240*7c478bd9Sstevel@tonic-gate 			hck->hcksum_stuff_offset = stuff;
8241*7c478bd9Sstevel@tonic-gate 			hck->hcksum_end_offset = end;
8242*7c478bd9Sstevel@tonic-gate 			hck->hcksum_cksum_val.inet_cksum = (uint16_t)value;
8243*7c478bd9Sstevel@tonic-gate 			hck->hcksum_flags = flags;
8244*7c478bd9Sstevel@tonic-gate 		}
8245*7c478bd9Sstevel@tonic-gate 	}
8246*7c478bd9Sstevel@tonic-gate 	return (rc);
8247*7c478bd9Sstevel@tonic-gate }
8248*7c478bd9Sstevel@tonic-gate 
8249*7c478bd9Sstevel@tonic-gate void
8250*7c478bd9Sstevel@tonic-gate hcksum_retrieve(mblk_t *mp, multidata_t *mmd, pdesc_t *pd,
8251*7c478bd9Sstevel@tonic-gate     uint32_t *start, uint32_t *stuff, uint32_t *end,
8252*7c478bd9Sstevel@tonic-gate     uint32_t *value, uint32_t *flags)
8253*7c478bd9Sstevel@tonic-gate {
8254*7c478bd9Sstevel@tonic-gate 	ASSERT(DB_TYPE(mp) == M_DATA || DB_TYPE(mp) == M_MULTIDATA);
8255*7c478bd9Sstevel@tonic-gate 	if (mp->b_datap->db_type == M_DATA) {
8256*7c478bd9Sstevel@tonic-gate 		if (flags != NULL) {
8257*7c478bd9Sstevel@tonic-gate 			*flags = mp->b_datap->db_struioun.cksum.flags;
8258*7c478bd9Sstevel@tonic-gate 			if (*flags & HCK_PARTIALCKSUM) {
8259*7c478bd9Sstevel@tonic-gate 				if (start != NULL)
8260*7c478bd9Sstevel@tonic-gate 					*start = (uint32_t)
8261*7c478bd9Sstevel@tonic-gate 					    mp->b_datap->db_cksumstart;
8262*7c478bd9Sstevel@tonic-gate 				if (stuff != NULL)
8263*7c478bd9Sstevel@tonic-gate 					*stuff = (uint32_t)
8264*7c478bd9Sstevel@tonic-gate 					    mp->b_datap->db_cksumstuff;
8265*7c478bd9Sstevel@tonic-gate 				if (end != NULL)
8266*7c478bd9Sstevel@tonic-gate 					*end =
8267*7c478bd9Sstevel@tonic-gate 					    (uint32_t)mp->b_datap->db_cksumend;
8268*7c478bd9Sstevel@tonic-gate 				if (value != NULL)
8269*7c478bd9Sstevel@tonic-gate 					*value =
8270*7c478bd9Sstevel@tonic-gate 					    (uint32_t)mp->b_datap->db_cksum16;
8271*7c478bd9Sstevel@tonic-gate 			}
8272*7c478bd9Sstevel@tonic-gate 		}
8273*7c478bd9Sstevel@tonic-gate 	} else {
8274*7c478bd9Sstevel@tonic-gate 		pattrinfo_t hck_attr = {PATTR_HCKSUM};
8275*7c478bd9Sstevel@tonic-gate 
8276*7c478bd9Sstevel@tonic-gate 		ASSERT(mmd != NULL);
8277*7c478bd9Sstevel@tonic-gate 
8278*7c478bd9Sstevel@tonic-gate 		/* get hardware checksum attribute */
8279*7c478bd9Sstevel@tonic-gate 		if (mmd_getpattr(mmd, pd, &hck_attr) != NULL) {
8280*7c478bd9Sstevel@tonic-gate 			pattr_hcksum_t *hck = (pattr_hcksum_t *)hck_attr.buf;
8281*7c478bd9Sstevel@tonic-gate 
8282*7c478bd9Sstevel@tonic-gate 			ASSERT(hck_attr.len >= sizeof (pattr_hcksum_t));
8283*7c478bd9Sstevel@tonic-gate 			if (flags != NULL)
8284*7c478bd9Sstevel@tonic-gate 				*flags = hck->hcksum_flags;
8285*7c478bd9Sstevel@tonic-gate 			if (start != NULL)
8286*7c478bd9Sstevel@tonic-gate 				*start = hck->hcksum_start_offset;
8287*7c478bd9Sstevel@tonic-gate 			if (stuff != NULL)
8288*7c478bd9Sstevel@tonic-gate 				*stuff = hck->hcksum_stuff_offset;
8289*7c478bd9Sstevel@tonic-gate 			if (end != NULL)
8290*7c478bd9Sstevel@tonic-gate 				*end = hck->hcksum_end_offset;
8291*7c478bd9Sstevel@tonic-gate 			if (value != NULL)
8292*7c478bd9Sstevel@tonic-gate 				*value = (uint32_t)
8293*7c478bd9Sstevel@tonic-gate 				    hck->hcksum_cksum_val.inet_cksum;
8294*7c478bd9Sstevel@tonic-gate 		}
8295*7c478bd9Sstevel@tonic-gate 	}
8296*7c478bd9Sstevel@tonic-gate }
8297*7c478bd9Sstevel@tonic-gate 
8298*7c478bd9Sstevel@tonic-gate /*
8299*7c478bd9Sstevel@tonic-gate  * Checksum buffer *bp for len bytes with psum partial checksum,
8300*7c478bd9Sstevel@tonic-gate  * or 0 if none, and return the 16 bit partial checksum.
8301*7c478bd9Sstevel@tonic-gate  */
8302*7c478bd9Sstevel@tonic-gate unsigned
8303*7c478bd9Sstevel@tonic-gate bcksum(uchar_t *bp, int len, unsigned int psum)
8304*7c478bd9Sstevel@tonic-gate {
8305*7c478bd9Sstevel@tonic-gate 	int odd = len & 1;
8306*7c478bd9Sstevel@tonic-gate 	extern unsigned int ip_ocsum();
8307*7c478bd9Sstevel@tonic-gate 
8308*7c478bd9Sstevel@tonic-gate 	if (((intptr_t)bp & 1) == 0 && !odd) {
8309*7c478bd9Sstevel@tonic-gate 		/*
8310*7c478bd9Sstevel@tonic-gate 		 * Bp is 16 bit aligned and len is multiple of 16 bit word.
8311*7c478bd9Sstevel@tonic-gate 		 */
8312*7c478bd9Sstevel@tonic-gate 		return (ip_ocsum((ushort_t *)bp, len >> 1, psum));
8313*7c478bd9Sstevel@tonic-gate 	}
8314*7c478bd9Sstevel@tonic-gate 	if (((intptr_t)bp & 1) != 0) {
8315*7c478bd9Sstevel@tonic-gate 		/*
8316*7c478bd9Sstevel@tonic-gate 		 * Bp isn't 16 bit aligned.
8317*7c478bd9Sstevel@tonic-gate 		 */
8318*7c478bd9Sstevel@tonic-gate 		unsigned int tsum;
8319*7c478bd9Sstevel@tonic-gate 
8320*7c478bd9Sstevel@tonic-gate #ifdef _LITTLE_ENDIAN
8321*7c478bd9Sstevel@tonic-gate 		psum += *bp;
8322*7c478bd9Sstevel@tonic-gate #else
8323*7c478bd9Sstevel@tonic-gate 		psum += *bp << 8;
8324*7c478bd9Sstevel@tonic-gate #endif
8325*7c478bd9Sstevel@tonic-gate 		len--;
8326*7c478bd9Sstevel@tonic-gate 		bp++;
8327*7c478bd9Sstevel@tonic-gate 		tsum = ip_ocsum((ushort_t *)bp, len >> 1, 0);
8328*7c478bd9Sstevel@tonic-gate 		psum += (tsum << 8) & 0xffff | (tsum >> 8);
8329*7c478bd9Sstevel@tonic-gate 		if (len & 1) {
8330*7c478bd9Sstevel@tonic-gate 			bp += len - 1;
8331*7c478bd9Sstevel@tonic-gate #ifdef _LITTLE_ENDIAN
8332*7c478bd9Sstevel@tonic-gate 			psum += *bp << 8;
8333*7c478bd9Sstevel@tonic-gate #else
8334*7c478bd9Sstevel@tonic-gate 			psum += *bp;
8335*7c478bd9Sstevel@tonic-gate #endif
8336*7c478bd9Sstevel@tonic-gate 		}
8337*7c478bd9Sstevel@tonic-gate 	} else {
8338*7c478bd9Sstevel@tonic-gate 		/*
8339*7c478bd9Sstevel@tonic-gate 		 * Bp is 16 bit aligned.
8340*7c478bd9Sstevel@tonic-gate 		 */
8341*7c478bd9Sstevel@tonic-gate 		psum = ip_ocsum((ushort_t *)bp, len >> 1, psum);
8342*7c478bd9Sstevel@tonic-gate 		if (odd) {
8343*7c478bd9Sstevel@tonic-gate 			bp += len - 1;
8344*7c478bd9Sstevel@tonic-gate #ifdef _LITTLE_ENDIAN
8345*7c478bd9Sstevel@tonic-gate 			psum += *bp;
8346*7c478bd9Sstevel@tonic-gate #else
8347*7c478bd9Sstevel@tonic-gate 			psum += *bp << 8;
8348*7c478bd9Sstevel@tonic-gate #endif
8349*7c478bd9Sstevel@tonic-gate 		}
8350*7c478bd9Sstevel@tonic-gate 	}
8351*7c478bd9Sstevel@tonic-gate 	/*
8352*7c478bd9Sstevel@tonic-gate 	 * Normalize psum to 16 bits before returning the new partial
8353*7c478bd9Sstevel@tonic-gate 	 * checksum. The max psum value before normalization is 0x3FDFE.
8354*7c478bd9Sstevel@tonic-gate 	 */
8355*7c478bd9Sstevel@tonic-gate 	return ((psum >> 16) + (psum & 0xFFFF));
8356*7c478bd9Sstevel@tonic-gate }
8357*7c478bd9Sstevel@tonic-gate 
8358*7c478bd9Sstevel@tonic-gate boolean_t
8359*7c478bd9Sstevel@tonic-gate is_vmloaned_mblk(mblk_t *mp, multidata_t *mmd, pdesc_t *pd)
8360*7c478bd9Sstevel@tonic-gate {
8361*7c478bd9Sstevel@tonic-gate 	boolean_t rc;
8362*7c478bd9Sstevel@tonic-gate 
8363*7c478bd9Sstevel@tonic-gate 	ASSERT(DB_TYPE(mp) == M_DATA || DB_TYPE(mp) == M_MULTIDATA);
8364*7c478bd9Sstevel@tonic-gate 	if (DB_TYPE(mp) == M_DATA) {
8365*7c478bd9Sstevel@tonic-gate 		rc = (((mp)->b_datap->db_struioflag & STRUIO_ZC) != 0);
8366*7c478bd9Sstevel@tonic-gate 	} else {
8367*7c478bd9Sstevel@tonic-gate 		pattrinfo_t zcopy_attr = {PATTR_ZCOPY};
8368*7c478bd9Sstevel@tonic-gate 
8369*7c478bd9Sstevel@tonic-gate 		ASSERT(mmd != NULL);
8370*7c478bd9Sstevel@tonic-gate 		rc = (mmd_getpattr(mmd, pd, &zcopy_attr) != NULL);
8371*7c478bd9Sstevel@tonic-gate 	}
8372*7c478bd9Sstevel@tonic-gate 	return (rc);
8373*7c478bd9Sstevel@tonic-gate }
8374*7c478bd9Sstevel@tonic-gate 
8375*7c478bd9Sstevel@tonic-gate void
8376*7c478bd9Sstevel@tonic-gate freemsgchain(mblk_t *mp)
8377*7c478bd9Sstevel@tonic-gate {
8378*7c478bd9Sstevel@tonic-gate 	mblk_t	*next;
8379*7c478bd9Sstevel@tonic-gate 
8380*7c478bd9Sstevel@tonic-gate 	while (mp != NULL) {
8381*7c478bd9Sstevel@tonic-gate 		next = mp->b_next;
8382*7c478bd9Sstevel@tonic-gate 		mp->b_next = NULL;
8383*7c478bd9Sstevel@tonic-gate 
8384*7c478bd9Sstevel@tonic-gate 		freemsg(mp);
8385*7c478bd9Sstevel@tonic-gate 		mp = next;
8386*7c478bd9Sstevel@tonic-gate 	}
8387*7c478bd9Sstevel@tonic-gate }
8388*7c478bd9Sstevel@tonic-gate 
8389*7c478bd9Sstevel@tonic-gate mblk_t *
8390*7c478bd9Sstevel@tonic-gate copymsgchain(mblk_t *mp)
8391*7c478bd9Sstevel@tonic-gate {
8392*7c478bd9Sstevel@tonic-gate 	mblk_t	*nmp = NULL;
8393*7c478bd9Sstevel@tonic-gate 	mblk_t	**nmpp = &nmp;
8394*7c478bd9Sstevel@tonic-gate 
8395*7c478bd9Sstevel@tonic-gate 	for (; mp != NULL; mp = mp->b_next) {
8396*7c478bd9Sstevel@tonic-gate 		if ((*nmpp = copymsg(mp)) == NULL) {
8397*7c478bd9Sstevel@tonic-gate 			freemsgchain(nmp);
8398*7c478bd9Sstevel@tonic-gate 			return (NULL);
8399*7c478bd9Sstevel@tonic-gate 		}
8400*7c478bd9Sstevel@tonic-gate 
8401*7c478bd9Sstevel@tonic-gate 		nmpp = &((*nmpp)->b_next);
8402*7c478bd9Sstevel@tonic-gate 	}
8403*7c478bd9Sstevel@tonic-gate 
8404*7c478bd9Sstevel@tonic-gate 	return (nmp);
8405*7c478bd9Sstevel@tonic-gate }
8406*7c478bd9Sstevel@tonic-gate 
8407*7c478bd9Sstevel@tonic-gate /* NOTE: Do not add code after this point. */
8408*7c478bd9Sstevel@tonic-gate #undef QLOCK
8409*7c478bd9Sstevel@tonic-gate 
8410*7c478bd9Sstevel@tonic-gate /*
8411*7c478bd9Sstevel@tonic-gate  * replacement for QLOCK macro for those that can't use it.
8412*7c478bd9Sstevel@tonic-gate  */
8413*7c478bd9Sstevel@tonic-gate kmutex_t *
8414*7c478bd9Sstevel@tonic-gate QLOCK(queue_t *q)
8415*7c478bd9Sstevel@tonic-gate {
8416*7c478bd9Sstevel@tonic-gate 	return (&(q)->q_lock);
8417*7c478bd9Sstevel@tonic-gate }
8418*7c478bd9Sstevel@tonic-gate 
8419*7c478bd9Sstevel@tonic-gate /*
8420*7c478bd9Sstevel@tonic-gate  * Dummy runqueues/queuerun functions functions for backwards compatibility.
8421*7c478bd9Sstevel@tonic-gate  */
8422*7c478bd9Sstevel@tonic-gate #undef runqueues
8423*7c478bd9Sstevel@tonic-gate void
8424*7c478bd9Sstevel@tonic-gate runqueues(void)
8425*7c478bd9Sstevel@tonic-gate {
8426*7c478bd9Sstevel@tonic-gate }
8427*7c478bd9Sstevel@tonic-gate 
8428*7c478bd9Sstevel@tonic-gate #undef queuerun
8429*7c478bd9Sstevel@tonic-gate void
8430*7c478bd9Sstevel@tonic-gate queuerun(void)
8431*7c478bd9Sstevel@tonic-gate {
8432*7c478bd9Sstevel@tonic-gate }
8433