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