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. 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 /* 30 * Filesystem FIFO type ops. All entry points are MPSAFE. We primarily 31 * use v_token to interlock operations. 32 */ 33 #include <sys/param.h> 34 #include <sys/systm.h> 35 #include <sys/unistd.h> 36 #include <sys/kernel.h> 37 #include <sys/lock.h> 38 #include <sys/malloc.h> 39 #include <sys/vnode.h> 40 #include <sys/socket.h> 41 #include <sys/socketvar.h> 42 #include <sys/filio.h> 43 #include <sys/fcntl.h> 44 #include <sys/file.h> 45 #include <sys/event.h> 46 #include <sys/un.h> 47 48 #include <sys/socketvar2.h> 49 50 #include "fifo.h" 51 52 /* 53 * This structure is associated with the FIFO vnode and stores 54 * the state associated with the FIFO. 55 */ 56 struct fifoinfo { 57 struct socket *fi_readsock; 58 struct socket *fi_writesock; 59 long fi_readers; 60 long fi_writers; 61 }; 62 63 static int fifo_badop (void); 64 static int fifo_print (struct vop_print_args *); 65 static int fifo_lookup (struct vop_old_lookup_args *); 66 static int fifo_open (struct vop_open_args *); 67 static int fifo_close (struct vop_close_args *); 68 static int fifo_read (struct vop_read_args *); 69 static int fifo_write (struct vop_write_args *); 70 static int fifo_ioctl (struct vop_ioctl_args *); 71 static int fifo_kqfilter (struct vop_kqfilter_args *); 72 static int fifo_inactive (struct vop_inactive_args *); 73 static int fifo_bmap (struct vop_bmap_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 { FILTEROP_ISFD, NULL, filt_fifordetach, filt_fiforead }; 84 static struct filterops fifowrite_filtops = 85 { FILTEROP_ISFD, NULL, filt_fifowdetach, filt_fifowrite }; 86 87 struct vop_ops fifo_vnode_vops = { 88 .vop_default = vop_defaultop, 89 .vop_access = (void *)vop_ebadf, 90 .vop_advlock = fifo_advlock, 91 .vop_bmap = fifo_bmap, 92 .vop_close = fifo_close, 93 .vop_old_create = (void *)fifo_badop, 94 .vop_getattr = (void *)vop_ebadf, 95 .vop_inactive = fifo_inactive, 96 .vop_ioctl = fifo_ioctl, 97 .vop_kqfilter = fifo_kqfilter, 98 .vop_old_link = (void *)fifo_badop, 99 .vop_old_lookup = fifo_lookup, 100 .vop_old_mkdir = (void *)fifo_badop, 101 .vop_old_mknod = (void *)fifo_badop, 102 .vop_open = fifo_open, 103 .vop_pathconf = fifo_pathconf, 104 .vop_print = fifo_print, 105 .vop_read = fifo_read, 106 .vop_readdir = (void *)fifo_badop, 107 .vop_readlink = (void *)fifo_badop, 108 .vop_reallocblks = (void *)fifo_badop, 109 .vop_reclaim = (void *)vop_null, 110 .vop_old_remove = (void *)fifo_badop, 111 .vop_old_rename = (void *)fifo_badop, 112 .vop_old_rmdir = (void *)fifo_badop, 113 .vop_setattr = (void *)vop_ebadf, 114 .vop_old_symlink = (void *)fifo_badop, 115 .vop_write = fifo_write 116 }; 117 118 VNODEOP_SET(fifo_vnode_vops); 119 120 static MALLOC_DEFINE(M_FIFOINFO, "Fifo info", "Fifo info entries"); 121 122 /* 123 * fifo_vnoperate() 124 */ 125 int 126 fifo_vnoperate(struct vop_generic_args *ap) 127 { 128 return (VOCALL(&fifo_vnode_vops, ap)); 129 } 130 131 /* 132 * Trivial lookup routine that always fails. 133 * 134 * fifo_lookup(struct vnode *a_dvp, struct vnode **a_vpp, 135 * struct componentname *a_cnp) 136 */ 137 /* ARGSUSED */ 138 static int 139 fifo_lookup(struct vop_old_lookup_args *ap) 140 { 141 *ap->a_vpp = NULL; 142 return (ENOTDIR); 143 } 144 145 /* 146 * Open called to set up a new instance of a fifo or 147 * to find an active instance of a fifo. 148 * 149 * fifo_open(struct vnode *a_vp, int a_mode, struct ucred *a_cred, 150 * struct file *a_fp) 151 */ 152 /* ARGSUSED */ 153 static int 154 fifo_open(struct vop_open_args *ap) 155 { 156 struct thread *td = curthread; 157 struct vnode *vp = ap->a_vp; 158 struct fifoinfo *fip; 159 struct socket *rso, *wso; 160 int error; 161 162 lwkt_gettoken(&vp->v_token); 163 if ((fip = vp->v_fifoinfo) == NULL) { 164 fip = kmalloc(sizeof(*fip), M_FIFOINFO, M_WAITOK); 165 vp->v_fifoinfo = fip; 166 error = socreate(AF_LOCAL, &rso, SOCK_STREAM, 0, td); 167 if (error) { 168 kfree(fip, M_FIFOINFO); 169 vp->v_fifoinfo = NULL; 170 goto done; 171 } 172 fip->fi_readsock = rso; 173 error = socreate(AF_LOCAL, &wso, SOCK_STREAM, 0, td); 174 if (error) { 175 soclose(rso, FNONBLOCK); 176 kfree(fip, M_FIFOINFO); 177 vp->v_fifoinfo = NULL; 178 goto done; 179 } 180 fip->fi_writesock = wso; 181 error = unp_connect2(wso, rso); 182 if (error) { 183 soclose(wso, FNONBLOCK); 184 soclose(rso, FNONBLOCK); 185 kfree(fip, M_FIFOINFO); 186 vp->v_fifoinfo = NULL; 187 goto done; 188 } 189 fip->fi_readers = fip->fi_writers = 0; 190 wso->so_snd.ssb_lowat = PIPE_BUF; 191 sosetstate(rso, SS_CANTRCVMORE); 192 } 193 if (ap->a_mode & FREAD) { 194 fip->fi_readers++; 195 if (fip->fi_readers == 1) { 196 soisreconnected(fip->fi_writesock); 197 if (fip->fi_writers > 0) { 198 wakeup((caddr_t)&fip->fi_writers); 199 sowwakeup(fip->fi_writesock); 200 } 201 } 202 } 203 if (ap->a_mode & FWRITE) { 204 fip->fi_writers++; 205 if (fip->fi_writers == 1) { 206 soisreconnected(fip->fi_readsock); 207 if (fip->fi_readers > 0) { 208 wakeup((caddr_t)&fip->fi_readers); 209 sorwakeup(fip->fi_writesock); 210 } 211 } 212 } 213 if ((ap->a_mode & FREAD) && (ap->a_mode & O_NONBLOCK) == 0) { 214 if (fip->fi_writers == 0) { 215 vn_unlock(vp); 216 error = tsleep((caddr_t)&fip->fi_readers, 217 PCATCH, "fifoor", 0); 218 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 219 if (error) 220 goto bad; 221 /* 222 * We must have got woken up because we had a writer. 223 * That (and not still having one) is the condition 224 * that we must wait for. 225 */ 226 } 227 } 228 if (ap->a_mode & FWRITE) { 229 if (ap->a_mode & O_NONBLOCK) { 230 if (fip->fi_readers == 0) { 231 error = ENXIO; 232 goto bad; 233 } 234 } else { 235 if (fip->fi_readers == 0) { 236 vn_unlock(vp); 237 error = tsleep((caddr_t)&fip->fi_writers, 238 PCATCH, "fifoow", 0); 239 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 240 if (error) 241 goto bad; 242 /* 243 * We must have got woken up because we had 244 * a reader. That (and not still having one) 245 * is the condition that we must wait for. 246 */ 247 } 248 } 249 } 250 vsetflags(vp, VNOTSEEKABLE); 251 error = vop_stdopen(ap); 252 lwkt_reltoken(&vp->v_token); 253 return (error); 254 bad: 255 vop_stdopen(ap); /* bump opencount/writecount as appropriate */ 256 VOP_CLOSE(vp, ap->a_mode, NULL); 257 done: 258 lwkt_reltoken(&vp->v_token); 259 return (error); 260 } 261 262 /* 263 * Vnode op for read 264 * 265 * fifo_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag, 266 * struct ucred *a_cred) 267 */ 268 /* ARGSUSED */ 269 static int 270 fifo_read(struct vop_read_args *ap) 271 { 272 struct uio *uio = ap->a_uio; 273 struct vnode *vp = ap->a_vp; 274 struct socket *rso = vp->v_fifoinfo->fi_readsock; 275 int error; 276 int flags; 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 flags = MSG_FNONBLOCKING; 286 else 287 flags = 0; 288 vn_unlock(vp); 289 lwkt_gettoken(&vp->v_token); 290 error = soreceive(rso, NULL, uio, NULL, NULL, &flags); 291 lwkt_reltoken(&vp->v_token); 292 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 293 return (error); 294 } 295 296 /* 297 * Vnode op for write 298 * 299 * fifo_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag, 300 * struct ucred *a_cred) 301 */ 302 /* ARGSUSED */ 303 static int 304 fifo_write(struct vop_write_args *ap) 305 { 306 struct thread *td = ap->a_uio->uio_td; 307 struct vnode *vp = ap->a_vp; 308 struct socket *wso = vp->v_fifoinfo->fi_writesock; 309 int error; 310 int flags; 311 312 #ifdef DIAGNOSTIC 313 if (ap->a_uio->uio_rw != UIO_WRITE) 314 panic("fifo_write mode"); 315 #endif 316 if (ap->a_ioflag & IO_NDELAY) 317 flags = MSG_FNONBLOCKING; 318 else 319 flags = 0; 320 vn_unlock(vp); 321 lwkt_gettoken(&vp->v_token); 322 error = sosend(wso, NULL, ap->a_uio, 0, NULL, flags, td); 323 lwkt_reltoken(&vp->v_token); 324 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 325 return (error); 326 } 327 328 /* 329 * Device ioctl operation. 330 * 331 * fifo_ioctl(struct vnode *a_vp, int a_command, caddr_t a_data, int a_fflag, 332 * struct ucred *a_cred, struct sysmsg *a_sysmsg) 333 */ 334 /* ARGSUSED */ 335 static int 336 fifo_ioctl(struct vop_ioctl_args *ap) 337 { 338 struct file filetmp; /* Local */ 339 struct vnode *vp = ap->a_vp; 340 int error; 341 342 if (ap->a_fflag & FREAD) { 343 filetmp.f_data = vp->v_fifoinfo->fi_readsock; 344 lwkt_gettoken(&vp->v_token); 345 error = soo_ioctl(&filetmp, ap->a_command, ap->a_data, 346 ap->a_cred, ap->a_sysmsg); 347 lwkt_reltoken(&vp->v_token); 348 if (error) 349 return (error); 350 } 351 if (ap->a_fflag & FWRITE) { 352 filetmp.f_data = vp->v_fifoinfo->fi_writesock; 353 lwkt_gettoken(&vp->v_token); 354 error = soo_ioctl(&filetmp, ap->a_command, ap->a_data, 355 ap->a_cred, ap->a_sysmsg); 356 lwkt_reltoken(&vp->v_token); 357 if (error) 358 return (error); 359 } 360 return (0); 361 } 362 363 /* 364 * fifo_kqfilter(struct vnode *a_vp, struct knote *a_kn) 365 */ 366 /* ARGSUSED */ 367 static int 368 fifo_kqfilter(struct vop_kqfilter_args *ap) 369 { 370 struct vnode *vp = ap->a_vp; 371 struct fifoinfo *fi = vp->v_fifoinfo; 372 struct socket *so; 373 struct signalsockbuf *ssb; 374 375 lwkt_gettoken(&vp->v_token); 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 ssb = &so->so_rcv; 382 break; 383 case EVFILT_WRITE: 384 ap->a_kn->kn_fop = &fifowrite_filtops; 385 so = fi->fi_writesock; 386 ssb = &so->so_snd; 387 break; 388 default: 389 lwkt_reltoken(&vp->v_token); 390 return (EOPNOTSUPP); 391 } 392 393 ap->a_kn->kn_hook = (caddr_t)vp; 394 ssb_insert_knote(ssb, ap->a_kn); 395 396 lwkt_reltoken(&vp->v_token); 397 return (0); 398 } 399 400 static void 401 filt_fifordetach(struct knote *kn) 402 { 403 struct vnode *vp = (void *)kn->kn_hook; 404 struct socket *so = vp->v_fifoinfo->fi_readsock; 405 406 lwkt_gettoken(&vp->v_token); 407 ssb_remove_knote(&so->so_rcv, kn); 408 lwkt_reltoken(&vp->v_token); 409 } 410 411 static int 412 filt_fiforead(struct knote *kn, long hint) 413 { 414 struct vnode *vp = (void *)kn->kn_hook; 415 struct socket *so = vp->v_fifoinfo->fi_readsock; 416 417 lwkt_gettoken(&vp->v_token); 418 kn->kn_data = so->so_rcv.ssb_cc; 419 if ((kn->kn_sfflags & NOTE_OLDAPI) == 0 && 420 so->so_state & SS_ISDISCONNECTED) { 421 if (kn->kn_data == 0) 422 kn->kn_flags |= EV_NODATA; 423 kn->kn_flags |= EV_EOF; 424 lwkt_reltoken(&vp->v_token); 425 return (1); 426 } 427 kn->kn_flags &= ~(EV_EOF | EV_NODATA); 428 lwkt_reltoken(&vp->v_token); 429 return (kn->kn_data > 0); 430 } 431 432 static void 433 filt_fifowdetach(struct knote *kn) 434 { 435 struct vnode *vp = (void *)kn->kn_hook; 436 struct socket *so = vp->v_fifoinfo->fi_writesock; 437 438 lwkt_gettoken(&vp->v_token); 439 ssb_remove_knote(&so->so_snd, kn); 440 lwkt_reltoken(&vp->v_token); 441 } 442 443 static int 444 filt_fifowrite(struct knote *kn, long hint) 445 { 446 struct vnode *vp = (void *)kn->kn_hook; 447 struct socket *so = vp->v_fifoinfo->fi_writesock; 448 449 lwkt_gettoken(&vp->v_token); 450 kn->kn_data = ssb_space(&so->so_snd); 451 if (so->so_state & SS_ISDISCONNECTED) { 452 kn->kn_flags |= (EV_EOF | EV_NODATA); 453 lwkt_reltoken(&vp->v_token); 454 return (1); 455 } 456 kn->kn_flags &= ~(EV_EOF | EV_NODATA); 457 lwkt_reltoken(&vp->v_token); 458 return (kn->kn_data >= so->so_snd.ssb_lowat); 459 } 460 461 /* 462 * fifo_inactive(struct vnode *a_vp) 463 */ 464 static int 465 fifo_inactive(struct vop_inactive_args *ap) 466 { 467 return (0); 468 } 469 470 /* 471 * This is a noop, simply returning what one has been given. 472 * 473 * fifo_bmap(struct vnode *a_vp, off_t a_loffset, 474 * off_t *a_doffsetp, int *a_runp, int *a_runb) 475 */ 476 static int 477 fifo_bmap(struct vop_bmap_args *ap) 478 { 479 if (ap->a_doffsetp != NULL) 480 *ap->a_doffsetp = ap->a_loffset; 481 if (ap->a_runp != NULL) 482 *ap->a_runp = 0; 483 if (ap->a_runb != NULL) 484 *ap->a_runb = 0; 485 return (0); 486 } 487 488 /* 489 * Device close routine 490 * 491 * fifo_close(struct vnode *a_vp, int a_fflag) 492 */ 493 /* ARGSUSED */ 494 static int 495 fifo_close(struct vop_close_args *ap) 496 { 497 struct vnode *vp = ap->a_vp; 498 struct fifoinfo *fip; 499 int error1, error2; 500 501 lwkt_gettoken(&vp->v_token); 502 fip = vp->v_fifoinfo; 503 if (ap->a_fflag & FREAD) { 504 fip->fi_readers--; 505 if (fip->fi_readers == 0) 506 soisdisconnected(fip->fi_writesock); 507 } 508 if (ap->a_fflag & FWRITE) { 509 fip->fi_writers--; 510 if (fip->fi_writers == 0) 511 soisdisconnected(fip->fi_readsock); 512 } 513 if (VREFCNT(vp) > 1) { 514 vop_stdclose(ap); 515 lwkt_reltoken(&vp->v_token); 516 return (0); 517 } 518 error1 = soclose(fip->fi_readsock, FNONBLOCK); 519 error2 = soclose(fip->fi_writesock, FNONBLOCK); 520 kfree(fip, M_FIFOINFO); 521 vp->v_fifoinfo = NULL; 522 if (error1) { 523 error2 = error1; 524 } else { 525 vop_stdclose(ap); 526 } 527 lwkt_reltoken(&vp->v_token); 528 return (error2); 529 } 530 531 532 /* 533 * Print out internal contents of a fifo vnode. 534 */ 535 int 536 fifo_printinfo(struct vnode *vp) 537 { 538 struct fifoinfo *fip = vp->v_fifoinfo; 539 540 kprintf(", fifo with %ld readers and %ld writers", 541 fip->fi_readers, fip->fi_writers); 542 return (0); 543 } 544 545 /* 546 * Print out the contents of a fifo vnode. 547 * 548 * fifo_print(struct vnode *a_vp) 549 */ 550 static int 551 fifo_print(struct vop_print_args *ap) 552 { 553 kprintf("tag VT_NON"); 554 fifo_printinfo(ap->a_vp); 555 kprintf("\n"); 556 return (0); 557 } 558 559 /* 560 * Return POSIX pathconf information applicable to fifo's. 561 * 562 * fifo_pathconf(struct vnode *a_vp, int a_name, int *a_retval) 563 */ 564 int 565 fifo_pathconf(struct vop_pathconf_args *ap) 566 { 567 switch (ap->a_name) { 568 case _PC_LINK_MAX: 569 *ap->a_retval = LINK_MAX; 570 return (0); 571 case _PC_PIPE_BUF: 572 *ap->a_retval = PIPE_BUF; 573 return (0); 574 case _PC_CHOWN_RESTRICTED: 575 *ap->a_retval = 1; 576 return (0); 577 default: 578 return (EINVAL); 579 } 580 /* NOTREACHED */ 581 } 582 583 /* 584 * Fifo advisory byte-level locks. 585 * 586 * fifo_advlock(struct vnode *a_vp, caddr_t a_id, int a_op, struct flock *a_fl, 587 * int a_flags) 588 */ 589 /* ARGSUSED */ 590 static int 591 fifo_advlock(struct vop_advlock_args *ap) 592 { 593 return ((ap->a_flags & F_POSIX) ? EINVAL : EOPNOTSUPP); 594 } 595 596 /* 597 * Fifo bad operation 598 */ 599 static int 600 fifo_badop(void) 601 { 602 panic("fifo_badop called"); 603 /* NOTREACHED */ 604 } 605