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