xref: /titanic_41/usr/src/uts/common/fs/fifofs/fifovnops.c (revision ba2e4443695ee6a6f420a35cd4fc3d3346d22932)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
22 /*	  All rights reserved.  	*/
23 
24 
25 /*
26  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
27  * Use is subject to license terms.
28  */
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 /*
33  * FIFOFS file system vnode operations.  This file system
34  * type supports STREAMS-based pipes and FIFOs.
35  */
36 #include <sys/types.h>
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/sysmacros.h>
40 #include <sys/cred.h>
41 #include <sys/errno.h>
42 #include <sys/time.h>
43 #include <sys/file.h>
44 #include <sys/fcntl.h>
45 #include <sys/kmem.h>
46 #include <sys/uio.h>
47 #include <sys/vfs.h>
48 #include <sys/vnode.h>
49 #include <sys/pathname.h>
50 #include <sys/signal.h>
51 #include <sys/user.h>
52 #include <sys/strsubr.h>
53 #include <sys/stream.h>
54 #include <sys/strsun.h>
55 #include <sys/strredir.h>
56 #include <sys/fs/fifonode.h>
57 #include <sys/fs/namenode.h>
58 #include <sys/stropts.h>
59 #include <sys/proc.h>
60 #include <sys/unistd.h>
61 #include <sys/debug.h>
62 #include <fs/fs_subr.h>
63 #include <sys/filio.h>
64 #include <sys/termio.h>
65 #include <sys/ddi.h>
66 #include <sys/vtrace.h>
67 #include <sys/policy.h>
68 #include <sys/tsol/label.h>
69 
70 /*
71  * Define the routines/data structures used in this file.
72  */
73 static int fifo_read(vnode_t *, uio_t *, int, cred_t *, caller_context_t *);
74 static int fifo_write(vnode_t *, uio_t *, int, cred_t *, caller_context_t *);
75 static int fifo_getattr(vnode_t *, vattr_t *, int, cred_t *);
76 static int fifo_setattr(vnode_t *, vattr_t *, int, cred_t *,
77 	caller_context_t *);
78 static int fifo_realvp(vnode_t *, vnode_t **);
79 static int fifo_access(vnode_t *, int, int, cred_t *);
80 static int fifo_fid(vnode_t *, fid_t *);
81 static int fifo_fsync(vnode_t *, int, cred_t *);
82 static int fifo_seek(vnode_t *, offset_t, offset_t *);
83 static int fifo_ioctl(vnode_t *, int, intptr_t, int, cred_t *, int *);
84 static int fifo_fastioctl(vnode_t *, int, intptr_t, int, cred_t *, int *);
85 static int fifo_strioctl(vnode_t *, int, intptr_t, int, cred_t *, int *);
86 static int fifo_poll(vnode_t *, short, int, short *, pollhead_t **);
87 static int fifo_pathconf(vnode_t *, int, ulong_t *, cred_t *);
88 static void fifo_inactive(vnode_t *, cred_t *);
89 static int fifo_rwlock(vnode_t *, int, caller_context_t *);
90 static void fifo_rwunlock(vnode_t *, int, caller_context_t *);
91 static int fifo_setsecattr(struct vnode *, vsecattr_t *, int, struct cred *);
92 static int fifo_getsecattr(struct vnode *, vsecattr_t *, int, struct cred *);
93 
94 /* functions local to this file */
95 static boolean_t fifo_stayfast_enter(fifonode_t *);
96 static void fifo_stayfast_exit(fifonode_t *);
97 
98 /*
99  * Define the data structures external to this file.
100  */
101 extern	dev_t	fifodev;
102 extern struct qinit fifo_stwdata;
103 extern struct qinit fifo_strdata;
104 extern kmutex_t ftable_lock;
105 
106 struct  streamtab fifoinfo = { &fifo_strdata, &fifo_stwdata, NULL, NULL };
107 
108 struct vnodeops *fifo_vnodeops;
109 
110 const fs_operation_def_t fifo_vnodeops_template[] = {
111 	VOPNAME_OPEN, fifo_open,
112 	VOPNAME_CLOSE, fifo_close,
113 	VOPNAME_READ, fifo_read,
114 	VOPNAME_WRITE, fifo_write,
115 	VOPNAME_IOCTL, fifo_ioctl,
116 	VOPNAME_GETATTR, fifo_getattr,
117 	VOPNAME_SETATTR, fifo_setattr,
118 	VOPNAME_ACCESS, fifo_access,
119 	VOPNAME_FSYNC, fifo_fsync,
120 	VOPNAME_INACTIVE, (fs_generic_func_p) fifo_inactive,
121 	VOPNAME_FID, fifo_fid,
122 	VOPNAME_RWLOCK, fifo_rwlock,
123 	VOPNAME_RWUNLOCK, (fs_generic_func_p) fifo_rwunlock,
124 	VOPNAME_SEEK, fifo_seek,
125 	VOPNAME_REALVP, fifo_realvp,
126 	VOPNAME_POLL, (fs_generic_func_p) fifo_poll,
127 	VOPNAME_PATHCONF, fifo_pathconf,
128 	VOPNAME_DISPOSE, fs_error,
129 	VOPNAME_SETSECATTR, fifo_setsecattr,
130 	VOPNAME_GETSECATTR, fifo_getsecattr,
131 	NULL, NULL
132 };
133 
134 /*
135  * Return the fifoinfo structure.
136  */
137 struct streamtab *
138 fifo_getinfo()
139 {
140 	return (&fifoinfo);
141 }
142 
143 /*
144  * Trusted Extensions enforces a restrictive policy for
145  * writing via cross-zone named pipes. A privileged global
146  * zone process may expose a named pipe by loopback mounting
147  * it from a lower-level zone to a higher-level zone. The
148  * kernel-enforced mount policy for lofs mounts ensures
149  * that such mounts are read-only in the higher-level
150  * zone. But this is not sufficient to prevent writing
151  * down via fifos.  This function prevents writing down
152  * by comparing the zone of the process which is requesting
153  * write access with the zone owning the named pipe rendezvous.
154  * For write access the zone of the named pipe must equal the
155  * zone of the writing process. Writing up is possible since
156  * the named pipe can be opened for read by a process in a
157  * higher level zone.
158  *
159  * An exception is made for the global zone to support trusted
160  * processes which enforce their own data flow policies.
161  */
162 static boolean_t
163 tsol_fifo_access(vnode_t *vp, int flag, cred_t *crp)
164 {
165 	fifonode_t	*fnp = VTOF(vp);
166 
167 	if (is_system_labeled() &&
168 	    (flag & FWRITE) &&
169 	    (!(fnp->fn_flag & ISPIPE))) {
170 		zone_t	*proc_zone;
171 
172 		proc_zone = crgetzone(crp);
173 		if (proc_zone != global_zone) {
174 			char		vpath[MAXPATHLEN];
175 			zone_t		*fifo_zone;
176 
177 			/*
178 			 * Get the pathname and use it to find
179 			 * the zone of the fifo.
180 			 */
181 			if (vnodetopath(rootdir, vp, vpath, sizeof (vpath),
182 			    kcred) == 0) {
183 				fifo_zone = zone_find_by_path(vpath);
184 				zone_rele(fifo_zone);
185 
186 				if (fifo_zone != global_zone &&
187 				    fifo_zone != proc_zone) {
188 					return (B_FALSE);
189 				}
190 			} else {
191 				return (B_FALSE);
192 			}
193 		}
194 	}
195 	return (B_TRUE);
196 }
197 
198 /*
199  * Open and stream a FIFO.
200  * If this is the first open of the file (FIFO is not streaming),
201  * initialize the fifonode and attach a stream to the vnode.
202  *
203  * Each end of a fifo must be synchronized with the other end.
204  * If not, the mated end may complete an open, I/O, close sequence
205  * before the end waiting in open ever wakes up.
206  * Note: namefs pipes come through this routine too.
207  */
208 int
209 fifo_open(vnode_t **vpp, int flag, cred_t *crp)
210 {
211 	vnode_t		*vp		= *vpp;
212 	fifonode_t	*fnp		= VTOF(vp);
213 	fifolock_t	*fn_lock	= fnp->fn_lock;
214 	int		error;
215 
216 	ASSERT(vp->v_type == VFIFO);
217 	ASSERT(vn_matchops(vp, fifo_vnodeops));
218 
219 	if (!tsol_fifo_access(vp, flag, crp))
220 		return (EACCES);
221 
222 	mutex_enter(&fn_lock->flk_lock);
223 	/*
224 	 * If we are the first reader, wake up any writers that
225 	 * may be waiting around.  wait for all of them to
226 	 * wake up before proceeding (i.e. fn_wsynccnt == 0)
227 	 */
228 	if (flag & FREAD) {
229 		fnp->fn_rcnt++;		/* record reader present */
230 		if (! (fnp->fn_flag & ISPIPE))
231 			fnp->fn_rsynccnt++;	/* record reader in open */
232 	}
233 
234 	/*
235 	 * If we are the first writer, wake up any readers that
236 	 * may be waiting around.  wait for all of them to
237 	 * wake up before proceeding (i.e. fn_rsynccnt == 0)
238 	 */
239 	if (flag & FWRITE) {
240 		fnp->fn_wcnt++;		/* record writer present */
241 		if (! (fnp->fn_flag & ISPIPE))
242 			fnp->fn_wsynccnt++;	/* record writer in open */
243 	}
244 	/*
245 	 * fifo_stropen will take care of twisting the queues on the first
246 	 * open.  The 1 being passed in means twist the queues on the first
247 	 * open.
248 	 */
249 	error = fifo_stropen(vpp, flag, crp, 1, 1);
250 	/*
251 	 * fifo_stropen() could have replaced vpp
252 	 * since fifo's are the only thing we need to sync up,
253 	 * everything else just returns;
254 	 * Note: don't need to hold lock since ISPIPE can't change
255 	 * and both old and new vp need to be pipes
256 	 */
257 	ASSERT(MUTEX_HELD(&VTOF(*vpp)->fn_lock->flk_lock));
258 	if (fnp->fn_flag & ISPIPE) {
259 		ASSERT(VTOF(*vpp)->fn_flag & ISPIPE);
260 		ASSERT(VTOF(*vpp)->fn_rsynccnt == 0);
261 		ASSERT(VTOF(*vpp)->fn_rsynccnt == 0);
262 		/*
263 		 * XXX note: should probably hold locks, but
264 		 * These values should not be changing
265 		 */
266 		ASSERT(fnp->fn_rsynccnt == 0);
267 		ASSERT(fnp->fn_wsynccnt == 0);
268 		mutex_exit(&VTOF(*vpp)->fn_lock->flk_lock);
269 		return (error);
270 	}
271 	/*
272 	 * vp can't change for FIFOS
273 	 */
274 	ASSERT(vp == *vpp);
275 	/*
276 	 * If we are opening for read (or writer)
277 	 *   indicate that the reader (or writer) is done with open
278 	 *   if there is a writer (or reader) waiting for us, wake them up
279 	 *	and indicate that at least 1 read (or write) open has occured
280 	 *	this is need in the event the read (or write) side closes
281 	 *	before the writer (or reader) has a chance to wake up
282 	 *	i.e. it sees that a reader (or writer) was once there
283 	 */
284 	if (flag & FREAD) {
285 		fnp->fn_rsynccnt--;	/* reader done with open */
286 		if (fnp->fn_flag & FIFOSYNC) {
287 			/*
288 			 * This indicates that a read open has occured
289 			 * Only need to set if writer is actually asleep
290 			 * Flag will be consumed by writer.
291 			 */
292 			fnp->fn_flag |= FIFOROCR;
293 			cv_broadcast(&fnp->fn_wait_cv);
294 		}
295 	}
296 	if (flag & FWRITE) {
297 		fnp->fn_wsynccnt--;	/* writer done with open */
298 		if (fnp->fn_flag & FIFOSYNC) {
299 			/*
300 			 * This indicates that a write open has occured
301 			 * Only need to set if reader is actually asleep
302 			 * Flag will be consumed by reader.
303 			 */
304 			fnp->fn_flag |= FIFOWOCR;
305 			cv_broadcast(&fnp->fn_wait_cv);
306 		}
307 	}
308 
309 	fnp->fn_flag &= ~FIFOSYNC;
310 
311 	/*
312 	 * errors don't wait around.. just return
313 	 * Note: XXX other end will wake up and continue despite error.
314 	 * There is no defined semantic on the correct course of option
315 	 * so we do what we've done in the past
316 	 */
317 	if (error != 0) {
318 		mutex_exit(&fnp->fn_lock->flk_lock);
319 		goto done;
320 	}
321 	ASSERT(fnp->fn_rsynccnt <= fnp->fn_rcnt);
322 	ASSERT(fnp->fn_wsynccnt <= fnp->fn_wcnt);
323 	/*
324 	 * FIFOWOCR (or FIFOROCR) indicates that the writer (or reader)
325 	 * has woken us up and is done with open (this way, if the other
326 	 * end has made it to close, we don't block forever in open)
327 	 * fn_wnct == fn_wsynccnt (or fn_rcnt == fn_rsynccnt) indicates
328 	 * that no writer (or reader) has yet made it through open
329 	 * This has the side benifit of that the first
330 	 * reader (or writer) will wait until the other end finishes open
331 	 */
332 	if (flag & FREAD) {
333 		while ((fnp->fn_flag & FIFOWOCR) == 0 &&
334 		    fnp->fn_wcnt == fnp->fn_wsynccnt) {
335 			if (flag & (FNDELAY|FNONBLOCK)) {
336 				mutex_exit(&fnp->fn_lock->flk_lock);
337 				goto done;
338 			}
339 			fnp->fn_insync++;
340 			fnp->fn_flag |= FIFOSYNC;
341 			if (!cv_wait_sig_swap(&fnp->fn_wait_cv,
342 			    &fnp->fn_lock->flk_lock)) {
343 				/*
344 				 * Last reader to wakeup clear writer
345 				 * Clear both writer and reader open
346 				 * occured flag incase other end is O_RDWR
347 				 */
348 				if (--fnp->fn_insync == 0 &&
349 				    fnp->fn_flag & FIFOWOCR) {
350 					fnp->fn_flag &= ~(FIFOWOCR|FIFOROCR);
351 				}
352 				mutex_exit(&fnp->fn_lock->flk_lock);
353 				(void) fifo_close(*vpp, flag, 1, 0, crp);
354 				error = EINTR;
355 				goto done;
356 			}
357 			/*
358 			 * Last reader to wakeup clear writer open occured flag
359 			 * Clear both writer and reader open occured flag
360 			 * incase other end is O_RDWR
361 			 */
362 			if (--fnp->fn_insync == 0 &&
363 			    fnp->fn_flag & FIFOWOCR) {
364 				fnp->fn_flag &= ~(FIFOWOCR|FIFOROCR);
365 				break;
366 			}
367 		}
368 	} else if (flag & FWRITE) {
369 		while ((fnp->fn_flag & FIFOROCR) == 0 &&
370 		    fnp->fn_rcnt == fnp->fn_rsynccnt) {
371 			if ((flag & (FNDELAY|FNONBLOCK)) && fnp->fn_rcnt == 0) {
372 				mutex_exit(&fnp->fn_lock->flk_lock);
373 				(void) fifo_close(*vpp, flag, 1, 0, crp);
374 				error = ENXIO;
375 				goto done;
376 			}
377 			fnp->fn_flag |= FIFOSYNC;
378 			fnp->fn_insync++;
379 			if (!cv_wait_sig_swap(&fnp->fn_wait_cv,
380 			    &fnp->fn_lock->flk_lock)) {
381 				/*
382 				 * Last writer to wakeup clear
383 				 * Clear both writer and reader open
384 				 * occured flag in case other end is O_RDWR
385 				 */
386 				if (--fnp->fn_insync == 0 &&
387 				    (fnp->fn_flag & FIFOROCR) != 0) {
388 					fnp->fn_flag &= ~(FIFOWOCR|FIFOROCR);
389 				}
390 				mutex_exit(&fnp->fn_lock->flk_lock);
391 				(void) fifo_close(*vpp, flag, 1, 0, crp);
392 				error = EINTR;
393 				goto done;
394 			}
395 			/*
396 			 * Last writer to wakeup clear reader open occured flag
397 			 * Clear both writer and reader open
398 			 * occured flag in case other end is O_RDWR
399 			 */
400 			if (--fnp->fn_insync == 0 &&
401 			    (fnp->fn_flag & FIFOROCR) != 0) {
402 				fnp->fn_flag &= ~(FIFOWOCR|FIFOROCR);
403 				break;
404 			}
405 		}
406 	}
407 	mutex_exit(&fn_lock->flk_lock);
408 done:
409 	return (error);
410 }
411 
412 /*
413  * Close down a stream.
414  * Call cleanlocks() and strclean() on every close.
415  * For last close send hangup message and force
416  * the other end of a named pipe to be unmounted.
417  * Mount guarantees that the mounted end will only call fifo_close()
418  * with a count of 1 when the unmount occurs.
419  * This routine will close down one end of a pipe or FIFO
420  * and free the stream head via strclose()
421  */
422 /*ARGSUSED*/
423 int
424 fifo_close(vnode_t *vp, int flag, int count, offset_t offset, cred_t *crp)
425 {
426 	fifonode_t	*fnp		= VTOF(vp);
427 	fifonode_t	*fn_dest	= fnp->fn_dest;
428 	int		error		= 0;
429 	fifolock_t	*fn_lock	= fnp->fn_lock;
430 	queue_t		*sd_wrq;
431 	vnode_t		*fn_dest_vp;
432 	int		senthang = 0;
433 
434 	ASSERT(vp->v_stream != NULL);
435 	/*
436 	 * clean locks and clear events.
437 	 */
438 	(void) cleanlocks(vp, ttoproc(curthread)->p_pid, 0);
439 	cleanshares(vp, ttoproc(curthread)->p_pid);
440 	strclean(vp);
441 
442 	/*
443 	 * If a file still has the pipe/FIFO open, return.
444 	 */
445 	if (count > 1)
446 		return (0);
447 
448 
449 	sd_wrq = strvp2wq(vp);
450 	mutex_enter(&fn_lock->flk_lock);
451 
452 	/*
453 	 * wait for pending opens to finish up
454 	 * note: this also has the side effect of single threading closes
455 	 */
456 	while (fn_lock->flk_ocsync)
457 		cv_wait(&fn_lock->flk_wait_cv, &fn_lock->flk_lock);
458 
459 	fn_lock->flk_ocsync = 1;
460 
461 	if (flag & FREAD) {
462 		fnp->fn_rcnt--;
463 	}
464 	/*
465 	 * If we are last writer wake up sleeping readers
466 	 * (They'll figure out that there are no more writers
467 	 * and do the right thing)
468 	 * send hangup down stream so that stream head will do the
469 	 * right thing.
470 	 */
471 	if (flag & FWRITE) {
472 		if (--fnp->fn_wcnt == 0 && fn_dest->fn_rcnt > 0) {
473 			if ((fn_dest->fn_flag & (FIFOFAST | FIFOWANTR)) ==
474 			    (FIFOFAST | FIFOWANTR)) {
475 				/*
476 				 * While we're at it, clear FIFOWANTW too
477 				 * Wake up any sleeping readers or
478 				 * writers.
479 				 */
480 				fn_dest->fn_flag &= ~(FIFOWANTR | FIFOWANTW);
481 				cv_broadcast(&fn_dest->fn_wait_cv);
482 			}
483 			/*
484 			 * This is needed incase the other side
485 			 * was opened non-blocking.  It is the
486 			 * only way we can tell that wcnt is 0 because
487 			 * of close instead of never having a writer
488 			 */
489 			if (!(fnp->fn_flag & ISPIPE))
490 				fnp->fn_flag |= FIFOCLOSE;
491 			/*
492 			 * Note: sending hangup effectively shuts down
493 			 * both reader and writer at other end.
494 			 */
495 			(void) putnextctl_wait(sd_wrq, M_HANGUP);
496 			senthang = 1;
497 		}
498 	}
499 
500 	/*
501 	 * For FIFOs we need to indicate to stream head that last reader
502 	 * has gone away so that an error is generated
503 	 * Pipes just need to wake up the other end so that it can
504 	 * notice this end has gone away.
505 	 */
506 
507 	if (fnp->fn_rcnt == 0 && fn_dest->fn_wcnt > 0) {
508 		if ((fn_dest->fn_flag & (FIFOFAST | FIFOWANTW)) ==
509 		    (FIFOFAST | FIFOWANTW)) {
510 			/*
511 			 * wake up any sleeping writers
512 			 */
513 			fn_dest->fn_flag &= ~FIFOWANTW;
514 			cv_broadcast(&fn_dest->fn_wait_cv);
515 		}
516 	}
517 
518 	/*
519 	 * if there are still processes with this FIFO open
520 	 *	clear open/close sync flag
521 	 *	and just return;
522 	 */
523 	if (--fnp->fn_open > 0) {
524 		ASSERT((fnp->fn_rcnt + fnp->fn_wcnt) != 0);
525 		fn_lock->flk_ocsync = 0;
526 		cv_broadcast(&fn_lock->flk_wait_cv);
527 		mutex_exit(&fn_lock->flk_lock);
528 		return (0);
529 	}
530 
531 	/*
532 	 * Need to send HANGUP if other side is still open
533 	 * (fnp->fn_rcnt or fnp->fn_wcnt may not be zero (some thread
534 	 * on this end of the pipe may still be in fifo_open())
535 	 *
536 	 * Note: we can get here with fn_rcnt and fn_wcnt != 0 if some
537 	 * thread is blocked somewhere in the fifo_open() path prior to
538 	 * fifo_stropen() incrementing fn_open.  This can occur for
539 	 * normal FIFOs as well as named pipes.  fn_rcnt and
540 	 * fn_wcnt only indicate attempts to open. fn_open indicates
541 	 * successful opens. Partially opened FIFOs should proceed
542 	 * normally; i.e. they will appear to be new opens.  Partially
543 	 * opened pipes will probably fail.
544 	 */
545 
546 	if (fn_dest->fn_open && senthang == 0)
547 		(void) putnextctl_wait(sd_wrq, M_HANGUP);
548 
549 
550 	/*
551 	 * If this a pipe and this is the first end to close,
552 	 * then we have a bit of cleanup work to do.
553 	 * 	Mark both ends of pipe as closed.
554 	 * 	Wake up anybody blocked at the other end and for named pipes,
555 	 *	Close down this end of the stream
556 	 *	Allow other opens/closes to continue
557 	 * 	force an unmount of other end.
558 	 * Otherwise if this is last close,
559 	 *	flush messages,
560 	 *	close down the stream
561 	 *	allow other opens/closes to continue
562 	 */
563 	fnp->fn_flag &= ~FIFOISOPEN;
564 	if ((fnp->fn_flag & ISPIPE) && !(fnp->fn_flag & FIFOCLOSE)) {
565 		fnp->fn_flag |= FIFOCLOSE;
566 		fn_dest->fn_flag |= FIFOCLOSE;
567 		if (fnp->fn_flag & FIFOFAST)
568 			fifo_fastflush(fnp);
569 		if (vp->v_stream != NULL) {
570 			mutex_exit(&fn_lock->flk_lock);
571 			(void) strclose(vp, flag, crp);
572 			mutex_enter(&fn_lock->flk_lock);
573 		}
574 		cv_broadcast(&fn_dest->fn_wait_cv);
575 		/*
576 		 * allow opens and closes to proceed
577 		 * Since this end is now closed down, any attempt
578 		 * to do anything with this end will fail
579 		 */
580 		fn_lock->flk_ocsync = 0;
581 		cv_broadcast(&fn_lock->flk_wait_cv);
582 		fn_dest_vp = FTOV(fn_dest);
583 		/*
584 		 * if other end of pipe has been opened and it's
585 		 * a named pipe, unmount it
586 		 */
587 		if (fn_dest_vp->v_stream &&
588 		    (fn_dest_vp->v_stream->sd_flag & STRMOUNT)) {
589 			/*
590 			 * We must hold the destination vnode because
591 			 * nm_unmountall() causes close to be called
592 			 * for the other end of named pipe.  This
593 			 * could free the vnode before we are ready.
594 			 */
595 			VN_HOLD(fn_dest_vp);
596 			mutex_exit(&fn_lock->flk_lock);
597 			error = nm_unmountall(fn_dest_vp, crp);
598 			ASSERT(error == 0);
599 			VN_RELE(fn_dest_vp);
600 		} else {
601 			ASSERT(vp->v_count >= 1);
602 			mutex_exit(&fn_lock->flk_lock);
603 		}
604 	} else {
605 		if (fnp->fn_flag & FIFOFAST)
606 			fifo_fastflush(fnp);
607 #if DEBUG
608 		fn_dest_vp = FTOV(fn_dest);
609 		if (fn_dest_vp->v_stream)
610 		    ASSERT((fn_dest_vp->v_stream->sd_flag & STRMOUNT) == 0);
611 #endif
612 		if (vp->v_stream != NULL) {
613 			mutex_exit(&fn_lock->flk_lock);
614 			(void) strclose(vp, flag, crp);
615 			mutex_enter(&fn_lock->flk_lock);
616 		}
617 		fn_lock->flk_ocsync = 0;
618 		cv_broadcast(&fn_lock->flk_wait_cv);
619 		cv_broadcast(&fn_dest->fn_wait_cv);
620 		mutex_exit(&fn_lock->flk_lock);
621 	}
622 	return (error);
623 }
624 
625 /*
626  * Read from a pipe or FIFO.
627  * return 0 if....
628  *    (1) user read request is 0 or no stream
629  *    (2) broken pipe with no data
630  *    (3) write-only FIFO with no data
631  *    (4) no data and FNDELAY flag is set.
632  * Otherwise return
633  *	EAGAIN if FNONBLOCK is set and no data to read
634  *	EINTR if signal recieved while waiting for data
635  *
636  * While there is no data to read....
637  *   -  if the NDELAY/NONBLOCK flag is set, return 0/EAGAIN.
638  *   -  wait for a write.
639  *
640  */
641 /*ARGSUSED*/
642 
643 static int
644 fifo_read(struct vnode *vp, struct uio *uiop, int ioflag, struct cred *crp,
645 	caller_context_t *ct)
646 {
647 	fifonode_t	*fnp		= VTOF(vp);
648 	fifonode_t	*fn_dest;
649 	fifolock_t	*fn_lock	= fnp->fn_lock;
650 	int		error		= 0;
651 	mblk_t		*bp;
652 
653 	ASSERT(vp->v_stream != NULL);
654 	if (uiop->uio_resid == 0)
655 		return (0);
656 
657 	mutex_enter(&fn_lock->flk_lock);
658 
659 	TRACE_2(TR_FAC_FIFO,
660 		TR_FIFOREAD_IN, "fifo_read in:%p fnp %p", vp, fnp);
661 
662 	if (! (fnp->fn_flag & FIFOFAST))
663 		goto stream_mode;
664 
665 	fn_dest	= fnp->fn_dest;
666 	/*
667 	 * Check for data on our input queue
668 	 */
669 
670 	while (fnp->fn_count == 0) {
671 		/*
672 		 * No data on first attempt and no writer, then EOF
673 		 */
674 		if (fn_dest->fn_wcnt == 0 || fn_dest->fn_rcnt == 0) {
675 			mutex_exit(&fn_lock->flk_lock);
676 			return (0);
677 		}
678 		/*
679 		 * no data found.. if non-blocking, return EAGAIN
680 		 * otherwise 0.
681 		 */
682 		if (uiop->uio_fmode & (FNDELAY|FNONBLOCK)) {
683 			mutex_exit(&fn_lock->flk_lock);
684 			if (uiop->uio_fmode & FNONBLOCK)
685 				return (EAGAIN);
686 			return (0);
687 		}
688 
689 		/*
690 		 * Note: FIFOs can get here with FIFOCLOSE set if
691 		 * write side is in the middle of opeining after
692 		 * it once closed. Pipes better not have FIFOCLOSE set
693 		 */
694 		ASSERT((fnp->fn_flag & (ISPIPE|FIFOCLOSE)) !=
695 		    (ISPIPE|FIFOCLOSE));
696 		/*
697 		 * wait for data
698 		 */
699 		fnp->fn_flag |= FIFOWANTR;
700 
701 		TRACE_1(TR_FAC_FIFO, TR_FIFOREAD_WAIT,
702 			"fiforead wait: %p", vp);
703 
704 		if (!cv_wait_sig_swap(&fnp->fn_wait_cv,
705 		    &fn_lock->flk_lock)) {
706 			error = EINTR;
707 			goto done;
708 		}
709 
710 		TRACE_1(TR_FAC_FIFO, TR_FIFOREAD_WAKE,
711 			"fiforead awake: %p", vp);
712 
713 		/*
714 		 * check to make sure we are still in fast mode
715 		 */
716 		if (!(fnp->fn_flag & FIFOFAST))
717 			goto stream_mode;
718 	}
719 
720 	ASSERT(fnp->fn_mp != NULL);
721 
722 	/* For pipes copy should not bypass cache */
723 	uiop->uio_extflg |= UIO_COPY_CACHED;
724 
725 	do {
726 		int bpsize = MBLKL(fnp->fn_mp);
727 		int uiosize = MIN(bpsize, uiop->uio_resid);
728 
729 		error = uiomove(fnp->fn_mp->b_rptr, uiosize, UIO_READ, uiop);
730 		if (error != 0)
731 			break;
732 
733 		fnp->fn_count -= uiosize;
734 
735 		if (bpsize <= uiosize) {
736 			bp = fnp->fn_mp;
737 			fnp->fn_mp = fnp->fn_mp->b_cont;
738 			freeb(bp);
739 
740 			if (uiop->uio_resid == 0)
741 				break;
742 
743 			while (fnp->fn_mp == NULL && fn_dest->fn_wwaitcnt > 0) {
744 				ASSERT(fnp->fn_count == 0);
745 
746 				if (uiop->uio_fmode & (FNDELAY|FNONBLOCK))
747 					goto trywake;
748 
749 				/*
750 				 * We've consumed all available data but there
751 				 * are threads waiting to write more, let them
752 				 * proceed before bailing.
753 				 */
754 
755 				fnp->fn_flag |= FIFOWANTR;
756 				fifo_wakewriter(fn_dest, fn_lock);
757 
758 				if (!cv_wait_sig(&fnp->fn_wait_cv,
759 				    &fn_lock->flk_lock))
760 					goto trywake;
761 
762 				if (!(fnp->fn_flag & FIFOFAST))
763 					goto stream_mode;
764 			}
765 		} else {
766 			fnp->fn_mp->b_rptr += uiosize;
767 			ASSERT(uiop->uio_resid == 0);
768 		}
769 	} while (uiop->uio_resid != 0 && fnp->fn_mp != NULL);
770 
771 trywake:
772 	ASSERT(msgdsize(fnp->fn_mp) == fnp->fn_count);
773 
774 	/*
775 	 * wake up any blocked writers, processes
776 	 * sleeping on POLLWRNORM, or processes waiting for SIGPOLL
777 	 * Note: checking for fn_count < Fifohiwat emulates
778 	 * STREAMS functionality when low water mark is 0
779 	 */
780 	if (fn_dest->fn_flag & (FIFOWANTW | FIFOHIWATW) &&
781 	    fnp->fn_count < Fifohiwat) {
782 		fifo_wakewriter(fn_dest, fn_lock);
783 	}
784 	goto done;
785 
786 	/*
787 	 * FIFO is in streams mode.. let the stream head handle it
788 	 */
789 stream_mode:
790 
791 	mutex_exit(&fn_lock->flk_lock);
792 	TRACE_1(TR_FAC_FIFO,
793 		TR_FIFOREAD_STREAM, "fifo_read stream_mode:%p", vp);
794 
795 	error = strread(vp, uiop, crp);
796 
797 	mutex_enter(&fn_lock->flk_lock);
798 
799 done:
800 	/*
801 	 * vnode update access time
802 	 */
803 	if (error == 0) {
804 		time_t now = gethrestime_sec();
805 
806 		if (fnp->fn_flag & ISPIPE)
807 			fnp->fn_dest->fn_atime = now;
808 		fnp->fn_atime = now;
809 	}
810 	TRACE_2(TR_FAC_FIFO,
811 		TR_FIFOREAD_OUT, "fifo_read out:%p error %d",
812 		vp, error);
813 	mutex_exit(&fn_lock->flk_lock);
814 	return (error);
815 }
816 
817 /*
818  * send SIGPIPE and return EPIPE if ...
819  *   (1) broken pipe (essentially, reader is gone)
820  *   (2) FIFO is not open for reading
821  * return 0 if...
822  *   (1) no stream
823  *   (2) user write request is for 0 bytes and SW_SNDZERO is not set
824  *	Note: SW_SNDZERO can't be set in fast mode
825  * While the stream is flow controlled....
826  *   -  if the NDELAY/NONBLOCK flag is set, return 0/EAGAIN.
827  *   -  unlock the fifonode and sleep waiting for a reader.
828  *   -  if a pipe and it has a mate, sleep waiting for its mate
829  *	to read.
830  */
831 /*ARGSUSED*/
832 static int
833 fifo_write(vnode_t *vp, uio_t *uiop, int ioflag, cred_t *crp,
834 	caller_context_t *ct)
835 {
836 	struct fifonode	*fnp, *fn_dest;
837 	fifolock_t	*fn_lock;
838 	struct stdata	*stp;
839 	int		error	= 0;
840 	int		write_size;
841 	int		size;
842 	int		fmode;
843 	mblk_t		*bp;
844 	boolean_t	hotread;
845 
846 	ASSERT(vp->v_stream);
847 	uiop->uio_loffset = 0;
848 	stp	= vp->v_stream;
849 
850 	/*
851 	 * remember original number of bytes requested. Used to determine if
852 	 * we actually have written anything at all
853 	 */
854 	write_size = uiop->uio_resid;
855 
856 	/*
857 	 * only send zero-length messages if SW_SNDZERO is set
858 	 * Note: we will be in streams mode if SW_SNDZERO is set
859 	 * XXX this streams interface should not be exposed
860 	 */
861 	if ((write_size == 0) && !(stp->sd_wput_opt & SW_SNDZERO))
862 		return (0);
863 
864 	fnp = VTOF(vp);
865 	fn_lock = fnp->fn_lock;
866 	fn_dest = fnp->fn_dest;
867 
868 	mutex_enter(&fn_lock->flk_lock);
869 
870 	TRACE_3(TR_FAC_FIFO,
871 		TR_FIFOWRITE_IN, "fifo_write in:%p fnp %p size %d",
872 		vp, fnp, write_size);
873 
874 	/*
875 	 * oops, no readers, error
876 	 */
877 	if (fn_dest->fn_rcnt == 0 || fn_dest->fn_wcnt == 0) {
878 		goto epipe;
879 	}
880 
881 	/*
882 	 * if we are not in fast mode, let streams handle it
883 	 */
884 	if (!(fnp->fn_flag & FIFOFAST))
885 		goto stream_mode;
886 
887 	fmode = uiop->uio_fmode & (FNDELAY|FNONBLOCK);
888 
889 	/* For pipes copy should not bypass cache */
890 	uiop->uio_extflg |= UIO_COPY_CACHED;
891 
892 	do  {
893 		/*
894 		 * check to make sure we are not over high water mark
895 		 */
896 		while (fn_dest->fn_count >= Fifohiwat) {
897 			/*
898 			 * Indicate that we have gone over high
899 			 * water mark
900 			 */
901 			/*
902 			 * if non-blocking, return
903 			 * only happens first time through loop
904 			 */
905 			if (fmode) {
906 				fnp->fn_flag |= FIFOHIWATW;
907 				if (uiop->uio_resid == write_size) {
908 					mutex_exit(&fn_lock->flk_lock);
909 					if (fmode & FNDELAY)
910 						return (0);
911 					else
912 						return (EAGAIN);
913 				}
914 				goto done;
915 			}
916 
917 			/*
918 			 * wait for things to drain
919 			 */
920 			fnp->fn_flag |= FIFOWANTW;
921 			fnp->fn_wwaitcnt++;
922 			TRACE_1(TR_FAC_FIFO, TR_FIFOWRITE_WAIT,
923 				"fifo_write wait: %p", vp);
924 			if (!cv_wait_sig_swap(&fnp->fn_wait_cv,
925 			    &fn_lock->flk_lock)) {
926 				error = EINTR;
927 				fnp->fn_wwaitcnt--;
928 				fifo_wakereader(fn_dest, fn_lock);
929 				goto done;
930 			}
931 			fnp->fn_wwaitcnt--;
932 
933 			TRACE_1(TR_FAC_FIFO, TR_FIFOWRITE_WAKE,
934 				"fifo_write wake: %p", vp);
935 
936 			/*
937 			 * check to make sure we're still in fast mode
938 			 */
939 			if (!(fnp->fn_flag & FIFOFAST))
940 				goto stream_mode;
941 
942 			/*
943 			 * make sure readers didn't go away
944 			 */
945 			if (fn_dest->fn_rcnt == 0 || fn_dest->fn_wcnt == 0) {
946 				goto epipe;
947 			}
948 		}
949 		/*
950 		 * If the write will put us over the high water mark,
951 		 * then we must break the message up into PIPE_BUF
952 		 * chunks to stay compliant with STREAMS
953 		 */
954 		if (uiop->uio_resid + fn_dest->fn_count > Fifohiwat)
955 			size = MIN(uiop->uio_resid, PIPE_BUF);
956 		else
957 			size = uiop->uio_resid;
958 
959 		/*
960 		 * We don't need to hold flk_lock across the allocb() and
961 		 * uiomove().  However, on a multiprocessor machine where both
962 		 * the reader and writer thread are on cpu's, we must be
963 		 * careful to only drop the lock if there's data to be read.
964 		 * This forces threads entering fifo_read() to spin or block
965 		 * on flk_lock, rather than acquiring flk_lock only to
966 		 * discover there's no data to read and being forced to go
967 		 * back to sleep, only to be woken up microseconds later by
968 		 * this writer thread.
969 		 */
970 		hotread = fn_dest->fn_count > 0;
971 		if (hotread) {
972 			if (!fifo_stayfast_enter(fnp))
973 				goto stream_mode;
974 			mutex_exit(&fn_lock->flk_lock);
975 		}
976 
977 		ASSERT(size != 0);
978 		/*
979 		 * Align the mblk with the user data so that
980 		 * copying in the data can take advantage of
981 		 * the double word alignment
982 		 */
983 		if ((bp = allocb(size + 8, BPRI_MED)) == NULL) {
984 			if (!hotread)
985 				mutex_exit(&fn_lock->flk_lock);
986 
987 			error = strwaitbuf(size, BPRI_MED);
988 
989 			mutex_enter(&fn_lock->flk_lock);
990 
991 			if (hotread) {
992 				/*
993 				 * As we dropped the mutex for a moment, we
994 				 * need to wake up any thread waiting to be
995 				 * allowed to go from fast mode to stream mode.
996 				 */
997 				fifo_stayfast_exit(fnp);
998 			}
999 			if (error != 0) {
1000 				goto done;
1001 			}
1002 			/*
1003 			 * check to make sure we're still in fast mode
1004 			 */
1005 			if (!(fnp->fn_flag & FIFOFAST))
1006 				goto stream_mode;
1007 
1008 			/*
1009 			 * make sure readers didn't go away
1010 			 */
1011 			if (fn_dest->fn_rcnt == 0 || fn_dest->fn_wcnt == 0) {
1012 				goto epipe;
1013 			}
1014 			/*
1015 			 * some other thread could have gotten in
1016 			 * need to go back and check hi water mark
1017 			 */
1018 			continue;
1019 		}
1020 		bp->b_rptr += ((uintptr_t)uiop->uio_iov->iov_base & 0x7);
1021 		bp->b_wptr = bp->b_rptr + size;
1022 		error = uiomove((caddr_t)bp->b_rptr, size, UIO_WRITE, uiop);
1023 		if (hotread) {
1024 			mutex_enter(&fn_lock->flk_lock);
1025 			/*
1026 			 * As we dropped the mutex for a moment, we need to:
1027 			 * - wake up any thread waiting to be allowed to go
1028 			 *   from fast mode to stream mode,
1029 			 * - make sure readers didn't go away.
1030 			 */
1031 			fifo_stayfast_exit(fnp);
1032 			if (fn_dest->fn_rcnt == 0 || fn_dest->fn_wcnt == 0) {
1033 				freeb(bp);
1034 				goto epipe;
1035 			}
1036 		}
1037 
1038 		if (error != 0) {
1039 			freeb(bp);
1040 			goto done;
1041 		}
1042 
1043 		fn_dest->fn_count += size;
1044 		if (fn_dest->fn_mp != NULL) {
1045 			fn_dest->fn_tail->b_cont = bp;
1046 			fn_dest->fn_tail = bp;
1047 		} else {
1048 			fn_dest->fn_mp = fn_dest->fn_tail = bp;
1049 			/*
1050 			 * This is the first bit of data; wake up any sleeping
1051 			 * readers, processes blocked in poll, and those
1052 			 * expecting a SIGPOLL.
1053 			 */
1054 			fifo_wakereader(fn_dest, fn_lock);
1055 		}
1056 	} while (uiop->uio_resid != 0);
1057 
1058 	goto done;
1059 
1060 stream_mode:
1061 	/*
1062 	 * streams mode
1063 	 *  let the stream head handle the write
1064 	 */
1065 	ASSERT(MUTEX_HELD(&fn_lock->flk_lock));
1066 
1067 	mutex_exit(&fn_lock->flk_lock);
1068 	TRACE_1(TR_FAC_FIFO,
1069 		TR_FIFOWRITE_STREAM, "fifo_write stream_mode:%p", vp);
1070 
1071 	error = strwrite(vp, uiop, crp);
1072 
1073 	mutex_enter(&fn_lock->flk_lock);
1074 
1075 done:
1076 	/*
1077 	 * update vnode modification and change times
1078 	 * make sure there were no errors and some data was transfered
1079 	 */
1080 	if (error == 0 && write_size != uiop->uio_resid) {
1081 		time_t now = gethrestime_sec();
1082 
1083 		if (fnp->fn_flag & ISPIPE) {
1084 			fn_dest->fn_mtime = fn_dest->fn_ctime = now;
1085 		}
1086 		fnp->fn_mtime = fnp->fn_ctime = now;
1087 	} else if (fn_dest->fn_rcnt == 0 || fn_dest->fn_wcnt == 0) {
1088 		goto epipe;
1089 	}
1090 	TRACE_3(TR_FAC_FIFO, TR_FIFOWRITE_OUT,
1091 		"fifo_write out: vp %p error %d fnp %p", vp, error, fnp);
1092 	mutex_exit(&fn_lock->flk_lock);
1093 	return (error);
1094 epipe:
1095 	error = EPIPE;
1096 	TRACE_3(TR_FAC_FIFO, TR_FIFOWRITE_OUT,
1097 		"fifo_write out: vp %p error %d fnp %p",
1098 		vp, error, fnp);
1099 	mutex_exit(&fn_lock->flk_lock);
1100 	tsignal(curthread, SIGPIPE);
1101 	return (error);
1102 }
1103 
1104 static int
1105 fifo_ioctl(vnode_t *vp, int cmd, intptr_t arg, int mode,
1106 	cred_t *cr, int *rvalp)
1107 {
1108 	/*
1109 	 * Just a quick check
1110 	 * Once we go to streams mode we don't ever revert back
1111 	 * So we do this quick check so as not to incur the overhead
1112 	 * associated with acquiring the lock
1113 	 */
1114 	return ((VTOF(vp)->fn_flag & FIFOFAST) ?
1115 		fifo_fastioctl(vp, cmd, arg, mode, cr, rvalp) :
1116 		fifo_strioctl(vp, cmd, arg, mode, cr, rvalp));
1117 }
1118 
1119 static int
1120 fifo_fastioctl(vnode_t *vp, int cmd, intptr_t arg, int mode,
1121 	cred_t *cr, int *rvalp)
1122 {
1123 	fifonode_t	*fnp		= VTOF(vp);
1124 	fifonode_t	*fn_dest;
1125 	int		error		= 0;
1126 	fifolock_t	*fn_lock	= fnp->fn_lock;
1127 	int		cnt;
1128 
1129 	/*
1130 	 * tty operations not allowed
1131 	 */
1132 	if (((cmd & IOCTYPE) == LDIOC) ||
1133 	    ((cmd & IOCTYPE) == tIOC) ||
1134 	    ((cmd & IOCTYPE) == TIOC)) {
1135 		return (EINVAL);
1136 	}
1137 
1138 	mutex_enter(&fn_lock->flk_lock);
1139 
1140 	if (!(fnp->fn_flag & FIFOFAST)) {
1141 		goto stream_mode;
1142 	}
1143 
1144 	switch (cmd) {
1145 
1146 	/*
1147 	 * Things we can't handle
1148 	 * These will switch us to streams mode.
1149 	 */
1150 	default:
1151 	case I_STR:
1152 	case I_SRDOPT:
1153 	case I_PUSH:
1154 	case I_FDINSERT:
1155 	case I_SENDFD:
1156 	case I_RECVFD:
1157 	case I_E_RECVFD:
1158 	case I_ATMARK:
1159 	case I_CKBAND:
1160 	case I_GETBAND:
1161 	case I_SWROPT:
1162 		goto turn_fastoff;
1163 
1164 	/*
1165 	 * Things that don't do damage
1166 	 * These things don't adjust the state of the
1167 	 * stream head (i_setcltime does, but we don't care)
1168 	 */
1169 	case I_FIND:
1170 	case I_GETSIG:
1171 	case FIONBIO:
1172 	case FIOASYNC:
1173 	case I_GRDOPT:	/* probably should not get this, but no harm */
1174 	case I_GWROPT:
1175 	case I_LIST:
1176 	case I_SETCLTIME:
1177 	case I_GETCLTIME:
1178 		mutex_exit(&fn_lock->flk_lock);
1179 		return (strioctl(vp, cmd, arg, mode, U_TO_K, cr, rvalp));
1180 
1181 	case I_CANPUT:
1182 		/*
1183 		 * We can only handle normal band canputs.
1184 		 * XXX : We could just always go to stream mode; after all
1185 		 * canput is a streams semantics type thing
1186 		 */
1187 		if (arg != 0) {
1188 			goto turn_fastoff;
1189 		}
1190 		*rvalp = (fnp->fn_dest->fn_count < Fifohiwat) ? 1 : 0;
1191 		mutex_exit(&fn_lock->flk_lock);
1192 		return (0);
1193 
1194 	case I_NREAD:
1195 		/*
1196 		 * This may seem a bit silly for non-streams semantics,
1197 		 * (After all, if they really want a message, they'll
1198 		 * probably use getmsg() anyway). but it doesn't hurt
1199 		 */
1200 		error = copyout((caddr_t)&fnp->fn_count, (caddr_t)arg,
1201 			sizeof (cnt));
1202 		if (error == 0) {
1203 			*rvalp = (fnp->fn_count == 0) ? 0 : 1;
1204 		}
1205 		break;
1206 
1207 	case FIORDCHK:
1208 		*rvalp = fnp->fn_count;
1209 		break;
1210 
1211 	case I_PEEK:
1212 	    {
1213 		STRUCT_DECL(strpeek, strpeek);
1214 		struct uio	uio;
1215 		struct iovec	iov;
1216 		int		count;
1217 		mblk_t		*bp;
1218 
1219 		STRUCT_INIT(strpeek, mode);
1220 
1221 		if (fnp->fn_count == 0) {
1222 			*rvalp = 0;
1223 			break;
1224 		}
1225 
1226 		error = copyin((caddr_t)arg, STRUCT_BUF(strpeek),
1227 		    STRUCT_SIZE(strpeek));
1228 		if (error)
1229 			break;
1230 
1231 		/*
1232 		 * can't have any high priority message when in fast mode
1233 		 */
1234 		if (STRUCT_FGET(strpeek, flags) & RS_HIPRI) {
1235 			*rvalp = 0;
1236 			break;
1237 		}
1238 
1239 		iov.iov_base = STRUCT_FGETP(strpeek, databuf.buf);
1240 		iov.iov_len = STRUCT_FGET(strpeek, databuf.maxlen);
1241 		uio.uio_iov = &iov;
1242 		uio.uio_iovcnt = 1;
1243 		uio.uio_loffset = 0;
1244 		uio.uio_segflg = UIO_USERSPACE;
1245 		uio.uio_fmode = 0;
1246 		/* For pipes copy should not bypass cache */
1247 		uio.uio_extflg = UIO_COPY_CACHED;
1248 		uio.uio_resid = iov.iov_len;
1249 		count = fnp->fn_count;
1250 		bp = fnp->fn_mp;
1251 		while (count > 0 && uio.uio_resid) {
1252 			cnt = MIN(uio.uio_resid, bp->b_wptr - bp->b_rptr);
1253 			if ((error = uiomove((char *)bp->b_rptr, cnt,
1254 			    UIO_READ, &uio)) != 0) {
1255 				break;
1256 			}
1257 			count -= cnt;
1258 			bp = bp->b_cont;
1259 		}
1260 		STRUCT_FSET(strpeek, databuf.len,
1261 		    STRUCT_FGET(strpeek, databuf.maxlen) - uio.uio_resid);
1262 		STRUCT_FSET(strpeek, flags, 0);
1263 		STRUCT_FSET(strpeek, ctlbuf.len,
1264 		    STRUCT_FGET(strpeek, ctlbuf.maxlen));
1265 
1266 		error = copyout(STRUCT_BUF(strpeek), (caddr_t)arg,
1267 		    STRUCT_SIZE(strpeek));
1268 		if (error == 0)
1269 			*rvalp = 1;
1270 		break;
1271 	    }
1272 
1273 	case FIONREAD:
1274 		/*
1275 		 * let user know total number of bytes in message queue
1276 		 */
1277 		error = copyout((caddr_t)&fnp->fn_count, (caddr_t)arg,
1278 			sizeof (fnp->fn_count));
1279 		if (error == 0)
1280 			*rvalp = 0;
1281 		break;
1282 
1283 	case I_SETSIG:
1284 		/*
1285 		 * let streams set up the signal masking for us
1286 		 * we just check to see if it's set
1287 		 * XXX : this interface should not be visible
1288 		 *  i.e. STREAM's framework is exposed.
1289 		 */
1290 		error = strioctl(vp, cmd, arg, mode, U_TO_K, cr, rvalp);
1291 		if (vp->v_stream->sd_sigflags & (S_INPUT|S_RDNORM|S_WRNORM))
1292 			fnp->fn_flag |= FIFOSETSIG;
1293 		else
1294 			fnp->fn_flag &= ~FIFOSETSIG;
1295 		break;
1296 
1297 	case I_FLUSH:
1298 		/*
1299 		 * flush them message queues
1300 		 */
1301 		if (arg & ~FLUSHRW) {
1302 			error = EINVAL;
1303 			break;
1304 		}
1305 		if (arg & FLUSHR) {
1306 			fifo_fastflush(fnp);
1307 		}
1308 		fn_dest = fnp->fn_dest;
1309 		if ((arg & FLUSHW)) {
1310 			fifo_fastflush(fn_dest);
1311 		}
1312 		/*
1313 		 * wake up any sleeping readers or writers
1314 		 * (waking readers probably doesn't make sense, but it
1315 		 *  doesn't hurt; i.e. we just got rid of all the data
1316 		 *  what's to read ?)
1317 		 */
1318 		if (fn_dest->fn_flag & (FIFOWANTW | FIFOWANTR)) {
1319 			fn_dest->fn_flag &= ~(FIFOWANTW | FIFOWANTR);
1320 			cv_broadcast(&fn_dest->fn_wait_cv);
1321 		}
1322 		*rvalp = 0;
1323 		break;
1324 
1325 	/*
1326 	 * Since no band data can ever get on a fifo in fast mode
1327 	 * just return 0.
1328 	 */
1329 	case I_FLUSHBAND:
1330 		error = 0;
1331 		*rvalp = 0;
1332 		break;
1333 
1334 	/*
1335 	 * invalid calls for stream head or fifos
1336 	 */
1337 
1338 	case I_POP:		/* shouldn't happen */
1339 	case I_LOOK:
1340 	case I_LINK:
1341 	case I_PLINK:
1342 	case I_UNLINK:
1343 	case I_PUNLINK:
1344 
1345 	/*
1346 	 * more invalid tty type of ioctls
1347 	 */
1348 
1349 	case SRIOCSREDIR:
1350 	case SRIOCISREDIR:
1351 		error = EINVAL;
1352 		break;
1353 
1354 	}
1355 	mutex_exit(&fn_lock->flk_lock);
1356 	return (error);
1357 
1358 turn_fastoff:
1359 	fifo_fastoff(fnp);
1360 
1361 stream_mode:
1362 	/*
1363 	 * streams mode
1364 	 */
1365 	mutex_exit(&fn_lock->flk_lock);
1366 	return (fifo_strioctl(vp, cmd, arg, mode, cr, rvalp));
1367 
1368 }
1369 
1370 /*
1371  * FIFO is in STREAMS mode; STREAMS framework does most of the work.
1372  */
1373 static int
1374 fifo_strioctl(vnode_t *vp, int cmd, intptr_t arg, int mode,
1375 	cred_t *cr, int *rvalp)
1376 {
1377 	fifonode_t	*fnp = VTOF(vp);
1378 	int		error;
1379 	fifolock_t	*fn_lock;
1380 
1381 	if (cmd == _I_GETPEERCRED) {
1382 		if (mode == FKIOCTL && fnp->fn_pcredp != NULL) {
1383 			k_peercred_t *kp = (k_peercred_t *)arg;
1384 			crhold(fnp->fn_pcredp);
1385 			kp->pc_cr = fnp->fn_pcredp;
1386 			kp->pc_cpid = fnp->fn_cpid;
1387 			return (0);
1388 		} else {
1389 			return (ENOTSUP);
1390 		}
1391 	}
1392 
1393 	error = strioctl(vp, cmd, arg, mode, U_TO_K, cr, rvalp);
1394 
1395 	switch (cmd) {
1396 	/*
1397 	 * The FIFOSEND flag is set to inform other processes that a file
1398 	 * descriptor is pending at the stream head of this pipe.
1399 	 * The flag is cleared and the sending process is awoken when
1400 	 * this process has completed recieving the file descriptor.
1401 	 * XXX This could become out of sync if the process does I_SENDFDs
1402 	 * and opens on connld attached to the same pipe.
1403 	 */
1404 	case I_RECVFD:
1405 	case I_E_RECVFD:
1406 		if (error == 0) {
1407 			fn_lock = fnp->fn_lock;
1408 			mutex_enter(&fn_lock->flk_lock);
1409 			if (fnp->fn_flag & FIFOSEND) {
1410 				fnp->fn_flag &= ~FIFOSEND;
1411 				cv_broadcast(&fnp->fn_dest->fn_wait_cv);
1412 			}
1413 			mutex_exit(&fn_lock->flk_lock);
1414 		}
1415 		break;
1416 	default:
1417 		break;
1418 	}
1419 
1420 	return (error);
1421 }
1422 
1423 /*
1424  * If shadowing a vnode (FIFOs), apply the VOP_GETATTR to the shadowed
1425  * vnode to Obtain the node information. If not shadowing (pipes), obtain
1426  * the node information from the credentials structure.
1427  */
1428 int
1429 fifo_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *crp)
1430 {
1431 	int		error		= 0;
1432 	fifonode_t	*fnp		= VTOF(vp);
1433 	queue_t		*qp;
1434 	qband_t		*bandp;
1435 	fifolock_t	*fn_lock	= fnp->fn_lock;
1436 
1437 	if (fnp->fn_realvp) {
1438 		/*
1439 		 * for FIFOs or mounted pipes
1440 		 */
1441 		if (error = VOP_GETATTR(fnp->fn_realvp, vap, flags, crp))
1442 			return (error);
1443 		mutex_enter(&fn_lock->flk_lock);
1444 		/* set current times from fnode, even if older than vnode */
1445 		vap->va_atime.tv_sec = fnp->fn_atime;
1446 		vap->va_atime.tv_nsec = 0;
1447 		vap->va_mtime.tv_sec = fnp->fn_mtime;
1448 		vap->va_mtime.tv_nsec = 0;
1449 		vap->va_ctime.tv_sec = fnp->fn_ctime;
1450 		vap->va_ctime.tv_nsec = 0;
1451 	} else {
1452 		/*
1453 		 * for non-attached/ordinary pipes
1454 		 */
1455 		vap->va_mode = 0;
1456 		mutex_enter(&fn_lock->flk_lock);
1457 		vap->va_atime.tv_sec = fnp->fn_atime;
1458 		vap->va_atime.tv_nsec = 0;
1459 		vap->va_mtime.tv_sec = fnp->fn_mtime;
1460 		vap->va_mtime.tv_nsec = 0;
1461 		vap->va_ctime.tv_sec = fnp->fn_ctime;
1462 		vap->va_ctime.tv_nsec = 0;
1463 		vap->va_uid = crgetuid(crp);
1464 		vap->va_gid = crgetgid(crp);
1465 		vap->va_nlink = 0;
1466 		vap->va_fsid = fifodev;
1467 		vap->va_nodeid = (ino64_t)fnp->fn_ino;
1468 		vap->va_rdev = 0;
1469 	}
1470 	vap->va_type = VFIFO;
1471 	vap->va_blksize = PIPE_BUF;
1472 	/*
1473 	 * Size is number of un-read bytes at the stream head and
1474 	 * nblocks is the unread bytes expressed in blocks.
1475 	 */
1476 	if (vp->v_stream && (fnp->fn_flag & FIFOISOPEN)) {
1477 		if ((fnp->fn_flag & FIFOFAST)) {
1478 			vap->va_size = (u_offset_t)fnp->fn_count;
1479 		} else {
1480 			qp = RD((strvp2wq(vp)));
1481 			vap->va_size = (u_offset_t)qp->q_count;
1482 			if (qp->q_nband != 0) {
1483 				mutex_enter(QLOCK(qp));
1484 				for (bandp = qp->q_bandp; bandp;
1485 				    bandp = bandp->qb_next)
1486 					vap->va_size += bandp->qb_count;
1487 				mutex_exit(QLOCK(qp));
1488 			}
1489 		}
1490 		vap->va_nblocks = (fsblkcnt64_t)btod(vap->va_size);
1491 	} else {
1492 		vap->va_size = (u_offset_t)0;
1493 		vap->va_nblocks = (fsblkcnt64_t)0;
1494 	}
1495 	mutex_exit(&fn_lock->flk_lock);
1496 	vap->va_seq = 0;
1497 	return (0);
1498 }
1499 
1500 /*
1501  * If shadowing a vnode, apply the VOP_SETATTR to it, and to the fnode.
1502  * Otherwise, set the time and return 0.
1503  */
1504 int
1505 fifo_setattr(
1506 	vnode_t			*vp,
1507 	vattr_t			*vap,
1508 	int			flags,
1509 	cred_t			*crp,
1510 	caller_context_t	*ctp)
1511 {
1512 	fifonode_t	*fnp	= VTOF(vp);
1513 	int		error	= 0;
1514 	fifolock_t	*fn_lock;
1515 
1516 	if (fnp->fn_realvp)
1517 		error = VOP_SETATTR(fnp->fn_realvp, vap, flags, crp, ctp);
1518 	if (error == 0) {
1519 		fn_lock = fnp->fn_lock;
1520 		mutex_enter(&fn_lock->flk_lock);
1521 		if (vap->va_mask & AT_ATIME)
1522 			fnp->fn_atime = vap->va_atime.tv_sec;
1523 		if (vap->va_mask & AT_MTIME)
1524 			fnp->fn_mtime = vap->va_mtime.tv_sec;
1525 		fnp->fn_ctime = gethrestime_sec();
1526 		mutex_exit(&fn_lock->flk_lock);
1527 	}
1528 	return (error);
1529 }
1530 
1531 /*
1532  * If shadowing a vnode, apply VOP_ACCESS to it.
1533  * Otherwise, return 0 (allow all access).
1534  */
1535 int
1536 fifo_access(vnode_t *vp, int mode, int flags, cred_t *crp)
1537 {
1538 	if (VTOF(vp)->fn_realvp)
1539 		return (VOP_ACCESS(VTOF(vp)->fn_realvp, mode, flags, crp));
1540 	else
1541 		return (0);
1542 }
1543 
1544 /*
1545  * If shadowing a vnode, apply the VOP_FSYNC to it.
1546  * Otherwise, return 0.
1547  */
1548 int
1549 fifo_fsync(vnode_t *vp, int syncflag, cred_t *crp)
1550 {
1551 	fifonode_t	*fnp	= VTOF(vp);
1552 	vattr_t		va;
1553 
1554 	if (fnp->fn_realvp == NULL)
1555 		return (0);
1556 
1557 	bzero((caddr_t)&va, sizeof (va));
1558 	va.va_mask = AT_MTIME | AT_ATIME;
1559 	if (VOP_GETATTR(fnp->fn_realvp, &va, 0, crp) == 0) {
1560 		va.va_mask = 0;
1561 		if (fnp->fn_mtime > va.va_mtime.tv_sec) {
1562 			va.va_mtime.tv_sec = fnp->fn_mtime;
1563 			va.va_mask = AT_MTIME;
1564 		}
1565 		if (fnp->fn_atime > va.va_atime.tv_sec) {
1566 			va.va_atime.tv_sec = fnp->fn_atime;
1567 			va.va_mask |= AT_ATIME;
1568 		}
1569 		if (va.va_mask != 0)
1570 			(void) VOP_SETATTR(fnp->fn_realvp, &va, 0, crp, NULL);
1571 	}
1572 	return (VOP_FSYNC(fnp->fn_realvp, syncflag, crp));
1573 }
1574 
1575 /*
1576  * Called when the upper level no longer holds references to the
1577  * vnode. Sync the file system and free the fifonode.
1578  */
1579 void
1580 fifo_inactive(vnode_t *vp, cred_t *crp)
1581 {
1582 	fifonode_t	*fnp;
1583 	fifolock_t	*fn_lock;
1584 
1585 	mutex_enter(&ftable_lock);
1586 	mutex_enter(&vp->v_lock);
1587 	ASSERT(vp->v_count >= 1);
1588 	if (--vp->v_count != 0) {
1589 		/*
1590 		 * Somebody accessed the fifo before we got a chance to
1591 		 * remove it.  They will remove it when they do a vn_rele.
1592 		 */
1593 		mutex_exit(&vp->v_lock);
1594 		mutex_exit(&ftable_lock);
1595 		return;
1596 	}
1597 	mutex_exit(&vp->v_lock);
1598 
1599 	fnp = VTOF(vp);
1600 
1601 	/*
1602 	 * remove fifo from fifo list so that no other process
1603 	 * can grab it.
1604 	 */
1605 	if (fnp->fn_realvp) {
1606 		(void) fiforemove(fnp);
1607 		mutex_exit(&ftable_lock);
1608 		(void) fifo_fsync(vp, FSYNC, crp);
1609 		VN_RELE(fnp->fn_realvp);
1610 		vp->v_vfsp = NULL;
1611 	} else
1612 		mutex_exit(&ftable_lock);
1613 
1614 	fn_lock = fnp->fn_lock;
1615 
1616 	mutex_enter(&fn_lock->flk_lock);
1617 	ASSERT(vp->v_stream == NULL);
1618 	ASSERT(vp->v_count == 0);
1619 	/*
1620 	 * if this is last reference to the lock, then we can
1621 	 * free everything up.
1622 	 */
1623 	if (--fn_lock->flk_ref == 0) {
1624 		mutex_exit(&fn_lock->flk_lock);
1625 		ASSERT(fnp->fn_open == 0);
1626 		ASSERT(fnp->fn_dest->fn_open == 0);
1627 		if (fnp->fn_mp) {
1628 			freemsg(fnp->fn_mp);
1629 			fnp->fn_mp = NULL;
1630 			fnp->fn_count = 0;
1631 		}
1632 		if (fnp->fn_pcredp != NULL) {
1633 			crfree(fnp->fn_pcredp);
1634 			fnp->fn_pcredp = NULL;
1635 		}
1636 		if (fnp->fn_flag & ISPIPE) {
1637 			fifonode_t *fn_dest = fnp->fn_dest;
1638 
1639 			vp = FTOV(fn_dest);
1640 			if (fn_dest->fn_mp) {
1641 				freemsg(fn_dest->fn_mp);
1642 				fn_dest->fn_mp = NULL;
1643 				fn_dest->fn_count = 0;
1644 			}
1645 			if (fn_dest->fn_pcredp != NULL) {
1646 				crfree(fn_dest->fn_pcredp);
1647 				fn_dest->fn_pcredp = NULL;
1648 			}
1649 			kmem_cache_free(pipe_cache, (fifodata_t *)fn_lock);
1650 		} else
1651 			kmem_cache_free(fnode_cache, (fifodata_t *)fn_lock);
1652 	} else {
1653 		mutex_exit(&fn_lock->flk_lock);
1654 	}
1655 }
1656 
1657 /*
1658  * If shadowing a vnode, apply the VOP_FID to it.
1659  * Otherwise, return EINVAL.
1660  */
1661 int
1662 fifo_fid(vnode_t *vp, fid_t *fidfnp)
1663 {
1664 	if (VTOF(vp)->fn_realvp)
1665 		return (VOP_FID(VTOF(vp)->fn_realvp, fidfnp));
1666 	else
1667 		return (EINVAL);
1668 }
1669 
1670 /*
1671  * Lock a fifonode.
1672  */
1673 /* ARGSUSED */
1674 int
1675 fifo_rwlock(vnode_t *vp, int write_lock, caller_context_t *ctp)
1676 {
1677 	return (-1);
1678 }
1679 
1680 /*
1681  * Unlock a fifonode.
1682  */
1683 /* ARGSUSED */
1684 void
1685 fifo_rwunlock(vnode_t *vp, int write_lock, caller_context_t *ctp)
1686 {
1687 }
1688 
1689 /*
1690  * Return error since seeks are not allowed on pipes.
1691  */
1692 /*ARGSUSED*/
1693 int
1694 fifo_seek(vnode_t *vp, offset_t ooff, offset_t *noffp)
1695 {
1696 	return (ESPIPE);
1697 }
1698 
1699 /*
1700  * If there is a realvp associated with vp, return it.
1701  */
1702 int
1703 fifo_realvp(vnode_t *vp, vnode_t **vpp)
1704 {
1705 	vnode_t *rvp;
1706 
1707 	if ((rvp = VTOF(vp)->fn_realvp) != NULL) {
1708 		vp = rvp;
1709 		if (VOP_REALVP(vp, &rvp) == 0)
1710 			vp = rvp;
1711 	}
1712 
1713 	*vpp = vp;
1714 	return (0);
1715 }
1716 
1717 /*
1718  * Poll for interesting events on a stream pipe
1719  */
1720 int
1721 fifo_poll(vnode_t *vp, short events, int anyyet, short *reventsp,
1722 	pollhead_t **phpp)
1723 {
1724 	fifonode_t	*fnp, *fn_dest;
1725 	fifolock_t	*fn_lock;
1726 	int		retevents;
1727 	struct stdata	*stp;
1728 
1729 	ASSERT(vp->v_stream != NULL);
1730 
1731 	stp = vp->v_stream;
1732 	retevents	= 0;
1733 	fnp		= VTOF(vp);
1734 	fn_dest		= fnp->fn_dest;
1735 	fn_lock		= fnp->fn_lock;
1736 
1737 	polllock(&stp->sd_pollist, &fn_lock->flk_lock);
1738 
1739 	/*
1740 	 * see if FIFO/pipe open
1741 	 */
1742 	if ((fnp->fn_flag & FIFOISOPEN) == 0) {
1743 		if (((events & (POLLIN | POLLRDNORM | POLLPRI | POLLRDBAND)) &&
1744 		    fnp->fn_rcnt == 0) ||
1745 		    ((events & (POLLWRNORM | POLLWRBAND)) &&
1746 		    fnp->fn_wcnt == 0)) {
1747 			mutex_exit(&fnp->fn_lock->flk_lock);
1748 			*reventsp = POLLERR;
1749 			return (0);
1750 		}
1751 	}
1752 
1753 	/*
1754 	 * if not in fast mode, let the stream head take care of it
1755 	 */
1756 	if (!(fnp->fn_flag & FIFOFAST)) {
1757 		mutex_exit(&fnp->fn_lock->flk_lock);
1758 		goto stream_mode;
1759 	}
1760 
1761 	/*
1762 	 * If this is a pipe.. check to see if the other
1763 	 * end is gone.  If we are a fifo, check to see
1764 	 * if write end is gone.
1765 	 */
1766 
1767 	if ((fnp->fn_flag & ISPIPE) && (fn_dest->fn_open == 0)) {
1768 		retevents = POLLHUP;
1769 	} else if ((fnp->fn_flag & (FIFOCLOSE | ISPIPE)) == FIFOCLOSE &&
1770 	    (fn_dest->fn_wcnt == 0)) {
1771 		/*
1772 		 * no writer at other end.
1773 		 * it was closed (versus yet to be opened)
1774 		 */
1775 			retevents = POLLHUP;
1776 	} else if (events & (POLLWRNORM | POLLWRBAND)) {
1777 		if (events & POLLWRNORM) {
1778 			if (fn_dest->fn_count < Fifohiwat)
1779 				retevents = POLLWRNORM;
1780 			else
1781 				fnp->fn_flag |= FIFOHIWATW;
1782 		}
1783 		/*
1784 		 * This is always true for fast pipes
1785 		 * (Note: will go to STREAMS mode if band data is written)
1786 		 */
1787 		if (events & POLLWRBAND)
1788 			retevents |= POLLWRBAND;
1789 	}
1790 	if (events & (POLLIN | POLLRDNORM)) {
1791 		if (fnp->fn_count)
1792 			retevents |= (events & (POLLIN | POLLRDNORM));
1793 	}
1794 
1795 	/*
1796 	 * if we happened to get something, return
1797 	 */
1798 
1799 	if ((*reventsp = (short)retevents) != 0) {
1800 		mutex_exit(&fnp->fn_lock->flk_lock);
1801 		return (0);
1802 	}
1803 
1804 	/*
1805 	 * If poll() has not found any events yet, set up event cell
1806 	 * to wake up the poll if a requested event occurs on this
1807 	 * pipe/fifo.
1808 	 */
1809 	if (!anyyet) {
1810 		if (events & POLLWRNORM)
1811 			fnp->fn_flag |= FIFOPOLLW;
1812 		if (events & (POLLIN | POLLRDNORM))
1813 			fnp->fn_flag |= FIFOPOLLR;
1814 		if (events & POLLRDBAND)
1815 			fnp->fn_flag |= FIFOPOLLRBAND;
1816 		/*
1817 		 * XXX Don't like exposing this from streams
1818 		 */
1819 		*phpp = &stp->sd_pollist;
1820 	}
1821 	mutex_exit(&fnp->fn_lock->flk_lock);
1822 	return (0);
1823 stream_mode:
1824 	return (strpoll(stp, events, anyyet, reventsp, phpp));
1825 }
1826 
1827 /*
1828  * POSIX pathconf() support.
1829  */
1830 /* ARGSUSED */
1831 int
1832 fifo_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr)
1833 {
1834 	ulong_t val;
1835 	int error = 0;
1836 
1837 	switch (cmd) {
1838 
1839 	case _PC_LINK_MAX:
1840 		val = MAXLINK;
1841 		break;
1842 
1843 	case _PC_MAX_CANON:
1844 		val = MAX_CANON;
1845 		break;
1846 
1847 	case _PC_MAX_INPUT:
1848 		val = MAX_INPUT;
1849 		break;
1850 
1851 	case _PC_NAME_MAX:
1852 		error = EINVAL;
1853 		break;
1854 
1855 	case _PC_PATH_MAX:
1856 	case _PC_SYMLINK_MAX:
1857 		val = MAXPATHLEN;
1858 		break;
1859 
1860 	case _PC_PIPE_BUF:
1861 		val = PIPE_BUF;
1862 		break;
1863 
1864 	case _PC_NO_TRUNC:
1865 		if (vp->v_vfsp->vfs_flag & VFS_NOTRUNC)
1866 			val = 1;	/* NOTRUNC is enabled for vp */
1867 		else
1868 			val = (ulong_t)-1;
1869 		break;
1870 
1871 	case _PC_VDISABLE:
1872 		val = _POSIX_VDISABLE;
1873 		break;
1874 
1875 	case _PC_CHOWN_RESTRICTED:
1876 		if (rstchown)
1877 			val = rstchown;		/* chown restricted enabled */
1878 		else
1879 			val = (ulong_t)-1;
1880 		break;
1881 
1882 	case _PC_FILESIZEBITS:
1883 		val = (ulong_t)-1;
1884 		break;
1885 
1886 	default:
1887 		if (VTOF(vp)->fn_realvp)
1888 			error = VOP_PATHCONF(VTOF(vp)->fn_realvp, cmd,
1889 			    &val, cr);
1890 		else
1891 			error = EINVAL;
1892 		break;
1893 	}
1894 
1895 	if (error == 0)
1896 		*valp = val;
1897 	return (error);
1898 }
1899 
1900 /*
1901  * If shadowing a vnode, apply VOP_SETSECATTR to it.
1902  * Otherwise, return NOSYS.
1903  */
1904 int
1905 fifo_setsecattr(struct vnode *vp, vsecattr_t *vsap, int flag, struct cred *crp)
1906 {
1907 	int error;
1908 
1909 	/*
1910 	 * The acl(2) system call tries to grab the write lock on the
1911 	 * file when setting an ACL, but fifofs does not implement
1912 	 * VOP_RWLOCK or VOP_RWUNLOCK, so we do it here instead.
1913 	 */
1914 	if (VTOF(vp)->fn_realvp) {
1915 		(void) VOP_RWLOCK(VTOF(vp)->fn_realvp, V_WRITELOCK_TRUE, NULL);
1916 		error = VOP_SETSECATTR(VTOF(vp)->fn_realvp, vsap, flag, crp);
1917 		VOP_RWUNLOCK(VTOF(vp)->fn_realvp, V_WRITELOCK_TRUE, NULL);
1918 		return (error);
1919 	} else
1920 		return (fs_nosys());
1921 }
1922 
1923 /*
1924  * If shadowing a vnode, apply VOP_GETSECATTR to it. Otherwise, fabricate
1925  * an ACL from the permission bits that fifo_getattr() makes up.
1926  */
1927 int
1928 fifo_getsecattr(struct vnode *vp, vsecattr_t *vsap, int flag, struct cred *crp)
1929 {
1930 	if (VTOF(vp)->fn_realvp)
1931 		return (VOP_GETSECATTR(VTOF(vp)->fn_realvp, vsap, flag, crp));
1932 	else
1933 		return (fs_fab_acl(vp, vsap, flag, crp));
1934 }
1935 
1936 
1937 /*
1938  * Set the FIFOSTAYFAST flag so nobody can turn the fifo into stream mode.
1939  * If the flag is already set then wait until it is removed - releasing
1940  * the lock.
1941  * If the fifo switches into stream mode while we are waiting, return failure.
1942  */
1943 static boolean_t
1944 fifo_stayfast_enter(fifonode_t *fnp)
1945 {
1946 	ASSERT(MUTEX_HELD(&fnp->fn_lock->flk_lock));
1947 	while (fnp->fn_flag & FIFOSTAYFAST) {
1948 		fnp->fn_flag |= FIFOWAITMODE;
1949 		cv_wait(&fnp->fn_wait_cv, &fnp->fn_lock->flk_lock);
1950 		fnp->fn_flag &= ~FIFOWAITMODE;
1951 	}
1952 	if (!(fnp->fn_flag & FIFOFAST))
1953 		return (B_FALSE);
1954 
1955 	fnp->fn_flag |= FIFOSTAYFAST;
1956 	return (B_TRUE);
1957 }
1958 
1959 /*
1960  * Unset the FIFOSTAYFAST flag and notify anybody waiting for this flag
1961  * to be removed:
1962  *	- threads wanting to turn into stream mode waiting in fifo_fastoff(),
1963  *	- other writers threads waiting in fifo_stayfast_enter().
1964  */
1965 static void
1966 fifo_stayfast_exit(fifonode_t *fnp)
1967 {
1968 	fifonode_t *fn_dest = fnp->fn_dest;
1969 
1970 	ASSERT(MUTEX_HELD(&fnp->fn_lock->flk_lock));
1971 
1972 	fnp->fn_flag &= ~FIFOSTAYFAST;
1973 
1974 	if (fnp->fn_flag & FIFOWAITMODE)
1975 		cv_broadcast(&fnp->fn_wait_cv);
1976 
1977 	if ((fnp->fn_flag & ISPIPE) && (fn_dest->fn_flag & FIFOWAITMODE))
1978 		cv_broadcast(&fn_dest->fn_wait_cv);
1979 }
1980