1 /* $NetBSD: fifo_vnops.c,v 1.39 2003/03/17 00:06:24 martin Exp $ */ 2 3 /* 4 * Copyright (c) 1990, 1993, 1995 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 * 35 * @(#)fifo_vnops.c 8.10 (Berkeley) 5/27/95 36 */ 37 38 #include <sys/cdefs.h> 39 __KERNEL_RCSID(0, "$NetBSD: fifo_vnops.c,v 1.39 2003/03/17 00:06:24 martin Exp $"); 40 41 #include <sys/param.h> 42 #include <sys/systm.h> 43 #include <sys/proc.h> 44 #include <sys/time.h> 45 #include <sys/namei.h> 46 #include <sys/vnode.h> 47 #include <sys/socket.h> 48 #include <sys/socketvar.h> 49 #include <sys/stat.h> 50 #include <sys/ioctl.h> 51 #include <sys/file.h> 52 #include <sys/errno.h> 53 #include <sys/malloc.h> 54 #include <sys/un.h> 55 #include <sys/poll.h> 56 #include <sys/event.h> 57 58 #include <miscfs/fifofs/fifo.h> 59 #include <miscfs/genfs/genfs.h> 60 61 /* 62 * This structure is associated with the FIFO vnode and stores 63 * the state associated with the FIFO. 64 */ 65 struct fifoinfo { 66 struct socket *fi_readsock; 67 struct socket *fi_writesock; 68 long fi_opencount; 69 long fi_readers; 70 long fi_writers; 71 }; 72 73 int (**fifo_vnodeop_p) __P((void *)); 74 const struct vnodeopv_entry_desc fifo_vnodeop_entries[] = { 75 { &vop_default_desc, vn_default_error }, 76 { &vop_lookup_desc, fifo_lookup }, /* lookup */ 77 { &vop_create_desc, fifo_create }, /* create */ 78 { &vop_mknod_desc, fifo_mknod }, /* mknod */ 79 { &vop_open_desc, fifo_open }, /* open */ 80 { &vop_close_desc, fifo_close }, /* close */ 81 { &vop_access_desc, fifo_access }, /* access */ 82 { &vop_getattr_desc, fifo_getattr }, /* getattr */ 83 { &vop_setattr_desc, fifo_setattr }, /* setattr */ 84 { &vop_read_desc, fifo_read }, /* read */ 85 { &vop_write_desc, fifo_write }, /* write */ 86 { &vop_lease_desc, fifo_lease_check }, /* lease */ 87 { &vop_ioctl_desc, fifo_ioctl }, /* ioctl */ 88 { &vop_poll_desc, fifo_poll }, /* poll */ 89 { &vop_kqfilter_desc, fifo_kqfilter }, /* kqfilter */ 90 { &vop_revoke_desc, fifo_revoke }, /* revoke */ 91 { &vop_mmap_desc, fifo_mmap }, /* mmap */ 92 { &vop_fsync_desc, fifo_fsync }, /* fsync */ 93 { &vop_seek_desc, fifo_seek }, /* seek */ 94 { &vop_remove_desc, fifo_remove }, /* remove */ 95 { &vop_link_desc, fifo_link }, /* link */ 96 { &vop_rename_desc, fifo_rename }, /* rename */ 97 { &vop_mkdir_desc, fifo_mkdir }, /* mkdir */ 98 { &vop_rmdir_desc, fifo_rmdir }, /* rmdir */ 99 { &vop_symlink_desc, fifo_symlink }, /* symlink */ 100 { &vop_readdir_desc, fifo_readdir }, /* readdir */ 101 { &vop_readlink_desc, fifo_readlink }, /* readlink */ 102 { &vop_abortop_desc, fifo_abortop }, /* abortop */ 103 { &vop_inactive_desc, fifo_inactive }, /* inactive */ 104 { &vop_reclaim_desc, fifo_reclaim }, /* reclaim */ 105 { &vop_lock_desc, fifo_lock }, /* lock */ 106 { &vop_unlock_desc, fifo_unlock }, /* unlock */ 107 { &vop_bmap_desc, fifo_bmap }, /* bmap */ 108 { &vop_strategy_desc, fifo_strategy }, /* strategy */ 109 { &vop_print_desc, fifo_print }, /* print */ 110 { &vop_islocked_desc, fifo_islocked }, /* islocked */ 111 { &vop_pathconf_desc, fifo_pathconf }, /* pathconf */ 112 { &vop_advlock_desc, fifo_advlock }, /* advlock */ 113 { &vop_blkatoff_desc, fifo_blkatoff }, /* blkatoff */ 114 { &vop_valloc_desc, fifo_valloc }, /* valloc */ 115 { &vop_vfree_desc, fifo_vfree }, /* vfree */ 116 { &vop_truncate_desc, fifo_truncate }, /* truncate */ 117 { &vop_update_desc, fifo_update }, /* update */ 118 { &vop_bwrite_desc, fifo_bwrite }, /* bwrite */ 119 { &vop_putpages_desc, fifo_putpages }, /* putpages */ 120 { (struct vnodeop_desc*)NULL, (int(*) __P((void *)))NULL } 121 }; 122 const struct vnodeopv_desc fifo_vnodeop_opv_desc = 123 { &fifo_vnodeop_p, fifo_vnodeop_entries }; 124 125 /* 126 * Trivial lookup routine that always fails. 127 */ 128 /* ARGSUSED */ 129 int 130 fifo_lookup(void *v) 131 { 132 struct vop_lookup_args /* { 133 struct vnode *a_dvp; 134 struct vnode **a_vpp; 135 struct componentname *a_cnp; 136 } */ *ap = v; 137 138 *ap->a_vpp = NULL; 139 return (ENOTDIR); 140 } 141 142 /* 143 * Open called to set up a new instance of a fifo or 144 * to find an active instance of a fifo. 145 */ 146 /* ARGSUSED */ 147 int 148 fifo_open(void *v) 149 { 150 struct vop_open_args /* { 151 struct vnode *a_vp; 152 int a_mode; 153 struct ucred *a_cred; 154 struct proc *a_p; 155 } */ *ap = v; 156 struct vnode *vp; 157 struct fifoinfo *fip; 158 struct proc *p; 159 struct socket *rso, *wso; 160 int error; 161 162 vp = ap->a_vp; 163 p = ap->a_p; 164 165 if ((fip = vp->v_fifoinfo) == NULL) { 166 MALLOC(fip, struct fifoinfo *, sizeof(*fip), M_VNODE, M_WAITOK); 167 vp->v_fifoinfo = fip; 168 if ((error = socreate(AF_LOCAL, &rso, SOCK_STREAM, 0)) != 0) { 169 free(fip, M_VNODE); 170 vp->v_fifoinfo = NULL; 171 return (error); 172 } 173 fip->fi_readsock = rso; 174 if ((error = socreate(AF_LOCAL, &wso, SOCK_STREAM, 0)) != 0) { 175 (void)soclose(rso); 176 free(fip, M_VNODE); 177 vp->v_fifoinfo = NULL; 178 return (error); 179 } 180 fip->fi_writesock = wso; 181 if ((error = unp_connect2(wso, rso)) != 0) { 182 (void)soclose(wso); 183 (void)soclose(rso); 184 free(fip, M_VNODE); 185 vp->v_fifoinfo = NULL; 186 return (error); 187 } 188 fip->fi_readers = fip->fi_writers = 0; 189 fip->fi_opencount = 0; 190 wso->so_state |= SS_CANTRCVMORE; 191 rso->so_state |= SS_CANTSENDMORE; 192 } 193 fip->fi_opencount++; 194 if (ap->a_mode & FREAD) { 195 if (fip->fi_readers++ == 0) { 196 fip->fi_writesock->so_state &= ~SS_CANTSENDMORE; 197 if (fip->fi_writers > 0) 198 wakeup((caddr_t)&fip->fi_writers); 199 } 200 } 201 if (ap->a_mode & FWRITE) { 202 if (fip->fi_writers++ == 0) { 203 fip->fi_readsock->so_state &= ~SS_CANTRCVMORE; 204 if (fip->fi_readers > 0) 205 wakeup((caddr_t)&fip->fi_readers); 206 } 207 } 208 if (ap->a_mode & FREAD) { 209 if (ap->a_mode & O_NONBLOCK) { 210 } else { 211 while (!soreadable(fip->fi_readsock) && fip->fi_writers == 0) { 212 VOP_UNLOCK(vp, 0); 213 error = tsleep((caddr_t)&fip->fi_readers, 214 PCATCH | PSOCK, "fifor", 0); 215 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 216 if (error) 217 goto bad; 218 } 219 } 220 } 221 if (ap->a_mode & FWRITE) { 222 if (ap->a_mode & O_NONBLOCK) { 223 if (fip->fi_readers == 0) { 224 error = ENXIO; 225 goto bad; 226 } 227 } else { 228 while (fip->fi_readers == 0) { 229 VOP_UNLOCK(vp, 0); 230 error = tsleep((caddr_t)&fip->fi_writers, 231 PCATCH | PSOCK, "fifow", 0); 232 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 233 if (error) 234 goto bad; 235 } 236 } 237 } 238 return (0); 239 bad: 240 VOP_CLOSE(vp, ap->a_mode, ap->a_cred, p); 241 return (error); 242 } 243 244 /* 245 * Vnode op for read 246 */ 247 /* ARGSUSED */ 248 int 249 fifo_read(void *v) 250 { 251 struct vop_read_args /* { 252 struct vnode *a_vp; 253 struct uio *a_uio; 254 int a_ioflag; 255 struct ucred *a_cred; 256 } */ *ap = v; 257 struct uio *uio; 258 struct socket *rso; 259 int error; 260 size_t startresid; 261 262 uio = ap->a_uio; 263 rso = ap->a_vp->v_fifoinfo->fi_readsock; 264 #ifdef DIAGNOSTIC 265 if (uio->uio_rw != UIO_READ) 266 panic("fifo_read mode"); 267 #endif 268 if (uio->uio_resid == 0) 269 return (0); 270 if (ap->a_ioflag & IO_NDELAY) 271 rso->so_state |= SS_NBIO; 272 startresid = uio->uio_resid; 273 VOP_UNLOCK(ap->a_vp, 0); 274 error = (*rso->so_receive)(rso, (struct mbuf **)0, uio, 275 (struct mbuf **)0, (struct mbuf **)0, (int *)0); 276 vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY); 277 /* 278 * Clear EOF indication after first such return. 279 */ 280 if (uio->uio_resid == startresid) 281 rso->so_state &= ~SS_CANTRCVMORE; 282 if (ap->a_ioflag & IO_NDELAY) { 283 rso->so_state &= ~SS_NBIO; 284 if (error == EWOULDBLOCK && 285 ap->a_vp->v_fifoinfo->fi_writers == 0) 286 error = 0; 287 } 288 return (error); 289 } 290 291 /* 292 * Vnode op for write 293 */ 294 /* ARGSUSED */ 295 int 296 fifo_write(void *v) 297 { 298 struct vop_write_args /* { 299 struct vnode *a_vp; 300 struct uio *a_uio; 301 int a_ioflag; 302 struct ucred *a_cred; 303 } */ *ap = v; 304 struct socket *wso; 305 int error; 306 307 wso = ap->a_vp->v_fifoinfo->fi_writesock; 308 #ifdef DIAGNOSTIC 309 if (ap->a_uio->uio_rw != UIO_WRITE) 310 panic("fifo_write mode"); 311 #endif 312 if (ap->a_ioflag & IO_NDELAY) 313 wso->so_state |= SS_NBIO; 314 VOP_UNLOCK(ap->a_vp, 0); 315 error = (*wso->so_send)(wso, (struct mbuf *)0, ap->a_uio, 0, 316 (struct mbuf *)0, 0); 317 vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY); 318 if (ap->a_ioflag & IO_NDELAY) 319 wso->so_state &= ~SS_NBIO; 320 return (error); 321 } 322 323 /* 324 * Device ioctl operation. 325 */ 326 /* ARGSUSED */ 327 int 328 fifo_ioctl(void *v) 329 { 330 struct vop_ioctl_args /* { 331 struct vnode *a_vp; 332 u_long a_command; 333 caddr_t a_data; 334 int a_fflag; 335 struct ucred *a_cred; 336 struct proc *a_p; 337 } */ *ap = v; 338 struct file filetmp; 339 int error; 340 341 if (ap->a_command == FIONBIO) 342 return (0); 343 if (ap->a_fflag & FREAD) { 344 filetmp.f_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_readsock; 345 error = soo_ioctl(&filetmp, ap->a_command, ap->a_data, ap->a_p); 346 if (error) 347 return (error); 348 } 349 if (ap->a_fflag & FWRITE) { 350 filetmp.f_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_writesock; 351 error = soo_ioctl(&filetmp, ap->a_command, ap->a_data, ap->a_p); 352 if (error) 353 return (error); 354 } 355 return (0); 356 } 357 358 /* ARGSUSED */ 359 int 360 fifo_poll(void *v) 361 { 362 struct vop_poll_args /* { 363 struct vnode *a_vp; 364 int a_events; 365 struct proc *a_p; 366 } */ *ap = v; 367 struct file filetmp; 368 int revents; 369 370 revents = 0; 371 if (ap->a_events & (POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND)) { 372 filetmp.f_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_readsock; 373 if (filetmp.f_data) 374 revents |= soo_poll(&filetmp, ap->a_events, ap->a_p); 375 } 376 if (ap->a_events & (POLLOUT | POLLWRNORM | POLLWRBAND)) { 377 filetmp.f_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_writesock; 378 if (filetmp.f_data) 379 revents |= soo_poll(&filetmp, ap->a_events, ap->a_p); 380 } 381 382 return (revents); 383 } 384 385 int 386 fifo_inactive(void *v) 387 { 388 struct vop_inactive_args /* { 389 struct vnode *a_vp; 390 struct proc *a_p; 391 } */ *ap = v; 392 393 VOP_UNLOCK(ap->a_vp, 0); 394 return (0); 395 } 396 397 /* 398 * This is a noop, simply returning what one has been given. 399 */ 400 int 401 fifo_bmap(void *v) 402 { 403 struct vop_bmap_args /* { 404 struct vnode *a_vp; 405 daddr_t a_bn; 406 struct vnode **a_vpp; 407 daddr_t *a_bnp; 408 int *a_runp; 409 } */ *ap = v; 410 411 if (ap->a_vpp != NULL) 412 *ap->a_vpp = ap->a_vp; 413 if (ap->a_bnp != NULL) 414 *ap->a_bnp = ap->a_bn; 415 if (ap->a_runp != NULL) 416 *ap->a_runp = 0; 417 return (0); 418 } 419 420 /* 421 * Device close routine 422 */ 423 /* ARGSUSED */ 424 int 425 fifo_close(void *v) 426 { 427 struct vop_close_args /* { 428 struct vnode *a_vp; 429 int a_fflag; 430 struct ucred *a_cred; 431 struct proc *a_p; 432 } */ *ap = v; 433 struct vnode *vp; 434 struct fifoinfo *fip; 435 436 vp = ap->a_vp; 437 fip = vp->v_fifoinfo; 438 if (ap->a_fflag & FREAD) { 439 if (--fip->fi_readers == 0) 440 socantsendmore(fip->fi_writesock); 441 } 442 if (ap->a_fflag & FWRITE) { 443 if (--fip->fi_writers == 0) 444 socantrcvmore(fip->fi_readsock); 445 } 446 if (--fip->fi_opencount == 0) { 447 (void) soclose(fip->fi_readsock); 448 (void) soclose(fip->fi_writesock); 449 FREE(fip, M_VNODE); 450 vp->v_fifoinfo = NULL; 451 } 452 return (0); 453 } 454 455 /* 456 * Print out the contents of a fifo vnode. 457 */ 458 int 459 fifo_print(void *v) 460 { 461 struct vop_print_args /* { 462 struct vnode *a_vp; 463 } */ *ap = v; 464 465 printf("tag VT_NON"); 466 fifo_printinfo(ap->a_vp); 467 printf("\n"); 468 return 0; 469 } 470 471 /* 472 * Print out internal contents of a fifo vnode. 473 */ 474 void 475 fifo_printinfo(struct vnode *vp) 476 { 477 struct fifoinfo *fip; 478 479 fip = vp->v_fifoinfo; 480 printf(", fifo with %ld readers and %ld writers", 481 fip->fi_readers, fip->fi_writers); 482 } 483 484 /* 485 * Return POSIX pathconf information applicable to fifo's. 486 */ 487 int 488 fifo_pathconf(void *v) 489 { 490 struct vop_pathconf_args /* { 491 struct vnode *a_vp; 492 int a_name; 493 register_t *a_retval; 494 } */ *ap = v; 495 496 switch (ap->a_name) { 497 case _PC_LINK_MAX: 498 *ap->a_retval = LINK_MAX; 499 return (0); 500 case _PC_PIPE_BUF: 501 *ap->a_retval = PIPE_BUF; 502 return (0); 503 case _PC_CHOWN_RESTRICTED: 504 *ap->a_retval = 1; 505 return (0); 506 case _PC_SYNC_IO: 507 *ap->a_retval = 1; 508 return (0); 509 default: 510 return (EINVAL); 511 } 512 /* NOTREACHED */ 513 } 514 515 static void 516 filt_fifordetach(struct knote *kn) 517 { 518 struct socket *so; 519 520 so = (struct socket *)kn->kn_hook; 521 SLIST_REMOVE(&so->so_rcv.sb_sel.sel_klist, kn, knote, kn_selnext); 522 if (SLIST_EMPTY(&so->so_rcv.sb_sel.sel_klist)) 523 so->so_rcv.sb_flags &= ~SB_KNOTE; 524 } 525 526 static int 527 filt_fiforead(struct knote *kn, long hint) 528 { 529 struct socket *so; 530 531 so = (struct socket *)kn->kn_hook; 532 kn->kn_data = so->so_rcv.sb_cc; 533 if (so->so_state & SS_CANTRCVMORE) { 534 kn->kn_flags |= EV_EOF; 535 return (1); 536 } 537 kn->kn_flags &= ~EV_EOF; 538 return (kn->kn_data > 0); 539 } 540 541 static void 542 filt_fifowdetach(struct knote *kn) 543 { 544 struct socket *so; 545 546 so = (struct socket *)kn->kn_hook; 547 SLIST_REMOVE(&so->so_snd.sb_sel.sel_klist, kn, knote, kn_selnext); 548 if (SLIST_EMPTY(&so->so_snd.sb_sel.sel_klist)) 549 so->so_snd.sb_flags &= ~SB_KNOTE; 550 } 551 552 static int 553 filt_fifowrite(struct knote *kn, long hint) 554 { 555 struct socket *so; 556 557 so = (struct socket *)kn->kn_hook; 558 kn->kn_data = sbspace(&so->so_snd); 559 if (so->so_state & SS_CANTSENDMORE) { 560 kn->kn_flags |= EV_EOF; 561 return (1); 562 } 563 kn->kn_flags &= ~EV_EOF; 564 return (kn->kn_data >= so->so_snd.sb_lowat); 565 } 566 567 static const struct filterops fiforead_filtops = 568 { 1, NULL, filt_fifordetach, filt_fiforead }; 569 static const struct filterops fifowrite_filtops = 570 { 1, NULL, filt_fifowdetach, filt_fifowrite }; 571 572 /* ARGSUSED */ 573 int 574 fifo_kqfilter(void *v) 575 { 576 struct vop_kqfilter_args /* { 577 struct vnode *a_vp; 578 struct knote *a_kn; 579 } */ *ap = v; 580 struct socket *so; 581 struct sockbuf *sb; 582 583 so = (struct socket *)ap->a_vp->v_fifoinfo->fi_readsock; 584 switch (ap->a_kn->kn_filter) { 585 case EVFILT_READ: 586 ap->a_kn->kn_fop = &fiforead_filtops; 587 sb = &so->so_rcv; 588 break; 589 case EVFILT_WRITE: 590 ap->a_kn->kn_fop = &fifowrite_filtops; 591 sb = &so->so_snd; 592 break; 593 default: 594 return (1); 595 } 596 597 ap->a_kn->kn_hook = so; 598 599 SLIST_INSERT_HEAD(&sb->sb_sel.sel_klist, ap->a_kn, kn_selnext); 600 sb->sb_flags |= SB_KNOTE; 601 return (0); 602 } 603