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$ 35 */ 36 37 #include <sys/param.h> 38 #include <sys/systm.h> 39 #include <sys/unistd.h> 40 #include <sys/kernel.h> 41 #include <sys/lock.h> 42 #include <sys/mutex.h> 43 #include <sys/malloc.h> 44 #include <sys/vnode.h> 45 #include <sys/proc.h> /* XXXKSE */ 46 #include <sys/socket.h> 47 #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> 53 #include <sys/un.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 */ 60 struct fifoinfo { 61 struct socket *fi_readsock; 62 struct socket *fi_writesock; 63 long fi_readers; 64 long fi_writers; 65 }; 66 67 static int fifo_badop(void); 68 static int fifo_print(struct vop_print_args *); 69 static int fifo_lookup(struct vop_lookup_args *); 70 static int fifo_open(struct vop_open_args *); 71 static int fifo_close(struct vop_close_args *); 72 static int fifo_read(struct vop_read_args *); 73 static int fifo_write(struct vop_write_args *); 74 static int fifo_ioctl(struct vop_ioctl_args *); 75 static int fifo_poll(struct vop_poll_args *); 76 static int fifo_kqfilter(struct vop_kqfilter_args *); 77 static int fifo_pathconf(struct vop_pathconf_args *); 78 static int fifo_advlock(struct vop_advlock_args *); 79 80 static void filt_fifordetach(struct knote *kn); 81 static int filt_fiforead(struct knote *kn, long hint); 82 static void filt_fifowdetach(struct knote *kn); 83 static int filt_fifowrite(struct knote *kn, long hint); 84 85 static struct filterops fiforead_filtops = 86 { 1, NULL, filt_fifordetach, filt_fiforead }; 87 static struct filterops fifowrite_filtops = 88 { 1, NULL, filt_fifowdetach, filt_fifowrite }; 89 90 vop_t **fifo_vnodeop_p; 91 static 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 }; 123 static struct vnodeopv_desc fifo_vnodeop_opv_desc = 124 { &fifo_vnodeop_p, fifo_vnodeop_entries }; 125 126 VNODEOP_SET(fifo_vnodeop_opv_desc); 127 128 int 129 fifo_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 */ 142 static int 143 fifo_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 */ 160 static int 161 fifo_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); 255 bad: 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 */ 264 static int 265 fifo_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 */ 300 static int 301 fifo_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 */ 332 static int 333 fifo_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; 344 int error = 0; 345 346 if (ap->a_command == FIONBIO) 347 return (0); 348 if (ap->a_fflag & FREAD) { 349 /* filetmp is local, hence not need be locked. */ 350 filetmp.f_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_readsock; 351 error = soo_ioctl(&filetmp, ap->a_command, ap->a_data, ap->a_td); 352 if (error) 353 goto err; 354 } 355 if (ap->a_fflag & FWRITE) { 356 /* filetmp is local, hence not need be locked. */ 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 goto err; 361 } 362 err: 363 return (error); 364 } 365 366 /* ARGSUSED */ 367 static int 368 fifo_kqfilter(ap) 369 struct vop_kqfilter_args /* { 370 struct vnode *a_vp; 371 struct knote *a_kn; 372 } */ *ap; 373 { 374 struct fifoinfo *fi = ap->a_vp->v_fifoinfo; 375 struct socket *so; 376 struct sockbuf *sb; 377 378 switch (ap->a_kn->kn_filter) { 379 case EVFILT_READ: 380 ap->a_kn->kn_fop = &fiforead_filtops; 381 so = fi->fi_readsock; 382 sb = &so->so_rcv; 383 break; 384 case EVFILT_WRITE: 385 ap->a_kn->kn_fop = &fifowrite_filtops; 386 so = fi->fi_writesock; 387 sb = &so->so_snd; 388 break; 389 default: 390 return (1); 391 } 392 393 ap->a_kn->kn_hook = (caddr_t)so; 394 395 SLIST_INSERT_HEAD(&sb->sb_sel.si_note, ap->a_kn, kn_selnext); 396 sb->sb_flags |= SB_KNOTE; 397 398 return (0); 399 } 400 401 static void 402 filt_fifordetach(struct knote *kn) 403 { 404 struct socket *so = (struct socket *)kn->kn_hook; 405 406 SLIST_REMOVE(&so->so_rcv.sb_sel.si_note, kn, knote, kn_selnext); 407 if (SLIST_EMPTY(&so->so_rcv.sb_sel.si_note)) 408 so->so_rcv.sb_flags &= ~SB_KNOTE; 409 } 410 411 static int 412 filt_fiforead(struct knote *kn, long hint) 413 { 414 struct socket *so = (struct socket *)kn->kn_hook; 415 416 kn->kn_data = so->so_rcv.sb_cc; 417 if (so->so_state & SS_CANTRCVMORE) { 418 kn->kn_flags |= EV_EOF; 419 return (1); 420 } 421 kn->kn_flags &= ~EV_EOF; 422 return (kn->kn_data > 0); 423 } 424 425 static void 426 filt_fifowdetach(struct knote *kn) 427 { 428 struct socket *so = (struct socket *)kn->kn_hook; 429 430 SLIST_REMOVE(&so->so_snd.sb_sel.si_note, kn, knote, kn_selnext); 431 if (SLIST_EMPTY(&so->so_snd.sb_sel.si_note)) 432 so->so_snd.sb_flags &= ~SB_KNOTE; 433 } 434 435 static int 436 filt_fifowrite(struct knote *kn, long hint) 437 { 438 struct socket *so = (struct socket *)kn->kn_hook; 439 440 kn->kn_data = sbspace(&so->so_snd); 441 if (so->so_state & SS_CANTSENDMORE) { 442 kn->kn_flags |= EV_EOF; 443 return (1); 444 } 445 kn->kn_flags &= ~EV_EOF; 446 return (kn->kn_data >= so->so_snd.sb_lowat); 447 } 448 449 /* ARGSUSED */ 450 static int 451 fifo_poll(ap) 452 struct vop_poll_args /* { 453 struct vnode *a_vp; 454 int a_events; 455 struct ucred *a_cred; 456 struct thread *a_td; 457 } */ *ap; 458 { 459 struct file filetmp; 460 int events, revents = 0; 461 462 events = ap->a_events & 463 (POLLIN | POLLINIGNEOF | POLLPRI | POLLRDNORM | POLLRDBAND); 464 if (events) { 465 /* 466 * Tell socket poll to ignore EOF so that we block if 467 * there is no writer (and no data). 468 */ 469 if (events & (POLLIN | POLLRDNORM)) { 470 events &= ~(POLLIN | POLLRDNORM); 471 events |= POLLINIGNEOF; 472 } 473 filetmp.f_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_readsock; 474 if (filetmp.f_data) 475 revents |= soo_poll(&filetmp, events, ap->a_cred, 476 ap->a_td); 477 /* 478 * If POLLIN or POLLRDNORM was requested and POLLINIGNEOF was 479 * not then convert POLLINIGNEOF back to POLLIN. 480 */ 481 events = ap->a_events & (POLLIN | POLLRDNORM | POLLINIGNEOF); 482 if ((events & (POLLIN | POLLRDNORM)) && 483 !(events & POLLINIGNEOF) && 484 (revents & POLLINIGNEOF)) { 485 revents &= ~POLLINIGNEOF; 486 revents |= (events & (POLLIN | POLLRDNORM)); 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 */ 503 static int 504 fifo_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 */ 541 int 542 fifo_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 */ 555 static int 556 fifo_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 */ 571 int 572 fifo_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 */ 600 static int 601 fifo_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 */ 617 static int 618 fifo_badop() 619 { 620 621 panic("fifo_badop called"); 622 /* NOTREACHED */ 623 } 624