fifovnops.c revision 7c478bd95313f5f23a4c958a745db2134aa03244
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * CDDL HEADER START
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * The contents of this file are subject to the terms of the
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Common Development and Distribution License, Version 1.0 only
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * (the "License"). You may not use this file except in compliance
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * with the License.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * or http://www.opensolaris.org/os/licensing.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * See the License for the specific language governing permissions
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * and limitations under the License.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * When distributing Covered Code, include this CDDL HEADER in each
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * If applicable, add the following below this CDDL HEADER, with the
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * fields enclosed by brackets "[]" replaced with your own identifying
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * information: Portions Copyright [yyyy] [name of copyright owner]
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * CDDL HEADER END
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland/* All rights reserved. */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Use is subject to license terms.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland#pragma ident "%Z%%M% %I% %E% SMI"
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * FIFOFS file system vnode operations. This file system
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * type supports STREAMS-based pipes and FIFOs.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Define the routines/data structures used in this file.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterlandstatic int fifo_read(vnode_t *, uio_t *, int, cred_t *, caller_context_t *);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterlandstatic int fifo_write(vnode_t *, uio_t *, int, cred_t *, caller_context_t *);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterlandstatic int fifo_getattr(vnode_t *, vattr_t *, int, cred_t *);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterlandstatic int fifo_setattr(vnode_t *, vattr_t *, int, cred_t *,
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterlandstatic int fifo_realvp(vnode_t *, vnode_t **);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterlandstatic int fifo_access(vnode_t *, int, int, cred_t *);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterlandstatic int fifo_fsync(vnode_t *, int, cred_t *);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterlandstatic int fifo_seek(vnode_t *, offset_t, offset_t *);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterlandstatic int fifo_ioctl(vnode_t *, int, intptr_t, int, cred_t *, int *);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterlandstatic int fifo_fastioctl(vnode_t *, int, intptr_t, int, cred_t *, int *);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterlandstatic int fifo_strioctl(vnode_t *, int, intptr_t, int, cred_t *, int *);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterlandstatic int fifo_poll(vnode_t *, short, int, short *, pollhead_t **);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterlandstatic int fifo_pathconf(vnode_t *, int, ulong_t *, cred_t *);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterlandstatic void fifo_inactive(vnode_t *, cred_t *);
b46ec01af51b4e66dbdba8ceb0a8e5ed36241df9okstatic int fifo_rwlock(vnode_t *, int, caller_context_t *);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterlandstatic void fifo_rwunlock(vnode_t *, int, caller_context_t *);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterlandstatic int fifo_setsecattr(struct vnode *, vsecattr_t *, int, struct cred *);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterlandstatic int fifo_getsecattr(struct vnode *, vsecattr_t *, int, struct cred *);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Define the data structures external to this file.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterlandstruct streamtab fifoinfo = { &fifo_strdata, &fifo_stwdata, NULL, NULL };
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterlandconst fs_operation_def_t fifo_vnodeops_template[] = {
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland VOPNAME_INACTIVE, (fs_generic_func_p) fifo_inactive,
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland VOPNAME_RWUNLOCK, (fs_generic_func_p) fifo_rwunlock,
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland VOPNAME_POLL, (fs_generic_func_p) fifo_poll,
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Return the fifoinfo structure.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Open and stream a FIFO.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * If this is the first open of the file (FIFO is not streaming),
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * initialize the fifonode and attach a stream to the vnode.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Each end of a fifo must be synchronized with the other end.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * If not, the mated end may complete an open, I/O, close sequence
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * before the end waiting in open ever wakes up.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Note: namefs pipes come through this routine too.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterlandfifo_open(vnode_t **vpp, int flag, cred_t *crp)
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * If we are the first reader, wake up any writers that
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * may be waiting around. wait for all of them to
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * wake up before proceeding (i.e. fn_wsynccnt == 0)
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland fnp->fn_rcnt++; /* record reader present */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland fnp->fn_rsynccnt++; /* record reader in open */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * If we are the first writer, wake up any readers that
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * may be waiting around. wait for all of them to
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * wake up before proceeding (i.e. fn_rsynccnt == 0)
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland fnp->fn_wcnt++; /* record writer present */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland fnp->fn_wsynccnt++; /* record writer in open */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * fifo_stropen will take care of twisting the queues on the first
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * open. The 1 being passed in means twist the queues on the first
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland error = fifo_stropen(vpp, flag, crp, 1, 1);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * fifo_stropen() could have replaced vpp
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * since fifo's are the only thing we need to sync up,
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * everything else just returns;
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Note: don't need to hold lock since ISPIPE can't change
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * and both old and new vp need to be pipes
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland ASSERT(MUTEX_HELD(&VTOF(*vpp)->fn_lock->flk_lock));
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * XXX note: should probably hold locks, but
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * These values should not be changing
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland mutex_exit(&VTOF(*vpp)->fn_lock->flk_lock);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * vp can't change for FIFOS
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * If we are opening for read (or writer)
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * indicate that the reader (or writer) is done with open
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * if there is a writer (or reader) waiting for us, wake them up
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * and indicate that at least 1 read (or write) open has occured
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * this is need in the event the read (or write) side closes
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * before the writer (or reader) has a chance to wake up
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * i.e. it sees that a reader (or writer) was once there
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland fnp->fn_rsynccnt--; /* reader done with open */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * This indicates that a read open has occured
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Only need to set if writer is actually asleep
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Flag will be consumed by writer.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland fnp->fn_wsynccnt--; /* writer done with open */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * This indicates that a write open has occured
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Only need to set if reader is actually asleep
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Flag will be consumed by reader.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * errors don't wait around.. just return
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Note: XXX other end will wake up and continue despite error.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * There is no defined semantic on the correct course of option
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * so we do what we've done in the past
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * FIFOWOCR (or FIFOROCR) indicates that the writer (or reader)
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * has woken us up and is done with open (this way, if the other
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * end has made it to close, we don't block forever in open)
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * fn_wnct == fn_wsynccnt (or fn_rcnt == fn_rsynccnt) indicates
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * that no writer (or reader) has yet made it through open
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * This has the side benifit of that the first
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * reader (or writer) will wait until the other end finishes open
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Last reader to wakeup clear writer
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Clear both writer and reader open
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * occured flag incase other end is O_RDWR
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Last reader to wakeup clear writer open occured flag
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Clear both writer and reader open occured flag
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * incase other end is O_RDWR
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland if ((flag & (FNDELAY|FNONBLOCK)) && fnp->fn_rcnt == 0) {
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Last writer to wakeup clear
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Clear both writer and reader open
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * occured flag in case other end is O_RDWR
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Last writer to wakeup clear reader open occured flag
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Clear both writer and reader open
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * occured flag in case other end is O_RDWR
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Close down a stream.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Call cleanlocks() and strclean() on every close.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * For last close send hangup message and force
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * the other end of a named pipe to be unmounted.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Mount guarantees that the mounted end will only call fifo_close()
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * with a count of 1 when the unmount occurs.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * This routine will close down one end of a pipe or FIFO
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * and free the stream head via strclose()
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterlandfifo_close(vnode_t *vp, int flag, int count, offset_t offset, cred_t *crp)
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * clean locks and clear events.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland (void) cleanlocks(vp, ttoproc(curthread)->p_pid, 0);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland cleanshares(vp, ttoproc(curthread)->p_pid);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * If a file still has the pipe/FIFO open, return.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * wait for pending opens to finish up
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * note: this also has the side effect of single threading closes
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland cv_wait(&fn_lock->flk_wait_cv, &fn_lock->flk_lock);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * If we are last writer wake up sleeping readers
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * (They'll figure out that there are no more writers
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * and do the right thing)
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * send hangup down stream so that stream head will do the
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * right thing.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland if (--fnp->fn_wcnt == 0 && fn_dest->fn_rcnt > 0) {
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland if ((fn_dest->fn_flag & (FIFOFAST | FIFOWANTR)) ==
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * While we're at it, clear FIFOWANTW too
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Wake up any sleeping readers or
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland fn_dest->fn_flag &= ~(FIFOWANTR | FIFOWANTW);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * This is needed incase the other side
4656d4747c8743290bfbe910c64cd75eb4e4af8dGarrett D'Amore * was opened non-blocking. It is the
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * only way we can tell that wcnt is 0 because
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * of close instead of never having a writer
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Note: sending hangup effectively shuts down
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * both reader and writer at other end.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * For FIFOs we need to indicate to stream head that last reader
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * has gone away so that an error is generated
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Pipes just need to wake up the other end so that it can
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * notice this end has gone away.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland if (fnp->fn_rcnt == 0 && fn_dest->fn_wcnt > 0) {
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland if ((fn_dest->fn_flag & (FIFOFAST | FIFOWANTW)) ==
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * wake up any sleeping writers
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * if there are still processes with this FIFO open
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * clear open/close sync flag
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * and just return;
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland ASSERT((fnp->fn_rcnt + fnp->fn_wcnt) != 0);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Need to send HANGUP if other side is still open
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * (fnp->fn_rcnt or fnp->fn_wcnt may not be zero (some thread
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * on this end of the pipe may still be in fifo_open())
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Note: we can get here with fn_rcnt and fn_wcnt != 0 if some
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * thread is blocked somewhere in the fifo_open() path prior to
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * fifo_stropen() incrementing fn_open. This can occur for
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * normal FIFOs as well as named pipes. fn_rcnt and
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * fn_wcnt only indicate attempts to open. fn_open indicates
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * successful opens. Partially opened FIFOs should proceed
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * normally; i.e. they will appear to be new opens. Partially
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * opened pipes will probably fail.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * If this a pipe and this is the first end to close,
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * then we have a bit of cleanup work to do.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Mark both ends of pipe as closed.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Wake up anybody blocked at the other end and for named pipes,
b46ec01af51b4e66dbdba8ceb0a8e5ed36241df9ok * Close down this end of the stream
b46ec01af51b4e66dbdba8ceb0a8e5ed36241df9ok * Allow other opens/closes to continue
b46ec01af51b4e66dbdba8ceb0a8e5ed36241df9ok * force an unmount of other end.
b46ec01af51b4e66dbdba8ceb0a8e5ed36241df9ok * Otherwise if this is last close,
b46ec01af51b4e66dbdba8ceb0a8e5ed36241df9ok * flush messages,
b46ec01af51b4e66dbdba8ceb0a8e5ed36241df9ok * close down the stream
b46ec01af51b4e66dbdba8ceb0a8e5ed36241df9ok * allow other opens/closes to continue
b46ec01af51b4e66dbdba8ceb0a8e5ed36241df9ok if ((fnp->fn_flag & ISPIPE) && !(fnp->fn_flag & FIFOCLOSE)) {
b46ec01af51b4e66dbdba8ceb0a8e5ed36241df9ok * allow opens and closes to proceed
b46ec01af51b4e66dbdba8ceb0a8e5ed36241df9ok * Since this end is now closed down, any attempt
b46ec01af51b4e66dbdba8ceb0a8e5ed36241df9ok * to do anything with this end will fail
b46ec01af51b4e66dbdba8ceb0a8e5ed36241df9ok * if other end of pipe has been opened and it's
b46ec01af51b4e66dbdba8ceb0a8e5ed36241df9ok * a named pipe, unmount it
b46ec01af51b4e66dbdba8ceb0a8e5ed36241df9ok * We must hold the destination vnode because
b46ec01af51b4e66dbdba8ceb0a8e5ed36241df9ok * nm_unmountall() causes close to be called
b46ec01af51b4e66dbdba8ceb0a8e5ed36241df9ok * for the other end of named pipe. This
b46ec01af51b4e66dbdba8ceb0a8e5ed36241df9ok * could free the vnode before we are ready.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Read from a pipe or FIFO.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * return 0 if....
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * (1) user read request is 0 or no stream
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * (2) broken pipe with no data
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * (3) write-only FIFO with no data
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * (4) no data and FNDELAY flag is set.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Otherwise return
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * EAGAIN if FNONBLOCK is set and no data to read
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * EINTR if signal recieved while waiting for data
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * While there is no data to read....
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * - if the NDELAY/NONBLOCK flag is set, return 0/EAGAIN.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * - wait for a write.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterlandfifo_read(struct vnode *vp, struct uio *uiop, int ioflag, struct cred *crp,
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland TR_FIFOREAD_IN, "fifo_read in:%p fnp %p", vp, fnp);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Check for data on our input queue
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * No data on first attempt and no writer, then EOF
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland if (fn_dest->fn_wcnt == 0 || fn_dest->fn_rcnt == 0) {
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * no data found.. if non-blocking, return EAGAIN
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * otherwise 0.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland if (uiop->uio_fmode & (FNDELAY|FNONBLOCK)) {
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Note: FIFOs can get here with FIFOCLOSE set if
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * write side is in the middle of opeining after
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * it once closed. Pipes better not have FIFOCLOSE set
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland ASSERT((fnp->fn_flag & (ISPIPE|FIFOCLOSE)) !=
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * wait for data
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * check to make sure we are still in fast mode
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* For pipes copy should not bypass cache */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland int uiosize = MIN(bpsize, uiop->uio_resid);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland error = uiomove(fnp->fn_mp->b_rptr, uiosize, UIO_READ, uiop);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland while (fnp->fn_mp == NULL && fn_dest->fn_wwaitcnt > 0) {
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * We've consumed all available data but there
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * are threads waiting to write more, let them
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * proceed before bailing.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland } while (uiop->uio_resid != 0 && fnp->fn_mp != NULL);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland ASSERT(msgdsize(fnp->fn_mp) == fnp->fn_count);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * wake up any blocked writers, processes
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * sleeping on POLLWRNORM, or processes waiting for SIGPOLL
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Note: checking for fn_count < Fifohiwat emulates
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * STREAMS functionality when low water mark is 0
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland if (fn_dest->fn_flag & (FIFOWANTW | FIFOHIWATW) &&
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * FIFO is in streams mode.. let the stream head handle it
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland TR_FIFOREAD_STREAM, "fifo_read stream_mode:%p", vp);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * vnode update access time
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland TR_FIFOREAD_OUT, "fifo_read out:%p error %d",
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * send SIGPIPE and return EPIPE if ...
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * (1) broken pipe (essentially, reader is gone)
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * (2) FIFO is not open for reading
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * return 0 if...
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * (1) no stream
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * (2) user write request is for 0 bytes and SW_SNDZERO is not set
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Note: SW_SNDZERO can't be set in fast mode
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * While the stream is flow controlled....
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * - if the NDELAY/NONBLOCK flag is set, return 0/EAGAIN.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * - unlock the fifonode and sleep waiting for a reader.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * - if a pipe and it has a mate, sleep waiting for its mate
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterlandfifo_write(vnode_t *vp, uio_t *uiop, int ioflag, cred_t *crp,
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * remember original number of bytes requested. Used to determine if
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * we actually have written anything at all
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * only send zero-length messages if SW_SNDZERO is set
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Note: we will be in streams mode if SW_SNDZERO is set
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * XXX this streams interface should not be exposed
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland if ((write_size == 0) && !(stp->sd_wput_opt & SW_SNDZERO))
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland TR_FIFOWRITE_IN, "fifo_write in:%p fnp %p size %d",
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * oops, no readers, error
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland if (fn_dest->fn_rcnt == 0 || fn_dest->fn_wcnt == 0) {
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * if we are not in fast mode, let streams handle it
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland fmode = uiop->uio_fmode & (FNDELAY|FNONBLOCK);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* For pipes copy should not bypass cache */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * check to make sure we are not over high water mark
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Indicate that we have gone over high
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * if non-blocking, return
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * only happens first time through loop
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * wait for things to drain
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * check to make sure we're still in fast mode
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * make sure readers didn't go away
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland if (fn_dest->fn_rcnt == 0 || fn_dest->fn_wcnt == 0) {
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * If the write will put us over the high water mark,
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * then we must break the message up into PIPE_BUF
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * chunks to stay compliant with STREAMS
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland if (uiop->uio_resid + fn_dest->fn_count > Fifohiwat)
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * We don't need to hold flk_lock across the allocb() and
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * uiomove(). However, on a multiprocessor machine where both
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * the reader and writer thread are on cpu's, we must be
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * careful to only drop the lock if there's data to be read.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * This forces threads entering fifo_read() to spin or block
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * on flk_lock, rather than acquiring flk_lock only to
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * discover there's no data to read and being forced to go
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * back to sleep, only to be woken up microseconds later by
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * this writer thread.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Align the mblk with the user data so that
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * copying in the data can take advantage of
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * the double word alignment
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland if ((bp = allocb(size + 8, BPRI_MED)) == NULL) {
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * check to make sure we're still in fast mode
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * make sure readers didn't go away
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland if (fn_dest->fn_rcnt == 0 || fn_dest->fn_wcnt == 0) {
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * some other thread could have gotten in
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * need to go back and check hi water mark
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland bp->b_rptr += ((uintptr_t)uiop->uio_iov->iov_base & 0x7);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland error = uiomove((caddr_t)bp->b_rptr, size, UIO_WRITE, uiop);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * This is the first bit of data; wake up any sleeping
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * readers, processes blocked in poll, and those
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * expecting a SIGPOLL.
goto done;
done:
goto epipe;
return (error);
return (error);
int error = 0;
int cnt;
return (EINVAL);
goto stream_mode;
switch (cmd) {
case I_STR:
case I_SRDOPT:
case I_PUSH:
case I_FDINSERT:
case I_SENDFD:
case I_RECVFD:
case I_E_RECVFD:
case I_ATMARK:
case I_CKBAND:
case I_GETBAND:
case I_SWROPT:
goto turn_fastoff;
case I_FIND:
case I_GETSIG:
case FIONBIO:
case FIOASYNC:
case I_GWROPT:
case I_LIST:
case I_SETCLTIME:
case I_GETCLTIME:
case I_CANPUT:
if (arg != 0) {
goto turn_fastoff;
case I_NREAD:
sizeof (cnt));
if (error == 0) {
case FIORDCHK:
case I_PEEK:
int count;
*rvalp = 0;
if (error)
*rvalp = 0;
if (error == 0)
case FIONREAD:
if (error == 0)
*rvalp = 0;
case I_SETSIG:
case I_FLUSH:
*rvalp = 0;
case I_FLUSHBAND:
error = 0;
*rvalp = 0;
case I_LOOK:
case I_LINK:
case I_PLINK:
case I_UNLINK:
case I_PUNLINK:
case SRIOCSREDIR:
case SRIOCISREDIR:
return (error);
int error;
return (ENOTSUP);
switch (cmd) {
case I_RECVFD:
case I_E_RECVFD:
if (error == 0) {
return (error);
int error = 0;
return (error);
* for non-attached/ordinary pipes
int flags,
int error = 0;
if (error == 0) {
return (error);
return (EINVAL);
return (ESPIPE);
int retevents;
retevents = 0;
goto stream_mode;
if (!anyyet) {
int error = 0;
switch (cmd) {
case _PC_LINK_MAX:
case _PC_MAX_CANON:
case _PC_MAX_INPUT:
case _PC_NAME_MAX:
case _PC_PATH_MAX:
case _PC_SYMLINK_MAX:
case _PC_PIPE_BUF:
case _PC_NO_TRUNC:
case _PC_VDISABLE:
case _PC_CHOWN_RESTRICTED:
if (rstchown)
case _PC_FILESIZEBITS:
if (error == 0)
return (error);
int error;
return (error);
return (fs_nosys());