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