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. We primarily
31 * use v_token to interlock operations.
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/unistd.h>
36 #include <sys/kernel.h>
38 #include <sys/malloc.h>
39 #include <sys/thread2.h>
40 #include <sys/vnode.h>
41 #include <sys/socket.h>
42 #include <sys/socketvar.h>
43 #include <sys/filio.h>
44 #include <sys/fcntl.h>
46 #include <sys/event.h>
49 #include <sys/socketvar2.h>
54 * This structure is associated with the FIFO vnode and stores
55 * the state associated with the FIFO.
58 struct socket *fi_readsock;
59 struct socket *fi_writesock;
64 static int fifo_badop (void);
65 static int fifo_print (struct vop_print_args *);
66 static int fifo_lookup (struct vop_old_lookup_args *);
67 static int fifo_open (struct vop_open_args *);
68 static int fifo_close (struct vop_close_args *);
69 static int fifo_read (struct vop_read_args *);
70 static int fifo_write (struct vop_write_args *);
71 static int fifo_ioctl (struct vop_ioctl_args *);
72 static int fifo_kqfilter (struct vop_kqfilter_args *);
73 static int fifo_inactive (struct vop_inactive_args *);
74 static int fifo_bmap (struct vop_bmap_args *);
75 static int fifo_pathconf (struct vop_pathconf_args *);
76 static int fifo_advlock (struct vop_advlock_args *);
78 static void filt_fifordetach(struct knote *kn);
79 static int filt_fiforead(struct knote *kn, long hint);
80 static void filt_fifowdetach(struct knote *kn);
81 static int filt_fifowrite(struct knote *kn, long hint);
83 static struct filterops fiforead_filtops =
84 { FILTEROP_ISFD, NULL, filt_fifordetach, filt_fiforead };
85 static struct filterops fifowrite_filtops =
86 { FILTEROP_ISFD, NULL, filt_fifowdetach, filt_fifowrite };
88 struct vop_ops fifo_vnode_vops = {
89 .vop_default = vop_defaultop,
90 .vop_access = (void *)vop_ebadf,
91 .vop_advlock = fifo_advlock,
92 .vop_bmap = fifo_bmap,
93 .vop_close = fifo_close,
94 .vop_old_create = (void *)fifo_badop,
95 .vop_getattr = (void *)vop_ebadf,
96 .vop_inactive = fifo_inactive,
97 .vop_ioctl = fifo_ioctl,
98 .vop_kqfilter = fifo_kqfilter,
99 .vop_old_link = (void *)fifo_badop,
100 .vop_old_lookup = fifo_lookup,
101 .vop_old_mkdir = (void *)fifo_badop,
102 .vop_old_mknod = (void *)fifo_badop,
103 .vop_open = fifo_open,
104 .vop_pathconf = fifo_pathconf,
105 .vop_print = fifo_print,
106 .vop_read = fifo_read,
107 .vop_readdir = (void *)fifo_badop,
108 .vop_readlink = (void *)fifo_badop,
109 .vop_reallocblks = (void *)fifo_badop,
110 .vop_reclaim = (void *)vop_null,
111 .vop_old_remove = (void *)fifo_badop,
112 .vop_old_rename = (void *)fifo_badop,
113 .vop_old_rmdir = (void *)fifo_badop,
114 .vop_setattr = (void *)vop_ebadf,
115 .vop_old_symlink = (void *)fifo_badop,
116 .vop_write = fifo_write
119 VNODEOP_SET(fifo_vnode_vops);
121 static MALLOC_DEFINE(M_FIFOINFO, "Fifo info", "Fifo info entries");
127 fifo_vnoperate(struct vop_generic_args *ap)
129 return (VOCALL(&fifo_vnode_vops, ap));
133 * Trivial lookup routine that always fails.
135 * fifo_lookup(struct vnode *a_dvp, struct vnode **a_vpp,
136 * struct componentname *a_cnp)
140 fifo_lookup(struct vop_old_lookup_args *ap)
147 * Open called to set up a new instance of a fifo or
148 * to find an active instance of a fifo.
150 * fifo_open(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
155 fifo_open(struct vop_open_args *ap)
157 struct thread *td = curthread;
158 struct vnode *vp = ap->a_vp;
159 struct fifoinfo *fip;
160 struct socket *rso, *wso;
163 lwkt_gettoken(&vp->v_token);
164 if ((fip = vp->v_fifoinfo) == NULL) {
165 fip = kmalloc(sizeof(*fip), M_FIFOINFO, M_WAITOK);
166 vp->v_fifoinfo = fip;
167 error = socreate(AF_LOCAL, &rso, SOCK_STREAM, 0, td);
169 kfree(fip, M_FIFOINFO);
170 vp->v_fifoinfo = NULL;
173 fip->fi_readsock = rso;
174 error = socreate(AF_LOCAL, &wso, SOCK_STREAM, 0, td);
176 soclose(rso, FNONBLOCK);
177 kfree(fip, M_FIFOINFO);
178 vp->v_fifoinfo = NULL;
181 fip->fi_writesock = wso;
182 error = unp_connect2(wso, rso);
184 soclose(wso, FNONBLOCK);
185 soclose(rso, FNONBLOCK);
186 kfree(fip, M_FIFOINFO);
187 vp->v_fifoinfo = NULL;
190 fip->fi_readers = fip->fi_writers = 0;
191 wso->so_snd.ssb_lowat = PIPE_BUF;
192 sosetstate(rso, SS_CANTRCVMORE);
194 if (ap->a_mode & FREAD) {
196 if (fip->fi_readers == 1) {
197 soisreconnected(fip->fi_writesock);
198 if (fip->fi_writers > 0) {
199 wakeup((caddr_t)&fip->fi_writers);
200 sowwakeup(fip->fi_writesock);
204 if (ap->a_mode & FWRITE) {
206 if (fip->fi_writers == 1) {
207 soisreconnected(fip->fi_readsock);
208 if (fip->fi_readers > 0) {
209 wakeup((caddr_t)&fip->fi_readers);
210 sorwakeup(fip->fi_writesock);
214 if ((ap->a_mode & FREAD) && (ap->a_mode & O_NONBLOCK) == 0) {
215 if (fip->fi_writers == 0) {
217 error = tsleep((caddr_t)&fip->fi_readers,
218 PCATCH, "fifoor", 0);
219 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
223 * We must have got woken up because we had a writer.
224 * That (and not still having one) is the condition
225 * that we must wait for.
229 if (ap->a_mode & FWRITE) {
230 if (ap->a_mode & O_NONBLOCK) {
231 if (fip->fi_readers == 0) {
236 if (fip->fi_readers == 0) {
238 error = tsleep((caddr_t)&fip->fi_writers,
239 PCATCH, "fifoow", 0);
240 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
244 * We must have got woken up because we had
245 * a reader. That (and not still having one)
246 * is the condition that we must wait for.
251 vsetflags(vp, VNOTSEEKABLE);
252 error = vop_stdopen(ap);
253 lwkt_reltoken(&vp->v_token);
256 vop_stdopen(ap); /* bump opencount/writecount as appropriate */
257 VOP_CLOSE(vp, ap->a_mode, NULL);
259 lwkt_reltoken(&vp->v_token);
266 * fifo_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
267 * struct ucred *a_cred)
271 fifo_read(struct vop_read_args *ap)
273 struct uio *uio = ap->a_uio;
274 struct vnode *vp = ap->a_vp;
275 struct socket *rso = vp->v_fifoinfo->fi_readsock;
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 flags = MSG_FNONBLOCKING;
290 lwkt_gettoken(&vp->v_token);
291 error = soreceive(rso, NULL, uio, NULL, NULL, &flags);
292 lwkt_reltoken(&vp->v_token);
293 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
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 thread *td = ap->a_uio->uio_td;
308 struct vnode *vp = ap->a_vp;
309 struct socket *wso = vp->v_fifoinfo->fi_writesock;
314 if (ap->a_uio->uio_rw != UIO_WRITE)
315 panic("fifo_write mode");
317 if (ap->a_ioflag & IO_NDELAY)
318 flags = MSG_FNONBLOCKING;
322 lwkt_gettoken(&vp->v_token);
323 error = sosend(wso, NULL, ap->a_uio, 0, NULL, flags, td);
324 lwkt_reltoken(&vp->v_token);
325 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
330 * Device ioctl operation.
332 * fifo_ioctl(struct vnode *a_vp, int a_command, caddr_t a_data, int a_fflag,
333 * struct ucred *a_cred, struct sysmsg *a_sysmsg)
337 fifo_ioctl(struct vop_ioctl_args *ap)
339 struct file filetmp; /* Local */
340 struct vnode *vp = ap->a_vp;
343 if (ap->a_fflag & FREAD) {
344 filetmp.f_data = vp->v_fifoinfo->fi_readsock;
345 lwkt_gettoken(&vp->v_token);
346 error = soo_ioctl(&filetmp, ap->a_command, ap->a_data,
347 ap->a_cred, ap->a_sysmsg);
348 lwkt_reltoken(&vp->v_token);
352 if (ap->a_fflag & FWRITE) {
353 filetmp.f_data = vp->v_fifoinfo->fi_writesock;
354 lwkt_gettoken(&vp->v_token);
355 error = soo_ioctl(&filetmp, ap->a_command, ap->a_data,
356 ap->a_cred, ap->a_sysmsg);
357 lwkt_reltoken(&vp->v_token);
365 * fifo_kqfilter(struct vnode *a_vp, struct knote *a_kn)
369 fifo_kqfilter(struct vop_kqfilter_args *ap)
371 struct vnode *vp = ap->a_vp;
372 struct fifoinfo *fi = vp->v_fifoinfo;
374 struct signalsockbuf *ssb;
376 lwkt_gettoken(&vp->v_token);
378 switch (ap->a_kn->kn_filter) {
380 ap->a_kn->kn_fop = &fiforead_filtops;
381 so = fi->fi_readsock;
385 ap->a_kn->kn_fop = &fifowrite_filtops;
386 so = fi->fi_writesock;
390 lwkt_reltoken(&vp->v_token);
394 ap->a_kn->kn_hook = (caddr_t)vp;
395 ssb_insert_knote(ssb, ap->a_kn);
397 lwkt_reltoken(&vp->v_token);
402 filt_fifordetach(struct knote *kn)
404 struct vnode *vp = (void *)kn->kn_hook;
405 struct socket *so = vp->v_fifoinfo->fi_readsock;
407 lwkt_gettoken(&vp->v_token);
408 ssb_remove_knote(&so->so_rcv, kn);
409 lwkt_reltoken(&vp->v_token);
413 filt_fiforead(struct knote *kn, long hint)
415 struct vnode *vp = (void *)kn->kn_hook;
416 struct socket *so = vp->v_fifoinfo->fi_readsock;
418 lwkt_gettoken(&vp->v_token);
419 kn->kn_data = so->so_rcv.ssb_cc;
420 if ((kn->kn_sfflags & NOTE_OLDAPI) == 0 &&
421 so->so_state & SS_ISDISCONNECTED) {
422 if (kn->kn_data == 0)
423 kn->kn_flags |= EV_NODATA;
424 kn->kn_flags |= EV_EOF;
425 lwkt_reltoken(&vp->v_token);
428 kn->kn_flags &= ~(EV_EOF | EV_NODATA);
429 lwkt_reltoken(&vp->v_token);
430 return (kn->kn_data > 0);
434 filt_fifowdetach(struct knote *kn)
436 struct vnode *vp = (void *)kn->kn_hook;
437 struct socket *so = vp->v_fifoinfo->fi_writesock;
439 lwkt_gettoken(&vp->v_token);
440 ssb_remove_knote(&so->so_snd, kn);
441 lwkt_reltoken(&vp->v_token);
445 filt_fifowrite(struct knote *kn, long hint)
447 struct vnode *vp = (void *)kn->kn_hook;
448 struct socket *so = vp->v_fifoinfo->fi_writesock;
450 lwkt_gettoken(&vp->v_token);
451 kn->kn_data = ssb_space(&so->so_snd);
452 if (so->so_state & SS_ISDISCONNECTED) {
453 kn->kn_flags |= (EV_EOF | EV_NODATA);
454 lwkt_reltoken(&vp->v_token);
457 kn->kn_flags &= ~(EV_EOF | EV_NODATA);
458 lwkt_reltoken(&vp->v_token);
459 return (kn->kn_data >= so->so_snd.ssb_lowat);
463 * fifo_inactive(struct vnode *a_vp)
466 fifo_inactive(struct vop_inactive_args *ap)
472 * This is a noop, simply returning what one has been given.
474 * fifo_bmap(struct vnode *a_vp, off_t a_loffset,
475 * off_t *a_doffsetp, int *a_runp, int *a_runb)
478 fifo_bmap(struct vop_bmap_args *ap)
480 if (ap->a_doffsetp != NULL)
481 *ap->a_doffsetp = ap->a_loffset;
482 if (ap->a_runp != NULL)
484 if (ap->a_runb != NULL)
490 * Device close routine
492 * fifo_close(struct vnode *a_vp, int a_fflag)
496 fifo_close(struct vop_close_args *ap)
498 struct vnode *vp = ap->a_vp;
499 struct fifoinfo *fip;
502 lwkt_gettoken(&vp->v_token);
503 fip = vp->v_fifoinfo;
504 if (ap->a_fflag & FREAD) {
506 if (fip->fi_readers == 0)
507 soisdisconnected(fip->fi_writesock);
509 if (ap->a_fflag & FWRITE) {
511 if (fip->fi_writers == 0)
512 soisdisconnected(fip->fi_readsock);
514 if (VREFCNT(vp) > 1) {
516 lwkt_reltoken(&vp->v_token);
519 error1 = soclose(fip->fi_readsock, FNONBLOCK);
520 error2 = soclose(fip->fi_writesock, FNONBLOCK);
521 kfree(fip, M_FIFOINFO);
522 vp->v_fifoinfo = NULL;
528 lwkt_reltoken(&vp->v_token);
534 * Print out internal contents of a fifo vnode.
537 fifo_printinfo(struct vnode *vp)
539 struct fifoinfo *fip = vp->v_fifoinfo;
541 kprintf(", fifo with %ld readers and %ld writers",
542 fip->fi_readers, fip->fi_writers);
547 * Print out the contents of a fifo vnode.
549 * fifo_print(struct vnode *a_vp)
552 fifo_print(struct vop_print_args *ap)
554 kprintf("tag VT_NON");
555 fifo_printinfo(ap->a_vp);
561 * Return POSIX pathconf information applicable to fifo's.
563 * fifo_pathconf(struct vnode *a_vp, int a_name, int *a_retval)
566 fifo_pathconf(struct vop_pathconf_args *ap)
568 switch (ap->a_name) {
570 *ap->a_retval = LINK_MAX;
573 *ap->a_retval = PIPE_BUF;
575 case _PC_CHOWN_RESTRICTED:
585 * Fifo advisory byte-level locks.
587 * fifo_advlock(struct vnode *a_vp, caddr_t a_id, int a_op, struct flock *a_fl,
592 fifo_advlock(struct vop_advlock_args *ap)
594 return ((ap->a_flags & F_POSIX) ? EINVAL : EOPNOTSUPP);
603 panic("fifo_badop called");