sys_pipe.c revision 76760
1/* 2 * Copyright (c) 1996 John S. Dyson 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice immediately at the beginning of the file, without modification, 10 * this list of conditions, and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. Absolutely no warranty of function or purpose is made by the author 15 * John S. Dyson. 16 * 4. Modifications may be freely made to this file if the above conditions 17 * are met. 18 * 19 * $FreeBSD: head/sys/kern/sys_pipe.c 76760 2001-05-17 19:47:09Z alfred $ 20 */ 21 22/* 23 * This file contains a high-performance replacement for the socket-based 24 * pipes scheme originally used in FreeBSD/4.4Lite. It does not support 25 * all features of sockets, but does do everything that pipes normally 26 * do. 27 */ 28 29/* 30 * This code has two modes of operation, a small write mode and a large 31 * write mode. The small write mode acts like conventional pipes with 32 * a kernel buffer. If the buffer is less than PIPE_MINDIRECT, then the 33 * "normal" pipe buffering is done. If the buffer is between PIPE_MINDIRECT 34 * and PIPE_SIZE in size, it is fully mapped and wired into the kernel, and 35 * the receiving process can copy it directly from the pages in the sending 36 * process. 37 * 38 * If the sending process receives a signal, it is possible that it will 39 * go away, and certainly its address space can change, because control 40 * is returned back to the user-mode side. In that case, the pipe code 41 * arranges to copy the buffer supplied by the user process, to a pageable 42 * kernel buffer, and the receiving process will grab the data from the 43 * pageable kernel buffer. Since signals don't happen all that often, 44 * the copy operation is normally eliminated. 45 * 46 * The constant PIPE_MINDIRECT is chosen to make sure that buffering will 47 * happen for small transfers so that the system will not spend all of 48 * its time context switching. PIPE_SIZE is constrained by the 49 * amount of kernel virtual memory. 50 */ 51 52#include <sys/param.h> 53#include <sys/systm.h> 54#include <sys/fcntl.h> 55#include <sys/file.h> 56#include <sys/filedesc.h> 57#include <sys/filio.h> 58#include <sys/lock.h> 59#include <sys/ttycom.h> 60#include <sys/stat.h> 61#include <sys/poll.h> 62#include <sys/selinfo.h> 63#include <sys/signalvar.h> 64#include <sys/sysproto.h> 65#include <sys/pipe.h> 66#include <sys/proc.h> 67#include <sys/vnode.h> 68#include <sys/uio.h> 69#include <sys/event.h> 70 71#include <vm/vm.h> 72#include <vm/vm_param.h> 73#include <vm/vm_object.h> 74#include <vm/vm_kern.h> 75#include <vm/vm_extern.h> 76#include <vm/pmap.h> 77#include <vm/vm_map.h> 78#include <vm/vm_page.h> 79#include <vm/vm_zone.h> 80 81/* 82 * Use this define if you want to disable *fancy* VM things. Expect an 83 * approx 30% decrease in transfer rate. This could be useful for 84 * NetBSD or OpenBSD. 85 */ 86/* #define PIPE_NODIRECT */ 87 88/* 89 * interfaces to the outside world 90 */ 91static int pipe_read __P((struct file *fp, struct uio *uio, 92 struct ucred *cred, int flags, struct proc *p)); 93static int pipe_write __P((struct file *fp, struct uio *uio, 94 struct ucred *cred, int flags, struct proc *p)); 95static int pipe_close __P((struct file *fp, struct proc *p)); 96static int pipe_poll __P((struct file *fp, int events, struct ucred *cred, 97 struct proc *p)); 98static int pipe_kqfilter __P((struct file *fp, struct knote *kn)); 99static int pipe_stat __P((struct file *fp, struct stat *sb, struct proc *p)); 100static int pipe_ioctl __P((struct file *fp, u_long cmd, caddr_t data, struct proc *p)); 101 102static struct fileops pipeops = { 103 pipe_read, pipe_write, pipe_ioctl, pipe_poll, pipe_kqfilter, 104 pipe_stat, pipe_close 105}; 106 107static void filt_pipedetach(struct knote *kn); 108static int filt_piperead(struct knote *kn, long hint); 109static int filt_pipewrite(struct knote *kn, long hint); 110 111static struct filterops pipe_rfiltops = 112 { 1, NULL, filt_pipedetach, filt_piperead }; 113static struct filterops pipe_wfiltops = 114 { 1, NULL, filt_pipedetach, filt_pipewrite }; 115 116 117/* 118 * Default pipe buffer size(s), this can be kind-of large now because pipe 119 * space is pageable. The pipe code will try to maintain locality of 120 * reference for performance reasons, so small amounts of outstanding I/O 121 * will not wipe the cache. 122 */ 123#define MINPIPESIZE (PIPE_SIZE/3) 124#define MAXPIPESIZE (2*PIPE_SIZE/3) 125 126/* 127 * Maximum amount of kva for pipes -- this is kind-of a soft limit, but 128 * is there so that on large systems, we don't exhaust it. 129 */ 130#define MAXPIPEKVA (8*1024*1024) 131 132/* 133 * Limit for direct transfers, we cannot, of course limit 134 * the amount of kva for pipes in general though. 135 */ 136#define LIMITPIPEKVA (16*1024*1024) 137 138/* 139 * Limit the number of "big" pipes 140 */ 141#define LIMITBIGPIPES 32 142static int nbigpipe; 143 144static int amountpipekva; 145 146static void pipeclose __P((struct pipe *cpipe)); 147static void pipe_free_kmem __P((struct pipe *cpipe)); 148static int pipe_create __P((struct pipe **cpipep)); 149static __inline int pipelock __P((struct pipe *cpipe, int catch)); 150static __inline void pipeunlock __P((struct pipe *cpipe)); 151static __inline void pipeselwakeup __P((struct pipe *cpipe)); 152#ifndef PIPE_NODIRECT 153static int pipe_build_write_buffer __P((struct pipe *wpipe, struct uio *uio)); 154static void pipe_destroy_write_buffer __P((struct pipe *wpipe)); 155static int pipe_direct_write __P((struct pipe *wpipe, struct uio *uio)); 156static void pipe_clone_write_buffer __P((struct pipe *wpipe)); 157#endif 158static int pipespace __P((struct pipe *cpipe, int size)); 159 160static vm_zone_t pipe_zone; 161 162/* 163 * The pipe system call for the DTYPE_PIPE type of pipes 164 */ 165 166/* ARGSUSED */ 167int 168pipe(p, uap) 169 struct proc *p; 170 struct pipe_args /* { 171 int dummy; 172 } */ *uap; 173{ 174 struct filedesc *fdp = p->p_fd; 175 struct file *rf, *wf; 176 struct pipe *rpipe, *wpipe; 177 int fd, error; 178 179 if (pipe_zone == NULL) 180 pipe_zone = zinit("PIPE", sizeof(struct pipe), 0, 0, 4); 181 182 rpipe = wpipe = NULL; 183 if (pipe_create(&rpipe) || pipe_create(&wpipe)) { 184 pipeclose(rpipe); 185 pipeclose(wpipe); 186 return (ENFILE); 187 } 188 189 rpipe->pipe_state |= PIPE_DIRECTOK; 190 wpipe->pipe_state |= PIPE_DIRECTOK; 191 192 error = falloc(p, &rf, &fd); 193 if (error) { 194 pipeclose(rpipe); 195 pipeclose(wpipe); 196 return (error); 197 } 198 fhold(rf); 199 p->p_retval[0] = fd; 200 201 /* 202 * Warning: once we've gotten past allocation of the fd for the 203 * read-side, we can only drop the read side via fdrop() in order 204 * to avoid races against processes which manage to dup() the read 205 * side while we are blocked trying to allocate the write side. 206 */ 207 rf->f_flag = FREAD | FWRITE; 208 rf->f_type = DTYPE_PIPE; 209 rf->f_data = (caddr_t)rpipe; 210 rf->f_ops = &pipeops; 211 error = falloc(p, &wf, &fd); 212 if (error) { 213 if (fdp->fd_ofiles[p->p_retval[0]] == rf) { 214 fdp->fd_ofiles[p->p_retval[0]] = NULL; 215 fdrop(rf, p); 216 } 217 fdrop(rf, p); 218 /* rpipe has been closed by fdrop(). */ 219 pipeclose(wpipe); 220 return (error); 221 } 222 wf->f_flag = FREAD | FWRITE; 223 wf->f_type = DTYPE_PIPE; 224 wf->f_data = (caddr_t)wpipe; 225 wf->f_ops = &pipeops; 226 p->p_retval[1] = fd; 227 228 rpipe->pipe_peer = wpipe; 229 wpipe->pipe_peer = rpipe; 230 fdrop(rf, p); 231 232 return (0); 233} 234 235/* 236 * Allocate kva for pipe circular buffer, the space is pageable 237 * This routine will 'realloc' the size of a pipe safely, if it fails 238 * it will retain the old buffer. 239 * If it fails it will return ENOMEM. 240 */ 241static int 242pipespace(cpipe, size) 243 struct pipe *cpipe; 244 int size; 245{ 246 struct vm_object *object; 247 caddr_t buffer; 248 int npages, error; 249 250 npages = round_page(size)/PAGE_SIZE; 251 /* 252 * Create an object, I don't like the idea of paging to/from 253 * kernel_object. 254 * XXX -- minor change needed here for NetBSD/OpenBSD VM systems. 255 */ 256 object = vm_object_allocate(OBJT_DEFAULT, npages); 257 buffer = (caddr_t) vm_map_min(kernel_map); 258 259 /* 260 * Insert the object into the kernel map, and allocate kva for it. 261 * The map entry is, by default, pageable. 262 * XXX -- minor change needed here for NetBSD/OpenBSD VM systems. 263 */ 264 error = vm_map_find(kernel_map, object, 0, 265 (vm_offset_t *) &buffer, size, 1, 266 VM_PROT_ALL, VM_PROT_ALL, 0); 267 268 if (error != KERN_SUCCESS) { 269 vm_object_deallocate(object); 270 return (ENOMEM); 271 } 272 273 /* free old resources if we're resizing */ 274 pipe_free_kmem(cpipe); 275 cpipe->pipe_buffer.object = object; 276 cpipe->pipe_buffer.buffer = buffer; 277 cpipe->pipe_buffer.size = size; 278 cpipe->pipe_buffer.in = 0; 279 cpipe->pipe_buffer.out = 0; 280 cpipe->pipe_buffer.cnt = 0; 281 amountpipekva += cpipe->pipe_buffer.size; 282 return (0); 283} 284 285/* 286 * initialize and allocate VM and memory for pipe 287 */ 288static int 289pipe_create(cpipep) 290 struct pipe **cpipep; 291{ 292 struct pipe *cpipe; 293 int error; 294 295 *cpipep = zalloc(pipe_zone); 296 if (*cpipep == NULL) 297 return (ENOMEM); 298 299 cpipe = *cpipep; 300 301 /* so pipespace()->pipe_free_kmem() doesn't follow junk pointer */ 302 cpipe->pipe_buffer.object = NULL; 303#ifndef PIPE_NODIRECT 304 cpipe->pipe_map.kva = NULL; 305#endif 306 /* 307 * protect so pipeclose() doesn't follow a junk pointer 308 * if pipespace() fails. 309 */ 310 bzero(&cpipe->pipe_sel, sizeof(cpipe->pipe_sel)); 311 cpipe->pipe_state = 0; 312 cpipe->pipe_peer = NULL; 313 cpipe->pipe_busy = 0; 314 315#ifndef PIPE_NODIRECT 316 /* 317 * pipe data structure initializations to support direct pipe I/O 318 */ 319 cpipe->pipe_map.cnt = 0; 320 cpipe->pipe_map.kva = 0; 321 cpipe->pipe_map.pos = 0; 322 cpipe->pipe_map.npages = 0; 323 /* cpipe->pipe_map.ms[] = invalid */ 324#endif 325 326 error = pipespace(cpipe, PIPE_SIZE); 327 if (error) 328 return (error); 329 330 vfs_timestamp(&cpipe->pipe_ctime); 331 cpipe->pipe_atime = cpipe->pipe_ctime; 332 cpipe->pipe_mtime = cpipe->pipe_ctime; 333 334 return (0); 335} 336 337 338/* 339 * lock a pipe for I/O, blocking other access 340 */ 341static __inline int 342pipelock(cpipe, catch) 343 struct pipe *cpipe; 344 int catch; 345{ 346 int error; 347 348 while (cpipe->pipe_state & PIPE_LOCK) { 349 cpipe->pipe_state |= PIPE_LWANT; 350 error = tsleep(cpipe, catch ? (PRIBIO | PCATCH) : PRIBIO, 351 "pipelk", 0); 352 if (error != 0) 353 return (error); 354 } 355 cpipe->pipe_state |= PIPE_LOCK; 356 return (0); 357} 358 359/* 360 * unlock a pipe I/O lock 361 */ 362static __inline void 363pipeunlock(cpipe) 364 struct pipe *cpipe; 365{ 366 367 cpipe->pipe_state &= ~PIPE_LOCK; 368 if (cpipe->pipe_state & PIPE_LWANT) { 369 cpipe->pipe_state &= ~PIPE_LWANT; 370 wakeup(cpipe); 371 } 372} 373 374static __inline void 375pipeselwakeup(cpipe) 376 struct pipe *cpipe; 377{ 378 379 if (cpipe->pipe_state & PIPE_SEL) { 380 cpipe->pipe_state &= ~PIPE_SEL; 381 selwakeup(&cpipe->pipe_sel); 382 } 383 if ((cpipe->pipe_state & PIPE_ASYNC) && cpipe->pipe_sigio) 384 pgsigio(cpipe->pipe_sigio, SIGIO, 0); 385 KNOTE(&cpipe->pipe_sel.si_note, 0); 386} 387 388/* ARGSUSED */ 389static int 390pipe_read(fp, uio, cred, flags, p) 391 struct file *fp; 392 struct uio *uio; 393 struct ucred *cred; 394 struct proc *p; 395 int flags; 396{ 397 struct pipe *rpipe = (struct pipe *) fp->f_data; 398 int error; 399 int nread = 0; 400 u_int size; 401 402 ++rpipe->pipe_busy; 403 error = pipelock(rpipe, 1); 404 if (error) 405 goto unlocked_error; 406 407 while (uio->uio_resid) { 408 /* 409 * normal pipe buffer receive 410 */ 411 if (rpipe->pipe_buffer.cnt > 0) { 412 size = rpipe->pipe_buffer.size - rpipe->pipe_buffer.out; 413 if (size > rpipe->pipe_buffer.cnt) 414 size = rpipe->pipe_buffer.cnt; 415 if (size > (u_int) uio->uio_resid) 416 size = (u_int) uio->uio_resid; 417 418 error = uiomove(&rpipe->pipe_buffer.buffer[rpipe->pipe_buffer.out], 419 size, uio); 420 if (error) 421 break; 422 423 rpipe->pipe_buffer.out += size; 424 if (rpipe->pipe_buffer.out >= rpipe->pipe_buffer.size) 425 rpipe->pipe_buffer.out = 0; 426 427 rpipe->pipe_buffer.cnt -= size; 428 429 /* 430 * If there is no more to read in the pipe, reset 431 * its pointers to the beginning. This improves 432 * cache hit stats. 433 */ 434 if (rpipe->pipe_buffer.cnt == 0) { 435 rpipe->pipe_buffer.in = 0; 436 rpipe->pipe_buffer.out = 0; 437 } 438 nread += size; 439#ifndef PIPE_NODIRECT 440 /* 441 * Direct copy, bypassing a kernel buffer. 442 */ 443 } else if ((size = rpipe->pipe_map.cnt) && 444 (rpipe->pipe_state & PIPE_DIRECTW)) { 445 caddr_t va; 446 if (size > (u_int) uio->uio_resid) 447 size = (u_int) uio->uio_resid; 448 449 va = (caddr_t) rpipe->pipe_map.kva + 450 rpipe->pipe_map.pos; 451 error = uiomove(va, size, uio); 452 if (error) 453 break; 454 nread += size; 455 rpipe->pipe_map.pos += size; 456 rpipe->pipe_map.cnt -= size; 457 if (rpipe->pipe_map.cnt == 0) { 458 rpipe->pipe_state &= ~PIPE_DIRECTW; 459 wakeup(rpipe); 460 } 461#endif 462 } else { 463 /* 464 * detect EOF condition 465 * read returns 0 on EOF, no need to set error 466 */ 467 if (rpipe->pipe_state & PIPE_EOF) 468 break; 469 470 /* 471 * If the "write-side" has been blocked, wake it up now. 472 */ 473 if (rpipe->pipe_state & PIPE_WANTW) { 474 rpipe->pipe_state &= ~PIPE_WANTW; 475 wakeup(rpipe); 476 } 477 478 /* 479 * Break if some data was read. 480 */ 481 if (nread > 0) 482 break; 483 484 /* 485 * Unlock the pipe buffer for our remaining processing. We 486 * will either break out with an error or we will sleep and 487 * relock to loop. 488 */ 489 pipeunlock(rpipe); 490 491 /* 492 * Handle non-blocking mode operation or 493 * wait for more data. 494 */ 495 if (fp->f_flag & FNONBLOCK) { 496 error = EAGAIN; 497 } else { 498 rpipe->pipe_state |= PIPE_WANTR; 499 if ((error = tsleep(rpipe, PRIBIO|PCATCH, "piperd", 0)) == 0) 500 error = pipelock(rpipe, 1); 501 } 502 if (error) 503 goto unlocked_error; 504 } 505 } 506 pipeunlock(rpipe); 507 508 if (error == 0) 509 vfs_timestamp(&rpipe->pipe_atime); 510unlocked_error: 511 --rpipe->pipe_busy; 512 513 /* 514 * PIPE_WANT processing only makes sense if pipe_busy is 0. 515 */ 516 if ((rpipe->pipe_busy == 0) && (rpipe->pipe_state & PIPE_WANT)) { 517 rpipe->pipe_state &= ~(PIPE_WANT|PIPE_WANTW); 518 wakeup(rpipe); 519 } else if (rpipe->pipe_buffer.cnt < MINPIPESIZE) { 520 /* 521 * Handle write blocking hysteresis. 522 */ 523 if (rpipe->pipe_state & PIPE_WANTW) { 524 rpipe->pipe_state &= ~PIPE_WANTW; 525 wakeup(rpipe); 526 } 527 } 528 529 if ((rpipe->pipe_buffer.size - rpipe->pipe_buffer.cnt) >= PIPE_BUF) 530 pipeselwakeup(rpipe); 531 532 return (error); 533} 534 535#ifndef PIPE_NODIRECT 536/* 537 * Map the sending processes' buffer into kernel space and wire it. 538 * This is similar to a physical write operation. 539 */ 540static int 541pipe_build_write_buffer(wpipe, uio) 542 struct pipe *wpipe; 543 struct uio *uio; 544{ 545 u_int size; 546 int i; 547 vm_offset_t addr, endaddr, paddr; 548 549 size = (u_int) uio->uio_iov->iov_len; 550 if (size > wpipe->pipe_buffer.size) 551 size = wpipe->pipe_buffer.size; 552 553 endaddr = round_page((vm_offset_t)uio->uio_iov->iov_base + size); 554 addr = trunc_page((vm_offset_t)uio->uio_iov->iov_base); 555 for (i = 0; addr < endaddr; addr += PAGE_SIZE, i++) { 556 vm_page_t m; 557 558 if (vm_fault_quick((caddr_t)addr, VM_PROT_READ) < 0 || 559 (paddr = pmap_kextract(addr)) == 0) { 560 int j; 561 562 for (j = 0; j < i; j++) 563 vm_page_unwire(wpipe->pipe_map.ms[j], 1); 564 return (EFAULT); 565 } 566 567 m = PHYS_TO_VM_PAGE(paddr); 568 vm_page_wire(m); 569 wpipe->pipe_map.ms[i] = m; 570 } 571 572/* 573 * set up the control block 574 */ 575 wpipe->pipe_map.npages = i; 576 wpipe->pipe_map.pos = 577 ((vm_offset_t) uio->uio_iov->iov_base) & PAGE_MASK; 578 wpipe->pipe_map.cnt = size; 579 580/* 581 * and map the buffer 582 */ 583 if (wpipe->pipe_map.kva == 0) { 584 /* 585 * We need to allocate space for an extra page because the 586 * address range might (will) span pages at times. 587 */ 588 wpipe->pipe_map.kva = kmem_alloc_pageable(kernel_map, 589 wpipe->pipe_buffer.size + PAGE_SIZE); 590 amountpipekva += wpipe->pipe_buffer.size + PAGE_SIZE; 591 } 592 pmap_qenter(wpipe->pipe_map.kva, wpipe->pipe_map.ms, 593 wpipe->pipe_map.npages); 594 595/* 596 * and update the uio data 597 */ 598 599 uio->uio_iov->iov_len -= size; 600 uio->uio_iov->iov_base += size; 601 if (uio->uio_iov->iov_len == 0) 602 uio->uio_iov++; 603 uio->uio_resid -= size; 604 uio->uio_offset += size; 605 return (0); 606} 607 608/* 609 * unmap and unwire the process buffer 610 */ 611static void 612pipe_destroy_write_buffer(wpipe) 613 struct pipe *wpipe; 614{ 615 int i; 616 617 if (wpipe->pipe_map.kva) { 618 pmap_qremove(wpipe->pipe_map.kva, wpipe->pipe_map.npages); 619 620 if (amountpipekva > MAXPIPEKVA) { 621 vm_offset_t kva = wpipe->pipe_map.kva; 622 wpipe->pipe_map.kva = 0; 623 kmem_free(kernel_map, kva, 624 wpipe->pipe_buffer.size + PAGE_SIZE); 625 amountpipekva -= wpipe->pipe_buffer.size + PAGE_SIZE; 626 } 627 } 628 for (i = 0; i < wpipe->pipe_map.npages; i++) 629 vm_page_unwire(wpipe->pipe_map.ms[i], 1); 630} 631 632/* 633 * In the case of a signal, the writing process might go away. This 634 * code copies the data into the circular buffer so that the source 635 * pages can be freed without loss of data. 636 */ 637static void 638pipe_clone_write_buffer(wpipe) 639 struct pipe *wpipe; 640{ 641 int size; 642 int pos; 643 644 size = wpipe->pipe_map.cnt; 645 pos = wpipe->pipe_map.pos; 646 bcopy((caddr_t) wpipe->pipe_map.kva + pos, 647 (caddr_t) wpipe->pipe_buffer.buffer, size); 648 649 wpipe->pipe_buffer.in = size; 650 wpipe->pipe_buffer.out = 0; 651 wpipe->pipe_buffer.cnt = size; 652 wpipe->pipe_state &= ~PIPE_DIRECTW; 653 654 pipe_destroy_write_buffer(wpipe); 655} 656 657/* 658 * This implements the pipe buffer write mechanism. Note that only 659 * a direct write OR a normal pipe write can be pending at any given time. 660 * If there are any characters in the pipe buffer, the direct write will 661 * be deferred until the receiving process grabs all of the bytes from 662 * the pipe buffer. Then the direct mapping write is set-up. 663 */ 664static int 665pipe_direct_write(wpipe, uio) 666 struct pipe *wpipe; 667 struct uio *uio; 668{ 669 int error; 670 671retry: 672 while (wpipe->pipe_state & PIPE_DIRECTW) { 673 if (wpipe->pipe_state & PIPE_WANTR) { 674 wpipe->pipe_state &= ~PIPE_WANTR; 675 wakeup(wpipe); 676 } 677 wpipe->pipe_state |= PIPE_WANTW; 678 error = tsleep(wpipe, PRIBIO | PCATCH, "pipdww", 0); 679 if (error) 680 goto error1; 681 if (wpipe->pipe_state & PIPE_EOF) { 682 error = EPIPE; 683 goto error1; 684 } 685 } 686 wpipe->pipe_map.cnt = 0; /* transfer not ready yet */ 687 if (wpipe->pipe_buffer.cnt > 0) { 688 if (wpipe->pipe_state & PIPE_WANTR) { 689 wpipe->pipe_state &= ~PIPE_WANTR; 690 wakeup(wpipe); 691 } 692 693 wpipe->pipe_state |= PIPE_WANTW; 694 error = tsleep(wpipe, PRIBIO | PCATCH, "pipdwc", 0); 695 if (error) 696 goto error1; 697 if (wpipe->pipe_state & PIPE_EOF) { 698 error = EPIPE; 699 goto error1; 700 } 701 goto retry; 702 } 703 704 wpipe->pipe_state |= PIPE_DIRECTW; 705 706 error = pipe_build_write_buffer(wpipe, uio); 707 if (error) { 708 wpipe->pipe_state &= ~PIPE_DIRECTW; 709 goto error1; 710 } 711 712 error = 0; 713 while (!error && (wpipe->pipe_state & PIPE_DIRECTW)) { 714 if (wpipe->pipe_state & PIPE_EOF) { 715 pipelock(wpipe, 0); 716 pipe_destroy_write_buffer(wpipe); 717 pipeunlock(wpipe); 718 pipeselwakeup(wpipe); 719 error = EPIPE; 720 goto error1; 721 } 722 if (wpipe->pipe_state & PIPE_WANTR) { 723 wpipe->pipe_state &= ~PIPE_WANTR; 724 wakeup(wpipe); 725 } 726 pipeselwakeup(wpipe); 727 error = tsleep(wpipe, PRIBIO | PCATCH, "pipdwt", 0); 728 } 729 730 pipelock(wpipe,0); 731 if (wpipe->pipe_state & PIPE_DIRECTW) { 732 /* 733 * this bit of trickery substitutes a kernel buffer for 734 * the process that might be going away. 735 */ 736 pipe_clone_write_buffer(wpipe); 737 } else { 738 pipe_destroy_write_buffer(wpipe); 739 } 740 pipeunlock(wpipe); 741 return (error); 742 743error1: 744 wakeup(wpipe); 745 return (error); 746} 747#endif 748 749static int 750pipe_write(fp, uio, cred, flags, p) 751 struct file *fp; 752 struct uio *uio; 753 struct ucred *cred; 754 struct proc *p; 755 int flags; 756{ 757 int error = 0; 758 int orig_resid; 759 struct pipe *wpipe, *rpipe; 760 761 rpipe = (struct pipe *) fp->f_data; 762 wpipe = rpipe->pipe_peer; 763 764 /* 765 * detect loss of pipe read side, issue SIGPIPE if lost. 766 */ 767 if ((wpipe == NULL) || (wpipe->pipe_state & PIPE_EOF)) { 768 return (EPIPE); 769 } 770 771 /* 772 * If it is advantageous to resize the pipe buffer, do 773 * so. 774 */ 775 if ((uio->uio_resid > PIPE_SIZE) && 776 (nbigpipe < LIMITBIGPIPES) && 777 (wpipe->pipe_state & PIPE_DIRECTW) == 0 && 778 (wpipe->pipe_buffer.size <= PIPE_SIZE) && 779 (wpipe->pipe_buffer.cnt == 0)) { 780 781 if ((error = pipelock(wpipe,1)) == 0) { 782 if (pipespace(wpipe, BIG_PIPE_SIZE) == 0) 783 nbigpipe++; 784 pipeunlock(wpipe); 785 } else { 786 return (error); 787 } 788 } 789 790 KASSERT(wpipe->pipe_buffer.buffer != NULL, ("pipe buffer gone")); 791 792 ++wpipe->pipe_busy; 793 orig_resid = uio->uio_resid; 794 while (uio->uio_resid) { 795 int space; 796 797#ifndef PIPE_NODIRECT 798 /* 799 * If the transfer is large, we can gain performance if 800 * we do process-to-process copies directly. 801 * If the write is non-blocking, we don't use the 802 * direct write mechanism. 803 * 804 * The direct write mechanism will detect the reader going 805 * away on us. 806 */ 807 if ((uio->uio_iov->iov_len >= PIPE_MINDIRECT) && 808 (fp->f_flag & FNONBLOCK) == 0 && 809 (wpipe->pipe_map.kva || (amountpipekva < LIMITPIPEKVA)) && 810 (uio->uio_iov->iov_len >= PIPE_MINDIRECT)) { 811 error = pipe_direct_write( wpipe, uio); 812 if (error) 813 break; 814 continue; 815 } 816#endif 817 818 /* 819 * Pipe buffered writes cannot be coincidental with 820 * direct writes. We wait until the currently executing 821 * direct write is completed before we start filling the 822 * pipe buffer. We break out if a signal occurs or the 823 * reader goes away. 824 */ 825 retrywrite: 826 while (wpipe->pipe_state & PIPE_DIRECTW) { 827 if (wpipe->pipe_state & PIPE_WANTR) { 828 wpipe->pipe_state &= ~PIPE_WANTR; 829 wakeup(wpipe); 830 } 831 error = tsleep(wpipe, PRIBIO | PCATCH, "pipbww", 0); 832 if (wpipe->pipe_state & PIPE_EOF) 833 break; 834 if (error) 835 break; 836 } 837 if (wpipe->pipe_state & PIPE_EOF) { 838 error = EPIPE; 839 break; 840 } 841 842 space = wpipe->pipe_buffer.size - wpipe->pipe_buffer.cnt; 843 844 /* Writes of size <= PIPE_BUF must be atomic. */ 845 if ((space < uio->uio_resid) && (orig_resid <= PIPE_BUF)) 846 space = 0; 847 848 if (space > 0 && (wpipe->pipe_buffer.cnt < PIPE_SIZE)) { 849 if ((error = pipelock(wpipe,1)) == 0) { 850 int size; /* Transfer size */ 851 int segsize; /* first segment to transfer */ 852 853 /* 854 * It is possible for a direct write to 855 * slip in on us... handle it here... 856 */ 857 if (wpipe->pipe_state & PIPE_DIRECTW) { 858 pipeunlock(wpipe); 859 goto retrywrite; 860 } 861 /* 862 * If a process blocked in uiomove, our 863 * value for space might be bad. 864 * 865 * XXX will we be ok if the reader has gone 866 * away here? 867 */ 868 if (space > wpipe->pipe_buffer.size - 869 wpipe->pipe_buffer.cnt) { 870 pipeunlock(wpipe); 871 goto retrywrite; 872 } 873 874 /* 875 * Transfer size is minimum of uio transfer 876 * and free space in pipe buffer. 877 */ 878 if (space > uio->uio_resid) 879 size = uio->uio_resid; 880 else 881 size = space; 882 /* 883 * First segment to transfer is minimum of 884 * transfer size and contiguous space in 885 * pipe buffer. If first segment to transfer 886 * is less than the transfer size, we've got 887 * a wraparound in the buffer. 888 */ 889 segsize = wpipe->pipe_buffer.size - 890 wpipe->pipe_buffer.in; 891 if (segsize > size) 892 segsize = size; 893 894 /* Transfer first segment */ 895 896 error = uiomove(&wpipe->pipe_buffer.buffer[wpipe->pipe_buffer.in], 897 segsize, uio); 898 899 if (error == 0 && segsize < size) { 900 /* 901 * Transfer remaining part now, to 902 * support atomic writes. Wraparound 903 * happened. 904 */ 905 if (wpipe->pipe_buffer.in + segsize != 906 wpipe->pipe_buffer.size) 907 panic("Expected pipe buffer wraparound disappeared"); 908 909 error = uiomove(&wpipe->pipe_buffer.buffer[0], 910 size - segsize, uio); 911 } 912 if (error == 0) { 913 wpipe->pipe_buffer.in += size; 914 if (wpipe->pipe_buffer.in >= 915 wpipe->pipe_buffer.size) { 916 if (wpipe->pipe_buffer.in != size - segsize + wpipe->pipe_buffer.size) 917 panic("Expected wraparound bad"); 918 wpipe->pipe_buffer.in = size - segsize; 919 } 920 921 wpipe->pipe_buffer.cnt += size; 922 if (wpipe->pipe_buffer.cnt > wpipe->pipe_buffer.size) 923 panic("Pipe buffer overflow"); 924 925 } 926 pipeunlock(wpipe); 927 } 928 if (error) 929 break; 930 931 } else { 932 /* 933 * If the "read-side" has been blocked, wake it up now. 934 */ 935 if (wpipe->pipe_state & PIPE_WANTR) { 936 wpipe->pipe_state &= ~PIPE_WANTR; 937 wakeup(wpipe); 938 } 939 940 /* 941 * don't block on non-blocking I/O 942 */ 943 if (fp->f_flag & FNONBLOCK) { 944 error = EAGAIN; 945 break; 946 } 947 948 /* 949 * We have no more space and have something to offer, 950 * wake up select/poll. 951 */ 952 pipeselwakeup(wpipe); 953 954 wpipe->pipe_state |= PIPE_WANTW; 955 error = tsleep(wpipe, PRIBIO | PCATCH, "pipewr", 0); 956 if (error != 0) 957 break; 958 /* 959 * If read side wants to go away, we just issue a signal 960 * to ourselves. 961 */ 962 if (wpipe->pipe_state & PIPE_EOF) { 963 error = EPIPE; 964 break; 965 } 966 } 967 } 968 969 --wpipe->pipe_busy; 970 if ((wpipe->pipe_busy == 0) && (wpipe->pipe_state & PIPE_WANT)) { 971 wpipe->pipe_state &= ~(PIPE_WANT | PIPE_WANTR); 972 wakeup(wpipe); 973 } else if (wpipe->pipe_buffer.cnt > 0) { 974 /* 975 * If we have put any characters in the buffer, we wake up 976 * the reader. 977 */ 978 if (wpipe->pipe_state & PIPE_WANTR) { 979 wpipe->pipe_state &= ~PIPE_WANTR; 980 wakeup(wpipe); 981 } 982 } 983 984 /* 985 * Don't return EPIPE if I/O was successful 986 */ 987 if ((wpipe->pipe_buffer.cnt == 0) && 988 (uio->uio_resid == 0) && 989 (error == EPIPE)) 990 error = 0; 991 992 if (error == 0) 993 vfs_timestamp(&wpipe->pipe_mtime); 994 995 /* 996 * We have something to offer, 997 * wake up select/poll. 998 */ 999 if (wpipe->pipe_buffer.cnt) 1000 pipeselwakeup(wpipe); 1001 1002 return (error); 1003} 1004 1005/* 1006 * we implement a very minimal set of ioctls for compatibility with sockets. 1007 */ 1008int 1009pipe_ioctl(fp, cmd, data, p) 1010 struct file *fp; 1011 u_long cmd; 1012 caddr_t data; 1013 struct proc *p; 1014{ 1015 struct pipe *mpipe = (struct pipe *)fp->f_data; 1016 1017 switch (cmd) { 1018 1019 case FIONBIO: 1020 return (0); 1021 1022 case FIOASYNC: 1023 if (*(int *)data) { 1024 mpipe->pipe_state |= PIPE_ASYNC; 1025 } else { 1026 mpipe->pipe_state &= ~PIPE_ASYNC; 1027 } 1028 return (0); 1029 1030 case FIONREAD: 1031 if (mpipe->pipe_state & PIPE_DIRECTW) 1032 *(int *)data = mpipe->pipe_map.cnt; 1033 else 1034 *(int *)data = mpipe->pipe_buffer.cnt; 1035 return (0); 1036 1037 case FIOSETOWN: 1038 return (fsetown(*(int *)data, &mpipe->pipe_sigio)); 1039 1040 case FIOGETOWN: 1041 *(int *)data = fgetown(mpipe->pipe_sigio); 1042 return (0); 1043 1044 /* This is deprecated, FIOSETOWN should be used instead. */ 1045 case TIOCSPGRP: 1046 return (fsetown(-(*(int *)data), &mpipe->pipe_sigio)); 1047 1048 /* This is deprecated, FIOGETOWN should be used instead. */ 1049 case TIOCGPGRP: 1050 *(int *)data = -fgetown(mpipe->pipe_sigio); 1051 return (0); 1052 1053 } 1054 return (ENOTTY); 1055} 1056 1057int 1058pipe_poll(fp, events, cred, p) 1059 struct file *fp; 1060 int events; 1061 struct ucred *cred; 1062 struct proc *p; 1063{ 1064 struct pipe *rpipe = (struct pipe *)fp->f_data; 1065 struct pipe *wpipe; 1066 int revents = 0; 1067 1068 wpipe = rpipe->pipe_peer; 1069 if (events & (POLLIN | POLLRDNORM)) 1070 if ((rpipe->pipe_state & PIPE_DIRECTW) || 1071 (rpipe->pipe_buffer.cnt > 0) || 1072 (rpipe->pipe_state & PIPE_EOF)) 1073 revents |= events & (POLLIN | POLLRDNORM); 1074 1075 if (events & (POLLOUT | POLLWRNORM)) 1076 if (wpipe == NULL || (wpipe->pipe_state & PIPE_EOF) || 1077 (((wpipe->pipe_state & PIPE_DIRECTW) == 0) && 1078 (wpipe->pipe_buffer.size - wpipe->pipe_buffer.cnt) >= PIPE_BUF)) 1079 revents |= events & (POLLOUT | POLLWRNORM); 1080 1081 if ((rpipe->pipe_state & PIPE_EOF) || 1082 (wpipe == NULL) || 1083 (wpipe->pipe_state & PIPE_EOF)) 1084 revents |= POLLHUP; 1085 1086 if (revents == 0) { 1087 if (events & (POLLIN | POLLRDNORM)) { 1088 selrecord(p, &rpipe->pipe_sel); 1089 rpipe->pipe_state |= PIPE_SEL; 1090 } 1091 1092 if (events & (POLLOUT | POLLWRNORM)) { 1093 selrecord(p, &wpipe->pipe_sel); 1094 wpipe->pipe_state |= PIPE_SEL; 1095 } 1096 } 1097 1098 return (revents); 1099} 1100 1101static int 1102pipe_stat(fp, ub, p) 1103 struct file *fp; 1104 struct stat *ub; 1105 struct proc *p; 1106{ 1107 struct pipe *pipe = (struct pipe *)fp->f_data; 1108 1109 bzero((caddr_t)ub, sizeof(*ub)); 1110 ub->st_mode = S_IFIFO; 1111 ub->st_blksize = pipe->pipe_buffer.size; 1112 ub->st_size = pipe->pipe_buffer.cnt; 1113 ub->st_blocks = (ub->st_size + ub->st_blksize - 1) / ub->st_blksize; 1114 ub->st_atimespec = pipe->pipe_atime; 1115 ub->st_mtimespec = pipe->pipe_mtime; 1116 ub->st_ctimespec = pipe->pipe_ctime; 1117 ub->st_uid = fp->f_cred->cr_uid; 1118 ub->st_gid = fp->f_cred->cr_gid; 1119 /* 1120 * Left as 0: st_dev, st_ino, st_nlink, st_rdev, st_flags, st_gen. 1121 * XXX (st_dev, st_ino) should be unique. 1122 */ 1123 return (0); 1124} 1125 1126/* ARGSUSED */ 1127static int 1128pipe_close(fp, p) 1129 struct file *fp; 1130 struct proc *p; 1131{ 1132 struct pipe *cpipe = (struct pipe *)fp->f_data; 1133 1134 fp->f_ops = &badfileops; 1135 fp->f_data = NULL; 1136 funsetown(cpipe->pipe_sigio); 1137 pipeclose(cpipe); 1138 return (0); 1139} 1140 1141static void 1142pipe_free_kmem(cpipe) 1143 struct pipe *cpipe; 1144{ 1145 1146 if (cpipe->pipe_buffer.buffer != NULL) { 1147 if (cpipe->pipe_buffer.size > PIPE_SIZE) 1148 --nbigpipe; 1149 amountpipekva -= cpipe->pipe_buffer.size; 1150 kmem_free(kernel_map, 1151 (vm_offset_t)cpipe->pipe_buffer.buffer, 1152 cpipe->pipe_buffer.size); 1153 cpipe->pipe_buffer.buffer = NULL; 1154 } 1155#ifndef PIPE_NODIRECT 1156 if (cpipe->pipe_map.kva != NULL) { 1157 amountpipekva -= cpipe->pipe_buffer.size + PAGE_SIZE; 1158 kmem_free(kernel_map, 1159 cpipe->pipe_map.kva, 1160 cpipe->pipe_buffer.size + PAGE_SIZE); 1161 cpipe->pipe_map.cnt = 0; 1162 cpipe->pipe_map.kva = 0; 1163 cpipe->pipe_map.pos = 0; 1164 cpipe->pipe_map.npages = 0; 1165 } 1166#endif 1167} 1168 1169/* 1170 * shutdown the pipe 1171 */ 1172static void 1173pipeclose(cpipe) 1174 struct pipe *cpipe; 1175{ 1176 struct pipe *ppipe; 1177 1178 if (cpipe) { 1179 1180 pipeselwakeup(cpipe); 1181 1182 /* 1183 * If the other side is blocked, wake it up saying that 1184 * we want to close it down. 1185 */ 1186 while (cpipe->pipe_busy) { 1187 wakeup(cpipe); 1188 cpipe->pipe_state |= PIPE_WANT | PIPE_EOF; 1189 tsleep(cpipe, PRIBIO, "pipecl", 0); 1190 } 1191 1192 /* 1193 * Disconnect from peer 1194 */ 1195 if ((ppipe = cpipe->pipe_peer) != NULL) { 1196 pipeselwakeup(ppipe); 1197 1198 ppipe->pipe_state |= PIPE_EOF; 1199 wakeup(ppipe); 1200 ppipe->pipe_peer = NULL; 1201 } 1202 1203 /* 1204 * free resources 1205 */ 1206 pipe_free_kmem(cpipe); 1207 zfree(pipe_zone, cpipe); 1208 } 1209} 1210 1211/*ARGSUSED*/ 1212static int 1213pipe_kqfilter(struct file *fp, struct knote *kn) 1214{ 1215 struct pipe *rpipe = (struct pipe *)kn->kn_fp->f_data; 1216 1217 switch (kn->kn_filter) { 1218 case EVFILT_READ: 1219 kn->kn_fop = &pipe_rfiltops; 1220 break; 1221 case EVFILT_WRITE: 1222 kn->kn_fop = &pipe_wfiltops; 1223 break; 1224 default: 1225 return (1); 1226 } 1227 1228 SLIST_INSERT_HEAD(&rpipe->pipe_sel.si_note, kn, kn_selnext); 1229 return (0); 1230} 1231 1232static void 1233filt_pipedetach(struct knote *kn) 1234{ 1235 struct pipe *rpipe = (struct pipe *)kn->kn_fp->f_data; 1236 1237 SLIST_REMOVE(&rpipe->pipe_sel.si_note, kn, knote, kn_selnext); 1238} 1239 1240/*ARGSUSED*/ 1241static int 1242filt_piperead(struct knote *kn, long hint) 1243{ 1244 struct pipe *rpipe = (struct pipe *)kn->kn_fp->f_data; 1245 struct pipe *wpipe = rpipe->pipe_peer; 1246 1247 kn->kn_data = rpipe->pipe_buffer.cnt; 1248 if ((kn->kn_data == 0) && (rpipe->pipe_state & PIPE_DIRECTW)) 1249 kn->kn_data = rpipe->pipe_map.cnt; 1250 1251 if ((rpipe->pipe_state & PIPE_EOF) || 1252 (wpipe == NULL) || (wpipe->pipe_state & PIPE_EOF)) { 1253 kn->kn_flags |= EV_EOF; 1254 return (1); 1255 } 1256 return (kn->kn_data > 0); 1257} 1258 1259/*ARGSUSED*/ 1260static int 1261filt_pipewrite(struct knote *kn, long hint) 1262{ 1263 struct pipe *rpipe = (struct pipe *)kn->kn_fp->f_data; 1264 struct pipe *wpipe = rpipe->pipe_peer; 1265 1266 if ((wpipe == NULL) || (wpipe->pipe_state & PIPE_EOF)) { 1267 kn->kn_data = 0; 1268 kn->kn_flags |= EV_EOF; 1269 return (1); 1270 } 1271 kn->kn_data = wpipe->pipe_buffer.size - wpipe->pipe_buffer.cnt; 1272 if (wpipe->pipe_state & PIPE_DIRECTW) 1273 kn->kn_data = 0; 1274 1275 return (kn->kn_data >= PIPE_BUF); 1276} 1277