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,
75 .fo_flags = DFLAG_PASSABLE
79 * This structure is associated with the FIFO vnode and stores
80 * the state associated with the FIFO.
81 * Notes about locking:
82 * - fi_readsock and fi_writesock are invariant since init time.
83 * - fi_readers and fi_writers are vnode lock protected.
84 * - fi_wgen is fif_mtx lock protected.
87 struct socket *fi_readsock;
88 struct socket *fi_writesock;
94 static vop_print_t fifo_print;
95 static vop_open_t fifo_open;
96 static vop_close_t fifo_close;
97 static vop_pathconf_t fifo_pathconf;
98 static vop_advlock_t fifo_advlock;
100 static void filt_fifordetach(struct knote *kn);
101 static int filt_fiforead(struct knote *kn, long hint);
102 static void filt_fifowdetach(struct knote *kn);
103 static int filt_fifowrite(struct knote *kn, long hint);
104 static void filt_fifodetach_notsup(struct knote *kn);
105 static int filt_fifo_notsup(struct knote *kn, long hint);
107 static struct filterops fiforead_filtops =
108 { 1, NULL, filt_fifordetach, filt_fiforead };
109 static struct filterops fifowrite_filtops =
110 { 1, NULL, filt_fifowdetach, filt_fifowrite };
111 static struct filterops fifo_notsup_filtops =
112 { 1, NULL, filt_fifodetach_notsup, filt_fifo_notsup };
114 struct vop_vector fifo_specops = {
115 .vop_default = &default_vnodeops,
117 .vop_advlock = fifo_advlock,
118 .vop_close = fifo_close,
119 .vop_create = VOP_PANIC,
120 .vop_getattr = VOP_EBADF,
121 .vop_ioctl = VOP_PANIC,
122 .vop_kqfilter = VOP_PANIC,
123 .vop_link = VOP_PANIC,
124 .vop_mkdir = VOP_PANIC,
125 .vop_mknod = VOP_PANIC,
126 .vop_open = fifo_open,
127 .vop_pathconf = fifo_pathconf,
128 .vop_print = fifo_print,
129 .vop_read = VOP_PANIC,
130 .vop_readdir = VOP_PANIC,
131 .vop_readlink = VOP_PANIC,
132 .vop_reallocblks = VOP_PANIC,
133 .vop_reclaim = VOP_NULL,
134 .vop_remove = VOP_PANIC,
135 .vop_rename = VOP_PANIC,
136 .vop_rmdir = VOP_PANIC,
137 .vop_setattr = VOP_EBADF,
138 .vop_symlink = VOP_PANIC,
139 .vop_write = VOP_PANIC,
143 MTX_SYSINIT(fifo, &fifo_mtx, "fifo mutex", MTX_DEF);
146 * Dispose of fifo resources.
149 fifo_cleanup(struct vnode *vp)
151 struct fifoinfo *fip = vp->v_fifoinfo;
153 ASSERT_VOP_ELOCKED(vp, "fifo_cleanup");
154 if (fip->fi_readers == 0 && fip->fi_writers == 0) {
155 vp->v_fifoinfo = NULL;
156 (void)soclose(fip->fi_readsock);
157 (void)soclose(fip->fi_writesock);
163 * Open called to set up a new instance of a fifo or
164 * to find an active instance of a fifo.
169 struct vop_open_args /* {
172 struct ucred *a_cred;
177 struct vnode *vp = ap->a_vp;
178 struct fifoinfo *fip;
179 struct thread *td = ap->a_td;
180 struct ucred *cred = ap->a_cred;
181 struct file *fp = ap->a_fp;
182 struct socket *rso, *wso;
185 ASSERT_VOP_ELOCKED(vp, "fifo_open");
188 if ((fip = vp->v_fifoinfo) == NULL) {
189 fip = malloc(sizeof(*fip), M_VNODE, M_WAITOK);
190 error = socreate(AF_LOCAL, &rso, SOCK_STREAM, 0, cred, td);
193 fip->fi_readsock = rso;
194 error = socreate(AF_LOCAL, &wso, SOCK_STREAM, 0, cred, td);
197 fip->fi_writesock = wso;
198 error = soconnect2(wso, rso);
199 /* Close the direction we do not use, so we can get POLLHUP. */
201 error = soshutdown(rso, SHUT_WR);
210 fip->fi_readers = fip->fi_writers = 0;
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 * Use the fifo_mtx lock here, in addition to the vnode lock,
222 * in order to allow vnode lock dropping before msleep() calls
223 * and still avoiding missed wakeups.
226 if (ap->a_mode & FREAD) {
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);
237 fp->f_seqcount = fip->fi_wgen - fip->fi_writers;
239 if (ap->a_mode & FWRITE) {
240 if ((ap->a_mode & O_NONBLOCK) && fip->fi_readers == 0) {
241 mtx_unlock(&fifo_mtx);
242 if (fip->fi_writers == 0)
247 if (fip->fi_writers == 1) {
248 SOCKBUF_LOCK(&fip->fi_readsock->so_rcv);
249 fip->fi_readsock->so_rcv.sb_state &= ~SBS_CANTRCVMORE;
250 SOCKBUF_UNLOCK(&fip->fi_readsock->so_rcv);
251 if (fip->fi_readers > 0) {
252 wakeup(&fip->fi_readers);
253 sorwakeup(fip->fi_readsock);
257 if ((ap->a_mode & O_NONBLOCK) == 0) {
258 if ((ap->a_mode & FREAD) && fip->fi_writers == 0) {
260 error = msleep(&fip->fi_readers, &fifo_mtx,
261 PDROP | PCATCH | PSOCK, "fifoor", 0);
262 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
265 if (fip->fi_readers == 0) {
266 socantsendmore(fip->fi_writesock);
273 * We must have got woken up because we had a writer.
274 * That (and not still having one) is the condition
275 * that we must wait for.
278 if ((ap->a_mode & FWRITE) && fip->fi_readers == 0) {
280 error = msleep(&fip->fi_writers, &fifo_mtx,
281 PDROP | PCATCH | PSOCK, "fifoow", 0);
282 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
285 if (fip->fi_writers == 0) {
286 socantrcvmore(fip->fi_readsock);
289 mtx_unlock(&fifo_mtx);
295 * We must have got woken up because we had
296 * a reader. That (and not still having one)
297 * is the condition that we must wait for.
302 mtx_unlock(&fifo_mtx);
303 KASSERT(fp != NULL, ("can't fifo/vnode bypass"));
304 KASSERT(fp->f_ops == &badfileops, ("not badfileops in fifo_open"));
305 finit(fp, fp->f_flag, DTYPE_FIFO, fip, &fifo_ops_f);
310 filt_fifordetach(struct knote *kn)
312 struct socket *so = (struct socket *)kn->kn_hook;
314 SOCKBUF_LOCK(&so->so_rcv);
315 knlist_remove(&so->so_rcv.sb_sel.si_note, kn, 1);
316 if (knlist_empty(&so->so_rcv.sb_sel.si_note))
317 so->so_rcv.sb_flags &= ~SB_KNOTE;
318 SOCKBUF_UNLOCK(&so->so_rcv);
322 filt_fiforead(struct knote *kn, long hint)
324 struct socket *so = (struct socket *)kn->kn_hook;
326 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
327 kn->kn_data = so->so_rcv.sb_cc;
328 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
329 kn->kn_flags |= EV_EOF;
332 kn->kn_flags &= ~EV_EOF;
333 return (kn->kn_data > 0);
338 filt_fifowdetach(struct knote *kn)
340 struct socket *so = (struct socket *)kn->kn_hook;
342 SOCKBUF_LOCK(&so->so_snd);
343 knlist_remove(&so->so_snd.sb_sel.si_note, kn, 1);
344 if (knlist_empty(&so->so_snd.sb_sel.si_note))
345 so->so_snd.sb_flags &= ~SB_KNOTE;
346 SOCKBUF_UNLOCK(&so->so_snd);
350 filt_fifowrite(struct knote *kn, long hint)
352 struct socket *so = (struct socket *)kn->kn_hook;
354 SOCKBUF_LOCK_ASSERT(&so->so_snd);
355 kn->kn_data = sbspace(&so->so_snd);
356 if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
357 kn->kn_flags |= EV_EOF;
360 kn->kn_flags &= ~EV_EOF;
361 return (kn->kn_data >= so->so_snd.sb_lowat);
366 filt_fifodetach_notsup(struct knote *kn)
372 filt_fifo_notsup(struct knote *kn, long hint)
379 * Device close routine
384 struct vop_close_args /* {
387 struct ucred *a_cred;
391 struct vnode *vp = ap->a_vp;
392 struct fifoinfo *fip = vp->v_fifoinfo;
394 ASSERT_VOP_ELOCKED(vp, "fifo_close");
396 printf("fifo_close: no v_fifoinfo %p\n", vp);
399 if (ap->a_fflag & FREAD) {
401 if (fip->fi_readers == 0)
402 socantsendmore(fip->fi_writesock);
404 if (ap->a_fflag & FWRITE) {
406 if (fip->fi_writers == 0) {
407 socantrcvmore(fip->fi_readsock);
410 mtx_unlock(&fifo_mtx);
418 * Print out internal contents of a fifo vnode.
424 register struct fifoinfo *fip = vp->v_fifoinfo;
427 printf(", NULL v_fifoinfo");
430 printf(", fifo with %ld readers and %ld writers",
431 fip->fi_readers, fip->fi_writers);
436 * Print out the contents of a fifo vnode.
440 struct vop_print_args /* {
445 fifo_printinfo(ap->a_vp);
451 * Return POSIX pathconf information applicable to fifo's.
455 struct vop_pathconf_args /* {
462 switch (ap->a_name) {
464 *ap->a_retval = LINK_MAX;
467 *ap->a_retval = PIPE_BUF;
469 case _PC_CHOWN_RESTRICTED:
479 * Fifo advisory byte-level locks.
484 struct vop_advlock_args /* {
493 return (ap->a_flags & F_FLOCK ? EOPNOTSUPP : EINVAL);
497 fifo_close_f(struct file *fp, struct thread *td)
500 return (vnops.fo_close(fp, td));
504 * The implementation of ioctl() for named fifos is complicated by the fact
505 * that we permit O_RDWR fifo file descriptors, meaning that the actions of
506 * ioctls may have to be applied to both the underlying sockets rather than
507 * just one. The original implementation simply forward the ioctl to one
508 * or both sockets based on fp->f_flag. We now consider each ioctl
509 * separately, as the composition effect requires careful ordering.
511 * We do not blindly pass all ioctls through to the socket in order to avoid
512 * providing unnecessary ioctls that might be improperly depended on by
513 * applications (such as socket-specific, routing, and interface ioctls).
515 * Unlike sys_pipe.c, fifos do not implement the deprecated TIOCSPGRP and
516 * TIOCGPGRP ioctls. Earlier implementations of fifos did forward SIOCSPGRP
517 * and SIOCGPGRP ioctls, so we might need to re-add those here.
520 fifo_ioctl_f(struct file *fp, u_long com, void *data, struct ucred *cred,
524 struct file filetmp; /* Local, so need not be locked. */
533 * Non-blocking I/O is implemented at the fifo layer using
534 * MSG_NBIO, so does not need to be forwarded down the stack.
542 * These socket ioctls don't have any ordering requirements,
543 * so are called in an arbitrary order, and only on the
544 * sockets indicated by the file descriptor rights.
546 * XXXRW: If O_RDWR and the read socket accepts an ioctl but
547 * the write socket doesn't, the socketpair is left in an
548 * inconsistent state.
550 if (fp->f_flag & FREAD) {
551 filetmp.f_data = fi->fi_readsock;
552 filetmp.f_cred = cred;
553 error = soo_ioctl(&filetmp, com, data, cred, td);
557 if (fp->f_flag & FWRITE) {
558 filetmp.f_data = fi->fi_writesock;
559 filetmp.f_cred = cred;
560 error = soo_ioctl(&filetmp, com, data, cred, td);
566 * FIONREAD will return 0 for non-readable descriptors, and
567 * the results of FIONREAD on the read socket for readable
570 if (!(fp->f_flag & FREAD)) {
574 filetmp.f_data = fi->fi_readsock;
575 filetmp.f_cred = cred;
576 return (soo_ioctl(&filetmp, com, data, cred, td));
584 * Because fifos are now a file descriptor layer object, EVFILT_VNODE is not
585 * implemented. Likely, fifo_kqfilter() should be removed, and
586 * fifo_kqfilter_f() should know how to forward the request to the underling
587 * vnode using f_vnode in the file descriptor here.
590 fifo_kqfilter_f(struct file *fp, struct knote *kn)
599 * If a filter is requested that is not supported by this file
600 * descriptor, don't return an error, but also don't ever generate an
603 if ((kn->kn_filter == EVFILT_READ) && !(fp->f_flag & FREAD)) {
604 kn->kn_fop = &fifo_notsup_filtops;
608 if ((kn->kn_filter == EVFILT_WRITE) && !(fp->f_flag & FWRITE)) {
609 kn->kn_fop = &fifo_notsup_filtops;
613 switch (kn->kn_filter) {
615 kn->kn_fop = &fiforead_filtops;
616 so = fi->fi_readsock;
620 kn->kn_fop = &fifowrite_filtops;
621 so = fi->fi_writesock;
628 kn->kn_hook = (caddr_t)so;
631 knlist_add(&sb->sb_sel.si_note, kn, 1);
632 sb->sb_flags |= SB_KNOTE;
639 fifo_poll_f(struct file *fp, int events, struct ucred *cred, struct thread *td)
641 struct fifoinfo *fip;
643 int levents, revents = 0;
647 (POLLIN | POLLINIGNEOF | POLLPRI | POLLRDNORM | POLLRDBAND);
648 if ((fp->f_flag & FREAD) && levents) {
649 filetmp.f_data = fip->fi_readsock;
650 filetmp.f_cred = cred;
652 if (fp->f_seqcount == fip->fi_wgen)
653 levents |= POLLINIGNEOF;
654 mtx_unlock(&fifo_mtx);
655 revents |= soo_poll(&filetmp, levents, cred, td);
657 levents = events & (POLLOUT | POLLWRNORM | POLLWRBAND);
658 if ((fp->f_flag & FWRITE) && levents) {
659 filetmp.f_data = fip->fi_writesock;
660 filetmp.f_cred = cred;
661 revents |= soo_poll(&filetmp, levents, cred, td);
667 fifo_read_f(struct file *fp, struct uio *uio, struct ucred *cred, int flags, struct thread *td)
669 struct fifoinfo *fip;
673 KASSERT(uio->uio_rw == UIO_READ,("fifo_read mode"));
674 if (uio->uio_resid == 0)
676 sflags = (fp->f_flag & FNONBLOCK) ? MSG_NBIO : 0;
677 return (soreceive(fip->fi_readsock, NULL, uio, NULL, NULL, &sflags));
681 fifo_stat_f(struct file *fp, struct stat *sb, struct ucred *cred, struct thread *td)
684 return (vnops.fo_stat(fp, sb, cred, td));
688 fifo_truncate_f(struct file *fp, off_t length, struct ucred *cred, struct thread *td)
691 return (vnops.fo_truncate(fp, length, cred, td));
695 fifo_write_f(struct file *fp, struct uio *uio, struct ucred *cred, int flags, struct thread *td)
697 struct fifoinfo *fip;
701 KASSERT(uio->uio_rw == UIO_WRITE,("fifo_write mode"));
702 sflags = (fp->f_flag & FNONBLOCK) ? MSG_NBIO : 0;
703 return (sosend(fip->fi_writesock, NULL, uio, 0, NULL, sflags, td));