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>
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;
64 static fo_truncate_t fifo_truncate_f;
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,
77 .fo_flags = DFLAG_PASSABLE
81 * This structure is associated with the FIFO vnode and stores
82 * the state associated with the FIFO.
83 * Notes about locking:
84 * - fi_readsock and fi_writesock are invariant since init time.
85 * - fi_readers and fi_writers are vnode lock protected.
86 * - fi_wgen is fif_mtx lock protected.
89 struct socket *fi_readsock;
90 struct socket *fi_writesock;
96 static vop_print_t fifo_print;
97 static vop_open_t fifo_open;
98 static vop_close_t fifo_close;
99 static vop_pathconf_t fifo_pathconf;
100 static vop_advlock_t fifo_advlock;
102 static void filt_fifordetach(struct knote *kn);
103 static int filt_fiforead(struct knote *kn, long hint);
104 static void filt_fifowdetach(struct knote *kn);
105 static int filt_fifowrite(struct knote *kn, long hint);
106 static void filt_fifodetach_notsup(struct knote *kn);
107 static int filt_fifo_notsup(struct knote *kn, long hint);
109 static struct filterops fiforead_filtops = {
111 .f_detach = filt_fifordetach,
112 .f_event = filt_fiforead,
114 static struct filterops fifowrite_filtops = {
116 .f_detach = filt_fifowdetach,
117 .f_event = filt_fifowrite,
119 static struct filterops fifo_notsup_filtops = {
121 .f_detach = filt_fifodetach_notsup,
122 .f_event = filt_fifo_notsup,
125 struct vop_vector fifo_specops = {
126 .vop_default = &default_vnodeops,
128 .vop_advlock = fifo_advlock,
129 .vop_close = fifo_close,
130 .vop_create = VOP_PANIC,
131 .vop_getattr = VOP_EBADF,
132 .vop_ioctl = VOP_PANIC,
133 .vop_kqfilter = VOP_PANIC,
134 .vop_link = VOP_PANIC,
135 .vop_mkdir = VOP_PANIC,
136 .vop_mknod = VOP_PANIC,
137 .vop_open = fifo_open,
138 .vop_pathconf = fifo_pathconf,
139 .vop_print = fifo_print,
140 .vop_read = VOP_PANIC,
141 .vop_readdir = VOP_PANIC,
142 .vop_readlink = VOP_PANIC,
143 .vop_reallocblks = VOP_PANIC,
144 .vop_reclaim = VOP_NULL,
145 .vop_remove = VOP_PANIC,
146 .vop_rename = VOP_PANIC,
147 .vop_rmdir = VOP_PANIC,
148 .vop_setattr = VOP_EBADF,
149 .vop_symlink = VOP_PANIC,
150 .vop_write = VOP_PANIC,
154 MTX_SYSINIT(fifo, &fifo_mtx, "fifo mutex", MTX_DEF);
157 * Dispose of fifo resources.
160 fifo_cleanup(struct vnode *vp)
162 struct fifoinfo *fip = vp->v_fifoinfo;
164 ASSERT_VOP_ELOCKED(vp, "fifo_cleanup");
165 if (fip->fi_readers == 0 && fip->fi_writers == 0) {
166 vp->v_fifoinfo = NULL;
167 (void)soclose(fip->fi_readsock);
168 (void)soclose(fip->fi_writesock);
174 * Open called to set up a new instance of a fifo or
175 * to find an active instance of a fifo.
180 struct vop_open_args /* {
183 struct ucred *a_cred;
188 struct vnode *vp = ap->a_vp;
189 struct fifoinfo *fip;
190 struct thread *td = ap->a_td;
191 struct ucred *cred = ap->a_cred;
192 struct file *fp = ap->a_fp;
193 struct socket *rso, *wso;
196 ASSERT_VOP_ELOCKED(vp, "fifo_open");
199 if ((fip = vp->v_fifoinfo) == NULL) {
200 fip = malloc(sizeof(*fip), M_VNODE, M_WAITOK);
201 error = socreate(AF_LOCAL, &rso, SOCK_STREAM, 0, cred, td);
204 fip->fi_readsock = rso;
205 error = socreate(AF_LOCAL, &wso, SOCK_STREAM, 0, cred, td);
208 fip->fi_writesock = wso;
209 error = soconnect2(wso, rso);
210 /* Close the direction we do not use, so we can get POLLHUP. */
212 error = soshutdown(rso, SHUT_WR);
221 fip->fi_wgen = fip->fi_readers = fip->fi_writers = 0;
222 wso->so_snd.sb_lowat = PIPE_BUF;
223 SOCKBUF_LOCK(&rso->so_rcv);
224 rso->so_rcv.sb_state |= SBS_CANTRCVMORE;
225 SOCKBUF_UNLOCK(&rso->so_rcv);
226 KASSERT(vp->v_fifoinfo == NULL,
227 ("fifo_open: v_fifoinfo race"));
228 vp->v_fifoinfo = fip;
232 * Use the fifo_mtx lock here, in addition to the vnode lock,
233 * in order to allow vnode lock dropping before msleep() calls
234 * and still avoiding missed wakeups.
237 if (ap->a_mode & FREAD) {
239 if (fip->fi_readers == 1) {
240 SOCKBUF_LOCK(&fip->fi_writesock->so_snd);
241 fip->fi_writesock->so_snd.sb_state &= ~SBS_CANTSENDMORE;
242 SOCKBUF_UNLOCK(&fip->fi_writesock->so_snd);
243 if (fip->fi_writers > 0) {
244 wakeup(&fip->fi_writers);
245 sowwakeup(fip->fi_writesock);
248 fp->f_seqcount = fip->fi_wgen - fip->fi_writers;
250 if (ap->a_mode & FWRITE) {
251 if ((ap->a_mode & O_NONBLOCK) && fip->fi_readers == 0) {
252 mtx_unlock(&fifo_mtx);
253 if (fip->fi_writers == 0)
258 if (fip->fi_writers == 1) {
259 SOCKBUF_LOCK(&fip->fi_readsock->so_rcv);
260 fip->fi_readsock->so_rcv.sb_state &= ~SBS_CANTRCVMORE;
261 SOCKBUF_UNLOCK(&fip->fi_readsock->so_rcv);
262 if (fip->fi_readers > 0) {
263 wakeup(&fip->fi_readers);
264 sorwakeup(fip->fi_readsock);
268 if ((ap->a_mode & O_NONBLOCK) == 0) {
269 if ((ap->a_mode & FREAD) && fip->fi_writers == 0) {
271 error = msleep(&fip->fi_readers, &fifo_mtx,
272 PDROP | PCATCH | PSOCK, "fifoor", 0);
273 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
276 if (fip->fi_readers == 0) {
277 socantsendmore(fip->fi_writesock);
284 * We must have got woken up because we had a writer.
285 * That (and not still having one) is the condition
286 * that we must wait for.
289 if ((ap->a_mode & FWRITE) && fip->fi_readers == 0) {
291 error = msleep(&fip->fi_writers, &fifo_mtx,
292 PDROP | PCATCH | PSOCK, "fifoow", 0);
293 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
296 if (fip->fi_writers == 0) {
297 socantrcvmore(fip->fi_readsock);
300 mtx_unlock(&fifo_mtx);
306 * We must have got woken up because we had
307 * a reader. That (and not still having one)
308 * is the condition that we must wait for.
313 mtx_unlock(&fifo_mtx);
314 KASSERT(fp != NULL, ("can't fifo/vnode bypass"));
315 KASSERT(fp->f_ops == &badfileops, ("not badfileops in fifo_open"));
316 finit(fp, fp->f_flag, DTYPE_FIFO, fip, &fifo_ops_f);
321 filt_fifordetach(struct knote *kn)
323 struct socket *so = (struct socket *)kn->kn_hook;
325 SOCKBUF_LOCK(&so->so_rcv);
326 knlist_remove(&so->so_rcv.sb_sel.si_note, kn, 1);
327 if (knlist_empty(&so->so_rcv.sb_sel.si_note))
328 so->so_rcv.sb_flags &= ~SB_KNOTE;
329 SOCKBUF_UNLOCK(&so->so_rcv);
333 filt_fiforead(struct knote *kn, long hint)
335 struct socket *so = (struct socket *)kn->kn_hook;
337 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
338 kn->kn_data = so->so_rcv.sb_cc;
339 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
340 kn->kn_flags |= EV_EOF;
343 kn->kn_flags &= ~EV_EOF;
344 return (kn->kn_data > 0);
349 filt_fifowdetach(struct knote *kn)
351 struct socket *so = (struct socket *)kn->kn_hook;
353 SOCKBUF_LOCK(&so->so_snd);
354 knlist_remove(&so->so_snd.sb_sel.si_note, kn, 1);
355 if (knlist_empty(&so->so_snd.sb_sel.si_note))
356 so->so_snd.sb_flags &= ~SB_KNOTE;
357 SOCKBUF_UNLOCK(&so->so_snd);
361 filt_fifowrite(struct knote *kn, long hint)
363 struct socket *so = (struct socket *)kn->kn_hook;
365 SOCKBUF_LOCK_ASSERT(&so->so_snd);
366 kn->kn_data = sbspace(&so->so_snd);
367 if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
368 kn->kn_flags |= EV_EOF;
371 kn->kn_flags &= ~EV_EOF;
372 return (kn->kn_data >= so->so_snd.sb_lowat);
377 filt_fifodetach_notsup(struct knote *kn)
383 filt_fifo_notsup(struct knote *kn, long hint)
390 * Device close routine
395 struct vop_close_args /* {
398 struct ucred *a_cred;
402 struct vnode *vp = ap->a_vp;
403 struct fifoinfo *fip = vp->v_fifoinfo;
405 ASSERT_VOP_ELOCKED(vp, "fifo_close");
407 printf("fifo_close: no v_fifoinfo %p\n", vp);
410 if (ap->a_fflag & FREAD) {
412 if (fip->fi_readers == 0)
413 socantsendmore(fip->fi_writesock);
415 if (ap->a_fflag & FWRITE) {
417 if (fip->fi_writers == 0) {
418 socantrcvmore(fip->fi_readsock);
421 mtx_unlock(&fifo_mtx);
429 * Print out internal contents of a fifo vnode.
435 register struct fifoinfo *fip = vp->v_fifoinfo;
438 printf(", NULL v_fifoinfo");
441 printf(", fifo with %ld readers and %ld writers",
442 fip->fi_readers, fip->fi_writers);
447 * Print out the contents of a fifo vnode.
451 struct vop_print_args /* {
456 fifo_printinfo(ap->a_vp);
462 * Return POSIX pathconf information applicable to fifo's.
466 struct vop_pathconf_args /* {
473 switch (ap->a_name) {
475 *ap->a_retval = LINK_MAX;
478 *ap->a_retval = PIPE_BUF;
480 case _PC_CHOWN_RESTRICTED:
490 * Fifo advisory byte-level locks.
495 struct vop_advlock_args /* {
504 return (ap->a_flags & F_FLOCK ? EOPNOTSUPP : EINVAL);
508 fifo_close_f(struct file *fp, struct thread *td)
511 return (vnops.fo_close(fp, td));
515 * The implementation of ioctl() for named fifos is complicated by the fact
516 * that we permit O_RDWR fifo file descriptors, meaning that the actions of
517 * ioctls may have to be applied to both the underlying sockets rather than
518 * just one. The original implementation simply forward the ioctl to one
519 * or both sockets based on fp->f_flag. We now consider each ioctl
520 * separately, as the composition effect requires careful ordering.
522 * We do not blindly pass all ioctls through to the socket in order to avoid
523 * providing unnecessary ioctls that might be improperly depended on by
524 * applications (such as socket-specific, routing, and interface ioctls).
526 * Unlike sys_pipe.c, fifos do not implement the deprecated TIOCSPGRP and
527 * TIOCGPGRP ioctls. Earlier implementations of fifos did forward SIOCSPGRP
528 * and SIOCGPGRP ioctls, so we might need to re-add those here.
531 fifo_ioctl_f(struct file *fp, u_long com, void *data, struct ucred *cred,
535 struct file filetmp; /* Local, so need not be locked. */
544 * Non-blocking I/O is implemented at the fifo layer using
545 * MSG_NBIO, so does not need to be forwarded down the stack.
553 * These socket ioctls don't have any ordering requirements,
554 * so are called in an arbitrary order, and only on the
555 * sockets indicated by the file descriptor rights.
557 * XXXRW: If O_RDWR and the read socket accepts an ioctl but
558 * the write socket doesn't, the socketpair is left in an
559 * inconsistent state.
561 if (fp->f_flag & FREAD) {
562 filetmp.f_data = fi->fi_readsock;
563 filetmp.f_cred = cred;
564 error = soo_ioctl(&filetmp, com, data, cred, td);
568 if (fp->f_flag & FWRITE) {
569 filetmp.f_data = fi->fi_writesock;
570 filetmp.f_cred = cred;
571 error = soo_ioctl(&filetmp, com, data, cred, td);
577 * FIONREAD will return 0 for non-readable descriptors, and
578 * the results of FIONREAD on the read socket for readable
581 if (!(fp->f_flag & FREAD)) {
585 filetmp.f_data = fi->fi_readsock;
586 filetmp.f_cred = cred;
587 return (soo_ioctl(&filetmp, com, data, cred, td));
595 * Because fifos are now a file descriptor layer object, EVFILT_VNODE is not
596 * implemented. Likely, fifo_kqfilter() should be removed, and
597 * fifo_kqfilter_f() should know how to forward the request to the underling
598 * vnode using f_vnode in the file descriptor here.
601 fifo_kqfilter_f(struct file *fp, struct knote *kn)
610 * If a filter is requested that is not supported by this file
611 * descriptor, don't return an error, but also don't ever generate an
614 if ((kn->kn_filter == EVFILT_READ) && !(fp->f_flag & FREAD)) {
615 kn->kn_fop = &fifo_notsup_filtops;
619 if ((kn->kn_filter == EVFILT_WRITE) && !(fp->f_flag & FWRITE)) {
620 kn->kn_fop = &fifo_notsup_filtops;
624 switch (kn->kn_filter) {
626 kn->kn_fop = &fiforead_filtops;
627 so = fi->fi_readsock;
631 kn->kn_fop = &fifowrite_filtops;
632 so = fi->fi_writesock;
639 kn->kn_hook = (caddr_t)so;
642 knlist_add(&sb->sb_sel.si_note, kn, 1);
643 sb->sb_flags |= SB_KNOTE;
650 fifo_poll_f(struct file *fp, int events, struct ucred *cred, struct thread *td)
652 struct fifoinfo *fip;
654 int levents, revents = 0;
658 (POLLIN | POLLINIGNEOF | POLLPRI | POLLRDNORM | POLLRDBAND);
659 if ((fp->f_flag & FREAD) && levents) {
660 filetmp.f_data = fip->fi_readsock;
661 filetmp.f_cred = cred;
663 if (fp->f_seqcount == fip->fi_wgen)
664 levents |= POLLINIGNEOF;
665 mtx_unlock(&fifo_mtx);
666 revents |= soo_poll(&filetmp, levents, cred, td);
668 levents = events & (POLLOUT | POLLWRNORM | POLLWRBAND);
669 if ((fp->f_flag & FWRITE) && levents) {
670 filetmp.f_data = fip->fi_writesock;
671 filetmp.f_cred = cred;
672 revents |= soo_poll(&filetmp, levents, cred, td);
678 fifo_read_f(struct file *fp, struct uio *uio, struct ucred *cred, int flags, struct thread *td)
680 struct fifoinfo *fip;
684 KASSERT(uio->uio_rw == UIO_READ,("fifo_read mode"));
685 if (uio->uio_resid == 0)
687 sflags = (fp->f_flag & FNONBLOCK) ? MSG_NBIO : 0;
688 return (soreceive(fip->fi_readsock, NULL, uio, NULL, NULL, &sflags));
692 fifo_stat_f(struct file *fp, struct stat *sb, struct ucred *cred, struct thread *td)
695 return (vnops.fo_stat(fp, sb, cred, td));
699 fifo_truncate_f(struct file *fp, off_t length, struct ucred *cred, struct thread *td)
702 return (vnops.fo_truncate(fp, length, cred, td));
706 fifo_write_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_WRITE,("fifo_write mode"));
713 sflags = (fp->f_flag & FNONBLOCK) ? MSG_NBIO : 0;
714 return (sosend(fip->fi_writesock, NULL, uio, 0, NULL, sflags, td));