Deleted Added
full compact
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>
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>
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>
47#include <sys/proc.h> /* XXXKSE */
48#include <sys/signalvar.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>
54#include <sys/unistd.h>
55#include <sys/vnode.h>
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}