xref: /titanic_52/usr/src/uts/common/sys/strsubr.h (revision b60f2a0b921611326383e4789e0874e9e8a2e708)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
22 /*	  All Rights Reserved  	*/
23 
24 
25 /*
26  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
27  * Use is subject to license terms.
28  */
29 
30 #ifndef _SYS_STRSUBR_H
31 #define	_SYS_STRSUBR_H
32 
33 #pragma ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.17 */
34 
35 /*
36  * WARNING:
37  * Everything in this file is private, belonging to the
38  * STREAMS subsystem.  The only guarantee made about the
39  * contents of this file is that if you include it, your
40  * code will not port to the next release.
41  */
42 #include <sys/stream.h>
43 #include <sys/stropts.h>
44 #include <sys/kstat.h>
45 #include <sys/uio.h>
46 #include <sys/proc.h>
47 #include <sys/netstack.h>
48 #include <sys/modhash.h>
49 
50 #ifdef	__cplusplus
51 extern "C" {
52 #endif
53 
54 /*
55  * In general, the STREAMS locks are disjoint; they are only held
56  * locally, and not simultaneously by a thread.  However, module
57  * code, including at the stream head, requires some locks to be
58  * acquired in order for its safety.
59  *	1. Stream level claim.  This prevents the value of q_next
60  *		from changing while module code is executing.
61  *	2. Queue level claim.  This prevents the value of q_ptr
62  *		from changing while put or service code is executing.
63  *		In addition, it provides for queue single-threading
64  *		for QPAIR and PERQ MT-safe modules.
65  *	3. Stream head lock.  May be held by the stream head module
66  *		to implement a read/write/open/close monitor.
67  *	   Note: that the only types of twisted stream supported are
68  *	   the pipe and transports which have read and write service
69  *	   procedures on both sides of the twist.
70  *	4. Queue lock.  May be acquired by utility routines on
71  *		behalf of a module.
72  */
73 
74 /*
75  * In general, sd_lock protects the consistency of the stdata
76  * structure.  Additionally, it is used with sd_monitor
77  * to implement an open/close monitor.  In particular, it protects
78  * the following fields:
79  *	sd_iocblk
80  *	sd_flag
81  *	sd_copyflag
82  *	sd_iocid
83  *	sd_iocwait
84  *	sd_sidp
85  *	sd_pgidp
86  *	sd_wroff
87  *	sd_tail
88  *	sd_rerror
89  *	sd_werror
90  *	sd_pushcnt
91  *	sd_sigflags
92  *	sd_siglist
93  *	sd_pollist
94  *	sd_mark
95  *	sd_closetime
96  *	sd_wakeq
97  *	sd_uiordq
98  *	sd_uiowrq
99  *	sd_maxblk
100  *
101  * The following fields are modified only by the allocator, which
102  * has exclusive access to them at that time:
103  *	sd_wrq
104  *	sd_strtab
105  *
106  * The following field is protected by the overlying file system
107  * code, guaranteeing single-threading of opens:
108  *	sd_vnode
109  *
110  * Stream-level locks should be acquired before any queue-level locks
111  *	are acquired.
112  *
113  * The stream head write queue lock(sd_wrq) is used to protect the
114  * fields qn_maxpsz and qn_minpsz because freezestr() which is
115  * necessary for strqset() only gets the queue lock.
116  */
117 
118 /*
119  * Function types for the parameterized stream head.
120  * The msgfunc_t takes the parameters:
121  * 	msgfunc(vnode_t *vp, mblk_t *mp, strwakeup_t *wakeups,
122  *		strsigset_t *firstmsgsigs, strsigset_t *allmsgsigs,
123  *		strpollset_t *pollwakeups);
124  * It returns an optional message to be processed by the stream head.
125  *
126  * The parameters for errfunc_t are:
127  *	errfunc(vnode *vp, int ispeek, int *clearerr);
128  * It returns an errno and zero if there was no pending error.
129  */
130 typedef uint_t	strwakeup_t;
131 typedef uint_t	strsigset_t;
132 typedef short	strpollset_t;
133 typedef uintptr_t callbparams_id_t;
134 typedef	mblk_t	*(*msgfunc_t)(vnode_t *, mblk_t *, strwakeup_t *,
135 			strsigset_t *, strsigset_t *, strpollset_t *);
136 typedef int 	(*errfunc_t)(vnode_t *, int, int *);
137 
138 /*
139  * Per stream sd_lock in putnext may be replaced by per cpu stream_putlocks
140  * each living in a separate cache line. putnext/canputnext grabs only one of
141  * stream_putlocks while strlock() (called on behalf of insertq()/removeq())
142  * acquires all stream_putlocks. Normally stream_putlocks are only employed
143  * for highly contended streams that have SQ_CIPUT queues in the critical path
144  * (e.g. NFS/UDP stream).
145  *
146  * stream_putlocks are dynamically assigned to stdata structure through
147  * sd_ciputctrl pointer possibly when a stream is already in use. Since
148  * strlock() uses stream_putlocks only under sd_lock acquiring sd_lock when
149  * assigning stream_putlocks to the stream ensures synchronization with
150  * strlock().
151  *
152  * For lock ordering purposes stream_putlocks are treated as the extension of
153  * sd_lock and are always grabbed right after grabbing sd_lock and released
154  * right before releasing sd_lock except putnext/canputnext where only one of
155  * stream_putlocks locks is used and where it is the first lock to grab.
156  */
157 
158 typedef struct ciputctrl_str {
159 	union _ciput_un {
160 		uchar_t	pad[64];
161 		struct _ciput_str {
162 			kmutex_t	ciput_lck;
163 			ushort_t	ciput_cnt;
164 		} ciput_str;
165 	} ciput_un;
166 } ciputctrl_t;
167 
168 #define	ciputctrl_lock	ciput_un.ciput_str.ciput_lck
169 #define	ciputctrl_count	ciput_un.ciput_str.ciput_cnt
170 
171 /*
172  * Header for a stream: interface to rest of system.
173  *
174  * NOTE: While this is a consolidation-private structure, some unbundled and
175  *       third-party products inappropriately make use of some of the fields.
176  *       As such, please take care to not gratuitously change any offsets of
177  *       existing members.
178  */
179 typedef struct stdata {
180 	struct queue	*sd_wrq;	/* write queue */
181 	struct msgb	*sd_iocblk;	/* return block for ioctl */
182 	struct vnode	*sd_vnode;	/* pointer to associated vnode */
183 	struct streamtab *sd_strtab;	/* pointer to streamtab for stream */
184 	uint_t		sd_flag;	/* state/flags */
185 	uint_t		sd_iocid;	/* ioctl id */
186 	struct pid	*sd_sidp;	/* controlling session info */
187 	struct pid	*sd_pgidp;	/* controlling process group info */
188 	ushort_t	sd_tail;	/* reserved space in written mblks */
189 	ushort_t	sd_wroff;	/* write offset */
190 	int		sd_rerror;	/* error to return on read ops */
191 	int		sd_werror;	/* error to return on write ops */
192 	int		sd_pushcnt;	/* number of pushes done on stream */
193 	int		sd_sigflags;	/* logical OR of all siglist events */
194 	struct strsig	*sd_siglist;	/* pid linked list to rcv SIGPOLL sig */
195 	struct pollhead sd_pollist;	/* list of all pollers to wake up */
196 	struct msgb	*sd_mark;	/* "marked" message on read queue */
197 	clock_t		sd_closetime;	/* time to wait to drain q in close */
198 	kmutex_t	sd_lock;	/* protect head consistency */
199 	kcondvar_t	sd_monitor;	/* open/close/push/pop monitor */
200 	kcondvar_t	sd_iocmonitor;	/* ioctl single-threading */
201 	kcondvar_t	sd_refmonitor;	/* sd_refcnt monitor */
202 	ssize_t		sd_qn_minpsz;	/* These two fields are a performance */
203 	ssize_t		sd_qn_maxpsz;	/* enhancements, cache the values in */
204 					/* the stream head so we don't have */
205 					/* to ask the module below the stream */
206 					/* head to get this information. */
207 	struct stdata	*sd_mate;	/* pointer to twisted stream mate */
208 	kthread_id_t	sd_freezer;	/* thread that froze stream */
209 	kmutex_t	sd_reflock;	/* Protects sd_refcnt */
210 	int		sd_refcnt;	/* number of claimstr */
211 	uint_t		sd_wakeq;	/* strwakeq()'s copy of sd_flag */
212 	struct queue	*sd_struiordq;	/* sync barrier struio() read queue */
213 	struct queue	*sd_struiowrq;	/* sync barrier struio() write queue */
214 	char		sd_struiodnak;	/* defer NAK of M_IOCTL by rput() */
215 	struct msgb	*sd_struionak;	/* pointer M_IOCTL mblk(s) to NAK */
216 	caddr_t		sd_t_audit_data; /* For audit purposes only */
217 	ssize_t		sd_maxblk;	/* maximum message block size */
218 	uint_t		sd_rput_opt;	/* options/flags for strrput */
219 	uint_t		sd_wput_opt;	/* options/flags for write/putmsg */
220 	uint_t		sd_read_opt;	/* options/flags for strread */
221 	msgfunc_t	sd_rprotofunc;	/* rput M_*PROTO routine */
222 	msgfunc_t	sd_rputdatafunc; /* read M_DATA routine */
223 	msgfunc_t	sd_rmiscfunc;	/* rput routine (non-data/proto) */
224 	msgfunc_t	sd_wputdatafunc; /* wput M_DATA routine */
225 	errfunc_t	sd_rderrfunc;	/* read side error callback */
226 	errfunc_t	sd_wrerrfunc;	/* write side error callback */
227 	/*
228 	 * support for low contention concurrent putnext.
229 	 */
230 	ciputctrl_t	*sd_ciputctrl;
231 	uint_t		sd_nciputctrl;
232 
233 	int		sd_anchor;	/* position of anchor in stream */
234 	/*
235 	 * Service scheduling at the stream head.
236 	 */
237 	kmutex_t	sd_qlock;
238 	struct queue	*sd_qhead;	/* Head of queues to be serviced. */
239 	struct queue	*sd_qtail;	/* Tail of queues to be serviced. */
240 	void		*sd_servid;	/* Service ID for bckgrnd schedule */
241 	ushort_t	sd_svcflags;	/* Servicing flags */
242 	short		sd_nqueues;	/* Number of queues in the list */
243 	kcondvar_t	sd_qcv;		/* Waiters for qhead to become empty */
244 	kcondvar_t	sd_zcopy_wait;
245 	uint_t		sd_copyflag;	/* copy-related flags */
246 	zoneid_t	sd_anchorzone;	/* Allow removal from same zone only */
247 } stdata_t;
248 
249 /*
250  * stdata servicing flags.
251  */
252 #define	STRS_WILLSERVICE	0x01
253 #define	STRS_SCHEDULED		0x02
254 
255 #define	STREAM_NEEDSERVICE(stp)	((stp)->sd_qhead != NULL)
256 
257 /*
258  * stdata flag field defines
259  */
260 #define	IOCWAIT		0x00000001	/* Someone is doing an ioctl */
261 #define	RSLEEP		0x00000002	/* Someone wants to read/recv msg */
262 #define	WSLEEP		0x00000004	/* Someone wants to write */
263 #define	STRPRI		0x00000008	/* An M_PCPROTO is at stream head */
264 #define	STRHUP		0x00000010	/* Device has vanished */
265 #define	STWOPEN		0x00000020	/* waiting for 1st open */
266 #define	STPLEX		0x00000040	/* stream is being multiplexed */
267 #define	STRISTTY	0x00000080	/* stream is a terminal */
268 #define	STRGETINPROG	0x00000100	/* (k)strgetmsg is running */
269 #define	IOCWAITNE	0x00000200	/* STR_NOERROR ioctl running */
270 #define	STRDERR		0x00000400	/* fatal read error from M_ERROR */
271 #define	STWRERR		0x00000800	/* fatal write error from M_ERROR */
272 #define	STRDERRNONPERSIST 0x00001000	/* nonpersistent read errors */
273 #define	STWRERRNONPERSIST 0x00002000	/* nonpersistent write errors */
274 #define	STRCLOSE	0x00004000	/* wait for a close to complete */
275 #define	SNDMREAD	0x00008000	/* used for read notification */
276 #define	OLDNDELAY	0x00010000	/* use old TTY semantics for */
277 					/* NDELAY reads and writes */
278 	/*		0x00020000	   unused */
279 	/*		0x00040000	   unused */
280 #define	STRTOSTOP	0x00080000	/* block background writes */
281 	/*		0x00100000	   unused */
282 	/*		0x00200000	   unused */
283 #define	STRMOUNT	0x00400000	/* stream is mounted */
284 #define	STRNOTATMARK	0x00800000	/* Not at mark (when empty read q) */
285 #define	STRDELIM	0x01000000	/* generate delimited messages */
286 #define	STRATMARK	0x02000000	/* At mark (due to MSGMARKNEXT) */
287 #define	STZCNOTIFY	0x04000000	/* wait for zerocopy mblk to be acked */
288 #define	STRPLUMB	0x08000000	/* push/pop pending */
289 #define	STREOF		0x10000000	/* End-of-file indication */
290 #define	STREOPENFAIL	0x20000000	/* indicates if re-open has failed */
291 #define	STRMATE		0x40000000	/* this stream is a mate */
292 #define	STRHASLINKS	0x80000000	/* I_LINKs under this stream */
293 
294 /*
295  * Copy-related flags (sd_copyflag), set by SO_COPYOPT.
296  */
297 #define	STZCVMSAFE	0x00000001	/* safe to borrow file (segmapped) */
298 					/* pages instead of bcopy */
299 #define	STZCVMUNSAFE	0x00000002	/* unsafe to borrow file pages */
300 #define	STRCOPYCACHED	0x00000004	/* copy should NOT bypass cache */
301 
302 /*
303  * Options and flags for strrput (sd_rput_opt)
304  */
305 #define	SR_POLLIN	0x00000001	/* pollwakeup needed for band0 data */
306 #define	SR_SIGALLDATA	0x00000002	/* Send SIGPOLL for all M_DATA */
307 #define	SR_CONSOL_DATA	0x00000004	/* Consolidate M_DATA onto q_last */
308 #define	SR_IGN_ZEROLEN	0x00000008	/* Ignore zero-length M_DATA */
309 
310 /*
311  * Options and flags for strwrite/strputmsg (sd_wput_opt)
312  */
313 #define	SW_SIGPIPE	0x00000001	/* Send SIGPIPE for write error */
314 #define	SW_RECHECK_ERR	0x00000002	/* Recheck errors in strwrite loop */
315 #define	SW_SNDZERO	0x00000004	/* send 0-length msg down pipe/FIFO */
316 
317 /*
318  * Options and flags for strread (sd_read_opt)
319  */
320 #define	RD_MSGDIS	0x00000001	/* read msg discard */
321 #define	RD_MSGNODIS	0x00000002	/* read msg no discard */
322 #define	RD_PROTDAT	0x00000004	/* read M_[PC]PROTO contents as data */
323 #define	RD_PROTDIS	0x00000008	/* discard M_[PC]PROTO blocks and */
324 					/* retain data blocks */
325 /*
326  * Flags parameter for strsetrputhooks() and strsetwputhooks().
327  * These flags define the interface for setting the above internal
328  * flags in sd_rput_opt and sd_wput_opt.
329  */
330 #define	SH_CONSOL_DATA	0x00000001	/* Consolidate M_DATA onto q_last */
331 #define	SH_SIGALLDATA	0x00000002	/* Send SIGPOLL for all M_DATA */
332 #define	SH_IGN_ZEROLEN	0x00000004	/* Drop zero-length M_DATA */
333 
334 #define	SH_SIGPIPE	0x00000100	/* Send SIGPIPE for write error */
335 #define	SH_RECHECK_ERR	0x00000200	/* Recheck errors in strwrite loop */
336 
337 /*
338  * Each queue points to a sync queue (the inner perimeter) which keeps
339  * track of the number of threads that are inside a given queue (sq_count)
340  * and also is used to implement the asynchronous putnext
341  * (by queuing messages if the queue can not be entered.)
342  *
343  * Messages are queued on sq_head/sq_tail including deferred qwriter(INNER)
344  * messages. The sq_head/sq_tail list is a singly-linked list with
345  * b_queue recording the queue and b_prev recording the function to
346  * be called (either the put procedure or a qwriter callback function.)
347  *
348  * The sq_count counter tracks the number of threads that are
349  * executing inside the perimeter or (in the case of outer perimeters)
350  * have some work queued for them relating to the perimeter. The sq_rmqcount
351  * counter tracks the subset which are in removeq() (usually invoked from
352  * qprocsoff(9F)).
353  *
354  * In addition a module writer can declare that the module has an outer
355  * perimeter (by setting D_MTOUTPERIM) in which case all inner perimeter
356  * syncq's for the module point (through sq_outer) to an outer perimeter
357  * syncq. The outer perimeter consists of the doubly linked list (sq_onext and
358  * sq_oprev) linking all the inner perimeter syncq's with out outer perimeter
359  * syncq. This is used to implement qwriter(OUTER) (an asynchronous way of
360  * getting exclusive access at the outer perimeter) and outer_enter/exit
361  * which are used by the framework to acquire exclusive access to the outer
362  * perimeter during open and close of modules that have set D_MTOUTPERIM.
363  *
364  * In the inner perimeter case sq_save is available for use by machine
365  * dependent code. sq_head/sq_tail are used to queue deferred messages on
366  * the inner perimeter syncqs and to queue become_writer requests on the
367  * outer perimeter syncqs.
368  *
369  * Note: machine dependent optimized versions of putnext may depend
370  * on the order of sq_flags and sq_count (so that they can e.g.
371  * read these two fields in a single load instruction.)
372  *
373  * Per perimeter SQLOCK/sq_count in putnext/put may be replaced by per cpu
374  * sq_putlocks/sq_putcounts each living in a separate cache line. Obviously
375  * sq_putlock[x] protects sq_putcount[x]. putnext/put routine will grab only 1
376  * of sq_putlocks and update only 1 of sq_putcounts. strlock() and many
377  * other routines in strsubr.c and ddi.c will grab all sq_putlocks (as well as
378  * SQLOCK) and figure out the count value as the sum of sq_count and all of
379  * sq_putcounts. The idea is to make critical fast path -- putnext -- much
380  * faster at the expense of much less often used slower path like
381  * strlock(). One known case where entersq/strlock is executed pretty often is
382  * SpecWeb but since IP is SQ_CIOC and socket TCP/IP stream is nextless
383  * there's no need to grab multiple sq_putlocks and look at sq_putcounts. See
384  * strsubr.c for more comments.
385  *
386  * Note regular SQLOCK and sq_count are still used in many routines
387  * (e.g. entersq(), rwnext()) in the same way as before sq_putlocks were
388  * introduced.
389  *
390  * To understand when all sq_putlocks need to be held and all sq_putcounts
391  * need to be added up one needs to look closely at putnext code. Basically if
392  * a routine like e.g. wait_syncq() needs to be sure that perimeter is empty
393  * all sq_putlocks/sq_putcounts need to be held/added up. On the other hand
394  * there's no need to hold all sq_putlocks and count all sq_putcounts in
395  * routines like leavesq()/dropsq() and etc. since the are usually exit
396  * counterparts of entersq/outer_enter() and etc. which have already either
397  * prevented put entry poins from executing or did not care about put
398  * entrypoints. entersq() doesn't need to care about sq_putlocks/sq_putcounts
399  * if the entry point has a shared access since put has the highest degree of
400  * concurrency and such entersq() does not intend to block out put
401  * entrypoints.
402  *
403  * Before sq_putcounts were introduced the standard way to wait for perimeter
404  * to become empty was:
405  *
406  *	mutex_enter(SQLOCK(sq));
407  *	while (sq->sq_count > 0) {
408  *		sq->sq_flags |= SQ_WANTWAKEUP;
409  *		cv_wait(&sq->sq_wait, SQLOCK(sq));
410  *	}
411  *	mutex_exit(SQLOCK(sq));
412  *
413  * The new way is:
414  *
415  * 	mutex_enter(SQLOCK(sq));
416  *	count = sq->sq_count;
417  *	SQ_PUTLOCKS_ENTER(sq);
418  *	SUM_SQ_PUTCOUNTS(sq, count);
419  *	while (count != 0) {
420  *		sq->sq_flags |= SQ_WANTWAKEUP;
421  *		SQ_PUTLOCKS_EXIT(sq);
422  *		cv_wait(&sq->sq_wait, SQLOCK(sq));
423  *		count = sq->sq_count;
424  *		SQ_PUTLOCKS_ENTER(sq);
425  *		SUM_SQ_PUTCOUNTS(sq, count);
426  *	}
427  *	SQ_PUTLOCKS_EXIT(sq);
428  *	mutex_exit(SQLOCK(sq));
429  *
430  * Note that SQ_WANTWAKEUP is set before dropping SQ_PUTLOCKS. This makes sure
431  * putnext won't skip a wakeup.
432  *
433  * sq_putlocks are treated as the extension of SQLOCK for lock ordering
434  * purposes and are always grabbed right after grabbing SQLOCK and released
435  * right before releasing SQLOCK. This also allows dynamic creation of
436  * sq_putlocks while holding SQLOCK (by making sq_ciputctrl non null even when
437  * the stream is already in use). Only in putnext one of sq_putlocks
438  * is grabbed instead of SQLOCK. putnext return path remembers what counter it
439  * incremented and decrements the right counter on its way out.
440  */
441 
442 struct syncq {
443 	kmutex_t	sq_lock;	/* atomic access to syncq */
444 	uint16_t	sq_count;	/* # threads inside */
445 	uint16_t	sq_flags;	/* state and some type info */
446 	/*
447 	 * Distributed syncq scheduling
448 	 *  The list of queue's is handled by sq_head and
449 	 *  sq_tail fields.
450 	 *
451 	 *  The list of events is handled by the sq_evhead and sq_evtail
452 	 *  fields.
453 	 */
454 	queue_t		*sq_head;	/* queue of deferred messages */
455 	queue_t		*sq_tail;	/* queue of deferred messages */
456 	mblk_t		*sq_evhead;	/* Event message on the syncq */
457 	mblk_t		*sq_evtail;
458 	uint_t		sq_nqueues;	/* # of queues on this sq */
459 	/*
460 	 * Concurrency and condition variables
461 	 */
462 	uint16_t	sq_type;	/* type (concurrency) of syncq */
463 	uint16_t	sq_rmqcount;	/* # threads inside removeq() */
464 	kcondvar_t 	sq_wait;	/* block on this sync queue */
465 	kcondvar_t 	sq_exitwait;	/* waiting for thread to leave the */
466 					/* inner perimeter */
467 	/*
468 	 * Handling synchronous callbacks such as qtimeout and qbufcall
469 	 */
470 	ushort_t	sq_callbflags;	/* flags for callback synchronization */
471 	callbparams_id_t sq_cancelid;	/* id of callback being cancelled */
472 	struct callbparams *sq_callbpend;	/* Pending callbacks */
473 
474 	/*
475 	 * Links forming an outer perimeter from one outer syncq and
476 	 * a set of inner sync queues.
477 	 */
478 	struct syncq	*sq_outer;	/* Pointer to outer perimeter */
479 	struct syncq	*sq_onext;	/* Linked list of syncq's making */
480 	struct syncq	*sq_oprev;	/* up the outer perimeter. */
481 	/*
482 	 * support for low contention concurrent putnext.
483 	 */
484 	ciputctrl_t	*sq_ciputctrl;
485 	uint_t		sq_nciputctrl;
486 	/*
487 	 * Counter for the number of threads wanting to become exclusive.
488 	 */
489 	uint_t		sq_needexcl;
490 	/*
491 	 * These two fields are used for scheduling a syncq for
492 	 * background processing. The sq_svcflag is protected by
493 	 * SQLOCK lock.
494 	 */
495 	struct syncq	*sq_next;	/* for syncq scheduling */
496 	void *		sq_servid;
497 	uint_t		sq_servcount;	/* # pending background threads */
498 	uint_t		sq_svcflags;	/* Scheduling flags	*/
499 	clock_t		sq_tstamp;	/* Time when was enabled */
500 	/*
501 	 * Maximum priority of the queues on this syncq.
502 	 */
503 	pri_t		sq_pri;
504 };
505 typedef struct syncq syncq_t;
506 
507 /*
508  * sync queue scheduling flags (for sq_svcflags).
509  */
510 #define	SQ_SERVICE	0x1		/* being serviced */
511 #define	SQ_BGTHREAD	0x2		/* awaiting service by bg thread */
512 #define	SQ_DISABLED	0x4		/* don't put syncq in service list */
513 
514 /*
515  * FASTPUT bit in sd_count/putcount.
516  */
517 #define	SQ_FASTPUT	0x8000
518 #define	SQ_FASTMASK	0x7FFF
519 
520 /*
521  * sync queue state flags
522  */
523 #define	SQ_EXCL		0x0001		/* exclusive access to inner */
524 					/*	perimeter */
525 #define	SQ_BLOCKED	0x0002		/* qprocsoff */
526 #define	SQ_FROZEN	0x0004		/* freezestr */
527 #define	SQ_WRITER	0x0008		/* qwriter(OUTER) pending or running */
528 #define	SQ_MESSAGES	0x0010		/* messages on syncq */
529 #define	SQ_WANTWAKEUP	0x0020		/* do cv_broadcast on sq_wait */
530 #define	SQ_WANTEXWAKEUP	0x0040		/* do cv_broadcast on sq_exitwait */
531 #define	SQ_EVENTS	0x0080		/* Events pending */
532 #define	SQ_QUEUED	(SQ_MESSAGES | SQ_EVENTS)
533 #define	SQ_FLAGMASK	0x00FF
534 
535 /*
536  * Test a queue to see if inner perimeter is exclusive.
537  */
538 #define	PERIM_EXCL(q)	((q)->q_syncq->sq_flags & SQ_EXCL)
539 
540 /*
541  * If any of these flags are set it is not possible for a thread to
542  * enter a put or service procedure. Instead it must either block
543  * or put the message on the syncq.
544  */
545 #define	SQ_GOAWAY	(SQ_EXCL|SQ_BLOCKED|SQ_FROZEN|SQ_WRITER|\
546 			SQ_QUEUED)
547 /*
548  * If any of these flags are set it not possible to drain the syncq
549  */
550 #define	SQ_STAYAWAY	(SQ_BLOCKED|SQ_FROZEN|SQ_WRITER)
551 
552 /*
553  * Flags to trigger syncq tail processing.
554  */
555 #define	SQ_TAIL		(SQ_QUEUED|SQ_WANTWAKEUP|SQ_WANTEXWAKEUP)
556 
557 /*
558  * Syncq types (stored in sq_type)
559  * The SQ_TYPES_IN_FLAGS (ciput) are also stored in sq_flags
560  * for performance reasons. Thus these type values have to be in the low
561  * 16 bits and not conflict with the sq_flags values above.
562  *
563  * Notes:
564  *  - putnext() and put() assume that the put procedures have the highest
565  *    degree of concurrency. Thus if any of the SQ_CI* are set then SQ_CIPUT
566  *    has to be set. This restriction can be lifted by adding code to putnext
567  *    and put that check that sq_count == 0 like entersq does.
568  *  - putnext() and put() does currently not handle !SQ_COPUT
569  *  - In order to implement !SQ_COCB outer_enter has to be fixed so that
570  *    the callback can be cancelled while cv_waiting in outer_enter.
571  *  - If SQ_CISVC needs to be implemented, qprocsoff() needs to wait
572  *    for the currently running services to stop (wait for QINSERVICE
573  *    to go off). disable_svc called from qprcosoff disables only
574  *    services that will be run in future.
575  *
576  * All the SQ_CO flags are set when there is no outer perimeter.
577  */
578 #define	SQ_CIPUT	0x0100		/* Concurrent inner put proc */
579 #define	SQ_CISVC	0x0200		/* Concurrent inner svc proc */
580 #define	SQ_CIOC		0x0400		/* Concurrent inner open/close */
581 #define	SQ_CICB		0x0800		/* Concurrent inner callback */
582 #define	SQ_COPUT	0x1000		/* Concurrent outer put proc */
583 #define	SQ_COSVC	0x2000		/* Concurrent outer svc proc */
584 #define	SQ_COOC		0x4000		/* Concurrent outer open/close */
585 #define	SQ_COCB		0x8000		/* Concurrent outer callback */
586 
587 /* Types also kept in sq_flags for performance */
588 #define	SQ_TYPES_IN_FLAGS	(SQ_CIPUT)
589 
590 #define	SQ_CI		(SQ_CIPUT|SQ_CISVC|SQ_CIOC|SQ_CICB)
591 #define	SQ_CO		(SQ_COPUT|SQ_COSVC|SQ_COOC|SQ_COCB)
592 #define	SQ_TYPEMASK	(SQ_CI|SQ_CO)
593 
594 /*
595  * Flag combinations passed to entersq and leavesq to specify the type
596  * of entry point.
597  */
598 #define	SQ_PUT		(SQ_CIPUT|SQ_COPUT)
599 #define	SQ_SVC		(SQ_CISVC|SQ_COSVC)
600 #define	SQ_OPENCLOSE	(SQ_CIOC|SQ_COOC)
601 #define	SQ_CALLBACK	(SQ_CICB|SQ_COCB)
602 
603 /*
604  * Other syncq types which are not copied into flags.
605  */
606 #define	SQ_PERMOD	0x01		/* Syncq is PERMOD */
607 
608 /*
609  * Asynchronous callback qun*** flag.
610  * The mechanism these flags are used in is one where callbacks enter
611  * the perimeter thanks to framework support. To use this mechanism
612  * the q* and qun* flavors of the callback routines must be used.
613  * e.g. qtimeout and quntimeout. The synchronization provided by the flags
614  * avoids deadlocks between blocking qun* routines and the perimeter
615  * lock.
616  */
617 #define	SQ_CALLB_BYPASSED	0x01		/* bypassed callback fn */
618 
619 /*
620  * Cancel callback mask.
621  * The mask expands as the number of cancelable callback types grows
622  * Note - separate callback flag because different callbacks have
623  * overlapping id space.
624  */
625 #define	SQ_CALLB_CANCEL_MASK	(SQ_CANCEL_TOUT|SQ_CANCEL_BUFCALL)
626 
627 #define	SQ_CANCEL_TOUT		0x02		/* cancel timeout request */
628 #define	SQ_CANCEL_BUFCALL	0x04		/* cancel bufcall request */
629 
630 typedef struct callbparams {
631 	syncq_t		*cbp_sq;
632 	void		(*cbp_func)(void *);
633 	void		*cbp_arg;
634 	callbparams_id_t cbp_id;
635 	uint_t		cbp_flags;
636 	struct callbparams *cbp_next;
637 	size_t		cbp_size;
638 } callbparams_t;
639 
640 typedef struct strbufcall {
641 	void		(*bc_func)(void *);
642 	void		*bc_arg;
643 	size_t		bc_size;
644 	bufcall_id_t	bc_id;
645 	struct strbufcall *bc_next;
646 	kthread_id_t	bc_executor;
647 } strbufcall_t;
648 
649 /*
650  * Structure of list of processes to be sent SIGPOLL/SIGURG signal
651  * on request.  The valid S_* events are defined in stropts.h.
652  */
653 typedef struct strsig {
654 	struct pid	*ss_pidp;	/* pid/pgrp pointer */
655 	pid_t		ss_pid;		/* positive pid, negative pgrp */
656 	int		ss_events;	/* S_* events */
657 	struct strsig	*ss_next;
658 } strsig_t;
659 
660 /*
661  * bufcall list
662  */
663 struct bclist {
664 	strbufcall_t	*bc_head;
665 	strbufcall_t	*bc_tail;
666 };
667 
668 /*
669  * Structure used to track mux links and unlinks.
670  */
671 struct mux_node {
672 	major_t		 mn_imaj;	/* internal major device number */
673 	uint16_t	 mn_indegree;	/* number of incoming edges */
674 	struct mux_node *mn_originp;	/* where we came from during search */
675 	struct mux_edge *mn_startp;	/* where search left off in mn_outp */
676 	struct mux_edge *mn_outp;	/* list of outgoing edges */
677 	uint_t		 mn_flags;	/* see below */
678 };
679 
680 /*
681  * Flags for mux_nodes.
682  */
683 #define	VISITED	1
684 
685 /*
686  * Edge structure - a list of these is hung off the
687  * mux_node to represent the outgoing edges.
688  */
689 struct mux_edge {
690 	struct mux_node	*me_nodep;	/* edge leads to this node */
691 	struct mux_edge	*me_nextp;	/* next edge */
692 	int		 me_muxid;	/* id of link */
693 	dev_t		 me_dev;	/* dev_t - used for kernel PUNLINK */
694 };
695 
696 /*
697  * Queue info
698  *
699  * The syncq is included here to reduce memory fragmentation
700  * for kernel memory allocators that only allocate in sizes that are
701  * powers of two. If the kernel memory allocator changes this should
702  * be revisited.
703  */
704 typedef struct queinfo {
705 	struct queue	qu_rqueue;	/* read queue - must be first */
706 	struct queue	qu_wqueue;	/* write queue - must be second */
707 	struct syncq	qu_syncq;	/* syncq - must be third */
708 } queinfo_t;
709 
710 /*
711  * Multiplexed streams info
712  */
713 typedef struct linkinfo {
714 	struct linkblk	li_lblk;	/* must be first */
715 	struct file	*li_fpdown;	/* file pointer for lower stream */
716 	struct linkinfo	*li_next;	/* next in list */
717 	struct linkinfo *li_prev;	/* previous in list */
718 } linkinfo_t;
719 
720 /*
721  * List of syncq's used by freeezestr/unfreezestr
722  */
723 typedef struct syncql {
724 	struct syncql	*sql_next;
725 	syncq_t		*sql_sq;
726 } syncql_t;
727 
728 typedef struct sqlist {
729 	syncql_t	*sqlist_head;
730 	size_t		sqlist_size;		/* structure size in bytes */
731 	size_t		sqlist_index;		/* next free entry in array */
732 	syncql_t	sqlist_array[4];	/* 4 or more entries */
733 } sqlist_t;
734 
735 typedef struct perdm {
736 	struct perdm		*dm_next;
737 	syncq_t			*dm_sq;
738 	struct streamtab	*dm_str;
739 	uint_t			dm_ref;
740 } perdm_t;
741 
742 #define	NEED_DM(dmp, qflag) \
743 	(dmp == NULL && (qflag & (QPERMOD | QMTOUTPERIM)))
744 
745 /*
746  * fmodsw_impl_t is used within the kernel. fmodsw is used by
747  * the modules/drivers. The information is copied from fmodsw
748  * defined in the module/driver into the fmodsw_impl_t structure
749  * during the module/driver initialization.
750  */
751 typedef struct fmodsw_impl	fmodsw_impl_t;
752 
753 struct fmodsw_impl {
754 	fmodsw_impl_t		*f_next;
755 	char			f_name[FMNAMESZ + 1];
756 	struct streamtab	*f_str;
757 	uint32_t		f_qflag;
758 	uint32_t		f_sqtype;
759 	perdm_t			*f_dmp;
760 	uint32_t		f_ref;
761 	uint32_t		f_hits;
762 };
763 
764 typedef enum {
765 	FMODSW_HOLD =	0x00000001,
766 	FMODSW_LOAD =	0x00000002
767 } fmodsw_flags_t;
768 
769 typedef struct cdevsw_impl {
770 	struct streamtab	*d_str;
771 	uint32_t		d_qflag;
772 	uint32_t		d_sqtype;
773 	perdm_t			*d_dmp;
774 } cdevsw_impl_t;
775 
776 /*
777  * Enumeration of the types of access that can be requested for a
778  * controlling terminal under job control.
779  */
780 enum jcaccess {
781 	JCREAD,			/* read data on a ctty */
782 	JCWRITE,		/* write data to a ctty */
783 	JCSETP,			/* set ctty parameters */
784 	JCGETP			/* get ctty parameters */
785 };
786 
787 struct str_stack {
788 	netstack_t	*ss_netstack;	/* Common netstack */
789 
790 	kmutex_t	ss_sad_lock;	/* autopush lock */
791 	mod_hash_t	*ss_sad_hash;
792 	size_t		ss_sad_hash_nchains;
793 	struct saddev	*ss_saddev;	/* sad device array */
794 	int		ss_sadcnt;	/* number of sad devices */
795 
796 	int		ss_devcnt;	/* number of mux_nodes */
797 	struct mux_node	*ss_mux_nodes;	/* mux info for cycle checking */
798 };
799 typedef struct str_stack str_stack_t;
800 
801 /*
802  * Finding related queues
803  */
804 #define	STREAM(q)	((q)->q_stream)
805 #define	SQ(rq)		((syncq_t *)((rq) + 2))
806 
807 /*
808  * Locking macros
809  */
810 #define	QLOCK(q)	(&(q)->q_lock)
811 #define	SQLOCK(sq)	(&(sq)->sq_lock)
812 
813 #define	STREAM_PUTLOCKS_ENTER(stp) {					       \
814 		ASSERT(MUTEX_HELD(&(stp)->sd_lock));			       \
815 		if ((stp)->sd_ciputctrl != NULL) {			       \
816 			int i;						       \
817 			int nlocks = (stp)->sd_nciputctrl;		       \
818 			ciputctrl_t *cip = (stp)->sd_ciputctrl;		       \
819 			for (i = 0; i <= nlocks; i++) {			       \
820 				mutex_enter(&cip[i].ciputctrl_lock);	       \
821 			}						       \
822 		}							       \
823 	}
824 
825 #define	STREAM_PUTLOCKS_EXIT(stp) {					       \
826 		ASSERT(MUTEX_HELD(&(stp)->sd_lock));			       \
827 		if ((stp)->sd_ciputctrl != NULL) {			       \
828 			int i;						       \
829 			int nlocks = (stp)->sd_nciputctrl;		       \
830 			ciputctrl_t *cip = (stp)->sd_ciputctrl;		       \
831 			for (i = 0; i <= nlocks; i++) {			       \
832 				mutex_exit(&cip[i].ciputctrl_lock);	       \
833 			}						       \
834 		}							       \
835 	}
836 
837 #define	SQ_PUTLOCKS_ENTER(sq) {						       \
838 		ASSERT(MUTEX_HELD(SQLOCK(sq)));				       \
839 		if ((sq)->sq_ciputctrl != NULL) {			       \
840 			int i;						       \
841 			int nlocks = (sq)->sq_nciputctrl;		       \
842 			ciputctrl_t *cip = (sq)->sq_ciputctrl;		       \
843 			ASSERT((sq)->sq_type & SQ_CIPUT);		       \
844 			for (i = 0; i <= nlocks; i++) {			       \
845 				mutex_enter(&cip[i].ciputctrl_lock);	       \
846 			}						       \
847 		}							       \
848 	}
849 
850 #define	SQ_PUTLOCKS_EXIT(sq) {						       \
851 		ASSERT(MUTEX_HELD(SQLOCK(sq)));				       \
852 		if ((sq)->sq_ciputctrl != NULL) {			       \
853 			int i;						       \
854 			int nlocks = (sq)->sq_nciputctrl;		       \
855 			ciputctrl_t *cip = (sq)->sq_ciputctrl;		       \
856 			ASSERT((sq)->sq_type & SQ_CIPUT);		       \
857 			for (i = 0; i <= nlocks; i++) {			       \
858 				mutex_exit(&cip[i].ciputctrl_lock);	       \
859 			}						       \
860 		}							       \
861 	}
862 
863 #define	SQ_PUTCOUNT_SETFAST(sq) {					\
864 		ASSERT(MUTEX_HELD(SQLOCK(sq)));				\
865 		if ((sq)->sq_ciputctrl != NULL) {			\
866 			int i;						\
867 			int nlocks = (sq)->sq_nciputctrl;		\
868 			ciputctrl_t *cip = (sq)->sq_ciputctrl;		\
869 			ASSERT((sq)->sq_type & SQ_CIPUT);		\
870 			for (i = 0; i <= nlocks; i++) {			\
871 				mutex_enter(&cip[i].ciputctrl_lock);	\
872 				cip[i].ciputctrl_count |= SQ_FASTPUT;	\
873 				mutex_exit(&cip[i].ciputctrl_lock);	\
874 			}						\
875 		}							\
876 	}
877 
878 #define	SQ_PUTCOUNT_CLRFAST(sq) {					\
879 		ASSERT(MUTEX_HELD(SQLOCK(sq)));				\
880 		if ((sq)->sq_ciputctrl != NULL) {			\
881 			int i;						\
882 			int nlocks = (sq)->sq_nciputctrl;		\
883 			ciputctrl_t *cip = (sq)->sq_ciputctrl;		\
884 			ASSERT((sq)->sq_type & SQ_CIPUT);		\
885 			for (i = 0; i <= nlocks; i++) {			\
886 				mutex_enter(&cip[i].ciputctrl_lock);	\
887 				cip[i].ciputctrl_count &= ~SQ_FASTPUT;	\
888 				mutex_exit(&cip[i].ciputctrl_lock);	\
889 			}						\
890 		}							\
891 	}
892 
893 
894 #ifdef	DEBUG
895 
896 #define	SQ_PUTLOCKS_HELD(sq) {						       \
897 		ASSERT(MUTEX_HELD(SQLOCK(sq)));				       \
898 		if ((sq)->sq_ciputctrl != NULL) {			       \
899 			int i;						       \
900 			int nlocks = (sq)->sq_nciputctrl;		       \
901 			ciputctrl_t *cip = (sq)->sq_ciputctrl;		       \
902 			ASSERT((sq)->sq_type & SQ_CIPUT);		       \
903 			for (i = 0; i <= nlocks; i++) {			       \
904 				ASSERT(MUTEX_HELD(&cip[i].ciputctrl_lock));    \
905 			}						       \
906 		}							       \
907 	}
908 
909 #define	SUMCHECK_SQ_PUTCOUNTS(sq, countcheck) {				       \
910 		if ((sq)->sq_ciputctrl != NULL) {			       \
911 			int i;						       \
912 			uint_t count = 0;				       \
913 			int ncounts = (sq)->sq_nciputctrl;		       \
914 			ASSERT((sq)->sq_type & SQ_CIPUT);		       \
915 			for (i = 0; i <= ncounts; i++) {		       \
916 				count +=				       \
917 				    (((sq)->sq_ciputctrl[i].ciputctrl_count) & \
918 				    SQ_FASTMASK);			       \
919 			}						       \
920 			ASSERT(count == (countcheck));			       \
921 		}							       \
922 	}
923 
924 #define	SUMCHECK_CIPUTCTRL_COUNTS(ciput, nciput, countcheck) {		       \
925 		int i;							       \
926 		uint_t count = 0;					       \
927 		ASSERT((ciput) != NULL);				       \
928 		for (i = 0; i <= (nciput); i++) {			       \
929 			count += (((ciput)[i].ciputctrl_count) &	       \
930 			    SQ_FASTMASK);				       \
931 		}							       \
932 		ASSERT(count == (countcheck));				       \
933 	}
934 
935 #else	/* DEBUG */
936 
937 #define	SQ_PUTLOCKS_HELD(sq)
938 #define	SUMCHECK_SQ_PUTCOUNTS(sq, countcheck)
939 #define	SUMCHECK_CIPUTCTRL_COUNTS(sq, nciput, countcheck)
940 
941 #endif	/* DEBUG */
942 
943 #define	SUM_SQ_PUTCOUNTS(sq, count) {					       \
944 		if ((sq)->sq_ciputctrl != NULL) {			       \
945 			int i;						       \
946 			int ncounts = (sq)->sq_nciputctrl;		       \
947 			ciputctrl_t *cip = (sq)->sq_ciputctrl;		       \
948 			ASSERT((sq)->sq_type & SQ_CIPUT);		       \
949 			for (i = 0; i <= ncounts; i++) {		       \
950 				(count) += ((cip[i].ciputctrl_count) &	       \
951 				    SQ_FASTMASK);			       \
952 			}						       \
953 		}							       \
954 	}
955 
956 #define	CLAIM_QNEXT_LOCK(stp)	mutex_enter(&(stp)->sd_lock)
957 #define	RELEASE_QNEXT_LOCK(stp)	mutex_exit(&(stp)->sd_lock)
958 
959 /*
960  * syncq message manipulation macros.
961  */
962 /*
963  * Put a message on the queue syncq.
964  * Assumes QLOCK held.
965  */
966 #define	SQPUT_MP(qp, mp)						\
967 	{								\
968 		qp->q_syncqmsgs++;					\
969 		if (qp->q_sqhead == NULL) {				\
970 			qp->q_sqhead = qp->q_sqtail = mp;		\
971 		} else {						\
972 			qp->q_sqtail->b_next = mp;			\
973 			qp->q_sqtail = mp;				\
974 		}							\
975 		set_qfull(qp);						\
976 	}
977 
978 /*
979  * Miscellaneous parameters and flags.
980  */
981 
982 /*
983  * Default timeout in milliseconds for ioctls and close
984  */
985 #define	STRTIMOUT 15000
986 
987 /*
988  * Flag values for stream io
989  */
990 #define	WRITEWAIT	0x1	/* waiting for write event */
991 #define	READWAIT	0x2	/* waiting for read event */
992 #define	NOINTR		0x4	/* error is not to be set for signal */
993 #define	GETWAIT		0x8	/* waiting for getmsg event */
994 
995 /*
996  * These flags need to be unique for stream io name space
997  * and copy modes name space.  These flags allow strwaitq
998  * and strdoioctl to proceed as if signals or errors on the stream
999  * head have not occurred; i.e. they will be detected by some other
1000  * means.
1001  * STR_NOSIG does not allow signals to interrupt the call
1002  * STR_NOERROR does not allow stream head read, write or hup errors to
1003  * affect the call.  When used with strdoioctl(), if a previous ioctl
1004  * is pending and times out, STR_NOERROR will cause strdoioctl() to not
1005  * return ETIME. If, however, the requested ioctl times out, ETIME
1006  * will be returned (use ic_timout instead)
1007  * STR_PEEK is used to inform strwaitq that the reader is peeking at data
1008  * and that a non-persistent error should not be cleared.
1009  * STR_DELAYERR is used to inform strwaitq that it should not check errors
1010  * after being awoken since, in addition to an error, there might also be
1011  * data queued on the stream head read queue.
1012  */
1013 #define	STR_NOSIG	0x10	/* Ignore signals during strdoioctl/strwaitq */
1014 #define	STR_NOERROR	0x20	/* Ignore errors during strdoioctl/strwaitq */
1015 #define	STR_PEEK	0x40	/* Peeking behavior on non-persistent errors */
1016 #define	STR_DELAYERR	0x80	/* Do not check errors on return */
1017 
1018 /*
1019  * Copy modes for tty and I_STR ioctls
1020  */
1021 #define	U_TO_K 	01			/* User to Kernel */
1022 #define	K_TO_K  02			/* Kernel to Kernel */
1023 
1024 /*
1025  * Mux defines.
1026  */
1027 #define	LINKNORMAL	0x01		/* normal mux link */
1028 #define	LINKPERSIST	0x02		/* persistent mux link */
1029 #define	LINKTYPEMASK	0x03		/* bitmask of all link types */
1030 #define	LINKCLOSE	0x04		/* unlink from strclose */
1031 
1032 /*
1033  * Definitions of Streams macros and function interfaces.
1034  */
1035 
1036 /*
1037  * Obsolete queue scheduling macros. They are not used anymore, but still kept
1038  * here for 3-d party modules and drivers who might still use them.
1039  */
1040 #define	setqsched()
1041 #define	qready()	1
1042 
1043 #ifdef _KERNEL
1044 #define	runqueues()
1045 #define	queuerun()
1046 #endif
1047 
1048 /* compatibility module for style 2 drivers with DR race condition */
1049 #define	DRMODNAME	"drcompat"
1050 
1051 /*
1052  * Macros dealing with mux_nodes.
1053  */
1054 #define	MUX_VISIT(X)	((X)->mn_flags |= VISITED)
1055 #define	MUX_CLEAR(X)	((X)->mn_flags &= (~VISITED)); \
1056 			((X)->mn_originp = NULL)
1057 #define	MUX_DIDVISIT(X)	((X)->mn_flags & VISITED)
1058 
1059 
1060 /*
1061  * Twisted stream macros
1062  */
1063 #define	STRMATED(X)	((X)->sd_flag & STRMATE)
1064 #define	STRLOCKMATES(X)	if (&((X)->sd_lock) > &(((X)->sd_mate)->sd_lock)) { \
1065 				mutex_enter(&((X)->sd_lock)); \
1066 				mutex_enter(&(((X)->sd_mate)->sd_lock));  \
1067 			} else {  \
1068 				mutex_enter(&(((X)->sd_mate)->sd_lock)); \
1069 				mutex_enter(&((X)->sd_lock)); \
1070 			}
1071 #define	STRUNLOCKMATES(X)	mutex_exit(&((X)->sd_lock)); \
1072 			mutex_exit(&(((X)->sd_mate)->sd_lock))
1073 
1074 #ifdef _KERNEL
1075 
1076 extern void strinit(void);
1077 extern int strdoioctl(struct stdata *, struct strioctl *, int, int,
1078     cred_t *, int *);
1079 extern void strsendsig(struct strsig *, int, uchar_t, int);
1080 extern void str_sendsig(vnode_t *, int, uchar_t, int);
1081 extern void strhup(struct stdata *);
1082 extern int qattach(queue_t *, dev_t *, int, cred_t *, fmodsw_impl_t *,
1083     boolean_t);
1084 extern int qreopen(queue_t *, dev_t *, int, cred_t *);
1085 extern void qdetach(queue_t *, int, int, cred_t *, boolean_t);
1086 extern void enterq(queue_t *);
1087 extern void leaveq(queue_t *);
1088 extern int putiocd(mblk_t *, caddr_t, int, cred_t *);
1089 extern int getiocd(mblk_t *, caddr_t, int);
1090 extern struct linkinfo *alloclink(queue_t *, queue_t *, struct file *);
1091 extern void lbfree(struct linkinfo *);
1092 extern int linkcycle(stdata_t *, stdata_t *, str_stack_t *);
1093 extern struct linkinfo *findlinks(stdata_t *, int, int, str_stack_t *);
1094 extern queue_t *getendq(queue_t *);
1095 extern int mlink(vnode_t *, int, int, cred_t *, int *, int);
1096 extern int mlink_file(vnode_t *, int, struct file *, cred_t *, int *, int);
1097 extern int munlink(struct stdata *, struct linkinfo *, int, cred_t *, int *,
1098     str_stack_t *);
1099 extern int munlinkall(struct stdata *, int, cred_t *, int *, str_stack_t *);
1100 extern void mux_addedge(stdata_t *, stdata_t *, int, str_stack_t *);
1101 extern void mux_rmvedge(stdata_t *, int, str_stack_t *);
1102 extern int devflg_to_qflag(struct streamtab *, uint32_t, uint32_t *,
1103     uint32_t *);
1104 extern void setq(queue_t *, struct qinit *, struct qinit *, perdm_t *,
1105     uint32_t, uint32_t, boolean_t);
1106 extern perdm_t *hold_dm(struct streamtab *, uint32_t, uint32_t);
1107 extern void rele_dm(perdm_t *);
1108 extern int strmakectl(struct strbuf *, int32_t, int32_t, mblk_t **);
1109 extern int strmakedata(ssize_t *, struct uio *, stdata_t *, int32_t, mblk_t **);
1110 extern int strmakemsg(struct strbuf *, ssize_t *, struct uio *,
1111     struct stdata *, int32_t, mblk_t **);
1112 extern int strgetmsg(vnode_t *, struct strbuf *, struct strbuf *, uchar_t *,
1113     int *, int, rval_t *);
1114 extern int strputmsg(vnode_t *, struct strbuf *, struct strbuf *, uchar_t,
1115     int flag, int fmode);
1116 extern int strstartplumb(struct stdata *, int, int);
1117 extern void strendplumb(struct stdata *);
1118 extern int stropen(struct vnode *, dev_t *, int, cred_t *);
1119 extern int strclose(struct vnode *, int, cred_t *);
1120 extern int strpoll(register struct stdata *, short, int, short *,
1121     struct pollhead **);
1122 extern void strclean(struct vnode *);
1123 extern void str_cn_clean();	/* XXX hook for consoles signal cleanup */
1124 extern int strwrite(struct vnode *, struct uio *, cred_t *);
1125 extern int strwrite_common(struct vnode *, struct uio *, cred_t *, int);
1126 extern int kstrwritemp(struct vnode *, mblk_t *, ushort_t);
1127 extern int strread(struct vnode *, struct uio *, cred_t *);
1128 extern int strioctl(struct vnode *, int, intptr_t, int, int, cred_t *, int *);
1129 extern int strrput(queue_t *, mblk_t *);
1130 extern int strrput_nondata(queue_t *, mblk_t *);
1131 extern mblk_t *strrput_proto(vnode_t *, mblk_t *,
1132     strwakeup_t *, strsigset_t *, strsigset_t *, strpollset_t *);
1133 extern mblk_t *strrput_misc(vnode_t *, mblk_t *,
1134     strwakeup_t *, strsigset_t *, strsigset_t *, strpollset_t *);
1135 extern int getiocseqno(void);
1136 extern int strwaitbuf(size_t, int);
1137 extern int strwaitq(stdata_t *, int, ssize_t, int, clock_t, int *);
1138 extern struct stdata *shalloc(queue_t *);
1139 extern void shfree(struct stdata *s);
1140 extern queue_t *allocq(void);
1141 extern void freeq(queue_t *);
1142 extern qband_t *allocband(void);
1143 extern void freeband(qband_t *);
1144 extern void freebs_enqueue(mblk_t *, dblk_t *);
1145 extern void setqback(queue_t *, unsigned char);
1146 extern int strcopyin(void *, void *, size_t, int);
1147 extern int strcopyout(void *, void *, size_t, int);
1148 extern void strsignal(struct stdata *, int, int32_t);
1149 extern clock_t str_cv_wait(kcondvar_t *, kmutex_t *, clock_t, int);
1150 extern void disable_svc(queue_t *);
1151 extern void remove_runlist(queue_t *);
1152 extern void wait_svc(queue_t *);
1153 extern void backenable(queue_t *, uchar_t);
1154 extern void set_qend(queue_t *);
1155 extern int strgeterr(stdata_t *, int32_t, int);
1156 extern void qenable_locked(queue_t *);
1157 extern mblk_t *getq_noenab(queue_t *);
1158 extern void rmvq_noenab(queue_t *, mblk_t *);
1159 extern void qbackenable(queue_t *, uchar_t);
1160 extern void set_qfull(queue_t *);
1161 
1162 extern void strblock(queue_t *);
1163 extern void strunblock(queue_t *);
1164 extern int qclaimed(queue_t *);
1165 extern int straccess(struct stdata *, enum jcaccess);
1166 
1167 extern void entersq(syncq_t *, int);
1168 extern void leavesq(syncq_t *, int);
1169 extern void claimq(queue_t *);
1170 extern void releaseq(queue_t *);
1171 extern void claimstr(queue_t *);
1172 extern void releasestr(queue_t *);
1173 extern void removeq(queue_t *);
1174 extern void insertq(struct stdata *, queue_t *);
1175 extern void drain_syncq(syncq_t *);
1176 extern void qfill_syncq(syncq_t *, queue_t *, mblk_t *);
1177 extern void qdrain_syncq(syncq_t *, queue_t *);
1178 extern int flush_syncq(syncq_t *, queue_t *);
1179 extern void wait_sq_svc(syncq_t *);
1180 
1181 extern void outer_enter(syncq_t *, uint16_t);
1182 extern void outer_exit(syncq_t *);
1183 extern void qwriter_inner(queue_t *, mblk_t *, void (*)());
1184 extern void qwriter_outer(queue_t *, mblk_t *, void (*)());
1185 
1186 extern callbparams_t *callbparams_alloc(syncq_t *, void (*)(void *),
1187     void *, int);
1188 extern void callbparams_free(syncq_t *, callbparams_t *);
1189 extern void callbparams_free_id(syncq_t *, callbparams_id_t, int32_t);
1190 extern void qcallbwrapper(void *);
1191 
1192 extern mblk_t *esballoc_wait(unsigned char *, size_t, uint_t, frtn_t *);
1193 extern mblk_t *esballoca(unsigned char *, size_t, uint_t, frtn_t *);
1194 extern mblk_t *desballoca(unsigned char *, size_t, uint_t, frtn_t *);
1195 extern int do_sendfp(struct stdata *, struct file *, struct cred *);
1196 extern int frozenstr(queue_t *);
1197 extern size_t xmsgsize(mblk_t *);
1198 
1199 extern void putnext_tail(syncq_t *, queue_t *, uint32_t);
1200 extern void stream_willservice(stdata_t *);
1201 extern void stream_runservice(stdata_t *);
1202 
1203 extern void strmate(vnode_t *, vnode_t *);
1204 extern queue_t *strvp2wq(vnode_t *);
1205 extern vnode_t *strq2vp(queue_t *);
1206 extern mblk_t *allocb_wait(size_t, uint_t, uint_t, int *);
1207 extern mblk_t *allocb_cred(size_t, cred_t *);
1208 extern mblk_t *allocb_cred_wait(size_t, uint_t, int *, cred_t *);
1209 extern mblk_t *allocb_tmpl(size_t, const mblk_t *);
1210 extern mblk_t *allocb_tryhard(size_t);
1211 extern void mblk_setcred(mblk_t *, cred_t *);
1212 extern void strpollwakeup(vnode_t *, short);
1213 extern int putnextctl_wait(queue_t *, int);
1214 
1215 extern int kstrputmsg(struct vnode *, mblk_t *, struct uio *, ssize_t,
1216     unsigned char, int, int);
1217 extern int kstrgetmsg(struct vnode *, mblk_t **, struct uio *,
1218     unsigned char *, int *, clock_t, rval_t *);
1219 
1220 extern void strsetrerror(vnode_t *, int, int, errfunc_t);
1221 extern void strsetwerror(vnode_t *, int, int, errfunc_t);
1222 extern void strseteof(vnode_t *, int);
1223 extern void strflushrq(vnode_t *, int);
1224 extern void strsetrputhooks(vnode_t *, uint_t, msgfunc_t, msgfunc_t);
1225 extern void strsetwputhooks(vnode_t *, uint_t, clock_t);
1226 extern void strsetrwputdatahooks(vnode_t *, msgfunc_t, msgfunc_t);
1227 extern int strwaitmark(vnode_t *);
1228 extern void strsignal_nolock(stdata_t *, int, int32_t);
1229 
1230 struct multidata_s;
1231 struct pdesc_s;
1232 extern int hcksum_assoc(mblk_t *, struct multidata_s *, struct pdesc_s  *,
1233     uint32_t, uint32_t, uint32_t, uint32_t, uint32_t, int);
1234 extern void hcksum_retrieve(mblk_t *, struct multidata_s *, struct pdesc_s *,
1235     uint32_t *, uint32_t *, uint32_t *, uint32_t *, uint32_t *);
1236 extern unsigned int bcksum(uchar_t *, int, unsigned int);
1237 extern boolean_t is_vmloaned_mblk(mblk_t *, struct multidata_s *,
1238     struct pdesc_s *);
1239 
1240 extern int fmodsw_register(const char *, struct streamtab *, int);
1241 extern int fmodsw_unregister(const char *);
1242 extern fmodsw_impl_t *fmodsw_find(const char *, fmodsw_flags_t);
1243 extern void fmodsw_rele(fmodsw_impl_t *);
1244 
1245 extern void freemsgchain(mblk_t *);
1246 extern mblk_t *copymsgchain(mblk_t *);
1247 
1248 extern mblk_t *mcopyinuio(struct stdata *, uio_t *, ssize_t, ssize_t, int *);
1249 
1250 /*
1251  * shared or externally configured data structures
1252  */
1253 extern ssize_t strmsgsz;		/* maximum stream message size */
1254 extern ssize_t strctlsz;		/* maximum size of ctl message */
1255 extern int nstrpush;			/* maximum number of pushes allowed */
1256 
1257 /*
1258  * Bufcalls related variables.
1259  */
1260 extern struct bclist strbcalls;		/* List of bufcalls */
1261 extern kmutex_t	strbcall_lock;		/* Protects the list of bufcalls */
1262 extern kcondvar_t strbcall_cv;		/* Signaling when a bufcall is added */
1263 extern kcondvar_t bcall_cv;	/* wait of executing bufcall completes */
1264 
1265 extern frtn_t frnop;
1266 
1267 extern struct kmem_cache *ciputctrl_cache;
1268 extern int n_ciputctrl;
1269 extern int max_n_ciputctrl;
1270 extern int min_n_ciputctrl;
1271 
1272 extern cdevsw_impl_t *devimpl;
1273 
1274 /*
1275  * esballoc queue for throttling
1276  */
1277 typedef struct esb_queue {
1278 	kmutex_t	eq_lock;
1279 	uint_t		eq_len;		/* number of queued messages */
1280 	mblk_t		*eq_head;	/* head of queue */
1281 	mblk_t		*eq_tail;	/* tail of queue */
1282 	uint_t		eq_flags;	/* esballoc queue flags */
1283 } esb_queue_t;
1284 
1285 /*
1286  * esballoc flags for queue processing.
1287  */
1288 #define	ESBQ_PROCESSING	0x01	/* queue is being processed */
1289 #define	ESBQ_TIMER	0x02	/* timer is active */
1290 
1291 extern void esballoc_queue_init(void);
1292 
1293 #endif	/* _KERNEL */
1294 
1295 /*
1296  * Note: Use of these macros are restricted to kernel/unix and
1297  * intended for the STREAMS framework.
1298  * All modules/drivers should include sys/ddi.h.
1299  *
1300  * Finding related queues
1301  */
1302 #define		_OTHERQ(q)	((q)->q_flag&QREADR? (q)+1: (q)-1)
1303 #define		_WR(q)		((q)->q_flag&QREADR? (q)+1: (q))
1304 #define		_RD(q)		((q)->q_flag&QREADR? (q): (q)-1)
1305 #define		_SAMESTR(q)	(!((q)->q_flag & QEND))
1306 
1307 /*
1308  * These are also declared here for modules/drivers that erroneously
1309  * include strsubr.h after ddi.h or fail to include ddi.h at all.
1310  */
1311 extern struct queue *OTHERQ(queue_t *); /* stream.h */
1312 extern struct queue *RD(queue_t *);
1313 extern struct queue *WR(queue_t *);
1314 extern int SAMESTR(queue_t *);
1315 
1316 /*
1317  * The following hardware checksum related macros are private
1318  * interfaces that are subject to change without notice.
1319  */
1320 #ifdef _KERNEL
1321 #define	DB_CKSUMSTART(mp)	((mp)->b_datap->db_cksumstart)
1322 #define	DB_CKSUMEND(mp)		((mp)->b_datap->db_cksumend)
1323 #define	DB_CKSUMSTUFF(mp)	((mp)->b_datap->db_cksumstuff)
1324 #define	DB_CKSUMFLAGS(mp)	((mp)->b_datap->db_struioun.cksum.flags)
1325 #define	DB_CKSUM16(mp)		((mp)->b_datap->db_cksum16)
1326 #define	DB_CKSUM32(mp)		((mp)->b_datap->db_cksum32)
1327 #define	DB_LSOFLAGS(mp)		((mp)->b_datap->db_struioun.cksum.flags)
1328 #define	DB_LSOMSS(mp)		((mp)->b_datap->db_struioun.cksum.pad)
1329 #endif	/* _KERNEL */
1330 
1331 #ifdef	__cplusplus
1332 }
1333 #endif
1334 
1335 
1336 #endif	/* _SYS_STRSUBR_H */
1337