xref: /freebsd/sys/fs/fifofs/fifo_vnops.c (revision 195ebc7e9e4b129de810833791a19dfb4349d6a9)
1 /*-
2  * Copyright (c) 1990, 1993, 1995
3  *	The Regents of the University of California.
4  * Copyright (c) 2005 Robert N. M. Watson
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  * 4. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  *	@(#)fifo_vnops.c	8.10 (Berkeley) 5/27/95
32  * $FreeBSD$
33  */
34 
35 #include <sys/param.h>
36 #include <sys/event.h>
37 #include <sys/file.h>
38 #include <sys/filedesc.h>
39 #include <sys/filio.h>
40 #include <sys/fcntl.h>
41 #include <sys/kernel.h>
42 #include <sys/lock.h>
43 #include <sys/mutex.h>
44 #include <sys/malloc.h>
45 #include <sys/poll.h>
46 #include <sys/proc.h>
47 #include <sys/signalvar.h>
48 #include <sys/socket.h>
49 #include <sys/socketvar.h>
50 #include <sys/sx.h>
51 #include <sys/systm.h>
52 #include <sys/un.h>
53 #include <sys/unistd.h>
54 #include <sys/vnode.h>
55 #include <fs/fifofs/fifo.h>
56 
57 static fo_rdwr_t        fifo_read_f;
58 static fo_rdwr_t        fifo_write_f;
59 static fo_ioctl_t       fifo_ioctl_f;
60 static fo_poll_t        fifo_poll_f;
61 static fo_kqfilter_t    fifo_kqfilter_f;
62 static fo_stat_t        fifo_stat_f;
63 static fo_close_t       fifo_close_f;
64 static fo_truncate_t    fifo_truncate_f;
65 
66 struct fileops fifo_ops_f = {
67 	.fo_read =      fifo_read_f,
68 	.fo_write =     fifo_write_f,
69 	.fo_truncate =  fifo_truncate_f,
70 	.fo_ioctl =     fifo_ioctl_f,
71 	.fo_poll =      fifo_poll_f,
72 	.fo_kqfilter =  fifo_kqfilter_f,
73 	.fo_stat =      fifo_stat_f,
74 	.fo_close =     fifo_close_f,
75 	.fo_flags =     DFLAG_PASSABLE
76 };
77 
78 /*
79  * This structure is associated with the FIFO vnode and stores
80  * the state associated with the FIFO.
81  */
82 struct fifoinfo {
83 	struct socket	*fi_readsock;
84 	struct socket	*fi_writesock;
85 	long		fi_readers;
86 	long		fi_writers;
87 };
88 
89 static vop_print_t	fifo_print;
90 static vop_open_t	fifo_open;
91 static vop_close_t	fifo_close;
92 static vop_ioctl_t	fifo_ioctl;
93 static vop_kqfilter_t	fifo_kqfilter;
94 static vop_pathconf_t	fifo_pathconf;
95 static vop_advlock_t	fifo_advlock;
96 
97 static void	filt_fifordetach(struct knote *kn);
98 static int	filt_fiforead(struct knote *kn, long hint);
99 static void	filt_fifowdetach(struct knote *kn);
100 static int	filt_fifowrite(struct knote *kn, long hint);
101 static void	filt_fifodetach_notsup(struct knote *kn);
102 static int	filt_fifo_notsup(struct knote *kn, long hint);
103 
104 static struct filterops fiforead_filtops =
105 	{ 1, NULL, filt_fifordetach, filt_fiforead };
106 static struct filterops fifowrite_filtops =
107 	{ 1, NULL, filt_fifowdetach, filt_fifowrite };
108 static struct filterops fifo_notsup_filtops =
109 	{ 1, NULL, filt_fifodetach_notsup, filt_fifo_notsup };
110 
111 struct vop_vector fifo_specops = {
112 	.vop_default =		&default_vnodeops,
113 
114 	.vop_access =		VOP_EBADF,
115 	.vop_advlock =		fifo_advlock,
116 	.vop_close =		fifo_close,
117 	.vop_create =		VOP_PANIC,
118 	.vop_getattr =		VOP_EBADF,
119 	.vop_ioctl =		fifo_ioctl,
120 	.vop_kqfilter =		fifo_kqfilter,
121 	.vop_link =		VOP_PANIC,
122 	.vop_mkdir =		VOP_PANIC,
123 	.vop_mknod =		VOP_PANIC,
124 	.vop_open =		fifo_open,
125 	.vop_pathconf =		fifo_pathconf,
126 	.vop_print =		fifo_print,
127 	.vop_read =		VOP_PANIC,
128 	.vop_readdir =		VOP_PANIC,
129 	.vop_readlink =		VOP_PANIC,
130 	.vop_reallocblks =	VOP_PANIC,
131 	.vop_reclaim =		VOP_NULL,
132 	.vop_remove =		VOP_PANIC,
133 	.vop_rename =		VOP_PANIC,
134 	.vop_rmdir =		VOP_PANIC,
135 	.vop_setattr =		VOP_EBADF,
136 	.vop_symlink =		VOP_PANIC,
137 	.vop_write =		VOP_PANIC,
138 };
139 
140 struct mtx fifo_mtx;
141 MTX_SYSINIT(fifo, &fifo_mtx, "fifo mutex", MTX_DEF);
142 
143 /*
144  * Dispose of fifo resources.
145  */
146 static void
147 fifo_cleanup(struct vnode *vp)
148 {
149 	struct fifoinfo *fip = vp->v_fifoinfo;
150 
151 	ASSERT_VOP_ELOCKED(vp, "fifo_cleanup");
152 	if (fip->fi_readers == 0 && fip->fi_writers == 0) {
153 		vp->v_fifoinfo = NULL;
154 		(void)soclose(fip->fi_readsock);
155 		(void)soclose(fip->fi_writesock);
156 		free(fip, M_VNODE);
157 	}
158 }
159 
160 /*
161  * Open called to set up a new instance of a fifo or
162  * to find an active instance of a fifo.
163  */
164 /* ARGSUSED */
165 static int
166 fifo_open(ap)
167 	struct vop_open_args /* {
168 		struct vnode *a_vp;
169 		int  a_mode;
170 		struct ucred *a_cred;
171 		struct thread *a_td;
172 		int a_fdidx;
173 	} */ *ap;
174 {
175 	struct vnode *vp = ap->a_vp;
176 	struct fifoinfo *fip;
177 	struct thread *td = ap->a_td;
178 	struct ucred *cred = ap->a_cred;
179 	struct file *fp = ap->a_fp;
180 	struct socket *rso, *wso;
181 	int error;
182 
183 	ASSERT_VOP_ELOCKED(vp, "fifo_open");
184 	if (fp == NULL)
185 		return (EINVAL);
186 	if ((fip = vp->v_fifoinfo) == NULL) {
187 		fip = malloc(sizeof(*fip), M_VNODE, M_WAITOK);
188 		error = socreate(AF_LOCAL, &rso, SOCK_STREAM, 0, cred, td);
189 		if (error)
190 			goto fail1;
191 		fip->fi_readsock = rso;
192 		error = socreate(AF_LOCAL, &wso, SOCK_STREAM, 0, cred, td);
193 		if (error)
194 			goto fail2;
195 		fip->fi_writesock = wso;
196 		error = soconnect2(wso, rso);
197 		if (error) {
198 			(void)soclose(wso);
199 fail2:
200 			(void)soclose(rso);
201 fail1:
202 			free(fip, M_VNODE);
203 			return (error);
204 		}
205 		fip->fi_readers = fip->fi_writers = 0;
206 		wso->so_snd.sb_lowat = PIPE_BUF;
207 		SOCKBUF_LOCK(&rso->so_rcv);
208 		rso->so_rcv.sb_state |= SBS_CANTRCVMORE;
209 		SOCKBUF_UNLOCK(&rso->so_rcv);
210 		KASSERT(vp->v_fifoinfo == NULL,
211 		    ("fifo_open: v_fifoinfo race"));
212 		vp->v_fifoinfo = fip;
213 	}
214 
215 	/*
216 	 * General access to fi_readers and fi_writers is protected using
217 	 * the vnode lock.
218 	 *
219 	 * Protect the increment of fi_readers and fi_writers and the
220 	 * associated calls to wakeup() with the fifo mutex in addition
221 	 * to the vnode lock.  This allows the vnode lock to be dropped
222 	 * for the msleep() calls below, and using the fifo mutex with
223 	 * msleep() prevents the wakeup from being missed.
224 	 */
225 	mtx_lock(&fifo_mtx);
226 	if (ap->a_mode & FREAD) {
227 		fip->fi_readers++;
228 		if (fip->fi_readers == 1) {
229 			SOCKBUF_LOCK(&fip->fi_writesock->so_snd);
230 			fip->fi_writesock->so_snd.sb_state &= ~SBS_CANTSENDMORE;
231 			SOCKBUF_UNLOCK(&fip->fi_writesock->so_snd);
232 			if (fip->fi_writers > 0) {
233 				wakeup(&fip->fi_writers);
234 				sowwakeup(fip->fi_writesock);
235 			}
236 		}
237 	}
238 	if (ap->a_mode & FWRITE) {
239 		if ((ap->a_mode & O_NONBLOCK) && fip->fi_readers == 0) {
240 			mtx_unlock(&fifo_mtx);
241 			return (ENXIO);
242 		}
243 		fip->fi_writers++;
244 		if (fip->fi_writers == 1) {
245 			SOCKBUF_LOCK(&fip->fi_readsock->so_rcv);
246 			fip->fi_readsock->so_rcv.sb_state &= ~SBS_CANTRCVMORE;
247 			SOCKBUF_UNLOCK(&fip->fi_readsock->so_rcv);
248 			if (fip->fi_readers > 0) {
249 				wakeup(&fip->fi_readers);
250 				sorwakeup(fip->fi_readsock);
251 			}
252 		}
253 	}
254 	if ((ap->a_mode & O_NONBLOCK) == 0) {
255 		if ((ap->a_mode & FREAD) && fip->fi_writers == 0) {
256 			VOP_UNLOCK(vp, 0);
257 			error = msleep(&fip->fi_readers, &fifo_mtx,
258 			    PDROP | PCATCH | PSOCK, "fifoor", 0);
259 			vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
260 			if (error) {
261 				fip->fi_readers--;
262 				if (fip->fi_readers == 0) {
263 					socantsendmore(fip->fi_writesock);
264 					fifo_cleanup(vp);
265 				}
266 				return (error);
267 			}
268 			mtx_lock(&fifo_mtx);
269 			/*
270 			 * We must have got woken up because we had a writer.
271 			 * That (and not still having one) is the condition
272 			 * that we must wait for.
273 			 */
274 		}
275 		if ((ap->a_mode & FWRITE) && fip->fi_readers == 0) {
276 			VOP_UNLOCK(vp, 0);
277 			error = msleep(&fip->fi_writers, &fifo_mtx,
278 			    PDROP | PCATCH | PSOCK, "fifoow", 0);
279 			vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
280 			if (error) {
281 				fip->fi_writers--;
282 				if (fip->fi_writers == 0) {
283 					socantrcvmore(fip->fi_readsock);
284 					fifo_cleanup(vp);
285 				}
286 				return (error);
287 			}
288 			/*
289 			 * We must have got woken up because we had
290 			 * a reader.  That (and not still having one)
291 			 * is the condition that we must wait for.
292 			 */
293 			mtx_lock(&fifo_mtx);
294 		}
295 	}
296 	mtx_unlock(&fifo_mtx);
297 	KASSERT(fp != NULL, ("can't fifo/vnode bypass"));
298 	KASSERT(fp->f_ops == &badfileops, ("not badfileops in fifo_open"));
299 	finit(fp, fp->f_flag, DTYPE_FIFO, fip, &fifo_ops_f);
300 	return (0);
301 }
302 
303 /*
304  * Now unused vnode ioctl routine.
305  */
306 /* ARGSUSED */
307 static int
308 fifo_ioctl(ap)
309 	struct vop_ioctl_args /* {
310 		struct vnode *a_vp;
311 		u_long  a_command;
312 		caddr_t  a_data;
313 		int  a_fflag;
314 		struct ucred *a_cred;
315 		struct thread *a_td;
316 	} */ *ap;
317 {
318 
319 	printf("WARNING: fifo_ioctl called unexpectedly\n");
320 	return (ENOTTY);
321 }
322 
323 /*
324  * Now unused vnode kqfilter routine.
325  */
326 /* ARGSUSED */
327 static int
328 fifo_kqfilter(ap)
329 	struct vop_kqfilter_args /* {
330 		struct vnode *a_vp;
331 		struct knote *a_kn;
332 	} */ *ap;
333 {
334 
335 	printf("WARNING: fifo_kqfilter called unexpectedly\n");
336 	return (EINVAL);
337 }
338 
339 static void
340 filt_fifordetach(struct knote *kn)
341 {
342 	struct socket *so = (struct socket *)kn->kn_hook;
343 
344 	SOCKBUF_LOCK(&so->so_rcv);
345 	knlist_remove(&so->so_rcv.sb_sel.si_note, kn, 1);
346 	if (knlist_empty(&so->so_rcv.sb_sel.si_note))
347 		so->so_rcv.sb_flags &= ~SB_KNOTE;
348 	SOCKBUF_UNLOCK(&so->so_rcv);
349 }
350 
351 static int
352 filt_fiforead(struct knote *kn, long hint)
353 {
354 	struct socket *so = (struct socket *)kn->kn_hook;
355 
356 	SOCKBUF_LOCK_ASSERT(&so->so_rcv);
357 	kn->kn_data = so->so_rcv.sb_cc;
358 	if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
359 		kn->kn_flags |= EV_EOF;
360 		return (1);
361 	} else {
362 		kn->kn_flags &= ~EV_EOF;
363 		return (kn->kn_data > 0);
364 	}
365 }
366 
367 static void
368 filt_fifowdetach(struct knote *kn)
369 {
370 	struct socket *so = (struct socket *)kn->kn_hook;
371 
372 	SOCKBUF_LOCK(&so->so_snd);
373 	knlist_remove(&so->so_snd.sb_sel.si_note, kn, 1);
374 	if (knlist_empty(&so->so_snd.sb_sel.si_note))
375 		so->so_snd.sb_flags &= ~SB_KNOTE;
376 	SOCKBUF_UNLOCK(&so->so_snd);
377 }
378 
379 static int
380 filt_fifowrite(struct knote *kn, long hint)
381 {
382 	struct socket *so = (struct socket *)kn->kn_hook;
383 
384 	SOCKBUF_LOCK_ASSERT(&so->so_snd);
385 	kn->kn_data = sbspace(&so->so_snd);
386 	if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
387 		kn->kn_flags |= EV_EOF;
388 		return (1);
389 	} else {
390 		kn->kn_flags &= ~EV_EOF;
391 	        return (kn->kn_data >= so->so_snd.sb_lowat);
392 	}
393 }
394 
395 static void
396 filt_fifodetach_notsup(struct knote *kn)
397 {
398 
399 }
400 
401 static int
402 filt_fifo_notsup(struct knote *kn, long hint)
403 {
404 
405 	return (0);
406 }
407 
408 /*
409  * Device close routine
410  */
411 /* ARGSUSED */
412 static int
413 fifo_close(ap)
414 	struct vop_close_args /* {
415 		struct vnode *a_vp;
416 		int  a_fflag;
417 		struct ucred *a_cred;
418 		struct thread *a_td;
419 	} */ *ap;
420 {
421 	struct vnode *vp = ap->a_vp;
422 	struct fifoinfo *fip = vp->v_fifoinfo;
423 
424 	ASSERT_VOP_ELOCKED(vp, "fifo_close");
425 	if (fip == NULL) {
426 		printf("fifo_close: no v_fifoinfo %p\n", vp);
427 		return (0);
428 	}
429 	if (ap->a_fflag & FREAD) {
430 		fip->fi_readers--;
431 		if (fip->fi_readers == 0)
432 			socantsendmore(fip->fi_writesock);
433 	}
434 	if (ap->a_fflag & FWRITE) {
435 		fip->fi_writers--;
436 		if (fip->fi_writers == 0)
437 			socantrcvmore(fip->fi_readsock);
438 	}
439 	fifo_cleanup(vp);
440 	return (0);
441 }
442 
443 /*
444  * Print out internal contents of a fifo vnode.
445  */
446 int
447 fifo_printinfo(vp)
448 	struct vnode *vp;
449 {
450 	register struct fifoinfo *fip = vp->v_fifoinfo;
451 
452 	if (fip == NULL){
453 		printf(", NULL v_fifoinfo");
454 		return (0);
455 	}
456 	printf(", fifo with %ld readers and %ld writers",
457 		fip->fi_readers, fip->fi_writers);
458 	return (0);
459 }
460 
461 /*
462  * Print out the contents of a fifo vnode.
463  */
464 static int
465 fifo_print(ap)
466 	struct vop_print_args /* {
467 		struct vnode *a_vp;
468 	} */ *ap;
469 {
470 	printf("    ");
471 	fifo_printinfo(ap->a_vp);
472 	printf("\n");
473 	return (0);
474 }
475 
476 /*
477  * Return POSIX pathconf information applicable to fifo's.
478  */
479 static int
480 fifo_pathconf(ap)
481 	struct vop_pathconf_args /* {
482 		struct vnode *a_vp;
483 		int a_name;
484 		int *a_retval;
485 	} */ *ap;
486 {
487 
488 	switch (ap->a_name) {
489 	case _PC_LINK_MAX:
490 		*ap->a_retval = LINK_MAX;
491 		return (0);
492 	case _PC_PIPE_BUF:
493 		*ap->a_retval = PIPE_BUF;
494 		return (0);
495 	case _PC_CHOWN_RESTRICTED:
496 		*ap->a_retval = 1;
497 		return (0);
498 	default:
499 		return (EINVAL);
500 	}
501 	/* NOTREACHED */
502 }
503 
504 /*
505  * Fifo advisory byte-level locks.
506  */
507 /* ARGSUSED */
508 static int
509 fifo_advlock(ap)
510 	struct vop_advlock_args /* {
511 		struct vnode *a_vp;
512 		caddr_t  a_id;
513 		int  a_op;
514 		struct flock *a_fl;
515 		int  a_flags;
516 	} */ *ap;
517 {
518 
519 	return (ap->a_flags & F_FLOCK ? EOPNOTSUPP : EINVAL);
520 }
521 
522 static int
523 fifo_close_f(struct file *fp, struct thread *td)
524 {
525 
526 	return (vnops.fo_close(fp, td));
527 }
528 
529 /*
530  * The implementation of ioctl() for named fifos is complicated by the fact
531  * that we permit O_RDWR fifo file descriptors, meaning that the actions of
532  * ioctls may have to be applied to both the underlying sockets rather than
533  * just one.  The original implementation simply forward the ioctl to one
534  * or both sockets based on fp->f_flag.  We now consider each ioctl
535  * separately, as the composition effect requires careful ordering.
536  *
537  * We do not blindly pass all ioctls through to the socket in order to avoid
538  * providing unnecessary ioctls that might be improperly depended on by
539  * applications (such as socket-specific, routing, and interface ioctls).
540  *
541  * Unlike sys_pipe.c, fifos do not implement the deprecated TIOCSPGRP and
542  * TIOCGPGRP ioctls.  Earlier implementations of fifos did forward SIOCSPGRP
543  * and SIOCGPGRP ioctls, so we might need to re-add those here.
544  */
545 static int
546 fifo_ioctl_f(struct file *fp, u_long com, void *data, struct ucred *cred,
547     struct thread *td)
548 {
549 	struct fifoinfo *fi;
550 	struct file filetmp;	/* Local, so need not be locked. */
551 	int error;
552 
553 	error = ENOTTY;
554 	fi = fp->f_data;
555 
556 	switch (com) {
557 	case FIONBIO:
558 		/*
559 		 * Non-blocking I/O is implemented at the fifo layer using
560 		 * MSG_NBIO, so does not need to be forwarded down the stack.
561 		 */
562 		return (0);
563 
564 	case FIOASYNC:
565 	case FIOSETOWN:
566 	case FIOGETOWN:
567 		/*
568 		 * These socket ioctls don't have any ordering requirements,
569 		 * so are called in an arbitrary order, and only on the
570 		 * sockets indicated by the file descriptor rights.
571 		 *
572 		 * XXXRW: If O_RDWR and the read socket accepts an ioctl but
573 		 * the write socket doesn't, the socketpair is left in an
574 		 * inconsistent state.
575 		 */
576 		if (fp->f_flag & FREAD) {
577 			filetmp.f_data = fi->fi_readsock;
578 			filetmp.f_cred = cred;
579 			error = soo_ioctl(&filetmp, com, data, cred, td);
580 			if (error)
581 				return (error);
582 		}
583 		if (fp->f_flag & FWRITE) {
584 			filetmp.f_data = fi->fi_writesock;
585 			filetmp.f_cred = cred;
586 			error = soo_ioctl(&filetmp, com, data, cred, td);
587 		}
588 		return (error);
589 
590 	case FIONREAD:
591 		/*
592 		 * FIONREAD will return 0 for non-readable descriptors, and
593 		 * the results of FIONREAD on the read socket for readable
594 		 * descriptors.
595 		 */
596 		if (!(fp->f_flag & FREAD)) {
597 			*(int *)data = 0;
598 			return (0);
599 		}
600 		filetmp.f_data = fi->fi_readsock;
601 		filetmp.f_cred = cred;
602 		return (soo_ioctl(&filetmp, com, data, cred, td));
603 
604 	default:
605 		return (ENOTTY);
606 	}
607 }
608 
609 /*
610  * Because fifos are now a file descriptor layer object, EVFILT_VNODE is not
611  * implemented.  Likely, fifo_kqfilter() should be removed, and
612  * fifo_kqfilter_f() should know how to forward the request to the underling
613  * vnode using f_vnode in the file descriptor here.
614  */
615 static int
616 fifo_kqfilter_f(struct file *fp, struct knote *kn)
617 {
618 	struct fifoinfo *fi;
619 	struct socket *so;
620 	struct sockbuf *sb;
621 
622 	fi = fp->f_data;
623 
624 	/*
625 	 * If a filter is requested that is not supported by this file
626 	 * descriptor, don't return an error, but also don't ever generate an
627 	 * event.
628 	 */
629 	if ((kn->kn_filter == EVFILT_READ) && !(fp->f_flag & FREAD)) {
630 		kn->kn_fop = &fifo_notsup_filtops;
631 		return (0);
632 	}
633 
634 	if ((kn->kn_filter == EVFILT_WRITE) && !(fp->f_flag & FWRITE)) {
635 		kn->kn_fop = &fifo_notsup_filtops;
636 		return (0);
637 	}
638 
639 	switch (kn->kn_filter) {
640 	case EVFILT_READ:
641 		kn->kn_fop = &fiforead_filtops;
642 		so = fi->fi_readsock;
643 		sb = &so->so_rcv;
644 		break;
645 	case EVFILT_WRITE:
646 		kn->kn_fop = &fifowrite_filtops;
647 		so = fi->fi_writesock;
648 		sb = &so->so_snd;
649 		break;
650 	default:
651 		return (EINVAL);
652 	}
653 
654 	kn->kn_hook = (caddr_t)so;
655 
656 	SOCKBUF_LOCK(sb);
657 	knlist_add(&sb->sb_sel.si_note, kn, 1);
658 	sb->sb_flags |= SB_KNOTE;
659 	SOCKBUF_UNLOCK(sb);
660 
661 	return (0);
662 }
663 
664 static int
665 fifo_poll_f(struct file *fp, int events, struct ucred *cred, struct thread *td)
666 {
667 	struct fifoinfo *fip;
668 	struct file filetmp;
669 	int levents, revents = 0;
670 
671 	fip = fp->f_data;
672 	levents = events &
673 	    (POLLIN | POLLINIGNEOF | POLLPRI | POLLRDNORM | POLLRDBAND);
674 	if ((fp->f_flag & FREAD) && levents) {
675 		/*
676 		 * If POLLIN or POLLRDNORM is requested and POLLINIGNEOF is
677 		 * not, then convert the first two to the last one.  This
678 		 * tells the socket poll function to ignore EOF so that we
679 		 * block if there is no writer (and no data).  Callers can
680 		 * set POLLINIGNEOF to get non-blocking behavior.
681 		 */
682 		if (levents & (POLLIN | POLLRDNORM) &&
683 		    !(levents & POLLINIGNEOF)) {
684 			levents &= ~(POLLIN | POLLRDNORM);
685 			levents |= POLLINIGNEOF;
686 		}
687 
688 		filetmp.f_data = fip->fi_readsock;
689 		filetmp.f_cred = cred;
690 		revents |= soo_poll(&filetmp, levents, cred, td);
691 
692 		/* Reverse the above conversion. */
693 		if ((revents & POLLINIGNEOF) && !(events & POLLINIGNEOF)) {
694 			revents |= (events & (POLLIN | POLLRDNORM));
695 			revents &= ~POLLINIGNEOF;
696 		}
697 	}
698 	levents = events & (POLLOUT | POLLWRNORM | POLLWRBAND);
699 	if ((fp->f_flag & FWRITE) && levents) {
700 		filetmp.f_data = fip->fi_writesock;
701 		filetmp.f_cred = cred;
702 		revents |= soo_poll(&filetmp, levents, cred, td);
703 	}
704 	return (revents);
705 }
706 
707 static int
708 fifo_read_f(struct file *fp, struct uio *uio, struct ucred *cred, int flags, struct thread *td)
709 {
710 	struct fifoinfo *fip;
711 	int sflags;
712 
713 	fip = fp->f_data;
714 	KASSERT(uio->uio_rw == UIO_READ,("fifo_read mode"));
715 	if (uio->uio_resid == 0)
716 		return (0);
717 	sflags = (fp->f_flag & FNONBLOCK) ? MSG_NBIO : 0;
718 	return (soreceive(fip->fi_readsock, NULL, uio, NULL, NULL, &sflags));
719 }
720 
721 static int
722 fifo_stat_f(struct file *fp, struct stat *sb, struct ucred *cred, struct thread *td)
723 {
724 
725 	return (vnops.fo_stat(fp, sb, cred, td));
726 }
727 
728 static int
729 fifo_truncate_f(struct file *fp, off_t length, struct ucred *cred, struct thread *td)
730 {
731 
732 	return (vnops.fo_truncate(fp, length, cred, td));
733 }
734 
735 static int
736 fifo_write_f(struct file *fp, struct uio *uio, struct ucred *cred, int flags, struct thread *td)
737 {
738 	struct fifoinfo *fip;
739 	int sflags;
740 
741 	fip = fp->f_data;
742 	KASSERT(uio->uio_rw == UIO_WRITE,("fifo_write mode"));
743 	sflags = (fp->f_flag & FNONBLOCK) ? MSG_NBIO : 0;
744 	return (sosend(fip->fi_writesock, NULL, uio, 0, NULL, sflags, td));
745 }
746