fifosubr.c revision 7e94462bf62a2c5de37402d56756925f69de047e
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
/*
* Copyright 2005 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* The routines defined in this file are supporting routines for FIFOFS
* file sytem type.
*/
#include <sys/sysmacros.h>
#if FIFODEBUG
int Fifo_verbose = 0; /* msg when switching out of fast mode */
#endif
/*
* This is the loadable module wrapper.
*/
extern struct qinit fifo_strdata;
struct vfsops *fifo_vfsops;
"fifofs",
0,
};
/*
* Module linkage information for the kernel.
*/
};
static struct modlinkage modlinkage = {
};
int
_init()
{
return (mod_install(&modlinkage));
}
int
{
}
/*
* Define data structures within this file.
* XXX should the hash size be configurable ?
*/
#define FIFOSHFT 5
#define FIFO_HASHSZ 63
#else
#endif
int fifofstype;
struct kmem_cache *fnode_cache;
struct kmem_cache *pipe_cache;
static void fifoinsert(fifonode_t *);
static void fifo_fastturnoff(fifonode_t *);
static void fifo_reinit_vp(vnode_t *);
/*
* Constructor/destructor routines for fifos and pipes.
*
* In the interest of code sharing, we define a common fifodata structure
* which consists of a fifolock and one or two fnodes. A fifo contains
* one fnode; a pipe contains two. The fifolock is shared by the fnodes,
* each of which points to it:
*
* --> --> --------- --- ---
* | | | lock | | |
* | | --------- | |
* | | | | fifo |
* | --- | fnode | | |
* | | | | pipe
* | --------- --- |
* | | | |
* ------- | fnode | |
* | | |
* --------- ---
*
* Since the fifolock is at the beginning of the fifodata structure,
* the fifolock address is the same as the fifodata address. Thus,
* we can determine the fifodata address from any of its member fnodes.
* This is essential for fifo_inactive.
*
* The fnode constructor is designed to handle any fifodata struture,
* deducing the number of fnodes from the total size. Thus, the fnode
* constructor does most of the work for the pipe constructor.
*/
/*ARGSUSED1*/
static int
{
flp->flk_ocsync = 0;
fnp->fn_rsynccnt = 0;
fnp->fn_wsynccnt = 0;
fnp->fn_wwaitcnt = 0;
/*
* 32-bit stat(2) may fail if fn_ino isn't initialized
*/
fnp++;
}
return (0);
}
static void
{
vn_invalid(vp);
fnp++;
}
}
static int
{
return (0);
}
static void
{
#ifdef DEBUG
#endif
}
/*
* Reinitialize a FIFO vnode (uses normal vnode reinit, but ensures that
* vnode type and flags are reset).
*/
{
}
/*
* unique device number for FIFOFS and initialize the FIFOFS hash.
* Create and initialize a "generic" vfs pointer that will be placed
* in the v_vfsp field of each pipe's vnode.
*/
int
{
static const fs_operation_def_t fifo_vfsops_template[] = {
};
int error;
fifofstype = fstype;
if (error != 0) {
return (error);
}
if (error != 0) {
(void) vfs_freevfsops_by_type(fstype);
return (error);
}
dev = 0;
}
fifovfsp->vfs_bcount = 0;
/*
* vnodes are cached aligned
*/
(void *)(sizeof (fifodata_t)), NULL, 0);
#if FIFODEBUG
#endif /* FIFODEBUG */
return (0);
}
/*
* Provide a shadow for a vnode. We create a new shadow before checking for an
* existing one, to minimize the amount of time we need to hold ftable_lock.
* If a vp already has a shadow in the hash list, return its shadow. If not,
* we hash the new vnode and return its pointer to the caller.
*/
vnode_t *
{
#if FIFODEBUG
if (! Fifo_fastmode) {
} else {
}
#else /* FIFODEBUG */
#endif /* FIFODEBUG */
/*
* initialize the times from vp.
*/
} else {
}
/*
* Grab the VP here to avoid holding locks
* whilst trying to acquire others.
*/
/*
* Release the vnode and free up our pre-prepared fnode.
* Zero the lock reference just to explicitly signal
* this is unused.
*/
}
return (newvp);
}
/*
* Create a pipe end by...
* allocating a vnode-fifonode pair and initializing the fifonode.
*/
void
{
#if FIFODEBUG
if (! Fifo_fastmode) {
} else {
}
#else /* FIFODEBUG */
#endif /* FIFODEBUG */
now = gethrestime_sec();
}
/*
* Attempt to establish a unique pipe id. Only un-named pipes use this
* routine.
*/
fifogetid(void)
{
return (fino);
}
/*
* The FIFOCONNLD flag is used when CONNLD has been pushed on the stream.
* If the flag is set, a new vnode is created by calling fifo_connld().
* Connld logic was moved to fifo_connld() to speed up the open
* race conditions between the connld module and fifos.
* This routine is single threaded for two reasons.
* 1) connld requests are synchronous; that is, they must block
* until the server does an I_RECVFD (oh, well). Single threading is
* the simplest way to accomplish this.
* 2) fifo_close() must not send M_HANGUP or M_ERROR while we are
* in stropen. Stropen() has a tendency to reset things and
* we would like streams to remember that a hangup occurred.
*/
int
{
int error = 0;
int firstopen = 0;
if (!lockheld)
/*
* FIFO is in the process of opening. Wait for it
* to complete before starting another open on it
* This prevents races associated with connld open
*/
if (!lockheld)
return (EINTR);
}
}
/*
* The other end of the pipe is almost closed so
* reject any other open on this end of the pipe
* This only happens with a pipe mounted under namefs
*/
if (!lockheld)
return (ENXIO);
}
/*
* can't allow close to happen while we are
* in the middle of stropen().
* M_HANGUP and M_ERROR could leave the stream in a strange state
*/
while (fn_lock->flk_ocsync)
/*
* This is a reopen, so we should release the fifo lock
* just in case some strange module pushed on connld
* has some odd side effect.
* Note: this stropen is on the oldvp. It will
* have no impact on the connld vp returned and
* strclose() will only be called when we release
* flk_ocsync
*/
fn_lock->flk_ocsync = 0;
goto out;
}
/*
* streams open done, allow close on other end if
* required. Do this now.. it could
* be a very long time before fifo_connld returns.
*/
/*
* we need to fake an open here so that if this
* end of the pipe closes, we don't loose the
* stream head (kind of like single threading
* open and close for this end of the pipe)
* We'll need to call fifo_close() to do clean
* up in case this end of the pipe was closed
* down while we were in fifo_connld()
*/
fn_lock->flk_ocsync = 0;
/*
* Connld has been pushed onto the pipe
* Create new pipe on behalf of connld
*/
goto out;
}
/*
* undo fake open. We need to call fifo_close
* because some other thread could have done
* a close and detach of the named pipe while
* we were in fifo_connld(), so
* we want to make sure the close completes (yuk)
*/
/*
* fifo_connld has changed the vp, so we
* need to re-initialize locals
*/
} else {
/*
* release lock in case there are modules pushed that
* could have some strange side effect
*/
/*
* If this is the first open of a fifo (dotwist
* will be non-zero) we will need to twist the queues.
*/
firstopen = 1;
/*
*/
fn_lock->flk_ocsync = 0;
goto out;
}
/*
* twist the ends of the fifo together
*/
/*
* Show that this open has succeeded
* and allow closes or other opens to proceed
*/
fn_lock->flk_ocsync = 0;
}
out:
if (error == 0) {
/*
* If this is a FIFO and has the close flag set
* and there are now writers, clear the close flag
* Note: close flag only gets set when last writer
* on a FIFO goes away.
*/
}
if (!lockheld)
return (error);
}
/*
* event that a fifo_open() was interrupted while the
* process was blocked.
*/
void
{
}
}
}
/*
* Insert a fifonode-vnode pair onto the fifoalloc hash list.
*/
static void
{
/*
* We don't need to hold fn_lock since we're holding ftable_lock and
* this routine is only called right after we've allocated an fnode.
* FIFO is inserted at head of NULL terminated doubly linked list.
*/
}
/*
* Find a fifonode-vnode pair on the fifoalloc hash list.
* vp is a vnode to be shadowed. If it's on the hash list,
* it already has a shadow, therefore return its corresponding
* fifonode.
*/
static fifonode_t *
{
return (fnode);
}
}
return (NULL);
}
/*
* Remove a fifonode-vnode pair from the fifoalloc hash list.
* This routine is called from the fifo_inactive() routine when a
* FIFO is being released.
* If the link to be removed is the only link, set fifoalloc to NULL.
*/
void
{
/*
* fast path... only 1 FIFO in this list entry
*/
} else {
/*
* if we are first entry
*/
break;
}
}
}
}
/*
* Flush all data from a fifo's message queue
*/
void
{
}
}
/*
* Note: This routine is single threaded
* Protected by FIFOOPEN flag (i.e. flk_lock is not held)
* Upon successful completion, the original fifo is unlocked
* and FIFOOPEN is cleared for the original vpp.
* The new fifo returned has FIFOOPEN set.
*/
static int
{
int error;
cred_t *c;
/*
* Get two vnodes that will represent the pipe ends for the new pipe.
*/
/*
* Allocate a file descriptor and file pointer for one of the pipe
* ends. The file descriptor will be used to send that pipe end to
* the process on the other end of this stream. Note that we get
* the file structure only, there is no file list entry allocated.
*/
return (error);
}
/*
* Create two new stream heads and attach them to the two vnodes for
* the new pipe.
*/
0 ||
#if DEBUG
error);
#endif
/*
* this will call fifo_close and VN_RELE on vp1
*/
return (error);
}
/*
* twist the ends of the pipe together
*/
/*
* Set our end to busy in open
* Note: Don't need lock around this because we're the only
* one who knows about it
*/
/*
* check to make sure neither end of pipe has gone away
*/
/*
* this will call fifo_close and VN_RELE on vp1
*/
goto out;
}
/*
* Tag the sender's credential on the pipe descriptor.
*/
/*
* send the file descriptor to other end of pipe
*/
/*
* this will call fifo_close and VN_RELE on vp1
*/
goto out;
}
/*
* Wait for other end to receive file descriptor
* FIFOCLOSE indicates that one or both sides of the pipe
* have gone away.
*/
goto out;
}
}
/*
* If either end of pipe has gone away and the other end did not
* receive pipe, reject the connld open
*/
goto out;
}
return (0);
out:
crhold(c);
crfree(c);
return (error);
}
/*
* Disable fastpath mode.
*/
void
{
return;
#if FIFODEBUG
if (Fifo_verbose)
#endif
}
}
/*
* flk_lock must be held while calling fifo_fastturnoff() to
* preserve data ordering (no reads or writes allowed)
*/
static void
{
int fn_flag;
/*
* Note: This end can't be closed if there
* is stuff in fn_mp
*/
/*
* Don't need to drop flk_lock across the put()
* since we're just moving the message from the fifo
* node to the STREAM head...
*/
}
/*
* Need to re-issue any pending poll requests
* so that the STREAMS framework sees them
* Writers would be waiting on fnp and readers on fn_dest
*/
(FIFOISOPEN | FIFOPOLLW)) {
}
}
}
/*
* wake up any sleeping processes so they can notice we went
* to streams mode
*/
}
/*
* Alternative version of fifo_fastoff()
*/
void
{
return;
}
}
/*
* Wake any sleeping writers, poll and send signals if necessary
* This module is only called when we drop below the hi water mark
* FIFOWANTW indicates that a process is sleeping in fifo_write()
* FIFOHIWATW indicates that we have either attempted a poll or
* non-blocking write and were over the high water mark
* This routine assumes a low water mark of 0.
*/
void
{
}
(FIFOHIWATW | FIFOISOPEN)) {
if (fn_dflag & FIFOSETSIG)
}
/*
* FIFOPOLLW can't be set without setting FIFOHIWAT
* This allows us to clear both here.
*/
}
/*
* wake up any sleeping readers, poll or send signal if needed
* FIFOWANTR indicates that a process is waiting in fifo_read() for data
* FIFOSETSIG indicates that SIGPOLL should be sent to process
* FIFOPOLLR indicates that a poll request for reading on the fifo was made
*/
void
{
}
if (fn_dflag & FIFOISOPEN) {
if (fn_dflag & FIFOSETSIG)
}
}