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 (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 22 /* All rights reserved. */ 23 24 25 /* 26 * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 27 * Use is subject to license terms. 28 */ 29 30 #pragma ident "%Z%%M% %I% %E% SMI" 31 32 /* 33 * FIFOFS file system vnode operations. This file system 34 * type supports STREAMS-based pipes and FIFOs. 35 */ 36 #include <sys/types.h> 37 #include <sys/param.h> 38 #include <sys/systm.h> 39 #include <sys/sysmacros.h> 40 #include <sys/cred.h> 41 #include <sys/errno.h> 42 #include <sys/time.h> 43 #include <sys/file.h> 44 #include <sys/fcntl.h> 45 #include <sys/kmem.h> 46 #include <sys/uio.h> 47 #include <sys/vfs.h> 48 #include <sys/vnode.h> 49 #include <sys/vfs_opreg.h> 50 #include <sys/pathname.h> 51 #include <sys/signal.h> 52 #include <sys/user.h> 53 #include <sys/strsubr.h> 54 #include <sys/stream.h> 55 #include <sys/strsun.h> 56 #include <sys/strredir.h> 57 #include <sys/fs/fifonode.h> 58 #include <sys/fs/namenode.h> 59 #include <sys/stropts.h> 60 #include <sys/proc.h> 61 #include <sys/unistd.h> 62 #include <sys/debug.h> 63 #include <fs/fs_subr.h> 64 #include <sys/filio.h> 65 #include <sys/termio.h> 66 #include <sys/ddi.h> 67 #include <sys/vtrace.h> 68 #include <sys/policy.h> 69 #include <sys/tsol/label.h> 70 71 /* 72 * Define the routines/data structures used in this file. 73 */ 74 static int fifo_read(vnode_t *, uio_t *, int, cred_t *, caller_context_t *); 75 static int fifo_write(vnode_t *, uio_t *, int, cred_t *, caller_context_t *); 76 static int fifo_getattr(vnode_t *, vattr_t *, int, cred_t *, 77 caller_context_t *); 78 static int fifo_setattr(vnode_t *, vattr_t *, int, cred_t *, 79 caller_context_t *); 80 static int fifo_realvp(vnode_t *, vnode_t **, caller_context_t *); 81 static int fifo_access(vnode_t *, int, int, cred_t *, caller_context_t *); 82 static int fifo_create(struct vnode *, char *, vattr_t *, enum vcexcl, 83 int, struct vnode **, struct cred *, int, caller_context_t *, 84 vsecattr_t *); 85 static int fifo_fid(vnode_t *, fid_t *, caller_context_t *); 86 static int fifo_fsync(vnode_t *, int, cred_t *, caller_context_t *); 87 static int fifo_seek(vnode_t *, offset_t, offset_t *, caller_context_t *); 88 static int fifo_ioctl(vnode_t *, int, intptr_t, int, cred_t *, int *, 89 caller_context_t *); 90 static int fifo_fastioctl(vnode_t *, int, intptr_t, int, cred_t *, int *); 91 static int fifo_strioctl(vnode_t *, int, intptr_t, int, cred_t *, int *); 92 static int fifo_poll(vnode_t *, short, int, short *, pollhead_t **, 93 caller_context_t *); 94 static int fifo_pathconf(vnode_t *, int, ulong_t *, cred_t *, 95 caller_context_t *); 96 static void fifo_inactive(vnode_t *, cred_t *, caller_context_t *); 97 static int fifo_rwlock(vnode_t *, int, caller_context_t *); 98 static void fifo_rwunlock(vnode_t *, int, caller_context_t *); 99 static int fifo_setsecattr(struct vnode *, vsecattr_t *, int, struct cred *, 100 caller_context_t *); 101 static int fifo_getsecattr(struct vnode *, vsecattr_t *, int, struct cred *, 102 caller_context_t *); 103 104 /* functions local to this file */ 105 static boolean_t fifo_stayfast_enter(fifonode_t *); 106 static void fifo_stayfast_exit(fifonode_t *); 107 108 /* 109 * Define the data structures external to this file. 110 */ 111 extern dev_t fifodev; 112 extern struct qinit fifo_stwdata; 113 extern struct qinit fifo_strdata; 114 extern kmutex_t ftable_lock; 115 116 struct streamtab fifoinfo = { &fifo_strdata, &fifo_stwdata, NULL, NULL }; 117 118 struct vnodeops *fifo_vnodeops; 119 120 const fs_operation_def_t fifo_vnodeops_template[] = { 121 VOPNAME_OPEN, { .vop_open = fifo_open }, 122 VOPNAME_CLOSE, { .vop_close = fifo_close }, 123 VOPNAME_READ, { .vop_read = fifo_read }, 124 VOPNAME_WRITE, { .vop_write = fifo_write }, 125 VOPNAME_IOCTL, { .vop_ioctl = fifo_ioctl }, 126 VOPNAME_GETATTR, { .vop_getattr = fifo_getattr }, 127 VOPNAME_SETATTR, { .vop_setattr = fifo_setattr }, 128 VOPNAME_ACCESS, { .vop_access = fifo_access }, 129 VOPNAME_CREATE, { .vop_create = fifo_create }, 130 VOPNAME_FSYNC, { .vop_fsync = fifo_fsync }, 131 VOPNAME_INACTIVE, { .vop_inactive = fifo_inactive }, 132 VOPNAME_FID, { .vop_fid = fifo_fid }, 133 VOPNAME_RWLOCK, { .vop_rwlock = fifo_rwlock }, 134 VOPNAME_RWUNLOCK, { .vop_rwunlock = fifo_rwunlock }, 135 VOPNAME_SEEK, { .vop_seek = fifo_seek }, 136 VOPNAME_REALVP, { .vop_realvp = fifo_realvp }, 137 VOPNAME_POLL, { .vop_poll = fifo_poll }, 138 VOPNAME_PATHCONF, { .vop_pathconf = fifo_pathconf }, 139 VOPNAME_DISPOSE, { .error = fs_error }, 140 VOPNAME_SETSECATTR, { .vop_setsecattr = fifo_setsecattr }, 141 VOPNAME_GETSECATTR, { .vop_getsecattr = fifo_getsecattr }, 142 NULL, NULL 143 }; 144 145 /* 146 * Return the fifoinfo structure. 147 */ 148 struct streamtab * 149 fifo_getinfo() 150 { 151 return (&fifoinfo); 152 } 153 154 /* 155 * Trusted Extensions enforces a restrictive policy for 156 * writing via cross-zone named pipes. A privileged global 157 * zone process may expose a named pipe by loopback mounting 158 * it from a lower-level zone to a higher-level zone. The 159 * kernel-enforced mount policy for lofs mounts ensures 160 * that such mounts are read-only in the higher-level 161 * zone. But this is not sufficient to prevent writing 162 * down via fifos. This function prevents writing down 163 * by comparing the zone of the process which is requesting 164 * write access with the zone owning the named pipe rendezvous. 165 * For write access the zone of the named pipe must equal the 166 * zone of the writing process. Writing up is possible since 167 * the named pipe can be opened for read by a process in a 168 * higher level zone. 169 * 170 * An exception is made for the global zone to support trusted 171 * processes which enforce their own data flow policies. 172 */ 173 static boolean_t 174 tsol_fifo_access(vnode_t *vp, int flag, cred_t *crp) 175 { 176 fifonode_t *fnp = VTOF(vp); 177 178 if (is_system_labeled() && 179 (flag & FWRITE) && 180 (!(fnp->fn_flag & ISPIPE))) { 181 zone_t *proc_zone; 182 183 proc_zone = crgetzone(crp); 184 if (proc_zone != global_zone) { 185 char vpath[MAXPATHLEN]; 186 zone_t *fifo_zone; 187 188 /* 189 * Get the pathname and use it to find 190 * the zone of the fifo. 191 */ 192 if (vnodetopath(rootdir, vp, vpath, sizeof (vpath), 193 kcred) == 0) { 194 fifo_zone = zone_find_by_path(vpath); 195 zone_rele(fifo_zone); 196 197 if (fifo_zone != global_zone && 198 fifo_zone != proc_zone) { 199 return (B_FALSE); 200 } 201 } else { 202 return (B_FALSE); 203 } 204 } 205 } 206 return (B_TRUE); 207 } 208 209 /* 210 * Open and stream a FIFO. 211 * If this is the first open of the file (FIFO is not streaming), 212 * initialize the fifonode and attach a stream to the vnode. 213 * 214 * Each end of a fifo must be synchronized with the other end. 215 * If not, the mated end may complete an open, I/O, close sequence 216 * before the end waiting in open ever wakes up. 217 * Note: namefs pipes come through this routine too. 218 */ 219 int 220 fifo_open(vnode_t **vpp, int flag, cred_t *crp, caller_context_t *ct) 221 { 222 vnode_t *vp = *vpp; 223 fifonode_t *fnp = VTOF(vp); 224 fifolock_t *fn_lock = fnp->fn_lock; 225 int error; 226 227 ASSERT(vp->v_type == VFIFO); 228 ASSERT(vn_matchops(vp, fifo_vnodeops)); 229 230 if (!tsol_fifo_access(vp, flag, crp)) 231 return (EACCES); 232 233 mutex_enter(&fn_lock->flk_lock); 234 /* 235 * If we are the first reader, wake up any writers that 236 * may be waiting around. wait for all of them to 237 * wake up before proceeding (i.e. fn_wsynccnt == 0) 238 */ 239 if (flag & FREAD) { 240 fnp->fn_rcnt++; /* record reader present */ 241 if (! (fnp->fn_flag & ISPIPE)) 242 fnp->fn_rsynccnt++; /* record reader in open */ 243 } 244 245 /* 246 * If we are the first writer, wake up any readers that 247 * may be waiting around. wait for all of them to 248 * wake up before proceeding (i.e. fn_rsynccnt == 0) 249 */ 250 if (flag & FWRITE) { 251 fnp->fn_wcnt++; /* record writer present */ 252 if (! (fnp->fn_flag & ISPIPE)) 253 fnp->fn_wsynccnt++; /* record writer in open */ 254 } 255 /* 256 * fifo_stropen will take care of twisting the queues on the first 257 * open. The 1 being passed in means twist the queues on the first 258 * open. 259 */ 260 error = fifo_stropen(vpp, flag, crp, 1, 1); 261 /* 262 * fifo_stropen() could have replaced vpp 263 * since fifo's are the only thing we need to sync up, 264 * everything else just returns; 265 * Note: don't need to hold lock since ISPIPE can't change 266 * and both old and new vp need to be pipes 267 */ 268 ASSERT(MUTEX_HELD(&VTOF(*vpp)->fn_lock->flk_lock)); 269 if (fnp->fn_flag & ISPIPE) { 270 ASSERT(VTOF(*vpp)->fn_flag & ISPIPE); 271 ASSERT(VTOF(*vpp)->fn_rsynccnt == 0); 272 ASSERT(VTOF(*vpp)->fn_rsynccnt == 0); 273 /* 274 * XXX note: should probably hold locks, but 275 * These values should not be changing 276 */ 277 ASSERT(fnp->fn_rsynccnt == 0); 278 ASSERT(fnp->fn_wsynccnt == 0); 279 mutex_exit(&VTOF(*vpp)->fn_lock->flk_lock); 280 return (error); 281 } 282 /* 283 * vp can't change for FIFOS 284 */ 285 ASSERT(vp == *vpp); 286 /* 287 * If we are opening for read (or writer) 288 * indicate that the reader (or writer) is done with open 289 * if there is a writer (or reader) waiting for us, wake them up 290 * and indicate that at least 1 read (or write) open has occurred 291 * this is need in the event the read (or write) side closes 292 * before the writer (or reader) has a chance to wake up 293 * i.e. it sees that a reader (or writer) was once there 294 */ 295 if (flag & FREAD) { 296 fnp->fn_rsynccnt--; /* reader done with open */ 297 if (fnp->fn_flag & FIFOSYNC) { 298 /* 299 * This indicates that a read open has occurred 300 * Only need to set if writer is actually asleep 301 * Flag will be consumed by writer. 302 */ 303 fnp->fn_flag |= FIFOROCR; 304 cv_broadcast(&fnp->fn_wait_cv); 305 } 306 } 307 if (flag & FWRITE) { 308 fnp->fn_wsynccnt--; /* writer done with open */ 309 if (fnp->fn_flag & FIFOSYNC) { 310 /* 311 * This indicates that a write open has occurred 312 * Only need to set if reader is actually asleep 313 * Flag will be consumed by reader. 314 */ 315 fnp->fn_flag |= FIFOWOCR; 316 cv_broadcast(&fnp->fn_wait_cv); 317 } 318 } 319 320 fnp->fn_flag &= ~FIFOSYNC; 321 322 /* 323 * errors don't wait around.. just return 324 * Note: XXX other end will wake up and continue despite error. 325 * There is no defined semantic on the correct course of option 326 * so we do what we've done in the past 327 */ 328 if (error != 0) { 329 mutex_exit(&fnp->fn_lock->flk_lock); 330 goto done; 331 } 332 ASSERT(fnp->fn_rsynccnt <= fnp->fn_rcnt); 333 ASSERT(fnp->fn_wsynccnt <= fnp->fn_wcnt); 334 /* 335 * FIFOWOCR (or FIFOROCR) indicates that the writer (or reader) 336 * has woken us up and is done with open (this way, if the other 337 * end has made it to close, we don't block forever in open) 338 * fn_wnct == fn_wsynccnt (or fn_rcnt == fn_rsynccnt) indicates 339 * that no writer (or reader) has yet made it through open 340 * This has the side benifit of that the first 341 * reader (or writer) will wait until the other end finishes open 342 */ 343 if (flag & FREAD) { 344 while ((fnp->fn_flag & FIFOWOCR) == 0 && 345 fnp->fn_wcnt == fnp->fn_wsynccnt) { 346 if (flag & (FNDELAY|FNONBLOCK)) { 347 mutex_exit(&fnp->fn_lock->flk_lock); 348 goto done; 349 } 350 fnp->fn_insync++; 351 fnp->fn_flag |= FIFOSYNC; 352 if (!cv_wait_sig_swap(&fnp->fn_wait_cv, 353 &fnp->fn_lock->flk_lock)) { 354 /* 355 * Last reader to wakeup clear writer 356 * Clear both writer and reader open 357 * occurred flag incase other end is O_RDWR 358 */ 359 if (--fnp->fn_insync == 0 && 360 fnp->fn_flag & FIFOWOCR) { 361 fnp->fn_flag &= ~(FIFOWOCR|FIFOROCR); 362 } 363 mutex_exit(&fnp->fn_lock->flk_lock); 364 (void) fifo_close(*vpp, flag, 1, 0, crp, ct); 365 error = EINTR; 366 goto done; 367 } 368 /* 369 * Last reader to wakeup clear writer open occurred flag 370 * Clear both writer and reader open occurred flag 371 * incase other end is O_RDWR 372 */ 373 if (--fnp->fn_insync == 0 && 374 fnp->fn_flag & FIFOWOCR) { 375 fnp->fn_flag &= ~(FIFOWOCR|FIFOROCR); 376 break; 377 } 378 } 379 } else if (flag & FWRITE) { 380 while ((fnp->fn_flag & FIFOROCR) == 0 && 381 fnp->fn_rcnt == fnp->fn_rsynccnt) { 382 if ((flag & (FNDELAY|FNONBLOCK)) && fnp->fn_rcnt == 0) { 383 mutex_exit(&fnp->fn_lock->flk_lock); 384 (void) fifo_close(*vpp, flag, 1, 0, crp, ct); 385 error = ENXIO; 386 goto done; 387 } 388 fnp->fn_flag |= FIFOSYNC; 389 fnp->fn_insync++; 390 if (!cv_wait_sig_swap(&fnp->fn_wait_cv, 391 &fnp->fn_lock->flk_lock)) { 392 /* 393 * Last writer to wakeup clear 394 * Clear both writer and reader open 395 * occurred flag in case other end is O_RDWR 396 */ 397 if (--fnp->fn_insync == 0 && 398 (fnp->fn_flag & FIFOROCR) != 0) { 399 fnp->fn_flag &= ~(FIFOWOCR|FIFOROCR); 400 } 401 mutex_exit(&fnp->fn_lock->flk_lock); 402 (void) fifo_close(*vpp, flag, 1, 0, crp, ct); 403 error = EINTR; 404 goto done; 405 } 406 /* 407 * Last writer to wakeup clear reader open occurred flag 408 * Clear both writer and reader open 409 * occurred flag in case other end is O_RDWR 410 */ 411 if (--fnp->fn_insync == 0 && 412 (fnp->fn_flag & FIFOROCR) != 0) { 413 fnp->fn_flag &= ~(FIFOWOCR|FIFOROCR); 414 break; 415 } 416 } 417 } 418 mutex_exit(&fn_lock->flk_lock); 419 done: 420 return (error); 421 } 422 423 /* 424 * Close down a stream. 425 * Call cleanlocks() and strclean() on every close. 426 * For last close send hangup message and force 427 * the other end of a named pipe to be unmounted. 428 * Mount guarantees that the mounted end will only call fifo_close() 429 * with a count of 1 when the unmount occurs. 430 * This routine will close down one end of a pipe or FIFO 431 * and free the stream head via strclose() 432 */ 433 /*ARGSUSED*/ 434 int 435 fifo_close(vnode_t *vp, int flag, int count, offset_t offset, cred_t *crp, 436 caller_context_t *ct) 437 { 438 fifonode_t *fnp = VTOF(vp); 439 fifonode_t *fn_dest = fnp->fn_dest; 440 int error = 0; 441 fifolock_t *fn_lock = fnp->fn_lock; 442 queue_t *sd_wrq; 443 vnode_t *fn_dest_vp; 444 int senthang = 0; 445 446 ASSERT(vp->v_stream != NULL); 447 /* 448 * clean locks and clear events. 449 */ 450 (void) cleanlocks(vp, ttoproc(curthread)->p_pid, 0); 451 cleanshares(vp, ttoproc(curthread)->p_pid); 452 strclean(vp); 453 454 /* 455 * If a file still has the pipe/FIFO open, return. 456 */ 457 if (count > 1) 458 return (0); 459 460 461 sd_wrq = strvp2wq(vp); 462 mutex_enter(&fn_lock->flk_lock); 463 464 /* 465 * wait for pending opens to finish up 466 * note: this also has the side effect of single threading closes 467 */ 468 while (fn_lock->flk_ocsync) 469 cv_wait(&fn_lock->flk_wait_cv, &fn_lock->flk_lock); 470 471 fn_lock->flk_ocsync = 1; 472 473 if (flag & FREAD) { 474 fnp->fn_rcnt--; 475 } 476 /* 477 * If we are last writer wake up sleeping readers 478 * (They'll figure out that there are no more writers 479 * and do the right thing) 480 * send hangup down stream so that stream head will do the 481 * right thing. 482 */ 483 if (flag & FWRITE) { 484 if (--fnp->fn_wcnt == 0 && fn_dest->fn_rcnt > 0) { 485 if ((fn_dest->fn_flag & (FIFOFAST | FIFOWANTR)) == 486 (FIFOFAST | FIFOWANTR)) { 487 /* 488 * While we're at it, clear FIFOWANTW too 489 * Wake up any sleeping readers or 490 * writers. 491 */ 492 fn_dest->fn_flag &= ~(FIFOWANTR | FIFOWANTW); 493 cv_broadcast(&fn_dest->fn_wait_cv); 494 } 495 /* 496 * This is needed incase the other side 497 * was opened non-blocking. It is the 498 * only way we can tell that wcnt is 0 because 499 * of close instead of never having a writer 500 */ 501 if (!(fnp->fn_flag & ISPIPE)) 502 fnp->fn_flag |= FIFOCLOSE; 503 /* 504 * Note: sending hangup effectively shuts down 505 * both reader and writer at other end. 506 */ 507 (void) putnextctl_wait(sd_wrq, M_HANGUP); 508 senthang = 1; 509 } 510 } 511 512 /* 513 * For FIFOs we need to indicate to stream head that last reader 514 * has gone away so that an error is generated 515 * Pipes just need to wake up the other end so that it can 516 * notice this end has gone away. 517 */ 518 519 if (fnp->fn_rcnt == 0 && fn_dest->fn_wcnt > 0) { 520 if ((fn_dest->fn_flag & (FIFOFAST | FIFOWANTW)) == 521 (FIFOFAST | FIFOWANTW)) { 522 /* 523 * wake up any sleeping writers 524 */ 525 fn_dest->fn_flag &= ~FIFOWANTW; 526 cv_broadcast(&fn_dest->fn_wait_cv); 527 } 528 } 529 530 /* 531 * if there are still processes with this FIFO open 532 * clear open/close sync flag 533 * and just return; 534 */ 535 if (--fnp->fn_open > 0) { 536 ASSERT((fnp->fn_rcnt + fnp->fn_wcnt) != 0); 537 fn_lock->flk_ocsync = 0; 538 cv_broadcast(&fn_lock->flk_wait_cv); 539 mutex_exit(&fn_lock->flk_lock); 540 return (0); 541 } 542 543 /* 544 * Need to send HANGUP if other side is still open 545 * (fnp->fn_rcnt or fnp->fn_wcnt may not be zero (some thread 546 * on this end of the pipe may still be in fifo_open()) 547 * 548 * Note: we can get here with fn_rcnt and fn_wcnt != 0 if some 549 * thread is blocked somewhere in the fifo_open() path prior to 550 * fifo_stropen() incrementing fn_open. This can occur for 551 * normal FIFOs as well as named pipes. fn_rcnt and 552 * fn_wcnt only indicate attempts to open. fn_open indicates 553 * successful opens. Partially opened FIFOs should proceed 554 * normally; i.e. they will appear to be new opens. Partially 555 * opened pipes will probably fail. 556 */ 557 558 if (fn_dest->fn_open && senthang == 0) 559 (void) putnextctl_wait(sd_wrq, M_HANGUP); 560 561 562 /* 563 * If this a pipe and this is the first end to close, 564 * then we have a bit of cleanup work to do. 565 * Mark both ends of pipe as closed. 566 * Wake up anybody blocked at the other end and for named pipes, 567 * Close down this end of the stream 568 * Allow other opens/closes to continue 569 * force an unmount of other end. 570 * Otherwise if this is last close, 571 * flush messages, 572 * close down the stream 573 * allow other opens/closes to continue 574 */ 575 fnp->fn_flag &= ~FIFOISOPEN; 576 if ((fnp->fn_flag & ISPIPE) && !(fnp->fn_flag & FIFOCLOSE)) { 577 fnp->fn_flag |= FIFOCLOSE; 578 fn_dest->fn_flag |= FIFOCLOSE; 579 if (fnp->fn_flag & FIFOFAST) 580 fifo_fastflush(fnp); 581 if (vp->v_stream != NULL) { 582 mutex_exit(&fn_lock->flk_lock); 583 (void) strclose(vp, flag, crp); 584 mutex_enter(&fn_lock->flk_lock); 585 } 586 cv_broadcast(&fn_dest->fn_wait_cv); 587 /* 588 * allow opens and closes to proceed 589 * Since this end is now closed down, any attempt 590 * to do anything with this end will fail 591 */ 592 fn_lock->flk_ocsync = 0; 593 cv_broadcast(&fn_lock->flk_wait_cv); 594 fn_dest_vp = FTOV(fn_dest); 595 /* 596 * if other end of pipe has been opened and it's 597 * a named pipe, unmount it 598 */ 599 if (fn_dest_vp->v_stream && 600 (fn_dest_vp->v_stream->sd_flag & STRMOUNT)) { 601 /* 602 * We must hold the destination vnode because 603 * nm_unmountall() causes close to be called 604 * for the other end of named pipe. This 605 * could free the vnode before we are ready. 606 */ 607 VN_HOLD(fn_dest_vp); 608 mutex_exit(&fn_lock->flk_lock); 609 error = nm_unmountall(fn_dest_vp, crp); 610 ASSERT(error == 0); 611 VN_RELE(fn_dest_vp); 612 } else { 613 ASSERT(vp->v_count >= 1); 614 mutex_exit(&fn_lock->flk_lock); 615 } 616 } else { 617 if (fnp->fn_flag & FIFOFAST) 618 fifo_fastflush(fnp); 619 #if DEBUG 620 fn_dest_vp = FTOV(fn_dest); 621 if (fn_dest_vp->v_stream) 622 ASSERT((fn_dest_vp->v_stream->sd_flag & STRMOUNT) == 0); 623 #endif 624 if (vp->v_stream != NULL) { 625 mutex_exit(&fn_lock->flk_lock); 626 (void) strclose(vp, flag, crp); 627 mutex_enter(&fn_lock->flk_lock); 628 } 629 fn_lock->flk_ocsync = 0; 630 cv_broadcast(&fn_lock->flk_wait_cv); 631 cv_broadcast(&fn_dest->fn_wait_cv); 632 mutex_exit(&fn_lock->flk_lock); 633 } 634 return (error); 635 } 636 637 /* 638 * Read from a pipe or FIFO. 639 * return 0 if.... 640 * (1) user read request is 0 or no stream 641 * (2) broken pipe with no data 642 * (3) write-only FIFO with no data 643 * (4) no data and FNDELAY flag is set. 644 * Otherwise return 645 * EAGAIN if FNONBLOCK is set and no data to read 646 * EINTR if signal recieved while waiting for data 647 * 648 * While there is no data to read.... 649 * - if the NDELAY/NONBLOCK flag is set, return 0/EAGAIN. 650 * - wait for a write. 651 * 652 */ 653 /*ARGSUSED*/ 654 655 static int 656 fifo_read(struct vnode *vp, struct uio *uiop, int ioflag, struct cred *crp, 657 caller_context_t *ct) 658 { 659 fifonode_t *fnp = VTOF(vp); 660 fifonode_t *fn_dest; 661 fifolock_t *fn_lock = fnp->fn_lock; 662 int error = 0; 663 mblk_t *bp; 664 665 ASSERT(vp->v_stream != NULL); 666 if (uiop->uio_resid == 0) 667 return (0); 668 669 mutex_enter(&fn_lock->flk_lock); 670 671 TRACE_2(TR_FAC_FIFO, 672 TR_FIFOREAD_IN, "fifo_read in:%p fnp %p", vp, fnp); 673 674 if (! (fnp->fn_flag & FIFOFAST)) 675 goto stream_mode; 676 677 fn_dest = fnp->fn_dest; 678 /* 679 * Check for data on our input queue 680 */ 681 682 while (fnp->fn_count == 0) { 683 /* 684 * No data on first attempt and no writer, then EOF 685 */ 686 if (fn_dest->fn_wcnt == 0 || fn_dest->fn_rcnt == 0) { 687 mutex_exit(&fn_lock->flk_lock); 688 return (0); 689 } 690 /* 691 * no data found.. if non-blocking, return EAGAIN 692 * otherwise 0. 693 */ 694 if (uiop->uio_fmode & (FNDELAY|FNONBLOCK)) { 695 mutex_exit(&fn_lock->flk_lock); 696 if (uiop->uio_fmode & FNONBLOCK) 697 return (EAGAIN); 698 return (0); 699 } 700 701 /* 702 * Note: FIFOs can get here with FIFOCLOSE set if 703 * write side is in the middle of opeining after 704 * it once closed. Pipes better not have FIFOCLOSE set 705 */ 706 ASSERT((fnp->fn_flag & (ISPIPE|FIFOCLOSE)) != 707 (ISPIPE|FIFOCLOSE)); 708 /* 709 * wait for data 710 */ 711 fnp->fn_flag |= FIFOWANTR; 712 713 TRACE_1(TR_FAC_FIFO, TR_FIFOREAD_WAIT, 714 "fiforead wait: %p", vp); 715 716 if (!cv_wait_sig_swap(&fnp->fn_wait_cv, 717 &fn_lock->flk_lock)) { 718 error = EINTR; 719 goto done; 720 } 721 722 TRACE_1(TR_FAC_FIFO, TR_FIFOREAD_WAKE, 723 "fiforead awake: %p", vp); 724 725 /* 726 * check to make sure we are still in fast mode 727 */ 728 if (!(fnp->fn_flag & FIFOFAST)) 729 goto stream_mode; 730 } 731 732 ASSERT(fnp->fn_mp != NULL); 733 734 /* For pipes copy should not bypass cache */ 735 uiop->uio_extflg |= UIO_COPY_CACHED; 736 737 do { 738 int bpsize = MBLKL(fnp->fn_mp); 739 int uiosize = MIN(bpsize, uiop->uio_resid); 740 741 error = uiomove(fnp->fn_mp->b_rptr, uiosize, UIO_READ, uiop); 742 if (error != 0) 743 break; 744 745 fnp->fn_count -= uiosize; 746 747 if (bpsize <= uiosize) { 748 bp = fnp->fn_mp; 749 fnp->fn_mp = fnp->fn_mp->b_cont; 750 freeb(bp); 751 752 if (uiop->uio_resid == 0) 753 break; 754 755 while (fnp->fn_mp == NULL && fn_dest->fn_wwaitcnt > 0) { 756 ASSERT(fnp->fn_count == 0); 757 758 if (uiop->uio_fmode & (FNDELAY|FNONBLOCK)) 759 goto trywake; 760 761 /* 762 * We've consumed all available data but there 763 * are threads waiting to write more, let them 764 * proceed before bailing. 765 */ 766 767 fnp->fn_flag |= FIFOWANTR; 768 fifo_wakewriter(fn_dest, fn_lock); 769 770 if (!cv_wait_sig(&fnp->fn_wait_cv, 771 &fn_lock->flk_lock)) 772 goto trywake; 773 774 if (!(fnp->fn_flag & FIFOFAST)) 775 goto stream_mode; 776 } 777 } else { 778 fnp->fn_mp->b_rptr += uiosize; 779 ASSERT(uiop->uio_resid == 0); 780 } 781 } while (uiop->uio_resid != 0 && fnp->fn_mp != NULL); 782 783 trywake: 784 ASSERT(msgdsize(fnp->fn_mp) == fnp->fn_count); 785 786 /* 787 * wake up any blocked writers, processes 788 * sleeping on POLLWRNORM, or processes waiting for SIGPOLL 789 * Note: checking for fn_count < Fifohiwat emulates 790 * STREAMS functionality when low water mark is 0 791 */ 792 if (fn_dest->fn_flag & (FIFOWANTW | FIFOHIWATW) && 793 fnp->fn_count < Fifohiwat) { 794 fifo_wakewriter(fn_dest, fn_lock); 795 } 796 goto done; 797 798 /* 799 * FIFO is in streams mode.. let the stream head handle it 800 */ 801 stream_mode: 802 803 mutex_exit(&fn_lock->flk_lock); 804 TRACE_1(TR_FAC_FIFO, 805 TR_FIFOREAD_STREAM, "fifo_read stream_mode:%p", vp); 806 807 error = strread(vp, uiop, crp); 808 809 mutex_enter(&fn_lock->flk_lock); 810 811 done: 812 /* 813 * vnode update access time 814 */ 815 if (error == 0) { 816 time_t now = gethrestime_sec(); 817 818 if (fnp->fn_flag & ISPIPE) 819 fnp->fn_dest->fn_atime = now; 820 fnp->fn_atime = now; 821 } 822 TRACE_2(TR_FAC_FIFO, 823 TR_FIFOREAD_OUT, "fifo_read out:%p error %d", 824 vp, error); 825 mutex_exit(&fn_lock->flk_lock); 826 return (error); 827 } 828 829 /* 830 * send SIGPIPE and return EPIPE if ... 831 * (1) broken pipe (essentially, reader is gone) 832 * (2) FIFO is not open for reading 833 * return 0 if... 834 * (1) no stream 835 * (2) user write request is for 0 bytes and SW_SNDZERO is not set 836 * Note: SW_SNDZERO can't be set in fast mode 837 * While the stream is flow controlled.... 838 * - if the NDELAY/NONBLOCK flag is set, return 0/EAGAIN. 839 * - unlock the fifonode and sleep waiting for a reader. 840 * - if a pipe and it has a mate, sleep waiting for its mate 841 * to read. 842 */ 843 /*ARGSUSED*/ 844 static int 845 fifo_write(vnode_t *vp, uio_t *uiop, int ioflag, cred_t *crp, 846 caller_context_t *ct) 847 { 848 struct fifonode *fnp, *fn_dest; 849 fifolock_t *fn_lock; 850 struct stdata *stp; 851 int error = 0; 852 int write_size; 853 int size; 854 int fmode; 855 mblk_t *bp; 856 boolean_t hotread; 857 858 ASSERT(vp->v_stream); 859 uiop->uio_loffset = 0; 860 stp = vp->v_stream; 861 862 /* 863 * remember original number of bytes requested. Used to determine if 864 * we actually have written anything at all 865 */ 866 write_size = uiop->uio_resid; 867 868 /* 869 * only send zero-length messages if SW_SNDZERO is set 870 * Note: we will be in streams mode if SW_SNDZERO is set 871 * XXX this streams interface should not be exposed 872 */ 873 if ((write_size == 0) && !(stp->sd_wput_opt & SW_SNDZERO)) 874 return (0); 875 876 fnp = VTOF(vp); 877 fn_lock = fnp->fn_lock; 878 fn_dest = fnp->fn_dest; 879 880 mutex_enter(&fn_lock->flk_lock); 881 882 TRACE_3(TR_FAC_FIFO, 883 TR_FIFOWRITE_IN, "fifo_write in:%p fnp %p size %d", 884 vp, fnp, write_size); 885 886 /* 887 * oops, no readers, error 888 */ 889 if (fn_dest->fn_rcnt == 0 || fn_dest->fn_wcnt == 0) { 890 goto epipe; 891 } 892 893 /* 894 * if we are not in fast mode, let streams handle it 895 */ 896 if (!(fnp->fn_flag & FIFOFAST)) 897 goto stream_mode; 898 899 fmode = uiop->uio_fmode & (FNDELAY|FNONBLOCK); 900 901 /* For pipes copy should not bypass cache */ 902 uiop->uio_extflg |= UIO_COPY_CACHED; 903 904 do { 905 /* 906 * check to make sure we are not over high water mark 907 */ 908 while (fn_dest->fn_count >= Fifohiwat) { 909 /* 910 * Indicate that we have gone over high 911 * water mark 912 */ 913 /* 914 * if non-blocking, return 915 * only happens first time through loop 916 */ 917 if (fmode) { 918 fnp->fn_flag |= FIFOHIWATW; 919 if (uiop->uio_resid == write_size) { 920 mutex_exit(&fn_lock->flk_lock); 921 if (fmode & FNDELAY) 922 return (0); 923 else 924 return (EAGAIN); 925 } 926 goto done; 927 } 928 929 /* 930 * wait for things to drain 931 */ 932 fnp->fn_flag |= FIFOWANTW; 933 fnp->fn_wwaitcnt++; 934 TRACE_1(TR_FAC_FIFO, TR_FIFOWRITE_WAIT, 935 "fifo_write wait: %p", vp); 936 if (!cv_wait_sig_swap(&fnp->fn_wait_cv, 937 &fn_lock->flk_lock)) { 938 error = EINTR; 939 fnp->fn_wwaitcnt--; 940 fifo_wakereader(fn_dest, fn_lock); 941 goto done; 942 } 943 fnp->fn_wwaitcnt--; 944 945 TRACE_1(TR_FAC_FIFO, TR_FIFOWRITE_WAKE, 946 "fifo_write wake: %p", vp); 947 948 /* 949 * check to make sure we're still in fast mode 950 */ 951 if (!(fnp->fn_flag & FIFOFAST)) 952 goto stream_mode; 953 954 /* 955 * make sure readers didn't go away 956 */ 957 if (fn_dest->fn_rcnt == 0 || fn_dest->fn_wcnt == 0) { 958 goto epipe; 959 } 960 } 961 /* 962 * If the write will put us over the high water mark, 963 * then we must break the message up into PIPE_BUF 964 * chunks to stay compliant with STREAMS 965 */ 966 if (uiop->uio_resid + fn_dest->fn_count > Fifohiwat) 967 size = MIN(uiop->uio_resid, PIPE_BUF); 968 else 969 size = uiop->uio_resid; 970 971 /* 972 * We don't need to hold flk_lock across the allocb() and 973 * uiomove(). However, on a multiprocessor machine where both 974 * the reader and writer thread are on cpu's, we must be 975 * careful to only drop the lock if there's data to be read. 976 * This forces threads entering fifo_read() to spin or block 977 * on flk_lock, rather than acquiring flk_lock only to 978 * discover there's no data to read and being forced to go 979 * back to sleep, only to be woken up microseconds later by 980 * this writer thread. 981 */ 982 hotread = fn_dest->fn_count > 0; 983 if (hotread) { 984 if (!fifo_stayfast_enter(fnp)) 985 goto stream_mode; 986 mutex_exit(&fn_lock->flk_lock); 987 } 988 989 ASSERT(size != 0); 990 /* 991 * Align the mblk with the user data so that 992 * copying in the data can take advantage of 993 * the double word alignment 994 */ 995 if ((bp = allocb(size + 8, BPRI_MED)) == NULL) { 996 if (!hotread) 997 mutex_exit(&fn_lock->flk_lock); 998 999 error = strwaitbuf(size, BPRI_MED); 1000 1001 mutex_enter(&fn_lock->flk_lock); 1002 1003 if (hotread) { 1004 /* 1005 * As we dropped the mutex for a moment, we 1006 * need to wake up any thread waiting to be 1007 * allowed to go from fast mode to stream mode. 1008 */ 1009 fifo_stayfast_exit(fnp); 1010 } 1011 if (error != 0) { 1012 goto done; 1013 } 1014 /* 1015 * check to make sure we're still in fast mode 1016 */ 1017 if (!(fnp->fn_flag & FIFOFAST)) 1018 goto stream_mode; 1019 1020 /* 1021 * make sure readers didn't go away 1022 */ 1023 if (fn_dest->fn_rcnt == 0 || fn_dest->fn_wcnt == 0) { 1024 goto epipe; 1025 } 1026 /* 1027 * some other thread could have gotten in 1028 * need to go back and check hi water mark 1029 */ 1030 continue; 1031 } 1032 bp->b_rptr += ((uintptr_t)uiop->uio_iov->iov_base & 0x7); 1033 bp->b_wptr = bp->b_rptr + size; 1034 error = uiomove((caddr_t)bp->b_rptr, size, UIO_WRITE, uiop); 1035 if (hotread) { 1036 mutex_enter(&fn_lock->flk_lock); 1037 /* 1038 * As we dropped the mutex for a moment, we need to: 1039 * - wake up any thread waiting to be allowed to go 1040 * from fast mode to stream mode, 1041 * - make sure readers didn't go away. 1042 */ 1043 fifo_stayfast_exit(fnp); 1044 if (fn_dest->fn_rcnt == 0 || fn_dest->fn_wcnt == 0) { 1045 freeb(bp); 1046 goto epipe; 1047 } 1048 } 1049 1050 if (error != 0) { 1051 freeb(bp); 1052 goto done; 1053 } 1054 1055 fn_dest->fn_count += size; 1056 if (fn_dest->fn_mp != NULL) { 1057 fn_dest->fn_tail->b_cont = bp; 1058 fn_dest->fn_tail = bp; 1059 } else { 1060 fn_dest->fn_mp = fn_dest->fn_tail = bp; 1061 /* 1062 * This is the first bit of data; wake up any sleeping 1063 * readers, processes blocked in poll, and those 1064 * expecting a SIGPOLL. 1065 */ 1066 fifo_wakereader(fn_dest, fn_lock); 1067 } 1068 } while (uiop->uio_resid != 0); 1069 1070 goto done; 1071 1072 stream_mode: 1073 /* 1074 * streams mode 1075 * let the stream head handle the write 1076 */ 1077 ASSERT(MUTEX_HELD(&fn_lock->flk_lock)); 1078 1079 mutex_exit(&fn_lock->flk_lock); 1080 TRACE_1(TR_FAC_FIFO, 1081 TR_FIFOWRITE_STREAM, "fifo_write stream_mode:%p", vp); 1082 1083 error = strwrite(vp, uiop, crp); 1084 1085 mutex_enter(&fn_lock->flk_lock); 1086 1087 done: 1088 /* 1089 * update vnode modification and change times 1090 * make sure there were no errors and some data was transferred 1091 */ 1092 if (error == 0 && write_size != uiop->uio_resid) { 1093 time_t now = gethrestime_sec(); 1094 1095 if (fnp->fn_flag & ISPIPE) { 1096 fn_dest->fn_mtime = fn_dest->fn_ctime = now; 1097 } 1098 fnp->fn_mtime = fnp->fn_ctime = now; 1099 } else if (fn_dest->fn_rcnt == 0 || fn_dest->fn_wcnt == 0) { 1100 goto epipe; 1101 } 1102 TRACE_3(TR_FAC_FIFO, TR_FIFOWRITE_OUT, 1103 "fifo_write out: vp %p error %d fnp %p", vp, error, fnp); 1104 mutex_exit(&fn_lock->flk_lock); 1105 return (error); 1106 epipe: 1107 error = EPIPE; 1108 TRACE_3(TR_FAC_FIFO, TR_FIFOWRITE_OUT, 1109 "fifo_write out: vp %p error %d fnp %p", 1110 vp, error, fnp); 1111 mutex_exit(&fn_lock->flk_lock); 1112 tsignal(curthread, SIGPIPE); 1113 return (error); 1114 } 1115 1116 /*ARGSUSED6*/ 1117 static int 1118 fifo_ioctl(vnode_t *vp, int cmd, intptr_t arg, int mode, 1119 cred_t *cr, int *rvalp, caller_context_t *ct) 1120 { 1121 /* 1122 * Just a quick check 1123 * Once we go to streams mode we don't ever revert back 1124 * So we do this quick check so as not to incur the overhead 1125 * associated with acquiring the lock 1126 */ 1127 return ((VTOF(vp)->fn_flag & FIFOFAST) ? 1128 fifo_fastioctl(vp, cmd, arg, mode, cr, rvalp) : 1129 fifo_strioctl(vp, cmd, arg, mode, cr, rvalp)); 1130 } 1131 1132 static int 1133 fifo_fastioctl(vnode_t *vp, int cmd, intptr_t arg, int mode, 1134 cred_t *cr, int *rvalp) 1135 { 1136 fifonode_t *fnp = VTOF(vp); 1137 fifonode_t *fn_dest; 1138 int error = 0; 1139 fifolock_t *fn_lock = fnp->fn_lock; 1140 int cnt; 1141 1142 /* 1143 * tty operations not allowed 1144 */ 1145 if (((cmd & IOCTYPE) == LDIOC) || 1146 ((cmd & IOCTYPE) == tIOC) || 1147 ((cmd & IOCTYPE) == TIOC)) { 1148 return (EINVAL); 1149 } 1150 1151 mutex_enter(&fn_lock->flk_lock); 1152 1153 if (!(fnp->fn_flag & FIFOFAST)) { 1154 goto stream_mode; 1155 } 1156 1157 switch (cmd) { 1158 1159 /* 1160 * Things we can't handle 1161 * These will switch us to streams mode. 1162 */ 1163 default: 1164 case I_STR: 1165 case I_SRDOPT: 1166 case I_PUSH: 1167 case I_FDINSERT: 1168 case I_SENDFD: 1169 case I_RECVFD: 1170 case I_E_RECVFD: 1171 case I_ATMARK: 1172 case I_CKBAND: 1173 case I_GETBAND: 1174 case I_SWROPT: 1175 goto turn_fastoff; 1176 1177 /* 1178 * Things that don't do damage 1179 * These things don't adjust the state of the 1180 * stream head (i_setcltime does, but we don't care) 1181 */ 1182 case I_FIND: 1183 case I_GETSIG: 1184 case FIONBIO: 1185 case FIOASYNC: 1186 case I_GRDOPT: /* probably should not get this, but no harm */ 1187 case I_GWROPT: 1188 case I_LIST: 1189 case I_SETCLTIME: 1190 case I_GETCLTIME: 1191 mutex_exit(&fn_lock->flk_lock); 1192 return (strioctl(vp, cmd, arg, mode, U_TO_K, cr, rvalp)); 1193 1194 case I_CANPUT: 1195 /* 1196 * We can only handle normal band canputs. 1197 * XXX : We could just always go to stream mode; after all 1198 * canput is a streams semantics type thing 1199 */ 1200 if (arg != 0) { 1201 goto turn_fastoff; 1202 } 1203 *rvalp = (fnp->fn_dest->fn_count < Fifohiwat) ? 1 : 0; 1204 mutex_exit(&fn_lock->flk_lock); 1205 return (0); 1206 1207 case I_NREAD: 1208 /* 1209 * This may seem a bit silly for non-streams semantics, 1210 * (After all, if they really want a message, they'll 1211 * probably use getmsg() anyway). but it doesn't hurt 1212 */ 1213 error = copyout((caddr_t)&fnp->fn_count, (caddr_t)arg, 1214 sizeof (cnt)); 1215 if (error == 0) { 1216 *rvalp = (fnp->fn_count == 0) ? 0 : 1; 1217 } 1218 break; 1219 1220 case FIORDCHK: 1221 *rvalp = fnp->fn_count; 1222 break; 1223 1224 case I_PEEK: 1225 { 1226 STRUCT_DECL(strpeek, strpeek); 1227 struct uio uio; 1228 struct iovec iov; 1229 int count; 1230 mblk_t *bp; 1231 1232 STRUCT_INIT(strpeek, mode); 1233 1234 if (fnp->fn_count == 0) { 1235 *rvalp = 0; 1236 break; 1237 } 1238 1239 error = copyin((caddr_t)arg, STRUCT_BUF(strpeek), 1240 STRUCT_SIZE(strpeek)); 1241 if (error) 1242 break; 1243 1244 /* 1245 * can't have any high priority message when in fast mode 1246 */ 1247 if (STRUCT_FGET(strpeek, flags) & RS_HIPRI) { 1248 *rvalp = 0; 1249 break; 1250 } 1251 1252 iov.iov_base = STRUCT_FGETP(strpeek, databuf.buf); 1253 iov.iov_len = STRUCT_FGET(strpeek, databuf.maxlen); 1254 uio.uio_iov = &iov; 1255 uio.uio_iovcnt = 1; 1256 uio.uio_loffset = 0; 1257 uio.uio_segflg = UIO_USERSPACE; 1258 uio.uio_fmode = 0; 1259 /* For pipes copy should not bypass cache */ 1260 uio.uio_extflg = UIO_COPY_CACHED; 1261 uio.uio_resid = iov.iov_len; 1262 count = fnp->fn_count; 1263 bp = fnp->fn_mp; 1264 while (count > 0 && uio.uio_resid) { 1265 cnt = MIN(uio.uio_resid, bp->b_wptr - bp->b_rptr); 1266 if ((error = uiomove((char *)bp->b_rptr, cnt, 1267 UIO_READ, &uio)) != 0) { 1268 break; 1269 } 1270 count -= cnt; 1271 bp = bp->b_cont; 1272 } 1273 STRUCT_FSET(strpeek, databuf.len, 1274 STRUCT_FGET(strpeek, databuf.maxlen) - uio.uio_resid); 1275 STRUCT_FSET(strpeek, flags, 0); 1276 STRUCT_FSET(strpeek, ctlbuf.len, 1277 STRUCT_FGET(strpeek, ctlbuf.maxlen)); 1278 1279 error = copyout(STRUCT_BUF(strpeek), (caddr_t)arg, 1280 STRUCT_SIZE(strpeek)); 1281 if (error == 0) 1282 *rvalp = 1; 1283 break; 1284 } 1285 1286 case FIONREAD: 1287 /* 1288 * let user know total number of bytes in message queue 1289 */ 1290 error = copyout((caddr_t)&fnp->fn_count, (caddr_t)arg, 1291 sizeof (fnp->fn_count)); 1292 if (error == 0) 1293 *rvalp = 0; 1294 break; 1295 1296 case I_SETSIG: 1297 /* 1298 * let streams set up the signal masking for us 1299 * we just check to see if it's set 1300 * XXX : this interface should not be visible 1301 * i.e. STREAM's framework is exposed. 1302 */ 1303 error = strioctl(vp, cmd, arg, mode, U_TO_K, cr, rvalp); 1304 if (vp->v_stream->sd_sigflags & (S_INPUT|S_RDNORM|S_WRNORM)) 1305 fnp->fn_flag |= FIFOSETSIG; 1306 else 1307 fnp->fn_flag &= ~FIFOSETSIG; 1308 break; 1309 1310 case I_FLUSH: 1311 /* 1312 * flush them message queues 1313 */ 1314 if (arg & ~FLUSHRW) { 1315 error = EINVAL; 1316 break; 1317 } 1318 if (arg & FLUSHR) { 1319 fifo_fastflush(fnp); 1320 } 1321 fn_dest = fnp->fn_dest; 1322 if ((arg & FLUSHW)) { 1323 fifo_fastflush(fn_dest); 1324 } 1325 /* 1326 * wake up any sleeping readers or writers 1327 * (waking readers probably doesn't make sense, but it 1328 * doesn't hurt; i.e. we just got rid of all the data 1329 * what's to read ?) 1330 */ 1331 if (fn_dest->fn_flag & (FIFOWANTW | FIFOWANTR)) { 1332 fn_dest->fn_flag &= ~(FIFOWANTW | FIFOWANTR); 1333 cv_broadcast(&fn_dest->fn_wait_cv); 1334 } 1335 *rvalp = 0; 1336 break; 1337 1338 /* 1339 * Since no band data can ever get on a fifo in fast mode 1340 * just return 0. 1341 */ 1342 case I_FLUSHBAND: 1343 error = 0; 1344 *rvalp = 0; 1345 break; 1346 1347 /* 1348 * invalid calls for stream head or fifos 1349 */ 1350 1351 case I_POP: /* shouldn't happen */ 1352 case I_LOOK: 1353 case I_LINK: 1354 case I_PLINK: 1355 case I_UNLINK: 1356 case I_PUNLINK: 1357 1358 /* 1359 * more invalid tty type of ioctls 1360 */ 1361 1362 case SRIOCSREDIR: 1363 case SRIOCISREDIR: 1364 error = EINVAL; 1365 break; 1366 1367 } 1368 mutex_exit(&fn_lock->flk_lock); 1369 return (error); 1370 1371 turn_fastoff: 1372 fifo_fastoff(fnp); 1373 1374 stream_mode: 1375 /* 1376 * streams mode 1377 */ 1378 mutex_exit(&fn_lock->flk_lock); 1379 return (fifo_strioctl(vp, cmd, arg, mode, cr, rvalp)); 1380 1381 } 1382 1383 /* 1384 * FIFO is in STREAMS mode; STREAMS framework does most of the work. 1385 */ 1386 static int 1387 fifo_strioctl(vnode_t *vp, int cmd, intptr_t arg, int mode, 1388 cred_t *cr, int *rvalp) 1389 { 1390 fifonode_t *fnp = VTOF(vp); 1391 int error; 1392 fifolock_t *fn_lock; 1393 1394 if (cmd == _I_GETPEERCRED) { 1395 if (mode == FKIOCTL && fnp->fn_pcredp != NULL) { 1396 k_peercred_t *kp = (k_peercred_t *)arg; 1397 crhold(fnp->fn_pcredp); 1398 kp->pc_cr = fnp->fn_pcredp; 1399 kp->pc_cpid = fnp->fn_cpid; 1400 return (0); 1401 } else { 1402 return (ENOTSUP); 1403 } 1404 } 1405 1406 error = strioctl(vp, cmd, arg, mode, U_TO_K, cr, rvalp); 1407 1408 switch (cmd) { 1409 /* 1410 * The FIFOSEND flag is set to inform other processes that a file 1411 * descriptor is pending at the stream head of this pipe. 1412 * The flag is cleared and the sending process is awoken when 1413 * this process has completed recieving the file descriptor. 1414 * XXX This could become out of sync if the process does I_SENDFDs 1415 * and opens on connld attached to the same pipe. 1416 */ 1417 case I_RECVFD: 1418 case I_E_RECVFD: 1419 if (error == 0) { 1420 fn_lock = fnp->fn_lock; 1421 mutex_enter(&fn_lock->flk_lock); 1422 if (fnp->fn_flag & FIFOSEND) { 1423 fnp->fn_flag &= ~FIFOSEND; 1424 cv_broadcast(&fnp->fn_dest->fn_wait_cv); 1425 } 1426 mutex_exit(&fn_lock->flk_lock); 1427 } 1428 break; 1429 default: 1430 break; 1431 } 1432 1433 return (error); 1434 } 1435 1436 /* 1437 * If shadowing a vnode (FIFOs), apply the VOP_GETATTR to the shadowed 1438 * vnode to Obtain the node information. If not shadowing (pipes), obtain 1439 * the node information from the credentials structure. 1440 */ 1441 int 1442 fifo_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *crp, 1443 caller_context_t *ct) 1444 { 1445 int error = 0; 1446 fifonode_t *fnp = VTOF(vp); 1447 queue_t *qp; 1448 qband_t *bandp; 1449 fifolock_t *fn_lock = fnp->fn_lock; 1450 1451 if (fnp->fn_realvp) { 1452 /* 1453 * for FIFOs or mounted pipes 1454 */ 1455 if (error = VOP_GETATTR(fnp->fn_realvp, vap, flags, crp, ct)) 1456 return (error); 1457 mutex_enter(&fn_lock->flk_lock); 1458 /* set current times from fnode, even if older than vnode */ 1459 vap->va_atime.tv_sec = fnp->fn_atime; 1460 vap->va_atime.tv_nsec = 0; 1461 vap->va_mtime.tv_sec = fnp->fn_mtime; 1462 vap->va_mtime.tv_nsec = 0; 1463 vap->va_ctime.tv_sec = fnp->fn_ctime; 1464 vap->va_ctime.tv_nsec = 0; 1465 } else { 1466 /* 1467 * for non-attached/ordinary pipes 1468 */ 1469 vap->va_mode = 0; 1470 mutex_enter(&fn_lock->flk_lock); 1471 vap->va_atime.tv_sec = fnp->fn_atime; 1472 vap->va_atime.tv_nsec = 0; 1473 vap->va_mtime.tv_sec = fnp->fn_mtime; 1474 vap->va_mtime.tv_nsec = 0; 1475 vap->va_ctime.tv_sec = fnp->fn_ctime; 1476 vap->va_ctime.tv_nsec = 0; 1477 vap->va_uid = crgetuid(crp); 1478 vap->va_gid = crgetgid(crp); 1479 vap->va_nlink = 0; 1480 vap->va_fsid = fifodev; 1481 vap->va_nodeid = (ino64_t)fnp->fn_ino; 1482 vap->va_rdev = 0; 1483 } 1484 vap->va_type = VFIFO; 1485 vap->va_blksize = PIPE_BUF; 1486 /* 1487 * Size is number of un-read bytes at the stream head and 1488 * nblocks is the unread bytes expressed in blocks. 1489 */ 1490 if (vp->v_stream && (fnp->fn_flag & FIFOISOPEN)) { 1491 if ((fnp->fn_flag & FIFOFAST)) { 1492 vap->va_size = (u_offset_t)fnp->fn_count; 1493 } else { 1494 qp = RD((strvp2wq(vp))); 1495 vap->va_size = (u_offset_t)qp->q_count; 1496 if (qp->q_nband != 0) { 1497 mutex_enter(QLOCK(qp)); 1498 for (bandp = qp->q_bandp; bandp; 1499 bandp = bandp->qb_next) 1500 vap->va_size += bandp->qb_count; 1501 mutex_exit(QLOCK(qp)); 1502 } 1503 } 1504 vap->va_nblocks = (fsblkcnt64_t)btod(vap->va_size); 1505 } else { 1506 vap->va_size = (u_offset_t)0; 1507 vap->va_nblocks = (fsblkcnt64_t)0; 1508 } 1509 mutex_exit(&fn_lock->flk_lock); 1510 vap->va_seq = 0; 1511 return (0); 1512 } 1513 1514 /* 1515 * If shadowing a vnode, apply the VOP_SETATTR to it, and to the fnode. 1516 * Otherwise, set the time and return 0. 1517 */ 1518 int 1519 fifo_setattr( 1520 vnode_t *vp, 1521 vattr_t *vap, 1522 int flags, 1523 cred_t *crp, 1524 caller_context_t *ctp) 1525 { 1526 fifonode_t *fnp = VTOF(vp); 1527 int error = 0; 1528 fifolock_t *fn_lock; 1529 1530 if (fnp->fn_realvp) 1531 error = VOP_SETATTR(fnp->fn_realvp, vap, flags, crp, ctp); 1532 if (error == 0) { 1533 fn_lock = fnp->fn_lock; 1534 mutex_enter(&fn_lock->flk_lock); 1535 if (vap->va_mask & AT_ATIME) 1536 fnp->fn_atime = vap->va_atime.tv_sec; 1537 if (vap->va_mask & AT_MTIME) 1538 fnp->fn_mtime = vap->va_mtime.tv_sec; 1539 fnp->fn_ctime = gethrestime_sec(); 1540 mutex_exit(&fn_lock->flk_lock); 1541 } 1542 return (error); 1543 } 1544 1545 /* 1546 * If shadowing a vnode, apply VOP_ACCESS to it. 1547 * Otherwise, return 0 (allow all access). 1548 */ 1549 int 1550 fifo_access(vnode_t *vp, int mode, int flags, cred_t *crp, caller_context_t *ct) 1551 { 1552 if (VTOF(vp)->fn_realvp) 1553 return (VOP_ACCESS(VTOF(vp)->fn_realvp, mode, flags, crp, ct)); 1554 else 1555 return (0); 1556 } 1557 1558 /* 1559 * This can be called if creat or an open with O_CREAT is done on the root 1560 * of a lofs mount where the mounted entity is a fifo. 1561 */ 1562 /*ARGSUSED*/ 1563 static int 1564 fifo_create(struct vnode *dvp, char *name, vattr_t *vap, enum vcexcl excl, 1565 int mode, struct vnode **vpp, struct cred *cr, int flag, 1566 caller_context_t *ct, vsecattr_t *vsecp) 1567 { 1568 int error; 1569 1570 ASSERT(dvp && (dvp->v_flag & VROOT) && *name == '\0'); 1571 if (excl == NONEXCL) { 1572 if (mode && (error = fifo_access(dvp, mode, 0, cr, ct))) 1573 return (error); 1574 VN_HOLD(dvp); 1575 return (0); 1576 } 1577 return (EEXIST); 1578 } 1579 1580 /* 1581 * If shadowing a vnode, apply the VOP_FSYNC to it. 1582 * Otherwise, return 0. 1583 */ 1584 int 1585 fifo_fsync(vnode_t *vp, int syncflag, cred_t *crp, caller_context_t *ct) 1586 { 1587 fifonode_t *fnp = VTOF(vp); 1588 vattr_t va; 1589 1590 if (fnp->fn_realvp == NULL) 1591 return (0); 1592 1593 bzero((caddr_t)&va, sizeof (va)); 1594 va.va_mask = AT_MTIME | AT_ATIME; 1595 if (VOP_GETATTR(fnp->fn_realvp, &va, 0, crp, ct) == 0) { 1596 va.va_mask = 0; 1597 if (fnp->fn_mtime > va.va_mtime.tv_sec) { 1598 va.va_mtime.tv_sec = fnp->fn_mtime; 1599 va.va_mask = AT_MTIME; 1600 } 1601 if (fnp->fn_atime > va.va_atime.tv_sec) { 1602 va.va_atime.tv_sec = fnp->fn_atime; 1603 va.va_mask |= AT_ATIME; 1604 } 1605 if (va.va_mask != 0) 1606 (void) VOP_SETATTR(fnp->fn_realvp, &va, 0, crp, ct); 1607 } 1608 return (VOP_FSYNC(fnp->fn_realvp, syncflag, crp, ct)); 1609 } 1610 1611 /* 1612 * Called when the upper level no longer holds references to the 1613 * vnode. Sync the file system and free the fifonode. 1614 */ 1615 void 1616 fifo_inactive(vnode_t *vp, cred_t *crp, caller_context_t *ct) 1617 { 1618 fifonode_t *fnp; 1619 fifolock_t *fn_lock; 1620 1621 mutex_enter(&ftable_lock); 1622 mutex_enter(&vp->v_lock); 1623 ASSERT(vp->v_count >= 1); 1624 if (--vp->v_count != 0) { 1625 /* 1626 * Somebody accessed the fifo before we got a chance to 1627 * remove it. They will remove it when they do a vn_rele. 1628 */ 1629 mutex_exit(&vp->v_lock); 1630 mutex_exit(&ftable_lock); 1631 return; 1632 } 1633 mutex_exit(&vp->v_lock); 1634 1635 fnp = VTOF(vp); 1636 1637 /* 1638 * remove fifo from fifo list so that no other process 1639 * can grab it. 1640 */ 1641 if (fnp->fn_realvp) { 1642 (void) fiforemove(fnp); 1643 mutex_exit(&ftable_lock); 1644 (void) fifo_fsync(vp, FSYNC, crp, ct); 1645 VN_RELE(fnp->fn_realvp); 1646 vp->v_vfsp = NULL; 1647 } else 1648 mutex_exit(&ftable_lock); 1649 1650 fn_lock = fnp->fn_lock; 1651 1652 mutex_enter(&fn_lock->flk_lock); 1653 ASSERT(vp->v_stream == NULL); 1654 ASSERT(vp->v_count == 0); 1655 /* 1656 * if this is last reference to the lock, then we can 1657 * free everything up. 1658 */ 1659 if (--fn_lock->flk_ref == 0) { 1660 mutex_exit(&fn_lock->flk_lock); 1661 ASSERT(fnp->fn_open == 0); 1662 ASSERT(fnp->fn_dest->fn_open == 0); 1663 if (fnp->fn_mp) { 1664 freemsg(fnp->fn_mp); 1665 fnp->fn_mp = NULL; 1666 fnp->fn_count = 0; 1667 } 1668 if (fnp->fn_pcredp != NULL) { 1669 crfree(fnp->fn_pcredp); 1670 fnp->fn_pcredp = NULL; 1671 } 1672 if (fnp->fn_flag & ISPIPE) { 1673 fifonode_t *fn_dest = fnp->fn_dest; 1674 1675 vp = FTOV(fn_dest); 1676 if (fn_dest->fn_mp) { 1677 freemsg(fn_dest->fn_mp); 1678 fn_dest->fn_mp = NULL; 1679 fn_dest->fn_count = 0; 1680 } 1681 if (fn_dest->fn_pcredp != NULL) { 1682 crfree(fn_dest->fn_pcredp); 1683 fn_dest->fn_pcredp = NULL; 1684 } 1685 kmem_cache_free(pipe_cache, (fifodata_t *)fn_lock); 1686 } else 1687 kmem_cache_free(fnode_cache, (fifodata_t *)fn_lock); 1688 } else { 1689 mutex_exit(&fn_lock->flk_lock); 1690 } 1691 } 1692 1693 /* 1694 * If shadowing a vnode, apply the VOP_FID to it. 1695 * Otherwise, return EINVAL. 1696 */ 1697 int 1698 fifo_fid(vnode_t *vp, fid_t *fidfnp, caller_context_t *ct) 1699 { 1700 if (VTOF(vp)->fn_realvp) 1701 return (VOP_FID(VTOF(vp)->fn_realvp, fidfnp, ct)); 1702 else 1703 return (EINVAL); 1704 } 1705 1706 /* 1707 * Lock a fifonode. 1708 */ 1709 /* ARGSUSED */ 1710 int 1711 fifo_rwlock(vnode_t *vp, int write_lock, caller_context_t *ctp) 1712 { 1713 return (-1); 1714 } 1715 1716 /* 1717 * Unlock a fifonode. 1718 */ 1719 /* ARGSUSED */ 1720 void 1721 fifo_rwunlock(vnode_t *vp, int write_lock, caller_context_t *ctp) 1722 { 1723 } 1724 1725 /* 1726 * Return error since seeks are not allowed on pipes. 1727 */ 1728 /*ARGSUSED*/ 1729 int 1730 fifo_seek(vnode_t *vp, offset_t ooff, offset_t *noffp, caller_context_t *ct) 1731 { 1732 return (ESPIPE); 1733 } 1734 1735 /* 1736 * If there is a realvp associated with vp, return it. 1737 */ 1738 int 1739 fifo_realvp(vnode_t *vp, vnode_t **vpp, caller_context_t *ct) 1740 { 1741 vnode_t *rvp; 1742 1743 if ((rvp = VTOF(vp)->fn_realvp) != NULL) { 1744 vp = rvp; 1745 if (VOP_REALVP(vp, &rvp, ct) == 0) 1746 vp = rvp; 1747 } 1748 1749 *vpp = vp; 1750 return (0); 1751 } 1752 1753 /* 1754 * Poll for interesting events on a stream pipe 1755 */ 1756 /* ARGSUSED */ 1757 int 1758 fifo_poll(vnode_t *vp, short events, int anyyet, short *reventsp, 1759 pollhead_t **phpp, caller_context_t *ct) 1760 { 1761 fifonode_t *fnp, *fn_dest; 1762 fifolock_t *fn_lock; 1763 int retevents; 1764 struct stdata *stp; 1765 1766 ASSERT(vp->v_stream != NULL); 1767 1768 stp = vp->v_stream; 1769 retevents = 0; 1770 fnp = VTOF(vp); 1771 fn_dest = fnp->fn_dest; 1772 fn_lock = fnp->fn_lock; 1773 1774 polllock(&stp->sd_pollist, &fn_lock->flk_lock); 1775 1776 /* 1777 * see if FIFO/pipe open 1778 */ 1779 if ((fnp->fn_flag & FIFOISOPEN) == 0) { 1780 if (((events & (POLLIN | POLLRDNORM | POLLPRI | POLLRDBAND)) && 1781 fnp->fn_rcnt == 0) || 1782 ((events & (POLLWRNORM | POLLWRBAND)) && 1783 fnp->fn_wcnt == 0)) { 1784 mutex_exit(&fnp->fn_lock->flk_lock); 1785 *reventsp = POLLERR; 1786 return (0); 1787 } 1788 } 1789 1790 /* 1791 * if not in fast mode, let the stream head take care of it 1792 */ 1793 if (!(fnp->fn_flag & FIFOFAST)) { 1794 mutex_exit(&fnp->fn_lock->flk_lock); 1795 goto stream_mode; 1796 } 1797 1798 /* 1799 * If this is a pipe.. check to see if the other 1800 * end is gone. If we are a fifo, check to see 1801 * if write end is gone. 1802 */ 1803 1804 if ((fnp->fn_flag & ISPIPE) && (fn_dest->fn_open == 0)) { 1805 retevents = POLLHUP; 1806 } else if ((fnp->fn_flag & (FIFOCLOSE | ISPIPE)) == FIFOCLOSE && 1807 (fn_dest->fn_wcnt == 0)) { 1808 /* 1809 * no writer at other end. 1810 * it was closed (versus yet to be opened) 1811 */ 1812 retevents = POLLHUP; 1813 } else if (events & (POLLWRNORM | POLLWRBAND)) { 1814 if (events & POLLWRNORM) { 1815 if (fn_dest->fn_count < Fifohiwat) 1816 retevents = POLLWRNORM; 1817 else 1818 fnp->fn_flag |= FIFOHIWATW; 1819 } 1820 /* 1821 * This is always true for fast pipes 1822 * (Note: will go to STREAMS mode if band data is written) 1823 */ 1824 if (events & POLLWRBAND) 1825 retevents |= POLLWRBAND; 1826 } 1827 if (events & (POLLIN | POLLRDNORM)) { 1828 if (fnp->fn_count) 1829 retevents |= (events & (POLLIN | POLLRDNORM)); 1830 } 1831 1832 /* 1833 * if we happened to get something, return 1834 */ 1835 1836 if ((*reventsp = (short)retevents) != 0) { 1837 mutex_exit(&fnp->fn_lock->flk_lock); 1838 return (0); 1839 } 1840 1841 /* 1842 * If poll() has not found any events yet, set up event cell 1843 * to wake up the poll if a requested event occurs on this 1844 * pipe/fifo. 1845 */ 1846 if (!anyyet) { 1847 if (events & POLLWRNORM) 1848 fnp->fn_flag |= FIFOPOLLW; 1849 if (events & (POLLIN | POLLRDNORM)) 1850 fnp->fn_flag |= FIFOPOLLR; 1851 if (events & POLLRDBAND) 1852 fnp->fn_flag |= FIFOPOLLRBAND; 1853 /* 1854 * XXX Don't like exposing this from streams 1855 */ 1856 *phpp = &stp->sd_pollist; 1857 } 1858 mutex_exit(&fnp->fn_lock->flk_lock); 1859 return (0); 1860 stream_mode: 1861 return (strpoll(stp, events, anyyet, reventsp, phpp)); 1862 } 1863 1864 /* 1865 * POSIX pathconf() support. 1866 */ 1867 /* ARGSUSED */ 1868 int 1869 fifo_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr, 1870 caller_context_t *ct) 1871 { 1872 ulong_t val; 1873 int error = 0; 1874 1875 switch (cmd) { 1876 1877 case _PC_LINK_MAX: 1878 val = MAXLINK; 1879 break; 1880 1881 case _PC_MAX_CANON: 1882 val = MAX_CANON; 1883 break; 1884 1885 case _PC_MAX_INPUT: 1886 val = MAX_INPUT; 1887 break; 1888 1889 case _PC_NAME_MAX: 1890 error = EINVAL; 1891 break; 1892 1893 case _PC_PATH_MAX: 1894 case _PC_SYMLINK_MAX: 1895 val = MAXPATHLEN; 1896 break; 1897 1898 case _PC_PIPE_BUF: 1899 val = PIPE_BUF; 1900 break; 1901 1902 case _PC_NO_TRUNC: 1903 if (vp->v_vfsp->vfs_flag & VFS_NOTRUNC) 1904 val = 1; /* NOTRUNC is enabled for vp */ 1905 else 1906 val = (ulong_t)-1; 1907 break; 1908 1909 case _PC_VDISABLE: 1910 val = _POSIX_VDISABLE; 1911 break; 1912 1913 case _PC_CHOWN_RESTRICTED: 1914 if (rstchown) 1915 val = rstchown; /* chown restricted enabled */ 1916 else 1917 val = (ulong_t)-1; 1918 break; 1919 1920 case _PC_FILESIZEBITS: 1921 val = (ulong_t)-1; 1922 break; 1923 1924 default: 1925 if (VTOF(vp)->fn_realvp) 1926 error = VOP_PATHCONF(VTOF(vp)->fn_realvp, cmd, 1927 &val, cr, ct); 1928 else 1929 error = EINVAL; 1930 break; 1931 } 1932 1933 if (error == 0) 1934 *valp = val; 1935 return (error); 1936 } 1937 1938 /* 1939 * If shadowing a vnode, apply VOP_SETSECATTR to it. 1940 * Otherwise, return NOSYS. 1941 */ 1942 int 1943 fifo_setsecattr(struct vnode *vp, vsecattr_t *vsap, int flag, struct cred *crp, 1944 caller_context_t *ct) 1945 { 1946 int error; 1947 1948 /* 1949 * The acl(2) system call tries to grab the write lock on the 1950 * file when setting an ACL, but fifofs does not implement 1951 * VOP_RWLOCK or VOP_RWUNLOCK, so we do it here instead. 1952 */ 1953 if (VTOF(vp)->fn_realvp) { 1954 (void) VOP_RWLOCK(VTOF(vp)->fn_realvp, V_WRITELOCK_TRUE, ct); 1955 error = VOP_SETSECATTR(VTOF(vp)->fn_realvp, vsap, flag, 1956 crp, ct); 1957 VOP_RWUNLOCK(VTOF(vp)->fn_realvp, V_WRITELOCK_TRUE, ct); 1958 return (error); 1959 } else 1960 return (fs_nosys()); 1961 } 1962 1963 /* 1964 * If shadowing a vnode, apply VOP_GETSECATTR to it. Otherwise, fabricate 1965 * an ACL from the permission bits that fifo_getattr() makes up. 1966 */ 1967 int 1968 fifo_getsecattr(struct vnode *vp, vsecattr_t *vsap, int flag, struct cred *crp, 1969 caller_context_t *ct) 1970 { 1971 if (VTOF(vp)->fn_realvp) 1972 return (VOP_GETSECATTR(VTOF(vp)->fn_realvp, vsap, flag, 1973 crp, ct)); 1974 else 1975 return (fs_fab_acl(vp, vsap, flag, crp, ct)); 1976 } 1977 1978 1979 /* 1980 * Set the FIFOSTAYFAST flag so nobody can turn the fifo into stream mode. 1981 * If the flag is already set then wait until it is removed - releasing 1982 * the lock. 1983 * If the fifo switches into stream mode while we are waiting, return failure. 1984 */ 1985 static boolean_t 1986 fifo_stayfast_enter(fifonode_t *fnp) 1987 { 1988 ASSERT(MUTEX_HELD(&fnp->fn_lock->flk_lock)); 1989 while (fnp->fn_flag & FIFOSTAYFAST) { 1990 fnp->fn_flag |= FIFOWAITMODE; 1991 cv_wait(&fnp->fn_wait_cv, &fnp->fn_lock->flk_lock); 1992 fnp->fn_flag &= ~FIFOWAITMODE; 1993 } 1994 if (!(fnp->fn_flag & FIFOFAST)) 1995 return (B_FALSE); 1996 1997 fnp->fn_flag |= FIFOSTAYFAST; 1998 return (B_TRUE); 1999 } 2000 2001 /* 2002 * Unset the FIFOSTAYFAST flag and notify anybody waiting for this flag 2003 * to be removed: 2004 * - threads wanting to turn into stream mode waiting in fifo_fastoff(), 2005 * - other writers threads waiting in fifo_stayfast_enter(). 2006 */ 2007 static void 2008 fifo_stayfast_exit(fifonode_t *fnp) 2009 { 2010 fifonode_t *fn_dest = fnp->fn_dest; 2011 2012 ASSERT(MUTEX_HELD(&fnp->fn_lock->flk_lock)); 2013 2014 fnp->fn_flag &= ~FIFOSTAYFAST; 2015 2016 if (fnp->fn_flag & FIFOWAITMODE) 2017 cv_broadcast(&fnp->fn_wait_cv); 2018 2019 if ((fnp->fn_flag & ISPIPE) && (fn_dest->fn_flag & FIFOWAITMODE)) 2020 cv_broadcast(&fn_dest->fn_wait_cv); 2021 } 2022