xref: /illumos-gate/usr/src/uts/common/os/streamio.c (revision 3db86aab)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 #include <sys/types.h>
33 #include <sys/sysmacros.h>
34 #include <sys/param.h>
35 #include <sys/errno.h>
36 #include <sys/signal.h>
37 #include <sys/stat.h>
38 #include <sys/proc.h>
39 #include <sys/cred.h>
40 #include <sys/user.h>
41 #include <sys/vnode.h>
42 #include <sys/file.h>
43 #include <sys/stream.h>
44 #include <sys/strsubr.h>
45 #include <sys/stropts.h>
46 #include <sys/tihdr.h>
47 #include <sys/var.h>
48 #include <sys/poll.h>
49 #include <sys/termio.h>
50 #include <sys/ttold.h>
51 #include <sys/systm.h>
52 #include <sys/uio.h>
53 #include <sys/cmn_err.h>
54 #include <sys/sad.h>
55 #include <sys/priocntl.h>
56 #include <sys/jioctl.h>
57 #include <sys/procset.h>
58 #include <sys/session.h>
59 #include <sys/kmem.h>
60 #include <sys/filio.h>
61 #include <sys/vtrace.h>
62 #include <sys/debug.h>
63 #include <sys/strredir.h>
64 #include <sys/fs/fifonode.h>
65 #include <sys/fs/snode.h>
66 #include <sys/strlog.h>
67 #include <sys/strsun.h>
68 #include <sys/project.h>
69 #include <sys/kbio.h>
70 #include <sys/msio.h>
71 #include <sys/tty.h>
72 #include <sys/ptyvar.h>
73 #include <sys/vuid_event.h>
74 #include <sys/modctl.h>
75 #include <sys/sunddi.h>
76 #include <sys/sunldi_impl.h>
77 #include <sys/autoconf.h>
78 #include <sys/policy.h>
79 
80 /*
81  * what is mblk_pull_len?
82  *
83  * If a streams message consists of many short messages,
84  * a performance degradation occurs from copyout overhead.
85  * To decrease the per mblk overhead, messages that are
86  * likely to consist of many small mblks are pulled up into
87  * one continuous chunk of memory.
88  *
89  * To avoid the processing overhead of examining every
90  * mblk, a quick heuristic is used. If the first mblk in
91  * the message is shorter than mblk_pull_len, it is likely
92  * that the rest of the mblk will be short.
93  *
94  * This heuristic was decided upon after performance tests
95  * indicated that anything more complex slowed down the main
96  * code path.
97  */
98 #define	MBLK_PULL_LEN 64
99 uint32_t mblk_pull_len = MBLK_PULL_LEN;
100 
101 /*
102  * The sgttyb_handling flag controls the handling of the old BSD
103  * TIOCGETP, TIOCSETP, and TIOCSETN ioctls as follows:
104  *
105  * 0 - Emit no warnings at all and retain old, broken behavior.
106  * 1 - Emit no warnings and silently handle new semantics.
107  * 2 - Send cmn_err(CE_NOTE) when either TIOCSETP or TIOCSETN is used
108  *     (once per system invocation).  Handle with new semantics.
109  * 3 - Send SIGSYS when any TIOCGETP, TIOCSETP, or TIOCSETN call is
110  *     made (so that offenders drop core and are easy to debug).
111  *
112  * The "new semantics" are that TIOCGETP returns B38400 for
113  * sg_[io]speed if the corresponding value is over B38400, and that
114  * TIOCSET[PN] accept B38400 in these cases to mean "retain current
115  * bit rate."
116  */
117 int sgttyb_handling = 1;
118 static boolean_t sgttyb_complaint;
119 
120 /* don't push drcompat module by default on Style-2 streams */
121 static int push_drcompat = 0;
122 
123 /*
124  * id value used to distinguish between different ioctl messages
125  */
126 static uint32_t ioc_id;
127 
128 static void putback(struct stdata *, queue_t *, mblk_t *, int);
129 static void strcleanall(struct vnode *);
130 static int strwsrv(queue_t *);
131 
132 /*
133  * qinit and module_info structures for stream head read and write queues
134  */
135 struct module_info strm_info = { 0, "strrhead", 0, INFPSZ, STRHIGH, STRLOW };
136 struct module_info stwm_info = { 0, "strwhead", 0, 0, 0, 0 };
137 struct qinit strdata = { strrput, NULL, NULL, NULL, NULL, &strm_info };
138 struct qinit stwdata = { NULL, strwsrv, NULL, NULL, NULL, &stwm_info };
139 struct module_info fiform_info = { 0, "fifostrrhead", 0, PIPE_BUF, FIFOHIWAT,
140     FIFOLOWAT };
141 struct module_info fifowm_info = { 0, "fifostrwhead", 0, 0, 0, 0 };
142 struct qinit fifo_strdata = { strrput, NULL, NULL, NULL, NULL, &fiform_info };
143 struct qinit fifo_stwdata = { NULL, strwsrv, NULL, NULL, NULL, &fifowm_info };
144 
145 extern kmutex_t	strresources;	/* protects global resources */
146 extern kmutex_t muxifier;	/* single-threads multiplexor creation */
147 kmutex_t sad_lock;		/* protects sad drivers autopush */
148 
149 static boolean_t msghasdata(mblk_t *bp);
150 #define	msgnodata(bp) (!msghasdata(bp))
151 
152 /*
153  * Stream head locking notes:
154  *	There are four monitors associated with the stream head:
155  *	1. v_stream monitor: in stropen() and strclose() v_lock
156  *		is held while the association of vnode and stream
157  *		head is established or tested for.
158  *	2. open/close/push/pop monitor: sd_lock is held while each
159  *		thread bids for exclusive access to this monitor
160  *		for opening or closing a stream.  In addition, this
161  *		monitor is entered during pushes and pops.  This
162  *		guarantees that during plumbing operations there
163  *		is only one thread trying to change the plumbing.
164  *		Any other threads present in the stream are only
165  *		using the plumbing.
166  *	3. read/write monitor: in the case of read, a thread holds
167  *		sd_lock while trying to get data from the stream
168  *		head queue.  if there is none to fulfill a read
169  *		request, it sets RSLEEP and calls cv_wait_sig() down
170  *		in strwaitq() to await the arrival of new data.
171  *		when new data arrives in strrput(), sd_lock is acquired
172  *		before testing for RSLEEP and calling cv_broadcast().
173  *		the behavior of strwrite(), strwsrv(), and WSLEEP
174  *		mirror this.
175  *	4. ioctl monitor: sd_lock is gotten to ensure that only one
176  *		thread is doing an ioctl at a time.
177  */
178 
179 static int
180 push_mod(queue_t *qp, dev_t *devp, struct stdata *stp, const char *name,
181     int anchor, cred_t *crp)
182 {
183 	int error;
184 	fmodsw_impl_t *fp;
185 
186 	if (stp->sd_flag & (STRHUP|STRDERR|STWRERR)) {
187 		error = (stp->sd_flag & STRHUP) ? ENXIO : EIO;
188 		return (error);
189 	}
190 	if (stp->sd_pushcnt >= nstrpush) {
191 		return (EINVAL);
192 	}
193 
194 	if ((fp = fmodsw_find(name, FMODSW_HOLD | FMODSW_LOAD)) == NULL) {
195 		stp->sd_flag |= STREOPENFAIL;
196 		return (EINVAL);
197 	}
198 
199 	/*
200 	 * push new module and call its open routine via qattach
201 	 */
202 	if ((error = qattach(qp, devp, 0, crp, fp, B_FALSE)) != 0)
203 		return (error);
204 
205 	/*
206 	 * Check to see if caller wants a STREAMS anchor
207 	 * put at this place in the stream, and add if so.
208 	 */
209 	mutex_enter(&stp->sd_lock);
210 	if (anchor == stp->sd_pushcnt)
211 		stp->sd_anchor = stp->sd_pushcnt;
212 	mutex_exit(&stp->sd_lock);
213 
214 	return (0);
215 }
216 
217 /*
218  * Open a stream device.
219  */
220 int
221 stropen(vnode_t *vp, dev_t *devp, int flag, cred_t *crp)
222 {
223 	struct stdata *stp;
224 	queue_t *qp;
225 	int s;
226 	dev_t dummydev;
227 	struct autopush *ap;
228 	int error = 0;
229 	ssize_t	rmin, rmax;
230 	int cloneopen;
231 	queue_t *brq;
232 	major_t major;
233 
234 #ifdef C2_AUDIT
235 	if (audit_active)
236 		audit_stropen(vp, devp, flag, crp);
237 #endif
238 
239 	/*
240 	 * If the stream already exists, wait for any open in progress
241 	 * to complete, then call the open function of each module and
242 	 * driver in the stream.  Otherwise create the stream.
243 	 */
244 	TRACE_1(TR_FAC_STREAMS_FR, TR_STROPEN, "stropen:%p", vp);
245 retry:
246 	mutex_enter(&vp->v_lock);
247 	if ((stp = vp->v_stream) != NULL) {
248 
249 		/*
250 		 * Waiting for stream to be created to device
251 		 * due to another open.
252 		 */
253 	    mutex_exit(&vp->v_lock);
254 
255 	    if (STRMATED(stp)) {
256 		struct stdata *strmatep = stp->sd_mate;
257 
258 		STRLOCKMATES(stp);
259 		if (strmatep->sd_flag & (STWOPEN|STRCLOSE|STRPLUMB)) {
260 			if (flag & (FNDELAY|FNONBLOCK)) {
261 				error = EAGAIN;
262 				mutex_exit(&strmatep->sd_lock);
263 				goto ckreturn;
264 			}
265 			mutex_exit(&stp->sd_lock);
266 			if (!cv_wait_sig(&strmatep->sd_monitor,
267 			    &strmatep->sd_lock)) {
268 				error = EINTR;
269 				mutex_exit(&strmatep->sd_lock);
270 				mutex_enter(&stp->sd_lock);
271 				goto ckreturn;
272 			}
273 			mutex_exit(&strmatep->sd_lock);
274 			goto retry;
275 		}
276 		if (stp->sd_flag & (STWOPEN|STRCLOSE|STRPLUMB)) {
277 			if (flag & (FNDELAY|FNONBLOCK)) {
278 				error = EAGAIN;
279 				mutex_exit(&strmatep->sd_lock);
280 				goto ckreturn;
281 			}
282 			mutex_exit(&strmatep->sd_lock);
283 			if (!cv_wait_sig(&stp->sd_monitor, &stp->sd_lock)) {
284 				error = EINTR;
285 				goto ckreturn;
286 			}
287 			mutex_exit(&stp->sd_lock);
288 			goto retry;
289 		}
290 
291 		if (stp->sd_flag & (STRDERR|STWRERR)) {
292 			error = EIO;
293 			mutex_exit(&strmatep->sd_lock);
294 			goto ckreturn;
295 		}
296 
297 		stp->sd_flag |= STWOPEN;
298 		STRUNLOCKMATES(stp);
299 	    } else {
300 		mutex_enter(&stp->sd_lock);
301 		if (stp->sd_flag & (STWOPEN|STRCLOSE|STRPLUMB)) {
302 			if (flag & (FNDELAY|FNONBLOCK)) {
303 				error = EAGAIN;
304 				goto ckreturn;
305 			}
306 			if (!cv_wait_sig(&stp->sd_monitor, &stp->sd_lock)) {
307 				error = EINTR;
308 				goto ckreturn;
309 			}
310 			mutex_exit(&stp->sd_lock);
311 			goto retry;  /* could be clone! */
312 		}
313 
314 		if (stp->sd_flag & (STRDERR|STWRERR)) {
315 			error = EIO;
316 			goto ckreturn;
317 		}
318 
319 		stp->sd_flag |= STWOPEN;
320 		mutex_exit(&stp->sd_lock);
321 	    }
322 
323 		/*
324 		 * Open all modules and devices down stream to notify
325 		 * that another user is streaming.  For modules, set the
326 		 * last argument to MODOPEN and do not pass any open flags.
327 		 * Ignore dummydev since this is not the first open.
328 		 */
329 	    claimstr(stp->sd_wrq);
330 	    qp = stp->sd_wrq;
331 	    while (_SAMESTR(qp)) {
332 		qp = qp->q_next;
333 		if ((error = qreopen(_RD(qp), devp, flag, crp)) != 0)
334 			break;
335 	    }
336 	    releasestr(stp->sd_wrq);
337 	    mutex_enter(&stp->sd_lock);
338 	    stp->sd_flag &= ~(STRHUP|STWOPEN|STRDERR|STWRERR);
339 	    stp->sd_rerror = 0;
340 	    stp->sd_werror = 0;
341 ckreturn:
342 	    cv_broadcast(&stp->sd_monitor);
343 	    mutex_exit(&stp->sd_lock);
344 	    return (error);
345 	}
346 
347 	/*
348 	 * This vnode isn't streaming.  SPECFS already
349 	 * checked for multiple vnodes pointing to the
350 	 * same stream, so create a stream to the driver.
351 	 */
352 	qp = allocq();
353 	stp = shalloc(qp);
354 
355 	/*
356 	 * Initialize stream head.  shalloc() has given us
357 	 * exclusive access, and we have the vnode locked;
358 	 * we can do whatever we want with stp.
359 	 */
360 	stp->sd_flag = STWOPEN;
361 	stp->sd_siglist = NULL;
362 	stp->sd_pollist.ph_list = NULL;
363 	stp->sd_sigflags = 0;
364 	stp->sd_mark = NULL;
365 	stp->sd_closetime = STRTIMOUT;
366 	stp->sd_sidp = NULL;
367 	stp->sd_pgidp = NULL;
368 	stp->sd_vnode = vp;
369 	stp->sd_rerror = 0;
370 	stp->sd_werror = 0;
371 	stp->sd_wroff = 0;
372 	stp->sd_tail = 0;
373 	stp->sd_iocblk = NULL;
374 	stp->sd_pushcnt = 0;
375 	stp->sd_qn_minpsz = 0;
376 	stp->sd_qn_maxpsz = INFPSZ - 1;	/* used to check for initialization */
377 	stp->sd_maxblk = INFPSZ;
378 	qp->q_ptr = _WR(qp)->q_ptr = stp;
379 	STREAM(qp) = STREAM(_WR(qp)) = stp;
380 	vp->v_stream = stp;
381 	mutex_exit(&vp->v_lock);
382 	if (vp->v_type == VFIFO) {
383 		stp->sd_flag |= OLDNDELAY;
384 		/*
385 		 * This means, both for pipes and fifos
386 		 * strwrite will send SIGPIPE if the other
387 		 * end is closed. For putmsg it depends
388 		 * on whether it is a XPG4_2 application
389 		 * or not
390 		 */
391 		stp->sd_wput_opt = SW_SIGPIPE;
392 
393 		/* setq might sleep in kmem_alloc - avoid holding locks. */
394 		setq(qp, &fifo_strdata, &fifo_stwdata, NULL, QMTSAFE,
395 		    SQ_CI|SQ_CO, B_FALSE);
396 
397 		set_qend(qp);
398 		stp->sd_strtab = fifo_getinfo();
399 		_WR(qp)->q_nfsrv = _WR(qp);
400 		qp->q_nfsrv = qp;
401 		/*
402 		 * Wake up others that are waiting for stream to be created.
403 		 */
404 		mutex_enter(&stp->sd_lock);
405 		/*
406 		 * nothing is be pushed on stream yet, so
407 		 * optimized stream head packetsizes are just that
408 		 * of the read queue
409 		 */
410 		stp->sd_qn_minpsz = qp->q_minpsz;
411 		stp->sd_qn_maxpsz = qp->q_maxpsz;
412 		stp->sd_flag &= ~STWOPEN;
413 		goto fifo_opendone;
414 	}
415 	/* setq might sleep in kmem_alloc - avoid holding locks. */
416 	setq(qp, &strdata, &stwdata, NULL, QMTSAFE, SQ_CI|SQ_CO, B_FALSE);
417 
418 	set_qend(qp);
419 
420 	/*
421 	 * Open driver and create stream to it (via qattach).
422 	 */
423 	cloneopen = (getmajor(*devp) == clone_major);
424 	if ((error = qattach(qp, devp, flag, crp, NULL, B_FALSE)) != 0) {
425 		mutex_enter(&vp->v_lock);
426 		vp->v_stream = NULL;
427 		mutex_exit(&vp->v_lock);
428 		mutex_enter(&stp->sd_lock);
429 		cv_broadcast(&stp->sd_monitor);
430 		mutex_exit(&stp->sd_lock);
431 		freeq(_RD(qp));
432 		shfree(stp);
433 		return (error);
434 	}
435 	/*
436 	 * Set sd_strtab after open in order to handle clonable drivers
437 	 */
438 	stp->sd_strtab = STREAMSTAB(getmajor(*devp));
439 
440 	/*
441 	 * Historical note: dummydev used to be be prior to the initial
442 	 * open (via qattach above), which made the value seen
443 	 * inconsistent between an I_PUSH and an autopush of a module.
444 	 */
445 	dummydev = *devp;
446 
447 	/*
448 	 * For clone open of old style (Q not associated) network driver,
449 	 * push DRMODNAME module to handle DL_ATTACH/DL_DETACH
450 	 */
451 	brq = _RD(_WR(qp)->q_next);
452 	major = getmajor(*devp);
453 	if (push_drcompat && cloneopen && NETWORK_DRV(major) &&
454 	    ((brq->q_flag & _QASSOCIATED) == 0)) {
455 		if (push_mod(qp, &dummydev, stp, DRMODNAME, 0, crp) != 0)
456 			cmn_err(CE_WARN, "cannot push " DRMODNAME
457 			    " streams module");
458 	}
459 
460 	/*
461 	 * check for autopush
462 	 */
463 	mutex_enter(&sad_lock);
464 	ap = strphash(getemajor(*devp));
465 #define	DEVT(ap)	makedevice(ap->ap_major, ap->ap_minor)
466 #define	DEVLT(ap)	makedevice(ap->ap_major, ap->ap_lastminor)
467 
468 	while (ap) {
469 		if (ap->ap_major == (getemajor(*devp))) {
470 			if (ap->ap_type == SAP_ALL)
471 				break;
472 			else if ((ap->ap_type == SAP_ONE) &&
473 			    (getminor(DEVT(ap)) == getminor(*devp)))
474 				break;
475 			else if (ap->ap_type == SAP_RANGE &&
476 			    getminor(*devp) >= getminor(DEVT(ap)) &&
477 			    getminor(*devp) <= getminor(DEVLT(ap)))
478 				break;
479 		}
480 		ap = ap->ap_nextp;
481 	}
482 	if (ap == NULL) {
483 		mutex_exit(&sad_lock);
484 		goto opendone;
485 	}
486 	ap->ap_cnt++;
487 	mutex_exit(&sad_lock);
488 	for (s = 0; s < ap->ap_npush; s++) {
489 		error = push_mod(qp, &dummydev, stp, ap->ap_list[s],
490 		    ap->ap_anchor, crp);
491 		if (error != 0)
492 			break;
493 	}
494 	mutex_enter(&sad_lock);
495 	if (--(ap->ap_cnt) <= 0)
496 		ap_free(ap);
497 	mutex_exit(&sad_lock);
498 
499 	/*
500 	 * let specfs know that open failed part way through
501 	 */
502 
503 	if (error) {
504 		mutex_enter(&stp->sd_lock);
505 		stp->sd_flag |= STREOPENFAIL;
506 		mutex_exit(&stp->sd_lock);
507 	}
508 
509 opendone:
510 
511 	/*
512 	 * Wake up others that are waiting for stream to be created.
513 	 */
514 	mutex_enter(&stp->sd_lock);
515 	stp->sd_flag &= ~STWOPEN;
516 
517 	/*
518 	 * As a performance concern we are caching the values of
519 	 * q_minpsz and q_maxpsz of the module below the stream
520 	 * head in the stream head.
521 	 */
522 	mutex_enter(QLOCK(stp->sd_wrq->q_next));
523 	rmin = stp->sd_wrq->q_next->q_minpsz;
524 	rmax = stp->sd_wrq->q_next->q_maxpsz;
525 	mutex_exit(QLOCK(stp->sd_wrq->q_next));
526 
527 	/* do this processing here as a performance concern */
528 	if (strmsgsz != 0) {
529 		if (rmax == INFPSZ)
530 			rmax = strmsgsz;
531 		else
532 			rmax = MIN(strmsgsz, rmax);
533 	}
534 
535 	mutex_enter(QLOCK(stp->sd_wrq));
536 	stp->sd_qn_minpsz = rmin;
537 	stp->sd_qn_maxpsz = rmax;
538 	mutex_exit(QLOCK(stp->sd_wrq));
539 
540 fifo_opendone:
541 	cv_broadcast(&stp->sd_monitor);
542 	mutex_exit(&stp->sd_lock);
543 	return (error);
544 }
545 
546 static int strsink(queue_t *, mblk_t *);
547 static struct qinit deadrend = {
548 	strsink, NULL, NULL, NULL, NULL, &strm_info, NULL
549 };
550 static struct qinit deadwend = {
551 	NULL, NULL, NULL, NULL, NULL, &stwm_info, NULL
552 };
553 
554 /*
555  * Close a stream.
556  * This is called from closef() on the last close of an open stream.
557  * Strclean() will already have removed the siglist and pollist
558  * information, so all that remains is to remove all multiplexor links
559  * for the stream, pop all the modules (and the driver), and free the
560  * stream structure.
561  */
562 
563 int
564 strclose(struct vnode *vp, int flag, cred_t *crp)
565 {
566 	struct stdata *stp;
567 	queue_t *qp;
568 	int rval;
569 	int freestp = 1;
570 	queue_t *rmq;
571 
572 #ifdef C2_AUDIT
573 	if (audit_active)
574 		audit_strclose(vp, flag, crp);
575 #endif
576 
577 	TRACE_1(TR_FAC_STREAMS_FR,
578 		TR_STRCLOSE, "strclose:%p", vp);
579 	ASSERT(vp->v_stream);
580 
581 	stp = vp->v_stream;
582 	ASSERT(!(stp->sd_flag & STPLEX));
583 	qp = stp->sd_wrq;
584 
585 	/*
586 	 * Needed so that strpoll will return non-zero for this fd.
587 	 * Note that with POLLNOERR STRHUP does still cause POLLHUP.
588 	 */
589 	mutex_enter(&stp->sd_lock);
590 	stp->sd_flag |= STRHUP;
591 	mutex_exit(&stp->sd_lock);
592 
593 	/*
594 	 * If the registered process or process group did not have an
595 	 * open instance of this stream then strclean would not be
596 	 * called. Thus at the time of closing all remaining siglist entries
597 	 * are removed.
598 	 */
599 	if (stp->sd_siglist != NULL)
600 		strcleanall(vp);
601 
602 	ASSERT(stp->sd_siglist == NULL);
603 	ASSERT(stp->sd_sigflags == 0);
604 
605 	if (STRMATED(stp)) {
606 		struct stdata *strmatep = stp->sd_mate;
607 		int waited = 1;
608 
609 		STRLOCKMATES(stp);
610 		while (waited) {
611 			waited = 0;
612 			while (stp->sd_flag & (STWOPEN|STRCLOSE|STRPLUMB)) {
613 				mutex_exit(&strmatep->sd_lock);
614 				cv_wait(&stp->sd_monitor, &stp->sd_lock);
615 				mutex_exit(&stp->sd_lock);
616 				STRLOCKMATES(stp);
617 				waited = 1;
618 			}
619 			while (strmatep->sd_flag &
620 			    (STWOPEN|STRCLOSE|STRPLUMB)) {
621 				mutex_exit(&stp->sd_lock);
622 				cv_wait(&strmatep->sd_monitor,
623 				    &strmatep->sd_lock);
624 				mutex_exit(&strmatep->sd_lock);
625 				STRLOCKMATES(stp);
626 				waited = 1;
627 			}
628 		}
629 		stp->sd_flag |= STRCLOSE;
630 		STRUNLOCKMATES(stp);
631 	} else {
632 		mutex_enter(&stp->sd_lock);
633 		stp->sd_flag |= STRCLOSE;
634 		mutex_exit(&stp->sd_lock);
635 	}
636 
637 	ASSERT(qp->q_first == NULL);	/* No more delayed write */
638 
639 	/* Check if an I_LINK was ever done on this stream */
640 	if (stp->sd_flag & STRHASLINKS) {
641 		(void) munlinkall(stp, LINKCLOSE|LINKNORMAL, crp, &rval);
642 	}
643 
644 	while (_SAMESTR(qp)) {
645 		/*
646 		 * Holding sd_lock prevents q_next from changing in
647 		 * this stream.
648 		 */
649 		mutex_enter(&stp->sd_lock);
650 		if (!(flag & (FNDELAY|FNONBLOCK)) && (stp->sd_closetime > 0)) {
651 
652 			/*
653 			 * sleep until awakened by strwsrv() or timeout
654 			 */
655 			for (;;) {
656 				mutex_enter(QLOCK(qp->q_next));
657 				if (!(qp->q_next->q_mblkcnt)) {
658 					mutex_exit(QLOCK(qp->q_next));
659 					break;
660 				}
661 				stp->sd_flag |= WSLEEP;
662 
663 				/* ensure strwsrv gets enabled */
664 				qp->q_next->q_flag |= QWANTW;
665 				mutex_exit(QLOCK(qp->q_next));
666 				/* get out if we timed out or recv'd a signal */
667 				if (str_cv_wait(&qp->q_wait, &stp->sd_lock,
668 				    stp->sd_closetime, 0) <= 0) {
669 					break;
670 				}
671 			}
672 			stp->sd_flag &= ~WSLEEP;
673 		}
674 		mutex_exit(&stp->sd_lock);
675 
676 		rmq = qp->q_next;
677 		if (rmq->q_flag & QISDRV) {
678 			ASSERT(!_SAMESTR(rmq));
679 			wait_sq_svc(_RD(qp)->q_syncq);
680 		}
681 
682 		qdetach(_RD(rmq), 1, flag, crp, B_FALSE);
683 	}
684 
685 	/*
686 	 * Since we call pollwakeup in close() now, the poll list should
687 	 * be empty in most cases. The only exception is the layered devices
688 	 * (e.g. the console drivers with redirection modules pushed on top
689 	 * of it).  We have to do this after calling qdetach() because
690 	 * the redirection module won't have torn down the console
691 	 * redirection until after qdetach() has been invoked.
692 	 */
693 	if (stp->sd_pollist.ph_list != NULL) {
694 		pollwakeup(&stp->sd_pollist, POLLERR);
695 		pollhead_clean(&stp->sd_pollist);
696 	}
697 	ASSERT(stp->sd_pollist.ph_list == NULL);
698 	ASSERT(stp->sd_sidp == NULL);
699 	ASSERT(stp->sd_pgidp == NULL);
700 
701 	/* Prevent qenable from re-enabling the stream head queue */
702 	disable_svc(_RD(qp));
703 
704 	/*
705 	 * Wait until service procedure of each queue is
706 	 * run, if QINSERVICE is set.
707 	 */
708 	wait_svc(_RD(qp));
709 
710 	/*
711 	 * Now, flush both queues.
712 	 */
713 	flushq(_RD(qp), FLUSHALL);
714 	flushq(qp, FLUSHALL);
715 
716 	/*
717 	 * If the write queue of the stream head is pointing to a
718 	 * read queue, we have a twisted stream.  If the read queue
719 	 * is alive, convert the stream head queues into a dead end.
720 	 * If the read queue is dead, free the dead pair.
721 	 */
722 	if (qp->q_next && !_SAMESTR(qp)) {
723 		if (qp->q_next->q_qinfo == &deadrend) {	/* half-closed pipe */
724 			flushq(qp->q_next, FLUSHALL); /* ensure no message */
725 			shfree(qp->q_next->q_stream);
726 			freeq(qp->q_next);
727 			freeq(_RD(qp));
728 		} else if (qp->q_next == _RD(qp)) {	/* fifo */
729 			freeq(_RD(qp));
730 		} else {				/* pipe */
731 			freestp = 0;
732 			/*
733 			 * The q_info pointers are never accessed when
734 			 * SQLOCK is held.
735 			 */
736 			ASSERT(qp->q_syncq == _RD(qp)->q_syncq);
737 			mutex_enter(SQLOCK(qp->q_syncq));
738 			qp->q_qinfo = &deadwend;
739 			_RD(qp)->q_qinfo = &deadrend;
740 			mutex_exit(SQLOCK(qp->q_syncq));
741 		}
742 	} else {
743 		freeq(_RD(qp)); /* free stream head queue pair */
744 	}
745 
746 	mutex_enter(&vp->v_lock);
747 	if (stp->sd_iocblk) {
748 		if (stp->sd_iocblk != (mblk_t *)-1) {
749 			freemsg(stp->sd_iocblk);
750 		}
751 		stp->sd_iocblk = NULL;
752 	}
753 	stp->sd_vnode = NULL;
754 	vp->v_stream = NULL;
755 	mutex_exit(&vp->v_lock);
756 	mutex_enter(&stp->sd_lock);
757 	stp->sd_flag &= ~STRCLOSE;
758 	cv_broadcast(&stp->sd_monitor);
759 	mutex_exit(&stp->sd_lock);
760 
761 	if (freestp)
762 		shfree(stp);
763 	return (0);
764 }
765 
766 static int
767 strsink(queue_t *q, mblk_t *bp)
768 {
769 	struct copyresp *resp;
770 
771 	switch (bp->b_datap->db_type) {
772 	case M_FLUSH:
773 		if ((*bp->b_rptr & FLUSHW) && !(bp->b_flag & MSGNOLOOP)) {
774 			*bp->b_rptr &= ~FLUSHR;
775 			bp->b_flag |= MSGNOLOOP;
776 			/*
777 			 * Protect against the driver passing up
778 			 * messages after it has done a qprocsoff.
779 			 */
780 			if (_OTHERQ(q)->q_next == NULL)
781 				freemsg(bp);
782 			else
783 				qreply(q, bp);
784 		} else {
785 			freemsg(bp);
786 		}
787 		break;
788 
789 	case M_COPYIN:
790 	case M_COPYOUT:
791 		if (bp->b_cont) {
792 			freemsg(bp->b_cont);
793 			bp->b_cont = NULL;
794 		}
795 		bp->b_datap->db_type = M_IOCDATA;
796 		bp->b_wptr = bp->b_rptr + sizeof (struct copyresp);
797 		resp = (struct copyresp *)bp->b_rptr;
798 		resp->cp_rval = (caddr_t)1;	/* failure */
799 		/*
800 		 * Protect against the driver passing up
801 		 * messages after it has done a qprocsoff.
802 		 */
803 		if (_OTHERQ(q)->q_next == NULL)
804 			freemsg(bp);
805 		else
806 			qreply(q, bp);
807 		break;
808 
809 	case M_IOCTL:
810 		if (bp->b_cont) {
811 			freemsg(bp->b_cont);
812 			bp->b_cont = NULL;
813 		}
814 		bp->b_datap->db_type = M_IOCNAK;
815 		/*
816 		 * Protect against the driver passing up
817 		 * messages after it has done a qprocsoff.
818 		 */
819 		if (_OTHERQ(q)->q_next == NULL)
820 			freemsg(bp);
821 		else
822 			qreply(q, bp);
823 		break;
824 
825 	default:
826 		freemsg(bp);
827 		break;
828 	}
829 
830 	return (0);
831 }
832 
833 /*
834  * Clean up after a process when it closes a stream.  This is called
835  * from closef for all closes, whereas strclose is called only for the
836  * last close on a stream.  The siglist is scanned for entries for the
837  * current process, and these are removed.
838  */
839 void
840 strclean(struct vnode *vp)
841 {
842 	strsig_t *ssp, *pssp, *tssp;
843 	stdata_t *stp;
844 	int update = 0;
845 
846 	TRACE_1(TR_FAC_STREAMS_FR,
847 		TR_STRCLEAN, "strclean:%p", vp);
848 	stp = vp->v_stream;
849 	pssp = NULL;
850 	mutex_enter(&stp->sd_lock);
851 	ssp = stp->sd_siglist;
852 	while (ssp) {
853 		if (ssp->ss_pidp == curproc->p_pidp) {
854 			tssp = ssp->ss_next;
855 			if (pssp)
856 				pssp->ss_next = tssp;
857 			else
858 				stp->sd_siglist = tssp;
859 			mutex_enter(&pidlock);
860 			PID_RELE(ssp->ss_pidp);
861 			mutex_exit(&pidlock);
862 			kmem_free(ssp, sizeof (strsig_t));
863 			update = 1;
864 			ssp = tssp;
865 		} else {
866 			pssp = ssp;
867 			ssp = ssp->ss_next;
868 		}
869 	}
870 	if (update) {
871 		stp->sd_sigflags = 0;
872 		for (ssp = stp->sd_siglist; ssp; ssp = ssp->ss_next)
873 			stp->sd_sigflags |= ssp->ss_events;
874 	}
875 	mutex_exit(&stp->sd_lock);
876 }
877 
878 /*
879  * Used on the last close to remove any remaining items on the siglist.
880  * These could be present on the siglist due to I_ESETSIG calls that
881  * use process groups or processed that do not have an open file descriptor
882  * for this stream (Such entries would not be removed by strclean).
883  */
884 static void
885 strcleanall(struct vnode *vp)
886 {
887 	strsig_t *ssp, *nssp;
888 	stdata_t *stp;
889 
890 	stp = vp->v_stream;
891 	mutex_enter(&stp->sd_lock);
892 	ssp = stp->sd_siglist;
893 	stp->sd_siglist = NULL;
894 	while (ssp) {
895 		nssp = ssp->ss_next;
896 		mutex_enter(&pidlock);
897 		PID_RELE(ssp->ss_pidp);
898 		mutex_exit(&pidlock);
899 		kmem_free(ssp, sizeof (strsig_t));
900 		ssp = nssp;
901 	}
902 	stp->sd_sigflags = 0;
903 	mutex_exit(&stp->sd_lock);
904 }
905 
906 /*
907  * Retrieve the next message from the logical stream head read queue
908  * using either rwnext (if sync stream) or getq_noenab.
909  * It is the callers responsibility to call qbackenable after
910  * it is finished with the message. The caller should not call
911  * qbackenable until after any putback calls to avoid spurious backenabling.
912  */
913 mblk_t *
914 strget(struct stdata *stp, queue_t *q, struct uio *uiop, int first,
915     int *errorp)
916 {
917 	mblk_t *bp;
918 	int error;
919 
920 	ASSERT(MUTEX_HELD(&stp->sd_lock));
921 	/* Holding sd_lock prevents the read queue from changing  */
922 
923 	if (uiop != NULL && stp->sd_struiordq != NULL &&
924 	    q->q_first == NULL &&
925 	    (!first || (stp->sd_wakeq & RSLEEP))) {
926 		/*
927 		 * Stream supports rwnext() for the read side.
928 		 * If this is the first time we're called by e.g. strread
929 		 * only do the downcall if there is a deferred wakeup
930 		 * (registered in sd_wakeq).
931 		 */
932 		struiod_t uiod;
933 
934 		if (first)
935 			stp->sd_wakeq &= ~RSLEEP;
936 
937 		(void) uiodup(uiop, &uiod.d_uio, uiod.d_iov,
938 			sizeof (uiod.d_iov) / sizeof (*uiod.d_iov));
939 		uiod.d_mp = 0;
940 		/*
941 		 * Mark that a thread is in rwnext on the read side
942 		 * to prevent strrput from nacking ioctls immediately.
943 		 * When the last concurrent rwnext returns
944 		 * the ioctls are nack'ed.
945 		 */
946 		ASSERT(MUTEX_HELD(&stp->sd_lock));
947 		stp->sd_struiodnak++;
948 		/*
949 		 * Note: rwnext will drop sd_lock.
950 		 */
951 		error = rwnext(q, &uiod);
952 		ASSERT(MUTEX_NOT_HELD(&stp->sd_lock));
953 		mutex_enter(&stp->sd_lock);
954 		stp->sd_struiodnak--;
955 		while (stp->sd_struiodnak == 0 &&
956 		    ((bp = stp->sd_struionak) != NULL)) {
957 			stp->sd_struionak = bp->b_next;
958 			bp->b_next = NULL;
959 			bp->b_datap->db_type = M_IOCNAK;
960 			/*
961 			 * Protect against the driver passing up
962 			 * messages after it has done a qprocsoff.
963 			 */
964 			if (_OTHERQ(q)->q_next == NULL)
965 				freemsg(bp);
966 			else {
967 				mutex_exit(&stp->sd_lock);
968 				qreply(q, bp);
969 				mutex_enter(&stp->sd_lock);
970 			}
971 		}
972 		ASSERT(MUTEX_HELD(&stp->sd_lock));
973 		if (error == 0 || error == EWOULDBLOCK) {
974 			if ((bp = uiod.d_mp) != NULL) {
975 				*errorp = 0;
976 				ASSERT(MUTEX_HELD(&stp->sd_lock));
977 				return (bp);
978 			}
979 			error = 0;
980 		} else if (error == EINVAL) {
981 			/*
982 			 * The stream plumbing must have
983 			 * changed while we were away, so
984 			 * just turn off rwnext()s.
985 			 */
986 			error = 0;
987 		} else if (error == EBUSY) {
988 			/*
989 			 * The module might have data in transit using putnext
990 			 * Fall back on waiting + getq.
991 			 */
992 			error = 0;
993 		} else {
994 			*errorp = error;
995 			ASSERT(MUTEX_HELD(&stp->sd_lock));
996 			return (NULL);
997 		}
998 		/*
999 		 * Try a getq in case a rwnext() generated mblk
1000 		 * has bubbled up via strrput().
1001 		 */
1002 	}
1003 	*errorp = 0;
1004 	ASSERT(MUTEX_HELD(&stp->sd_lock));
1005 	return (getq_noenab(q));
1006 }
1007 
1008 /*
1009  * Copy out the message pointed to by `bp' into the uio pointed to by `uiop'.
1010  * If the message does not fit in the uio the remainder of it is returned;
1011  * otherwise NULL is returned.  Any embedded zero-length mblk_t's are
1012  * consumed, even if uio_resid reaches zero.  On error, `*errorp' is set to
1013  * the error code, the message is consumed, and NULL is returned.
1014  */
1015 static mblk_t *
1016 struiocopyout(mblk_t *bp, struct uio *uiop, int *errorp)
1017 {
1018 	int error;
1019 	ptrdiff_t n;
1020 	mblk_t *nbp;
1021 
1022 	ASSERT(bp->b_wptr >= bp->b_rptr);
1023 
1024 	do {
1025 		if ((n = MIN(uiop->uio_resid, MBLKL(bp))) != 0) {
1026 			ASSERT(n > 0);
1027 
1028 			error = uiomove(bp->b_rptr, n, UIO_READ, uiop);
1029 			if (error != 0) {
1030 				freemsg(bp);
1031 				*errorp = error;
1032 				return (NULL);
1033 			}
1034 		}
1035 
1036 		bp->b_rptr += n;
1037 		while (bp != NULL && (bp->b_rptr >= bp->b_wptr)) {
1038 			nbp = bp;
1039 			bp = bp->b_cont;
1040 			freeb(nbp);
1041 		}
1042 	} while (bp != NULL && uiop->uio_resid > 0);
1043 
1044 	*errorp = 0;
1045 	return (bp);
1046 }
1047 
1048 /*
1049  * Read a stream according to the mode flags in sd_flag:
1050  *
1051  * (default mode)		- Byte stream, msg boundaries are ignored
1052  * RD_MSGDIS (msg discard)	- Read on msg boundaries and throw away
1053  *				any data remaining in msg
1054  * RD_MSGNODIS (msg non-discard) - Read on msg boundaries and put back
1055  *				any remaining data on head of read queue
1056  *
1057  * Consume readable messages on the front of the queue until
1058  * ttolwp(curthread)->lwp_count
1059  * is satisfied, the readable messages are exhausted, or a message
1060  * boundary is reached in a message mode.  If no data was read and
1061  * the stream was not opened with the NDELAY flag, block until data arrives.
1062  * Otherwise return the data read and update the count.
1063  *
1064  * In default mode a 0 length message signifies end-of-file and terminates
1065  * a read in progress.  The 0 length message is removed from the queue
1066  * only if it is the only message read (no data is read).
1067  *
1068  * An attempt to read an M_PROTO or M_PCPROTO message results in an
1069  * EBADMSG error return, unless either RD_PROTDAT or RD_PROTDIS are set.
1070  * If RD_PROTDAT is set, M_PROTO and M_PCPROTO messages are read as data.
1071  * If RD_PROTDIS is set, the M_PROTO and M_PCPROTO parts of the message
1072  * are unlinked from and M_DATA blocks in the message, the protos are
1073  * thrown away, and the data is read.
1074  */
1075 /* ARGSUSED */
1076 int
1077 strread(struct vnode *vp, struct uio *uiop, cred_t *crp)
1078 {
1079 	struct stdata *stp;
1080 	mblk_t *bp, *nbp;
1081 	queue_t *q;
1082 	int error = 0;
1083 	uint_t old_sd_flag;
1084 	int first;
1085 	char rflg;
1086 	uint_t mark;		/* Contains MSG*MARK and _LASTMARK */
1087 #define	_LASTMARK	0x8000	/* Distinct from MSG*MARK */
1088 	short delim;
1089 	unsigned char pri = 0;
1090 	char waitflag;
1091 	unsigned char type;
1092 
1093 	TRACE_1(TR_FAC_STREAMS_FR,
1094 		TR_STRREAD_ENTER, "strread:%p", vp);
1095 	ASSERT(vp->v_stream);
1096 	stp = vp->v_stream;
1097 
1098 	if (stp->sd_sidp != NULL && stp->sd_vnode->v_type != VFIFO)
1099 		if (error = straccess(stp, JCREAD))
1100 			return (error);
1101 
1102 	mutex_enter(&stp->sd_lock);
1103 	if (stp->sd_flag & (STRDERR|STPLEX)) {
1104 		error = strgeterr(stp, STRDERR|STPLEX, 0);
1105 		if (error != 0) {
1106 			mutex_exit(&stp->sd_lock);
1107 			return (error);
1108 		}
1109 	}
1110 
1111 	/*
1112 	 * Loop terminates when uiop->uio_resid == 0.
1113 	 */
1114 	rflg = 0;
1115 	waitflag = READWAIT;
1116 	q = _RD(stp->sd_wrq);
1117 	for (;;) {
1118 		ASSERT(MUTEX_HELD(&stp->sd_lock));
1119 		old_sd_flag = stp->sd_flag;
1120 		mark = 0;
1121 		delim = 0;
1122 		first = 1;
1123 		while ((bp = strget(stp, q, uiop, first, &error)) == NULL) {
1124 			int done = 0;
1125 
1126 			ASSERT(MUTEX_HELD(&stp->sd_lock));
1127 
1128 			if (error != 0)
1129 				goto oops;
1130 
1131 			if (stp->sd_flag & (STRHUP|STREOF)) {
1132 				goto oops;
1133 			}
1134 			if (rflg && !(stp->sd_flag & STRDELIM)) {
1135 				goto oops;
1136 			}
1137 			/*
1138 			 * If a read(fd,buf,0) has been done, there is no
1139 			 * need to sleep. We always have zero bytes to
1140 			 * return.
1141 			 */
1142 			if (uiop->uio_resid == 0) {
1143 				goto oops;
1144 			}
1145 
1146 			qbackenable(q, 0);
1147 
1148 			TRACE_3(TR_FAC_STREAMS_FR, TR_STRREAD_WAIT,
1149 				"strread calls strwaitq:%p, %p, %p",
1150 				vp, uiop, crp);
1151 			if ((error = strwaitq(stp, waitflag, uiop->uio_resid,
1152 			    uiop->uio_fmode, -1, &done)) != 0 || done) {
1153 				TRACE_3(TR_FAC_STREAMS_FR, TR_STRREAD_DONE,
1154 					"strread error or done:%p, %p, %p",
1155 					vp, uiop, crp);
1156 				if ((uiop->uio_fmode & FNDELAY) &&
1157 				    (stp->sd_flag & OLDNDELAY) &&
1158 				    (error == EAGAIN))
1159 					error = 0;
1160 				goto oops;
1161 			}
1162 			TRACE_3(TR_FAC_STREAMS_FR, TR_STRREAD_AWAKE,
1163 				"strread awakes:%p, %p, %p", vp, uiop, crp);
1164 			if (stp->sd_sidp != NULL &&
1165 			    stp->sd_vnode->v_type != VFIFO) {
1166 				mutex_exit(&stp->sd_lock);
1167 				if (error = straccess(stp, JCREAD))
1168 					goto oops1;
1169 				mutex_enter(&stp->sd_lock);
1170 			}
1171 			first = 0;
1172 		}
1173 		ASSERT(MUTEX_HELD(&stp->sd_lock));
1174 		ASSERT(bp);
1175 		pri = bp->b_band;
1176 		/*
1177 		 * Extract any mark information. If the message is not
1178 		 * completely consumed this information will be put in the mblk
1179 		 * that is putback.
1180 		 * If MSGMARKNEXT is set and the message is completely consumed
1181 		 * the STRATMARK flag will be set below. Likewise, if
1182 		 * MSGNOTMARKNEXT is set and the message is
1183 		 * completely consumed STRNOTATMARK will be set.
1184 		 *
1185 		 * For some unknown reason strread only breaks the read at the
1186 		 * last mark.
1187 		 */
1188 		mark = bp->b_flag & (MSGMARK | MSGMARKNEXT | MSGNOTMARKNEXT);
1189 		ASSERT((mark & (MSGMARKNEXT|MSGNOTMARKNEXT)) !=
1190 			(MSGMARKNEXT|MSGNOTMARKNEXT));
1191 		if (mark != 0 && bp == stp->sd_mark) {
1192 			if (rflg) {
1193 				putback(stp, q, bp, pri);
1194 				goto oops;
1195 			}
1196 			mark |= _LASTMARK;
1197 			stp->sd_mark = NULL;
1198 		}
1199 		if ((stp->sd_flag & STRDELIM) && (bp->b_flag & MSGDELIM))
1200 			delim = 1;
1201 		mutex_exit(&stp->sd_lock);
1202 
1203 		if (STREAM_NEEDSERVICE(stp))
1204 			stream_runservice(stp);
1205 
1206 		type = bp->b_datap->db_type;
1207 
1208 		switch (type) {
1209 
1210 		case M_DATA:
1211 ismdata:
1212 			if (msgnodata(bp)) {
1213 				if (mark || delim) {
1214 					freemsg(bp);
1215 				} else if (rflg) {
1216 
1217 					/*
1218 					 * If already read data put zero
1219 					 * length message back on queue else
1220 					 * free msg and return 0.
1221 					 */
1222 					bp->b_band = pri;
1223 					mutex_enter(&stp->sd_lock);
1224 					putback(stp, q, bp, pri);
1225 					mutex_exit(&stp->sd_lock);
1226 				} else {
1227 					freemsg(bp);
1228 				}
1229 				error =  0;
1230 				goto oops1;
1231 			}
1232 
1233 			rflg = 1;
1234 			waitflag |= NOINTR;
1235 			bp = struiocopyout(bp, uiop, &error);
1236 			if (error != 0)
1237 				goto oops1;
1238 
1239 			mutex_enter(&stp->sd_lock);
1240 			if (bp) {
1241 				/*
1242 				 * Have remaining data in message.
1243 				 * Free msg if in discard mode.
1244 				 */
1245 				if (stp->sd_read_opt & RD_MSGDIS) {
1246 					freemsg(bp);
1247 				} else {
1248 					bp->b_band = pri;
1249 					if ((mark & _LASTMARK) &&
1250 					    (stp->sd_mark == NULL))
1251 						stp->sd_mark = bp;
1252 					bp->b_flag |= mark & ~_LASTMARK;
1253 					if (delim)
1254 						bp->b_flag |= MSGDELIM;
1255 					if (msgnodata(bp))
1256 						freemsg(bp);
1257 					else
1258 						putback(stp, q, bp, pri);
1259 				}
1260 			} else {
1261 				/*
1262 				 * Consumed the complete message.
1263 				 * Move the MSG*MARKNEXT information
1264 				 * to the stream head just in case
1265 				 * the read queue becomes empty.
1266 				 *
1267 				 * If the stream head was at the mark
1268 				 * (STRATMARK) before we dropped sd_lock above
1269 				 * and some data was consumed then we have
1270 				 * moved past the mark thus STRATMARK is
1271 				 * cleared. However, if a message arrived in
1272 				 * strrput during the copyout above causing
1273 				 * STRATMARK to be set we can not clear that
1274 				 * flag.
1275 				 */
1276 				if (mark &
1277 				    (MSGMARKNEXT|MSGNOTMARKNEXT|MSGMARK)) {
1278 					if (mark & MSGMARKNEXT) {
1279 						stp->sd_flag &= ~STRNOTATMARK;
1280 						stp->sd_flag |= STRATMARK;
1281 					} else if (mark & MSGNOTMARKNEXT) {
1282 						stp->sd_flag &= ~STRATMARK;
1283 						stp->sd_flag |= STRNOTATMARK;
1284 					} else {
1285 						stp->sd_flag &=
1286 						    ~(STRATMARK|STRNOTATMARK);
1287 					}
1288 				} else if (rflg && (old_sd_flag & STRATMARK)) {
1289 					stp->sd_flag &= ~STRATMARK;
1290 				}
1291 			}
1292 
1293 			/*
1294 			 * Check for signal messages at the front of the read
1295 			 * queue and generate the signal(s) if appropriate.
1296 			 * The only signal that can be on queue is M_SIG at
1297 			 * this point.
1298 			 */
1299 			while ((((bp = q->q_first)) != NULL) &&
1300 				(bp->b_datap->db_type == M_SIG)) {
1301 				bp = getq_noenab(q);
1302 				/*
1303 				 * sd_lock is held so the content of the
1304 				 * read queue can not change.
1305 				 */
1306 				ASSERT(bp != NULL &&
1307 					bp->b_datap->db_type == M_SIG);
1308 				strsignal_nolock(stp, *bp->b_rptr,
1309 					(int32_t)bp->b_band);
1310 				mutex_exit(&stp->sd_lock);
1311 				freemsg(bp);
1312 				if (STREAM_NEEDSERVICE(stp))
1313 					stream_runservice(stp);
1314 				mutex_enter(&stp->sd_lock);
1315 			}
1316 
1317 			if ((uiop->uio_resid == 0) || (mark & _LASTMARK) ||
1318 			    delim ||
1319 			    (stp->sd_read_opt & (RD_MSGDIS|RD_MSGNODIS))) {
1320 				goto oops;
1321 			}
1322 			continue;
1323 
1324 		case M_SIG:
1325 			strsignal(stp, *bp->b_rptr, (int32_t)bp->b_band);
1326 			freemsg(bp);
1327 			mutex_enter(&stp->sd_lock);
1328 			continue;
1329 
1330 		case M_PROTO:
1331 		case M_PCPROTO:
1332 			/*
1333 			 * Only data messages are readable.
1334 			 * Any others generate an error, unless
1335 			 * RD_PROTDIS or RD_PROTDAT is set.
1336 			 */
1337 			if (stp->sd_read_opt & RD_PROTDAT) {
1338 				for (nbp = bp; nbp; nbp = nbp->b_next) {
1339 				    if ((nbp->b_datap->db_type == M_PROTO) ||
1340 					(nbp->b_datap->db_type == M_PCPROTO))
1341 					nbp->b_datap->db_type = M_DATA;
1342 				    else
1343 					break;
1344 				}
1345 				/*
1346 				 * clear stream head hi pri flag based on
1347 				 * first message
1348 				 */
1349 				if (type == M_PCPROTO) {
1350 					mutex_enter(&stp->sd_lock);
1351 					stp->sd_flag &= ~STRPRI;
1352 					mutex_exit(&stp->sd_lock);
1353 				}
1354 				goto ismdata;
1355 			} else if (stp->sd_read_opt & RD_PROTDIS) {
1356 				/*
1357 				 * discard non-data messages
1358 				 */
1359 				while (bp &&
1360 				    ((bp->b_datap->db_type == M_PROTO) ||
1361 				    (bp->b_datap->db_type == M_PCPROTO))) {
1362 					nbp = unlinkb(bp);
1363 					freeb(bp);
1364 					bp = nbp;
1365 				}
1366 				/*
1367 				 * clear stream head hi pri flag based on
1368 				 * first message
1369 				 */
1370 				if (type == M_PCPROTO) {
1371 					mutex_enter(&stp->sd_lock);
1372 					stp->sd_flag &= ~STRPRI;
1373 					mutex_exit(&stp->sd_lock);
1374 				}
1375 				if (bp) {
1376 					bp->b_band = pri;
1377 					goto ismdata;
1378 				} else {
1379 					break;
1380 				}
1381 			}
1382 			/* FALLTHRU */
1383 		case M_PASSFP:
1384 			if ((bp->b_datap->db_type == M_PASSFP) &&
1385 			    (stp->sd_read_opt & RD_PROTDIS)) {
1386 				freemsg(bp);
1387 				break;
1388 			}
1389 			mutex_enter(&stp->sd_lock);
1390 			putback(stp, q, bp, pri);
1391 			mutex_exit(&stp->sd_lock);
1392 			if (rflg == 0)
1393 				error = EBADMSG;
1394 			goto oops1;
1395 
1396 		default:
1397 			/*
1398 			 * Garbage on stream head read queue.
1399 			 */
1400 			cmn_err(CE_WARN, "bad %x found at stream head\n",
1401 				bp->b_datap->db_type);
1402 			freemsg(bp);
1403 			goto oops1;
1404 		}
1405 		mutex_enter(&stp->sd_lock);
1406 	}
1407 oops:
1408 	mutex_exit(&stp->sd_lock);
1409 oops1:
1410 	qbackenable(q, pri);
1411 	return (error);
1412 #undef	_LASTMARK
1413 }
1414 
1415 /*
1416  * Default processing of M_PROTO/M_PCPROTO messages.
1417  * Determine which wakeups and signals are needed.
1418  * This can be replaced by a user-specified procedure for kernel users
1419  * of STREAMS.
1420  */
1421 /* ARGSUSED */
1422 mblk_t *
1423 strrput_proto(vnode_t *vp, mblk_t *mp,
1424     strwakeup_t *wakeups, strsigset_t *firstmsgsigs,
1425     strsigset_t *allmsgsigs, strpollset_t *pollwakeups)
1426 {
1427 	*wakeups = RSLEEP;
1428 	*allmsgsigs = 0;
1429 
1430 	switch (mp->b_datap->db_type) {
1431 	case M_PROTO:
1432 		if (mp->b_band == 0) {
1433 			*firstmsgsigs = S_INPUT | S_RDNORM;
1434 			*pollwakeups = POLLIN | POLLRDNORM;
1435 		} else {
1436 			*firstmsgsigs = S_INPUT | S_RDBAND;
1437 			*pollwakeups = POLLIN | POLLRDBAND;
1438 		}
1439 		break;
1440 	case M_PCPROTO:
1441 		*firstmsgsigs = S_HIPRI;
1442 		*pollwakeups = POLLPRI;
1443 		break;
1444 	}
1445 	return (mp);
1446 }
1447 
1448 /*
1449  * Default processing of everything but M_DATA, M_PROTO, M_PCPROTO and
1450  * M_PASSFP messages.
1451  * Determine which wakeups and signals are needed.
1452  * This can be replaced by a user-specified procedure for kernel users
1453  * of STREAMS.
1454  */
1455 /* ARGSUSED */
1456 mblk_t *
1457 strrput_misc(vnode_t *vp, mblk_t *mp,
1458     strwakeup_t *wakeups, strsigset_t *firstmsgsigs,
1459     strsigset_t *allmsgsigs, strpollset_t *pollwakeups)
1460 {
1461 	*wakeups = 0;
1462 	*firstmsgsigs = 0;
1463 	*allmsgsigs = 0;
1464 	*pollwakeups = 0;
1465 	return (mp);
1466 }
1467 
1468 /*
1469  * Stream read put procedure.  Called from downstream driver/module
1470  * with messages for the stream head.  Data, protocol, and in-stream
1471  * signal messages are placed on the queue, others are handled directly.
1472  */
1473 int
1474 strrput(queue_t *q, mblk_t *bp)
1475 {
1476 	struct stdata	*stp;
1477 	ulong_t		rput_opt;
1478 	strwakeup_t	wakeups;
1479 	strsigset_t	firstmsgsigs;	/* Signals if first message on queue */
1480 	strsigset_t	allmsgsigs;	/* Signals for all messages */
1481 	strsigset_t	signals;	/* Signals events to generate */
1482 	strpollset_t	pollwakeups;
1483 	mblk_t		*nextbp;
1484 	uchar_t		band = 0;
1485 	int		hipri_sig;
1486 
1487 	stp = (struct stdata *)q->q_ptr;
1488 	/*
1489 	 * Use rput_opt for optimized access to the SR_ flags except
1490 	 * SR_POLLIN. That flag has to be checked under sd_lock since it
1491 	 * is modified by strpoll().
1492 	 */
1493 	rput_opt = stp->sd_rput_opt;
1494 
1495 	ASSERT(qclaimed(q));
1496 	TRACE_2(TR_FAC_STREAMS_FR, TR_STRRPUT_ENTER,
1497 		"strrput called with message type:q %p bp %p", q, bp);
1498 
1499 	/*
1500 	 * Perform initial processing and pass to the parameterized functions.
1501 	 */
1502 	ASSERT(bp->b_next == NULL);
1503 
1504 	switch (bp->b_datap->db_type) {
1505 	case M_DATA:
1506 		/*
1507 		 * sockfs is the only consumer of STREOF and when it is set,
1508 		 * it implies that the receiver is not interested in receiving
1509 		 * any more data, hence the mblk is freed to prevent unnecessary
1510 		 * message queueing at the stream head.
1511 		 */
1512 		if (stp->sd_flag == STREOF) {
1513 			freemsg(bp);
1514 			return (0);
1515 		}
1516 		if ((rput_opt & SR_IGN_ZEROLEN) &&
1517 		    bp->b_rptr == bp->b_wptr && msgnodata(bp)) {
1518 			/*
1519 			 * Ignore zero-length M_DATA messages. These might be
1520 			 * generated by some transports.
1521 			 * The zero-length M_DATA messages, even if they
1522 			 * are ignored, should effect the atmark tracking and
1523 			 * should wake up a thread sleeping in strwaitmark.
1524 			 */
1525 			mutex_enter(&stp->sd_lock);
1526 			if (bp->b_flag & MSGMARKNEXT) {
1527 				/*
1528 				 * Record the position of the mark either
1529 				 * in q_last or in STRATMARK.
1530 				 */
1531 				if (q->q_last != NULL) {
1532 					q->q_last->b_flag &= ~MSGNOTMARKNEXT;
1533 					q->q_last->b_flag |= MSGMARKNEXT;
1534 				} else {
1535 					stp->sd_flag &= ~STRNOTATMARK;
1536 					stp->sd_flag |= STRATMARK;
1537 				}
1538 			} else if (bp->b_flag & MSGNOTMARKNEXT) {
1539 				/*
1540 				 * Record that this is not the position of
1541 				 * the mark either in q_last or in
1542 				 * STRNOTATMARK.
1543 				 */
1544 				if (q->q_last != NULL) {
1545 					q->q_last->b_flag &= ~MSGMARKNEXT;
1546 					q->q_last->b_flag |= MSGNOTMARKNEXT;
1547 				} else {
1548 					stp->sd_flag &= ~STRATMARK;
1549 					stp->sd_flag |= STRNOTATMARK;
1550 				}
1551 			}
1552 			if (stp->sd_flag & RSLEEP) {
1553 				stp->sd_flag &= ~RSLEEP;
1554 				cv_broadcast(&q->q_wait);
1555 			}
1556 			mutex_exit(&stp->sd_lock);
1557 			freemsg(bp);
1558 			return (0);
1559 		}
1560 		wakeups = RSLEEP;
1561 		if (bp->b_band == 0) {
1562 			firstmsgsigs = S_INPUT | S_RDNORM;
1563 			pollwakeups = POLLIN | POLLRDNORM;
1564 		} else {
1565 			firstmsgsigs = S_INPUT | S_RDBAND;
1566 			pollwakeups = POLLIN | POLLRDBAND;
1567 		}
1568 		if (rput_opt & SR_SIGALLDATA)
1569 			allmsgsigs = firstmsgsigs;
1570 		else
1571 			allmsgsigs = 0;
1572 
1573 		mutex_enter(&stp->sd_lock);
1574 		if ((rput_opt & SR_CONSOL_DATA) &&
1575 		    (bp->b_flag & (MSGMARK|MSGDELIM)) == 0) {
1576 			/*
1577 			 * Consolidate on M_DATA message onto an M_DATA,
1578 			 * M_PROTO, or M_PCPROTO by merging it with q_last.
1579 			 * The consolidation does not take place if
1580 			 * the old message is marked with either of the
1581 			 * marks or the delim flag or if the new
1582 			 * message is marked with MSGMARK. The MSGMARK
1583 			 * check is needed to handle the odd semantics of
1584 			 * MSGMARK where essentially the whole message
1585 			 * is to be treated as marked.
1586 			 * Carry any MSGMARKNEXT  and MSGNOTMARKNEXT from the
1587 			 * new message to the front of the b_cont chain.
1588 			 */
1589 			mblk_t *lbp;
1590 
1591 			lbp = q->q_last;
1592 			if (lbp != NULL &&
1593 			    (lbp->b_datap->db_type == M_DATA ||
1594 			    lbp->b_datap->db_type == M_PROTO ||
1595 			    lbp->b_datap->db_type == M_PCPROTO) &&
1596 			    !(lbp->b_flag & (MSGDELIM|MSGMARK|
1597 			    MSGMARKNEXT))) {
1598 				rmvq_noenab(q, lbp);
1599 				/*
1600 				 * The first message in the b_cont list
1601 				 * tracks MSGMARKNEXT and MSGNOTMARKNEXT.
1602 				 * We need to handle the case where we
1603 				 * are appending
1604 				 *
1605 				 * 1) a MSGMARKNEXT to a MSGNOTMARKNEXT.
1606 				 * 2) a MSGMARKNEXT to a plain message.
1607 				 * 3) a MSGNOTMARKNEXT to a plain message
1608 				 * 4) a MSGNOTMARKNEXT to a MSGNOTMARKNEXT
1609 				 *    message.
1610 				 *
1611 				 * Thus we never append a MSGMARKNEXT or
1612 				 * MSGNOTMARKNEXT to a MSGMARKNEXT message.
1613 				 */
1614 				if (bp->b_flag & MSGMARKNEXT) {
1615 					lbp->b_flag |= MSGMARKNEXT;
1616 					lbp->b_flag &= ~MSGNOTMARKNEXT;
1617 					bp->b_flag &= ~MSGMARKNEXT;
1618 				} else if (bp->b_flag & MSGNOTMARKNEXT) {
1619 					lbp->b_flag |= MSGNOTMARKNEXT;
1620 					bp->b_flag &= ~MSGNOTMARKNEXT;
1621 				}
1622 
1623 				linkb(lbp, bp);
1624 				bp = lbp;
1625 				/*
1626 				 * The new message logically isn't the first
1627 				 * even though the q_first check below thinks
1628 				 * it is. Clear the firstmsgsigs to make it
1629 				 * not appear to be first.
1630 				 */
1631 				firstmsgsigs = 0;
1632 			}
1633 		}
1634 		break;
1635 
1636 	case M_PASSFP:
1637 		wakeups = RSLEEP;
1638 		allmsgsigs = 0;
1639 		if (bp->b_band == 0) {
1640 			firstmsgsigs = S_INPUT | S_RDNORM;
1641 			pollwakeups = POLLIN | POLLRDNORM;
1642 		} else {
1643 			firstmsgsigs = S_INPUT | S_RDBAND;
1644 			pollwakeups = POLLIN | POLLRDBAND;
1645 		}
1646 		mutex_enter(&stp->sd_lock);
1647 		break;
1648 
1649 	case M_PROTO:
1650 	case M_PCPROTO:
1651 		ASSERT(stp->sd_rprotofunc != NULL);
1652 		bp = (stp->sd_rprotofunc)(stp->sd_vnode, bp,
1653 			&wakeups, &firstmsgsigs, &allmsgsigs, &pollwakeups);
1654 #define	ALLSIG	(S_INPUT|S_HIPRI|S_OUTPUT|S_MSG|S_ERROR|S_HANGUP|S_RDNORM|\
1655 		S_WRNORM|S_RDBAND|S_WRBAND|S_BANDURG)
1656 #define	ALLPOLL	(POLLIN|POLLPRI|POLLOUT|POLLRDNORM|POLLWRNORM|POLLRDBAND|\
1657 		POLLWRBAND)
1658 
1659 		ASSERT((wakeups & ~(RSLEEP|WSLEEP)) == 0);
1660 		ASSERT((firstmsgsigs & ~ALLSIG) == 0);
1661 		ASSERT((allmsgsigs & ~ALLSIG) == 0);
1662 		ASSERT((pollwakeups & ~ALLPOLL) == 0);
1663 
1664 		mutex_enter(&stp->sd_lock);
1665 		break;
1666 
1667 	default:
1668 		ASSERT(stp->sd_rmiscfunc != NULL);
1669 		bp = (stp->sd_rmiscfunc)(stp->sd_vnode, bp,
1670 			&wakeups, &firstmsgsigs, &allmsgsigs, &pollwakeups);
1671 		ASSERT((wakeups & ~(RSLEEP|WSLEEP)) == 0);
1672 		ASSERT((firstmsgsigs & ~ALLSIG) == 0);
1673 		ASSERT((allmsgsigs & ~ALLSIG) == 0);
1674 		ASSERT((pollwakeups & ~ALLPOLL) == 0);
1675 #undef	ALLSIG
1676 #undef	ALLPOLL
1677 		mutex_enter(&stp->sd_lock);
1678 		break;
1679 	}
1680 	ASSERT(MUTEX_HELD(&stp->sd_lock));
1681 
1682 	/* By default generate superset of signals */
1683 	signals = (firstmsgsigs | allmsgsigs);
1684 
1685 	/*
1686 	 * The  proto and misc functions can return multiple messages
1687 	 * as a b_next chain. Such messages are processed separately.
1688 	 */
1689 one_more:
1690 	hipri_sig = 0;
1691 	if (bp == NULL) {
1692 		nextbp = NULL;
1693 	} else {
1694 		nextbp = bp->b_next;
1695 		bp->b_next = NULL;
1696 
1697 		switch (bp->b_datap->db_type) {
1698 		case M_PCPROTO:
1699 			/*
1700 			 * Only one priority protocol message is allowed at the
1701 			 * stream head at a time.
1702 			 */
1703 			if (stp->sd_flag & STRPRI) {
1704 				TRACE_0(TR_FAC_STREAMS_FR, TR_STRRPUT_PROTERR,
1705 				    "M_PCPROTO already at head");
1706 				freemsg(bp);
1707 				mutex_exit(&stp->sd_lock);
1708 				goto done;
1709 			}
1710 			stp->sd_flag |= STRPRI;
1711 			hipri_sig = 1;
1712 			/* FALLTHRU */
1713 		case M_DATA:
1714 		case M_PROTO:
1715 		case M_PASSFP:
1716 			band = bp->b_band;
1717 			/*
1718 			 * Marking doesn't work well when messages
1719 			 * are marked in more than one band.  We only
1720 			 * remember the last message received, even if
1721 			 * it is placed on the queue ahead of other
1722 			 * marked messages.
1723 			 */
1724 			if (bp->b_flag & MSGMARK)
1725 				stp->sd_mark = bp;
1726 			(void) putq(q, bp);
1727 
1728 			/*
1729 			 * If message is a PCPROTO message, always use
1730 			 * firstmsgsigs to determine if a signal should be
1731 			 * sent as strrput is the only place to send
1732 			 * signals for PCPROTO. Other messages are based on
1733 			 * the STRGETINPROG flag. The flag determines if
1734 			 * strrput or (k)strgetmsg will be responsible for
1735 			 * sending the signals, in the firstmsgsigs case.
1736 			 */
1737 			if ((hipri_sig == 1) ||
1738 			    (((stp->sd_flag & STRGETINPROG) == 0) &&
1739 			    (q->q_first == bp)))
1740 				signals = (firstmsgsigs | allmsgsigs);
1741 			else
1742 				signals = allmsgsigs;
1743 			break;
1744 
1745 		default:
1746 			mutex_exit(&stp->sd_lock);
1747 			(void) strrput_nondata(q, bp);
1748 			mutex_enter(&stp->sd_lock);
1749 			break;
1750 		}
1751 	}
1752 	ASSERT(MUTEX_HELD(&stp->sd_lock));
1753 	/*
1754 	 * Wake sleeping read/getmsg and cancel deferred wakeup
1755 	 */
1756 	if (wakeups & RSLEEP)
1757 		stp->sd_wakeq &= ~RSLEEP;
1758 
1759 	wakeups &= stp->sd_flag;
1760 	if (wakeups & RSLEEP) {
1761 		stp->sd_flag &= ~RSLEEP;
1762 		cv_broadcast(&q->q_wait);
1763 	}
1764 	if (wakeups & WSLEEP) {
1765 		stp->sd_flag &= ~WSLEEP;
1766 		cv_broadcast(&_WR(q)->q_wait);
1767 	}
1768 
1769 	if (pollwakeups != 0) {
1770 		if (pollwakeups == (POLLIN | POLLRDNORM)) {
1771 			/*
1772 			 * Can't use rput_opt since it was not
1773 			 * read when sd_lock was held and SR_POLLIN is changed
1774 			 * by strpoll() under sd_lock.
1775 			 */
1776 			if (!(stp->sd_rput_opt & SR_POLLIN))
1777 				goto no_pollwake;
1778 			stp->sd_rput_opt &= ~SR_POLLIN;
1779 		}
1780 		mutex_exit(&stp->sd_lock);
1781 		pollwakeup(&stp->sd_pollist, pollwakeups);
1782 		mutex_enter(&stp->sd_lock);
1783 	}
1784 no_pollwake:
1785 
1786 	/*
1787 	 * strsendsig can handle multiple signals with a
1788 	 * single call.
1789 	 */
1790 	if (stp->sd_sigflags & signals)
1791 		strsendsig(stp->sd_siglist, signals, band, 0);
1792 	mutex_exit(&stp->sd_lock);
1793 
1794 
1795 done:
1796 	if (nextbp == NULL)
1797 		return (0);
1798 
1799 	/*
1800 	 * Any signals were handled the first time.
1801 	 * Wakeups and pollwakeups are redone to avoid any race
1802 	 * conditions - all the messages are not queued until the
1803 	 * last message has been processed by strrput.
1804 	 */
1805 	bp = nextbp;
1806 	signals = firstmsgsigs = allmsgsigs = 0;
1807 	mutex_enter(&stp->sd_lock);
1808 	goto one_more;
1809 }
1810 
1811 static void
1812 log_dupioc(queue_t *rq, mblk_t *bp)
1813 {
1814 	queue_t *wq, *qp;
1815 	char *modnames, *mnp, *dname;
1816 	size_t maxmodstr;
1817 	boolean_t islast;
1818 
1819 	/*
1820 	 * Allocate a buffer large enough to hold the names of nstrpush modules
1821 	 * and one driver, with spaces between and NUL terminator.  If we can't
1822 	 * get memory, then we'll just log the driver name.
1823 	 */
1824 	maxmodstr = nstrpush * (FMNAMESZ + 1);
1825 	mnp = modnames = kmem_alloc(maxmodstr, KM_NOSLEEP);
1826 
1827 	/* march down write side to print log message down to the driver */
1828 	wq = WR(rq);
1829 
1830 	/* make sure q_next doesn't shift around while we're grabbing data */
1831 	claimstr(wq);
1832 	qp = wq->q_next;
1833 	do {
1834 		if ((dname = qp->q_qinfo->qi_minfo->mi_idname) == NULL)
1835 			dname = "?";
1836 		islast = !SAMESTR(qp) || qp->q_next == NULL;
1837 		if (modnames == NULL) {
1838 			/*
1839 			 * If we don't have memory, then get the driver name in
1840 			 * the log where we can see it.  Note that memory
1841 			 * pressure is a possible cause of these sorts of bugs.
1842 			 */
1843 			if (islast) {
1844 				modnames = dname;
1845 				maxmodstr = 0;
1846 			}
1847 		} else {
1848 			mnp += snprintf(mnp, FMNAMESZ + 1, "%s", dname);
1849 			if (!islast)
1850 				*mnp++ = ' ';
1851 		}
1852 		qp = qp->q_next;
1853 	} while (!islast);
1854 	releasestr(wq);
1855 	/* Cannot happen unless stream head is corrupt. */
1856 	ASSERT(modnames != NULL);
1857 	(void) strlog(rq->q_qinfo->qi_minfo->mi_idnum, 0, 1,
1858 	    SL_CONSOLE|SL_TRACE|SL_ERROR,
1859 	    "Warning: stream %p received duplicate %X M_IOC%s; module list: %s",
1860 	    rq->q_ptr, ((struct iocblk *)bp->b_rptr)->ioc_cmd,
1861 	    (DB_TYPE(bp) == M_IOCACK ? "ACK" : "NAK"), modnames);
1862 	if (maxmodstr != 0)
1863 		kmem_free(modnames, maxmodstr);
1864 }
1865 
1866 int
1867 strrput_nondata(queue_t *q, mblk_t *bp)
1868 {
1869 	struct stdata *stp;
1870 	struct iocblk *iocbp;
1871 	struct stroptions *sop;
1872 	struct copyreq *reqp;
1873 	struct copyresp *resp;
1874 	unsigned char bpri;
1875 	unsigned char  flushed_already = 0;
1876 
1877 	stp = (struct stdata *)q->q_ptr;
1878 
1879 	ASSERT(!(stp->sd_flag & STPLEX));
1880 	ASSERT(qclaimed(q));
1881 
1882 	switch (bp->b_datap->db_type) {
1883 	case M_ERROR:
1884 		/*
1885 		 * An error has occurred downstream, the errno is in the first
1886 		 * bytes of the message.
1887 		 */
1888 		if ((bp->b_wptr - bp->b_rptr) == 2) {	/* New flavor */
1889 			unsigned char rw = 0;
1890 
1891 			mutex_enter(&stp->sd_lock);
1892 			if (*bp->b_rptr != NOERROR) {	/* read error */
1893 				if (*bp->b_rptr != 0) {
1894 					if (stp->sd_flag & STRDERR)
1895 						flushed_already |= FLUSHR;
1896 					stp->sd_flag |= STRDERR;
1897 					rw |= FLUSHR;
1898 				} else {
1899 					stp->sd_flag &= ~STRDERR;
1900 				}
1901 				stp->sd_rerror = *bp->b_rptr;
1902 			}
1903 			bp->b_rptr++;
1904 			if (*bp->b_rptr != NOERROR) {	/* write error */
1905 				if (*bp->b_rptr != 0) {
1906 					if (stp->sd_flag & STWRERR)
1907 						flushed_already |= FLUSHW;
1908 					stp->sd_flag |= STWRERR;
1909 					rw |= FLUSHW;
1910 				} else {
1911 					stp->sd_flag &= ~STWRERR;
1912 				}
1913 				stp->sd_werror = *bp->b_rptr;
1914 			}
1915 			if (rw) {
1916 				TRACE_2(TR_FAC_STREAMS_FR, TR_STRRPUT_WAKE,
1917 					"strrput cv_broadcast:q %p, bp %p",
1918 					q, bp);
1919 				cv_broadcast(&q->q_wait); /* readers */
1920 				cv_broadcast(&_WR(q)->q_wait); /* writers */
1921 				cv_broadcast(&stp->sd_monitor); /* ioctllers */
1922 
1923 				mutex_exit(&stp->sd_lock);
1924 				pollwakeup(&stp->sd_pollist, POLLERR);
1925 				mutex_enter(&stp->sd_lock);
1926 
1927 				if (stp->sd_sigflags & S_ERROR)
1928 					strsendsig(stp->sd_siglist, S_ERROR, 0,
1929 					    ((rw & FLUSHR) ? stp->sd_rerror :
1930 					    stp->sd_werror));
1931 				mutex_exit(&stp->sd_lock);
1932 				/*
1933 				 * Send the M_FLUSH only
1934 				 * for the first M_ERROR
1935 				 * message on the stream
1936 				 */
1937 				if (flushed_already == rw) {
1938 					freemsg(bp);
1939 					return (0);
1940 				}
1941 
1942 				bp->b_datap->db_type = M_FLUSH;
1943 				*bp->b_rptr = rw;
1944 				bp->b_wptr = bp->b_rptr + 1;
1945 				/*
1946 				 * Protect against the driver
1947 				 * passing up messages after
1948 				 * it has done a qprocsoff
1949 				 */
1950 				if (_OTHERQ(q)->q_next == NULL)
1951 					freemsg(bp);
1952 				else
1953 					qreply(q, bp);
1954 				return (0);
1955 			} else
1956 				mutex_exit(&stp->sd_lock);
1957 		} else if (*bp->b_rptr != 0) {		/* Old flavor */
1958 				if (stp->sd_flag & (STRDERR|STWRERR))
1959 					flushed_already = FLUSHRW;
1960 				mutex_enter(&stp->sd_lock);
1961 				stp->sd_flag |= (STRDERR|STWRERR);
1962 				stp->sd_rerror = *bp->b_rptr;
1963 				stp->sd_werror = *bp->b_rptr;
1964 				TRACE_2(TR_FAC_STREAMS_FR,
1965 					TR_STRRPUT_WAKE2,
1966 					"strrput wakeup #2:q %p, bp %p", q, bp);
1967 				cv_broadcast(&q->q_wait); /* the readers */
1968 				cv_broadcast(&_WR(q)->q_wait); /* the writers */
1969 				cv_broadcast(&stp->sd_monitor); /* ioctllers */
1970 
1971 				mutex_exit(&stp->sd_lock);
1972 				pollwakeup(&stp->sd_pollist, POLLERR);
1973 				mutex_enter(&stp->sd_lock);
1974 
1975 				if (stp->sd_sigflags & S_ERROR)
1976 					strsendsig(stp->sd_siglist, S_ERROR, 0,
1977 					    (stp->sd_werror ? stp->sd_werror :
1978 					    stp->sd_rerror));
1979 				mutex_exit(&stp->sd_lock);
1980 
1981 				/*
1982 				 * Send the M_FLUSH only
1983 				 * for the first M_ERROR
1984 				 * message on the stream
1985 				 */
1986 				if (flushed_already != FLUSHRW) {
1987 					bp->b_datap->db_type = M_FLUSH;
1988 					*bp->b_rptr = FLUSHRW;
1989 					/*
1990 					 * Protect against the driver passing up
1991 					 * messages after it has done a
1992 					 * qprocsoff.
1993 					 */
1994 				if (_OTHERQ(q)->q_next == NULL)
1995 					freemsg(bp);
1996 				else
1997 					qreply(q, bp);
1998 				return (0);
1999 				}
2000 		}
2001 		freemsg(bp);
2002 		return (0);
2003 
2004 	case M_HANGUP:
2005 
2006 		freemsg(bp);
2007 		mutex_enter(&stp->sd_lock);
2008 		stp->sd_werror = ENXIO;
2009 		stp->sd_flag |= STRHUP;
2010 		stp->sd_flag &= ~(WSLEEP|RSLEEP);
2011 
2012 		/*
2013 		 * send signal if controlling tty
2014 		 */
2015 
2016 		if (stp->sd_sidp) {
2017 			prsignal(stp->sd_sidp, SIGHUP);
2018 			if (stp->sd_sidp != stp->sd_pgidp)
2019 				pgsignal(stp->sd_pgidp, SIGTSTP);
2020 		}
2021 
2022 		/*
2023 		 * wake up read, write, and exception pollers and
2024 		 * reset wakeup mechanism.
2025 		 */
2026 		cv_broadcast(&q->q_wait);	/* the readers */
2027 		cv_broadcast(&_WR(q)->q_wait);	/* the writers */
2028 		cv_broadcast(&stp->sd_monitor);	/* the ioctllers */
2029 		mutex_exit(&stp->sd_lock);
2030 		strhup(stp);
2031 		return (0);
2032 
2033 	case M_UNHANGUP:
2034 		freemsg(bp);
2035 		mutex_enter(&stp->sd_lock);
2036 		stp->sd_werror = 0;
2037 		stp->sd_flag &= ~STRHUP;
2038 		mutex_exit(&stp->sd_lock);
2039 		return (0);
2040 
2041 	case M_SIG:
2042 		/*
2043 		 * Someone downstream wants to post a signal.  The
2044 		 * signal to post is contained in the first byte of the
2045 		 * message.  If the message would go on the front of
2046 		 * the queue, send a signal to the process group
2047 		 * (if not SIGPOLL) or to the siglist processes
2048 		 * (SIGPOLL).  If something is already on the queue,
2049 		 * OR if we are delivering a delayed suspend (*sigh*
2050 		 * another "tty" hack) and there's no one sleeping already,
2051 		 * just enqueue the message.
2052 		 */
2053 		mutex_enter(&stp->sd_lock);
2054 		if (q->q_first || (*bp->b_rptr == SIGTSTP &&
2055 		    !(stp->sd_flag & RSLEEP))) {
2056 			(void) putq(q, bp);
2057 			mutex_exit(&stp->sd_lock);
2058 			return (0);
2059 		}
2060 		mutex_exit(&stp->sd_lock);
2061 		/* FALLTHRU */
2062 
2063 	case M_PCSIG:
2064 		/*
2065 		 * Don't enqueue, just post the signal.
2066 		 */
2067 		strsignal(stp, *bp->b_rptr, 0L);
2068 		freemsg(bp);
2069 		return (0);
2070 
2071 	case M_FLUSH:
2072 		/*
2073 		 * Flush queues.  The indication of which queues to flush
2074 		 * is in the first byte of the message.  If the read queue
2075 		 * is specified, then flush it.  If FLUSHBAND is set, just
2076 		 * flush the band specified by the second byte of the message.
2077 		 *
2078 		 * If a module has issued a M_SETOPT to not flush hi
2079 		 * priority messages off of the stream head, then pass this
2080 		 * flag into the flushq code to preserve such messages.
2081 		 */
2082 
2083 		if (*bp->b_rptr & FLUSHR) {
2084 			mutex_enter(&stp->sd_lock);
2085 			if (*bp->b_rptr & FLUSHBAND) {
2086 				ASSERT((bp->b_wptr - bp->b_rptr) >= 2);
2087 				flushband(q, *(bp->b_rptr + 1), FLUSHALL);
2088 			} else
2089 				flushq_common(q, FLUSHALL,
2090 				    stp->sd_read_opt & RFLUSHPCPROT);
2091 			if ((q->q_first == NULL) ||
2092 			    (q->q_first->b_datap->db_type < QPCTL))
2093 				stp->sd_flag &= ~STRPRI;
2094 			else {
2095 				ASSERT(stp->sd_flag & STRPRI);
2096 			}
2097 			mutex_exit(&stp->sd_lock);
2098 		}
2099 		if ((*bp->b_rptr & FLUSHW) && !(bp->b_flag & MSGNOLOOP)) {
2100 			*bp->b_rptr &= ~FLUSHR;
2101 			bp->b_flag |= MSGNOLOOP;
2102 			/*
2103 			 * Protect against the driver passing up
2104 			 * messages after it has done a qprocsoff.
2105 			 */
2106 			if (_OTHERQ(q)->q_next == NULL)
2107 				freemsg(bp);
2108 			else
2109 				qreply(q, bp);
2110 			return (0);
2111 		}
2112 		freemsg(bp);
2113 		return (0);
2114 
2115 	case M_IOCACK:
2116 	case M_IOCNAK:
2117 		iocbp = (struct iocblk *)bp->b_rptr;
2118 		/*
2119 		 * If not waiting for ACK or NAK then just free msg.
2120 		 * If incorrect id sequence number then just free msg.
2121 		 * If already have ACK or NAK for user then this is a
2122 		 *    duplicate, display a warning and free the msg.
2123 		 */
2124 		mutex_enter(&stp->sd_lock);
2125 		if ((stp->sd_flag & IOCWAIT) == 0 || stp->sd_iocblk ||
2126 		    (stp->sd_iocid != iocbp->ioc_id)) {
2127 			/*
2128 			 * If the ACK/NAK is a dup, display a message
2129 			 * Dup is when sd_iocid == ioc_id, and
2130 			 * sd_iocblk == <valid ptr> or -1 (the former
2131 			 * is when an ioctl has been put on the stream
2132 			 * head, but has not yet been consumed, the
2133 			 * later is when it has been consumed).
2134 			 */
2135 			if ((stp->sd_iocid == iocbp->ioc_id) &&
2136 			    (stp->sd_iocblk != NULL)) {
2137 				log_dupioc(q, bp);
2138 			}
2139 			freemsg(bp);
2140 			mutex_exit(&stp->sd_lock);
2141 			return (0);
2142 		}
2143 
2144 		/*
2145 		 * Assign ACK or NAK to user and wake up.
2146 		 */
2147 		stp->sd_iocblk = bp;
2148 		cv_broadcast(&stp->sd_monitor);
2149 		mutex_exit(&stp->sd_lock);
2150 		return (0);
2151 
2152 	case M_COPYIN:
2153 	case M_COPYOUT:
2154 		reqp = (struct copyreq *)bp->b_rptr;
2155 
2156 		/*
2157 		 * If not waiting for ACK or NAK then just fail request.
2158 		 * If already have ACK, NAK, or copy request, then just
2159 		 * fail request.
2160 		 * If incorrect id sequence number then just fail request.
2161 		 */
2162 		mutex_enter(&stp->sd_lock);
2163 		if ((stp->sd_flag & IOCWAIT) == 0 || stp->sd_iocblk ||
2164 		    (stp->sd_iocid != reqp->cq_id)) {
2165 			if (bp->b_cont) {
2166 				freemsg(bp->b_cont);
2167 				bp->b_cont = NULL;
2168 			}
2169 			bp->b_datap->db_type = M_IOCDATA;
2170 			bp->b_wptr = bp->b_rptr + sizeof (struct copyresp);
2171 			resp = (struct copyresp *)bp->b_rptr;
2172 			resp->cp_rval = (caddr_t)1;	/* failure */
2173 			mutex_exit(&stp->sd_lock);
2174 			putnext(stp->sd_wrq, bp);
2175 			return (0);
2176 		}
2177 
2178 		/*
2179 		 * Assign copy request to user and wake up.
2180 		 */
2181 		stp->sd_iocblk = bp;
2182 		cv_broadcast(&stp->sd_monitor);
2183 		mutex_exit(&stp->sd_lock);
2184 		return (0);
2185 
2186 	case M_SETOPTS:
2187 		/*
2188 		 * Set stream head options (read option, write offset,
2189 		 * min/max packet size, and/or high/low water marks for
2190 		 * the read side only).
2191 		 */
2192 
2193 		bpri = 0;
2194 		sop = (struct stroptions *)bp->b_rptr;
2195 		mutex_enter(&stp->sd_lock);
2196 		if (sop->so_flags & SO_READOPT) {
2197 			switch (sop->so_readopt & RMODEMASK) {
2198 			case RNORM:
2199 				stp->sd_read_opt &= ~(RD_MSGDIS | RD_MSGNODIS);
2200 				break;
2201 
2202 			case RMSGD:
2203 				stp->sd_read_opt =
2204 				    ((stp->sd_read_opt & ~RD_MSGNODIS) |
2205 				    RD_MSGDIS);
2206 				break;
2207 
2208 			case RMSGN:
2209 				stp->sd_read_opt =
2210 				    ((stp->sd_read_opt & ~RD_MSGDIS) |
2211 				    RD_MSGNODIS);
2212 				break;
2213 			}
2214 			switch (sop->so_readopt & RPROTMASK) {
2215 			case RPROTNORM:
2216 				stp->sd_read_opt &= ~(RD_PROTDAT | RD_PROTDIS);
2217 				break;
2218 
2219 			case RPROTDAT:
2220 				stp->sd_read_opt =
2221 				    ((stp->sd_read_opt & ~RD_PROTDIS) |
2222 				    RD_PROTDAT);
2223 				break;
2224 
2225 			case RPROTDIS:
2226 				stp->sd_read_opt =
2227 				    ((stp->sd_read_opt & ~RD_PROTDAT) |
2228 				    RD_PROTDIS);
2229 				break;
2230 			}
2231 			switch (sop->so_readopt & RFLUSHMASK) {
2232 			case RFLUSHPCPROT:
2233 				/*
2234 				 * This sets the stream head to NOT flush
2235 				 * M_PCPROTO messages.
2236 				 */
2237 				stp->sd_read_opt |= RFLUSHPCPROT;
2238 				break;
2239 			}
2240 		}
2241 		if (sop->so_flags & SO_ERROPT) {
2242 			switch (sop->so_erropt & RERRMASK) {
2243 			case RERRNORM:
2244 				stp->sd_flag &= ~STRDERRNONPERSIST;
2245 				break;
2246 			case RERRNONPERSIST:
2247 				stp->sd_flag |= STRDERRNONPERSIST;
2248 				break;
2249 			}
2250 			switch (sop->so_erropt & WERRMASK) {
2251 			case WERRNORM:
2252 				stp->sd_flag &= ~STWRERRNONPERSIST;
2253 				break;
2254 			case WERRNONPERSIST:
2255 				stp->sd_flag |= STWRERRNONPERSIST;
2256 				break;
2257 			}
2258 		}
2259 		if (sop->so_flags & SO_COPYOPT) {
2260 			if (sop->so_copyopt & ZCVMSAFE) {
2261 				stp->sd_copyflag |= STZCVMSAFE;
2262 				stp->sd_copyflag &= ~STZCVMUNSAFE;
2263 			} else if (sop->so_copyopt & ZCVMUNSAFE) {
2264 				stp->sd_copyflag |= STZCVMUNSAFE;
2265 				stp->sd_copyflag &= ~STZCVMSAFE;
2266 			}
2267 
2268 			if (sop->so_copyopt & COPYCACHED) {
2269 				stp->sd_copyflag |= STRCOPYCACHED;
2270 			}
2271 		}
2272 		if (sop->so_flags & SO_WROFF)
2273 			stp->sd_wroff = sop->so_wroff;
2274 		if (sop->so_flags & SO_TAIL)
2275 			stp->sd_tail = sop->so_tail;
2276 		if (sop->so_flags & SO_MINPSZ)
2277 			q->q_minpsz = sop->so_minpsz;
2278 		if (sop->so_flags & SO_MAXPSZ)
2279 			q->q_maxpsz = sop->so_maxpsz;
2280 		if (sop->so_flags & SO_MAXBLK)
2281 			stp->sd_maxblk = sop->so_maxblk;
2282 		if (sop->so_flags & SO_HIWAT) {
2283 		    if (sop->so_flags & SO_BAND) {
2284 			if (strqset(q, QHIWAT, sop->so_band, sop->so_hiwat))
2285 				cmn_err(CE_WARN,
2286 				    "strrput: could not allocate qband\n");
2287 			else
2288 				bpri = sop->so_band;
2289 		    } else {
2290 			q->q_hiwat = sop->so_hiwat;
2291 		    }
2292 		}
2293 		if (sop->so_flags & SO_LOWAT) {
2294 		    if (sop->so_flags & SO_BAND) {
2295 			if (strqset(q, QLOWAT, sop->so_band, sop->so_lowat))
2296 				cmn_err(CE_WARN,
2297 				    "strrput: could not allocate qband\n");
2298 			else
2299 				bpri = sop->so_band;
2300 		    } else {
2301 			q->q_lowat = sop->so_lowat;
2302 		    }
2303 		}
2304 		if (sop->so_flags & SO_MREADON)
2305 			stp->sd_flag |= SNDMREAD;
2306 		if (sop->so_flags & SO_MREADOFF)
2307 			stp->sd_flag &= ~SNDMREAD;
2308 		if (sop->so_flags & SO_NDELON)
2309 			stp->sd_flag |= OLDNDELAY;
2310 		if (sop->so_flags & SO_NDELOFF)
2311 			stp->sd_flag &= ~OLDNDELAY;
2312 		if (sop->so_flags & SO_ISTTY)
2313 			stp->sd_flag |= STRISTTY;
2314 		if (sop->so_flags & SO_ISNTTY)
2315 			stp->sd_flag &= ~STRISTTY;
2316 		if (sop->so_flags & SO_TOSTOP)
2317 			stp->sd_flag |= STRTOSTOP;
2318 		if (sop->so_flags & SO_TONSTOP)
2319 			stp->sd_flag &= ~STRTOSTOP;
2320 		if (sop->so_flags & SO_DELIM)
2321 			stp->sd_flag |= STRDELIM;
2322 		if (sop->so_flags & SO_NODELIM)
2323 			stp->sd_flag &= ~STRDELIM;
2324 
2325 		mutex_exit(&stp->sd_lock);
2326 		freemsg(bp);
2327 
2328 		/* Check backenable in case the water marks changed */
2329 		qbackenable(q, bpri);
2330 		return (0);
2331 
2332 	/*
2333 	 * The following set of cases deal with situations where two stream
2334 	 * heads are connected to each other (twisted streams).  These messages
2335 	 * have no meaning at the stream head.
2336 	 */
2337 	case M_BREAK:
2338 	case M_CTL:
2339 	case M_DELAY:
2340 	case M_START:
2341 	case M_STOP:
2342 	case M_IOCDATA:
2343 	case M_STARTI:
2344 	case M_STOPI:
2345 		freemsg(bp);
2346 		return (0);
2347 
2348 	case M_IOCTL:
2349 		/*
2350 		 * Always NAK this condition
2351 		 * (makes no sense)
2352 		 * If there is one or more threads in the read side
2353 		 * rwnext we have to defer the nacking until that thread
2354 		 * returns (in strget).
2355 		 */
2356 		mutex_enter(&stp->sd_lock);
2357 		if (stp->sd_struiodnak != 0) {
2358 			/*
2359 			 * Defer NAK to the streamhead. Queue at the end
2360 			 * the list.
2361 			 */
2362 			mblk_t *mp = stp->sd_struionak;
2363 
2364 			while (mp && mp->b_next)
2365 				mp = mp->b_next;
2366 			if (mp)
2367 				mp->b_next = bp;
2368 			else
2369 				stp->sd_struionak = bp;
2370 			bp->b_next = NULL;
2371 			mutex_exit(&stp->sd_lock);
2372 			return (0);
2373 		}
2374 		mutex_exit(&stp->sd_lock);
2375 
2376 		bp->b_datap->db_type = M_IOCNAK;
2377 		/*
2378 		 * Protect against the driver passing up
2379 		 * messages after it has done a qprocsoff.
2380 		 */
2381 		if (_OTHERQ(q)->q_next == NULL)
2382 			freemsg(bp);
2383 		else
2384 			qreply(q, bp);
2385 		return (0);
2386 
2387 	default:
2388 #ifdef DEBUG
2389 		cmn_err(CE_WARN,
2390 			"bad message type %x received at stream head\n",
2391 			bp->b_datap->db_type);
2392 #endif
2393 		freemsg(bp);
2394 		return (0);
2395 	}
2396 
2397 	/* NOTREACHED */
2398 }
2399 
2400 /*
2401  * Check if the stream pointed to by `stp' can be written to, and return an
2402  * error code if not.  If `eiohup' is set, then return EIO if STRHUP is set.
2403  * If `sigpipeok' is set and the SW_SIGPIPE option is enabled on the stream,
2404  * then always return EPIPE and send a SIGPIPE to the invoking thread.
2405  */
2406 static int
2407 strwriteable(struct stdata *stp, boolean_t eiohup, boolean_t sigpipeok)
2408 {
2409 	int error;
2410 
2411 	ASSERT(MUTEX_HELD(&stp->sd_lock));
2412 
2413 	/*
2414 	 * For modem support, POSIX states that on writes, EIO should
2415 	 * be returned if the stream has been hung up.
2416 	 */
2417 	if (eiohup && (stp->sd_flag & (STPLEX|STRHUP)) == STRHUP)
2418 		error = EIO;
2419 	else
2420 		error = strgeterr(stp, STRHUP|STPLEX|STWRERR, 0);
2421 
2422 	if (error != 0) {
2423 		if (!(stp->sd_flag & STPLEX) &&
2424 		    (stp->sd_wput_opt & SW_SIGPIPE) && sigpipeok) {
2425 			tsignal(curthread, SIGPIPE);
2426 			error = EPIPE;
2427 		}
2428 	}
2429 
2430 	return (error);
2431 }
2432 
2433 /*
2434  * Copyin and send data down a stream.
2435  * The caller will allocate and copyin any control part that precedes the
2436  * message and pass than in as mctl.
2437  *
2438  * Caller should *not* hold sd_lock.
2439  * When EWOULDBLOCK is returned the caller has to redo the canputnext
2440  * under sd_lock in order to avoid missing a backenabling wakeup.
2441  *
2442  * Use iosize = -1 to not send any M_DATA. iosize = 0 sends zero-length M_DATA.
2443  *
2444  * Set MSG_IGNFLOW in flags to ignore flow control for hipri messages.
2445  * For sync streams we can only ignore flow control by reverting to using
2446  * putnext.
2447  *
2448  * If sd_maxblk is less than *iosize this routine might return without
2449  * transferring all of *iosize. In all cases, on return *iosize will contain
2450  * the amount of data that was transferred.
2451  */
2452 static int
2453 strput(struct stdata *stp, mblk_t *mctl, struct uio *uiop, ssize_t *iosize,
2454     int b_flag, int pri, int flags)
2455 {
2456 	struiod_t uiod;
2457 	mblk_t *mp;
2458 	queue_t *wqp = stp->sd_wrq;
2459 	int error = 0;
2460 	ssize_t count = *iosize;
2461 	cred_t *cr;
2462 
2463 	ASSERT(MUTEX_NOT_HELD(&stp->sd_lock));
2464 
2465 	if (uiop != NULL && count >= 0)
2466 		flags |= stp->sd_struiowrq ? STRUIO_POSTPONE : 0;
2467 
2468 	if (!(flags & STRUIO_POSTPONE)) {
2469 		/*
2470 		 * Use regular canputnext, strmakedata, putnext sequence.
2471 		 */
2472 		if (pri == 0) {
2473 			if (!canputnext(wqp) && !(flags & MSG_IGNFLOW)) {
2474 				freemsg(mctl);
2475 				return (EWOULDBLOCK);
2476 			}
2477 		} else {
2478 			if (!(flags & MSG_IGNFLOW) && !bcanputnext(wqp, pri)) {
2479 				freemsg(mctl);
2480 				return (EWOULDBLOCK);
2481 			}
2482 		}
2483 
2484 		if ((error = strmakedata(iosize, uiop, stp, flags,
2485 					&mp)) != 0) {
2486 			freemsg(mctl);
2487 			/*
2488 			 * need to change return code to ENOMEM
2489 			 * so that this is not confused with
2490 			 * flow control, EAGAIN.
2491 			 */
2492 
2493 			if (error == EAGAIN)
2494 				return (ENOMEM);
2495 			else
2496 				return (error);
2497 		}
2498 		if (mctl != NULL) {
2499 			if (mctl->b_cont == NULL)
2500 				mctl->b_cont = mp;
2501 			else if (mp != NULL)
2502 				linkb(mctl, mp);
2503 			mp = mctl;
2504 			/*
2505 			 * Note that for interrupt thread, the CRED() is
2506 			 * NULL. Don't bother with the pid either.
2507 			 */
2508 			if ((cr = CRED()) != NULL) {
2509 				mblk_setcred(mp, cr);
2510 				DB_CPID(mp) = curproc->p_pid;
2511 			}
2512 		} else if (mp == NULL)
2513 			return (0);
2514 
2515 		mp->b_flag |= b_flag;
2516 		mp->b_band = (uchar_t)pri;
2517 
2518 		if (flags & MSG_IGNFLOW) {
2519 			/*
2520 			 * XXX Hack: Don't get stuck running service
2521 			 * procedures. This is needed for sockfs when
2522 			 * sending the unbind message out of the rput
2523 			 * procedure - we don't want a put procedure
2524 			 * to run service procedures.
2525 			 */
2526 			putnext(wqp, mp);
2527 		} else {
2528 			stream_willservice(stp);
2529 			putnext(wqp, mp);
2530 			stream_runservice(stp);
2531 		}
2532 		return (0);
2533 	}
2534 	/*
2535 	 * Stream supports rwnext() for the write side.
2536 	 */
2537 	if ((error = strmakedata(iosize, uiop, stp, flags, &mp)) != 0) {
2538 		freemsg(mctl);
2539 		/*
2540 		 * map EAGAIN to ENOMEM since EAGAIN means "flow controlled".
2541 		 */
2542 		return (error == EAGAIN ? ENOMEM : error);
2543 	}
2544 	if (mctl != NULL) {
2545 		if (mctl->b_cont == NULL)
2546 			mctl->b_cont = mp;
2547 		else if (mp != NULL)
2548 			linkb(mctl, mp);
2549 		mp = mctl;
2550 		/*
2551 		 * Note that for interrupt thread, the CRED() is
2552 		 * NULL.  Don't bother with the pid either.
2553 		 */
2554 		if ((cr = CRED()) != NULL) {
2555 			mblk_setcred(mp, cr);
2556 			DB_CPID(mp) = curproc->p_pid;
2557 		}
2558 	} else if (mp == NULL) {
2559 		return (0);
2560 	}
2561 
2562 	mp->b_flag |= b_flag;
2563 	mp->b_band = (uchar_t)pri;
2564 
2565 	(void) uiodup(uiop, &uiod.d_uio, uiod.d_iov,
2566 		sizeof (uiod.d_iov) / sizeof (*uiod.d_iov));
2567 	uiod.d_uio.uio_offset = 0;
2568 	uiod.d_mp = mp;
2569 	error = rwnext(wqp, &uiod);
2570 	if (! uiod.d_mp) {
2571 		uioskip(uiop, *iosize);
2572 		return (error);
2573 	}
2574 	ASSERT(mp == uiod.d_mp);
2575 	if (error == EINVAL) {
2576 		/*
2577 		 * The stream plumbing must have changed while
2578 		 * we were away, so just turn off rwnext()s.
2579 		 */
2580 		error = 0;
2581 	} else if (error == EBUSY || error == EWOULDBLOCK) {
2582 		/*
2583 		 * Couldn't enter a perimeter or took a page fault,
2584 		 * so fall-back to putnext().
2585 		 */
2586 		error = 0;
2587 	} else {
2588 		freemsg(mp);
2589 		return (error);
2590 	}
2591 	/* Have to check canput before consuming data from the uio */
2592 	if (pri == 0) {
2593 		if (!canputnext(wqp) && !(flags & MSG_IGNFLOW)) {
2594 			freemsg(mp);
2595 			return (EWOULDBLOCK);
2596 		}
2597 	} else {
2598 		if (!bcanputnext(wqp, pri) && !(flags & MSG_IGNFLOW)) {
2599 			freemsg(mp);
2600 			return (EWOULDBLOCK);
2601 		}
2602 	}
2603 	ASSERT(mp == uiod.d_mp);
2604 	/* Copyin data from the uio */
2605 	if ((error = struioget(wqp, mp, &uiod, 0)) != 0) {
2606 		freemsg(mp);
2607 		return (error);
2608 	}
2609 	uioskip(uiop, *iosize);
2610 	if (flags & MSG_IGNFLOW) {
2611 		/*
2612 		 * XXX Hack: Don't get stuck running service procedures.
2613 		 * This is needed for sockfs when sending the unbind message
2614 		 * out of the rput procedure - we don't want a put procedure
2615 		 * to run service procedures.
2616 		 */
2617 		putnext(wqp, mp);
2618 	} else {
2619 		stream_willservice(stp);
2620 		putnext(wqp, mp);
2621 		stream_runservice(stp);
2622 	}
2623 	return (0);
2624 }
2625 
2626 /*
2627  * Write attempts to break the write request into messages conforming
2628  * with the minimum and maximum packet sizes set downstream.
2629  *
2630  * Write will not block if downstream queue is full and
2631  * O_NDELAY is set, otherwise it will block waiting for the queue to get room.
2632  *
2633  * A write of zero bytes gets packaged into a zero length message and sent
2634  * downstream like any other message.
2635  *
2636  * If buffers of the requested sizes are not available, the write will
2637  * sleep until the buffers become available.
2638  *
2639  * Write (if specified) will supply a write offset in a message if it
2640  * makes sense. This can be specified by downstream modules as part of
2641  * a M_SETOPTS message.  Write will not supply the write offset if it
2642  * cannot supply any data in a buffer.  In other words, write will never
2643  * send down an empty packet due to a write offset.
2644  */
2645 /* ARGSUSED2 */
2646 int
2647 strwrite(struct vnode *vp, struct uio *uiop, cred_t *crp)
2648 {
2649 	return (strwrite_common(vp, uiop, crp, 0));
2650 }
2651 
2652 /* ARGSUSED2 */
2653 int
2654 strwrite_common(struct vnode *vp, struct uio *uiop, cred_t *crp, int wflag)
2655 {
2656 	struct stdata *stp;
2657 	struct queue *wqp;
2658 	ssize_t rmin, rmax;
2659 	ssize_t iosize;
2660 	int waitflag;
2661 	int tempmode;
2662 	int error = 0;
2663 	int b_flag;
2664 
2665 	ASSERT(vp->v_stream);
2666 	stp = vp->v_stream;
2667 
2668 	if (stp->sd_sidp != NULL && stp->sd_vnode->v_type != VFIFO)
2669 		if ((error = straccess(stp, JCWRITE)) != 0)
2670 			return (error);
2671 
2672 	if (stp->sd_flag & (STWRERR|STRHUP|STPLEX)) {
2673 		mutex_enter(&stp->sd_lock);
2674 		error = strwriteable(stp, B_TRUE, B_TRUE);
2675 		mutex_exit(&stp->sd_lock);
2676 		if (error != 0)
2677 			return (error);
2678 	}
2679 
2680 	wqp = stp->sd_wrq;
2681 
2682 	/* get these values from them cached in the stream head */
2683 	rmin = stp->sd_qn_minpsz;
2684 	rmax = stp->sd_qn_maxpsz;
2685 
2686 	/*
2687 	 * Check the min/max packet size constraints.  If min packet size
2688 	 * is non-zero, the write cannot be split into multiple messages
2689 	 * and still guarantee the size constraints.
2690 	 */
2691 	TRACE_1(TR_FAC_STREAMS_FR, TR_STRWRITE_IN, "strwrite in:q %p", wqp);
2692 
2693 	ASSERT((rmax >= 0) || (rmax == INFPSZ));
2694 	if (rmax == 0) {
2695 		return (0);
2696 	}
2697 	if (rmin > 0) {
2698 		if (uiop->uio_resid < rmin) {
2699 			TRACE_3(TR_FAC_STREAMS_FR, TR_STRWRITE_OUT,
2700 				"strwrite out:q %p out %d error %d",
2701 				wqp, 0, ERANGE);
2702 			return (ERANGE);
2703 		}
2704 		if ((rmax != INFPSZ) && (uiop->uio_resid > rmax)) {
2705 			TRACE_3(TR_FAC_STREAMS_FR, TR_STRWRITE_OUT,
2706 				"strwrite out:q %p out %d error %d",
2707 				wqp, 1, ERANGE);
2708 			return (ERANGE);
2709 		}
2710 	}
2711 
2712 	/*
2713 	 * Do until count satisfied or error.
2714 	 */
2715 	waitflag = WRITEWAIT | wflag;
2716 	if (stp->sd_flag & OLDNDELAY)
2717 		tempmode = uiop->uio_fmode & ~FNDELAY;
2718 	else
2719 		tempmode = uiop->uio_fmode;
2720 
2721 	if (rmax == INFPSZ)
2722 		rmax = uiop->uio_resid;
2723 
2724 	/*
2725 	 * Note that tempmode does not get used in strput/strmakedata
2726 	 * but only in strwaitq. The other routines use uio_fmode
2727 	 * unmodified.
2728 	 */
2729 
2730 	/* LINTED: constant in conditional context */
2731 	while (1) {	/* breaks when uio_resid reaches zero */
2732 		/*
2733 		 * Determine the size of the next message to be
2734 		 * packaged.  May have to break write into several
2735 		 * messages based on max packet size.
2736 		 */
2737 		iosize = MIN(uiop->uio_resid, rmax);
2738 
2739 		/*
2740 		 * Put block downstream when flow control allows it.
2741 		 */
2742 		if ((stp->sd_flag & STRDELIM) && (uiop->uio_resid == iosize))
2743 			b_flag = MSGDELIM;
2744 		else
2745 			b_flag = 0;
2746 
2747 		for (;;) {
2748 			int done = 0;
2749 
2750 			error = strput(stp, NULL, uiop, &iosize, b_flag,
2751 				0, 0);
2752 			if (error == 0)
2753 				break;
2754 			if (error != EWOULDBLOCK)
2755 				goto out;
2756 
2757 			mutex_enter(&stp->sd_lock);
2758 			/*
2759 			 * Check for a missed wakeup.
2760 			 * Needed since strput did not hold sd_lock across
2761 			 * the canputnext.
2762 			 */
2763 			if (canputnext(wqp)) {
2764 				/* Try again */
2765 				mutex_exit(&stp->sd_lock);
2766 				continue;
2767 			}
2768 			TRACE_1(TR_FAC_STREAMS_FR, TR_STRWRITE_WAIT,
2769 				"strwrite wait:q %p wait", wqp);
2770 			if ((error = strwaitq(stp, waitflag, (ssize_t)0,
2771 			    tempmode, -1, &done)) != 0 || done) {
2772 				mutex_exit(&stp->sd_lock);
2773 				if ((vp->v_type == VFIFO) &&
2774 				    (uiop->uio_fmode & FNDELAY) &&
2775 				    (error == EAGAIN))
2776 					error = 0;
2777 				goto out;
2778 			}
2779 			TRACE_1(TR_FAC_STREAMS_FR, TR_STRWRITE_WAKE,
2780 				"strwrite wake:q %p awakes", wqp);
2781 			mutex_exit(&stp->sd_lock);
2782 			if (stp->sd_sidp != NULL &&
2783 			    stp->sd_vnode->v_type != VFIFO)
2784 				if (error = straccess(stp, JCWRITE))
2785 					goto out;
2786 		}
2787 		waitflag |= NOINTR;
2788 		TRACE_2(TR_FAC_STREAMS_FR, TR_STRWRITE_RESID,
2789 			"strwrite resid:q %p uiop %p", wqp, uiop);
2790 		if (uiop->uio_resid) {
2791 			/* Recheck for errors - needed for sockets */
2792 			if ((stp->sd_wput_opt & SW_RECHECK_ERR) &&
2793 			    (stp->sd_flag & (STWRERR|STRHUP|STPLEX))) {
2794 				mutex_enter(&stp->sd_lock);
2795 				error = strwriteable(stp, B_FALSE, B_TRUE);
2796 				mutex_exit(&stp->sd_lock);
2797 				if (error != 0)
2798 					return (error);
2799 			}
2800 			continue;
2801 		}
2802 		break;
2803 	}
2804 out:
2805 	/*
2806 	 * For historical reasons, applications expect EAGAIN when a data
2807 	 * mblk_t cannot be allocated, so change ENOMEM back to EAGAIN.
2808 	 */
2809 	if (error == ENOMEM)
2810 		error = EAGAIN;
2811 	TRACE_3(TR_FAC_STREAMS_FR, TR_STRWRITE_OUT,
2812 		"strwrite out:q %p out %d error %d", wqp, 2, error);
2813 	return (error);
2814 }
2815 
2816 /*
2817  * Stream head write service routine.
2818  * Its job is to wake up any sleeping writers when a queue
2819  * downstream needs data (part of the flow control in putq and getq).
2820  * It also must wake anyone sleeping on a poll().
2821  * For stream head right below mux module, it must also invoke put procedure
2822  * of next downstream module.
2823  */
2824 int
2825 strwsrv(queue_t *q)
2826 {
2827 	struct stdata *stp;
2828 	queue_t *tq;
2829 	qband_t *qbp;
2830 	int i;
2831 	qband_t *myqbp;
2832 	int isevent;
2833 	unsigned char	qbf[NBAND];	/* band flushing backenable flags */
2834 
2835 	TRACE_1(TR_FAC_STREAMS_FR,
2836 		TR_STRWSRV, "strwsrv:q %p", q);
2837 	stp = (struct stdata *)q->q_ptr;
2838 	ASSERT(qclaimed(q));
2839 	mutex_enter(&stp->sd_lock);
2840 	ASSERT(!(stp->sd_flag & STPLEX));
2841 
2842 	if (stp->sd_flag & WSLEEP) {
2843 		stp->sd_flag &= ~WSLEEP;
2844 		cv_broadcast(&q->q_wait);
2845 	}
2846 	mutex_exit(&stp->sd_lock);
2847 
2848 	/* The other end of a stream pipe went away. */
2849 	if ((tq = q->q_next) == NULL) {
2850 		return (0);
2851 	}
2852 
2853 	/* Find the next module forward that has a service procedure */
2854 	claimstr(q);
2855 	tq = q->q_nfsrv;
2856 	ASSERT(tq != NULL);
2857 
2858 	if ((q->q_flag & QBACK)) {
2859 		if ((tq->q_flag & QFULL)) {
2860 			mutex_enter(QLOCK(tq));
2861 			if (!(tq->q_flag & QFULL)) {
2862 				mutex_exit(QLOCK(tq));
2863 				goto wakeup;
2864 			}
2865 			/*
2866 			 * The queue must have become full again. Set QWANTW
2867 			 * again so strwsrv will be back enabled when
2868 			 * the queue becomes non-full next time.
2869 			 */
2870 			tq->q_flag |= QWANTW;
2871 			mutex_exit(QLOCK(tq));
2872 		} else {
2873 		wakeup:
2874 			pollwakeup(&stp->sd_pollist, POLLWRNORM);
2875 			mutex_enter(&stp->sd_lock);
2876 			if (stp->sd_sigflags & S_WRNORM)
2877 				strsendsig(stp->sd_siglist, S_WRNORM, 0, 0);
2878 			mutex_exit(&stp->sd_lock);
2879 		}
2880 	}
2881 
2882 	isevent = 0;
2883 	i = 1;
2884 	bzero((caddr_t)qbf, NBAND);
2885 	mutex_enter(QLOCK(tq));
2886 	if ((myqbp = q->q_bandp) != NULL)
2887 		for (qbp = tq->q_bandp; qbp && myqbp; qbp = qbp->qb_next) {
2888 			ASSERT(myqbp);
2889 			if ((myqbp->qb_flag & QB_BACK)) {
2890 				if (qbp->qb_flag & QB_FULL) {
2891 					/*
2892 					 * The band must have become full again.
2893 					 * Set QB_WANTW again so strwsrv will
2894 					 * be back enabled when the band becomes
2895 					 * non-full next time.
2896 					 */
2897 					qbp->qb_flag |= QB_WANTW;
2898 				} else {
2899 					isevent = 1;
2900 					qbf[i] = 1;
2901 				}
2902 			}
2903 			myqbp = myqbp->qb_next;
2904 			i++;
2905 		}
2906 	mutex_exit(QLOCK(tq));
2907 
2908 	if (isevent) {
2909 	    for (i = tq->q_nband; i; i--) {
2910 		if (qbf[i]) {
2911 			pollwakeup(&stp->sd_pollist, POLLWRBAND);
2912 			mutex_enter(&stp->sd_lock);
2913 			if (stp->sd_sigflags & S_WRBAND)
2914 				strsendsig(stp->sd_siglist, S_WRBAND,
2915 					(uchar_t)i, 0);
2916 			mutex_exit(&stp->sd_lock);
2917 		}
2918 	    }
2919 	}
2920 
2921 	releasestr(q);
2922 	return (0);
2923 }
2924 
2925 /*
2926  * Special case of strcopyin/strcopyout for copying
2927  * struct strioctl that can deal with both data
2928  * models.
2929  */
2930 
2931 #ifdef	_LP64
2932 
2933 static int
2934 strcopyin_strioctl(void *from, void *to, int flag, int copyflag)
2935 {
2936 	struct	strioctl32 strioc32;
2937 	struct	strioctl *striocp;
2938 
2939 	if (copyflag & U_TO_K) {
2940 		ASSERT((copyflag & K_TO_K) == 0);
2941 
2942 		if ((flag & FMODELS) == DATAMODEL_ILP32) {
2943 			if (copyin(from, &strioc32, sizeof (strioc32)))
2944 				return (EFAULT);
2945 
2946 			striocp = (struct strioctl *)to;
2947 			striocp->ic_cmd	= strioc32.ic_cmd;
2948 			striocp->ic_timout = strioc32.ic_timout;
2949 			striocp->ic_len	= strioc32.ic_len;
2950 			striocp->ic_dp	= (char *)(uintptr_t)strioc32.ic_dp;
2951 
2952 		} else { /* NATIVE data model */
2953 			if (copyin(from, to, sizeof (struct strioctl))) {
2954 				return (EFAULT);
2955 			} else {
2956 				return (0);
2957 			}
2958 		}
2959 	} else {
2960 		ASSERT(copyflag & K_TO_K);
2961 		bcopy(from, to, sizeof (struct strioctl));
2962 	}
2963 	return (0);
2964 }
2965 
2966 static int
2967 strcopyout_strioctl(void *from, void *to, int flag, int copyflag)
2968 {
2969 	struct	strioctl32 strioc32;
2970 	struct	strioctl *striocp;
2971 
2972 	if (copyflag & U_TO_K) {
2973 		ASSERT((copyflag & K_TO_K) == 0);
2974 
2975 		if ((flag & FMODELS) == DATAMODEL_ILP32) {
2976 			striocp = (struct strioctl *)from;
2977 			strioc32.ic_cmd	= striocp->ic_cmd;
2978 			strioc32.ic_timout = striocp->ic_timout;
2979 			strioc32.ic_len	= striocp->ic_len;
2980 			strioc32.ic_dp	= (caddr32_t)(uintptr_t)striocp->ic_dp;
2981 			ASSERT((char *)(uintptr_t)strioc32.ic_dp ==
2982 			    striocp->ic_dp);
2983 
2984 			if (copyout(&strioc32, to, sizeof (strioc32)))
2985 				return (EFAULT);
2986 
2987 		} else { /* NATIVE data model */
2988 			if (copyout(from, to, sizeof (struct strioctl))) {
2989 				return (EFAULT);
2990 			} else {
2991 				return (0);
2992 			}
2993 		}
2994 	} else {
2995 		ASSERT(copyflag & K_TO_K);
2996 		bcopy(from, to, sizeof (struct strioctl));
2997 	}
2998 	return (0);
2999 }
3000 
3001 #else	/* ! _LP64 */
3002 
3003 /* ARGSUSED2 */
3004 static int
3005 strcopyin_strioctl(void *from, void *to, int flag, int copyflag)
3006 {
3007 	return (strcopyin(from, to, sizeof (struct strioctl), copyflag));
3008 }
3009 
3010 /* ARGSUSED2 */
3011 static int
3012 strcopyout_strioctl(void *from, void *to, int flag, int copyflag)
3013 {
3014 	return (strcopyout(from, to, sizeof (struct strioctl), copyflag));
3015 }
3016 
3017 #endif	/* _LP64 */
3018 
3019 /*
3020  * Determine type of job control semantics expected by user.  The
3021  * possibilities are:
3022  *	JCREAD	- Behaves like read() on fd; send SIGTTIN
3023  *	JCWRITE	- Behaves like write() on fd; send SIGTTOU if TOSTOP set
3024  *	JCSETP	- Sets a value in the stream; send SIGTTOU, ignore TOSTOP
3025  *	JCGETP	- Gets a value in the stream; no signals.
3026  * See straccess in strsubr.c for usage of these values.
3027  *
3028  * This routine also returns -1 for I_STR as a special case; the
3029  * caller must call again with the real ioctl number for
3030  * classification.
3031  */
3032 static int
3033 job_control_type(int cmd)
3034 {
3035 	switch (cmd) {
3036 	case I_STR:
3037 		return (-1);
3038 
3039 	case I_RECVFD:
3040 	case I_E_RECVFD:
3041 		return (JCREAD);
3042 
3043 	case I_FDINSERT:
3044 	case I_SENDFD:
3045 		return (JCWRITE);
3046 
3047 	case TCSETA:
3048 	case TCSETAW:
3049 	case TCSETAF:
3050 	case TCSBRK:
3051 	case TCXONC:
3052 	case TCFLSH:
3053 	case TCDSET:	/* Obsolete */
3054 	case TIOCSWINSZ:
3055 	case TCSETS:
3056 	case TCSETSW:
3057 	case TCSETSF:
3058 	case TIOCSETD:
3059 	case TIOCHPCL:
3060 	case TIOCSETP:
3061 	case TIOCSETN:
3062 	case TIOCEXCL:
3063 	case TIOCNXCL:
3064 	case TIOCFLUSH:
3065 	case TIOCSETC:
3066 	case TIOCLBIS:
3067 	case TIOCLBIC:
3068 	case TIOCLSET:
3069 	case TIOCSBRK:
3070 	case TIOCCBRK:
3071 	case TIOCSDTR:
3072 	case TIOCCDTR:
3073 	case TIOCSLTC:
3074 	case TIOCSTOP:
3075 	case TIOCSTART:
3076 	case TIOCSTI:
3077 	case TIOCSPGRP:
3078 	case TIOCMSET:
3079 	case TIOCMBIS:
3080 	case TIOCMBIC:
3081 	case TIOCREMOTE:
3082 	case TIOCSIGNAL:
3083 	case LDSETT:
3084 	case LDSMAP:	/* Obsolete */
3085 	case DIOCSETP:
3086 	case I_FLUSH:
3087 	case I_SRDOPT:
3088 	case I_SETSIG:
3089 	case I_SWROPT:
3090 	case I_FLUSHBAND:
3091 	case I_SETCLTIME:
3092 	case I_SERROPT:
3093 	case I_ESETSIG:
3094 	case FIONBIO:
3095 	case FIOASYNC:
3096 	case FIOSETOWN:
3097 	case JBOOT:	/* Obsolete */
3098 	case JTERM:	/* Obsolete */
3099 	case JTIMOM:	/* Obsolete */
3100 	case JZOMBOOT:	/* Obsolete */
3101 	case JAGENT:	/* Obsolete */
3102 	case JTRUN:	/* Obsolete */
3103 	case JXTPROTO:	/* Obsolete */
3104 		return (JCSETP);
3105 	}
3106 
3107 	return (JCGETP);
3108 }
3109 
3110 /*
3111  * ioctl for streams
3112  */
3113 int
3114 strioctl(struct vnode *vp, int cmd, intptr_t arg, int flag, int copyflag,
3115     cred_t *crp, int *rvalp)
3116 {
3117 	struct stdata *stp;
3118 	struct strioctl strioc;
3119 	struct uio uio;
3120 	struct iovec iov;
3121 	int access;
3122 	mblk_t *mp;
3123 	int error = 0;
3124 	int done = 0;
3125 	ssize_t	rmin, rmax;
3126 	queue_t *wrq;
3127 	queue_t *rdq;
3128 	boolean_t kioctl = B_FALSE;
3129 
3130 	if (flag & FKIOCTL) {
3131 		copyflag = K_TO_K;
3132 		kioctl = B_TRUE;
3133 	}
3134 	ASSERT(vp->v_stream);
3135 	ASSERT(copyflag == U_TO_K || copyflag == K_TO_K);
3136 	stp = vp->v_stream;
3137 
3138 	TRACE_3(TR_FAC_STREAMS_FR, TR_IOCTL_ENTER,
3139 		"strioctl:stp %p cmd %X arg %lX", stp, cmd, arg);
3140 
3141 #ifdef C2_AUDIT
3142 	if (audit_active)
3143 		audit_strioctl(vp, cmd, arg, flag, copyflag, crp, rvalp);
3144 #endif
3145 
3146 	/*
3147 	 * If the copy is kernel to kernel, make sure that the FNATIVE
3148 	 * flag is set.  After this it would be a serious error to have
3149 	 * no model flag.
3150 	 */
3151 	if (copyflag == K_TO_K)
3152 		flag = (flag & ~FMODELS) | FNATIVE;
3153 
3154 	ASSERT((flag & FMODELS) != 0);
3155 
3156 	wrq = stp->sd_wrq;
3157 	rdq = _RD(wrq);
3158 
3159 	access = job_control_type(cmd);
3160 
3161 	/* We should never see these here, should be handled by iwscn */
3162 	if (cmd == SRIOCSREDIR || cmd == SRIOCISREDIR)
3163 		return (EINVAL);
3164 
3165 	if (access != -1 && stp->sd_sidp != NULL &&
3166 	    stp->sd_vnode->v_type != VFIFO)
3167 		if (error = straccess(stp, access))
3168 			return (error);
3169 
3170 	/*
3171 	 * Check for sgttyb-related ioctls first, and complain as
3172 	 * necessary.
3173 	 */
3174 	switch (cmd) {
3175 	case TIOCGETP:
3176 	case TIOCSETP:
3177 	case TIOCSETN:
3178 		if (sgttyb_handling >= 2 && !sgttyb_complaint) {
3179 			sgttyb_complaint = B_TRUE;
3180 			cmn_err(CE_NOTE,
3181 			    "application used obsolete TIOC[GS]ET");
3182 		}
3183 		if (sgttyb_handling >= 3) {
3184 			tsignal(curthread, SIGSYS);
3185 			return (EIO);
3186 		}
3187 		break;
3188 	}
3189 
3190 	mutex_enter(&stp->sd_lock);
3191 
3192 	switch (cmd) {
3193 	case I_RECVFD:
3194 	case I_E_RECVFD:
3195 	case I_PEEK:
3196 	case I_NREAD:
3197 	case FIONREAD:
3198 	case FIORDCHK:
3199 	case I_ATMARK:
3200 	case FIONBIO:
3201 	case FIOASYNC:
3202 		if (stp->sd_flag & (STRDERR|STPLEX)) {
3203 			error = strgeterr(stp, STRDERR|STPLEX, 0);
3204 			if (error != 0) {
3205 				mutex_exit(&stp->sd_lock);
3206 				return (error);
3207 			}
3208 		}
3209 		break;
3210 
3211 	default:
3212 		if (stp->sd_flag & (STRDERR|STWRERR|STPLEX)) {
3213 			error = strgeterr(stp, STRDERR|STWRERR|STPLEX, 0);
3214 			if (error != 0) {
3215 				mutex_exit(&stp->sd_lock);
3216 				return (error);
3217 			}
3218 		}
3219 	}
3220 
3221 	mutex_exit(&stp->sd_lock);
3222 
3223 	switch (cmd) {
3224 	default:
3225 		/*
3226 		 * The stream head has hardcoded knowledge of a
3227 		 * miscellaneous collection of terminal-, keyboard- and
3228 		 * mouse-related ioctls, enumerated below.  This hardcoded
3229 		 * knowledge allows the stream head to automatically
3230 		 * convert transparent ioctl requests made by userland
3231 		 * programs into I_STR ioctls which many old STREAMS
3232 		 * modules and drivers require.
3233 		 *
3234 		 * No new ioctls should ever be added to this list.
3235 		 * Instead, the STREAMS module or driver should be written
3236 		 * to either handle transparent ioctls or require any
3237 		 * userland programs to use I_STR ioctls (by returning
3238 		 * EINVAL to any transparent ioctl requests).
3239 		 *
3240 		 * More importantly, removing ioctls from this list should
3241 		 * be done with the utmost care, since our STREAMS modules
3242 		 * and drivers *count* on the stream head performing this
3243 		 * conversion, and thus may panic while processing
3244 		 * transparent ioctl request for one of these ioctls (keep
3245 		 * in mind that third party modules and drivers may have
3246 		 * similar problems).
3247 		 */
3248 		if (((cmd & IOCTYPE) == LDIOC) ||
3249 		    ((cmd & IOCTYPE) == tIOC) ||
3250 		    ((cmd & IOCTYPE) == TIOC) ||
3251 		    ((cmd & IOCTYPE) == KIOC) ||
3252 		    ((cmd & IOCTYPE) == MSIOC) ||
3253 		    ((cmd & IOCTYPE) == VUIOC)) {
3254 			/*
3255 			 * The ioctl is a tty ioctl - set up strioc buffer
3256 			 * and call strdoioctl() to do the work.
3257 			 */
3258 			if (stp->sd_flag & STRHUP)
3259 				return (ENXIO);
3260 			strioc.ic_cmd = cmd;
3261 			strioc.ic_timout = INFTIM;
3262 
3263 			switch (cmd) {
3264 
3265 			case TCXONC:
3266 			case TCSBRK:
3267 			case TCFLSH:
3268 			case TCDSET:
3269 				{
3270 				int native_arg = (int)arg;
3271 				strioc.ic_len = sizeof (int);
3272 				strioc.ic_dp = (char *)&native_arg;
3273 				return (strdoioctl(stp, &strioc, flag,
3274 				    K_TO_K, crp, rvalp));
3275 				}
3276 
3277 			case TCSETA:
3278 			case TCSETAW:
3279 			case TCSETAF:
3280 				strioc.ic_len = sizeof (struct termio);
3281 				strioc.ic_dp = (char *)arg;
3282 				return (strdoioctl(stp, &strioc, flag,
3283 					copyflag, crp, rvalp));
3284 
3285 			case TCSETS:
3286 			case TCSETSW:
3287 			case TCSETSF:
3288 				strioc.ic_len = sizeof (struct termios);
3289 				strioc.ic_dp = (char *)arg;
3290 				return (strdoioctl(stp, &strioc, flag,
3291 					copyflag, crp, rvalp));
3292 
3293 			case LDSETT:
3294 				strioc.ic_len = sizeof (struct termcb);
3295 				strioc.ic_dp = (char *)arg;
3296 				return (strdoioctl(stp, &strioc, flag,
3297 					copyflag, crp, rvalp));
3298 
3299 			case TIOCSETP:
3300 				strioc.ic_len = sizeof (struct sgttyb);
3301 				strioc.ic_dp = (char *)arg;
3302 				return (strdoioctl(stp, &strioc, flag,
3303 					copyflag, crp, rvalp));
3304 
3305 			case TIOCSTI:
3306 				if ((flag & FREAD) == 0 &&
3307 				    secpolicy_sti(crp) != 0) {
3308 					return (EPERM);
3309 				}
3310 				if (stp->sd_sidp !=
3311 				    ttoproc(curthread)->p_sessp->s_sidp &&
3312 				    secpolicy_sti(crp) != 0) {
3313 					return (EACCES);
3314 				}
3315 
3316 				strioc.ic_len = sizeof (char);
3317 				strioc.ic_dp = (char *)arg;
3318 				return (strdoioctl(stp, &strioc, flag,
3319 					copyflag, crp, rvalp));
3320 
3321 			case TIOCSWINSZ:
3322 				strioc.ic_len = sizeof (struct winsize);
3323 				strioc.ic_dp = (char *)arg;
3324 				return (strdoioctl(stp, &strioc, flag,
3325 					copyflag, crp, rvalp));
3326 
3327 			case TIOCSSIZE:
3328 				strioc.ic_len = sizeof (struct ttysize);
3329 				strioc.ic_dp = (char *)arg;
3330 				return (strdoioctl(stp, &strioc, flag,
3331 					copyflag, crp, rvalp));
3332 
3333 			case TIOCSSOFTCAR:
3334 			case KIOCTRANS:
3335 			case KIOCTRANSABLE:
3336 			case KIOCCMD:
3337 			case KIOCSDIRECT:
3338 			case KIOCSCOMPAT:
3339 			case KIOCSKABORTEN:
3340 			case KIOCSRPTDELAY:
3341 			case KIOCSRPTRATE:
3342 			case VUIDSFORMAT:
3343 			case TIOCSPPS:
3344 				strioc.ic_len = sizeof (int);
3345 				strioc.ic_dp = (char *)arg;
3346 				return (strdoioctl(stp, &strioc, flag,
3347 					copyflag, crp, rvalp));
3348 
3349 			case KIOCSETKEY:
3350 			case KIOCGETKEY:
3351 				strioc.ic_len = sizeof (struct kiockey);
3352 				strioc.ic_dp = (char *)arg;
3353 				return (strdoioctl(stp, &strioc, flag,
3354 					copyflag, crp, rvalp));
3355 
3356 			case KIOCSKEY:
3357 			case KIOCGKEY:
3358 				strioc.ic_len = sizeof (struct kiockeymap);
3359 				strioc.ic_dp = (char *)arg;
3360 				return (strdoioctl(stp, &strioc, flag,
3361 					copyflag, crp, rvalp));
3362 
3363 			case KIOCSLED:
3364 				/* arg is a pointer to char */
3365 				strioc.ic_len = sizeof (char);
3366 				strioc.ic_dp = (char *)arg;
3367 				return (strdoioctl(stp, &strioc, flag,
3368 					copyflag, crp, rvalp));
3369 
3370 			case MSIOSETPARMS:
3371 				strioc.ic_len = sizeof (Ms_parms);
3372 				strioc.ic_dp = (char *)arg;
3373 				return (strdoioctl(stp, &strioc, flag,
3374 					copyflag, crp, rvalp));
3375 
3376 			case VUIDSADDR:
3377 			case VUIDGADDR:
3378 				strioc.ic_len = sizeof (struct vuid_addr_probe);
3379 				strioc.ic_dp = (char *)arg;
3380 				return (strdoioctl(stp, &strioc, flag,
3381 					copyflag, crp, rvalp));
3382 
3383 			/*
3384 			 * These M_IOCTL's don't require any data to be sent
3385 			 * downstream, and the driver will allocate and link
3386 			 * on its own mblk_t upon M_IOCACK -- thus we set
3387 			 * ic_len to zero and set ic_dp to arg so we know
3388 			 * where to copyout to later.
3389 			 */
3390 			case TIOCGSOFTCAR:
3391 			case TIOCGWINSZ:
3392 			case TIOCGSIZE:
3393 			case KIOCGTRANS:
3394 			case KIOCGTRANSABLE:
3395 			case KIOCTYPE:
3396 			case KIOCGDIRECT:
3397 			case KIOCGCOMPAT:
3398 			case KIOCLAYOUT:
3399 			case KIOCGLED:
3400 			case MSIOGETPARMS:
3401 			case MSIOBUTTONS:
3402 			case VUIDGFORMAT:
3403 			case TIOCGPPS:
3404 			case TIOCGPPSEV:
3405 			case TCGETA:
3406 			case TCGETS:
3407 			case LDGETT:
3408 			case TIOCGETP:
3409 			case KIOCGRPTDELAY:
3410 			case KIOCGRPTRATE:
3411 				strioc.ic_len = 0;
3412 				strioc.ic_dp = (char *)arg;
3413 				return (strdoioctl(stp, &strioc, flag,
3414 					copyflag, crp, rvalp));
3415 			}
3416 		}
3417 
3418 		/*
3419 		 * Unknown cmd - send it down as a transparent ioctl.
3420 		 */
3421 		strioc.ic_cmd = cmd;
3422 		strioc.ic_timout = INFTIM;
3423 		strioc.ic_len = TRANSPARENT;
3424 		strioc.ic_dp = (char *)&arg;
3425 
3426 		return (strdoioctl(stp, &strioc, flag, copyflag, crp, rvalp));
3427 
3428 	case I_STR:
3429 		/*
3430 		 * Stream ioctl.  Read in an strioctl buffer from the user
3431 		 * along with any data specified and send it downstream.
3432 		 * Strdoioctl will wait allow only one ioctl message at
3433 		 * a time, and waits for the acknowledgement.
3434 		 */
3435 
3436 		if (stp->sd_flag & STRHUP)
3437 			return (ENXIO);
3438 
3439 		error = strcopyin_strioctl((void *)arg, &strioc, flag,
3440 		    copyflag);
3441 		if (error != 0)
3442 			return (error);
3443 
3444 		if ((strioc.ic_len < 0) || (strioc.ic_timout < -1))
3445 			return (EINVAL);
3446 
3447 		access = job_control_type(strioc.ic_cmd);
3448 		if (access != -1 && stp->sd_sidp != NULL &&
3449 		    stp->sd_vnode->v_type != VFIFO &&
3450 		    (error = straccess(stp, access)) != 0)
3451 			return (error);
3452 
3453 		/*
3454 		 * The I_STR facility provides a trap door for malicious
3455 		 * code to send down bogus streamio(7I) ioctl commands to
3456 		 * unsuspecting STREAMS modules and drivers which expect to
3457 		 * only get these messages from the stream head.
3458 		 * Explicitly prohibit any streamio ioctls which can be
3459 		 * passed downstream by the stream head.  Note that we do
3460 		 * not block all streamio ioctls because the ioctl
3461 		 * numberspace is not well managed and thus it's possible
3462 		 * that a module or driver's ioctl numbers may accidentally
3463 		 * collide with them.
3464 		 */
3465 		switch (strioc.ic_cmd) {
3466 		case I_LINK:
3467 		case I_PLINK:
3468 		case I_UNLINK:
3469 		case I_PUNLINK:
3470 		case _I_GETPEERCRED:
3471 		case _I_PLINK_LH:
3472 			return (EINVAL);
3473 		}
3474 
3475 		error = strdoioctl(stp, &strioc, flag, copyflag, crp, rvalp);
3476 		if (error == 0) {
3477 			error = strcopyout_strioctl(&strioc, (void *)arg,
3478 			    flag, copyflag);
3479 		}
3480 		return (error);
3481 
3482 	case I_NREAD:
3483 		/*
3484 		 * Return number of bytes of data in first message
3485 		 * in queue in "arg" and return the number of messages
3486 		 * in queue in return value.
3487 		 */
3488 	    {
3489 		size_t	size;
3490 		int	retval;
3491 		int	count = 0;
3492 
3493 		mutex_enter(QLOCK(rdq));
3494 
3495 		size = msgdsize(rdq->q_first);
3496 		for (mp = rdq->q_first; mp != NULL; mp = mp->b_next)
3497 			count++;
3498 
3499 		mutex_exit(QLOCK(rdq));
3500 		if (stp->sd_struiordq) {
3501 			infod_t infod;
3502 
3503 			infod.d_cmd = INFOD_COUNT;
3504 			infod.d_count = 0;
3505 			if (count == 0) {
3506 				infod.d_cmd |= INFOD_FIRSTBYTES;
3507 				infod.d_bytes = 0;
3508 			}
3509 			infod.d_res = 0;
3510 			(void) infonext(rdq, &infod);
3511 			count += infod.d_count;
3512 			if (infod.d_res & INFOD_FIRSTBYTES)
3513 				size = infod.d_bytes;
3514 		}
3515 
3516 		/*
3517 		 * Drop down from size_t to the "int" required by the
3518 		 * interface.  Cap at INT_MAX.
3519 		 */
3520 		retval = MIN(size, INT_MAX);
3521 		error = strcopyout(&retval, (void *)arg, sizeof (retval),
3522 		    copyflag);
3523 		if (!error)
3524 			*rvalp = count;
3525 		return (error);
3526 	    }
3527 
3528 	case FIONREAD:
3529 		/*
3530 		 * Return number of bytes of data in all data messages
3531 		 * in queue in "arg".
3532 		 */
3533 	    {
3534 		size_t	size = 0;
3535 		int	retval;
3536 
3537 		mutex_enter(QLOCK(rdq));
3538 		for (mp = rdq->q_first; mp != NULL; mp = mp->b_next)
3539 			size += msgdsize(mp);
3540 		mutex_exit(QLOCK(rdq));
3541 
3542 		if (stp->sd_struiordq) {
3543 			infod_t infod;
3544 
3545 			infod.d_cmd = INFOD_BYTES;
3546 			infod.d_res = 0;
3547 			infod.d_bytes = 0;
3548 			(void) infonext(rdq, &infod);
3549 			size += infod.d_bytes;
3550 		}
3551 
3552 		/*
3553 		 * Drop down from size_t to the "int" required by the
3554 		 * interface.  Cap at INT_MAX.
3555 		 */
3556 		retval = MIN(size, INT_MAX);
3557 		error = strcopyout(&retval, (void *)arg, sizeof (retval),
3558 		    copyflag);
3559 
3560 		*rvalp = 0;
3561 		return (error);
3562 	    }
3563 	case FIORDCHK:
3564 		/*
3565 		 * FIORDCHK does not use arg value (like FIONREAD),
3566 		 * instead a count is returned. I_NREAD value may
3567 		 * not be accurate but safe. The real thing to do is
3568 		 * to add the msgdsizes of all data  messages until
3569 		 * a non-data message.
3570 		 */
3571 	    {
3572 		size_t size = 0;
3573 
3574 		mutex_enter(QLOCK(rdq));
3575 		for (mp = rdq->q_first; mp != NULL; mp = mp->b_next)
3576 			size += msgdsize(mp);
3577 		mutex_exit(QLOCK(rdq));
3578 
3579 		if (stp->sd_struiordq) {
3580 			infod_t infod;
3581 
3582 			infod.d_cmd = INFOD_BYTES;
3583 			infod.d_res = 0;
3584 			infod.d_bytes = 0;
3585 			(void) infonext(rdq, &infod);
3586 			size += infod.d_bytes;
3587 		}
3588 
3589 		/*
3590 		 * Since ioctl returns an int, and memory sizes under
3591 		 * LP64 may not fit, we return INT_MAX if the count was
3592 		 * actually greater.
3593 		 */
3594 		*rvalp = MIN(size, INT_MAX);
3595 		return (0);
3596 	    }
3597 
3598 	case I_FIND:
3599 		/*
3600 		 * Get module name.
3601 		 */
3602 	    {
3603 		char mname[FMNAMESZ + 1];
3604 		queue_t *q;
3605 
3606 		error = (copyflag & U_TO_K ? copyinstr : copystr)((void *)arg,
3607 		    mname, FMNAMESZ + 1, NULL);
3608 		if (error)
3609 			return ((error == ENAMETOOLONG) ? EINVAL : EFAULT);
3610 
3611 		/*
3612 		 * Return EINVAL if we're handed a bogus module name.
3613 		 */
3614 		if (fmodsw_find(mname, FMODSW_LOAD) == NULL) {
3615 			TRACE_0(TR_FAC_STREAMS_FR,
3616 				TR_I_CANT_FIND, "couldn't I_FIND");
3617 			return (EINVAL);
3618 		}
3619 
3620 		*rvalp = 0;
3621 
3622 		/* Look downstream to see if module is there. */
3623 		claimstr(stp->sd_wrq);
3624 		for (q = stp->sd_wrq->q_next; q; q = q->q_next) {
3625 			if (q->q_flag&QREADR) {
3626 				q = NULL;
3627 				break;
3628 			}
3629 			if (strcmp(mname, q->q_qinfo->qi_minfo->mi_idname) == 0)
3630 				break;
3631 		}
3632 		releasestr(stp->sd_wrq);
3633 
3634 		*rvalp = (q ? 1 : 0);
3635 		return (error);
3636 	    }
3637 
3638 	case I_PUSH:
3639 	case __I_PUSH_NOCTTY:
3640 		/*
3641 		 * Push a module.
3642 		 * For the case __I_PUSH_NOCTTY push a module but
3643 		 * do not allocate controlling tty. See bugid 4025044
3644 		 */
3645 
3646 	    {
3647 		char mname[FMNAMESZ + 1];
3648 		fmodsw_impl_t *fp;
3649 		dev_t dummydev;
3650 
3651 		if (stp->sd_flag & STRHUP)
3652 			return (ENXIO);
3653 
3654 		/*
3655 		 * Get module name and look up in fmodsw.
3656 		 */
3657 		error = (copyflag & U_TO_K ? copyinstr : copystr)((void *)arg,
3658 		    mname, FMNAMESZ + 1, NULL);
3659 		if (error)
3660 			return ((error == ENAMETOOLONG) ? EINVAL : EFAULT);
3661 
3662 		if ((fp = fmodsw_find(mname, FMODSW_HOLD | FMODSW_LOAD)) ==
3663 		    NULL)
3664 			return (EINVAL);
3665 
3666 		TRACE_2(TR_FAC_STREAMS_FR, TR_I_PUSH,
3667 		    "I_PUSH:fp %p stp %p", fp, stp);
3668 
3669 		if (error = strstartplumb(stp, flag, cmd)) {
3670 			fmodsw_rele(fp);
3671 			return (error);
3672 		}
3673 
3674 		/*
3675 		 * See if any more modules can be pushed on this stream.
3676 		 * Note that this check must be done after strstartplumb()
3677 		 * since otherwise multiple threads issuing I_PUSHes on
3678 		 * the same stream will be able to exceed nstrpush.
3679 		 */
3680 		mutex_enter(&stp->sd_lock);
3681 		if (stp->sd_pushcnt >= nstrpush) {
3682 			fmodsw_rele(fp);
3683 			strendplumb(stp);
3684 			mutex_exit(&stp->sd_lock);
3685 			return (EINVAL);
3686 		}
3687 		mutex_exit(&stp->sd_lock);
3688 
3689 		/*
3690 		 * Push new module and call its open routine
3691 		 * via qattach().  Modules don't change device
3692 		 * numbers, so just ignore dummydev here.
3693 		 */
3694 		dummydev = vp->v_rdev;
3695 		if ((error = qattach(rdq, &dummydev, 0, crp, fp,
3696 		    B_FALSE)) == 0) {
3697 			if (vp->v_type == VCHR && /* sorry, no pipes allowed */
3698 			    (cmd == I_PUSH) && (stp->sd_flag & STRISTTY)) {
3699 				/*
3700 				 * try to allocate it as a controlling terminal
3701 				 */
3702 				stralloctty(stp);
3703 			}
3704 		}
3705 
3706 		mutex_enter(&stp->sd_lock);
3707 
3708 		/*
3709 		 * As a performance concern we are caching the values of
3710 		 * q_minpsz and q_maxpsz of the module below the stream
3711 		 * head in the stream head.
3712 		 */
3713 		mutex_enter(QLOCK(stp->sd_wrq->q_next));
3714 		rmin = stp->sd_wrq->q_next->q_minpsz;
3715 		rmax = stp->sd_wrq->q_next->q_maxpsz;
3716 		mutex_exit(QLOCK(stp->sd_wrq->q_next));
3717 
3718 		/* Do this processing here as a performance concern */
3719 		if (strmsgsz != 0) {
3720 			if (rmax == INFPSZ)
3721 				rmax = strmsgsz;
3722 			else  {
3723 				if (vp->v_type == VFIFO)
3724 					rmax = MIN(PIPE_BUF, rmax);
3725 				else	rmax = MIN(strmsgsz, rmax);
3726 			}
3727 		}
3728 
3729 		mutex_enter(QLOCK(wrq));
3730 		stp->sd_qn_minpsz = rmin;
3731 		stp->sd_qn_maxpsz = rmax;
3732 		mutex_exit(QLOCK(wrq));
3733 
3734 		strendplumb(stp);
3735 		mutex_exit(&stp->sd_lock);
3736 		return (error);
3737 	    }
3738 
3739 	case I_POP:
3740 	    {
3741 		queue_t	*q;
3742 
3743 		if (stp->sd_flag & STRHUP)
3744 			return (ENXIO);
3745 		if (!wrq->q_next)	/* for broken pipes */
3746 			return (EINVAL);
3747 
3748 		if (error = strstartplumb(stp, flag, cmd))
3749 			return (error);
3750 
3751 		/*
3752 		 * If there is an anchor on this stream and popping
3753 		 * the current module would attempt to pop through the
3754 		 * anchor, then disallow the pop unless we have sufficient
3755 		 * privileges; take the cheapest (non-locking) check
3756 		 * first.
3757 		 */
3758 		if (secpolicy_net_config(crp, B_TRUE) != 0) {
3759 			mutex_enter(&stp->sd_lock);
3760 			/*
3761 			 * Anchors only apply if there's at least one
3762 			 * module on the stream (sd_pushcnt > 0).
3763 			 */
3764 			if (stp->sd_pushcnt > 0 &&
3765 			    stp->sd_pushcnt == stp->sd_anchor &&
3766 			    stp->sd_vnode->v_type != VFIFO) {
3767 				strendplumb(stp);
3768 				mutex_exit(&stp->sd_lock);
3769 				/* Audit and report error */
3770 				return (secpolicy_net_config(crp, B_FALSE));
3771 			}
3772 			mutex_exit(&stp->sd_lock);
3773 		}
3774 
3775 		q = wrq->q_next;
3776 		TRACE_2(TR_FAC_STREAMS_FR, TR_I_POP,
3777 			"I_POP:%p from %p", q, stp);
3778 		if (q->q_next == NULL || (q->q_flag & (QREADR|QISDRV))) {
3779 			error = EINVAL;
3780 		} else {
3781 			qdetach(_RD(q), 1, flag, crp, B_FALSE);
3782 			error = 0;
3783 		}
3784 		mutex_enter(&stp->sd_lock);
3785 
3786 		/*
3787 		 * As a performance concern we are caching the values of
3788 		 * q_minpsz and q_maxpsz of the module below the stream
3789 		 * head in the stream head.
3790 		 */
3791 		mutex_enter(QLOCK(wrq->q_next));
3792 		rmin = wrq->q_next->q_minpsz;
3793 		rmax = wrq->q_next->q_maxpsz;
3794 		mutex_exit(QLOCK(wrq->q_next));
3795 
3796 		/* Do this processing here as a performance concern */
3797 		if (strmsgsz != 0) {
3798 			if (rmax == INFPSZ)
3799 				rmax = strmsgsz;
3800 			else  {
3801 				if (vp->v_type == VFIFO)
3802 					rmax = MIN(PIPE_BUF, rmax);
3803 				else	rmax = MIN(strmsgsz, rmax);
3804 			}
3805 		}
3806 
3807 		mutex_enter(QLOCK(wrq));
3808 		stp->sd_qn_minpsz = rmin;
3809 		stp->sd_qn_maxpsz = rmax;
3810 		mutex_exit(QLOCK(wrq));
3811 
3812 		/* If we popped through the anchor, then reset the anchor. */
3813 		if (stp->sd_pushcnt < stp->sd_anchor)
3814 			stp->sd_anchor = 0;
3815 
3816 		strendplumb(stp);
3817 		mutex_exit(&stp->sd_lock);
3818 		return (error);
3819 	    }
3820 
3821 	case _I_MUXID2FD:
3822 	{
3823 		/*
3824 		 * Create a fd for a I_PLINK'ed lower stream with a given
3825 		 * muxid.  With the fd, application can send down ioctls,
3826 		 * like I_LIST, to the previously I_PLINK'ed stream.  Note
3827 		 * that after getting the fd, the application has to do an
3828 		 * I_PUNLINK on the muxid before it can do any operation
3829 		 * on the lower stream.  This is required by spec1170.
3830 		 *
3831 		 * The fd used to do this ioctl should point to the same
3832 		 * controlling device used to do the I_PLINK.  If it uses
3833 		 * a different stream or an invalid muxid, I_MUXID2FD will
3834 		 * fail.  The error code is set to EINVAL.
3835 		 *
3836 		 * The intended use of this interface is the following.
3837 		 * An application I_PLINK'ed a stream and exits.  The fd
3838 		 * to the lower stream is gone.  Another application
3839 		 * wants to get a fd to the lower stream, it uses I_MUXID2FD.
3840 		 */
3841 		int muxid = (int)arg;
3842 		int fd;
3843 		linkinfo_t *linkp;
3844 		struct file *fp;
3845 
3846 		/*
3847 		 * Do not allow the wildcard muxid.  This ioctl is not
3848 		 * intended to find arbitrary link.
3849 		 */
3850 		if (muxid == 0) {
3851 			return (EINVAL);
3852 		}
3853 
3854 		mutex_enter(&muxifier);
3855 		linkp = findlinks(vp->v_stream, muxid, LINKPERSIST);
3856 		if (linkp == NULL) {
3857 			mutex_exit(&muxifier);
3858 			return (EINVAL);
3859 		}
3860 
3861 		if ((fd = ufalloc(0)) == -1) {
3862 			mutex_exit(&muxifier);
3863 			return (EMFILE);
3864 		}
3865 		fp = linkp->li_fpdown;
3866 		mutex_enter(&fp->f_tlock);
3867 		fp->f_count++;
3868 		mutex_exit(&fp->f_tlock);
3869 		mutex_exit(&muxifier);
3870 		setf(fd, fp);
3871 		*rvalp = fd;
3872 		return (0);
3873 	}
3874 
3875 	case _I_INSERT:
3876 	{
3877 		/*
3878 		 * To insert a module to a given position in a stream.
3879 		 * In the first release, only allow privileged user
3880 		 * to use this ioctl.
3881 		 *
3882 		 * Note that we do not plan to support this ioctl
3883 		 * on pipes in the first release.  We want to learn more
3884 		 * about the implications of these ioctls before extending
3885 		 * their support.  And we do not think these features are
3886 		 * valuable for pipes.
3887 		 *
3888 		 * Neither do we support O/C hot stream.  Note that only
3889 		 * the upper streams of TCP/IP stack are O/C hot streams.
3890 		 * The lower IP stream is not.
3891 		 * When there is a O/C cold barrier, we only allow inserts
3892 		 * above the barrier.
3893 		 */
3894 		STRUCT_DECL(strmodconf, strmodinsert);
3895 		char mod_name[FMNAMESZ + 1];
3896 		fmodsw_impl_t *fp;
3897 		dev_t dummydev;
3898 		queue_t *tmp_wrq;
3899 		int pos;
3900 		boolean_t is_insert;
3901 
3902 		STRUCT_INIT(strmodinsert, flag);
3903 		if (stp->sd_flag & STRHUP)
3904 			return (ENXIO);
3905 		if (STRMATED(stp))
3906 			return (EINVAL);
3907 		if ((error = secpolicy_net_config(crp, B_FALSE)) != 0)
3908 			return (error);
3909 
3910 		error = strcopyin((void *)arg, STRUCT_BUF(strmodinsert),
3911 		    STRUCT_SIZE(strmodinsert), copyflag);
3912 		if (error)
3913 			return (error);
3914 
3915 		/*
3916 		 * Get module name and look up in fmodsw.
3917 		 */
3918 		error = (copyflag & U_TO_K ? copyinstr :
3919 		    copystr)(STRUCT_FGETP(strmodinsert, mod_name),
3920 		    mod_name, FMNAMESZ + 1, NULL);
3921 		if (error)
3922 			return ((error == ENAMETOOLONG) ? EINVAL : EFAULT);
3923 
3924 		if ((fp = fmodsw_find(mod_name, FMODSW_HOLD | FMODSW_LOAD)) ==
3925 		    NULL)
3926 			return (EINVAL);
3927 
3928 		if (error = strstartplumb(stp, flag, cmd)) {
3929 			fmodsw_rele(fp);
3930 			return (error);
3931 		}
3932 
3933 		/*
3934 		 * Is this _I_INSERT just like an I_PUSH?  We need to know
3935 		 * this because we do some optimizations if this is a
3936 		 * module being pushed.
3937 		 */
3938 		pos = STRUCT_FGET(strmodinsert, pos);
3939 		is_insert = (pos != 0);
3940 
3941 		/*
3942 		 * Make sure pos is valid.  Even though it is not an I_PUSH,
3943 		 * we impose the same limit on the number of modules in a
3944 		 * stream.
3945 		 */
3946 		mutex_enter(&stp->sd_lock);
3947 		if (stp->sd_pushcnt >= nstrpush || pos < 0 ||
3948 		    pos > stp->sd_pushcnt) {
3949 			fmodsw_rele(fp);
3950 			strendplumb(stp);
3951 			mutex_exit(&stp->sd_lock);
3952 			return (EINVAL);
3953 		}
3954 		mutex_exit(&stp->sd_lock);
3955 
3956 		/*
3957 		 * First find the correct position this module to
3958 		 * be inserted.  We don't need to call claimstr()
3959 		 * as the stream should not be changing at this point.
3960 		 *
3961 		 * Insert new module and call its open routine
3962 		 * via qattach().  Modules don't change device
3963 		 * numbers, so just ignore dummydev here.
3964 		 */
3965 		for (tmp_wrq = stp->sd_wrq; pos > 0;
3966 		    tmp_wrq = tmp_wrq->q_next, pos--) {
3967 			ASSERT(SAMESTR(tmp_wrq));
3968 		}
3969 		dummydev = vp->v_rdev;
3970 		if ((error = qattach(_RD(tmp_wrq), &dummydev, 0, crp,
3971 		    fp, is_insert)) != 0) {
3972 			mutex_enter(&stp->sd_lock);
3973 			strendplumb(stp);
3974 			mutex_exit(&stp->sd_lock);
3975 			return (error);
3976 		}
3977 
3978 		mutex_enter(&stp->sd_lock);
3979 
3980 		/*
3981 		 * As a performance concern we are caching the values of
3982 		 * q_minpsz and q_maxpsz of the module below the stream
3983 		 * head in the stream head.
3984 		 */
3985 		if (!is_insert) {
3986 			mutex_enter(QLOCK(stp->sd_wrq->q_next));
3987 			rmin = stp->sd_wrq->q_next->q_minpsz;
3988 			rmax = stp->sd_wrq->q_next->q_maxpsz;
3989 			mutex_exit(QLOCK(stp->sd_wrq->q_next));
3990 
3991 			/* Do this processing here as a performance concern */
3992 			if (strmsgsz != 0) {
3993 				if (rmax == INFPSZ) {
3994 					rmax = strmsgsz;
3995 				} else  {
3996 					rmax = MIN(strmsgsz, rmax);
3997 				}
3998 			}
3999 
4000 			mutex_enter(QLOCK(wrq));
4001 			stp->sd_qn_minpsz = rmin;
4002 			stp->sd_qn_maxpsz = rmax;
4003 			mutex_exit(QLOCK(wrq));
4004 		}
4005 
4006 		/*
4007 		 * Need to update the anchor value if this module is
4008 		 * inserted below the anchor point.
4009 		 */
4010 		if (stp->sd_anchor != 0) {
4011 			pos = STRUCT_FGET(strmodinsert, pos);
4012 			if (pos >= (stp->sd_pushcnt - stp->sd_anchor))
4013 				stp->sd_anchor++;
4014 		}
4015 
4016 		strendplumb(stp);
4017 		mutex_exit(&stp->sd_lock);
4018 		return (0);
4019 	}
4020 
4021 	case _I_REMOVE:
4022 	{
4023 		/*
4024 		 * To remove a module with a given name in a stream.  The
4025 		 * caller of this ioctl needs to provide both the name and
4026 		 * the position of the module to be removed.  This eliminates
4027 		 * the ambiguity of removal if a module is inserted/pushed
4028 		 * multiple times in a stream.  In the first release, only
4029 		 * allow privileged user to use this ioctl.
4030 		 *
4031 		 * Note that we do not plan to support this ioctl
4032 		 * on pipes in the first release.  We want to learn more
4033 		 * about the implications of these ioctls before extending
4034 		 * their support.  And we do not think these features are
4035 		 * valuable for pipes.
4036 		 *
4037 		 * Neither do we support O/C hot stream.  Note that only
4038 		 * the upper streams of TCP/IP stack are O/C hot streams.
4039 		 * The lower IP stream is not.
4040 		 * When there is a O/C cold barrier we do not allow removal
4041 		 * below the barrier.
4042 		 *
4043 		 * Also note that _I_REMOVE cannot be used to remove a
4044 		 * driver or the stream head.
4045 		 */
4046 		STRUCT_DECL(strmodconf, strmodremove);
4047 		queue_t	*q;
4048 		int pos;
4049 		char mod_name[FMNAMESZ + 1];
4050 		boolean_t is_remove;
4051 
4052 		STRUCT_INIT(strmodremove, flag);
4053 		if (stp->sd_flag & STRHUP)
4054 			return (ENXIO);
4055 		if (STRMATED(stp))
4056 			return (EINVAL);
4057 		if ((error = secpolicy_net_config(crp, B_FALSE)) != 0)
4058 			return (error);
4059 
4060 		error = strcopyin((void *)arg, STRUCT_BUF(strmodremove),
4061 		    STRUCT_SIZE(strmodremove), copyflag);
4062 		if (error)
4063 			return (error);
4064 
4065 		error = (copyflag & U_TO_K ? copyinstr :
4066 		    copystr)(STRUCT_FGETP(strmodremove, mod_name),
4067 		    mod_name, FMNAMESZ + 1, NULL);
4068 		if (error)
4069 			return ((error == ENAMETOOLONG) ? EINVAL : EFAULT);
4070 
4071 		if ((error = strstartplumb(stp, flag, cmd)) != 0)
4072 			return (error);
4073 
4074 		/*
4075 		 * Match the name of given module to the name of module at
4076 		 * the given position.
4077 		 */
4078 		pos = STRUCT_FGET(strmodremove, pos);
4079 
4080 		is_remove = (pos != 0);
4081 		for (q = stp->sd_wrq->q_next; SAMESTR(q) && pos > 0;
4082 		    q = q->q_next, pos--)
4083 			;
4084 		if (pos > 0 || ! SAMESTR(q) ||
4085 		    strncmp(q->q_qinfo->qi_minfo->mi_idname, mod_name,
4086 		    strlen(q->q_qinfo->qi_minfo->mi_idname)) != 0) {
4087 			mutex_enter(&stp->sd_lock);
4088 			strendplumb(stp);
4089 			mutex_exit(&stp->sd_lock);
4090 			return (EINVAL);
4091 		}
4092 
4093 		ASSERT(!(q->q_flag & QREADR));
4094 		qdetach(_RD(q), 1, flag, crp, is_remove);
4095 
4096 		mutex_enter(&stp->sd_lock);
4097 
4098 		/*
4099 		 * As a performance concern we are caching the values of
4100 		 * q_minpsz and q_maxpsz of the module below the stream
4101 		 * head in the stream head.
4102 		 */
4103 		if (!is_remove) {
4104 			mutex_enter(QLOCK(wrq->q_next));
4105 			rmin = wrq->q_next->q_minpsz;
4106 			rmax = wrq->q_next->q_maxpsz;
4107 			mutex_exit(QLOCK(wrq->q_next));
4108 
4109 			/* Do this processing here as a performance concern */
4110 			if (strmsgsz != 0) {
4111 				if (rmax == INFPSZ)
4112 					rmax = strmsgsz;
4113 				else  {
4114 					if (vp->v_type == VFIFO)
4115 						rmax = MIN(PIPE_BUF, rmax);
4116 					else	rmax = MIN(strmsgsz, rmax);
4117 				}
4118 			}
4119 
4120 			mutex_enter(QLOCK(wrq));
4121 			stp->sd_qn_minpsz = rmin;
4122 			stp->sd_qn_maxpsz = rmax;
4123 			mutex_exit(QLOCK(wrq));
4124 		}
4125 
4126 		/*
4127 		 * Need to update the anchor value if this module is removed
4128 		 * at or below the anchor point.  If the removed module is at
4129 		 * the anchor point, remove the anchor for this stream if
4130 		 * there is no module above the anchor point.  Otherwise, if
4131 		 * the removed module is below the anchor point, decrement the
4132 		 * anchor point by 1.
4133 		 */
4134 		if (stp->sd_anchor != 0) {
4135 			pos = STRUCT_FGET(strmodremove, pos);
4136 			if (pos == 0)
4137 				stp->sd_anchor = 0;
4138 			else if (pos > (stp->sd_pushcnt - stp->sd_anchor + 1))
4139 				stp->sd_anchor--;
4140 		}
4141 
4142 		strendplumb(stp);
4143 		mutex_exit(&stp->sd_lock);
4144 		return (0);
4145 	}
4146 
4147 	case I_ANCHOR:
4148 		/*
4149 		 * Set the anchor position on the stream to reside at
4150 		 * the top module (in other words, the top module
4151 		 * cannot be popped).  Anchors with a FIFO make no
4152 		 * obvious sense, so they're not allowed.
4153 		 */
4154 		mutex_enter(&stp->sd_lock);
4155 
4156 		if (stp->sd_vnode->v_type == VFIFO) {
4157 			mutex_exit(&stp->sd_lock);
4158 			return (EINVAL);
4159 		}
4160 
4161 		stp->sd_anchor = stp->sd_pushcnt;
4162 
4163 		mutex_exit(&stp->sd_lock);
4164 		return (0);
4165 
4166 	case I_LOOK:
4167 		/*
4168 		 * Get name of first module downstream.
4169 		 * If no module, return an error.
4170 		 */
4171 	    {
4172 		claimstr(wrq);
4173 		if (_SAMESTR(wrq) && wrq->q_next->q_next) {
4174 			char *name = wrq->q_next->q_qinfo->qi_minfo->mi_idname;
4175 			error = strcopyout(name, (void *)arg, strlen(name) + 1,
4176 			    copyflag);
4177 			releasestr(wrq);
4178 			return (error);
4179 		}
4180 		releasestr(wrq);
4181 		return (EINVAL);
4182 	    }
4183 
4184 	case I_LINK:
4185 	case I_PLINK:
4186 		/*
4187 		 * Link a multiplexor.
4188 		 */
4189 		return (mlink(vp, cmd, (int)arg, crp, rvalp, 0));
4190 
4191 	case _I_PLINK_LH:
4192 		/*
4193 		 * Link a multiplexor: Call must originate from kernel.
4194 		 */
4195 		if (kioctl)
4196 			return (ldi_mlink_lh(vp, cmd, arg, crp, rvalp));
4197 
4198 		return (EINVAL);
4199 	case I_UNLINK:
4200 	case I_PUNLINK:
4201 		/*
4202 		 * Unlink a multiplexor.
4203 		 * If arg is -1, unlink all links for which this is the
4204 		 * controlling stream.  Otherwise, arg is an index number
4205 		 * for a link to be removed.
4206 		 */
4207 	    {
4208 		struct linkinfo *linkp;
4209 		int native_arg = (int)arg;
4210 		int type;
4211 
4212 		TRACE_1(TR_FAC_STREAMS_FR,
4213 			TR_I_UNLINK, "I_UNLINK/I_PUNLINK:%p", stp);
4214 		if (vp->v_type == VFIFO) {
4215 			return (EINVAL);
4216 		}
4217 		if (cmd == I_UNLINK)
4218 			type = LINKNORMAL;
4219 		else	/* I_PUNLINK */
4220 			type = LINKPERSIST;
4221 		if (native_arg == 0) {
4222 			return (EINVAL);
4223 		}
4224 		if (native_arg == MUXID_ALL)
4225 			error = munlinkall(stp, type, crp, rvalp);
4226 		else {
4227 			mutex_enter(&muxifier);
4228 			if (!(linkp = findlinks(stp, (int)arg, type))) {
4229 				/* invalid user supplied index number */
4230 				mutex_exit(&muxifier);
4231 				return (EINVAL);
4232 			}
4233 			/* munlink drops the muxifier lock */
4234 			error = munlink(stp, linkp, type, crp, rvalp);
4235 		}
4236 		return (error);
4237 	    }
4238 
4239 	case I_FLUSH:
4240 		/*
4241 		 * send a flush message downstream
4242 		 * flush message can indicate
4243 		 * FLUSHR - flush read queue
4244 		 * FLUSHW - flush write queue
4245 		 * FLUSHRW - flush read/write queue
4246 		 */
4247 		if (stp->sd_flag & STRHUP)
4248 			return (ENXIO);
4249 		if (arg & ~FLUSHRW)
4250 			return (EINVAL);
4251 
4252 		for (;;) {
4253 			if (putnextctl1(stp->sd_wrq, M_FLUSH, (int)arg)) {
4254 				break;
4255 			}
4256 			if (error = strwaitbuf(1, BPRI_HI)) {
4257 				return (error);
4258 			}
4259 		}
4260 
4261 		/*
4262 		 * Send down an unsupported ioctl and wait for the nack
4263 		 * in order to allow the M_FLUSH to propagate back
4264 		 * up to the stream head.
4265 		 * Replaces if (qready()) runqueues();
4266 		 */
4267 		strioc.ic_cmd = -1;	/* The unsupported ioctl */
4268 		strioc.ic_timout = 0;
4269 		strioc.ic_len = 0;
4270 		strioc.ic_dp = NULL;
4271 		(void) strdoioctl(stp, &strioc, flag, K_TO_K, crp, rvalp);
4272 		*rvalp = 0;
4273 		return (0);
4274 
4275 	case I_FLUSHBAND:
4276 	    {
4277 		struct bandinfo binfo;
4278 
4279 		error = strcopyin((void *)arg, &binfo, sizeof (binfo),
4280 		    copyflag);
4281 		if (error)
4282 			return (error);
4283 		if (stp->sd_flag & STRHUP)
4284 			return (ENXIO);
4285 		if (binfo.bi_flag & ~FLUSHRW)
4286 			return (EINVAL);
4287 		while (!(mp = allocb(2, BPRI_HI))) {
4288 			if (error = strwaitbuf(2, BPRI_HI))
4289 				return (error);
4290 		}
4291 		mp->b_datap->db_type = M_FLUSH;
4292 		*mp->b_wptr++ = binfo.bi_flag | FLUSHBAND;
4293 		*mp->b_wptr++ = binfo.bi_pri;
4294 		putnext(stp->sd_wrq, mp);
4295 		/*
4296 		 * Send down an unsupported ioctl and wait for the nack
4297 		 * in order to allow the M_FLUSH to propagate back
4298 		 * up to the stream head.
4299 		 * Replaces if (qready()) runqueues();
4300 		 */
4301 		strioc.ic_cmd = -1;	/* The unsupported ioctl */
4302 		strioc.ic_timout = 0;
4303 		strioc.ic_len = 0;
4304 		strioc.ic_dp = NULL;
4305 		(void) strdoioctl(stp, &strioc, flag, K_TO_K, crp, rvalp);
4306 		*rvalp = 0;
4307 		return (0);
4308 	    }
4309 
4310 	case I_SRDOPT:
4311 		/*
4312 		 * Set read options
4313 		 *
4314 		 * RNORM - default stream mode
4315 		 * RMSGN - message no discard
4316 		 * RMSGD - message discard
4317 		 * RPROTNORM - fail read with EBADMSG for M_[PC]PROTOs
4318 		 * RPROTDAT - convert M_[PC]PROTOs to M_DATAs
4319 		 * RPROTDIS - discard M_[PC]PROTOs and retain M_DATAs
4320 		 */
4321 		if (arg & ~(RMODEMASK | RPROTMASK))
4322 			return (EINVAL);
4323 
4324 		if ((arg & (RMSGD|RMSGN)) == (RMSGD|RMSGN))
4325 			return (EINVAL);
4326 
4327 		mutex_enter(&stp->sd_lock);
4328 		switch (arg & RMODEMASK) {
4329 		case RNORM:
4330 			stp->sd_read_opt &= ~(RD_MSGDIS | RD_MSGNODIS);
4331 			break;
4332 		case RMSGD:
4333 			stp->sd_read_opt = (stp->sd_read_opt & ~RD_MSGNODIS) |
4334 			    RD_MSGDIS;
4335 			break;
4336 		case RMSGN:
4337 			stp->sd_read_opt = (stp->sd_read_opt & ~RD_MSGDIS) |
4338 			    RD_MSGNODIS;
4339 			break;
4340 		}
4341 
4342 		switch (arg & RPROTMASK) {
4343 		case RPROTNORM:
4344 			stp->sd_read_opt &= ~(RD_PROTDAT | RD_PROTDIS);
4345 			break;
4346 
4347 		case RPROTDAT:
4348 			stp->sd_read_opt = ((stp->sd_read_opt & ~RD_PROTDIS) |
4349 			    RD_PROTDAT);
4350 			break;
4351 
4352 		case RPROTDIS:
4353 			stp->sd_read_opt = ((stp->sd_read_opt & ~RD_PROTDAT) |
4354 			    RD_PROTDIS);
4355 			break;
4356 		}
4357 		mutex_exit(&stp->sd_lock);
4358 		return (0);
4359 
4360 	case I_GRDOPT:
4361 		/*
4362 		 * Get read option and return the value
4363 		 * to spot pointed to by arg
4364 		 */
4365 	    {
4366 		int rdopt;
4367 
4368 		rdopt = ((stp->sd_read_opt & RD_MSGDIS) ? RMSGD :
4369 		    ((stp->sd_read_opt & RD_MSGNODIS) ? RMSGN : RNORM));
4370 		rdopt |= ((stp->sd_read_opt & RD_PROTDAT) ? RPROTDAT :
4371 		    ((stp->sd_read_opt & RD_PROTDIS) ? RPROTDIS : RPROTNORM));
4372 
4373 		return (strcopyout(&rdopt, (void *)arg, sizeof (int),
4374 		    copyflag));
4375 	    }
4376 
4377 	case I_SERROPT:
4378 		/*
4379 		 * Set error options
4380 		 *
4381 		 * RERRNORM - persistent read errors
4382 		 * RERRNONPERSIST - non-persistent read errors
4383 		 * WERRNORM - persistent write errors
4384 		 * WERRNONPERSIST - non-persistent write errors
4385 		 */
4386 		if (arg & ~(RERRMASK | WERRMASK))
4387 			return (EINVAL);
4388 
4389 		mutex_enter(&stp->sd_lock);
4390 		switch (arg & RERRMASK) {
4391 		case RERRNORM:
4392 			stp->sd_flag &= ~STRDERRNONPERSIST;
4393 			break;
4394 		case RERRNONPERSIST:
4395 			stp->sd_flag |= STRDERRNONPERSIST;
4396 			break;
4397 		}
4398 		switch (arg & WERRMASK) {
4399 		case WERRNORM:
4400 			stp->sd_flag &= ~STWRERRNONPERSIST;
4401 			break;
4402 		case WERRNONPERSIST:
4403 			stp->sd_flag |= STWRERRNONPERSIST;
4404 			break;
4405 		}
4406 		mutex_exit(&stp->sd_lock);
4407 		return (0);
4408 
4409 	case I_GERROPT:
4410 		/*
4411 		 * Get error option and return the value
4412 		 * to spot pointed to by arg
4413 		 */
4414 	    {
4415 		int erropt = 0;
4416 
4417 		erropt |= (stp->sd_flag & STRDERRNONPERSIST) ? RERRNONPERSIST :
4418 			RERRNORM;
4419 		erropt |= (stp->sd_flag & STWRERRNONPERSIST) ? WERRNONPERSIST :
4420 			WERRNORM;
4421 		return (strcopyout(&erropt, (void *)arg, sizeof (int),
4422 		    copyflag));
4423 	    }
4424 
4425 	case I_SETSIG:
4426 		/*
4427 		 * Register the calling proc to receive the SIGPOLL
4428 		 * signal based on the events given in arg.  If
4429 		 * arg is zero, remove the proc from register list.
4430 		 */
4431 	    {
4432 		strsig_t *ssp, *pssp;
4433 		struct pid *pidp;
4434 
4435 		pssp = NULL;
4436 		pidp = curproc->p_pidp;
4437 		/*
4438 		 * Hold sd_lock to prevent traversal of sd_siglist while
4439 		 * it is modified.
4440 		 */
4441 		mutex_enter(&stp->sd_lock);
4442 		for (ssp = stp->sd_siglist; ssp && (ssp->ss_pidp != pidp);
4443 			pssp = ssp, ssp = ssp->ss_next)
4444 			;
4445 
4446 		if (arg) {
4447 			if (arg & ~(S_INPUT|S_HIPRI|S_MSG|S_HANGUP|S_ERROR|
4448 			    S_RDNORM|S_WRNORM|S_RDBAND|S_WRBAND|S_BANDURG)) {
4449 				mutex_exit(&stp->sd_lock);
4450 				return (EINVAL);
4451 			}
4452 			if ((arg & S_BANDURG) && !(arg & S_RDBAND)) {
4453 				mutex_exit(&stp->sd_lock);
4454 				return (EINVAL);
4455 			}
4456 
4457 			/*
4458 			 * If proc not already registered, add it
4459 			 * to list.
4460 			 */
4461 			if (!ssp) {
4462 				ssp = kmem_alloc(sizeof (strsig_t), KM_SLEEP);
4463 				ssp->ss_pidp = pidp;
4464 				ssp->ss_pid = pidp->pid_id;
4465 				ssp->ss_next = NULL;
4466 				if (pssp)
4467 					pssp->ss_next = ssp;
4468 				else
4469 					stp->sd_siglist = ssp;
4470 				mutex_enter(&pidlock);
4471 				PID_HOLD(pidp);
4472 				mutex_exit(&pidlock);
4473 			}
4474 
4475 			/*
4476 			 * Set events.
4477 			 */
4478 			ssp->ss_events = (int)arg;
4479 		} else {
4480 			/*
4481 			 * Remove proc from register list.
4482 			 */
4483 			if (ssp) {
4484 				mutex_enter(&pidlock);
4485 				PID_RELE(pidp);
4486 				mutex_exit(&pidlock);
4487 				if (pssp)
4488 					pssp->ss_next = ssp->ss_next;
4489 				else
4490 					stp->sd_siglist = ssp->ss_next;
4491 				kmem_free(ssp, sizeof (strsig_t));
4492 			} else {
4493 				mutex_exit(&stp->sd_lock);
4494 				return (EINVAL);
4495 			}
4496 		}
4497 
4498 		/*
4499 		 * Recalculate OR of sig events.
4500 		 */
4501 		stp->sd_sigflags = 0;
4502 		for (ssp = stp->sd_siglist; ssp; ssp = ssp->ss_next)
4503 			stp->sd_sigflags |= ssp->ss_events;
4504 		mutex_exit(&stp->sd_lock);
4505 		return (0);
4506 	    }
4507 
4508 	case I_GETSIG:
4509 		/*
4510 		 * Return (in arg) the current registration of events
4511 		 * for which the calling proc is to be signaled.
4512 		 */
4513 	    {
4514 		struct strsig *ssp;
4515 		struct pid  *pidp;
4516 
4517 		pidp = curproc->p_pidp;
4518 		mutex_enter(&stp->sd_lock);
4519 		for (ssp = stp->sd_siglist; ssp; ssp = ssp->ss_next)
4520 			if (ssp->ss_pidp == pidp) {
4521 				error = strcopyout(&ssp->ss_events, (void *)arg,
4522 				    sizeof (int), copyflag);
4523 				mutex_exit(&stp->sd_lock);
4524 				return (error);
4525 			}
4526 		mutex_exit(&stp->sd_lock);
4527 		return (EINVAL);
4528 	    }
4529 
4530 	case I_ESETSIG:
4531 		/*
4532 		 * Register the ss_pid to receive the SIGPOLL
4533 		 * signal based on the events is ss_events arg.  If
4534 		 * ss_events is zero, remove the proc from register list.
4535 		 */
4536 	{
4537 		struct strsig *ssp, *pssp;
4538 		struct proc *proc;
4539 		struct pid  *pidp;
4540 		pid_t pid;
4541 		struct strsigset ss;
4542 
4543 		error = strcopyin((void *)arg, &ss, sizeof (ss), copyflag);
4544 		if (error)
4545 			return (error);
4546 
4547 		pid = ss.ss_pid;
4548 
4549 		if (ss.ss_events != 0) {
4550 			/*
4551 			 * Permissions check by sending signal 0.
4552 			 * Note that when kill fails it does a set_errno
4553 			 * causing the system call to fail.
4554 			 */
4555 			error = kill(pid, 0);
4556 			if (error) {
4557 				return (error);
4558 			}
4559 		}
4560 		mutex_enter(&pidlock);
4561 		if (pid == 0)
4562 			proc = curproc;
4563 		else if (pid < 0)
4564 			proc = pgfind(-pid);
4565 		else
4566 			proc = prfind(pid);
4567 		if (proc == NULL) {
4568 			mutex_exit(&pidlock);
4569 			return (ESRCH);
4570 		}
4571 		if (pid < 0)
4572 			pidp = proc->p_pgidp;
4573 		else
4574 			pidp = proc->p_pidp;
4575 		ASSERT(pidp);
4576 		/*
4577 		 * Get a hold on the pid structure while referencing it.
4578 		 * There is a separate PID_HOLD should it be inserted
4579 		 * in the list below.
4580 		 */
4581 		PID_HOLD(pidp);
4582 		mutex_exit(&pidlock);
4583 
4584 		pssp = NULL;
4585 		/*
4586 		 * Hold sd_lock to prevent traversal of sd_siglist while
4587 		 * it is modified.
4588 		 */
4589 		mutex_enter(&stp->sd_lock);
4590 		for (ssp = stp->sd_siglist; ssp && (ssp->ss_pid != pid);
4591 				pssp = ssp, ssp = ssp->ss_next)
4592 			;
4593 
4594 		if (ss.ss_events) {
4595 			if (ss.ss_events &
4596 			    ~(S_INPUT|S_HIPRI|S_MSG|S_HANGUP|S_ERROR|
4597 			    S_RDNORM|S_WRNORM|S_RDBAND|S_WRBAND|S_BANDURG)) {
4598 				mutex_exit(&stp->sd_lock);
4599 				mutex_enter(&pidlock);
4600 				PID_RELE(pidp);
4601 				mutex_exit(&pidlock);
4602 				return (EINVAL);
4603 			}
4604 			if ((ss.ss_events & S_BANDURG) &&
4605 			    !(ss.ss_events & S_RDBAND)) {
4606 				mutex_exit(&stp->sd_lock);
4607 				mutex_enter(&pidlock);
4608 				PID_RELE(pidp);
4609 				mutex_exit(&pidlock);
4610 				return (EINVAL);
4611 			}
4612 
4613 			/*
4614 			 * If proc not already registered, add it
4615 			 * to list.
4616 			 */
4617 			if (!ssp) {
4618 				ssp = kmem_alloc(sizeof (strsig_t), KM_SLEEP);
4619 				ssp->ss_pidp = pidp;
4620 				ssp->ss_pid = pid;
4621 				ssp->ss_next = NULL;
4622 				if (pssp)
4623 					pssp->ss_next = ssp;
4624 				else
4625 					stp->sd_siglist = ssp;
4626 				mutex_enter(&pidlock);
4627 				PID_HOLD(pidp);
4628 				mutex_exit(&pidlock);
4629 			}
4630 
4631 			/*
4632 			 * Set events.
4633 			 */
4634 			ssp->ss_events = ss.ss_events;
4635 		} else {
4636 			/*
4637 			 * Remove proc from register list.
4638 			 */
4639 			if (ssp) {
4640 				mutex_enter(&pidlock);
4641 				PID_RELE(pidp);
4642 				mutex_exit(&pidlock);
4643 				if (pssp)
4644 					pssp->ss_next = ssp->ss_next;
4645 				else
4646 					stp->sd_siglist = ssp->ss_next;
4647 				kmem_free(ssp, sizeof (strsig_t));
4648 			} else {
4649 				mutex_exit(&stp->sd_lock);
4650 				mutex_enter(&pidlock);
4651 				PID_RELE(pidp);
4652 				mutex_exit(&pidlock);
4653 				return (EINVAL);
4654 			}
4655 		}
4656 
4657 		/*
4658 		 * Recalculate OR of sig events.
4659 		 */
4660 		stp->sd_sigflags = 0;
4661 		for (ssp = stp->sd_siglist; ssp; ssp = ssp->ss_next)
4662 			stp->sd_sigflags |= ssp->ss_events;
4663 		mutex_exit(&stp->sd_lock);
4664 		mutex_enter(&pidlock);
4665 		PID_RELE(pidp);
4666 		mutex_exit(&pidlock);
4667 		return (0);
4668 	    }
4669 
4670 	case I_EGETSIG:
4671 		/*
4672 		 * Return (in arg) the current registration of events
4673 		 * for which the calling proc is to be signaled.
4674 		 */
4675 	    {
4676 		struct strsig *ssp;
4677 		struct proc *proc;
4678 		pid_t pid;
4679 		struct pid  *pidp;
4680 		struct strsigset ss;
4681 
4682 		error = strcopyin((void *)arg, &ss, sizeof (ss), copyflag);
4683 		if (error)
4684 			return (error);
4685 
4686 		pid = ss.ss_pid;
4687 		mutex_enter(&pidlock);
4688 		if (pid == 0)
4689 			proc = curproc;
4690 		else if (pid < 0)
4691 			proc = pgfind(-pid);
4692 		else
4693 			proc = prfind(pid);
4694 		if (proc == NULL) {
4695 			mutex_exit(&pidlock);
4696 			return (ESRCH);
4697 		}
4698 		if (pid < 0)
4699 			pidp = proc->p_pgidp;
4700 		else
4701 			pidp = proc->p_pidp;
4702 
4703 		/* Prevent the pidp from being reassigned */
4704 		PID_HOLD(pidp);
4705 		mutex_exit(&pidlock);
4706 
4707 		mutex_enter(&stp->sd_lock);
4708 		for (ssp = stp->sd_siglist; ssp; ssp = ssp->ss_next)
4709 			if (ssp->ss_pid == pid) {
4710 				ss.ss_pid = ssp->ss_pid;
4711 				ss.ss_events = ssp->ss_events;
4712 				error = strcopyout(&ss, (void *)arg,
4713 				    sizeof (struct strsigset), copyflag);
4714 				mutex_exit(&stp->sd_lock);
4715 				mutex_enter(&pidlock);
4716 				PID_RELE(pidp);
4717 				mutex_exit(&pidlock);
4718 				return (error);
4719 			}
4720 		mutex_exit(&stp->sd_lock);
4721 		mutex_enter(&pidlock);
4722 		PID_RELE(pidp);
4723 		mutex_exit(&pidlock);
4724 		return (EINVAL);
4725 	    }
4726 
4727 	case I_PEEK:
4728 	    {
4729 		STRUCT_DECL(strpeek, strpeek);
4730 		size_t n;
4731 		mblk_t *fmp, *tmp_mp = NULL;
4732 
4733 		STRUCT_INIT(strpeek, flag);
4734 
4735 		error = strcopyin((void *)arg, STRUCT_BUF(strpeek),
4736 		    STRUCT_SIZE(strpeek), copyflag);
4737 		if (error)
4738 			return (error);
4739 
4740 		mutex_enter(QLOCK(rdq));
4741 		/*
4742 		 * Skip the invalid messages
4743 		 */
4744 		for (mp = rdq->q_first; mp != NULL; mp = mp->b_next)
4745 			if (mp->b_datap->db_type != M_SIG)
4746 				break;
4747 
4748 		/*
4749 		 * If user has requested to peek at a high priority message
4750 		 * and first message is not, return 0
4751 		 */
4752 		if (mp != NULL) {
4753 			if ((STRUCT_FGET(strpeek, flags) & RS_HIPRI) &&
4754 			    queclass(mp) == QNORM) {
4755 				*rvalp = 0;
4756 				mutex_exit(QLOCK(rdq));
4757 				return (0);
4758 			}
4759 		} else if (stp->sd_struiordq == NULL ||
4760 		    (STRUCT_FGET(strpeek, flags) & RS_HIPRI)) {
4761 			/*
4762 			 * No mblks to look at at the streamhead and
4763 			 * 1). This isn't a synch stream or
4764 			 * 2). This is a synch stream but caller wants high
4765 			 *	priority messages which is not supported by
4766 			 *	the synch stream. (it only supports QNORM)
4767 			 */
4768 			*rvalp = 0;
4769 			mutex_exit(QLOCK(rdq));
4770 			return (0);
4771 		}
4772 
4773 		fmp = mp;
4774 
4775 		if (mp && mp->b_datap->db_type == M_PASSFP) {
4776 			mutex_exit(QLOCK(rdq));
4777 			return (EBADMSG);
4778 		}
4779 
4780 		ASSERT(mp == NULL || mp->b_datap->db_type == M_PCPROTO ||
4781 		    mp->b_datap->db_type == M_PROTO ||
4782 		    mp->b_datap->db_type == M_DATA);
4783 
4784 		if (mp && mp->b_datap->db_type == M_PCPROTO) {
4785 			STRUCT_FSET(strpeek, flags, RS_HIPRI);
4786 		} else {
4787 			STRUCT_FSET(strpeek, flags, 0);
4788 		}
4789 
4790 
4791 		if (mp && ((tmp_mp = dupmsg(mp)) == NULL)) {
4792 			mutex_exit(QLOCK(rdq));
4793 			return (ENOSR);
4794 		}
4795 		mutex_exit(QLOCK(rdq));
4796 
4797 		/*
4798 		 * set mp = tmp_mp, so that I_PEEK processing can continue.
4799 		 * tmp_mp is used to free the dup'd message.
4800 		 */
4801 		mp = tmp_mp;
4802 
4803 		uio.uio_fmode = 0;
4804 		uio.uio_extflg = UIO_COPY_CACHED;
4805 		uio.uio_segflg = (copyflag == U_TO_K) ? UIO_USERSPACE :
4806 		    UIO_SYSSPACE;
4807 		uio.uio_limit = 0;
4808 		/*
4809 		 * First process PROTO blocks, if any.
4810 		 * If user doesn't want to get ctl info by setting maxlen <= 0,
4811 		 * then set len to -1/0 and skip control blocks part.
4812 		 */
4813 		if (STRUCT_FGET(strpeek, ctlbuf.maxlen) < 0)
4814 			STRUCT_FSET(strpeek, ctlbuf.len, -1);
4815 		else if (STRUCT_FGET(strpeek, ctlbuf.maxlen) == 0)
4816 			STRUCT_FSET(strpeek, ctlbuf.len, 0);
4817 		else {
4818 			int	ctl_part = 0;
4819 
4820 			iov.iov_base = STRUCT_FGETP(strpeek, ctlbuf.buf);
4821 			iov.iov_len = STRUCT_FGET(strpeek, ctlbuf.maxlen);
4822 			uio.uio_iov = &iov;
4823 			uio.uio_resid = iov.iov_len;
4824 			uio.uio_loffset = 0;
4825 			uio.uio_iovcnt = 1;
4826 			while (mp && mp->b_datap->db_type != M_DATA &&
4827 			    uio.uio_resid >= 0) {
4828 				ASSERT(STRUCT_FGET(strpeek, flags) == 0 ?
4829 				    mp->b_datap->db_type == M_PROTO :
4830 				    mp->b_datap->db_type == M_PCPROTO);
4831 
4832 				if ((n = MIN(uio.uio_resid,
4833 				    mp->b_wptr - mp->b_rptr)) != 0 &&
4834 				    (error = uiomove((char *)mp->b_rptr, n,
4835 				    UIO_READ, &uio)) != 0) {
4836 					freemsg(tmp_mp);
4837 					return (error);
4838 				}
4839 				ctl_part = 1;
4840 				mp = mp->b_cont;
4841 			}
4842 			/* No ctl message */
4843 			if (ctl_part == 0)
4844 				STRUCT_FSET(strpeek, ctlbuf.len, -1);
4845 			else
4846 				STRUCT_FSET(strpeek, ctlbuf.len,
4847 				    STRUCT_FGET(strpeek, ctlbuf.maxlen) -
4848 				    uio.uio_resid);
4849 		}
4850 
4851 		/*
4852 		 * Now process DATA blocks, if any.
4853 		 * If user doesn't want to get data info by setting maxlen <= 0,
4854 		 * then set len to -1/0 and skip data blocks part.
4855 		 */
4856 		if (STRUCT_FGET(strpeek, databuf.maxlen) < 0)
4857 			STRUCT_FSET(strpeek, databuf.len, -1);
4858 		else if (STRUCT_FGET(strpeek, databuf.maxlen) == 0)
4859 			STRUCT_FSET(strpeek, databuf.len, 0);
4860 		else {
4861 			int	data_part = 0;
4862 
4863 			iov.iov_base = STRUCT_FGETP(strpeek, databuf.buf);
4864 			iov.iov_len = STRUCT_FGET(strpeek, databuf.maxlen);
4865 			uio.uio_iov = &iov;
4866 			uio.uio_resid = iov.iov_len;
4867 			uio.uio_loffset = 0;
4868 			uio.uio_iovcnt = 1;
4869 			while (mp && uio.uio_resid) {
4870 				if (mp->b_datap->db_type == M_DATA) {
4871 					if ((n = MIN(uio.uio_resid,
4872 					    mp->b_wptr - mp->b_rptr)) != 0 &&
4873 					    (error = uiomove((char *)mp->b_rptr,
4874 						n, UIO_READ, &uio)) != 0) {
4875 						freemsg(tmp_mp);
4876 						return (error);
4877 					}
4878 					data_part = 1;
4879 				}
4880 				ASSERT(data_part == 0 ||
4881 				    mp->b_datap->db_type == M_DATA);
4882 				mp = mp->b_cont;
4883 			}
4884 			/* No data message */
4885 			if (data_part == 0)
4886 				STRUCT_FSET(strpeek, databuf.len, -1);
4887 			else
4888 				STRUCT_FSET(strpeek, databuf.len,
4889 				    STRUCT_FGET(strpeek, databuf.maxlen) -
4890 				    uio.uio_resid);
4891 		}
4892 		freemsg(tmp_mp);
4893 
4894 		/*
4895 		 * It is a synch stream and user wants to get
4896 		 * data (maxlen > 0).
4897 		 * uio setup is done by the codes that process DATA
4898 		 * blocks above.
4899 		 */
4900 		if ((fmp == NULL) && STRUCT_FGET(strpeek, databuf.maxlen) > 0) {
4901 			infod_t infod;
4902 
4903 			infod.d_cmd = INFOD_COPYOUT;
4904 			infod.d_res = 0;
4905 			infod.d_uiop = &uio;
4906 			error = infonext(rdq, &infod);
4907 			if (error == EINVAL || error == EBUSY)
4908 				error = 0;
4909 			if (error)
4910 				return (error);
4911 			STRUCT_FSET(strpeek, databuf.len, STRUCT_FGET(strpeek,
4912 			    databuf.maxlen) - uio.uio_resid);
4913 			if (STRUCT_FGET(strpeek, databuf.len) == 0) {
4914 				/*
4915 				 * No data found by the infonext().
4916 				 */
4917 				STRUCT_FSET(strpeek, databuf.len, -1);
4918 			}
4919 		}
4920 		error = strcopyout(STRUCT_BUF(strpeek), (void *)arg,
4921 		    STRUCT_SIZE(strpeek), copyflag);
4922 		if (error) {
4923 			return (error);
4924 		}
4925 		/*
4926 		 * If there is no message retrieved, set return code to 0
4927 		 * otherwise, set it to 1.
4928 		 */
4929 		if (STRUCT_FGET(strpeek, ctlbuf.len) == -1 &&
4930 		    STRUCT_FGET(strpeek, databuf.len) == -1)
4931 			*rvalp = 0;
4932 		else
4933 			*rvalp = 1;
4934 		return (0);
4935 	    }
4936 
4937 	case I_FDINSERT:
4938 	    {
4939 		STRUCT_DECL(strfdinsert, strfdinsert);
4940 		struct file *resftp;
4941 		struct stdata *resstp;
4942 		t_uscalar_t	ival;
4943 		ssize_t msgsize;
4944 		struct strbuf mctl;
4945 
4946 		STRUCT_INIT(strfdinsert, flag);
4947 		if (stp->sd_flag & STRHUP)
4948 			return (ENXIO);
4949 		/*
4950 		 * STRDERR, STWRERR and STPLEX tested above.
4951 		 */
4952 		error = strcopyin((void *)arg, STRUCT_BUF(strfdinsert),
4953 		    STRUCT_SIZE(strfdinsert), copyflag);
4954 		if (error)
4955 			return (error);
4956 
4957 		if (STRUCT_FGET(strfdinsert, offset) < 0 ||
4958 		    (STRUCT_FGET(strfdinsert, offset) %
4959 		    sizeof (t_uscalar_t)) != 0)
4960 			return (EINVAL);
4961 		if ((resftp = getf(STRUCT_FGET(strfdinsert, fildes))) != NULL) {
4962 			if ((resstp = resftp->f_vnode->v_stream) == NULL) {
4963 				releasef(STRUCT_FGET(strfdinsert, fildes));
4964 				return (EINVAL);
4965 			}
4966 		} else
4967 			return (EINVAL);
4968 
4969 		mutex_enter(&resstp->sd_lock);
4970 		if (resstp->sd_flag & (STRDERR|STWRERR|STRHUP|STPLEX)) {
4971 			error = strgeterr(resstp,
4972 					STRDERR|STWRERR|STRHUP|STPLEX, 0);
4973 			if (error != 0) {
4974 				mutex_exit(&resstp->sd_lock);
4975 				releasef(STRUCT_FGET(strfdinsert, fildes));
4976 				return (error);
4977 			}
4978 		}
4979 		mutex_exit(&resstp->sd_lock);
4980 
4981 #ifdef	_ILP32
4982 		{
4983 			queue_t	*q;
4984 			queue_t	*mate = NULL;
4985 
4986 			/* get read queue of stream terminus */
4987 			claimstr(resstp->sd_wrq);
4988 			for (q = resstp->sd_wrq->q_next; q->q_next != NULL;
4989 			    q = q->q_next)
4990 				if (!STRMATED(resstp) && STREAM(q) != resstp &&
4991 				    mate == NULL) {
4992 					ASSERT(q->q_qinfo->qi_srvp);
4993 					ASSERT(_OTHERQ(q)->q_qinfo->qi_srvp);
4994 					claimstr(q);
4995 					mate = q;
4996 				}
4997 			q = _RD(q);
4998 			if (mate)
4999 				releasestr(mate);
5000 			releasestr(resstp->sd_wrq);
5001 			ival = (t_uscalar_t)q;
5002 		}
5003 #else
5004 		ival = (t_uscalar_t)getminor(resftp->f_vnode->v_rdev);
5005 #endif	/* _ILP32 */
5006 
5007 		if (STRUCT_FGET(strfdinsert, ctlbuf.len) <
5008 		    STRUCT_FGET(strfdinsert, offset) + sizeof (t_uscalar_t)) {
5009 			releasef(STRUCT_FGET(strfdinsert, fildes));
5010 			return (EINVAL);
5011 		}
5012 
5013 		/*
5014 		 * Check for legal flag value.
5015 		 */
5016 		if (STRUCT_FGET(strfdinsert, flags) & ~RS_HIPRI) {
5017 			releasef(STRUCT_FGET(strfdinsert, fildes));
5018 			return (EINVAL);
5019 		}
5020 
5021 		/* get these values from those cached in the stream head */
5022 		mutex_enter(QLOCK(stp->sd_wrq));
5023 		rmin = stp->sd_qn_minpsz;
5024 		rmax = stp->sd_qn_maxpsz;
5025 		mutex_exit(QLOCK(stp->sd_wrq));
5026 
5027 		/*
5028 		 * Make sure ctl and data sizes together fall within
5029 		 * the limits of the max and min receive packet sizes
5030 		 * and do not exceed system limit.  A negative data
5031 		 * length means that no data part is to be sent.
5032 		 */
5033 		ASSERT((rmax >= 0) || (rmax == INFPSZ));
5034 		if (rmax == 0) {
5035 			releasef(STRUCT_FGET(strfdinsert, fildes));
5036 			return (ERANGE);
5037 		}
5038 		if ((msgsize = STRUCT_FGET(strfdinsert, databuf.len)) < 0)
5039 			msgsize = 0;
5040 		if ((msgsize < rmin) ||
5041 		    ((msgsize > rmax) && (rmax != INFPSZ)) ||
5042 		    (STRUCT_FGET(strfdinsert, ctlbuf.len) > strctlsz)) {
5043 			releasef(STRUCT_FGET(strfdinsert, fildes));
5044 			return (ERANGE);
5045 		}
5046 
5047 		mutex_enter(&stp->sd_lock);
5048 		while (!(STRUCT_FGET(strfdinsert, flags) & RS_HIPRI) &&
5049 		    !canputnext(stp->sd_wrq)) {
5050 			if ((error = strwaitq(stp, WRITEWAIT, (ssize_t)0,
5051 			    flag, -1, &done)) != 0 || done) {
5052 				mutex_exit(&stp->sd_lock);
5053 				releasef(STRUCT_FGET(strfdinsert, fildes));
5054 				return (error);
5055 			}
5056 			if (stp->sd_sidp != NULL &&
5057 			    stp->sd_vnode->v_type != VFIFO) {
5058 				mutex_exit(&stp->sd_lock);
5059 				if (error = straccess(stp, access)) {
5060 					releasef(
5061 					    STRUCT_FGET(strfdinsert, fildes));
5062 					return (error);
5063 				}
5064 				mutex_enter(&stp->sd_lock);
5065 			}
5066 		}
5067 		mutex_exit(&stp->sd_lock);
5068 
5069 		/*
5070 		 * Copy strfdinsert.ctlbuf into native form of
5071 		 * ctlbuf to pass down into strmakemsg().
5072 		 */
5073 		mctl.maxlen = STRUCT_FGET(strfdinsert, ctlbuf.maxlen);
5074 		mctl.len = STRUCT_FGET(strfdinsert, ctlbuf.len);
5075 		mctl.buf = STRUCT_FGETP(strfdinsert, ctlbuf.buf);
5076 
5077 		iov.iov_base = STRUCT_FGETP(strfdinsert, databuf.buf);
5078 		iov.iov_len = STRUCT_FGET(strfdinsert, databuf.len);
5079 		uio.uio_iov = &iov;
5080 		uio.uio_iovcnt = 1;
5081 		uio.uio_loffset = 0;
5082 		uio.uio_segflg = (copyflag == U_TO_K) ? UIO_USERSPACE :
5083 		    UIO_SYSSPACE;
5084 		uio.uio_fmode = 0;
5085 		uio.uio_extflg = UIO_COPY_CACHED;
5086 		uio.uio_resid = iov.iov_len;
5087 		if ((error = strmakemsg(&mctl,
5088 		    &msgsize, &uio, stp,
5089 		    STRUCT_FGET(strfdinsert, flags), &mp)) != 0 || !mp) {
5090 			STRUCT_FSET(strfdinsert, databuf.len, msgsize);
5091 			releasef(STRUCT_FGET(strfdinsert, fildes));
5092 			return (error);
5093 		}
5094 
5095 		STRUCT_FSET(strfdinsert, databuf.len, msgsize);
5096 
5097 		/*
5098 		 * Place the possibly reencoded queue pointer 'offset' bytes
5099 		 * from the start of the control portion of the message.
5100 		 */
5101 		*((t_uscalar_t *)(mp->b_rptr +
5102 		    STRUCT_FGET(strfdinsert, offset))) = ival;
5103 
5104 		/*
5105 		 * Put message downstream.
5106 		 */
5107 		stream_willservice(stp);
5108 		putnext(stp->sd_wrq, mp);
5109 		stream_runservice(stp);
5110 		releasef(STRUCT_FGET(strfdinsert, fildes));
5111 		return (error);
5112 	    }
5113 
5114 	case I_SENDFD:
5115 	    {
5116 		struct file *fp;
5117 
5118 		if ((fp = getf((int)arg)) == NULL)
5119 			return (EBADF);
5120 		error = do_sendfp(stp, fp, crp);
5121 #ifdef C2_AUDIT
5122 		if (audit_active) {
5123 			audit_fdsend((int)arg, fp, error);
5124 		}
5125 #endif
5126 		releasef((int)arg);
5127 		return (error);
5128 	    }
5129 
5130 	case I_RECVFD:
5131 	case I_E_RECVFD:
5132 	    {
5133 		struct k_strrecvfd *srf;
5134 		int i, fd;
5135 
5136 		mutex_enter(&stp->sd_lock);
5137 		while (!(mp = getq(rdq))) {
5138 			if (stp->sd_flag & (STRHUP|STREOF)) {
5139 				mutex_exit(&stp->sd_lock);
5140 				return (ENXIO);
5141 			}
5142 			if ((error = strwaitq(stp, GETWAIT, (ssize_t)0,
5143 			    flag, -1, &done)) != 0 || done) {
5144 				mutex_exit(&stp->sd_lock);
5145 				return (error);
5146 			}
5147 			if (stp->sd_sidp != NULL &&
5148 			    stp->sd_vnode->v_type != VFIFO) {
5149 				mutex_exit(&stp->sd_lock);
5150 				if (error = straccess(stp, access))
5151 					return (error);
5152 				mutex_enter(&stp->sd_lock);
5153 			}
5154 		}
5155 		if (mp->b_datap->db_type != M_PASSFP) {
5156 			putback(stp, rdq, mp, mp->b_band);
5157 			mutex_exit(&stp->sd_lock);
5158 			return (EBADMSG);
5159 		}
5160 		mutex_exit(&stp->sd_lock);
5161 
5162 		srf = (struct k_strrecvfd *)mp->b_rptr;
5163 		if ((fd = ufalloc(0)) == -1) {
5164 			mutex_enter(&stp->sd_lock);
5165 			putback(stp, rdq, mp, mp->b_band);
5166 			mutex_exit(&stp->sd_lock);
5167 			return (EMFILE);
5168 		}
5169 		if (cmd == I_RECVFD) {
5170 			struct o_strrecvfd	ostrfd;
5171 
5172 			/* check to see if uid/gid values are too large. */
5173 
5174 			if (srf->uid > (o_uid_t)USHRT_MAX ||
5175 			    srf->gid > (o_gid_t)USHRT_MAX) {
5176 				mutex_enter(&stp->sd_lock);
5177 				putback(stp, rdq, mp, mp->b_band);
5178 				mutex_exit(&stp->sd_lock);
5179 				setf(fd, NULL);	/* release fd entry */
5180 				return (EOVERFLOW);
5181 			}
5182 
5183 			ostrfd.fd = fd;
5184 			ostrfd.uid = (o_uid_t)srf->uid;
5185 			ostrfd.gid = (o_gid_t)srf->gid;
5186 
5187 			/* Null the filler bits */
5188 			for (i = 0; i < 8; i++)
5189 				ostrfd.fill[i] = 0;
5190 
5191 			error = strcopyout(&ostrfd, (void *)arg,
5192 			    sizeof (struct o_strrecvfd), copyflag);
5193 		} else {		/* I_E_RECVFD */
5194 			struct strrecvfd	strfd;
5195 
5196 			strfd.fd = fd;
5197 			strfd.uid = srf->uid;
5198 			strfd.gid = srf->gid;
5199 
5200 			/* null the filler bits */
5201 			for (i = 0; i < 8; i++)
5202 				strfd.fill[i] = 0;
5203 
5204 			error = strcopyout(&strfd, (void *)arg,
5205 			    sizeof (struct strrecvfd), copyflag);
5206 		}
5207 
5208 		if (error) {
5209 			setf(fd, NULL);	/* release fd entry */
5210 			mutex_enter(&stp->sd_lock);
5211 			putback(stp, rdq, mp, mp->b_band);
5212 			mutex_exit(&stp->sd_lock);
5213 			return (error);
5214 		}
5215 #ifdef C2_AUDIT
5216 		if (audit_active) {
5217 			audit_fdrecv(fd, srf->fp);
5218 		}
5219 #endif
5220 
5221 		/*
5222 		 * Always increment f_count since the freemsg() below will
5223 		 * always call free_passfp() which performs a closef().
5224 		 */
5225 		mutex_enter(&srf->fp->f_tlock);
5226 		srf->fp->f_count++;
5227 		mutex_exit(&srf->fp->f_tlock);
5228 		setf(fd, srf->fp);
5229 		freemsg(mp);
5230 		return (0);
5231 	    }
5232 
5233 	case I_SWROPT:
5234 		/*
5235 		 * Set/clear the write options. arg is a bit
5236 		 * mask with any of the following bits set...
5237 		 * 	SNDZERO - send zero length message
5238 		 *	SNDPIPE - send sigpipe to process if
5239 		 *		sd_werror is set and process is
5240 		 *		doing a write or putmsg.
5241 		 * The new stream head write options should reflect
5242 		 * what is in arg.
5243 		 */
5244 		if (arg & ~(SNDZERO|SNDPIPE))
5245 			return (EINVAL);
5246 
5247 		mutex_enter(&stp->sd_lock);
5248 		stp->sd_wput_opt &= ~(SW_SIGPIPE|SW_SNDZERO);
5249 		if (arg & SNDZERO)
5250 			stp->sd_wput_opt |= SW_SNDZERO;
5251 		if (arg & SNDPIPE)
5252 			stp->sd_wput_opt |= SW_SIGPIPE;
5253 		mutex_exit(&stp->sd_lock);
5254 		return (0);
5255 
5256 	case I_GWROPT:
5257 	    {
5258 		int wropt = 0;
5259 
5260 		if (stp->sd_wput_opt & SW_SNDZERO)
5261 			wropt |= SNDZERO;
5262 		if (stp->sd_wput_opt & SW_SIGPIPE)
5263 			wropt |= SNDPIPE;
5264 		return (strcopyout(&wropt, (void *)arg, sizeof (wropt),
5265 		    copyflag));
5266 	    }
5267 
5268 	case I_LIST:
5269 		/*
5270 		 * Returns all the modules found on this stream,
5271 		 * upto the driver. If argument is NULL, return the
5272 		 * number of modules (including driver). If argument
5273 		 * is not NULL, copy the names into the structure
5274 		 * provided.
5275 		 */
5276 
5277 	    {
5278 		queue_t *q;
5279 		int num_modules, space_allocated;
5280 		STRUCT_DECL(str_list, strlist);
5281 		struct str_mlist *mlist_ptr;
5282 
5283 		if (arg == NULL) { /* Return number of modules plus driver */
5284 			q = stp->sd_wrq;
5285 			if (stp->sd_vnode->v_type == VFIFO) {
5286 				*rvalp = stp->sd_pushcnt;
5287 			} else {
5288 				*rvalp = stp->sd_pushcnt + 1;
5289 			}
5290 		} else {
5291 			STRUCT_INIT(strlist, flag);
5292 
5293 			error = strcopyin((void *)arg, STRUCT_BUF(strlist),
5294 			    STRUCT_SIZE(strlist), copyflag);
5295 			if (error)
5296 				return (error);
5297 
5298 			space_allocated = STRUCT_FGET(strlist, sl_nmods);
5299 			if ((space_allocated) <= 0)
5300 				return (EINVAL);
5301 			claimstr(stp->sd_wrq);
5302 			q = stp->sd_wrq;
5303 			num_modules = 0;
5304 			while (_SAMESTR(q) && (space_allocated != 0)) {
5305 				char *name =
5306 				    q->q_next->q_qinfo->qi_minfo->mi_idname;
5307 
5308 				mlist_ptr = STRUCT_FGETP(strlist, sl_modlist);
5309 
5310 				error = strcopyout(name, mlist_ptr,
5311 				    strlen(name) + 1, copyflag);
5312 
5313 				if (error) {
5314 					releasestr(stp->sd_wrq);
5315 					return (error);
5316 				}
5317 				q = q->q_next;
5318 				space_allocated--;
5319 				num_modules++;
5320 				mlist_ptr =
5321 				    (struct str_mlist *)((uintptr_t)mlist_ptr +
5322 				    sizeof (struct str_mlist));
5323 				STRUCT_FSETP(strlist, sl_modlist, mlist_ptr);
5324 			}
5325 			releasestr(stp->sd_wrq);
5326 			error = strcopyout(&num_modules, (void *)arg,
5327 			    sizeof (int), copyflag);
5328 		}
5329 		return (error);
5330 	    }
5331 
5332 	case I_CKBAND:
5333 	    {
5334 		queue_t *q;
5335 		qband_t *qbp;
5336 
5337 		if ((arg < 0) || (arg >= NBAND))
5338 			return (EINVAL);
5339 		q = _RD(stp->sd_wrq);
5340 		mutex_enter(QLOCK(q));
5341 		if (arg > (int)q->q_nband) {
5342 			*rvalp = 0;
5343 		} else {
5344 			if (arg == 0) {
5345 				if (q->q_first)
5346 					*rvalp = 1;
5347 				else
5348 					*rvalp = 0;
5349 			} else {
5350 				qbp = q->q_bandp;
5351 				while (--arg > 0)
5352 					qbp = qbp->qb_next;
5353 				if (qbp->qb_first)
5354 					*rvalp = 1;
5355 				else
5356 					*rvalp = 0;
5357 			}
5358 		}
5359 		mutex_exit(QLOCK(q));
5360 		return (0);
5361 	    }
5362 
5363 	case I_GETBAND:
5364 	    {
5365 		int intpri;
5366 		queue_t *q;
5367 
5368 		q = _RD(stp->sd_wrq);
5369 		mutex_enter(QLOCK(q));
5370 		mp = q->q_first;
5371 		if (!mp) {
5372 			mutex_exit(QLOCK(q));
5373 			return (ENODATA);
5374 		}
5375 		intpri = (int)mp->b_band;
5376 		error = strcopyout(&intpri, (void *)arg, sizeof (int),
5377 		    copyflag);
5378 		mutex_exit(QLOCK(q));
5379 		return (error);
5380 	    }
5381 
5382 	case I_ATMARK:
5383 	    {
5384 		queue_t *q;
5385 
5386 		if (arg & ~(ANYMARK|LASTMARK))
5387 			return (EINVAL);
5388 		q = _RD(stp->sd_wrq);
5389 		mutex_enter(&stp->sd_lock);
5390 		if ((stp->sd_flag & STRATMARK) && (arg == ANYMARK)) {
5391 			*rvalp = 1;
5392 		} else {
5393 			mutex_enter(QLOCK(q));
5394 			mp = q->q_first;
5395 
5396 			if (mp == NULL)
5397 				*rvalp = 0;
5398 			else if ((arg == ANYMARK) && (mp->b_flag & MSGMARK))
5399 				*rvalp = 1;
5400 			else if ((arg == LASTMARK) && (mp == stp->sd_mark))
5401 				*rvalp = 1;
5402 			else
5403 				*rvalp = 0;
5404 			mutex_exit(QLOCK(q));
5405 		}
5406 		mutex_exit(&stp->sd_lock);
5407 		return (0);
5408 	    }
5409 
5410 	case I_CANPUT:
5411 	    {
5412 		char band;
5413 
5414 		if ((arg < 0) || (arg >= NBAND))
5415 			return (EINVAL);
5416 		band = (char)arg;
5417 		*rvalp = bcanputnext(stp->sd_wrq, band);
5418 		return (0);
5419 	    }
5420 
5421 	case I_SETCLTIME:
5422 	    {
5423 		int closetime;
5424 
5425 		error = strcopyin((void *)arg, &closetime, sizeof (int),
5426 		    copyflag);
5427 		if (error)
5428 			return (error);
5429 		if (closetime < 0)
5430 			return (EINVAL);
5431 
5432 		stp->sd_closetime = closetime;
5433 		return (0);
5434 	    }
5435 
5436 	case I_GETCLTIME:
5437 	    {
5438 		int closetime;
5439 
5440 		closetime = stp->sd_closetime;
5441 		return (strcopyout(&closetime, (void *)arg, sizeof (int),
5442 		    copyflag));
5443 	    }
5444 
5445 	case TIOCGSID:
5446 	{
5447 		pid_t sid;
5448 
5449 		mutex_enter(&pidlock);
5450 		if (stp->sd_sidp == NULL) {
5451 			mutex_exit(&pidlock);
5452 			return (ENOTTY);
5453 		}
5454 		sid = stp->sd_sidp->pid_id;
5455 		mutex_exit(&pidlock);
5456 		return (strcopyout(&sid, (void *)arg, sizeof (pid_t),
5457 		    copyflag));
5458 	}
5459 
5460 	case TIOCSPGRP:
5461 	{
5462 		pid_t pgrp;
5463 		proc_t *q;
5464 		pid_t	sid, fg_pgid, bg_pgid;
5465 
5466 		if (error = strcopyin((void *)arg, &pgrp, sizeof (pid_t),
5467 		    copyflag))
5468 			return (error);
5469 		mutex_enter(&stp->sd_lock);
5470 		mutex_enter(&pidlock);
5471 		if (stp->sd_sidp != ttoproc(curthread)->p_sessp->s_sidp) {
5472 			mutex_exit(&pidlock);
5473 			mutex_exit(&stp->sd_lock);
5474 			return (ENOTTY);
5475 		}
5476 		if (pgrp == stp->sd_pgidp->pid_id) {
5477 			mutex_exit(&pidlock);
5478 			mutex_exit(&stp->sd_lock);
5479 			return (0);
5480 		}
5481 		if (pgrp <= 0 || pgrp >= maxpid) {
5482 			mutex_exit(&pidlock);
5483 			mutex_exit(&stp->sd_lock);
5484 			return (EINVAL);
5485 		}
5486 		if ((q = pgfind(pgrp)) == NULL ||
5487 		    q->p_sessp != ttoproc(curthread)->p_sessp) {
5488 			mutex_exit(&pidlock);
5489 			mutex_exit(&stp->sd_lock);
5490 			return (EPERM);
5491 		}
5492 		sid = stp->sd_sidp->pid_id;
5493 		fg_pgid = q->p_pgrp;
5494 		bg_pgid = stp->sd_pgidp->pid_id;
5495 		CL_SET_PROCESS_GROUP(curthread, sid, bg_pgid, fg_pgid);
5496 		PID_RELE(stp->sd_pgidp);
5497 		stp->sd_pgidp = q->p_pgidp;
5498 		PID_HOLD(stp->sd_pgidp);
5499 		mutex_exit(&pidlock);
5500 		mutex_exit(&stp->sd_lock);
5501 		return (0);
5502 	}
5503 
5504 	case TIOCGPGRP:
5505 	{
5506 		pid_t pgrp;
5507 
5508 		mutex_enter(&pidlock);
5509 		if (stp->sd_sidp == NULL) {
5510 			mutex_exit(&pidlock);
5511 			return (ENOTTY);
5512 		}
5513 		pgrp = stp->sd_pgidp->pid_id;
5514 		mutex_exit(&pidlock);
5515 		return (strcopyout(&pgrp, (void *)arg, sizeof (pid_t),
5516 		    copyflag));
5517 	}
5518 
5519 	case FIONBIO:
5520 	case FIOASYNC:
5521 		return (0);	/* handled by the upper layer */
5522 	}
5523 }
5524 
5525 /*
5526  * Custom free routine used for M_PASSFP messages.
5527  */
5528 static void
5529 free_passfp(struct k_strrecvfd *srf)
5530 {
5531 	(void) closef(srf->fp);
5532 	kmem_free(srf, sizeof (struct k_strrecvfd) + sizeof (frtn_t));
5533 }
5534 
5535 /* ARGSUSED */
5536 int
5537 do_sendfp(struct stdata *stp, struct file *fp, struct cred *cr)
5538 {
5539 	queue_t *qp, *nextqp;
5540 	struct k_strrecvfd *srf;
5541 	mblk_t *mp;
5542 	frtn_t *frtnp;
5543 	size_t bufsize;
5544 	queue_t	*mate = NULL;
5545 	syncq_t	*sq = NULL;
5546 	int retval = 0;
5547 
5548 	if (stp->sd_flag & STRHUP)
5549 		return (ENXIO);
5550 
5551 	claimstr(stp->sd_wrq);
5552 
5553 	/* Fastpath, we have a pipe, and we are already mated, use it. */
5554 	if (STRMATED(stp)) {
5555 		qp = _RD(stp->sd_mate->sd_wrq);
5556 		claimstr(qp);
5557 		mate = qp;
5558 	} else { /* Not already mated. */
5559 
5560 		/*
5561 		 * Walk the stream to the end of this one.
5562 		 * assumes that the claimstr() will prevent
5563 		 * plumbing between the stream head and the
5564 		 * driver from changing
5565 		 */
5566 		qp = stp->sd_wrq;
5567 
5568 		/*
5569 		 * Loop until we reach the end of this stream.
5570 		 * On completion, qp points to the write queue
5571 		 * at the end of the stream, or the read queue
5572 		 * at the stream head if this is a fifo.
5573 		 */
5574 		while (((qp = qp->q_next) != NULL) && _SAMESTR(qp))
5575 			;
5576 
5577 		/*
5578 		 * Just in case we get a q_next which is NULL, but
5579 		 * not at the end of the stream.  This is actually
5580 		 * broken, so we set an assert to catch it in
5581 		 * debug, and set an error and return if not debug.
5582 		 */
5583 		ASSERT(qp);
5584 		if (qp == NULL) {
5585 			releasestr(stp->sd_wrq);
5586 			return (EINVAL);
5587 		}
5588 
5589 		/*
5590 		 * Enter the syncq for the driver, so (hopefully)
5591 		 * the queue values will not change on us.
5592 		 * XXXX - This will only prevent the race IFF only
5593 		 *   the write side modifies the q_next member, and
5594 		 *   the put procedure is protected by at least
5595 		 *   MT_PERQ.
5596 		 */
5597 		if ((sq = qp->q_syncq) != NULL)
5598 			entersq(sq, SQ_PUT);
5599 
5600 		/* Now get the q_next value from this qp. */
5601 		nextqp = qp->q_next;
5602 
5603 		/*
5604 		 * If nextqp exists and the other stream is different
5605 		 * from this one claim the stream, set the mate, and
5606 		 * get the read queue at the stream head of the other
5607 		 * stream.  Assumes that nextqp was at least valid when
5608 		 * we got it.  Hopefully the entersq of the driver
5609 		 * will prevent it from changing on us.
5610 		 */
5611 		if ((nextqp != NULL) && (STREAM(nextqp) != stp)) {
5612 			ASSERT(qp->q_qinfo->qi_srvp);
5613 			ASSERT(_OTHERQ(qp)->q_qinfo->qi_srvp);
5614 			ASSERT(_OTHERQ(qp->q_next)->q_qinfo->qi_srvp);
5615 			claimstr(nextqp);
5616 
5617 			/* Make sure we still have a q_next */
5618 			if (nextqp != qp->q_next) {
5619 				releasestr(stp->sd_wrq);
5620 				releasestr(nextqp);
5621 				return (EINVAL);
5622 			}
5623 
5624 			qp = _RD(STREAM(nextqp)->sd_wrq);
5625 			mate = qp;
5626 		}
5627 		/* If we entered the synq above, leave it. */
5628 		if (sq != NULL)
5629 			leavesq(sq, SQ_PUT);
5630 	} /*  STRMATED(STP)  */
5631 
5632 	/* XXX prevents substitution of the ops vector */
5633 	if (qp->q_qinfo != &strdata && qp->q_qinfo != &fifo_strdata) {
5634 		retval = EINVAL;
5635 		goto out;
5636 	}
5637 
5638 	if (qp->q_flag & QFULL) {
5639 		retval = EAGAIN;
5640 		goto out;
5641 	}
5642 
5643 	/*
5644 	 * Since M_PASSFP messages include a file descriptor, we use
5645 	 * esballoc() and specify a custom free routine (free_passfp()) that
5646 	 * will close the descriptor as part of freeing the message.  For
5647 	 * convenience, we stash the frtn_t right after the data block.
5648 	 */
5649 	bufsize = sizeof (struct k_strrecvfd) + sizeof (frtn_t);
5650 	srf = kmem_alloc(bufsize, KM_NOSLEEP);
5651 	if (srf == NULL) {
5652 		retval = EAGAIN;
5653 		goto out;
5654 	}
5655 
5656 	frtnp = (frtn_t *)(srf + 1);
5657 	frtnp->free_arg = (caddr_t)srf;
5658 	frtnp->free_func = free_passfp;
5659 
5660 	mp = esballoc((uchar_t *)srf, bufsize, BPRI_MED, frtnp);
5661 	if (mp == NULL) {
5662 		kmem_free(srf, bufsize);
5663 		retval = EAGAIN;
5664 		goto out;
5665 	}
5666 	mp->b_wptr += sizeof (struct k_strrecvfd);
5667 	mp->b_datap->db_type = M_PASSFP;
5668 
5669 	srf->fp = fp;
5670 	srf->uid = crgetuid(curthread->t_cred);
5671 	srf->gid = crgetgid(curthread->t_cred);
5672 	mutex_enter(&fp->f_tlock);
5673 	fp->f_count++;
5674 	mutex_exit(&fp->f_tlock);
5675 
5676 	put(qp, mp);
5677 out:
5678 	releasestr(stp->sd_wrq);
5679 	if (mate)
5680 		releasestr(mate);
5681 	return (retval);
5682 }
5683 
5684 /*
5685  * Send an ioctl message downstream and wait for acknowledgement.
5686  * flags may be set to either U_TO_K or K_TO_K and a combination
5687  * of STR_NOERROR or STR_NOSIG
5688  * STR_NOSIG: Signals are essentially ignored or held and have
5689  *	no effect for the duration of the call.
5690  * STR_NOERROR: Ignores stream head read, write and hup errors.
5691  *	Additionally, if an existing ioctl times out, it is assumed
5692  *	lost and and this ioctl will continue as if the previous ioctl had
5693  *	finished.  ETIME may be returned if this ioctl times out (i.e.
5694  *	ic_timout is not INFTIM).  Non-stream head errors may be returned if
5695  *	the ioc_error indicates that the driver/module had problems,
5696  *	an EFAULT was found when accessing user data, a lack of
5697  * 	resources, etc.
5698  */
5699 int
5700 strdoioctl(
5701 	struct stdata *stp,
5702 	struct strioctl *strioc,
5703 	int fflags,		/* file flags with model info */
5704 	int flag,
5705 	cred_t *crp,
5706 	int *rvalp)
5707 {
5708 	mblk_t *bp;
5709 	struct iocblk *iocbp;
5710 	struct copyreq *reqp;
5711 	struct copyresp *resp;
5712 	int id;
5713 	int transparent = 0;
5714 	int error = 0;
5715 	int len = 0;
5716 	caddr_t taddr;
5717 	int copyflag = (flag & (U_TO_K | K_TO_K));
5718 	int sigflag = (flag & STR_NOSIG);
5719 	int errs;
5720 	uint_t waitflags;
5721 
5722 	ASSERT(copyflag == U_TO_K || copyflag == K_TO_K);
5723 	ASSERT((fflags & FMODELS) != 0);
5724 
5725 	TRACE_2(TR_FAC_STREAMS_FR,
5726 		TR_STRDOIOCTL,
5727 		"strdoioctl:stp %p strioc %p", stp, strioc);
5728 	if (strioc->ic_len == TRANSPARENT) {	/* send arg in M_DATA block */
5729 		transparent = 1;
5730 		strioc->ic_len = sizeof (intptr_t);
5731 	}
5732 
5733 	if (strioc->ic_len < 0 || (strmsgsz > 0 && strioc->ic_len > strmsgsz))
5734 		return (EINVAL);
5735 
5736 	if ((bp = allocb_cred_wait(sizeof (union ioctypes), sigflag, &error,
5737 	    crp)) == NULL)
5738 			return (error);
5739 
5740 	bzero(bp->b_wptr, sizeof (union ioctypes));
5741 
5742 	iocbp = (struct iocblk *)bp->b_wptr;
5743 	iocbp->ioc_count = strioc->ic_len;
5744 	iocbp->ioc_cmd = strioc->ic_cmd;
5745 	iocbp->ioc_flag = (fflags & FMODELS);
5746 
5747 	crhold(crp);
5748 	iocbp->ioc_cr = crp;
5749 	DB_TYPE(bp) = M_IOCTL;
5750 	DB_CPID(bp) = curproc->p_pid;
5751 	bp->b_wptr += sizeof (struct iocblk);
5752 
5753 	if (flag & STR_NOERROR)
5754 		errs = STPLEX;
5755 	else
5756 		errs = STRHUP|STRDERR|STWRERR|STPLEX;
5757 
5758 	/*
5759 	 * If there is data to copy into ioctl block, do so.
5760 	 */
5761 	if (iocbp->ioc_count > 0) {
5762 		if (transparent)
5763 			/*
5764 			 * Note: STR_NOERROR does not have an effect
5765 			 * in putiocd()
5766 			 */
5767 			id = K_TO_K | sigflag;
5768 		else
5769 			id = flag;
5770 		if ((error = putiocd(bp, strioc->ic_dp, id, crp)) != 0) {
5771 			freemsg(bp);
5772 			crfree(crp);
5773 			return (error);
5774 		}
5775 
5776 		/*
5777 		 * We could have slept copying in user pages.
5778 		 * Recheck the stream head state (the other end
5779 		 * of a pipe could have gone away).
5780 		 */
5781 		if (stp->sd_flag & errs) {
5782 			mutex_enter(&stp->sd_lock);
5783 			error = strgeterr(stp, errs, 0);
5784 			mutex_exit(&stp->sd_lock);
5785 			if (error != 0) {
5786 				freemsg(bp);
5787 				crfree(crp);
5788 				return (error);
5789 			}
5790 		}
5791 	}
5792 	if (transparent)
5793 		iocbp->ioc_count = TRANSPARENT;
5794 
5795 	/*
5796 	 * Block for up to STRTIMOUT milliseconds if there is an outstanding
5797 	 * ioctl for this stream already running.  All processes
5798 	 * sleeping here will be awakened as a result of an ACK
5799 	 * or NAK being received for the outstanding ioctl, or
5800 	 * as a result of the timer expiring on the outstanding
5801 	 * ioctl (a failure), or as a result of any waiting
5802 	 * process's timer expiring (also a failure).
5803 	 */
5804 
5805 	error = 0;
5806 	mutex_enter(&stp->sd_lock);
5807 	while (stp->sd_flag & (IOCWAIT | IOCWAITNE)) {
5808 		clock_t cv_rval;
5809 
5810 		TRACE_0(TR_FAC_STREAMS_FR,
5811 			TR_STRDOIOCTL_WAIT,
5812 			"strdoioctl sleeps - IOCWAIT");
5813 		cv_rval = str_cv_wait(&stp->sd_iocmonitor, &stp->sd_lock,
5814 		    STRTIMOUT, sigflag);
5815 		if (cv_rval <= 0) {
5816 			if (cv_rval == 0) {
5817 				error = EINTR;
5818 			} else {
5819 				if (flag & STR_NOERROR) {
5820 					/*
5821 					 * Terminating current ioctl in
5822 					 * progress -- assume it got lost and
5823 					 * wake up the other thread so that the
5824 					 * operation completes.
5825 					 */
5826 					if (!(stp->sd_flag & IOCWAITNE)) {
5827 						stp->sd_flag |= IOCWAITNE;
5828 						cv_broadcast(&stp->sd_monitor);
5829 					}
5830 					/*
5831 					 * Otherwise, there's a running
5832 					 * STR_NOERROR -- we have no choice
5833 					 * here but to wait forever (or until
5834 					 * interrupted).
5835 					 */
5836 				} else {
5837 					/*
5838 					 * pending ioctl has caused
5839 					 * us to time out
5840 					 */
5841 					error = ETIME;
5842 				}
5843 			}
5844 		} else if ((stp->sd_flag & errs)) {
5845 			error = strgeterr(stp, errs, 0);
5846 		}
5847 		if (error) {
5848 			mutex_exit(&stp->sd_lock);
5849 			freemsg(bp);
5850 			crfree(crp);
5851 			return (error);
5852 		}
5853 	}
5854 
5855 	/*
5856 	 * Have control of ioctl mechanism.
5857 	 * Send down ioctl packet and wait for response.
5858 	 */
5859 	if (stp->sd_iocblk != (mblk_t *)-1) {
5860 		freemsg(stp->sd_iocblk);
5861 	}
5862 	stp->sd_iocblk = NULL;
5863 
5864 	/*
5865 	 * If this is marked with 'noerror' (internal; mostly
5866 	 * I_{P,}{UN,}LINK), then make sure nobody else is able to get
5867 	 * in here by setting IOCWAITNE.
5868 	 */
5869 	waitflags = IOCWAIT;
5870 	if (flag & STR_NOERROR)
5871 		waitflags |= IOCWAITNE;
5872 
5873 	stp->sd_flag |= waitflags;
5874 
5875 	/*
5876 	 * Assign sequence number.
5877 	 */
5878 	iocbp->ioc_id = stp->sd_iocid = getiocseqno();
5879 
5880 	mutex_exit(&stp->sd_lock);
5881 
5882 	TRACE_1(TR_FAC_STREAMS_FR,
5883 		TR_STRDOIOCTL_PUT, "strdoioctl put: stp %p", stp);
5884 	stream_willservice(stp);
5885 	putnext(stp->sd_wrq, bp);
5886 	stream_runservice(stp);
5887 
5888 	/*
5889 	 * Timed wait for acknowledgment.  The wait time is limited by the
5890 	 * timeout value, which must be a positive integer (number of
5891 	 * milliseconds) to wait, or 0 (use default value of STRTIMOUT
5892 	 * milliseconds), or -1 (wait forever).  This will be awakened
5893 	 * either by an ACK/NAK message arriving, the timer expiring, or
5894 	 * the timer expiring on another ioctl waiting for control of the
5895 	 * mechanism.
5896 	 */
5897 waitioc:
5898 	mutex_enter(&stp->sd_lock);
5899 
5900 
5901 	/*
5902 	 * If the reply has already arrived, don't sleep.  If awakened from
5903 	 * the sleep, fail only if the reply has not arrived by then.
5904 	 * Otherwise, process the reply.
5905 	 */
5906 	while (!stp->sd_iocblk) {
5907 		clock_t cv_rval;
5908 
5909 		if (stp->sd_flag & errs) {
5910 			error = strgeterr(stp, errs, 0);
5911 			if (error != 0) {
5912 				stp->sd_flag &= ~waitflags;
5913 				cv_broadcast(&stp->sd_iocmonitor);
5914 				mutex_exit(&stp->sd_lock);
5915 				crfree(crp);
5916 				return (error);
5917 			}
5918 		}
5919 
5920 		TRACE_0(TR_FAC_STREAMS_FR,
5921 			TR_STRDOIOCTL_WAIT2,
5922 			"strdoioctl sleeps awaiting reply");
5923 		ASSERT(error == 0);
5924 
5925 		cv_rval = str_cv_wait(&stp->sd_monitor, &stp->sd_lock,
5926 		    (strioc->ic_timout ?
5927 		    strioc->ic_timout * 1000 : STRTIMOUT), sigflag);
5928 
5929 		/*
5930 		 * There are four possible cases here: interrupt, timeout,
5931 		 * wakeup by IOCWAITNE (above), or wakeup by strrput_nondata (a
5932 		 * valid M_IOCTL reply).
5933 		 *
5934 		 * If we've been awakened by a STR_NOERROR ioctl on some other
5935 		 * thread, then sd_iocblk will still be NULL, and IOCWAITNE
5936 		 * will be set.  Pretend as if we just timed out.  Note that
5937 		 * this other thread waited at least STRTIMOUT before trying to
5938 		 * awaken our thread, so this is indistinguishable (even for
5939 		 * INFTIM) from the case where we failed with ETIME waiting on
5940 		 * IOCWAIT in the prior loop.
5941 		 */
5942 		if (cv_rval > 0 && !(flag & STR_NOERROR) &&
5943 		    stp->sd_iocblk == NULL && (stp->sd_flag & IOCWAITNE)) {
5944 			cv_rval = -1;
5945 		}
5946 
5947 		/*
5948 		 * note: STR_NOERROR does not protect
5949 		 * us here.. use ic_timout < 0
5950 		 */
5951 		if (cv_rval <= 0) {
5952 			if (cv_rval == 0) {
5953 				error = EINTR;
5954 			} else {
5955 				error =  ETIME;
5956 			}
5957 			/*
5958 			 * A message could have come in after we were scheduled
5959 			 * but before we were actually run.
5960 			 */
5961 			bp = stp->sd_iocblk;
5962 			stp->sd_iocblk = NULL;
5963 			if (bp != NULL) {
5964 				if ((bp->b_datap->db_type == M_COPYIN) ||
5965 				    (bp->b_datap->db_type == M_COPYOUT)) {
5966 					mutex_exit(&stp->sd_lock);
5967 					if (bp->b_cont) {
5968 						freemsg(bp->b_cont);
5969 						bp->b_cont = NULL;
5970 					}
5971 					bp->b_datap->db_type = M_IOCDATA;
5972 					bp->b_wptr = bp->b_rptr +
5973 						sizeof (struct copyresp);
5974 					resp = (struct copyresp *)bp->b_rptr;
5975 					resp->cp_rval =
5976 					    (caddr_t)1; /* failure */
5977 					stream_willservice(stp);
5978 					putnext(stp->sd_wrq, bp);
5979 					stream_runservice(stp);
5980 					mutex_enter(&stp->sd_lock);
5981 				} else {
5982 					freemsg(bp);
5983 				}
5984 			}
5985 			stp->sd_flag &= ~waitflags;
5986 			cv_broadcast(&stp->sd_iocmonitor);
5987 			mutex_exit(&stp->sd_lock);
5988 			crfree(crp);
5989 			return (error);
5990 		}
5991 	}
5992 	bp = stp->sd_iocblk;
5993 	/*
5994 	 * Note: it is strictly impossible to get here with sd_iocblk set to
5995 	 * -1.  This is because the initial loop above doesn't allow any new
5996 	 * ioctls into the fray until all others have passed this point.
5997 	 */
5998 	ASSERT(bp != NULL && bp != (mblk_t *)-1);
5999 	TRACE_1(TR_FAC_STREAMS_FR,
6000 		TR_STRDOIOCTL_ACK, "strdoioctl got reply: bp %p", bp);
6001 	if ((bp->b_datap->db_type == M_IOCACK) ||
6002 	    (bp->b_datap->db_type == M_IOCNAK)) {
6003 		/* for detection of duplicate ioctl replies */
6004 		stp->sd_iocblk = (mblk_t *)-1;
6005 		stp->sd_flag &= ~waitflags;
6006 		cv_broadcast(&stp->sd_iocmonitor);
6007 		mutex_exit(&stp->sd_lock);
6008 	} else {
6009 		/*
6010 		 * flags not cleared here because we're still doing
6011 		 * copy in/out for ioctl.
6012 		 */
6013 		stp->sd_iocblk = NULL;
6014 		mutex_exit(&stp->sd_lock);
6015 	}
6016 
6017 
6018 	/*
6019 	 * Have received acknowledgment.
6020 	 */
6021 
6022 	switch (bp->b_datap->db_type) {
6023 	case M_IOCACK:
6024 		/*
6025 		 * Positive ack.
6026 		 */
6027 		iocbp = (struct iocblk *)bp->b_rptr;
6028 
6029 		/*
6030 		 * Set error if indicated.
6031 		 */
6032 		if (iocbp->ioc_error) {
6033 			error = iocbp->ioc_error;
6034 			break;
6035 		}
6036 
6037 		/*
6038 		 * Set return value.
6039 		 */
6040 		*rvalp = iocbp->ioc_rval;
6041 
6042 		/*
6043 		 * Data may have been returned in ACK message (ioc_count > 0).
6044 		 * If so, copy it out to the user's buffer.
6045 		 */
6046 		if (iocbp->ioc_count && !transparent) {
6047 			if (error = getiocd(bp, strioc->ic_dp, copyflag))
6048 				break;
6049 		}
6050 		if (!transparent) {
6051 			if (len)	/* an M_COPYOUT was used with I_STR */
6052 				strioc->ic_len = len;
6053 			else
6054 				strioc->ic_len = (int)iocbp->ioc_count;
6055 		}
6056 		break;
6057 
6058 	case M_IOCNAK:
6059 		/*
6060 		 * Negative ack.
6061 		 *
6062 		 * The only thing to do is set error as specified
6063 		 * in neg ack packet.
6064 		 */
6065 		iocbp = (struct iocblk *)bp->b_rptr;
6066 
6067 		error = (iocbp->ioc_error ? iocbp->ioc_error : EINVAL);
6068 		break;
6069 
6070 	case M_COPYIN:
6071 		/*
6072 		 * Driver or module has requested user ioctl data.
6073 		 */
6074 		reqp = (struct copyreq *)bp->b_rptr;
6075 
6076 		/*
6077 		 * M_COPYIN should *never* have a message attached, though
6078 		 * it's harmless if it does -- thus, panic on a DEBUG
6079 		 * kernel and just free it on a non-DEBUG build.
6080 		 */
6081 		ASSERT(bp->b_cont == NULL);
6082 		if (bp->b_cont != NULL) {
6083 			freemsg(bp->b_cont);
6084 			bp->b_cont = NULL;
6085 		}
6086 
6087 		error = putiocd(bp, reqp->cq_addr, flag, crp);
6088 		if (error && bp->b_cont) {
6089 			freemsg(bp->b_cont);
6090 			bp->b_cont = NULL;
6091 		}
6092 
6093 		bp->b_wptr = bp->b_rptr + sizeof (struct copyresp);
6094 		bp->b_datap->db_type = M_IOCDATA;
6095 
6096 		mblk_setcred(bp, crp);
6097 		DB_CPID(bp) = curproc->p_pid;
6098 		resp = (struct copyresp *)bp->b_rptr;
6099 		resp->cp_rval = (caddr_t)(uintptr_t)error;
6100 		resp->cp_flag = (fflags & FMODELS);
6101 
6102 		stream_willservice(stp);
6103 		putnext(stp->sd_wrq, bp);
6104 		stream_runservice(stp);
6105 
6106 		if (error) {
6107 			mutex_enter(&stp->sd_lock);
6108 			stp->sd_flag &= ~waitflags;
6109 			cv_broadcast(&stp->sd_iocmonitor);
6110 			mutex_exit(&stp->sd_lock);
6111 			crfree(crp);
6112 			return (error);
6113 		}
6114 
6115 		goto waitioc;
6116 
6117 	case M_COPYOUT:
6118 		/*
6119 		 * Driver or module has ioctl data for a user.
6120 		 */
6121 		reqp = (struct copyreq *)bp->b_rptr;
6122 		ASSERT(bp->b_cont != NULL);
6123 
6124 		/*
6125 		 * Always (transparent or non-transparent )
6126 		 * use the address specified in the request
6127 		 */
6128 		taddr = reqp->cq_addr;
6129 		if (!transparent)
6130 			len = (int)reqp->cq_size;
6131 
6132 		/* copyout data to the provided address */
6133 		error = getiocd(bp, taddr, copyflag);
6134 
6135 		freemsg(bp->b_cont);
6136 		bp->b_cont = NULL;
6137 
6138 		bp->b_wptr = bp->b_rptr + sizeof (struct copyresp);
6139 		bp->b_datap->db_type = M_IOCDATA;
6140 
6141 		mblk_setcred(bp, crp);
6142 		DB_CPID(bp) = curproc->p_pid;
6143 		resp = (struct copyresp *)bp->b_rptr;
6144 		resp->cp_rval = (caddr_t)(uintptr_t)error;
6145 		resp->cp_flag = (fflags & FMODELS);
6146 
6147 		stream_willservice(stp);
6148 		putnext(stp->sd_wrq, bp);
6149 		stream_runservice(stp);
6150 
6151 		if (error) {
6152 			mutex_enter(&stp->sd_lock);
6153 			stp->sd_flag &= ~waitflags;
6154 			cv_broadcast(&stp->sd_iocmonitor);
6155 			mutex_exit(&stp->sd_lock);
6156 			crfree(crp);
6157 			return (error);
6158 		}
6159 		goto waitioc;
6160 
6161 	default:
6162 		ASSERT(0);
6163 		mutex_enter(&stp->sd_lock);
6164 		stp->sd_flag &= ~waitflags;
6165 		cv_broadcast(&stp->sd_iocmonitor);
6166 		mutex_exit(&stp->sd_lock);
6167 		break;
6168 	}
6169 
6170 	freemsg(bp);
6171 	crfree(crp);
6172 	return (error);
6173 }
6174 
6175 /*
6176  * For the SunOS keyboard driver.
6177  * Return the next available "ioctl" sequence number.
6178  * Exported, so that streams modules can send "ioctl" messages
6179  * downstream from their open routine.
6180  */
6181 int
6182 getiocseqno(void)
6183 {
6184 	int	i;
6185 
6186 	mutex_enter(&strresources);
6187 	i = ++ioc_id;
6188 	mutex_exit(&strresources);
6189 	return (i);
6190 }
6191 
6192 /*
6193  * Get the next message from the read queue.  If the message is
6194  * priority, STRPRI will have been set by strrput().  This flag
6195  * should be reset only when the entire message at the front of the
6196  * queue as been consumed.
6197  *
6198  * NOTE: strgetmsg and kstrgetmsg have much of the logic in common.
6199  */
6200 int
6201 strgetmsg(
6202 	struct vnode *vp,
6203 	struct strbuf *mctl,
6204 	struct strbuf *mdata,
6205 	unsigned char *prip,
6206 	int *flagsp,
6207 	int fmode,
6208 	rval_t *rvp)
6209 {
6210 	struct stdata *stp;
6211 	mblk_t *bp, *nbp;
6212 	mblk_t *savemp = NULL;
6213 	mblk_t *savemptail = NULL;
6214 	uint_t old_sd_flag;
6215 	int flg;
6216 	int more = 0;
6217 	int error = 0;
6218 	char first = 1;
6219 	uint_t mark;		/* Contains MSG*MARK and _LASTMARK */
6220 #define	_LASTMARK	0x8000	/* Distinct from MSG*MARK */
6221 	unsigned char pri = 0;
6222 	queue_t *q;
6223 	int	pr = 0;			/* Partial read successful */
6224 	struct uio uios;
6225 	struct uio *uiop = &uios;
6226 	struct iovec iovs;
6227 	unsigned char type;
6228 
6229 	TRACE_1(TR_FAC_STREAMS_FR, TR_STRGETMSG_ENTER,
6230 		"strgetmsg:%p", vp);
6231 
6232 	ASSERT(vp->v_stream);
6233 	stp = vp->v_stream;
6234 	rvp->r_val1 = 0;
6235 
6236 	if (stp->sd_sidp != NULL && stp->sd_vnode->v_type != VFIFO)
6237 		if (error = straccess(stp, JCREAD))
6238 			return (error);
6239 
6240 	/* Fast check of flags before acquiring the lock */
6241 	if (stp->sd_flag & (STRDERR|STPLEX)) {
6242 		mutex_enter(&stp->sd_lock);
6243 		error = strgeterr(stp, STRDERR|STPLEX, 0);
6244 		mutex_exit(&stp->sd_lock);
6245 		if (error != 0)
6246 			return (error);
6247 	}
6248 
6249 	switch (*flagsp) {
6250 	case MSG_HIPRI:
6251 		if (*prip != 0)
6252 			return (EINVAL);
6253 		break;
6254 
6255 	case MSG_ANY:
6256 	case MSG_BAND:
6257 		break;
6258 
6259 	default:
6260 		return (EINVAL);
6261 	}
6262 	/*
6263 	 * Setup uio and iov for data part
6264 	 */
6265 	iovs.iov_base = mdata->buf;
6266 	iovs.iov_len = mdata->maxlen;
6267 	uios.uio_iov = &iovs;
6268 	uios.uio_iovcnt = 1;
6269 	uios.uio_loffset = 0;
6270 	uios.uio_segflg = UIO_USERSPACE;
6271 	uios.uio_fmode = 0;
6272 	uios.uio_extflg = UIO_COPY_CACHED;
6273 	uios.uio_resid = mdata->maxlen;
6274 	uios.uio_offset = 0;
6275 
6276 	q = _RD(stp->sd_wrq);
6277 	mutex_enter(&stp->sd_lock);
6278 	old_sd_flag = stp->sd_flag;
6279 	mark = 0;
6280 	for (;;) {
6281 		int done = 0;
6282 		mblk_t *q_first = q->q_first;
6283 
6284 		/*
6285 		 * Get the next message of appropriate priority
6286 		 * from the stream head.  If the caller is interested
6287 		 * in band or hipri messages, then they should already
6288 		 * be enqueued at the stream head.  On the other hand
6289 		 * if the caller wants normal (band 0) messages, they
6290 		 * might be deferred in a synchronous stream and they
6291 		 * will need to be pulled up.
6292 		 *
6293 		 * After we have dequeued a message, we might find that
6294 		 * it was a deferred M_SIG that was enqueued at the
6295 		 * stream head.  It must now be posted as part of the
6296 		 * read by calling strsignal_nolock().
6297 		 *
6298 		 * Also note that strrput does not enqueue an M_PCSIG,
6299 		 * and there cannot be more than one hipri message,
6300 		 * so there was no need to have the M_PCSIG case.
6301 		 *
6302 		 * At some time it might be nice to try and wrap the
6303 		 * functionality of kstrgetmsg() and strgetmsg() into
6304 		 * a common routine so to reduce the amount of replicated
6305 		 * code (since they are extremely similar).
6306 		 */
6307 		if (!(*flagsp & (MSG_HIPRI|MSG_BAND))) {
6308 			/* Asking for normal, band0 data */
6309 			bp = strget(stp, q, uiop, first, &error);
6310 			ASSERT(MUTEX_HELD(&stp->sd_lock));
6311 			if (bp != NULL) {
6312 				if (bp->b_datap->db_type == M_SIG) {
6313 					strsignal_nolock(stp, *bp->b_rptr,
6314 					    (int32_t)bp->b_band);
6315 					continue;
6316 				} else {
6317 					break;
6318 				}
6319 			}
6320 			if (error != 0) {
6321 				goto getmout;
6322 			}
6323 
6324 		/*
6325 		 * We can't depend on the value of STRPRI here because
6326 		 * the stream head may be in transit. Therefore, we
6327 		 * must look at the type of the first message to
6328 		 * determine if a high priority messages is waiting
6329 		 */
6330 		} else if ((*flagsp & MSG_HIPRI) && q_first != NULL &&
6331 			    q_first->b_datap->db_type >= QPCTL &&
6332 			    (bp = getq_noenab(q)) != NULL) {
6333 			/* Asked for HIPRI and got one */
6334 			ASSERT(bp->b_datap->db_type >= QPCTL);
6335 			break;
6336 		} else if ((*flagsp & MSG_BAND) && q_first != NULL &&
6337 			    ((q_first->b_band >= *prip) ||
6338 			    q_first->b_datap->db_type >= QPCTL) &&
6339 			    (bp = getq_noenab(q)) != NULL) {
6340 			/*
6341 			 * Asked for at least band "prip" and got either at
6342 			 * least that band or a hipri message.
6343 			 */
6344 			ASSERT(bp->b_band >= *prip ||
6345 				bp->b_datap->db_type >= QPCTL);
6346 			if (bp->b_datap->db_type == M_SIG) {
6347 				strsignal_nolock(stp, *bp->b_rptr,
6348 				    (int32_t)bp->b_band);
6349 				continue;
6350 			} else {
6351 				break;
6352 			}
6353 		}
6354 
6355 		/* No data. Time to sleep? */
6356 		qbackenable(q, 0);
6357 
6358 		/*
6359 		 * If STRHUP or STREOF, return 0 length control and data.
6360 		 * If resid is 0, then a read(fd,buf,0) was done. Do not
6361 		 * sleep to satisfy this request because by default we have
6362 		 * zero bytes to return.
6363 		 */
6364 		if ((stp->sd_flag & (STRHUP|STREOF)) || (mctl->maxlen == 0 &&
6365 		    mdata->maxlen == 0)) {
6366 			mctl->len = mdata->len = 0;
6367 			*flagsp = 0;
6368 			mutex_exit(&stp->sd_lock);
6369 			return (0);
6370 		}
6371 		TRACE_2(TR_FAC_STREAMS_FR, TR_STRGETMSG_WAIT,
6372 			"strgetmsg calls strwaitq:%p, %p",
6373 			vp, uiop);
6374 		if (((error = strwaitq(stp, GETWAIT, (ssize_t)0, fmode, -1,
6375 		    &done)) != 0) || done) {
6376 			TRACE_2(TR_FAC_STREAMS_FR, TR_STRGETMSG_DONE,
6377 				"strgetmsg error or done:%p, %p",
6378 				vp, uiop);
6379 			mutex_exit(&stp->sd_lock);
6380 			return (error);
6381 		}
6382 		TRACE_2(TR_FAC_STREAMS_FR, TR_STRGETMSG_AWAKE,
6383 			"strgetmsg awakes:%p, %p", vp, uiop);
6384 		if (stp->sd_sidp != NULL && stp->sd_vnode->v_type != VFIFO) {
6385 			mutex_exit(&stp->sd_lock);
6386 			if (error = straccess(stp, JCREAD))
6387 				return (error);
6388 			mutex_enter(&stp->sd_lock);
6389 		}
6390 		first = 0;
6391 	}
6392 	ASSERT(bp != NULL);
6393 	/*
6394 	 * Extract any mark information. If the message is not completely
6395 	 * consumed this information will be put in the mblk
6396 	 * that is putback.
6397 	 * If MSGMARKNEXT is set and the message is completely consumed
6398 	 * the STRATMARK flag will be set below. Likewise, if
6399 	 * MSGNOTMARKNEXT is set and the message is
6400 	 * completely consumed STRNOTATMARK will be set.
6401 	 */
6402 	mark = bp->b_flag & (MSGMARK | MSGMARKNEXT | MSGNOTMARKNEXT);
6403 	ASSERT((mark & (MSGMARKNEXT|MSGNOTMARKNEXT)) !=
6404 		(MSGMARKNEXT|MSGNOTMARKNEXT));
6405 	if (mark != 0 && bp == stp->sd_mark) {
6406 		mark |= _LASTMARK;
6407 		stp->sd_mark = NULL;
6408 	}
6409 	/*
6410 	 * keep track of the original message type and priority
6411 	 */
6412 	pri = bp->b_band;
6413 	type = bp->b_datap->db_type;
6414 	if (type == M_PASSFP) {
6415 		if ((mark & _LASTMARK) && (stp->sd_mark == NULL))
6416 			stp->sd_mark = bp;
6417 		bp->b_flag |= mark & ~_LASTMARK;
6418 		putback(stp, q, bp, pri);
6419 		qbackenable(q, pri);
6420 		mutex_exit(&stp->sd_lock);
6421 		return (EBADMSG);
6422 	}
6423 	ASSERT(type != M_SIG);
6424 
6425 	/*
6426 	 * Set this flag so strrput will not generate signals. Need to
6427 	 * make sure this flag is cleared before leaving this routine
6428 	 * else signals will stop being sent.
6429 	 */
6430 	stp->sd_flag |= STRGETINPROG;
6431 	mutex_exit(&stp->sd_lock);
6432 
6433 	if (STREAM_NEEDSERVICE(stp))
6434 		stream_runservice(stp);
6435 
6436 	/*
6437 	 * Set HIPRI flag if message is priority.
6438 	 */
6439 	if (type >= QPCTL)
6440 		flg = MSG_HIPRI;
6441 	else
6442 		flg = MSG_BAND;
6443 
6444 	/*
6445 	 * First process PROTO or PCPROTO blocks, if any.
6446 	 */
6447 	if (mctl->maxlen >= 0 && type != M_DATA) {
6448 		size_t	n, bcnt;
6449 		char	*ubuf;
6450 
6451 		bcnt = mctl->maxlen;
6452 		ubuf = mctl->buf;
6453 		while (bp != NULL && bp->b_datap->db_type != M_DATA) {
6454 			if ((n = MIN(bcnt, bp->b_wptr - bp->b_rptr)) != 0 &&
6455 			    copyout(bp->b_rptr, ubuf, n)) {
6456 				error = EFAULT;
6457 				mutex_enter(&stp->sd_lock);
6458 				/*
6459 				 * clear stream head pri flag based on
6460 				 * first message type
6461 				 */
6462 				if (type >= QPCTL) {
6463 					ASSERT(type == M_PCPROTO);
6464 					stp->sd_flag &= ~STRPRI;
6465 				}
6466 				more = 0;
6467 				freemsg(bp);
6468 				goto getmout;
6469 			}
6470 			ubuf += n;
6471 			bp->b_rptr += n;
6472 			if (bp->b_rptr >= bp->b_wptr) {
6473 				nbp = bp;
6474 				bp = bp->b_cont;
6475 				freeb(nbp);
6476 			}
6477 			ASSERT(n <= bcnt);
6478 			bcnt -= n;
6479 			if (bcnt == 0)
6480 				break;
6481 		}
6482 		mctl->len = mctl->maxlen - bcnt;
6483 	} else
6484 		mctl->len = -1;
6485 
6486 	if (bp && bp->b_datap->db_type != M_DATA) {
6487 		/*
6488 		 * More PROTO blocks in msg.
6489 		 */
6490 		more |= MORECTL;
6491 		savemp = bp;
6492 		while (bp && bp->b_datap->db_type != M_DATA) {
6493 			savemptail = bp;
6494 			bp = bp->b_cont;
6495 		}
6496 		savemptail->b_cont = NULL;
6497 	}
6498 
6499 	/*
6500 	 * Now process DATA blocks, if any.
6501 	 */
6502 	if (mdata->maxlen >= 0 && bp) {
6503 		/*
6504 		 * struiocopyout will consume a potential zero-length
6505 		 * M_DATA even if uio_resid is zero.
6506 		 */
6507 		size_t oldresid = uiop->uio_resid;
6508 
6509 		bp = struiocopyout(bp, uiop, &error);
6510 		if (error != 0) {
6511 			mutex_enter(&stp->sd_lock);
6512 			/*
6513 			 * clear stream head hi pri flag based on
6514 			 * first message
6515 			 */
6516 			if (type >= QPCTL) {
6517 				ASSERT(type == M_PCPROTO);
6518 				stp->sd_flag &= ~STRPRI;
6519 			}
6520 			more = 0;
6521 			freemsg(savemp);
6522 			goto getmout;
6523 		}
6524 		/*
6525 		 * (pr == 1) indicates a partial read.
6526 		 */
6527 		if (oldresid > uiop->uio_resid)
6528 			pr = 1;
6529 		mdata->len = mdata->maxlen - uiop->uio_resid;
6530 	} else
6531 		mdata->len = -1;
6532 
6533 	if (bp) {			/* more data blocks in msg */
6534 		more |= MOREDATA;
6535 		if (savemp)
6536 			savemptail->b_cont = bp;
6537 		else
6538 			savemp = bp;
6539 	}
6540 
6541 	mutex_enter(&stp->sd_lock);
6542 	if (savemp) {
6543 		if (pr && (savemp->b_datap->db_type == M_DATA) &&
6544 		    msgnodata(savemp)) {
6545 			/*
6546 			 * Avoid queuing a zero-length tail part of
6547 			 * a message. pr=1 indicates that we read some of
6548 			 * the message.
6549 			 */
6550 			freemsg(savemp);
6551 			more &= ~MOREDATA;
6552 			/*
6553 			 * clear stream head hi pri flag based on
6554 			 * first message
6555 			 */
6556 			if (type >= QPCTL) {
6557 				ASSERT(type == M_PCPROTO);
6558 				stp->sd_flag &= ~STRPRI;
6559 			}
6560 		} else {
6561 			savemp->b_band = pri;
6562 			/*
6563 			 * If the first message was HIPRI and the one we're
6564 			 * putting back isn't, then clear STRPRI, otherwise
6565 			 * set STRPRI again.  Note that we must set STRPRI
6566 			 * again since the flush logic in strrput_nondata()
6567 			 * may have cleared it while we had sd_lock dropped.
6568 			 */
6569 			if (type >= QPCTL) {
6570 				ASSERT(type == M_PCPROTO);
6571 				if (queclass(savemp) < QPCTL)
6572 					stp->sd_flag &= ~STRPRI;
6573 				else
6574 					stp->sd_flag |= STRPRI;
6575 			} else if (queclass(savemp) >= QPCTL) {
6576 				/*
6577 				 * The first message was not a HIPRI message,
6578 				 * but the one we are about to putback is.
6579 				 * For simplicitly, we do not allow for HIPRI
6580 				 * messages to be embedded in the message
6581 				 * body, so just force it to same type as
6582 				 * first message.
6583 				 */
6584 				ASSERT(type == M_DATA || type == M_PROTO);
6585 				ASSERT(savemp->b_datap->db_type == M_PCPROTO);
6586 				savemp->b_datap->db_type = type;
6587 			}
6588 			if (mark != 0) {
6589 				savemp->b_flag |= mark & ~_LASTMARK;
6590 				if ((mark & _LASTMARK) &&
6591 				    (stp->sd_mark == NULL)) {
6592 					/*
6593 					 * If another marked message arrived
6594 					 * while sd_lock was not held sd_mark
6595 					 * would be non-NULL.
6596 					 */
6597 					stp->sd_mark = savemp;
6598 				}
6599 			}
6600 			putback(stp, q, savemp, pri);
6601 		}
6602 	} else {
6603 		/*
6604 		 * The complete message was consumed.
6605 		 *
6606 		 * If another M_PCPROTO arrived while sd_lock was not held
6607 		 * it would have been discarded since STRPRI was still set.
6608 		 *
6609 		 * Move the MSG*MARKNEXT information
6610 		 * to the stream head just in case
6611 		 * the read queue becomes empty.
6612 		 * clear stream head hi pri flag based on
6613 		 * first message
6614 		 *
6615 		 * If the stream head was at the mark
6616 		 * (STRATMARK) before we dropped sd_lock above
6617 		 * and some data was consumed then we have
6618 		 * moved past the mark thus STRATMARK is
6619 		 * cleared. However, if a message arrived in
6620 		 * strrput during the copyout above causing
6621 		 * STRATMARK to be set we can not clear that
6622 		 * flag.
6623 		 */
6624 		if (type >= QPCTL) {
6625 			ASSERT(type == M_PCPROTO);
6626 			stp->sd_flag &= ~STRPRI;
6627 		}
6628 		if (mark & (MSGMARKNEXT|MSGNOTMARKNEXT|MSGMARK)) {
6629 			if (mark & MSGMARKNEXT) {
6630 				stp->sd_flag &= ~STRNOTATMARK;
6631 				stp->sd_flag |= STRATMARK;
6632 			} else if (mark & MSGNOTMARKNEXT) {
6633 				stp->sd_flag &= ~STRATMARK;
6634 				stp->sd_flag |= STRNOTATMARK;
6635 			} else {
6636 				stp->sd_flag &= ~(STRATMARK|STRNOTATMARK);
6637 			}
6638 		} else if (pr && (old_sd_flag & STRATMARK)) {
6639 			stp->sd_flag &= ~STRATMARK;
6640 		}
6641 	}
6642 
6643 	*flagsp = flg;
6644 	*prip = pri;
6645 
6646 	/*
6647 	 * Getmsg cleanup processing - if the state of the queue has changed
6648 	 * some signals may need to be sent and/or poll awakened.
6649 	 */
6650 getmout:
6651 	qbackenable(q, pri);
6652 
6653 	/*
6654 	 * We dropped the stream head lock above. Send all M_SIG messages
6655 	 * before processing stream head for SIGPOLL messages.
6656 	 */
6657 	ASSERT(MUTEX_HELD(&stp->sd_lock));
6658 	while ((bp = q->q_first) != NULL &&
6659 	    (bp->b_datap->db_type == M_SIG)) {
6660 		/*
6661 		 * sd_lock is held so the content of the read queue can not
6662 		 * change.
6663 		 */
6664 		bp = getq(q);
6665 		ASSERT(bp != NULL && bp->b_datap->db_type == M_SIG);
6666 
6667 		strsignal_nolock(stp, *bp->b_rptr, (int32_t)bp->b_band);
6668 		mutex_exit(&stp->sd_lock);
6669 		freemsg(bp);
6670 		if (STREAM_NEEDSERVICE(stp))
6671 			stream_runservice(stp);
6672 		mutex_enter(&stp->sd_lock);
6673 	}
6674 
6675 	/*
6676 	 * stream head cannot change while we make the determination
6677 	 * whether or not to send a signal. Drop the flag to allow strrput
6678 	 * to send firstmsgsigs again.
6679 	 */
6680 	stp->sd_flag &= ~STRGETINPROG;
6681 
6682 	/*
6683 	 * If the type of message at the front of the queue changed
6684 	 * due to the receive the appropriate signals and pollwakeup events
6685 	 * are generated. The type of changes are:
6686 	 *	Processed a hipri message, q_first is not hipri.
6687 	 *	Processed a band X message, and q_first is band Y.
6688 	 * The generated signals and pollwakeups are identical to what
6689 	 * strrput() generates should the message that is now on q_first
6690 	 * arrive to an empty read queue.
6691 	 *
6692 	 * Note: only strrput will send a signal for a hipri message.
6693 	 */
6694 	if ((bp = q->q_first) != NULL && !(stp->sd_flag & STRPRI)) {
6695 		strsigset_t signals = 0;
6696 		strpollset_t pollwakeups = 0;
6697 
6698 		if (flg & MSG_HIPRI) {
6699 			/*
6700 			 * Removed a hipri message. Regular data at
6701 			 * the front of  the queue.
6702 			 */
6703 			if (bp->b_band == 0) {
6704 				signals = S_INPUT | S_RDNORM;
6705 				pollwakeups = POLLIN | POLLRDNORM;
6706 			} else {
6707 				signals = S_INPUT | S_RDBAND;
6708 				pollwakeups = POLLIN | POLLRDBAND;
6709 			}
6710 		} else if (pri != bp->b_band) {
6711 			/*
6712 			 * The band is different for the new q_first.
6713 			 */
6714 			if (bp->b_band == 0) {
6715 				signals = S_RDNORM;
6716 				pollwakeups = POLLIN | POLLRDNORM;
6717 			} else {
6718 				signals = S_RDBAND;
6719 				pollwakeups = POLLIN | POLLRDBAND;
6720 			}
6721 		}
6722 
6723 		if (pollwakeups != 0) {
6724 			if (pollwakeups == (POLLIN | POLLRDNORM)) {
6725 				if (!(stp->sd_rput_opt & SR_POLLIN))
6726 					goto no_pollwake;
6727 				stp->sd_rput_opt &= ~SR_POLLIN;
6728 			}
6729 			mutex_exit(&stp->sd_lock);
6730 			pollwakeup(&stp->sd_pollist, pollwakeups);
6731 			mutex_enter(&stp->sd_lock);
6732 		}
6733 no_pollwake:
6734 
6735 		if (stp->sd_sigflags & signals)
6736 			strsendsig(stp->sd_siglist, signals, bp->b_band, 0);
6737 	}
6738 	mutex_exit(&stp->sd_lock);
6739 
6740 	rvp->r_val1 = more;
6741 	return (error);
6742 #undef	_LASTMARK
6743 }
6744 
6745 /*
6746  * Get the next message from the read queue.  If the message is
6747  * priority, STRPRI will have been set by strrput().  This flag
6748  * should be reset only when the entire message at the front of the
6749  * queue as been consumed.
6750  *
6751  * If uiop is NULL all data is returned in mctlp.
6752  * Note that a NULL uiop implies that FNDELAY and FNONBLOCK are assumed
6753  * not enabled.
6754  * The timeout parameter is in milliseconds; -1 for infinity.
6755  * This routine handles the consolidation private flags:
6756  *	MSG_IGNERROR	Ignore any stream head error except STPLEX.
6757  *	MSG_DELAYERROR	Defer the error check until the queue is empty.
6758  *	MSG_HOLDSIG	Hold signals while waiting for data.
6759  *	MSG_IPEEK	Only peek at messages.
6760  *	MSG_DISCARDTAIL	Discard the tail M_DATA part of the message
6761  *			that doesn't fit.
6762  *	MSG_NOMARK	If the message is marked leave it on the queue.
6763  *
6764  * NOTE: strgetmsg and kstrgetmsg have much of the logic in common.
6765  */
6766 int
6767 kstrgetmsg(
6768 	struct vnode *vp,
6769 	mblk_t **mctlp,
6770 	struct uio *uiop,
6771 	unsigned char *prip,
6772 	int *flagsp,
6773 	clock_t timout,
6774 	rval_t *rvp)
6775 {
6776 	struct stdata *stp;
6777 	mblk_t *bp, *nbp;
6778 	mblk_t *savemp = NULL;
6779 	mblk_t *savemptail = NULL;
6780 	int flags;
6781 	uint_t old_sd_flag;
6782 	int flg;
6783 	int more = 0;
6784 	int error = 0;
6785 	char first = 1;
6786 	uint_t mark;		/* Contains MSG*MARK and _LASTMARK */
6787 #define	_LASTMARK	0x8000	/* Distinct from MSG*MARK */
6788 	unsigned char pri = 0;
6789 	queue_t *q;
6790 	int	pr = 0;			/* Partial read successful */
6791 	unsigned char type;
6792 
6793 	TRACE_1(TR_FAC_STREAMS_FR, TR_KSTRGETMSG_ENTER,
6794 		"kstrgetmsg:%p", vp);
6795 
6796 	ASSERT(vp->v_stream);
6797 	stp = vp->v_stream;
6798 	rvp->r_val1 = 0;
6799 
6800 	if (stp->sd_sidp != NULL && stp->sd_vnode->v_type != VFIFO)
6801 		if (error = straccess(stp, JCREAD))
6802 			return (error);
6803 
6804 	flags = *flagsp;
6805 	/* Fast check of flags before acquiring the lock */
6806 	if (stp->sd_flag & (STRDERR|STPLEX)) {
6807 		if ((stp->sd_flag & STPLEX) ||
6808 		    (flags & (MSG_IGNERROR|MSG_DELAYERROR)) == 0) {
6809 			mutex_enter(&stp->sd_lock);
6810 			error = strgeterr(stp, STRDERR|STPLEX,
6811 					(flags & MSG_IPEEK));
6812 			mutex_exit(&stp->sd_lock);
6813 			if (error != 0)
6814 				return (error);
6815 		}
6816 	}
6817 
6818 	switch (flags & (MSG_HIPRI|MSG_ANY|MSG_BAND)) {
6819 	case MSG_HIPRI:
6820 		if (*prip != 0)
6821 			return (EINVAL);
6822 		break;
6823 
6824 	case MSG_ANY:
6825 	case MSG_BAND:
6826 		break;
6827 
6828 	default:
6829 		return (EINVAL);
6830 	}
6831 
6832 retry:
6833 	q = _RD(stp->sd_wrq);
6834 	mutex_enter(&stp->sd_lock);
6835 	old_sd_flag = stp->sd_flag;
6836 	mark = 0;
6837 	for (;;) {
6838 		int done = 0;
6839 		int waitflag;
6840 		int fmode;
6841 		mblk_t *q_first = q->q_first;
6842 
6843 		/*
6844 		 * This section of the code operates just like the code
6845 		 * in strgetmsg().  There is a comment there about what
6846 		 * is going on here.
6847 		 */
6848 		if (!(flags & (MSG_HIPRI|MSG_BAND))) {
6849 			/* Asking for normal, band0 data */
6850 			bp = strget(stp, q, uiop, first, &error);
6851 			ASSERT(MUTEX_HELD(&stp->sd_lock));
6852 			if (bp != NULL) {
6853 				if (bp->b_datap->db_type == M_SIG) {
6854 					strsignal_nolock(stp, *bp->b_rptr,
6855 					    (int32_t)bp->b_band);
6856 					continue;
6857 				} else {
6858 					break;
6859 				}
6860 			}
6861 			if (error != 0) {
6862 				goto getmout;
6863 			}
6864 		/*
6865 		 * We can't depend on the value of STRPRI here because
6866 		 * the stream head may be in transit. Therefore, we
6867 		 * must look at the type of the first message to
6868 		 * determine if a high priority messages is waiting
6869 		 */
6870 		} else if ((flags & MSG_HIPRI) && q_first != NULL &&
6871 			    q_first->b_datap->db_type >= QPCTL &&
6872 			    (bp = getq_noenab(q)) != NULL) {
6873 			ASSERT(bp->b_datap->db_type >= QPCTL);
6874 			break;
6875 		} else if ((flags & MSG_BAND) && q_first != NULL &&
6876 			    ((q_first->b_band >= *prip) ||
6877 			    q_first->b_datap->db_type >= QPCTL) &&
6878 			    (bp = getq_noenab(q)) != NULL) {
6879 			/*
6880 			 * Asked for at least band "prip" and got either at
6881 			 * least that band or a hipri message.
6882 			 */
6883 			ASSERT(bp->b_band >= *prip ||
6884 				bp->b_datap->db_type >= QPCTL);
6885 			if (bp->b_datap->db_type == M_SIG) {
6886 				strsignal_nolock(stp, *bp->b_rptr,
6887 				    (int32_t)bp->b_band);
6888 				continue;
6889 			} else {
6890 				break;
6891 			}
6892 		}
6893 
6894 		/* No data. Time to sleep? */
6895 		qbackenable(q, 0);
6896 
6897 		/*
6898 		 * Delayed error notification?
6899 		 */
6900 		if ((stp->sd_flag & (STRDERR|STPLEX)) &&
6901 		    (flags & (MSG_IGNERROR|MSG_DELAYERROR)) == MSG_DELAYERROR) {
6902 			error = strgeterr(stp, STRDERR|STPLEX,
6903 					(flags & MSG_IPEEK));
6904 			if (error != 0) {
6905 				mutex_exit(&stp->sd_lock);
6906 				return (error);
6907 			}
6908 		}
6909 
6910 		/*
6911 		 * If STRHUP or STREOF, return 0 length control and data.
6912 		 * If a read(fd,buf,0) has been done, do not sleep, just
6913 		 * return.
6914 		 *
6915 		 * If mctlp == NULL and uiop == NULL, then the code will
6916 		 * do the strwaitq. This is an understood way of saying
6917 		 * sleep "polling" until a message is received.
6918 		 */
6919 		if ((stp->sd_flag & (STRHUP|STREOF)) ||
6920 		    (uiop != NULL && uiop->uio_resid == 0)) {
6921 			if (mctlp != NULL)
6922 				*mctlp = NULL;
6923 			*flagsp = 0;
6924 			mutex_exit(&stp->sd_lock);
6925 			return (0);
6926 		}
6927 
6928 		waitflag = GETWAIT;
6929 		if (flags &
6930 		    (MSG_HOLDSIG|MSG_IGNERROR|MSG_IPEEK|MSG_DELAYERROR)) {
6931 			if (flags & MSG_HOLDSIG)
6932 				waitflag |= STR_NOSIG;
6933 			if (flags & MSG_IGNERROR)
6934 				waitflag |= STR_NOERROR;
6935 			if (flags & MSG_IPEEK)
6936 				waitflag |= STR_PEEK;
6937 			if (flags & MSG_DELAYERROR)
6938 				waitflag |= STR_DELAYERR;
6939 		}
6940 		if (uiop != NULL)
6941 			fmode = uiop->uio_fmode;
6942 		else
6943 			fmode = 0;
6944 
6945 		TRACE_2(TR_FAC_STREAMS_FR, TR_KSTRGETMSG_WAIT,
6946 			"kstrgetmsg calls strwaitq:%p, %p",
6947 			vp, uiop);
6948 		if (((error = strwaitq(stp, waitflag, (ssize_t)0,
6949 		    fmode, timout, &done)) != 0) || done) {
6950 			TRACE_2(TR_FAC_STREAMS_FR, TR_KSTRGETMSG_DONE,
6951 				"kstrgetmsg error or done:%p, %p",
6952 				vp, uiop);
6953 			mutex_exit(&stp->sd_lock);
6954 			return (error);
6955 		}
6956 		TRACE_2(TR_FAC_STREAMS_FR, TR_KSTRGETMSG_AWAKE,
6957 			"kstrgetmsg awakes:%p, %p", vp, uiop);
6958 		if (stp->sd_sidp != NULL && stp->sd_vnode->v_type != VFIFO) {
6959 			mutex_exit(&stp->sd_lock);
6960 			if (error = straccess(stp, JCREAD))
6961 				return (error);
6962 			mutex_enter(&stp->sd_lock);
6963 		}
6964 		first = 0;
6965 	}
6966 	ASSERT(bp != NULL);
6967 	/*
6968 	 * Extract any mark information. If the message is not completely
6969 	 * consumed this information will be put in the mblk
6970 	 * that is putback.
6971 	 * If MSGMARKNEXT is set and the message is completely consumed
6972 	 * the STRATMARK flag will be set below. Likewise, if
6973 	 * MSGNOTMARKNEXT is set and the message is
6974 	 * completely consumed STRNOTATMARK will be set.
6975 	 */
6976 	mark = bp->b_flag & (MSGMARK | MSGMARKNEXT | MSGNOTMARKNEXT);
6977 	ASSERT((mark & (MSGMARKNEXT|MSGNOTMARKNEXT)) !=
6978 		(MSGMARKNEXT|MSGNOTMARKNEXT));
6979 	pri = bp->b_band;
6980 	if (mark != 0) {
6981 		/*
6982 		 * If the caller doesn't want the mark return.
6983 		 * Used to implement MSG_WAITALL in sockets.
6984 		 */
6985 		if (flags & MSG_NOMARK) {
6986 			putback(stp, q, bp, pri);
6987 			qbackenable(q, pri);
6988 			mutex_exit(&stp->sd_lock);
6989 			return (EWOULDBLOCK);
6990 		}
6991 		if (bp == stp->sd_mark) {
6992 			mark |= _LASTMARK;
6993 			stp->sd_mark = NULL;
6994 		}
6995 	}
6996 
6997 	/*
6998 	 * keep track of the first message type
6999 	 */
7000 	type = bp->b_datap->db_type;
7001 
7002 	if (bp->b_datap->db_type == M_PASSFP) {
7003 		if ((mark & _LASTMARK) && (stp->sd_mark == NULL))
7004 			stp->sd_mark = bp;
7005 		bp->b_flag |= mark & ~_LASTMARK;
7006 		putback(stp, q, bp, pri);
7007 		qbackenable(q, pri);
7008 		mutex_exit(&stp->sd_lock);
7009 		return (EBADMSG);
7010 	}
7011 	ASSERT(type != M_SIG);
7012 
7013 	if (flags & MSG_IPEEK) {
7014 		/*
7015 		 * Clear any struioflag - we do the uiomove over again
7016 		 * when peeking since it simplifies the code.
7017 		 *
7018 		 * Dup the message and put the original back on the queue.
7019 		 * If dupmsg() fails, try again with copymsg() to see if
7020 		 * there is indeed a shortage of memory.  dupmsg() may fail
7021 		 * if db_ref in any of the messages reaches its limit.
7022 		 */
7023 		if ((nbp = dupmsg(bp)) == NULL && (nbp = copymsg(bp)) == NULL) {
7024 			/*
7025 			 * Restore the state of the stream head since we
7026 			 * need to drop sd_lock (strwaitbuf is sleeping).
7027 			 */
7028 			size_t size = msgdsize(bp);
7029 
7030 			if ((mark & _LASTMARK) && (stp->sd_mark == NULL))
7031 				stp->sd_mark = bp;
7032 			bp->b_flag |= mark & ~_LASTMARK;
7033 			putback(stp, q, bp, pri);
7034 			mutex_exit(&stp->sd_lock);
7035 			error = strwaitbuf(size, BPRI_HI);
7036 			if (error) {
7037 				/*
7038 				 * There is no net change to the queue thus
7039 				 * no need to qbackenable.
7040 				 */
7041 				return (error);
7042 			}
7043 			goto retry;
7044 		}
7045 
7046 		if ((mark & _LASTMARK) && (stp->sd_mark == NULL))
7047 			stp->sd_mark = bp;
7048 		bp->b_flag |= mark & ~_LASTMARK;
7049 		putback(stp, q, bp, pri);
7050 		bp = nbp;
7051 	}
7052 
7053 	/*
7054 	 * Set this flag so strrput will not generate signals. Need to
7055 	 * make sure this flag is cleared before leaving this routine
7056 	 * else signals will stop being sent.
7057 	 */
7058 	stp->sd_flag |= STRGETINPROG;
7059 	mutex_exit(&stp->sd_lock);
7060 
7061 	if ((stp->sd_rputdatafunc != NULL) && (DB_TYPE(bp) == M_DATA) &&
7062 	    (!(DB_FLAGS(bp) & DBLK_COOKED))) {
7063 
7064 		bp = (stp->sd_rputdatafunc)(
7065 		    stp->sd_vnode, bp, NULL,
7066 		    NULL, NULL, NULL);
7067 
7068 		if (bp == NULL)
7069 			goto retry;
7070 
7071 		DB_FLAGS(bp) |= DBLK_COOKED;
7072 	}
7073 
7074 	if (STREAM_NEEDSERVICE(stp))
7075 		stream_runservice(stp);
7076 
7077 	/*
7078 	 * Set HIPRI flag if message is priority.
7079 	 */
7080 	if (type >= QPCTL)
7081 		flg = MSG_HIPRI;
7082 	else
7083 		flg = MSG_BAND;
7084 
7085 	/*
7086 	 * First process PROTO or PCPROTO blocks, if any.
7087 	 */
7088 	if (mctlp != NULL && type != M_DATA) {
7089 		mblk_t *nbp;
7090 
7091 		*mctlp = bp;
7092 		while (bp->b_cont && bp->b_cont->b_datap->db_type != M_DATA)
7093 			bp = bp->b_cont;
7094 		nbp = bp->b_cont;
7095 		bp->b_cont = NULL;
7096 		bp = nbp;
7097 	}
7098 
7099 	if (bp && bp->b_datap->db_type != M_DATA) {
7100 		/*
7101 		 * More PROTO blocks in msg. Will only happen if mctlp is NULL.
7102 		 */
7103 		more |= MORECTL;
7104 		savemp = bp;
7105 		while (bp && bp->b_datap->db_type != M_DATA) {
7106 			savemptail = bp;
7107 			bp = bp->b_cont;
7108 		}
7109 		savemptail->b_cont = NULL;
7110 	}
7111 
7112 	/*
7113 	 * Now process DATA blocks, if any.
7114 	 */
7115 	if (uiop == NULL) {
7116 		/* Append data to tail of mctlp */
7117 		if (mctlp != NULL) {
7118 			mblk_t **mpp = mctlp;
7119 
7120 			while (*mpp != NULL)
7121 				mpp = &((*mpp)->b_cont);
7122 			*mpp = bp;
7123 			bp = NULL;
7124 		}
7125 	} else if (uiop->uio_resid >= 0 && bp) {
7126 		size_t oldresid = uiop->uio_resid;
7127 
7128 		/*
7129 		 * If a streams message is likely to consist
7130 		 * of many small mblks, it is pulled up into
7131 		 * one continuous chunk of memory.
7132 		 * see longer comment at top of page
7133 		 * by mblk_pull_len declaration.
7134 		 */
7135 
7136 		if (MBLKL(bp) < mblk_pull_len) {
7137 			(void) pullupmsg(bp, -1);
7138 		}
7139 
7140 		bp = struiocopyout(bp, uiop, &error);
7141 		if (error != 0) {
7142 			if (mctlp != NULL) {
7143 				freemsg(*mctlp);
7144 				*mctlp = NULL;
7145 			} else
7146 				freemsg(savemp);
7147 			mutex_enter(&stp->sd_lock);
7148 			/*
7149 			 * clear stream head hi pri flag based on
7150 			 * first message
7151 			 */
7152 			if (!(flags & MSG_IPEEK) && (type >= QPCTL)) {
7153 				ASSERT(type == M_PCPROTO);
7154 				stp->sd_flag &= ~STRPRI;
7155 			}
7156 			more = 0;
7157 			goto getmout;
7158 		}
7159 		/*
7160 		 * (pr == 1) indicates a partial read.
7161 		 */
7162 		if (oldresid > uiop->uio_resid)
7163 			pr = 1;
7164 	}
7165 
7166 	if (bp) {			/* more data blocks in msg */
7167 		more |= MOREDATA;
7168 		if (savemp)
7169 			savemptail->b_cont = bp;
7170 		else
7171 			savemp = bp;
7172 	}
7173 
7174 	mutex_enter(&stp->sd_lock);
7175 	if (savemp) {
7176 		if (flags & (MSG_IPEEK|MSG_DISCARDTAIL)) {
7177 			/*
7178 			 * When MSG_DISCARDTAIL is set or
7179 			 * when peeking discard any tail. When peeking this
7180 			 * is the tail of the dup that was copied out - the
7181 			 * message has already been putback on the queue.
7182 			 * Return MOREDATA to the caller even though the data
7183 			 * is discarded. This is used by sockets (to
7184 			 * set MSG_TRUNC).
7185 			 */
7186 			freemsg(savemp);
7187 			if (!(flags & MSG_IPEEK) && (type >= QPCTL)) {
7188 				ASSERT(type == M_PCPROTO);
7189 				stp->sd_flag &= ~STRPRI;
7190 			}
7191 		} else if (pr && (savemp->b_datap->db_type == M_DATA) &&
7192 			    msgnodata(savemp)) {
7193 			/*
7194 			 * Avoid queuing a zero-length tail part of
7195 			 * a message. pr=1 indicates that we read some of
7196 			 * the message.
7197 			 */
7198 			freemsg(savemp);
7199 			more &= ~MOREDATA;
7200 			if (type >= QPCTL) {
7201 				ASSERT(type == M_PCPROTO);
7202 				stp->sd_flag &= ~STRPRI;
7203 			}
7204 		} else {
7205 			savemp->b_band = pri;
7206 			/*
7207 			 * If the first message was HIPRI and the one we're
7208 			 * putting back isn't, then clear STRPRI, otherwise
7209 			 * set STRPRI again.  Note that we must set STRPRI
7210 			 * again since the flush logic in strrput_nondata()
7211 			 * may have cleared it while we had sd_lock dropped.
7212 			 */
7213 			if (type >= QPCTL) {
7214 				ASSERT(type == M_PCPROTO);
7215 				if (queclass(savemp) < QPCTL)
7216 					stp->sd_flag &= ~STRPRI;
7217 				else
7218 					stp->sd_flag |= STRPRI;
7219 			} else if (queclass(savemp) >= QPCTL) {
7220 				/*
7221 				 * The first message was not a HIPRI message,
7222 				 * but the one we are about to putback is.
7223 				 * For simplicitly, we do not allow for HIPRI
7224 				 * messages to be embedded in the message
7225 				 * body, so just force it to same type as
7226 				 * first message.
7227 				 */
7228 				ASSERT(type == M_DATA || type == M_PROTO);
7229 				ASSERT(savemp->b_datap->db_type == M_PCPROTO);
7230 				savemp->b_datap->db_type = type;
7231 			}
7232 			if (mark != 0) {
7233 				if ((mark & _LASTMARK) &&
7234 				    (stp->sd_mark == NULL)) {
7235 					/*
7236 					 * If another marked message arrived
7237 					 * while sd_lock was not held sd_mark
7238 					 * would be non-NULL.
7239 					 */
7240 					stp->sd_mark = savemp;
7241 				}
7242 				savemp->b_flag |= mark & ~_LASTMARK;
7243 			}
7244 			putback(stp, q, savemp, pri);
7245 		}
7246 	} else if (!(flags & MSG_IPEEK)) {
7247 		/*
7248 		 * The complete message was consumed.
7249 		 *
7250 		 * If another M_PCPROTO arrived while sd_lock was not held
7251 		 * it would have been discarded since STRPRI was still set.
7252 		 *
7253 		 * Move the MSG*MARKNEXT information
7254 		 * to the stream head just in case
7255 		 * the read queue becomes empty.
7256 		 * clear stream head hi pri flag based on
7257 		 * first message
7258 		 *
7259 		 * If the stream head was at the mark
7260 		 * (STRATMARK) before we dropped sd_lock above
7261 		 * and some data was consumed then we have
7262 		 * moved past the mark thus STRATMARK is
7263 		 * cleared. However, if a message arrived in
7264 		 * strrput during the copyout above causing
7265 		 * STRATMARK to be set we can not clear that
7266 		 * flag.
7267 		 * XXX A "perimeter" would help by single-threading strrput,
7268 		 * strread, strgetmsg and kstrgetmsg.
7269 		 */
7270 		if (type >= QPCTL) {
7271 			ASSERT(type == M_PCPROTO);
7272 			stp->sd_flag &= ~STRPRI;
7273 		}
7274 		if (mark & (MSGMARKNEXT|MSGNOTMARKNEXT|MSGMARK)) {
7275 			if (mark & MSGMARKNEXT) {
7276 				stp->sd_flag &= ~STRNOTATMARK;
7277 				stp->sd_flag |= STRATMARK;
7278 			} else if (mark & MSGNOTMARKNEXT) {
7279 				stp->sd_flag &= ~STRATMARK;
7280 				stp->sd_flag |= STRNOTATMARK;
7281 			} else {
7282 				stp->sd_flag &= ~(STRATMARK|STRNOTATMARK);
7283 			}
7284 		} else if (pr && (old_sd_flag & STRATMARK)) {
7285 			stp->sd_flag &= ~STRATMARK;
7286 		}
7287 	}
7288 
7289 	*flagsp = flg;
7290 	*prip = pri;
7291 
7292 	/*
7293 	 * Getmsg cleanup processing - if the state of the queue has changed
7294 	 * some signals may need to be sent and/or poll awakened.
7295 	 */
7296 getmout:
7297 	qbackenable(q, pri);
7298 
7299 	/*
7300 	 * We dropped the stream head lock above. Send all M_SIG messages
7301 	 * before processing stream head for SIGPOLL messages.
7302 	 */
7303 	ASSERT(MUTEX_HELD(&stp->sd_lock));
7304 	while ((bp = q->q_first) != NULL &&
7305 	    (bp->b_datap->db_type == M_SIG)) {
7306 		/*
7307 		 * sd_lock is held so the content of the read queue can not
7308 		 * change.
7309 		 */
7310 		bp = getq(q);
7311 		ASSERT(bp != NULL && bp->b_datap->db_type == M_SIG);
7312 
7313 		strsignal_nolock(stp, *bp->b_rptr, (int32_t)bp->b_band);
7314 		mutex_exit(&stp->sd_lock);
7315 		freemsg(bp);
7316 		if (STREAM_NEEDSERVICE(stp))
7317 			stream_runservice(stp);
7318 		mutex_enter(&stp->sd_lock);
7319 	}
7320 
7321 	/*
7322 	 * stream head cannot change while we make the determination
7323 	 * whether or not to send a signal. Drop the flag to allow strrput
7324 	 * to send firstmsgsigs again.
7325 	 */
7326 	stp->sd_flag &= ~STRGETINPROG;
7327 
7328 	/*
7329 	 * If the type of message at the front of the queue changed
7330 	 * due to the receive the appropriate signals and pollwakeup events
7331 	 * are generated. The type of changes are:
7332 	 *	Processed a hipri message, q_first is not hipri.
7333 	 *	Processed a band X message, and q_first is band Y.
7334 	 * The generated signals and pollwakeups are identical to what
7335 	 * strrput() generates should the message that is now on q_first
7336 	 * arrive to an empty read queue.
7337 	 *
7338 	 * Note: only strrput will send a signal for a hipri message.
7339 	 */
7340 	if ((bp = q->q_first) != NULL && !(stp->sd_flag & STRPRI)) {
7341 		strsigset_t signals = 0;
7342 		strpollset_t pollwakeups = 0;
7343 
7344 		if (flg & MSG_HIPRI) {
7345 			/*
7346 			 * Removed a hipri message. Regular data at
7347 			 * the front of  the queue.
7348 			 */
7349 			if (bp->b_band == 0) {
7350 				signals = S_INPUT | S_RDNORM;
7351 				pollwakeups = POLLIN | POLLRDNORM;
7352 			} else {
7353 				signals = S_INPUT | S_RDBAND;
7354 				pollwakeups = POLLIN | POLLRDBAND;
7355 			}
7356 		} else if (pri != bp->b_band) {
7357 			/*
7358 			 * The band is different for the new q_first.
7359 			 */
7360 			if (bp->b_band == 0) {
7361 				signals = S_RDNORM;
7362 				pollwakeups = POLLIN | POLLRDNORM;
7363 			} else {
7364 				signals = S_RDBAND;
7365 				pollwakeups = POLLIN | POLLRDBAND;
7366 			}
7367 		}
7368 
7369 		if (pollwakeups != 0) {
7370 			if (pollwakeups == (POLLIN | POLLRDNORM)) {
7371 				if (!(stp->sd_rput_opt & SR_POLLIN))
7372 					goto no_pollwake;
7373 				stp->sd_rput_opt &= ~SR_POLLIN;
7374 			}
7375 			mutex_exit(&stp->sd_lock);
7376 			pollwakeup(&stp->sd_pollist, pollwakeups);
7377 			mutex_enter(&stp->sd_lock);
7378 		}
7379 no_pollwake:
7380 
7381 		if (stp->sd_sigflags & signals)
7382 			strsendsig(stp->sd_siglist, signals, bp->b_band, 0);
7383 	}
7384 	mutex_exit(&stp->sd_lock);
7385 
7386 	rvp->r_val1 = more;
7387 	return (error);
7388 #undef	_LASTMARK
7389 }
7390 
7391 /*
7392  * Put a message downstream.
7393  *
7394  * NOTE: strputmsg and kstrputmsg have much of the logic in common.
7395  */
7396 int
7397 strputmsg(
7398 	struct vnode *vp,
7399 	struct strbuf *mctl,
7400 	struct strbuf *mdata,
7401 	unsigned char pri,
7402 	int flag,
7403 	int fmode)
7404 {
7405 	struct stdata *stp;
7406 	queue_t *wqp;
7407 	mblk_t *mp;
7408 	ssize_t msgsize;
7409 	ssize_t rmin, rmax;
7410 	int error;
7411 	struct uio uios;
7412 	struct uio *uiop = &uios;
7413 	struct iovec iovs;
7414 	int xpg4 = 0;
7415 
7416 	ASSERT(vp->v_stream);
7417 	stp = vp->v_stream;
7418 	wqp = stp->sd_wrq;
7419 
7420 	/*
7421 	 * If it is an XPG4 application, we need to send
7422 	 * SIGPIPE below
7423 	 */
7424 
7425 	xpg4 = (flag & MSG_XPG4) ? 1 : 0;
7426 	flag &= ~MSG_XPG4;
7427 
7428 #ifdef C2_AUDIT
7429 	if (audit_active)
7430 		audit_strputmsg(vp, mctl, mdata, pri, flag, fmode);
7431 #endif
7432 
7433 	if (stp->sd_sidp != NULL && stp->sd_vnode->v_type != VFIFO)
7434 		if (error = straccess(stp, JCWRITE))
7435 			return (error);
7436 
7437 	if (stp->sd_flag & (STWRERR|STRHUP|STPLEX)) {
7438 		mutex_enter(&stp->sd_lock);
7439 		error = strwriteable(stp, B_FALSE, xpg4);
7440 		mutex_exit(&stp->sd_lock);
7441 		if (error != 0)
7442 			return (error);
7443 	}
7444 
7445 	/*
7446 	 * Check for legal flag value.
7447 	 */
7448 	switch (flag) {
7449 	case MSG_HIPRI:
7450 		if ((mctl->len < 0) || (pri != 0))
7451 			return (EINVAL);
7452 		break;
7453 	case MSG_BAND:
7454 		break;
7455 
7456 	default:
7457 		return (EINVAL);
7458 	}
7459 
7460 	TRACE_1(TR_FAC_STREAMS_FR, TR_STRPUTMSG_IN,
7461 		"strputmsg in:stp %p", stp);
7462 
7463 	/* get these values from those cached in the stream head */
7464 	rmin = stp->sd_qn_minpsz;
7465 	rmax = stp->sd_qn_maxpsz;
7466 
7467 	/*
7468 	 * Make sure ctl and data sizes together fall within the
7469 	 * limits of the max and min receive packet sizes and do
7470 	 * not exceed system limit.
7471 	 */
7472 	ASSERT((rmax >= 0) || (rmax == INFPSZ));
7473 	if (rmax == 0) {
7474 		return (ERANGE);
7475 	}
7476 	/*
7477 	 * Use the MAXIMUM of sd_maxblk and q_maxpsz.
7478 	 * Needed to prevent partial failures in the strmakedata loop.
7479 	 */
7480 	if (stp->sd_maxblk != INFPSZ && rmax != INFPSZ && rmax < stp->sd_maxblk)
7481 		rmax = stp->sd_maxblk;
7482 
7483 	if ((msgsize = mdata->len) < 0) {
7484 		msgsize = 0;
7485 		rmin = 0;	/* no range check for NULL data part */
7486 	}
7487 	if ((msgsize < rmin) ||
7488 	    ((msgsize > rmax) && (rmax != INFPSZ)) ||
7489 	    (mctl->len > strctlsz)) {
7490 		return (ERANGE);
7491 	}
7492 
7493 	/*
7494 	 * Setup uio and iov for data part
7495 	 */
7496 	iovs.iov_base = mdata->buf;
7497 	iovs.iov_len = msgsize;
7498 	uios.uio_iov = &iovs;
7499 	uios.uio_iovcnt = 1;
7500 	uios.uio_loffset = 0;
7501 	uios.uio_segflg = UIO_USERSPACE;
7502 	uios.uio_fmode = fmode;
7503 	uios.uio_extflg = UIO_COPY_DEFAULT;
7504 	uios.uio_resid = msgsize;
7505 	uios.uio_offset = 0;
7506 
7507 	/* Ignore flow control in strput for HIPRI */
7508 	if (flag & MSG_HIPRI)
7509 		flag |= MSG_IGNFLOW;
7510 
7511 	for (;;) {
7512 		int done = 0;
7513 
7514 		/*
7515 		 * strput will always free the ctl mblk - even when strput
7516 		 * fails.
7517 		 */
7518 		if ((error = strmakectl(mctl, flag, fmode, &mp)) != 0) {
7519 			TRACE_3(TR_FAC_STREAMS_FR, TR_STRPUTMSG_OUT,
7520 				"strputmsg out:stp %p out %d error %d",
7521 				stp, 1, error);
7522 			return (error);
7523 		}
7524 		/*
7525 		 * Verify that the whole message can be transferred by
7526 		 * strput.
7527 		 */
7528 		ASSERT(stp->sd_maxblk == INFPSZ ||
7529 			stp->sd_maxblk >= mdata->len);
7530 
7531 		msgsize = mdata->len;
7532 		error = strput(stp, mp, uiop, &msgsize, 0, pri, flag);
7533 		mdata->len = msgsize;
7534 
7535 		if (error == 0)
7536 			break;
7537 
7538 		if (error != EWOULDBLOCK)
7539 			goto out;
7540 
7541 		mutex_enter(&stp->sd_lock);
7542 		/*
7543 		 * Check for a missed wakeup.
7544 		 * Needed since strput did not hold sd_lock across
7545 		 * the canputnext.
7546 		 */
7547 		if (bcanputnext(wqp, pri)) {
7548 			/* Try again */
7549 			mutex_exit(&stp->sd_lock);
7550 			continue;
7551 		}
7552 		TRACE_2(TR_FAC_STREAMS_FR, TR_STRPUTMSG_WAIT,
7553 			"strputmsg wait:stp %p waits pri %d", stp, pri);
7554 		if (((error = strwaitq(stp, WRITEWAIT, (ssize_t)0, fmode, -1,
7555 		    &done)) != 0) || done) {
7556 			mutex_exit(&stp->sd_lock);
7557 			TRACE_3(TR_FAC_STREAMS_FR, TR_STRPUTMSG_OUT,
7558 				"strputmsg out:q %p out %d error %d",
7559 				stp, 0, error);
7560 			return (error);
7561 		}
7562 		TRACE_1(TR_FAC_STREAMS_FR, TR_STRPUTMSG_WAKE,
7563 			"strputmsg wake:stp %p wakes", stp);
7564 		mutex_exit(&stp->sd_lock);
7565 		if (stp->sd_sidp != NULL && stp->sd_vnode->v_type != VFIFO)
7566 			if (error = straccess(stp, JCWRITE))
7567 				return (error);
7568 	}
7569 out:
7570 	/*
7571 	 * For historic reasons, applications expect EAGAIN
7572 	 * when data mblk could not be allocated. so change
7573 	 * ENOMEM back to EAGAIN
7574 	 */
7575 	if (error == ENOMEM)
7576 		error = EAGAIN;
7577 	TRACE_3(TR_FAC_STREAMS_FR, TR_STRPUTMSG_OUT,
7578 		"strputmsg out:stp %p out %d error %d", stp, 2, error);
7579 	return (error);
7580 }
7581 
7582 /*
7583  * Put a message downstream.
7584  * Can send only an M_PROTO/M_PCPROTO by passing in a NULL uiop.
7585  * The fmode flag (NDELAY, NONBLOCK) is the or of the flags in the uio
7586  * and the fmode parameter.
7587  *
7588  * This routine handles the consolidation private flags:
7589  *	MSG_IGNERROR	Ignore any stream head error except STPLEX.
7590  *	MSG_HOLDSIG	Hold signals while waiting for data.
7591  *	MSG_IGNFLOW	Don't check streams flow control.
7592  *
7593  * NOTE: strputmsg and kstrputmsg have much of the logic in common.
7594  */
7595 int
7596 kstrputmsg(
7597 	struct vnode *vp,
7598 	mblk_t *mctl,
7599 	struct uio *uiop,
7600 	ssize_t msgsize,
7601 	unsigned char pri,
7602 	int flag,
7603 	int fmode)
7604 {
7605 	struct stdata *stp;
7606 	queue_t *wqp;
7607 	ssize_t rmin, rmax;
7608 	int error;
7609 
7610 	ASSERT(vp->v_stream);
7611 	stp = vp->v_stream;
7612 	wqp = stp->sd_wrq;
7613 #ifdef C2_AUDIT
7614 	if (audit_active)
7615 		audit_strputmsg(vp, NULL, NULL, pri, flag, fmode);
7616 #endif
7617 	if (mctl == NULL)
7618 		return (EINVAL);
7619 
7620 	if (stp->sd_sidp != NULL && stp->sd_vnode->v_type != VFIFO) {
7621 		if (error = straccess(stp, JCWRITE)) {
7622 			freemsg(mctl);
7623 			return (error);
7624 		}
7625 	}
7626 
7627 	if ((stp->sd_flag & STPLEX) || !(flag & MSG_IGNERROR)) {
7628 		if (stp->sd_flag & (STWRERR|STRHUP|STPLEX)) {
7629 			mutex_enter(&stp->sd_lock);
7630 			error = strwriteable(stp, B_FALSE, B_TRUE);
7631 			mutex_exit(&stp->sd_lock);
7632 			if (error != 0) {
7633 				freemsg(mctl);
7634 				return (error);
7635 			}
7636 		}
7637 	}
7638 
7639 	/*
7640 	 * Check for legal flag value.
7641 	 */
7642 	switch (flag & (MSG_HIPRI|MSG_BAND|MSG_ANY)) {
7643 	case MSG_HIPRI:
7644 		if (pri != 0) {
7645 			freemsg(mctl);
7646 			return (EINVAL);
7647 		}
7648 		break;
7649 	case MSG_BAND:
7650 		break;
7651 	default:
7652 		freemsg(mctl);
7653 		return (EINVAL);
7654 	}
7655 
7656 	TRACE_1(TR_FAC_STREAMS_FR, TR_KSTRPUTMSG_IN,
7657 		"kstrputmsg in:stp %p", stp);
7658 
7659 	/* get these values from those cached in the stream head */
7660 	rmin = stp->sd_qn_minpsz;
7661 	rmax = stp->sd_qn_maxpsz;
7662 
7663 	/*
7664 	 * Make sure ctl and data sizes together fall within the
7665 	 * limits of the max and min receive packet sizes and do
7666 	 * not exceed system limit.
7667 	 */
7668 	ASSERT((rmax >= 0) || (rmax == INFPSZ));
7669 	if (rmax == 0) {
7670 		freemsg(mctl);
7671 		return (ERANGE);
7672 	}
7673 	/*
7674 	 * Use the MAXIMUM of sd_maxblk and q_maxpsz.
7675 	 * Needed to prevent partial failures in the strmakedata loop.
7676 	 */
7677 	if (stp->sd_maxblk != INFPSZ && rmax != INFPSZ && rmax < stp->sd_maxblk)
7678 		rmax = stp->sd_maxblk;
7679 
7680 	if (uiop == NULL) {
7681 		msgsize = -1;
7682 		rmin = -1;	/* no range check for NULL data part */
7683 	} else {
7684 		/* Use uio flags as well as the fmode parameter flags */
7685 		fmode |= uiop->uio_fmode;
7686 
7687 		if ((msgsize < rmin) ||
7688 		    ((msgsize > rmax) && (rmax != INFPSZ))) {
7689 			freemsg(mctl);
7690 			return (ERANGE);
7691 		}
7692 	}
7693 
7694 	/* Ignore flow control in strput for HIPRI */
7695 	if (flag & MSG_HIPRI)
7696 		flag |= MSG_IGNFLOW;
7697 
7698 	for (;;) {
7699 		int done = 0;
7700 		int waitflag;
7701 		mblk_t *mp;
7702 
7703 		/*
7704 		 * strput will always free the ctl mblk - even when strput
7705 		 * fails. If MSG_IGNFLOW is set then any error returned
7706 		 * will cause us to break the loop, so we don't need a copy
7707 		 * of the message. If MSG_IGNFLOW is not set, then we can
7708 		 * get hit by flow control and be forced to try again. In
7709 		 * this case we need to have a copy of the message. We
7710 		 * do this using copymsg since the message may get modified
7711 		 * by something below us.
7712 		 *
7713 		 * We've observed that many TPI providers do not check db_ref
7714 		 * on the control messages but blindly reuse them for the
7715 		 * T_OK_ACK/T_ERROR_ACK. Thus using copymsg is more
7716 		 * friendly to such providers than using dupmsg. Also, note
7717 		 * that sockfs uses MSG_IGNFLOW for all TPI control messages.
7718 		 * Only data messages are subject to flow control, hence
7719 		 * subject to this copymsg.
7720 		 */
7721 		if (flag & MSG_IGNFLOW) {
7722 			mp = mctl;
7723 			mctl = NULL;
7724 		} else {
7725 			do {
7726 				/*
7727 				 * If a message has a free pointer, the message
7728 				 * must be dupmsg to maintain this pointer.
7729 				 * Code using this facility must be sure
7730 				 * that modules below will not change the
7731 				 * contents of the dblk without checking db_ref
7732 				 * first. If db_ref is > 1, then the module
7733 				 * needs to do a copymsg first. Otherwise,
7734 				 * the contents of the dblk may become
7735 				 * inconsistent because the freesmg/freeb below
7736 				 * may end up calling atomic_add_32_nv.
7737 				 * The atomic_add_32_nv in freeb (accessing
7738 				 * all of db_ref, db_type, db_flags, and
7739 				 * db_struioflag) does not prevent other threads
7740 				 * from concurrently trying to modify e.g.
7741 				 * db_type.
7742 				 */
7743 				if (mctl->b_datap->db_frtnp != NULL)
7744 					mp = dupmsg(mctl);
7745 				else
7746 					mp = copymsg(mctl);
7747 
7748 				if (mp != NULL)
7749 					break;
7750 
7751 				error = strwaitbuf(msgdsize(mctl), BPRI_MED);
7752 				if (error) {
7753 					freemsg(mctl);
7754 					return (error);
7755 				}
7756 			} while (mp == NULL);
7757 		}
7758 		/*
7759 		 * Verify that all of msgsize can be transferred by
7760 		 * strput.
7761 		 */
7762 		ASSERT(stp->sd_maxblk == INFPSZ || stp->sd_maxblk >= msgsize);
7763 		error = strput(stp, mp, uiop, &msgsize, 0, pri, flag);
7764 		if (error == 0)
7765 			break;
7766 
7767 		if (error != EWOULDBLOCK)
7768 			goto out;
7769 
7770 		/*
7771 		 * IF MSG_IGNFLOW is set we should have broken out of loop
7772 		 * above.
7773 		 */
7774 		ASSERT(!(flag & MSG_IGNFLOW));
7775 		mutex_enter(&stp->sd_lock);
7776 		/*
7777 		 * Check for a missed wakeup.
7778 		 * Needed since strput did not hold sd_lock across
7779 		 * the canputnext.
7780 		 */
7781 		if (bcanputnext(wqp, pri)) {
7782 			/* Try again */
7783 			mutex_exit(&stp->sd_lock);
7784 			continue;
7785 		}
7786 		TRACE_2(TR_FAC_STREAMS_FR, TR_KSTRPUTMSG_WAIT,
7787 			"kstrputmsg wait:stp %p waits pri %d", stp, pri);
7788 
7789 		waitflag = WRITEWAIT;
7790 		if (flag & (MSG_HOLDSIG|MSG_IGNERROR)) {
7791 			if (flag & MSG_HOLDSIG)
7792 				waitflag |= STR_NOSIG;
7793 			if (flag & MSG_IGNERROR)
7794 				waitflag |= STR_NOERROR;
7795 		}
7796 		if (((error = strwaitq(stp, waitflag,
7797 		    (ssize_t)0, fmode, -1, &done)) != 0) || done) {
7798 			mutex_exit(&stp->sd_lock);
7799 			TRACE_3(TR_FAC_STREAMS_FR, TR_KSTRPUTMSG_OUT,
7800 				"kstrputmsg out:stp %p out %d error %d",
7801 				stp, 0, error);
7802 			freemsg(mctl);
7803 			return (error);
7804 		}
7805 		TRACE_1(TR_FAC_STREAMS_FR, TR_KSTRPUTMSG_WAKE,
7806 			"kstrputmsg wake:stp %p wakes", stp);
7807 		mutex_exit(&stp->sd_lock);
7808 		if (stp->sd_sidp != NULL && stp->sd_vnode->v_type != VFIFO) {
7809 			if (error = straccess(stp, JCWRITE)) {
7810 				freemsg(mctl);
7811 				return (error);
7812 			}
7813 		}
7814 	}
7815 out:
7816 	freemsg(mctl);
7817 	/*
7818 	 * For historic reasons, applications expect EAGAIN
7819 	 * when data mblk could not be allocated. so change
7820 	 * ENOMEM back to EAGAIN
7821 	 */
7822 	if (error == ENOMEM)
7823 		error = EAGAIN;
7824 	TRACE_3(TR_FAC_STREAMS_FR, TR_KSTRPUTMSG_OUT,
7825 		"kstrputmsg out:stp %p out %d error %d", stp, 2, error);
7826 	return (error);
7827 }
7828 
7829 /*
7830  * Determines whether the necessary conditions are set on a stream
7831  * for it to be readable, writeable, or have exceptions.
7832  *
7833  * strpoll handles the consolidation private events:
7834  *	POLLNOERR	Do not return POLLERR even if there are stream
7835  *			head errors.
7836  *			Used by sockfs.
7837  *	POLLRDDATA	Do not return POLLIN unless at least one message on
7838  *			the queue contains one or more M_DATA mblks. Thus
7839  *			when this flag is set a queue with only
7840  *			M_PROTO/M_PCPROTO mblks does not return POLLIN.
7841  *			Used by sockfs to ignore T_EXDATA_IND messages.
7842  *
7843  * Note: POLLRDDATA assumes that synch streams only return messages with
7844  * an M_DATA attached (i.e. not messages consisting of only
7845  * an M_PROTO/M_PCPROTO part).
7846  */
7847 int
7848 strpoll(
7849 	struct stdata *stp,
7850 	short events_arg,
7851 	int anyyet,
7852 	short *reventsp,
7853 	struct pollhead **phpp)
7854 {
7855 	int events = (ushort_t)events_arg;
7856 	int retevents = 0;
7857 	mblk_t *mp;
7858 	qband_t *qbp;
7859 	long sd_flags = stp->sd_flag;
7860 	int headlocked = 0;
7861 
7862 	/*
7863 	 * For performance, a single 'if' tests for most possible edge
7864 	 * conditions in one shot
7865 	 */
7866 	if (sd_flags & (STPLEX | STRDERR | STWRERR)) {
7867 		if (sd_flags & STPLEX) {
7868 			*reventsp = POLLNVAL;
7869 			return (EINVAL);
7870 		}
7871 		if (((events & (POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI)) &&
7872 		    (sd_flags & STRDERR)) ||
7873 		    ((events & (POLLOUT | POLLWRNORM | POLLWRBAND)) &&
7874 		    (sd_flags & STWRERR))) {
7875 			if (!(events & POLLNOERR)) {
7876 				*reventsp = POLLERR;
7877 				return (0);
7878 			}
7879 		}
7880 	}
7881 	if (sd_flags & STRHUP) {
7882 		retevents |= POLLHUP;
7883 	} else if (events & (POLLWRNORM | POLLWRBAND)) {
7884 		queue_t *tq;
7885 		queue_t	*qp = stp->sd_wrq;
7886 
7887 		claimstr(qp);
7888 		/* Find next module forward that has a service procedure */
7889 		tq = qp->q_next->q_nfsrv;
7890 		ASSERT(tq != NULL);
7891 
7892 		polllock(&stp->sd_pollist, QLOCK(tq));
7893 		if (events & POLLWRNORM) {
7894 			queue_t *sqp;
7895 
7896 			if (tq->q_flag & QFULL)
7897 				/* ensure backq svc procedure runs */
7898 				tq->q_flag |= QWANTW;
7899 			else if ((sqp = stp->sd_struiowrq) != NULL) {
7900 				/* Check sync stream barrier write q */
7901 				mutex_exit(QLOCK(tq));
7902 				polllock(&stp->sd_pollist, QLOCK(sqp));
7903 				if (sqp->q_flag & QFULL)
7904 					/* ensure pollwakeup() is done */
7905 					sqp->q_flag |= QWANTWSYNC;
7906 				else
7907 					retevents |= POLLOUT;
7908 				/* More write events to process ??? */
7909 				if (! (events & POLLWRBAND)) {
7910 					mutex_exit(QLOCK(sqp));
7911 					releasestr(qp);
7912 					goto chkrd;
7913 				}
7914 				mutex_exit(QLOCK(sqp));
7915 				polllock(&stp->sd_pollist, QLOCK(tq));
7916 			} else
7917 				retevents |= POLLOUT;
7918 		}
7919 		if (events & POLLWRBAND) {
7920 			qbp = tq->q_bandp;
7921 			if (qbp) {
7922 				while (qbp) {
7923 					if (qbp->qb_flag & QB_FULL)
7924 						qbp->qb_flag |= QB_WANTW;
7925 					else
7926 						retevents |= POLLWRBAND;
7927 					qbp = qbp->qb_next;
7928 				}
7929 			} else {
7930 				retevents |= POLLWRBAND;
7931 			}
7932 		}
7933 		mutex_exit(QLOCK(tq));
7934 		releasestr(qp);
7935 	}
7936 chkrd:
7937 	if (sd_flags & STRPRI) {
7938 		retevents |= (events & POLLPRI);
7939 	} else if (events & (POLLRDNORM | POLLRDBAND | POLLIN)) {
7940 		queue_t	*qp = _RD(stp->sd_wrq);
7941 		int normevents = (events & (POLLIN | POLLRDNORM));
7942 
7943 		/*
7944 		 * Note: Need to do polllock() here since ps_lock may be
7945 		 * held. See bug 4191544.
7946 		 */
7947 		polllock(&stp->sd_pollist, &stp->sd_lock);
7948 		headlocked = 1;
7949 		mp = qp->q_first;
7950 		while (mp) {
7951 			/*
7952 			 * For POLLRDDATA we scan b_cont and b_next until we
7953 			 * find an M_DATA.
7954 			 */
7955 			if ((events & POLLRDDATA) &&
7956 			    mp->b_datap->db_type != M_DATA) {
7957 				mblk_t *nmp = mp->b_cont;
7958 
7959 				while (nmp != NULL &&
7960 				    nmp->b_datap->db_type != M_DATA)
7961 					nmp = nmp->b_cont;
7962 				if (nmp == NULL) {
7963 					mp = mp->b_next;
7964 					continue;
7965 				}
7966 			}
7967 			if (mp->b_band == 0)
7968 				retevents |= normevents;
7969 			else
7970 				retevents |= (events & (POLLIN | POLLRDBAND));
7971 			break;
7972 		}
7973 		if (! (retevents & normevents) &&
7974 		    (stp->sd_wakeq & RSLEEP)) {
7975 			/*
7976 			 * Sync stream barrier read queue has data.
7977 			 */
7978 			retevents |= normevents;
7979 		}
7980 		/* Treat eof as normal data */
7981 		if (sd_flags & STREOF)
7982 			retevents |= normevents;
7983 	}
7984 
7985 	*reventsp = (short)retevents;
7986 	if (retevents) {
7987 		if (headlocked)
7988 			mutex_exit(&stp->sd_lock);
7989 		return (0);
7990 	}
7991 
7992 	/*
7993 	 * If poll() has not found any events yet, set up event cell
7994 	 * to wake up the poll if a requested event occurs on this
7995 	 * stream.  Check for collisions with outstanding poll requests.
7996 	 */
7997 	if (!anyyet) {
7998 		*phpp = &stp->sd_pollist;
7999 		if (headlocked == 0) {
8000 			polllock(&stp->sd_pollist, &stp->sd_lock);
8001 			headlocked = 1;
8002 		}
8003 		stp->sd_rput_opt |= SR_POLLIN;
8004 	}
8005 	if (headlocked)
8006 		mutex_exit(&stp->sd_lock);
8007 	return (0);
8008 }
8009 
8010 /*
8011  * The purpose of putback() is to assure sleeping polls/reads
8012  * are awakened when there are no new messages arriving at the,
8013  * stream head, and a message is placed back on the read queue.
8014  *
8015  * sd_lock must be held when messages are placed back on stream
8016  * head.  (getq() holds sd_lock when it removes messages from
8017  * the queue)
8018  */
8019 
8020 static void
8021 putback(struct stdata *stp, queue_t *q, mblk_t *bp, int band)
8022 {
8023 	ASSERT(MUTEX_HELD(&stp->sd_lock));
8024 	(void) putbq(q, bp);
8025 	/*
8026 	 * A message may have come in when the sd_lock was dropped in the
8027 	 * calling routine. If this is the case and STR*ATMARK info was
8028 	 * received, need to move that from the stream head to the q_last
8029 	 * so that SIOCATMARK can return the proper value.
8030 	 */
8031 	if (stp->sd_flag & (STRATMARK | STRNOTATMARK)) {
8032 		unsigned short *flagp = &q->q_last->b_flag;
8033 		uint_t b_flag = (uint_t)*flagp;
8034 
8035 		if (stp->sd_flag & STRATMARK) {
8036 			b_flag &= ~MSGNOTMARKNEXT;
8037 			b_flag |= MSGMARKNEXT;
8038 			stp->sd_flag &= ~STRATMARK;
8039 		} else {
8040 			b_flag &= ~MSGMARKNEXT;
8041 			b_flag |= MSGNOTMARKNEXT;
8042 			stp->sd_flag &= ~STRNOTATMARK;
8043 		}
8044 		*flagp = (unsigned short) b_flag;
8045 	}
8046 
8047 #ifdef	DEBUG
8048 	/*
8049 	 * Make sure that the flags are not messed up.
8050 	 */
8051 	{
8052 		mblk_t *mp;
8053 		mp = q->q_last;
8054 		while (mp != NULL) {
8055 			ASSERT((mp->b_flag & (MSGMARKNEXT|MSGNOTMARKNEXT)) !=
8056 			    (MSGMARKNEXT|MSGNOTMARKNEXT));
8057 			mp = mp->b_cont;
8058 		}
8059 	}
8060 #endif
8061 	if (q->q_first == bp) {
8062 		short pollevents;
8063 
8064 		if (stp->sd_flag & RSLEEP) {
8065 			stp->sd_flag &= ~RSLEEP;
8066 			cv_broadcast(&q->q_wait);
8067 		}
8068 		if (stp->sd_flag & STRPRI) {
8069 			pollevents = POLLPRI;
8070 		} else {
8071 			if (band == 0) {
8072 				if (!(stp->sd_rput_opt & SR_POLLIN))
8073 					return;
8074 				stp->sd_rput_opt &= ~SR_POLLIN;
8075 				pollevents = POLLIN | POLLRDNORM;
8076 			} else {
8077 				pollevents = POLLIN | POLLRDBAND;
8078 			}
8079 		}
8080 		mutex_exit(&stp->sd_lock);
8081 		pollwakeup(&stp->sd_pollist, pollevents);
8082 		mutex_enter(&stp->sd_lock);
8083 	}
8084 }
8085 
8086 /*
8087  * Return the held vnode attached to the stream head of a
8088  * given queue
8089  * It is the responsibility of the calling routine to ensure
8090  * that the queue does not go away (e.g. pop).
8091  */
8092 vnode_t *
8093 strq2vp(queue_t *qp)
8094 {
8095 	vnode_t *vp;
8096 	vp = STREAM(qp)->sd_vnode;
8097 	ASSERT(vp != NULL);
8098 	VN_HOLD(vp);
8099 	return (vp);
8100 }
8101 
8102 /*
8103  * return the stream head write queue for the given vp
8104  * It is the responsibility of the calling routine to ensure
8105  * that the stream or vnode do not close.
8106  */
8107 queue_t *
8108 strvp2wq(vnode_t *vp)
8109 {
8110 	ASSERT(vp->v_stream != NULL);
8111 	return (vp->v_stream->sd_wrq);
8112 }
8113 
8114 /*
8115  * pollwakeup stream head
8116  * It is the responsibility of the calling routine to ensure
8117  * that the stream or vnode do not close.
8118  */
8119 void
8120 strpollwakeup(vnode_t *vp, short event)
8121 {
8122 	ASSERT(vp->v_stream);
8123 	pollwakeup(&vp->v_stream->sd_pollist, event);
8124 }
8125 
8126 /*
8127  * Mate the stream heads of two vnodes together. If the two vnodes are the
8128  * same, we just make the write-side point at the read-side -- otherwise,
8129  * we do a full mate.  Only works on vnodes associated with streams that are
8130  * still being built and thus have only a stream head.
8131  */
8132 void
8133 strmate(vnode_t *vp1, vnode_t *vp2)
8134 {
8135 	queue_t *wrq1 = strvp2wq(vp1);
8136 	queue_t *wrq2 = strvp2wq(vp2);
8137 
8138 	/*
8139 	 * Verify that there are no modules on the stream yet.  We also
8140 	 * rely on the stream head always having a service procedure to
8141 	 * avoid tweaking q_nfsrv.
8142 	 */
8143 	ASSERT(wrq1->q_next == NULL && wrq2->q_next == NULL);
8144 	ASSERT(wrq1->q_qinfo->qi_srvp != NULL);
8145 	ASSERT(wrq2->q_qinfo->qi_srvp != NULL);
8146 
8147 	/*
8148 	 * If the queues are the same, just twist; otherwise do a full mate.
8149 	 */
8150 	if (wrq1 == wrq2) {
8151 		wrq1->q_next = _RD(wrq1);
8152 	} else {
8153 		wrq1->q_next = _RD(wrq2);
8154 		wrq2->q_next = _RD(wrq1);
8155 		STREAM(wrq1)->sd_mate = STREAM(wrq2);
8156 		STREAM(wrq1)->sd_flag |= STRMATE;
8157 		STREAM(wrq2)->sd_mate = STREAM(wrq1);
8158 		STREAM(wrq2)->sd_flag |= STRMATE;
8159 	}
8160 }
8161 
8162 /*
8163  * XXX will go away when console is correctly fixed.
8164  * Clean up the console PIDS, from previous I_SETSIG,
8165  * called only for cnopen which never calls strclean().
8166  */
8167 void
8168 str_cn_clean(struct vnode *vp)
8169 {
8170 	strsig_t *ssp, *pssp, *tssp;
8171 	struct stdata *stp;
8172 	struct pid  *pidp;
8173 	int update = 0;
8174 
8175 	ASSERT(vp->v_stream);
8176 	stp = vp->v_stream;
8177 	pssp = NULL;
8178 	mutex_enter(&stp->sd_lock);
8179 	ssp = stp->sd_siglist;
8180 	while (ssp) {
8181 		mutex_enter(&pidlock);
8182 		pidp = ssp->ss_pidp;
8183 		/*
8184 		 * Get rid of PID if the proc is gone.
8185 		 */
8186 		if (pidp->pid_prinactive) {
8187 			tssp = ssp->ss_next;
8188 			if (pssp)
8189 				pssp->ss_next = tssp;
8190 			else
8191 				stp->sd_siglist = tssp;
8192 			ASSERT(pidp->pid_ref <= 1);
8193 			PID_RELE(ssp->ss_pidp);
8194 			mutex_exit(&pidlock);
8195 			kmem_free(ssp, sizeof (strsig_t));
8196 			update = 1;
8197 			ssp = tssp;
8198 			continue;
8199 		} else
8200 			mutex_exit(&pidlock);
8201 		pssp = ssp;
8202 		ssp = ssp->ss_next;
8203 	}
8204 	if (update) {
8205 		stp->sd_sigflags = 0;
8206 		for (ssp = stp->sd_siglist; ssp; ssp = ssp->ss_next)
8207 			stp->sd_sigflags |= ssp->ss_events;
8208 	}
8209 	mutex_exit(&stp->sd_lock);
8210 }
8211 
8212 /*
8213  * Return B_TRUE if there is data in the message, B_FALSE otherwise.
8214  */
8215 static boolean_t
8216 msghasdata(mblk_t *bp)
8217 {
8218 	for (; bp; bp = bp->b_cont)
8219 		if (bp->b_datap->db_type == M_DATA) {
8220 			ASSERT(bp->b_wptr >= bp->b_rptr);
8221 			if (bp->b_wptr > bp->b_rptr)
8222 				return (B_TRUE);
8223 		}
8224 	return (B_FALSE);
8225 }
8226