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