2 * Copyright (c) 1990, 1993, 1995
3 * The Regents of the University of California.
4 * Copyright (c) 2005 Robert N. M. Watson
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
31 * @(#)fifo_vnops.c 8.10 (Berkeley) 5/27/95
35 #include <sys/param.h>
36 #include <sys/event.h>
38 #include <sys/filedesc.h>
39 #include <sys/filio.h>
40 #include <sys/fcntl.h>
41 #include <sys/kernel.h>
43 #include <sys/mutex.h>
44 #include <sys/malloc.h>
46 #include <sys/proc.h> /* XXXKSE */
47 #include <sys/signalvar.h>
48 #include <sys/socket.h>
49 #include <sys/socketvar.h>
51 #include <sys/systm.h>
53 #include <sys/unistd.h>
54 #include <sys/vnode.h>
55 #include <fs/fifofs/fifo.h>
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;
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
77 * This structure is associated with the FIFO vnode and stores the state
78 * associated with the FIFO.
80 * XXXRW: The presence of an sx lock here is undesirable, and exists to avoid
81 * exposing threading race conditions in the socket code that have not yet
82 * been resolved. Once those problems are resolved, the sx lock here should
86 struct socket *fi_readsock;
87 struct socket *fi_writesock;
93 static vop_print_t fifo_print;
94 static vop_open_t fifo_open;
95 static vop_close_t fifo_close;
96 static vop_ioctl_t fifo_ioctl;
97 static vop_kqfilter_t fifo_kqfilter;
98 static vop_pathconf_t fifo_pathconf;
99 static vop_advlock_t fifo_advlock;
101 static void filt_fifordetach(struct knote *kn);
102 static int filt_fiforead(struct knote *kn, long hint);
103 static void filt_fifowdetach(struct knote *kn);
104 static int filt_fifowrite(struct knote *kn, long hint);
105 static void filt_fifodetach_notsup(struct knote *kn);
106 static int filt_fifo_notsup(struct knote *kn, long hint);
108 static struct filterops fiforead_filtops =
109 { 1, NULL, filt_fifordetach, filt_fiforead };
110 static struct filterops fifowrite_filtops =
111 { 1, NULL, filt_fifowdetach, filt_fifowrite };
112 static struct filterops fifo_notsup_filtops =
113 { 1, NULL, filt_fifodetach_notsup, filt_fifo_notsup };
115 struct vop_vector fifo_specops = {
116 .vop_default = &default_vnodeops,
118 .vop_access = VOP_EBADF,
119 .vop_advlock = fifo_advlock,
120 .vop_close = fifo_close,
121 .vop_create = VOP_PANIC,
122 .vop_getattr = VOP_EBADF,
123 .vop_ioctl = fifo_ioctl,
124 .vop_kqfilter = fifo_kqfilter,
125 .vop_lease = VOP_NULL,
126 .vop_link = VOP_PANIC,
127 .vop_mkdir = VOP_PANIC,
128 .vop_mknod = VOP_PANIC,
129 .vop_open = fifo_open,
130 .vop_pathconf = fifo_pathconf,
131 .vop_print = fifo_print,
132 .vop_read = VOP_PANIC,
133 .vop_readdir = VOP_PANIC,
134 .vop_readlink = VOP_PANIC,
135 .vop_reallocblks = VOP_PANIC,
136 .vop_reclaim = VOP_NULL,
137 .vop_remove = VOP_PANIC,
138 .vop_rename = VOP_PANIC,
139 .vop_rmdir = VOP_PANIC,
140 .vop_setattr = VOP_EBADF,
141 .vop_symlink = VOP_PANIC,
142 .vop_write = VOP_PANIC,
146 MTX_SYSINIT(fifo, &fifo_mtx, "fifo mutex", MTX_DEF);
149 * Dispose of fifo resources.
152 fifo_cleanup(struct vnode *vp)
154 struct fifoinfo *fip = vp->v_fifoinfo;
156 ASSERT_VOP_LOCKED(vp, "fifo_cleanup");
157 if (fip->fi_readers == 0 && fip->fi_writers == 0) {
158 vp->v_fifoinfo = NULL;
159 (void)soclose(fip->fi_readsock);
160 (void)soclose(fip->fi_writesock);
161 sx_destroy(&fip->fi_sx);
167 * Open called to set up a new instance of a fifo or
168 * to find an active instance of a fifo.
173 struct vop_open_args /* {
176 struct ucred *a_cred;
180 struct vnode *vp = ap->a_vp;
181 struct fifoinfo *fip;
182 struct thread *td = ap->a_td;
183 struct ucred *cred = ap->a_cred;
184 struct socket *rso, *wso;
188 ASSERT_VOP_LOCKED(vp, "fifo_open");
189 if ((fip = vp->v_fifoinfo) == NULL) {
190 MALLOC(fip, struct fifoinfo *, sizeof(*fip), M_VNODE,
192 error = socreate(AF_LOCAL, &rso, SOCK_STREAM, 0, cred, td);
195 fip->fi_readsock = rso;
196 error = socreate(AF_LOCAL, &wso, SOCK_STREAM, 0, cred, td);
199 fip->fi_writesock = wso;
200 error = soconnect2(wso, rso);
209 fip->fi_readers = fip->fi_writers = 0;
210 sx_init(&fip->fi_sx, "fifo_sx");
211 wso->so_snd.sb_lowat = PIPE_BUF;
212 SOCKBUF_LOCK(&rso->so_rcv);
213 rso->so_rcv.sb_state |= SBS_CANTRCVMORE;
214 SOCKBUF_UNLOCK(&rso->so_rcv);
215 KASSERT(vp->v_fifoinfo == NULL,
216 ("fifo_open: v_fifoinfo race"));
217 vp->v_fifoinfo = fip;
221 * General access to fi_readers and fi_writers is protected using
224 * Protect the increment of fi_readers and fi_writers and the
225 * associated calls to wakeup() with the fifo mutex in addition
226 * to the vnode lock. This allows the vnode lock to be dropped
227 * for the msleep() calls below, and using the fifo mutex with
228 * msleep() prevents the wakeup from being missed.
231 if (ap->a_mode & FREAD) {
233 if (fip->fi_readers == 1) {
234 SOCKBUF_LOCK(&fip->fi_writesock->so_snd);
235 fip->fi_writesock->so_snd.sb_state &= ~SBS_CANTSENDMORE;
236 SOCKBUF_UNLOCK(&fip->fi_writesock->so_snd);
237 if (fip->fi_writers > 0) {
238 wakeup(&fip->fi_writers);
239 sowwakeup(fip->fi_writesock);
243 if (ap->a_mode & FWRITE) {
244 if ((ap->a_mode & O_NONBLOCK) && fip->fi_readers == 0) {
245 mtx_unlock(&fifo_mtx);
249 if (fip->fi_writers == 1) {
250 SOCKBUF_LOCK(&fip->fi_readsock->so_rcv);
251 fip->fi_readsock->so_rcv.sb_state &= ~SBS_CANTRCVMORE;
252 SOCKBUF_UNLOCK(&fip->fi_readsock->so_rcv);
253 if (fip->fi_readers > 0) {
254 wakeup(&fip->fi_readers);
255 sorwakeup(fip->fi_readsock);
259 if ((ap->a_mode & O_NONBLOCK) == 0) {
260 if ((ap->a_mode & FREAD) && fip->fi_writers == 0) {
261 VOP_UNLOCK(vp, 0, td);
262 error = msleep(&fip->fi_readers, &fifo_mtx,
263 PDROP | PCATCH | PSOCK, "fifoor", 0);
264 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
267 if (fip->fi_readers == 0) {
268 socantsendmore(fip->fi_writesock);
275 * We must have got woken up because we had a writer.
276 * That (and not still having one) is the condition
277 * that we must wait for.
280 if ((ap->a_mode & FWRITE) && fip->fi_readers == 0) {
281 VOP_UNLOCK(vp, 0, td);
282 error = msleep(&fip->fi_writers, &fifo_mtx,
283 PDROP | PCATCH | PSOCK, "fifoow", 0);
284 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
287 if (fip->fi_writers == 0) {
288 socantrcvmore(fip->fi_readsock);
294 * We must have got woken up because we had
295 * a reader. That (and not still having one)
296 * is the condition that we must wait for.
301 mtx_unlock(&fifo_mtx);
302 KASSERT(ap->a_fdidx >= 0, ("can't fifo/vnode bypass %d", ap->a_fdidx));
303 fp = ap->a_td->td_proc->p_fd->fd_ofiles[ap->a_fdidx];
304 KASSERT(fp->f_ops == &badfileops, ("not badfileops in fifo_open"));
305 fp->f_ops = &fifo_ops_f;
311 * Now unused vnode ioctl routine.
316 struct vop_ioctl_args /* {
321 struct ucred *a_cred;
326 printf("WARNING: fifo_ioctl called unexpectedly\n");
331 * Now unused vnode kqfilter routine.
336 struct vop_kqfilter_args /* {
342 printf("WARNING: fifo_kqfilter called unexpectedly\n");
347 filt_fifordetach(struct knote *kn)
349 struct socket *so = (struct socket *)kn->kn_hook;
351 SOCKBUF_LOCK(&so->so_rcv);
352 knlist_remove(&so->so_rcv.sb_sel.si_note, kn, 1);
353 if (knlist_empty(&so->so_rcv.sb_sel.si_note))
354 so->so_rcv.sb_flags &= ~SB_KNOTE;
355 SOCKBUF_UNLOCK(&so->so_rcv);
359 filt_fiforead(struct knote *kn, long hint)
361 struct socket *so = (struct socket *)kn->kn_hook;
363 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
364 kn->kn_data = so->so_rcv.sb_cc;
365 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
366 kn->kn_flags |= EV_EOF;
369 kn->kn_flags &= ~EV_EOF;
370 return (kn->kn_data > 0);
375 filt_fifowdetach(struct knote *kn)
377 struct socket *so = (struct socket *)kn->kn_hook;
379 SOCKBUF_LOCK(&so->so_snd);
380 knlist_remove(&so->so_snd.sb_sel.si_note, kn, 1);
381 if (knlist_empty(&so->so_snd.sb_sel.si_note))
382 so->so_snd.sb_flags &= ~SB_KNOTE;
383 SOCKBUF_UNLOCK(&so->so_snd);
387 filt_fifowrite(struct knote *kn, long hint)
389 struct socket *so = (struct socket *)kn->kn_hook;
391 SOCKBUF_LOCK_ASSERT(&so->so_snd);
392 kn->kn_data = sbspace(&so->so_snd);
393 if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
394 kn->kn_flags |= EV_EOF;
397 kn->kn_flags &= ~EV_EOF;
398 return (kn->kn_data >= so->so_snd.sb_lowat);
403 filt_fifodetach_notsup(struct knote *kn)
409 filt_fifo_notsup(struct knote *kn, long hint)
416 * Device close routine
421 struct vop_close_args /* {
424 struct ucred *a_cred;
428 struct vnode *vp = ap->a_vp;
429 struct fifoinfo *fip = vp->v_fifoinfo;
431 ASSERT_VOP_LOCKED(vp, "fifo_close");
432 if (ap->a_fflag & FREAD) {
434 if (fip->fi_readers == 0)
435 socantsendmore(fip->fi_writesock);
437 if (ap->a_fflag & FWRITE) {
439 if (fip->fi_writers == 0)
440 socantrcvmore(fip->fi_readsock);
447 * Print out internal contents of a fifo vnode.
453 register struct fifoinfo *fip = vp->v_fifoinfo;
455 printf(", fifo with %ld readers and %ld writers",
456 fip->fi_readers, fip->fi_writers);
461 * Print out the contents of a fifo vnode.
465 struct vop_print_args /* {
469 fifo_printinfo(ap->a_vp);
475 * Return POSIX pathconf information applicable to fifo's.
479 struct vop_pathconf_args /* {
486 switch (ap->a_name) {
488 *ap->a_retval = LINK_MAX;
491 *ap->a_retval = PIPE_BUF;
493 case _PC_CHOWN_RESTRICTED:
503 * Fifo advisory byte-level locks.
508 struct vop_advlock_args /* {
517 return (ap->a_flags & F_FLOCK ? EOPNOTSUPP : EINVAL);
521 fifo_close_f(struct file *fp, struct thread *td)
524 return (vnops.fo_close(fp, td));
528 * The implementation of ioctl() for named fifos is complicated by the fact
529 * that we permit O_RDWR fifo file descriptors, meaning that the actions of
530 * ioctls may have to be applied to both the underlying sockets rather than
531 * just one. The original implementation simply forward the ioctl to one
532 * or both sockets based on fp->f_flag. We now consider each ioctl
533 * separately, as the composition effect requires careful ordering.
535 * We do not blindly pass all ioctls through to the socket in order to avoid
536 * providing unnecessary ioctls that might be improperly depended on by
537 * applications (such as socket-specific, routing, and interface ioctls).
539 * Unlike sys_pipe.c, fifos do not implement the deprecated TIOCSPGRP and
540 * TIOCGPGRP ioctls. Earlier implementations of fifos did forward SIOCSPGRP
541 * and SIOCGPGRP ioctls, so we might need to re-add those here.
544 fifo_ioctl_f(struct file *fp, u_long com, void *data, struct ucred *cred,
548 struct file filetmp; /* Local, so need not be locked. */
557 * Non-blocking I/O is implemented at the fifo layer using
558 * MSG_NBIO, so does not need to be forwarded down the stack.
566 * These socket ioctls don't have any ordering requirements,
567 * so are called in an arbitrary order, and only on the
568 * sockets indicated by the file descriptor rights.
570 * XXXRW: If O_RDWR and the read socket accepts an ioctl but
571 * the write socket doesn't, the socketpair is left in an
572 * inconsistent state.
574 if (fp->f_flag & FREAD) {
575 filetmp.f_data = fi->fi_readsock;
576 filetmp.f_cred = cred;
577 error = soo_ioctl(&filetmp, com, data, cred, td);
581 if (fp->f_flag & FWRITE) {
582 filetmp.f_data = fi->fi_writesock;
583 filetmp.f_cred = cred;
584 error = soo_ioctl(&filetmp, com, data, cred, td);
590 * FIONREAD will return 0 for non-readable descriptors, and
591 * the results of FIONREAD on the read socket for readable
594 if (!(fp->f_flag & FREAD)) {
598 filetmp.f_data = fi->fi_readsock;
599 filetmp.f_cred = cred;
600 return (soo_ioctl(&filetmp, com, data, cred, td));
608 * Because fifos are now a file descriptor layer object, EVFILT_VNODE is not
609 * implemented. Likely, fifo_kqfilter() should be removed, and
610 * fifo_kqfilter_f() should know how to forward the request to the underling
611 * vnode using f_vnode in the file descriptor here.
614 fifo_kqfilter_f(struct file *fp, struct knote *kn)
623 * If a filter is requested that is not supported by this file
624 * descriptor, don't return an error, but also don't ever generate an
627 if ((kn->kn_filter == EVFILT_READ) && !(fp->f_flag & FREAD)) {
628 kn->kn_fop = &fifo_notsup_filtops;
632 if ((kn->kn_filter == EVFILT_WRITE) && !(fp->f_flag & FWRITE)) {
633 kn->kn_fop = &fifo_notsup_filtops;
637 switch (kn->kn_filter) {
639 kn->kn_fop = &fiforead_filtops;
640 so = fi->fi_readsock;
644 kn->kn_fop = &fifowrite_filtops;
645 so = fi->fi_writesock;
652 kn->kn_hook = (caddr_t)so;
655 knlist_add(&sb->sb_sel.si_note, kn, 1);
656 sb->sb_flags |= SB_KNOTE;
663 fifo_poll_f(struct file *fp, int events, struct ucred *cred, struct thread *td)
665 struct fifoinfo *fip;
667 int levents, revents = 0;
671 (POLLIN | POLLINIGNEOF | POLLPRI | POLLRDNORM | POLLRDBAND);
672 if ((fp->f_flag & FREAD) && levents) {
674 * If POLLIN or POLLRDNORM is requested and POLLINIGNEOF is
675 * not, then convert the first two to the last one. This
676 * tells the socket poll function to ignore EOF so that we
677 * block if there is no writer (and no data). Callers can
678 * set POLLINIGNEOF to get non-blocking behavior.
680 if (levents & (POLLIN | POLLRDNORM) &&
681 !(levents & POLLINIGNEOF)) {
682 levents &= ~(POLLIN | POLLRDNORM);
683 levents |= POLLINIGNEOF;
686 filetmp.f_data = fip->fi_readsock;
687 filetmp.f_cred = cred;
688 revents |= soo_poll(&filetmp, levents, cred, td);
690 /* Reverse the above conversion. */
691 if ((revents & POLLINIGNEOF) && !(events & POLLINIGNEOF)) {
692 revents |= (events & (POLLIN | POLLRDNORM));
693 revents &= ~POLLINIGNEOF;
696 levents = events & (POLLOUT | POLLWRNORM | POLLWRBAND);
697 if ((fp->f_flag & FWRITE) && levents) {
698 filetmp.f_data = fip->fi_writesock;
699 filetmp.f_cred = cred;
700 revents |= soo_poll(&filetmp, levents, cred, td);
706 fifo_read_f(struct file *fp, struct uio *uio, struct ucred *cred, int flags, struct thread *td)
708 struct fifoinfo *fip;
712 KASSERT(uio->uio_rw == UIO_READ,("fifo_read mode"));
713 if (uio->uio_resid == 0)
715 sflags = (fp->f_flag & FNONBLOCK) ? MSG_NBIO : 0;
716 sx_xlock(&fip->fi_sx);
717 error = soreceive(fip->fi_readsock, NULL, uio, NULL, NULL, &sflags);
718 sx_xunlock(&fip->fi_sx);
723 fifo_stat_f(struct file *fp, struct stat *sb, struct ucred *cred, struct thread *td)
726 return (vnops.fo_stat(fp, sb, cred, td));
730 fifo_write_f(struct file *fp, struct uio *uio, struct ucred *cred, int flags, struct thread *td)
732 struct fifoinfo *fip;
736 KASSERT(uio->uio_rw == UIO_WRITE,("fifo_write mode"));
737 sflags = (fp->f_flag & FNONBLOCK) ? MSG_NBIO : 0;
738 sx_xlock(&fip->fi_sx);
739 error = sosend(fip->fi_writesock, NULL, uio, 0, NULL, sflags, td);
740 sx_xunlock(&fip->fi_sx);