Deleted Added
full compact
fifo_vnops.c (94995) fifo_vnops.c (95759)
1/*
2 * Copyright (c) 1990, 1993, 1995
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
20 *
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
31 * SUCH DAMAGE.
32 *
33 * @(#)fifo_vnops.c 8.10 (Berkeley) 5/27/95
1/*
2 * Copyright (c) 1990, 1993, 1995
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
20 *
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
31 * SUCH DAMAGE.
32 *
33 * @(#)fifo_vnops.c 8.10 (Berkeley) 5/27/95
34 * $FreeBSD: head/sys/fs/fifofs/fifo_vnops.c 94995 2002-04-18 14:47:34Z alfred $
34 * $FreeBSD: head/sys/fs/fifofs/fifo_vnops.c 95759 2002-04-30 01:54:54Z tanimura $
35 */
36
37#include <sys/param.h>
35 */
36
37#include <sys/param.h>
38#include <sys/systm.h>
39#include <sys/unistd.h>
38#include <sys/event.h>
39#include <sys/filio.h>
40#include <sys/fcntl.h>
41#include <sys/file.h>
40#include <sys/kernel.h>
41#include <sys/lock.h>
42#include <sys/mutex.h>
43#include <sys/malloc.h>
42#include <sys/kernel.h>
43#include <sys/lock.h>
44#include <sys/mutex.h>
45#include <sys/malloc.h>
44#include <sys/vnode.h>
46#include <sys/poll.h>
45#include <sys/proc.h> /* XXXKSE */
47#include <sys/proc.h> /* XXXKSE */
48#include <sys/signalvar.h>
46#include <sys/socket.h>
47#include <sys/socketvar.h>
49#include <sys/socket.h>
50#include <sys/socketvar.h>
48#include <sys/filio.h>
49#include <sys/fcntl.h>
50#include <sys/file.h>
51#include <sys/event.h>
52#include <sys/poll.h>
51#include <sys/sx.h>
52#include <sys/systm.h>
53#include <sys/un.h>
53#include <sys/un.h>
54#include <sys/unistd.h>
55#include <sys/vnode.h>
54#include <fs/fifofs/fifo.h>
55
56/*
57 * This structure is associated with the FIFO vnode and stores
58 * the state associated with the FIFO.
59 */
60struct fifoinfo {
61 struct socket *fi_readsock;
62 struct socket *fi_writesock;
63 long fi_readers;
64 long fi_writers;
65};
66
67static int fifo_badop(void);
68static int fifo_print(struct vop_print_args *);
69static int fifo_lookup(struct vop_lookup_args *);
70static int fifo_open(struct vop_open_args *);
71static int fifo_close(struct vop_close_args *);
72static int fifo_read(struct vop_read_args *);
73static int fifo_write(struct vop_write_args *);
74static int fifo_ioctl(struct vop_ioctl_args *);
75static int fifo_poll(struct vop_poll_args *);
76static int fifo_kqfilter(struct vop_kqfilter_args *);
77static int fifo_pathconf(struct vop_pathconf_args *);
78static int fifo_advlock(struct vop_advlock_args *);
79
80static void filt_fifordetach(struct knote *kn);
81static int filt_fiforead(struct knote *kn, long hint);
82static void filt_fifowdetach(struct knote *kn);
83static int filt_fifowrite(struct knote *kn, long hint);
84
85static struct filterops fiforead_filtops =
86 { 1, NULL, filt_fifordetach, filt_fiforead };
87static struct filterops fifowrite_filtops =
88 { 1, NULL, filt_fifowdetach, filt_fifowrite };
89
90vop_t **fifo_vnodeop_p;
91static struct vnodeopv_entry_desc fifo_vnodeop_entries[] = {
92 { &vop_default_desc, (vop_t *) vop_defaultop },
93 { &vop_access_desc, (vop_t *) vop_ebadf },
94 { &vop_advlock_desc, (vop_t *) fifo_advlock },
95 { &vop_close_desc, (vop_t *) fifo_close },
96 { &vop_create_desc, (vop_t *) fifo_badop },
97 { &vop_getattr_desc, (vop_t *) vop_ebadf },
98 { &vop_getwritemount_desc, (vop_t *) vop_stdgetwritemount },
99 { &vop_ioctl_desc, (vop_t *) fifo_ioctl },
100 { &vop_kqfilter_desc, (vop_t *) fifo_kqfilter },
101 { &vop_lease_desc, (vop_t *) vop_null },
102 { &vop_link_desc, (vop_t *) fifo_badop },
103 { &vop_lookup_desc, (vop_t *) fifo_lookup },
104 { &vop_mkdir_desc, (vop_t *) fifo_badop },
105 { &vop_mknod_desc, (vop_t *) fifo_badop },
106 { &vop_open_desc, (vop_t *) fifo_open },
107 { &vop_pathconf_desc, (vop_t *) fifo_pathconf },
108 { &vop_poll_desc, (vop_t *) fifo_poll },
109 { &vop_print_desc, (vop_t *) fifo_print },
110 { &vop_read_desc, (vop_t *) fifo_read },
111 { &vop_readdir_desc, (vop_t *) fifo_badop },
112 { &vop_readlink_desc, (vop_t *) fifo_badop },
113 { &vop_reallocblks_desc, (vop_t *) fifo_badop },
114 { &vop_reclaim_desc, (vop_t *) vop_null },
115 { &vop_remove_desc, (vop_t *) fifo_badop },
116 { &vop_rename_desc, (vop_t *) fifo_badop },
117 { &vop_rmdir_desc, (vop_t *) fifo_badop },
118 { &vop_setattr_desc, (vop_t *) vop_ebadf },
119 { &vop_symlink_desc, (vop_t *) fifo_badop },
120 { &vop_write_desc, (vop_t *) fifo_write },
121 { NULL, NULL }
122};
123static struct vnodeopv_desc fifo_vnodeop_opv_desc =
124 { &fifo_vnodeop_p, fifo_vnodeop_entries };
125
126VNODEOP_SET(fifo_vnodeop_opv_desc);
127
128int
129fifo_vnoperate(ap)
130 struct vop_generic_args /* {
131 struct vnodeop_desc *a_desc;
132 <other random data follows, presumably>
133 } */ *ap;
134{
135 return (VOCALL(fifo_vnodeop_p, ap->a_desc->vdesc_offset, ap));
136}
137
138/*
139 * Trivial lookup routine that always fails.
140 */
141/* ARGSUSED */
142static int
143fifo_lookup(ap)
144 struct vop_lookup_args /* {
145 struct vnode * a_dvp;
146 struct vnode ** a_vpp;
147 struct componentname * a_cnp;
148 } */ *ap;
149{
150
151 *ap->a_vpp = NULL;
152 return (ENOTDIR);
153}
154
155/*
156 * Open called to set up a new instance of a fifo or
157 * to find an active instance of a fifo.
158 */
159/* ARGSUSED */
160static int
161fifo_open(ap)
162 struct vop_open_args /* {
163 struct vnode *a_vp;
164 int a_mode;
165 struct ucred *a_cred;
166 struct thread *a_td;
167 } */ *ap;
168{
169 struct vnode *vp = ap->a_vp;
170 struct fifoinfo *fip;
171 struct thread *td = ap->a_td;
172 struct socket *rso, *wso;
173 int error;
174
175 if ((fip = vp->v_fifoinfo) == NULL) {
176 MALLOC(fip, struct fifoinfo *, sizeof(*fip), M_VNODE, M_WAITOK);
177 vp->v_fifoinfo = fip;
178 error = socreate(AF_LOCAL, &rso, SOCK_STREAM, 0,
179 ap->a_td->td_ucred, ap->a_td);
180 if (error) {
181 free(fip, M_VNODE);
182 vp->v_fifoinfo = NULL;
183 return (error);
184 }
185 fip->fi_readsock = rso;
186 error = socreate(AF_LOCAL, &wso, SOCK_STREAM, 0,
187 ap->a_td->td_ucred, ap->a_td);
188 if (error) {
189 (void)soclose(rso);
190 free(fip, M_VNODE);
191 vp->v_fifoinfo = NULL;
192 return (error);
193 }
194 fip->fi_writesock = wso;
195 error = unp_connect2(wso, rso);
196 if (error) {
197 (void)soclose(wso);
198 (void)soclose(rso);
199 free(fip, M_VNODE);
200 vp->v_fifoinfo = NULL;
201 return (error);
202 }
203 fip->fi_readers = fip->fi_writers = 0;
204 wso->so_snd.sb_lowat = PIPE_BUF;
205 rso->so_state |= SS_CANTRCVMORE;
206 }
207 if (ap->a_mode & FREAD) {
208 fip->fi_readers++;
209 if (fip->fi_readers == 1) {
210 fip->fi_writesock->so_state &= ~SS_CANTSENDMORE;
211 if (fip->fi_writers > 0) {
212 wakeup((caddr_t)&fip->fi_writers);
213 sowwakeup(fip->fi_writesock);
214 }
215 }
216 }
217 if (ap->a_mode & FWRITE) {
218 fip->fi_writers++;
219 if (fip->fi_writers == 1) {
220 fip->fi_readsock->so_state &= ~SS_CANTRCVMORE;
221 if (fip->fi_readers > 0) {
222 wakeup((caddr_t)&fip->fi_readers);
223 sorwakeup(fip->fi_writesock);
224 }
225 }
226 }
227 if ((ap->a_mode & FREAD) && (ap->a_mode & O_NONBLOCK) == 0) {
228 while (fip->fi_writers == 0) {
229 VOP_UNLOCK(vp, 0, td);
230 error = tsleep((caddr_t)&fip->fi_readers,
231 PCATCH | PSOCK, "fifoor", 0);
232 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
233 if (error)
234 goto bad;
235 }
236 }
237 if (ap->a_mode & FWRITE) {
238 if (ap->a_mode & O_NONBLOCK) {
239 if (fip->fi_readers == 0) {
240 error = ENXIO;
241 goto bad;
242 }
243 } else {
244 while (fip->fi_readers == 0) {
245 VOP_UNLOCK(vp, 0, td);
246 error = tsleep((caddr_t)&fip->fi_writers,
247 PCATCH | PSOCK, "fifoow", 0);
248 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
249 if (error)
250 goto bad;
251 }
252 }
253 }
254 return (0);
255bad:
256 VOP_CLOSE(vp, ap->a_mode, ap->a_cred, td);
257 return (error);
258}
259
260/*
261 * Vnode op for read
262 */
263/* ARGSUSED */
264static int
265fifo_read(ap)
266 struct vop_read_args /* {
267 struct vnode *a_vp;
268 struct uio *a_uio;
269 int a_ioflag;
270 struct ucred *a_cred;
271 } */ *ap;
272{
273 struct uio *uio = ap->a_uio;
274 struct socket *rso = ap->a_vp->v_fifoinfo->fi_readsock;
275 struct thread *td = uio->uio_td;
276 int error, startresid;
277
278#ifdef DIAGNOSTIC
279 if (uio->uio_rw != UIO_READ)
280 panic("fifo_read mode");
281#endif
282 if (uio->uio_resid == 0)
283 return (0);
284 if (ap->a_ioflag & IO_NDELAY)
285 rso->so_state |= SS_NBIO;
286 startresid = uio->uio_resid;
287 VOP_UNLOCK(ap->a_vp, 0, td);
288 error = soreceive(rso, (struct sockaddr **)0, uio, (struct mbuf **)0,
289 (struct mbuf **)0, (int *)0);
290 vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY, td);
291 if (ap->a_ioflag & IO_NDELAY)
292 rso->so_state &= ~SS_NBIO;
293 return (error);
294}
295
296/*
297 * Vnode op for write
298 */
299/* ARGSUSED */
300static int
301fifo_write(ap)
302 struct vop_write_args /* {
303 struct vnode *a_vp;
304 struct uio *a_uio;
305 int a_ioflag;
306 struct ucred *a_cred;
307 } */ *ap;
308{
309 struct socket *wso = ap->a_vp->v_fifoinfo->fi_writesock;
310 struct thread *td = ap->a_uio->uio_td;
311 int error;
312
313#ifdef DIAGNOSTIC
314 if (ap->a_uio->uio_rw != UIO_WRITE)
315 panic("fifo_write mode");
316#endif
317 if (ap->a_ioflag & IO_NDELAY)
318 wso->so_state |= SS_NBIO;
319 VOP_UNLOCK(ap->a_vp, 0, td);
320 error = sosend(wso, (struct sockaddr *)0, ap->a_uio, 0,
321 (struct mbuf *)0, 0, td);
322 vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY, td);
323 if (ap->a_ioflag & IO_NDELAY)
324 wso->so_state &= ~SS_NBIO;
325 return (error);
326}
327
328/*
329 * Device ioctl operation.
330 */
331/* ARGSUSED */
332static int
333fifo_ioctl(ap)
334 struct vop_ioctl_args /* {
335 struct vnode *a_vp;
336 int a_command;
337 caddr_t a_data;
338 int a_fflag;
339 struct ucred *a_cred;
340 struct thread *a_td;
341 } */ *ap;
342{
343 struct file filetmp; /* Local, so need not be locked. */
344 int error;
345
346 if (ap->a_command == FIONBIO)
347 return (0);
348 if (ap->a_fflag & FREAD) {
349 filetmp.f_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_readsock;
350 error = soo_ioctl(&filetmp, ap->a_command, ap->a_data, ap->a_td);
351 if (error)
352 return (error);
353 }
354 if (ap->a_fflag & FWRITE) {
355 filetmp.f_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_writesock;
356 error = soo_ioctl(&filetmp, ap->a_command, ap->a_data, ap->a_td);
357 if (error)
358 return (error);
359 }
360 return (0);
361}
362
363/* ARGSUSED */
364static int
365fifo_kqfilter(ap)
366 struct vop_kqfilter_args /* {
367 struct vnode *a_vp;
368 struct knote *a_kn;
369 } */ *ap;
370{
371 struct fifoinfo *fi = ap->a_vp->v_fifoinfo;
372 struct socket *so;
373 struct sockbuf *sb;
374
375 switch (ap->a_kn->kn_filter) {
376 case EVFILT_READ:
377 ap->a_kn->kn_fop = &fiforead_filtops;
378 so = fi->fi_readsock;
379 sb = &so->so_rcv;
380 break;
381 case EVFILT_WRITE:
382 ap->a_kn->kn_fop = &fifowrite_filtops;
383 so = fi->fi_writesock;
384 sb = &so->so_snd;
385 break;
386 default:
387 return (1);
388 }
389
390 ap->a_kn->kn_hook = (caddr_t)so;
391
392 SLIST_INSERT_HEAD(&sb->sb_sel.si_note, ap->a_kn, kn_selnext);
393 sb->sb_flags |= SB_KNOTE;
394
395 return (0);
396}
397
398static void
399filt_fifordetach(struct knote *kn)
400{
401 struct socket *so = (struct socket *)kn->kn_hook;
402
403 SLIST_REMOVE(&so->so_rcv.sb_sel.si_note, kn, knote, kn_selnext);
404 if (SLIST_EMPTY(&so->so_rcv.sb_sel.si_note))
405 so->so_rcv.sb_flags &= ~SB_KNOTE;
406}
407
408static int
409filt_fiforead(struct knote *kn, long hint)
410{
411 struct socket *so = (struct socket *)kn->kn_hook;
412
413 kn->kn_data = so->so_rcv.sb_cc;
414 if (so->so_state & SS_CANTRCVMORE) {
415 kn->kn_flags |= EV_EOF;
416 return (1);
417 }
418 kn->kn_flags &= ~EV_EOF;
419 return (kn->kn_data > 0);
420}
421
422static void
423filt_fifowdetach(struct knote *kn)
424{
425 struct socket *so = (struct socket *)kn->kn_hook;
426
427 SLIST_REMOVE(&so->so_snd.sb_sel.si_note, kn, knote, kn_selnext);
428 if (SLIST_EMPTY(&so->so_snd.sb_sel.si_note))
429 so->so_snd.sb_flags &= ~SB_KNOTE;
430}
431
432static int
433filt_fifowrite(struct knote *kn, long hint)
434{
435 struct socket *so = (struct socket *)kn->kn_hook;
436
437 kn->kn_data = sbspace(&so->so_snd);
438 if (so->so_state & SS_CANTSENDMORE) {
439 kn->kn_flags |= EV_EOF;
440 return (1);
441 }
442 kn->kn_flags &= ~EV_EOF;
443 return (kn->kn_data >= so->so_snd.sb_lowat);
444}
445
446/* ARGSUSED */
447static int
448fifo_poll(ap)
449 struct vop_poll_args /* {
450 struct vnode *a_vp;
451 int a_events;
452 struct ucred *a_cred;
453 struct thread *a_td;
454 } */ *ap;
455{
456 struct file filetmp;
457 int events, revents = 0;
458
459 events = ap->a_events &
460 (POLLIN | POLLINIGNEOF | POLLPRI | POLLRDNORM | POLLRDBAND);
461 if (events) {
462 /*
463 * If POLLIN or POLLRDNORM is requested and POLLINIGNEOF is
464 * not, then convert the first two to the last one. This
465 * tells the socket poll function to ignore EOF so that we
466 * block if there is no writer (and no data). Callers can
467 * set POLLINIGNEOF to get non-blocking behavior.
468 */
469 if (events & (POLLIN | POLLRDNORM) &&
470 !(events & POLLINIGNEOF)) {
471 events &= ~(POLLIN | POLLRDNORM);
472 events |= POLLINIGNEOF;
473 }
474
475 filetmp.f_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_readsock;
476 if (filetmp.f_data)
477 revents |= soo_poll(&filetmp, events, ap->a_cred,
478 ap->a_td);
479
480 /* Reverse the above conversion. */
481 if ((revents & POLLINIGNEOF) &&
482 !(ap->a_events & POLLINIGNEOF)) {
483 revents |= (ap->a_events & (POLLIN | POLLRDNORM));
484 revents &= ~POLLINIGNEOF;
485 }
486 }
487 events = ap->a_events & (POLLOUT | POLLWRNORM | POLLWRBAND);
488 if (events) {
489 filetmp.f_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_writesock;
490 if (filetmp.f_data)
491 revents |= soo_poll(&filetmp, events, ap->a_cred,
492 ap->a_td);
493 }
494 return (revents);
495}
496
497/*
498 * Device close routine
499 */
500/* ARGSUSED */
501static int
502fifo_close(ap)
503 struct vop_close_args /* {
504 struct vnode *a_vp;
505 int a_fflag;
506 struct ucred *a_cred;
507 struct thread *a_td;
508 } */ *ap;
509{
510 register struct vnode *vp = ap->a_vp;
511 register struct fifoinfo *fip = vp->v_fifoinfo;
512 int error1, error2;
513
514 if (ap->a_fflag & FREAD) {
515 fip->fi_readers--;
516 if (fip->fi_readers == 0)
517 socantsendmore(fip->fi_writesock);
518 }
519 if (ap->a_fflag & FWRITE) {
520 fip->fi_writers--;
521 if (fip->fi_writers == 0)
522 socantrcvmore(fip->fi_readsock);
523 }
524 if (vp->v_usecount > 1)
525 return (0);
526 error1 = soclose(fip->fi_readsock);
527 error2 = soclose(fip->fi_writesock);
528 FREE(fip, M_VNODE);
529 vp->v_fifoinfo = NULL;
530 if (error1)
531 return (error1);
532 return (error2);
533}
534
535
536/*
537 * Print out internal contents of a fifo vnode.
538 */
539int
540fifo_printinfo(vp)
541 struct vnode *vp;
542{
543 register struct fifoinfo *fip = vp->v_fifoinfo;
544
545 printf(", fifo with %ld readers and %ld writers",
546 fip->fi_readers, fip->fi_writers);
547 return (0);
548}
549
550/*
551 * Print out the contents of a fifo vnode.
552 */
553static int
554fifo_print(ap)
555 struct vop_print_args /* {
556 struct vnode *a_vp;
557 } */ *ap;
558{
559
560 printf("tag VT_NON");
561 fifo_printinfo(ap->a_vp);
562 printf("\n");
563 return (0);
564}
565
566/*
567 * Return POSIX pathconf information applicable to fifo's.
568 */
569int
570fifo_pathconf(ap)
571 struct vop_pathconf_args /* {
572 struct vnode *a_vp;
573 int a_name;
574 int *a_retval;
575 } */ *ap;
576{
577
578 switch (ap->a_name) {
579 case _PC_LINK_MAX:
580 *ap->a_retval = LINK_MAX;
581 return (0);
582 case _PC_PIPE_BUF:
583 *ap->a_retval = PIPE_BUF;
584 return (0);
585 case _PC_CHOWN_RESTRICTED:
586 *ap->a_retval = 1;
587 return (0);
588 default:
589 return (EINVAL);
590 }
591 /* NOTREACHED */
592}
593
594/*
595 * Fifo advisory byte-level locks.
596 */
597/* ARGSUSED */
598static int
599fifo_advlock(ap)
600 struct vop_advlock_args /* {
601 struct vnode *a_vp;
602 caddr_t a_id;
603 int a_op;
604 struct flock *a_fl;
605 int a_flags;
606 } */ *ap;
607{
608
609 return (ap->a_flags & F_FLOCK ? EOPNOTSUPP : EINVAL);
610}
611
612/*
613 * Fifo bad operation
614 */
615static int
616fifo_badop()
617{
618
619 panic("fifo_badop called");
620 /* NOTREACHED */
621}
56#include <fs/fifofs/fifo.h>
57
58/*
59 * This structure is associated with the FIFO vnode and stores
60 * the state associated with the FIFO.
61 */
62struct fifoinfo {
63 struct socket *fi_readsock;
64 struct socket *fi_writesock;
65 long fi_readers;
66 long fi_writers;
67};
68
69static int fifo_badop(void);
70static int fifo_print(struct vop_print_args *);
71static int fifo_lookup(struct vop_lookup_args *);
72static int fifo_open(struct vop_open_args *);
73static int fifo_close(struct vop_close_args *);
74static int fifo_read(struct vop_read_args *);
75static int fifo_write(struct vop_write_args *);
76static int fifo_ioctl(struct vop_ioctl_args *);
77static int fifo_poll(struct vop_poll_args *);
78static int fifo_kqfilter(struct vop_kqfilter_args *);
79static int fifo_pathconf(struct vop_pathconf_args *);
80static int fifo_advlock(struct vop_advlock_args *);
81
82static void filt_fifordetach(struct knote *kn);
83static int filt_fiforead(struct knote *kn, long hint);
84static void filt_fifowdetach(struct knote *kn);
85static int filt_fifowrite(struct knote *kn, long hint);
86
87static struct filterops fiforead_filtops =
88 { 1, NULL, filt_fifordetach, filt_fiforead };
89static struct filterops fifowrite_filtops =
90 { 1, NULL, filt_fifowdetach, filt_fifowrite };
91
92vop_t **fifo_vnodeop_p;
93static struct vnodeopv_entry_desc fifo_vnodeop_entries[] = {
94 { &vop_default_desc, (vop_t *) vop_defaultop },
95 { &vop_access_desc, (vop_t *) vop_ebadf },
96 { &vop_advlock_desc, (vop_t *) fifo_advlock },
97 { &vop_close_desc, (vop_t *) fifo_close },
98 { &vop_create_desc, (vop_t *) fifo_badop },
99 { &vop_getattr_desc, (vop_t *) vop_ebadf },
100 { &vop_getwritemount_desc, (vop_t *) vop_stdgetwritemount },
101 { &vop_ioctl_desc, (vop_t *) fifo_ioctl },
102 { &vop_kqfilter_desc, (vop_t *) fifo_kqfilter },
103 { &vop_lease_desc, (vop_t *) vop_null },
104 { &vop_link_desc, (vop_t *) fifo_badop },
105 { &vop_lookup_desc, (vop_t *) fifo_lookup },
106 { &vop_mkdir_desc, (vop_t *) fifo_badop },
107 { &vop_mknod_desc, (vop_t *) fifo_badop },
108 { &vop_open_desc, (vop_t *) fifo_open },
109 { &vop_pathconf_desc, (vop_t *) fifo_pathconf },
110 { &vop_poll_desc, (vop_t *) fifo_poll },
111 { &vop_print_desc, (vop_t *) fifo_print },
112 { &vop_read_desc, (vop_t *) fifo_read },
113 { &vop_readdir_desc, (vop_t *) fifo_badop },
114 { &vop_readlink_desc, (vop_t *) fifo_badop },
115 { &vop_reallocblks_desc, (vop_t *) fifo_badop },
116 { &vop_reclaim_desc, (vop_t *) vop_null },
117 { &vop_remove_desc, (vop_t *) fifo_badop },
118 { &vop_rename_desc, (vop_t *) fifo_badop },
119 { &vop_rmdir_desc, (vop_t *) fifo_badop },
120 { &vop_setattr_desc, (vop_t *) vop_ebadf },
121 { &vop_symlink_desc, (vop_t *) fifo_badop },
122 { &vop_write_desc, (vop_t *) fifo_write },
123 { NULL, NULL }
124};
125static struct vnodeopv_desc fifo_vnodeop_opv_desc =
126 { &fifo_vnodeop_p, fifo_vnodeop_entries };
127
128VNODEOP_SET(fifo_vnodeop_opv_desc);
129
130int
131fifo_vnoperate(ap)
132 struct vop_generic_args /* {
133 struct vnodeop_desc *a_desc;
134 <other random data follows, presumably>
135 } */ *ap;
136{
137 return (VOCALL(fifo_vnodeop_p, ap->a_desc->vdesc_offset, ap));
138}
139
140/*
141 * Trivial lookup routine that always fails.
142 */
143/* ARGSUSED */
144static int
145fifo_lookup(ap)
146 struct vop_lookup_args /* {
147 struct vnode * a_dvp;
148 struct vnode ** a_vpp;
149 struct componentname * a_cnp;
150 } */ *ap;
151{
152
153 *ap->a_vpp = NULL;
154 return (ENOTDIR);
155}
156
157/*
158 * Open called to set up a new instance of a fifo or
159 * to find an active instance of a fifo.
160 */
161/* ARGSUSED */
162static int
163fifo_open(ap)
164 struct vop_open_args /* {
165 struct vnode *a_vp;
166 int a_mode;
167 struct ucred *a_cred;
168 struct thread *a_td;
169 } */ *ap;
170{
171 struct vnode *vp = ap->a_vp;
172 struct fifoinfo *fip;
173 struct thread *td = ap->a_td;
174 struct socket *rso, *wso;
175 int error;
176
177 if ((fip = vp->v_fifoinfo) == NULL) {
178 MALLOC(fip, struct fifoinfo *, sizeof(*fip), M_VNODE, M_WAITOK);
179 vp->v_fifoinfo = fip;
180 error = socreate(AF_LOCAL, &rso, SOCK_STREAM, 0,
181 ap->a_td->td_ucred, ap->a_td);
182 if (error) {
183 free(fip, M_VNODE);
184 vp->v_fifoinfo = NULL;
185 return (error);
186 }
187 fip->fi_readsock = rso;
188 error = socreate(AF_LOCAL, &wso, SOCK_STREAM, 0,
189 ap->a_td->td_ucred, ap->a_td);
190 if (error) {
191 (void)soclose(rso);
192 free(fip, M_VNODE);
193 vp->v_fifoinfo = NULL;
194 return (error);
195 }
196 fip->fi_writesock = wso;
197 error = unp_connect2(wso, rso);
198 if (error) {
199 (void)soclose(wso);
200 (void)soclose(rso);
201 free(fip, M_VNODE);
202 vp->v_fifoinfo = NULL;
203 return (error);
204 }
205 fip->fi_readers = fip->fi_writers = 0;
206 wso->so_snd.sb_lowat = PIPE_BUF;
207 rso->so_state |= SS_CANTRCVMORE;
208 }
209 if (ap->a_mode & FREAD) {
210 fip->fi_readers++;
211 if (fip->fi_readers == 1) {
212 fip->fi_writesock->so_state &= ~SS_CANTSENDMORE;
213 if (fip->fi_writers > 0) {
214 wakeup((caddr_t)&fip->fi_writers);
215 sowwakeup(fip->fi_writesock);
216 }
217 }
218 }
219 if (ap->a_mode & FWRITE) {
220 fip->fi_writers++;
221 if (fip->fi_writers == 1) {
222 fip->fi_readsock->so_state &= ~SS_CANTRCVMORE;
223 if (fip->fi_readers > 0) {
224 wakeup((caddr_t)&fip->fi_readers);
225 sorwakeup(fip->fi_writesock);
226 }
227 }
228 }
229 if ((ap->a_mode & FREAD) && (ap->a_mode & O_NONBLOCK) == 0) {
230 while (fip->fi_writers == 0) {
231 VOP_UNLOCK(vp, 0, td);
232 error = tsleep((caddr_t)&fip->fi_readers,
233 PCATCH | PSOCK, "fifoor", 0);
234 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
235 if (error)
236 goto bad;
237 }
238 }
239 if (ap->a_mode & FWRITE) {
240 if (ap->a_mode & O_NONBLOCK) {
241 if (fip->fi_readers == 0) {
242 error = ENXIO;
243 goto bad;
244 }
245 } else {
246 while (fip->fi_readers == 0) {
247 VOP_UNLOCK(vp, 0, td);
248 error = tsleep((caddr_t)&fip->fi_writers,
249 PCATCH | PSOCK, "fifoow", 0);
250 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
251 if (error)
252 goto bad;
253 }
254 }
255 }
256 return (0);
257bad:
258 VOP_CLOSE(vp, ap->a_mode, ap->a_cred, td);
259 return (error);
260}
261
262/*
263 * Vnode op for read
264 */
265/* ARGSUSED */
266static int
267fifo_read(ap)
268 struct vop_read_args /* {
269 struct vnode *a_vp;
270 struct uio *a_uio;
271 int a_ioflag;
272 struct ucred *a_cred;
273 } */ *ap;
274{
275 struct uio *uio = ap->a_uio;
276 struct socket *rso = ap->a_vp->v_fifoinfo->fi_readsock;
277 struct thread *td = uio->uio_td;
278 int error, startresid;
279
280#ifdef DIAGNOSTIC
281 if (uio->uio_rw != UIO_READ)
282 panic("fifo_read mode");
283#endif
284 if (uio->uio_resid == 0)
285 return (0);
286 if (ap->a_ioflag & IO_NDELAY)
287 rso->so_state |= SS_NBIO;
288 startresid = uio->uio_resid;
289 VOP_UNLOCK(ap->a_vp, 0, td);
290 error = soreceive(rso, (struct sockaddr **)0, uio, (struct mbuf **)0,
291 (struct mbuf **)0, (int *)0);
292 vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY, td);
293 if (ap->a_ioflag & IO_NDELAY)
294 rso->so_state &= ~SS_NBIO;
295 return (error);
296}
297
298/*
299 * Vnode op for write
300 */
301/* ARGSUSED */
302static int
303fifo_write(ap)
304 struct vop_write_args /* {
305 struct vnode *a_vp;
306 struct uio *a_uio;
307 int a_ioflag;
308 struct ucred *a_cred;
309 } */ *ap;
310{
311 struct socket *wso = ap->a_vp->v_fifoinfo->fi_writesock;
312 struct thread *td = ap->a_uio->uio_td;
313 int error;
314
315#ifdef DIAGNOSTIC
316 if (ap->a_uio->uio_rw != UIO_WRITE)
317 panic("fifo_write mode");
318#endif
319 if (ap->a_ioflag & IO_NDELAY)
320 wso->so_state |= SS_NBIO;
321 VOP_UNLOCK(ap->a_vp, 0, td);
322 error = sosend(wso, (struct sockaddr *)0, ap->a_uio, 0,
323 (struct mbuf *)0, 0, td);
324 vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY, td);
325 if (ap->a_ioflag & IO_NDELAY)
326 wso->so_state &= ~SS_NBIO;
327 return (error);
328}
329
330/*
331 * Device ioctl operation.
332 */
333/* ARGSUSED */
334static int
335fifo_ioctl(ap)
336 struct vop_ioctl_args /* {
337 struct vnode *a_vp;
338 int a_command;
339 caddr_t a_data;
340 int a_fflag;
341 struct ucred *a_cred;
342 struct thread *a_td;
343 } */ *ap;
344{
345 struct file filetmp; /* Local, so need not be locked. */
346 int error;
347
348 if (ap->a_command == FIONBIO)
349 return (0);
350 if (ap->a_fflag & FREAD) {
351 filetmp.f_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_readsock;
352 error = soo_ioctl(&filetmp, ap->a_command, ap->a_data, ap->a_td);
353 if (error)
354 return (error);
355 }
356 if (ap->a_fflag & FWRITE) {
357 filetmp.f_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_writesock;
358 error = soo_ioctl(&filetmp, ap->a_command, ap->a_data, ap->a_td);
359 if (error)
360 return (error);
361 }
362 return (0);
363}
364
365/* ARGSUSED */
366static int
367fifo_kqfilter(ap)
368 struct vop_kqfilter_args /* {
369 struct vnode *a_vp;
370 struct knote *a_kn;
371 } */ *ap;
372{
373 struct fifoinfo *fi = ap->a_vp->v_fifoinfo;
374 struct socket *so;
375 struct sockbuf *sb;
376
377 switch (ap->a_kn->kn_filter) {
378 case EVFILT_READ:
379 ap->a_kn->kn_fop = &fiforead_filtops;
380 so = fi->fi_readsock;
381 sb = &so->so_rcv;
382 break;
383 case EVFILT_WRITE:
384 ap->a_kn->kn_fop = &fifowrite_filtops;
385 so = fi->fi_writesock;
386 sb = &so->so_snd;
387 break;
388 default:
389 return (1);
390 }
391
392 ap->a_kn->kn_hook = (caddr_t)so;
393
394 SLIST_INSERT_HEAD(&sb->sb_sel.si_note, ap->a_kn, kn_selnext);
395 sb->sb_flags |= SB_KNOTE;
396
397 return (0);
398}
399
400static void
401filt_fifordetach(struct knote *kn)
402{
403 struct socket *so = (struct socket *)kn->kn_hook;
404
405 SLIST_REMOVE(&so->so_rcv.sb_sel.si_note, kn, knote, kn_selnext);
406 if (SLIST_EMPTY(&so->so_rcv.sb_sel.si_note))
407 so->so_rcv.sb_flags &= ~SB_KNOTE;
408}
409
410static int
411filt_fiforead(struct knote *kn, long hint)
412{
413 struct socket *so = (struct socket *)kn->kn_hook;
414
415 kn->kn_data = so->so_rcv.sb_cc;
416 if (so->so_state & SS_CANTRCVMORE) {
417 kn->kn_flags |= EV_EOF;
418 return (1);
419 }
420 kn->kn_flags &= ~EV_EOF;
421 return (kn->kn_data > 0);
422}
423
424static void
425filt_fifowdetach(struct knote *kn)
426{
427 struct socket *so = (struct socket *)kn->kn_hook;
428
429 SLIST_REMOVE(&so->so_snd.sb_sel.si_note, kn, knote, kn_selnext);
430 if (SLIST_EMPTY(&so->so_snd.sb_sel.si_note))
431 so->so_snd.sb_flags &= ~SB_KNOTE;
432}
433
434static int
435filt_fifowrite(struct knote *kn, long hint)
436{
437 struct socket *so = (struct socket *)kn->kn_hook;
438
439 kn->kn_data = sbspace(&so->so_snd);
440 if (so->so_state & SS_CANTSENDMORE) {
441 kn->kn_flags |= EV_EOF;
442 return (1);
443 }
444 kn->kn_flags &= ~EV_EOF;
445 return (kn->kn_data >= so->so_snd.sb_lowat);
446}
447
448/* ARGSUSED */
449static int
450fifo_poll(ap)
451 struct vop_poll_args /* {
452 struct vnode *a_vp;
453 int a_events;
454 struct ucred *a_cred;
455 struct thread *a_td;
456 } */ *ap;
457{
458 struct file filetmp;
459 int events, revents = 0;
460
461 events = ap->a_events &
462 (POLLIN | POLLINIGNEOF | POLLPRI | POLLRDNORM | POLLRDBAND);
463 if (events) {
464 /*
465 * If POLLIN or POLLRDNORM is requested and POLLINIGNEOF is
466 * not, then convert the first two to the last one. This
467 * tells the socket poll function to ignore EOF so that we
468 * block if there is no writer (and no data). Callers can
469 * set POLLINIGNEOF to get non-blocking behavior.
470 */
471 if (events & (POLLIN | POLLRDNORM) &&
472 !(events & POLLINIGNEOF)) {
473 events &= ~(POLLIN | POLLRDNORM);
474 events |= POLLINIGNEOF;
475 }
476
477 filetmp.f_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_readsock;
478 if (filetmp.f_data)
479 revents |= soo_poll(&filetmp, events, ap->a_cred,
480 ap->a_td);
481
482 /* Reverse the above conversion. */
483 if ((revents & POLLINIGNEOF) &&
484 !(ap->a_events & POLLINIGNEOF)) {
485 revents |= (ap->a_events & (POLLIN | POLLRDNORM));
486 revents &= ~POLLINIGNEOF;
487 }
488 }
489 events = ap->a_events & (POLLOUT | POLLWRNORM | POLLWRBAND);
490 if (events) {
491 filetmp.f_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_writesock;
492 if (filetmp.f_data)
493 revents |= soo_poll(&filetmp, events, ap->a_cred,
494 ap->a_td);
495 }
496 return (revents);
497}
498
499/*
500 * Device close routine
501 */
502/* ARGSUSED */
503static int
504fifo_close(ap)
505 struct vop_close_args /* {
506 struct vnode *a_vp;
507 int a_fflag;
508 struct ucred *a_cred;
509 struct thread *a_td;
510 } */ *ap;
511{
512 register struct vnode *vp = ap->a_vp;
513 register struct fifoinfo *fip = vp->v_fifoinfo;
514 int error1, error2;
515
516 if (ap->a_fflag & FREAD) {
517 fip->fi_readers--;
518 if (fip->fi_readers == 0)
519 socantsendmore(fip->fi_writesock);
520 }
521 if (ap->a_fflag & FWRITE) {
522 fip->fi_writers--;
523 if (fip->fi_writers == 0)
524 socantrcvmore(fip->fi_readsock);
525 }
526 if (vp->v_usecount > 1)
527 return (0);
528 error1 = soclose(fip->fi_readsock);
529 error2 = soclose(fip->fi_writesock);
530 FREE(fip, M_VNODE);
531 vp->v_fifoinfo = NULL;
532 if (error1)
533 return (error1);
534 return (error2);
535}
536
537
538/*
539 * Print out internal contents of a fifo vnode.
540 */
541int
542fifo_printinfo(vp)
543 struct vnode *vp;
544{
545 register struct fifoinfo *fip = vp->v_fifoinfo;
546
547 printf(", fifo with %ld readers and %ld writers",
548 fip->fi_readers, fip->fi_writers);
549 return (0);
550}
551
552/*
553 * Print out the contents of a fifo vnode.
554 */
555static int
556fifo_print(ap)
557 struct vop_print_args /* {
558 struct vnode *a_vp;
559 } */ *ap;
560{
561
562 printf("tag VT_NON");
563 fifo_printinfo(ap->a_vp);
564 printf("\n");
565 return (0);
566}
567
568/*
569 * Return POSIX pathconf information applicable to fifo's.
570 */
571int
572fifo_pathconf(ap)
573 struct vop_pathconf_args /* {
574 struct vnode *a_vp;
575 int a_name;
576 int *a_retval;
577 } */ *ap;
578{
579
580 switch (ap->a_name) {
581 case _PC_LINK_MAX:
582 *ap->a_retval = LINK_MAX;
583 return (0);
584 case _PC_PIPE_BUF:
585 *ap->a_retval = PIPE_BUF;
586 return (0);
587 case _PC_CHOWN_RESTRICTED:
588 *ap->a_retval = 1;
589 return (0);
590 default:
591 return (EINVAL);
592 }
593 /* NOTREACHED */
594}
595
596/*
597 * Fifo advisory byte-level locks.
598 */
599/* ARGSUSED */
600static int
601fifo_advlock(ap)
602 struct vop_advlock_args /* {
603 struct vnode *a_vp;
604 caddr_t a_id;
605 int a_op;
606 struct flock *a_fl;
607 int a_flags;
608 } */ *ap;
609{
610
611 return (ap->a_flags & F_FLOCK ? EOPNOTSUPP : EINVAL);
612}
613
614/*
615 * Fifo bad operation
616 */
617static int
618fifo_badop()
619{
620
621 panic("fifo_badop called");
622 /* NOTREACHED */
623}