1 /* 2 * Copyright (c) 1990, 1993 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.2 (Berkeley) 1/4/94 34 */ 35 36 #include <sys/param.h> 37 #include <sys/proc.h> 38 #include <sys/time.h> 39 #include <sys/namei.h> 40 #include <sys/vnode.h> 41 #include <sys/socket.h> 42 #include <sys/socketvar.h> 43 #include <sys/stat.h> 44 #include <sys/systm.h> 45 #include <sys/ioctl.h> 46 #include <sys/file.h> 47 #include <sys/errno.h> 48 #include <sys/malloc.h> 49 #include <miscfs/fifofs/fifo.h> 50 51 /* 52 * This structure is associated with the FIFO vnode and stores 53 * the state associated with the FIFO. 54 */ 55 struct fifoinfo { 56 struct socket *fi_readsock; 57 struct socket *fi_writesock; 58 long fi_readers; 59 long fi_writers; 60 }; 61 62 int (**fifo_vnodeop_p)(); 63 struct vnodeopv_entry_desc fifo_vnodeop_entries[] = { 64 { &vop_default_desc, vn_default_error }, 65 { &vop_lookup_desc, fifo_lookup }, /* lookup */ 66 { &vop_create_desc, fifo_create }, /* create */ 67 { &vop_mknod_desc, fifo_mknod }, /* mknod */ 68 { &vop_open_desc, fifo_open }, /* open */ 69 { &vop_close_desc, fifo_close }, /* close */ 70 { &vop_access_desc, fifo_access }, /* access */ 71 { &vop_getattr_desc, fifo_getattr }, /* getattr */ 72 { &vop_setattr_desc, fifo_setattr }, /* setattr */ 73 { &vop_read_desc, fifo_read }, /* read */ 74 { &vop_write_desc, fifo_write }, /* write */ 75 { &vop_ioctl_desc, fifo_ioctl }, /* ioctl */ 76 { &vop_select_desc, fifo_select }, /* select */ 77 { &vop_mmap_desc, fifo_mmap }, /* mmap */ 78 { &vop_fsync_desc, fifo_fsync }, /* fsync */ 79 { &vop_seek_desc, fifo_seek }, /* seek */ 80 { &vop_remove_desc, fifo_remove }, /* remove */ 81 { &vop_link_desc, fifo_link }, /* link */ 82 { &vop_rename_desc, fifo_rename }, /* rename */ 83 { &vop_mkdir_desc, fifo_mkdir }, /* mkdir */ 84 { &vop_rmdir_desc, fifo_rmdir }, /* rmdir */ 85 { &vop_symlink_desc, fifo_symlink }, /* symlink */ 86 { &vop_readdir_desc, fifo_readdir }, /* readdir */ 87 { &vop_readlink_desc, fifo_readlink }, /* readlink */ 88 { &vop_abortop_desc, fifo_abortop }, /* abortop */ 89 { &vop_inactive_desc, fifo_inactive }, /* inactive */ 90 { &vop_reclaim_desc, fifo_reclaim }, /* reclaim */ 91 { &vop_lock_desc, fifo_lock }, /* lock */ 92 { &vop_unlock_desc, fifo_unlock }, /* unlock */ 93 { &vop_bmap_desc, fifo_bmap }, /* bmap */ 94 { &vop_strategy_desc, fifo_strategy }, /* strategy */ 95 { &vop_print_desc, fifo_print }, /* print */ 96 { &vop_islocked_desc, fifo_islocked }, /* islocked */ 97 { &vop_pathconf_desc, fifo_pathconf }, /* pathconf */ 98 { &vop_advlock_desc, fifo_advlock }, /* advlock */ 99 { &vop_blkatoff_desc, fifo_blkatoff }, /* blkatoff */ 100 { &vop_valloc_desc, fifo_valloc }, /* valloc */ 101 { &vop_vfree_desc, fifo_vfree }, /* vfree */ 102 { &vop_truncate_desc, fifo_truncate }, /* truncate */ 103 { &vop_update_desc, fifo_update }, /* update */ 104 { &vop_bwrite_desc, fifo_bwrite }, /* bwrite */ 105 { (struct vnodeop_desc*)NULL, (int(*)())NULL } 106 }; 107 struct vnodeopv_desc fifo_vnodeop_opv_desc = 108 { &fifo_vnodeop_p, fifo_vnodeop_entries }; 109 110 /* 111 * Trivial lookup routine that always fails. 112 */ 113 /* ARGSUSED */ 114 int 115 fifo_lookup(ap) 116 struct vop_lookup_args /* { 117 struct vnode * a_dvp; 118 struct vnode ** a_vpp; 119 struct componentname * a_cnp; 120 } */ *ap; 121 { 122 123 *ap->a_vpp = NULL; 124 return (ENOTDIR); 125 } 126 127 /* 128 * Open called to set up a new instance of a fifo or 129 * to find an active instance of a fifo. 130 */ 131 /* ARGSUSED */ 132 int 133 fifo_open(ap) 134 struct vop_open_args /* { 135 struct vnode *a_vp; 136 int a_mode; 137 struct ucred *a_cred; 138 struct proc *a_p; 139 } */ *ap; 140 { 141 register struct vnode *vp = ap->a_vp; 142 register struct fifoinfo *fip; 143 struct socket *rso, *wso; 144 int error; 145 static char openstr[] = "fifo"; 146 147 if ((ap->a_mode & (FREAD|FWRITE)) == (FREAD|FWRITE)) 148 return (EINVAL); 149 if ((fip = vp->v_fifoinfo) == NULL) { 150 MALLOC(fip, struct fifoinfo *, sizeof(*fip), M_VNODE, M_WAITOK); 151 vp->v_fifoinfo = fip; 152 if (error = socreate(AF_UNIX, &rso, SOCK_STREAM, 0)) { 153 free(fip, M_VNODE); 154 vp->v_fifoinfo = NULL; 155 return (error); 156 } 157 fip->fi_readsock = rso; 158 if (error = socreate(AF_UNIX, &wso, SOCK_STREAM, 0)) { 159 (void)soclose(rso); 160 free(fip, M_VNODE); 161 vp->v_fifoinfo = NULL; 162 return (error); 163 } 164 fip->fi_writesock = wso; 165 if (error = unp_connect2(wso, rso)) { 166 (void)soclose(wso); 167 (void)soclose(rso); 168 free(fip, M_VNODE); 169 vp->v_fifoinfo = NULL; 170 return (error); 171 } 172 fip->fi_readers = fip->fi_writers = 0; 173 wso->so_state |= SS_CANTRCVMORE; 174 rso->so_state |= SS_CANTSENDMORE; 175 } 176 error = 0; 177 if (ap->a_mode & FREAD) { 178 fip->fi_readers++; 179 if (fip->fi_readers == 1) { 180 fip->fi_writesock->so_state &= ~SS_CANTSENDMORE; 181 if (fip->fi_writers > 0) 182 wakeup((caddr_t)&fip->fi_writers); 183 } 184 if (ap->a_mode & O_NONBLOCK) 185 return (0); 186 while (fip->fi_writers == 0) { 187 VOP_UNLOCK(vp); 188 error = tsleep((caddr_t)&fip->fi_readers, 189 PCATCH | PSOCK, openstr, 0); 190 VOP_LOCK(vp); 191 if (error) 192 break; 193 } 194 } else { 195 fip->fi_writers++; 196 if (fip->fi_readers == 0 && (ap->a_mode & O_NONBLOCK)) { 197 error = ENXIO; 198 } else { 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 while (fip->fi_readers == 0) { 205 VOP_UNLOCK(vp); 206 error = tsleep((caddr_t)&fip->fi_writers, 207 PCATCH | PSOCK, openstr, 0); 208 VOP_LOCK(vp); 209 if (error) 210 break; 211 } 212 } 213 } 214 if (error) 215 VOP_CLOSE(vp, ap->a_mode, ap->a_cred, ap->a_p); 216 return (error); 217 } 218 219 /* 220 * Vnode op for read 221 */ 222 /* ARGSUSED */ 223 int 224 fifo_read(ap) 225 struct vop_read_args /* { 226 struct vnode *a_vp; 227 struct uio *a_uio; 228 int a_ioflag; 229 struct ucred *a_cred; 230 } */ *ap; 231 { 232 register struct uio *uio = ap->a_uio; 233 register struct socket *rso = ap->a_vp->v_fifoinfo->fi_readsock; 234 int error, startresid; 235 236 #ifdef DIAGNOSTIC 237 if (uio->uio_rw != UIO_READ) 238 panic("fifo_read mode"); 239 #endif 240 if (uio->uio_resid == 0) 241 return (0); 242 if (ap->a_ioflag & IO_NDELAY) 243 rso->so_state |= SS_NBIO; 244 startresid = uio->uio_resid; 245 VOP_UNLOCK(ap->a_vp); 246 error = soreceive(rso, (struct mbuf **)0, uio, (int *)0, 247 (struct mbuf **)0, (struct mbuf **)0); 248 VOP_LOCK(ap->a_vp); 249 /* 250 * Clear EOF indication after first such return. 251 */ 252 if (uio->uio_resid == startresid) 253 rso->so_state &= ~SS_CANTRCVMORE; 254 if (ap->a_ioflag & IO_NDELAY) 255 rso->so_state &= ~SS_NBIO; 256 return (error); 257 } 258 259 /* 260 * Vnode op for write 261 */ 262 /* ARGSUSED */ 263 int 264 fifo_write(ap) 265 struct vop_write_args /* { 266 struct vnode *a_vp; 267 struct uio *a_uio; 268 int a_ioflag; 269 struct ucred *a_cred; 270 } */ *ap; 271 { 272 struct socket *wso = ap->a_vp->v_fifoinfo->fi_writesock; 273 int error; 274 275 #ifdef DIAGNOSTIC 276 if (ap->a_uio->uio_rw != UIO_WRITE) 277 panic("fifo_write mode"); 278 #endif 279 if (ap->a_ioflag & IO_NDELAY) 280 wso->so_state |= SS_NBIO; 281 VOP_UNLOCK(ap->a_vp); 282 error = sosend(wso, (struct mbuf *)0, ap->a_uio, 0, (struct mbuf *)0, 0); 283 VOP_LOCK(ap->a_vp); 284 if (ap->a_ioflag & IO_NDELAY) 285 wso->so_state &= ~SS_NBIO; 286 return (error); 287 } 288 289 /* 290 * Device ioctl operation. 291 */ 292 /* ARGSUSED */ 293 int 294 fifo_ioctl(ap) 295 struct vop_ioctl_args /* { 296 struct vnode *a_vp; 297 int a_command; 298 caddr_t a_data; 299 int a_fflag; 300 struct ucred *a_cred; 301 struct proc *a_p; 302 } */ *ap; 303 { 304 struct file filetmp; 305 306 if (ap->a_command == FIONBIO) 307 return (0); 308 if (ap->a_fflag & FREAD) 309 filetmp.f_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_readsock; 310 else 311 filetmp.f_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_writesock; 312 return (soo_ioctl(&filetmp, ap->a_command, ap->a_data, ap->a_p)); 313 } 314 315 /* ARGSUSED */ 316 int 317 fifo_select(ap) 318 struct vop_select_args /* { 319 struct vnode *a_vp; 320 int a_which; 321 int a_fflags; 322 struct ucred *a_cred; 323 struct proc *a_p; 324 } */ *ap; 325 { 326 struct file filetmp; 327 328 if (ap->a_fflags & FREAD) 329 filetmp.f_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_readsock; 330 else 331 filetmp.f_data = (caddr_t)ap->a_vp->v_fifoinfo->fi_writesock; 332 return (soo_select(&filetmp, ap->a_which, ap->a_p)); 333 } 334 335 /* 336 * This is a noop, simply returning what one has been given. 337 */ 338 int 339 fifo_bmap(ap) 340 struct vop_bmap_args /* { 341 struct vnode *a_vp; 342 daddr_t a_bn; 343 struct vnode **a_vpp; 344 daddr_t *a_bnp; 345 } */ *ap; 346 { 347 348 if (ap->a_vpp != NULL) 349 *ap->a_vpp = ap->a_vp; 350 if (ap->a_bnp != NULL) 351 *ap->a_bnp = ap->a_bn; 352 return (0); 353 } 354 355 /* 356 * At the moment we do not do any locking. 357 */ 358 /* ARGSUSED */ 359 int 360 fifo_lock(ap) 361 struct vop_lock_args /* { 362 struct vnode *a_vp; 363 } */ *ap; 364 { 365 366 return (0); 367 } 368 369 /* ARGSUSED */ 370 int 371 fifo_unlock(ap) 372 struct vop_unlock_args /* { 373 struct vnode *a_vp; 374 } */ *ap; 375 { 376 377 return (0); 378 } 379 380 /* 381 * Device close routine 382 */ 383 /* ARGSUSED */ 384 int 385 fifo_close(ap) 386 struct vop_close_args /* { 387 struct vnode *a_vp; 388 int a_fflag; 389 struct ucred *a_cred; 390 struct proc *a_p; 391 } */ *ap; 392 { 393 register struct vnode *vp = ap->a_vp; 394 register struct fifoinfo *fip = vp->v_fifoinfo; 395 int error1, error2; 396 397 if (ap->a_fflag & FWRITE) { 398 fip->fi_writers--; 399 if (fip->fi_writers == 0) 400 socantrcvmore(fip->fi_readsock); 401 } else { 402 fip->fi_readers--; 403 if (fip->fi_readers == 0) 404 socantsendmore(fip->fi_writesock); 405 } 406 if (vp->v_usecount > 1) 407 return (0); 408 error1 = soclose(fip->fi_readsock); 409 error2 = soclose(fip->fi_writesock); 410 FREE(fip, M_VNODE); 411 vp->v_fifoinfo = NULL; 412 if (error1) 413 return (error1); 414 return (error2); 415 } 416 417 /* 418 * Print out the contents of a fifo vnode. 419 */ 420 int 421 fifo_print(ap) 422 struct vop_print_args /* { 423 struct vnode *a_vp; 424 } */ *ap; 425 { 426 427 printf("tag VT_NON"); 428 fifo_printinfo(ap->a_vp); 429 printf("\n"); 430 return (0); 431 } 432 433 /* 434 * Print out internal contents of a fifo vnode. 435 */ 436 int 437 fifo_printinfo(vp) 438 struct vnode *vp; 439 { 440 register struct fifoinfo *fip = vp->v_fifoinfo; 441 442 printf(", fifo with %d readers and %d writers", 443 fip->fi_readers, fip->fi_writers); 444 return (0); 445 } 446 447 /* 448 * Return POSIX pathconf information applicable to fifo's. 449 */ 450 int 451 fifo_pathconf(ap) 452 struct vop_pathconf_args /* { 453 struct vnode *a_vp; 454 int a_name; 455 int *a_retval; 456 } */ *ap; 457 { 458 459 switch (ap->a_name) { 460 case _PC_LINK_MAX: 461 *ap->a_retval = LINK_MAX; 462 return (0); 463 case _PC_PIPE_BUF: 464 *ap->a_retval = PIPE_BUF; 465 return (0); 466 case _PC_CHOWN_RESTRICTED: 467 *ap->a_retval = 1; 468 return (0); 469 default: 470 return (EINVAL); 471 } 472 /* NOTREACHED */ 473 } 474 475 /* 476 * Fifo failed operation 477 */ 478 int 479 fifo_ebadf() 480 { 481 482 return (EBADF); 483 } 484 485 /* 486 * Fifo advisory byte-level locks. 487 */ 488 /* ARGSUSED */ 489 int 490 fifo_advlock(ap) 491 struct vop_advlock_args /* { 492 struct vnode *a_vp; 493 caddr_t a_id; 494 int a_op; 495 struct flock *a_fl; 496 int a_flags; 497 } */ *ap; 498 { 499 500 return (EOPNOTSUPP); 501 } 502 503 /* 504 * Fifo bad operation 505 */ 506 int 507 fifo_badop() 508 { 509 510 panic("fifo_badop called"); 511 /* NOTREACHED */ 512 } 513