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