xref: /titanic_50/usr/src/uts/common/fs/fifofs/fifosubr.c (revision 79659ce54ea319c9c890f6dd5dbeb1146d725cb9)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
22 
23 /*
24  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 
28 #pragma ident	"%Z%%M%	%I%	%E% SMI"
29 
30 /*
31  * The routines defined in this file are supporting routines for FIFOFS
32  * file system type.
33  */
34 #include <sys/types.h>
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/debug.h>
38 #include <sys/errno.h>
39 #include <sys/time.h>
40 #include <sys/kmem.h>
41 #include <sys/inline.h>
42 #include <sys/file.h>
43 #include <sys/proc.h>
44 #include <sys/stat.h>
45 #include <sys/sysmacros.h>
46 #include <sys/var.h>
47 #include <sys/vfs.h>
48 #include <sys/vfs_opreg.h>
49 #include <sys/vnode.h>
50 #include <sys/mode.h>
51 #include <sys/signal.h>
52 #include <sys/user.h>
53 #include <sys/uio.h>
54 #include <sys/flock.h>
55 #include <sys/stream.h>
56 #include <sys/fs/fifonode.h>
57 #include <sys/strsubr.h>
58 #include <sys/stropts.h>
59 #include <sys/cmn_err.h>
60 #include <fs/fs_subr.h>
61 #include <sys/ddi.h>
62 
63 
64 #if FIFODEBUG
65 int Fifo_fastmode = 1;		/* pipes/fifos will be opened in fast mode */
66 int Fifo_verbose = 0;		/* msg when switching out of fast mode */
67 int Fifohiwat = FIFOHIWAT;	/* Modifiable FIFO high water mark */
68 #endif
69 
70 /*
71  * This is the loadable module wrapper.
72  */
73 #include <sys/modctl.h>
74 
75 extern struct qinit fifo_strdata;
76 
77 struct vfsops *fifo_vfsops;
78 
79 static vfsdef_t vfw = {
80 	VFSDEF_VERSION,
81 	"fifofs",
82 	fifoinit,
83 	0,
84 	NULL
85 };
86 
87 /*
88  * Module linkage information for the kernel.
89  */
90 extern struct mod_ops mod_fsops;
91 
92 static struct modlfs modlfs = {
93 	&mod_fsops, "filesystem for fifo", &vfw
94 };
95 
96 static struct modlinkage modlinkage = {
97 	MODREV_1, (void *)&modlfs, NULL
98 };
99 
100 int
101 _init()
102 {
103 	return (mod_install(&modlinkage));
104 }
105 
106 int
107 _info(struct modinfo *modinfop)
108 {
109 	return (mod_info(&modlinkage, modinfop));
110 }
111 
112 /*
113  * Define data structures within this file.
114  * XXX should the hash size be configurable ?
115  */
116 #define	FIFOSHFT	5
117 #define	FIFO_HASHSZ	63
118 
119 #if ((FIFO_HASHSZ & (FIFO_HASHSZ - 1)) == 0)
120 #define	FIFOHASH(vp) (((uintptr_t)(vp) >> FIFOSHFT) & (FIFO_HASHSZ - 1))
121 #else
122 #define	FIFOHASH(vp) (((uintptr_t)(vp) >> FIFOSHFT) % FIFO_HASHSZ)
123 #endif
124 
125 fifonode_t	*fifoalloc[FIFO_HASHSZ];
126 dev_t		fifodev;
127 struct vfs	*fifovfsp;
128 int		fifofstype;
129 
130 kmutex_t ftable_lock;
131 static kmutex_t fino_lock;
132 struct kmem_cache *fnode_cache;
133 struct kmem_cache *pipe_cache;
134 
135 static void fifoinsert(fifonode_t *);
136 static fifonode_t *fifofind(vnode_t *);
137 static int fifo_connld(struct vnode **, int, cred_t *);
138 static void fifo_fastturnoff(fifonode_t *);
139 
140 static void fifo_reinit_vp(vnode_t *);
141 
142 static void fnode_destructor(void *, void *);
143 
144 /*
145  * Constructor/destructor routines for fifos and pipes.
146  *
147  * In the interest of code sharing, we define a common fifodata structure
148  * which consists of a fifolock and one or two fnodes.  A fifo contains
149  * one fnode; a pipe contains two.  The fifolock is shared by the fnodes,
150  * each of which points to it:
151  *
152  *	--> -->	---------  --- ---
153  *	|   |	| lock	|   |	|
154  *	|   |	---------   |	|
155  *	|   |	|	|  fifo	|
156  *	|   --- | fnode	|   |	|
157  *	|	|	|   |  pipe
158  *	|	---------  ---	|
159  *	|	|	|	|
160  *	------- | fnode	|	|
161  *		|	|	|
162  *		---------      ---
163  *
164  * Since the fifolock is at the beginning of the fifodata structure,
165  * the fifolock address is the same as the fifodata address.  Thus,
166  * we can determine the fifodata address from any of its member fnodes.
167  * This is essential for fifo_inactive.
168  *
169  * The fnode constructor is designed to handle any fifodata structure,
170  * deducing the number of fnodes from the total size.  Thus, the fnode
171  * constructor does most of the work for the pipe constructor.
172  */
173 static int
174 fnode_constructor(void *buf, void *cdrarg, int kmflags)
175 {
176 	fifodata_t *fdp = buf;
177 	fifolock_t *flp = &fdp->fifo_lock;
178 	fifonode_t *fnp = &fdp->fifo_fnode[0];
179 	size_t size = (uintptr_t)cdrarg;
180 
181 	mutex_init(&flp->flk_lock, NULL, MUTEX_DEFAULT, NULL);
182 	cv_init(&flp->flk_wait_cv, NULL, CV_DEFAULT, NULL);
183 	flp->flk_ocsync = 0;
184 
185 	while ((char *)fnp < (char *)buf + size) {
186 
187 		vnode_t *vp;
188 
189 		vp = vn_alloc(kmflags);
190 		if (vp == NULL) {
191 			fnp->fn_vnode = NULL; /* mark for destructor */
192 			fnode_destructor(buf, cdrarg);
193 			return (-1);
194 		}
195 		fnp->fn_vnode = vp;
196 
197 		fnp->fn_lock = flp;
198 		fnp->fn_open = 0;
199 		fnp->fn_dest = fnp;
200 		fnp->fn_mp = NULL;
201 		fnp->fn_count = 0;
202 		fnp->fn_rsynccnt = 0;
203 		fnp->fn_wsynccnt = 0;
204 		fnp->fn_wwaitcnt = 0;
205 		fnp->fn_insync = 0;
206 		fnp->fn_pcredp = NULL;
207 		fnp->fn_cpid = -1;
208 		/*
209 		 * 32-bit stat(2) may fail if fn_ino isn't initialized
210 		 */
211 		fnp->fn_ino = 0;
212 
213 		cv_init(&fnp->fn_wait_cv, NULL, CV_DEFAULT, NULL);
214 
215 		vn_setops(vp, fifo_vnodeops);
216 		vp->v_stream = NULL;
217 		vp->v_type = VFIFO;
218 		vp->v_data = (caddr_t)fnp;
219 		vp->v_flag = VNOMAP | VNOSWAP;
220 		vn_exists(vp);
221 		fnp++;
222 	}
223 	return (0);
224 }
225 
226 static void
227 fnode_destructor(void *buf, void *cdrarg)
228 {
229 	fifodata_t *fdp = buf;
230 	fifolock_t *flp = &fdp->fifo_lock;
231 	fifonode_t *fnp = &fdp->fifo_fnode[0];
232 	size_t size = (uintptr_t)cdrarg;
233 
234 	mutex_destroy(&flp->flk_lock);
235 	cv_destroy(&flp->flk_wait_cv);
236 	ASSERT(flp->flk_ocsync == 0);
237 
238 	while ((char *)fnp < (char *)buf + size) {
239 
240 		vnode_t *vp = FTOV(fnp);
241 
242 		if (vp == NULL) {
243 			return; /* constructor failed here */
244 		}
245 
246 		ASSERT(fnp->fn_mp == NULL);
247 		ASSERT(fnp->fn_count == 0);
248 		ASSERT(fnp->fn_lock == flp);
249 		ASSERT(fnp->fn_open == 0);
250 		ASSERT(fnp->fn_insync == 0);
251 		ASSERT(fnp->fn_rsynccnt == 0 && fnp->fn_wsynccnt == 0);
252 		ASSERT(fnp->fn_wwaitcnt == 0);
253 		ASSERT(fnp->fn_pcredp == NULL);
254 		ASSERT(vn_matchops(vp, fifo_vnodeops));
255 		ASSERT(vp->v_stream == NULL);
256 		ASSERT(vp->v_type == VFIFO);
257 		ASSERT(vp->v_data == (caddr_t)fnp);
258 		ASSERT((vp->v_flag & (VNOMAP|VNOSWAP)) == (VNOMAP|VNOSWAP));
259 
260 		cv_destroy(&fnp->fn_wait_cv);
261 		vn_invalid(vp);
262 		vn_free(vp);
263 
264 		fnp++;
265 	}
266 }
267 
268 static int
269 pipe_constructor(void *buf, void *cdrarg, int kmflags)
270 {
271 	fifodata_t *fdp = buf;
272 	fifonode_t *fnp1 = &fdp->fifo_fnode[0];
273 	fifonode_t *fnp2 = &fdp->fifo_fnode[1];
274 	vnode_t *vp1;
275 	vnode_t *vp2;
276 
277 	(void) fnode_constructor(buf, cdrarg, kmflags);
278 
279 	vp1 = FTOV(fnp1);
280 	vp2 = FTOV(fnp2);
281 
282 	vp1->v_vfsp	= vp2->v_vfsp		= fifovfsp;
283 	vp1->v_rdev	= vp2->v_rdev		= fifodev;
284 	fnp1->fn_realvp	= fnp2->fn_realvp	= NULL;
285 	fnp1->fn_dest	= fnp2;
286 	fnp2->fn_dest	= fnp1;
287 
288 	return (0);
289 }
290 
291 static void
292 pipe_destructor(void *buf, void *cdrarg)
293 {
294 #ifdef DEBUG
295 	fifodata_t *fdp = buf;
296 	fifonode_t *fnp1 = &fdp->fifo_fnode[0];
297 	fifonode_t *fnp2 = &fdp->fifo_fnode[1];
298 	vnode_t *vp1 = FTOV(fnp1);
299 	vnode_t *vp2 = FTOV(fnp2);
300 
301 	ASSERT(vp1->v_vfsp == fifovfsp);
302 	ASSERT(vp2->v_vfsp == fifovfsp);
303 	ASSERT(vp1->v_rdev == fifodev);
304 	ASSERT(vp2->v_rdev == fifodev);
305 #endif
306 	fnode_destructor(buf, cdrarg);
307 }
308 
309 /*
310  * Reinitialize a FIFO vnode (uses normal vnode reinit, but ensures that
311  * vnode type and flags are reset).
312  */
313 
314 static void fifo_reinit_vp(vnode_t *vp)
315 {
316 	vn_reinit(vp);
317 	vp->v_type = VFIFO;
318 	vp->v_flag &= VROOT;
319 	vp->v_flag |= VNOMAP | VNOSWAP;
320 }
321 
322 /*
323  * Save file system type/index, initialize vfs operations vector, get
324  * unique device number for FIFOFS and initialize the FIFOFS hash.
325  * Create and initialize a "generic" vfs pointer that will be placed
326  * in the v_vfsp field of each pipe's vnode.
327  */
328 int
329 fifoinit(int fstype, char *name)
330 {
331 	static const fs_operation_def_t fifo_vfsops_template[] = {
332 		NULL, NULL
333 	};
334 	int error;
335 	major_t dev;
336 
337 	fifofstype = fstype;
338 	error = vfs_setfsops(fstype, fifo_vfsops_template, &fifo_vfsops);
339 	if (error != 0) {
340 		cmn_err(CE_WARN, "fifoinit: bad vfs ops template");
341 		return (error);
342 	}
343 
344 	error = vn_make_ops(name, fifo_vnodeops_template, &fifo_vnodeops);
345 	if (error != 0) {
346 		(void) vfs_freevfsops_by_type(fstype);
347 		cmn_err(CE_WARN, "fifoinit: bad vnode ops template");
348 		return (error);
349 	}
350 
351 	if ((dev = getudev()) == (major_t)-1) {
352 		cmn_err(CE_WARN, "fifoinit: can't get unique device number");
353 		dev = 0;
354 	}
355 	fifodev = makedevice(dev, 0);
356 
357 	fifovfsp = kmem_zalloc(sizeof (struct vfs), KM_SLEEP);
358 	fifovfsp->vfs_next = NULL;
359 	vfs_setops(fifovfsp, fifo_vfsops);
360 	fifovfsp->vfs_vnodecovered = NULL;
361 	fifovfsp->vfs_flag = 0;
362 	fifovfsp->vfs_bsize = 1024;
363 	fifovfsp->vfs_fstype = fifofstype;
364 	vfs_make_fsid(&fifovfsp->vfs_fsid, fifodev, fifofstype);
365 	fifovfsp->vfs_data = NULL;
366 	fifovfsp->vfs_dev = fifodev;
367 	fifovfsp->vfs_bcount = 0;
368 
369 	mutex_init(&ftable_lock, NULL, MUTEX_DEFAULT, NULL);
370 	mutex_init(&fino_lock, NULL, MUTEX_DEFAULT, NULL);
371 
372 	/*
373 	 * vnodes are cached aligned
374 	 */
375 	fnode_cache = kmem_cache_create("fnode_cache",
376 	    sizeof (fifodata_t) - sizeof (fifonode_t), 32,
377 	    fnode_constructor, fnode_destructor, NULL,
378 	    (void *)(sizeof (fifodata_t) - sizeof (fifonode_t)), NULL, 0);
379 
380 	pipe_cache = kmem_cache_create("pipe_cache", sizeof (fifodata_t), 32,
381 	    pipe_constructor, pipe_destructor, NULL,
382 	    (void *)(sizeof (fifodata_t)), NULL, 0);
383 
384 #if FIFODEBUG
385 	if (Fifohiwat < FIFOHIWAT)
386 		Fifohiwat = FIFOHIWAT;
387 #endif /* FIFODEBUG */
388 	fifo_strdata.qi_minfo->mi_hiwat = Fifohiwat;
389 
390 	return (0);
391 }
392 
393 /*
394  * Provide a shadow for a vnode.  We create a new shadow before checking for an
395  * existing one, to minimize the amount of time we need to hold ftable_lock.
396  * If a vp already has a shadow in the hash list, return its shadow.  If not,
397  * we hash the new vnode and return its pointer to the caller.
398  */
399 vnode_t *
400 fifovp(vnode_t *vp, cred_t *crp)
401 {
402 	fifonode_t *fnp;
403 	fifonode_t *spec_fnp;   /* Speculative fnode ptr. */
404 	fifodata_t *fdp;
405 	vnode_t *newvp;
406 	struct vattr va;
407 	vnode_t	*rvp;
408 
409 	ASSERT(vp != NULL);
410 
411 	fdp = kmem_cache_alloc(fnode_cache, KM_SLEEP);
412 
413 	fdp->fifo_lock.flk_ref = 1;
414 	fnp = &fdp->fifo_fnode[0];
415 
416 	/*
417 	 * Its possible that fifo nodes on different lofs mountpoints
418 	 * shadow the same real filesystem fifo node.
419 	 * In this case its necessary to get and store the realvp.
420 	 * This way different fifo nodes sharing the same real vnode
421 	 * can use realvp for communication.
422 	 */
423 
424 	if (VOP_REALVP(vp, &rvp, NULL) == 0)
425 			vp = rvp;
426 
427 	fnp->fn_realvp	= vp;
428 	fnp->fn_wcnt	= 0;
429 	fnp->fn_rcnt	= 0;
430 
431 #if FIFODEBUG
432 	if (! Fifo_fastmode) {
433 		fnp->fn_flag	= 0;
434 	} else {
435 		fnp->fn_flag	= FIFOFAST;
436 	}
437 #else /* FIFODEBUG */
438 	fnp->fn_flag	= FIFOFAST;
439 #endif /* FIFODEBUG */
440 
441 	/*
442 	 * initialize the times from vp.
443 	 */
444 	va.va_mask = AT_TIMES;
445 	if (VOP_GETATTR(vp, &va, 0, crp, NULL) == 0) {
446 		fnp->fn_atime = va.va_atime.tv_sec;
447 		fnp->fn_mtime = va.va_mtime.tv_sec;
448 		fnp->fn_ctime = va.va_ctime.tv_sec;
449 	} else {
450 		fnp->fn_atime = 0;
451 		fnp->fn_mtime = 0;
452 		fnp->fn_ctime = 0;
453 	}
454 
455 	/*
456 	 * Grab the VP here to avoid holding locks
457 	 * whilst trying to acquire others.
458 	 */
459 
460 	VN_HOLD(vp);
461 
462 	mutex_enter(&ftable_lock);
463 
464 	if ((spec_fnp = fifofind(vp)) != NULL) {
465 		mutex_exit(&ftable_lock);
466 
467 		/*
468 		 * Release the vnode and free up our pre-prepared fnode.
469 		 * Zero the lock reference just to explicitly signal
470 		 * this is unused.
471 		 */
472 		VN_RELE(vp);
473 		fdp->fifo_lock.flk_ref = 0;
474 		kmem_cache_free(fnode_cache, fdp);
475 
476 		return (FTOV(spec_fnp));
477 	}
478 
479 	newvp = FTOV(fnp);
480 	fifo_reinit_vp(newvp);
481 	/*
482 	 * Since the fifo vnode's v_vfsp needs to point to the
483 	 * underlying filesystem's vfsp we need to bump up the
484 	 * underlying filesystem's vfs reference count.
485 	 * The count is decremented when the fifo node is
486 	 * inactivated.
487 	 */
488 
489 	VFS_HOLD(vp->v_vfsp);
490 	newvp->v_vfsp = vp->v_vfsp;
491 	newvp->v_rdev = vp->v_rdev;
492 	newvp->v_flag |= (vp->v_flag & VROOT);
493 
494 	fifoinsert(fnp);
495 	mutex_exit(&ftable_lock);
496 
497 	return (newvp);
498 }
499 
500 /*
501  * Create a pipe end by...
502  * allocating a vnode-fifonode pair and initializing the fifonode.
503  */
504 void
505 makepipe(vnode_t **vpp1, vnode_t **vpp2)
506 {
507 	fifonode_t *fnp1;
508 	fifonode_t *fnp2;
509 	vnode_t *nvp1;
510 	vnode_t *nvp2;
511 	fifodata_t *fdp;
512 	time_t now;
513 
514 	fdp = kmem_cache_alloc(pipe_cache, KM_SLEEP);
515 	fdp->fifo_lock.flk_ref = 2;
516 	fnp1 = &fdp->fifo_fnode[0];
517 	fnp2 = &fdp->fifo_fnode[1];
518 
519 	fnp1->fn_wcnt	= fnp2->fn_wcnt		= 1;
520 	fnp1->fn_rcnt	= fnp2->fn_rcnt		= 1;
521 #if FIFODEBUG
522 	if (! Fifo_fastmode) {
523 		fnp1->fn_flag	= fnp2->fn_flag		= ISPIPE;
524 	} else {
525 		fnp1->fn_flag	= fnp2->fn_flag		= ISPIPE | FIFOFAST;
526 	}
527 #else /* FIFODEBUG */
528 	fnp1->fn_flag	= fnp2->fn_flag		= ISPIPE | FIFOFAST;
529 #endif /* FIFODEBUG */
530 	now = gethrestime_sec();
531 	fnp1->fn_atime	= fnp2->fn_atime	= now;
532 	fnp1->fn_mtime	= fnp2->fn_mtime	= now;
533 	fnp1->fn_ctime	= fnp2->fn_ctime	= now;
534 
535 	*vpp1 = nvp1 = FTOV(fnp1);
536 	*vpp2 = nvp2 = FTOV(fnp2);
537 
538 	fifo_reinit_vp(nvp1);		/* Reinitialize vnodes for reuse... */
539 	fifo_reinit_vp(nvp2);
540 	nvp1->v_vfsp = fifovfsp; 	/* Need to re-establish VFS & device */
541 	nvp2->v_vfsp = fifovfsp; 	/* before we can reuse this vnode. */
542 	nvp1->v_rdev = fifodev;
543 	nvp2->v_rdev = fifodev;
544 }
545 
546 /*
547  * Attempt to establish a unique pipe id.  Only un-named pipes use this
548  * routine.
549  */
550 ino_t
551 fifogetid(void)
552 {
553 	static ino_t fifo_ino = 0;
554 	ino_t fino;
555 
556 	mutex_enter(&fino_lock);
557 	fino = fifo_ino++;
558 	mutex_exit(&fino_lock);
559 	return (fino);
560 }
561 
562 
563 /*
564  * Stream a pipe/FIFO.
565  * The FIFOCONNLD flag is used when CONNLD has been pushed on the stream.
566  * If the flag is set, a new vnode is created by calling fifo_connld().
567  * Connld logic was moved to fifo_connld() to speed up the open
568  * operation, simplify the connld/fifo interaction, and remove inherent
569  * race conditions between the connld module and fifos.
570  * This routine is single threaded for two reasons.
571  * 1) connld requests are synchronous; that is, they must block
572  *    until the server does an I_RECVFD (oh, well).  Single threading is
573  *    the simplest way to accomplish this.
574  * 2) fifo_close() must not send M_HANGUP or M_ERROR while we are
575  *    in stropen. Stropen() has a tendency to reset things and
576  *    we would like streams to remember that a hangup occurred.
577  */
578 int
579 fifo_stropen(vnode_t **vpp, int flag, cred_t *crp, int dotwist, int lockheld)
580 {
581 	int error = 0;
582 	vnode_t *oldvp = *vpp;
583 	fifonode_t *fnp = VTOF(*vpp);
584 	dev_t pdev = 0;
585 	int firstopen = 0;
586 	fifolock_t *fn_lock;
587 
588 	fn_lock = fnp->fn_lock;
589 	if (!lockheld)
590 		mutex_enter(&fn_lock->flk_lock);
591 	ASSERT(MUTEX_HELD(&fnp->fn_lock->flk_lock));
592 
593 	/*
594 	 * FIFO is in the process of opening. Wait for it
595 	 * to complete before starting another open on it
596 	 * This prevents races associated with connld open
597 	 */
598 	while (fnp->fn_flag & FIFOOPEN) {
599 		if (!cv_wait_sig(&fnp->fn_wait_cv, &fn_lock->flk_lock)) {
600 			fifo_cleanup(oldvp, flag);
601 			if (!lockheld)
602 				mutex_exit(&fn_lock->flk_lock);
603 			return (EINTR);
604 		}
605 	}
606 
607 	/*
608 	 * The other end of the pipe is almost closed so
609 	 * reject any other open on this end of the pipe
610 	 * This only happens with a pipe mounted under namefs
611 	 */
612 	if ((fnp->fn_flag & (FIFOCLOSE|ISPIPE)) == (FIFOCLOSE|ISPIPE)) {
613 		fifo_cleanup(oldvp, flag);
614 		cv_broadcast(&fnp->fn_wait_cv);
615 		if (!lockheld)
616 			mutex_exit(&fn_lock->flk_lock);
617 		return (ENXIO);
618 	}
619 
620 	fnp->fn_flag |= FIFOOPEN;
621 
622 	/*
623 	 * can't allow close to happen while we are
624 	 * in the middle of stropen().
625 	 * M_HANGUP and M_ERROR could leave the stream in a strange state
626 	 */
627 	while (fn_lock->flk_ocsync)
628 		cv_wait(&fn_lock->flk_wait_cv, &fn_lock->flk_lock);
629 
630 	fn_lock->flk_ocsync = 1;
631 
632 	if (fnp->fn_flag & FIFOCONNLD) {
633 		/*
634 		 * This is a reopen, so we should release the fifo lock
635 		 * just in case some strange module pushed on connld
636 		 * has some odd side effect.
637 		 * Note: this stropen is on the oldvp.  It will
638 		 * have no impact on the connld vp returned and
639 		 * strclose() will only be called when we release
640 		 * flk_ocsync
641 		 */
642 		mutex_exit(&fn_lock->flk_lock);
643 		if ((error = stropen(oldvp, &pdev, flag, crp)) != 0) {
644 			mutex_enter(&fn_lock->flk_lock);
645 			fifo_cleanup(oldvp, flag);
646 			fn_lock->flk_ocsync = 0;
647 			cv_broadcast(&fn_lock->flk_wait_cv);
648 			goto out;
649 		}
650 		/*
651 		 * streams open done, allow close on other end if
652 		 * required.  Do this now.. it could
653 		 * be a very long time before fifo_connld returns.
654 		 */
655 		mutex_enter(&fn_lock->flk_lock);
656 		/*
657 		 * we need to fake an open here so that if this
658 		 * end of the pipe closes, we don't loose the
659 		 * stream head (kind of like single threading
660 		 * open and close for this end of the pipe)
661 		 * We'll need to call fifo_close() to do clean
662 		 * up in case this end of the pipe was closed
663 		 * down while we were in fifo_connld()
664 		 */
665 		ASSERT(fnp->fn_open > 0);
666 		fnp->fn_open++;
667 		fn_lock->flk_ocsync = 0;
668 		cv_broadcast(&fn_lock->flk_wait_cv);
669 		mutex_exit(&fn_lock->flk_lock);
670 		/*
671 		 * Connld has been pushed onto the pipe
672 		 * Create new pipe on behalf of connld
673 		 */
674 		if (error = fifo_connld(vpp, flag, crp)) {
675 			(void) fifo_close(oldvp, flag, 1, 0, crp, NULL);
676 			mutex_enter(&fn_lock->flk_lock);
677 			goto out;
678 		}
679 		/*
680 		 * undo fake open.  We need to call fifo_close
681 		 * because some other thread could have done
682 		 * a close and detach of the named pipe while
683 		 * we were in fifo_connld(), so
684 		 * we want to make sure the close completes (yuk)
685 		 */
686 		(void) fifo_close(oldvp, flag, 1, 0, crp, NULL);
687 		/*
688 		 * fifo_connld has changed the vp, so we
689 		 * need to re-initialize locals
690 		 */
691 		fnp = VTOF(*vpp);
692 		fn_lock = fnp->fn_lock;
693 		mutex_enter(&fn_lock->flk_lock);
694 	} else {
695 		/*
696 		 * release lock in case there are modules pushed that
697 		 * could have some strange side effect
698 		 */
699 
700 		mutex_exit(&fn_lock->flk_lock);
701 
702 		/*
703 		 * If this is the first open of a fifo (dotwist
704 		 * will be non-zero) we will need to twist the queues.
705 		 */
706 		if (oldvp->v_stream == NULL)
707 			firstopen = 1;
708 
709 
710 		/*
711 		 * normal open of pipe/fifo
712 		 */
713 
714 		if ((error = stropen(oldvp, &pdev, flag, crp)) != 0) {
715 			mutex_enter(&fn_lock->flk_lock);
716 			fifo_cleanup(oldvp, flag);
717 			ASSERT(fnp->fn_open != 0 || oldvp->v_stream == NULL);
718 			fn_lock->flk_ocsync = 0;
719 			cv_broadcast(&fn_lock->flk_wait_cv);
720 			goto out;
721 		}
722 		mutex_enter(&fn_lock->flk_lock);
723 
724 		/*
725 		 * twist the ends of the fifo together
726 		 */
727 		if (dotwist && firstopen)
728 			strmate(*vpp, *vpp);
729 
730 		/*
731 		 * Show that this open has succeeded
732 		 * and allow closes or other opens to proceed
733 		 */
734 		fnp->fn_open++;
735 		fn_lock->flk_ocsync = 0;
736 		cv_broadcast(&fn_lock->flk_wait_cv);
737 	}
738 out:
739 	fnp->fn_flag &= ~FIFOOPEN;
740 	if (error == 0) {
741 		fnp->fn_flag |= FIFOISOPEN;
742 		/*
743 		 * If this is a FIFO and has the close flag set
744 		 * and there are now writers, clear the close flag
745 		 * Note: close flag only gets set when last writer
746 		 * on a FIFO goes away.
747 		 */
748 		if (((fnp->fn_flag & (ISPIPE|FIFOCLOSE)) == FIFOCLOSE) &&
749 		    fnp->fn_wcnt > 0)
750 			fnp->fn_flag &= ~FIFOCLOSE;
751 	}
752 	cv_broadcast(&fnp->fn_wait_cv);
753 	if (!lockheld)
754 		mutex_exit(&fn_lock->flk_lock);
755 	return (error);
756 }
757 
758 /*
759  * Clean up the state of a FIFO and/or mounted pipe in the
760  * event that a fifo_open() was interrupted while the
761  * process was blocked.
762  */
763 void
764 fifo_cleanup(vnode_t *vp, int flag)
765 {
766 	fifonode_t *fnp = VTOF(vp);
767 
768 	ASSERT(MUTEX_HELD(&fnp->fn_lock->flk_lock));
769 
770 	cleanlocks(vp, curproc->p_pid, 0);
771 	cleanshares(vp, curproc->p_pid);
772 	if (flag & FREAD) {
773 		fnp->fn_rcnt--;
774 	}
775 	if (flag & FWRITE) {
776 		fnp->fn_wcnt--;
777 	}
778 	cv_broadcast(&fnp->fn_wait_cv);
779 }
780 
781 
782 /*
783  * Insert a fifonode-vnode pair onto the fifoalloc hash list.
784  */
785 static void
786 fifoinsert(fifonode_t *fnp)
787 {
788 	int idx = FIFOHASH(fnp->fn_realvp);
789 
790 	/*
791 	 * We don't need to hold fn_lock since we're holding ftable_lock and
792 	 * this routine is only called right after we've allocated an fnode.
793 	 * FIFO is inserted at head of NULL terminated doubly linked list.
794 	 */
795 
796 	ASSERT(MUTEX_HELD(&ftable_lock));
797 	fnp->fn_backp = NULL;
798 	fnp->fn_nextp = fifoalloc[idx];
799 	fifoalloc[idx] = fnp;
800 	if (fnp->fn_nextp)
801 		fnp->fn_nextp->fn_backp = fnp;
802 }
803 
804 /*
805  * Find a fifonode-vnode pair on the fifoalloc hash list.
806  * vp is a vnode to be shadowed. If it's on the hash list,
807  * it already has a shadow, therefore return its corresponding
808  * fifonode.
809  */
810 static fifonode_t *
811 fifofind(vnode_t *vp)
812 {
813 	fifonode_t *fnode;
814 
815 	ASSERT(MUTEX_HELD(&ftable_lock));
816 	for (fnode = fifoalloc[FIFOHASH(vp)]; fnode; fnode = fnode->fn_nextp) {
817 		if (fnode->fn_realvp == vp) {
818 			VN_HOLD(FTOV(fnode));
819 			return (fnode);
820 		}
821 	}
822 	return (NULL);
823 }
824 
825 /*
826  * Remove a fifonode-vnode pair from the fifoalloc hash list.
827  * This routine is called from the fifo_inactive() routine when a
828  * FIFO is being released.
829  * If the link to be removed is the only link, set fifoalloc to NULL.
830  */
831 void
832 fiforemove(fifonode_t *fnp)
833 {
834 	int idx = FIFOHASH(fnp->fn_realvp);
835 	fifonode_t *fnode;
836 
837 	ASSERT(MUTEX_HELD(&ftable_lock));
838 	fnode = fifoalloc[idx];
839 	/*
840 	 * fast path... only 1 FIFO in this list entry
841 	 */
842 	if (fnode != NULL && fnode == fnp &&
843 	    !fnode->fn_nextp && !fnode->fn_backp) {
844 		fifoalloc[idx] = NULL;
845 	} else {
846 
847 		for (;  fnode;  fnode = fnode->fn_nextp) {
848 			if (fnode == fnp) {
849 				/*
850 				 * if we are first entry
851 				 */
852 				if (fnp == fifoalloc[idx])
853 					fifoalloc[idx] = fnp->fn_nextp;
854 				if (fnode->fn_nextp)
855 					fnode->fn_nextp->fn_backp =
856 					    fnode->fn_backp;
857 				if (fnode->fn_backp)
858 					fnode->fn_backp->fn_nextp =
859 					    fnode->fn_nextp;
860 				break;
861 			}
862 		}
863 	}
864 }
865 
866 /*
867  * Flush all data from a fifo's message queue
868  */
869 
870 void
871 fifo_fastflush(fifonode_t *fnp)
872 {
873 	mblk_t *bp;
874 	ASSERT(MUTEX_HELD(&fnp->fn_lock->flk_lock));
875 
876 	if ((bp = fnp->fn_mp) != NULL) {
877 		fnp->fn_mp = NULL;
878 		fnp->fn_count = 0;
879 		freemsg(bp);
880 	}
881 	fifo_wakewriter(fnp->fn_dest, fnp->fn_lock);
882 }
883 
884 /*
885  * Note:  This routine is single threaded
886  *  Protected by FIFOOPEN flag (i.e. flk_lock is not held)
887  *  Upon successful completion, the original fifo is unlocked
888  *  and FIFOOPEN is cleared for the original vpp.
889  *  The new fifo returned has FIFOOPEN set.
890  */
891 static int
892 fifo_connld(struct vnode **vpp, int flag, cred_t *crp)
893 {
894 	struct vnode *vp1;
895 	struct vnode *vp2;
896 	struct fifonode *oldfnp;
897 	struct fifonode *fn_dest;
898 	int error;
899 	struct file *filep;
900 	struct fifolock *fn_lock;
901 	cred_t *c;
902 
903 	/*
904 	 * Get two vnodes that will represent the pipe ends for the new pipe.
905 	 */
906 	makepipe(&vp1, &vp2);
907 
908 	/*
909 	 * Allocate a file descriptor and file pointer for one of the pipe
910 	 * ends. The file descriptor will be used to send that pipe end to
911 	 * the process on the other end of this stream. Note that we get
912 	 * the file structure only, there is no file list entry allocated.
913 	 */
914 	if (error = falloc(vp1, FWRITE|FREAD, &filep, NULL)) {
915 		VN_RELE(vp1);
916 		VN_RELE(vp2);
917 		return (error);
918 	}
919 	mutex_exit(&filep->f_tlock);
920 	oldfnp = VTOF(*vpp);
921 	fn_lock = oldfnp->fn_lock;
922 	fn_dest = oldfnp->fn_dest;
923 
924 	/*
925 	 * Create two new stream heads and attach them to the two vnodes for
926 	 * the new pipe.
927 	 */
928 	if ((error = fifo_stropen(&vp1, FREAD|FWRITE, filep->f_cred, 0, 0)) !=
929 	    0 ||
930 	    (error = fifo_stropen(&vp2, flag, filep->f_cred, 0, 0)) != 0) {
931 #if DEBUG
932 		cmn_err(CE_NOTE, "fifo stropen failed error 0x%x", error);
933 #endif
934 		/*
935 		 * this will call fifo_close and VN_RELE on vp1
936 		 */
937 		(void) closef(filep);
938 		VN_RELE(vp2);
939 		return (error);
940 	}
941 
942 	/*
943 	 * twist the ends of the pipe together
944 	 */
945 	strmate(vp1, vp2);
946 
947 	/*
948 	 * Set our end to busy in open
949 	 * Note: Don't need lock around this because we're the only
950 	 * one who knows about it
951 	 */
952 	VTOF(vp2)->fn_flag |= FIFOOPEN;
953 
954 	mutex_enter(&fn_lock->flk_lock);
955 
956 	fn_dest->fn_flag |= FIFOSEND;
957 	/*
958 	 * check to make sure neither end of pipe has gone away
959 	 */
960 	if (!(fn_dest->fn_flag & FIFOISOPEN)) {
961 		error = ENXIO;
962 		fn_dest->fn_flag &= ~FIFOSEND;
963 		mutex_exit(&fn_lock->flk_lock);
964 		/*
965 		 * this will call fifo_close and VN_RELE on vp1
966 		 */
967 		goto out;
968 	}
969 	mutex_exit(&fn_lock->flk_lock);
970 
971 	/*
972 	 * Tag the sender's credential on the pipe descriptor.
973 	 */
974 	crhold(VTOF(vp1)->fn_pcredp = crp);
975 	VTOF(vp1)->fn_cpid = curproc->p_pid;
976 
977 	/*
978 	 * send the file descriptor to other end of pipe
979 	 */
980 	if (error = do_sendfp((*vpp)->v_stream, filep, crp)) {
981 		mutex_enter(&fn_lock->flk_lock);
982 		fn_dest->fn_flag &= ~FIFOSEND;
983 		mutex_exit(&fn_lock->flk_lock);
984 		/*
985 		 * this will call fifo_close and VN_RELE on vp1
986 		 */
987 		goto out;
988 	}
989 
990 	mutex_enter(&fn_lock->flk_lock);
991 	/*
992 	 * Wait for other end to receive file descriptor
993 	 * FIFOCLOSE indicates that one or both sides of the pipe
994 	 * have gone away.
995 	 */
996 	while ((fn_dest->fn_flag & (FIFOCLOSE | FIFOSEND)) == FIFOSEND) {
997 		if (!cv_wait_sig(&oldfnp->fn_wait_cv, &fn_lock->flk_lock)) {
998 			error = EINTR;
999 			fn_dest->fn_flag &= ~FIFOSEND;
1000 			mutex_exit(&fn_lock->flk_lock);
1001 			goto out;
1002 		}
1003 	}
1004 	/*
1005 	 * If either end of pipe has gone away and the other end did not
1006 	 * receive pipe, reject the connld open
1007 	 */
1008 	if ((fn_dest->fn_flag & FIFOSEND)) {
1009 		error = ENXIO;
1010 		fn_dest->fn_flag &= ~FIFOSEND;
1011 		mutex_exit(&fn_lock->flk_lock);
1012 		goto out;
1013 	}
1014 
1015 	oldfnp->fn_flag &= ~FIFOOPEN;
1016 	cv_broadcast(&oldfnp->fn_wait_cv);
1017 	mutex_exit(&fn_lock->flk_lock);
1018 
1019 	VN_RELE(*vpp);
1020 	*vpp = vp2;
1021 	(void) closef(filep);
1022 	return (0);
1023 out:
1024 	c = filep->f_cred;
1025 	crhold(c);
1026 	(void) closef(filep);
1027 	VTOF(vp2)->fn_flag &= ~FIFOOPEN;
1028 	(void) fifo_close(vp2, flag, 1, (offset_t)0, c, NULL);
1029 	crfree(c);
1030 	VN_RELE(vp2);
1031 	return (error);
1032 }
1033 
1034 /*
1035  * Disable fastpath mode.
1036  */
1037 void
1038 fifo_fastoff(fifonode_t *fnp)
1039 {
1040 	ASSERT(MUTEX_HELD(&fnp->fn_lock->flk_lock));
1041 	ASSERT(FTOV(fnp)->v_stream);
1042 
1043 	/* FIFOSTAYFAST is set => FIFOFAST is set */
1044 	while ((fnp->fn_flag & FIFOSTAYFAST) || ((fnp->fn_flag & ISPIPE) &&
1045 	    (fnp->fn_dest->fn_flag & FIFOSTAYFAST))) {
1046 		ASSERT(fnp->fn_flag & FIFOFAST);
1047 		/* indicate someone is waiting to turn into stream mode */
1048 		fnp->fn_flag |= FIFOWAITMODE;
1049 		cv_wait(&fnp->fn_wait_cv, &fnp->fn_lock->flk_lock);
1050 		fnp->fn_flag &= ~FIFOWAITMODE;
1051 	}
1052 
1053 	/* as we may have relased the lock, test the FIFOFAST flag here */
1054 	if (!(fnp->fn_flag & FIFOFAST))
1055 		return;
1056 #if FIFODEBUG
1057 	if (Fifo_verbose)
1058 		cmn_err(CE_NOTE, "Fifo reverting to streams mode\n");
1059 #endif
1060 
1061 	fifo_fastturnoff(fnp);
1062 	if (fnp->fn_flag & ISPIPE) {
1063 		fifo_fastturnoff(fnp->fn_dest);
1064 	}
1065 }
1066 
1067 
1068 /*
1069  * flk_lock must be held while calling fifo_fastturnoff() to
1070  * preserve data ordering (no reads or writes allowed)
1071  */
1072 
1073 static void
1074 fifo_fastturnoff(fifonode_t *fnp)
1075 {
1076 	fifonode_t *fn_dest = fnp->fn_dest;
1077 	mblk_t	*fn_mp;
1078 	int	fn_flag;
1079 
1080 	ASSERT(MUTEX_HELD(&fnp->fn_lock->flk_lock));
1081 	/*
1082 	 * Note: This end can't be closed if there
1083 	 * is stuff in fn_mp
1084 	 */
1085 	if ((fn_mp = fnp->fn_mp) != NULL) {
1086 		ASSERT(fnp->fn_flag & FIFOISOPEN);
1087 		ASSERT(FTOV(fnp)->v_stream != NULL);
1088 		ASSERT(FTOV(fnp)->v_stream->sd_wrq != NULL);
1089 		ASSERT(RD(FTOV(fnp)->v_stream->sd_wrq) != NULL);
1090 		ASSERT(strvp2wq(FTOV(fnp)) != NULL);
1091 		fnp->fn_mp = NULL;
1092 		fnp->fn_count = 0;
1093 		/*
1094 		 * Don't need to drop flk_lock across the put()
1095 		 * since we're just moving the message from the fifo
1096 		 * node to the STREAM head...
1097 		 */
1098 		put(RD(strvp2wq(FTOV(fnp))), fn_mp);
1099 	}
1100 
1101 	/*
1102 	 * Need to re-issue any pending poll requests
1103 	 * so that the STREAMS framework sees them
1104 	 * Writers would be waiting on fnp and readers on fn_dest
1105 	 */
1106 	if ((fnp->fn_flag & (FIFOISOPEN | FIFOPOLLW)) ==
1107 	    (FIFOISOPEN | FIFOPOLLW)) {
1108 		strpollwakeup(FTOV(fnp), POLLWRNORM);
1109 	}
1110 	fn_flag = fn_dest->fn_flag;
1111 	if ((fn_flag & FIFOISOPEN) == FIFOISOPEN) {
1112 		if ((fn_flag & (FIFOPOLLR | FIFOPOLLRBAND))) {
1113 			strpollwakeup(FTOV(fn_dest), POLLIN|POLLRDNORM);
1114 		}
1115 	}
1116 	/*
1117 	 * wake up any sleeping processes so they can notice we went
1118 	 * to streams mode
1119 	 */
1120 	fnp->fn_flag &= ~(FIFOFAST|FIFOWANTW|FIFOWANTR);
1121 	cv_broadcast(&fnp->fn_wait_cv);
1122 }
1123 
1124 /*
1125  * Alternative version of fifo_fastoff()
1126  * optimized for putmsg/getmsg.
1127  */
1128 void
1129 fifo_vfastoff(vnode_t *vp)
1130 {
1131 	fifonode_t	*fnp = VTOF(vp);
1132 
1133 	mutex_enter(&fnp->fn_lock->flk_lock);
1134 	if (!(fnp->fn_flag & FIFOFAST)) {
1135 		mutex_exit(&fnp->fn_lock->flk_lock);
1136 		return;
1137 	}
1138 	fifo_fastoff(fnp);
1139 	mutex_exit(&fnp->fn_lock->flk_lock);
1140 }
1141 
1142 /*
1143  * Wake any sleeping writers, poll and send signals if necessary
1144  * This module is only called when we drop below the hi water mark
1145  * FIFOWANTW indicates that a process is sleeping in fifo_write()
1146  * FIFOHIWATW indicates that we have either attempted a poll or
1147  * non-blocking write and were over the high water mark
1148  * This routine assumes a low water mark of 0.
1149  */
1150 
1151 void
1152 fifo_wakewriter(fifonode_t *fn_dest, fifolock_t *fn_lock)
1153 {
1154 	int fn_dflag = fn_dest->fn_flag;
1155 
1156 	ASSERT(MUTEX_HELD(&fn_lock->flk_lock));
1157 	ASSERT(fn_dest->fn_dest->fn_count < Fifohiwat);
1158 	if ((fn_dflag & FIFOWANTW)) {
1159 		cv_broadcast(&fn_dest->fn_wait_cv);
1160 	}
1161 	if ((fn_dflag & (FIFOHIWATW | FIFOISOPEN)) ==
1162 	    (FIFOHIWATW | FIFOISOPEN)) {
1163 		if (fn_dflag & FIFOPOLLW)
1164 			strpollwakeup(FTOV(fn_dest), POLLWRNORM);
1165 		if (fn_dflag & FIFOSETSIG)
1166 			str_sendsig(FTOV(fn_dest), S_WRNORM, 0, 0);
1167 	}
1168 	/*
1169 	 * FIFOPOLLW can't be set without setting FIFOHIWAT
1170 	 * This allows us to clear both here.
1171 	 */
1172 	fn_dest->fn_flag = fn_dflag & ~(FIFOWANTW | FIFOHIWATW | FIFOPOLLW);
1173 }
1174 
1175 /*
1176  * wake up any sleeping readers, poll or send signal if needed
1177  * FIFOWANTR indicates that a process is waiting in fifo_read() for data
1178  * FIFOSETSIG indicates that SIGPOLL should be sent to process
1179  * FIFOPOLLR indicates that a poll request for reading on the fifo was made
1180  */
1181 
1182 void
1183 fifo_wakereader(fifonode_t *fn_dest, fifolock_t *fn_lock)
1184 {
1185 	int fn_dflag = fn_dest->fn_flag;
1186 
1187 	ASSERT(MUTEX_HELD(&fn_lock->flk_lock));
1188 	if (fn_dflag & FIFOWANTR) {
1189 		cv_broadcast(&fn_dest->fn_wait_cv);
1190 	}
1191 	if (fn_dflag & FIFOISOPEN) {
1192 		if (fn_dflag & FIFOPOLLR)
1193 			strpollwakeup(FTOV(fn_dest), POLLIN | POLLRDNORM);
1194 		if (fn_dflag & FIFOSETSIG)
1195 			str_sendsig(FTOV(fn_dest), S_INPUT | S_RDNORM, 0, 0);
1196 	}
1197 	fn_dest->fn_flag = fn_dflag & ~(FIFOWANTR | FIFOPOLLR);
1198 }
1199