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