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