dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / kernel / fs / fifofs / fifosubr.c
blob55988cf927cc8386ced6a582b5b3eab52aea79b1
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
21 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
24 * Copyright (c) 1988, 2010, Oracle and/or its affiliates. All rights reserved.
28 * The routines defined in this file are supporting routines for FIFOFS
29 * file system type.
31 #include <sys/types.h>
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/debug.h>
35 #include <sys/errno.h>
36 #include <sys/time.h>
37 #include <sys/kmem.h>
38 #include <sys/inline.h>
39 #include <sys/file.h>
40 #include <sys/proc.h>
41 #include <sys/stat.h>
42 #include <sys/sysmacros.h>
43 #include <sys/var.h>
44 #include <sys/vfs.h>
45 #include <sys/vnode.h>
46 #include <sys/mode.h>
47 #include <sys/signal.h>
48 #include <sys/user.h>
49 #include <sys/uio.h>
50 #include <sys/flock.h>
51 #include <sys/stream.h>
52 #include <sys/fs/fifonode.h>
53 #include <sys/strsubr.h>
54 #include <sys/stropts.h>
55 #include <sys/cmn_err.h>
56 #include <sys/fs_subr.h>
57 #include <sys/ddi.h>
60 #if FIFODEBUG
61 int Fifo_fastmode = 1; /* pipes/fifos will be opened in fast mode */
62 int Fifo_verbose = 0; /* msg when switching out of fast mode */
63 int Fifohiwat = FIFOHIWAT; /* Modifiable FIFO high water mark */
64 #endif
67 * This is the loadable module wrapper.
69 #include <sys/modctl.h>
71 extern struct qinit fifo_strdata;
73 /* yes, we want all defaults */
74 static const struct vfsops fifo_vfsops;
76 static vfsdef_t vfw = {
77 VFSDEF_VERSION,
78 "fifofs",
79 fifoinit,
80 VSW_ZMOUNT,
81 NULL
85 * Module linkage information for the kernel.
87 extern struct mod_ops mod_fsops;
89 static struct modlfs modlfs = {
90 &mod_fsops, "filesystem for fifo", &vfw
93 static struct modlinkage modlinkage = {
94 MODREV_1, (void *)&modlfs, NULL
97 int
98 _init()
100 return (mod_install(&modlinkage));
104 _info(struct modinfo *modinfop)
106 return (mod_info(&modlinkage, modinfop));
110 * Define data structures within this file.
111 * XXX should the hash size be configurable ?
113 #define FIFOSHFT 5
114 #define FIFO_HASHSZ 63
116 #if ((FIFO_HASHSZ & (FIFO_HASHSZ - 1)) == 0)
117 #define FIFOHASH(vp) (((uintptr_t)(vp) >> FIFOSHFT) & (FIFO_HASHSZ - 1))
118 #else
119 #define FIFOHASH(vp) (((uintptr_t)(vp) >> FIFOSHFT) % FIFO_HASHSZ)
120 #endif
122 fifonode_t *fifoalloc[FIFO_HASHSZ];
123 dev_t fifodev;
124 struct vfs *fifovfsp;
125 int fifofstype;
127 kmutex_t ftable_lock;
128 static kmutex_t fino_lock;
129 struct kmem_cache *fnode_cache;
130 struct kmem_cache *pipe_cache;
132 static void fifoinsert(fifonode_t *);
133 static fifonode_t *fifofind(vnode_t *);
134 static int fifo_connld(struct vnode **, int, cred_t *);
135 static void fifo_fastturnoff(fifonode_t *);
137 static void fifo_reinit_vp(vnode_t *);
139 static void fnode_destructor(void *, void *);
142 * Constructor/destructor routines for fifos and pipes.
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:
149 * --> --> --------- --- ---
150 * | | | lock | | |
151 * | | --------- | |
152 * | | | | fifo |
153 * | --- | fnode | | |
154 * | | | | pipe
155 * | --------- --- |
156 * | | | |
157 * ------- | fnode | |
158 * | | |
159 * --------- ---
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.
166 * The fnode constructor is designed to handle any fifodata structure,
167 * deducing the number of fnodes from the total size. Thus, the fnode
168 * constructor does most of the work for the pipe constructor.
170 static int
171 fnode_constructor(void *buf, void *cdrarg, int kmflags)
173 fifodata_t *fdp = buf;
174 fifolock_t *flp = &fdp->fifo_lock;
175 fifonode_t *fnp = &fdp->fifo_fnode[0];
176 size_t size = (uintptr_t)cdrarg;
178 mutex_init(&flp->flk_lock, NULL, MUTEX_DEFAULT, NULL);
179 cv_init(&flp->flk_wait_cv, NULL, CV_DEFAULT, NULL);
180 flp->flk_ocsync = 0;
182 while ((char *)fnp < (char *)buf + size) {
184 vnode_t *vp;
186 vp = vn_alloc(kmflags);
187 if (vp == NULL) {
188 fnp->fn_vnode = NULL; /* mark for destructor */
189 fnode_destructor(buf, cdrarg);
190 return (-1);
192 fnp->fn_vnode = vp;
194 fnp->fn_lock = flp;
195 fnp->fn_open = 0;
196 fnp->fn_dest = fnp;
197 fnp->fn_mp = NULL;
198 fnp->fn_count = 0;
199 fnp->fn_rsynccnt = 0;
200 fnp->fn_wsynccnt = 0;
201 fnp->fn_wwaitcnt = 0;
202 fnp->fn_insync = 0;
203 fnp->fn_pcredp = NULL;
204 fnp->fn_cpid = -1;
206 * 32-bit stat(2) may fail if fn_ino isn't initialized
208 fnp->fn_ino = 0;
210 cv_init(&fnp->fn_wait_cv, NULL, CV_DEFAULT, NULL);
212 vn_setops(vp, &fifo_vnodeops);
213 vp->v_stream = NULL;
214 vp->v_type = VFIFO;
215 vp->v_data = (caddr_t)fnp;
216 vp->v_flag = VNOMAP | VNOSWAP;
217 vn_exists(vp);
218 fnp++;
220 return (0);
223 static void
224 fnode_destructor(void *buf, void *cdrarg)
226 fifodata_t *fdp = buf;
227 fifolock_t *flp = &fdp->fifo_lock;
228 fifonode_t *fnp = &fdp->fifo_fnode[0];
229 size_t size = (uintptr_t)cdrarg;
231 mutex_destroy(&flp->flk_lock);
232 cv_destroy(&flp->flk_wait_cv);
233 ASSERT(flp->flk_ocsync == 0);
235 while ((char *)fnp < (char *)buf + size) {
237 vnode_t *vp = FTOV(fnp);
239 if (vp == NULL) {
240 return; /* constructor failed here */
243 ASSERT(fnp->fn_mp == NULL);
244 ASSERT(fnp->fn_count == 0);
245 ASSERT(fnp->fn_lock == flp);
246 ASSERT(fnp->fn_open == 0);
247 ASSERT(fnp->fn_insync == 0);
248 ASSERT(fnp->fn_rsynccnt == 0 && fnp->fn_wsynccnt == 0);
249 ASSERT(fnp->fn_wwaitcnt == 0);
250 ASSERT(fnp->fn_pcredp == NULL);
251 ASSERT(vn_matchops(vp, &fifo_vnodeops));
252 ASSERT(vp->v_stream == NULL);
253 ASSERT(vp->v_type == VFIFO);
254 ASSERT(vp->v_data == (caddr_t)fnp);
255 ASSERT((vp->v_flag & (VNOMAP|VNOSWAP)) == (VNOMAP|VNOSWAP));
257 cv_destroy(&fnp->fn_wait_cv);
258 vn_invalid(vp);
259 vn_free(vp);
261 fnp++;
265 static int
266 pipe_constructor(void *buf, void *cdrarg, int kmflags)
268 fifodata_t *fdp = buf;
269 fifonode_t *fnp1 = &fdp->fifo_fnode[0];
270 fifonode_t *fnp2 = &fdp->fifo_fnode[1];
271 vnode_t *vp1;
272 vnode_t *vp2;
274 (void) fnode_constructor(buf, cdrarg, kmflags);
276 vp1 = FTOV(fnp1);
277 vp2 = FTOV(fnp2);
279 vp1->v_vfsp = vp2->v_vfsp = fifovfsp;
280 vp1->v_rdev = vp2->v_rdev = fifodev;
281 fnp1->fn_realvp = fnp2->fn_realvp = NULL;
282 fnp1->fn_dest = fnp2;
283 fnp2->fn_dest = fnp1;
285 return (0);
288 static void
289 pipe_destructor(void *buf, void *cdrarg)
291 #ifdef DEBUG
292 fifodata_t *fdp = buf;
293 fifonode_t *fnp1 = &fdp->fifo_fnode[0];
294 fifonode_t *fnp2 = &fdp->fifo_fnode[1];
295 vnode_t *vp1 = FTOV(fnp1);
296 vnode_t *vp2 = FTOV(fnp2);
298 ASSERT(vp1->v_vfsp == fifovfsp);
299 ASSERT(vp2->v_vfsp == fifovfsp);
300 ASSERT(vp1->v_rdev == fifodev);
301 ASSERT(vp2->v_rdev == fifodev);
302 #endif
303 fnode_destructor(buf, cdrarg);
307 * Reinitialize a FIFO vnode (uses normal vnode reinit, but ensures that
308 * vnode type and flags are reset).
311 static void fifo_reinit_vp(vnode_t *vp)
313 vn_reinit(vp);
314 vp->v_type = VFIFO;
315 vp->v_flag &= VROOT;
316 vp->v_flag |= VNOMAP | VNOSWAP;
320 * Save file system type/index, initialize vfs operations vector, get
321 * unique device number for FIFOFS and initialize the FIFOFS hash.
322 * Create and initialize a "generic" vfs pointer that will be placed
323 * in the v_vfsp field of each pipe's vnode.
326 fifoinit(int fstype, char *name)
328 int error;
329 major_t dev;
331 fifofstype = fstype;
332 error = vfs_setfsops(fstype, &fifo_vfsops);
333 if (error != 0) {
334 cmn_err(CE_WARN, "fifoinit: bad fstype");
335 return (error);
338 if ((dev = getudev()) == (major_t)-1) {
339 cmn_err(CE_WARN, "fifoinit: can't get unique device number");
340 dev = 0;
342 fifodev = makedevice(dev, 0);
344 fifovfsp = kmem_zalloc(sizeof (struct vfs), KM_SLEEP);
345 fifovfsp->vfs_next = NULL;
346 vfs_setops(fifovfsp, &fifo_vfsops);
347 fifovfsp->vfs_vnodecovered = NULL;
348 fifovfsp->vfs_flag = 0;
349 fifovfsp->vfs_bsize = 1024;
350 fifovfsp->vfs_fstype = fifofstype;
351 vfs_make_fsid(&fifovfsp->vfs_fsid, fifodev, fifofstype);
352 fifovfsp->vfs_data = NULL;
353 fifovfsp->vfs_dev = fifodev;
354 fifovfsp->vfs_bcount = 0;
357 * It is necessary to initialize vfs_count here to 1.
358 * This prevents the fifovfsp from getting freed when
359 * a thread does a VFS_HOLD followed by a VFS_RELE
360 * on the fifovfsp
362 * The fifovfsp should never be freed.
364 fifovfsp->vfs_count = 1;
366 mutex_init(&ftable_lock, NULL, MUTEX_DEFAULT, NULL);
367 mutex_init(&fino_lock, NULL, MUTEX_DEFAULT, NULL);
370 * vnodes are cached aligned
372 fnode_cache = kmem_cache_create("fnode_cache",
373 sizeof (fifodata_t) - sizeof (fifonode_t), 32,
374 fnode_constructor, fnode_destructor, NULL,
375 (void *)(sizeof (fifodata_t) - sizeof (fifonode_t)), NULL, 0);
377 pipe_cache = kmem_cache_create("pipe_cache", sizeof (fifodata_t), 32,
378 pipe_constructor, pipe_destructor, NULL,
379 (void *)(sizeof (fifodata_t)), NULL, 0);
381 #if FIFODEBUG
382 if (Fifohiwat < FIFOHIWAT)
383 Fifohiwat = FIFOHIWAT;
384 #endif /* FIFODEBUG */
385 fifo_strdata.qi_minfo->mi_hiwat = Fifohiwat;
387 return (0);
391 * Provide a shadow for a vnode. We create a new shadow before checking for an
392 * existing one, to minimize the amount of time we need to hold ftable_lock.
393 * If a vp already has a shadow in the hash list, return its shadow. If not,
394 * we hash the new vnode and return its pointer to the caller.
396 vnode_t *
397 fifovp(vnode_t *vp, cred_t *crp)
399 fifonode_t *fnp;
400 fifonode_t *spec_fnp; /* Speculative fnode ptr. */
401 fifodata_t *fdp;
402 vnode_t *newvp;
403 struct vattr va;
404 vnode_t *rvp;
406 ASSERT(vp != NULL);
408 fdp = kmem_cache_alloc(fnode_cache, KM_SLEEP);
410 fdp->fifo_lock.flk_ref = 1;
411 fnp = &fdp->fifo_fnode[0];
414 * Its possible that fifo nodes on different lofs mountpoints
415 * shadow the same real filesystem fifo node.
416 * In this case its necessary to get and store the realvp.
417 * This way different fifo nodes sharing the same real vnode
418 * can use realvp for communication.
421 if (fop_realvp(vp, &rvp, NULL) == 0)
422 vp = rvp;
424 fnp->fn_realvp = vp;
425 fnp->fn_wcnt = 0;
426 fnp->fn_rcnt = 0;
428 #if FIFODEBUG
429 if (! Fifo_fastmode) {
430 fnp->fn_flag = 0;
431 } else {
432 fnp->fn_flag = FIFOFAST;
434 #else /* FIFODEBUG */
435 fnp->fn_flag = FIFOFAST;
436 #endif /* FIFODEBUG */
439 * initialize the times from vp.
441 va.va_mask = AT_TIMES;
442 if (fop_getattr(vp, &va, 0, crp, NULL) == 0) {
443 fnp->fn_atime = va.va_atime.tv_sec;
444 fnp->fn_mtime = va.va_mtime.tv_sec;
445 fnp->fn_ctime = va.va_ctime.tv_sec;
446 } else {
447 fnp->fn_atime = 0;
448 fnp->fn_mtime = 0;
449 fnp->fn_ctime = 0;
453 * Grab the VP here to avoid holding locks
454 * whilst trying to acquire others.
457 VN_HOLD(vp);
459 mutex_enter(&ftable_lock);
461 if ((spec_fnp = fifofind(vp)) != NULL) {
462 mutex_exit(&ftable_lock);
465 * Release the vnode and free up our pre-prepared fnode.
466 * Zero the lock reference just to explicitly signal
467 * this is unused.
469 VN_RELE(vp);
470 fdp->fifo_lock.flk_ref = 0;
471 kmem_cache_free(fnode_cache, fdp);
473 return (FTOV(spec_fnp));
476 newvp = FTOV(fnp);
477 fifo_reinit_vp(newvp);
479 * Since the fifo vnode's v_vfsp needs to point to the
480 * underlying filesystem's vfsp we need to bump up the
481 * underlying filesystem's vfs reference count.
482 * The count is decremented when the fifo node is
483 * inactivated.
486 VFS_HOLD(vp->v_vfsp);
487 newvp->v_vfsp = vp->v_vfsp;
488 newvp->v_rdev = vp->v_rdev;
489 newvp->v_flag |= (vp->v_flag & VROOT);
491 fifoinsert(fnp);
492 mutex_exit(&ftable_lock);
494 return (newvp);
498 * Create a pipe end by...
499 * allocating a vnode-fifonode pair and initializing the fifonode.
501 void
502 makepipe(vnode_t **vpp1, vnode_t **vpp2)
504 fifonode_t *fnp1;
505 fifonode_t *fnp2;
506 vnode_t *nvp1;
507 vnode_t *nvp2;
508 fifodata_t *fdp;
509 time_t now;
511 fdp = kmem_cache_alloc(pipe_cache, KM_SLEEP);
512 fdp->fifo_lock.flk_ref = 2;
513 fnp1 = &fdp->fifo_fnode[0];
514 fnp2 = &fdp->fifo_fnode[1];
516 fnp1->fn_wcnt = fnp2->fn_wcnt = 1;
517 fnp1->fn_rcnt = fnp2->fn_rcnt = 1;
518 #if FIFODEBUG
519 if (! Fifo_fastmode) {
520 fnp1->fn_flag = fnp2->fn_flag = ISPIPE;
521 } else {
522 fnp1->fn_flag = fnp2->fn_flag = ISPIPE | FIFOFAST;
524 #else /* FIFODEBUG */
525 fnp1->fn_flag = fnp2->fn_flag = ISPIPE | FIFOFAST;
526 #endif /* FIFODEBUG */
527 now = gethrestime_sec();
528 fnp1->fn_atime = fnp2->fn_atime = now;
529 fnp1->fn_mtime = fnp2->fn_mtime = now;
530 fnp1->fn_ctime = fnp2->fn_ctime = now;
532 *vpp1 = nvp1 = FTOV(fnp1);
533 *vpp2 = nvp2 = FTOV(fnp2);
535 fifo_reinit_vp(nvp1); /* Reinitialize vnodes for reuse... */
536 fifo_reinit_vp(nvp2);
537 nvp1->v_vfsp = fifovfsp; /* Need to re-establish VFS & device */
538 nvp2->v_vfsp = fifovfsp; /* before we can reuse this vnode. */
539 nvp1->v_rdev = fifodev;
540 nvp2->v_rdev = fifodev;
544 * Attempt to establish a unique pipe id. Only un-named pipes use this
545 * routine.
547 ino_t
548 fifogetid(void)
550 static ino_t fifo_ino = 0;
551 ino_t fino;
553 mutex_enter(&fino_lock);
554 fino = fifo_ino++;
555 mutex_exit(&fino_lock);
556 return (fino);
561 * Stream a pipe/FIFO.
562 * The FIFOCONNLD flag is used when CONNLD has been pushed on the stream.
563 * If the flag is set, a new vnode is created by calling fifo_connld().
564 * Connld logic was moved to fifo_connld() to speed up the open
565 * operation, simplify the connld/fifo interaction, and remove inherent
566 * race conditions between the connld module and fifos.
567 * This routine is single threaded for two reasons.
568 * 1) connld requests are synchronous; that is, they must block
569 * until the server does an I_RECVFD (oh, well). Single threading is
570 * the simplest way to accomplish this.
571 * 2) fifo_close() must not send M_HANGUP or M_ERROR while we are
572 * in stropen. Stropen() has a tendency to reset things and
573 * we would like streams to remember that a hangup occurred.
576 fifo_stropen(vnode_t **vpp, int flag, cred_t *crp, int dotwist, int lockheld)
578 int error = 0;
579 vnode_t *oldvp = *vpp;
580 fifonode_t *fnp = VTOF(*vpp);
581 dev_t pdev = 0;
582 int firstopen = 0;
583 fifolock_t *fn_lock;
585 fn_lock = fnp->fn_lock;
586 if (!lockheld)
587 mutex_enter(&fn_lock->flk_lock);
588 ASSERT(MUTEX_HELD(&fnp->fn_lock->flk_lock));
591 * FIFO is in the process of opening. Wait for it
592 * to complete before starting another open on it
593 * This prevents races associated with connld open
595 while (fnp->fn_flag & FIFOOPEN) {
596 if (!cv_wait_sig(&fnp->fn_wait_cv, &fn_lock->flk_lock)) {
597 fifo_cleanup(oldvp, flag);
598 if (!lockheld)
599 mutex_exit(&fn_lock->flk_lock);
600 return (EINTR);
605 * The other end of the pipe is almost closed so
606 * reject any other open on this end of the pipe
607 * This only happens with a pipe mounted under namefs
609 if ((fnp->fn_flag & (FIFOCLOSE|ISPIPE)) == (FIFOCLOSE|ISPIPE)) {
610 fifo_cleanup(oldvp, flag);
611 cv_broadcast(&fnp->fn_wait_cv);
612 if (!lockheld)
613 mutex_exit(&fn_lock->flk_lock);
614 return (ENXIO);
617 fnp->fn_flag |= FIFOOPEN;
620 * can't allow close to happen while we are
621 * in the middle of stropen().
622 * M_HANGUP and M_ERROR could leave the stream in a strange state
624 while (fn_lock->flk_ocsync)
625 cv_wait(&fn_lock->flk_wait_cv, &fn_lock->flk_lock);
627 fn_lock->flk_ocsync = 1;
629 if (fnp->fn_flag & FIFOCONNLD) {
631 * This is a reopen, so we should release the fifo lock
632 * just in case some strange module pushed on connld
633 * has some odd side effect.
634 * Note: this stropen is on the oldvp. It will
635 * have no impact on the connld vp returned and
636 * strclose() will only be called when we release
637 * flk_ocsync
639 mutex_exit(&fn_lock->flk_lock);
640 if ((error = stropen(oldvp, &pdev, flag, crp)) != 0) {
641 mutex_enter(&fn_lock->flk_lock);
642 fifo_cleanup(oldvp, flag);
643 fn_lock->flk_ocsync = 0;
644 cv_broadcast(&fn_lock->flk_wait_cv);
645 goto out;
648 * streams open done, allow close on other end if
649 * required. Do this now.. it could
650 * be a very long time before fifo_connld returns.
652 mutex_enter(&fn_lock->flk_lock);
654 * we need to fake an open here so that if this
655 * end of the pipe closes, we don't loose the
656 * stream head (kind of like single threading
657 * open and close for this end of the pipe)
658 * We'll need to call fifo_close() to do clean
659 * up in case this end of the pipe was closed
660 * down while we were in fifo_connld()
662 ASSERT(fnp->fn_open > 0);
663 fnp->fn_open++;
664 fn_lock->flk_ocsync = 0;
665 cv_broadcast(&fn_lock->flk_wait_cv);
666 mutex_exit(&fn_lock->flk_lock);
668 * Connld has been pushed onto the pipe
669 * Create new pipe on behalf of connld
671 if (error = fifo_connld(vpp, flag, crp)) {
672 (void) fifo_close(oldvp, flag, 1, 0, crp, NULL);
673 mutex_enter(&fn_lock->flk_lock);
674 goto out;
677 * undo fake open. We need to call fifo_close
678 * because some other thread could have done
679 * a close and detach of the named pipe while
680 * we were in fifo_connld(), so
681 * we want to make sure the close completes (yuk)
683 (void) fifo_close(oldvp, flag, 1, 0, crp, NULL);
685 * fifo_connld has changed the vp, so we
686 * need to re-initialize locals
688 fnp = VTOF(*vpp);
689 fn_lock = fnp->fn_lock;
690 mutex_enter(&fn_lock->flk_lock);
691 } else {
693 * release lock in case there are modules pushed that
694 * could have some strange side effect
697 mutex_exit(&fn_lock->flk_lock);
700 * If this is the first open of a fifo (dotwist
701 * will be non-zero) we will need to twist the queues.
703 if (oldvp->v_stream == NULL)
704 firstopen = 1;
708 * normal open of pipe/fifo
711 if ((error = stropen(oldvp, &pdev, flag, crp)) != 0) {
712 mutex_enter(&fn_lock->flk_lock);
713 fifo_cleanup(oldvp, flag);
714 ASSERT(fnp->fn_open != 0 || oldvp->v_stream == NULL);
715 fn_lock->flk_ocsync = 0;
716 cv_broadcast(&fn_lock->flk_wait_cv);
717 goto out;
719 mutex_enter(&fn_lock->flk_lock);
722 * twist the ends of the fifo together
724 if (dotwist && firstopen)
725 strmate(*vpp, *vpp);
728 * Show that this open has succeeded
729 * and allow closes or other opens to proceed
731 fnp->fn_open++;
732 fn_lock->flk_ocsync = 0;
733 cv_broadcast(&fn_lock->flk_wait_cv);
735 out:
736 fnp->fn_flag &= ~FIFOOPEN;
737 if (error == 0) {
738 fnp->fn_flag |= FIFOISOPEN;
740 * If this is a FIFO and has the close flag set
741 * and there are now writers, clear the close flag
742 * Note: close flag only gets set when last writer
743 * on a FIFO goes away.
745 if (((fnp->fn_flag & (ISPIPE|FIFOCLOSE)) == FIFOCLOSE) &&
746 fnp->fn_wcnt > 0)
747 fnp->fn_flag &= ~FIFOCLOSE;
749 cv_broadcast(&fnp->fn_wait_cv);
750 if (!lockheld)
751 mutex_exit(&fn_lock->flk_lock);
752 return (error);
756 * Clean up the state of a FIFO and/or mounted pipe in the
757 * event that a fifo_open() was interrupted while the
758 * process was blocked.
760 void
761 fifo_cleanup(vnode_t *vp, int flag)
763 fifonode_t *fnp = VTOF(vp);
765 ASSERT(MUTEX_HELD(&fnp->fn_lock->flk_lock));
767 cleanlocks(vp, curproc->p_pid, 0);
768 cleanshares(vp, curproc->p_pid);
769 if (flag & FREAD) {
770 fnp->fn_rcnt--;
772 if (flag & FWRITE) {
773 fnp->fn_wcnt--;
775 cv_broadcast(&fnp->fn_wait_cv);
780 * Insert a fifonode-vnode pair onto the fifoalloc hash list.
782 static void
783 fifoinsert(fifonode_t *fnp)
785 int idx = FIFOHASH(fnp->fn_realvp);
788 * We don't need to hold fn_lock since we're holding ftable_lock and
789 * this routine is only called right after we've allocated an fnode.
790 * FIFO is inserted at head of NULL terminated doubly linked list.
793 ASSERT(MUTEX_HELD(&ftable_lock));
794 fnp->fn_backp = NULL;
795 fnp->fn_nextp = fifoalloc[idx];
796 fifoalloc[idx] = fnp;
797 if (fnp->fn_nextp)
798 fnp->fn_nextp->fn_backp = fnp;
802 * Find a fifonode-vnode pair on the fifoalloc hash list.
803 * vp is a vnode to be shadowed. If it's on the hash list,
804 * it already has a shadow, therefore return its corresponding
805 * fifonode.
807 static fifonode_t *
808 fifofind(vnode_t *vp)
810 fifonode_t *fnode;
812 ASSERT(MUTEX_HELD(&ftable_lock));
813 for (fnode = fifoalloc[FIFOHASH(vp)]; fnode; fnode = fnode->fn_nextp) {
814 if (fnode->fn_realvp == vp) {
815 VN_HOLD(FTOV(fnode));
816 return (fnode);
819 return (NULL);
823 * Remove a fifonode-vnode pair from the fifoalloc hash list.
824 * This routine is called from the fifo_inactive() routine when a
825 * FIFO is being released.
826 * If the link to be removed is the only link, set fifoalloc to NULL.
828 void
829 fiforemove(fifonode_t *fnp)
831 int idx = FIFOHASH(fnp->fn_realvp);
832 fifonode_t *fnode;
834 ASSERT(MUTEX_HELD(&ftable_lock));
835 fnode = fifoalloc[idx];
837 * fast path... only 1 FIFO in this list entry
839 if (fnode != NULL && fnode == fnp &&
840 !fnode->fn_nextp && !fnode->fn_backp) {
841 fifoalloc[idx] = NULL;
842 } else {
844 for (; fnode; fnode = fnode->fn_nextp) {
845 if (fnode == fnp) {
847 * if we are first entry
849 if (fnp == fifoalloc[idx])
850 fifoalloc[idx] = fnp->fn_nextp;
851 if (fnode->fn_nextp)
852 fnode->fn_nextp->fn_backp =
853 fnode->fn_backp;
854 if (fnode->fn_backp)
855 fnode->fn_backp->fn_nextp =
856 fnode->fn_nextp;
857 break;
864 * Flush all data from a fifo's message queue
867 void
868 fifo_fastflush(fifonode_t *fnp)
870 mblk_t *bp;
871 ASSERT(MUTEX_HELD(&fnp->fn_lock->flk_lock));
873 if ((bp = fnp->fn_mp) != NULL) {
874 fnp->fn_mp = NULL;
875 fnp->fn_count = 0;
876 freemsg(bp);
878 fifo_wakewriter(fnp->fn_dest, fnp->fn_lock);
882 * Note: This routine is single threaded
883 * Protected by FIFOOPEN flag (i.e. flk_lock is not held)
884 * Upon successful completion, the original fifo is unlocked
885 * and FIFOOPEN is cleared for the original vpp.
886 * The new fifo returned has FIFOOPEN set.
888 static int
889 fifo_connld(struct vnode **vpp, int flag, cred_t *crp)
891 struct vnode *vp1;
892 struct vnode *vp2;
893 struct fifonode *oldfnp;
894 struct fifonode *fn_dest;
895 int error;
896 struct file *filep;
897 struct fifolock *fn_lock;
898 cred_t *c;
901 * Get two vnodes that will represent the pipe ends for the new pipe.
903 makepipe(&vp1, &vp2);
906 * Allocate a file descriptor and file pointer for one of the pipe
907 * ends. The file descriptor will be used to send that pipe end to
908 * the process on the other end of this stream. Note that we get
909 * the file structure only, there is no file list entry allocated.
911 if (error = falloc(vp1, FWRITE|FREAD, &filep, NULL)) {
912 VN_RELE(vp1);
913 VN_RELE(vp2);
914 return (error);
916 mutex_exit(&filep->f_tlock);
917 oldfnp = VTOF(*vpp);
918 fn_lock = oldfnp->fn_lock;
919 fn_dest = oldfnp->fn_dest;
922 * Create two new stream heads and attach them to the two vnodes for
923 * the new pipe.
925 if ((error = fifo_stropen(&vp1, FREAD|FWRITE, filep->f_cred, 0, 0)) !=
926 0 ||
927 (error = fifo_stropen(&vp2, flag, filep->f_cred, 0, 0)) != 0) {
928 #if DEBUG
929 cmn_err(CE_NOTE, "fifo stropen failed error 0x%x", error);
930 #endif
932 * this will call fifo_close and VN_RELE on vp1
934 (void) closef(filep);
935 VN_RELE(vp2);
936 return (error);
940 * twist the ends of the pipe together
942 strmate(vp1, vp2);
945 * Set our end to busy in open
946 * Note: Don't need lock around this because we're the only
947 * one who knows about it
949 VTOF(vp2)->fn_flag |= FIFOOPEN;
951 mutex_enter(&fn_lock->flk_lock);
953 fn_dest->fn_flag |= FIFOSEND;
955 * check to make sure neither end of pipe has gone away
957 if (!(fn_dest->fn_flag & FIFOISOPEN)) {
958 error = ENXIO;
959 fn_dest->fn_flag &= ~FIFOSEND;
960 mutex_exit(&fn_lock->flk_lock);
962 * this will call fifo_close and VN_RELE on vp1
964 goto out;
966 mutex_exit(&fn_lock->flk_lock);
969 * Tag the sender's credential on the pipe descriptor.
971 crhold(VTOF(vp1)->fn_pcredp = crp);
972 VTOF(vp1)->fn_cpid = curproc->p_pid;
975 * send the file descriptor to other end of pipe
977 if (error = do_sendfp((*vpp)->v_stream, filep, crp)) {
978 mutex_enter(&fn_lock->flk_lock);
979 fn_dest->fn_flag &= ~FIFOSEND;
980 mutex_exit(&fn_lock->flk_lock);
982 * this will call fifo_close and VN_RELE on vp1
984 goto out;
987 mutex_enter(&fn_lock->flk_lock);
989 * Wait for other end to receive file descriptor
990 * FIFOCLOSE indicates that one or both sides of the pipe
991 * have gone away.
993 while ((fn_dest->fn_flag & (FIFOCLOSE | FIFOSEND)) == FIFOSEND) {
994 if (!cv_wait_sig(&oldfnp->fn_wait_cv, &fn_lock->flk_lock)) {
995 error = EINTR;
996 fn_dest->fn_flag &= ~FIFOSEND;
997 mutex_exit(&fn_lock->flk_lock);
998 goto out;
1002 * If either end of pipe has gone away and the other end did not
1003 * receive pipe, reject the connld open
1005 if ((fn_dest->fn_flag & FIFOSEND)) {
1006 error = ENXIO;
1007 fn_dest->fn_flag &= ~FIFOSEND;
1008 mutex_exit(&fn_lock->flk_lock);
1009 goto out;
1012 oldfnp->fn_flag &= ~FIFOOPEN;
1013 cv_broadcast(&oldfnp->fn_wait_cv);
1014 mutex_exit(&fn_lock->flk_lock);
1016 VN_RELE(*vpp);
1017 *vpp = vp2;
1018 (void) closef(filep);
1019 return (0);
1020 out:
1021 c = filep->f_cred;
1022 crhold(c);
1023 (void) closef(filep);
1024 VTOF(vp2)->fn_flag &= ~FIFOOPEN;
1025 (void) fifo_close(vp2, flag, 1, (offset_t)0, c, NULL);
1026 crfree(c);
1027 VN_RELE(vp2);
1028 return (error);
1032 * Disable fastpath mode.
1034 void
1035 fifo_fastoff(fifonode_t *fnp)
1037 ASSERT(MUTEX_HELD(&fnp->fn_lock->flk_lock));
1038 ASSERT(FTOV(fnp)->v_stream);
1040 /* FIFOSTAYFAST is set => FIFOFAST is set */
1041 while ((fnp->fn_flag & FIFOSTAYFAST) || ((fnp->fn_flag & ISPIPE) &&
1042 (fnp->fn_dest->fn_flag & FIFOSTAYFAST))) {
1043 ASSERT(fnp->fn_flag & FIFOFAST);
1044 /* indicate someone is waiting to turn into stream mode */
1045 fnp->fn_flag |= FIFOWAITMODE;
1046 cv_wait(&fnp->fn_wait_cv, &fnp->fn_lock->flk_lock);
1047 fnp->fn_flag &= ~FIFOWAITMODE;
1050 /* as we may have relased the lock, test the FIFOFAST flag here */
1051 if (!(fnp->fn_flag & FIFOFAST))
1052 return;
1053 #if FIFODEBUG
1054 if (Fifo_verbose)
1055 cmn_err(CE_NOTE, "Fifo reverting to streams mode\n");
1056 #endif
1058 fifo_fastturnoff(fnp);
1059 if (fnp->fn_flag & ISPIPE) {
1060 fifo_fastturnoff(fnp->fn_dest);
1066 * flk_lock must be held while calling fifo_fastturnoff() to
1067 * preserve data ordering (no reads or writes allowed)
1070 static void
1071 fifo_fastturnoff(fifonode_t *fnp)
1073 fifonode_t *fn_dest = fnp->fn_dest;
1074 mblk_t *fn_mp;
1075 int fn_flag;
1077 ASSERT(MUTEX_HELD(&fnp->fn_lock->flk_lock));
1079 * Note: This end can't be closed if there
1080 * is stuff in fn_mp
1082 if ((fn_mp = fnp->fn_mp) != NULL) {
1083 ASSERT(fnp->fn_flag & FIFOISOPEN);
1084 ASSERT(FTOV(fnp)->v_stream != NULL);
1085 ASSERT(FTOV(fnp)->v_stream->sd_wrq != NULL);
1086 ASSERT(RD(FTOV(fnp)->v_stream->sd_wrq) != NULL);
1087 ASSERT(strvp2wq(FTOV(fnp)) != NULL);
1088 fnp->fn_mp = NULL;
1089 fnp->fn_count = 0;
1091 * Don't need to drop flk_lock across the put()
1092 * since we're just moving the message from the fifo
1093 * node to the STREAM head...
1095 put(RD(strvp2wq(FTOV(fnp))), fn_mp);
1099 * Need to re-issue any pending poll requests
1100 * so that the STREAMS framework sees them
1101 * Writers would be waiting on fnp and readers on fn_dest
1103 if ((fnp->fn_flag & (FIFOISOPEN | FIFOPOLLW)) ==
1104 (FIFOISOPEN | FIFOPOLLW)) {
1105 strpollwakeup(FTOV(fnp), POLLWRNORM);
1107 fn_flag = fn_dest->fn_flag;
1108 if ((fn_flag & FIFOISOPEN) == FIFOISOPEN) {
1109 if ((fn_flag & (FIFOPOLLR | FIFOPOLLRBAND))) {
1110 strpollwakeup(FTOV(fn_dest), POLLIN|POLLRDNORM);
1114 * wake up any sleeping processes so they can notice we went
1115 * to streams mode
1117 fnp->fn_flag &= ~(FIFOFAST|FIFOWANTW|FIFOWANTR);
1118 cv_broadcast(&fnp->fn_wait_cv);
1122 * Alternative version of fifo_fastoff()
1123 * optimized for putmsg/getmsg.
1125 void
1126 fifo_vfastoff(vnode_t *vp)
1128 fifonode_t *fnp = VTOF(vp);
1130 mutex_enter(&fnp->fn_lock->flk_lock);
1131 if (!(fnp->fn_flag & FIFOFAST)) {
1132 mutex_exit(&fnp->fn_lock->flk_lock);
1133 return;
1135 fifo_fastoff(fnp);
1136 mutex_exit(&fnp->fn_lock->flk_lock);
1140 * Wake any sleeping writers, poll and send signals if necessary
1141 * This module is only called when we drop below the hi water mark
1142 * FIFOWANTW indicates that a process is sleeping in fifo_write()
1143 * FIFOHIWATW indicates that we have either attempted a poll or
1144 * non-blocking write and were over the high water mark
1145 * This routine assumes a low water mark of 0.
1148 void
1149 fifo_wakewriter(fifonode_t *fn_dest, fifolock_t *fn_lock)
1151 int fn_dflag = fn_dest->fn_flag;
1153 ASSERT(MUTEX_HELD(&fn_lock->flk_lock));
1154 ASSERT(fn_dest->fn_dest->fn_count < Fifohiwat);
1155 if ((fn_dflag & FIFOWANTW)) {
1156 cv_broadcast(&fn_dest->fn_wait_cv);
1158 if ((fn_dflag & (FIFOHIWATW | FIFOISOPEN)) ==
1159 (FIFOHIWATW | FIFOISOPEN)) {
1160 if (fn_dflag & FIFOPOLLW)
1161 strpollwakeup(FTOV(fn_dest), POLLWRNORM);
1162 if (fn_dflag & FIFOSETSIG)
1163 str_sendsig(FTOV(fn_dest), S_WRNORM, 0, 0);
1166 * FIFOPOLLW can't be set without setting FIFOHIWAT
1167 * This allows us to clear both here.
1169 fn_dest->fn_flag = fn_dflag & ~(FIFOWANTW | FIFOHIWATW | FIFOPOLLW);
1173 * wake up any sleeping readers, poll or send signal if needed
1174 * FIFOWANTR indicates that a process is waiting in fifo_read() for data
1175 * FIFOSETSIG indicates that SIGPOLL should be sent to process
1176 * FIFOPOLLR indicates that a poll request for reading on the fifo was made
1179 void
1180 fifo_wakereader(fifonode_t *fn_dest, fifolock_t *fn_lock)
1182 int fn_dflag = fn_dest->fn_flag;
1184 ASSERT(MUTEX_HELD(&fn_lock->flk_lock));
1185 if (fn_dflag & FIFOWANTR) {
1186 cv_broadcast(&fn_dest->fn_wait_cv);
1188 if (fn_dflag & FIFOISOPEN) {
1189 if (fn_dflag & FIFOPOLLR)
1190 strpollwakeup(FTOV(fn_dest), POLLIN | POLLRDNORM);
1191 if (fn_dflag & FIFOSETSIG)
1192 str_sendsig(FTOV(fn_dest), S_INPUT | S_RDNORM, 0, 0);
1194 fn_dest->fn_flag = fn_dflag & ~(FIFOWANTR | FIFOPOLLR);