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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * Filesystem FIFO type ops. All entry points are MPSAFE.
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/unistd.h>
35 #include <sys/kernel.h>
37 #include <sys/malloc.h>
38 #include <sys/vnode.h>
39 #include <sys/socket.h>
40 #include <sys/socketvar.h>
41 #include <sys/filio.h>
42 #include <sys/fcntl.h>
44 #include <sys/event.h>
47 #include <sys/socketvar2.h>
52 * This structure is associated with the FIFO vnode and stores
53 * the state associated with the FIFO.
56 struct socket *fi_readsock;
57 struct socket *fi_writesock;
62 #define FIFO_LOCK_POOL 128
63 #define FIFO_LOCK_MASK (FIFO_LOCK_POOL - 1)
65 static int fifo_badop (void);
66 static int fifo_print (struct vop_print_args *);
67 static int fifo_lookup (struct vop_old_lookup_args *);
68 static int fifo_open (struct vop_open_args *);
69 static int fifo_close (struct vop_close_args *);
70 static int fifo_read (struct vop_read_args *);
71 static int fifo_write (struct vop_write_args *);
72 static int fifo_ioctl (struct vop_ioctl_args *);
73 static int fifo_kqfilter (struct vop_kqfilter_args *);
74 static int fifo_inactive (struct vop_inactive_args *);
75 static int fifo_bmap (struct vop_bmap_args *);
76 static int fifo_pathconf (struct vop_pathconf_args *);
77 static int fifo_advlock (struct vop_advlock_args *);
79 static void filt_fifordetach(struct knote *kn);
80 static int filt_fiforead(struct knote *kn, long hint);
81 static void filt_fifowdetach(struct knote *kn);
82 static int filt_fifowrite(struct knote *kn, long hint);
84 static struct filterops fiforead_filtops =
85 { FILTEROP_ISFD, NULL, filt_fifordetach, filt_fiforead };
86 static struct filterops fifowrite_filtops =
87 { FILTEROP_ISFD, NULL, filt_fifowdetach, filt_fifowrite };
89 struct vop_ops fifo_vnode_vops = {
90 .vop_default = vop_defaultop,
91 .vop_access = (void *)vop_ebadf,
92 .vop_advlock = fifo_advlock,
93 .vop_bmap = fifo_bmap,
94 .vop_close = fifo_close,
95 .vop_old_create = (void *)fifo_badop,
96 .vop_getattr = (void *)vop_ebadf,
97 .vop_inactive = fifo_inactive,
98 .vop_ioctl = fifo_ioctl,
99 .vop_kqfilter = fifo_kqfilter,
100 .vop_old_link = (void *)fifo_badop,
101 .vop_old_lookup = fifo_lookup,
102 .vop_old_mkdir = (void *)fifo_badop,
103 .vop_old_mknod = (void *)fifo_badop,
104 .vop_open = fifo_open,
105 .vop_pathconf = fifo_pathconf,
106 .vop_print = fifo_print,
107 .vop_read = fifo_read,
108 .vop_readdir = (void *)fifo_badop,
109 .vop_readlink = (void *)fifo_badop,
110 .vop_reallocblks = (void *)fifo_badop,
111 .vop_reclaim = (void *)vop_null,
112 .vop_old_remove = (void *)fifo_badop,
113 .vop_old_rename = (void *)fifo_badop,
114 .vop_old_rmdir = (void *)fifo_badop,
115 .vop_setattr = (void *)vop_ebadf,
116 .vop_old_symlink = (void *)fifo_badop,
117 .vop_write = fifo_write
120 VNODEOP_SET(fifo_vnode_vops);
122 static MALLOC_DEFINE(M_FIFOINFO, "Fifo info", "Fifo info entries");
125 * The vnode might be using a shared lock, we need an exclusive lock
126 * for open/close sequencing. Create a little pool of locks.
128 static struct lock fifo_locks[FIFO_LOCK_POOL];
132 fifo_lock(struct vnode *vp)
136 hv = ((intptr_t)vp / sizeof(*vp)) & FIFO_LOCK_MASK;
137 lockmgr(&fifo_locks[hv], LK_EXCLUSIVE);
142 fifo_unlock(struct vnode *vp)
146 hv = ((intptr_t)vp / sizeof(*vp)) & FIFO_LOCK_MASK;
147 lockmgr(&fifo_locks[hv], LK_RELEASE);
155 for (i = 0; i < FIFO_LOCK_POOL; ++i)
156 lockinit(&fifo_locks[i], "fifolk", 0, 0);
158 SYSINIT(fifoinit, SI_SUB_PSEUDO, SI_ORDER_ANY, fifo_init, NULL);
164 fifo_vnoperate(struct vop_generic_args *ap)
166 return (VOCALL(&fifo_vnode_vops, ap));
170 * Trivial lookup routine that always fails.
172 * fifo_lookup(struct vnode *a_dvp, struct vnode **a_vpp,
173 * struct componentname *a_cnp)
177 fifo_lookup(struct vop_old_lookup_args *ap)
184 * Create/destroy the socket pairs for the fifo
188 fifo_fip_create(int *errorp)
190 struct thread *td = curthread;
191 struct fifoinfo *fip;
192 struct socket *rso, *wso;
194 fip = kmalloc(sizeof(*fip), M_FIFOINFO, M_WAITOK);
195 *errorp = socreate(AF_LOCAL, &rso, SOCK_STREAM, 0, td);
197 kfree(fip, M_FIFOINFO);
200 rso->so_options &= ~SO_LINGER;
201 fip->fi_readsock = rso;
202 *errorp = socreate(AF_LOCAL, &wso, SOCK_STREAM, 0, td);
204 soclose(rso, FNONBLOCK);
205 kfree(fip, M_FIFOINFO);
208 wso->so_options &= ~SO_LINGER;
209 fip->fi_writesock = wso;
210 *errorp = unp_connect2(wso, rso);
212 soclose(wso, FNONBLOCK);
213 soclose(rso, FNONBLOCK);
214 kfree(fip, M_FIFOINFO);
217 fip->fi_readers = fip->fi_writers = 0;
218 wso->so_snd.ssb_lowat = PIPE_BUF;
219 sosetstate(rso, SS_CANTRCVMORE);
225 fifo_fip_destroy(struct fifoinfo *fip)
229 error1 = soclose(fip->fi_readsock, FNONBLOCK);
230 error = soclose(fip->fi_writesock, FNONBLOCK);
231 kfree(fip, M_FIFOINFO);
238 * Open called to set up a new instance of a fifo or
239 * to find an active instance of a fifo.
241 * fifo_open(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
246 fifo_open(struct vop_open_args *ap)
248 struct vnode *vp = ap->a_vp;
249 struct fifoinfo *fip;
255 * Create the fip if necessary
258 if ((fip = vp->v_fifoinfo) == NULL) {
259 fip = fifo_fip_create(&error);
264 vp->v_fifoinfo = fip;
268 * Adjust fi_readers and fi_writers interlocked and issue wakeups
271 if (ap->a_mode & FREAD) {
273 if (fip->fi_readers == 1) {
274 soisreconnected(fip->fi_writesock);
275 if (fip->fi_writers > 0) {
276 wakeup((caddr_t)&fip->fi_writers);
277 sowwakeup(fip->fi_writesock);
281 if (ap->a_mode & FWRITE) {
283 if (fip->fi_writers == 1) {
284 soisreconnected(fip->fi_readsock);
285 if (fip->fi_readers > 0) {
286 wakeup((caddr_t)&fip->fi_readers);
287 sorwakeup(fip->fi_writesock);
293 * Handle blocking as appropriate
295 if ((ap->a_mode & FREAD) && (ap->a_mode & O_NONBLOCK) == 0) {
296 if (fip->fi_writers == 0) {
299 error = tsleep((caddr_t)&fip->fi_readers,
300 PCATCH, "fifoor", 0);
301 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
306 * We must have got woken up because we had a writer.
307 * That (and not still having one) is the condition
308 * that we must wait for.
312 if (ap->a_mode & FWRITE) {
313 if (ap->a_mode & O_NONBLOCK) {
314 if (fip->fi_readers == 0) {
319 if (fip->fi_readers == 0) {
322 error = tsleep((caddr_t)&fip->fi_writers,
323 PCATCH, "fifoow", 0);
324 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
329 * We must have got woken up because we had
330 * a reader. That (and not still having one)
331 * is the condition that we must wait for.
336 vsetflags(vp, VNOTSEEKABLE);
337 error = vop_stdopen(ap);
343 if (ap->a_mode & FREAD) {
345 if (fip->fi_readers == 0)
346 soisdisconnected(fip->fi_writesock);
348 if (ap->a_mode & FWRITE) {
350 if (fip->fi_writers == 0)
351 soisdisconnected(fip->fi_readsock);
353 if (fip->fi_readers == 0 && fip->fi_writers == 0) {
354 vp->v_fifoinfo = NULL;
355 (void)fifo_fip_destroy(fip);
365 * fifo_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
366 * struct ucred *a_cred)
370 fifo_read(struct vop_read_args *ap)
372 struct uio *uio = ap->a_uio;
373 struct vnode *vp = ap->a_vp;
374 struct socket *rso = vp->v_fifoinfo->fi_readsock;
379 if (uio->uio_rw != UIO_READ)
380 panic("fifo_read mode");
382 if (uio->uio_resid == 0)
384 if (ap->a_ioflag & IO_NDELAY)
385 flags = MSG_FNONBLOCKING;
389 lwkt_gettoken(&vp->v_token);
390 error = soreceive(rso, NULL, uio, NULL, NULL, &flags);
391 lwkt_reltoken(&vp->v_token);
392 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
399 * fifo_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
400 * struct ucred *a_cred)
404 fifo_write(struct vop_write_args *ap)
406 struct thread *td = ap->a_uio->uio_td;
407 struct vnode *vp = ap->a_vp;
408 struct socket *wso = vp->v_fifoinfo->fi_writesock;
413 if (ap->a_uio->uio_rw != UIO_WRITE)
414 panic("fifo_write mode");
416 if (ap->a_ioflag & IO_NDELAY)
417 flags = MSG_FNONBLOCKING;
421 lwkt_gettoken(&vp->v_token);
422 error = sosend(wso, NULL, ap->a_uio, 0, NULL, flags, td);
423 lwkt_reltoken(&vp->v_token);
424 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
429 * Device ioctl operation.
431 * fifo_ioctl(struct vnode *a_vp, int a_command, caddr_t a_data, int a_fflag,
432 * struct ucred *a_cred, struct sysmsg *a_sysmsg)
436 fifo_ioctl(struct vop_ioctl_args *ap)
438 struct file filetmp; /* Local */
439 struct vnode *vp = ap->a_vp;
442 if (ap->a_fflag & FREAD) {
443 filetmp.f_data = vp->v_fifoinfo->fi_readsock;
444 lwkt_gettoken(&vp->v_token);
445 error = soo_ioctl(&filetmp, ap->a_command, ap->a_data,
446 ap->a_cred, ap->a_sysmsg);
447 lwkt_reltoken(&vp->v_token);
451 if (ap->a_fflag & FWRITE) {
452 filetmp.f_data = vp->v_fifoinfo->fi_writesock;
453 lwkt_gettoken(&vp->v_token);
454 error = soo_ioctl(&filetmp, ap->a_command, ap->a_data,
455 ap->a_cred, ap->a_sysmsg);
456 lwkt_reltoken(&vp->v_token);
464 * fifo_kqfilter(struct vnode *a_vp, struct knote *a_kn)
468 fifo_kqfilter(struct vop_kqfilter_args *ap)
470 struct vnode *vp = ap->a_vp;
471 struct fifoinfo *fi = vp->v_fifoinfo;
473 struct signalsockbuf *ssb;
475 lwkt_gettoken(&vp->v_token);
477 switch (ap->a_kn->kn_filter) {
479 ap->a_kn->kn_fop = &fiforead_filtops;
480 so = fi->fi_readsock;
484 ap->a_kn->kn_fop = &fifowrite_filtops;
485 so = fi->fi_writesock;
489 lwkt_reltoken(&vp->v_token);
493 ap->a_kn->kn_hook = (caddr_t)vp;
494 ssb_insert_knote(ssb, ap->a_kn);
496 lwkt_reltoken(&vp->v_token);
501 filt_fifordetach(struct knote *kn)
503 struct vnode *vp = (void *)kn->kn_hook;
504 struct socket *so = vp->v_fifoinfo->fi_readsock;
506 lwkt_gettoken(&vp->v_token);
507 ssb_remove_knote(&so->so_rcv, kn);
508 lwkt_reltoken(&vp->v_token);
512 filt_fiforead(struct knote *kn, long hint)
514 struct vnode *vp = (void *)kn->kn_hook;
515 struct socket *so = vp->v_fifoinfo->fi_readsock;
517 lwkt_gettoken(&vp->v_token);
518 kn->kn_data = so->so_rcv.ssb_cc;
519 if ((kn->kn_sfflags & NOTE_OLDAPI) == 0 &&
520 so->so_state & SS_ISDISCONNECTED) {
521 if (kn->kn_data == 0)
522 kn->kn_flags |= EV_NODATA;
523 kn->kn_flags |= EV_EOF;
524 lwkt_reltoken(&vp->v_token);
527 kn->kn_flags &= ~(EV_EOF | EV_NODATA);
528 lwkt_reltoken(&vp->v_token);
529 return (kn->kn_data > 0);
533 filt_fifowdetach(struct knote *kn)
535 struct vnode *vp = (void *)kn->kn_hook;
536 struct socket *so = vp->v_fifoinfo->fi_writesock;
538 lwkt_gettoken(&vp->v_token);
539 ssb_remove_knote(&so->so_snd, kn);
540 lwkt_reltoken(&vp->v_token);
544 filt_fifowrite(struct knote *kn, long hint)
546 struct vnode *vp = (void *)kn->kn_hook;
547 struct socket *so = vp->v_fifoinfo->fi_writesock;
549 lwkt_gettoken(&vp->v_token);
550 kn->kn_data = ssb_space(&so->so_snd);
551 if (so->so_state & SS_ISDISCONNECTED) {
552 kn->kn_flags |= (EV_EOF | EV_NODATA);
553 lwkt_reltoken(&vp->v_token);
556 kn->kn_flags &= ~(EV_EOF | EV_NODATA);
557 lwkt_reltoken(&vp->v_token);
558 return (kn->kn_data >= so->so_snd.ssb_lowat);
562 * fifo_inactive(struct vnode *a_vp)
565 fifo_inactive(struct vop_inactive_args *ap)
571 * This is a noop, simply returning what one has been given.
573 * fifo_bmap(struct vnode *a_vp, off_t a_loffset,
574 * off_t *a_doffsetp, int *a_runp, int *a_runb)
577 fifo_bmap(struct vop_bmap_args *ap)
579 if (ap->a_doffsetp != NULL)
580 *ap->a_doffsetp = ap->a_loffset;
581 if (ap->a_runp != NULL)
583 if (ap->a_runb != NULL)
589 * Device close routine
591 * fifo_close(struct vnode *a_vp, int a_fflag)
595 fifo_close(struct vop_close_args *ap)
597 struct vnode *vp = ap->a_vp;
598 struct fifoinfo *fip;
602 fip = vp->v_fifoinfo;
608 if (ap->a_fflag & FREAD) {
610 if (fip->fi_readers == 0)
611 soisdisconnected(fip->fi_writesock);
613 if (ap->a_fflag & FWRITE) {
615 if (fip->fi_writers == 0)
616 soisdisconnected(fip->fi_readsock);
618 if (fip->fi_readers == 0 && fip->fi_writers == 0) {
619 vp->v_fifoinfo = NULL;
620 error = fifo_fip_destroy(fip);
631 * Print out internal contents of a fifo vnode.
634 fifo_printinfo(struct vnode *vp)
636 struct fifoinfo *fip = vp->v_fifoinfo;
638 kprintf(", fifo with %ld readers and %ld writers",
639 fip->fi_readers, fip->fi_writers);
644 * Print out the contents of a fifo vnode.
646 * fifo_print(struct vnode *a_vp)
649 fifo_print(struct vop_print_args *ap)
651 kprintf("tag VT_NON");
652 fifo_printinfo(ap->a_vp);
658 * Return POSIX pathconf information applicable to fifo's.
660 * fifo_pathconf(struct vnode *a_vp, int a_name, int *a_retval)
663 fifo_pathconf(struct vop_pathconf_args *ap)
665 switch (ap->a_name) {
667 *ap->a_retval = LINK_MAX;
670 *ap->a_retval = PIPE_BUF;
672 case _PC_CHOWN_RESTRICTED:
682 * Fifo advisory byte-level locks.
684 * fifo_advlock(struct vnode *a_vp, caddr_t a_id, int a_op, struct flock *a_fl,
689 fifo_advlock(struct vop_advlock_args *ap)
691 return ((ap->a_flags & F_POSIX) ? EINVAL : EOPNOTSUPP);
700 panic("fifo_badop called");