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