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