NetBSD/sys/miscfs/fifofs/fifo_vnops.c

506 lines
12 KiB
C
Raw Normal View History

1998-03-01 05:20:01 +03:00
/* $NetBSD: fifo_vnops.c,v 1.25 1998/03/01 02:21:30 fvdl Exp $ */
1993-03-21 12:45:37 +03:00
/*
1998-03-01 05:20:01 +03:00
* Copyright (c) 1990, 1993, 1995
* The Regents of the University of California. All rights reserved.
1993-03-21 12:45:37 +03:00
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
1998-03-01 05:20:01 +03:00
* @(#)fifo_vnops.c 8.10 (Berkeley) 5/27/95
1993-03-21 12:45:37 +03:00
*/
1993-12-18 06:48:27 +03:00
#include <sys/param.h>
1998-03-01 05:20:01 +03:00
#include <sys/systm.h>
#include <sys/proc.h>
1993-12-18 06:48:27 +03:00
#include <sys/time.h>
#include <sys/namei.h>
#include <sys/vnode.h>
#include <sys/socket.h>
#include <sys/socketvar.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/file.h>
#include <sys/errno.h>
#include <sys/malloc.h>
1996-02-10 01:39:56 +03:00
#include <sys/un.h>
1996-09-07 16:40:22 +04:00
#include <sys/poll.h>
1993-12-18 06:48:27 +03:00
#include <miscfs/fifofs/fifo.h>
#include <miscfs/genfs/genfs.h>
1993-03-21 12:45:37 +03:00
/*
* This structure is associated with the FIFO vnode and stores
* the state associated with the FIFO.
*/
struct fifoinfo {
struct socket *fi_readsock;
struct socket *fi_writesock;
long fi_readers;
long fi_writers;
};
1996-02-10 01:39:56 +03:00
int (**fifo_vnodeop_p) __P((void *));
struct vnodeopv_entry_desc fifo_vnodeop_entries[] = {
{ &vop_default_desc, vn_default_error },
{ &vop_lookup_desc, fifo_lookup }, /* lookup */
{ &vop_create_desc, fifo_create }, /* create */
{ &vop_mknod_desc, fifo_mknod }, /* mknod */
{ &vop_open_desc, fifo_open }, /* open */
{ &vop_close_desc, fifo_close }, /* close */
{ &vop_access_desc, fifo_access }, /* access */
{ &vop_getattr_desc, fifo_getattr }, /* getattr */
{ &vop_setattr_desc, fifo_setattr }, /* setattr */
{ &vop_read_desc, fifo_read }, /* read */
{ &vop_write_desc, fifo_write }, /* write */
{ &vop_lease_desc, fifo_lease_check }, /* lease */
{ &vop_ioctl_desc, fifo_ioctl }, /* ioctl */
1996-09-07 16:40:22 +04:00
{ &vop_poll_desc, fifo_poll }, /* poll */
1998-03-01 05:20:01 +03:00
{ &vop_revoke_desc, fifo_revoke }, /* revoke */
{ &vop_mmap_desc, fifo_mmap }, /* mmap */
{ &vop_fsync_desc, fifo_fsync }, /* fsync */
{ &vop_seek_desc, fifo_seek }, /* seek */
{ &vop_remove_desc, fifo_remove }, /* remove */
{ &vop_link_desc, fifo_link }, /* link */
{ &vop_rename_desc, fifo_rename }, /* rename */
{ &vop_mkdir_desc, fifo_mkdir }, /* mkdir */
{ &vop_rmdir_desc, fifo_rmdir }, /* rmdir */
{ &vop_symlink_desc, fifo_symlink }, /* symlink */
{ &vop_readdir_desc, fifo_readdir }, /* readdir */
{ &vop_readlink_desc, fifo_readlink }, /* readlink */
{ &vop_abortop_desc, fifo_abortop }, /* abortop */
{ &vop_inactive_desc, fifo_inactive }, /* inactive */
{ &vop_reclaim_desc, fifo_reclaim }, /* reclaim */
{ &vop_lock_desc, fifo_lock }, /* lock */
{ &vop_unlock_desc, fifo_unlock }, /* unlock */
{ &vop_bmap_desc, fifo_bmap }, /* bmap */
{ &vop_strategy_desc, fifo_strategy }, /* strategy */
{ &vop_print_desc, fifo_print }, /* print */
{ &vop_islocked_desc, fifo_islocked }, /* islocked */
{ &vop_pathconf_desc, fifo_pathconf }, /* pathconf */
{ &vop_advlock_desc, fifo_advlock }, /* advlock */
{ &vop_blkatoff_desc, fifo_blkatoff }, /* blkatoff */
{ &vop_valloc_desc, fifo_valloc }, /* valloc */
{ &vop_vfree_desc, fifo_vfree }, /* vfree */
{ &vop_truncate_desc, fifo_truncate }, /* truncate */
{ &vop_update_desc, fifo_update }, /* update */
{ &vop_bwrite_desc, fifo_bwrite }, /* bwrite */
1996-02-10 01:39:56 +03:00
{ (struct vnodeop_desc*)NULL, (int(*) __P((void *)))NULL }
1993-03-21 12:45:37 +03:00
};
struct vnodeopv_desc fifo_vnodeop_opv_desc =
{ &fifo_vnodeop_p, fifo_vnodeop_entries };
1993-03-21 12:45:37 +03:00
/*
* Trivial lookup routine that always fails.
*/
/* ARGSUSED */
1996-02-10 01:39:56 +03:00
int
fifo_lookup(v)
void *v;
{
struct vop_lookup_args /* {
struct vnode * a_dvp;
struct vnode ** a_vpp;
struct componentname * a_cnp;
1996-02-10 01:39:56 +03:00
} */ *ap = v;
*ap->a_vpp = NULL;
1993-03-21 12:45:37 +03:00
return (ENOTDIR);
}
/*
* Open called to set up a new instance of a fifo or
* to find an active instance of a fifo.
*/
/* ARGSUSED */
1996-02-10 01:39:56 +03:00
int
fifo_open(v)
void *v;
{
struct vop_open_args /* {
struct vnode *a_vp;
int a_mode;
struct ucred *a_cred;
struct proc *a_p;
1996-02-10 01:39:56 +03:00
} */ *ap = v;
1998-03-01 05:20:01 +03:00
struct vnode *vp = ap->a_vp;
struct fifoinfo *fip;
struct proc *p = ap->a_p;
1993-03-21 12:45:37 +03:00
struct socket *rso, *wso;
int error;
1997-10-09 17:12:01 +04:00
static const char openstr[] = "fifo";
1993-03-21 12:45:37 +03:00
if ((fip = vp->v_fifoinfo) == NULL) {
MALLOC(fip, struct fifoinfo *, sizeof(*fip), M_VNODE, M_WAITOK);
vp->v_fifoinfo = fip;
1996-02-10 01:39:56 +03:00
if ((error = socreate(AF_LOCAL, &rso, SOCK_STREAM, 0)) != 0) {
1993-03-21 12:45:37 +03:00
free(fip, M_VNODE);
vp->v_fifoinfo = NULL;
return (error);
}
fip->fi_readsock = rso;
1996-02-10 01:39:56 +03:00
if ((error = socreate(AF_LOCAL, &wso, SOCK_STREAM, 0)) != 0) {
1993-03-21 12:45:37 +03:00
(void)soclose(rso);
free(fip, M_VNODE);
vp->v_fifoinfo = NULL;
return (error);
}
fip->fi_writesock = wso;
1996-02-10 01:39:56 +03:00
if ((error = unp_connect2(wso, rso)) != 0) {
1993-03-21 12:45:37 +03:00
(void)soclose(wso);
(void)soclose(rso);
free(fip, M_VNODE);
vp->v_fifoinfo = NULL;
return (error);
}
fip->fi_readers = fip->fi_writers = 0;
1993-03-21 12:45:37 +03:00
wso->so_state |= SS_CANTRCVMORE;
rso->so_state |= SS_CANTSENDMORE;
}
if (ap->a_mode & FREAD) {
1995-04-15 03:30:14 +04:00
if (fip->fi_readers++ == 0) {
1993-03-21 12:45:37 +03:00
fip->fi_writesock->so_state &= ~SS_CANTSENDMORE;
if (fip->fi_writers > 0)
wakeup((caddr_t)&fip->fi_writers);
}
1995-04-15 03:30:14 +04:00
}
if (ap->a_mode & FWRITE) {
if (fip->fi_writers++ == 0) {
fip->fi_readsock->so_state &= ~SS_CANTRCVMORE;
if (fip->fi_readers > 0)
wakeup((caddr_t)&fip->fi_readers);
}
1995-04-15 03:30:14 +04:00
}
if (ap->a_mode & FREAD) {
if (ap->a_mode & O_NONBLOCK) {
1993-03-21 12:45:37 +03:00
} else {
1995-04-15 03:30:14 +04:00
while (fip->fi_writers == 0) {
1998-03-01 05:20:01 +03:00
VOP_UNLOCK(vp, 0);
1995-04-15 03:30:14 +04:00
error = tsleep((caddr_t)&fip->fi_readers,
PCATCH | PSOCK, openstr, 0);
1998-03-01 05:20:01 +03:00
vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1995-04-15 03:30:14 +04:00
if (error)
goto bad;
1993-03-21 12:45:37 +03:00
}
1995-04-15 03:30:14 +04:00
}
}
if (ap->a_mode & FWRITE) {
if (ap->a_mode & O_NONBLOCK) {
if (fip->fi_readers == 0) {
error = ENXIO;
goto bad;
}
} else {
while (fip->fi_readers == 0) {
1998-03-01 05:20:01 +03:00
VOP_UNLOCK(vp, 0);
error = tsleep((caddr_t)&fip->fi_writers,
PCATCH | PSOCK, openstr, 0);
1998-03-01 05:20:01 +03:00
vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
if (error)
1995-04-15 03:30:14 +04:00
goto bad;
}
1993-03-21 12:45:37 +03:00
}
}
1995-04-15 03:30:14 +04:00
return (0);
bad:
1998-03-01 05:20:01 +03:00
VOP_CLOSE(vp, ap->a_mode, ap->a_cred, p);
1993-03-21 12:45:37 +03:00
return (error);
}
/*
* Vnode op for read
*/
/* ARGSUSED */
1996-02-10 01:39:56 +03:00
int
fifo_read(v)
void *v;
{
struct vop_read_args /* {
struct vnode *a_vp;
struct uio *a_uio;
int a_ioflag;
struct ucred *a_cred;
1996-02-10 01:39:56 +03:00
} */ *ap = v;
1998-03-01 05:20:01 +03:00
struct uio *uio = ap->a_uio;
struct socket *rso = ap->a_vp->v_fifoinfo->fi_readsock;
1993-03-21 12:45:37 +03:00
int error, startresid;
#ifdef DIAGNOSTIC
if (uio->uio_rw != UIO_READ)
panic("fifo_read mode");
#endif
if (uio->uio_resid == 0)
return (0);
if (ap->a_ioflag & IO_NDELAY)
1993-03-21 12:45:37 +03:00
rso->so_state |= SS_NBIO;
startresid = uio->uio_resid;
1998-03-01 05:20:01 +03:00
VOP_UNLOCK(ap->a_vp, 0);
error = soreceive(rso, (struct mbuf **)0, uio, (struct mbuf **)0,
(struct mbuf **)0, (int *)0);
1998-03-01 05:20:01 +03:00
vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY);
1993-03-21 12:45:37 +03:00
/*
* Clear EOF indication after first such return.
*/
if (uio->uio_resid == startresid)
rso->so_state &= ~SS_CANTRCVMORE;
if (ap->a_ioflag & IO_NDELAY) {
1993-03-21 12:45:37 +03:00
rso->so_state &= ~SS_NBIO;
if (error == EWOULDBLOCK &&
ap->a_vp->v_fifoinfo->fi_writers == 0)
error = 0;
}
1993-03-21 12:45:37 +03:00
return (error);
}
/*
* Vnode op for write
*/
/* ARGSUSED */
1996-02-10 01:39:56 +03:00
int
fifo_write(v)
void *v;
{
struct vop_write_args /* {
struct vnode *a_vp;
struct uio *a_uio;
int a_ioflag;
struct ucred *a_cred;
1996-02-10 01:39:56 +03:00
} */ *ap = v;
struct socket *wso = ap->a_vp->v_fifoinfo->fi_writesock;
1993-03-21 12:45:37 +03:00
int error;
#ifdef DIAGNOSTIC
if (ap->a_uio->uio_rw != UIO_WRITE)
1993-03-21 12:45:37 +03:00
panic("fifo_write mode");
#endif
if (ap->a_ioflag & IO_NDELAY)
1993-03-21 12:45:37 +03:00
wso->so_state |= SS_NBIO;
1998-03-01 05:20:01 +03:00
VOP_UNLOCK(ap->a_vp, 0);
error = sosend(wso, (struct mbuf *)0, ap->a_uio, 0, (struct mbuf *)0, 0);
1998-03-01 05:20:01 +03:00
vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY);
if (ap->a_ioflag & IO_NDELAY)
1993-03-21 12:45:37 +03:00
wso->so_state &= ~SS_NBIO;
return (error);
}
/*
* Device ioctl operation.
*/
/* ARGSUSED */
1996-02-10 01:39:56 +03:00
int
fifo_ioctl(v)
void *v;
{
struct vop_ioctl_args /* {
struct vnode *a_vp;
u_long a_command;
caddr_t a_data;
int a_fflag;
struct ucred *a_cred;
struct proc *a_p;
1996-02-10 01:39:56 +03:00
} */ *ap = v;
1993-03-21 12:45:37 +03:00
struct file filetmp;
1995-04-15 03:30:14 +04:00
int error;
1993-03-21 12:45:37 +03:00
if (ap->a_command == FIONBIO)
1993-03-21 12:45:37 +03:00
return (0);
1995-04-15 03:30:14 +04:00
if (ap->a_fflag & FREAD) {
filetmp.f_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_readsock;
1995-04-15 03:30:14 +04:00
error = soo_ioctl(&filetmp, ap->a_command, ap->a_data, ap->a_p);
if (error)
return (error);
}
if (ap->a_fflag & FWRITE) {
filetmp.f_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_writesock;
1995-04-15 03:30:14 +04:00
error = soo_ioctl(&filetmp, ap->a_command, ap->a_data, ap->a_p);
if (error)
return (error);
}
return (0);
1993-03-21 12:45:37 +03:00
}
/* ARGSUSED */
1996-02-10 01:39:56 +03:00
int
1996-09-07 16:40:22 +04:00
fifo_poll(v)
1996-02-10 01:39:56 +03:00
void *v;
{
1996-09-07 16:40:22 +04:00
struct vop_poll_args /* {
struct vnode *a_vp;
1996-09-07 16:40:22 +04:00
int a_events;
struct proc *a_p;
1996-02-10 01:39:56 +03:00
} */ *ap = v;
1993-03-21 12:45:37 +03:00
struct file filetmp;
1996-09-07 16:40:22 +04:00
int revents = 0;
1993-03-21 12:45:37 +03:00
1996-09-07 16:40:22 +04:00
if (ap->a_events & (POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND)) {
filetmp.f_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_readsock;
1996-09-07 16:40:22 +04:00
if (filetmp.f_data)
revents |= soo_poll(&filetmp, ap->a_events, ap->a_p);
1995-04-15 03:30:14 +04:00
}
1996-09-07 16:40:22 +04:00
if (ap->a_events & (POLLOUT | POLLWRNORM | POLLWRBAND)) {
filetmp.f_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_writesock;
1996-09-07 16:40:22 +04:00
if (filetmp.f_data)
revents |= soo_poll(&filetmp, ap->a_events, ap->a_p);
1995-04-15 03:30:14 +04:00
}
1996-09-07 16:40:22 +04:00
return (revents);
1993-03-21 12:45:37 +03:00
}
1998-03-01 05:20:01 +03:00
int
fifo_inactive(v)
void *v;
{
struct vop_inactive_args /* {
struct vnode *a_vp;
struct proc *a_p;
} */ *ap = v;
VOP_UNLOCK(ap->a_vp, 0);
return (0);
}
1993-03-21 12:45:37 +03:00
/*
* This is a noop, simply returning what one has been given.
*/
1996-02-10 01:39:56 +03:00
int
fifo_bmap(v)
void *v;
{
struct vop_bmap_args /* {
struct vnode *a_vp;
daddr_t a_bn;
struct vnode **a_vpp;
daddr_t *a_bnp;
1998-03-01 05:20:01 +03:00
int *a_runp;
1996-02-10 01:39:56 +03:00
} */ *ap = v;
1993-03-21 12:45:37 +03:00
if (ap->a_vpp != NULL)
*ap->a_vpp = ap->a_vp;
if (ap->a_bnp != NULL)
*ap->a_bnp = ap->a_bn;
1998-03-01 05:20:01 +03:00
if (ap->a_runp != NULL)
*ap->a_runp = 0;
1993-03-21 12:45:37 +03:00
return (0);
}
/*
* Device close routine
*/
/* ARGSUSED */
1996-02-10 01:39:56 +03:00
int
fifo_close(v)
void *v;
{
struct vop_close_args /* {
struct vnode *a_vp;
int a_fflag;
struct ucred *a_cred;
struct proc *a_p;
1996-02-10 01:39:56 +03:00
} */ *ap = v;
register struct vnode *vp = ap->a_vp;
1993-03-21 12:45:37 +03:00
register struct fifoinfo *fip = vp->v_fifoinfo;
int error1, error2;
1995-04-15 03:30:14 +04:00
if (ap->a_fflag & FREAD) {
if (--fip->fi_readers == 0)
socantsendmore(fip->fi_writesock);
}
if (ap->a_fflag & FWRITE) {
1995-04-15 03:30:14 +04:00
if (--fip->fi_writers == 0)
1993-03-21 12:45:37 +03:00
socantrcvmore(fip->fi_readsock);
}
if (vp->v_usecount > 1)
return (0);
error1 = soclose(fip->fi_readsock);
error2 = soclose(fip->fi_writesock);
FREE(fip, M_VNODE);
vp->v_fifoinfo = NULL;
if (error1)
return (error1);
return (error2);
}
/*
* Print out the contents of a fifo vnode.
*/
1996-02-10 01:39:56 +03:00
int
fifo_print(v)
void *v;
{
struct vop_print_args /* {
struct vnode *a_vp;
1996-02-10 01:39:56 +03:00
} */ *ap = v;
1993-03-21 12:45:37 +03:00
1996-10-13 06:21:25 +04:00
printf("tag VT_NON");
fifo_printinfo(ap->a_vp);
1996-10-13 06:21:25 +04:00
printf("\n");
1996-02-10 01:39:56 +03:00
return 0;
1993-03-21 12:45:37 +03:00
}
/*
* Print out internal contents of a fifo vnode.
*/
1996-02-10 01:39:56 +03:00
void
1993-03-21 12:45:37 +03:00
fifo_printinfo(vp)
struct vnode *vp;
{
register struct fifoinfo *fip = vp->v_fifoinfo;
1996-10-13 06:21:25 +04:00
printf(", fifo with %ld readers and %ld writers",
1996-10-11 02:46:11 +04:00
fip->fi_readers, fip->fi_writers);
1993-03-21 12:45:37 +03:00
}
/*
* Return POSIX pathconf information applicable to fifo's.
*/
1996-02-10 01:39:56 +03:00
int
fifo_pathconf(v)
void *v;
{
struct vop_pathconf_args /* {
struct vnode *a_vp;
int a_name;
register_t *a_retval;
1996-02-10 01:39:56 +03:00
} */ *ap = v;
switch (ap->a_name) {
case _PC_LINK_MAX:
*ap->a_retval = LINK_MAX;
return (0);
case _PC_PIPE_BUF:
*ap->a_retval = PIPE_BUF;
return (0);
case _PC_CHOWN_RESTRICTED:
*ap->a_retval = 1;
return (0);
default:
return (EINVAL);
}
/* NOTREACHED */
}