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 * $Id$ 35 */ 36 37 #include <sys/param.h> 38 #include <sys/systm.h> 39 #include <sys/proc.h> 40 #include <sys/time.h> 41 #include <sys/namei.h> 42 #include <sys/unistd.h> 43 #include <sys/kernel.h> 44 #include <sys/vnode.h> 45 #include <sys/socket.h> 46 #include <sys/socketvar.h> 47 #include <sys/stat.h> 48 #include <sys/ioctl.h> 49 #include <sys/file.h> 50 #include <sys/errno.h> 51 #include <sys/malloc.h> 52 #include <sys/un.h> 53 #include <miscfs/fifofs/fifo.h> 54 55 /* 56 * This structure is associated with the FIFO vnode and stores 57 * the state associated with the FIFO. 58 */ 59 struct fifoinfo { 60 struct socket *fi_readsock; 61 struct socket *fi_writesock; 62 long fi_readers; 63 long fi_writers; 64 }; 65 66 static int fifo_ebadf __P((void)); 67 static int fifo_print __P((struct vop_print_args *)); 68 69 vop_t **fifo_vnodeop_p; 70 static struct vnodeopv_entry_desc fifo_vnodeop_entries[] = { 71 { &vop_default_desc, (vop_t *)vn_default_error }, 72 { &vop_lookup_desc, (vop_t *)fifo_lookup }, /* lookup */ 73 { &vop_create_desc, (vop_t *)fifo_create }, /* create */ 74 { &vop_mknod_desc, (vop_t *)fifo_mknod }, /* mknod */ 75 { &vop_open_desc, (vop_t *)fifo_open }, /* open */ 76 { &vop_close_desc, (vop_t *)fifo_close }, /* close */ 77 { &vop_access_desc, (vop_t *)fifo_access }, /* access */ 78 { &vop_getattr_desc, (vop_t *)fifo_getattr }, /* getattr */ 79 { &vop_setattr_desc, (vop_t *)fifo_setattr }, /* setattr */ 80 { &vop_read_desc, (vop_t *)fifo_read }, /* read */ 81 { &vop_write_desc, (vop_t *)fifo_write }, /* write */ 82 { &vop_lease_desc, (vop_t *)fifo_lease_check }, /* lease */ 83 { &vop_ioctl_desc, (vop_t *)fifo_ioctl }, /* ioctl */ 84 { &vop_select_desc, (vop_t *)fifo_select }, /* select */ 85 { &vop_revoke_desc, (vop_t *)fifo_revoke }, /* revoke */ 86 { &vop_mmap_desc, (vop_t *)fifo_mmap }, /* mmap */ 87 { &vop_fsync_desc, (vop_t *)fifo_fsync }, /* fsync */ 88 { &vop_seek_desc, (vop_t *)fifo_seek }, /* seek */ 89 { &vop_remove_desc, (vop_t *)fifo_remove }, /* remove */ 90 { &vop_link_desc, (vop_t *)fifo_link }, /* link */ 91 { &vop_rename_desc, (vop_t *)fifo_rename }, /* rename */ 92 { &vop_mkdir_desc, (vop_t *)fifo_mkdir }, /* mkdir */ 93 { &vop_rmdir_desc, (vop_t *)fifo_rmdir }, /* rmdir */ 94 { &vop_symlink_desc, (vop_t *)fifo_symlink }, /* symlink */ 95 { &vop_readdir_desc, (vop_t *)fifo_readdir }, /* readdir */ 96 { &vop_readlink_desc, (vop_t *)fifo_readlink }, /* readlink */ 97 { &vop_abortop_desc, (vop_t *)fifo_abortop }, /* abortop */ 98 { &vop_inactive_desc, (vop_t *)fifo_inactive }, /* inactive */ 99 { &vop_reclaim_desc, (vop_t *)fifo_reclaim }, /* reclaim */ 100 { &vop_lock_desc, (vop_t *)fifo_lock }, /* lock */ 101 { &vop_unlock_desc, (vop_t *)fifo_unlock }, /* unlock */ 102 { &vop_bmap_desc, (vop_t *)fifo_bmap }, /* bmap */ 103 { &vop_strategy_desc, (vop_t *)fifo_strategy }, /* strategy */ 104 { &vop_print_desc, (vop_t *)fifo_print }, /* print */ 105 { &vop_islocked_desc, (vop_t *)fifo_islocked }, /* islocked */ 106 { &vop_pathconf_desc, (vop_t *)fifo_pathconf }, /* pathconf */ 107 { &vop_advlock_desc, (vop_t *)fifo_advlock }, /* advlock */ 108 { &vop_blkatoff_desc, (vop_t *)fifo_blkatoff }, /* blkatoff */ 109 { &vop_valloc_desc, (vop_t *)fifo_valloc }, /* valloc */ 110 { &vop_vfree_desc, (vop_t *)fifo_vfree }, /* vfree */ 111 { &vop_truncate_desc, (vop_t *)fifo_truncate }, /* truncate */ 112 { &vop_update_desc, (vop_t *)fifo_update }, /* update */ 113 { &vop_bwrite_desc, (vop_t *)fifo_bwrite }, /* bwrite */ 114 { NULL, NULL } 115 }; 116 static struct vnodeopv_desc fifo_vnodeop_opv_desc = 117 { &fifo_vnodeop_p, fifo_vnodeop_entries }; 118 119 VNODEOP_SET(fifo_vnodeop_opv_desc); 120 121 /* 122 * Trivial lookup routine that always fails. 123 */ 124 /* ARGSUSED */ 125 int 126 fifo_lookup(ap) 127 struct vop_lookup_args /* { 128 struct vnode * a_dvp; 129 struct vnode ** a_vpp; 130 struct componentname * a_cnp; 131 } */ *ap; 132 { 133 134 *ap->a_vpp = NULL; 135 return (ENOTDIR); 136 } 137 138 /* 139 * Open called to set up a new instance of a fifo or 140 * to find an active instance of a fifo. 141 */ 142 /* ARGSUSED */ 143 int 144 fifo_open(ap) 145 struct vop_open_args /* { 146 struct vnode *a_vp; 147 int a_mode; 148 struct ucred *a_cred; 149 struct proc *a_p; 150 } */ *ap; 151 { 152 struct vnode *vp = ap->a_vp; 153 struct fifoinfo *fip; 154 struct proc *p = ap->a_p; 155 struct socket *rso, *wso; 156 int error; 157 static char openstr[] = "fifo"; 158 159 if ((fip = vp->v_fifoinfo) == NULL) { 160 MALLOC(fip, struct fifoinfo *, sizeof(*fip), M_VNODE, M_WAITOK); 161 vp->v_fifoinfo = fip; 162 error = socreate(AF_LOCAL, &rso, SOCK_STREAM, 0, ap->a_p); 163 if (error) { 164 free(fip, M_VNODE); 165 vp->v_fifoinfo = NULL; 166 return (error); 167 } 168 fip->fi_readsock = rso; 169 error = socreate(AF_LOCAL, &wso, SOCK_STREAM, 0, ap->a_p); 170 if (error) { 171 (void)soclose(rso); 172 free(fip, M_VNODE); 173 vp->v_fifoinfo = NULL; 174 return (error); 175 } 176 fip->fi_writesock = wso; 177 error = unp_connect2(wso, rso); 178 if (error) { 179 (void)soclose(wso); 180 (void)soclose(rso); 181 free(fip, M_VNODE); 182 vp->v_fifoinfo = NULL; 183 return (error); 184 } 185 fip->fi_readers = fip->fi_writers = 0; 186 wso->so_state |= SS_CANTRCVMORE; 187 rso->so_state |= SS_CANTSENDMORE; 188 } 189 if (ap->a_mode & FREAD) { 190 fip->fi_readers++; 191 if (fip->fi_readers == 1) { 192 fip->fi_writesock->so_state &= ~SS_CANTSENDMORE; 193 if (fip->fi_writers > 0) 194 wakeup((caddr_t)&fip->fi_writers); 195 } 196 } 197 if (ap->a_mode & FWRITE) { 198 fip->fi_writers++; 199 if (fip->fi_writers == 1) { 200 fip->fi_readsock->so_state &= ~SS_CANTRCVMORE; 201 if (fip->fi_readers > 0) 202 wakeup((caddr_t)&fip->fi_readers); 203 } 204 } 205 if ((ap->a_mode & FREAD) && (ap->a_mode & O_NONBLOCK) == 0) { 206 while (fip->fi_writers == 0) { 207 VOP_UNLOCK(vp, 0, p); 208 error = tsleep((caddr_t)&fip->fi_readers, 209 PCATCH | PSOCK, openstr, 0); 210 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p); 211 if (error) 212 goto bad; 213 } 214 } 215 if (ap->a_mode & FWRITE) { 216 if (ap->a_mode & O_NONBLOCK) { 217 if (fip->fi_readers == 0) { 218 error = ENXIO; 219 goto bad; 220 } 221 } else { 222 while (fip->fi_readers == 0) { 223 VOP_UNLOCK(vp, 0, p); 224 error = tsleep((caddr_t)&fip->fi_writers, 225 PCATCH | PSOCK, openstr, 0); 226 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p); 227 if (error) 228 goto bad; 229 } 230 } 231 } 232 return (0); 233 bad: 234 VOP_CLOSE(vp, ap->a_mode, ap->a_cred, p); 235 return (error); 236 } 237 238 /* 239 * Vnode op for read 240 */ 241 /* ARGSUSED */ 242 int 243 fifo_read(ap) 244 struct vop_read_args /* { 245 struct vnode *a_vp; 246 struct uio *a_uio; 247 int a_ioflag; 248 struct ucred *a_cred; 249 } */ *ap; 250 { 251 struct uio *uio = ap->a_uio; 252 struct socket *rso = ap->a_vp->v_fifoinfo->fi_readsock; 253 struct proc *p = uio->uio_procp; 254 int error, startresid; 255 256 #ifdef DIAGNOSTIC 257 if (uio->uio_rw != UIO_READ) 258 panic("fifo_read mode"); 259 #endif 260 if (uio->uio_resid == 0) 261 return (0); 262 if (ap->a_ioflag & IO_NDELAY) 263 rso->so_state |= SS_NBIO; 264 startresid = uio->uio_resid; 265 VOP_UNLOCK(ap->a_vp, 0, p); 266 error = soreceive(rso, (struct mbuf **)0, uio, (struct mbuf **)0, 267 (struct mbuf **)0, (int *)0); 268 vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY, p); 269 /* 270 * Clear EOF indication after first such return. 271 */ 272 if (uio->uio_resid == startresid) 273 rso->so_state &= ~SS_CANTRCVMORE; 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 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 mbuf *)0, ap->a_uio, 0, (struct mbuf *)0, 0); 304 vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY, p); 305 if (ap->a_ioflag & IO_NDELAY) 306 wso->so_state &= ~SS_NBIO; 307 return (error); 308 } 309 310 /* 311 * Device ioctl operation. 312 */ 313 /* ARGSUSED */ 314 int 315 fifo_ioctl(ap) 316 struct vop_ioctl_args /* { 317 struct vnode *a_vp; 318 int a_command; 319 caddr_t a_data; 320 int a_fflag; 321 struct ucred *a_cred; 322 struct proc *a_p; 323 } */ *ap; 324 { 325 struct file filetmp; 326 int error; 327 328 if (ap->a_command == FIONBIO) 329 return (0); 330 if (ap->a_fflag & FREAD) { 331 filetmp.f_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_readsock; 332 error = soo_ioctl(&filetmp, ap->a_command, ap->a_data, ap->a_p); 333 if (error) 334 return (error); 335 } 336 if (ap->a_fflag & FWRITE) { 337 filetmp.f_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_writesock; 338 error = soo_ioctl(&filetmp, ap->a_command, ap->a_data, ap->a_p); 339 if (error) 340 return (error); 341 } 342 return (0); 343 } 344 345 /* ARGSUSED */ 346 int 347 fifo_select(ap) 348 struct vop_select_args /* { 349 struct vnode *a_vp; 350 int a_which; 351 int a_fflags; 352 struct ucred *a_cred; 353 struct proc *a_p; 354 } */ *ap; 355 { 356 struct file filetmp; 357 int ready; 358 359 if (ap->a_fflags & FREAD) { 360 filetmp.f_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_readsock; 361 ready = soo_select(&filetmp, ap->a_which, ap->a_p); 362 if (ready) 363 return (ready); 364 } 365 if (ap->a_fflags & FWRITE) { 366 filetmp.f_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_writesock; 367 ready = soo_select(&filetmp, ap->a_which, ap->a_p); 368 if (ready) 369 return (ready); 370 } 371 return (0); 372 } 373 374 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 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 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 failed operation 511 */ 512 static int 513 fifo_ebadf() 514 { 515 516 return (EBADF); 517 } 518 519 /* 520 * Fifo advisory byte-level locks. 521 */ 522 /* ARGSUSED */ 523 int 524 fifo_advlock(ap) 525 struct vop_advlock_args /* { 526 struct vnode *a_vp; 527 caddr_t a_id; 528 int a_op; 529 struct flock *a_fl; 530 int a_flags; 531 } */ *ap; 532 { 533 534 return (ap->a_flags & F_FLOCK ? EOPNOTSUPP : EINVAL); 535 } 536 537 /* 538 * Fifo bad operation 539 */ 540 int 541 fifo_badop() 542 { 543 544 panic("fifo_badop called"); 545 /* NOTREACHED */ 546 } 547