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