17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
56fc927dcSgfaden  * Common Development and Distribution License (the "License").
66fc927dcSgfaden  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
227c478bd9Sstevel@tonic-gate 
237c478bd9Sstevel@tonic-gate /*
24*0fbb751dSJohn Levon  * Copyright (c) 1988, 2010, Oracle and/or its affiliates. All rights reserved.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*
287c478bd9Sstevel@tonic-gate  * The routines defined in this file are supporting routines for FIFOFS
29da6c28aaSamw  * file system type.
307c478bd9Sstevel@tonic-gate  */
317c478bd9Sstevel@tonic-gate #include <sys/types.h>
327c478bd9Sstevel@tonic-gate #include <sys/param.h>
337c478bd9Sstevel@tonic-gate #include <sys/systm.h>
347c478bd9Sstevel@tonic-gate #include <sys/debug.h>
357c478bd9Sstevel@tonic-gate #include <sys/errno.h>
367c478bd9Sstevel@tonic-gate #include <sys/time.h>
377c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
387c478bd9Sstevel@tonic-gate #include <sys/inline.h>
397c478bd9Sstevel@tonic-gate #include <sys/file.h>
407c478bd9Sstevel@tonic-gate #include <sys/proc.h>
417c478bd9Sstevel@tonic-gate #include <sys/stat.h>
427c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
437c478bd9Sstevel@tonic-gate #include <sys/var.h>
447c478bd9Sstevel@tonic-gate #include <sys/vfs.h>
45aa59c4cbSrsb #include <sys/vfs_opreg.h>
467c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
477c478bd9Sstevel@tonic-gate #include <sys/mode.h>
487c478bd9Sstevel@tonic-gate #include <sys/signal.h>
497c478bd9Sstevel@tonic-gate #include <sys/user.h>
507c478bd9Sstevel@tonic-gate #include <sys/uio.h>
517c478bd9Sstevel@tonic-gate #include <sys/flock.h>
527c478bd9Sstevel@tonic-gate #include <sys/stream.h>
537c478bd9Sstevel@tonic-gate #include <sys/fs/fifonode.h>
547c478bd9Sstevel@tonic-gate #include <sys/strsubr.h>
557c478bd9Sstevel@tonic-gate #include <sys/stropts.h>
567c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
577c478bd9Sstevel@tonic-gate #include <fs/fs_subr.h>
587c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate #if FIFODEBUG
627c478bd9Sstevel@tonic-gate int Fifo_fastmode = 1;		/* pipes/fifos will be opened in fast mode */
637c478bd9Sstevel@tonic-gate int Fifo_verbose = 0;		/* msg when switching out of fast mode */
647c478bd9Sstevel@tonic-gate int Fifohiwat = FIFOHIWAT;	/* Modifiable FIFO high water mark */
657c478bd9Sstevel@tonic-gate #endif
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate /*
687c478bd9Sstevel@tonic-gate  * This is the loadable module wrapper.
697c478bd9Sstevel@tonic-gate  */
707c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate extern struct qinit fifo_strdata;
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate struct vfsops *fifo_vfsops;
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate static vfsdef_t vfw = {
777c478bd9Sstevel@tonic-gate 	VFSDEF_VERSION,
787c478bd9Sstevel@tonic-gate 	"fifofs",
797c478bd9Sstevel@tonic-gate 	fifoinit,
80*0fbb751dSJohn Levon 	VSW_ZMOUNT,
817c478bd9Sstevel@tonic-gate 	NULL
827c478bd9Sstevel@tonic-gate };
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate /*
857c478bd9Sstevel@tonic-gate  * Module linkage information for the kernel.
867c478bd9Sstevel@tonic-gate  */
877c478bd9Sstevel@tonic-gate extern struct mod_ops mod_fsops;
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate static struct modlfs modlfs = {
907c478bd9Sstevel@tonic-gate 	&mod_fsops, "filesystem for fifo", &vfw
917c478bd9Sstevel@tonic-gate };
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
947c478bd9Sstevel@tonic-gate 	MODREV_1, (void *)&modlfs, NULL
957c478bd9Sstevel@tonic-gate };
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate int
_init()987c478bd9Sstevel@tonic-gate _init()
997c478bd9Sstevel@tonic-gate {
1007c478bd9Sstevel@tonic-gate 	return (mod_install(&modlinkage));
1017c478bd9Sstevel@tonic-gate }
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate int
_info(struct modinfo * modinfop)1047c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
1057c478bd9Sstevel@tonic-gate {
1067c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
1077c478bd9Sstevel@tonic-gate }
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate /*
1107c478bd9Sstevel@tonic-gate  * Define data structures within this file.
1117c478bd9Sstevel@tonic-gate  * XXX should the hash size be configurable ?
1127c478bd9Sstevel@tonic-gate  */
1137c478bd9Sstevel@tonic-gate #define	FIFOSHFT	5
1147c478bd9Sstevel@tonic-gate #define	FIFO_HASHSZ	63
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate #if ((FIFO_HASHSZ & (FIFO_HASHSZ - 1)) == 0)
1177c478bd9Sstevel@tonic-gate #define	FIFOHASH(vp) (((uintptr_t)(vp) >> FIFOSHFT) & (FIFO_HASHSZ - 1))
1187c478bd9Sstevel@tonic-gate #else
1197c478bd9Sstevel@tonic-gate #define	FIFOHASH(vp) (((uintptr_t)(vp) >> FIFOSHFT) % FIFO_HASHSZ)
1207c478bd9Sstevel@tonic-gate #endif
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate fifonode_t	*fifoalloc[FIFO_HASHSZ];
1237c478bd9Sstevel@tonic-gate dev_t		fifodev;
1247c478bd9Sstevel@tonic-gate struct vfs	*fifovfsp;
1257c478bd9Sstevel@tonic-gate int		fifofstype;
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate kmutex_t ftable_lock;
1287c478bd9Sstevel@tonic-gate static kmutex_t fino_lock;
1297c478bd9Sstevel@tonic-gate struct kmem_cache *fnode_cache;
1307c478bd9Sstevel@tonic-gate struct kmem_cache *pipe_cache;
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate static void fifoinsert(fifonode_t *);
1337c478bd9Sstevel@tonic-gate static fifonode_t *fifofind(vnode_t *);
1347c478bd9Sstevel@tonic-gate static int fifo_connld(struct vnode **, int, cred_t *);
1357c478bd9Sstevel@tonic-gate static void fifo_fastturnoff(fifonode_t *);
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate static void fifo_reinit_vp(vnode_t *);
1387c478bd9Sstevel@tonic-gate 
139b5fca8f8Stomee static void fnode_destructor(void *, void *);
140b5fca8f8Stomee 
1417c478bd9Sstevel@tonic-gate /*
1427c478bd9Sstevel@tonic-gate  * Constructor/destructor routines for fifos and pipes.
1437c478bd9Sstevel@tonic-gate  *
1447c478bd9Sstevel@tonic-gate  * In the interest of code sharing, we define a common fifodata structure
1457c478bd9Sstevel@tonic-gate  * which consists of a fifolock and one or two fnodes.  A fifo contains
1467c478bd9Sstevel@tonic-gate  * one fnode; a pipe contains two.  The fifolock is shared by the fnodes,
1477c478bd9Sstevel@tonic-gate  * each of which points to it:
1487c478bd9Sstevel@tonic-gate  *
1497c478bd9Sstevel@tonic-gate  *	--> -->	---------  --- ---
1507c478bd9Sstevel@tonic-gate  *	|   |	| lock	|   |	|
1517c478bd9Sstevel@tonic-gate  *	|   |	---------   |	|
1527c478bd9Sstevel@tonic-gate  *	|   |	|	|  fifo	|
1537c478bd9Sstevel@tonic-gate  *	|   --- | fnode	|   |	|
1547c478bd9Sstevel@tonic-gate  *	|	|	|   |  pipe
1557c478bd9Sstevel@tonic-gate  *	|	---------  ---	|
1567c478bd9Sstevel@tonic-gate  *	|	|	|	|
1577c478bd9Sstevel@tonic-gate  *	------- | fnode	|	|
1587c478bd9Sstevel@tonic-gate  *		|	|	|
1597c478bd9Sstevel@tonic-gate  *		---------      ---
1607c478bd9Sstevel@tonic-gate  *
1617c478bd9Sstevel@tonic-gate  * Since the fifolock is at the beginning of the fifodata structure,
1627c478bd9Sstevel@tonic-gate  * the fifolock address is the same as the fifodata address.  Thus,
1637c478bd9Sstevel@tonic-gate  * we can determine the fifodata address from any of its member fnodes.
1647c478bd9Sstevel@tonic-gate  * This is essential for fifo_inactive.
1657c478bd9Sstevel@tonic-gate  *
166da6c28aaSamw  * The fnode constructor is designed to handle any fifodata structure,
1677c478bd9Sstevel@tonic-gate  * deducing the number of fnodes from the total size.  Thus, the fnode
1687c478bd9Sstevel@tonic-gate  * constructor does most of the work for the pipe constructor.
1697c478bd9Sstevel@tonic-gate  */
1707c478bd9Sstevel@tonic-gate static int
fnode_constructor(void * buf,void * cdrarg,int kmflags)1717c478bd9Sstevel@tonic-gate fnode_constructor(void *buf, void *cdrarg, int kmflags)
1727c478bd9Sstevel@tonic-gate {
1737c478bd9Sstevel@tonic-gate 	fifodata_t *fdp = buf;
1747c478bd9Sstevel@tonic-gate 	fifolock_t *flp = &fdp->fifo_lock;
1757c478bd9Sstevel@tonic-gate 	fifonode_t *fnp = &fdp->fifo_fnode[0];
1767c478bd9Sstevel@tonic-gate 	size_t size = (uintptr_t)cdrarg;
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate 	mutex_init(&flp->flk_lock, NULL, MUTEX_DEFAULT, NULL);
1797c478bd9Sstevel@tonic-gate 	cv_init(&flp->flk_wait_cv, NULL, CV_DEFAULT, NULL);
1807c478bd9Sstevel@tonic-gate 	flp->flk_ocsync = 0;
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 	while ((char *)fnp < (char *)buf + size) {
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 		vnode_t *vp;
1857c478bd9Sstevel@tonic-gate 
186b5fca8f8Stomee 		vp = vn_alloc(kmflags);
187b5fca8f8Stomee 		if (vp == NULL) {
188b5fca8f8Stomee 			fnp->fn_vnode = NULL; /* mark for destructor */
189b5fca8f8Stomee 			fnode_destructor(buf, cdrarg);
190b5fca8f8Stomee 			return (-1);
191b5fca8f8Stomee 		}
1927c478bd9Sstevel@tonic-gate 		fnp->fn_vnode = vp;
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate 		fnp->fn_lock = flp;
1957c478bd9Sstevel@tonic-gate 		fnp->fn_open = 0;
1967c478bd9Sstevel@tonic-gate 		fnp->fn_dest = fnp;
1977c478bd9Sstevel@tonic-gate 		fnp->fn_mp = NULL;
1987c478bd9Sstevel@tonic-gate 		fnp->fn_count = 0;
1997c478bd9Sstevel@tonic-gate 		fnp->fn_rsynccnt = 0;
2007c478bd9Sstevel@tonic-gate 		fnp->fn_wsynccnt = 0;
2017c478bd9Sstevel@tonic-gate 		fnp->fn_wwaitcnt = 0;
2027c478bd9Sstevel@tonic-gate 		fnp->fn_insync = 0;
2037c478bd9Sstevel@tonic-gate 		fnp->fn_pcredp = NULL;
2047c478bd9Sstevel@tonic-gate 		fnp->fn_cpid = -1;
2057c478bd9Sstevel@tonic-gate 		/*
2067c478bd9Sstevel@tonic-gate 		 * 32-bit stat(2) may fail if fn_ino isn't initialized
2077c478bd9Sstevel@tonic-gate 		 */
2087c478bd9Sstevel@tonic-gate 		fnp->fn_ino = 0;
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 		cv_init(&fnp->fn_wait_cv, NULL, CV_DEFAULT, NULL);
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 		vn_setops(vp, fifo_vnodeops);
2137c478bd9Sstevel@tonic-gate 		vp->v_stream = NULL;
2147c478bd9Sstevel@tonic-gate 		vp->v_type = VFIFO;
2157c478bd9Sstevel@tonic-gate 		vp->v_data = (caddr_t)fnp;
2167c478bd9Sstevel@tonic-gate 		vp->v_flag = VNOMAP | VNOSWAP;
2177c478bd9Sstevel@tonic-gate 		vn_exists(vp);
2187c478bd9Sstevel@tonic-gate 		fnp++;
2197c478bd9Sstevel@tonic-gate 	}
2207c478bd9Sstevel@tonic-gate 	return (0);
2217c478bd9Sstevel@tonic-gate }
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate static void
fnode_destructor(void * buf,void * cdrarg)2247c478bd9Sstevel@tonic-gate fnode_destructor(void *buf, void *cdrarg)
2257c478bd9Sstevel@tonic-gate {
2267c478bd9Sstevel@tonic-gate 	fifodata_t *fdp = buf;
2277c478bd9Sstevel@tonic-gate 	fifolock_t *flp = &fdp->fifo_lock;
2287c478bd9Sstevel@tonic-gate 	fifonode_t *fnp = &fdp->fifo_fnode[0];
2297c478bd9Sstevel@tonic-gate 	size_t size = (uintptr_t)cdrarg;
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 	mutex_destroy(&flp->flk_lock);
2327c478bd9Sstevel@tonic-gate 	cv_destroy(&flp->flk_wait_cv);
2337c478bd9Sstevel@tonic-gate 	ASSERT(flp->flk_ocsync == 0);
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 	while ((char *)fnp < (char *)buf + size) {
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 		vnode_t *vp = FTOV(fnp);
2387c478bd9Sstevel@tonic-gate 
239b5fca8f8Stomee 		if (vp == NULL) {
240b5fca8f8Stomee 			return; /* constructor failed here */
241b5fca8f8Stomee 		}
242b5fca8f8Stomee 
2437c478bd9Sstevel@tonic-gate 		ASSERT(fnp->fn_mp == NULL);
2447c478bd9Sstevel@tonic-gate 		ASSERT(fnp->fn_count == 0);
2457c478bd9Sstevel@tonic-gate 		ASSERT(fnp->fn_lock == flp);
2467c478bd9Sstevel@tonic-gate 		ASSERT(fnp->fn_open == 0);
2477c478bd9Sstevel@tonic-gate 		ASSERT(fnp->fn_insync == 0);
2487c478bd9Sstevel@tonic-gate 		ASSERT(fnp->fn_rsynccnt == 0 && fnp->fn_wsynccnt == 0);
2497c478bd9Sstevel@tonic-gate 		ASSERT(fnp->fn_wwaitcnt == 0);
2507c478bd9Sstevel@tonic-gate 		ASSERT(fnp->fn_pcredp == NULL);
2517c478bd9Sstevel@tonic-gate 		ASSERT(vn_matchops(vp, fifo_vnodeops));
2527c478bd9Sstevel@tonic-gate 		ASSERT(vp->v_stream == NULL);
2537c478bd9Sstevel@tonic-gate 		ASSERT(vp->v_type == VFIFO);
2547c478bd9Sstevel@tonic-gate 		ASSERT(vp->v_data == (caddr_t)fnp);
2557c478bd9Sstevel@tonic-gate 		ASSERT((vp->v_flag & (VNOMAP|VNOSWAP)) == (VNOMAP|VNOSWAP));
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate 		cv_destroy(&fnp->fn_wait_cv);
2587c478bd9Sstevel@tonic-gate 		vn_invalid(vp);
2597c478bd9Sstevel@tonic-gate 		vn_free(vp);
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate 		fnp++;
2627c478bd9Sstevel@tonic-gate 	}
2637c478bd9Sstevel@tonic-gate }
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate static int
pipe_constructor(void * buf,void * cdrarg,int kmflags)2667c478bd9Sstevel@tonic-gate pipe_constructor(void *buf, void *cdrarg, int kmflags)
2677c478bd9Sstevel@tonic-gate {
2687c478bd9Sstevel@tonic-gate 	fifodata_t *fdp = buf;
2697c478bd9Sstevel@tonic-gate 	fifonode_t *fnp1 = &fdp->fifo_fnode[0];
2707c478bd9Sstevel@tonic-gate 	fifonode_t *fnp2 = &fdp->fifo_fnode[1];
2717c478bd9Sstevel@tonic-gate 	vnode_t *vp1;
2727c478bd9Sstevel@tonic-gate 	vnode_t *vp2;
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 	(void) fnode_constructor(buf, cdrarg, kmflags);
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate 	vp1 = FTOV(fnp1);
2777c478bd9Sstevel@tonic-gate 	vp2 = FTOV(fnp2);
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate 	vp1->v_vfsp	= vp2->v_vfsp		= fifovfsp;
2807c478bd9Sstevel@tonic-gate 	vp1->v_rdev	= vp2->v_rdev		= fifodev;
2817c478bd9Sstevel@tonic-gate 	fnp1->fn_realvp	= fnp2->fn_realvp	= NULL;
2827c478bd9Sstevel@tonic-gate 	fnp1->fn_dest	= fnp2;
2837c478bd9Sstevel@tonic-gate 	fnp2->fn_dest	= fnp1;
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate 	return (0);
2867c478bd9Sstevel@tonic-gate }
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate static void
pipe_destructor(void * buf,void * cdrarg)2897c478bd9Sstevel@tonic-gate pipe_destructor(void *buf, void *cdrarg)
2907c478bd9Sstevel@tonic-gate {
2917c478bd9Sstevel@tonic-gate #ifdef DEBUG
2927c478bd9Sstevel@tonic-gate 	fifodata_t *fdp = buf;
2937c478bd9Sstevel@tonic-gate 	fifonode_t *fnp1 = &fdp->fifo_fnode[0];
2947c478bd9Sstevel@tonic-gate 	fifonode_t *fnp2 = &fdp->fifo_fnode[1];
2957c478bd9Sstevel@tonic-gate 	vnode_t *vp1 = FTOV(fnp1);
2967c478bd9Sstevel@tonic-gate 	vnode_t *vp2 = FTOV(fnp2);
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 	ASSERT(vp1->v_vfsp == fifovfsp);
2997c478bd9Sstevel@tonic-gate 	ASSERT(vp2->v_vfsp == fifovfsp);
3007c478bd9Sstevel@tonic-gate 	ASSERT(vp1->v_rdev == fifodev);
3017c478bd9Sstevel@tonic-gate 	ASSERT(vp2->v_rdev == fifodev);
3027c478bd9Sstevel@tonic-gate #endif
3037c478bd9Sstevel@tonic-gate 	fnode_destructor(buf, cdrarg);
3047c478bd9Sstevel@tonic-gate }
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate /*
3077c478bd9Sstevel@tonic-gate  * Reinitialize a FIFO vnode (uses normal vnode reinit, but ensures that
3087c478bd9Sstevel@tonic-gate  * vnode type and flags are reset).
3097c478bd9Sstevel@tonic-gate  */
3107c478bd9Sstevel@tonic-gate 
fifo_reinit_vp(vnode_t * vp)3117c478bd9Sstevel@tonic-gate static void fifo_reinit_vp(vnode_t *vp)
3127c478bd9Sstevel@tonic-gate {
3137c478bd9Sstevel@tonic-gate 	vn_reinit(vp);
3147c478bd9Sstevel@tonic-gate 	vp->v_type = VFIFO;
3159acbbeafSnn 	vp->v_flag &= VROOT;
3169acbbeafSnn 	vp->v_flag |= VNOMAP | VNOSWAP;
3177c478bd9Sstevel@tonic-gate }
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate /*
3207c478bd9Sstevel@tonic-gate  * Save file system type/index, initialize vfs operations vector, get
3217c478bd9Sstevel@tonic-gate  * unique device number for FIFOFS and initialize the FIFOFS hash.
3227c478bd9Sstevel@tonic-gate  * Create and initialize a "generic" vfs pointer that will be placed
3237c478bd9Sstevel@tonic-gate  * in the v_vfsp field of each pipe's vnode.
3247c478bd9Sstevel@tonic-gate  */
3257c478bd9Sstevel@tonic-gate int
fifoinit(int fstype,char * name)3267c478bd9Sstevel@tonic-gate fifoinit(int fstype, char *name)
3277c478bd9Sstevel@tonic-gate {
3287c478bd9Sstevel@tonic-gate 	static const fs_operation_def_t fifo_vfsops_template[] = {
3297c478bd9Sstevel@tonic-gate 		NULL, NULL
3307c478bd9Sstevel@tonic-gate 	};
3317c478bd9Sstevel@tonic-gate 	int error;
3327c478bd9Sstevel@tonic-gate 	major_t dev;
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate 	fifofstype = fstype;
3357c478bd9Sstevel@tonic-gate 	error = vfs_setfsops(fstype, fifo_vfsops_template, &fifo_vfsops);
3367c478bd9Sstevel@tonic-gate 	if (error != 0) {
3377c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "fifoinit: bad vfs ops template");
3387c478bd9Sstevel@tonic-gate 		return (error);
3397c478bd9Sstevel@tonic-gate 	}
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 	error = vn_make_ops(name, fifo_vnodeops_template, &fifo_vnodeops);
3427c478bd9Sstevel@tonic-gate 	if (error != 0) {
3437c478bd9Sstevel@tonic-gate 		(void) vfs_freevfsops_by_type(fstype);
3447c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "fifoinit: bad vnode ops template");
3457c478bd9Sstevel@tonic-gate 		return (error);
3467c478bd9Sstevel@tonic-gate 	}
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate 	if ((dev = getudev()) == (major_t)-1) {
3497c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "fifoinit: can't get unique device number");
3507c478bd9Sstevel@tonic-gate 		dev = 0;
3517c478bd9Sstevel@tonic-gate 	}
3527c478bd9Sstevel@tonic-gate 	fifodev = makedevice(dev, 0);
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate 	fifovfsp = kmem_zalloc(sizeof (struct vfs), KM_SLEEP);
3557c478bd9Sstevel@tonic-gate 	fifovfsp->vfs_next = NULL;
3567c478bd9Sstevel@tonic-gate 	vfs_setops(fifovfsp, fifo_vfsops);
3577c478bd9Sstevel@tonic-gate 	fifovfsp->vfs_vnodecovered = NULL;
3587c478bd9Sstevel@tonic-gate 	fifovfsp->vfs_flag = 0;
3597c478bd9Sstevel@tonic-gate 	fifovfsp->vfs_bsize = 1024;
3607c478bd9Sstevel@tonic-gate 	fifovfsp->vfs_fstype = fifofstype;
3617c478bd9Sstevel@tonic-gate 	vfs_make_fsid(&fifovfsp->vfs_fsid, fifodev, fifofstype);
3627c478bd9Sstevel@tonic-gate 	fifovfsp->vfs_data = NULL;
3637c478bd9Sstevel@tonic-gate 	fifovfsp->vfs_dev = fifodev;
3647c478bd9Sstevel@tonic-gate 	fifovfsp->vfs_bcount = 0;
3657c478bd9Sstevel@tonic-gate 
366c87e4823Svk 	/*
367c87e4823Svk 	 * It is necessary to initialize vfs_count here to 1.
368c87e4823Svk 	 * This prevents the fifovfsp from getting freed when
369c87e4823Svk 	 * a thread does a VFS_HOLD followed by a VFS_RELE
370c87e4823Svk 	 * on the fifovfsp
371c87e4823Svk 	 *
372c87e4823Svk 	 * The fifovfsp should never be freed.
373c87e4823Svk 	 */
374c87e4823Svk 	fifovfsp->vfs_count = 1;
375c87e4823Svk 
3767c478bd9Sstevel@tonic-gate 	mutex_init(&ftable_lock, NULL, MUTEX_DEFAULT, NULL);
3777c478bd9Sstevel@tonic-gate 	mutex_init(&fino_lock, NULL, MUTEX_DEFAULT, NULL);
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate 	/*
3807c478bd9Sstevel@tonic-gate 	 * vnodes are cached aligned
3817c478bd9Sstevel@tonic-gate 	 */
3827c478bd9Sstevel@tonic-gate 	fnode_cache = kmem_cache_create("fnode_cache",
3835056eb0dSbpramod 	    sizeof (fifodata_t) - sizeof (fifonode_t), 32,
3845056eb0dSbpramod 	    fnode_constructor, fnode_destructor, NULL,
3855056eb0dSbpramod 	    (void *)(sizeof (fifodata_t) - sizeof (fifonode_t)), NULL, 0);
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate 	pipe_cache = kmem_cache_create("pipe_cache", sizeof (fifodata_t), 32,
3885056eb0dSbpramod 	    pipe_constructor, pipe_destructor, NULL,
3895056eb0dSbpramod 	    (void *)(sizeof (fifodata_t)), NULL, 0);
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate #if FIFODEBUG
3927c478bd9Sstevel@tonic-gate 	if (Fifohiwat < FIFOHIWAT)
3937c478bd9Sstevel@tonic-gate 		Fifohiwat = FIFOHIWAT;
3947c478bd9Sstevel@tonic-gate #endif /* FIFODEBUG */
3957c478bd9Sstevel@tonic-gate 	fifo_strdata.qi_minfo->mi_hiwat = Fifohiwat;
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate 	return (0);
3987c478bd9Sstevel@tonic-gate }
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate /*
4017e94462bSpeterte  * Provide a shadow for a vnode.  We create a new shadow before checking for an
4027e94462bSpeterte  * existing one, to minimize the amount of time we need to hold ftable_lock.
4037e94462bSpeterte  * If a vp already has a shadow in the hash list, return its shadow.  If not,
4047e94462bSpeterte  * we hash the new vnode and return its pointer to the caller.
4057c478bd9Sstevel@tonic-gate  */
4067c478bd9Sstevel@tonic-gate vnode_t *
fifovp(vnode_t * vp,cred_t * crp)4077c478bd9Sstevel@tonic-gate fifovp(vnode_t *vp, cred_t *crp)
4087c478bd9Sstevel@tonic-gate {
4097c478bd9Sstevel@tonic-gate 	fifonode_t *fnp;
4107e94462bSpeterte 	fifonode_t *spec_fnp;   /* Speculative fnode ptr. */
4117c478bd9Sstevel@tonic-gate 	fifodata_t *fdp;
4127c478bd9Sstevel@tonic-gate 	vnode_t *newvp;
4137c478bd9Sstevel@tonic-gate 	struct vattr va;
414ae063e84Snr 	vnode_t	*rvp;
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate 	ASSERT(vp != NULL);
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate 	fdp = kmem_cache_alloc(fnode_cache, KM_SLEEP);
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate 	fdp->fifo_lock.flk_ref = 1;
4217c478bd9Sstevel@tonic-gate 	fnp = &fdp->fifo_fnode[0];
4226fc927dcSgfaden 
4236fc927dcSgfaden 	/*
424ae063e84Snr 	 * Its possible that fifo nodes on different lofs mountpoints
425ae063e84Snr 	 * shadow the same real filesystem fifo node.
426ae063e84Snr 	 * In this case its necessary to get and store the realvp.
427ae063e84Snr 	 * This way different fifo nodes sharing the same real vnode
428ae063e84Snr 	 * can use realvp for communication.
4296fc927dcSgfaden 	 */
430da6c28aaSamw 
431da6c28aaSamw 	if (VOP_REALVP(vp, &rvp, NULL) == 0)
432da6c28aaSamw 			vp = rvp;
4336fc927dcSgfaden 
4347c478bd9Sstevel@tonic-gate 	fnp->fn_realvp	= vp;
4357c478bd9Sstevel@tonic-gate 	fnp->fn_wcnt	= 0;
4367c478bd9Sstevel@tonic-gate 	fnp->fn_rcnt	= 0;
4377e94462bSpeterte 
4387c478bd9Sstevel@tonic-gate #if FIFODEBUG
4397c478bd9Sstevel@tonic-gate 	if (! Fifo_fastmode) {
4407c478bd9Sstevel@tonic-gate 		fnp->fn_flag	= 0;
4417c478bd9Sstevel@tonic-gate 	} else {
4427c478bd9Sstevel@tonic-gate 		fnp->fn_flag	= FIFOFAST;
4437c478bd9Sstevel@tonic-gate 	}
4447c478bd9Sstevel@tonic-gate #else /* FIFODEBUG */
4457c478bd9Sstevel@tonic-gate 	fnp->fn_flag	= FIFOFAST;
4467c478bd9Sstevel@tonic-gate #endif /* FIFODEBUG */
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate 	/*
4497c478bd9Sstevel@tonic-gate 	 * initialize the times from vp.
4507c478bd9Sstevel@tonic-gate 	 */
4517c478bd9Sstevel@tonic-gate 	va.va_mask = AT_TIMES;
452da6c28aaSamw 	if (VOP_GETATTR(vp, &va, 0, crp, NULL) == 0) {
4537c478bd9Sstevel@tonic-gate 		fnp->fn_atime = va.va_atime.tv_sec;
4547c478bd9Sstevel@tonic-gate 		fnp->fn_mtime = va.va_mtime.tv_sec;
4557c478bd9Sstevel@tonic-gate 		fnp->fn_ctime = va.va_ctime.tv_sec;
4567c478bd9Sstevel@tonic-gate 	} else {
4577c478bd9Sstevel@tonic-gate 		fnp->fn_atime = 0;
4587c478bd9Sstevel@tonic-gate 		fnp->fn_mtime = 0;
4597c478bd9Sstevel@tonic-gate 		fnp->fn_ctime = 0;
4607c478bd9Sstevel@tonic-gate 	}
4617c478bd9Sstevel@tonic-gate 
4627e94462bSpeterte 	/*
4637e94462bSpeterte 	 * Grab the VP here to avoid holding locks
4647e94462bSpeterte 	 * whilst trying to acquire others.
4657e94462bSpeterte 	 */
4667e94462bSpeterte 
4677c478bd9Sstevel@tonic-gate 	VN_HOLD(vp);
4687c478bd9Sstevel@tonic-gate 
4697e94462bSpeterte 	mutex_enter(&ftable_lock);
4707e94462bSpeterte 
4717e94462bSpeterte 	if ((spec_fnp = fifofind(vp)) != NULL) {
4727e94462bSpeterte 		mutex_exit(&ftable_lock);
4737e94462bSpeterte 
4747e94462bSpeterte 		/*
4757e94462bSpeterte 		 * Release the vnode and free up our pre-prepared fnode.
4767e94462bSpeterte 		 * Zero the lock reference just to explicitly signal
4777e94462bSpeterte 		 * this is unused.
4787e94462bSpeterte 		 */
4797e94462bSpeterte 		VN_RELE(vp);
4807e94462bSpeterte 		fdp->fifo_lock.flk_ref = 0;
4817e94462bSpeterte 		kmem_cache_free(fnode_cache, fdp);
4827e94462bSpeterte 
4837e94462bSpeterte 		return (FTOV(spec_fnp));
4847e94462bSpeterte 	}
4857e94462bSpeterte 
4867c478bd9Sstevel@tonic-gate 	newvp = FTOV(fnp);
4877c478bd9Sstevel@tonic-gate 	fifo_reinit_vp(newvp);
4885056eb0dSbpramod 	/*
489132c40dcSbpramod 	 * Since the fifo vnode's v_vfsp needs to point to the
490132c40dcSbpramod 	 * underlying filesystem's vfsp we need to bump up the
491132c40dcSbpramod 	 * underlying filesystem's vfs reference count.
492132c40dcSbpramod 	 * The count is decremented when the fifo node is
493132c40dcSbpramod 	 * inactivated.
4945056eb0dSbpramod 	 */
495132c40dcSbpramod 
496132c40dcSbpramod 	VFS_HOLD(vp->v_vfsp);
497132c40dcSbpramod 	newvp->v_vfsp = vp->v_vfsp;
498132c40dcSbpramod 	newvp->v_rdev = vp->v_rdev;
4999acbbeafSnn 	newvp->v_flag |= (vp->v_flag & VROOT);
5007c478bd9Sstevel@tonic-gate 
5017c478bd9Sstevel@tonic-gate 	fifoinsert(fnp);
5027c478bd9Sstevel@tonic-gate 	mutex_exit(&ftable_lock);
5037c478bd9Sstevel@tonic-gate 
5047c478bd9Sstevel@tonic-gate 	return (newvp);
5057c478bd9Sstevel@tonic-gate }
5067c478bd9Sstevel@tonic-gate 
5077c478bd9Sstevel@tonic-gate /*
5087c478bd9Sstevel@tonic-gate  * Create a pipe end by...
5097c478bd9Sstevel@tonic-gate  * allocating a vnode-fifonode pair and initializing the fifonode.
5107c478bd9Sstevel@tonic-gate  */
5117c478bd9Sstevel@tonic-gate void
makepipe(vnode_t ** vpp1,vnode_t ** vpp2)5127c478bd9Sstevel@tonic-gate makepipe(vnode_t **vpp1, vnode_t **vpp2)
5137c478bd9Sstevel@tonic-gate {
5147c478bd9Sstevel@tonic-gate 	fifonode_t *fnp1;
5157c478bd9Sstevel@tonic-gate 	fifonode_t *fnp2;
5167c478bd9Sstevel@tonic-gate 	vnode_t *nvp1;
5177c478bd9Sstevel@tonic-gate 	vnode_t *nvp2;
5187c478bd9Sstevel@tonic-gate 	fifodata_t *fdp;
5197c478bd9Sstevel@tonic-gate 	time_t now;
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate 	fdp = kmem_cache_alloc(pipe_cache, KM_SLEEP);
5227c478bd9Sstevel@tonic-gate 	fdp->fifo_lock.flk_ref = 2;
5237c478bd9Sstevel@tonic-gate 	fnp1 = &fdp->fifo_fnode[0];
5247c478bd9Sstevel@tonic-gate 	fnp2 = &fdp->fifo_fnode[1];
5257c478bd9Sstevel@tonic-gate 
5267c478bd9Sstevel@tonic-gate 	fnp1->fn_wcnt	= fnp2->fn_wcnt		= 1;
5277c478bd9Sstevel@tonic-gate 	fnp1->fn_rcnt	= fnp2->fn_rcnt		= 1;
5287c478bd9Sstevel@tonic-gate #if FIFODEBUG
5297c478bd9Sstevel@tonic-gate 	if (! Fifo_fastmode) {
5307c478bd9Sstevel@tonic-gate 		fnp1->fn_flag	= fnp2->fn_flag		= ISPIPE;
5317c478bd9Sstevel@tonic-gate 	} else {
5327c478bd9Sstevel@tonic-gate 		fnp1->fn_flag	= fnp2->fn_flag		= ISPIPE | FIFOFAST;
5337c478bd9Sstevel@tonic-gate 	}
5347c478bd9Sstevel@tonic-gate #else /* FIFODEBUG */
5357c478bd9Sstevel@tonic-gate 	fnp1->fn_flag	= fnp2->fn_flag		= ISPIPE | FIFOFAST;
5367c478bd9Sstevel@tonic-gate #endif /* FIFODEBUG */
5377c478bd9Sstevel@tonic-gate 	now = gethrestime_sec();
5387c478bd9Sstevel@tonic-gate 	fnp1->fn_atime	= fnp2->fn_atime	= now;
5397c478bd9Sstevel@tonic-gate 	fnp1->fn_mtime	= fnp2->fn_mtime	= now;
5407c478bd9Sstevel@tonic-gate 	fnp1->fn_ctime	= fnp2->fn_ctime	= now;
5417c478bd9Sstevel@tonic-gate 
5427c478bd9Sstevel@tonic-gate 	*vpp1 = nvp1 = FTOV(fnp1);
5437c478bd9Sstevel@tonic-gate 	*vpp2 = nvp2 = FTOV(fnp2);
5447c478bd9Sstevel@tonic-gate 
5457c478bd9Sstevel@tonic-gate 	fifo_reinit_vp(nvp1);		/* Reinitialize vnodes for reuse... */
5467c478bd9Sstevel@tonic-gate 	fifo_reinit_vp(nvp2);
5477c478bd9Sstevel@tonic-gate 	nvp1->v_vfsp = fifovfsp; 	/* Need to re-establish VFS & device */
5487c478bd9Sstevel@tonic-gate 	nvp2->v_vfsp = fifovfsp; 	/* before we can reuse this vnode. */
5497c478bd9Sstevel@tonic-gate 	nvp1->v_rdev = fifodev;
5507c478bd9Sstevel@tonic-gate 	nvp2->v_rdev = fifodev;
5517c478bd9Sstevel@tonic-gate }
5527c478bd9Sstevel@tonic-gate 
5537c478bd9Sstevel@tonic-gate /*
5547c478bd9Sstevel@tonic-gate  * Attempt to establish a unique pipe id.  Only un-named pipes use this
5557c478bd9Sstevel@tonic-gate  * routine.
5567c478bd9Sstevel@tonic-gate  */
5577c478bd9Sstevel@tonic-gate ino_t
fifogetid(void)5587c478bd9Sstevel@tonic-gate fifogetid(void)
5597c478bd9Sstevel@tonic-gate {
5607c478bd9Sstevel@tonic-gate 	static ino_t fifo_ino = 0;
5617c478bd9Sstevel@tonic-gate 	ino_t fino;
5627c478bd9Sstevel@tonic-gate 
5637c478bd9Sstevel@tonic-gate 	mutex_enter(&fino_lock);
5647c478bd9Sstevel@tonic-gate 	fino = fifo_ino++;
5657c478bd9Sstevel@tonic-gate 	mutex_exit(&fino_lock);
5667c478bd9Sstevel@tonic-gate 	return (fino);
5677c478bd9Sstevel@tonic-gate }
5687c478bd9Sstevel@tonic-gate 
5697c478bd9Sstevel@tonic-gate 
5707c478bd9Sstevel@tonic-gate /*
5717c478bd9Sstevel@tonic-gate  * Stream a pipe/FIFO.
5727c478bd9Sstevel@tonic-gate  * The FIFOCONNLD flag is used when CONNLD has been pushed on the stream.
5737c478bd9Sstevel@tonic-gate  * If the flag is set, a new vnode is created by calling fifo_connld().
5747c478bd9Sstevel@tonic-gate  * Connld logic was moved to fifo_connld() to speed up the open
5757c478bd9Sstevel@tonic-gate  * operation, simplify the connld/fifo interaction, and remove inherent
5767c478bd9Sstevel@tonic-gate  * race conditions between the connld module and fifos.
5777c478bd9Sstevel@tonic-gate  * This routine is single threaded for two reasons.
5787c478bd9Sstevel@tonic-gate  * 1) connld requests are synchronous; that is, they must block
5797c478bd9Sstevel@tonic-gate  *    until the server does an I_RECVFD (oh, well).  Single threading is
5807c478bd9Sstevel@tonic-gate  *    the simplest way to accomplish this.
5817c478bd9Sstevel@tonic-gate  * 2) fifo_close() must not send M_HANGUP or M_ERROR while we are
5827c478bd9Sstevel@tonic-gate  *    in stropen. Stropen() has a tendency to reset things and
5837c478bd9Sstevel@tonic-gate  *    we would like streams to remember that a hangup occurred.
5847c478bd9Sstevel@tonic-gate  */
5857c478bd9Sstevel@tonic-gate int
fifo_stropen(vnode_t ** vpp,int flag,cred_t * crp,int dotwist,int lockheld)5867c478bd9Sstevel@tonic-gate fifo_stropen(vnode_t **vpp, int flag, cred_t *crp, int dotwist, int lockheld)
5877c478bd9Sstevel@tonic-gate {
5887c478bd9Sstevel@tonic-gate 	int error = 0;
5897c478bd9Sstevel@tonic-gate 	vnode_t *oldvp = *vpp;
5907c478bd9Sstevel@tonic-gate 	fifonode_t *fnp = VTOF(*vpp);
5917c478bd9Sstevel@tonic-gate 	dev_t pdev = 0;
5927c478bd9Sstevel@tonic-gate 	int firstopen = 0;
5937c478bd9Sstevel@tonic-gate 	fifolock_t *fn_lock;
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate 	fn_lock = fnp->fn_lock;
5967c478bd9Sstevel@tonic-gate 	if (!lockheld)
5977c478bd9Sstevel@tonic-gate 		mutex_enter(&fn_lock->flk_lock);
5987c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&fnp->fn_lock->flk_lock));
5997c478bd9Sstevel@tonic-gate 
6007c478bd9Sstevel@tonic-gate 	/*
6017c478bd9Sstevel@tonic-gate 	 * FIFO is in the process of opening. Wait for it
6027c478bd9Sstevel@tonic-gate 	 * to complete before starting another open on it
6037c478bd9Sstevel@tonic-gate 	 * This prevents races associated with connld open
6047c478bd9Sstevel@tonic-gate 	 */
6057c478bd9Sstevel@tonic-gate 	while (fnp->fn_flag & FIFOOPEN) {
6067c478bd9Sstevel@tonic-gate 		if (!cv_wait_sig(&fnp->fn_wait_cv, &fn_lock->flk_lock)) {
6077c478bd9Sstevel@tonic-gate 			fifo_cleanup(oldvp, flag);
6087c478bd9Sstevel@tonic-gate 			if (!lockheld)
6097c478bd9Sstevel@tonic-gate 				mutex_exit(&fn_lock->flk_lock);
6107c478bd9Sstevel@tonic-gate 			return (EINTR);
6117c478bd9Sstevel@tonic-gate 		}
6127c478bd9Sstevel@tonic-gate 	}
6137c478bd9Sstevel@tonic-gate 
6147c478bd9Sstevel@tonic-gate 	/*
6157c478bd9Sstevel@tonic-gate 	 * The other end of the pipe is almost closed so
6167c478bd9Sstevel@tonic-gate 	 * reject any other open on this end of the pipe
6177c478bd9Sstevel@tonic-gate 	 * This only happens with a pipe mounted under namefs
6187c478bd9Sstevel@tonic-gate 	 */
6197c478bd9Sstevel@tonic-gate 	if ((fnp->fn_flag & (FIFOCLOSE|ISPIPE)) == (FIFOCLOSE|ISPIPE)) {
6207c478bd9Sstevel@tonic-gate 		fifo_cleanup(oldvp, flag);
6217c478bd9Sstevel@tonic-gate 		cv_broadcast(&fnp->fn_wait_cv);
6227c478bd9Sstevel@tonic-gate 		if (!lockheld)
6237c478bd9Sstevel@tonic-gate 			mutex_exit(&fn_lock->flk_lock);
6247c478bd9Sstevel@tonic-gate 		return (ENXIO);
6257c478bd9Sstevel@tonic-gate 	}
6267c478bd9Sstevel@tonic-gate 
6277c478bd9Sstevel@tonic-gate 	fnp->fn_flag |= FIFOOPEN;
6287c478bd9Sstevel@tonic-gate 
6297c478bd9Sstevel@tonic-gate 	/*
6307c478bd9Sstevel@tonic-gate 	 * can't allow close to happen while we are
6317c478bd9Sstevel@tonic-gate 	 * in the middle of stropen().
6327c478bd9Sstevel@tonic-gate 	 * M_HANGUP and M_ERROR could leave the stream in a strange state
6337c478bd9Sstevel@tonic-gate 	 */
6347c478bd9Sstevel@tonic-gate 	while (fn_lock->flk_ocsync)
6357c478bd9Sstevel@tonic-gate 		cv_wait(&fn_lock->flk_wait_cv, &fn_lock->flk_lock);
6367c478bd9Sstevel@tonic-gate 
6377c478bd9Sstevel@tonic-gate 	fn_lock->flk_ocsync = 1;
6387c478bd9Sstevel@tonic-gate 
6397c478bd9Sstevel@tonic-gate 	if (fnp->fn_flag & FIFOCONNLD) {
6407c478bd9Sstevel@tonic-gate 		/*
6417c478bd9Sstevel@tonic-gate 		 * This is a reopen, so we should release the fifo lock
6427c478bd9Sstevel@tonic-gate 		 * just in case some strange module pushed on connld
6437c478bd9Sstevel@tonic-gate 		 * has some odd side effect.
6447c478bd9Sstevel@tonic-gate 		 * Note: this stropen is on the oldvp.  It will
6457c478bd9Sstevel@tonic-gate 		 * have no impact on the connld vp returned and
6467c478bd9Sstevel@tonic-gate 		 * strclose() will only be called when we release
6477c478bd9Sstevel@tonic-gate 		 * flk_ocsync
6487c478bd9Sstevel@tonic-gate 		 */
6497c478bd9Sstevel@tonic-gate 		mutex_exit(&fn_lock->flk_lock);
6507c478bd9Sstevel@tonic-gate 		if ((error = stropen(oldvp, &pdev, flag, crp)) != 0) {
6517c478bd9Sstevel@tonic-gate 			mutex_enter(&fn_lock->flk_lock);
6527c478bd9Sstevel@tonic-gate 			fifo_cleanup(oldvp, flag);
6537c478bd9Sstevel@tonic-gate 			fn_lock->flk_ocsync = 0;
6547c478bd9Sstevel@tonic-gate 			cv_broadcast(&fn_lock->flk_wait_cv);
6557c478bd9Sstevel@tonic-gate 			goto out;
6567c478bd9Sstevel@tonic-gate 		}
6577c478bd9Sstevel@tonic-gate 		/*
6587c478bd9Sstevel@tonic-gate 		 * streams open done, allow close on other end if
6597c478bd9Sstevel@tonic-gate 		 * required.  Do this now.. it could
6607c478bd9Sstevel@tonic-gate 		 * be a very long time before fifo_connld returns.
6617c478bd9Sstevel@tonic-gate 		 */
6627c478bd9Sstevel@tonic-gate 		mutex_enter(&fn_lock->flk_lock);
6637c478bd9Sstevel@tonic-gate 		/*
6647c478bd9Sstevel@tonic-gate 		 * we need to fake an open here so that if this
6657c478bd9Sstevel@tonic-gate 		 * end of the pipe closes, we don't loose the
6667c478bd9Sstevel@tonic-gate 		 * stream head (kind of like single threading
6677c478bd9Sstevel@tonic-gate 		 * open and close for this end of the pipe)
6687c478bd9Sstevel@tonic-gate 		 * We'll need to call fifo_close() to do clean
6697c478bd9Sstevel@tonic-gate 		 * up in case this end of the pipe was closed
6707c478bd9Sstevel@tonic-gate 		 * down while we were in fifo_connld()
6717c478bd9Sstevel@tonic-gate 		 */
6727c478bd9Sstevel@tonic-gate 		ASSERT(fnp->fn_open > 0);
6737c478bd9Sstevel@tonic-gate 		fnp->fn_open++;
6747c478bd9Sstevel@tonic-gate 		fn_lock->flk_ocsync = 0;
6757c478bd9Sstevel@tonic-gate 		cv_broadcast(&fn_lock->flk_wait_cv);
6767c478bd9Sstevel@tonic-gate 		mutex_exit(&fn_lock->flk_lock);
6777c478bd9Sstevel@tonic-gate 		/*
6787c478bd9Sstevel@tonic-gate 		 * Connld has been pushed onto the pipe
6797c478bd9Sstevel@tonic-gate 		 * Create new pipe on behalf of connld
6807c478bd9Sstevel@tonic-gate 		 */
6817c478bd9Sstevel@tonic-gate 		if (error = fifo_connld(vpp, flag, crp)) {
682da6c28aaSamw 			(void) fifo_close(oldvp, flag, 1, 0, crp, NULL);
6837c478bd9Sstevel@tonic-gate 			mutex_enter(&fn_lock->flk_lock);
6847c478bd9Sstevel@tonic-gate 			goto out;
6857c478bd9Sstevel@tonic-gate 		}
6867c478bd9Sstevel@tonic-gate 		/*
6877c478bd9Sstevel@tonic-gate 		 * undo fake open.  We need to call fifo_close
6887c478bd9Sstevel@tonic-gate 		 * because some other thread could have done
6897c478bd9Sstevel@tonic-gate 		 * a close and detach of the named pipe while
6907c478bd9Sstevel@tonic-gate 		 * we were in fifo_connld(), so
6917c478bd9Sstevel@tonic-gate 		 * we want to make sure the close completes (yuk)
6927c478bd9Sstevel@tonic-gate 		 */
693da6c28aaSamw 		(void) fifo_close(oldvp, flag, 1, 0, crp, NULL);
6947c478bd9Sstevel@tonic-gate 		/*
6957c478bd9Sstevel@tonic-gate 		 * fifo_connld has changed the vp, so we
6967c478bd9Sstevel@tonic-gate 		 * need to re-initialize locals
6977c478bd9Sstevel@tonic-gate 		 */
6987c478bd9Sstevel@tonic-gate 		fnp = VTOF(*vpp);
6997c478bd9Sstevel@tonic-gate 		fn_lock = fnp->fn_lock;
7007c478bd9Sstevel@tonic-gate 		mutex_enter(&fn_lock->flk_lock);
7017c478bd9Sstevel@tonic-gate 	} else {
7027c478bd9Sstevel@tonic-gate 		/*
7037c478bd9Sstevel@tonic-gate 		 * release lock in case there are modules pushed that
7047c478bd9Sstevel@tonic-gate 		 * could have some strange side effect
7057c478bd9Sstevel@tonic-gate 		 */
7067c478bd9Sstevel@tonic-gate 
7077c478bd9Sstevel@tonic-gate 		mutex_exit(&fn_lock->flk_lock);
7087c478bd9Sstevel@tonic-gate 
7097c478bd9Sstevel@tonic-gate 		/*
7107c478bd9Sstevel@tonic-gate 		 * If this is the first open of a fifo (dotwist
7117c478bd9Sstevel@tonic-gate 		 * will be non-zero) we will need to twist the queues.
7127c478bd9Sstevel@tonic-gate 		 */
7137c478bd9Sstevel@tonic-gate 		if (oldvp->v_stream == NULL)
7147c478bd9Sstevel@tonic-gate 			firstopen = 1;
7157c478bd9Sstevel@tonic-gate 
7167c478bd9Sstevel@tonic-gate 
7177c478bd9Sstevel@tonic-gate 		/*
7187c478bd9Sstevel@tonic-gate 		 * normal open of pipe/fifo
7197c478bd9Sstevel@tonic-gate 		 */
7207c478bd9Sstevel@tonic-gate 
7217c478bd9Sstevel@tonic-gate 		if ((error = stropen(oldvp, &pdev, flag, crp)) != 0) {
7227c478bd9Sstevel@tonic-gate 			mutex_enter(&fn_lock->flk_lock);
7237c478bd9Sstevel@tonic-gate 			fifo_cleanup(oldvp, flag);
7247c478bd9Sstevel@tonic-gate 			ASSERT(fnp->fn_open != 0 || oldvp->v_stream == NULL);
7257c478bd9Sstevel@tonic-gate 			fn_lock->flk_ocsync = 0;
7267c478bd9Sstevel@tonic-gate 			cv_broadcast(&fn_lock->flk_wait_cv);
7277c478bd9Sstevel@tonic-gate 			goto out;
7287c478bd9Sstevel@tonic-gate 		}
7297c478bd9Sstevel@tonic-gate 		mutex_enter(&fn_lock->flk_lock);
7307c478bd9Sstevel@tonic-gate 
7317c478bd9Sstevel@tonic-gate 		/*
7327c478bd9Sstevel@tonic-gate 		 * twist the ends of the fifo together
7337c478bd9Sstevel@tonic-gate 		 */
7347c478bd9Sstevel@tonic-gate 		if (dotwist && firstopen)
7357c478bd9Sstevel@tonic-gate 			strmate(*vpp, *vpp);
7367c478bd9Sstevel@tonic-gate 
7377c478bd9Sstevel@tonic-gate 		/*
7387c478bd9Sstevel@tonic-gate 		 * Show that this open has succeeded
7397c478bd9Sstevel@tonic-gate 		 * and allow closes or other opens to proceed
7407c478bd9Sstevel@tonic-gate 		 */
7417c478bd9Sstevel@tonic-gate 		fnp->fn_open++;
7427c478bd9Sstevel@tonic-gate 		fn_lock->flk_ocsync = 0;
7437c478bd9Sstevel@tonic-gate 		cv_broadcast(&fn_lock->flk_wait_cv);
7447c478bd9Sstevel@tonic-gate 	}
7457c478bd9Sstevel@tonic-gate out:
7467c478bd9Sstevel@tonic-gate 	fnp->fn_flag &= ~FIFOOPEN;
7477c478bd9Sstevel@tonic-gate 	if (error == 0) {
7487c478bd9Sstevel@tonic-gate 		fnp->fn_flag |= FIFOISOPEN;
7497c478bd9Sstevel@tonic-gate 		/*
7507c478bd9Sstevel@tonic-gate 		 * If this is a FIFO and has the close flag set
7517c478bd9Sstevel@tonic-gate 		 * and there are now writers, clear the close flag
7527c478bd9Sstevel@tonic-gate 		 * Note: close flag only gets set when last writer
7537c478bd9Sstevel@tonic-gate 		 * on a FIFO goes away.
7547c478bd9Sstevel@tonic-gate 		 */
7557c478bd9Sstevel@tonic-gate 		if (((fnp->fn_flag & (ISPIPE|FIFOCLOSE)) == FIFOCLOSE) &&
7567c478bd9Sstevel@tonic-gate 		    fnp->fn_wcnt > 0)
7577c478bd9Sstevel@tonic-gate 			fnp->fn_flag &= ~FIFOCLOSE;
7587c478bd9Sstevel@tonic-gate 	}
7597c478bd9Sstevel@tonic-gate 	cv_broadcast(&fnp->fn_wait_cv);
7607c478bd9Sstevel@tonic-gate 	if (!lockheld)
7617c478bd9Sstevel@tonic-gate 		mutex_exit(&fn_lock->flk_lock);
7627c478bd9Sstevel@tonic-gate 	return (error);
7637c478bd9Sstevel@tonic-gate }
7647c478bd9Sstevel@tonic-gate 
7657c478bd9Sstevel@tonic-gate /*
7667c478bd9Sstevel@tonic-gate  * Clean up the state of a FIFO and/or mounted pipe in the
7677c478bd9Sstevel@tonic-gate  * event that a fifo_open() was interrupted while the
7687c478bd9Sstevel@tonic-gate  * process was blocked.
7697c478bd9Sstevel@tonic-gate  */
7707c478bd9Sstevel@tonic-gate void
fifo_cleanup(vnode_t * vp,int flag)7717c478bd9Sstevel@tonic-gate fifo_cleanup(vnode_t *vp, int flag)
7727c478bd9Sstevel@tonic-gate {
7737c478bd9Sstevel@tonic-gate 	fifonode_t *fnp = VTOF(vp);
7747c478bd9Sstevel@tonic-gate 
7757c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&fnp->fn_lock->flk_lock));
7767c478bd9Sstevel@tonic-gate 
7777c478bd9Sstevel@tonic-gate 	cleanlocks(vp, curproc->p_pid, 0);
7787c478bd9Sstevel@tonic-gate 	cleanshares(vp, curproc->p_pid);
7797c478bd9Sstevel@tonic-gate 	if (flag & FREAD) {
7807c478bd9Sstevel@tonic-gate 		fnp->fn_rcnt--;
7817c478bd9Sstevel@tonic-gate 	}
7827c478bd9Sstevel@tonic-gate 	if (flag & FWRITE) {
7837c478bd9Sstevel@tonic-gate 		fnp->fn_wcnt--;
7847c478bd9Sstevel@tonic-gate 	}
7857c478bd9Sstevel@tonic-gate 	cv_broadcast(&fnp->fn_wait_cv);
7867c478bd9Sstevel@tonic-gate }
7877c478bd9Sstevel@tonic-gate 
7887c478bd9Sstevel@tonic-gate 
7897c478bd9Sstevel@tonic-gate /*
7907c478bd9Sstevel@tonic-gate  * Insert a fifonode-vnode pair onto the fifoalloc hash list.
7917c478bd9Sstevel@tonic-gate  */
7927c478bd9Sstevel@tonic-gate static void
fifoinsert(fifonode_t * fnp)7937c478bd9Sstevel@tonic-gate fifoinsert(fifonode_t *fnp)
7947c478bd9Sstevel@tonic-gate {
7957c478bd9Sstevel@tonic-gate 	int idx = FIFOHASH(fnp->fn_realvp);
7967c478bd9Sstevel@tonic-gate 
7977c478bd9Sstevel@tonic-gate 	/*
7987c478bd9Sstevel@tonic-gate 	 * We don't need to hold fn_lock since we're holding ftable_lock and
7997c478bd9Sstevel@tonic-gate 	 * this routine is only called right after we've allocated an fnode.
8007c478bd9Sstevel@tonic-gate 	 * FIFO is inserted at head of NULL terminated doubly linked list.
8017c478bd9Sstevel@tonic-gate 	 */
8027c478bd9Sstevel@tonic-gate 
8037c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ftable_lock));
8047c478bd9Sstevel@tonic-gate 	fnp->fn_backp = NULL;
8057c478bd9Sstevel@tonic-gate 	fnp->fn_nextp = fifoalloc[idx];
8067c478bd9Sstevel@tonic-gate 	fifoalloc[idx] = fnp;
8077c478bd9Sstevel@tonic-gate 	if (fnp->fn_nextp)
8087c478bd9Sstevel@tonic-gate 		fnp->fn_nextp->fn_backp = fnp;
8097c478bd9Sstevel@tonic-gate }
8107c478bd9Sstevel@tonic-gate 
8117c478bd9Sstevel@tonic-gate /*
8127c478bd9Sstevel@tonic-gate  * Find a fifonode-vnode pair on the fifoalloc hash list.
8137c478bd9Sstevel@tonic-gate  * vp is a vnode to be shadowed. If it's on the hash list,
8147c478bd9Sstevel@tonic-gate  * it already has a shadow, therefore return its corresponding
8157c478bd9Sstevel@tonic-gate  * fifonode.
8167c478bd9Sstevel@tonic-gate  */
8177c478bd9Sstevel@tonic-gate static fifonode_t *
fifofind(vnode_t * vp)8187c478bd9Sstevel@tonic-gate fifofind(vnode_t *vp)
8197c478bd9Sstevel@tonic-gate {
8207c478bd9Sstevel@tonic-gate 	fifonode_t *fnode;
8217c478bd9Sstevel@tonic-gate 
8227c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ftable_lock));
8237c478bd9Sstevel@tonic-gate 	for (fnode = fifoalloc[FIFOHASH(vp)]; fnode; fnode = fnode->fn_nextp) {
8247c478bd9Sstevel@tonic-gate 		if (fnode->fn_realvp == vp) {
8257c478bd9Sstevel@tonic-gate 			VN_HOLD(FTOV(fnode));
8267c478bd9Sstevel@tonic-gate 			return (fnode);
8277c478bd9Sstevel@tonic-gate 		}
8287c478bd9Sstevel@tonic-gate 	}
8297c478bd9Sstevel@tonic-gate 	return (NULL);
8307c478bd9Sstevel@tonic-gate }
8317c478bd9Sstevel@tonic-gate 
8327c478bd9Sstevel@tonic-gate /*
8337c478bd9Sstevel@tonic-gate  * Remove a fifonode-vnode pair from the fifoalloc hash list.
8347c478bd9Sstevel@tonic-gate  * This routine is called from the fifo_inactive() routine when a
8357c478bd9Sstevel@tonic-gate  * FIFO is being released.
8367c478bd9Sstevel@tonic-gate  * If the link to be removed is the only link, set fifoalloc to NULL.
8377c478bd9Sstevel@tonic-gate  */
8387c478bd9Sstevel@tonic-gate void
fiforemove(fifonode_t * fnp)8397c478bd9Sstevel@tonic-gate fiforemove(fifonode_t *fnp)
8407c478bd9Sstevel@tonic-gate {
8417c478bd9Sstevel@tonic-gate 	int idx = FIFOHASH(fnp->fn_realvp);
8427c478bd9Sstevel@tonic-gate 	fifonode_t *fnode;
8437c478bd9Sstevel@tonic-gate 
8447c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ftable_lock));
8457c478bd9Sstevel@tonic-gate 	fnode = fifoalloc[idx];
8467c478bd9Sstevel@tonic-gate 	/*
8477c478bd9Sstevel@tonic-gate 	 * fast path... only 1 FIFO in this list entry
8487c478bd9Sstevel@tonic-gate 	 */
8497c478bd9Sstevel@tonic-gate 	if (fnode != NULL && fnode == fnp &&
8505056eb0dSbpramod 	    !fnode->fn_nextp && !fnode->fn_backp) {
851b5fca8f8Stomee 		fifoalloc[idx] = NULL;
8527c478bd9Sstevel@tonic-gate 	} else {
8537c478bd9Sstevel@tonic-gate 
8547c478bd9Sstevel@tonic-gate 		for (;  fnode;  fnode = fnode->fn_nextp) {
8557c478bd9Sstevel@tonic-gate 			if (fnode == fnp) {
8567c478bd9Sstevel@tonic-gate 				/*
8577c478bd9Sstevel@tonic-gate 				 * if we are first entry
8587c478bd9Sstevel@tonic-gate 				 */
8597c478bd9Sstevel@tonic-gate 				if (fnp == fifoalloc[idx])
8607c478bd9Sstevel@tonic-gate 					fifoalloc[idx] = fnp->fn_nextp;
8617c478bd9Sstevel@tonic-gate 				if (fnode->fn_nextp)
8627c478bd9Sstevel@tonic-gate 					fnode->fn_nextp->fn_backp =
8635056eb0dSbpramod 					    fnode->fn_backp;
8647c478bd9Sstevel@tonic-gate 				if (fnode->fn_backp)
8657c478bd9Sstevel@tonic-gate 					fnode->fn_backp->fn_nextp =
8665056eb0dSbpramod 					    fnode->fn_nextp;
8677c478bd9Sstevel@tonic-gate 				break;
8687c478bd9Sstevel@tonic-gate 			}
8697c478bd9Sstevel@tonic-gate 		}
8707c478bd9Sstevel@tonic-gate 	}
8717c478bd9Sstevel@tonic-gate }
8727c478bd9Sstevel@tonic-gate 
8737c478bd9Sstevel@tonic-gate /*
8747c478bd9Sstevel@tonic-gate  * Flush all data from a fifo's message queue
8757c478bd9Sstevel@tonic-gate  */
8767c478bd9Sstevel@tonic-gate 
8777c478bd9Sstevel@tonic-gate void
fifo_fastflush(fifonode_t * fnp)8787c478bd9Sstevel@tonic-gate fifo_fastflush(fifonode_t *fnp)
8797c478bd9Sstevel@tonic-gate {
8807c478bd9Sstevel@tonic-gate 	mblk_t *bp;
8817c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&fnp->fn_lock->flk_lock));
8827c478bd9Sstevel@tonic-gate 
8837c478bd9Sstevel@tonic-gate 	if ((bp = fnp->fn_mp) != NULL) {
8847c478bd9Sstevel@tonic-gate 		fnp->fn_mp = NULL;
8857c478bd9Sstevel@tonic-gate 		fnp->fn_count = 0;
8867c478bd9Sstevel@tonic-gate 		freemsg(bp);
8877c478bd9Sstevel@tonic-gate 	}
8887c478bd9Sstevel@tonic-gate 	fifo_wakewriter(fnp->fn_dest, fnp->fn_lock);
8897c478bd9Sstevel@tonic-gate }
8907c478bd9Sstevel@tonic-gate 
8917c478bd9Sstevel@tonic-gate /*
8927c478bd9Sstevel@tonic-gate  * Note:  This routine is single threaded
8937c478bd9Sstevel@tonic-gate  *  Protected by FIFOOPEN flag (i.e. flk_lock is not held)
8947c478bd9Sstevel@tonic-gate  *  Upon successful completion, the original fifo is unlocked
8957c478bd9Sstevel@tonic-gate  *  and FIFOOPEN is cleared for the original vpp.
8967c478bd9Sstevel@tonic-gate  *  The new fifo returned has FIFOOPEN set.
8977c478bd9Sstevel@tonic-gate  */
8987c478bd9Sstevel@tonic-gate static int
fifo_connld(struct vnode ** vpp,int flag,cred_t * crp)8997c478bd9Sstevel@tonic-gate fifo_connld(struct vnode **vpp, int flag, cred_t *crp)
9007c478bd9Sstevel@tonic-gate {
9017c478bd9Sstevel@tonic-gate 	struct vnode *vp1;
9027c478bd9Sstevel@tonic-gate 	struct vnode *vp2;
9037c478bd9Sstevel@tonic-gate 	struct fifonode *oldfnp;
9047c478bd9Sstevel@tonic-gate 	struct fifonode *fn_dest;
9057c478bd9Sstevel@tonic-gate 	int error;
9067c478bd9Sstevel@tonic-gate 	struct file *filep;
9077c478bd9Sstevel@tonic-gate 	struct fifolock *fn_lock;
9087c478bd9Sstevel@tonic-gate 	cred_t *c;
9097c478bd9Sstevel@tonic-gate 
9107c478bd9Sstevel@tonic-gate 	/*
9117c478bd9Sstevel@tonic-gate 	 * Get two vnodes that will represent the pipe ends for the new pipe.
9127c478bd9Sstevel@tonic-gate 	 */
9137c478bd9Sstevel@tonic-gate 	makepipe(&vp1, &vp2);
9147c478bd9Sstevel@tonic-gate 
9157c478bd9Sstevel@tonic-gate 	/*
9167c478bd9Sstevel@tonic-gate 	 * Allocate a file descriptor and file pointer for one of the pipe
9177c478bd9Sstevel@tonic-gate 	 * ends. The file descriptor will be used to send that pipe end to
9187c478bd9Sstevel@tonic-gate 	 * the process on the other end of this stream. Note that we get
9197c478bd9Sstevel@tonic-gate 	 * the file structure only, there is no file list entry allocated.
9207c478bd9Sstevel@tonic-gate 	 */
9217c478bd9Sstevel@tonic-gate 	if (error = falloc(vp1, FWRITE|FREAD, &filep, NULL)) {
9227c478bd9Sstevel@tonic-gate 		VN_RELE(vp1);
9237c478bd9Sstevel@tonic-gate 		VN_RELE(vp2);
9247c478bd9Sstevel@tonic-gate 		return (error);
9257c478bd9Sstevel@tonic-gate 	}
9267c478bd9Sstevel@tonic-gate 	mutex_exit(&filep->f_tlock);
9277c478bd9Sstevel@tonic-gate 	oldfnp = VTOF(*vpp);
9287c478bd9Sstevel@tonic-gate 	fn_lock = oldfnp->fn_lock;
9297c478bd9Sstevel@tonic-gate 	fn_dest = oldfnp->fn_dest;
9307c478bd9Sstevel@tonic-gate 
9317c478bd9Sstevel@tonic-gate 	/*
9327c478bd9Sstevel@tonic-gate 	 * Create two new stream heads and attach them to the two vnodes for
9337c478bd9Sstevel@tonic-gate 	 * the new pipe.
9347c478bd9Sstevel@tonic-gate 	 */
9357c478bd9Sstevel@tonic-gate 	if ((error = fifo_stropen(&vp1, FREAD|FWRITE, filep->f_cred, 0, 0)) !=
9367c478bd9Sstevel@tonic-gate 	    0 ||
9377c478bd9Sstevel@tonic-gate 	    (error = fifo_stropen(&vp2, flag, filep->f_cred, 0, 0)) != 0) {
9387c478bd9Sstevel@tonic-gate #if DEBUG
939b5fca8f8Stomee 		cmn_err(CE_NOTE, "fifo stropen failed error 0x%x", error);
9407c478bd9Sstevel@tonic-gate #endif
9417c478bd9Sstevel@tonic-gate 		/*
9427c478bd9Sstevel@tonic-gate 		 * this will call fifo_close and VN_RELE on vp1
9437c478bd9Sstevel@tonic-gate 		 */
9447c478bd9Sstevel@tonic-gate 		(void) closef(filep);
9457c478bd9Sstevel@tonic-gate 		VN_RELE(vp2);
9467c478bd9Sstevel@tonic-gate 		return (error);
9477c478bd9Sstevel@tonic-gate 	}
9487c478bd9Sstevel@tonic-gate 
9497c478bd9Sstevel@tonic-gate 	/*
9507c478bd9Sstevel@tonic-gate 	 * twist the ends of the pipe together
9517c478bd9Sstevel@tonic-gate 	 */
9527c478bd9Sstevel@tonic-gate 	strmate(vp1, vp2);
9537c478bd9Sstevel@tonic-gate 
9547c478bd9Sstevel@tonic-gate 	/*
9557c478bd9Sstevel@tonic-gate 	 * Set our end to busy in open
9567c478bd9Sstevel@tonic-gate 	 * Note: Don't need lock around this because we're the only
9577c478bd9Sstevel@tonic-gate 	 * one who knows about it
9587c478bd9Sstevel@tonic-gate 	 */
9597c478bd9Sstevel@tonic-gate 	VTOF(vp2)->fn_flag |= FIFOOPEN;
9607c478bd9Sstevel@tonic-gate 
9617c478bd9Sstevel@tonic-gate 	mutex_enter(&fn_lock->flk_lock);
9627c478bd9Sstevel@tonic-gate 
9637c478bd9Sstevel@tonic-gate 	fn_dest->fn_flag |= FIFOSEND;
9647c478bd9Sstevel@tonic-gate 	/*
9657c478bd9Sstevel@tonic-gate 	 * check to make sure neither end of pipe has gone away
9667c478bd9Sstevel@tonic-gate 	 */
9677c478bd9Sstevel@tonic-gate 	if (!(fn_dest->fn_flag & FIFOISOPEN)) {
9687c478bd9Sstevel@tonic-gate 		error = ENXIO;
9697c478bd9Sstevel@tonic-gate 		fn_dest->fn_flag &= ~FIFOSEND;
9707c478bd9Sstevel@tonic-gate 		mutex_exit(&fn_lock->flk_lock);
9717c478bd9Sstevel@tonic-gate 		/*
9727c478bd9Sstevel@tonic-gate 		 * this will call fifo_close and VN_RELE on vp1
9737c478bd9Sstevel@tonic-gate 		 */
9747c478bd9Sstevel@tonic-gate 		goto out;
9757c478bd9Sstevel@tonic-gate 	}
9767c478bd9Sstevel@tonic-gate 	mutex_exit(&fn_lock->flk_lock);
9777c478bd9Sstevel@tonic-gate 
9787c478bd9Sstevel@tonic-gate 	/*
9797c478bd9Sstevel@tonic-gate 	 * Tag the sender's credential on the pipe descriptor.
9807c478bd9Sstevel@tonic-gate 	 */
9817c478bd9Sstevel@tonic-gate 	crhold(VTOF(vp1)->fn_pcredp = crp);
9827c478bd9Sstevel@tonic-gate 	VTOF(vp1)->fn_cpid = curproc->p_pid;
9837c478bd9Sstevel@tonic-gate 
9847c478bd9Sstevel@tonic-gate 	/*
9857c478bd9Sstevel@tonic-gate 	 * send the file descriptor to other end of pipe
9867c478bd9Sstevel@tonic-gate 	 */
9877c478bd9Sstevel@tonic-gate 	if (error = do_sendfp((*vpp)->v_stream, filep, crp)) {
9887c478bd9Sstevel@tonic-gate 		mutex_enter(&fn_lock->flk_lock);
9897c478bd9Sstevel@tonic-gate 		fn_dest->fn_flag &= ~FIFOSEND;
9907c478bd9Sstevel@tonic-gate 		mutex_exit(&fn_lock->flk_lock);
9917c478bd9Sstevel@tonic-gate 		/*
9927c478bd9Sstevel@tonic-gate 		 * this will call fifo_close and VN_RELE on vp1
9937c478bd9Sstevel@tonic-gate 		 */
9947c478bd9Sstevel@tonic-gate 		goto out;
9957c478bd9Sstevel@tonic-gate 	}
9967c478bd9Sstevel@tonic-gate 
9977c478bd9Sstevel@tonic-gate 	mutex_enter(&fn_lock->flk_lock);
9987c478bd9Sstevel@tonic-gate 	/*
9997c478bd9Sstevel@tonic-gate 	 * Wait for other end to receive file descriptor
10007c478bd9Sstevel@tonic-gate 	 * FIFOCLOSE indicates that one or both sides of the pipe
10017c478bd9Sstevel@tonic-gate 	 * have gone away.
10027c478bd9Sstevel@tonic-gate 	 */
10037c478bd9Sstevel@tonic-gate 	while ((fn_dest->fn_flag & (FIFOCLOSE | FIFOSEND)) == FIFOSEND) {
10047c478bd9Sstevel@tonic-gate 		if (!cv_wait_sig(&oldfnp->fn_wait_cv, &fn_lock->flk_lock)) {
10057c478bd9Sstevel@tonic-gate 			error = EINTR;
10067c478bd9Sstevel@tonic-gate 			fn_dest->fn_flag &= ~FIFOSEND;
10077c478bd9Sstevel@tonic-gate 			mutex_exit(&fn_lock->flk_lock);
10087c478bd9Sstevel@tonic-gate 			goto out;
10097c478bd9Sstevel@tonic-gate 		}
10107c478bd9Sstevel@tonic-gate 	}
10117c478bd9Sstevel@tonic-gate 	/*
10127c478bd9Sstevel@tonic-gate 	 * If either end of pipe has gone away and the other end did not
10137c478bd9Sstevel@tonic-gate 	 * receive pipe, reject the connld open
10147c478bd9Sstevel@tonic-gate 	 */
10157c478bd9Sstevel@tonic-gate 	if ((fn_dest->fn_flag & FIFOSEND)) {
10167c478bd9Sstevel@tonic-gate 		error = ENXIO;
10177c478bd9Sstevel@tonic-gate 		fn_dest->fn_flag &= ~FIFOSEND;
10187c478bd9Sstevel@tonic-gate 		mutex_exit(&fn_lock->flk_lock);
10197c478bd9Sstevel@tonic-gate 		goto out;
10207c478bd9Sstevel@tonic-gate 	}
10217c478bd9Sstevel@tonic-gate 
10227c478bd9Sstevel@tonic-gate 	oldfnp->fn_flag &= ~FIFOOPEN;
10237c478bd9Sstevel@tonic-gate 	cv_broadcast(&oldfnp->fn_wait_cv);
10247c478bd9Sstevel@tonic-gate 	mutex_exit(&fn_lock->flk_lock);
10257c478bd9Sstevel@tonic-gate 
10267c478bd9Sstevel@tonic-gate 	VN_RELE(*vpp);
10277c478bd9Sstevel@tonic-gate 	*vpp = vp2;
10287c478bd9Sstevel@tonic-gate 	(void) closef(filep);
10297c478bd9Sstevel@tonic-gate 	return (0);
10307c478bd9Sstevel@tonic-gate out:
10317c478bd9Sstevel@tonic-gate 	c = filep->f_cred;
10327c478bd9Sstevel@tonic-gate 	crhold(c);
10337c478bd9Sstevel@tonic-gate 	(void) closef(filep);
10347c478bd9Sstevel@tonic-gate 	VTOF(vp2)->fn_flag &= ~FIFOOPEN;
1035da6c28aaSamw 	(void) fifo_close(vp2, flag, 1, (offset_t)0, c, NULL);
10367c478bd9Sstevel@tonic-gate 	crfree(c);
10377c478bd9Sstevel@tonic-gate 	VN_RELE(vp2);
10387c478bd9Sstevel@tonic-gate 	return (error);
10397c478bd9Sstevel@tonic-gate }
10407c478bd9Sstevel@tonic-gate 
10417c478bd9Sstevel@tonic-gate /*
10427c478bd9Sstevel@tonic-gate  * Disable fastpath mode.
10437c478bd9Sstevel@tonic-gate  */
10447c478bd9Sstevel@tonic-gate void
fifo_fastoff(fifonode_t * fnp)10457c478bd9Sstevel@tonic-gate fifo_fastoff(fifonode_t *fnp)
10467c478bd9Sstevel@tonic-gate {
10477c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&fnp->fn_lock->flk_lock));
10487c478bd9Sstevel@tonic-gate 	ASSERT(FTOV(fnp)->v_stream);
10497c478bd9Sstevel@tonic-gate 
105039cba716Swroche 	/* FIFOSTAYFAST is set => FIFOFAST is set */
105139cba716Swroche 	while ((fnp->fn_flag & FIFOSTAYFAST) || ((fnp->fn_flag & ISPIPE) &&
105239cba716Swroche 	    (fnp->fn_dest->fn_flag & FIFOSTAYFAST))) {
105339cba716Swroche 		ASSERT(fnp->fn_flag & FIFOFAST);
105439cba716Swroche 		/* indicate someone is waiting to turn into stream mode */
105539cba716Swroche 		fnp->fn_flag |= FIFOWAITMODE;
105639cba716Swroche 		cv_wait(&fnp->fn_wait_cv, &fnp->fn_lock->flk_lock);
105739cba716Swroche 		fnp->fn_flag &= ~FIFOWAITMODE;
105839cba716Swroche 	}
10597c478bd9Sstevel@tonic-gate 
106039cba716Swroche 	/* as we may have relased the lock, test the FIFOFAST flag here */
10617c478bd9Sstevel@tonic-gate 	if (!(fnp->fn_flag & FIFOFAST))
10627c478bd9Sstevel@tonic-gate 		return;
10637c478bd9Sstevel@tonic-gate #if FIFODEBUG
10647c478bd9Sstevel@tonic-gate 	if (Fifo_verbose)
10657c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "Fifo reverting to streams mode\n");
10667c478bd9Sstevel@tonic-gate #endif
10677c478bd9Sstevel@tonic-gate 
10687c478bd9Sstevel@tonic-gate 	fifo_fastturnoff(fnp);
10697c478bd9Sstevel@tonic-gate 	if (fnp->fn_flag & ISPIPE) {
10707c478bd9Sstevel@tonic-gate 		fifo_fastturnoff(fnp->fn_dest);
10717c478bd9Sstevel@tonic-gate 	}
10727c478bd9Sstevel@tonic-gate }
10737c478bd9Sstevel@tonic-gate 
10747c478bd9Sstevel@tonic-gate 
10757c478bd9Sstevel@tonic-gate /*
10767c478bd9Sstevel@tonic-gate  * flk_lock must be held while calling fifo_fastturnoff() to
10777c478bd9Sstevel@tonic-gate  * preserve data ordering (no reads or writes allowed)
10787c478bd9Sstevel@tonic-gate  */
10797c478bd9Sstevel@tonic-gate 
10807c478bd9Sstevel@tonic-gate static void
fifo_fastturnoff(fifonode_t * fnp)10817c478bd9Sstevel@tonic-gate fifo_fastturnoff(fifonode_t *fnp)
10827c478bd9Sstevel@tonic-gate {
10837c478bd9Sstevel@tonic-gate 	fifonode_t *fn_dest = fnp->fn_dest;
10847c478bd9Sstevel@tonic-gate 	mblk_t	*fn_mp;
10857c478bd9Sstevel@tonic-gate 	int	fn_flag;
10867c478bd9Sstevel@tonic-gate 
10877c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&fnp->fn_lock->flk_lock));
10887c478bd9Sstevel@tonic-gate 	/*
10897c478bd9Sstevel@tonic-gate 	 * Note: This end can't be closed if there
10907c478bd9Sstevel@tonic-gate 	 * is stuff in fn_mp
10917c478bd9Sstevel@tonic-gate 	 */
10927c478bd9Sstevel@tonic-gate 	if ((fn_mp = fnp->fn_mp) != NULL) {
10937c478bd9Sstevel@tonic-gate 		ASSERT(fnp->fn_flag & FIFOISOPEN);
10947c478bd9Sstevel@tonic-gate 		ASSERT(FTOV(fnp)->v_stream != NULL);
10957c478bd9Sstevel@tonic-gate 		ASSERT(FTOV(fnp)->v_stream->sd_wrq != NULL);
10967c478bd9Sstevel@tonic-gate 		ASSERT(RD(FTOV(fnp)->v_stream->sd_wrq) != NULL);
10977c478bd9Sstevel@tonic-gate 		ASSERT(strvp2wq(FTOV(fnp)) != NULL);
10987c478bd9Sstevel@tonic-gate 		fnp->fn_mp = NULL;
10997c478bd9Sstevel@tonic-gate 		fnp->fn_count = 0;
11007c478bd9Sstevel@tonic-gate 		/*
11017c478bd9Sstevel@tonic-gate 		 * Don't need to drop flk_lock across the put()
11027c478bd9Sstevel@tonic-gate 		 * since we're just moving the message from the fifo
11037c478bd9Sstevel@tonic-gate 		 * node to the STREAM head...
11047c478bd9Sstevel@tonic-gate 		 */
11057c478bd9Sstevel@tonic-gate 		put(RD(strvp2wq(FTOV(fnp))), fn_mp);
11067c478bd9Sstevel@tonic-gate 	}
11077c478bd9Sstevel@tonic-gate 
11087c478bd9Sstevel@tonic-gate 	/*
11097c478bd9Sstevel@tonic-gate 	 * Need to re-issue any pending poll requests
11107c478bd9Sstevel@tonic-gate 	 * so that the STREAMS framework sees them
11117c478bd9Sstevel@tonic-gate 	 * Writers would be waiting on fnp and readers on fn_dest
11127c478bd9Sstevel@tonic-gate 	 */
11137c478bd9Sstevel@tonic-gate 	if ((fnp->fn_flag & (FIFOISOPEN | FIFOPOLLW)) ==
11147c478bd9Sstevel@tonic-gate 	    (FIFOISOPEN | FIFOPOLLW)) {
11157c478bd9Sstevel@tonic-gate 		strpollwakeup(FTOV(fnp), POLLWRNORM);
11167c478bd9Sstevel@tonic-gate 	}
11177c478bd9Sstevel@tonic-gate 	fn_flag = fn_dest->fn_flag;
11187c478bd9Sstevel@tonic-gate 	if ((fn_flag & FIFOISOPEN) == FIFOISOPEN) {
11197c478bd9Sstevel@tonic-gate 		if ((fn_flag & (FIFOPOLLR | FIFOPOLLRBAND))) {
11207c478bd9Sstevel@tonic-gate 			strpollwakeup(FTOV(fn_dest), POLLIN|POLLRDNORM);
11217c478bd9Sstevel@tonic-gate 		}
11227c478bd9Sstevel@tonic-gate 	}
11237c478bd9Sstevel@tonic-gate 	/*
11247c478bd9Sstevel@tonic-gate 	 * wake up any sleeping processes so they can notice we went
11257c478bd9Sstevel@tonic-gate 	 * to streams mode
11267c478bd9Sstevel@tonic-gate 	 */
11277c478bd9Sstevel@tonic-gate 	fnp->fn_flag &= ~(FIFOFAST|FIFOWANTW|FIFOWANTR);
11287c478bd9Sstevel@tonic-gate 	cv_broadcast(&fnp->fn_wait_cv);
11297c478bd9Sstevel@tonic-gate }
11307c478bd9Sstevel@tonic-gate 
11317c478bd9Sstevel@tonic-gate /*
11327c478bd9Sstevel@tonic-gate  * Alternative version of fifo_fastoff()
11337c478bd9Sstevel@tonic-gate  * optimized for putmsg/getmsg.
11347c478bd9Sstevel@tonic-gate  */
11357c478bd9Sstevel@tonic-gate void
fifo_vfastoff(vnode_t * vp)11367c478bd9Sstevel@tonic-gate fifo_vfastoff(vnode_t *vp)
11377c478bd9Sstevel@tonic-gate {
11387c478bd9Sstevel@tonic-gate 	fifonode_t	*fnp = VTOF(vp);
11397c478bd9Sstevel@tonic-gate 
11407c478bd9Sstevel@tonic-gate 	mutex_enter(&fnp->fn_lock->flk_lock);
11417c478bd9Sstevel@tonic-gate 	if (!(fnp->fn_flag & FIFOFAST)) {
11427c478bd9Sstevel@tonic-gate 		mutex_exit(&fnp->fn_lock->flk_lock);
11437c478bd9Sstevel@tonic-gate 		return;
11447c478bd9Sstevel@tonic-gate 	}
11457c478bd9Sstevel@tonic-gate 	fifo_fastoff(fnp);
11467c478bd9Sstevel@tonic-gate 	mutex_exit(&fnp->fn_lock->flk_lock);
11477c478bd9Sstevel@tonic-gate }
11487c478bd9Sstevel@tonic-gate 
11497c478bd9Sstevel@tonic-gate /*
11507c478bd9Sstevel@tonic-gate  * Wake any sleeping writers, poll and send signals if necessary
11517c478bd9Sstevel@tonic-gate  * This module is only called when we drop below the hi water mark
11527c478bd9Sstevel@tonic-gate  * FIFOWANTW indicates that a process is sleeping in fifo_write()
11537c478bd9Sstevel@tonic-gate  * FIFOHIWATW indicates that we have either attempted a poll or
11547c478bd9Sstevel@tonic-gate  * non-blocking write and were over the high water mark
11557c478bd9Sstevel@tonic-gate  * This routine assumes a low water mark of 0.
11567c478bd9Sstevel@tonic-gate  */
11577c478bd9Sstevel@tonic-gate 
11587c478bd9Sstevel@tonic-gate void
fifo_wakewriter(fifonode_t * fn_dest,fifolock_t * fn_lock)11597c478bd9Sstevel@tonic-gate fifo_wakewriter(fifonode_t *fn_dest, fifolock_t *fn_lock)
11607c478bd9Sstevel@tonic-gate {
11617c478bd9Sstevel@tonic-gate 	int fn_dflag = fn_dest->fn_flag;
11627c478bd9Sstevel@tonic-gate 
11637c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&fn_lock->flk_lock));
11647c478bd9Sstevel@tonic-gate 	ASSERT(fn_dest->fn_dest->fn_count < Fifohiwat);
11657c478bd9Sstevel@tonic-gate 	if ((fn_dflag & FIFOWANTW)) {
11667c478bd9Sstevel@tonic-gate 		cv_broadcast(&fn_dest->fn_wait_cv);
11677c478bd9Sstevel@tonic-gate 	}
11687c478bd9Sstevel@tonic-gate 	if ((fn_dflag & (FIFOHIWATW | FIFOISOPEN)) ==
11697c478bd9Sstevel@tonic-gate 	    (FIFOHIWATW | FIFOISOPEN)) {
11707c478bd9Sstevel@tonic-gate 		if (fn_dflag & FIFOPOLLW)
11717c478bd9Sstevel@tonic-gate 			strpollwakeup(FTOV(fn_dest), POLLWRNORM);
11727c478bd9Sstevel@tonic-gate 		if (fn_dflag & FIFOSETSIG)
11737c478bd9Sstevel@tonic-gate 			str_sendsig(FTOV(fn_dest), S_WRNORM, 0, 0);
11747c478bd9Sstevel@tonic-gate 	}
11757c478bd9Sstevel@tonic-gate 	/*
11767c478bd9Sstevel@tonic-gate 	 * FIFOPOLLW can't be set without setting FIFOHIWAT
11777c478bd9Sstevel@tonic-gate 	 * This allows us to clear both here.
11787c478bd9Sstevel@tonic-gate 	 */
11797c478bd9Sstevel@tonic-gate 	fn_dest->fn_flag = fn_dflag & ~(FIFOWANTW | FIFOHIWATW | FIFOPOLLW);
11807c478bd9Sstevel@tonic-gate }
11817c478bd9Sstevel@tonic-gate 
11827c478bd9Sstevel@tonic-gate /*
11837c478bd9Sstevel@tonic-gate  * wake up any sleeping readers, poll or send signal if needed
11847c478bd9Sstevel@tonic-gate  * FIFOWANTR indicates that a process is waiting in fifo_read() for data
11857c478bd9Sstevel@tonic-gate  * FIFOSETSIG indicates that SIGPOLL should be sent to process
11867c478bd9Sstevel@tonic-gate  * FIFOPOLLR indicates that a poll request for reading on the fifo was made
11877c478bd9Sstevel@tonic-gate  */
11887c478bd9Sstevel@tonic-gate 
11897c478bd9Sstevel@tonic-gate void
fifo_wakereader(fifonode_t * fn_dest,fifolock_t * fn_lock)11907c478bd9Sstevel@tonic-gate fifo_wakereader(fifonode_t *fn_dest, fifolock_t *fn_lock)
11917c478bd9Sstevel@tonic-gate {
11927c478bd9Sstevel@tonic-gate 	int fn_dflag = fn_dest->fn_flag;
11937c478bd9Sstevel@tonic-gate 
11947c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&fn_lock->flk_lock));
11957c478bd9Sstevel@tonic-gate 	if (fn_dflag & FIFOWANTR) {
11967c478bd9Sstevel@tonic-gate 		cv_broadcast(&fn_dest->fn_wait_cv);
11977c478bd9Sstevel@tonic-gate 	}
11987c478bd9Sstevel@tonic-gate 	if (fn_dflag & FIFOISOPEN) {
11997c478bd9Sstevel@tonic-gate 		if (fn_dflag & FIFOPOLLR)
12007c478bd9Sstevel@tonic-gate 			strpollwakeup(FTOV(fn_dest), POLLIN | POLLRDNORM);
12017c478bd9Sstevel@tonic-gate 		if (fn_dflag & FIFOSETSIG)
12027c478bd9Sstevel@tonic-gate 			str_sendsig(FTOV(fn_dest), S_INPUT | S_RDNORM, 0, 0);
12037c478bd9Sstevel@tonic-gate 	}
12047c478bd9Sstevel@tonic-gate 	fn_dest->fn_flag = fn_dflag & ~(FIFOWANTR | FIFOPOLLR);
12057c478bd9Sstevel@tonic-gate }
1206