Home | History | Annotate | Line # | Download | only in fifofs
fifo_vnops.c revision 1.55
      1  1.55      elad /*	$NetBSD: fifo_vnops.c,v 1.55 2006/05/14 21:31:52 elad Exp $	*/
      2   1.9       cgd 
      3   1.1       cgd /*
      4  1.25      fvdl  * Copyright (c) 1990, 1993, 1995
      5   1.8   mycroft  *	The Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8   1.1       cgd  * modification, are permitted provided that the following conditions
      9   1.1       cgd  * are met:
     10   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15  1.42       agc  * 3. Neither the name of the University nor the names of its contributors
     16   1.1       cgd  *    may be used to endorse or promote products derived from this software
     17   1.1       cgd  *    without specific prior written permission.
     18   1.1       cgd  *
     19   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29   1.1       cgd  * SUCH DAMAGE.
     30   1.1       cgd  *
     31  1.25      fvdl  *	@(#)fifo_vnops.c	8.10 (Berkeley) 5/27/95
     32   1.1       cgd  */
     33  1.32     lukem 
     34  1.32     lukem #include <sys/cdefs.h>
     35  1.55      elad __KERNEL_RCSID(0, "$NetBSD: fifo_vnops.c,v 1.55 2006/05/14 21:31:52 elad Exp $");
     36   1.1       cgd 
     37   1.6   mycroft #include <sys/param.h>
     38  1.25      fvdl #include <sys/systm.h>
     39   1.8   mycroft #include <sys/proc.h>
     40   1.6   mycroft #include <sys/time.h>
     41   1.6   mycroft #include <sys/namei.h>
     42   1.6   mycroft #include <sys/vnode.h>
     43   1.6   mycroft #include <sys/socket.h>
     44  1.45      matt #include <sys/protosw.h>
     45   1.6   mycroft #include <sys/socketvar.h>
     46   1.6   mycroft #include <sys/stat.h>
     47   1.6   mycroft #include <sys/ioctl.h>
     48   1.6   mycroft #include <sys/file.h>
     49   1.6   mycroft #include <sys/errno.h>
     50   1.6   mycroft #include <sys/malloc.h>
     51  1.17  christos #include <sys/un.h>
     52  1.20   mycroft #include <sys/poll.h>
     53  1.36  jdolecek #include <sys/event.h>
     54  1.19   mycroft 
     55   1.6   mycroft #include <miscfs/fifofs/fifo.h>
     56  1.19   mycroft #include <miscfs/genfs/genfs.h>
     57   1.1       cgd 
     58   1.1       cgd /*
     59   1.1       cgd  * This structure is associated with the FIFO vnode and stores
     60   1.1       cgd  * the state associated with the FIFO.
     61   1.1       cgd  */
     62   1.1       cgd struct fifoinfo {
     63   1.1       cgd 	struct socket	*fi_readsock;
     64   1.1       cgd 	struct socket	*fi_writesock;
     65   1.1       cgd 	long		fi_readers;
     66   1.1       cgd 	long		fi_writers;
     67   1.1       cgd };
     68   1.1       cgd 
     69  1.52   xtraeme int (**fifo_vnodeop_p)(void *);
     70  1.29  jdolecek const struct vnodeopv_entry_desc fifo_vnodeop_entries[] = {
     71   1.8   mycroft 	{ &vop_default_desc, vn_default_error },
     72   1.8   mycroft 	{ &vop_lookup_desc, fifo_lookup },		/* lookup */
     73   1.8   mycroft 	{ &vop_create_desc, fifo_create },		/* create */
     74   1.8   mycroft 	{ &vop_mknod_desc, fifo_mknod },		/* mknod */
     75   1.8   mycroft 	{ &vop_open_desc, fifo_open },			/* open */
     76   1.8   mycroft 	{ &vop_close_desc, fifo_close },		/* close */
     77   1.8   mycroft 	{ &vop_access_desc, fifo_access },		/* access */
     78   1.8   mycroft 	{ &vop_getattr_desc, fifo_getattr },		/* getattr */
     79   1.8   mycroft 	{ &vop_setattr_desc, fifo_setattr },		/* setattr */
     80   1.8   mycroft 	{ &vop_read_desc, fifo_read },			/* read */
     81   1.8   mycroft 	{ &vop_write_desc, fifo_write },		/* write */
     82  1.13   mycroft 	{ &vop_lease_desc, fifo_lease_check },		/* lease */
     83   1.8   mycroft 	{ &vop_ioctl_desc, fifo_ioctl },		/* ioctl */
     84  1.20   mycroft 	{ &vop_poll_desc, fifo_poll },			/* poll */
     85  1.36  jdolecek 	{ &vop_kqfilter_desc, fifo_kqfilter },		/* kqfilter */
     86  1.25      fvdl 	{ &vop_revoke_desc, fifo_revoke },		/* revoke */
     87   1.8   mycroft 	{ &vop_mmap_desc, fifo_mmap },			/* mmap */
     88   1.8   mycroft 	{ &vop_fsync_desc, fifo_fsync },		/* fsync */
     89   1.8   mycroft 	{ &vop_seek_desc, fifo_seek },			/* seek */
     90   1.8   mycroft 	{ &vop_remove_desc, fifo_remove },		/* remove */
     91   1.8   mycroft 	{ &vop_link_desc, fifo_link },			/* link */
     92   1.8   mycroft 	{ &vop_rename_desc, fifo_rename },		/* rename */
     93   1.8   mycroft 	{ &vop_mkdir_desc, fifo_mkdir },		/* mkdir */
     94   1.8   mycroft 	{ &vop_rmdir_desc, fifo_rmdir },		/* rmdir */
     95   1.8   mycroft 	{ &vop_symlink_desc, fifo_symlink },		/* symlink */
     96   1.8   mycroft 	{ &vop_readdir_desc, fifo_readdir },		/* readdir */
     97   1.8   mycroft 	{ &vop_readlink_desc, fifo_readlink },		/* readlink */
     98   1.8   mycroft 	{ &vop_abortop_desc, fifo_abortop },		/* abortop */
     99   1.8   mycroft 	{ &vop_inactive_desc, fifo_inactive },		/* inactive */
    100   1.8   mycroft 	{ &vop_reclaim_desc, fifo_reclaim },		/* reclaim */
    101   1.8   mycroft 	{ &vop_lock_desc, fifo_lock },			/* lock */
    102   1.8   mycroft 	{ &vop_unlock_desc, fifo_unlock },		/* unlock */
    103   1.8   mycroft 	{ &vop_bmap_desc, fifo_bmap },			/* bmap */
    104   1.8   mycroft 	{ &vop_strategy_desc, fifo_strategy },		/* strategy */
    105   1.8   mycroft 	{ &vop_print_desc, fifo_print },		/* print */
    106   1.8   mycroft 	{ &vop_islocked_desc, fifo_islocked },		/* islocked */
    107   1.8   mycroft 	{ &vop_pathconf_desc, fifo_pathconf },		/* pathconf */
    108   1.8   mycroft 	{ &vop_advlock_desc, fifo_advlock },		/* advlock */
    109   1.8   mycroft 	{ &vop_bwrite_desc, fifo_bwrite },		/* bwrite */
    110  1.31  sommerfe 	{ &vop_putpages_desc, fifo_putpages }, 		/* putpages */
    111  1.52   xtraeme 	{ (struct vnodeop_desc*)NULL, (int(*)(void *))NULL }
    112   1.1       cgd };
    113  1.29  jdolecek const struct vnodeopv_desc fifo_vnodeop_opv_desc =
    114   1.8   mycroft 	{ &fifo_vnodeop_p, fifo_vnodeop_entries };
    115   1.1       cgd 
    116   1.1       cgd /*
    117   1.1       cgd  * Trivial lookup routine that always fails.
    118   1.1       cgd  */
    119   1.1       cgd /* ARGSUSED */
    120  1.17  christos int
    121  1.30     lukem fifo_lookup(void *v)
    122  1.17  christos {
    123   1.8   mycroft 	struct vop_lookup_args /* {
    124  1.30     lukem 		struct vnode		*a_dvp;
    125  1.30     lukem 		struct vnode		**a_vpp;
    126  1.30     lukem 		struct componentname	*a_cnp;
    127  1.17  christos 	} */ *ap = v;
    128  1.51     perry 
    129   1.8   mycroft 	*ap->a_vpp = NULL;
    130   1.1       cgd 	return (ENOTDIR);
    131   1.1       cgd }
    132   1.1       cgd 
    133   1.1       cgd /*
    134   1.1       cgd  * Open called to set up a new instance of a fifo or
    135   1.1       cgd  * to find an active instance of a fifo.
    136   1.1       cgd  */
    137   1.1       cgd /* ARGSUSED */
    138  1.17  christos int
    139  1.30     lukem fifo_open(void *v)
    140  1.17  christos {
    141   1.8   mycroft 	struct vop_open_args /* {
    142  1.30     lukem 		struct vnode	*a_vp;
    143  1.30     lukem 		int		a_mode;
    144  1.55      elad 		kauth_cred_t	a_cred;
    145  1.54  christos 		struct lwp	*a_l;
    146  1.17  christos 	} */ *ap = v;
    147  1.30     lukem 	struct vnode	*vp;
    148  1.30     lukem 	struct fifoinfo	*fip;
    149  1.54  christos 	struct lwp	*l = ap->a_l;
    150  1.30     lukem 	struct proc	*p;
    151  1.30     lukem 	struct socket	*rso, *wso;
    152  1.30     lukem 	int		error;
    153   1.1       cgd 
    154  1.30     lukem 	vp = ap->a_vp;
    155  1.54  christos 	p = ap->a_l->l_proc;
    156  1.30     lukem 
    157   1.1       cgd 	if ((fip = vp->v_fifoinfo) == NULL) {
    158   1.1       cgd 		MALLOC(fip, struct fifoinfo *, sizeof(*fip), M_VNODE, M_WAITOK);
    159   1.1       cgd 		vp->v_fifoinfo = fip;
    160  1.54  christos 		error = socreate(AF_LOCAL, &rso, SOCK_STREAM, 0, l);
    161  1.49  jonathan 		if (error != 0) {
    162   1.1       cgd 			free(fip, M_VNODE);
    163   1.1       cgd 			vp->v_fifoinfo = NULL;
    164   1.1       cgd 			return (error);
    165   1.1       cgd 		}
    166   1.1       cgd 		fip->fi_readsock = rso;
    167  1.54  christos 		error = socreate(AF_LOCAL, &wso, SOCK_STREAM, 0, l);
    168  1.49  jonathan 		if (error != 0) {
    169   1.1       cgd 			(void)soclose(rso);
    170   1.1       cgd 			free(fip, M_VNODE);
    171   1.1       cgd 			vp->v_fifoinfo = NULL;
    172   1.1       cgd 			return (error);
    173   1.1       cgd 		}
    174   1.1       cgd 		fip->fi_writesock = wso;
    175  1.45      matt 		if ((error = unp_connect2(wso, rso, PRU_CONNECT2)) != 0) {
    176   1.1       cgd 			(void)soclose(wso);
    177   1.1       cgd 			(void)soclose(rso);
    178   1.1       cgd 			free(fip, M_VNODE);
    179   1.1       cgd 			vp->v_fifoinfo = NULL;
    180   1.1       cgd 			return (error);
    181   1.1       cgd 		}
    182   1.8   mycroft 		fip->fi_readers = fip->fi_writers = 0;
    183   1.1       cgd 		wso->so_state |= SS_CANTRCVMORE;
    184   1.1       cgd 		rso->so_state |= SS_CANTSENDMORE;
    185   1.1       cgd 	}
    186   1.8   mycroft 	if (ap->a_mode & FREAD) {
    187  1.16   mycroft 		if (fip->fi_readers++ == 0) {
    188   1.1       cgd 			fip->fi_writesock->so_state &= ~SS_CANTSENDMORE;
    189   1.1       cgd 			if (fip->fi_writers > 0)
    190  1.47       jrf 				wakeup(&fip->fi_writers);
    191   1.1       cgd 		}
    192  1.16   mycroft 	}
    193  1.16   mycroft 	if (ap->a_mode & FWRITE) {
    194  1.16   mycroft 		if (fip->fi_writers++ == 0) {
    195  1.16   mycroft 			fip->fi_readsock->so_state &= ~SS_CANTRCVMORE;
    196  1.16   mycroft 			if (fip->fi_readers > 0)
    197  1.47       jrf 				wakeup(&fip->fi_readers);
    198   1.2       cgd 		}
    199  1.16   mycroft 	}
    200  1.16   mycroft 	if (ap->a_mode & FREAD) {
    201  1.16   mycroft 		if (ap->a_mode & O_NONBLOCK) {
    202   1.1       cgd 		} else {
    203  1.39    martin 			while (!soreadable(fip->fi_readsock) && fip->fi_writers == 0) {
    204  1.25      fvdl 				VOP_UNLOCK(vp, 0);
    205  1.47       jrf 				error = tsleep(&fip->fi_readers,
    206  1.38  jdolecek 				    PCATCH | PSOCK, "fifor", 0);
    207  1.25      fvdl 				vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    208  1.16   mycroft 				if (error)
    209  1.16   mycroft 					goto bad;
    210   1.1       cgd 			}
    211  1.16   mycroft 		}
    212  1.16   mycroft 	}
    213  1.16   mycroft 	if (ap->a_mode & FWRITE) {
    214  1.16   mycroft 		if (ap->a_mode & O_NONBLOCK) {
    215  1.16   mycroft 			if (fip->fi_readers == 0) {
    216  1.16   mycroft 				error = ENXIO;
    217  1.16   mycroft 				goto bad;
    218  1.16   mycroft 			}
    219  1.16   mycroft 		} else {
    220   1.2       cgd 			while (fip->fi_readers == 0) {
    221  1.25      fvdl 				VOP_UNLOCK(vp, 0);
    222  1.47       jrf 				error = tsleep(&fip->fi_writers,
    223  1.38  jdolecek 				    PCATCH | PSOCK, "fifow", 0);
    224  1.25      fvdl 				vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    225   1.8   mycroft 				if (error)
    226  1.16   mycroft 					goto bad;
    227   1.2       cgd 			}
    228   1.1       cgd 		}
    229   1.1       cgd 	}
    230  1.16   mycroft 	return (0);
    231  1.30     lukem  bad:
    232  1.54  christos 	VOP_CLOSE(vp, ap->a_mode, ap->a_cred, ap->a_l);
    233   1.1       cgd 	return (error);
    234   1.1       cgd }
    235   1.1       cgd 
    236   1.1       cgd /*
    237   1.1       cgd  * Vnode op for read
    238   1.1       cgd  */
    239   1.1       cgd /* ARGSUSED */
    240  1.17  christos int
    241  1.30     lukem fifo_read(void *v)
    242  1.17  christos {
    243   1.8   mycroft 	struct vop_read_args /* {
    244  1.30     lukem 		struct vnode	*a_vp;
    245  1.30     lukem 		struct uio	*a_uio;
    246  1.30     lukem 		int		a_ioflag;
    247  1.55      elad 		kauth_cred_t	a_cred;
    248  1.17  christos 	} */ *ap = v;
    249  1.30     lukem 	struct uio	*uio;
    250  1.30     lukem 	struct socket	*rso;
    251  1.35   thorpej 	int		error;
    252  1.35   thorpej 	size_t		startresid;
    253   1.1       cgd 
    254  1.30     lukem 	uio = ap->a_uio;
    255  1.30     lukem 	rso = ap->a_vp->v_fifoinfo->fi_readsock;
    256   1.1       cgd #ifdef DIAGNOSTIC
    257   1.1       cgd 	if (uio->uio_rw != UIO_READ)
    258   1.1       cgd 		panic("fifo_read mode");
    259   1.1       cgd #endif
    260   1.1       cgd 	if (uio->uio_resid == 0)
    261   1.1       cgd 		return (0);
    262   1.8   mycroft 	if (ap->a_ioflag & IO_NDELAY)
    263   1.1       cgd 		rso->so_state |= SS_NBIO;
    264   1.1       cgd 	startresid = uio->uio_resid;
    265  1.25      fvdl 	VOP_UNLOCK(ap->a_vp, 0);
    266  1.27      matt 	error = (*rso->so_receive)(rso, (struct mbuf **)0, uio,
    267  1.27      matt 	    (struct mbuf **)0, (struct mbuf **)0, (int *)0);
    268  1.25      fvdl 	vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY);
    269   1.1       cgd 	/*
    270   1.1       cgd 	 * Clear EOF indication after first such return.
    271   1.1       cgd 	 */
    272   1.1       cgd 	if (uio->uio_resid == startresid)
    273   1.1       cgd 		rso->so_state &= ~SS_CANTRCVMORE;
    274  1.23    kleink 	if (ap->a_ioflag & IO_NDELAY) {
    275   1.1       cgd 		rso->so_state &= ~SS_NBIO;
    276  1.23    kleink 		if (error == EWOULDBLOCK &&
    277  1.23    kleink 		    ap->a_vp->v_fifoinfo->fi_writers == 0)
    278  1.23    kleink 			error = 0;
    279  1.23    kleink 	}
    280   1.1       cgd 	return (error);
    281   1.1       cgd }
    282   1.1       cgd 
    283   1.1       cgd /*
    284   1.1       cgd  * Vnode op for write
    285   1.1       cgd  */
    286   1.1       cgd /* ARGSUSED */
    287  1.17  christos int
    288  1.30     lukem fifo_write(void *v)
    289  1.17  christos {
    290   1.8   mycroft 	struct vop_write_args /* {
    291  1.30     lukem 		struct vnode	*a_vp;
    292  1.30     lukem 		struct uio	*a_uio;
    293  1.30     lukem 		int		a_ioflag;
    294  1.55      elad 		kauth_cred_t	a_cred;
    295  1.17  christos 	} */ *ap = v;
    296  1.30     lukem 	struct socket	*wso;
    297  1.30     lukem 	int		error;
    298   1.1       cgd 
    299  1.30     lukem 	wso = ap->a_vp->v_fifoinfo->fi_writesock;
    300   1.1       cgd #ifdef DIAGNOSTIC
    301   1.8   mycroft 	if (ap->a_uio->uio_rw != UIO_WRITE)
    302   1.1       cgd 		panic("fifo_write mode");
    303   1.1       cgd #endif
    304   1.8   mycroft 	if (ap->a_ioflag & IO_NDELAY)
    305   1.1       cgd 		wso->so_state |= SS_NBIO;
    306  1.25      fvdl 	VOP_UNLOCK(ap->a_vp, 0);
    307  1.27      matt 	error = (*wso->so_send)(wso, (struct mbuf *)0, ap->a_uio, 0,
    308  1.54  christos 	    (struct mbuf *)0, 0, curlwp /*XXX*/);
    309  1.25      fvdl 	vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY);
    310   1.8   mycroft 	if (ap->a_ioflag & IO_NDELAY)
    311   1.1       cgd 		wso->so_state &= ~SS_NBIO;
    312   1.1       cgd 	return (error);
    313   1.1       cgd }
    314   1.1       cgd 
    315   1.1       cgd /*
    316   1.1       cgd  * Device ioctl operation.
    317   1.1       cgd  */
    318   1.1       cgd /* ARGSUSED */
    319  1.17  christos int
    320  1.30     lukem fifo_ioctl(void *v)
    321  1.17  christos {
    322   1.8   mycroft 	struct vop_ioctl_args /* {
    323  1.30     lukem 		struct vnode	*a_vp;
    324  1.30     lukem 		u_long		a_command;
    325  1.48       jrf 		void		*a_data;
    326  1.30     lukem 		int		a_fflag;
    327  1.55      elad 		kauth_cred_t	a_cred;
    328  1.54  christos 		struct lwp	*a_l;
    329  1.17  christos 	} */ *ap = v;
    330  1.30     lukem 	struct file	filetmp;
    331  1.30     lukem 	int		error;
    332   1.1       cgd 
    333   1.8   mycroft 	if (ap->a_command == FIONBIO)
    334   1.1       cgd 		return (0);
    335  1.16   mycroft 	if (ap->a_fflag & FREAD) {
    336  1.48       jrf 		filetmp.f_data = ap->a_vp->v_fifoinfo->fi_readsock;
    337  1.54  christos 		error = soo_ioctl(&filetmp, ap->a_command, ap->a_data, ap->a_l);
    338  1.16   mycroft 		if (error)
    339  1.16   mycroft 			return (error);
    340  1.16   mycroft 	}
    341  1.16   mycroft 	if (ap->a_fflag & FWRITE) {
    342  1.48       jrf 		filetmp.f_data = ap->a_vp->v_fifoinfo->fi_writesock;
    343  1.54  christos 		error = soo_ioctl(&filetmp, ap->a_command, ap->a_data, ap->a_l);
    344  1.16   mycroft 		if (error)
    345  1.16   mycroft 			return (error);
    346  1.16   mycroft 	}
    347  1.16   mycroft 	return (0);
    348   1.1       cgd }
    349   1.1       cgd 
    350   1.1       cgd /* ARGSUSED */
    351  1.17  christos int
    352  1.30     lukem fifo_poll(void *v)
    353  1.17  christos {
    354  1.20   mycroft 	struct vop_poll_args /* {
    355  1.30     lukem 		struct vnode	*a_vp;
    356  1.30     lukem 		int		a_events;
    357  1.54  christos 		struct lwp	*a_l;
    358  1.17  christos 	} */ *ap = v;
    359  1.30     lukem 	struct file	filetmp;
    360  1.30     lukem 	int		revents;
    361   1.1       cgd 
    362  1.30     lukem 	revents = 0;
    363  1.20   mycroft 	if (ap->a_events & (POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND)) {
    364  1.48       jrf 		filetmp.f_data = ap->a_vp->v_fifoinfo->fi_readsock;
    365  1.20   mycroft 		if (filetmp.f_data)
    366  1.54  christos 			revents |= soo_poll(&filetmp, ap->a_events, ap->a_l);
    367  1.16   mycroft 	}
    368  1.20   mycroft 	if (ap->a_events & (POLLOUT | POLLWRNORM | POLLWRBAND)) {
    369  1.48       jrf 		filetmp.f_data = ap->a_vp->v_fifoinfo->fi_writesock;
    370  1.20   mycroft 		if (filetmp.f_data)
    371  1.54  christos 			revents |= soo_poll(&filetmp, ap->a_events, ap->a_l);
    372  1.16   mycroft 	}
    373  1.20   mycroft 
    374  1.20   mycroft 	return (revents);
    375   1.1       cgd }
    376   1.1       cgd 
    377  1.25      fvdl int
    378  1.30     lukem fifo_inactive(void *v)
    379  1.25      fvdl {
    380  1.25      fvdl 	struct vop_inactive_args /* {
    381  1.30     lukem 		struct vnode	*a_vp;
    382  1.54  christos 		struct lwp	*a_l;
    383  1.25      fvdl 	} */ *ap = v;
    384  1.25      fvdl 
    385  1.25      fvdl 	VOP_UNLOCK(ap->a_vp, 0);
    386  1.25      fvdl 	return (0);
    387  1.25      fvdl }
    388  1.25      fvdl 
    389   1.1       cgd /*
    390   1.1       cgd  * This is a noop, simply returning what one has been given.
    391   1.1       cgd  */
    392  1.17  christos int
    393  1.30     lukem fifo_bmap(void *v)
    394  1.17  christos {
    395   1.8   mycroft 	struct vop_bmap_args /* {
    396  1.30     lukem 		struct vnode	*a_vp;
    397  1.30     lukem 		daddr_t		a_bn;
    398  1.30     lukem 		struct vnode	**a_vpp;
    399  1.30     lukem 		daddr_t		*a_bnp;
    400  1.30     lukem 		int		*a_runp;
    401  1.17  christos 	} */ *ap = v;
    402   1.1       cgd 
    403   1.8   mycroft 	if (ap->a_vpp != NULL)
    404   1.8   mycroft 		*ap->a_vpp = ap->a_vp;
    405   1.8   mycroft 	if (ap->a_bnp != NULL)
    406   1.8   mycroft 		*ap->a_bnp = ap->a_bn;
    407  1.25      fvdl 	if (ap->a_runp != NULL)
    408  1.25      fvdl 		*ap->a_runp = 0;
    409   1.1       cgd 	return (0);
    410   1.1       cgd }
    411   1.1       cgd 
    412   1.1       cgd /*
    413   1.1       cgd  * Device close routine
    414   1.1       cgd  */
    415   1.1       cgd /* ARGSUSED */
    416  1.17  christos int
    417  1.30     lukem fifo_close(void *v)
    418  1.17  christos {
    419   1.8   mycroft 	struct vop_close_args /* {
    420  1.30     lukem 		struct vnode	*a_vp;
    421  1.30     lukem 		int		a_fflag;
    422  1.55      elad 		kauth_cred_t	a_cred;
    423  1.54  christos 		struct lwp	*a_l;
    424  1.17  christos 	} */ *ap = v;
    425  1.30     lukem 	struct vnode	*vp;
    426  1.30     lukem 	struct fifoinfo	*fip;
    427  1.50   mycroft 	int isrevoke;
    428   1.1       cgd 
    429  1.30     lukem 	vp = ap->a_vp;
    430  1.30     lukem 	fip = vp->v_fifoinfo;
    431  1.50   mycroft 	isrevoke = (ap->a_fflag & (FREAD | FWRITE | FNONBLOCK)) == FNONBLOCK;
    432  1.50   mycroft 	if (isrevoke) {
    433  1.50   mycroft 		if (fip->fi_readers != 0) {
    434  1.50   mycroft 			fip->fi_readers = 0;
    435  1.50   mycroft 			socantsendmore(fip->fi_writesock);
    436  1.50   mycroft 		}
    437  1.50   mycroft 		if (fip->fi_writers != 0) {
    438  1.50   mycroft 			fip->fi_writers = 0;
    439  1.50   mycroft 			socantrcvmore(fip->fi_readsock);
    440  1.50   mycroft 		}
    441  1.50   mycroft 	} else {
    442  1.50   mycroft 		if ((ap->a_fflag & FREAD) && --fip->fi_readers == 0)
    443  1.16   mycroft 			socantsendmore(fip->fi_writesock);
    444  1.50   mycroft 		if ((ap->a_fflag & FWRITE) && --fip->fi_writers == 0)
    445   1.1       cgd 			socantrcvmore(fip->fi_readsock);
    446   1.1       cgd 	}
    447  1.50   mycroft 	/* Shut down if all readers and writers are gone. */
    448  1.50   mycroft 	if ((fip->fi_readers + fip->fi_writers) == 0) {
    449  1.34       chs 		(void) soclose(fip->fi_readsock);
    450  1.34       chs 		(void) soclose(fip->fi_writesock);
    451  1.34       chs 		FREE(fip, M_VNODE);
    452  1.34       chs 		vp->v_fifoinfo = NULL;
    453  1.34       chs 	}
    454  1.34       chs 	return (0);
    455   1.1       cgd }
    456   1.1       cgd 
    457   1.1       cgd /*
    458   1.1       cgd  * Print out the contents of a fifo vnode.
    459   1.1       cgd  */
    460  1.17  christos int
    461  1.30     lukem fifo_print(void *v)
    462  1.17  christos {
    463   1.8   mycroft 	struct vop_print_args /* {
    464  1.30     lukem 		struct vnode	*a_vp;
    465  1.17  christos 	} */ *ap = v;
    466   1.1       cgd 
    467  1.22  christos 	printf("tag VT_NON");
    468   1.8   mycroft 	fifo_printinfo(ap->a_vp);
    469  1.22  christos 	printf("\n");
    470  1.17  christos 	return 0;
    471   1.1       cgd }
    472   1.1       cgd 
    473   1.1       cgd /*
    474   1.1       cgd  * Print out internal contents of a fifo vnode.
    475   1.1       cgd  */
    476  1.17  christos void
    477  1.30     lukem fifo_printinfo(struct vnode *vp)
    478   1.1       cgd {
    479  1.30     lukem 	struct fifoinfo	*fip;
    480   1.1       cgd 
    481  1.30     lukem 	fip = vp->v_fifoinfo;
    482  1.22  christos 	printf(", fifo with %ld readers and %ld writers",
    483  1.21  christos 	    fip->fi_readers, fip->fi_writers);
    484   1.1       cgd }
    485   1.1       cgd 
    486   1.1       cgd /*
    487   1.8   mycroft  * Return POSIX pathconf information applicable to fifo's.
    488   1.8   mycroft  */
    489  1.17  christos int
    490  1.30     lukem fifo_pathconf(void *v)
    491  1.17  christos {
    492   1.8   mycroft 	struct vop_pathconf_args /* {
    493  1.30     lukem 		struct vnode	*a_vp;
    494  1.30     lukem 		int		a_name;
    495  1.30     lukem 		register_t	*a_retval;
    496  1.17  christos 	} */ *ap = v;
    497   1.8   mycroft 
    498   1.8   mycroft 	switch (ap->a_name) {
    499   1.8   mycroft 	case _PC_LINK_MAX:
    500   1.8   mycroft 		*ap->a_retval = LINK_MAX;
    501   1.8   mycroft 		return (0);
    502   1.8   mycroft 	case _PC_PIPE_BUF:
    503   1.8   mycroft 		*ap->a_retval = PIPE_BUF;
    504   1.8   mycroft 		return (0);
    505   1.8   mycroft 	case _PC_CHOWN_RESTRICTED:
    506  1.26    kleink 		*ap->a_retval = 1;
    507  1.26    kleink 		return (0);
    508  1.26    kleink 	case _PC_SYNC_IO:
    509   1.8   mycroft 		*ap->a_retval = 1;
    510   1.8   mycroft 		return (0);
    511   1.8   mycroft 	default:
    512   1.8   mycroft 		return (EINVAL);
    513   1.8   mycroft 	}
    514   1.1       cgd 	/* NOTREACHED */
    515  1.36  jdolecek }
    516  1.36  jdolecek 
    517  1.36  jdolecek static void
    518  1.36  jdolecek filt_fifordetach(struct knote *kn)
    519  1.36  jdolecek {
    520  1.36  jdolecek 	struct socket *so;
    521  1.36  jdolecek 
    522  1.36  jdolecek 	so = (struct socket *)kn->kn_hook;
    523  1.37  christos 	SLIST_REMOVE(&so->so_rcv.sb_sel.sel_klist, kn, knote, kn_selnext);
    524  1.37  christos 	if (SLIST_EMPTY(&so->so_rcv.sb_sel.sel_klist))
    525  1.36  jdolecek 		so->so_rcv.sb_flags &= ~SB_KNOTE;
    526  1.36  jdolecek }
    527  1.36  jdolecek 
    528  1.36  jdolecek static int
    529  1.36  jdolecek filt_fiforead(struct knote *kn, long hint)
    530  1.36  jdolecek {
    531  1.36  jdolecek 	struct socket *so;
    532  1.36  jdolecek 
    533  1.36  jdolecek 	so = (struct socket *)kn->kn_hook;
    534  1.36  jdolecek 	kn->kn_data = so->so_rcv.sb_cc;
    535  1.36  jdolecek 	if (so->so_state & SS_CANTRCVMORE) {
    536  1.36  jdolecek 		kn->kn_flags |= EV_EOF;
    537  1.36  jdolecek 		return (1);
    538  1.36  jdolecek 	}
    539  1.36  jdolecek 	kn->kn_flags &= ~EV_EOF;
    540  1.36  jdolecek 	return (kn->kn_data > 0);
    541  1.36  jdolecek }
    542  1.36  jdolecek 
    543  1.36  jdolecek static void
    544  1.36  jdolecek filt_fifowdetach(struct knote *kn)
    545  1.36  jdolecek {
    546  1.36  jdolecek 	struct socket *so;
    547  1.36  jdolecek 
    548  1.36  jdolecek 	so = (struct socket *)kn->kn_hook;
    549  1.37  christos 	SLIST_REMOVE(&so->so_snd.sb_sel.sel_klist, kn, knote, kn_selnext);
    550  1.37  christos 	if (SLIST_EMPTY(&so->so_snd.sb_sel.sel_klist))
    551  1.36  jdolecek 		so->so_snd.sb_flags &= ~SB_KNOTE;
    552  1.36  jdolecek }
    553  1.36  jdolecek 
    554  1.36  jdolecek static int
    555  1.36  jdolecek filt_fifowrite(struct knote *kn, long hint)
    556  1.36  jdolecek {
    557  1.36  jdolecek 	struct socket *so;
    558  1.36  jdolecek 
    559  1.36  jdolecek 	so = (struct socket *)kn->kn_hook;
    560  1.36  jdolecek 	kn->kn_data = sbspace(&so->so_snd);
    561  1.36  jdolecek 	if (so->so_state & SS_CANTSENDMORE) {
    562  1.36  jdolecek 		kn->kn_flags |= EV_EOF;
    563  1.36  jdolecek 		return (1);
    564  1.36  jdolecek 	}
    565  1.36  jdolecek 	kn->kn_flags &= ~EV_EOF;
    566  1.36  jdolecek 	return (kn->kn_data >= so->so_snd.sb_lowat);
    567  1.36  jdolecek }
    568  1.36  jdolecek 
    569  1.36  jdolecek static const struct filterops fiforead_filtops =
    570  1.36  jdolecek 	{ 1, NULL, filt_fifordetach, filt_fiforead };
    571  1.36  jdolecek static const struct filterops fifowrite_filtops =
    572  1.36  jdolecek 	{ 1, NULL, filt_fifowdetach, filt_fifowrite };
    573  1.36  jdolecek 
    574  1.36  jdolecek /* ARGSUSED */
    575  1.36  jdolecek int
    576  1.36  jdolecek fifo_kqfilter(void *v)
    577  1.36  jdolecek {
    578  1.36  jdolecek 	struct vop_kqfilter_args /* {
    579  1.36  jdolecek 		struct vnode *a_vp;
    580  1.36  jdolecek 		struct knote *a_kn;
    581  1.36  jdolecek 	} */ *ap = v;
    582  1.36  jdolecek 	struct socket	*so;
    583  1.36  jdolecek 	struct sockbuf	*sb;
    584  1.36  jdolecek 
    585  1.36  jdolecek 	so = (struct socket *)ap->a_vp->v_fifoinfo->fi_readsock;
    586  1.36  jdolecek 	switch (ap->a_kn->kn_filter) {
    587  1.36  jdolecek 	case EVFILT_READ:
    588  1.36  jdolecek 		ap->a_kn->kn_fop = &fiforead_filtops;
    589  1.36  jdolecek 		sb = &so->so_rcv;
    590  1.36  jdolecek 		break;
    591  1.36  jdolecek 	case EVFILT_WRITE:
    592  1.36  jdolecek 		ap->a_kn->kn_fop = &fifowrite_filtops;
    593  1.36  jdolecek 		sb = &so->so_snd;
    594  1.36  jdolecek 		break;
    595  1.36  jdolecek 	default:
    596  1.36  jdolecek 		return (1);
    597  1.36  jdolecek 	}
    598  1.36  jdolecek 
    599  1.36  jdolecek 	ap->a_kn->kn_hook = so;
    600  1.36  jdolecek 
    601  1.37  christos 	SLIST_INSERT_HEAD(&sb->sb_sel.sel_klist, ap->a_kn, kn_selnext);
    602  1.36  jdolecek 	sb->sb_flags |= SB_KNOTE;
    603  1.36  jdolecek 	return (0);
    604   1.1       cgd }
    605