Home | History | Annotate | Line # | Download | only in fifofs
fifo_vnops.c revision 1.57.30.2
      1  1.57.30.2       mjf /*	$NetBSD: fifo_vnops.c,v 1.57.30.2 2008/02/18 21:07:00 mjf 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.57.30.2       mjf __KERNEL_RCSID(0, "$NetBSD: fifo_vnops.c,v 1.57.30.2 2008/02/18 21:07:00 mjf 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.57.30.2       mjf #include <sys/atomic.h>
     55       1.19   mycroft 
     56        1.6   mycroft #include <miscfs/fifofs/fifo.h>
     57       1.19   mycroft #include <miscfs/genfs/genfs.h>
     58        1.1       cgd 
     59        1.1       cgd /*
     60        1.1       cgd  * This structure is associated with the FIFO vnode and stores
     61        1.1       cgd  * the state associated with the FIFO.
     62        1.1       cgd  */
     63        1.1       cgd struct fifoinfo {
     64        1.1       cgd 	struct socket	*fi_readsock;
     65        1.1       cgd 	struct socket	*fi_writesock;
     66        1.1       cgd 	long		fi_readers;
     67        1.1       cgd 	long		fi_writers;
     68        1.1       cgd };
     69        1.1       cgd 
     70       1.52   xtraeme int (**fifo_vnodeop_p)(void *);
     71       1.29  jdolecek const struct vnodeopv_entry_desc fifo_vnodeop_entries[] = {
     72        1.8   mycroft 	{ &vop_default_desc, vn_default_error },
     73        1.8   mycroft 	{ &vop_lookup_desc, fifo_lookup },		/* lookup */
     74        1.8   mycroft 	{ &vop_create_desc, fifo_create },		/* create */
     75        1.8   mycroft 	{ &vop_mknod_desc, fifo_mknod },		/* mknod */
     76        1.8   mycroft 	{ &vop_open_desc, fifo_open },			/* open */
     77        1.8   mycroft 	{ &vop_close_desc, fifo_close },		/* close */
     78        1.8   mycroft 	{ &vop_access_desc, fifo_access },		/* access */
     79        1.8   mycroft 	{ &vop_getattr_desc, fifo_getattr },		/* getattr */
     80        1.8   mycroft 	{ &vop_setattr_desc, fifo_setattr },		/* setattr */
     81        1.8   mycroft 	{ &vop_read_desc, fifo_read },			/* read */
     82        1.8   mycroft 	{ &vop_write_desc, fifo_write },		/* write */
     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.17  christos 	} */ *ap = v;
    146  1.57.30.1       mjf 	struct lwp	*l = curlwp;
    147       1.30     lukem 	struct vnode	*vp;
    148       1.30     lukem 	struct fifoinfo	*fip;
    149       1.30     lukem 	struct proc	*p;
    150       1.30     lukem 	struct socket	*rso, *wso;
    151       1.30     lukem 	int		error;
    152        1.1       cgd 
    153       1.30     lukem 	vp = ap->a_vp;
    154  1.57.30.1       mjf 	p = l->l_proc;
    155       1.30     lukem 
    156  1.57.30.2       mjf 	KERNEL_LOCK(1, NULL);
    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.57.30.2       mjf 			goto done;
    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.57.30.2       mjf 			goto done;
    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.57.30.2       mjf 			goto done;
    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.57.30.2       mjf 	KERNEL_UNLOCK_ONE(NULL);
    231       1.16   mycroft 	return (0);
    232       1.30     lukem  bad:
    233  1.57.30.1       mjf 	VOP_CLOSE(vp, ap->a_mode, ap->a_cred);
    234  1.57.30.2       mjf  done:
    235  1.57.30.2       mjf 	KERNEL_UNLOCK_ONE(NULL);
    236        1.1       cgd 	return (error);
    237        1.1       cgd }
    238        1.1       cgd 
    239        1.1       cgd /*
    240        1.1       cgd  * Vnode op for read
    241        1.1       cgd  */
    242        1.1       cgd /* ARGSUSED */
    243       1.17  christos int
    244       1.30     lukem fifo_read(void *v)
    245       1.17  christos {
    246        1.8   mycroft 	struct vop_read_args /* {
    247       1.30     lukem 		struct vnode	*a_vp;
    248       1.30     lukem 		struct uio	*a_uio;
    249       1.30     lukem 		int		a_ioflag;
    250       1.55      elad 		kauth_cred_t	a_cred;
    251       1.17  christos 	} */ *ap = v;
    252       1.30     lukem 	struct uio	*uio;
    253       1.30     lukem 	struct socket	*rso;
    254       1.35   thorpej 	int		error;
    255       1.35   thorpej 	size_t		startresid;
    256        1.1       cgd 
    257       1.30     lukem 	uio = ap->a_uio;
    258       1.30     lukem 	rso = ap->a_vp->v_fifoinfo->fi_readsock;
    259        1.1       cgd #ifdef DIAGNOSTIC
    260        1.1       cgd 	if (uio->uio_rw != UIO_READ)
    261        1.1       cgd 		panic("fifo_read mode");
    262        1.1       cgd #endif
    263        1.1       cgd 	if (uio->uio_resid == 0)
    264        1.1       cgd 		return (0);
    265  1.57.30.2       mjf 	KERNEL_LOCK(1, NULL);
    266        1.8   mycroft 	if (ap->a_ioflag & IO_NDELAY)
    267  1.57.30.2       mjf 		rso->so_nbio = 1;
    268        1.1       cgd 	startresid = uio->uio_resid;
    269       1.25      fvdl 	VOP_UNLOCK(ap->a_vp, 0);
    270       1.27      matt 	error = (*rso->so_receive)(rso, (struct mbuf **)0, uio,
    271       1.27      matt 	    (struct mbuf **)0, (struct mbuf **)0, (int *)0);
    272       1.25      fvdl 	vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY);
    273        1.1       cgd 	/*
    274        1.1       cgd 	 * Clear EOF indication after first such return.
    275        1.1       cgd 	 */
    276        1.1       cgd 	if (uio->uio_resid == startresid)
    277        1.1       cgd 		rso->so_state &= ~SS_CANTRCVMORE;
    278       1.23    kleink 	if (ap->a_ioflag & IO_NDELAY) {
    279  1.57.30.2       mjf 		rso->so_nbio = 0;
    280       1.23    kleink 		if (error == EWOULDBLOCK &&
    281       1.23    kleink 		    ap->a_vp->v_fifoinfo->fi_writers == 0)
    282       1.23    kleink 			error = 0;
    283       1.23    kleink 	}
    284  1.57.30.2       mjf 	KERNEL_UNLOCK_ONE(NULL);
    285        1.1       cgd 	return (error);
    286        1.1       cgd }
    287        1.1       cgd 
    288        1.1       cgd /*
    289        1.1       cgd  * Vnode op for write
    290        1.1       cgd  */
    291        1.1       cgd /* ARGSUSED */
    292       1.17  christos int
    293       1.30     lukem fifo_write(void *v)
    294       1.17  christos {
    295        1.8   mycroft 	struct vop_write_args /* {
    296       1.30     lukem 		struct vnode	*a_vp;
    297       1.30     lukem 		struct uio	*a_uio;
    298       1.30     lukem 		int		a_ioflag;
    299       1.55      elad 		kauth_cred_t	a_cred;
    300       1.17  christos 	} */ *ap = v;
    301       1.30     lukem 	struct socket	*wso;
    302       1.30     lukem 	int		error;
    303        1.1       cgd 
    304       1.30     lukem 	wso = ap->a_vp->v_fifoinfo->fi_writesock;
    305        1.1       cgd #ifdef DIAGNOSTIC
    306        1.8   mycroft 	if (ap->a_uio->uio_rw != UIO_WRITE)
    307        1.1       cgd 		panic("fifo_write mode");
    308        1.1       cgd #endif
    309  1.57.30.2       mjf 	KERNEL_LOCK(1, NULL);
    310        1.8   mycroft 	if (ap->a_ioflag & IO_NDELAY)
    311  1.57.30.2       mjf 		wso->so_nbio = 1;
    312       1.25      fvdl 	VOP_UNLOCK(ap->a_vp, 0);
    313       1.27      matt 	error = (*wso->so_send)(wso, (struct mbuf *)0, ap->a_uio, 0,
    314       1.54  christos 	    (struct mbuf *)0, 0, curlwp /*XXX*/);
    315       1.25      fvdl 	vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY);
    316        1.8   mycroft 	if (ap->a_ioflag & IO_NDELAY)
    317  1.57.30.2       mjf 		wso->so_nbio = 0;
    318  1.57.30.2       mjf 	KERNEL_UNLOCK_ONE(NULL);
    319        1.1       cgd 	return (error);
    320        1.1       cgd }
    321        1.1       cgd 
    322        1.1       cgd /*
    323        1.1       cgd  * Device ioctl operation.
    324        1.1       cgd  */
    325        1.1       cgd /* ARGSUSED */
    326       1.17  christos int
    327       1.30     lukem fifo_ioctl(void *v)
    328       1.17  christos {
    329        1.8   mycroft 	struct vop_ioctl_args /* {
    330       1.30     lukem 		struct vnode	*a_vp;
    331       1.30     lukem 		u_long		a_command;
    332       1.48       jrf 		void		*a_data;
    333       1.30     lukem 		int		a_fflag;
    334       1.55      elad 		kauth_cred_t	a_cred;
    335       1.54  christos 		struct lwp	*a_l;
    336       1.17  christos 	} */ *ap = v;
    337       1.30     lukem 	struct file	filetmp;
    338       1.30     lukem 	int		error;
    339        1.1       cgd 
    340        1.8   mycroft 	if (ap->a_command == FIONBIO)
    341        1.1       cgd 		return (0);
    342       1.16   mycroft 	if (ap->a_fflag & FREAD) {
    343       1.48       jrf 		filetmp.f_data = ap->a_vp->v_fifoinfo->fi_readsock;
    344  1.57.30.1       mjf 		error = soo_ioctl(&filetmp, ap->a_command, ap->a_data, curlwp);
    345       1.16   mycroft 		if (error)
    346       1.16   mycroft 			return (error);
    347       1.16   mycroft 	}
    348       1.16   mycroft 	if (ap->a_fflag & FWRITE) {
    349       1.48       jrf 		filetmp.f_data = ap->a_vp->v_fifoinfo->fi_writesock;
    350  1.57.30.1       mjf 		error = soo_ioctl(&filetmp, ap->a_command, ap->a_data, curlwp);
    351       1.16   mycroft 		if (error)
    352       1.16   mycroft 			return (error);
    353       1.16   mycroft 	}
    354       1.16   mycroft 	return (0);
    355        1.1       cgd }
    356        1.1       cgd 
    357        1.1       cgd /* ARGSUSED */
    358       1.17  christos int
    359       1.30     lukem fifo_poll(void *v)
    360       1.17  christos {
    361       1.20   mycroft 	struct vop_poll_args /* {
    362       1.30     lukem 		struct vnode	*a_vp;
    363       1.30     lukem 		int		a_events;
    364       1.54  christos 		struct lwp	*a_l;
    365       1.17  christos 	} */ *ap = v;
    366       1.30     lukem 	struct file	filetmp;
    367       1.30     lukem 	int		revents;
    368        1.1       cgd 
    369       1.30     lukem 	revents = 0;
    370       1.20   mycroft 	if (ap->a_events & (POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND)) {
    371       1.48       jrf 		filetmp.f_data = ap->a_vp->v_fifoinfo->fi_readsock;
    372       1.20   mycroft 		if (filetmp.f_data)
    373  1.57.30.1       mjf 			revents |= soo_poll(&filetmp, ap->a_events, curlwp);
    374       1.16   mycroft 	}
    375       1.20   mycroft 	if (ap->a_events & (POLLOUT | POLLWRNORM | POLLWRBAND)) {
    376       1.48       jrf 		filetmp.f_data = ap->a_vp->v_fifoinfo->fi_writesock;
    377       1.20   mycroft 		if (filetmp.f_data)
    378  1.57.30.1       mjf 			revents |= soo_poll(&filetmp, ap->a_events, curlwp);
    379       1.16   mycroft 	}
    380       1.20   mycroft 
    381       1.20   mycroft 	return (revents);
    382        1.1       cgd }
    383        1.1       cgd 
    384       1.25      fvdl int
    385       1.30     lukem fifo_inactive(void *v)
    386       1.25      fvdl {
    387       1.25      fvdl 	struct vop_inactive_args /* {
    388       1.30     lukem 		struct vnode	*a_vp;
    389       1.54  christos 		struct lwp	*a_l;
    390       1.25      fvdl 	} */ *ap = v;
    391       1.25      fvdl 
    392       1.25      fvdl 	VOP_UNLOCK(ap->a_vp, 0);
    393       1.25      fvdl 	return (0);
    394       1.25      fvdl }
    395       1.25      fvdl 
    396        1.1       cgd /*
    397        1.1       cgd  * This is a noop, simply returning what one has been given.
    398        1.1       cgd  */
    399       1.17  christos int
    400       1.30     lukem fifo_bmap(void *v)
    401       1.17  christos {
    402        1.8   mycroft 	struct vop_bmap_args /* {
    403       1.30     lukem 		struct vnode	*a_vp;
    404       1.30     lukem 		daddr_t		a_bn;
    405       1.30     lukem 		struct vnode	**a_vpp;
    406       1.30     lukem 		daddr_t		*a_bnp;
    407       1.30     lukem 		int		*a_runp;
    408       1.17  christos 	} */ *ap = v;
    409        1.1       cgd 
    410        1.8   mycroft 	if (ap->a_vpp != NULL)
    411        1.8   mycroft 		*ap->a_vpp = ap->a_vp;
    412        1.8   mycroft 	if (ap->a_bnp != NULL)
    413        1.8   mycroft 		*ap->a_bnp = ap->a_bn;
    414       1.25      fvdl 	if (ap->a_runp != NULL)
    415       1.25      fvdl 		*ap->a_runp = 0;
    416        1.1       cgd 	return (0);
    417        1.1       cgd }
    418        1.1       cgd 
    419        1.1       cgd /*
    420        1.1       cgd  * Device close routine
    421        1.1       cgd  */
    422        1.1       cgd /* ARGSUSED */
    423       1.17  christos int
    424       1.30     lukem fifo_close(void *v)
    425       1.17  christos {
    426        1.8   mycroft 	struct vop_close_args /* {
    427       1.30     lukem 		struct vnode	*a_vp;
    428       1.30     lukem 		int		a_fflag;
    429       1.55      elad 		kauth_cred_t	a_cred;
    430       1.54  christos 		struct lwp	*a_l;
    431       1.17  christos 	} */ *ap = v;
    432       1.30     lukem 	struct vnode	*vp;
    433       1.30     lukem 	struct fifoinfo	*fip;
    434       1.50   mycroft 	int isrevoke;
    435        1.1       cgd 
    436       1.30     lukem 	vp = ap->a_vp;
    437       1.30     lukem 	fip = vp->v_fifoinfo;
    438       1.50   mycroft 	isrevoke = (ap->a_fflag & (FREAD | FWRITE | FNONBLOCK)) == FNONBLOCK;
    439  1.57.30.2       mjf 	KERNEL_LOCK(1, NULL);
    440       1.50   mycroft 	if (isrevoke) {
    441       1.50   mycroft 		if (fip->fi_readers != 0) {
    442       1.50   mycroft 			fip->fi_readers = 0;
    443       1.50   mycroft 			socantsendmore(fip->fi_writesock);
    444       1.50   mycroft 		}
    445       1.50   mycroft 		if (fip->fi_writers != 0) {
    446       1.50   mycroft 			fip->fi_writers = 0;
    447       1.50   mycroft 			socantrcvmore(fip->fi_readsock);
    448       1.50   mycroft 		}
    449       1.50   mycroft 	} else {
    450       1.50   mycroft 		if ((ap->a_fflag & FREAD) && --fip->fi_readers == 0)
    451       1.16   mycroft 			socantsendmore(fip->fi_writesock);
    452       1.50   mycroft 		if ((ap->a_fflag & FWRITE) && --fip->fi_writers == 0)
    453        1.1       cgd 			socantrcvmore(fip->fi_readsock);
    454        1.1       cgd 	}
    455       1.50   mycroft 	/* Shut down if all readers and writers are gone. */
    456       1.50   mycroft 	if ((fip->fi_readers + fip->fi_writers) == 0) {
    457       1.34       chs 		(void) soclose(fip->fi_readsock);
    458       1.34       chs 		(void) soclose(fip->fi_writesock);
    459       1.34       chs 		FREE(fip, M_VNODE);
    460       1.34       chs 		vp->v_fifoinfo = NULL;
    461       1.34       chs 	}
    462  1.57.30.2       mjf 	KERNEL_UNLOCK_ONE(NULL);
    463       1.34       chs 	return (0);
    464        1.1       cgd }
    465        1.1       cgd 
    466        1.1       cgd /*
    467        1.1       cgd  * Print out the contents of a fifo vnode.
    468        1.1       cgd  */
    469       1.17  christos int
    470       1.30     lukem fifo_print(void *v)
    471       1.17  christos {
    472        1.8   mycroft 	struct vop_print_args /* {
    473       1.30     lukem 		struct vnode	*a_vp;
    474       1.17  christos 	} */ *ap = v;
    475        1.1       cgd 
    476       1.22  christos 	printf("tag VT_NON");
    477        1.8   mycroft 	fifo_printinfo(ap->a_vp);
    478       1.22  christos 	printf("\n");
    479       1.17  christos 	return 0;
    480        1.1       cgd }
    481        1.1       cgd 
    482        1.1       cgd /*
    483        1.1       cgd  * Print out internal contents of a fifo vnode.
    484        1.1       cgd  */
    485       1.17  christos void
    486       1.30     lukem fifo_printinfo(struct vnode *vp)
    487        1.1       cgd {
    488       1.30     lukem 	struct fifoinfo	*fip;
    489        1.1       cgd 
    490       1.30     lukem 	fip = vp->v_fifoinfo;
    491       1.22  christos 	printf(", fifo with %ld readers and %ld writers",
    492       1.21  christos 	    fip->fi_readers, fip->fi_writers);
    493        1.1       cgd }
    494        1.1       cgd 
    495        1.1       cgd /*
    496        1.8   mycroft  * Return POSIX pathconf information applicable to fifo's.
    497        1.8   mycroft  */
    498       1.17  christos int
    499       1.30     lukem fifo_pathconf(void *v)
    500       1.17  christos {
    501        1.8   mycroft 	struct vop_pathconf_args /* {
    502       1.30     lukem 		struct vnode	*a_vp;
    503       1.30     lukem 		int		a_name;
    504       1.30     lukem 		register_t	*a_retval;
    505       1.17  christos 	} */ *ap = v;
    506        1.8   mycroft 
    507        1.8   mycroft 	switch (ap->a_name) {
    508        1.8   mycroft 	case _PC_LINK_MAX:
    509        1.8   mycroft 		*ap->a_retval = LINK_MAX;
    510        1.8   mycroft 		return (0);
    511        1.8   mycroft 	case _PC_PIPE_BUF:
    512        1.8   mycroft 		*ap->a_retval = PIPE_BUF;
    513        1.8   mycroft 		return (0);
    514        1.8   mycroft 	case _PC_CHOWN_RESTRICTED:
    515       1.26    kleink 		*ap->a_retval = 1;
    516       1.26    kleink 		return (0);
    517       1.26    kleink 	case _PC_SYNC_IO:
    518        1.8   mycroft 		*ap->a_retval = 1;
    519        1.8   mycroft 		return (0);
    520        1.8   mycroft 	default:
    521        1.8   mycroft 		return (EINVAL);
    522        1.8   mycroft 	}
    523        1.1       cgd 	/* NOTREACHED */
    524       1.36  jdolecek }
    525       1.36  jdolecek 
    526       1.36  jdolecek static void
    527       1.36  jdolecek filt_fifordetach(struct knote *kn)
    528       1.36  jdolecek {
    529       1.36  jdolecek 	struct socket *so;
    530       1.36  jdolecek 
    531       1.36  jdolecek 	so = (struct socket *)kn->kn_hook;
    532       1.37  christos 	SLIST_REMOVE(&so->so_rcv.sb_sel.sel_klist, kn, knote, kn_selnext);
    533       1.37  christos 	if (SLIST_EMPTY(&so->so_rcv.sb_sel.sel_klist))
    534       1.36  jdolecek 		so->so_rcv.sb_flags &= ~SB_KNOTE;
    535       1.36  jdolecek }
    536       1.36  jdolecek 
    537       1.36  jdolecek static int
    538       1.57  christos filt_fiforead(struct knote *kn, long hint)
    539       1.36  jdolecek {
    540       1.36  jdolecek 	struct socket *so;
    541       1.36  jdolecek 
    542       1.36  jdolecek 	so = (struct socket *)kn->kn_hook;
    543       1.36  jdolecek 	kn->kn_data = so->so_rcv.sb_cc;
    544       1.36  jdolecek 	if (so->so_state & SS_CANTRCVMORE) {
    545       1.36  jdolecek 		kn->kn_flags |= EV_EOF;
    546       1.36  jdolecek 		return (1);
    547       1.36  jdolecek 	}
    548       1.36  jdolecek 	kn->kn_flags &= ~EV_EOF;
    549       1.36  jdolecek 	return (kn->kn_data > 0);
    550       1.36  jdolecek }
    551       1.36  jdolecek 
    552       1.36  jdolecek static void
    553       1.36  jdolecek filt_fifowdetach(struct knote *kn)
    554       1.36  jdolecek {
    555       1.36  jdolecek 	struct socket *so;
    556       1.36  jdolecek 
    557       1.36  jdolecek 	so = (struct socket *)kn->kn_hook;
    558       1.37  christos 	SLIST_REMOVE(&so->so_snd.sb_sel.sel_klist, kn, knote, kn_selnext);
    559       1.37  christos 	if (SLIST_EMPTY(&so->so_snd.sb_sel.sel_klist))
    560       1.36  jdolecek 		so->so_snd.sb_flags &= ~SB_KNOTE;
    561       1.36  jdolecek }
    562       1.36  jdolecek 
    563       1.36  jdolecek static int
    564       1.57  christos filt_fifowrite(struct knote *kn, long hint)
    565       1.36  jdolecek {
    566       1.36  jdolecek 	struct socket *so;
    567       1.36  jdolecek 
    568       1.36  jdolecek 	so = (struct socket *)kn->kn_hook;
    569       1.36  jdolecek 	kn->kn_data = sbspace(&so->so_snd);
    570       1.36  jdolecek 	if (so->so_state & SS_CANTSENDMORE) {
    571       1.36  jdolecek 		kn->kn_flags |= EV_EOF;
    572       1.36  jdolecek 		return (1);
    573       1.36  jdolecek 	}
    574       1.36  jdolecek 	kn->kn_flags &= ~EV_EOF;
    575       1.36  jdolecek 	return (kn->kn_data >= so->so_snd.sb_lowat);
    576       1.36  jdolecek }
    577       1.36  jdolecek 
    578       1.36  jdolecek static const struct filterops fiforead_filtops =
    579       1.36  jdolecek 	{ 1, NULL, filt_fifordetach, filt_fiforead };
    580       1.36  jdolecek static const struct filterops fifowrite_filtops =
    581       1.36  jdolecek 	{ 1, NULL, filt_fifowdetach, filt_fifowrite };
    582       1.36  jdolecek 
    583       1.36  jdolecek /* ARGSUSED */
    584       1.36  jdolecek int
    585       1.36  jdolecek fifo_kqfilter(void *v)
    586       1.36  jdolecek {
    587       1.36  jdolecek 	struct vop_kqfilter_args /* {
    588       1.36  jdolecek 		struct vnode *a_vp;
    589       1.36  jdolecek 		struct knote *a_kn;
    590       1.36  jdolecek 	} */ *ap = v;
    591       1.36  jdolecek 	struct socket	*so;
    592       1.36  jdolecek 	struct sockbuf	*sb;
    593       1.36  jdolecek 
    594       1.36  jdolecek 	so = (struct socket *)ap->a_vp->v_fifoinfo->fi_readsock;
    595       1.36  jdolecek 	switch (ap->a_kn->kn_filter) {
    596       1.36  jdolecek 	case EVFILT_READ:
    597       1.36  jdolecek 		ap->a_kn->kn_fop = &fiforead_filtops;
    598       1.36  jdolecek 		sb = &so->so_rcv;
    599       1.36  jdolecek 		break;
    600       1.36  jdolecek 	case EVFILT_WRITE:
    601       1.36  jdolecek 		ap->a_kn->kn_fop = &fifowrite_filtops;
    602       1.36  jdolecek 		sb = &so->so_snd;
    603       1.36  jdolecek 		break;
    604       1.36  jdolecek 	default:
    605  1.57.30.1       mjf 		return (EINVAL);
    606       1.36  jdolecek 	}
    607       1.36  jdolecek 
    608       1.36  jdolecek 	ap->a_kn->kn_hook = so;
    609       1.36  jdolecek 
    610       1.37  christos 	SLIST_INSERT_HEAD(&sb->sb_sel.sel_klist, ap->a_kn, kn_selnext);
    611       1.36  jdolecek 	sb->sb_flags |= SB_KNOTE;
    612       1.36  jdolecek 	return (0);
    613        1.1       cgd }
    614