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