2 * Copyright (c) 1990, 1993, 1995
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#)fifo_vnops.c 8.10 (Berkeley) 5/27/95
34 * $FreeBSD: src/sys/miscfs/fifofs/fifo_vnops.c,v 1.45.2.4 2003/04/22 10:11:24 bde Exp $
35 * $DragonFly: src/sys/vfs/fifofs/fifo_vnops.c,v 1.18 2005/02/15 08:32:18 joerg Exp $
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/unistd.h>
41 #include <sys/kernel.h>
43 #include <sys/malloc.h>
44 #include <sys/vnode.h>
45 #include <sys/socket.h>
46 #include <sys/socketvar.h>
47 #include <sys/filio.h>
48 #include <sys/fcntl.h>
50 #include <sys/event.h>
56 * This structure is associated with the FIFO vnode and stores
57 * the state associated with the FIFO.
60 struct socket *fi_readsock;
61 struct socket *fi_writesock;
66 static int fifo_badop (void);
67 static int fifo_print (struct vop_print_args *);
68 static int fifo_lookup (struct vop_lookup_args *);
69 static int fifo_open (struct vop_open_args *);
70 static int fifo_close (struct vop_close_args *);
71 static int fifo_read (struct vop_read_args *);
72 static int fifo_write (struct vop_write_args *);
73 static int fifo_ioctl (struct vop_ioctl_args *);
74 static int fifo_poll (struct vop_poll_args *);
75 static int fifo_kqfilter (struct vop_kqfilter_args *);
76 static int fifo_inactive (struct vop_inactive_args *);
77 static int fifo_bmap (struct vop_bmap_args *);
78 static int fifo_pathconf (struct vop_pathconf_args *);
79 static int fifo_advlock (struct vop_advlock_args *);
81 static void filt_fifordetach(struct knote *kn);
82 static int filt_fiforead(struct knote *kn, long hint);
83 static void filt_fifowdetach(struct knote *kn);
84 static int filt_fifowrite(struct knote *kn, long hint);
86 static struct filterops fiforead_filtops =
87 { 1, NULL, filt_fifordetach, filt_fiforead };
88 static struct filterops fifowrite_filtops =
89 { 1, NULL, filt_fifowdetach, filt_fifowrite };
91 struct vop_ops *fifo_vnode_vops;
92 static struct vnodeopv_entry_desc fifo_vnodeop_entries[] = {
93 { &vop_default_desc, (vnodeopv_entry_t) vop_defaultop },
94 { &vop_access_desc, (vnodeopv_entry_t) vop_ebadf },
95 { &vop_advlock_desc, (vnodeopv_entry_t) fifo_advlock },
96 { &vop_bmap_desc, (vnodeopv_entry_t) fifo_bmap },
97 { &vop_close_desc, (vnodeopv_entry_t) fifo_close },
98 { &vop_create_desc, (vnodeopv_entry_t) fifo_badop },
99 { &vop_getattr_desc, (vnodeopv_entry_t) vop_ebadf },
100 { &vop_inactive_desc, (vnodeopv_entry_t) fifo_inactive },
101 { &vop_ioctl_desc, (vnodeopv_entry_t) fifo_ioctl },
102 { &vop_kqfilter_desc, (vnodeopv_entry_t) fifo_kqfilter },
103 { &vop_lease_desc, (vnodeopv_entry_t) vop_null },
104 { &vop_link_desc, (vnodeopv_entry_t) fifo_badop },
105 { &vop_lookup_desc, (vnodeopv_entry_t) fifo_lookup },
106 { &vop_mkdir_desc, (vnodeopv_entry_t) fifo_badop },
107 { &vop_mknod_desc, (vnodeopv_entry_t) fifo_badop },
108 { &vop_open_desc, (vnodeopv_entry_t) fifo_open },
109 { &vop_pathconf_desc, (vnodeopv_entry_t) fifo_pathconf },
110 { &vop_poll_desc, (vnodeopv_entry_t) fifo_poll },
111 { &vop_print_desc, (vnodeopv_entry_t) fifo_print },
112 { &vop_read_desc, (vnodeopv_entry_t) fifo_read },
113 { &vop_readdir_desc, (vnodeopv_entry_t) fifo_badop },
114 { &vop_readlink_desc, (vnodeopv_entry_t) fifo_badop },
115 { &vop_reallocblks_desc, (vnodeopv_entry_t) fifo_badop },
116 { &vop_reclaim_desc, (vnodeopv_entry_t) vop_null },
117 { &vop_remove_desc, (vnodeopv_entry_t) fifo_badop },
118 { &vop_rename_desc, (vnodeopv_entry_t) fifo_badop },
119 { &vop_rmdir_desc, (vnodeopv_entry_t) fifo_badop },
120 { &vop_setattr_desc, (vnodeopv_entry_t) vop_ebadf },
121 { &vop_symlink_desc, (vnodeopv_entry_t) fifo_badop },
122 { &vop_write_desc, (vnodeopv_entry_t) fifo_write },
125 static struct vnodeopv_desc fifo_vnodeop_opv_desc =
126 { &fifo_vnode_vops, fifo_vnodeop_entries };
128 VNODEOP_SET(fifo_vnodeop_opv_desc);
130 static MALLOC_DEFINE(M_FIFOINFO, "Fifo info", "Fifo info entries");
133 * fifo_vnoperate(struct vnodeop_desc *a_desc, ...)
136 fifo_vnoperate(struct vop_generic_args *ap)
138 return (VOCALL(fifo_vnode_vops, ap));
142 * Trivial lookup routine that always fails.
144 * fifo_lookup(struct vnode *a_dvp, struct vnode **a_vpp,
145 * struct componentname *a_cnp)
149 fifo_lookup(struct vop_lookup_args *ap)
156 * Open called to set up a new instance of a fifo or
157 * to find an active instance of a fifo.
159 * fifo_open(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
160 * struct thread *a_td)
164 fifo_open(struct vop_open_args *ap)
166 struct vnode *vp = ap->a_vp;
167 struct fifoinfo *fip;
168 struct socket *rso, *wso;
171 if ((fip = vp->v_fifoinfo) == NULL) {
172 MALLOC(fip, struct fifoinfo *, sizeof(*fip), M_FIFOINFO, M_WAITOK);
173 vp->v_fifoinfo = fip;
174 error = socreate(AF_LOCAL, &rso, SOCK_STREAM, 0, ap->a_td);
176 free(fip, M_FIFOINFO);
177 vp->v_fifoinfo = NULL;
180 fip->fi_readsock = rso;
181 error = socreate(AF_LOCAL, &wso, SOCK_STREAM, 0, ap->a_td);
184 free(fip, M_FIFOINFO);
185 vp->v_fifoinfo = NULL;
188 fip->fi_writesock = wso;
189 error = unp_connect2(wso, rso);
193 free(fip, M_FIFOINFO);
194 vp->v_fifoinfo = NULL;
197 fip->fi_readers = fip->fi_writers = 0;
198 wso->so_snd.sb_lowat = PIPE_BUF;
199 rso->so_state |= SS_CANTRCVMORE;
201 if (ap->a_mode & FREAD) {
203 if (fip->fi_readers == 1) {
204 fip->fi_writesock->so_state &= ~SS_CANTSENDMORE;
205 if (fip->fi_writers > 0) {
206 wakeup((caddr_t)&fip->fi_writers);
207 sowwakeup(fip->fi_writesock);
211 if (ap->a_mode & FWRITE) {
213 if (fip->fi_writers == 1) {
214 fip->fi_readsock->so_state &= ~SS_CANTRCVMORE;
215 if (fip->fi_readers > 0) {
216 wakeup((caddr_t)&fip->fi_readers);
217 sorwakeup(fip->fi_writesock);
221 if ((ap->a_mode & FREAD) && (ap->a_mode & O_NONBLOCK) == 0) {
222 if (fip->fi_writers == 0) {
223 VOP_UNLOCK(vp, 0, ap->a_td);
224 error = tsleep((caddr_t)&fip->fi_readers,
225 PCATCH, "fifoor", 0);
226 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, ap->a_td);
230 * We must have got woken up because we had a writer.
231 * That (and not still having one) is the condition
232 * that we must wait for.
236 if (ap->a_mode & FWRITE) {
237 if (ap->a_mode & O_NONBLOCK) {
238 if (fip->fi_readers == 0) {
243 if (fip->fi_readers == 0) {
244 VOP_UNLOCK(vp, 0, ap->a_td);
245 error = tsleep((caddr_t)&fip->fi_writers,
246 PCATCH, "fifoow", 0);
247 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, ap->a_td);
251 * We must have got woken up because we had
252 * a reader. That (and not still having one)
253 * is the condition that we must wait for.
260 VOP_CLOSE(vp, ap->a_mode, ap->a_td);
267 * fifo_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
268 * struct ucred *a_cred)
272 fifo_read(struct vop_read_args *ap)
274 struct uio *uio = ap->a_uio;
275 struct socket *rso = ap->a_vp->v_fifoinfo->fi_readsock;
276 struct thread *td = uio->uio_td;
277 int error, startresid;
280 if (uio->uio_rw != UIO_READ)
281 panic("fifo_read mode");
283 if (uio->uio_resid == 0)
285 if (ap->a_ioflag & IO_NDELAY)
286 rso->so_state |= SS_NBIO;
287 startresid = uio->uio_resid;
288 VOP_UNLOCK(ap->a_vp, 0, td);
289 error = soreceive(rso, (struct sockaddr **)0, uio, (struct mbuf **)0,
290 (struct mbuf **)0, (int *)0);
291 vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY, td);
292 if (ap->a_ioflag & IO_NDELAY)
293 rso->so_state &= ~SS_NBIO;
300 * fifo_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
301 * struct ucred *a_cred)
305 fifo_write(struct vop_write_args *ap)
307 struct socket *wso = ap->a_vp->v_fifoinfo->fi_writesock;
308 struct thread *td = ap->a_uio->uio_td;
312 if (ap->a_uio->uio_rw != UIO_WRITE)
313 panic("fifo_write mode");
315 if (ap->a_ioflag & IO_NDELAY)
316 wso->so_state |= SS_NBIO;
317 VOP_UNLOCK(ap->a_vp, 0, td);
318 error = sosend(wso, (struct sockaddr *)0, ap->a_uio, 0,
319 (struct mbuf *)0, 0, td);
320 vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY, td);
321 if (ap->a_ioflag & IO_NDELAY)
322 wso->so_state &= ~SS_NBIO;
327 * Device ioctl operation.
329 * fifo_ioctl(struct vnode *a_vp, int a_command, caddr_t a_data, int a_fflag,
330 * struct ucred *a_cred, struct thread *a_td)
334 fifo_ioctl(struct vop_ioctl_args *ap)
336 struct file filetmp; /* Local */
339 if (ap->a_command == FIONBIO)
341 if (ap->a_fflag & FREAD) {
342 filetmp.f_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_readsock;
343 error = soo_ioctl(&filetmp, ap->a_command, ap->a_data, ap->a_td);
347 if (ap->a_fflag & FWRITE) {
348 filetmp.f_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_writesock;
349 error = soo_ioctl(&filetmp, ap->a_command, ap->a_data, ap->a_td);
357 * fifo_kqfilter(struct vnode *a_vp, struct knote *a_kn)
361 fifo_kqfilter(struct vop_kqfilter_args *ap)
363 struct fifoinfo *fi = ap->a_vp->v_fifoinfo;
367 switch (ap->a_kn->kn_filter) {
369 ap->a_kn->kn_fop = &fiforead_filtops;
370 so = fi->fi_readsock;
374 ap->a_kn->kn_fop = &fifowrite_filtops;
375 so = fi->fi_writesock;
382 ap->a_kn->kn_hook = (caddr_t)so;
384 SLIST_INSERT_HEAD(&sb->sb_sel.si_note, ap->a_kn, kn_selnext);
385 sb->sb_flags |= SB_KNOTE;
391 filt_fifordetach(struct knote *kn)
393 struct socket *so = (struct socket *)kn->kn_hook;
395 SLIST_REMOVE(&so->so_rcv.sb_sel.si_note, kn, knote, kn_selnext);
396 if (SLIST_EMPTY(&so->so_rcv.sb_sel.si_note))
397 so->so_rcv.sb_flags &= ~SB_KNOTE;
401 filt_fiforead(struct knote *kn, long hint)
403 struct socket *so = (struct socket *)kn->kn_hook;
405 kn->kn_data = so->so_rcv.sb_cc;
406 if (so->so_state & SS_CANTRCVMORE) {
407 kn->kn_flags |= EV_EOF;
410 kn->kn_flags &= ~EV_EOF;
411 return (kn->kn_data > 0);
415 filt_fifowdetach(struct knote *kn)
417 struct socket *so = (struct socket *)kn->kn_hook;
419 SLIST_REMOVE(&so->so_snd.sb_sel.si_note, kn, knote, kn_selnext);
420 if (SLIST_EMPTY(&so->so_snd.sb_sel.si_note))
421 so->so_snd.sb_flags &= ~SB_KNOTE;
425 filt_fifowrite(struct knote *kn, long hint)
427 struct socket *so = (struct socket *)kn->kn_hook;
429 kn->kn_data = sbspace(&so->so_snd);
430 if (so->so_state & SS_CANTSENDMORE) {
431 kn->kn_flags |= EV_EOF;
434 kn->kn_flags &= ~EV_EOF;
435 return (kn->kn_data >= so->so_snd.sb_lowat);
439 * fifo_poll(struct vnode *a_vp, int a_events, struct ucred *a_cred,
440 * struct thread *a_td)
444 fifo_poll(struct vop_poll_args *ap)
447 int events, revents = 0;
449 events = ap->a_events &
450 (POLLIN | POLLINIGNEOF | POLLPRI | POLLRDNORM | POLLRDBAND);
453 * If POLLIN or POLLRDNORM is requested and POLLINIGNEOF is
454 * not, then convert the first two to the last one. This
455 * tells the socket poll function to ignore EOF so that we
456 * block if there is no writer (and no data). Callers can
457 * set POLLINIGNEOF to get non-blocking behavior.
459 if (events & (POLLIN | POLLRDNORM) &&
460 !(events & POLLINIGNEOF)) {
461 events &= ~(POLLIN | POLLRDNORM);
462 events |= POLLINIGNEOF;
465 filetmp.f_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_readsock;
467 revents |= soo_poll(&filetmp, events, ap->a_cred,
470 /* Reverse the above conversion. */
471 if ((revents & POLLINIGNEOF) &&
472 !(ap->a_events & POLLINIGNEOF)) {
473 revents |= (ap->a_events & (POLLIN | POLLRDNORM));
474 revents &= ~POLLINIGNEOF;
477 events = ap->a_events & (POLLOUT | POLLWRNORM | POLLWRBAND);
479 filetmp.f_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_writesock;
481 revents |= soo_poll(&filetmp, events, ap->a_cred,
488 * fifo_inactive(struct vnode *a_vp, struct thread *a_td)
491 fifo_inactive(struct vop_inactive_args *ap)
497 * This is a noop, simply returning what one has been given.
499 * fifo_bmap(struct vnode *a_vp, daddr_t a_bn, struct vnode **a_vpp,
500 * daddr_t *a_bnp, int *a_runp, int *a_runb)
503 fifo_bmap(struct vop_bmap_args *ap)
505 if (ap->a_vpp != NULL)
506 *ap->a_vpp = ap->a_vp;
507 if (ap->a_bnp != NULL)
508 *ap->a_bnp = ap->a_bn;
509 if (ap->a_runp != NULL)
511 if (ap->a_runb != NULL)
517 * Device close routine
519 * fifo_close(struct vnode *a_vp, int a_fflag, struct ucred *a_cred,
520 * struct thread *a_td)
524 fifo_close(struct vop_close_args *ap)
526 struct vnode *vp = ap->a_vp;
527 struct fifoinfo *fip = vp->v_fifoinfo;
530 if (ap->a_fflag & FREAD) {
532 if (fip->fi_readers == 0)
533 socantsendmore(fip->fi_writesock);
535 if (ap->a_fflag & FWRITE) {
537 if (fip->fi_writers == 0)
538 socantrcvmore(fip->fi_readsock);
540 if (vp->v_usecount > 1)
542 error1 = soclose(fip->fi_readsock);
543 error2 = soclose(fip->fi_writesock);
544 FREE(fip, M_FIFOINFO);
545 vp->v_fifoinfo = NULL;
553 * Print out internal contents of a fifo vnode.
556 fifo_printinfo(struct vnode *vp)
558 struct fifoinfo *fip = vp->v_fifoinfo;
560 printf(", fifo with %ld readers and %ld writers",
561 fip->fi_readers, fip->fi_writers);
566 * Print out the contents of a fifo vnode.
568 * fifo_print(struct vnode *a_vp)
571 fifo_print(struct vop_print_args *ap)
573 printf("tag VT_NON");
574 fifo_printinfo(ap->a_vp);
580 * Return POSIX pathconf information applicable to fifo's.
582 * fifo_pathconf(struct vnode *a_vp, int a_name, int *a_retval)
585 fifo_pathconf(struct vop_pathconf_args *ap)
587 switch (ap->a_name) {
589 *ap->a_retval = LINK_MAX;
592 *ap->a_retval = PIPE_BUF;
594 case _PC_CHOWN_RESTRICTED:
604 * Fifo advisory byte-level locks.
606 * fifo_advlock(struct vnode *a_vp, caddr_t a_id, int a_op, struct flock *a_fl,
611 fifo_advlock(struct vop_advlock_args *ap)
613 return (ap->a_flags & F_FLOCK ? EOPNOTSUPP : EINVAL);
622 panic("fifo_badop called");