2 * Copyright (c) 1996 John S. Dyson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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
16 * 4. Modifications may be freely made to this file if the above conditions
19 * $FreeBSD: src/sys/kern/sys_pipe.c,v 1.60.2.13 2002/08/05 15:05:15 des Exp $
20 * $DragonFly: src/sys/kern/sys_pipe.c,v 1.37 2006/05/26 00:33:09 dillon Exp $
24 * This file contains a high-performance replacement for the socket-based
25 * pipes scheme originally used in FreeBSD/4.4Lite. It does not support
26 * all features of sockets, but does do everything that pipes normally
31 * This code has two modes of operation, a small write mode and a large
32 * write mode. The small write mode acts like conventional pipes with
33 * a kernel buffer. If the buffer is less than PIPE_MINDIRECT, then the
34 * "normal" pipe buffering is done. If the buffer is between PIPE_MINDIRECT
35 * and PIPE_SIZE in size, it is fully mapped and wired into the kernel, and
36 * the receiving process can copy it directly from the pages in the sending
39 * If the sending process receives a signal, it is possible that it will
40 * go away, and certainly its address space can change, because control
41 * is returned back to the user-mode side. In that case, the pipe code
42 * arranges to copy the buffer supplied by the user process, to a pageable
43 * kernel buffer, and the receiving process will grab the data from the
44 * pageable kernel buffer. Since signals don't happen all that often,
45 * the copy operation is normally eliminated.
47 * The constant PIPE_MINDIRECT is chosen to make sure that buffering will
48 * happen for small transfers so that the system will not spend all of
49 * its time context switching. PIPE_SIZE is constrained by the
50 * amount of kernel virtual memory.
53 #include <sys/param.h>
54 #include <sys/systm.h>
55 #include <sys/kernel.h>
57 #include <sys/fcntl.h>
59 #include <sys/filedesc.h>
60 #include <sys/filio.h>
61 #include <sys/ttycom.h>
64 #include <sys/select.h>
65 #include <sys/signalvar.h>
66 #include <sys/sysproto.h>
68 #include <sys/vnode.h>
70 #include <sys/event.h>
71 #include <sys/globaldata.h>
72 #include <sys/module.h>
73 #include <sys/malloc.h>
74 #include <sys/sysctl.h>
75 #include <sys/socket.h>
78 #include <vm/vm_param.h>
80 #include <vm/vm_object.h>
81 #include <vm/vm_kern.h>
82 #include <vm/vm_extern.h>
84 #include <vm/vm_map.h>
85 #include <vm/vm_page.h>
86 #include <vm/vm_zone.h>
88 #include <sys/file2.h>
90 #include <machine/cpufunc.h>
93 * interfaces to the outside world
95 static int pipe_read (struct file *fp, struct uio *uio,
96 struct ucred *cred, int flags);
97 static int pipe_write (struct file *fp, struct uio *uio,
98 struct ucred *cred, int flags);
99 static int pipe_close (struct file *fp);
100 static int pipe_shutdown (struct file *fp, int how);
101 static int pipe_poll (struct file *fp, int events, struct ucred *cred);
102 static int pipe_kqfilter (struct file *fp, struct knote *kn);
103 static int pipe_stat (struct file *fp, struct stat *sb, struct ucred *cred);
104 static int pipe_ioctl (struct file *fp, u_long cmd, caddr_t data, struct ucred *cred);
106 static struct fileops pipeops = {
109 pipe_read, pipe_write, pipe_ioctl, pipe_poll, pipe_kqfilter,
110 pipe_stat, pipe_close, pipe_shutdown
113 static void filt_pipedetach(struct knote *kn);
114 static int filt_piperead(struct knote *kn, long hint);
115 static int filt_pipewrite(struct knote *kn, long hint);
117 static struct filterops pipe_rfiltops =
118 { 1, NULL, filt_pipedetach, filt_piperead };
119 static struct filterops pipe_wfiltops =
120 { 1, NULL, filt_pipedetach, filt_pipewrite };
122 MALLOC_DEFINE(M_PIPE, "pipe", "pipe structures");
125 * Default pipe buffer size(s), this can be kind-of large now because pipe
126 * space is pageable. The pipe code will try to maintain locality of
127 * reference for performance reasons, so small amounts of outstanding I/O
128 * will not wipe the cache.
130 #define MINPIPESIZE (PIPE_SIZE/3)
131 #define MAXPIPESIZE (2*PIPE_SIZE/3)
134 * Maximum amount of kva for pipes -- this is kind-of a soft limit, but
135 * is there so that on large systems, we don't exhaust it.
137 #define MAXPIPEKVA (8*1024*1024)
140 * Limit for direct transfers, we cannot, of course limit
141 * the amount of kva for pipes in general though.
143 #define LIMITPIPEKVA (16*1024*1024)
146 * Limit the number of "big" pipes
148 #define LIMITBIGPIPES 32
149 #define PIPEQ_MAX_CACHE 16 /* per-cpu pipe structure cache */
151 static int pipe_maxbig = LIMITBIGPIPES;
152 static int pipe_maxcache = PIPEQ_MAX_CACHE;
153 static int pipe_nbig;
154 static int pipe_bcache_alloc;
155 static int pipe_bkmem_alloc;
156 static int pipe_dwrite_enable = 1; /* 0:copy, 1:kmem/sfbuf 2:force */
157 static int pipe_dwrite_sfbuf = 1; /* 0:kmem_map 1:sfbufs 2:sfbufs_dmap */
158 /* 3:sfbuf_dmap w/ forced invlpg */
160 SYSCTL_NODE(_kern, OID_AUTO, pipe, CTLFLAG_RW, 0, "Pipe operation");
161 SYSCTL_INT(_kern_pipe, OID_AUTO, nbig,
162 CTLFLAG_RD, &pipe_nbig, 0, "numer of big pipes allocated");
163 SYSCTL_INT(_kern_pipe, OID_AUTO, maxcache,
164 CTLFLAG_RW, &pipe_maxcache, 0, "max pipes cached per-cpu");
165 SYSCTL_INT(_kern_pipe, OID_AUTO, maxbig,
166 CTLFLAG_RW, &pipe_maxbig, 0, "max number of big pipes");
167 SYSCTL_INT(_kern_pipe, OID_AUTO, dwrite_enable,
168 CTLFLAG_RW, &pipe_dwrite_enable, 0, "1:enable/2:force direct writes");
169 SYSCTL_INT(_kern_pipe, OID_AUTO, dwrite_sfbuf,
170 CTLFLAG_RW, &pipe_dwrite_sfbuf, 0,
171 "(if dwrite_enable) 0:kmem 1:sfbuf 2:sfbuf_dmap 3:sfbuf_dmap_forceinvlpg");
172 #if !defined(NO_PIPE_SYSCTL_STATS)
173 SYSCTL_INT(_kern_pipe, OID_AUTO, bcache_alloc,
174 CTLFLAG_RW, &pipe_bcache_alloc, 0, "pipe buffer from pcpu cache");
175 SYSCTL_INT(_kern_pipe, OID_AUTO, bkmem_alloc,
176 CTLFLAG_RW, &pipe_bkmem_alloc, 0, "pipe buffer from kmem");
179 static void pipeclose (struct pipe *cpipe);
180 static void pipe_free_kmem (struct pipe *cpipe);
181 static int pipe_create (struct pipe **cpipep);
182 static __inline int pipelock (struct pipe *cpipe, int catch);
183 static __inline void pipeunlock (struct pipe *cpipe);
184 static __inline void pipeselwakeup (struct pipe *cpipe);
185 #ifndef PIPE_NODIRECT
186 static int pipe_build_write_buffer (struct pipe *wpipe, struct uio *uio);
187 static int pipe_direct_write (struct pipe *wpipe, struct uio *uio);
188 static void pipe_clone_write_buffer (struct pipe *wpipe);
190 static int pipespace (struct pipe *cpipe, int size);
193 * The pipe system call for the DTYPE_PIPE type of pipes
195 * pipe_ARgs(int dummy)
200 pipe(struct pipe_args *uap)
202 struct thread *td = curthread;
203 struct proc *p = td->td_proc;
204 struct file *rf, *wf;
205 struct pipe *rpipe, *wpipe;
210 rpipe = wpipe = NULL;
211 if (pipe_create(&rpipe) || pipe_create(&wpipe)) {
217 rpipe->pipe_state |= PIPE_DIRECTOK;
218 wpipe->pipe_state |= PIPE_DIRECTOK;
221 * Select the direct-map features to use for this pipe. Since the
222 * sysctl's can change on the fly we record the settings when the
225 * Generally speaking the system will default to what we consider
226 * to be the best-balanced and most stable option. Right now this
227 * is SFBUF1. Modes 2 and 3 are considered experiemental at the
230 wpipe->pipe_feature = PIPE_COPY;
231 if (pipe_dwrite_enable) {
232 switch(pipe_dwrite_sfbuf) {
234 wpipe->pipe_feature = PIPE_KMEM;
237 wpipe->pipe_feature = PIPE_SFBUF1;
241 wpipe->pipe_feature = PIPE_SFBUF2;
245 rpipe->pipe_feature = wpipe->pipe_feature;
247 error = falloc(p, &rf, &fd1);
253 uap->sysmsg_fds[0] = fd1;
256 * Warning: once we've gotten past allocation of the fd for the
257 * read-side, we can only drop the read side via fdrop() in order
258 * to avoid races against processes which manage to dup() the read
259 * side while we are blocked trying to allocate the write side.
261 rf->f_type = DTYPE_PIPE;
262 rf->f_flag = FREAD | FWRITE;
263 rf->f_ops = &pipeops;
265 error = falloc(p, &wf, &fd2);
267 fsetfd(p, NULL, fd1);
269 /* rpipe has been closed by fdrop(). */
273 wf->f_type = DTYPE_PIPE;
274 wf->f_flag = FREAD | FWRITE;
275 wf->f_ops = &pipeops;
277 uap->sysmsg_fds[1] = fd2;
279 rpipe->pipe_peer = wpipe;
280 wpipe->pipe_peer = rpipe;
291 * Allocate kva for pipe circular buffer, the space is pageable
292 * This routine will 'realloc' the size of a pipe safely, if it fails
293 * it will retain the old buffer.
294 * If it fails it will return ENOMEM.
297 pipespace(struct pipe *cpipe, int size)
299 struct vm_object *object;
303 npages = round_page(size) / PAGE_SIZE;
304 object = cpipe->pipe_buffer.object;
307 * [re]create the object if necessary and reserve space for it
308 * in the kernel_map. The object and memory are pageable. On
309 * success, free the old resources before assigning the new
312 if (object == NULL || object->size != npages) {
313 object = vm_object_allocate(OBJT_DEFAULT, npages);
314 buffer = (caddr_t) vm_map_min(kernel_map);
316 error = vm_map_find(kernel_map, object, 0,
317 (vm_offset_t *) &buffer, size, 1,
318 VM_PROT_ALL, VM_PROT_ALL, 0);
320 if (error != KERN_SUCCESS) {
321 vm_object_deallocate(object);
324 pipe_free_kmem(cpipe);
325 cpipe->pipe_buffer.object = object;
326 cpipe->pipe_buffer.buffer = buffer;
327 cpipe->pipe_buffer.size = size;
332 cpipe->pipe_buffer.in = 0;
333 cpipe->pipe_buffer.out = 0;
334 cpipe->pipe_buffer.cnt = 0;
339 * Initialize and allocate VM and memory for pipe, pulling the pipe from
340 * our per-cpu cache if possible. For now make sure it is sized for the
341 * smaller PIPE_SIZE default.
345 struct pipe **cpipep;
347 globaldata_t gd = mycpu;
351 if ((cpipe = gd->gd_pipeq) != NULL) {
352 gd->gd_pipeq = cpipe->pipe_peer;
354 cpipe->pipe_peer = NULL;
356 cpipe = malloc(sizeof(struct pipe), M_PIPE, M_WAITOK|M_ZERO);
359 if ((error = pipespace(cpipe, PIPE_SIZE)) != 0)
361 vfs_timestamp(&cpipe->pipe_ctime);
362 cpipe->pipe_atime = cpipe->pipe_ctime;
363 cpipe->pipe_mtime = cpipe->pipe_ctime;
369 * lock a pipe for I/O, blocking other access
372 pipelock(cpipe, catch)
378 while (cpipe->pipe_state & PIPE_LOCK) {
379 cpipe->pipe_state |= PIPE_LWANT;
380 error = tsleep(cpipe, (catch ? PCATCH : 0), "pipelk", 0);
384 cpipe->pipe_state |= PIPE_LOCK;
389 * unlock a pipe I/O lock
396 cpipe->pipe_state &= ~PIPE_LOCK;
397 if (cpipe->pipe_state & PIPE_LWANT) {
398 cpipe->pipe_state &= ~PIPE_LWANT;
408 if (cpipe->pipe_state & PIPE_SEL) {
409 cpipe->pipe_state &= ~PIPE_SEL;
410 selwakeup(&cpipe->pipe_sel);
412 if ((cpipe->pipe_state & PIPE_ASYNC) && cpipe->pipe_sigio)
413 pgsigio(cpipe->pipe_sigio, SIGIO, 0);
414 KNOTE(&cpipe->pipe_sel.si_note, 0);
418 * MPALMOSTSAFE (acquires mplock)
421 pipe_read(struct file *fp, struct uio *uio, struct ucred *cred, int flags)
429 rpipe = (struct pipe *) fp->f_data;
431 error = pipelock(rpipe, 1);
435 while (uio->uio_resid) {
438 if (rpipe->pipe_buffer.cnt > 0) {
440 * normal pipe buffer receive
442 size = rpipe->pipe_buffer.size - rpipe->pipe_buffer.out;
443 if (size > rpipe->pipe_buffer.cnt)
444 size = rpipe->pipe_buffer.cnt;
445 if (size > (u_int) uio->uio_resid)
446 size = (u_int) uio->uio_resid;
448 error = uiomove(&rpipe->pipe_buffer.buffer[rpipe->pipe_buffer.out],
453 rpipe->pipe_buffer.out += size;
454 if (rpipe->pipe_buffer.out >= rpipe->pipe_buffer.size)
455 rpipe->pipe_buffer.out = 0;
457 rpipe->pipe_buffer.cnt -= size;
460 * If there is no more to read in the pipe, reset
461 * its pointers to the beginning. This improves
464 if (rpipe->pipe_buffer.cnt == 0) {
465 rpipe->pipe_buffer.in = 0;
466 rpipe->pipe_buffer.out = 0;
469 #ifndef PIPE_NODIRECT
470 } else if (rpipe->pipe_kva &&
471 rpipe->pipe_feature == PIPE_KMEM &&
472 (rpipe->pipe_state & (PIPE_DIRECTW|PIPE_DIRECTIP))
476 * Direct copy using source-side kva mapping
478 size = rpipe->pipe_map.xio_bytes -
479 rpipe->pipe_buffer.out;
480 if (size > (u_int)uio->uio_resid)
481 size = (u_int)uio->uio_resid;
482 va = (caddr_t)rpipe->pipe_kva +
483 xio_kvaoffset(&rpipe->pipe_map, rpipe->pipe_buffer.out);
484 error = uiomove(va, size, uio);
488 rpipe->pipe_buffer.out += size;
489 if (rpipe->pipe_buffer.out == rpipe->pipe_map.xio_bytes) {
490 rpipe->pipe_state |= PIPE_DIRECTIP;
491 rpipe->pipe_state &= ~PIPE_DIRECTW;
492 /* reset out index for copy mode */
493 rpipe->pipe_buffer.out = 0;
496 } else if (rpipe->pipe_buffer.out != rpipe->pipe_map.xio_bytes &&
498 rpipe->pipe_feature == PIPE_SFBUF2 &&
499 (rpipe->pipe_state & (PIPE_DIRECTW|PIPE_DIRECTIP))
503 * Direct copy, bypassing a kernel buffer. We cannot
504 * mess with the direct-write buffer until
505 * PIPE_DIRECTIP is cleared. In order to prevent
506 * the pipe_write code from racing itself in
507 * direct_write, we set DIRECTIP when we clear
508 * DIRECTW after we have exhausted the buffer.
510 if (pipe_dwrite_sfbuf == 3)
511 rpipe->pipe_kvamask = 0;
512 pmap_qenter2(rpipe->pipe_kva, rpipe->pipe_map.xio_pages,
513 rpipe->pipe_map.xio_npages,
514 &rpipe->pipe_kvamask);
515 size = rpipe->pipe_map.xio_bytes -
516 rpipe->pipe_buffer.out;
517 if (size > (u_int)uio->uio_resid)
518 size = (u_int)uio->uio_resid;
519 va = (caddr_t)rpipe->pipe_kva + xio_kvaoffset(&rpipe->pipe_map, rpipe->pipe_buffer.out);
520 error = uiomove(va, size, uio);
524 rpipe->pipe_buffer.out += size;
525 if (rpipe->pipe_buffer.out == rpipe->pipe_map.xio_bytes) {
526 rpipe->pipe_state |= PIPE_DIRECTIP;
527 rpipe->pipe_state &= ~PIPE_DIRECTW;
528 /* reset out index for copy mode */
529 rpipe->pipe_buffer.out = 0;
532 } else if (rpipe->pipe_buffer.out != rpipe->pipe_map.xio_bytes &&
533 rpipe->pipe_feature == PIPE_SFBUF1 &&
534 (rpipe->pipe_state & (PIPE_DIRECTW|PIPE_DIRECTIP))
538 * Direct copy, bypassing a kernel buffer. We cannot
539 * mess with the direct-write buffer until
540 * PIPE_DIRECTIP is cleared. In order to prevent
541 * the pipe_write code from racing itself in
542 * direct_write, we set DIRECTIP when we clear
543 * DIRECTW after we have exhausted the buffer.
545 error = xio_uio_copy(&rpipe->pipe_map, rpipe->pipe_buffer.out, uio, &size);
549 rpipe->pipe_buffer.out += size;
550 if (rpipe->pipe_buffer.out == rpipe->pipe_map.xio_bytes) {
551 rpipe->pipe_state |= PIPE_DIRECTIP;
552 rpipe->pipe_state &= ~PIPE_DIRECTW;
553 /* reset out index for copy mode */
554 rpipe->pipe_buffer.out = 0;
560 * detect EOF condition
561 * read returns 0 on EOF, no need to set error
563 if (rpipe->pipe_state & PIPE_EOF)
567 * If the "write-side" has been blocked, wake it up now.
569 if (rpipe->pipe_state & PIPE_WANTW) {
570 rpipe->pipe_state &= ~PIPE_WANTW;
575 * Break if some data was read.
581 * Unlock the pipe buffer for our remaining
582 * processing. We will either break out with an
583 * error or we will sleep and relock to loop.
588 * Handle non-blocking mode operation or
589 * wait for more data.
591 if (fp->f_flag & FNONBLOCK) {
594 rpipe->pipe_state |= PIPE_WANTR;
595 if ((error = tsleep(rpipe, PCATCH|PNORESCHED,
596 "piperd", 0)) == 0) {
597 error = pipelock(rpipe, 1);
607 vfs_timestamp(&rpipe->pipe_atime);
612 * PIPE_WANT processing only makes sense if pipe_busy is 0.
614 if ((rpipe->pipe_busy == 0) && (rpipe->pipe_state & PIPE_WANT)) {
615 rpipe->pipe_state &= ~(PIPE_WANT|PIPE_WANTW);
617 } else if (rpipe->pipe_buffer.cnt < MINPIPESIZE) {
619 * Handle write blocking hysteresis.
621 if (rpipe->pipe_state & PIPE_WANTW) {
622 rpipe->pipe_state &= ~PIPE_WANTW;
627 if ((rpipe->pipe_buffer.size - rpipe->pipe_buffer.cnt) >= PIPE_BUF)
628 pipeselwakeup(rpipe);
633 #ifndef PIPE_NODIRECT
635 * Map the sending processes' buffer into kernel space and wire it.
636 * This is similar to a physical write operation.
639 pipe_build_write_buffer(wpipe, uio)
646 size = (u_int) uio->uio_iov->iov_len;
647 if (size > wpipe->pipe_buffer.size)
648 size = wpipe->pipe_buffer.size;
650 if (uio->uio_segflg == UIO_SYSSPACE) {
651 error = xio_init_kbuf(&wpipe->pipe_map, uio->uio_iov->iov_base,
654 error = xio_init_ubuf(&wpipe->pipe_map, uio->uio_iov->iov_base,
657 wpipe->pipe_buffer.out = 0;
662 * Create a kernel map for KMEM and SFBUF2 copy modes. SFBUF2 will
663 * map the pages on the target while KMEM maps the pages now.
665 switch(wpipe->pipe_feature) {
668 if (wpipe->pipe_kva == NULL) {
670 kmem_alloc_nofault(kernel_map, XIO_INTERNAL_SIZE);
671 wpipe->pipe_kvamask = 0;
673 if (wpipe->pipe_feature == PIPE_KMEM) {
674 pmap_qenter(wpipe->pipe_kva, wpipe->pipe_map.xio_pages,
675 wpipe->pipe_map.xio_npages);
683 * And update the uio data. The XIO might have loaded fewer bytes
684 * then requested so reload 'size'.
686 size = wpipe->pipe_map.xio_bytes;
687 uio->uio_iov->iov_len -= size;
688 uio->uio_iov->iov_base += size;
689 if (uio->uio_iov->iov_len == 0)
691 uio->uio_resid -= size;
692 uio->uio_offset += size;
697 * In the case of a signal, the writing process might go away. This
698 * code copies the data into the circular buffer so that the source
699 * pages can be freed without loss of data.
701 * Note that in direct mode pipe_buffer.out is used to track the
702 * XIO offset. We are converting the direct mode into buffered mode
703 * which changes the meaning of pipe_buffer.out.
706 pipe_clone_write_buffer(wpipe)
712 offset = wpipe->pipe_buffer.out;
713 size = wpipe->pipe_map.xio_bytes - offset;
715 KKASSERT(size <= wpipe->pipe_buffer.size);
717 wpipe->pipe_buffer.in = size;
718 wpipe->pipe_buffer.out = 0;
719 wpipe->pipe_buffer.cnt = size;
720 wpipe->pipe_state &= ~(PIPE_DIRECTW | PIPE_DIRECTIP);
722 xio_copy_xtok(&wpipe->pipe_map, offset, wpipe->pipe_buffer.buffer, size);
723 xio_release(&wpipe->pipe_map);
724 if (wpipe->pipe_kva) {
725 pmap_qremove(wpipe->pipe_kva, XIO_INTERNAL_PAGES);
726 kmem_free(kernel_map, wpipe->pipe_kva, XIO_INTERNAL_SIZE);
727 wpipe->pipe_kva = NULL;
732 * This implements the pipe buffer write mechanism. Note that only
733 * a direct write OR a normal pipe write can be pending at any given time.
734 * If there are any characters in the pipe buffer, the direct write will
735 * be deferred until the receiving process grabs all of the bytes from
736 * the pipe buffer. Then the direct mapping write is set-up.
739 pipe_direct_write(wpipe, uio)
746 while (wpipe->pipe_state & (PIPE_DIRECTW|PIPE_DIRECTIP)) {
747 if (wpipe->pipe_state & PIPE_WANTR) {
748 wpipe->pipe_state &= ~PIPE_WANTR;
751 wpipe->pipe_state |= PIPE_WANTW;
752 error = tsleep(wpipe, PCATCH, "pipdww", 0);
755 if (wpipe->pipe_state & PIPE_EOF) {
760 KKASSERT(wpipe->pipe_map.xio_bytes == 0);
761 if (wpipe->pipe_buffer.cnt > 0) {
762 if (wpipe->pipe_state & PIPE_WANTR) {
763 wpipe->pipe_state &= ~PIPE_WANTR;
767 wpipe->pipe_state |= PIPE_WANTW;
768 error = tsleep(wpipe, PCATCH, "pipdwc", 0);
771 if (wpipe->pipe_state & PIPE_EOF) {
779 * Build our direct-write buffer
781 wpipe->pipe_state |= PIPE_DIRECTW | PIPE_DIRECTIP;
782 error = pipe_build_write_buffer(wpipe, uio);
785 wpipe->pipe_state &= ~PIPE_DIRECTIP;
788 * Wait until the receiver has snarfed the data. Since we are likely
789 * going to sleep we optimize the case and yield synchronously,
790 * possibly avoiding the tsleep().
793 while (!error && (wpipe->pipe_state & PIPE_DIRECTW)) {
794 if (wpipe->pipe_state & PIPE_EOF) {
796 xio_release(&wpipe->pipe_map);
797 if (wpipe->pipe_kva) {
798 pmap_qremove(wpipe->pipe_kva, XIO_INTERNAL_PAGES);
799 kmem_free(kernel_map, wpipe->pipe_kva, XIO_INTERNAL_SIZE);
800 wpipe->pipe_kva = NULL;
803 pipeselwakeup(wpipe);
807 if (wpipe->pipe_state & PIPE_WANTR) {
808 wpipe->pipe_state &= ~PIPE_WANTR;
811 pipeselwakeup(wpipe);
812 error = tsleep(wpipe, PCATCH|PNORESCHED, "pipdwt", 0);
815 if (wpipe->pipe_state & PIPE_DIRECTW) {
817 * this bit of trickery substitutes a kernel buffer for
818 * the process that might be going away.
820 pipe_clone_write_buffer(wpipe);
821 KKASSERT((wpipe->pipe_state & PIPE_DIRECTIP) == 0);
824 * note: The pipe_kva mapping is not qremove'd here. For
825 * legacy PIPE_KMEM mode this constitutes an improvement
826 * over the original FreeBSD-4 algorithm. For PIPE_SFBUF2
827 * mode the kva mapping must not be removed to get the
830 * For testing purposes we will give the original algorithm
831 * the benefit of the doubt 'what it could have been', and
832 * keep the optimization.
834 KKASSERT(wpipe->pipe_state & PIPE_DIRECTIP);
835 xio_release(&wpipe->pipe_map);
836 wpipe->pipe_state &= ~PIPE_DIRECTIP;
842 * Direct-write error, clear the direct write flags.
845 wpipe->pipe_state &= ~(PIPE_DIRECTW | PIPE_DIRECTIP);
849 * General error, wakeup the other side if it happens to be sleeping.
858 * MPALMOSTSAFE - acquires mplock
861 pipe_write(struct file *fp, struct uio *uio, struct ucred *cred, int flags)
865 struct pipe *wpipe, *rpipe;
868 rpipe = (struct pipe *) fp->f_data;
869 wpipe = rpipe->pipe_peer;
872 * detect loss of pipe read side, issue SIGPIPE if lost.
874 if ((wpipe == NULL) || (wpipe->pipe_state & PIPE_EOF)) {
881 * If it is advantageous to resize the pipe buffer, do
884 if ((uio->uio_resid > PIPE_SIZE) &&
885 (pipe_nbig < pipe_maxbig) &&
886 (wpipe->pipe_state & (PIPE_DIRECTW|PIPE_DIRECTIP)) == 0 &&
887 (wpipe->pipe_buffer.size <= PIPE_SIZE) &&
888 (wpipe->pipe_buffer.cnt == 0)) {
890 if ((error = pipelock(wpipe,1)) == 0) {
891 if (pipespace(wpipe, BIG_PIPE_SIZE) == 0)
898 * If an early error occured unbusy and return, waking up any pending
903 if ((wpipe->pipe_busy == 0) &&
904 (wpipe->pipe_state & PIPE_WANT)) {
905 wpipe->pipe_state &= ~(PIPE_WANT | PIPE_WANTR);
912 KASSERT(wpipe->pipe_buffer.buffer != NULL, ("pipe buffer gone"));
914 orig_resid = uio->uio_resid;
916 while (uio->uio_resid) {
919 #ifndef PIPE_NODIRECT
921 * If the transfer is large, we can gain performance if
922 * we do process-to-process copies directly.
923 * If the write is non-blocking, we don't use the
924 * direct write mechanism.
926 * The direct write mechanism will detect the reader going
929 if ((uio->uio_iov->iov_len >= PIPE_MINDIRECT ||
930 pipe_dwrite_enable > 1) &&
931 (fp->f_flag & FNONBLOCK) == 0 &&
932 pipe_dwrite_enable) {
933 error = pipe_direct_write( wpipe, uio);
941 * Pipe buffered writes cannot be coincidental with
942 * direct writes. We wait until the currently executing
943 * direct write is completed before we start filling the
944 * pipe buffer. We break out if a signal occurs or the
948 while (wpipe->pipe_state & (PIPE_DIRECTW|PIPE_DIRECTIP)) {
949 if (wpipe->pipe_state & PIPE_WANTR) {
950 wpipe->pipe_state &= ~PIPE_WANTR;
953 error = tsleep(wpipe, PCATCH, "pipbww", 0);
954 if (wpipe->pipe_state & PIPE_EOF)
959 if (wpipe->pipe_state & PIPE_EOF) {
964 space = wpipe->pipe_buffer.size - wpipe->pipe_buffer.cnt;
966 /* Writes of size <= PIPE_BUF must be atomic. */
967 if ((space < uio->uio_resid) && (orig_resid <= PIPE_BUF))
971 * Write to fill, read size handles write hysteresis. Also
972 * additional restrictions can cause select-based non-blocking
976 if ((error = pipelock(wpipe,1)) == 0) {
977 int size; /* Transfer size */
978 int segsize; /* first segment to transfer */
981 * It is possible for a direct write to
982 * slip in on us... handle it here...
984 if (wpipe->pipe_state & (PIPE_DIRECTW|PIPE_DIRECTIP)) {
989 * If a process blocked in uiomove, our
990 * value for space might be bad.
992 * XXX will we be ok if the reader has gone
995 if (space > wpipe->pipe_buffer.size -
996 wpipe->pipe_buffer.cnt) {
1002 * Transfer size is minimum of uio transfer
1003 * and free space in pipe buffer.
1005 if (space > uio->uio_resid)
1006 size = uio->uio_resid;
1010 * First segment to transfer is minimum of
1011 * transfer size and contiguous space in
1012 * pipe buffer. If first segment to transfer
1013 * is less than the transfer size, we've got
1014 * a wraparound in the buffer.
1016 segsize = wpipe->pipe_buffer.size -
1017 wpipe->pipe_buffer.in;
1021 /* Transfer first segment */
1023 error = uiomove(&wpipe->pipe_buffer.buffer[wpipe->pipe_buffer.in],
1026 if (error == 0 && segsize < size) {
1028 * Transfer remaining part now, to
1029 * support atomic writes. Wraparound
1032 if (wpipe->pipe_buffer.in + segsize !=
1033 wpipe->pipe_buffer.size)
1034 panic("Expected pipe buffer wraparound disappeared");
1036 error = uiomove(&wpipe->pipe_buffer.buffer[0],
1037 size - segsize, uio);
1040 wpipe->pipe_buffer.in += size;
1041 if (wpipe->pipe_buffer.in >=
1042 wpipe->pipe_buffer.size) {
1043 if (wpipe->pipe_buffer.in != size - segsize + wpipe->pipe_buffer.size)
1044 panic("Expected wraparound bad");
1045 wpipe->pipe_buffer.in = size - segsize;
1048 wpipe->pipe_buffer.cnt += size;
1049 if (wpipe->pipe_buffer.cnt > wpipe->pipe_buffer.size)
1050 panic("Pipe buffer overflow");
1060 * If the "read-side" has been blocked, wake it up now
1061 * and yield to let it drain synchronously rather
1064 if (wpipe->pipe_state & PIPE_WANTR) {
1065 wpipe->pipe_state &= ~PIPE_WANTR;
1070 * don't block on non-blocking I/O
1072 if (fp->f_flag & FNONBLOCK) {
1078 * We have no more space and have something to offer,
1079 * wake up select/poll.
1081 pipeselwakeup(wpipe);
1083 wpipe->pipe_state |= PIPE_WANTW;
1084 error = tsleep(wpipe, PCATCH|PNORESCHED, "pipewr", 0);
1088 * If read side wants to go away, we just issue a signal
1091 if (wpipe->pipe_state & PIPE_EOF) {
1100 if ((wpipe->pipe_busy == 0) && (wpipe->pipe_state & PIPE_WANT)) {
1101 wpipe->pipe_state &= ~(PIPE_WANT | PIPE_WANTR);
1103 } else if (wpipe->pipe_buffer.cnt > 0) {
1105 * If we have put any characters in the buffer, we wake up
1108 if (wpipe->pipe_state & PIPE_WANTR) {
1109 wpipe->pipe_state &= ~PIPE_WANTR;
1115 * Don't return EPIPE if I/O was successful
1117 if ((wpipe->pipe_buffer.cnt == 0) &&
1118 (uio->uio_resid == 0) &&
1124 vfs_timestamp(&wpipe->pipe_mtime);
1127 * We have something to offer,
1128 * wake up select/poll.
1130 if (wpipe->pipe_buffer.cnt)
1131 pipeselwakeup(wpipe);
1137 * MPALMOSTSAFE - acquires mplock
1139 * we implement a very minimal set of ioctls for compatibility with sockets.
1142 pipe_ioctl(struct file *fp, u_long cmd, caddr_t data, struct ucred *cred)
1148 mpipe = (struct pipe *)fp->f_data;
1156 mpipe->pipe_state |= PIPE_ASYNC;
1158 mpipe->pipe_state &= ~PIPE_ASYNC;
1163 if (mpipe->pipe_state & PIPE_DIRECTW) {
1164 *(int *)data = mpipe->pipe_map.xio_bytes -
1165 mpipe->pipe_buffer.out;
1167 *(int *)data = mpipe->pipe_buffer.cnt;
1172 error = fsetown(*(int *)data, &mpipe->pipe_sigio);
1175 *(int *)data = fgetown(mpipe->pipe_sigio);
1179 /* This is deprecated, FIOSETOWN should be used instead. */
1180 error = fsetown(-(*(int *)data), &mpipe->pipe_sigio);
1184 /* This is deprecated, FIOGETOWN should be used instead. */
1185 *(int *)data = -fgetown(mpipe->pipe_sigio);
1197 * MPALMOSTSAFE - acquires mplock
1200 pipe_poll(struct file *fp, int events, struct ucred *cred)
1207 rpipe = (struct pipe *)fp->f_data;
1208 wpipe = rpipe->pipe_peer;
1209 if (events & (POLLIN | POLLRDNORM))
1210 if ((rpipe->pipe_state & PIPE_DIRECTW) ||
1211 (rpipe->pipe_buffer.cnt > 0) ||
1212 (rpipe->pipe_state & PIPE_EOF))
1213 revents |= events & (POLLIN | POLLRDNORM);
1215 if (events & (POLLOUT | POLLWRNORM))
1216 if (wpipe == NULL || (wpipe->pipe_state & PIPE_EOF) ||
1217 (((wpipe->pipe_state & PIPE_DIRECTW) == 0) &&
1218 (wpipe->pipe_buffer.size - wpipe->pipe_buffer.cnt) >= PIPE_BUF))
1219 revents |= events & (POLLOUT | POLLWRNORM);
1221 if ((rpipe->pipe_state & PIPE_EOF) ||
1223 (wpipe->pipe_state & PIPE_EOF))
1227 if (events & (POLLIN | POLLRDNORM)) {
1228 selrecord(curthread, &rpipe->pipe_sel);
1229 rpipe->pipe_state |= PIPE_SEL;
1232 if (events & (POLLOUT | POLLWRNORM)) {
1233 selrecord(curthread, &wpipe->pipe_sel);
1234 wpipe->pipe_state |= PIPE_SEL;
1242 * MPALMOSTSAFE - acquires mplock
1245 pipe_stat(struct file *fp, struct stat *ub, struct ucred *cred)
1250 pipe = (struct pipe *)fp->f_data;
1252 bzero((caddr_t)ub, sizeof(*ub));
1253 ub->st_mode = S_IFIFO;
1254 ub->st_blksize = pipe->pipe_buffer.size;
1255 ub->st_size = pipe->pipe_buffer.cnt;
1256 if (ub->st_size == 0 && (pipe->pipe_state & PIPE_DIRECTW)) {
1257 ub->st_size = pipe->pipe_map.xio_bytes -
1258 pipe->pipe_buffer.out;
1260 ub->st_blocks = (ub->st_size + ub->st_blksize - 1) / ub->st_blksize;
1261 ub->st_atimespec = pipe->pipe_atime;
1262 ub->st_mtimespec = pipe->pipe_mtime;
1263 ub->st_ctimespec = pipe->pipe_ctime;
1265 * Left as 0: st_dev, st_ino, st_nlink, st_uid, st_gid, st_rdev,
1267 * XXX (st_dev, st_ino) should be unique.
1274 * MPALMOSTSAFE - acquires mplock
1277 pipe_close(struct file *fp)
1279 struct pipe *cpipe = (struct pipe *)fp->f_data;
1282 fp->f_ops = &badfileops;
1284 funsetown(cpipe->pipe_sigio);
1291 * Shutdown one or both directions of a full-duplex pipe.
1293 * MPALMOSTSAFE - acquires mplock
1296 pipe_shutdown(struct file *fp, int how)
1303 rpipe = (struct pipe *)fp->f_data;
1309 rpipe->pipe_state |= PIPE_EOF;
1310 pipeselwakeup(rpipe);
1311 if (rpipe->pipe_busy)
1319 if (rpipe && (wpipe = rpipe->pipe_peer) != NULL) {
1320 wpipe->pipe_state |= PIPE_EOF;
1321 pipeselwakeup(wpipe);
1322 if (wpipe->pipe_busy)
1332 pipe_free_kmem(struct pipe *cpipe)
1334 if (cpipe->pipe_buffer.buffer != NULL) {
1335 if (cpipe->pipe_buffer.size > PIPE_SIZE)
1337 kmem_free(kernel_map,
1338 (vm_offset_t)cpipe->pipe_buffer.buffer,
1339 cpipe->pipe_buffer.size);
1340 cpipe->pipe_buffer.buffer = NULL;
1341 cpipe->pipe_buffer.object = NULL;
1343 #ifndef PIPE_NODIRECT
1344 KKASSERT(cpipe->pipe_map.xio_bytes == 0 &&
1345 cpipe->pipe_map.xio_offset == 0 &&
1346 cpipe->pipe_map.xio_npages == 0);
1354 pipeclose(struct pipe *cpipe)
1362 pipeselwakeup(cpipe);
1365 * If the other side is blocked, wake it up saying that
1366 * we want to close it down.
1368 while (cpipe->pipe_busy) {
1370 cpipe->pipe_state |= PIPE_WANT | PIPE_EOF;
1371 tsleep(cpipe, 0, "pipecl", 0);
1375 * Disconnect from peer
1377 if ((ppipe = cpipe->pipe_peer) != NULL) {
1378 pipeselwakeup(ppipe);
1380 ppipe->pipe_state |= PIPE_EOF;
1382 KNOTE(&ppipe->pipe_sel.si_note, 0);
1383 ppipe->pipe_peer = NULL;
1386 if (cpipe->pipe_kva) {
1387 pmap_qremove(cpipe->pipe_kva, XIO_INTERNAL_PAGES);
1388 kmem_free(kernel_map, cpipe->pipe_kva, XIO_INTERNAL_SIZE);
1389 cpipe->pipe_kva = NULL;
1393 * free or cache resources
1396 if (gd->gd_pipeqcount >= pipe_maxcache ||
1397 cpipe->pipe_buffer.size != PIPE_SIZE
1399 pipe_free_kmem(cpipe);
1400 free(cpipe, M_PIPE);
1402 KKASSERT(cpipe->pipe_map.xio_npages == 0 &&
1403 cpipe->pipe_map.xio_bytes == 0 &&
1404 cpipe->pipe_map.xio_offset == 0);
1405 cpipe->pipe_state = 0;
1406 cpipe->pipe_busy = 0;
1407 cpipe->pipe_peer = gd->gd_pipeq;
1408 gd->gd_pipeq = cpipe;
1409 ++gd->gd_pipeqcount;
1414 * MPALMOSTSAFE - acquires mplock
1417 pipe_kqfilter(struct file *fp, struct knote *kn)
1422 cpipe = (struct pipe *)kn->kn_fp->f_data;
1424 switch (kn->kn_filter) {
1426 kn->kn_fop = &pipe_rfiltops;
1429 kn->kn_fop = &pipe_wfiltops;
1430 cpipe = cpipe->pipe_peer;
1431 if (cpipe == NULL) {
1432 /* other end of pipe has been closed */
1440 kn->kn_hook = (caddr_t)cpipe;
1442 SLIST_INSERT_HEAD(&cpipe->pipe_sel.si_note, kn, kn_selnext);
1448 filt_pipedetach(struct knote *kn)
1450 struct pipe *cpipe = (struct pipe *)kn->kn_hook;
1452 SLIST_REMOVE(&cpipe->pipe_sel.si_note, kn, knote, kn_selnext);
1457 filt_piperead(struct knote *kn, long hint)
1459 struct pipe *rpipe = (struct pipe *)kn->kn_fp->f_data;
1460 struct pipe *wpipe = rpipe->pipe_peer;
1462 kn->kn_data = rpipe->pipe_buffer.cnt;
1463 if ((kn->kn_data == 0) && (rpipe->pipe_state & PIPE_DIRECTW)) {
1464 kn->kn_data = rpipe->pipe_map.xio_bytes -
1465 rpipe->pipe_buffer.out;
1468 if ((rpipe->pipe_state & PIPE_EOF) ||
1469 (wpipe == NULL) || (wpipe->pipe_state & PIPE_EOF)) {
1470 kn->kn_flags |= EV_EOF;
1473 return (kn->kn_data > 0);
1478 filt_pipewrite(struct knote *kn, long hint)
1480 struct pipe *rpipe = (struct pipe *)kn->kn_fp->f_data;
1481 struct pipe *wpipe = rpipe->pipe_peer;
1483 if ((wpipe == NULL) || (wpipe->pipe_state & PIPE_EOF)) {
1485 kn->kn_flags |= EV_EOF;
1488 kn->kn_data = wpipe->pipe_buffer.size - wpipe->pipe_buffer.cnt;
1489 if (wpipe->pipe_state & PIPE_DIRECTW)
1492 return (kn->kn_data >= PIPE_BUF);