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