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
34 * Filesystem FIFO type ops. All entry points are MPSAFE. We primarily
35 * use v_token to interlock operations.
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/unistd.h>
40 #include <sys/kernel.h>
42 #include <sys/malloc.h>
43 #include <sys/thread2.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>
53 #include <sys/thread2.h>
58 * This structure is associated with the FIFO vnode and stores
59 * the state associated with the FIFO.
62 struct socket *fi_readsock;
63 struct socket *fi_writesock;
68 static int fifo_badop (void);
69 static int fifo_print (struct vop_print_args *);
70 static int fifo_lookup (struct vop_old_lookup_args *);
71 static int fifo_open (struct vop_open_args *);
72 static int fifo_close (struct vop_close_args *);
73 static int fifo_read (struct vop_read_args *);
74 static int fifo_write (struct vop_write_args *);
75 static int fifo_ioctl (struct vop_ioctl_args *);
76 static int fifo_kqfilter (struct vop_kqfilter_args *);
77 static int fifo_inactive (struct vop_inactive_args *);
78 static int fifo_bmap (struct vop_bmap_args *);
79 static int fifo_pathconf (struct vop_pathconf_args *);
80 static int fifo_advlock (struct vop_advlock_args *);
82 static void filt_fifordetach(struct knote *kn);
83 static int filt_fiforead(struct knote *kn, long hint);
84 static void filt_fifowdetach(struct knote *kn);
85 static int filt_fifowrite(struct knote *kn, long hint);
87 static struct filterops fiforead_filtops =
88 { FILTEROP_ISFD, NULL, filt_fifordetach, filt_fiforead };
89 static struct filterops fifowrite_filtops =
90 { FILTEROP_ISFD, NULL, filt_fifowdetach, filt_fifowrite };
92 struct vop_ops fifo_vnode_vops = {
93 .vop_default = vop_defaultop,
94 .vop_access = (void *)vop_ebadf,
95 .vop_advlock = fifo_advlock,
96 .vop_bmap = fifo_bmap,
97 .vop_close = fifo_close,
98 .vop_old_create = (void *)fifo_badop,
99 .vop_getattr = (void *)vop_ebadf,
100 .vop_inactive = fifo_inactive,
101 .vop_ioctl = fifo_ioctl,
102 .vop_kqfilter = fifo_kqfilter,
103 .vop_old_link = (void *)fifo_badop,
104 .vop_old_lookup = fifo_lookup,
105 .vop_old_mkdir = (void *)fifo_badop,
106 .vop_old_mknod = (void *)fifo_badop,
107 .vop_open = fifo_open,
108 .vop_pathconf = fifo_pathconf,
109 .vop_print = fifo_print,
110 .vop_read = fifo_read,
111 .vop_readdir = (void *)fifo_badop,
112 .vop_readlink = (void *)fifo_badop,
113 .vop_reallocblks = (void *)fifo_badop,
114 .vop_reclaim = (void *)vop_null,
115 .vop_old_remove = (void *)fifo_badop,
116 .vop_old_rename = (void *)fifo_badop,
117 .vop_old_rmdir = (void *)fifo_badop,
118 .vop_setattr = (void *)vop_ebadf,
119 .vop_old_symlink = (void *)fifo_badop,
120 .vop_write = fifo_write
123 VNODEOP_SET(fifo_vnode_vops);
125 static MALLOC_DEFINE(M_FIFOINFO, "Fifo info", "Fifo info entries");
131 fifo_vnoperate(struct vop_generic_args *ap)
133 return (VOCALL(&fifo_vnode_vops, ap));
137 * Trivial lookup routine that always fails.
139 * fifo_lookup(struct vnode *a_dvp, struct vnode **a_vpp,
140 * struct componentname *a_cnp)
144 fifo_lookup(struct vop_old_lookup_args *ap)
151 * Open called to set up a new instance of a fifo or
152 * to find an active instance of a fifo.
154 * fifo_open(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
159 fifo_open(struct vop_open_args *ap)
161 struct thread *td = curthread;
162 struct vnode *vp = ap->a_vp;
163 struct fifoinfo *fip;
164 struct socket *rso, *wso;
167 lwkt_gettoken(&vp->v_token);
168 if ((fip = vp->v_fifoinfo) == NULL) {
169 MALLOC(fip, struct fifoinfo *, sizeof(*fip), M_FIFOINFO, M_WAITOK);
170 vp->v_fifoinfo = fip;
171 error = socreate(AF_LOCAL, &rso, SOCK_STREAM, 0, td);
173 kfree(fip, M_FIFOINFO);
174 vp->v_fifoinfo = NULL;
177 fip->fi_readsock = rso;
178 error = socreate(AF_LOCAL, &wso, SOCK_STREAM, 0, td);
180 soclose(rso, FNONBLOCK);
181 kfree(fip, M_FIFOINFO);
182 vp->v_fifoinfo = NULL;
185 fip->fi_writesock = wso;
186 error = unp_connect2(wso, rso);
188 soclose(wso, FNONBLOCK);
189 soclose(rso, FNONBLOCK);
190 kfree(fip, M_FIFOINFO);
191 vp->v_fifoinfo = NULL;
194 fip->fi_readers = fip->fi_writers = 0;
195 wso->so_snd.ssb_lowat = PIPE_BUF;
196 rso->so_state |= SS_CANTRCVMORE;
198 if (ap->a_mode & FREAD) {
200 if (fip->fi_readers == 1) {
201 soisreconnected(fip->fi_writesock);
202 if (fip->fi_writers > 0) {
203 wakeup((caddr_t)&fip->fi_writers);
204 sowwakeup(fip->fi_writesock);
208 if (ap->a_mode & FWRITE) {
210 if (fip->fi_writers == 1) {
211 soisreconnected(fip->fi_readsock);
212 if (fip->fi_readers > 0) {
213 wakeup((caddr_t)&fip->fi_readers);
214 sorwakeup(fip->fi_writesock);
218 if ((ap->a_mode & FREAD) && (ap->a_mode & O_NONBLOCK) == 0) {
219 if (fip->fi_writers == 0) {
221 error = tsleep((caddr_t)&fip->fi_readers,
222 PCATCH, "fifoor", 0);
223 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
227 * We must have got woken up because we had a writer.
228 * That (and not still having one) is the condition
229 * that we must wait for.
233 if (ap->a_mode & FWRITE) {
234 if (ap->a_mode & O_NONBLOCK) {
235 if (fip->fi_readers == 0) {
240 if (fip->fi_readers == 0) {
242 error = tsleep((caddr_t)&fip->fi_writers,
243 PCATCH, "fifoow", 0);
244 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
248 * We must have got woken up because we had
249 * a reader. That (and not still having one)
250 * is the condition that we must wait for.
255 vsetflags(vp, VNOTSEEKABLE);
256 error = vop_stdopen(ap);
257 lwkt_reltoken(&vp->v_token);
260 vop_stdopen(ap); /* bump opencount/writecount as appropriate */
261 VOP_CLOSE(vp, ap->a_mode);
263 lwkt_reltoken(&vp->v_token);
270 * fifo_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
271 * struct ucred *a_cred)
275 fifo_read(struct vop_read_args *ap)
277 struct uio *uio = ap->a_uio;
278 struct vnode *vp = ap->a_vp;
279 struct socket *rso = vp->v_fifoinfo->fi_readsock;
280 int error, startresid;
284 if (uio->uio_rw != UIO_READ)
285 panic("fifo_read mode");
287 if (uio->uio_resid == 0)
289 if (ap->a_ioflag & IO_NDELAY)
290 flags = MSG_FNONBLOCKING;
293 startresid = uio->uio_resid;
295 lwkt_gettoken(&vp->v_token);
296 error = soreceive(rso, NULL, uio, NULL, NULL, &flags);
297 lwkt_reltoken(&vp->v_token);
298 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
305 * fifo_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
306 * struct ucred *a_cred)
310 fifo_write(struct vop_write_args *ap)
312 struct thread *td = ap->a_uio->uio_td;
313 struct vnode *vp = ap->a_vp;
314 struct socket *wso = vp->v_fifoinfo->fi_writesock;
319 if (ap->a_uio->uio_rw != UIO_WRITE)
320 panic("fifo_write mode");
322 if (ap->a_ioflag & IO_NDELAY)
323 flags = MSG_FNONBLOCKING;
327 lwkt_gettoken(&vp->v_token);
328 error = sosend(wso, NULL, ap->a_uio, 0, NULL, flags, td);
329 lwkt_reltoken(&vp->v_token);
330 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
335 * Device ioctl operation.
337 * fifo_ioctl(struct vnode *a_vp, int a_command, caddr_t a_data, int a_fflag,
338 * struct ucred *a_cred, struct sysmsg *a_sysmsg)
342 fifo_ioctl(struct vop_ioctl_args *ap)
344 struct file filetmp; /* Local */
345 struct vnode *vp = ap->a_vp;
348 if (ap->a_fflag & FREAD) {
349 filetmp.f_data = vp->v_fifoinfo->fi_readsock;
350 lwkt_gettoken(&vp->v_token);
351 error = soo_ioctl(&filetmp, ap->a_command, ap->a_data,
352 ap->a_cred, ap->a_sysmsg);
353 lwkt_reltoken(&vp->v_token);
357 if (ap->a_fflag & FWRITE) {
358 filetmp.f_data = vp->v_fifoinfo->fi_writesock;
359 lwkt_gettoken(&vp->v_token);
360 error = soo_ioctl(&filetmp, ap->a_command, ap->a_data,
361 ap->a_cred, ap->a_sysmsg);
362 lwkt_reltoken(&vp->v_token);
370 * fifo_kqfilter(struct vnode *a_vp, struct knote *a_kn)
374 fifo_kqfilter(struct vop_kqfilter_args *ap)
376 struct vnode *vp = ap->a_vp;
377 struct fifoinfo *fi = vp->v_fifoinfo;
379 struct signalsockbuf *ssb;
381 lwkt_gettoken(&vp->v_token);
383 switch (ap->a_kn->kn_filter) {
385 ap->a_kn->kn_fop = &fiforead_filtops;
386 so = fi->fi_readsock;
390 ap->a_kn->kn_fop = &fifowrite_filtops;
391 so = fi->fi_writesock;
395 lwkt_reltoken(&vp->v_token);
399 ap->a_kn->kn_hook = (caddr_t)vp;
400 ssb_insert_knote(ssb, ap->a_kn);
402 lwkt_reltoken(&vp->v_token);
407 filt_fifordetach(struct knote *kn)
409 struct vnode *vp = (void *)kn->kn_hook;
410 struct socket *so = vp->v_fifoinfo->fi_readsock;
412 lwkt_gettoken(&vp->v_token);
413 ssb_remove_knote(&so->so_rcv, kn);
414 lwkt_reltoken(&vp->v_token);
418 filt_fiforead(struct knote *kn, long hint)
420 struct vnode *vp = (void *)kn->kn_hook;
421 struct socket *so = vp->v_fifoinfo->fi_readsock;
423 lwkt_gettoken(&vp->v_token);
424 kn->kn_data = so->so_rcv.ssb_cc;
425 if (so->so_state & SS_ISDISCONNECTED) {
426 kn->kn_flags |= EV_EOF;
427 lwkt_reltoken(&vp->v_token);
430 kn->kn_flags &= ~EV_EOF;
431 lwkt_reltoken(&vp->v_token);
432 return (kn->kn_data > 0);
436 filt_fifowdetach(struct knote *kn)
438 struct vnode *vp = (void *)kn->kn_hook;
439 struct socket *so = vp->v_fifoinfo->fi_writesock;
441 lwkt_gettoken(&vp->v_token);
442 ssb_remove_knote(&so->so_snd, kn);
443 lwkt_reltoken(&vp->v_token);
447 filt_fifowrite(struct knote *kn, long hint)
449 struct vnode *vp = (void *)kn->kn_hook;
450 struct socket *so = vp->v_fifoinfo->fi_writesock;
452 lwkt_gettoken(&vp->v_token);
453 kn->kn_data = ssb_space(&so->so_snd);
454 if (so->so_state & SS_ISDISCONNECTED) {
455 kn->kn_flags |= EV_EOF;
456 lwkt_reltoken(&vp->v_token);
459 kn->kn_flags &= ~EV_EOF;
460 lwkt_reltoken(&vp->v_token);
461 return (kn->kn_data >= so->so_snd.ssb_lowat);
465 * fifo_inactive(struct vnode *a_vp)
468 fifo_inactive(struct vop_inactive_args *ap)
474 * This is a noop, simply returning what one has been given.
476 * fifo_bmap(struct vnode *a_vp, off_t a_loffset,
477 * off_t *a_doffsetp, int *a_runp, int *a_runb)
480 fifo_bmap(struct vop_bmap_args *ap)
482 if (ap->a_doffsetp != NULL)
483 *ap->a_doffsetp = ap->a_loffset;
484 if (ap->a_runp != NULL)
486 if (ap->a_runb != NULL)
492 * Device close routine
494 * fifo_close(struct vnode *a_vp, int a_fflag)
498 fifo_close(struct vop_close_args *ap)
500 struct vnode *vp = ap->a_vp;
501 struct fifoinfo *fip;
504 lwkt_gettoken(&vp->v_token);
505 fip = vp->v_fifoinfo;
506 if (ap->a_fflag & FREAD) {
508 if (fip->fi_readers == 0)
509 soisdisconnected(fip->fi_writesock);
511 if (ap->a_fflag & FWRITE) {
513 if (fip->fi_writers == 0)
514 soisdisconnected(fip->fi_readsock);
516 if (vp->v_sysref.refcnt > 1) {
518 lwkt_reltoken(&vp->v_token);
521 error1 = soclose(fip->fi_readsock, FNONBLOCK);
522 error2 = soclose(fip->fi_writesock, FNONBLOCK);
523 FREE(fip, M_FIFOINFO);
524 vp->v_fifoinfo = NULL;
530 lwkt_reltoken(&vp->v_token);
536 * Print out internal contents of a fifo vnode.
539 fifo_printinfo(struct vnode *vp)
541 struct fifoinfo *fip = vp->v_fifoinfo;
543 kprintf(", fifo with %ld readers and %ld writers",
544 fip->fi_readers, fip->fi_writers);
549 * Print out the contents of a fifo vnode.
551 * fifo_print(struct vnode *a_vp)
554 fifo_print(struct vop_print_args *ap)
556 kprintf("tag VT_NON");
557 fifo_printinfo(ap->a_vp);
563 * Return POSIX pathconf information applicable to fifo's.
565 * fifo_pathconf(struct vnode *a_vp, int a_name, int *a_retval)
568 fifo_pathconf(struct vop_pathconf_args *ap)
570 switch (ap->a_name) {
572 *ap->a_retval = LINK_MAX;
575 *ap->a_retval = PIPE_BUF;
577 case _PC_CHOWN_RESTRICTED:
587 * Fifo advisory byte-level locks.
589 * fifo_advlock(struct vnode *a_vp, caddr_t a_id, int a_op, struct flock *a_fl,
594 fifo_advlock(struct vop_advlock_args *ap)
596 return ((ap->a_flags & F_POSIX) ? EINVAL : EOPNOTSUPP);
605 panic("fifo_badop called");