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