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