xref: /onnv-gate/usr/src/uts/common/fs/fifofs/fifosubr.c (revision 12633:9f2cda0ed938)
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
51799Sgfaden  * Common Development and Distribution License (the "License").
61799Sgfaden  * 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*12633Sjohn.levon@sun.com  * Copyright (c) 1988, 2010, Oracle and/or its affiliates. All rights reserved.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*
280Sstevel@tonic-gate  * The routines defined in this file are supporting routines for FIFOFS
295331Samw  * file system type.
300Sstevel@tonic-gate  */
310Sstevel@tonic-gate #include <sys/types.h>
320Sstevel@tonic-gate #include <sys/param.h>
330Sstevel@tonic-gate #include <sys/systm.h>
340Sstevel@tonic-gate #include <sys/debug.h>
350Sstevel@tonic-gate #include <sys/errno.h>
360Sstevel@tonic-gate #include <sys/time.h>
370Sstevel@tonic-gate #include <sys/kmem.h>
380Sstevel@tonic-gate #include <sys/inline.h>
390Sstevel@tonic-gate #include <sys/file.h>
400Sstevel@tonic-gate #include <sys/proc.h>
410Sstevel@tonic-gate #include <sys/stat.h>
420Sstevel@tonic-gate #include <sys/sysmacros.h>
430Sstevel@tonic-gate #include <sys/var.h>
440Sstevel@tonic-gate #include <sys/vfs.h>
453898Srsb #include <sys/vfs_opreg.h>
460Sstevel@tonic-gate #include <sys/vnode.h>
470Sstevel@tonic-gate #include <sys/mode.h>
480Sstevel@tonic-gate #include <sys/signal.h>
490Sstevel@tonic-gate #include <sys/user.h>
500Sstevel@tonic-gate #include <sys/uio.h>
510Sstevel@tonic-gate #include <sys/flock.h>
520Sstevel@tonic-gate #include <sys/stream.h>
530Sstevel@tonic-gate #include <sys/fs/fifonode.h>
540Sstevel@tonic-gate #include <sys/strsubr.h>
550Sstevel@tonic-gate #include <sys/stropts.h>
560Sstevel@tonic-gate #include <sys/cmn_err.h>
570Sstevel@tonic-gate #include <fs/fs_subr.h>
580Sstevel@tonic-gate #include <sys/ddi.h>
590Sstevel@tonic-gate 
600Sstevel@tonic-gate 
610Sstevel@tonic-gate #if FIFODEBUG
620Sstevel@tonic-gate int Fifo_fastmode = 1;		/* pipes/fifos will be opened in fast mode */
630Sstevel@tonic-gate int Fifo_verbose = 0;		/* msg when switching out of fast mode */
640Sstevel@tonic-gate int Fifohiwat = FIFOHIWAT;	/* Modifiable FIFO high water mark */
650Sstevel@tonic-gate #endif
660Sstevel@tonic-gate 
670Sstevel@tonic-gate /*
680Sstevel@tonic-gate  * This is the loadable module wrapper.
690Sstevel@tonic-gate  */
700Sstevel@tonic-gate #include <sys/modctl.h>
710Sstevel@tonic-gate 
720Sstevel@tonic-gate extern struct qinit fifo_strdata;
730Sstevel@tonic-gate 
740Sstevel@tonic-gate struct vfsops *fifo_vfsops;
750Sstevel@tonic-gate 
760Sstevel@tonic-gate static vfsdef_t vfw = {
770Sstevel@tonic-gate 	VFSDEF_VERSION,
780Sstevel@tonic-gate 	"fifofs",
790Sstevel@tonic-gate 	fifoinit,
80*12633Sjohn.levon@sun.com 	VSW_ZMOUNT,
810Sstevel@tonic-gate 	NULL
820Sstevel@tonic-gate };
830Sstevel@tonic-gate 
840Sstevel@tonic-gate /*
850Sstevel@tonic-gate  * Module linkage information for the kernel.
860Sstevel@tonic-gate  */
870Sstevel@tonic-gate extern struct mod_ops mod_fsops;
880Sstevel@tonic-gate 
890Sstevel@tonic-gate static struct modlfs modlfs = {
900Sstevel@tonic-gate 	&mod_fsops, "filesystem for fifo", &vfw
910Sstevel@tonic-gate };
920Sstevel@tonic-gate 
930Sstevel@tonic-gate static struct modlinkage modlinkage = {
940Sstevel@tonic-gate 	MODREV_1, (void *)&modlfs, NULL
950Sstevel@tonic-gate };
960Sstevel@tonic-gate 
970Sstevel@tonic-gate int
_init()980Sstevel@tonic-gate _init()
990Sstevel@tonic-gate {
1000Sstevel@tonic-gate 	return (mod_install(&modlinkage));
1010Sstevel@tonic-gate }
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate int
_info(struct modinfo * modinfop)1040Sstevel@tonic-gate _info(struct modinfo *modinfop)
1050Sstevel@tonic-gate {
1060Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
1070Sstevel@tonic-gate }
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate /*
1100Sstevel@tonic-gate  * Define data structures within this file.
1110Sstevel@tonic-gate  * XXX should the hash size be configurable ?
1120Sstevel@tonic-gate  */
1130Sstevel@tonic-gate #define	FIFOSHFT	5
1140Sstevel@tonic-gate #define	FIFO_HASHSZ	63
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate #if ((FIFO_HASHSZ & (FIFO_HASHSZ - 1)) == 0)
1170Sstevel@tonic-gate #define	FIFOHASH(vp) (((uintptr_t)(vp) >> FIFOSHFT) & (FIFO_HASHSZ - 1))
1180Sstevel@tonic-gate #else
1190Sstevel@tonic-gate #define	FIFOHASH(vp) (((uintptr_t)(vp) >> FIFOSHFT) % FIFO_HASHSZ)
1200Sstevel@tonic-gate #endif
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate fifonode_t	*fifoalloc[FIFO_HASHSZ];
1230Sstevel@tonic-gate dev_t		fifodev;
1240Sstevel@tonic-gate struct vfs	*fifovfsp;
1250Sstevel@tonic-gate int		fifofstype;
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate kmutex_t ftable_lock;
1280Sstevel@tonic-gate static kmutex_t fino_lock;
1290Sstevel@tonic-gate struct kmem_cache *fnode_cache;
1300Sstevel@tonic-gate struct kmem_cache *pipe_cache;
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate static void fifoinsert(fifonode_t *);
1330Sstevel@tonic-gate static fifonode_t *fifofind(vnode_t *);
1340Sstevel@tonic-gate static int fifo_connld(struct vnode **, int, cred_t *);
1350Sstevel@tonic-gate static void fifo_fastturnoff(fifonode_t *);
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate static void fifo_reinit_vp(vnode_t *);
1380Sstevel@tonic-gate 
1396712Stomee static void fnode_destructor(void *, void *);
1406712Stomee 
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  *
1665331Samw  * The fnode constructor is designed to handle any fifodata structure,
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 static int
fnode_constructor(void * buf,void * cdrarg,int kmflags)1710Sstevel@tonic-gate fnode_constructor(void *buf, void *cdrarg, int kmflags)
1720Sstevel@tonic-gate {
1730Sstevel@tonic-gate 	fifodata_t *fdp = buf;
1740Sstevel@tonic-gate 	fifolock_t *flp = &fdp->fifo_lock;
1750Sstevel@tonic-gate 	fifonode_t *fnp = &fdp->fifo_fnode[0];
1760Sstevel@tonic-gate 	size_t size = (uintptr_t)cdrarg;
1770Sstevel@tonic-gate 
1780Sstevel@tonic-gate 	mutex_init(&flp->flk_lock, NULL, MUTEX_DEFAULT, NULL);
1790Sstevel@tonic-gate 	cv_init(&flp->flk_wait_cv, NULL, CV_DEFAULT, NULL);
1800Sstevel@tonic-gate 	flp->flk_ocsync = 0;
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate 	while ((char *)fnp < (char *)buf + size) {
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate 		vnode_t *vp;
1850Sstevel@tonic-gate 
1866712Stomee 		vp = vn_alloc(kmflags);
1876712Stomee 		if (vp == NULL) {
1886712Stomee 			fnp->fn_vnode = NULL; /* mark for destructor */
1896712Stomee 			fnode_destructor(buf, cdrarg);
1906712Stomee 			return (-1);
1916712Stomee 		}
1920Sstevel@tonic-gate 		fnp->fn_vnode = vp;
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate 		fnp->fn_lock = flp;
1950Sstevel@tonic-gate 		fnp->fn_open = 0;
1960Sstevel@tonic-gate 		fnp->fn_dest = fnp;
1970Sstevel@tonic-gate 		fnp->fn_mp = NULL;
1980Sstevel@tonic-gate 		fnp->fn_count = 0;
1990Sstevel@tonic-gate 		fnp->fn_rsynccnt = 0;
2000Sstevel@tonic-gate 		fnp->fn_wsynccnt = 0;
2010Sstevel@tonic-gate 		fnp->fn_wwaitcnt = 0;
2020Sstevel@tonic-gate 		fnp->fn_insync = 0;
2030Sstevel@tonic-gate 		fnp->fn_pcredp = NULL;
2040Sstevel@tonic-gate 		fnp->fn_cpid = -1;
2050Sstevel@tonic-gate 		/*
2060Sstevel@tonic-gate 		 * 32-bit stat(2) may fail if fn_ino isn't initialized
2070Sstevel@tonic-gate 		 */
2080Sstevel@tonic-gate 		fnp->fn_ino = 0;
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate 		cv_init(&fnp->fn_wait_cv, NULL, CV_DEFAULT, NULL);
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate 		vn_setops(vp, fifo_vnodeops);
2130Sstevel@tonic-gate 		vp->v_stream = NULL;
2140Sstevel@tonic-gate 		vp->v_type = VFIFO;
2150Sstevel@tonic-gate 		vp->v_data = (caddr_t)fnp;
2160Sstevel@tonic-gate 		vp->v_flag = VNOMAP | VNOSWAP;
2170Sstevel@tonic-gate 		vn_exists(vp);
2180Sstevel@tonic-gate 		fnp++;
2190Sstevel@tonic-gate 	}
2200Sstevel@tonic-gate 	return (0);
2210Sstevel@tonic-gate }
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate static void
fnode_destructor(void * buf,void * cdrarg)2240Sstevel@tonic-gate fnode_destructor(void *buf, void *cdrarg)
2250Sstevel@tonic-gate {
2260Sstevel@tonic-gate 	fifodata_t *fdp = buf;
2270Sstevel@tonic-gate 	fifolock_t *flp = &fdp->fifo_lock;
2280Sstevel@tonic-gate 	fifonode_t *fnp = &fdp->fifo_fnode[0];
2290Sstevel@tonic-gate 	size_t size = (uintptr_t)cdrarg;
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate 	mutex_destroy(&flp->flk_lock);
2320Sstevel@tonic-gate 	cv_destroy(&flp->flk_wait_cv);
2330Sstevel@tonic-gate 	ASSERT(flp->flk_ocsync == 0);
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate 	while ((char *)fnp < (char *)buf + size) {
2360Sstevel@tonic-gate 
2370Sstevel@tonic-gate 		vnode_t *vp = FTOV(fnp);
2380Sstevel@tonic-gate 
2396712Stomee 		if (vp == NULL) {
2406712Stomee 			return; /* constructor failed here */
2416712Stomee 		}
2426712Stomee 
2430Sstevel@tonic-gate 		ASSERT(fnp->fn_mp == NULL);
2440Sstevel@tonic-gate 		ASSERT(fnp->fn_count == 0);
2450Sstevel@tonic-gate 		ASSERT(fnp->fn_lock == flp);
2460Sstevel@tonic-gate 		ASSERT(fnp->fn_open == 0);
2470Sstevel@tonic-gate 		ASSERT(fnp->fn_insync == 0);
2480Sstevel@tonic-gate 		ASSERT(fnp->fn_rsynccnt == 0 && fnp->fn_wsynccnt == 0);
2490Sstevel@tonic-gate 		ASSERT(fnp->fn_wwaitcnt == 0);
2500Sstevel@tonic-gate 		ASSERT(fnp->fn_pcredp == NULL);
2510Sstevel@tonic-gate 		ASSERT(vn_matchops(vp, fifo_vnodeops));
2520Sstevel@tonic-gate 		ASSERT(vp->v_stream == NULL);
2530Sstevel@tonic-gate 		ASSERT(vp->v_type == VFIFO);
2540Sstevel@tonic-gate 		ASSERT(vp->v_data == (caddr_t)fnp);
2550Sstevel@tonic-gate 		ASSERT((vp->v_flag & (VNOMAP|VNOSWAP)) == (VNOMAP|VNOSWAP));
2560Sstevel@tonic-gate 
2570Sstevel@tonic-gate 		cv_destroy(&fnp->fn_wait_cv);
2580Sstevel@tonic-gate 		vn_invalid(vp);
2590Sstevel@tonic-gate 		vn_free(vp);
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate 		fnp++;
2620Sstevel@tonic-gate 	}
2630Sstevel@tonic-gate }
2640Sstevel@tonic-gate 
2650Sstevel@tonic-gate static int
pipe_constructor(void * buf,void * cdrarg,int kmflags)2660Sstevel@tonic-gate pipe_constructor(void *buf, void *cdrarg, int kmflags)
2670Sstevel@tonic-gate {
2680Sstevel@tonic-gate 	fifodata_t *fdp = buf;
2690Sstevel@tonic-gate 	fifonode_t *fnp1 = &fdp->fifo_fnode[0];
2700Sstevel@tonic-gate 	fifonode_t *fnp2 = &fdp->fifo_fnode[1];
2710Sstevel@tonic-gate 	vnode_t *vp1;
2720Sstevel@tonic-gate 	vnode_t *vp2;
2730Sstevel@tonic-gate 
2740Sstevel@tonic-gate 	(void) fnode_constructor(buf, cdrarg, kmflags);
2750Sstevel@tonic-gate 
2760Sstevel@tonic-gate 	vp1 = FTOV(fnp1);
2770Sstevel@tonic-gate 	vp2 = FTOV(fnp2);
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate 	vp1->v_vfsp	= vp2->v_vfsp		= fifovfsp;
2800Sstevel@tonic-gate 	vp1->v_rdev	= vp2->v_rdev		= fifodev;
2810Sstevel@tonic-gate 	fnp1->fn_realvp	= fnp2->fn_realvp	= NULL;
2820Sstevel@tonic-gate 	fnp1->fn_dest	= fnp2;
2830Sstevel@tonic-gate 	fnp2->fn_dest	= fnp1;
2840Sstevel@tonic-gate 
2850Sstevel@tonic-gate 	return (0);
2860Sstevel@tonic-gate }
2870Sstevel@tonic-gate 
2880Sstevel@tonic-gate static void
pipe_destructor(void * buf,void * cdrarg)2890Sstevel@tonic-gate pipe_destructor(void *buf, void *cdrarg)
2900Sstevel@tonic-gate {
2910Sstevel@tonic-gate #ifdef DEBUG
2920Sstevel@tonic-gate 	fifodata_t *fdp = buf;
2930Sstevel@tonic-gate 	fifonode_t *fnp1 = &fdp->fifo_fnode[0];
2940Sstevel@tonic-gate 	fifonode_t *fnp2 = &fdp->fifo_fnode[1];
2950Sstevel@tonic-gate 	vnode_t *vp1 = FTOV(fnp1);
2960Sstevel@tonic-gate 	vnode_t *vp2 = FTOV(fnp2);
2970Sstevel@tonic-gate 
2980Sstevel@tonic-gate 	ASSERT(vp1->v_vfsp == fifovfsp);
2990Sstevel@tonic-gate 	ASSERT(vp2->v_vfsp == fifovfsp);
3000Sstevel@tonic-gate 	ASSERT(vp1->v_rdev == fifodev);
3010Sstevel@tonic-gate 	ASSERT(vp2->v_rdev == fifodev);
3020Sstevel@tonic-gate #endif
3030Sstevel@tonic-gate 	fnode_destructor(buf, cdrarg);
3040Sstevel@tonic-gate }
3050Sstevel@tonic-gate 
3060Sstevel@tonic-gate /*
3070Sstevel@tonic-gate  * Reinitialize a FIFO vnode (uses normal vnode reinit, but ensures that
3080Sstevel@tonic-gate  * vnode type and flags are reset).
3090Sstevel@tonic-gate  */
3100Sstevel@tonic-gate 
fifo_reinit_vp(vnode_t * vp)3110Sstevel@tonic-gate static void fifo_reinit_vp(vnode_t *vp)
3120Sstevel@tonic-gate {
3130Sstevel@tonic-gate 	vn_reinit(vp);
3140Sstevel@tonic-gate 	vp->v_type = VFIFO;
3152712Snn35248 	vp->v_flag &= VROOT;
3162712Snn35248 	vp->v_flag |= VNOMAP | VNOSWAP;
3170Sstevel@tonic-gate }
3180Sstevel@tonic-gate 
3190Sstevel@tonic-gate /*
3200Sstevel@tonic-gate  * Save file system type/index, initialize vfs operations vector, get
3210Sstevel@tonic-gate  * unique device number for FIFOFS and initialize the FIFOFS hash.
3220Sstevel@tonic-gate  * Create and initialize a "generic" vfs pointer that will be placed
3230Sstevel@tonic-gate  * in the v_vfsp field of each pipe's vnode.
3240Sstevel@tonic-gate  */
3250Sstevel@tonic-gate int
fifoinit(int fstype,char * name)3260Sstevel@tonic-gate fifoinit(int fstype, char *name)
3270Sstevel@tonic-gate {
3280Sstevel@tonic-gate 	static const fs_operation_def_t fifo_vfsops_template[] = {
3290Sstevel@tonic-gate 		NULL, NULL
3300Sstevel@tonic-gate 	};
3310Sstevel@tonic-gate 	int error;
3320Sstevel@tonic-gate 	major_t dev;
3330Sstevel@tonic-gate 
3340Sstevel@tonic-gate 	fifofstype = fstype;
3350Sstevel@tonic-gate 	error = vfs_setfsops(fstype, fifo_vfsops_template, &fifo_vfsops);
3360Sstevel@tonic-gate 	if (error != 0) {
3370Sstevel@tonic-gate 		cmn_err(CE_WARN, "fifoinit: bad vfs ops template");
3380Sstevel@tonic-gate 		return (error);
3390Sstevel@tonic-gate 	}
3400Sstevel@tonic-gate 
3410Sstevel@tonic-gate 	error = vn_make_ops(name, fifo_vnodeops_template, &fifo_vnodeops);
3420Sstevel@tonic-gate 	if (error != 0) {
3430Sstevel@tonic-gate 		(void) vfs_freevfsops_by_type(fstype);
3440Sstevel@tonic-gate 		cmn_err(CE_WARN, "fifoinit: bad vnode ops template");
3450Sstevel@tonic-gate 		return (error);
3460Sstevel@tonic-gate 	}
3470Sstevel@tonic-gate 
3480Sstevel@tonic-gate 	if ((dev = getudev()) == (major_t)-1) {
3490Sstevel@tonic-gate 		cmn_err(CE_WARN, "fifoinit: can't get unique device number");
3500Sstevel@tonic-gate 		dev = 0;
3510Sstevel@tonic-gate 	}
3520Sstevel@tonic-gate 	fifodev = makedevice(dev, 0);
3530Sstevel@tonic-gate 
3540Sstevel@tonic-gate 	fifovfsp = kmem_zalloc(sizeof (struct vfs), KM_SLEEP);
3550Sstevel@tonic-gate 	fifovfsp->vfs_next = NULL;
3560Sstevel@tonic-gate 	vfs_setops(fifovfsp, fifo_vfsops);
3570Sstevel@tonic-gate 	fifovfsp->vfs_vnodecovered = NULL;
3580Sstevel@tonic-gate 	fifovfsp->vfs_flag = 0;
3590Sstevel@tonic-gate 	fifovfsp->vfs_bsize = 1024;
3600Sstevel@tonic-gate 	fifovfsp->vfs_fstype = fifofstype;
3610Sstevel@tonic-gate 	vfs_make_fsid(&fifovfsp->vfs_fsid, fifodev, fifofstype);
3620Sstevel@tonic-gate 	fifovfsp->vfs_data = NULL;
3630Sstevel@tonic-gate 	fifovfsp->vfs_dev = fifodev;
3640Sstevel@tonic-gate 	fifovfsp->vfs_bcount = 0;
3650Sstevel@tonic-gate 
3667196Svk210190 	/*
3677196Svk210190 	 * It is necessary to initialize vfs_count here to 1.
3687196Svk210190 	 * This prevents the fifovfsp from getting freed when
3697196Svk210190 	 * a thread does a VFS_HOLD followed by a VFS_RELE
3707196Svk210190 	 * on the fifovfsp
3717196Svk210190 	 *
3727196Svk210190 	 * The fifovfsp should never be freed.
3737196Svk210190 	 */
3747196Svk210190 	fifovfsp->vfs_count = 1;
3757196Svk210190 
3760Sstevel@tonic-gate 	mutex_init(&ftable_lock, NULL, MUTEX_DEFAULT, NULL);
3770Sstevel@tonic-gate 	mutex_init(&fino_lock, NULL, MUTEX_DEFAULT, NULL);
3780Sstevel@tonic-gate 
3790Sstevel@tonic-gate 	/*
3800Sstevel@tonic-gate 	 * vnodes are cached aligned
3810Sstevel@tonic-gate 	 */
3820Sstevel@tonic-gate 	fnode_cache = kmem_cache_create("fnode_cache",
3835927Sbpramod 	    sizeof (fifodata_t) - sizeof (fifonode_t), 32,
3845927Sbpramod 	    fnode_constructor, fnode_destructor, NULL,
3855927Sbpramod 	    (void *)(sizeof (fifodata_t) - sizeof (fifonode_t)), NULL, 0);
3860Sstevel@tonic-gate 
3870Sstevel@tonic-gate 	pipe_cache = kmem_cache_create("pipe_cache", sizeof (fifodata_t), 32,
3885927Sbpramod 	    pipe_constructor, pipe_destructor, NULL,
3895927Sbpramod 	    (void *)(sizeof (fifodata_t)), NULL, 0);
3900Sstevel@tonic-gate 
3910Sstevel@tonic-gate #if FIFODEBUG
3920Sstevel@tonic-gate 	if (Fifohiwat < FIFOHIWAT)
3930Sstevel@tonic-gate 		Fifohiwat = FIFOHIWAT;
3940Sstevel@tonic-gate #endif /* FIFODEBUG */
3950Sstevel@tonic-gate 	fifo_strdata.qi_minfo->mi_hiwat = Fifohiwat;
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate 	return (0);
3980Sstevel@tonic-gate }
3990Sstevel@tonic-gate 
4000Sstevel@tonic-gate /*
401142Speterte  * Provide a shadow for a vnode.  We create a new shadow before checking for an
402142Speterte  * existing one, to minimize the amount of time we need to hold ftable_lock.
403142Speterte  * If a vp already has a shadow in the hash list, return its shadow.  If not,
404142Speterte  * we hash the new vnode and return its pointer to the caller.
4050Sstevel@tonic-gate  */
4060Sstevel@tonic-gate vnode_t *
fifovp(vnode_t * vp,cred_t * crp)4070Sstevel@tonic-gate fifovp(vnode_t *vp, cred_t *crp)
4080Sstevel@tonic-gate {
4090Sstevel@tonic-gate 	fifonode_t *fnp;
410142Speterte 	fifonode_t *spec_fnp;   /* Speculative fnode ptr. */
4110Sstevel@tonic-gate 	fifodata_t *fdp;
4120Sstevel@tonic-gate 	vnode_t *newvp;
4130Sstevel@tonic-gate 	struct vattr va;
4143947Snr123932 	vnode_t	*rvp;
4150Sstevel@tonic-gate 
4160Sstevel@tonic-gate 	ASSERT(vp != NULL);
4170Sstevel@tonic-gate 
4180Sstevel@tonic-gate 	fdp = kmem_cache_alloc(fnode_cache, KM_SLEEP);
4190Sstevel@tonic-gate 
4200Sstevel@tonic-gate 	fdp->fifo_lock.flk_ref = 1;
4210Sstevel@tonic-gate 	fnp = &fdp->fifo_fnode[0];
4221799Sgfaden 
4231799Sgfaden 	/*
4243947Snr123932 	 * Its possible that fifo nodes on different lofs mountpoints
4253947Snr123932 	 * shadow the same real filesystem fifo node.
4263947Snr123932 	 * In this case its necessary to get and store the realvp.
4273947Snr123932 	 * This way different fifo nodes sharing the same real vnode
4283947Snr123932 	 * can use realvp for communication.
4291799Sgfaden 	 */
4305331Samw 
4315331Samw 	if (VOP_REALVP(vp, &rvp, NULL) == 0)
4325331Samw 			vp = rvp;
4331799Sgfaden 
4340Sstevel@tonic-gate 	fnp->fn_realvp	= vp;
4350Sstevel@tonic-gate 	fnp->fn_wcnt	= 0;
4360Sstevel@tonic-gate 	fnp->fn_rcnt	= 0;
437142Speterte 
4380Sstevel@tonic-gate #if FIFODEBUG
4390Sstevel@tonic-gate 	if (! Fifo_fastmode) {
4400Sstevel@tonic-gate 		fnp->fn_flag	= 0;
4410Sstevel@tonic-gate 	} else {
4420Sstevel@tonic-gate 		fnp->fn_flag	= FIFOFAST;
4430Sstevel@tonic-gate 	}
4440Sstevel@tonic-gate #else /* FIFODEBUG */
4450Sstevel@tonic-gate 	fnp->fn_flag	= FIFOFAST;
4460Sstevel@tonic-gate #endif /* FIFODEBUG */
4470Sstevel@tonic-gate 
4480Sstevel@tonic-gate 	/*
4490Sstevel@tonic-gate 	 * initialize the times from vp.
4500Sstevel@tonic-gate 	 */
4510Sstevel@tonic-gate 	va.va_mask = AT_TIMES;
4525331Samw 	if (VOP_GETATTR(vp, &va, 0, crp, NULL) == 0) {
4530Sstevel@tonic-gate 		fnp->fn_atime = va.va_atime.tv_sec;
4540Sstevel@tonic-gate 		fnp->fn_mtime = va.va_mtime.tv_sec;
4550Sstevel@tonic-gate 		fnp->fn_ctime = va.va_ctime.tv_sec;
4560Sstevel@tonic-gate 	} else {
4570Sstevel@tonic-gate 		fnp->fn_atime = 0;
4580Sstevel@tonic-gate 		fnp->fn_mtime = 0;
4590Sstevel@tonic-gate 		fnp->fn_ctime = 0;
4600Sstevel@tonic-gate 	}
4610Sstevel@tonic-gate 
462142Speterte 	/*
463142Speterte 	 * Grab the VP here to avoid holding locks
464142Speterte 	 * whilst trying to acquire others.
465142Speterte 	 */
466142Speterte 
4670Sstevel@tonic-gate 	VN_HOLD(vp);
4680Sstevel@tonic-gate 
469142Speterte 	mutex_enter(&ftable_lock);
470142Speterte 
471142Speterte 	if ((spec_fnp = fifofind(vp)) != NULL) {
472142Speterte 		mutex_exit(&ftable_lock);
473142Speterte 
474142Speterte 		/*
475142Speterte 		 * Release the vnode and free up our pre-prepared fnode.
476142Speterte 		 * Zero the lock reference just to explicitly signal
477142Speterte 		 * this is unused.
478142Speterte 		 */
479142Speterte 		VN_RELE(vp);
480142Speterte 		fdp->fifo_lock.flk_ref = 0;
481142Speterte 		kmem_cache_free(fnode_cache, fdp);
482142Speterte 
483142Speterte 		return (FTOV(spec_fnp));
484142Speterte 	}
485142Speterte 
4860Sstevel@tonic-gate 	newvp = FTOV(fnp);
4870Sstevel@tonic-gate 	fifo_reinit_vp(newvp);
4886283Sbpramod 	/*
4896283Sbpramod 	 * Since the fifo vnode's v_vfsp needs to point to the
4906283Sbpramod 	 * underlying filesystem's vfsp we need to bump up the
4916283Sbpramod 	 * underlying filesystem's vfs reference count.
4926283Sbpramod 	 * The count is decremented when the fifo node is
4936283Sbpramod 	 * inactivated.
4946283Sbpramod 	 */
4955927Sbpramod 
4966283Sbpramod 	VFS_HOLD(vp->v_vfsp);
4976283Sbpramod 	newvp->v_vfsp = vp->v_vfsp;
4986283Sbpramod 	newvp->v_rdev = vp->v_rdev;
4992712Snn35248 	newvp->v_flag |= (vp->v_flag & VROOT);
5000Sstevel@tonic-gate 
5010Sstevel@tonic-gate 	fifoinsert(fnp);
5020Sstevel@tonic-gate 	mutex_exit(&ftable_lock);
5030Sstevel@tonic-gate 
5040Sstevel@tonic-gate 	return (newvp);
5050Sstevel@tonic-gate }
5060Sstevel@tonic-gate 
5070Sstevel@tonic-gate /*
5080Sstevel@tonic-gate  * Create a pipe end by...
5090Sstevel@tonic-gate  * allocating a vnode-fifonode pair and initializing the fifonode.
5100Sstevel@tonic-gate  */
5110Sstevel@tonic-gate void
makepipe(vnode_t ** vpp1,vnode_t ** vpp2)5120Sstevel@tonic-gate makepipe(vnode_t **vpp1, vnode_t **vpp2)
5130Sstevel@tonic-gate {
5140Sstevel@tonic-gate 	fifonode_t *fnp1;
5150Sstevel@tonic-gate 	fifonode_t *fnp2;
5160Sstevel@tonic-gate 	vnode_t *nvp1;
5170Sstevel@tonic-gate 	vnode_t *nvp2;
5180Sstevel@tonic-gate 	fifodata_t *fdp;
5190Sstevel@tonic-gate 	time_t now;
5200Sstevel@tonic-gate 
5210Sstevel@tonic-gate 	fdp = kmem_cache_alloc(pipe_cache, KM_SLEEP);
5220Sstevel@tonic-gate 	fdp->fifo_lock.flk_ref = 2;
5230Sstevel@tonic-gate 	fnp1 = &fdp->fifo_fnode[0];
5240Sstevel@tonic-gate 	fnp2 = &fdp->fifo_fnode[1];
5250Sstevel@tonic-gate 
5260Sstevel@tonic-gate 	fnp1->fn_wcnt	= fnp2->fn_wcnt		= 1;
5270Sstevel@tonic-gate 	fnp1->fn_rcnt	= fnp2->fn_rcnt		= 1;
5280Sstevel@tonic-gate #if FIFODEBUG
5290Sstevel@tonic-gate 	if (! Fifo_fastmode) {
5300Sstevel@tonic-gate 		fnp1->fn_flag	= fnp2->fn_flag		= ISPIPE;
5310Sstevel@tonic-gate 	} else {
5320Sstevel@tonic-gate 		fnp1->fn_flag	= fnp2->fn_flag		= ISPIPE | FIFOFAST;
5330Sstevel@tonic-gate 	}
5340Sstevel@tonic-gate #else /* FIFODEBUG */
5350Sstevel@tonic-gate 	fnp1->fn_flag	= fnp2->fn_flag		= ISPIPE | FIFOFAST;
5360Sstevel@tonic-gate #endif /* FIFODEBUG */
5370Sstevel@tonic-gate 	now = gethrestime_sec();
5380Sstevel@tonic-gate 	fnp1->fn_atime	= fnp2->fn_atime	= now;
5390Sstevel@tonic-gate 	fnp1->fn_mtime	= fnp2->fn_mtime	= now;
5400Sstevel@tonic-gate 	fnp1->fn_ctime	= fnp2->fn_ctime	= now;
5410Sstevel@tonic-gate 
5420Sstevel@tonic-gate 	*vpp1 = nvp1 = FTOV(fnp1);
5430Sstevel@tonic-gate 	*vpp2 = nvp2 = FTOV(fnp2);
5440Sstevel@tonic-gate 
5450Sstevel@tonic-gate 	fifo_reinit_vp(nvp1);		/* Reinitialize vnodes for reuse... */
5460Sstevel@tonic-gate 	fifo_reinit_vp(nvp2);
5470Sstevel@tonic-gate 	nvp1->v_vfsp = fifovfsp; 	/* Need to re-establish VFS & device */
5480Sstevel@tonic-gate 	nvp2->v_vfsp = fifovfsp; 	/* before we can reuse this vnode. */
5490Sstevel@tonic-gate 	nvp1->v_rdev = fifodev;
5500Sstevel@tonic-gate 	nvp2->v_rdev = fifodev;
5510Sstevel@tonic-gate }
5520Sstevel@tonic-gate 
5530Sstevel@tonic-gate /*
5540Sstevel@tonic-gate  * Attempt to establish a unique pipe id.  Only un-named pipes use this
5550Sstevel@tonic-gate  * routine.
5560Sstevel@tonic-gate  */
5570Sstevel@tonic-gate ino_t
fifogetid(void)5580Sstevel@tonic-gate fifogetid(void)
5590Sstevel@tonic-gate {
5600Sstevel@tonic-gate 	static ino_t fifo_ino = 0;
5610Sstevel@tonic-gate 	ino_t fino;
5620Sstevel@tonic-gate 
5630Sstevel@tonic-gate 	mutex_enter(&fino_lock);
5640Sstevel@tonic-gate 	fino = fifo_ino++;
5650Sstevel@tonic-gate 	mutex_exit(&fino_lock);
5660Sstevel@tonic-gate 	return (fino);
5670Sstevel@tonic-gate }
5680Sstevel@tonic-gate 
5690Sstevel@tonic-gate 
5700Sstevel@tonic-gate /*
5710Sstevel@tonic-gate  * Stream a pipe/FIFO.
5720Sstevel@tonic-gate  * The FIFOCONNLD flag is used when CONNLD has been pushed on the stream.
5730Sstevel@tonic-gate  * If the flag is set, a new vnode is created by calling fifo_connld().
5740Sstevel@tonic-gate  * Connld logic was moved to fifo_connld() to speed up the open
5750Sstevel@tonic-gate  * operation, simplify the connld/fifo interaction, and remove inherent
5760Sstevel@tonic-gate  * race conditions between the connld module and fifos.
5770Sstevel@tonic-gate  * This routine is single threaded for two reasons.
5780Sstevel@tonic-gate  * 1) connld requests are synchronous; that is, they must block
5790Sstevel@tonic-gate  *    until the server does an I_RECVFD (oh, well).  Single threading is
5800Sstevel@tonic-gate  *    the simplest way to accomplish this.
5810Sstevel@tonic-gate  * 2) fifo_close() must not send M_HANGUP or M_ERROR while we are
5820Sstevel@tonic-gate  *    in stropen. Stropen() has a tendency to reset things and
5830Sstevel@tonic-gate  *    we would like streams to remember that a hangup occurred.
5840Sstevel@tonic-gate  */
5850Sstevel@tonic-gate int
fifo_stropen(vnode_t ** vpp,int flag,cred_t * crp,int dotwist,int lockheld)5860Sstevel@tonic-gate fifo_stropen(vnode_t **vpp, int flag, cred_t *crp, int dotwist, int lockheld)
5870Sstevel@tonic-gate {
5880Sstevel@tonic-gate 	int error = 0;
5890Sstevel@tonic-gate 	vnode_t *oldvp = *vpp;
5900Sstevel@tonic-gate 	fifonode_t *fnp = VTOF(*vpp);
5910Sstevel@tonic-gate 	dev_t pdev = 0;
5920Sstevel@tonic-gate 	int firstopen = 0;
5930Sstevel@tonic-gate 	fifolock_t *fn_lock;
5940Sstevel@tonic-gate 
5950Sstevel@tonic-gate 	fn_lock = fnp->fn_lock;
5960Sstevel@tonic-gate 	if (!lockheld)
5970Sstevel@tonic-gate 		mutex_enter(&fn_lock->flk_lock);
5980Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&fnp->fn_lock->flk_lock));
5990Sstevel@tonic-gate 
6000Sstevel@tonic-gate 	/*
6010Sstevel@tonic-gate 	 * FIFO is in the process of opening. Wait for it
6020Sstevel@tonic-gate 	 * to complete before starting another open on it
6030Sstevel@tonic-gate 	 * This prevents races associated with connld open
6040Sstevel@tonic-gate 	 */
6050Sstevel@tonic-gate 	while (fnp->fn_flag & FIFOOPEN) {
6060Sstevel@tonic-gate 		if (!cv_wait_sig(&fnp->fn_wait_cv, &fn_lock->flk_lock)) {
6070Sstevel@tonic-gate 			fifo_cleanup(oldvp, flag);
6080Sstevel@tonic-gate 			if (!lockheld)
6090Sstevel@tonic-gate 				mutex_exit(&fn_lock->flk_lock);
6100Sstevel@tonic-gate 			return (EINTR);
6110Sstevel@tonic-gate 		}
6120Sstevel@tonic-gate 	}
6130Sstevel@tonic-gate 
6140Sstevel@tonic-gate 	/*
6150Sstevel@tonic-gate 	 * The other end of the pipe is almost closed so
6160Sstevel@tonic-gate 	 * reject any other open on this end of the pipe
6170Sstevel@tonic-gate 	 * This only happens with a pipe mounted under namefs
6180Sstevel@tonic-gate 	 */
6190Sstevel@tonic-gate 	if ((fnp->fn_flag & (FIFOCLOSE|ISPIPE)) == (FIFOCLOSE|ISPIPE)) {
6200Sstevel@tonic-gate 		fifo_cleanup(oldvp, flag);
6210Sstevel@tonic-gate 		cv_broadcast(&fnp->fn_wait_cv);
6220Sstevel@tonic-gate 		if (!lockheld)
6230Sstevel@tonic-gate 			mutex_exit(&fn_lock->flk_lock);
6240Sstevel@tonic-gate 		return (ENXIO);
6250Sstevel@tonic-gate 	}
6260Sstevel@tonic-gate 
6270Sstevel@tonic-gate 	fnp->fn_flag |= FIFOOPEN;
6280Sstevel@tonic-gate 
6290Sstevel@tonic-gate 	/*
6300Sstevel@tonic-gate 	 * can't allow close to happen while we are
6310Sstevel@tonic-gate 	 * in the middle of stropen().
6320Sstevel@tonic-gate 	 * M_HANGUP and M_ERROR could leave the stream in a strange state
6330Sstevel@tonic-gate 	 */
6340Sstevel@tonic-gate 	while (fn_lock->flk_ocsync)
6350Sstevel@tonic-gate 		cv_wait(&fn_lock->flk_wait_cv, &fn_lock->flk_lock);
6360Sstevel@tonic-gate 
6370Sstevel@tonic-gate 	fn_lock->flk_ocsync = 1;
6380Sstevel@tonic-gate 
6390Sstevel@tonic-gate 	if (fnp->fn_flag & FIFOCONNLD) {
6400Sstevel@tonic-gate 		/*
6410Sstevel@tonic-gate 		 * This is a reopen, so we should release the fifo lock
6420Sstevel@tonic-gate 		 * just in case some strange module pushed on connld
6430Sstevel@tonic-gate 		 * has some odd side effect.
6440Sstevel@tonic-gate 		 * Note: this stropen is on the oldvp.  It will
6450Sstevel@tonic-gate 		 * have no impact on the connld vp returned and
6460Sstevel@tonic-gate 		 * strclose() will only be called when we release
6470Sstevel@tonic-gate 		 * flk_ocsync
6480Sstevel@tonic-gate 		 */
6490Sstevel@tonic-gate 		mutex_exit(&fn_lock->flk_lock);
6500Sstevel@tonic-gate 		if ((error = stropen(oldvp, &pdev, flag, crp)) != 0) {
6510Sstevel@tonic-gate 			mutex_enter(&fn_lock->flk_lock);
6520Sstevel@tonic-gate 			fifo_cleanup(oldvp, flag);
6530Sstevel@tonic-gate 			fn_lock->flk_ocsync = 0;
6540Sstevel@tonic-gate 			cv_broadcast(&fn_lock->flk_wait_cv);
6550Sstevel@tonic-gate 			goto out;
6560Sstevel@tonic-gate 		}
6570Sstevel@tonic-gate 		/*
6580Sstevel@tonic-gate 		 * streams open done, allow close on other end if
6590Sstevel@tonic-gate 		 * required.  Do this now.. it could
6600Sstevel@tonic-gate 		 * be a very long time before fifo_connld returns.
6610Sstevel@tonic-gate 		 */
6620Sstevel@tonic-gate 		mutex_enter(&fn_lock->flk_lock);
6630Sstevel@tonic-gate 		/*
6640Sstevel@tonic-gate 		 * we need to fake an open here so that if this
6650Sstevel@tonic-gate 		 * end of the pipe closes, we don't loose the
6660Sstevel@tonic-gate 		 * stream head (kind of like single threading
6670Sstevel@tonic-gate 		 * open and close for this end of the pipe)
6680Sstevel@tonic-gate 		 * We'll need to call fifo_close() to do clean
6690Sstevel@tonic-gate 		 * up in case this end of the pipe was closed
6700Sstevel@tonic-gate 		 * down while we were in fifo_connld()
6710Sstevel@tonic-gate 		 */
6720Sstevel@tonic-gate 		ASSERT(fnp->fn_open > 0);
6730Sstevel@tonic-gate 		fnp->fn_open++;
6740Sstevel@tonic-gate 		fn_lock->flk_ocsync = 0;
6750Sstevel@tonic-gate 		cv_broadcast(&fn_lock->flk_wait_cv);
6760Sstevel@tonic-gate 		mutex_exit(&fn_lock->flk_lock);
6770Sstevel@tonic-gate 		/*
6780Sstevel@tonic-gate 		 * Connld has been pushed onto the pipe
6790Sstevel@tonic-gate 		 * Create new pipe on behalf of connld
6800Sstevel@tonic-gate 		 */
6810Sstevel@tonic-gate 		if (error = fifo_connld(vpp, flag, crp)) {
6825331Samw 			(void) fifo_close(oldvp, flag, 1, 0, crp, NULL);
6830Sstevel@tonic-gate 			mutex_enter(&fn_lock->flk_lock);
6840Sstevel@tonic-gate 			goto out;
6850Sstevel@tonic-gate 		}
6860Sstevel@tonic-gate 		/*
6870Sstevel@tonic-gate 		 * undo fake open.  We need to call fifo_close
6880Sstevel@tonic-gate 		 * because some other thread could have done
6890Sstevel@tonic-gate 		 * a close and detach of the named pipe while
6900Sstevel@tonic-gate 		 * we were in fifo_connld(), so
6910Sstevel@tonic-gate 		 * we want to make sure the close completes (yuk)
6920Sstevel@tonic-gate 		 */
6935331Samw 		(void) fifo_close(oldvp, flag, 1, 0, crp, NULL);
6940Sstevel@tonic-gate 		/*
6950Sstevel@tonic-gate 		 * fifo_connld has changed the vp, so we
6960Sstevel@tonic-gate 		 * need to re-initialize locals
6970Sstevel@tonic-gate 		 */
6980Sstevel@tonic-gate 		fnp = VTOF(*vpp);
6990Sstevel@tonic-gate 		fn_lock = fnp->fn_lock;
7000Sstevel@tonic-gate 		mutex_enter(&fn_lock->flk_lock);
7010Sstevel@tonic-gate 	} else {
7020Sstevel@tonic-gate 		/*
7030Sstevel@tonic-gate 		 * release lock in case there are modules pushed that
7040Sstevel@tonic-gate 		 * could have some strange side effect
7050Sstevel@tonic-gate 		 */
7060Sstevel@tonic-gate 
7070Sstevel@tonic-gate 		mutex_exit(&fn_lock->flk_lock);
7080Sstevel@tonic-gate 
7090Sstevel@tonic-gate 		/*
7100Sstevel@tonic-gate 		 * If this is the first open of a fifo (dotwist
7110Sstevel@tonic-gate 		 * will be non-zero) we will need to twist the queues.
7120Sstevel@tonic-gate 		 */
7130Sstevel@tonic-gate 		if (oldvp->v_stream == NULL)
7140Sstevel@tonic-gate 			firstopen = 1;
7150Sstevel@tonic-gate 
7160Sstevel@tonic-gate 
7170Sstevel@tonic-gate 		/*
7180Sstevel@tonic-gate 		 * normal open of pipe/fifo
7190Sstevel@tonic-gate 		 */
7200Sstevel@tonic-gate 
7210Sstevel@tonic-gate 		if ((error = stropen(oldvp, &pdev, flag, crp)) != 0) {
7220Sstevel@tonic-gate 			mutex_enter(&fn_lock->flk_lock);
7230Sstevel@tonic-gate 			fifo_cleanup(oldvp, flag);
7240Sstevel@tonic-gate 			ASSERT(fnp->fn_open != 0 || oldvp->v_stream == NULL);
7250Sstevel@tonic-gate 			fn_lock->flk_ocsync = 0;
7260Sstevel@tonic-gate 			cv_broadcast(&fn_lock->flk_wait_cv);
7270Sstevel@tonic-gate 			goto out;
7280Sstevel@tonic-gate 		}
7290Sstevel@tonic-gate 		mutex_enter(&fn_lock->flk_lock);
7300Sstevel@tonic-gate 
7310Sstevel@tonic-gate 		/*
7320Sstevel@tonic-gate 		 * twist the ends of the fifo together
7330Sstevel@tonic-gate 		 */
7340Sstevel@tonic-gate 		if (dotwist && firstopen)
7350Sstevel@tonic-gate 			strmate(*vpp, *vpp);
7360Sstevel@tonic-gate 
7370Sstevel@tonic-gate 		/*
7380Sstevel@tonic-gate 		 * Show that this open has succeeded
7390Sstevel@tonic-gate 		 * and allow closes or other opens to proceed
7400Sstevel@tonic-gate 		 */
7410Sstevel@tonic-gate 		fnp->fn_open++;
7420Sstevel@tonic-gate 		fn_lock->flk_ocsync = 0;
7430Sstevel@tonic-gate 		cv_broadcast(&fn_lock->flk_wait_cv);
7440Sstevel@tonic-gate 	}
7450Sstevel@tonic-gate out:
7460Sstevel@tonic-gate 	fnp->fn_flag &= ~FIFOOPEN;
7470Sstevel@tonic-gate 	if (error == 0) {
7480Sstevel@tonic-gate 		fnp->fn_flag |= FIFOISOPEN;
7490Sstevel@tonic-gate 		/*
7500Sstevel@tonic-gate 		 * If this is a FIFO and has the close flag set
7510Sstevel@tonic-gate 		 * and there are now writers, clear the close flag
7520Sstevel@tonic-gate 		 * Note: close flag only gets set when last writer
7530Sstevel@tonic-gate 		 * on a FIFO goes away.
7540Sstevel@tonic-gate 		 */
7550Sstevel@tonic-gate 		if (((fnp->fn_flag & (ISPIPE|FIFOCLOSE)) == FIFOCLOSE) &&
7560Sstevel@tonic-gate 		    fnp->fn_wcnt > 0)
7570Sstevel@tonic-gate 			fnp->fn_flag &= ~FIFOCLOSE;
7580Sstevel@tonic-gate 	}
7590Sstevel@tonic-gate 	cv_broadcast(&fnp->fn_wait_cv);
7600Sstevel@tonic-gate 	if (!lockheld)
7610Sstevel@tonic-gate 		mutex_exit(&fn_lock->flk_lock);
7620Sstevel@tonic-gate 	return (error);
7630Sstevel@tonic-gate }
7640Sstevel@tonic-gate 
7650Sstevel@tonic-gate /*
7660Sstevel@tonic-gate  * Clean up the state of a FIFO and/or mounted pipe in the
7670Sstevel@tonic-gate  * event that a fifo_open() was interrupted while the
7680Sstevel@tonic-gate  * process was blocked.
7690Sstevel@tonic-gate  */
7700Sstevel@tonic-gate void
fifo_cleanup(vnode_t * vp,int flag)7710Sstevel@tonic-gate fifo_cleanup(vnode_t *vp, int flag)
7720Sstevel@tonic-gate {
7730Sstevel@tonic-gate 	fifonode_t *fnp = VTOF(vp);
7740Sstevel@tonic-gate 
7750Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&fnp->fn_lock->flk_lock));
7760Sstevel@tonic-gate 
7770Sstevel@tonic-gate 	cleanlocks(vp, curproc->p_pid, 0);
7780Sstevel@tonic-gate 	cleanshares(vp, curproc->p_pid);
7790Sstevel@tonic-gate 	if (flag & FREAD) {
7800Sstevel@tonic-gate 		fnp->fn_rcnt--;
7810Sstevel@tonic-gate 	}
7820Sstevel@tonic-gate 	if (flag & FWRITE) {
7830Sstevel@tonic-gate 		fnp->fn_wcnt--;
7840Sstevel@tonic-gate 	}
7850Sstevel@tonic-gate 	cv_broadcast(&fnp->fn_wait_cv);
7860Sstevel@tonic-gate }
7870Sstevel@tonic-gate 
7880Sstevel@tonic-gate 
7890Sstevel@tonic-gate /*
7900Sstevel@tonic-gate  * Insert a fifonode-vnode pair onto the fifoalloc hash list.
7910Sstevel@tonic-gate  */
7920Sstevel@tonic-gate static void
fifoinsert(fifonode_t * fnp)7930Sstevel@tonic-gate fifoinsert(fifonode_t *fnp)
7940Sstevel@tonic-gate {
7950Sstevel@tonic-gate 	int idx = FIFOHASH(fnp->fn_realvp);
7960Sstevel@tonic-gate 
7970Sstevel@tonic-gate 	/*
7980Sstevel@tonic-gate 	 * We don't need to hold fn_lock since we're holding ftable_lock and
7990Sstevel@tonic-gate 	 * this routine is only called right after we've allocated an fnode.
8000Sstevel@tonic-gate 	 * FIFO is inserted at head of NULL terminated doubly linked list.
8010Sstevel@tonic-gate 	 */
8020Sstevel@tonic-gate 
8030Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ftable_lock));
8040Sstevel@tonic-gate 	fnp->fn_backp = NULL;
8050Sstevel@tonic-gate 	fnp->fn_nextp = fifoalloc[idx];
8060Sstevel@tonic-gate 	fifoalloc[idx] = fnp;
8070Sstevel@tonic-gate 	if (fnp->fn_nextp)
8080Sstevel@tonic-gate 		fnp->fn_nextp->fn_backp = fnp;
8090Sstevel@tonic-gate }
8100Sstevel@tonic-gate 
8110Sstevel@tonic-gate /*
8120Sstevel@tonic-gate  * Find a fifonode-vnode pair on the fifoalloc hash list.
8130Sstevel@tonic-gate  * vp is a vnode to be shadowed. If it's on the hash list,
8140Sstevel@tonic-gate  * it already has a shadow, therefore return its corresponding
8150Sstevel@tonic-gate  * fifonode.
8160Sstevel@tonic-gate  */
8170Sstevel@tonic-gate static fifonode_t *
fifofind(vnode_t * vp)8180Sstevel@tonic-gate fifofind(vnode_t *vp)
8190Sstevel@tonic-gate {
8200Sstevel@tonic-gate 	fifonode_t *fnode;
8210Sstevel@tonic-gate 
8220Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ftable_lock));
8230Sstevel@tonic-gate 	for (fnode = fifoalloc[FIFOHASH(vp)]; fnode; fnode = fnode->fn_nextp) {
8240Sstevel@tonic-gate 		if (fnode->fn_realvp == vp) {
8250Sstevel@tonic-gate 			VN_HOLD(FTOV(fnode));
8260Sstevel@tonic-gate 			return (fnode);
8270Sstevel@tonic-gate 		}
8280Sstevel@tonic-gate 	}
8290Sstevel@tonic-gate 	return (NULL);
8300Sstevel@tonic-gate }
8310Sstevel@tonic-gate 
8320Sstevel@tonic-gate /*
8330Sstevel@tonic-gate  * Remove a fifonode-vnode pair from the fifoalloc hash list.
8340Sstevel@tonic-gate  * This routine is called from the fifo_inactive() routine when a
8350Sstevel@tonic-gate  * FIFO is being released.
8360Sstevel@tonic-gate  * If the link to be removed is the only link, set fifoalloc to NULL.
8370Sstevel@tonic-gate  */
8380Sstevel@tonic-gate void
fiforemove(fifonode_t * fnp)8390Sstevel@tonic-gate fiforemove(fifonode_t *fnp)
8400Sstevel@tonic-gate {
8410Sstevel@tonic-gate 	int idx = FIFOHASH(fnp->fn_realvp);
8420Sstevel@tonic-gate 	fifonode_t *fnode;
8430Sstevel@tonic-gate 
8440Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ftable_lock));
8450Sstevel@tonic-gate 	fnode = fifoalloc[idx];
8460Sstevel@tonic-gate 	/*
8470Sstevel@tonic-gate 	 * fast path... only 1 FIFO in this list entry
8480Sstevel@tonic-gate 	 */
8490Sstevel@tonic-gate 	if (fnode != NULL && fnode == fnp &&
8505927Sbpramod 	    !fnode->fn_nextp && !fnode->fn_backp) {
8516712Stomee 		fifoalloc[idx] = NULL;
8520Sstevel@tonic-gate 	} else {
8530Sstevel@tonic-gate 
8540Sstevel@tonic-gate 		for (;  fnode;  fnode = fnode->fn_nextp) {
8550Sstevel@tonic-gate 			if (fnode == fnp) {
8560Sstevel@tonic-gate 				/*
8570Sstevel@tonic-gate 				 * if we are first entry
8580Sstevel@tonic-gate 				 */
8590Sstevel@tonic-gate 				if (fnp == fifoalloc[idx])
8600Sstevel@tonic-gate 					fifoalloc[idx] = fnp->fn_nextp;
8610Sstevel@tonic-gate 				if (fnode->fn_nextp)
8620Sstevel@tonic-gate 					fnode->fn_nextp->fn_backp =
8635927Sbpramod 					    fnode->fn_backp;
8640Sstevel@tonic-gate 				if (fnode->fn_backp)
8650Sstevel@tonic-gate 					fnode->fn_backp->fn_nextp =
8665927Sbpramod 					    fnode->fn_nextp;
8670Sstevel@tonic-gate 				break;
8680Sstevel@tonic-gate 			}
8690Sstevel@tonic-gate 		}
8700Sstevel@tonic-gate 	}
8710Sstevel@tonic-gate }
8720Sstevel@tonic-gate 
8730Sstevel@tonic-gate /*
8740Sstevel@tonic-gate  * Flush all data from a fifo's message queue
8750Sstevel@tonic-gate  */
8760Sstevel@tonic-gate 
8770Sstevel@tonic-gate void
fifo_fastflush(fifonode_t * fnp)8780Sstevel@tonic-gate fifo_fastflush(fifonode_t *fnp)
8790Sstevel@tonic-gate {
8800Sstevel@tonic-gate 	mblk_t *bp;
8810Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&fnp->fn_lock->flk_lock));
8820Sstevel@tonic-gate 
8830Sstevel@tonic-gate 	if ((bp = fnp->fn_mp) != NULL) {
8840Sstevel@tonic-gate 		fnp->fn_mp = NULL;
8850Sstevel@tonic-gate 		fnp->fn_count = 0;
8860Sstevel@tonic-gate 		freemsg(bp);
8870Sstevel@tonic-gate 	}
8880Sstevel@tonic-gate 	fifo_wakewriter(fnp->fn_dest, fnp->fn_lock);
8890Sstevel@tonic-gate }
8900Sstevel@tonic-gate 
8910Sstevel@tonic-gate /*
8920Sstevel@tonic-gate  * Note:  This routine is single threaded
8930Sstevel@tonic-gate  *  Protected by FIFOOPEN flag (i.e. flk_lock is not held)
8940Sstevel@tonic-gate  *  Upon successful completion, the original fifo is unlocked
8950Sstevel@tonic-gate  *  and FIFOOPEN is cleared for the original vpp.
8960Sstevel@tonic-gate  *  The new fifo returned has FIFOOPEN set.
8970Sstevel@tonic-gate  */
8980Sstevel@tonic-gate static int
fifo_connld(struct vnode ** vpp,int flag,cred_t * crp)8990Sstevel@tonic-gate fifo_connld(struct vnode **vpp, int flag, cred_t *crp)
9000Sstevel@tonic-gate {
9010Sstevel@tonic-gate 	struct vnode *vp1;
9020Sstevel@tonic-gate 	struct vnode *vp2;
9030Sstevel@tonic-gate 	struct fifonode *oldfnp;
9040Sstevel@tonic-gate 	struct fifonode *fn_dest;
9050Sstevel@tonic-gate 	int error;
9060Sstevel@tonic-gate 	struct file *filep;
9070Sstevel@tonic-gate 	struct fifolock *fn_lock;
9080Sstevel@tonic-gate 	cred_t *c;
9090Sstevel@tonic-gate 
9100Sstevel@tonic-gate 	/*
9110Sstevel@tonic-gate 	 * Get two vnodes that will represent the pipe ends for the new pipe.
9120Sstevel@tonic-gate 	 */
9130Sstevel@tonic-gate 	makepipe(&vp1, &vp2);
9140Sstevel@tonic-gate 
9150Sstevel@tonic-gate 	/*
9160Sstevel@tonic-gate 	 * Allocate a file descriptor and file pointer for one of the pipe
9170Sstevel@tonic-gate 	 * ends. The file descriptor will be used to send that pipe end to
9180Sstevel@tonic-gate 	 * the process on the other end of this stream. Note that we get
9190Sstevel@tonic-gate 	 * the file structure only, there is no file list entry allocated.
9200Sstevel@tonic-gate 	 */
9210Sstevel@tonic-gate 	if (error = falloc(vp1, FWRITE|FREAD, &filep, NULL)) {
9220Sstevel@tonic-gate 		VN_RELE(vp1);
9230Sstevel@tonic-gate 		VN_RELE(vp2);
9240Sstevel@tonic-gate 		return (error);
9250Sstevel@tonic-gate 	}
9260Sstevel@tonic-gate 	mutex_exit(&filep->f_tlock);
9270Sstevel@tonic-gate 	oldfnp = VTOF(*vpp);
9280Sstevel@tonic-gate 	fn_lock = oldfnp->fn_lock;
9290Sstevel@tonic-gate 	fn_dest = oldfnp->fn_dest;
9300Sstevel@tonic-gate 
9310Sstevel@tonic-gate 	/*
9320Sstevel@tonic-gate 	 * Create two new stream heads and attach them to the two vnodes for
9330Sstevel@tonic-gate 	 * the new pipe.
9340Sstevel@tonic-gate 	 */
9350Sstevel@tonic-gate 	if ((error = fifo_stropen(&vp1, FREAD|FWRITE, filep->f_cred, 0, 0)) !=
9360Sstevel@tonic-gate 	    0 ||
9370Sstevel@tonic-gate 	    (error = fifo_stropen(&vp2, flag, filep->f_cred, 0, 0)) != 0) {
9380Sstevel@tonic-gate #if DEBUG
9396712Stomee 		cmn_err(CE_NOTE, "fifo stropen failed error 0x%x", error);
9400Sstevel@tonic-gate #endif
9410Sstevel@tonic-gate 		/*
9420Sstevel@tonic-gate 		 * this will call fifo_close and VN_RELE on vp1
9430Sstevel@tonic-gate 		 */
9440Sstevel@tonic-gate 		(void) closef(filep);
9450Sstevel@tonic-gate 		VN_RELE(vp2);
9460Sstevel@tonic-gate 		return (error);
9470Sstevel@tonic-gate 	}
9480Sstevel@tonic-gate 
9490Sstevel@tonic-gate 	/*
9500Sstevel@tonic-gate 	 * twist the ends of the pipe together
9510Sstevel@tonic-gate 	 */
9520Sstevel@tonic-gate 	strmate(vp1, vp2);
9530Sstevel@tonic-gate 
9540Sstevel@tonic-gate 	/*
9550Sstevel@tonic-gate 	 * Set our end to busy in open
9560Sstevel@tonic-gate 	 * Note: Don't need lock around this because we're the only
9570Sstevel@tonic-gate 	 * one who knows about it
9580Sstevel@tonic-gate 	 */
9590Sstevel@tonic-gate 	VTOF(vp2)->fn_flag |= FIFOOPEN;
9600Sstevel@tonic-gate 
9610Sstevel@tonic-gate 	mutex_enter(&fn_lock->flk_lock);
9620Sstevel@tonic-gate 
9630Sstevel@tonic-gate 	fn_dest->fn_flag |= FIFOSEND;
9640Sstevel@tonic-gate 	/*
9650Sstevel@tonic-gate 	 * check to make sure neither end of pipe has gone away
9660Sstevel@tonic-gate 	 */
9670Sstevel@tonic-gate 	if (!(fn_dest->fn_flag & FIFOISOPEN)) {
9680Sstevel@tonic-gate 		error = ENXIO;
9690Sstevel@tonic-gate 		fn_dest->fn_flag &= ~FIFOSEND;
9700Sstevel@tonic-gate 		mutex_exit(&fn_lock->flk_lock);
9710Sstevel@tonic-gate 		/*
9720Sstevel@tonic-gate 		 * this will call fifo_close and VN_RELE on vp1
9730Sstevel@tonic-gate 		 */
9740Sstevel@tonic-gate 		goto out;
9750Sstevel@tonic-gate 	}
9760Sstevel@tonic-gate 	mutex_exit(&fn_lock->flk_lock);
9770Sstevel@tonic-gate 
9780Sstevel@tonic-gate 	/*
9790Sstevel@tonic-gate 	 * Tag the sender's credential on the pipe descriptor.
9800Sstevel@tonic-gate 	 */
9810Sstevel@tonic-gate 	crhold(VTOF(vp1)->fn_pcredp = crp);
9820Sstevel@tonic-gate 	VTOF(vp1)->fn_cpid = curproc->p_pid;
9830Sstevel@tonic-gate 
9840Sstevel@tonic-gate 	/*
9850Sstevel@tonic-gate 	 * send the file descriptor to other end of pipe
9860Sstevel@tonic-gate 	 */
9870Sstevel@tonic-gate 	if (error = do_sendfp((*vpp)->v_stream, filep, crp)) {
9880Sstevel@tonic-gate 		mutex_enter(&fn_lock->flk_lock);
9890Sstevel@tonic-gate 		fn_dest->fn_flag &= ~FIFOSEND;
9900Sstevel@tonic-gate 		mutex_exit(&fn_lock->flk_lock);
9910Sstevel@tonic-gate 		/*
9920Sstevel@tonic-gate 		 * this will call fifo_close and VN_RELE on vp1
9930Sstevel@tonic-gate 		 */
9940Sstevel@tonic-gate 		goto out;
9950Sstevel@tonic-gate 	}
9960Sstevel@tonic-gate 
9970Sstevel@tonic-gate 	mutex_enter(&fn_lock->flk_lock);
9980Sstevel@tonic-gate 	/*
9990Sstevel@tonic-gate 	 * Wait for other end to receive file descriptor
10000Sstevel@tonic-gate 	 * FIFOCLOSE indicates that one or both sides of the pipe
10010Sstevel@tonic-gate 	 * have gone away.
10020Sstevel@tonic-gate 	 */
10030Sstevel@tonic-gate 	while ((fn_dest->fn_flag & (FIFOCLOSE | FIFOSEND)) == FIFOSEND) {
10040Sstevel@tonic-gate 		if (!cv_wait_sig(&oldfnp->fn_wait_cv, &fn_lock->flk_lock)) {
10050Sstevel@tonic-gate 			error = EINTR;
10060Sstevel@tonic-gate 			fn_dest->fn_flag &= ~FIFOSEND;
10070Sstevel@tonic-gate 			mutex_exit(&fn_lock->flk_lock);
10080Sstevel@tonic-gate 			goto out;
10090Sstevel@tonic-gate 		}
10100Sstevel@tonic-gate 	}
10110Sstevel@tonic-gate 	/*
10120Sstevel@tonic-gate 	 * If either end of pipe has gone away and the other end did not
10130Sstevel@tonic-gate 	 * receive pipe, reject the connld open
10140Sstevel@tonic-gate 	 */
10150Sstevel@tonic-gate 	if ((fn_dest->fn_flag & FIFOSEND)) {
10160Sstevel@tonic-gate 		error = ENXIO;
10170Sstevel@tonic-gate 		fn_dest->fn_flag &= ~FIFOSEND;
10180Sstevel@tonic-gate 		mutex_exit(&fn_lock->flk_lock);
10190Sstevel@tonic-gate 		goto out;
10200Sstevel@tonic-gate 	}
10210Sstevel@tonic-gate 
10220Sstevel@tonic-gate 	oldfnp->fn_flag &= ~FIFOOPEN;
10230Sstevel@tonic-gate 	cv_broadcast(&oldfnp->fn_wait_cv);
10240Sstevel@tonic-gate 	mutex_exit(&fn_lock->flk_lock);
10250Sstevel@tonic-gate 
10260Sstevel@tonic-gate 	VN_RELE(*vpp);
10270Sstevel@tonic-gate 	*vpp = vp2;
10280Sstevel@tonic-gate 	(void) closef(filep);
10290Sstevel@tonic-gate 	return (0);
10300Sstevel@tonic-gate out:
10310Sstevel@tonic-gate 	c = filep->f_cred;
10320Sstevel@tonic-gate 	crhold(c);
10330Sstevel@tonic-gate 	(void) closef(filep);
10340Sstevel@tonic-gate 	VTOF(vp2)->fn_flag &= ~FIFOOPEN;
10355331Samw 	(void) fifo_close(vp2, flag, 1, (offset_t)0, c, NULL);
10360Sstevel@tonic-gate 	crfree(c);
10370Sstevel@tonic-gate 	VN_RELE(vp2);
10380Sstevel@tonic-gate 	return (error);
10390Sstevel@tonic-gate }
10400Sstevel@tonic-gate 
10410Sstevel@tonic-gate /*
10420Sstevel@tonic-gate  * Disable fastpath mode.
10430Sstevel@tonic-gate  */
10440Sstevel@tonic-gate void
fifo_fastoff(fifonode_t * fnp)10450Sstevel@tonic-gate fifo_fastoff(fifonode_t *fnp)
10460Sstevel@tonic-gate {
10470Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&fnp->fn_lock->flk_lock));
10480Sstevel@tonic-gate 	ASSERT(FTOV(fnp)->v_stream);
10490Sstevel@tonic-gate 
10502069Swroche 	/* FIFOSTAYFAST is set => FIFOFAST is set */
10512069Swroche 	while ((fnp->fn_flag & FIFOSTAYFAST) || ((fnp->fn_flag & ISPIPE) &&
10522069Swroche 	    (fnp->fn_dest->fn_flag & FIFOSTAYFAST))) {
10532069Swroche 		ASSERT(fnp->fn_flag & FIFOFAST);
10542069Swroche 		/* indicate someone is waiting to turn into stream mode */
10552069Swroche 		fnp->fn_flag |= FIFOWAITMODE;
10562069Swroche 		cv_wait(&fnp->fn_wait_cv, &fnp->fn_lock->flk_lock);
10572069Swroche 		fnp->fn_flag &= ~FIFOWAITMODE;
10582069Swroche 	}
10590Sstevel@tonic-gate 
10602069Swroche 	/* as we may have relased the lock, test the FIFOFAST flag here */
10610Sstevel@tonic-gate 	if (!(fnp->fn_flag & FIFOFAST))
10620Sstevel@tonic-gate 		return;
10630Sstevel@tonic-gate #if FIFODEBUG
10640Sstevel@tonic-gate 	if (Fifo_verbose)
10650Sstevel@tonic-gate 		cmn_err(CE_NOTE, "Fifo reverting to streams mode\n");
10660Sstevel@tonic-gate #endif
10670Sstevel@tonic-gate 
10680Sstevel@tonic-gate 	fifo_fastturnoff(fnp);
10690Sstevel@tonic-gate 	if (fnp->fn_flag & ISPIPE) {
10700Sstevel@tonic-gate 		fifo_fastturnoff(fnp->fn_dest);
10710Sstevel@tonic-gate 	}
10720Sstevel@tonic-gate }
10730Sstevel@tonic-gate 
10740Sstevel@tonic-gate 
10750Sstevel@tonic-gate /*
10760Sstevel@tonic-gate  * flk_lock must be held while calling fifo_fastturnoff() to
10770Sstevel@tonic-gate  * preserve data ordering (no reads or writes allowed)
10780Sstevel@tonic-gate  */
10790Sstevel@tonic-gate 
10800Sstevel@tonic-gate static void
fifo_fastturnoff(fifonode_t * fnp)10810Sstevel@tonic-gate fifo_fastturnoff(fifonode_t *fnp)
10820Sstevel@tonic-gate {
10830Sstevel@tonic-gate 	fifonode_t *fn_dest = fnp->fn_dest;
10840Sstevel@tonic-gate 	mblk_t	*fn_mp;
10850Sstevel@tonic-gate 	int	fn_flag;
10860Sstevel@tonic-gate 
10870Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&fnp->fn_lock->flk_lock));
10880Sstevel@tonic-gate 	/*
10890Sstevel@tonic-gate 	 * Note: This end can't be closed if there
10900Sstevel@tonic-gate 	 * is stuff in fn_mp
10910Sstevel@tonic-gate 	 */
10920Sstevel@tonic-gate 	if ((fn_mp = fnp->fn_mp) != NULL) {
10930Sstevel@tonic-gate 		ASSERT(fnp->fn_flag & FIFOISOPEN);
10940Sstevel@tonic-gate 		ASSERT(FTOV(fnp)->v_stream != NULL);
10950Sstevel@tonic-gate 		ASSERT(FTOV(fnp)->v_stream->sd_wrq != NULL);
10960Sstevel@tonic-gate 		ASSERT(RD(FTOV(fnp)->v_stream->sd_wrq) != NULL);
10970Sstevel@tonic-gate 		ASSERT(strvp2wq(FTOV(fnp)) != NULL);
10980Sstevel@tonic-gate 		fnp->fn_mp = NULL;
10990Sstevel@tonic-gate 		fnp->fn_count = 0;
11000Sstevel@tonic-gate 		/*
11010Sstevel@tonic-gate 		 * Don't need to drop flk_lock across the put()
11020Sstevel@tonic-gate 		 * since we're just moving the message from the fifo
11030Sstevel@tonic-gate 		 * node to the STREAM head...
11040Sstevel@tonic-gate 		 */
11050Sstevel@tonic-gate 		put(RD(strvp2wq(FTOV(fnp))), fn_mp);
11060Sstevel@tonic-gate 	}
11070Sstevel@tonic-gate 
11080Sstevel@tonic-gate 	/*
11090Sstevel@tonic-gate 	 * Need to re-issue any pending poll requests
11100Sstevel@tonic-gate 	 * so that the STREAMS framework sees them
11110Sstevel@tonic-gate 	 * Writers would be waiting on fnp and readers on fn_dest
11120Sstevel@tonic-gate 	 */
11130Sstevel@tonic-gate 	if ((fnp->fn_flag & (FIFOISOPEN | FIFOPOLLW)) ==
11140Sstevel@tonic-gate 	    (FIFOISOPEN | FIFOPOLLW)) {
11150Sstevel@tonic-gate 		strpollwakeup(FTOV(fnp), POLLWRNORM);
11160Sstevel@tonic-gate 	}
11170Sstevel@tonic-gate 	fn_flag = fn_dest->fn_flag;
11180Sstevel@tonic-gate 	if ((fn_flag & FIFOISOPEN) == FIFOISOPEN) {
11190Sstevel@tonic-gate 		if ((fn_flag & (FIFOPOLLR | FIFOPOLLRBAND))) {
11200Sstevel@tonic-gate 			strpollwakeup(FTOV(fn_dest), POLLIN|POLLRDNORM);
11210Sstevel@tonic-gate 		}
11220Sstevel@tonic-gate 	}
11230Sstevel@tonic-gate 	/*
11240Sstevel@tonic-gate 	 * wake up any sleeping processes so they can notice we went
11250Sstevel@tonic-gate 	 * to streams mode
11260Sstevel@tonic-gate 	 */
11270Sstevel@tonic-gate 	fnp->fn_flag &= ~(FIFOFAST|FIFOWANTW|FIFOWANTR);
11280Sstevel@tonic-gate 	cv_broadcast(&fnp->fn_wait_cv);
11290Sstevel@tonic-gate }
11300Sstevel@tonic-gate 
11310Sstevel@tonic-gate /*
11320Sstevel@tonic-gate  * Alternative version of fifo_fastoff()
11330Sstevel@tonic-gate  * optimized for putmsg/getmsg.
11340Sstevel@tonic-gate  */
11350Sstevel@tonic-gate void
fifo_vfastoff(vnode_t * vp)11360Sstevel@tonic-gate fifo_vfastoff(vnode_t *vp)
11370Sstevel@tonic-gate {
11380Sstevel@tonic-gate 	fifonode_t	*fnp = VTOF(vp);
11390Sstevel@tonic-gate 
11400Sstevel@tonic-gate 	mutex_enter(&fnp->fn_lock->flk_lock);
11410Sstevel@tonic-gate 	if (!(fnp->fn_flag & FIFOFAST)) {
11420Sstevel@tonic-gate 		mutex_exit(&fnp->fn_lock->flk_lock);
11430Sstevel@tonic-gate 		return;
11440Sstevel@tonic-gate 	}
11450Sstevel@tonic-gate 	fifo_fastoff(fnp);
11460Sstevel@tonic-gate 	mutex_exit(&fnp->fn_lock->flk_lock);
11470Sstevel@tonic-gate }
11480Sstevel@tonic-gate 
11490Sstevel@tonic-gate /*
11500Sstevel@tonic-gate  * Wake any sleeping writers, poll and send signals if necessary
11510Sstevel@tonic-gate  * This module is only called when we drop below the hi water mark
11520Sstevel@tonic-gate  * FIFOWANTW indicates that a process is sleeping in fifo_write()
11530Sstevel@tonic-gate  * FIFOHIWATW indicates that we have either attempted a poll or
11540Sstevel@tonic-gate  * non-blocking write and were over the high water mark
11550Sstevel@tonic-gate  * This routine assumes a low water mark of 0.
11560Sstevel@tonic-gate  */
11570Sstevel@tonic-gate 
11580Sstevel@tonic-gate void
fifo_wakewriter(fifonode_t * fn_dest,fifolock_t * fn_lock)11590Sstevel@tonic-gate fifo_wakewriter(fifonode_t *fn_dest, fifolock_t *fn_lock)
11600Sstevel@tonic-gate {
11610Sstevel@tonic-gate 	int fn_dflag = fn_dest->fn_flag;
11620Sstevel@tonic-gate 
11630Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&fn_lock->flk_lock));
11640Sstevel@tonic-gate 	ASSERT(fn_dest->fn_dest->fn_count < Fifohiwat);
11650Sstevel@tonic-gate 	if ((fn_dflag & FIFOWANTW)) {
11660Sstevel@tonic-gate 		cv_broadcast(&fn_dest->fn_wait_cv);
11670Sstevel@tonic-gate 	}
11680Sstevel@tonic-gate 	if ((fn_dflag & (FIFOHIWATW | FIFOISOPEN)) ==
11690Sstevel@tonic-gate 	    (FIFOHIWATW | FIFOISOPEN)) {
11700Sstevel@tonic-gate 		if (fn_dflag & FIFOPOLLW)
11710Sstevel@tonic-gate 			strpollwakeup(FTOV(fn_dest), POLLWRNORM);
11720Sstevel@tonic-gate 		if (fn_dflag & FIFOSETSIG)
11730Sstevel@tonic-gate 			str_sendsig(FTOV(fn_dest), S_WRNORM, 0, 0);
11740Sstevel@tonic-gate 	}
11750Sstevel@tonic-gate 	/*
11760Sstevel@tonic-gate 	 * FIFOPOLLW can't be set without setting FIFOHIWAT
11770Sstevel@tonic-gate 	 * This allows us to clear both here.
11780Sstevel@tonic-gate 	 */
11790Sstevel@tonic-gate 	fn_dest->fn_flag = fn_dflag & ~(FIFOWANTW | FIFOHIWATW | FIFOPOLLW);
11800Sstevel@tonic-gate }
11810Sstevel@tonic-gate 
11820Sstevel@tonic-gate /*
11830Sstevel@tonic-gate  * wake up any sleeping readers, poll or send signal if needed
11840Sstevel@tonic-gate  * FIFOWANTR indicates that a process is waiting in fifo_read() for data
11850Sstevel@tonic-gate  * FIFOSETSIG indicates that SIGPOLL should be sent to process
11860Sstevel@tonic-gate  * FIFOPOLLR indicates that a poll request for reading on the fifo was made
11870Sstevel@tonic-gate  */
11880Sstevel@tonic-gate 
11890Sstevel@tonic-gate void
fifo_wakereader(fifonode_t * fn_dest,fifolock_t * fn_lock)11900Sstevel@tonic-gate fifo_wakereader(fifonode_t *fn_dest, fifolock_t *fn_lock)
11910Sstevel@tonic-gate {
11920Sstevel@tonic-gate 	int fn_dflag = fn_dest->fn_flag;
11930Sstevel@tonic-gate 
11940Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&fn_lock->flk_lock));
11950Sstevel@tonic-gate 	if (fn_dflag & FIFOWANTR) {
11960Sstevel@tonic-gate 		cv_broadcast(&fn_dest->fn_wait_cv);
11970Sstevel@tonic-gate 	}
11980Sstevel@tonic-gate 	if (fn_dflag & FIFOISOPEN) {
11990Sstevel@tonic-gate 		if (fn_dflag & FIFOPOLLR)
12000Sstevel@tonic-gate 			strpollwakeup(FTOV(fn_dest), POLLIN | POLLRDNORM);
12010Sstevel@tonic-gate 		if (fn_dflag & FIFOSETSIG)
12020Sstevel@tonic-gate 			str_sendsig(FTOV(fn_dest), S_INPUT | S_RDNORM, 0, 0);
12030Sstevel@tonic-gate 	}
12040Sstevel@tonic-gate 	fn_dest->fn_flag = fn_dflag & ~(FIFOWANTR | FIFOPOLLR);
12050Sstevel@tonic-gate }
1206