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