xref: /illumos-gate/usr/src/uts/common/fs/fifofs/fifosubr.c (revision eae2e508a8e70b1ec407b10bd068c080651bbe5c)
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 /*
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 structure,
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 &= VROOT;
309 	vp->v_flag |= VNOMAP | VNOSWAP;
310 }
311 
312 /*
313  * Save file system type/index, initialize vfs operations vector, get
314  * unique device number for FIFOFS and initialize the FIFOFS hash.
315  * Create and initialize a "generic" vfs pointer that will be placed
316  * in the v_vfsp field of each pipe's vnode.
317  */
318 int
319 fifoinit(int fstype, char *name)
320 {
321 	static const fs_operation_def_t fifo_vfsops_template[] = {
322 		NULL, NULL
323 	};
324 	int error;
325 	major_t dev;
326 
327 	fifofstype = fstype;
328 	error = vfs_setfsops(fstype, fifo_vfsops_template, &fifo_vfsops);
329 	if (error != 0) {
330 		cmn_err(CE_WARN, "fifoinit: bad vfs ops template");
331 		return (error);
332 	}
333 
334 	error = vn_make_ops(name, fifo_vnodeops_template, &fifo_vnodeops);
335 	if (error != 0) {
336 		(void) vfs_freevfsops_by_type(fstype);
337 		cmn_err(CE_WARN, "fifoinit: bad vnode ops template");
338 		return (error);
339 	}
340 
341 	if ((dev = getudev()) == (major_t)-1) {
342 		cmn_err(CE_WARN, "fifoinit: can't get unique device number");
343 		dev = 0;
344 	}
345 	fifodev = makedevice(dev, 0);
346 
347 	fifovfsp = kmem_zalloc(sizeof (struct vfs), KM_SLEEP);
348 	fifovfsp->vfs_next = NULL;
349 	vfs_setops(fifovfsp, fifo_vfsops);
350 	fifovfsp->vfs_vnodecovered = NULL;
351 	fifovfsp->vfs_flag = 0;
352 	fifovfsp->vfs_bsize = 1024;
353 	fifovfsp->vfs_fstype = fifofstype;
354 	vfs_make_fsid(&fifovfsp->vfs_fsid, fifodev, fifofstype);
355 	fifovfsp->vfs_data = NULL;
356 	fifovfsp->vfs_dev = fifodev;
357 	fifovfsp->vfs_bcount = 0;
358 
359 	mutex_init(&ftable_lock, NULL, MUTEX_DEFAULT, NULL);
360 	mutex_init(&fino_lock, NULL, MUTEX_DEFAULT, NULL);
361 
362 	/*
363 	 * vnodes are cached aligned
364 	 */
365 	fnode_cache = kmem_cache_create("fnode_cache",
366 	    sizeof (fifodata_t) - sizeof (fifonode_t), 32,
367 	    fnode_constructor, fnode_destructor, NULL,
368 	    (void *)(sizeof (fifodata_t) - sizeof (fifonode_t)), NULL, 0);
369 
370 	pipe_cache = kmem_cache_create("pipe_cache", sizeof (fifodata_t), 32,
371 	    pipe_constructor, pipe_destructor, NULL,
372 	    (void *)(sizeof (fifodata_t)), NULL, 0);
373 
374 #if FIFODEBUG
375 	if (Fifohiwat < FIFOHIWAT)
376 		Fifohiwat = FIFOHIWAT;
377 #endif /* FIFODEBUG */
378 	fifo_strdata.qi_minfo->mi_hiwat = Fifohiwat;
379 
380 	return (0);
381 }
382 
383 /*
384  * Provide a shadow for a vnode.  We create a new shadow before checking for an
385  * existing one, to minimize the amount of time we need to hold ftable_lock.
386  * If a vp already has a shadow in the hash list, return its shadow.  If not,
387  * we hash the new vnode and return its pointer to the caller.
388  */
389 vnode_t *
390 fifovp(vnode_t *vp, cred_t *crp)
391 {
392 	fifonode_t *fnp;
393 	fifonode_t *spec_fnp;   /* Speculative fnode ptr. */
394 	fifodata_t *fdp;
395 	vnode_t *newvp;
396 	struct vattr va;
397 	vnode_t	*rvp;
398 
399 	ASSERT(vp != NULL);
400 
401 	fdp = kmem_cache_alloc(fnode_cache, KM_SLEEP);
402 
403 	fdp->fifo_lock.flk_ref = 1;
404 	fnp = &fdp->fifo_fnode[0];
405 
406 	/*
407 	 * Its possible that fifo nodes on different lofs mountpoints
408 	 * shadow the same real filesystem fifo node.
409 	 * In this case its necessary to get and store the realvp.
410 	 * This way different fifo nodes sharing the same real vnode
411 	 * can use realvp for communication.
412 	 */
413 
414 	if (VOP_REALVP(vp, &rvp, NULL) == 0)
415 			vp = rvp;
416 
417 	fnp->fn_realvp	= vp;
418 	fnp->fn_wcnt	= 0;
419 	fnp->fn_rcnt	= 0;
420 
421 #if FIFODEBUG
422 	if (! Fifo_fastmode) {
423 		fnp->fn_flag	= 0;
424 	} else {
425 		fnp->fn_flag	= FIFOFAST;
426 	}
427 #else /* FIFODEBUG */
428 	fnp->fn_flag	= FIFOFAST;
429 #endif /* FIFODEBUG */
430 
431 	/*
432 	 * initialize the times from vp.
433 	 */
434 	va.va_mask = AT_TIMES;
435 	if (VOP_GETATTR(vp, &va, 0, crp, NULL) == 0) {
436 		fnp->fn_atime = va.va_atime.tv_sec;
437 		fnp->fn_mtime = va.va_mtime.tv_sec;
438 		fnp->fn_ctime = va.va_ctime.tv_sec;
439 	} else {
440 		fnp->fn_atime = 0;
441 		fnp->fn_mtime = 0;
442 		fnp->fn_ctime = 0;
443 	}
444 
445 	/*
446 	 * Grab the VP here to avoid holding locks
447 	 * whilst trying to acquire others.
448 	 */
449 
450 	VN_HOLD(vp);
451 
452 	mutex_enter(&ftable_lock);
453 
454 	if ((spec_fnp = fifofind(vp)) != NULL) {
455 		mutex_exit(&ftable_lock);
456 
457 		/*
458 		 * Release the vnode and free up our pre-prepared fnode.
459 		 * Zero the lock reference just to explicitly signal
460 		 * this is unused.
461 		 */
462 		VN_RELE(vp);
463 		fdp->fifo_lock.flk_ref = 0;
464 		kmem_cache_free(fnode_cache, fdp);
465 
466 		return (FTOV(spec_fnp));
467 	}
468 
469 	newvp = FTOV(fnp);
470 	fifo_reinit_vp(newvp);
471 	/*
472 	 * Since the fifo vnode's v_vfsp needs to point to the
473 	 * underlying filesystem's vfsp we need to bump up the
474 	 * underlying filesystem's vfs reference count.
475 	 * The count is decremented when the fifo node is
476 	 * inactivated.
477 	 */
478 
479 	VFS_HOLD(vp->v_vfsp);
480 	newvp->v_vfsp = vp->v_vfsp;
481 	newvp->v_rdev = vp->v_rdev;
482 	newvp->v_flag |= (vp->v_flag & VROOT);
483 
484 	fifoinsert(fnp);
485 	mutex_exit(&ftable_lock);
486 
487 	return (newvp);
488 }
489 
490 /*
491  * Create a pipe end by...
492  * allocating a vnode-fifonode pair and initializing the fifonode.
493  */
494 void
495 makepipe(vnode_t **vpp1, vnode_t **vpp2)
496 {
497 	fifonode_t *fnp1;
498 	fifonode_t *fnp2;
499 	vnode_t *nvp1;
500 	vnode_t *nvp2;
501 	fifodata_t *fdp;
502 	time_t now;
503 
504 	fdp = kmem_cache_alloc(pipe_cache, KM_SLEEP);
505 	fdp->fifo_lock.flk_ref = 2;
506 	fnp1 = &fdp->fifo_fnode[0];
507 	fnp2 = &fdp->fifo_fnode[1];
508 
509 	fnp1->fn_wcnt	= fnp2->fn_wcnt		= 1;
510 	fnp1->fn_rcnt	= fnp2->fn_rcnt		= 1;
511 #if FIFODEBUG
512 	if (! Fifo_fastmode) {
513 		fnp1->fn_flag	= fnp2->fn_flag		= ISPIPE;
514 	} else {
515 		fnp1->fn_flag	= fnp2->fn_flag		= ISPIPE | FIFOFAST;
516 	}
517 #else /* FIFODEBUG */
518 	fnp1->fn_flag	= fnp2->fn_flag		= ISPIPE | FIFOFAST;
519 #endif /* FIFODEBUG */
520 	now = gethrestime_sec();
521 	fnp1->fn_atime	= fnp2->fn_atime	= now;
522 	fnp1->fn_mtime	= fnp2->fn_mtime	= now;
523 	fnp1->fn_ctime	= fnp2->fn_ctime	= now;
524 
525 	*vpp1 = nvp1 = FTOV(fnp1);
526 	*vpp2 = nvp2 = FTOV(fnp2);
527 
528 	fifo_reinit_vp(nvp1);		/* Reinitialize vnodes for reuse... */
529 	fifo_reinit_vp(nvp2);
530 	nvp1->v_vfsp = fifovfsp; 	/* Need to re-establish VFS & device */
531 	nvp2->v_vfsp = fifovfsp; 	/* before we can reuse this vnode. */
532 	nvp1->v_rdev = fifodev;
533 	nvp2->v_rdev = fifodev;
534 }
535 
536 /*
537  * Attempt to establish a unique pipe id.  Only un-named pipes use this
538  * routine.
539  */
540 ino_t
541 fifogetid(void)
542 {
543 	static ino_t fifo_ino = 0;
544 	ino_t fino;
545 
546 	mutex_enter(&fino_lock);
547 	fino = fifo_ino++;
548 	mutex_exit(&fino_lock);
549 	return (fino);
550 }
551 
552 
553 /*
554  * Stream a pipe/FIFO.
555  * The FIFOCONNLD flag is used when CONNLD has been pushed on the stream.
556  * If the flag is set, a new vnode is created by calling fifo_connld().
557  * Connld logic was moved to fifo_connld() to speed up the open
558  * operation, simplify the connld/fifo interaction, and remove inherent
559  * race conditions between the connld module and fifos.
560  * This routine is single threaded for two reasons.
561  * 1) connld requests are synchronous; that is, they must block
562  *    until the server does an I_RECVFD (oh, well).  Single threading is
563  *    the simplest way to accomplish this.
564  * 2) fifo_close() must not send M_HANGUP or M_ERROR while we are
565  *    in stropen. Stropen() has a tendency to reset things and
566  *    we would like streams to remember that a hangup occurred.
567  */
568 int
569 fifo_stropen(vnode_t **vpp, int flag, cred_t *crp, int dotwist, int lockheld)
570 {
571 	int error = 0;
572 	vnode_t *oldvp = *vpp;
573 	fifonode_t *fnp = VTOF(*vpp);
574 	dev_t pdev = 0;
575 	int firstopen = 0;
576 	fifolock_t *fn_lock;
577 
578 	fn_lock = fnp->fn_lock;
579 	if (!lockheld)
580 		mutex_enter(&fn_lock->flk_lock);
581 	ASSERT(MUTEX_HELD(&fnp->fn_lock->flk_lock));
582 
583 	/*
584 	 * FIFO is in the process of opening. Wait for it
585 	 * to complete before starting another open on it
586 	 * This prevents races associated with connld open
587 	 */
588 	while (fnp->fn_flag & FIFOOPEN) {
589 		if (!cv_wait_sig(&fnp->fn_wait_cv, &fn_lock->flk_lock)) {
590 			fifo_cleanup(oldvp, flag);
591 			if (!lockheld)
592 				mutex_exit(&fn_lock->flk_lock);
593 			return (EINTR);
594 		}
595 	}
596 
597 	/*
598 	 * The other end of the pipe is almost closed so
599 	 * reject any other open on this end of the pipe
600 	 * This only happens with a pipe mounted under namefs
601 	 */
602 	if ((fnp->fn_flag & (FIFOCLOSE|ISPIPE)) == (FIFOCLOSE|ISPIPE)) {
603 		fifo_cleanup(oldvp, flag);
604 		cv_broadcast(&fnp->fn_wait_cv);
605 		if (!lockheld)
606 			mutex_exit(&fn_lock->flk_lock);
607 		return (ENXIO);
608 	}
609 
610 	fnp->fn_flag |= FIFOOPEN;
611 
612 	/*
613 	 * can't allow close to happen while we are
614 	 * in the middle of stropen().
615 	 * M_HANGUP and M_ERROR could leave the stream in a strange state
616 	 */
617 	while (fn_lock->flk_ocsync)
618 		cv_wait(&fn_lock->flk_wait_cv, &fn_lock->flk_lock);
619 
620 	fn_lock->flk_ocsync = 1;
621 
622 	if (fnp->fn_flag & FIFOCONNLD) {
623 		/*
624 		 * This is a reopen, so we should release the fifo lock
625 		 * just in case some strange module pushed on connld
626 		 * has some odd side effect.
627 		 * Note: this stropen is on the oldvp.  It will
628 		 * have no impact on the connld vp returned and
629 		 * strclose() will only be called when we release
630 		 * flk_ocsync
631 		 */
632 		mutex_exit(&fn_lock->flk_lock);
633 		if ((error = stropen(oldvp, &pdev, flag, crp)) != 0) {
634 			mutex_enter(&fn_lock->flk_lock);
635 			fifo_cleanup(oldvp, flag);
636 			fn_lock->flk_ocsync = 0;
637 			cv_broadcast(&fn_lock->flk_wait_cv);
638 			goto out;
639 		}
640 		/*
641 		 * streams open done, allow close on other end if
642 		 * required.  Do this now.. it could
643 		 * be a very long time before fifo_connld returns.
644 		 */
645 		mutex_enter(&fn_lock->flk_lock);
646 		/*
647 		 * we need to fake an open here so that if this
648 		 * end of the pipe closes, we don't loose the
649 		 * stream head (kind of like single threading
650 		 * open and close for this end of the pipe)
651 		 * We'll need to call fifo_close() to do clean
652 		 * up in case this end of the pipe was closed
653 		 * down while we were in fifo_connld()
654 		 */
655 		ASSERT(fnp->fn_open > 0);
656 		fnp->fn_open++;
657 		fn_lock->flk_ocsync = 0;
658 		cv_broadcast(&fn_lock->flk_wait_cv);
659 		mutex_exit(&fn_lock->flk_lock);
660 		/*
661 		 * Connld has been pushed onto the pipe
662 		 * Create new pipe on behalf of connld
663 		 */
664 		if (error = fifo_connld(vpp, flag, crp)) {
665 			(void) fifo_close(oldvp, flag, 1, 0, crp, NULL);
666 			mutex_enter(&fn_lock->flk_lock);
667 			goto out;
668 		}
669 		/*
670 		 * undo fake open.  We need to call fifo_close
671 		 * because some other thread could have done
672 		 * a close and detach of the named pipe while
673 		 * we were in fifo_connld(), so
674 		 * we want to make sure the close completes (yuk)
675 		 */
676 		(void) fifo_close(oldvp, flag, 1, 0, crp, NULL);
677 		/*
678 		 * fifo_connld has changed the vp, so we
679 		 * need to re-initialize locals
680 		 */
681 		fnp = VTOF(*vpp);
682 		fn_lock = fnp->fn_lock;
683 		mutex_enter(&fn_lock->flk_lock);
684 	} else {
685 		/*
686 		 * release lock in case there are modules pushed that
687 		 * could have some strange side effect
688 		 */
689 
690 		mutex_exit(&fn_lock->flk_lock);
691 
692 		/*
693 		 * If this is the first open of a fifo (dotwist
694 		 * will be non-zero) we will need to twist the queues.
695 		 */
696 		if (oldvp->v_stream == NULL)
697 			firstopen = 1;
698 
699 
700 		/*
701 		 * normal open of pipe/fifo
702 		 */
703 
704 		if ((error = stropen(oldvp, &pdev, flag, crp)) != 0) {
705 			mutex_enter(&fn_lock->flk_lock);
706 			fifo_cleanup(oldvp, flag);
707 			ASSERT(fnp->fn_open != 0 || oldvp->v_stream == NULL);
708 			fn_lock->flk_ocsync = 0;
709 			cv_broadcast(&fn_lock->flk_wait_cv);
710 			goto out;
711 		}
712 		mutex_enter(&fn_lock->flk_lock);
713 
714 		/*
715 		 * twist the ends of the fifo together
716 		 */
717 		if (dotwist && firstopen)
718 			strmate(*vpp, *vpp);
719 
720 		/*
721 		 * Show that this open has succeeded
722 		 * and allow closes or other opens to proceed
723 		 */
724 		fnp->fn_open++;
725 		fn_lock->flk_ocsync = 0;
726 		cv_broadcast(&fn_lock->flk_wait_cv);
727 	}
728 out:
729 	fnp->fn_flag &= ~FIFOOPEN;
730 	if (error == 0) {
731 		fnp->fn_flag |= FIFOISOPEN;
732 		/*
733 		 * If this is a FIFO and has the close flag set
734 		 * and there are now writers, clear the close flag
735 		 * Note: close flag only gets set when last writer
736 		 * on a FIFO goes away.
737 		 */
738 		if (((fnp->fn_flag & (ISPIPE|FIFOCLOSE)) == FIFOCLOSE) &&
739 		    fnp->fn_wcnt > 0)
740 			fnp->fn_flag &= ~FIFOCLOSE;
741 	}
742 	cv_broadcast(&fnp->fn_wait_cv);
743 	if (!lockheld)
744 		mutex_exit(&fn_lock->flk_lock);
745 	return (error);
746 }
747 
748 /*
749  * Clean up the state of a FIFO and/or mounted pipe in the
750  * event that a fifo_open() was interrupted while the
751  * process was blocked.
752  */
753 void
754 fifo_cleanup(vnode_t *vp, int flag)
755 {
756 	fifonode_t *fnp = VTOF(vp);
757 
758 	ASSERT(MUTEX_HELD(&fnp->fn_lock->flk_lock));
759 
760 	cleanlocks(vp, curproc->p_pid, 0);
761 	cleanshares(vp, curproc->p_pid);
762 	if (flag & FREAD) {
763 		fnp->fn_rcnt--;
764 	}
765 	if (flag & FWRITE) {
766 		fnp->fn_wcnt--;
767 	}
768 	cv_broadcast(&fnp->fn_wait_cv);
769 }
770 
771 
772 /*
773  * Insert a fifonode-vnode pair onto the fifoalloc hash list.
774  */
775 static void
776 fifoinsert(fifonode_t *fnp)
777 {
778 	int idx = FIFOHASH(fnp->fn_realvp);
779 
780 	/*
781 	 * We don't need to hold fn_lock since we're holding ftable_lock and
782 	 * this routine is only called right after we've allocated an fnode.
783 	 * FIFO is inserted at head of NULL terminated doubly linked list.
784 	 */
785 
786 	ASSERT(MUTEX_HELD(&ftable_lock));
787 	fnp->fn_backp = NULL;
788 	fnp->fn_nextp = fifoalloc[idx];
789 	fifoalloc[idx] = fnp;
790 	if (fnp->fn_nextp)
791 		fnp->fn_nextp->fn_backp = fnp;
792 }
793 
794 /*
795  * Find a fifonode-vnode pair on the fifoalloc hash list.
796  * vp is a vnode to be shadowed. If it's on the hash list,
797  * it already has a shadow, therefore return its corresponding
798  * fifonode.
799  */
800 static fifonode_t *
801 fifofind(vnode_t *vp)
802 {
803 	fifonode_t *fnode;
804 
805 	ASSERT(MUTEX_HELD(&ftable_lock));
806 	for (fnode = fifoalloc[FIFOHASH(vp)]; fnode; fnode = fnode->fn_nextp) {
807 		if (fnode->fn_realvp == vp) {
808 			VN_HOLD(FTOV(fnode));
809 			return (fnode);
810 		}
811 	}
812 	return (NULL);
813 }
814 
815 /*
816  * Remove a fifonode-vnode pair from the fifoalloc hash list.
817  * This routine is called from the fifo_inactive() routine when a
818  * FIFO is being released.
819  * If the link to be removed is the only link, set fifoalloc to NULL.
820  */
821 void
822 fiforemove(fifonode_t *fnp)
823 {
824 	int idx = FIFOHASH(fnp->fn_realvp);
825 	fifonode_t *fnode;
826 
827 	ASSERT(MUTEX_HELD(&ftable_lock));
828 	fnode = fifoalloc[idx];
829 	/*
830 	 * fast path... only 1 FIFO in this list entry
831 	 */
832 	if (fnode != NULL && fnode == fnp &&
833 	    !fnode->fn_nextp && !fnode->fn_backp) {
834 			fifoalloc[idx] = NULL;
835 	} else {
836 
837 		for (;  fnode;  fnode = fnode->fn_nextp) {
838 			if (fnode == fnp) {
839 				/*
840 				 * if we are first entry
841 				 */
842 				if (fnp == fifoalloc[idx])
843 					fifoalloc[idx] = fnp->fn_nextp;
844 				if (fnode->fn_nextp)
845 					fnode->fn_nextp->fn_backp =
846 					    fnode->fn_backp;
847 				if (fnode->fn_backp)
848 					fnode->fn_backp->fn_nextp =
849 					    fnode->fn_nextp;
850 				break;
851 			}
852 		}
853 	}
854 }
855 
856 /*
857  * Flush all data from a fifo's message queue
858  */
859 
860 void
861 fifo_fastflush(fifonode_t *fnp)
862 {
863 	mblk_t *bp;
864 	ASSERT(MUTEX_HELD(&fnp->fn_lock->flk_lock));
865 
866 	if ((bp = fnp->fn_mp) != NULL) {
867 		fnp->fn_mp = NULL;
868 		fnp->fn_count = 0;
869 		freemsg(bp);
870 	}
871 	fifo_wakewriter(fnp->fn_dest, fnp->fn_lock);
872 }
873 
874 /*
875  * Note:  This routine is single threaded
876  *  Protected by FIFOOPEN flag (i.e. flk_lock is not held)
877  *  Upon successful completion, the original fifo is unlocked
878  *  and FIFOOPEN is cleared for the original vpp.
879  *  The new fifo returned has FIFOOPEN set.
880  */
881 static int
882 fifo_connld(struct vnode **vpp, int flag, cred_t *crp)
883 {
884 	struct vnode *vp1;
885 	struct vnode *vp2;
886 	struct fifonode *oldfnp;
887 	struct fifonode *fn_dest;
888 	int error;
889 	struct file *filep;
890 	struct fifolock *fn_lock;
891 	cred_t *c;
892 
893 	/*
894 	 * Get two vnodes that will represent the pipe ends for the new pipe.
895 	 */
896 	makepipe(&vp1, &vp2);
897 
898 	/*
899 	 * Allocate a file descriptor and file pointer for one of the pipe
900 	 * ends. The file descriptor will be used to send that pipe end to
901 	 * the process on the other end of this stream. Note that we get
902 	 * the file structure only, there is no file list entry allocated.
903 	 */
904 	if (error = falloc(vp1, FWRITE|FREAD, &filep, NULL)) {
905 		VN_RELE(vp1);
906 		VN_RELE(vp2);
907 		return (error);
908 	}
909 	mutex_exit(&filep->f_tlock);
910 	oldfnp = VTOF(*vpp);
911 	fn_lock = oldfnp->fn_lock;
912 	fn_dest = oldfnp->fn_dest;
913 
914 	/*
915 	 * Create two new stream heads and attach them to the two vnodes for
916 	 * the new pipe.
917 	 */
918 	if ((error = fifo_stropen(&vp1, FREAD|FWRITE, filep->f_cred, 0, 0)) !=
919 	    0 ||
920 	    (error = fifo_stropen(&vp2, flag, filep->f_cred, 0, 0)) != 0) {
921 #if DEBUG
922 		cmn_err(CE_NOTE, "fifo stropen failed error 0x%x",
923 		    error);
924 #endif
925 		/*
926 		 * this will call fifo_close and VN_RELE on vp1
927 		 */
928 		(void) closef(filep);
929 		VN_RELE(vp2);
930 		return (error);
931 	}
932 
933 	/*
934 	 * twist the ends of the pipe together
935 	 */
936 	strmate(vp1, vp2);
937 
938 	/*
939 	 * Set our end to busy in open
940 	 * Note: Don't need lock around this because we're the only
941 	 * one who knows about it
942 	 */
943 	VTOF(vp2)->fn_flag |= FIFOOPEN;
944 
945 	mutex_enter(&fn_lock->flk_lock);
946 
947 	fn_dest->fn_flag |= FIFOSEND;
948 	/*
949 	 * check to make sure neither end of pipe has gone away
950 	 */
951 	if (!(fn_dest->fn_flag & FIFOISOPEN)) {
952 		error = ENXIO;
953 		fn_dest->fn_flag &= ~FIFOSEND;
954 		mutex_exit(&fn_lock->flk_lock);
955 		/*
956 		 * this will call fifo_close and VN_RELE on vp1
957 		 */
958 		goto out;
959 	}
960 	mutex_exit(&fn_lock->flk_lock);
961 
962 	/*
963 	 * Tag the sender's credential on the pipe descriptor.
964 	 */
965 	crhold(VTOF(vp1)->fn_pcredp = crp);
966 	VTOF(vp1)->fn_cpid = curproc->p_pid;
967 
968 	/*
969 	 * send the file descriptor to other end of pipe
970 	 */
971 	if (error = do_sendfp((*vpp)->v_stream, filep, crp)) {
972 		mutex_enter(&fn_lock->flk_lock);
973 		fn_dest->fn_flag &= ~FIFOSEND;
974 		mutex_exit(&fn_lock->flk_lock);
975 		/*
976 		 * this will call fifo_close and VN_RELE on vp1
977 		 */
978 		goto out;
979 	}
980 
981 	mutex_enter(&fn_lock->flk_lock);
982 	/*
983 	 * Wait for other end to receive file descriptor
984 	 * FIFOCLOSE indicates that one or both sides of the pipe
985 	 * have gone away.
986 	 */
987 	while ((fn_dest->fn_flag & (FIFOCLOSE | FIFOSEND)) == FIFOSEND) {
988 		if (!cv_wait_sig(&oldfnp->fn_wait_cv, &fn_lock->flk_lock)) {
989 			error = EINTR;
990 			fn_dest->fn_flag &= ~FIFOSEND;
991 			mutex_exit(&fn_lock->flk_lock);
992 			goto out;
993 		}
994 	}
995 	/*
996 	 * If either end of pipe has gone away and the other end did not
997 	 * receive pipe, reject the connld open
998 	 */
999 	if ((fn_dest->fn_flag & FIFOSEND)) {
1000 		error = ENXIO;
1001 		fn_dest->fn_flag &= ~FIFOSEND;
1002 		mutex_exit(&fn_lock->flk_lock);
1003 		goto out;
1004 	}
1005 
1006 	oldfnp->fn_flag &= ~FIFOOPEN;
1007 	cv_broadcast(&oldfnp->fn_wait_cv);
1008 	mutex_exit(&fn_lock->flk_lock);
1009 
1010 	VN_RELE(*vpp);
1011 	*vpp = vp2;
1012 	(void) closef(filep);
1013 	return (0);
1014 out:
1015 	c = filep->f_cred;
1016 	crhold(c);
1017 	(void) closef(filep);
1018 	VTOF(vp2)->fn_flag &= ~FIFOOPEN;
1019 	(void) fifo_close(vp2, flag, 1, (offset_t)0, c, NULL);
1020 	crfree(c);
1021 	VN_RELE(vp2);
1022 	return (error);
1023 }
1024 
1025 /*
1026  * Disable fastpath mode.
1027  */
1028 void
1029 fifo_fastoff(fifonode_t *fnp)
1030 {
1031 	ASSERT(MUTEX_HELD(&fnp->fn_lock->flk_lock));
1032 	ASSERT(FTOV(fnp)->v_stream);
1033 
1034 	/* FIFOSTAYFAST is set => FIFOFAST is set */
1035 	while ((fnp->fn_flag & FIFOSTAYFAST) || ((fnp->fn_flag & ISPIPE) &&
1036 	    (fnp->fn_dest->fn_flag & FIFOSTAYFAST))) {
1037 		ASSERT(fnp->fn_flag & FIFOFAST);
1038 		/* indicate someone is waiting to turn into stream mode */
1039 		fnp->fn_flag |= FIFOWAITMODE;
1040 		cv_wait(&fnp->fn_wait_cv, &fnp->fn_lock->flk_lock);
1041 		fnp->fn_flag &= ~FIFOWAITMODE;
1042 	}
1043 
1044 	/* as we may have relased the lock, test the FIFOFAST flag here */
1045 	if (!(fnp->fn_flag & FIFOFAST))
1046 		return;
1047 #if FIFODEBUG
1048 	if (Fifo_verbose)
1049 		cmn_err(CE_NOTE, "Fifo reverting to streams mode\n");
1050 #endif
1051 
1052 	fifo_fastturnoff(fnp);
1053 	if (fnp->fn_flag & ISPIPE) {
1054 		fifo_fastturnoff(fnp->fn_dest);
1055 	}
1056 }
1057 
1058 
1059 /*
1060  * flk_lock must be held while calling fifo_fastturnoff() to
1061  * preserve data ordering (no reads or writes allowed)
1062  */
1063 
1064 static void
1065 fifo_fastturnoff(fifonode_t *fnp)
1066 {
1067 	fifonode_t *fn_dest = fnp->fn_dest;
1068 	mblk_t	*fn_mp;
1069 	int	fn_flag;
1070 
1071 	ASSERT(MUTEX_HELD(&fnp->fn_lock->flk_lock));
1072 	/*
1073 	 * Note: This end can't be closed if there
1074 	 * is stuff in fn_mp
1075 	 */
1076 	if ((fn_mp = fnp->fn_mp) != NULL) {
1077 		ASSERT(fnp->fn_flag & FIFOISOPEN);
1078 		ASSERT(FTOV(fnp)->v_stream != NULL);
1079 		ASSERT(FTOV(fnp)->v_stream->sd_wrq != NULL);
1080 		ASSERT(RD(FTOV(fnp)->v_stream->sd_wrq) != NULL);
1081 		ASSERT(strvp2wq(FTOV(fnp)) != NULL);
1082 		fnp->fn_mp = NULL;
1083 		fnp->fn_count = 0;
1084 		/*
1085 		 * Don't need to drop flk_lock across the put()
1086 		 * since we're just moving the message from the fifo
1087 		 * node to the STREAM head...
1088 		 */
1089 		put(RD(strvp2wq(FTOV(fnp))), fn_mp);
1090 	}
1091 
1092 	/*
1093 	 * Need to re-issue any pending poll requests
1094 	 * so that the STREAMS framework sees them
1095 	 * Writers would be waiting on fnp and readers on fn_dest
1096 	 */
1097 	if ((fnp->fn_flag & (FIFOISOPEN | FIFOPOLLW)) ==
1098 	    (FIFOISOPEN | FIFOPOLLW)) {
1099 		strpollwakeup(FTOV(fnp), POLLWRNORM);
1100 	}
1101 	fn_flag = fn_dest->fn_flag;
1102 	if ((fn_flag & FIFOISOPEN) == FIFOISOPEN) {
1103 		if ((fn_flag & (FIFOPOLLR | FIFOPOLLRBAND))) {
1104 			strpollwakeup(FTOV(fn_dest), POLLIN|POLLRDNORM);
1105 		}
1106 	}
1107 	/*
1108 	 * wake up any sleeping processes so they can notice we went
1109 	 * to streams mode
1110 	 */
1111 	fnp->fn_flag &= ~(FIFOFAST|FIFOWANTW|FIFOWANTR);
1112 	cv_broadcast(&fnp->fn_wait_cv);
1113 }
1114 
1115 /*
1116  * Alternative version of fifo_fastoff()
1117  * optimized for putmsg/getmsg.
1118  */
1119 void
1120 fifo_vfastoff(vnode_t *vp)
1121 {
1122 	fifonode_t	*fnp = VTOF(vp);
1123 
1124 	mutex_enter(&fnp->fn_lock->flk_lock);
1125 	if (!(fnp->fn_flag & FIFOFAST)) {
1126 		mutex_exit(&fnp->fn_lock->flk_lock);
1127 		return;
1128 	}
1129 	fifo_fastoff(fnp);
1130 	mutex_exit(&fnp->fn_lock->flk_lock);
1131 }
1132 
1133 /*
1134  * Wake any sleeping writers, poll and send signals if necessary
1135  * This module is only called when we drop below the hi water mark
1136  * FIFOWANTW indicates that a process is sleeping in fifo_write()
1137  * FIFOHIWATW indicates that we have either attempted a poll or
1138  * non-blocking write and were over the high water mark
1139  * This routine assumes a low water mark of 0.
1140  */
1141 
1142 void
1143 fifo_wakewriter(fifonode_t *fn_dest, fifolock_t *fn_lock)
1144 {
1145 	int fn_dflag = fn_dest->fn_flag;
1146 
1147 	ASSERT(MUTEX_HELD(&fn_lock->flk_lock));
1148 	ASSERT(fn_dest->fn_dest->fn_count < Fifohiwat);
1149 	if ((fn_dflag & FIFOWANTW)) {
1150 		cv_broadcast(&fn_dest->fn_wait_cv);
1151 	}
1152 	if ((fn_dflag & (FIFOHIWATW | FIFOISOPEN)) ==
1153 	    (FIFOHIWATW | FIFOISOPEN)) {
1154 		if (fn_dflag & FIFOPOLLW)
1155 			strpollwakeup(FTOV(fn_dest), POLLWRNORM);
1156 		if (fn_dflag & FIFOSETSIG)
1157 			str_sendsig(FTOV(fn_dest), S_WRNORM, 0, 0);
1158 	}
1159 	/*
1160 	 * FIFOPOLLW can't be set without setting FIFOHIWAT
1161 	 * This allows us to clear both here.
1162 	 */
1163 	fn_dest->fn_flag = fn_dflag & ~(FIFOWANTW | FIFOHIWATW | FIFOPOLLW);
1164 }
1165 
1166 /*
1167  * wake up any sleeping readers, poll or send signal if needed
1168  * FIFOWANTR indicates that a process is waiting in fifo_read() for data
1169  * FIFOSETSIG indicates that SIGPOLL should be sent to process
1170  * FIFOPOLLR indicates that a poll request for reading on the fifo was made
1171  */
1172 
1173 void
1174 fifo_wakereader(fifonode_t *fn_dest, fifolock_t *fn_lock)
1175 {
1176 	int fn_dflag = fn_dest->fn_flag;
1177 
1178 	ASSERT(MUTEX_HELD(&fn_lock->flk_lock));
1179 	if (fn_dflag & FIFOWANTR) {
1180 		cv_broadcast(&fn_dest->fn_wait_cv);
1181 	}
1182 	if (fn_dflag & FIFOISOPEN) {
1183 		if (fn_dflag & FIFOPOLLR)
1184 			strpollwakeup(FTOV(fn_dest), POLLIN | POLLRDNORM);
1185 		if (fn_dflag & FIFOSETSIG)
1186 			str_sendsig(FTOV(fn_dest), S_INPUT | S_RDNORM, 0, 0);
1187 	}
1188 	fn_dest->fn_flag = fn_dflag & ~(FIFOWANTR | FIFOPOLLR);
1189 }
1190