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