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