xref: /onnv-gate/usr/src/uts/common/fs/fifofs/fifosubr.c (revision 1799:410b1fe26fef)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*1799Sgfaden  * Common Development and Distribution License (the "License").
6*1799Sgfaden  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
220Sstevel@tonic-gate 
230Sstevel@tonic-gate /*
24*1799Sgfaden  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
250Sstevel@tonic-gate  * Use is subject to license terms.
260Sstevel@tonic-gate  */
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
290Sstevel@tonic-gate 
300Sstevel@tonic-gate /*
310Sstevel@tonic-gate  * The routines defined in this file are supporting routines for FIFOFS
320Sstevel@tonic-gate  * file sytem type.
330Sstevel@tonic-gate  */
340Sstevel@tonic-gate #include <sys/types.h>
350Sstevel@tonic-gate #include <sys/param.h>
360Sstevel@tonic-gate #include <sys/systm.h>
370Sstevel@tonic-gate #include <sys/debug.h>
380Sstevel@tonic-gate #include <sys/errno.h>
390Sstevel@tonic-gate #include <sys/time.h>
400Sstevel@tonic-gate #include <sys/kmem.h>
410Sstevel@tonic-gate #include <sys/inline.h>
420Sstevel@tonic-gate #include <sys/file.h>
430Sstevel@tonic-gate #include <sys/proc.h>
440Sstevel@tonic-gate #include <sys/stat.h>
450Sstevel@tonic-gate #include <sys/sysmacros.h>
460Sstevel@tonic-gate #include <sys/var.h>
470Sstevel@tonic-gate #include <sys/vfs.h>
480Sstevel@tonic-gate #include <sys/vnode.h>
490Sstevel@tonic-gate #include <sys/mode.h>
500Sstevel@tonic-gate #include <sys/signal.h>
510Sstevel@tonic-gate #include <sys/user.h>
520Sstevel@tonic-gate #include <sys/uio.h>
530Sstevel@tonic-gate #include <sys/flock.h>
540Sstevel@tonic-gate #include <sys/stream.h>
550Sstevel@tonic-gate #include <sys/fs/fifonode.h>
560Sstevel@tonic-gate #include <sys/strsubr.h>
570Sstevel@tonic-gate #include <sys/stropts.h>
580Sstevel@tonic-gate #include <sys/cmn_err.h>
590Sstevel@tonic-gate #include <fs/fs_subr.h>
600Sstevel@tonic-gate #include <sys/ddi.h>
610Sstevel@tonic-gate 
620Sstevel@tonic-gate 
630Sstevel@tonic-gate #if FIFODEBUG
640Sstevel@tonic-gate int Fifo_fastmode = 1;		/* pipes/fifos will be opened in fast mode */
650Sstevel@tonic-gate int Fifo_verbose = 0;		/* msg when switching out of fast mode */
660Sstevel@tonic-gate int Fifohiwat = FIFOHIWAT;	/* Modifiable FIFO high water mark */
670Sstevel@tonic-gate #endif
680Sstevel@tonic-gate 
690Sstevel@tonic-gate /*
700Sstevel@tonic-gate  * This is the loadable module wrapper.
710Sstevel@tonic-gate  */
720Sstevel@tonic-gate #include <sys/modctl.h>
730Sstevel@tonic-gate 
740Sstevel@tonic-gate extern struct qinit fifo_strdata;
750Sstevel@tonic-gate 
760Sstevel@tonic-gate struct vfsops *fifo_vfsops;
770Sstevel@tonic-gate 
780Sstevel@tonic-gate static vfsdef_t vfw = {
790Sstevel@tonic-gate 	VFSDEF_VERSION,
800Sstevel@tonic-gate 	"fifofs",
810Sstevel@tonic-gate 	fifoinit,
820Sstevel@tonic-gate 	0,
830Sstevel@tonic-gate 	NULL
840Sstevel@tonic-gate };
850Sstevel@tonic-gate 
860Sstevel@tonic-gate /*
870Sstevel@tonic-gate  * Module linkage information for the kernel.
880Sstevel@tonic-gate  */
890Sstevel@tonic-gate extern struct mod_ops mod_fsops;
900Sstevel@tonic-gate 
910Sstevel@tonic-gate static struct modlfs modlfs = {
920Sstevel@tonic-gate 	&mod_fsops, "filesystem for fifo", &vfw
930Sstevel@tonic-gate };
940Sstevel@tonic-gate 
950Sstevel@tonic-gate static struct modlinkage modlinkage = {
960Sstevel@tonic-gate 	MODREV_1, (void *)&modlfs, NULL
970Sstevel@tonic-gate };
980Sstevel@tonic-gate 
990Sstevel@tonic-gate int
1000Sstevel@tonic-gate _init()
1010Sstevel@tonic-gate {
1020Sstevel@tonic-gate 	return (mod_install(&modlinkage));
1030Sstevel@tonic-gate }
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate int
1060Sstevel@tonic-gate _info(struct modinfo *modinfop)
1070Sstevel@tonic-gate {
1080Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
1090Sstevel@tonic-gate }
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate /*
1120Sstevel@tonic-gate  * Define data structures within this file.
1130Sstevel@tonic-gate  * XXX should the hash size be configurable ?
1140Sstevel@tonic-gate  */
1150Sstevel@tonic-gate #define	FIFOSHFT	5
1160Sstevel@tonic-gate #define	FIFO_HASHSZ	63
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate #if ((FIFO_HASHSZ & (FIFO_HASHSZ - 1)) == 0)
1190Sstevel@tonic-gate #define	FIFOHASH(vp) (((uintptr_t)(vp) >> FIFOSHFT) & (FIFO_HASHSZ - 1))
1200Sstevel@tonic-gate #else
1210Sstevel@tonic-gate #define	FIFOHASH(vp) (((uintptr_t)(vp) >> FIFOSHFT) % FIFO_HASHSZ)
1220Sstevel@tonic-gate #endif
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate fifonode_t	*fifoalloc[FIFO_HASHSZ];
1250Sstevel@tonic-gate dev_t		fifodev;
1260Sstevel@tonic-gate struct vfs	*fifovfsp;
1270Sstevel@tonic-gate int		fifofstype;
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate kmutex_t ftable_lock;
1300Sstevel@tonic-gate static kmutex_t fino_lock;
1310Sstevel@tonic-gate struct kmem_cache *fnode_cache;
1320Sstevel@tonic-gate struct kmem_cache *pipe_cache;
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate static void fifoinsert(fifonode_t *);
1350Sstevel@tonic-gate static fifonode_t *fifofind(vnode_t *);
1360Sstevel@tonic-gate static int fifo_connld(struct vnode **, int, cred_t *);
1370Sstevel@tonic-gate static void fifo_fastturnoff(fifonode_t *);
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate static void fifo_reinit_vp(vnode_t *);
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate /*
1420Sstevel@tonic-gate  * Constructor/destructor routines for fifos and pipes.
1430Sstevel@tonic-gate  *
1440Sstevel@tonic-gate  * In the interest of code sharing, we define a common fifodata structure
1450Sstevel@tonic-gate  * which consists of a fifolock and one or two fnodes.  A fifo contains
1460Sstevel@tonic-gate  * one fnode; a pipe contains two.  The fifolock is shared by the fnodes,
1470Sstevel@tonic-gate  * each of which points to it:
1480Sstevel@tonic-gate  *
1490Sstevel@tonic-gate  *	--> -->	---------  --- ---
1500Sstevel@tonic-gate  *	|   |	| lock	|   |	|
1510Sstevel@tonic-gate  *	|   |	---------   |	|
1520Sstevel@tonic-gate  *	|   |	|	|  fifo	|
1530Sstevel@tonic-gate  *	|   --- | fnode	|   |	|
1540Sstevel@tonic-gate  *	|	|	|   |  pipe
1550Sstevel@tonic-gate  *	|	---------  ---	|
1560Sstevel@tonic-gate  *	|	|	|	|
1570Sstevel@tonic-gate  *	------- | fnode	|	|
1580Sstevel@tonic-gate  *		|	|	|
1590Sstevel@tonic-gate  *		---------      ---
1600Sstevel@tonic-gate  *
1610Sstevel@tonic-gate  * Since the fifolock is at the beginning of the fifodata structure,
1620Sstevel@tonic-gate  * the fifolock address is the same as the fifodata address.  Thus,
1630Sstevel@tonic-gate  * we can determine the fifodata address from any of its member fnodes.
1640Sstevel@tonic-gate  * This is essential for fifo_inactive.
1650Sstevel@tonic-gate  *
1660Sstevel@tonic-gate  * The fnode constructor is designed to handle any fifodata struture,
1670Sstevel@tonic-gate  * deducing the number of fnodes from the total size.  Thus, the fnode
1680Sstevel@tonic-gate  * constructor does most of the work for the pipe constructor.
1690Sstevel@tonic-gate  */
1700Sstevel@tonic-gate /*ARGSUSED1*/
1710Sstevel@tonic-gate static int
1720Sstevel@tonic-gate fnode_constructor(void *buf, void *cdrarg, int kmflags)
1730Sstevel@tonic-gate {
1740Sstevel@tonic-gate 	fifodata_t *fdp = buf;
1750Sstevel@tonic-gate 	fifolock_t *flp = &fdp->fifo_lock;
1760Sstevel@tonic-gate 	fifonode_t *fnp = &fdp->fifo_fnode[0];
1770Sstevel@tonic-gate 	size_t size = (uintptr_t)cdrarg;
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate 	mutex_init(&flp->flk_lock, NULL, MUTEX_DEFAULT, NULL);
1800Sstevel@tonic-gate 	cv_init(&flp->flk_wait_cv, NULL, CV_DEFAULT, NULL);
1810Sstevel@tonic-gate 	flp->flk_ocsync = 0;
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate 	while ((char *)fnp < (char *)buf + size) {
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate 		vnode_t *vp;
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate 		vp = vn_alloc(KM_SLEEP);
1880Sstevel@tonic-gate 		fnp->fn_vnode = vp;
1890Sstevel@tonic-gate 
1900Sstevel@tonic-gate 		fnp->fn_lock = flp;
1910Sstevel@tonic-gate 		fnp->fn_open = 0;
1920Sstevel@tonic-gate 		fnp->fn_dest = fnp;
1930Sstevel@tonic-gate 		fnp->fn_mp = NULL;
1940Sstevel@tonic-gate 		fnp->fn_count = 0;
1950Sstevel@tonic-gate 		fnp->fn_rsynccnt = 0;
1960Sstevel@tonic-gate 		fnp->fn_wsynccnt = 0;
1970Sstevel@tonic-gate 		fnp->fn_wwaitcnt = 0;
1980Sstevel@tonic-gate 		fnp->fn_insync = 0;
1990Sstevel@tonic-gate 		fnp->fn_pcredp = NULL;
2000Sstevel@tonic-gate 		fnp->fn_cpid = -1;
2010Sstevel@tonic-gate 		/*
2020Sstevel@tonic-gate 		 * 32-bit stat(2) may fail if fn_ino isn't initialized
2030Sstevel@tonic-gate 		 */
2040Sstevel@tonic-gate 		fnp->fn_ino = 0;
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate 		cv_init(&fnp->fn_wait_cv, NULL, CV_DEFAULT, NULL);
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate 		vn_setops(vp, fifo_vnodeops);
2090Sstevel@tonic-gate 		vp->v_stream = NULL;
2100Sstevel@tonic-gate 		vp->v_type = VFIFO;
2110Sstevel@tonic-gate 		vp->v_data = (caddr_t)fnp;
2120Sstevel@tonic-gate 		vp->v_flag = VNOMAP | VNOSWAP;
2130Sstevel@tonic-gate 		vn_exists(vp);
2140Sstevel@tonic-gate 		fnp++;
2150Sstevel@tonic-gate 	}
2160Sstevel@tonic-gate 	return (0);
2170Sstevel@tonic-gate }
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate static void
2200Sstevel@tonic-gate fnode_destructor(void *buf, void *cdrarg)
2210Sstevel@tonic-gate {
2220Sstevel@tonic-gate 	fifodata_t *fdp = buf;
2230Sstevel@tonic-gate 	fifolock_t *flp = &fdp->fifo_lock;
2240Sstevel@tonic-gate 	fifonode_t *fnp = &fdp->fifo_fnode[0];
2250Sstevel@tonic-gate 	size_t size = (uintptr_t)cdrarg;
2260Sstevel@tonic-gate 
2270Sstevel@tonic-gate 	mutex_destroy(&flp->flk_lock);
2280Sstevel@tonic-gate 	cv_destroy(&flp->flk_wait_cv);
2290Sstevel@tonic-gate 	ASSERT(flp->flk_ocsync == 0);
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate 	while ((char *)fnp < (char *)buf + size) {
2320Sstevel@tonic-gate 
2330Sstevel@tonic-gate 		vnode_t *vp = FTOV(fnp);
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate 		ASSERT(fnp->fn_mp == NULL);
2360Sstevel@tonic-gate 		ASSERT(fnp->fn_count == 0);
2370Sstevel@tonic-gate 		ASSERT(fnp->fn_lock == flp);
2380Sstevel@tonic-gate 		ASSERT(fnp->fn_open == 0);
2390Sstevel@tonic-gate 		ASSERT(fnp->fn_insync == 0);
2400Sstevel@tonic-gate 		ASSERT(fnp->fn_rsynccnt == 0 && fnp->fn_wsynccnt == 0);
2410Sstevel@tonic-gate 		ASSERT(fnp->fn_wwaitcnt == 0);
2420Sstevel@tonic-gate 		ASSERT(fnp->fn_pcredp == NULL);
2430Sstevel@tonic-gate 		ASSERT(vn_matchops(vp, fifo_vnodeops));
2440Sstevel@tonic-gate 		ASSERT(vp->v_stream == NULL);
2450Sstevel@tonic-gate 		ASSERT(vp->v_type == VFIFO);
2460Sstevel@tonic-gate 		ASSERT(vp->v_data == (caddr_t)fnp);
2470Sstevel@tonic-gate 		ASSERT((vp->v_flag & (VNOMAP|VNOSWAP)) == (VNOMAP|VNOSWAP));
2480Sstevel@tonic-gate 
2490Sstevel@tonic-gate 		cv_destroy(&fnp->fn_wait_cv);
2500Sstevel@tonic-gate 		vn_invalid(vp);
2510Sstevel@tonic-gate 		vn_free(vp);
2520Sstevel@tonic-gate 
2530Sstevel@tonic-gate 		fnp++;
2540Sstevel@tonic-gate 	}
2550Sstevel@tonic-gate }
2560Sstevel@tonic-gate 
2570Sstevel@tonic-gate static int
2580Sstevel@tonic-gate pipe_constructor(void *buf, void *cdrarg, int kmflags)
2590Sstevel@tonic-gate {
2600Sstevel@tonic-gate 	fifodata_t *fdp = buf;
2610Sstevel@tonic-gate 	fifonode_t *fnp1 = &fdp->fifo_fnode[0];
2620Sstevel@tonic-gate 	fifonode_t *fnp2 = &fdp->fifo_fnode[1];
2630Sstevel@tonic-gate 	vnode_t *vp1;
2640Sstevel@tonic-gate 	vnode_t *vp2;
2650Sstevel@tonic-gate 
2660Sstevel@tonic-gate 	(void) fnode_constructor(buf, cdrarg, kmflags);
2670Sstevel@tonic-gate 
2680Sstevel@tonic-gate 	vp1 = FTOV(fnp1);
2690Sstevel@tonic-gate 	vp2 = FTOV(fnp2);
2700Sstevel@tonic-gate 
2710Sstevel@tonic-gate 	vp1->v_vfsp	= vp2->v_vfsp		= fifovfsp;
2720Sstevel@tonic-gate 	vp1->v_rdev	= vp2->v_rdev		= fifodev;
2730Sstevel@tonic-gate 	fnp1->fn_realvp	= fnp2->fn_realvp	= NULL;
2740Sstevel@tonic-gate 	fnp1->fn_dest	= fnp2;
2750Sstevel@tonic-gate 	fnp2->fn_dest	= fnp1;
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate 	return (0);
2780Sstevel@tonic-gate }
2790Sstevel@tonic-gate 
2800Sstevel@tonic-gate static void
2810Sstevel@tonic-gate pipe_destructor(void *buf, void *cdrarg)
2820Sstevel@tonic-gate {
2830Sstevel@tonic-gate #ifdef DEBUG
2840Sstevel@tonic-gate 	fifodata_t *fdp = buf;
2850Sstevel@tonic-gate 	fifonode_t *fnp1 = &fdp->fifo_fnode[0];
2860Sstevel@tonic-gate 	fifonode_t *fnp2 = &fdp->fifo_fnode[1];
2870Sstevel@tonic-gate 	vnode_t *vp1 = FTOV(fnp1);
2880Sstevel@tonic-gate 	vnode_t *vp2 = FTOV(fnp2);
2890Sstevel@tonic-gate 
2900Sstevel@tonic-gate 	ASSERT(vp1->v_vfsp == fifovfsp);
2910Sstevel@tonic-gate 	ASSERT(vp2->v_vfsp == fifovfsp);
2920Sstevel@tonic-gate 	ASSERT(vp1->v_rdev == fifodev);
2930Sstevel@tonic-gate 	ASSERT(vp2->v_rdev == fifodev);
2940Sstevel@tonic-gate #endif
2950Sstevel@tonic-gate 	fnode_destructor(buf, cdrarg);
2960Sstevel@tonic-gate }
2970Sstevel@tonic-gate 
2980Sstevel@tonic-gate /*
2990Sstevel@tonic-gate  * Reinitialize a FIFO vnode (uses normal vnode reinit, but ensures that
3000Sstevel@tonic-gate  * vnode type and flags are reset).
3010Sstevel@tonic-gate  */
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate static void fifo_reinit_vp(vnode_t *vp)
3040Sstevel@tonic-gate {
3050Sstevel@tonic-gate 	vn_reinit(vp);
3060Sstevel@tonic-gate 	vp->v_type = VFIFO;
3070Sstevel@tonic-gate 	vp->v_flag = VNOMAP | VNOSWAP;
3080Sstevel@tonic-gate }
3090Sstevel@tonic-gate 
3100Sstevel@tonic-gate /*
3110Sstevel@tonic-gate  * Save file system type/index, initialize vfs operations vector, get
3120Sstevel@tonic-gate  * unique device number for FIFOFS and initialize the FIFOFS hash.
3130Sstevel@tonic-gate  * Create and initialize a "generic" vfs pointer that will be placed
3140Sstevel@tonic-gate  * in the v_vfsp field of each pipe's vnode.
3150Sstevel@tonic-gate  */
3160Sstevel@tonic-gate int
3170Sstevel@tonic-gate fifoinit(int fstype, char *name)
3180Sstevel@tonic-gate {
3190Sstevel@tonic-gate 	static const fs_operation_def_t fifo_vfsops_template[] = {
3200Sstevel@tonic-gate 		NULL, NULL
3210Sstevel@tonic-gate 	};
3220Sstevel@tonic-gate 	int error;
3230Sstevel@tonic-gate 	major_t dev;
3240Sstevel@tonic-gate 
3250Sstevel@tonic-gate 	fifofstype = fstype;
3260Sstevel@tonic-gate 	error = vfs_setfsops(fstype, fifo_vfsops_template, &fifo_vfsops);
3270Sstevel@tonic-gate 	if (error != 0) {
3280Sstevel@tonic-gate 		cmn_err(CE_WARN, "fifoinit: bad vfs ops template");
3290Sstevel@tonic-gate 		return (error);
3300Sstevel@tonic-gate 	}
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate 	error = vn_make_ops(name, fifo_vnodeops_template, &fifo_vnodeops);
3330Sstevel@tonic-gate 	if (error != 0) {
3340Sstevel@tonic-gate 		(void) vfs_freevfsops_by_type(fstype);
3350Sstevel@tonic-gate 		cmn_err(CE_WARN, "fifoinit: bad vnode ops template");
3360Sstevel@tonic-gate 		return (error);
3370Sstevel@tonic-gate 	}
3380Sstevel@tonic-gate 
3390Sstevel@tonic-gate 	if ((dev = getudev()) == (major_t)-1) {
3400Sstevel@tonic-gate 		cmn_err(CE_WARN, "fifoinit: can't get unique device number");
3410Sstevel@tonic-gate 		dev = 0;
3420Sstevel@tonic-gate 	}
3430Sstevel@tonic-gate 	fifodev = makedevice(dev, 0);
3440Sstevel@tonic-gate 
3450Sstevel@tonic-gate 	fifovfsp = kmem_zalloc(sizeof (struct vfs), KM_SLEEP);
3460Sstevel@tonic-gate 	fifovfsp->vfs_next = NULL;
3470Sstevel@tonic-gate 	vfs_setops(fifovfsp, fifo_vfsops);
3480Sstevel@tonic-gate 	fifovfsp->vfs_vnodecovered = NULL;
3490Sstevel@tonic-gate 	fifovfsp->vfs_flag = 0;
3500Sstevel@tonic-gate 	fifovfsp->vfs_bsize = 1024;
3510Sstevel@tonic-gate 	fifovfsp->vfs_fstype = fifofstype;
3520Sstevel@tonic-gate 	vfs_make_fsid(&fifovfsp->vfs_fsid, fifodev, fifofstype);
3530Sstevel@tonic-gate 	fifovfsp->vfs_data = NULL;
3540Sstevel@tonic-gate 	fifovfsp->vfs_dev = fifodev;
3550Sstevel@tonic-gate 	fifovfsp->vfs_bcount = 0;
3560Sstevel@tonic-gate 
3570Sstevel@tonic-gate 	mutex_init(&ftable_lock, NULL, MUTEX_DEFAULT, NULL);
3580Sstevel@tonic-gate 	mutex_init(&fino_lock, NULL, MUTEX_DEFAULT, NULL);
3590Sstevel@tonic-gate 
3600Sstevel@tonic-gate 	/*
3610Sstevel@tonic-gate 	 * vnodes are cached aligned
3620Sstevel@tonic-gate 	 */
3630Sstevel@tonic-gate 	fnode_cache = kmem_cache_create("fnode_cache",
3640Sstevel@tonic-gate 		sizeof (fifodata_t) - sizeof (fifonode_t), 32,
3650Sstevel@tonic-gate 		fnode_constructor, fnode_destructor, NULL,
3660Sstevel@tonic-gate 		(void *)(sizeof (fifodata_t) - sizeof (fifonode_t)), NULL, 0);
3670Sstevel@tonic-gate 
3680Sstevel@tonic-gate 	pipe_cache = kmem_cache_create("pipe_cache", sizeof (fifodata_t), 32,
3690Sstevel@tonic-gate 		pipe_constructor, pipe_destructor, NULL,
3700Sstevel@tonic-gate 		(void *)(sizeof (fifodata_t)), NULL, 0);
3710Sstevel@tonic-gate 
3720Sstevel@tonic-gate #if FIFODEBUG
3730Sstevel@tonic-gate 	if (Fifohiwat < FIFOHIWAT)
3740Sstevel@tonic-gate 		Fifohiwat = FIFOHIWAT;
3750Sstevel@tonic-gate #endif /* FIFODEBUG */
3760Sstevel@tonic-gate 	fifo_strdata.qi_minfo->mi_hiwat = Fifohiwat;
3770Sstevel@tonic-gate 
3780Sstevel@tonic-gate 	return (0);
3790Sstevel@tonic-gate }
3800Sstevel@tonic-gate 
3810Sstevel@tonic-gate /*
382142Speterte  * Provide a shadow for a vnode.  We create a new shadow before checking for an
383142Speterte  * existing one, to minimize the amount of time we need to hold ftable_lock.
384142Speterte  * If a vp already has a shadow in the hash list, return its shadow.  If not,
385142Speterte  * we hash the new vnode and return its pointer to the caller.
3860Sstevel@tonic-gate  */
3870Sstevel@tonic-gate vnode_t *
3880Sstevel@tonic-gate fifovp(vnode_t *vp, cred_t *crp)
3890Sstevel@tonic-gate {
3900Sstevel@tonic-gate 	fifonode_t *fnp;
391142Speterte 	fifonode_t *spec_fnp;   /* Speculative fnode ptr. */
3920Sstevel@tonic-gate 	fifodata_t *fdp;
3930Sstevel@tonic-gate 	vnode_t *newvp;
3940Sstevel@tonic-gate 	struct vattr va;
3950Sstevel@tonic-gate 
3960Sstevel@tonic-gate 	ASSERT(vp != NULL);
3970Sstevel@tonic-gate 
3980Sstevel@tonic-gate 	fdp = kmem_cache_alloc(fnode_cache, KM_SLEEP);
3990Sstevel@tonic-gate 
4000Sstevel@tonic-gate 	fdp->fifo_lock.flk_ref = 1;
4010Sstevel@tonic-gate 	fnp = &fdp->fifo_fnode[0];
402*1799Sgfaden 
403*1799Sgfaden 	/*
404*1799Sgfaden 	 * In Trusted Extensions cross-zone named pipes
405*1799Sgfaden 	 * are supported subject to the MAC policy. Since
406*1799Sgfaden 	 * cross-zone access is done using lofs mounts,
407*1799Sgfaden 	 * it is necessary to use the real vnode so that
408*1799Sgfaden 	 * matching ends of the fifo can find each other.
409*1799Sgfaden 	 */
410*1799Sgfaden 	if (is_system_labeled()) {
411*1799Sgfaden 		vnode_t	*rvp;
412*1799Sgfaden 
413*1799Sgfaden 		if (VOP_REALVP(vp, &rvp) == 0)
414*1799Sgfaden 			vp = rvp;
415*1799Sgfaden 	}
416*1799Sgfaden 
4170Sstevel@tonic-gate 	fnp->fn_realvp	= vp;
4180Sstevel@tonic-gate 	fnp->fn_wcnt	= 0;
4190Sstevel@tonic-gate 	fnp->fn_rcnt	= 0;
420142Speterte 
4210Sstevel@tonic-gate #if FIFODEBUG
4220Sstevel@tonic-gate 	if (! Fifo_fastmode) {
4230Sstevel@tonic-gate 		fnp->fn_flag	= 0;
4240Sstevel@tonic-gate 	} else {
4250Sstevel@tonic-gate 		fnp->fn_flag	= FIFOFAST;
4260Sstevel@tonic-gate 	}
4270Sstevel@tonic-gate #else /* FIFODEBUG */
4280Sstevel@tonic-gate 	fnp->fn_flag	= FIFOFAST;
4290Sstevel@tonic-gate #endif /* FIFODEBUG */
4300Sstevel@tonic-gate 
4310Sstevel@tonic-gate 	/*
4320Sstevel@tonic-gate 	 * initialize the times from vp.
4330Sstevel@tonic-gate 	 */
4340Sstevel@tonic-gate 	va.va_mask = AT_TIMES;
4350Sstevel@tonic-gate 	if (VOP_GETATTR(vp, &va, 0, crp) == 0) {
4360Sstevel@tonic-gate 		fnp->fn_atime = va.va_atime.tv_sec;
4370Sstevel@tonic-gate 		fnp->fn_mtime = va.va_mtime.tv_sec;
4380Sstevel@tonic-gate 		fnp->fn_ctime = va.va_ctime.tv_sec;
4390Sstevel@tonic-gate 	} else {
4400Sstevel@tonic-gate 		fnp->fn_atime = 0;
4410Sstevel@tonic-gate 		fnp->fn_mtime = 0;
4420Sstevel@tonic-gate 		fnp->fn_ctime = 0;
4430Sstevel@tonic-gate 	}
4440Sstevel@tonic-gate 
445142Speterte 	/*
446142Speterte 	 * Grab the VP here to avoid holding locks
447142Speterte 	 * whilst trying to acquire others.
448142Speterte 	 */
449142Speterte 
4500Sstevel@tonic-gate 	VN_HOLD(vp);
4510Sstevel@tonic-gate 
452142Speterte 	mutex_enter(&ftable_lock);
453142Speterte 
454142Speterte 	if ((spec_fnp = fifofind(vp)) != NULL) {
455142Speterte 		mutex_exit(&ftable_lock);
456142Speterte 
457142Speterte 		/*
458142Speterte 		 * Release the vnode and free up our pre-prepared fnode.
459142Speterte 		 * Zero the lock reference just to explicitly signal
460142Speterte 		 * this is unused.
461142Speterte 		 */
462142Speterte 		VN_RELE(vp);
463142Speterte 		fdp->fifo_lock.flk_ref = 0;
464142Speterte 		kmem_cache_free(fnode_cache, fdp);
465142Speterte 
466142Speterte 		return (FTOV(spec_fnp));
467142Speterte 	}
468142Speterte 
4690Sstevel@tonic-gate 	newvp = FTOV(fnp);
4700Sstevel@tonic-gate 	fifo_reinit_vp(newvp);
4710Sstevel@tonic-gate 	newvp->v_vfsp = vp->v_vfsp;
4720Sstevel@tonic-gate 	newvp->v_rdev = vp->v_rdev;
4730Sstevel@tonic-gate 
4740Sstevel@tonic-gate 	fifoinsert(fnp);
4750Sstevel@tonic-gate 	mutex_exit(&ftable_lock);
4760Sstevel@tonic-gate 
4770Sstevel@tonic-gate 	return (newvp);
4780Sstevel@tonic-gate }
4790Sstevel@tonic-gate 
4800Sstevel@tonic-gate /*
4810Sstevel@tonic-gate  * Create a pipe end by...
4820Sstevel@tonic-gate  * allocating a vnode-fifonode pair and initializing the fifonode.
4830Sstevel@tonic-gate  */
4840Sstevel@tonic-gate void
4850Sstevel@tonic-gate makepipe(vnode_t **vpp1, vnode_t **vpp2)
4860Sstevel@tonic-gate {
4870Sstevel@tonic-gate 	fifonode_t *fnp1;
4880Sstevel@tonic-gate 	fifonode_t *fnp2;
4890Sstevel@tonic-gate 	vnode_t *nvp1;
4900Sstevel@tonic-gate 	vnode_t *nvp2;
4910Sstevel@tonic-gate 	fifodata_t *fdp;
4920Sstevel@tonic-gate 	time_t now;
4930Sstevel@tonic-gate 
4940Sstevel@tonic-gate 	fdp = kmem_cache_alloc(pipe_cache, KM_SLEEP);
4950Sstevel@tonic-gate 	fdp->fifo_lock.flk_ref = 2;
4960Sstevel@tonic-gate 	fnp1 = &fdp->fifo_fnode[0];
4970Sstevel@tonic-gate 	fnp2 = &fdp->fifo_fnode[1];
4980Sstevel@tonic-gate 
4990Sstevel@tonic-gate 	fnp1->fn_wcnt	= fnp2->fn_wcnt		= 1;
5000Sstevel@tonic-gate 	fnp1->fn_rcnt	= fnp2->fn_rcnt		= 1;
5010Sstevel@tonic-gate #if FIFODEBUG
5020Sstevel@tonic-gate 	if (! Fifo_fastmode) {
5030Sstevel@tonic-gate 		fnp1->fn_flag	= fnp2->fn_flag		= ISPIPE;
5040Sstevel@tonic-gate 	} else {
5050Sstevel@tonic-gate 		fnp1->fn_flag	= fnp2->fn_flag		= ISPIPE | FIFOFAST;
5060Sstevel@tonic-gate 	}
5070Sstevel@tonic-gate #else /* FIFODEBUG */
5080Sstevel@tonic-gate 	fnp1->fn_flag	= fnp2->fn_flag		= ISPIPE | FIFOFAST;
5090Sstevel@tonic-gate #endif /* FIFODEBUG */
5100Sstevel@tonic-gate 	now = gethrestime_sec();
5110Sstevel@tonic-gate 	fnp1->fn_atime	= fnp2->fn_atime	= now;
5120Sstevel@tonic-gate 	fnp1->fn_mtime	= fnp2->fn_mtime	= now;
5130Sstevel@tonic-gate 	fnp1->fn_ctime	= fnp2->fn_ctime	= now;
5140Sstevel@tonic-gate 
5150Sstevel@tonic-gate 	*vpp1 = nvp1 = FTOV(fnp1);
5160Sstevel@tonic-gate 	*vpp2 = nvp2 = FTOV(fnp2);
5170Sstevel@tonic-gate 
5180Sstevel@tonic-gate 	fifo_reinit_vp(nvp1);		/* Reinitialize vnodes for reuse... */
5190Sstevel@tonic-gate 	fifo_reinit_vp(nvp2);
5200Sstevel@tonic-gate 	nvp1->v_vfsp = fifovfsp; 	/* Need to re-establish VFS & device */
5210Sstevel@tonic-gate 	nvp2->v_vfsp = fifovfsp; 	/* before we can reuse this vnode. */
5220Sstevel@tonic-gate 	nvp1->v_rdev = fifodev;
5230Sstevel@tonic-gate 	nvp2->v_rdev = fifodev;
5240Sstevel@tonic-gate }
5250Sstevel@tonic-gate 
5260Sstevel@tonic-gate /*
5270Sstevel@tonic-gate  * Attempt to establish a unique pipe id.  Only un-named pipes use this
5280Sstevel@tonic-gate  * routine.
5290Sstevel@tonic-gate  */
5300Sstevel@tonic-gate ino_t
5310Sstevel@tonic-gate fifogetid(void)
5320Sstevel@tonic-gate {
5330Sstevel@tonic-gate 	static ino_t fifo_ino = 0;
5340Sstevel@tonic-gate 	ino_t fino;
5350Sstevel@tonic-gate 
5360Sstevel@tonic-gate 	mutex_enter(&fino_lock);
5370Sstevel@tonic-gate 	fino = fifo_ino++;
5380Sstevel@tonic-gate 	mutex_exit(&fino_lock);
5390Sstevel@tonic-gate 	return (fino);
5400Sstevel@tonic-gate }
5410Sstevel@tonic-gate 
5420Sstevel@tonic-gate 
5430Sstevel@tonic-gate /*
5440Sstevel@tonic-gate  * Stream a pipe/FIFO.
5450Sstevel@tonic-gate  * The FIFOCONNLD flag is used when CONNLD has been pushed on the stream.
5460Sstevel@tonic-gate  * If the flag is set, a new vnode is created by calling fifo_connld().
5470Sstevel@tonic-gate  * Connld logic was moved to fifo_connld() to speed up the open
5480Sstevel@tonic-gate  * operation, simplify the connld/fifo interaction, and remove inherent
5490Sstevel@tonic-gate  * race conditions between the connld module and fifos.
5500Sstevel@tonic-gate  * This routine is single threaded for two reasons.
5510Sstevel@tonic-gate  * 1) connld requests are synchronous; that is, they must block
5520Sstevel@tonic-gate  *    until the server does an I_RECVFD (oh, well).  Single threading is
5530Sstevel@tonic-gate  *    the simplest way to accomplish this.
5540Sstevel@tonic-gate  * 2) fifo_close() must not send M_HANGUP or M_ERROR while we are
5550Sstevel@tonic-gate  *    in stropen. Stropen() has a tendency to reset things and
5560Sstevel@tonic-gate  *    we would like streams to remember that a hangup occurred.
5570Sstevel@tonic-gate  */
5580Sstevel@tonic-gate int
5590Sstevel@tonic-gate fifo_stropen(vnode_t **vpp, int flag, cred_t *crp, int dotwist, int lockheld)
5600Sstevel@tonic-gate {
5610Sstevel@tonic-gate 	int error = 0;
5620Sstevel@tonic-gate 	vnode_t *oldvp = *vpp;
5630Sstevel@tonic-gate 	fifonode_t *fnp = VTOF(*vpp);
5640Sstevel@tonic-gate 	dev_t pdev = 0;
5650Sstevel@tonic-gate 	int firstopen = 0;
5660Sstevel@tonic-gate 	fifolock_t *fn_lock;
5670Sstevel@tonic-gate 
5680Sstevel@tonic-gate 	fn_lock = fnp->fn_lock;
5690Sstevel@tonic-gate 	if (!lockheld)
5700Sstevel@tonic-gate 		mutex_enter(&fn_lock->flk_lock);
5710Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&fnp->fn_lock->flk_lock));
5720Sstevel@tonic-gate 
5730Sstevel@tonic-gate 	/*
5740Sstevel@tonic-gate 	 * FIFO is in the process of opening. Wait for it
5750Sstevel@tonic-gate 	 * to complete before starting another open on it
5760Sstevel@tonic-gate 	 * This prevents races associated with connld open
5770Sstevel@tonic-gate 	 */
5780Sstevel@tonic-gate 	while (fnp->fn_flag & FIFOOPEN) {
5790Sstevel@tonic-gate 		if (!cv_wait_sig(&fnp->fn_wait_cv, &fn_lock->flk_lock)) {
5800Sstevel@tonic-gate 			fifo_cleanup(oldvp, flag);
5810Sstevel@tonic-gate 			if (!lockheld)
5820Sstevel@tonic-gate 				mutex_exit(&fn_lock->flk_lock);
5830Sstevel@tonic-gate 			return (EINTR);
5840Sstevel@tonic-gate 		}
5850Sstevel@tonic-gate 	}
5860Sstevel@tonic-gate 
5870Sstevel@tonic-gate 	/*
5880Sstevel@tonic-gate 	 * The other end of the pipe is almost closed so
5890Sstevel@tonic-gate 	 * reject any other open on this end of the pipe
5900Sstevel@tonic-gate 	 * This only happens with a pipe mounted under namefs
5910Sstevel@tonic-gate 	 */
5920Sstevel@tonic-gate 	if ((fnp->fn_flag & (FIFOCLOSE|ISPIPE)) == (FIFOCLOSE|ISPIPE)) {
5930Sstevel@tonic-gate 		fifo_cleanup(oldvp, flag);
5940Sstevel@tonic-gate 		cv_broadcast(&fnp->fn_wait_cv);
5950Sstevel@tonic-gate 		if (!lockheld)
5960Sstevel@tonic-gate 			mutex_exit(&fn_lock->flk_lock);
5970Sstevel@tonic-gate 		return (ENXIO);
5980Sstevel@tonic-gate 	}
5990Sstevel@tonic-gate 
6000Sstevel@tonic-gate 	fnp->fn_flag |= FIFOOPEN;
6010Sstevel@tonic-gate 
6020Sstevel@tonic-gate 	/*
6030Sstevel@tonic-gate 	 * can't allow close to happen while we are
6040Sstevel@tonic-gate 	 * in the middle of stropen().
6050Sstevel@tonic-gate 	 * M_HANGUP and M_ERROR could leave the stream in a strange state
6060Sstevel@tonic-gate 	 */
6070Sstevel@tonic-gate 	while (fn_lock->flk_ocsync)
6080Sstevel@tonic-gate 		cv_wait(&fn_lock->flk_wait_cv, &fn_lock->flk_lock);
6090Sstevel@tonic-gate 
6100Sstevel@tonic-gate 	fn_lock->flk_ocsync = 1;
6110Sstevel@tonic-gate 
6120Sstevel@tonic-gate 	if (fnp->fn_flag & FIFOCONNLD) {
6130Sstevel@tonic-gate 		/*
6140Sstevel@tonic-gate 		 * This is a reopen, so we should release the fifo lock
6150Sstevel@tonic-gate 		 * just in case some strange module pushed on connld
6160Sstevel@tonic-gate 		 * has some odd side effect.
6170Sstevel@tonic-gate 		 * Note: this stropen is on the oldvp.  It will
6180Sstevel@tonic-gate 		 * have no impact on the connld vp returned and
6190Sstevel@tonic-gate 		 * strclose() will only be called when we release
6200Sstevel@tonic-gate 		 * flk_ocsync
6210Sstevel@tonic-gate 		 */
6220Sstevel@tonic-gate 		mutex_exit(&fn_lock->flk_lock);
6230Sstevel@tonic-gate 		if ((error = stropen(oldvp, &pdev, flag, crp)) != 0) {
6240Sstevel@tonic-gate 			mutex_enter(&fn_lock->flk_lock);
6250Sstevel@tonic-gate 			fifo_cleanup(oldvp, flag);
6260Sstevel@tonic-gate 			fn_lock->flk_ocsync = 0;
6270Sstevel@tonic-gate 			cv_broadcast(&fn_lock->flk_wait_cv);
6280Sstevel@tonic-gate 			goto out;
6290Sstevel@tonic-gate 		}
6300Sstevel@tonic-gate 		/*
6310Sstevel@tonic-gate 		 * streams open done, allow close on other end if
6320Sstevel@tonic-gate 		 * required.  Do this now.. it could
6330Sstevel@tonic-gate 		 * be a very long time before fifo_connld returns.
6340Sstevel@tonic-gate 		 */
6350Sstevel@tonic-gate 		mutex_enter(&fn_lock->flk_lock);
6360Sstevel@tonic-gate 		/*
6370Sstevel@tonic-gate 		 * we need to fake an open here so that if this
6380Sstevel@tonic-gate 		 * end of the pipe closes, we don't loose the
6390Sstevel@tonic-gate 		 * stream head (kind of like single threading
6400Sstevel@tonic-gate 		 * open and close for this end of the pipe)
6410Sstevel@tonic-gate 		 * We'll need to call fifo_close() to do clean
6420Sstevel@tonic-gate 		 * up in case this end of the pipe was closed
6430Sstevel@tonic-gate 		 * down while we were in fifo_connld()
6440Sstevel@tonic-gate 		 */
6450Sstevel@tonic-gate 		ASSERT(fnp->fn_open > 0);
6460Sstevel@tonic-gate 		fnp->fn_open++;
6470Sstevel@tonic-gate 		fn_lock->flk_ocsync = 0;
6480Sstevel@tonic-gate 		cv_broadcast(&fn_lock->flk_wait_cv);
6490Sstevel@tonic-gate 		mutex_exit(&fn_lock->flk_lock);
6500Sstevel@tonic-gate 		/*
6510Sstevel@tonic-gate 		 * Connld has been pushed onto the pipe
6520Sstevel@tonic-gate 		 * Create new pipe on behalf of connld
6530Sstevel@tonic-gate 		 */
6540Sstevel@tonic-gate 		if (error = fifo_connld(vpp, flag, crp)) {
6550Sstevel@tonic-gate 			(void) fifo_close(oldvp, flag, 1, 0, crp);
6560Sstevel@tonic-gate 			mutex_enter(&fn_lock->flk_lock);
6570Sstevel@tonic-gate 			goto out;
6580Sstevel@tonic-gate 		}
6590Sstevel@tonic-gate 		/*
6600Sstevel@tonic-gate 		 * undo fake open.  We need to call fifo_close
6610Sstevel@tonic-gate 		 * because some other thread could have done
6620Sstevel@tonic-gate 		 * a close and detach of the named pipe while
6630Sstevel@tonic-gate 		 * we were in fifo_connld(), so
6640Sstevel@tonic-gate 		 * we want to make sure the close completes (yuk)
6650Sstevel@tonic-gate 		 */
6660Sstevel@tonic-gate 		(void) fifo_close(oldvp, flag, 1, 0, crp);
6670Sstevel@tonic-gate 		/*
6680Sstevel@tonic-gate 		 * fifo_connld has changed the vp, so we
6690Sstevel@tonic-gate 		 * need to re-initialize locals
6700Sstevel@tonic-gate 		 */
6710Sstevel@tonic-gate 		fnp = VTOF(*vpp);
6720Sstevel@tonic-gate 		fn_lock = fnp->fn_lock;
6730Sstevel@tonic-gate 		mutex_enter(&fn_lock->flk_lock);
6740Sstevel@tonic-gate 	} else {
6750Sstevel@tonic-gate 		/*
6760Sstevel@tonic-gate 		 * release lock in case there are modules pushed that
6770Sstevel@tonic-gate 		 * could have some strange side effect
6780Sstevel@tonic-gate 		 */
6790Sstevel@tonic-gate 
6800Sstevel@tonic-gate 		mutex_exit(&fn_lock->flk_lock);
6810Sstevel@tonic-gate 
6820Sstevel@tonic-gate 		/*
6830Sstevel@tonic-gate 		 * If this is the first open of a fifo (dotwist
6840Sstevel@tonic-gate 		 * will be non-zero) we will need to twist the queues.
6850Sstevel@tonic-gate 		 */
6860Sstevel@tonic-gate 		if (oldvp->v_stream == NULL)
6870Sstevel@tonic-gate 			firstopen = 1;
6880Sstevel@tonic-gate 
6890Sstevel@tonic-gate 
6900Sstevel@tonic-gate 		/*
6910Sstevel@tonic-gate 		 * normal open of pipe/fifo
6920Sstevel@tonic-gate 		 */
6930Sstevel@tonic-gate 
6940Sstevel@tonic-gate 		if ((error = stropen(oldvp, &pdev, flag, crp)) != 0) {
6950Sstevel@tonic-gate 			mutex_enter(&fn_lock->flk_lock);
6960Sstevel@tonic-gate 			fifo_cleanup(oldvp, flag);
6970Sstevel@tonic-gate 			ASSERT(fnp->fn_open != 0 || oldvp->v_stream == NULL);
6980Sstevel@tonic-gate 			fn_lock->flk_ocsync = 0;
6990Sstevel@tonic-gate 			cv_broadcast(&fn_lock->flk_wait_cv);
7000Sstevel@tonic-gate 			goto out;
7010Sstevel@tonic-gate 		}
7020Sstevel@tonic-gate 		mutex_enter(&fn_lock->flk_lock);
7030Sstevel@tonic-gate 
7040Sstevel@tonic-gate 		/*
7050Sstevel@tonic-gate 		 * twist the ends of the fifo together
7060Sstevel@tonic-gate 		 */
7070Sstevel@tonic-gate 		if (dotwist && firstopen)
7080Sstevel@tonic-gate 			strmate(*vpp, *vpp);
7090Sstevel@tonic-gate 
7100Sstevel@tonic-gate 		/*
7110Sstevel@tonic-gate 		 * Show that this open has succeeded
7120Sstevel@tonic-gate 		 * and allow closes or other opens to proceed
7130Sstevel@tonic-gate 		 */
7140Sstevel@tonic-gate 		fnp->fn_open++;
7150Sstevel@tonic-gate 		fn_lock->flk_ocsync = 0;
7160Sstevel@tonic-gate 		cv_broadcast(&fn_lock->flk_wait_cv);
7170Sstevel@tonic-gate 	}
7180Sstevel@tonic-gate out:
7190Sstevel@tonic-gate 	fnp->fn_flag &= ~FIFOOPEN;
7200Sstevel@tonic-gate 	if (error == 0) {
7210Sstevel@tonic-gate 		fnp->fn_flag |= FIFOISOPEN;
7220Sstevel@tonic-gate 		/*
7230Sstevel@tonic-gate 		 * If this is a FIFO and has the close flag set
7240Sstevel@tonic-gate 		 * and there are now writers, clear the close flag
7250Sstevel@tonic-gate 		 * Note: close flag only gets set when last writer
7260Sstevel@tonic-gate 		 * on a FIFO goes away.
7270Sstevel@tonic-gate 		 */
7280Sstevel@tonic-gate 		if (((fnp->fn_flag & (ISPIPE|FIFOCLOSE)) == FIFOCLOSE) &&
7290Sstevel@tonic-gate 		    fnp->fn_wcnt > 0)
7300Sstevel@tonic-gate 			fnp->fn_flag &= ~FIFOCLOSE;
7310Sstevel@tonic-gate 	}
7320Sstevel@tonic-gate 	cv_broadcast(&fnp->fn_wait_cv);
7330Sstevel@tonic-gate 	if (!lockheld)
7340Sstevel@tonic-gate 		mutex_exit(&fn_lock->flk_lock);
7350Sstevel@tonic-gate 	return (error);
7360Sstevel@tonic-gate }
7370Sstevel@tonic-gate 
7380Sstevel@tonic-gate /*
7390Sstevel@tonic-gate  * Clean up the state of a FIFO and/or mounted pipe in the
7400Sstevel@tonic-gate  * event that a fifo_open() was interrupted while the
7410Sstevel@tonic-gate  * process was blocked.
7420Sstevel@tonic-gate  */
7430Sstevel@tonic-gate void
7440Sstevel@tonic-gate fifo_cleanup(vnode_t *vp, int flag)
7450Sstevel@tonic-gate {
7460Sstevel@tonic-gate 	fifonode_t *fnp = VTOF(vp);
7470Sstevel@tonic-gate 
7480Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&fnp->fn_lock->flk_lock));
7490Sstevel@tonic-gate 
7500Sstevel@tonic-gate 	cleanlocks(vp, curproc->p_pid, 0);
7510Sstevel@tonic-gate 	cleanshares(vp, curproc->p_pid);
7520Sstevel@tonic-gate 	if (flag & FREAD) {
7530Sstevel@tonic-gate 		fnp->fn_rcnt--;
7540Sstevel@tonic-gate 	}
7550Sstevel@tonic-gate 	if (flag & FWRITE) {
7560Sstevel@tonic-gate 		fnp->fn_wcnt--;
7570Sstevel@tonic-gate 	}
7580Sstevel@tonic-gate 	cv_broadcast(&fnp->fn_wait_cv);
7590Sstevel@tonic-gate }
7600Sstevel@tonic-gate 
7610Sstevel@tonic-gate 
7620Sstevel@tonic-gate /*
7630Sstevel@tonic-gate  * Insert a fifonode-vnode pair onto the fifoalloc hash list.
7640Sstevel@tonic-gate  */
7650Sstevel@tonic-gate static void
7660Sstevel@tonic-gate fifoinsert(fifonode_t *fnp)
7670Sstevel@tonic-gate {
7680Sstevel@tonic-gate 	int idx = FIFOHASH(fnp->fn_realvp);
7690Sstevel@tonic-gate 
7700Sstevel@tonic-gate 	/*
7710Sstevel@tonic-gate 	 * We don't need to hold fn_lock since we're holding ftable_lock and
7720Sstevel@tonic-gate 	 * this routine is only called right after we've allocated an fnode.
7730Sstevel@tonic-gate 	 * FIFO is inserted at head of NULL terminated doubly linked list.
7740Sstevel@tonic-gate 	 */
7750Sstevel@tonic-gate 
7760Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ftable_lock));
7770Sstevel@tonic-gate 	fnp->fn_backp = NULL;
7780Sstevel@tonic-gate 	fnp->fn_nextp = fifoalloc[idx];
7790Sstevel@tonic-gate 	fifoalloc[idx] = fnp;
7800Sstevel@tonic-gate 	if (fnp->fn_nextp)
7810Sstevel@tonic-gate 		fnp->fn_nextp->fn_backp = fnp;
7820Sstevel@tonic-gate }
7830Sstevel@tonic-gate 
7840Sstevel@tonic-gate /*
7850Sstevel@tonic-gate  * Find a fifonode-vnode pair on the fifoalloc hash list.
7860Sstevel@tonic-gate  * vp is a vnode to be shadowed. If it's on the hash list,
7870Sstevel@tonic-gate  * it already has a shadow, therefore return its corresponding
7880Sstevel@tonic-gate  * fifonode.
7890Sstevel@tonic-gate  */
7900Sstevel@tonic-gate static fifonode_t *
7910Sstevel@tonic-gate fifofind(vnode_t *vp)
7920Sstevel@tonic-gate {
7930Sstevel@tonic-gate 	fifonode_t *fnode;
7940Sstevel@tonic-gate 
7950Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ftable_lock));
7960Sstevel@tonic-gate 	for (fnode = fifoalloc[FIFOHASH(vp)]; fnode; fnode = fnode->fn_nextp) {
7970Sstevel@tonic-gate 		if (fnode->fn_realvp == vp) {
7980Sstevel@tonic-gate 			VN_HOLD(FTOV(fnode));
7990Sstevel@tonic-gate 			return (fnode);
8000Sstevel@tonic-gate 		}
8010Sstevel@tonic-gate 	}
8020Sstevel@tonic-gate 	return (NULL);
8030Sstevel@tonic-gate }
8040Sstevel@tonic-gate 
8050Sstevel@tonic-gate /*
8060Sstevel@tonic-gate  * Remove a fifonode-vnode pair from the fifoalloc hash list.
8070Sstevel@tonic-gate  * This routine is called from the fifo_inactive() routine when a
8080Sstevel@tonic-gate  * FIFO is being released.
8090Sstevel@tonic-gate  * If the link to be removed is the only link, set fifoalloc to NULL.
8100Sstevel@tonic-gate  */
8110Sstevel@tonic-gate void
8120Sstevel@tonic-gate fiforemove(fifonode_t *fnp)
8130Sstevel@tonic-gate {
8140Sstevel@tonic-gate 	int idx = FIFOHASH(fnp->fn_realvp);
8150Sstevel@tonic-gate 	fifonode_t *fnode;
8160Sstevel@tonic-gate 
8170Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ftable_lock));
8180Sstevel@tonic-gate 	fnode = fifoalloc[idx];
8190Sstevel@tonic-gate 	/*
8200Sstevel@tonic-gate 	 * fast path... only 1 FIFO in this list entry
8210Sstevel@tonic-gate 	 */
8220Sstevel@tonic-gate 	if (fnode != NULL && fnode == fnp &&
8230Sstevel@tonic-gate 		!fnode->fn_nextp && !fnode->fn_backp) {
8240Sstevel@tonic-gate 			fifoalloc[idx] = NULL;
8250Sstevel@tonic-gate 	} else {
8260Sstevel@tonic-gate 
8270Sstevel@tonic-gate 		for (;  fnode;  fnode = fnode->fn_nextp) {
8280Sstevel@tonic-gate 			if (fnode == fnp) {
8290Sstevel@tonic-gate 				/*
8300Sstevel@tonic-gate 				 * if we are first entry
8310Sstevel@tonic-gate 				 */
8320Sstevel@tonic-gate 				if (fnp == fifoalloc[idx])
8330Sstevel@tonic-gate 					fifoalloc[idx] = fnp->fn_nextp;
8340Sstevel@tonic-gate 				if (fnode->fn_nextp)
8350Sstevel@tonic-gate 					fnode->fn_nextp->fn_backp =
8360Sstevel@tonic-gate 						fnode->fn_backp;
8370Sstevel@tonic-gate 				if (fnode->fn_backp)
8380Sstevel@tonic-gate 					fnode->fn_backp->fn_nextp =
8390Sstevel@tonic-gate 						fnode->fn_nextp;
8400Sstevel@tonic-gate 				break;
8410Sstevel@tonic-gate 			}
8420Sstevel@tonic-gate 		}
8430Sstevel@tonic-gate 	}
8440Sstevel@tonic-gate }
8450Sstevel@tonic-gate 
8460Sstevel@tonic-gate /*
8470Sstevel@tonic-gate  * Flush all data from a fifo's message queue
8480Sstevel@tonic-gate  */
8490Sstevel@tonic-gate 
8500Sstevel@tonic-gate void
8510Sstevel@tonic-gate fifo_fastflush(fifonode_t *fnp)
8520Sstevel@tonic-gate {
8530Sstevel@tonic-gate 	mblk_t *bp;
8540Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&fnp->fn_lock->flk_lock));
8550Sstevel@tonic-gate 
8560Sstevel@tonic-gate 	if ((bp = fnp->fn_mp) != NULL) {
8570Sstevel@tonic-gate 		fnp->fn_mp = NULL;
8580Sstevel@tonic-gate 		fnp->fn_count = 0;
8590Sstevel@tonic-gate 		freemsg(bp);
8600Sstevel@tonic-gate 	}
8610Sstevel@tonic-gate 	fifo_wakewriter(fnp->fn_dest, fnp->fn_lock);
8620Sstevel@tonic-gate }
8630Sstevel@tonic-gate 
8640Sstevel@tonic-gate /*
8650Sstevel@tonic-gate  * Note:  This routine is single threaded
8660Sstevel@tonic-gate  *  Protected by FIFOOPEN flag (i.e. flk_lock is not held)
8670Sstevel@tonic-gate  *  Upon successful completion, the original fifo is unlocked
8680Sstevel@tonic-gate  *  and FIFOOPEN is cleared for the original vpp.
8690Sstevel@tonic-gate  *  The new fifo returned has FIFOOPEN set.
8700Sstevel@tonic-gate  */
8710Sstevel@tonic-gate static int
8720Sstevel@tonic-gate fifo_connld(struct vnode **vpp, int flag, cred_t *crp)
8730Sstevel@tonic-gate {
8740Sstevel@tonic-gate 	struct vnode *vp1;
8750Sstevel@tonic-gate 	struct vnode *vp2;
8760Sstevel@tonic-gate 	struct fifonode *oldfnp;
8770Sstevel@tonic-gate 	struct fifonode *fn_dest;
8780Sstevel@tonic-gate 	int error;
8790Sstevel@tonic-gate 	struct file *filep;
8800Sstevel@tonic-gate 	struct fifolock *fn_lock;
8810Sstevel@tonic-gate 	cred_t *c;
8820Sstevel@tonic-gate 
8830Sstevel@tonic-gate 	/*
8840Sstevel@tonic-gate 	 * Get two vnodes that will represent the pipe ends for the new pipe.
8850Sstevel@tonic-gate 	 */
8860Sstevel@tonic-gate 	makepipe(&vp1, &vp2);
8870Sstevel@tonic-gate 
8880Sstevel@tonic-gate 	/*
8890Sstevel@tonic-gate 	 * Allocate a file descriptor and file pointer for one of the pipe
8900Sstevel@tonic-gate 	 * ends. The file descriptor will be used to send that pipe end to
8910Sstevel@tonic-gate 	 * the process on the other end of this stream. Note that we get
8920Sstevel@tonic-gate 	 * the file structure only, there is no file list entry allocated.
8930Sstevel@tonic-gate 	 */
8940Sstevel@tonic-gate 	if (error = falloc(vp1, FWRITE|FREAD, &filep, NULL)) {
8950Sstevel@tonic-gate 		VN_RELE(vp1);
8960Sstevel@tonic-gate 		VN_RELE(vp2);
8970Sstevel@tonic-gate 		return (error);
8980Sstevel@tonic-gate 	}
8990Sstevel@tonic-gate 	mutex_exit(&filep->f_tlock);
9000Sstevel@tonic-gate 	oldfnp = VTOF(*vpp);
9010Sstevel@tonic-gate 	fn_lock = oldfnp->fn_lock;
9020Sstevel@tonic-gate 	fn_dest = oldfnp->fn_dest;
9030Sstevel@tonic-gate 
9040Sstevel@tonic-gate 	/*
9050Sstevel@tonic-gate 	 * Create two new stream heads and attach them to the two vnodes for
9060Sstevel@tonic-gate 	 * the new pipe.
9070Sstevel@tonic-gate 	 */
9080Sstevel@tonic-gate 	if ((error = fifo_stropen(&vp1, FREAD|FWRITE, filep->f_cred, 0, 0)) !=
9090Sstevel@tonic-gate 	    0 ||
9100Sstevel@tonic-gate 	    (error = fifo_stropen(&vp2, flag, filep->f_cred, 0, 0)) != 0) {
9110Sstevel@tonic-gate #if DEBUG
9120Sstevel@tonic-gate 		cmn_err(CE_NOTE, "fifo stropen failed error 0x%x",
9130Sstevel@tonic-gate 			error);
9140Sstevel@tonic-gate #endif
9150Sstevel@tonic-gate 		/*
9160Sstevel@tonic-gate 		 * this will call fifo_close and VN_RELE on vp1
9170Sstevel@tonic-gate 		 */
9180Sstevel@tonic-gate 		(void) closef(filep);
9190Sstevel@tonic-gate 		VN_RELE(vp2);
9200Sstevel@tonic-gate 		return (error);
9210Sstevel@tonic-gate 	}
9220Sstevel@tonic-gate 
9230Sstevel@tonic-gate 	/*
9240Sstevel@tonic-gate 	 * twist the ends of the pipe together
9250Sstevel@tonic-gate 	 */
9260Sstevel@tonic-gate 	strmate(vp1, vp2);
9270Sstevel@tonic-gate 
9280Sstevel@tonic-gate 	/*
9290Sstevel@tonic-gate 	 * Set our end to busy in open
9300Sstevel@tonic-gate 	 * Note: Don't need lock around this because we're the only
9310Sstevel@tonic-gate 	 * one who knows about it
9320Sstevel@tonic-gate 	 */
9330Sstevel@tonic-gate 	VTOF(vp2)->fn_flag |= FIFOOPEN;
9340Sstevel@tonic-gate 
9350Sstevel@tonic-gate 	mutex_enter(&fn_lock->flk_lock);
9360Sstevel@tonic-gate 
9370Sstevel@tonic-gate 	fn_dest->fn_flag |= FIFOSEND;
9380Sstevel@tonic-gate 	/*
9390Sstevel@tonic-gate 	 * check to make sure neither end of pipe has gone away
9400Sstevel@tonic-gate 	 */
9410Sstevel@tonic-gate 	if (!(fn_dest->fn_flag & FIFOISOPEN)) {
9420Sstevel@tonic-gate 		error = ENXIO;
9430Sstevel@tonic-gate 		fn_dest->fn_flag &= ~FIFOSEND;
9440Sstevel@tonic-gate 		mutex_exit(&fn_lock->flk_lock);
9450Sstevel@tonic-gate 		/*
9460Sstevel@tonic-gate 		 * this will call fifo_close and VN_RELE on vp1
9470Sstevel@tonic-gate 		 */
9480Sstevel@tonic-gate 		goto out;
9490Sstevel@tonic-gate 	}
9500Sstevel@tonic-gate 	mutex_exit(&fn_lock->flk_lock);
9510Sstevel@tonic-gate 
9520Sstevel@tonic-gate 	/*
9530Sstevel@tonic-gate 	 * Tag the sender's credential on the pipe descriptor.
9540Sstevel@tonic-gate 	 */
9550Sstevel@tonic-gate 	crhold(VTOF(vp1)->fn_pcredp = crp);
9560Sstevel@tonic-gate 	VTOF(vp1)->fn_cpid = curproc->p_pid;
9570Sstevel@tonic-gate 
9580Sstevel@tonic-gate 	/*
9590Sstevel@tonic-gate 	 * send the file descriptor to other end of pipe
9600Sstevel@tonic-gate 	 */
9610Sstevel@tonic-gate 	if (error = do_sendfp((*vpp)->v_stream, filep, crp)) {
9620Sstevel@tonic-gate 		mutex_enter(&fn_lock->flk_lock);
9630Sstevel@tonic-gate 		fn_dest->fn_flag &= ~FIFOSEND;
9640Sstevel@tonic-gate 		mutex_exit(&fn_lock->flk_lock);
9650Sstevel@tonic-gate 		/*
9660Sstevel@tonic-gate 		 * this will call fifo_close and VN_RELE on vp1
9670Sstevel@tonic-gate 		 */
9680Sstevel@tonic-gate 		goto out;
9690Sstevel@tonic-gate 	}
9700Sstevel@tonic-gate 
9710Sstevel@tonic-gate 	mutex_enter(&fn_lock->flk_lock);
9720Sstevel@tonic-gate 	/*
9730Sstevel@tonic-gate 	 * Wait for other end to receive file descriptor
9740Sstevel@tonic-gate 	 * FIFOCLOSE indicates that one or both sides of the pipe
9750Sstevel@tonic-gate 	 * have gone away.
9760Sstevel@tonic-gate 	 */
9770Sstevel@tonic-gate 	while ((fn_dest->fn_flag & (FIFOCLOSE | FIFOSEND)) == FIFOSEND) {
9780Sstevel@tonic-gate 		if (!cv_wait_sig(&oldfnp->fn_wait_cv, &fn_lock->flk_lock)) {
9790Sstevel@tonic-gate 			error = EINTR;
9800Sstevel@tonic-gate 			fn_dest->fn_flag &= ~FIFOSEND;
9810Sstevel@tonic-gate 			mutex_exit(&fn_lock->flk_lock);
9820Sstevel@tonic-gate 			goto out;
9830Sstevel@tonic-gate 		}
9840Sstevel@tonic-gate 	}
9850Sstevel@tonic-gate 	/*
9860Sstevel@tonic-gate 	 * If either end of pipe has gone away and the other end did not
9870Sstevel@tonic-gate 	 * receive pipe, reject the connld open
9880Sstevel@tonic-gate 	 */
9890Sstevel@tonic-gate 	if ((fn_dest->fn_flag & FIFOSEND)) {
9900Sstevel@tonic-gate 		error = ENXIO;
9910Sstevel@tonic-gate 		fn_dest->fn_flag &= ~FIFOSEND;
9920Sstevel@tonic-gate 		mutex_exit(&fn_lock->flk_lock);
9930Sstevel@tonic-gate 		goto out;
9940Sstevel@tonic-gate 	}
9950Sstevel@tonic-gate 
9960Sstevel@tonic-gate 	oldfnp->fn_flag &= ~FIFOOPEN;
9970Sstevel@tonic-gate 	cv_broadcast(&oldfnp->fn_wait_cv);
9980Sstevel@tonic-gate 	mutex_exit(&fn_lock->flk_lock);
9990Sstevel@tonic-gate 
10000Sstevel@tonic-gate 	VN_RELE(*vpp);
10010Sstevel@tonic-gate 	*vpp = vp2;
10020Sstevel@tonic-gate 	(void) closef(filep);
10030Sstevel@tonic-gate 	return (0);
10040Sstevel@tonic-gate out:
10050Sstevel@tonic-gate 	c = filep->f_cred;
10060Sstevel@tonic-gate 	crhold(c);
10070Sstevel@tonic-gate 	(void) closef(filep);
10080Sstevel@tonic-gate 	VTOF(vp2)->fn_flag &= ~FIFOOPEN;
10090Sstevel@tonic-gate 	(void) fifo_close(vp2, flag, 1, (offset_t)0, c);
10100Sstevel@tonic-gate 	crfree(c);
10110Sstevel@tonic-gate 	VN_RELE(vp2);
10120Sstevel@tonic-gate 	return (error);
10130Sstevel@tonic-gate }
10140Sstevel@tonic-gate 
10150Sstevel@tonic-gate /*
10160Sstevel@tonic-gate  * Disable fastpath mode.
10170Sstevel@tonic-gate  */
10180Sstevel@tonic-gate void
10190Sstevel@tonic-gate fifo_fastoff(fifonode_t *fnp)
10200Sstevel@tonic-gate {
10210Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&fnp->fn_lock->flk_lock));
10220Sstevel@tonic-gate 	ASSERT(FTOV(fnp)->v_stream);
10230Sstevel@tonic-gate 
10240Sstevel@tonic-gate 
10250Sstevel@tonic-gate 	if (!(fnp->fn_flag & FIFOFAST))
10260Sstevel@tonic-gate 		return;
10270Sstevel@tonic-gate #if FIFODEBUG
10280Sstevel@tonic-gate 	if (Fifo_verbose)
10290Sstevel@tonic-gate 		cmn_err(CE_NOTE, "Fifo reverting to streams mode\n");
10300Sstevel@tonic-gate #endif
10310Sstevel@tonic-gate 
10320Sstevel@tonic-gate 	fifo_fastturnoff(fnp);
10330Sstevel@tonic-gate 	if (fnp->fn_flag & ISPIPE) {
10340Sstevel@tonic-gate 		fifo_fastturnoff(fnp->fn_dest);
10350Sstevel@tonic-gate 	}
10360Sstevel@tonic-gate }
10370Sstevel@tonic-gate 
10380Sstevel@tonic-gate 
10390Sstevel@tonic-gate /*
10400Sstevel@tonic-gate  * flk_lock must be held while calling fifo_fastturnoff() to
10410Sstevel@tonic-gate  * preserve data ordering (no reads or writes allowed)
10420Sstevel@tonic-gate  */
10430Sstevel@tonic-gate 
10440Sstevel@tonic-gate static void
10450Sstevel@tonic-gate fifo_fastturnoff(fifonode_t *fnp)
10460Sstevel@tonic-gate {
10470Sstevel@tonic-gate 	fifonode_t *fn_dest = fnp->fn_dest;
10480Sstevel@tonic-gate 	mblk_t	*fn_mp;
10490Sstevel@tonic-gate 	int	fn_flag;
10500Sstevel@tonic-gate 
10510Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&fnp->fn_lock->flk_lock));
10520Sstevel@tonic-gate 	/*
10530Sstevel@tonic-gate 	 * Note: This end can't be closed if there
10540Sstevel@tonic-gate 	 * is stuff in fn_mp
10550Sstevel@tonic-gate 	 */
10560Sstevel@tonic-gate 	if ((fn_mp = fnp->fn_mp) != NULL) {
10570Sstevel@tonic-gate 		ASSERT(fnp->fn_flag & FIFOISOPEN);
10580Sstevel@tonic-gate 		ASSERT(FTOV(fnp)->v_stream != NULL);
10590Sstevel@tonic-gate 		ASSERT(FTOV(fnp)->v_stream->sd_wrq != NULL);
10600Sstevel@tonic-gate 		ASSERT(RD(FTOV(fnp)->v_stream->sd_wrq) != NULL);
10610Sstevel@tonic-gate 		ASSERT(strvp2wq(FTOV(fnp)) != NULL);
10620Sstevel@tonic-gate 		fnp->fn_mp = NULL;
10630Sstevel@tonic-gate 		fnp->fn_count = 0;
10640Sstevel@tonic-gate 		/*
10650Sstevel@tonic-gate 		 * Don't need to drop flk_lock across the put()
10660Sstevel@tonic-gate 		 * since we're just moving the message from the fifo
10670Sstevel@tonic-gate 		 * node to the STREAM head...
10680Sstevel@tonic-gate 		 */
10690Sstevel@tonic-gate 		put(RD(strvp2wq(FTOV(fnp))), fn_mp);
10700Sstevel@tonic-gate 	}
10710Sstevel@tonic-gate 
10720Sstevel@tonic-gate 	/*
10730Sstevel@tonic-gate 	 * Need to re-issue any pending poll requests
10740Sstevel@tonic-gate 	 * so that the STREAMS framework sees them
10750Sstevel@tonic-gate 	 * Writers would be waiting on fnp and readers on fn_dest
10760Sstevel@tonic-gate 	 */
10770Sstevel@tonic-gate 	if ((fnp->fn_flag & (FIFOISOPEN | FIFOPOLLW)) ==
10780Sstevel@tonic-gate 	    (FIFOISOPEN | FIFOPOLLW)) {
10790Sstevel@tonic-gate 		strpollwakeup(FTOV(fnp), POLLWRNORM);
10800Sstevel@tonic-gate 	}
10810Sstevel@tonic-gate 	fn_flag = fn_dest->fn_flag;
10820Sstevel@tonic-gate 	if ((fn_flag & FIFOISOPEN) == FIFOISOPEN) {
10830Sstevel@tonic-gate 		if ((fn_flag & (FIFOPOLLR | FIFOPOLLRBAND))) {
10840Sstevel@tonic-gate 			strpollwakeup(FTOV(fn_dest), POLLIN|POLLRDNORM);
10850Sstevel@tonic-gate 		}
10860Sstevel@tonic-gate 	}
10870Sstevel@tonic-gate 	/*
10880Sstevel@tonic-gate 	 * wake up any sleeping processes so they can notice we went
10890Sstevel@tonic-gate 	 * to streams mode
10900Sstevel@tonic-gate 	 */
10910Sstevel@tonic-gate 	fnp->fn_flag &= ~(FIFOFAST|FIFOWANTW|FIFOWANTR);
10920Sstevel@tonic-gate 	cv_broadcast(&fnp->fn_wait_cv);
10930Sstevel@tonic-gate }
10940Sstevel@tonic-gate 
10950Sstevel@tonic-gate /*
10960Sstevel@tonic-gate  * Alternative version of fifo_fastoff()
10970Sstevel@tonic-gate  * optimized for putmsg/getmsg.
10980Sstevel@tonic-gate  */
10990Sstevel@tonic-gate void
11000Sstevel@tonic-gate fifo_vfastoff(vnode_t *vp)
11010Sstevel@tonic-gate {
11020Sstevel@tonic-gate 	fifonode_t	*fnp = VTOF(vp);
11030Sstevel@tonic-gate 
11040Sstevel@tonic-gate 	mutex_enter(&fnp->fn_lock->flk_lock);
11050Sstevel@tonic-gate 	if (!(fnp->fn_flag & FIFOFAST)) {
11060Sstevel@tonic-gate 		mutex_exit(&fnp->fn_lock->flk_lock);
11070Sstevel@tonic-gate 		return;
11080Sstevel@tonic-gate 	}
11090Sstevel@tonic-gate 	fifo_fastoff(fnp);
11100Sstevel@tonic-gate 	mutex_exit(&fnp->fn_lock->flk_lock);
11110Sstevel@tonic-gate }
11120Sstevel@tonic-gate 
11130Sstevel@tonic-gate /*
11140Sstevel@tonic-gate  * Wake any sleeping writers, poll and send signals if necessary
11150Sstevel@tonic-gate  * This module is only called when we drop below the hi water mark
11160Sstevel@tonic-gate  * FIFOWANTW indicates that a process is sleeping in fifo_write()
11170Sstevel@tonic-gate  * FIFOHIWATW indicates that we have either attempted a poll or
11180Sstevel@tonic-gate  * non-blocking write and were over the high water mark
11190Sstevel@tonic-gate  * This routine assumes a low water mark of 0.
11200Sstevel@tonic-gate  */
11210Sstevel@tonic-gate 
11220Sstevel@tonic-gate void
11230Sstevel@tonic-gate fifo_wakewriter(fifonode_t *fn_dest, fifolock_t *fn_lock)
11240Sstevel@tonic-gate {
11250Sstevel@tonic-gate 	int fn_dflag = fn_dest->fn_flag;
11260Sstevel@tonic-gate 
11270Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&fn_lock->flk_lock));
11280Sstevel@tonic-gate 	ASSERT(fn_dest->fn_dest->fn_count < Fifohiwat);
11290Sstevel@tonic-gate 	if ((fn_dflag & FIFOWANTW)) {
11300Sstevel@tonic-gate 		cv_broadcast(&fn_dest->fn_wait_cv);
11310Sstevel@tonic-gate 	}
11320Sstevel@tonic-gate 	if ((fn_dflag & (FIFOHIWATW | FIFOISOPEN)) ==
11330Sstevel@tonic-gate 	    (FIFOHIWATW | FIFOISOPEN)) {
11340Sstevel@tonic-gate 		if (fn_dflag & FIFOPOLLW)
11350Sstevel@tonic-gate 			strpollwakeup(FTOV(fn_dest), POLLWRNORM);
11360Sstevel@tonic-gate 		if (fn_dflag & FIFOSETSIG)
11370Sstevel@tonic-gate 			str_sendsig(FTOV(fn_dest), S_WRNORM, 0, 0);
11380Sstevel@tonic-gate 	}
11390Sstevel@tonic-gate 	/*
11400Sstevel@tonic-gate 	 * FIFOPOLLW can't be set without setting FIFOHIWAT
11410Sstevel@tonic-gate 	 * This allows us to clear both here.
11420Sstevel@tonic-gate 	 */
11430Sstevel@tonic-gate 	fn_dest->fn_flag = fn_dflag & ~(FIFOWANTW | FIFOHIWATW | FIFOPOLLW);
11440Sstevel@tonic-gate }
11450Sstevel@tonic-gate 
11460Sstevel@tonic-gate /*
11470Sstevel@tonic-gate  * wake up any sleeping readers, poll or send signal if needed
11480Sstevel@tonic-gate  * FIFOWANTR indicates that a process is waiting in fifo_read() for data
11490Sstevel@tonic-gate  * FIFOSETSIG indicates that SIGPOLL should be sent to process
11500Sstevel@tonic-gate  * FIFOPOLLR indicates that a poll request for reading on the fifo was made
11510Sstevel@tonic-gate  */
11520Sstevel@tonic-gate 
11530Sstevel@tonic-gate void
11540Sstevel@tonic-gate fifo_wakereader(fifonode_t *fn_dest, fifolock_t *fn_lock)
11550Sstevel@tonic-gate {
11560Sstevel@tonic-gate 	int fn_dflag = fn_dest->fn_flag;
11570Sstevel@tonic-gate 
11580Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&fn_lock->flk_lock));
11590Sstevel@tonic-gate 	if (fn_dflag & FIFOWANTR) {
11600Sstevel@tonic-gate 		cv_broadcast(&fn_dest->fn_wait_cv);
11610Sstevel@tonic-gate 	}
11620Sstevel@tonic-gate 	if (fn_dflag & FIFOISOPEN) {
11630Sstevel@tonic-gate 		if (fn_dflag & FIFOPOLLR)
11640Sstevel@tonic-gate 			strpollwakeup(FTOV(fn_dest), POLLIN | POLLRDNORM);
11650Sstevel@tonic-gate 		if (fn_dflag & FIFOSETSIG)
11660Sstevel@tonic-gate 			str_sendsig(FTOV(fn_dest), S_INPUT | S_RDNORM, 0, 0);
11670Sstevel@tonic-gate 	}
11680Sstevel@tonic-gate 	fn_dest->fn_flag = fn_dflag & ~(FIFOWANTR | FIFOPOLLR);
11690Sstevel@tonic-gate }
1170