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.46 2008/05/08 01:31:01 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 = {
107 .fo_read = pipe_read,
108 .fo_write = pipe_write,
109 .fo_ioctl = pipe_ioctl,
110 .fo_poll = pipe_poll,
111 .fo_kqfilter = pipe_kqfilter,
112 .fo_stat = pipe_stat,
113 .fo_close = pipe_close,
114 .fo_shutdown = pipe_shutdown
117 static void filt_pipedetach(struct knote *kn);
118 static int filt_piperead(struct knote *kn, long hint);
119 static int filt_pipewrite(struct knote *kn, long hint);
121 static struct filterops pipe_rfiltops =
122 { 1, NULL, filt_pipedetach, filt_piperead };
123 static struct filterops pipe_wfiltops =
124 { 1, NULL, filt_pipedetach, filt_pipewrite };
126 MALLOC_DEFINE(M_PIPE, "pipe", "pipe structures");
129 * Default pipe buffer size(s), this can be kind-of large now because pipe
130 * space is pageable. The pipe code will try to maintain locality of
131 * reference for performance reasons, so small amounts of outstanding I/O
132 * will not wipe the cache.
134 #define MINPIPESIZE (PIPE_SIZE/3)
135 #define MAXPIPESIZE (2*PIPE_SIZE/3)
138 * Maximum amount of kva for pipes -- this is kind-of a soft limit, but
139 * is there so that on large systems, we don't exhaust it.
141 #define MAXPIPEKVA (8*1024*1024)
144 * Limit for direct transfers, we cannot, of course limit
145 * the amount of kva for pipes in general though.
147 #define LIMITPIPEKVA (16*1024*1024)
150 * Limit the number of "big" pipes
152 #define LIMITBIGPIPES 32
153 #define PIPEQ_MAX_CACHE 16 /* per-cpu pipe structure cache */
155 static int pipe_maxbig = LIMITBIGPIPES;
156 static int pipe_maxcache = PIPEQ_MAX_CACHE;
157 static int pipe_bigcount;
158 static int pipe_nbig;
159 static int pipe_bcache_alloc;
160 static int pipe_bkmem_alloc;
162 * There's a bug in the sfbuf-based direct write code, not yet located.
163 * Disable it for now.
165 static int pipe_dwrite_enable = 0; /* 0:copy, 1:kmem/sfbuf 2:force */
166 static int pipe_dwrite_sfbuf = 1; /* 0:kmem_map 1:sfbufs 2:sfbufs_dmap */
167 /* 3:sfbuf_dmap w/ forced invlpg */
169 SYSCTL_NODE(_kern, OID_AUTO, pipe, CTLFLAG_RW, 0, "Pipe operation");
170 SYSCTL_INT(_kern_pipe, OID_AUTO, nbig,
171 CTLFLAG_RD, &pipe_nbig, 0, "numer of big pipes allocated");
172 SYSCTL_INT(_kern_pipe, OID_AUTO, bigcount,
173 CTLFLAG_RW, &pipe_bigcount, 0, "number of times pipe expanded");
174 SYSCTL_INT(_kern_pipe, OID_AUTO, maxcache,
175 CTLFLAG_RW, &pipe_maxcache, 0, "max pipes cached per-cpu");
176 SYSCTL_INT(_kern_pipe, OID_AUTO, maxbig,
177 CTLFLAG_RW, &pipe_maxbig, 0, "max number of big pipes");
178 SYSCTL_INT(_kern_pipe, OID_AUTO, dwrite_enable,
179 CTLFLAG_RW, &pipe_dwrite_enable, 0, "1:enable/2:force direct writes");
180 SYSCTL_INT(_kern_pipe, OID_AUTO, dwrite_sfbuf,
181 CTLFLAG_RW, &pipe_dwrite_sfbuf, 0,
182 "(if dwrite_enable) 0:kmem 1:sfbuf 2:sfbuf_dmap 3:sfbuf_dmap_forceinvlpg");
183 #if !defined(NO_PIPE_SYSCTL_STATS)
184 SYSCTL_INT(_kern_pipe, OID_AUTO, bcache_alloc,
185 CTLFLAG_RW, &pipe_bcache_alloc, 0, "pipe buffer from pcpu cache");
186 SYSCTL_INT(_kern_pipe, OID_AUTO, bkmem_alloc,
187 CTLFLAG_RW, &pipe_bkmem_alloc, 0, "pipe buffer from kmem");
190 static void pipeclose (struct pipe *cpipe);
191 static void pipe_free_kmem (struct pipe *cpipe);
192 static int pipe_create (struct pipe **cpipep);
193 static __inline int pipelock (struct pipe *cpipe, int catch);
194 static __inline void pipeunlock (struct pipe *cpipe);
195 static __inline void pipeselwakeup (struct pipe *cpipe);
196 #ifndef PIPE_NODIRECT
197 static int pipe_build_write_buffer (struct pipe *wpipe, struct uio *uio);
198 static int pipe_direct_write (struct pipe *wpipe, struct uio *uio);
199 static void pipe_clone_write_buffer (struct pipe *wpipe);
201 static int pipespace (struct pipe *cpipe, int size);
204 * The pipe system call for the DTYPE_PIPE type of pipes
206 * pipe_ARgs(int dummy)
211 sys_pipe(struct pipe_args *uap)
213 struct thread *td = curthread;
214 struct proc *p = td->td_proc;
215 struct file *rf, *wf;
216 struct pipe *rpipe, *wpipe;
221 rpipe = wpipe = NULL;
222 if (pipe_create(&rpipe) || pipe_create(&wpipe)) {
228 rpipe->pipe_state |= PIPE_DIRECTOK;
229 wpipe->pipe_state |= PIPE_DIRECTOK;
232 * Select the direct-map features to use for this pipe. Since the
233 * sysctl's can change on the fly we record the settings when the
236 * Generally speaking the system will default to what we consider
237 * to be the best-balanced and most stable option. Right now this
238 * is SFBUF1. Modes 2 and 3 are considered experiemental at the
241 wpipe->pipe_feature = PIPE_COPY;
242 if (pipe_dwrite_enable) {
243 switch(pipe_dwrite_sfbuf) {
245 wpipe->pipe_feature = PIPE_KMEM;
248 wpipe->pipe_feature = PIPE_SFBUF1;
252 wpipe->pipe_feature = PIPE_SFBUF2;
256 rpipe->pipe_feature = wpipe->pipe_feature;
258 error = falloc(p, &rf, &fd1);
264 uap->sysmsg_fds[0] = fd1;
267 * Warning: once we've gotten past allocation of the fd for the
268 * read-side, we can only drop the read side via fdrop() in order
269 * to avoid races against processes which manage to dup() the read
270 * side while we are blocked trying to allocate the write side.
272 rf->f_type = DTYPE_PIPE;
273 rf->f_flag = FREAD | FWRITE;
274 rf->f_ops = &pipeops;
276 error = falloc(p, &wf, &fd2);
278 fsetfd(p, NULL, fd1);
280 /* rpipe has been closed by fdrop(). */
284 wf->f_type = DTYPE_PIPE;
285 wf->f_flag = FREAD | FWRITE;
286 wf->f_ops = &pipeops;
288 uap->sysmsg_fds[1] = fd2;
290 rpipe->pipe_peer = wpipe;
291 wpipe->pipe_peer = rpipe;
302 * Allocate kva for pipe circular buffer, the space is pageable
303 * This routine will 'realloc' the size of a pipe safely, if it fails
304 * it will retain the old buffer.
305 * If it fails it will return ENOMEM.
308 pipespace(struct pipe *cpipe, int size)
310 struct vm_object *object;
314 npages = round_page(size) / PAGE_SIZE;
315 object = cpipe->pipe_buffer.object;
318 * [re]create the object if necessary and reserve space for it
319 * in the kernel_map. The object and memory are pageable. On
320 * success, free the old resources before assigning the new
323 if (object == NULL || object->size != npages) {
324 object = vm_object_allocate(OBJT_DEFAULT, npages);
325 buffer = (caddr_t)vm_map_min(&kernel_map);
327 error = vm_map_find(&kernel_map, object, 0,
328 (vm_offset_t *)&buffer, size,
331 VM_PROT_ALL, VM_PROT_ALL,
334 if (error != KERN_SUCCESS) {
335 vm_object_deallocate(object);
338 pipe_free_kmem(cpipe);
339 cpipe->pipe_buffer.object = object;
340 cpipe->pipe_buffer.buffer = buffer;
341 cpipe->pipe_buffer.size = size;
346 cpipe->pipe_buffer.in = 0;
347 cpipe->pipe_buffer.out = 0;
348 cpipe->pipe_buffer.cnt = 0;
353 * Initialize and allocate VM and memory for pipe, pulling the pipe from
354 * our per-cpu cache if possible. For now make sure it is sized for the
355 * smaller PIPE_SIZE default.
358 pipe_create(struct pipe **cpipep)
360 globaldata_t gd = mycpu;
364 if ((cpipe = gd->gd_pipeq) != NULL) {
365 gd->gd_pipeq = cpipe->pipe_peer;
367 cpipe->pipe_peer = NULL;
369 cpipe = kmalloc(sizeof(struct pipe), M_PIPE, M_WAITOK|M_ZERO);
372 if ((error = pipespace(cpipe, PIPE_SIZE)) != 0)
374 vfs_timestamp(&cpipe->pipe_ctime);
375 cpipe->pipe_atime = cpipe->pipe_ctime;
376 cpipe->pipe_mtime = cpipe->pipe_ctime;
382 * lock a pipe for I/O, blocking other access
385 pipelock(struct pipe *cpipe, int catch)
389 while (cpipe->pipe_state & PIPE_LOCK) {
390 cpipe->pipe_state |= PIPE_LWANT;
391 error = tsleep(cpipe, (catch ? PCATCH : 0), "pipelk", 0);
395 cpipe->pipe_state |= PIPE_LOCK;
400 * unlock a pipe I/O lock
403 pipeunlock(struct pipe *cpipe)
406 cpipe->pipe_state &= ~PIPE_LOCK;
407 if (cpipe->pipe_state & PIPE_LWANT) {
408 cpipe->pipe_state &= ~PIPE_LWANT;
414 pipeselwakeup(struct pipe *cpipe)
417 if (cpipe->pipe_state & PIPE_SEL) {
418 cpipe->pipe_state &= ~PIPE_SEL;
419 selwakeup(&cpipe->pipe_sel);
421 if ((cpipe->pipe_state & PIPE_ASYNC) && cpipe->pipe_sigio)
422 pgsigio(cpipe->pipe_sigio, SIGIO, 0);
423 KNOTE(&cpipe->pipe_sel.si_note, 0);
427 * MPALMOSTSAFE (acquires mplock)
430 pipe_read(struct file *fp, struct uio *uio, struct ucred *cred, int fflags)
439 rpipe = (struct pipe *) fp->f_data;
441 error = pipelock(rpipe, 1);
445 if (fflags & O_FBLOCKING)
447 else if (fflags & O_FNONBLOCKING)
449 else if (fp->f_flag & O_NONBLOCK)
454 while (uio->uio_resid) {
457 if (rpipe->pipe_buffer.cnt > 0) {
459 * normal pipe buffer receive
461 size = rpipe->pipe_buffer.size - rpipe->pipe_buffer.out;
462 if (size > rpipe->pipe_buffer.cnt)
463 size = rpipe->pipe_buffer.cnt;
464 if (size > (u_int) uio->uio_resid)
465 size = (u_int) uio->uio_resid;
467 error = uiomove(&rpipe->pipe_buffer.buffer[rpipe->pipe_buffer.out],
472 rpipe->pipe_buffer.out += size;
473 if (rpipe->pipe_buffer.out >= rpipe->pipe_buffer.size)
474 rpipe->pipe_buffer.out = 0;
476 rpipe->pipe_buffer.cnt -= size;
479 * If there is no more to read in the pipe, reset
480 * its pointers to the beginning. This improves
483 if (rpipe->pipe_buffer.cnt == 0) {
484 rpipe->pipe_buffer.in = 0;
485 rpipe->pipe_buffer.out = 0;
488 #ifndef PIPE_NODIRECT
489 } else if (rpipe->pipe_kva &&
490 rpipe->pipe_feature == PIPE_KMEM &&
491 (rpipe->pipe_state & (PIPE_DIRECTW|PIPE_DIRECTIP))
495 * Direct copy using source-side kva mapping
497 size = rpipe->pipe_map.xio_bytes -
498 rpipe->pipe_buffer.out;
499 if (size > (u_int)uio->uio_resid)
500 size = (u_int)uio->uio_resid;
501 va = (caddr_t)rpipe->pipe_kva +
502 xio_kvaoffset(&rpipe->pipe_map, rpipe->pipe_buffer.out);
503 error = uiomove(va, size, uio);
507 rpipe->pipe_buffer.out += size;
508 if (rpipe->pipe_buffer.out == rpipe->pipe_map.xio_bytes) {
509 rpipe->pipe_state |= PIPE_DIRECTIP;
510 rpipe->pipe_state &= ~PIPE_DIRECTW;
511 /* reset out index for copy mode */
512 rpipe->pipe_buffer.out = 0;
515 } else if (rpipe->pipe_buffer.out != rpipe->pipe_map.xio_bytes &&
517 rpipe->pipe_feature == PIPE_SFBUF2 &&
518 (rpipe->pipe_state & (PIPE_DIRECTW|PIPE_DIRECTIP))
522 * Direct copy, bypassing a kernel buffer. We cannot
523 * mess with the direct-write buffer until
524 * PIPE_DIRECTIP is cleared. In order to prevent
525 * the pipe_write code from racing itself in
526 * direct_write, we set DIRECTIP when we clear
527 * DIRECTW after we have exhausted the buffer.
529 if (pipe_dwrite_sfbuf == 3)
530 rpipe->pipe_kvamask = 0;
531 pmap_qenter2(rpipe->pipe_kva, rpipe->pipe_map.xio_pages,
532 rpipe->pipe_map.xio_npages,
533 &rpipe->pipe_kvamask);
534 size = rpipe->pipe_map.xio_bytes -
535 rpipe->pipe_buffer.out;
536 if (size > (u_int)uio->uio_resid)
537 size = (u_int)uio->uio_resid;
538 va = (caddr_t)rpipe->pipe_kva + xio_kvaoffset(&rpipe->pipe_map, rpipe->pipe_buffer.out);
539 error = uiomove(va, size, uio);
543 rpipe->pipe_buffer.out += size;
544 if (rpipe->pipe_buffer.out == rpipe->pipe_map.xio_bytes) {
545 rpipe->pipe_state |= PIPE_DIRECTIP;
546 rpipe->pipe_state &= ~PIPE_DIRECTW;
547 /* reset out index for copy mode */
548 rpipe->pipe_buffer.out = 0;
551 } else if (rpipe->pipe_buffer.out != rpipe->pipe_map.xio_bytes &&
552 rpipe->pipe_feature == PIPE_SFBUF1 &&
553 (rpipe->pipe_state & (PIPE_DIRECTW|PIPE_DIRECTIP))
557 * Direct copy, bypassing a kernel buffer. We cannot
558 * mess with the direct-write buffer until
559 * PIPE_DIRECTIP is cleared. In order to prevent
560 * the pipe_write code from racing itself in
561 * direct_write, we set DIRECTIP when we clear
562 * DIRECTW after we have exhausted the buffer.
564 error = xio_uio_copy(&rpipe->pipe_map, rpipe->pipe_buffer.out, uio, &size);
568 rpipe->pipe_buffer.out += size;
569 if (rpipe->pipe_buffer.out == rpipe->pipe_map.xio_bytes) {
570 rpipe->pipe_state |= PIPE_DIRECTIP;
571 rpipe->pipe_state &= ~PIPE_DIRECTW;
572 /* reset out index for copy mode */
573 rpipe->pipe_buffer.out = 0;
579 * detect EOF condition
580 * read returns 0 on EOF, no need to set error
582 if (rpipe->pipe_state & PIPE_EOF)
586 * If the "write-side" has been blocked, wake it up now.
588 if (rpipe->pipe_state & PIPE_WANTW) {
589 rpipe->pipe_state &= ~PIPE_WANTW;
594 * Break if some data was read.
600 * Unlock the pipe buffer for our remaining
601 * processing. We will either break out with an
602 * error or we will sleep and relock to loop.
607 * Handle non-blocking mode operation or
608 * wait for more data.
613 rpipe->pipe_state |= PIPE_WANTR;
614 if ((error = tsleep(rpipe, PCATCH|PNORESCHED,
615 "piperd", 0)) == 0) {
616 error = pipelock(rpipe, 1);
626 vfs_timestamp(&rpipe->pipe_atime);
631 * PIPE_WANT processing only makes sense if pipe_busy is 0.
633 if ((rpipe->pipe_busy == 0) && (rpipe->pipe_state & PIPE_WANT)) {
634 rpipe->pipe_state &= ~(PIPE_WANT|PIPE_WANTW);
636 } else if (rpipe->pipe_buffer.cnt < MINPIPESIZE) {
638 * Handle write blocking hysteresis.
640 if (rpipe->pipe_state & PIPE_WANTW) {
641 rpipe->pipe_state &= ~PIPE_WANTW;
646 if ((rpipe->pipe_buffer.size - rpipe->pipe_buffer.cnt) >= PIPE_BUF)
647 pipeselwakeup(rpipe);
652 #ifndef PIPE_NODIRECT
654 * Map the sending processes' buffer into kernel space and wire it.
655 * This is similar to a physical write operation.
658 pipe_build_write_buffer(struct pipe *wpipe, struct uio *uio)
663 size = (u_int) uio->uio_iov->iov_len;
664 if (size > wpipe->pipe_buffer.size)
665 size = wpipe->pipe_buffer.size;
667 if (uio->uio_segflg == UIO_SYSSPACE) {
668 error = xio_init_kbuf(&wpipe->pipe_map, uio->uio_iov->iov_base,
671 error = xio_init_ubuf(&wpipe->pipe_map, uio->uio_iov->iov_base,
674 wpipe->pipe_buffer.out = 0;
679 * Create a kernel map for KMEM and SFBUF2 copy modes. SFBUF2 will
680 * map the pages on the target while KMEM maps the pages now.
682 switch(wpipe->pipe_feature) {
685 if (wpipe->pipe_kva == NULL) {
687 kmem_alloc_nofault(&kernel_map, XIO_INTERNAL_SIZE);
688 wpipe->pipe_kvamask = 0;
690 if (wpipe->pipe_feature == PIPE_KMEM) {
691 pmap_qenter(wpipe->pipe_kva, wpipe->pipe_map.xio_pages,
692 wpipe->pipe_map.xio_npages);
700 * And update the uio data. The XIO might have loaded fewer bytes
701 * then requested so reload 'size'.
703 size = wpipe->pipe_map.xio_bytes;
704 uio->uio_iov->iov_len -= size;
705 uio->uio_iov->iov_base += size;
706 if (uio->uio_iov->iov_len == 0)
708 uio->uio_resid -= size;
709 uio->uio_offset += size;
714 * In the case of a signal, the writing process might go away. This
715 * code copies the data into the circular buffer so that the source
716 * pages can be freed without loss of data.
718 * Note that in direct mode pipe_buffer.out is used to track the
719 * XIO offset. We are converting the direct mode into buffered mode
720 * which changes the meaning of pipe_buffer.out.
723 pipe_clone_write_buffer(struct pipe *wpipe)
728 offset = wpipe->pipe_buffer.out;
729 size = wpipe->pipe_map.xio_bytes - offset;
731 KKASSERT(size <= wpipe->pipe_buffer.size);
733 wpipe->pipe_buffer.in = size;
734 wpipe->pipe_buffer.out = 0;
735 wpipe->pipe_buffer.cnt = size;
736 wpipe->pipe_state &= ~(PIPE_DIRECTW | PIPE_DIRECTIP);
738 xio_copy_xtok(&wpipe->pipe_map, offset, wpipe->pipe_buffer.buffer, size);
739 xio_release(&wpipe->pipe_map);
740 if (wpipe->pipe_kva) {
741 pmap_qremove(wpipe->pipe_kva, XIO_INTERNAL_PAGES);
742 kmem_free(&kernel_map, wpipe->pipe_kva, XIO_INTERNAL_SIZE);
743 wpipe->pipe_kva = NULL;
748 * This implements the pipe buffer write mechanism. Note that only
749 * a direct write OR a normal pipe write can be pending at any given time.
750 * If there are any characters in the pipe buffer, the direct write will
751 * be deferred until the receiving process grabs all of the bytes from
752 * the pipe buffer. Then the direct mapping write is set-up.
755 pipe_direct_write(struct pipe *wpipe, struct uio *uio)
760 while (wpipe->pipe_state & (PIPE_DIRECTW|PIPE_DIRECTIP)) {
761 if (wpipe->pipe_state & PIPE_WANTR) {
762 wpipe->pipe_state &= ~PIPE_WANTR;
765 wpipe->pipe_state |= PIPE_WANTW;
766 error = tsleep(wpipe, PCATCH, "pipdww", 0);
769 if (wpipe->pipe_state & PIPE_EOF) {
774 KKASSERT(wpipe->pipe_map.xio_bytes == 0);
775 if (wpipe->pipe_buffer.cnt > 0) {
776 if (wpipe->pipe_state & PIPE_WANTR) {
777 wpipe->pipe_state &= ~PIPE_WANTR;
781 wpipe->pipe_state |= PIPE_WANTW;
782 error = tsleep(wpipe, PCATCH, "pipdwc", 0);
785 if (wpipe->pipe_state & PIPE_EOF) {
793 * Build our direct-write buffer
795 wpipe->pipe_state |= PIPE_DIRECTW | PIPE_DIRECTIP;
796 error = pipe_build_write_buffer(wpipe, uio);
799 wpipe->pipe_state &= ~PIPE_DIRECTIP;
802 * Wait until the receiver has snarfed the data. Since we are likely
803 * going to sleep we optimize the case and yield synchronously,
804 * possibly avoiding the tsleep().
807 while (!error && (wpipe->pipe_state & PIPE_DIRECTW)) {
808 if (wpipe->pipe_state & PIPE_EOF) {
810 xio_release(&wpipe->pipe_map);
811 if (wpipe->pipe_kva) {
812 pmap_qremove(wpipe->pipe_kva, XIO_INTERNAL_PAGES);
813 kmem_free(&kernel_map, wpipe->pipe_kva, XIO_INTERNAL_SIZE);
814 wpipe->pipe_kva = NULL;
817 pipeselwakeup(wpipe);
821 if (wpipe->pipe_state & PIPE_WANTR) {
822 wpipe->pipe_state &= ~PIPE_WANTR;
825 pipeselwakeup(wpipe);
826 error = tsleep(wpipe, PCATCH|PNORESCHED, "pipdwt", 0);
829 if (wpipe->pipe_state & PIPE_DIRECTW) {
831 * this bit of trickery substitutes a kernel buffer for
832 * the process that might be going away.
834 pipe_clone_write_buffer(wpipe);
835 KKASSERT((wpipe->pipe_state & PIPE_DIRECTIP) == 0);
838 * note: The pipe_kva mapping is not qremove'd here. For
839 * legacy PIPE_KMEM mode this constitutes an improvement
840 * over the original FreeBSD-4 algorithm. For PIPE_SFBUF2
841 * mode the kva mapping must not be removed to get the
844 * For testing purposes we will give the original algorithm
845 * the benefit of the doubt 'what it could have been', and
846 * keep the optimization.
848 KKASSERT(wpipe->pipe_state & PIPE_DIRECTIP);
849 xio_release(&wpipe->pipe_map);
850 wpipe->pipe_state &= ~PIPE_DIRECTIP;
856 * Direct-write error, clear the direct write flags.
859 wpipe->pipe_state &= ~(PIPE_DIRECTW | PIPE_DIRECTIP);
863 * General error, wakeup the other side if it happens to be sleeping.
872 * MPALMOSTSAFE - acquires mplock
875 pipe_write(struct file *fp, struct uio *uio, struct ucred *cred, int fflags)
880 struct pipe *wpipe, *rpipe;
883 rpipe = (struct pipe *) fp->f_data;
884 wpipe = rpipe->pipe_peer;
887 * detect loss of pipe read side, issue SIGPIPE if lost.
889 if ((wpipe == NULL) || (wpipe->pipe_state & PIPE_EOF)) {
895 if (fflags & O_FBLOCKING)
897 else if (fflags & O_FNONBLOCKING)
899 else if (fp->f_flag & O_NONBLOCK)
905 * If it is advantageous to resize the pipe buffer, do
908 if ((uio->uio_resid > PIPE_SIZE) &&
909 (pipe_nbig < pipe_maxbig) &&
910 (wpipe->pipe_state & (PIPE_DIRECTW|PIPE_DIRECTIP)) == 0 &&
911 (wpipe->pipe_buffer.size <= PIPE_SIZE) &&
912 (wpipe->pipe_buffer.cnt == 0) &&
913 (error = pipelock(wpipe, 1)) == 0) {
915 * Recheck after lock.
917 if ((pipe_nbig < pipe_maxbig) &&
918 (wpipe->pipe_state & (PIPE_DIRECTW|PIPE_DIRECTIP)) == 0 &&
919 (wpipe->pipe_buffer.size <= PIPE_SIZE) &&
920 (wpipe->pipe_buffer.cnt == 0)) {
921 if (pipespace(wpipe, BIG_PIPE_SIZE) == 0) {
930 * If an early error occured unbusy and return, waking up any pending
935 if ((wpipe->pipe_busy == 0) &&
936 (wpipe->pipe_state & PIPE_WANT)) {
937 wpipe->pipe_state &= ~(PIPE_WANT | PIPE_WANTR);
944 KASSERT(wpipe->pipe_buffer.buffer != NULL, ("pipe buffer gone"));
946 orig_resid = uio->uio_resid;
948 while (uio->uio_resid) {
951 #ifndef PIPE_NODIRECT
953 * If the transfer is large, we can gain performance if
954 * we do process-to-process copies directly.
955 * If the write is non-blocking, we don't use the
956 * direct write mechanism.
958 * The direct write mechanism will detect the reader going
961 if ((uio->uio_iov->iov_len >= PIPE_MINDIRECT ||
962 pipe_dwrite_enable > 1) &&
964 pipe_dwrite_enable) {
965 error = pipe_direct_write( wpipe, uio);
973 * Pipe buffered writes cannot be coincidental with
974 * direct writes. We wait until the currently executing
975 * direct write is completed before we start filling the
976 * pipe buffer. We break out if a signal occurs or the
980 while (wpipe->pipe_state & (PIPE_DIRECTW|PIPE_DIRECTIP)) {
981 if (wpipe->pipe_state & PIPE_WANTR) {
982 wpipe->pipe_state &= ~PIPE_WANTR;
985 error = tsleep(wpipe, PCATCH, "pipbww", 0);
986 if (wpipe->pipe_state & PIPE_EOF)
991 if (wpipe->pipe_state & PIPE_EOF) {
996 space = wpipe->pipe_buffer.size - wpipe->pipe_buffer.cnt;
998 /* Writes of size <= PIPE_BUF must be atomic. */
999 if ((space < uio->uio_resid) && (orig_resid <= PIPE_BUF))
1003 * Write to fill, read size handles write hysteresis. Also
1004 * additional restrictions can cause select-based non-blocking
1008 if ((error = pipelock(wpipe,1)) == 0) {
1009 int size; /* Transfer size */
1010 int segsize; /* first segment to transfer */
1013 * It is possible for a direct write to
1014 * slip in on us... handle it here...
1016 if (wpipe->pipe_state & (PIPE_DIRECTW|PIPE_DIRECTIP)) {
1021 * If a process blocked in uiomove, our
1022 * value for space might be bad.
1024 * XXX will we be ok if the reader has gone
1027 if (space > wpipe->pipe_buffer.size -
1028 wpipe->pipe_buffer.cnt) {
1034 * Transfer size is minimum of uio transfer
1035 * and free space in pipe buffer.
1037 if (space > uio->uio_resid)
1038 size = uio->uio_resid;
1042 * First segment to transfer is minimum of
1043 * transfer size and contiguous space in
1044 * pipe buffer. If first segment to transfer
1045 * is less than the transfer size, we've got
1046 * a wraparound in the buffer.
1048 segsize = wpipe->pipe_buffer.size -
1049 wpipe->pipe_buffer.in;
1053 /* Transfer first segment */
1055 error = uiomove(&wpipe->pipe_buffer.buffer[wpipe->pipe_buffer.in],
1058 if (error == 0 && segsize < size) {
1060 * Transfer remaining part now, to
1061 * support atomic writes. Wraparound
1064 if (wpipe->pipe_buffer.in + segsize !=
1065 wpipe->pipe_buffer.size)
1066 panic("Expected pipe buffer wraparound disappeared");
1068 error = uiomove(&wpipe->pipe_buffer.buffer[0],
1069 size - segsize, uio);
1072 wpipe->pipe_buffer.in += size;
1073 if (wpipe->pipe_buffer.in >=
1074 wpipe->pipe_buffer.size) {
1075 if (wpipe->pipe_buffer.in != size - segsize + wpipe->pipe_buffer.size)
1076 panic("Expected wraparound bad");
1077 wpipe->pipe_buffer.in = size - segsize;
1080 wpipe->pipe_buffer.cnt += size;
1081 if (wpipe->pipe_buffer.cnt > wpipe->pipe_buffer.size)
1082 panic("Pipe buffer overflow");
1092 * If the "read-side" has been blocked, wake it up now
1093 * and yield to let it drain synchronously rather
1096 if (wpipe->pipe_state & PIPE_WANTR) {
1097 wpipe->pipe_state &= ~PIPE_WANTR;
1102 * don't block on non-blocking I/O
1110 * We have no more space and have something to offer,
1111 * wake up select/poll.
1113 pipeselwakeup(wpipe);
1115 wpipe->pipe_state |= PIPE_WANTW;
1116 error = tsleep(wpipe, PCATCH|PNORESCHED, "pipewr", 0);
1120 * If read side wants to go away, we just issue a signal
1123 if (wpipe->pipe_state & PIPE_EOF) {
1132 if ((wpipe->pipe_busy == 0) && (wpipe->pipe_state & PIPE_WANT)) {
1133 wpipe->pipe_state &= ~(PIPE_WANT | PIPE_WANTR);
1135 } else if (wpipe->pipe_buffer.cnt > 0) {
1137 * If we have put any characters in the buffer, we wake up
1140 if (wpipe->pipe_state & PIPE_WANTR) {
1141 wpipe->pipe_state &= ~PIPE_WANTR;
1147 * Don't return EPIPE if I/O was successful
1149 if ((wpipe->pipe_buffer.cnt == 0) &&
1150 (uio->uio_resid == 0) &&
1156 vfs_timestamp(&wpipe->pipe_mtime);
1159 * We have something to offer,
1160 * wake up select/poll.
1162 if (wpipe->pipe_buffer.cnt)
1163 pipeselwakeup(wpipe);
1169 * MPALMOSTSAFE - acquires mplock
1171 * we implement a very minimal set of ioctls for compatibility with sockets.
1174 pipe_ioctl(struct file *fp, u_long cmd, caddr_t data, struct ucred *cred)
1180 mpipe = (struct pipe *)fp->f_data;
1185 mpipe->pipe_state |= PIPE_ASYNC;
1187 mpipe->pipe_state &= ~PIPE_ASYNC;
1192 if (mpipe->pipe_state & PIPE_DIRECTW) {
1193 *(int *)data = mpipe->pipe_map.xio_bytes -
1194 mpipe->pipe_buffer.out;
1196 *(int *)data = mpipe->pipe_buffer.cnt;
1201 error = fsetown(*(int *)data, &mpipe->pipe_sigio);
1204 *(int *)data = fgetown(mpipe->pipe_sigio);
1208 /* This is deprecated, FIOSETOWN should be used instead. */
1209 error = fsetown(-(*(int *)data), &mpipe->pipe_sigio);
1213 /* This is deprecated, FIOGETOWN should be used instead. */
1214 *(int *)data = -fgetown(mpipe->pipe_sigio);
1226 * MPALMOSTSAFE - acquires mplock
1229 pipe_poll(struct file *fp, int events, struct ucred *cred)
1236 rpipe = (struct pipe *)fp->f_data;
1237 wpipe = rpipe->pipe_peer;
1238 if (events & (POLLIN | POLLRDNORM))
1239 if ((rpipe->pipe_state & PIPE_DIRECTW) ||
1240 (rpipe->pipe_buffer.cnt > 0) ||
1241 (rpipe->pipe_state & PIPE_EOF))
1242 revents |= events & (POLLIN | POLLRDNORM);
1244 if (events & (POLLOUT | POLLWRNORM))
1245 if (wpipe == NULL || (wpipe->pipe_state & PIPE_EOF) ||
1246 (((wpipe->pipe_state & PIPE_DIRECTW) == 0) &&
1247 (wpipe->pipe_buffer.size - wpipe->pipe_buffer.cnt) >= PIPE_BUF))
1248 revents |= events & (POLLOUT | POLLWRNORM);
1250 if ((rpipe->pipe_state & PIPE_EOF) ||
1252 (wpipe->pipe_state & PIPE_EOF))
1256 if (events & (POLLIN | POLLRDNORM)) {
1257 selrecord(curthread, &rpipe->pipe_sel);
1258 rpipe->pipe_state |= PIPE_SEL;
1261 if (events & (POLLOUT | POLLWRNORM)) {
1262 selrecord(curthread, &wpipe->pipe_sel);
1263 wpipe->pipe_state |= PIPE_SEL;
1271 * MPALMOSTSAFE - acquires mplock
1274 pipe_stat(struct file *fp, struct stat *ub, struct ucred *cred)
1279 pipe = (struct pipe *)fp->f_data;
1281 bzero((caddr_t)ub, sizeof(*ub));
1282 ub->st_mode = S_IFIFO;
1283 ub->st_blksize = pipe->pipe_buffer.size;
1284 ub->st_size = pipe->pipe_buffer.cnt;
1285 if (ub->st_size == 0 && (pipe->pipe_state & PIPE_DIRECTW)) {
1286 ub->st_size = pipe->pipe_map.xio_bytes -
1287 pipe->pipe_buffer.out;
1289 ub->st_blocks = (ub->st_size + ub->st_blksize - 1) / ub->st_blksize;
1290 ub->st_atimespec = pipe->pipe_atime;
1291 ub->st_mtimespec = pipe->pipe_mtime;
1292 ub->st_ctimespec = pipe->pipe_ctime;
1294 * Left as 0: st_dev, st_ino, st_nlink, st_uid, st_gid, st_rdev,
1296 * XXX (st_dev, st_ino) should be unique.
1303 * MPALMOSTSAFE - acquires mplock
1306 pipe_close(struct file *fp)
1311 cpipe = (struct pipe *)fp->f_data;
1312 fp->f_ops = &badfileops;
1314 funsetown(cpipe->pipe_sigio);
1321 * Shutdown one or both directions of a full-duplex pipe.
1323 * MPALMOSTSAFE - acquires mplock
1326 pipe_shutdown(struct file *fp, int how)
1333 rpipe = (struct pipe *)fp->f_data;
1339 rpipe->pipe_state |= PIPE_EOF;
1340 pipeselwakeup(rpipe);
1341 if (rpipe->pipe_busy)
1349 if (rpipe && (wpipe = rpipe->pipe_peer) != NULL) {
1350 wpipe->pipe_state |= PIPE_EOF;
1351 pipeselwakeup(wpipe);
1352 if (wpipe->pipe_busy)
1362 pipe_free_kmem(struct pipe *cpipe)
1364 if (cpipe->pipe_buffer.buffer != NULL) {
1365 if (cpipe->pipe_buffer.size > PIPE_SIZE)
1367 kmem_free(&kernel_map,
1368 (vm_offset_t)cpipe->pipe_buffer.buffer,
1369 cpipe->pipe_buffer.size);
1370 cpipe->pipe_buffer.buffer = NULL;
1371 cpipe->pipe_buffer.object = NULL;
1373 #ifndef PIPE_NODIRECT
1374 KKASSERT(cpipe->pipe_map.xio_bytes == 0 &&
1375 cpipe->pipe_map.xio_offset == 0 &&
1376 cpipe->pipe_map.xio_npages == 0);
1384 pipeclose(struct pipe *cpipe)
1392 pipeselwakeup(cpipe);
1395 * If the other side is blocked, wake it up saying that
1396 * we want to close it down.
1398 while (cpipe->pipe_busy) {
1400 cpipe->pipe_state |= PIPE_WANT | PIPE_EOF;
1401 tsleep(cpipe, 0, "pipecl", 0);
1405 * Disconnect from peer
1407 if ((ppipe = cpipe->pipe_peer) != NULL) {
1408 pipeselwakeup(ppipe);
1410 ppipe->pipe_state |= PIPE_EOF;
1412 KNOTE(&ppipe->pipe_sel.si_note, 0);
1413 ppipe->pipe_peer = NULL;
1416 if (cpipe->pipe_kva) {
1417 pmap_qremove(cpipe->pipe_kva, XIO_INTERNAL_PAGES);
1418 kmem_free(&kernel_map, cpipe->pipe_kva, XIO_INTERNAL_SIZE);
1419 cpipe->pipe_kva = NULL;
1423 * free or cache resources
1426 if (gd->gd_pipeqcount >= pipe_maxcache ||
1427 cpipe->pipe_buffer.size != PIPE_SIZE
1429 pipe_free_kmem(cpipe);
1430 kfree(cpipe, M_PIPE);
1432 KKASSERT(cpipe->pipe_map.xio_npages == 0 &&
1433 cpipe->pipe_map.xio_bytes == 0 &&
1434 cpipe->pipe_map.xio_offset == 0);
1435 cpipe->pipe_state = 0;
1436 cpipe->pipe_busy = 0;
1437 cpipe->pipe_peer = gd->gd_pipeq;
1438 gd->gd_pipeq = cpipe;
1439 ++gd->gd_pipeqcount;
1444 * MPALMOSTSAFE - acquires mplock
1447 pipe_kqfilter(struct file *fp, struct knote *kn)
1452 cpipe = (struct pipe *)kn->kn_fp->f_data;
1454 switch (kn->kn_filter) {
1456 kn->kn_fop = &pipe_rfiltops;
1459 kn->kn_fop = &pipe_wfiltops;
1460 cpipe = cpipe->pipe_peer;
1461 if (cpipe == NULL) {
1462 /* other end of pipe has been closed */
1470 kn->kn_hook = (caddr_t)cpipe;
1472 SLIST_INSERT_HEAD(&cpipe->pipe_sel.si_note, kn, kn_selnext);
1478 filt_pipedetach(struct knote *kn)
1480 struct pipe *cpipe = (struct pipe *)kn->kn_hook;
1482 SLIST_REMOVE(&cpipe->pipe_sel.si_note, kn, knote, kn_selnext);
1487 filt_piperead(struct knote *kn, long hint)
1489 struct pipe *rpipe = (struct pipe *)kn->kn_fp->f_data;
1490 struct pipe *wpipe = rpipe->pipe_peer;
1492 kn->kn_data = rpipe->pipe_buffer.cnt;
1493 if ((kn->kn_data == 0) && (rpipe->pipe_state & PIPE_DIRECTW)) {
1494 kn->kn_data = rpipe->pipe_map.xio_bytes -
1495 rpipe->pipe_buffer.out;
1498 if ((rpipe->pipe_state & PIPE_EOF) ||
1499 (wpipe == NULL) || (wpipe->pipe_state & PIPE_EOF)) {
1500 kn->kn_flags |= EV_EOF;
1503 return (kn->kn_data > 0);
1508 filt_pipewrite(struct knote *kn, long hint)
1510 struct pipe *rpipe = (struct pipe *)kn->kn_fp->f_data;
1511 struct pipe *wpipe = rpipe->pipe_peer;
1513 if ((wpipe == NULL) || (wpipe->pipe_state & PIPE_EOF)) {
1515 kn->kn_flags |= EV_EOF;
1518 kn->kn_data = wpipe->pipe_buffer.size - wpipe->pipe_buffer.cnt;
1519 if (wpipe->pipe_state & PIPE_DIRECTW)
1522 return (kn->kn_data >= PIPE_BUF);