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