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/malloc.h> 43 #include <sys/vnode.h> 44 #include <sys/socket.h> 45 #include <sys/socketvar.h> 46 #include <sys/filio.h> 47 #include <sys/fcntl.h> 48 #include <sys/file.h> 49 #include <sys/poll.h> 50 #include <sys/un.h> 51 #include <miscfs/fifofs/fifo.h> 52 53 /* 54 * This structure is associated with the FIFO vnode and stores 55 * the state associated with the FIFO. 56 */ 57 struct fifoinfo { 58 struct socket *fi_readsock; 59 struct socket *fi_writesock; 60 long fi_readers; 61 long fi_writers; 62 }; 63 64 static int fifo_badop __P((void)); 65 static int fifo_print __P((struct vop_print_args *)); 66 static int fifo_lookup __P((struct vop_lookup_args *)); 67 static int fifo_open __P((struct vop_open_args *)); 68 static int fifo_close __P((struct vop_close_args *)); 69 static int fifo_read __P((struct vop_read_args *)); 70 static int fifo_write __P((struct vop_write_args *)); 71 static int fifo_ioctl __P((struct vop_ioctl_args *)); 72 static int fifo_poll __P((struct vop_poll_args *)); 73 static int fifo_inactive __P((struct vop_inactive_args *)); 74 static int fifo_bmap __P((struct vop_bmap_args *)); 75 static int fifo_pathconf __P((struct vop_pathconf_args *)); 76 static int fifo_advlock __P((struct vop_advlock_args *)); 77 78 79 vop_t **fifo_vnodeop_p; 80 static struct vnodeopv_entry_desc fifo_vnodeop_entries[] = { 81 { &vop_default_desc, (vop_t *) vop_defaultop }, 82 { &vop_access_desc, (vop_t *) vop_ebadf }, 83 { &vop_advlock_desc, (vop_t *) fifo_advlock }, 84 { &vop_bmap_desc, (vop_t *) fifo_bmap }, 85 { &vop_close_desc, (vop_t *) fifo_close }, 86 { &vop_create_desc, (vop_t *) fifo_badop }, 87 { &vop_getattr_desc, (vop_t *) vop_ebadf }, 88 { &vop_inactive_desc, (vop_t *) fifo_inactive }, 89 { &vop_ioctl_desc, (vop_t *) fifo_ioctl }, 90 { &vop_lease_desc, (vop_t *) vop_null }, 91 { &vop_link_desc, (vop_t *) fifo_badop }, 92 { &vop_lookup_desc, (vop_t *) fifo_lookup }, 93 { &vop_mkdir_desc, (vop_t *) fifo_badop }, 94 { &vop_mknod_desc, (vop_t *) fifo_badop }, 95 { &vop_open_desc, (vop_t *) fifo_open }, 96 { &vop_pathconf_desc, (vop_t *) fifo_pathconf }, 97 { &vop_poll_desc, (vop_t *) fifo_poll }, 98 { &vop_print_desc, (vop_t *) fifo_print }, 99 { &vop_read_desc, (vop_t *) fifo_read }, 100 { &vop_readdir_desc, (vop_t *) fifo_badop }, 101 { &vop_readlink_desc, (vop_t *) fifo_badop }, 102 { &vop_reallocblks_desc, (vop_t *) fifo_badop }, 103 { &vop_reclaim_desc, (vop_t *) vop_null }, 104 { &vop_remove_desc, (vop_t *) fifo_badop }, 105 { &vop_rename_desc, (vop_t *) fifo_badop }, 106 { &vop_rmdir_desc, (vop_t *) fifo_badop }, 107 { &vop_setattr_desc, (vop_t *) vop_ebadf }, 108 { &vop_symlink_desc, (vop_t *) fifo_badop }, 109 { &vop_write_desc, (vop_t *) fifo_write }, 110 { NULL, NULL } 111 }; 112 static struct vnodeopv_desc fifo_vnodeop_opv_desc = 113 { &fifo_vnodeop_p, fifo_vnodeop_entries }; 114 115 VNODEOP_SET(fifo_vnodeop_opv_desc); 116 117 int 118 fifo_vnoperate(ap) 119 struct vop_generic_args /* { 120 struct vnodeop_desc *a_desc; 121 <other random data follows, presumably> 122 } */ *ap; 123 { 124 return (VOCALL(fifo_vnodeop_p, ap->a_desc->vdesc_offset, ap)); 125 } 126 127 /* 128 * Trivial lookup routine that always fails. 129 */ 130 /* ARGSUSED */ 131 static int 132 fifo_lookup(ap) 133 struct vop_lookup_args /* { 134 struct vnode * a_dvp; 135 struct vnode ** a_vpp; 136 struct componentname * a_cnp; 137 } */ *ap; 138 { 139 140 *ap->a_vpp = NULL; 141 return (ENOTDIR); 142 } 143 144 /* 145 * Open called to set up a new instance of a fifo or 146 * to find an active instance of a fifo. 147 */ 148 /* ARGSUSED */ 149 static int 150 fifo_open(ap) 151 struct vop_open_args /* { 152 struct vnode *a_vp; 153 int a_mode; 154 struct ucred *a_cred; 155 struct proc *a_p; 156 } */ *ap; 157 { 158 struct vnode *vp = ap->a_vp; 159 struct fifoinfo *fip; 160 struct proc *p = ap->a_p; 161 struct socket *rso, *wso; 162 int error; 163 164 if ((fip = vp->v_fifoinfo) == NULL) { 165 MALLOC(fip, struct fifoinfo *, sizeof(*fip), M_VNODE, M_WAITOK); 166 vp->v_fifoinfo = fip; 167 error = socreate(AF_LOCAL, &rso, SOCK_STREAM, 0, ap->a_p); 168 if (error) { 169 free(fip, M_VNODE); 170 vp->v_fifoinfo = NULL; 171 return (error); 172 } 173 fip->fi_readsock = rso; 174 error = socreate(AF_LOCAL, &wso, SOCK_STREAM, 0, ap->a_p); 175 if (error) { 176 (void)soclose(rso); 177 free(fip, M_VNODE); 178 vp->v_fifoinfo = NULL; 179 return (error); 180 } 181 fip->fi_writesock = wso; 182 error = unp_connect2(wso, rso); 183 if (error) { 184 (void)soclose(wso); 185 (void)soclose(rso); 186 free(fip, M_VNODE); 187 vp->v_fifoinfo = NULL; 188 return (error); 189 } 190 fip->fi_readers = fip->fi_writers = 0; 191 wso->so_snd.sb_lowat = PIPE_BUF; 192 rso->so_state |= SS_CANTRCVMORE; 193 } 194 if (ap->a_mode & FREAD) { 195 fip->fi_readers++; 196 if (fip->fi_readers == 1) { 197 fip->fi_writesock->so_state &= ~SS_CANTSENDMORE; 198 if (fip->fi_writers > 0) 199 wakeup((caddr_t)&fip->fi_writers); 200 } 201 } 202 if (ap->a_mode & FWRITE) { 203 fip->fi_writers++; 204 if (fip->fi_writers == 1) { 205 fip->fi_readsock->so_state &= ~SS_CANTRCVMORE; 206 if (fip->fi_readers > 0) 207 wakeup((caddr_t)&fip->fi_readers); 208 } 209 } 210 if ((ap->a_mode & FREAD) && (ap->a_mode & O_NONBLOCK) == 0) { 211 while (fip->fi_writers == 0) { 212 VOP_UNLOCK(vp, 0, p); 213 error = tsleep((caddr_t)&fip->fi_readers, 214 PCATCH | PSOCK, "fifoor", 0); 215 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p); 216 if (error) 217 goto bad; 218 } 219 } 220 if (ap->a_mode & FWRITE) { 221 if (ap->a_mode & O_NONBLOCK) { 222 if (fip->fi_readers == 0) { 223 error = ENXIO; 224 goto bad; 225 } 226 } else { 227 while (fip->fi_readers == 0) { 228 VOP_UNLOCK(vp, 0, p); 229 error = tsleep((caddr_t)&fip->fi_writers, 230 PCATCH | PSOCK, "fifoow", 0); 231 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p); 232 if (error) 233 goto bad; 234 } 235 } 236 } 237 return (0); 238 bad: 239 VOP_CLOSE(vp, ap->a_mode, ap->a_cred, p); 240 return (error); 241 } 242 243 /* 244 * Vnode op for read 245 */ 246 /* ARGSUSED */ 247 static int 248 fifo_read(ap) 249 struct vop_read_args /* { 250 struct vnode *a_vp; 251 struct uio *a_uio; 252 int a_ioflag; 253 struct ucred *a_cred; 254 } */ *ap; 255 { 256 struct uio *uio = ap->a_uio; 257 struct socket *rso = ap->a_vp->v_fifoinfo->fi_readsock; 258 struct proc *p = uio->uio_procp; 259 int error, startresid; 260 261 #ifdef DIAGNOSTIC 262 if (uio->uio_rw != UIO_READ) 263 panic("fifo_read mode"); 264 #endif 265 if (uio->uio_resid == 0) 266 return (0); 267 if (ap->a_ioflag & IO_NDELAY) 268 rso->so_state |= SS_NBIO; 269 startresid = uio->uio_resid; 270 VOP_UNLOCK(ap->a_vp, 0, p); 271 error = soreceive(rso, (struct sockaddr **)0, uio, (struct mbuf **)0, 272 (struct mbuf **)0, (int *)0); 273 vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY, p); 274 if (ap->a_ioflag & IO_NDELAY) 275 rso->so_state &= ~SS_NBIO; 276 return (error); 277 } 278 279 /* 280 * Vnode op for write 281 */ 282 /* ARGSUSED */ 283 static int 284 fifo_write(ap) 285 struct vop_write_args /* { 286 struct vnode *a_vp; 287 struct uio *a_uio; 288 int a_ioflag; 289 struct ucred *a_cred; 290 } */ *ap; 291 { 292 struct socket *wso = ap->a_vp->v_fifoinfo->fi_writesock; 293 struct proc *p = ap->a_uio->uio_procp; 294 int error; 295 296 #ifdef DIAGNOSTIC 297 if (ap->a_uio->uio_rw != UIO_WRITE) 298 panic("fifo_write mode"); 299 #endif 300 if (ap->a_ioflag & IO_NDELAY) 301 wso->so_state |= SS_NBIO; 302 VOP_UNLOCK(ap->a_vp, 0, p); 303 error = sosend(wso, (struct sockaddr *)0, ap->a_uio, 0, 304 (struct mbuf *)0, 0, p); 305 vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY, p); 306 if (ap->a_ioflag & IO_NDELAY) 307 wso->so_state &= ~SS_NBIO; 308 return (error); 309 } 310 311 /* 312 * Device ioctl operation. 313 */ 314 /* ARGSUSED */ 315 static int 316 fifo_ioctl(ap) 317 struct vop_ioctl_args /* { 318 struct vnode *a_vp; 319 int a_command; 320 caddr_t a_data; 321 int a_fflag; 322 struct ucred *a_cred; 323 struct proc *a_p; 324 } */ *ap; 325 { 326 struct file filetmp; 327 int error; 328 329 if (ap->a_command == FIONBIO) 330 return (0); 331 if (ap->a_fflag & FREAD) { 332 filetmp.f_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_readsock; 333 error = soo_ioctl(&filetmp, ap->a_command, ap->a_data, ap->a_p); 334 if (error) 335 return (error); 336 } 337 if (ap->a_fflag & FWRITE) { 338 filetmp.f_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_writesock; 339 error = soo_ioctl(&filetmp, ap->a_command, ap->a_data, ap->a_p); 340 if (error) 341 return (error); 342 } 343 return (0); 344 } 345 346 /* ARGSUSED */ 347 static int 348 fifo_poll(ap) 349 struct vop_poll_args /* { 350 struct vnode *a_vp; 351 int a_events; 352 struct ucred *a_cred; 353 struct proc *a_p; 354 } */ *ap; 355 { 356 struct file filetmp; 357 int revents = 0; 358 359 if (ap->a_events & (POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND)) { 360 filetmp.f_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_readsock; 361 if (filetmp.f_data) 362 revents |= soo_poll(&filetmp, ap->a_events, ap->a_cred, 363 ap->a_p); 364 } 365 if (ap->a_events & (POLLOUT | POLLWRNORM | POLLWRBAND)) { 366 filetmp.f_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_writesock; 367 if (filetmp.f_data) 368 revents |= soo_poll(&filetmp, ap->a_events, ap->a_cred, 369 ap->a_p); 370 } 371 return (revents); 372 } 373 374 static int 375 fifo_inactive(ap) 376 struct vop_inactive_args /* { 377 struct vnode *a_vp; 378 struct proc *a_p; 379 } */ *ap; 380 { 381 382 VOP_UNLOCK(ap->a_vp, 0, ap->a_p); 383 return (0); 384 } 385 386 /* 387 * This is a noop, simply returning what one has been given. 388 */ 389 static int 390 fifo_bmap(ap) 391 struct vop_bmap_args /* { 392 struct vnode *a_vp; 393 daddr_t a_bn; 394 struct vnode **a_vpp; 395 daddr_t *a_bnp; 396 int *a_runp; 397 int *a_runb; 398 } */ *ap; 399 { 400 401 if (ap->a_vpp != NULL) 402 *ap->a_vpp = ap->a_vp; 403 if (ap->a_bnp != NULL) 404 *ap->a_bnp = ap->a_bn; 405 if (ap->a_runp != NULL) 406 *ap->a_runp = 0; 407 if (ap->a_runb != NULL) 408 *ap->a_runb = 0; 409 return (0); 410 } 411 412 /* 413 * Device close routine 414 */ 415 /* ARGSUSED */ 416 static int 417 fifo_close(ap) 418 struct vop_close_args /* { 419 struct vnode *a_vp; 420 int a_fflag; 421 struct ucred *a_cred; 422 struct proc *a_p; 423 } */ *ap; 424 { 425 register struct vnode *vp = ap->a_vp; 426 register struct fifoinfo *fip = vp->v_fifoinfo; 427 int error1, error2; 428 429 if (ap->a_fflag & FREAD) { 430 fip->fi_readers--; 431 if (fip->fi_readers == 0) 432 socantsendmore(fip->fi_writesock); 433 } 434 if (ap->a_fflag & FWRITE) { 435 fip->fi_writers--; 436 if (fip->fi_writers == 0) 437 socantrcvmore(fip->fi_readsock); 438 } 439 if (vp->v_usecount > 1) 440 return (0); 441 error1 = soclose(fip->fi_readsock); 442 error2 = soclose(fip->fi_writesock); 443 FREE(fip, M_VNODE); 444 vp->v_fifoinfo = NULL; 445 if (error1) 446 return (error1); 447 return (error2); 448 } 449 450 451 /* 452 * Print out internal contents of a fifo vnode. 453 */ 454 int 455 fifo_printinfo(vp) 456 struct vnode *vp; 457 { 458 register struct fifoinfo *fip = vp->v_fifoinfo; 459 460 printf(", fifo with %ld readers and %ld writers", 461 fip->fi_readers, fip->fi_writers); 462 return (0); 463 } 464 465 /* 466 * Print out the contents of a fifo vnode. 467 */ 468 static int 469 fifo_print(ap) 470 struct vop_print_args /* { 471 struct vnode *a_vp; 472 } */ *ap; 473 { 474 475 printf("tag VT_NON"); 476 fifo_printinfo(ap->a_vp); 477 printf("\n"); 478 return (0); 479 } 480 481 /* 482 * Return POSIX pathconf information applicable to fifo's. 483 */ 484 int 485 fifo_pathconf(ap) 486 struct vop_pathconf_args /* { 487 struct vnode *a_vp; 488 int a_name; 489 int *a_retval; 490 } */ *ap; 491 { 492 493 switch (ap->a_name) { 494 case _PC_LINK_MAX: 495 *ap->a_retval = LINK_MAX; 496 return (0); 497 case _PC_PIPE_BUF: 498 *ap->a_retval = PIPE_BUF; 499 return (0); 500 case _PC_CHOWN_RESTRICTED: 501 *ap->a_retval = 1; 502 return (0); 503 default: 504 return (EINVAL); 505 } 506 /* NOTREACHED */ 507 } 508 509 /* 510 * Fifo advisory byte-level locks. 511 */ 512 /* ARGSUSED */ 513 static int 514 fifo_advlock(ap) 515 struct vop_advlock_args /* { 516 struct vnode *a_vp; 517 caddr_t a_id; 518 int a_op; 519 struct flock *a_fl; 520 int a_flags; 521 } */ *ap; 522 { 523 524 return (ap->a_flags & F_FLOCK ? EOPNOTSUPP : EINVAL); 525 } 526 527 /* 528 * Fifo bad operation 529 */ 530 static int 531 fifo_badop() 532 { 533 534 panic("fifo_badop called"); 535 /* NOTREACHED */ 536 } 537