1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 23 24 /* 25 * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 26 * Use is subject to license terms. 27 */ 28 29 #pragma ident "%Z%%M% %I% %E% SMI" 30 31 /* 32 * The routines defined in this file are supporting routines for FIFOFS 33 * file sytem type. 34 */ 35 #include <sys/types.h> 36 #include <sys/param.h> 37 #include <sys/systm.h> 38 #include <sys/debug.h> 39 #include <sys/errno.h> 40 #include <sys/time.h> 41 #include <sys/kmem.h> 42 #include <sys/inline.h> 43 #include <sys/file.h> 44 #include <sys/proc.h> 45 #include <sys/stat.h> 46 #include <sys/sysmacros.h> 47 #include <sys/var.h> 48 #include <sys/vfs.h> 49 #include <sys/vnode.h> 50 #include <sys/mode.h> 51 #include <sys/signal.h> 52 #include <sys/user.h> 53 #include <sys/uio.h> 54 #include <sys/flock.h> 55 #include <sys/stream.h> 56 #include <sys/fs/fifonode.h> 57 #include <sys/strsubr.h> 58 #include <sys/stropts.h> 59 #include <sys/cmn_err.h> 60 #include <fs/fs_subr.h> 61 #include <sys/ddi.h> 62 63 64 #if FIFODEBUG 65 int Fifo_fastmode = 1; /* pipes/fifos will be opened in fast mode */ 66 int Fifo_verbose = 0; /* msg when switching out of fast mode */ 67 int Fifohiwat = FIFOHIWAT; /* Modifiable FIFO high water mark */ 68 #endif 69 70 /* 71 * This is the loadable module wrapper. 72 */ 73 #include <sys/modctl.h> 74 75 extern struct qinit fifo_strdata; 76 77 struct vfsops *fifo_vfsops; 78 79 static vfsdef_t vfw = { 80 VFSDEF_VERSION, 81 "fifofs", 82 fifoinit, 83 0, 84 NULL 85 }; 86 87 /* 88 * Module linkage information for the kernel. 89 */ 90 extern struct mod_ops mod_fsops; 91 92 static struct modlfs modlfs = { 93 &mod_fsops, "filesystem for fifo", &vfw 94 }; 95 96 static struct modlinkage modlinkage = { 97 MODREV_1, (void *)&modlfs, NULL 98 }; 99 100 int 101 _init() 102 { 103 return (mod_install(&modlinkage)); 104 } 105 106 int 107 _info(struct modinfo *modinfop) 108 { 109 return (mod_info(&modlinkage, modinfop)); 110 } 111 112 /* 113 * Define data structures within this file. 114 * XXX should the hash size be configurable ? 115 */ 116 #define FIFOSHFT 5 117 #define FIFO_HASHSZ 63 118 119 #if ((FIFO_HASHSZ & (FIFO_HASHSZ - 1)) == 0) 120 #define FIFOHASH(vp) (((uintptr_t)(vp) >> FIFOSHFT) & (FIFO_HASHSZ - 1)) 121 #else 122 #define FIFOHASH(vp) (((uintptr_t)(vp) >> FIFOSHFT) % FIFO_HASHSZ) 123 #endif 124 125 fifonode_t *fifoalloc[FIFO_HASHSZ]; 126 dev_t fifodev; 127 struct vfs *fifovfsp; 128 int fifofstype; 129 130 kmutex_t ftable_lock; 131 static kmutex_t fino_lock; 132 struct kmem_cache *fnode_cache; 133 struct kmem_cache *pipe_cache; 134 135 static void fifoinsert(fifonode_t *); 136 static fifonode_t *fifofind(vnode_t *); 137 static int fifo_connld(struct vnode **, int, cred_t *); 138 static void fifo_fastturnoff(fifonode_t *); 139 140 static void fifo_reinit_vp(vnode_t *); 141 142 /* 143 * Constructor/destructor routines for fifos and pipes. 144 * 145 * In the interest of code sharing, we define a common fifodata structure 146 * which consists of a fifolock and one or two fnodes. A fifo contains 147 * one fnode; a pipe contains two. The fifolock is shared by the fnodes, 148 * each of which points to it: 149 * 150 * --> --> --------- --- --- 151 * | | | lock | | | 152 * | | --------- | | 153 * | | | | fifo | 154 * | --- | fnode | | | 155 * | | | | pipe 156 * | --------- --- | 157 * | | | | 158 * ------- | fnode | | 159 * | | | 160 * --------- --- 161 * 162 * Since the fifolock is at the beginning of the fifodata structure, 163 * the fifolock address is the same as the fifodata address. Thus, 164 * we can determine the fifodata address from any of its member fnodes. 165 * This is essential for fifo_inactive. 166 * 167 * The fnode constructor is designed to handle any fifodata struture, 168 * deducing the number of fnodes from the total size. Thus, the fnode 169 * constructor does most of the work for the pipe constructor. 170 */ 171 /*ARGSUSED1*/ 172 static int 173 fnode_constructor(void *buf, void *cdrarg, int kmflags) 174 { 175 fifodata_t *fdp = buf; 176 fifolock_t *flp = &fdp->fifo_lock; 177 fifonode_t *fnp = &fdp->fifo_fnode[0]; 178 size_t size = (uintptr_t)cdrarg; 179 180 mutex_init(&flp->flk_lock, NULL, MUTEX_DEFAULT, NULL); 181 cv_init(&flp->flk_wait_cv, NULL, CV_DEFAULT, NULL); 182 flp->flk_ocsync = 0; 183 184 while ((char *)fnp < (char *)buf + size) { 185 186 vnode_t *vp; 187 188 vp = vn_alloc(KM_SLEEP); 189 fnp->fn_vnode = vp; 190 191 fnp->fn_lock = flp; 192 fnp->fn_open = 0; 193 fnp->fn_dest = fnp; 194 fnp->fn_mp = NULL; 195 fnp->fn_count = 0; 196 fnp->fn_rsynccnt = 0; 197 fnp->fn_wsynccnt = 0; 198 fnp->fn_wwaitcnt = 0; 199 fnp->fn_insync = 0; 200 fnp->fn_pcredp = NULL; 201 fnp->fn_cpid = -1; 202 /* 203 * 32-bit stat(2) may fail if fn_ino isn't initialized 204 */ 205 fnp->fn_ino = 0; 206 207 cv_init(&fnp->fn_wait_cv, NULL, CV_DEFAULT, NULL); 208 209 vn_setops(vp, fifo_vnodeops); 210 vp->v_stream = NULL; 211 vp->v_type = VFIFO; 212 vp->v_data = (caddr_t)fnp; 213 vp->v_flag = VNOMAP | VNOSWAP; 214 vn_exists(vp); 215 fnp++; 216 } 217 return (0); 218 } 219 220 static void 221 fnode_destructor(void *buf, void *cdrarg) 222 { 223 fifodata_t *fdp = buf; 224 fifolock_t *flp = &fdp->fifo_lock; 225 fifonode_t *fnp = &fdp->fifo_fnode[0]; 226 size_t size = (uintptr_t)cdrarg; 227 228 mutex_destroy(&flp->flk_lock); 229 cv_destroy(&flp->flk_wait_cv); 230 ASSERT(flp->flk_ocsync == 0); 231 232 while ((char *)fnp < (char *)buf + size) { 233 234 vnode_t *vp = FTOV(fnp); 235 236 ASSERT(fnp->fn_mp == NULL); 237 ASSERT(fnp->fn_count == 0); 238 ASSERT(fnp->fn_lock == flp); 239 ASSERT(fnp->fn_open == 0); 240 ASSERT(fnp->fn_insync == 0); 241 ASSERT(fnp->fn_rsynccnt == 0 && fnp->fn_wsynccnt == 0); 242 ASSERT(fnp->fn_wwaitcnt == 0); 243 ASSERT(fnp->fn_pcredp == NULL); 244 ASSERT(vn_matchops(vp, fifo_vnodeops)); 245 ASSERT(vp->v_stream == NULL); 246 ASSERT(vp->v_type == VFIFO); 247 ASSERT(vp->v_data == (caddr_t)fnp); 248 ASSERT((vp->v_flag & (VNOMAP|VNOSWAP)) == (VNOMAP|VNOSWAP)); 249 250 cv_destroy(&fnp->fn_wait_cv); 251 vn_invalid(vp); 252 vn_free(vp); 253 254 fnp++; 255 } 256 } 257 258 static int 259 pipe_constructor(void *buf, void *cdrarg, int kmflags) 260 { 261 fifodata_t *fdp = buf; 262 fifonode_t *fnp1 = &fdp->fifo_fnode[0]; 263 fifonode_t *fnp2 = &fdp->fifo_fnode[1]; 264 vnode_t *vp1; 265 vnode_t *vp2; 266 267 (void) fnode_constructor(buf, cdrarg, kmflags); 268 269 vp1 = FTOV(fnp1); 270 vp2 = FTOV(fnp2); 271 272 vp1->v_vfsp = vp2->v_vfsp = fifovfsp; 273 vp1->v_rdev = vp2->v_rdev = fifodev; 274 fnp1->fn_realvp = fnp2->fn_realvp = NULL; 275 fnp1->fn_dest = fnp2; 276 fnp2->fn_dest = fnp1; 277 278 return (0); 279 } 280 281 static void 282 pipe_destructor(void *buf, void *cdrarg) 283 { 284 #ifdef DEBUG 285 fifodata_t *fdp = buf; 286 fifonode_t *fnp1 = &fdp->fifo_fnode[0]; 287 fifonode_t *fnp2 = &fdp->fifo_fnode[1]; 288 vnode_t *vp1 = FTOV(fnp1); 289 vnode_t *vp2 = FTOV(fnp2); 290 291 ASSERT(vp1->v_vfsp == fifovfsp); 292 ASSERT(vp2->v_vfsp == fifovfsp); 293 ASSERT(vp1->v_rdev == fifodev); 294 ASSERT(vp2->v_rdev == fifodev); 295 #endif 296 fnode_destructor(buf, cdrarg); 297 } 298 299 /* 300 * Reinitialize a FIFO vnode (uses normal vnode reinit, but ensures that 301 * vnode type and flags are reset). 302 */ 303 304 static void fifo_reinit_vp(vnode_t *vp) 305 { 306 vn_reinit(vp); 307 vp->v_type = VFIFO; 308 vp->v_flag = VNOMAP | VNOSWAP; 309 } 310 311 /* 312 * Save file system type/index, initialize vfs operations vector, get 313 * unique device number for FIFOFS and initialize the FIFOFS hash. 314 * Create and initialize a "generic" vfs pointer that will be placed 315 * in the v_vfsp field of each pipe's vnode. 316 */ 317 int 318 fifoinit(int fstype, char *name) 319 { 320 static const fs_operation_def_t fifo_vfsops_template[] = { 321 NULL, NULL 322 }; 323 int error; 324 major_t dev; 325 326 fifofstype = fstype; 327 error = vfs_setfsops(fstype, fifo_vfsops_template, &fifo_vfsops); 328 if (error != 0) { 329 cmn_err(CE_WARN, "fifoinit: bad vfs ops template"); 330 return (error); 331 } 332 333 error = vn_make_ops(name, fifo_vnodeops_template, &fifo_vnodeops); 334 if (error != 0) { 335 (void) vfs_freevfsops_by_type(fstype); 336 cmn_err(CE_WARN, "fifoinit: bad vnode ops template"); 337 return (error); 338 } 339 340 if ((dev = getudev()) == (major_t)-1) { 341 cmn_err(CE_WARN, "fifoinit: can't get unique device number"); 342 dev = 0; 343 } 344 fifodev = makedevice(dev, 0); 345 346 fifovfsp = kmem_zalloc(sizeof (struct vfs), KM_SLEEP); 347 fifovfsp->vfs_next = NULL; 348 vfs_setops(fifovfsp, fifo_vfsops); 349 fifovfsp->vfs_vnodecovered = NULL; 350 fifovfsp->vfs_flag = 0; 351 fifovfsp->vfs_bsize = 1024; 352 fifovfsp->vfs_fstype = fifofstype; 353 vfs_make_fsid(&fifovfsp->vfs_fsid, fifodev, fifofstype); 354 fifovfsp->vfs_data = NULL; 355 fifovfsp->vfs_dev = fifodev; 356 fifovfsp->vfs_bcount = 0; 357 358 mutex_init(&ftable_lock, NULL, MUTEX_DEFAULT, NULL); 359 mutex_init(&fino_lock, NULL, MUTEX_DEFAULT, NULL); 360 361 /* 362 * vnodes are cached aligned 363 */ 364 fnode_cache = kmem_cache_create("fnode_cache", 365 sizeof (fifodata_t) - sizeof (fifonode_t), 32, 366 fnode_constructor, fnode_destructor, NULL, 367 (void *)(sizeof (fifodata_t) - sizeof (fifonode_t)), NULL, 0); 368 369 pipe_cache = kmem_cache_create("pipe_cache", sizeof (fifodata_t), 32, 370 pipe_constructor, pipe_destructor, NULL, 371 (void *)(sizeof (fifodata_t)), NULL, 0); 372 373 #if FIFODEBUG 374 if (Fifohiwat < FIFOHIWAT) 375 Fifohiwat = FIFOHIWAT; 376 #endif /* FIFODEBUG */ 377 fifo_strdata.qi_minfo->mi_hiwat = Fifohiwat; 378 379 return (0); 380 } 381 382 /* 383 * Provide a shadow for a vnode. If vp already has a shadow in the hash list, 384 * return its shadow. Otherwise, create a vnode to shadow vp, hash the 385 * new vnode and return its pointer to the caller. 386 */ 387 vnode_t * 388 fifovp(vnode_t *vp, cred_t *crp) 389 { 390 fifonode_t *fnp; 391 fifodata_t *fdp; 392 vnode_t *newvp; 393 struct vattr va; 394 395 ASSERT(vp != NULL); 396 397 fdp = kmem_cache_alloc(fnode_cache, KM_SLEEP); 398 399 mutex_enter(&ftable_lock); 400 401 if ((fnp = fifofind(vp)) != NULL) { 402 mutex_exit(&ftable_lock); 403 #if DEBUG 404 fdp->fifo_fnode[0].fn_wcnt = 0; 405 fdp->fifo_fnode[0].fn_rcnt = 0; 406 #endif 407 kmem_cache_free(fnode_cache, fdp); 408 return (FTOV(fnp)); 409 } 410 411 fdp->fifo_lock.flk_ref = 1; 412 fnp = &fdp->fifo_fnode[0]; 413 fnp->fn_realvp = vp; 414 fnp->fn_wcnt = 0; 415 fnp->fn_rcnt = 0; 416 #if FIFODEBUG 417 if (! Fifo_fastmode) { 418 fnp->fn_flag = 0; 419 } else { 420 fnp->fn_flag = FIFOFAST; 421 } 422 #else /* FIFODEBUG */ 423 fnp->fn_flag = FIFOFAST; 424 #endif /* FIFODEBUG */ 425 426 /* 427 * initialize the times from vp. 428 */ 429 va.va_mask = AT_TIMES; 430 if (VOP_GETATTR(vp, &va, 0, crp) == 0) { 431 fnp->fn_atime = va.va_atime.tv_sec; 432 fnp->fn_mtime = va.va_mtime.tv_sec; 433 fnp->fn_ctime = va.va_ctime.tv_sec; 434 } else { 435 fnp->fn_atime = 0; 436 fnp->fn_mtime = 0; 437 fnp->fn_ctime = 0; 438 } 439 440 VN_HOLD(vp); 441 442 newvp = FTOV(fnp); 443 fifo_reinit_vp(newvp); 444 newvp->v_vfsp = vp->v_vfsp; 445 newvp->v_rdev = vp->v_rdev; 446 447 fifoinsert(fnp); 448 mutex_exit(&ftable_lock); 449 450 return (newvp); 451 } 452 453 /* 454 * Create a pipe end by... 455 * allocating a vnode-fifonode pair and initializing the fifonode. 456 */ 457 void 458 makepipe(vnode_t **vpp1, vnode_t **vpp2) 459 { 460 fifonode_t *fnp1; 461 fifonode_t *fnp2; 462 vnode_t *nvp1; 463 vnode_t *nvp2; 464 fifodata_t *fdp; 465 time_t now; 466 467 fdp = kmem_cache_alloc(pipe_cache, KM_SLEEP); 468 fdp->fifo_lock.flk_ref = 2; 469 fnp1 = &fdp->fifo_fnode[0]; 470 fnp2 = &fdp->fifo_fnode[1]; 471 472 fnp1->fn_wcnt = fnp2->fn_wcnt = 1; 473 fnp1->fn_rcnt = fnp2->fn_rcnt = 1; 474 #if FIFODEBUG 475 if (! Fifo_fastmode) { 476 fnp1->fn_flag = fnp2->fn_flag = ISPIPE; 477 } else { 478 fnp1->fn_flag = fnp2->fn_flag = ISPIPE | FIFOFAST; 479 } 480 #else /* FIFODEBUG */ 481 fnp1->fn_flag = fnp2->fn_flag = ISPIPE | FIFOFAST; 482 #endif /* FIFODEBUG */ 483 now = gethrestime_sec(); 484 fnp1->fn_atime = fnp2->fn_atime = now; 485 fnp1->fn_mtime = fnp2->fn_mtime = now; 486 fnp1->fn_ctime = fnp2->fn_ctime = now; 487 488 *vpp1 = nvp1 = FTOV(fnp1); 489 *vpp2 = nvp2 = FTOV(fnp2); 490 491 fifo_reinit_vp(nvp1); /* Reinitialize vnodes for reuse... */ 492 fifo_reinit_vp(nvp2); 493 nvp1->v_vfsp = fifovfsp; /* Need to re-establish VFS & device */ 494 nvp2->v_vfsp = fifovfsp; /* before we can reuse this vnode. */ 495 nvp1->v_rdev = fifodev; 496 nvp2->v_rdev = fifodev; 497 } 498 499 /* 500 * Attempt to establish a unique pipe id. Only un-named pipes use this 501 * routine. 502 */ 503 ino_t 504 fifogetid(void) 505 { 506 static ino_t fifo_ino = 0; 507 ino_t fino; 508 509 mutex_enter(&fino_lock); 510 fino = fifo_ino++; 511 mutex_exit(&fino_lock); 512 return (fino); 513 } 514 515 516 /* 517 * Stream a pipe/FIFO. 518 * The FIFOCONNLD flag is used when CONNLD has been pushed on the stream. 519 * If the flag is set, a new vnode is created by calling fifo_connld(). 520 * Connld logic was moved to fifo_connld() to speed up the open 521 * operation, simplify the connld/fifo interaction, and remove inherent 522 * race conditions between the connld module and fifos. 523 * This routine is single threaded for two reasons. 524 * 1) connld requests are synchronous; that is, they must block 525 * until the server does an I_RECVFD (oh, well). Single threading is 526 * the simplest way to accomplish this. 527 * 2) fifo_close() must not send M_HANGUP or M_ERROR while we are 528 * in stropen. Stropen() has a tendency to reset things and 529 * we would like streams to remember that a hangup occurred. 530 */ 531 int 532 fifo_stropen(vnode_t **vpp, int flag, cred_t *crp, int dotwist, int lockheld) 533 { 534 int error = 0; 535 vnode_t *oldvp = *vpp; 536 fifonode_t *fnp = VTOF(*vpp); 537 dev_t pdev = 0; 538 int firstopen = 0; 539 fifolock_t *fn_lock; 540 541 fn_lock = fnp->fn_lock; 542 if (!lockheld) 543 mutex_enter(&fn_lock->flk_lock); 544 ASSERT(MUTEX_HELD(&fnp->fn_lock->flk_lock)); 545 546 /* 547 * FIFO is in the process of opening. Wait for it 548 * to complete before starting another open on it 549 * This prevents races associated with connld open 550 */ 551 while (fnp->fn_flag & FIFOOPEN) { 552 if (!cv_wait_sig(&fnp->fn_wait_cv, &fn_lock->flk_lock)) { 553 fifo_cleanup(oldvp, flag); 554 if (!lockheld) 555 mutex_exit(&fn_lock->flk_lock); 556 return (EINTR); 557 } 558 } 559 560 /* 561 * The other end of the pipe is almost closed so 562 * reject any other open on this end of the pipe 563 * This only happens with a pipe mounted under namefs 564 */ 565 if ((fnp->fn_flag & (FIFOCLOSE|ISPIPE)) == (FIFOCLOSE|ISPIPE)) { 566 fifo_cleanup(oldvp, flag); 567 cv_broadcast(&fnp->fn_wait_cv); 568 if (!lockheld) 569 mutex_exit(&fn_lock->flk_lock); 570 return (ENXIO); 571 } 572 573 fnp->fn_flag |= FIFOOPEN; 574 575 /* 576 * can't allow close to happen while we are 577 * in the middle of stropen(). 578 * M_HANGUP and M_ERROR could leave the stream in a strange state 579 */ 580 while (fn_lock->flk_ocsync) 581 cv_wait(&fn_lock->flk_wait_cv, &fn_lock->flk_lock); 582 583 fn_lock->flk_ocsync = 1; 584 585 if (fnp->fn_flag & FIFOCONNLD) { 586 /* 587 * This is a reopen, so we should release the fifo lock 588 * just in case some strange module pushed on connld 589 * has some odd side effect. 590 * Note: this stropen is on the oldvp. It will 591 * have no impact on the connld vp returned and 592 * strclose() will only be called when we release 593 * flk_ocsync 594 */ 595 mutex_exit(&fn_lock->flk_lock); 596 if ((error = stropen(oldvp, &pdev, flag, crp)) != 0) { 597 mutex_enter(&fn_lock->flk_lock); 598 fifo_cleanup(oldvp, flag); 599 fn_lock->flk_ocsync = 0; 600 cv_broadcast(&fn_lock->flk_wait_cv); 601 goto out; 602 } 603 /* 604 * streams open done, allow close on other end if 605 * required. Do this now.. it could 606 * be a very long time before fifo_connld returns. 607 */ 608 mutex_enter(&fn_lock->flk_lock); 609 /* 610 * we need to fake an open here so that if this 611 * end of the pipe closes, we don't loose the 612 * stream head (kind of like single threading 613 * open and close for this end of the pipe) 614 * We'll need to call fifo_close() to do clean 615 * up in case this end of the pipe was closed 616 * down while we were in fifo_connld() 617 */ 618 ASSERT(fnp->fn_open > 0); 619 fnp->fn_open++; 620 fn_lock->flk_ocsync = 0; 621 cv_broadcast(&fn_lock->flk_wait_cv); 622 mutex_exit(&fn_lock->flk_lock); 623 /* 624 * Connld has been pushed onto the pipe 625 * Create new pipe on behalf of connld 626 */ 627 if (error = fifo_connld(vpp, flag, crp)) { 628 (void) fifo_close(oldvp, flag, 1, 0, crp); 629 mutex_enter(&fn_lock->flk_lock); 630 goto out; 631 } 632 /* 633 * undo fake open. We need to call fifo_close 634 * because some other thread could have done 635 * a close and detach of the named pipe while 636 * we were in fifo_connld(), so 637 * we want to make sure the close completes (yuk) 638 */ 639 (void) fifo_close(oldvp, flag, 1, 0, crp); 640 /* 641 * fifo_connld has changed the vp, so we 642 * need to re-initialize locals 643 */ 644 fnp = VTOF(*vpp); 645 fn_lock = fnp->fn_lock; 646 mutex_enter(&fn_lock->flk_lock); 647 } else { 648 /* 649 * release lock in case there are modules pushed that 650 * could have some strange side effect 651 */ 652 653 mutex_exit(&fn_lock->flk_lock); 654 655 /* 656 * If this is the first open of a fifo (dotwist 657 * will be non-zero) we will need to twist the queues. 658 */ 659 if (oldvp->v_stream == NULL) 660 firstopen = 1; 661 662 663 /* 664 * normal open of pipe/fifo 665 */ 666 667 if ((error = stropen(oldvp, &pdev, flag, crp)) != 0) { 668 mutex_enter(&fn_lock->flk_lock); 669 fifo_cleanup(oldvp, flag); 670 ASSERT(fnp->fn_open != 0 || oldvp->v_stream == NULL); 671 fn_lock->flk_ocsync = 0; 672 cv_broadcast(&fn_lock->flk_wait_cv); 673 goto out; 674 } 675 mutex_enter(&fn_lock->flk_lock); 676 677 /* 678 * twist the ends of the fifo together 679 */ 680 if (dotwist && firstopen) 681 strmate(*vpp, *vpp); 682 683 /* 684 * Show that this open has succeeded 685 * and allow closes or other opens to proceed 686 */ 687 fnp->fn_open++; 688 fn_lock->flk_ocsync = 0; 689 cv_broadcast(&fn_lock->flk_wait_cv); 690 } 691 out: 692 fnp->fn_flag &= ~FIFOOPEN; 693 if (error == 0) { 694 fnp->fn_flag |= FIFOISOPEN; 695 /* 696 * If this is a FIFO and has the close flag set 697 * and there are now writers, clear the close flag 698 * Note: close flag only gets set when last writer 699 * on a FIFO goes away. 700 */ 701 if (((fnp->fn_flag & (ISPIPE|FIFOCLOSE)) == FIFOCLOSE) && 702 fnp->fn_wcnt > 0) 703 fnp->fn_flag &= ~FIFOCLOSE; 704 } 705 cv_broadcast(&fnp->fn_wait_cv); 706 if (!lockheld) 707 mutex_exit(&fn_lock->flk_lock); 708 return (error); 709 } 710 711 /* 712 * Clean up the state of a FIFO and/or mounted pipe in the 713 * event that a fifo_open() was interrupted while the 714 * process was blocked. 715 */ 716 void 717 fifo_cleanup(vnode_t *vp, int flag) 718 { 719 fifonode_t *fnp = VTOF(vp); 720 721 ASSERT(MUTEX_HELD(&fnp->fn_lock->flk_lock)); 722 723 cleanlocks(vp, curproc->p_pid, 0); 724 cleanshares(vp, curproc->p_pid); 725 if (flag & FREAD) { 726 fnp->fn_rcnt--; 727 } 728 if (flag & FWRITE) { 729 fnp->fn_wcnt--; 730 } 731 cv_broadcast(&fnp->fn_wait_cv); 732 } 733 734 735 /* 736 * Insert a fifonode-vnode pair onto the fifoalloc hash list. 737 */ 738 static void 739 fifoinsert(fifonode_t *fnp) 740 { 741 int idx = FIFOHASH(fnp->fn_realvp); 742 743 /* 744 * We don't need to hold fn_lock since we're holding ftable_lock and 745 * this routine is only called right after we've allocated an fnode. 746 * FIFO is inserted at head of NULL terminated doubly linked list. 747 */ 748 749 ASSERT(MUTEX_HELD(&ftable_lock)); 750 fnp->fn_backp = NULL; 751 fnp->fn_nextp = fifoalloc[idx]; 752 fifoalloc[idx] = fnp; 753 if (fnp->fn_nextp) 754 fnp->fn_nextp->fn_backp = fnp; 755 } 756 757 /* 758 * Find a fifonode-vnode pair on the fifoalloc hash list. 759 * vp is a vnode to be shadowed. If it's on the hash list, 760 * it already has a shadow, therefore return its corresponding 761 * fifonode. 762 */ 763 static fifonode_t * 764 fifofind(vnode_t *vp) 765 { 766 fifonode_t *fnode; 767 768 ASSERT(MUTEX_HELD(&ftable_lock)); 769 for (fnode = fifoalloc[FIFOHASH(vp)]; fnode; fnode = fnode->fn_nextp) { 770 if (fnode->fn_realvp == vp) { 771 VN_HOLD(FTOV(fnode)); 772 return (fnode); 773 } 774 } 775 return (NULL); 776 } 777 778 /* 779 * Remove a fifonode-vnode pair from the fifoalloc hash list. 780 * This routine is called from the fifo_inactive() routine when a 781 * FIFO is being released. 782 * If the link to be removed is the only link, set fifoalloc to NULL. 783 */ 784 void 785 fiforemove(fifonode_t *fnp) 786 { 787 int idx = FIFOHASH(fnp->fn_realvp); 788 fifonode_t *fnode; 789 790 ASSERT(MUTEX_HELD(&ftable_lock)); 791 fnode = fifoalloc[idx]; 792 /* 793 * fast path... only 1 FIFO in this list entry 794 */ 795 if (fnode != NULL && fnode == fnp && 796 !fnode->fn_nextp && !fnode->fn_backp) { 797 fifoalloc[idx] = NULL; 798 } else { 799 800 for (; fnode; fnode = fnode->fn_nextp) { 801 if (fnode == fnp) { 802 /* 803 * if we are first entry 804 */ 805 if (fnp == fifoalloc[idx]) 806 fifoalloc[idx] = fnp->fn_nextp; 807 if (fnode->fn_nextp) 808 fnode->fn_nextp->fn_backp = 809 fnode->fn_backp; 810 if (fnode->fn_backp) 811 fnode->fn_backp->fn_nextp = 812 fnode->fn_nextp; 813 break; 814 } 815 } 816 } 817 } 818 819 /* 820 * Flush all data from a fifo's message queue 821 */ 822 823 void 824 fifo_fastflush(fifonode_t *fnp) 825 { 826 mblk_t *bp; 827 ASSERT(MUTEX_HELD(&fnp->fn_lock->flk_lock)); 828 829 if ((bp = fnp->fn_mp) != NULL) { 830 fnp->fn_mp = NULL; 831 fnp->fn_count = 0; 832 freemsg(bp); 833 } 834 fifo_wakewriter(fnp->fn_dest, fnp->fn_lock); 835 } 836 837 /* 838 * Note: This routine is single threaded 839 * Protected by FIFOOPEN flag (i.e. flk_lock is not held) 840 * Upon successful completion, the original fifo is unlocked 841 * and FIFOOPEN is cleared for the original vpp. 842 * The new fifo returned has FIFOOPEN set. 843 */ 844 static int 845 fifo_connld(struct vnode **vpp, int flag, cred_t *crp) 846 { 847 struct vnode *vp1; 848 struct vnode *vp2; 849 struct fifonode *oldfnp; 850 struct fifonode *fn_dest; 851 int error; 852 struct file *filep; 853 struct fifolock *fn_lock; 854 cred_t *c; 855 856 /* 857 * Get two vnodes that will represent the pipe ends for the new pipe. 858 */ 859 makepipe(&vp1, &vp2); 860 861 /* 862 * Allocate a file descriptor and file pointer for one of the pipe 863 * ends. The file descriptor will be used to send that pipe end to 864 * the process on the other end of this stream. Note that we get 865 * the file structure only, there is no file list entry allocated. 866 */ 867 if (error = falloc(vp1, FWRITE|FREAD, &filep, NULL)) { 868 VN_RELE(vp1); 869 VN_RELE(vp2); 870 return (error); 871 } 872 mutex_exit(&filep->f_tlock); 873 oldfnp = VTOF(*vpp); 874 fn_lock = oldfnp->fn_lock; 875 fn_dest = oldfnp->fn_dest; 876 877 /* 878 * Create two new stream heads and attach them to the two vnodes for 879 * the new pipe. 880 */ 881 if ((error = fifo_stropen(&vp1, FREAD|FWRITE, filep->f_cred, 0, 0)) != 882 0 || 883 (error = fifo_stropen(&vp2, flag, filep->f_cred, 0, 0)) != 0) { 884 #if DEBUG 885 cmn_err(CE_NOTE, "fifo stropen failed error 0x%x", 886 error); 887 #endif 888 /* 889 * this will call fifo_close and VN_RELE on vp1 890 */ 891 (void) closef(filep); 892 VN_RELE(vp2); 893 return (error); 894 } 895 896 /* 897 * twist the ends of the pipe together 898 */ 899 strmate(vp1, vp2); 900 901 /* 902 * Set our end to busy in open 903 * Note: Don't need lock around this because we're the only 904 * one who knows about it 905 */ 906 VTOF(vp2)->fn_flag |= FIFOOPEN; 907 908 mutex_enter(&fn_lock->flk_lock); 909 910 fn_dest->fn_flag |= FIFOSEND; 911 /* 912 * check to make sure neither end of pipe has gone away 913 */ 914 if (!(fn_dest->fn_flag & FIFOISOPEN)) { 915 error = ENXIO; 916 fn_dest->fn_flag &= ~FIFOSEND; 917 mutex_exit(&fn_lock->flk_lock); 918 /* 919 * this will call fifo_close and VN_RELE on vp1 920 */ 921 goto out; 922 } 923 mutex_exit(&fn_lock->flk_lock); 924 925 /* 926 * Tag the sender's credential on the pipe descriptor. 927 */ 928 crhold(VTOF(vp1)->fn_pcredp = crp); 929 VTOF(vp1)->fn_cpid = curproc->p_pid; 930 931 /* 932 * send the file descriptor to other end of pipe 933 */ 934 if (error = do_sendfp((*vpp)->v_stream, filep, crp)) { 935 mutex_enter(&fn_lock->flk_lock); 936 fn_dest->fn_flag &= ~FIFOSEND; 937 mutex_exit(&fn_lock->flk_lock); 938 /* 939 * this will call fifo_close and VN_RELE on vp1 940 */ 941 goto out; 942 } 943 944 mutex_enter(&fn_lock->flk_lock); 945 /* 946 * Wait for other end to receive file descriptor 947 * FIFOCLOSE indicates that one or both sides of the pipe 948 * have gone away. 949 */ 950 while ((fn_dest->fn_flag & (FIFOCLOSE | FIFOSEND)) == FIFOSEND) { 951 if (!cv_wait_sig(&oldfnp->fn_wait_cv, &fn_lock->flk_lock)) { 952 error = EINTR; 953 fn_dest->fn_flag &= ~FIFOSEND; 954 mutex_exit(&fn_lock->flk_lock); 955 goto out; 956 } 957 } 958 /* 959 * If either end of pipe has gone away and the other end did not 960 * receive pipe, reject the connld open 961 */ 962 if ((fn_dest->fn_flag & FIFOSEND)) { 963 error = ENXIO; 964 fn_dest->fn_flag &= ~FIFOSEND; 965 mutex_exit(&fn_lock->flk_lock); 966 goto out; 967 } 968 969 oldfnp->fn_flag &= ~FIFOOPEN; 970 cv_broadcast(&oldfnp->fn_wait_cv); 971 mutex_exit(&fn_lock->flk_lock); 972 973 VN_RELE(*vpp); 974 *vpp = vp2; 975 (void) closef(filep); 976 return (0); 977 out: 978 c = filep->f_cred; 979 crhold(c); 980 (void) closef(filep); 981 VTOF(vp2)->fn_flag &= ~FIFOOPEN; 982 (void) fifo_close(vp2, flag, 1, (offset_t)0, c); 983 crfree(c); 984 VN_RELE(vp2); 985 return (error); 986 } 987 988 /* 989 * Disable fastpath mode. 990 */ 991 void 992 fifo_fastoff(fifonode_t *fnp) 993 { 994 ASSERT(MUTEX_HELD(&fnp->fn_lock->flk_lock)); 995 ASSERT(FTOV(fnp)->v_stream); 996 997 998 if (!(fnp->fn_flag & FIFOFAST)) 999 return; 1000 #if FIFODEBUG 1001 if (Fifo_verbose) 1002 cmn_err(CE_NOTE, "Fifo reverting to streams mode\n"); 1003 #endif 1004 1005 fifo_fastturnoff(fnp); 1006 if (fnp->fn_flag & ISPIPE) { 1007 fifo_fastturnoff(fnp->fn_dest); 1008 } 1009 } 1010 1011 1012 /* 1013 * flk_lock must be held while calling fifo_fastturnoff() to 1014 * preserve data ordering (no reads or writes allowed) 1015 */ 1016 1017 static void 1018 fifo_fastturnoff(fifonode_t *fnp) 1019 { 1020 fifonode_t *fn_dest = fnp->fn_dest; 1021 mblk_t *fn_mp; 1022 int fn_flag; 1023 1024 ASSERT(MUTEX_HELD(&fnp->fn_lock->flk_lock)); 1025 /* 1026 * Note: This end can't be closed if there 1027 * is stuff in fn_mp 1028 */ 1029 if ((fn_mp = fnp->fn_mp) != NULL) { 1030 ASSERT(fnp->fn_flag & FIFOISOPEN); 1031 ASSERT(FTOV(fnp)->v_stream != NULL); 1032 ASSERT(FTOV(fnp)->v_stream->sd_wrq != NULL); 1033 ASSERT(RD(FTOV(fnp)->v_stream->sd_wrq) != NULL); 1034 ASSERT(strvp2wq(FTOV(fnp)) != NULL); 1035 fnp->fn_mp = NULL; 1036 fnp->fn_count = 0; 1037 /* 1038 * Don't need to drop flk_lock across the put() 1039 * since we're just moving the message from the fifo 1040 * node to the STREAM head... 1041 */ 1042 put(RD(strvp2wq(FTOV(fnp))), fn_mp); 1043 } 1044 1045 /* 1046 * Need to re-issue any pending poll requests 1047 * so that the STREAMS framework sees them 1048 * Writers would be waiting on fnp and readers on fn_dest 1049 */ 1050 if ((fnp->fn_flag & (FIFOISOPEN | FIFOPOLLW)) == 1051 (FIFOISOPEN | FIFOPOLLW)) { 1052 strpollwakeup(FTOV(fnp), POLLWRNORM); 1053 } 1054 fn_flag = fn_dest->fn_flag; 1055 if ((fn_flag & FIFOISOPEN) == FIFOISOPEN) { 1056 if ((fn_flag & (FIFOPOLLR | FIFOPOLLRBAND))) { 1057 strpollwakeup(FTOV(fn_dest), POLLIN|POLLRDNORM); 1058 } 1059 } 1060 /* 1061 * wake up any sleeping processes so they can notice we went 1062 * to streams mode 1063 */ 1064 fnp->fn_flag &= ~(FIFOFAST|FIFOWANTW|FIFOWANTR); 1065 cv_broadcast(&fnp->fn_wait_cv); 1066 } 1067 1068 /* 1069 * Alternative version of fifo_fastoff() 1070 * optimized for putmsg/getmsg. 1071 */ 1072 void 1073 fifo_vfastoff(vnode_t *vp) 1074 { 1075 fifonode_t *fnp = VTOF(vp); 1076 1077 mutex_enter(&fnp->fn_lock->flk_lock); 1078 if (!(fnp->fn_flag & FIFOFAST)) { 1079 mutex_exit(&fnp->fn_lock->flk_lock); 1080 return; 1081 } 1082 fifo_fastoff(fnp); 1083 mutex_exit(&fnp->fn_lock->flk_lock); 1084 } 1085 1086 /* 1087 * Wake any sleeping writers, poll and send signals if necessary 1088 * This module is only called when we drop below the hi water mark 1089 * FIFOWANTW indicates that a process is sleeping in fifo_write() 1090 * FIFOHIWATW indicates that we have either attempted a poll or 1091 * non-blocking write and were over the high water mark 1092 * This routine assumes a low water mark of 0. 1093 */ 1094 1095 void 1096 fifo_wakewriter(fifonode_t *fn_dest, fifolock_t *fn_lock) 1097 { 1098 int fn_dflag = fn_dest->fn_flag; 1099 1100 ASSERT(MUTEX_HELD(&fn_lock->flk_lock)); 1101 ASSERT(fn_dest->fn_dest->fn_count < Fifohiwat); 1102 if ((fn_dflag & FIFOWANTW)) { 1103 cv_broadcast(&fn_dest->fn_wait_cv); 1104 } 1105 if ((fn_dflag & (FIFOHIWATW | FIFOISOPEN)) == 1106 (FIFOHIWATW | FIFOISOPEN)) { 1107 if (fn_dflag & FIFOPOLLW) 1108 strpollwakeup(FTOV(fn_dest), POLLWRNORM); 1109 if (fn_dflag & FIFOSETSIG) 1110 str_sendsig(FTOV(fn_dest), S_WRNORM, 0, 0); 1111 } 1112 /* 1113 * FIFOPOLLW can't be set without setting FIFOHIWAT 1114 * This allows us to clear both here. 1115 */ 1116 fn_dest->fn_flag = fn_dflag & ~(FIFOWANTW | FIFOHIWATW | FIFOPOLLW); 1117 } 1118 1119 /* 1120 * wake up any sleeping readers, poll or send signal if needed 1121 * FIFOWANTR indicates that a process is waiting in fifo_read() for data 1122 * FIFOSETSIG indicates that SIGPOLL should be sent to process 1123 * FIFOPOLLR indicates that a poll request for reading on the fifo was made 1124 */ 1125 1126 void 1127 fifo_wakereader(fifonode_t *fn_dest, fifolock_t *fn_lock) 1128 { 1129 int fn_dflag = fn_dest->fn_flag; 1130 1131 ASSERT(MUTEX_HELD(&fn_lock->flk_lock)); 1132 if (fn_dflag & FIFOWANTR) { 1133 cv_broadcast(&fn_dest->fn_wait_cv); 1134 } 1135 if (fn_dflag & FIFOISOPEN) { 1136 if (fn_dflag & FIFOPOLLR) 1137 strpollwakeup(FTOV(fn_dest), POLLIN | POLLRDNORM); 1138 if (fn_dflag & FIFOSETSIG) 1139 str_sendsig(FTOV(fn_dest), S_INPUT | S_RDNORM, 0, 0); 1140 } 1141 fn_dest->fn_flag = fn_dflag & ~(FIFOWANTR | FIFOPOLLR); 1142 } 1143