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.22 2004/05/13 23:49:23 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>
77 #include <vm/vm_param.h>
79 #include <vm/vm_object.h>
80 #include <vm/vm_kern.h>
81 #include <vm/vm_extern.h>
83 #include <vm/vm_map.h>
84 #include <vm/vm_page.h>
85 #include <vm/vm_zone.h>
87 #include <sys/file2.h>
89 #include <machine/cpufunc.h>
92 * interfaces to the outside world
94 static int pipe_read (struct file *fp, struct uio *uio,
95 struct ucred *cred, int flags, struct thread *td);
96 static int pipe_write (struct file *fp, struct uio *uio,
97 struct ucred *cred, int flags, struct thread *td);
98 static int pipe_close (struct file *fp, struct thread *td);
99 static int pipe_poll (struct file *fp, int events, struct ucred *cred,
101 static int pipe_kqfilter (struct file *fp, struct knote *kn);
102 static int pipe_stat (struct file *fp, struct stat *sb, struct thread *td);
103 static int pipe_ioctl (struct file *fp, u_long cmd, caddr_t data, struct thread *td);
105 static struct fileops pipeops = {
108 pipe_read, pipe_write, pipe_ioctl, pipe_poll, pipe_kqfilter,
109 pipe_stat, pipe_close
112 static void filt_pipedetach(struct knote *kn);
113 static int filt_piperead(struct knote *kn, long hint);
114 static int filt_pipewrite(struct knote *kn, long hint);
116 static struct filterops pipe_rfiltops =
117 { 1, NULL, filt_pipedetach, filt_piperead };
118 static struct filterops pipe_wfiltops =
119 { 1, NULL, filt_pipedetach, filt_pipewrite };
121 MALLOC_DEFINE(M_PIPE, "pipe", "pipe structures");
124 * Default pipe buffer size(s), this can be kind-of large now because pipe
125 * space is pageable. The pipe code will try to maintain locality of
126 * reference for performance reasons, so small amounts of outstanding I/O
127 * will not wipe the cache.
129 #define MINPIPESIZE (PIPE_SIZE/3)
130 #define MAXPIPESIZE (2*PIPE_SIZE/3)
133 * Maximum amount of kva for pipes -- this is kind-of a soft limit, but
134 * is there so that on large systems, we don't exhaust it.
136 #define MAXPIPEKVA (8*1024*1024)
139 * Limit for direct transfers, we cannot, of course limit
140 * the amount of kva for pipes in general though.
142 #define LIMITPIPEKVA (16*1024*1024)
145 * Limit the number of "big" pipes
147 #define LIMITBIGPIPES 32
148 #define PIPEQ_MAX_CACHE 16 /* per-cpu pipe structure cache */
150 static int pipe_maxbig = LIMITBIGPIPES;
151 static int pipe_maxcache = PIPEQ_MAX_CACHE;
152 static int pipe_nbig;
153 static int pipe_bcache_alloc;
154 static int pipe_bkmem_alloc;
155 static int pipe_dwrite_enable = 1; /* 0:copy, 1:kmem/sfbuf 2:force */
156 static int pipe_dwrite_sfbuf = 1; /* 0:kmem_map 1:sfbufs 2:sfbufs_dmap */
157 /* 3:sfbuf_dmap w/ forced invlpg */
159 SYSCTL_NODE(_kern, OID_AUTO, pipe, CTLFLAG_RW, 0, "Pipe operation");
160 SYSCTL_INT(_kern_pipe, OID_AUTO, nbig,
161 CTLFLAG_RD, &pipe_nbig, 0, "numer of big pipes allocated");
162 SYSCTL_INT(_kern_pipe, OID_AUTO, maxcache,
163 CTLFLAG_RW, &pipe_maxcache, 0, "max pipes cached per-cpu");
164 SYSCTL_INT(_kern_pipe, OID_AUTO, maxbig,
165 CTLFLAG_RW, &pipe_maxbig, 0, "max number of big pipes");
166 SYSCTL_INT(_kern_pipe, OID_AUTO, dwrite_enable,
167 CTLFLAG_RW, &pipe_dwrite_enable, 0, "1:enable/2:force direct writes");
168 SYSCTL_INT(_kern_pipe, OID_AUTO, dwrite_sfbuf,
169 CTLFLAG_RW, &pipe_dwrite_sfbuf, 0, "(if dwrite_enable) 0:kmem 1:sfbuf 2:sfbuf_dmap 3:sfbuf_dmap_forceinvlpg");
170 #if !defined(NO_PIPE_SYSCTL_STATS)
171 SYSCTL_INT(_kern_pipe, OID_AUTO, bcache_alloc,
172 CTLFLAG_RW, &pipe_bcache_alloc, 0, "pipe buffer from pcpu cache");
173 SYSCTL_INT(_kern_pipe, OID_AUTO, bkmem_alloc,
174 CTLFLAG_RW, &pipe_bkmem_alloc, 0, "pipe buffer from kmem");
177 static void pipeclose (struct pipe *cpipe);
178 static void pipe_free_kmem (struct pipe *cpipe);
179 static int pipe_create (struct pipe **cpipep);
180 static __inline int pipelock (struct pipe *cpipe, int catch);
181 static __inline void pipeunlock (struct pipe *cpipe);
182 static __inline void pipeselwakeup (struct pipe *cpipe);
183 #ifndef PIPE_NODIRECT
184 static int pipe_build_write_buffer (struct pipe *wpipe, struct uio *uio);
185 static int pipe_direct_write (struct pipe *wpipe, struct uio *uio);
186 static void pipe_clone_write_buffer (struct pipe *wpipe);
188 static int pipespace (struct pipe *cpipe, int size);
191 * The pipe system call for the DTYPE_PIPE type of pipes
193 * pipe_ARgs(int dummy)
198 pipe(struct pipe_args *uap)
200 struct thread *td = curthread;
201 struct proc *p = td->td_proc;
202 struct filedesc *fdp;
203 struct file *rf, *wf;
204 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);
254 uap->sysmsg_fds[0] = fd1;
257 * Warning: once we've gotten past allocation of the fd for the
258 * read-side, we can only drop the read side via fdrop() in order
259 * to avoid races against processes which manage to dup() the read
260 * side while we are blocked trying to allocate the write side.
262 rf->f_flag = FREAD | FWRITE;
263 rf->f_type = DTYPE_PIPE;
264 rf->f_data = (caddr_t)rpipe;
265 rf->f_ops = &pipeops;
266 error = falloc(p, &wf, &fd2);
268 if (fdp->fd_ofiles[fd1] == rf) {
269 fdp->fd_ofiles[fd1] = NULL;
273 /* rpipe has been closed by fdrop(). */
277 wf->f_flag = FREAD | FWRITE;
278 wf->f_type = DTYPE_PIPE;
279 wf->f_data = (caddr_t)wpipe;
280 wf->f_ops = &pipeops;
281 uap->sysmsg_fds[1] = fd2;
283 rpipe->pipe_peer = wpipe;
284 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);
419 pipe_read(struct file *fp, struct uio *uio, struct ucred *cred,
420 int flags, struct thread *td)
422 struct pipe *rpipe = (struct pipe *) fp->f_data;
428 error = pipelock(rpipe, 1);
432 while (uio->uio_resid) {
435 if (rpipe->pipe_buffer.cnt > 0) {
437 * normal pipe buffer receive
439 size = rpipe->pipe_buffer.size - rpipe->pipe_buffer.out;
440 if (size > rpipe->pipe_buffer.cnt)
441 size = rpipe->pipe_buffer.cnt;
442 if (size > (u_int) uio->uio_resid)
443 size = (u_int) uio->uio_resid;
445 error = uiomove(&rpipe->pipe_buffer.buffer[rpipe->pipe_buffer.out],
450 rpipe->pipe_buffer.out += size;
451 if (rpipe->pipe_buffer.out >= rpipe->pipe_buffer.size)
452 rpipe->pipe_buffer.out = 0;
454 rpipe->pipe_buffer.cnt -= size;
457 * If there is no more to read in the pipe, reset
458 * its pointers to the beginning. This improves
461 if (rpipe->pipe_buffer.cnt == 0) {
462 rpipe->pipe_buffer.in = 0;
463 rpipe->pipe_buffer.out = 0;
466 #ifndef PIPE_NODIRECT
467 } else if (rpipe->pipe_kva &&
468 rpipe->pipe_feature == PIPE_KMEM &&
469 (rpipe->pipe_state & (PIPE_DIRECTW|PIPE_DIRECTIP))
473 * Direct copy using source-side kva mapping
475 size = rpipe->pipe_map.xio_bytes;
476 if (size > (u_int)uio->uio_resid)
477 size = (u_int)uio->uio_resid;
478 va = (caddr_t)rpipe->pipe_kva + rpipe->pipe_map.xio_offset;
479 error = uiomove(va, size, uio);
483 rpipe->pipe_map.xio_offset += size;
484 rpipe->pipe_map.xio_bytes -= size;
485 if (rpipe->pipe_map.xio_bytes == 0) {
486 rpipe->pipe_state |= PIPE_DIRECTIP;
487 rpipe->pipe_state &= ~PIPE_DIRECTW;
490 } else if (rpipe->pipe_map.xio_bytes &&
492 rpipe->pipe_feature == PIPE_SFBUF2 &&
493 (rpipe->pipe_state & (PIPE_DIRECTW|PIPE_DIRECTIP))
497 * Direct copy, bypassing a kernel buffer. We cannot
498 * mess with the direct-write buffer until
499 * PIPE_DIRECTIP is cleared. In order to prevent
500 * the pipe_write code from racing itself in
501 * direct_write, we set DIRECTIP when we clear
502 * DIRECTW after we have exhausted the buffer.
504 if (pipe_dwrite_sfbuf == 3)
505 rpipe->pipe_kvamask = 0;
506 pmap_qenter2(rpipe->pipe_kva, rpipe->pipe_map.xio_pages,
507 rpipe->pipe_map.xio_npages,
508 &rpipe->pipe_kvamask);
509 size = rpipe->pipe_map.xio_bytes;
510 if (size > (u_int)uio->uio_resid)
511 size = (u_int)uio->uio_resid;
512 va = (caddr_t)rpipe->pipe_kva +
513 rpipe->pipe_map.xio_offset;
514 error = uiomove(va, size, uio);
518 rpipe->pipe_map.xio_offset += size;
519 rpipe->pipe_map.xio_bytes -= size;
520 if (rpipe->pipe_map.xio_bytes == 0) {
521 rpipe->pipe_state |= PIPE_DIRECTIP;
522 rpipe->pipe_state &= ~PIPE_DIRECTW;
525 } else if (rpipe->pipe_map.xio_bytes &&
526 rpipe->pipe_feature == PIPE_SFBUF1 &&
527 (rpipe->pipe_state & (PIPE_DIRECTW|PIPE_DIRECTIP))
531 * Direct copy, bypassing a kernel buffer. We cannot
532 * mess with the direct-write buffer until
533 * PIPE_DIRECTIP is cleared. In order to prevent
534 * the pipe_write code from racing itself in
535 * direct_write, we set DIRECTIP when we clear
536 * DIRECTW after we have exhausted the buffer.
538 error = xio_uio_copy(&rpipe->pipe_map, uio, &size);
542 if (rpipe->pipe_map.xio_bytes == 0) {
543 rpipe->pipe_state |= PIPE_DIRECTIP;
544 rpipe->pipe_state &= ~PIPE_DIRECTW;
550 * detect EOF condition
551 * read returns 0 on EOF, no need to set error
553 if (rpipe->pipe_state & PIPE_EOF)
557 * If the "write-side" has been blocked, wake it up now.
559 if (rpipe->pipe_state & PIPE_WANTW) {
560 rpipe->pipe_state &= ~PIPE_WANTW;
565 * Break if some data was read.
571 * Unlock the pipe buffer for our remaining
572 * processing. We will either break out with an
573 * error or we will sleep and relock to loop.
578 * Handle non-blocking mode operation or
579 * wait for more data.
581 if (fp->f_flag & FNONBLOCK) {
584 rpipe->pipe_state |= PIPE_WANTR;
585 if ((error = tsleep(rpipe, PCATCH|PNORESCHED,
586 "piperd", 0)) == 0) {
587 error = pipelock(rpipe, 1);
597 vfs_timestamp(&rpipe->pipe_atime);
602 * PIPE_WANT processing only makes sense if pipe_busy is 0.
604 if ((rpipe->pipe_busy == 0) && (rpipe->pipe_state & PIPE_WANT)) {
605 rpipe->pipe_state &= ~(PIPE_WANT|PIPE_WANTW);
607 } else if (rpipe->pipe_buffer.cnt < MINPIPESIZE) {
609 * Handle write blocking hysteresis.
611 if (rpipe->pipe_state & PIPE_WANTW) {
612 rpipe->pipe_state &= ~PIPE_WANTW;
617 if ((rpipe->pipe_buffer.size - rpipe->pipe_buffer.cnt) >= PIPE_BUF)
618 pipeselwakeup(rpipe);
622 #ifndef PIPE_NODIRECT
624 * Map the sending processes' buffer into kernel space and wire it.
625 * This is similar to a physical write operation.
628 pipe_build_write_buffer(wpipe, uio)
635 size = (u_int) uio->uio_iov->iov_len;
636 if (size > wpipe->pipe_buffer.size)
637 size = wpipe->pipe_buffer.size;
639 error = xio_init_ubuf(&wpipe->pipe_map, uio->uio_iov->iov_base,
645 * Create a kernel map for KMEM and SFBUF2 copy modes. SFBUF2 will
646 * map the pages on the target while KMEM maps the pages now.
648 switch(wpipe->pipe_feature) {
651 if (wpipe->pipe_kva == NULL) {
653 kmem_alloc_nofault(kernel_map, XIO_INTERNAL_SIZE);
654 wpipe->pipe_kvamask = 0;
656 if (wpipe->pipe_feature == PIPE_KMEM) {
657 pmap_qenter(wpipe->pipe_kva, wpipe->pipe_map.xio_pages,
658 wpipe->pipe_map.xio_npages);
666 * And update the uio data. The XIO might have loaded fewer bytes
667 * then requested so reload 'size'.
669 size = wpipe->pipe_map.xio_bytes;
670 uio->uio_iov->iov_len -= size;
671 uio->uio_iov->iov_base += size;
672 if (uio->uio_iov->iov_len == 0)
674 uio->uio_resid -= size;
675 uio->uio_offset += size;
680 * In the case of a signal, the writing process might go away. This
681 * code copies the data into the circular buffer so that the source
682 * pages can be freed without loss of data.
685 pipe_clone_write_buffer(wpipe)
690 size = wpipe->pipe_map.xio_bytes;
692 KKASSERT(size <= wpipe->pipe_buffer.size);
694 wpipe->pipe_buffer.in = size;
695 wpipe->pipe_buffer.out = 0;
696 wpipe->pipe_buffer.cnt = size;
697 wpipe->pipe_state &= ~(PIPE_DIRECTW | PIPE_DIRECTIP);
699 xio_copy_xtok(&wpipe->pipe_map, wpipe->pipe_buffer.buffer, size);
700 xio_release(&wpipe->pipe_map);
701 if (wpipe->pipe_kva) {
702 pmap_qremove(wpipe->pipe_kva, XIO_INTERNAL_PAGES);
703 kmem_free(kernel_map, wpipe->pipe_kva, XIO_INTERNAL_SIZE);
704 wpipe->pipe_kva = NULL;
709 * This implements the pipe buffer write mechanism. Note that only
710 * a direct write OR a normal pipe write can be pending at any given time.
711 * If there are any characters in the pipe buffer, the direct write will
712 * be deferred until the receiving process grabs all of the bytes from
713 * the pipe buffer. Then the direct mapping write is set-up.
716 pipe_direct_write(wpipe, uio)
723 while (wpipe->pipe_state & (PIPE_DIRECTW|PIPE_DIRECTIP)) {
724 if (wpipe->pipe_state & PIPE_WANTR) {
725 wpipe->pipe_state &= ~PIPE_WANTR;
728 wpipe->pipe_state |= PIPE_WANTW;
729 error = tsleep(wpipe, PCATCH, "pipdww", 0);
732 if (wpipe->pipe_state & PIPE_EOF) {
737 KKASSERT(wpipe->pipe_map.xio_bytes == 0);
738 if (wpipe->pipe_buffer.cnt > 0) {
739 if (wpipe->pipe_state & PIPE_WANTR) {
740 wpipe->pipe_state &= ~PIPE_WANTR;
744 wpipe->pipe_state |= PIPE_WANTW;
745 error = tsleep(wpipe, PCATCH, "pipdwc", 0);
748 if (wpipe->pipe_state & PIPE_EOF) {
756 * Build our direct-write buffer
758 wpipe->pipe_state |= PIPE_DIRECTW | PIPE_DIRECTIP;
759 error = pipe_build_write_buffer(wpipe, uio);
762 wpipe->pipe_state &= ~PIPE_DIRECTIP;
765 * Wait until the receiver has snarfed the data. Since we are likely
766 * going to sleep we optimize the case and yield synchronously,
767 * possibly avoiding the tsleep().
770 while (!error && (wpipe->pipe_state & PIPE_DIRECTW)) {
771 if (wpipe->pipe_state & PIPE_EOF) {
773 xio_release(&wpipe->pipe_map);
774 if (wpipe->pipe_kva) {
775 pmap_qremove(wpipe->pipe_kva, XIO_INTERNAL_PAGES);
776 kmem_free(kernel_map, wpipe->pipe_kva, XIO_INTERNAL_SIZE);
777 wpipe->pipe_kva = NULL;
780 pipeselwakeup(wpipe);
784 if (wpipe->pipe_state & PIPE_WANTR) {
785 wpipe->pipe_state &= ~PIPE_WANTR;
788 pipeselwakeup(wpipe);
789 error = tsleep(wpipe, PCATCH|PNORESCHED, "pipdwt", 0);
792 if (wpipe->pipe_state & PIPE_DIRECTW) {
794 * this bit of trickery substitutes a kernel buffer for
795 * the process that might be going away.
797 pipe_clone_write_buffer(wpipe);
798 KKASSERT((wpipe->pipe_state & PIPE_DIRECTIP) == 0);
801 * note: The pipe_kva mapping is not qremove'd here. For
802 * legacy PIPE_KMEM mode this constitutes an improvement
803 * over the original FreeBSD-4 algorithm. For PIPE_SFBUF2
804 * mode the kva mapping must not be removed to get the
807 * For testing purposes we will give the original algorithm
808 * the benefit of the doubt 'what it could have been', and
809 * keep the optimization.
811 KKASSERT(wpipe->pipe_state & PIPE_DIRECTIP);
812 xio_release(&wpipe->pipe_map);
813 wpipe->pipe_state &= ~PIPE_DIRECTIP;
819 * Direct-write error, clear the direct write flags.
822 wpipe->pipe_state &= ~(PIPE_DIRECTW | PIPE_DIRECTIP);
826 * General error, wakeup the other side if it happens to be sleeping.
835 pipe_write(struct file *fp, struct uio *uio, struct ucred *cred,
836 int flags, struct thread *td)
840 struct pipe *wpipe, *rpipe;
842 rpipe = (struct pipe *) fp->f_data;
843 wpipe = rpipe->pipe_peer;
846 * detect loss of pipe read side, issue SIGPIPE if lost.
848 if ((wpipe == NULL) || (wpipe->pipe_state & PIPE_EOF)) {
854 * If it is advantageous to resize the pipe buffer, do
857 if ((uio->uio_resid > PIPE_SIZE) &&
858 (pipe_nbig < pipe_maxbig) &&
859 (wpipe->pipe_state & (PIPE_DIRECTW|PIPE_DIRECTIP)) == 0 &&
860 (wpipe->pipe_buffer.size <= PIPE_SIZE) &&
861 (wpipe->pipe_buffer.cnt == 0)) {
863 if ((error = pipelock(wpipe,1)) == 0) {
864 if (pipespace(wpipe, BIG_PIPE_SIZE) == 0)
871 * If an early error occured unbusy and return, waking up any pending
876 if ((wpipe->pipe_busy == 0) &&
877 (wpipe->pipe_state & PIPE_WANT)) {
878 wpipe->pipe_state &= ~(PIPE_WANT | PIPE_WANTR);
884 KASSERT(wpipe->pipe_buffer.buffer != NULL, ("pipe buffer gone"));
886 orig_resid = uio->uio_resid;
888 while (uio->uio_resid) {
891 #ifndef PIPE_NODIRECT
893 * If the transfer is large, we can gain performance if
894 * we do process-to-process copies directly.
895 * If the write is non-blocking, we don't use the
896 * direct write mechanism.
898 * The direct write mechanism will detect the reader going
901 if ((uio->uio_iov->iov_len >= PIPE_MINDIRECT ||
902 pipe_dwrite_enable > 1) &&
903 (fp->f_flag & FNONBLOCK) == 0 &&
904 pipe_dwrite_enable) {
905 error = pipe_direct_write( wpipe, uio);
913 * Pipe buffered writes cannot be coincidental with
914 * direct writes. We wait until the currently executing
915 * direct write is completed before we start filling the
916 * pipe buffer. We break out if a signal occurs or the
920 while (wpipe->pipe_state & (PIPE_DIRECTW|PIPE_DIRECTIP)) {
921 if (wpipe->pipe_state & PIPE_WANTR) {
922 wpipe->pipe_state &= ~PIPE_WANTR;
925 error = tsleep(wpipe, PCATCH, "pipbww", 0);
926 if (wpipe->pipe_state & PIPE_EOF)
931 if (wpipe->pipe_state & PIPE_EOF) {
936 space = wpipe->pipe_buffer.size - wpipe->pipe_buffer.cnt;
938 /* Writes of size <= PIPE_BUF must be atomic. */
939 if ((space < uio->uio_resid) && (orig_resid <= PIPE_BUF))
943 * Write to fill, read size handles write hysteresis. Also
944 * additional restrictions can cause select-based non-blocking
948 if ((error = pipelock(wpipe,1)) == 0) {
949 int size; /* Transfer size */
950 int segsize; /* first segment to transfer */
953 * It is possible for a direct write to
954 * slip in on us... handle it here...
956 if (wpipe->pipe_state & (PIPE_DIRECTW|PIPE_DIRECTIP)) {
961 * If a process blocked in uiomove, our
962 * value for space might be bad.
964 * XXX will we be ok if the reader has gone
967 if (space > wpipe->pipe_buffer.size -
968 wpipe->pipe_buffer.cnt) {
974 * Transfer size is minimum of uio transfer
975 * and free space in pipe buffer.
977 if (space > uio->uio_resid)
978 size = uio->uio_resid;
982 * First segment to transfer is minimum of
983 * transfer size and contiguous space in
984 * pipe buffer. If first segment to transfer
985 * is less than the transfer size, we've got
986 * a wraparound in the buffer.
988 segsize = wpipe->pipe_buffer.size -
989 wpipe->pipe_buffer.in;
993 /* Transfer first segment */
995 error = uiomove(&wpipe->pipe_buffer.buffer[wpipe->pipe_buffer.in],
998 if (error == 0 && segsize < size) {
1000 * Transfer remaining part now, to
1001 * support atomic writes. Wraparound
1004 if (wpipe->pipe_buffer.in + segsize !=
1005 wpipe->pipe_buffer.size)
1006 panic("Expected pipe buffer wraparound disappeared");
1008 error = uiomove(&wpipe->pipe_buffer.buffer[0],
1009 size - segsize, uio);
1012 wpipe->pipe_buffer.in += size;
1013 if (wpipe->pipe_buffer.in >=
1014 wpipe->pipe_buffer.size) {
1015 if (wpipe->pipe_buffer.in != size - segsize + wpipe->pipe_buffer.size)
1016 panic("Expected wraparound bad");
1017 wpipe->pipe_buffer.in = size - segsize;
1020 wpipe->pipe_buffer.cnt += size;
1021 if (wpipe->pipe_buffer.cnt > wpipe->pipe_buffer.size)
1022 panic("Pipe buffer overflow");
1032 * If the "read-side" has been blocked, wake it up now
1033 * and yield to let it drain synchronously rather
1036 if (wpipe->pipe_state & PIPE_WANTR) {
1037 wpipe->pipe_state &= ~PIPE_WANTR;
1042 * don't block on non-blocking I/O
1044 if (fp->f_flag & FNONBLOCK) {
1050 * We have no more space and have something to offer,
1051 * wake up select/poll.
1053 pipeselwakeup(wpipe);
1055 wpipe->pipe_state |= PIPE_WANTW;
1056 error = tsleep(wpipe, PCATCH|PNORESCHED, "pipewr", 0);
1060 * If read side wants to go away, we just issue a signal
1063 if (wpipe->pipe_state & PIPE_EOF) {
1072 if ((wpipe->pipe_busy == 0) && (wpipe->pipe_state & PIPE_WANT)) {
1073 wpipe->pipe_state &= ~(PIPE_WANT | PIPE_WANTR);
1075 } else if (wpipe->pipe_buffer.cnt > 0) {
1077 * If we have put any characters in the buffer, we wake up
1080 if (wpipe->pipe_state & PIPE_WANTR) {
1081 wpipe->pipe_state &= ~PIPE_WANTR;
1087 * Don't return EPIPE if I/O was successful
1089 if ((wpipe->pipe_buffer.cnt == 0) &&
1090 (uio->uio_resid == 0) &&
1096 vfs_timestamp(&wpipe->pipe_mtime);
1099 * We have something to offer,
1100 * wake up select/poll.
1102 if (wpipe->pipe_buffer.cnt)
1103 pipeselwakeup(wpipe);
1109 * we implement a very minimal set of ioctls for compatibility with sockets.
1112 pipe_ioctl(struct file *fp, u_long cmd, caddr_t data, struct thread *td)
1114 struct pipe *mpipe = (struct pipe *)fp->f_data;
1123 mpipe->pipe_state |= PIPE_ASYNC;
1125 mpipe->pipe_state &= ~PIPE_ASYNC;
1130 if (mpipe->pipe_state & PIPE_DIRECTW) {
1131 *(int *)data = mpipe->pipe_map.xio_bytes;
1133 *(int *)data = mpipe->pipe_buffer.cnt;
1138 return (fsetown(*(int *)data, &mpipe->pipe_sigio));
1141 *(int *)data = fgetown(mpipe->pipe_sigio);
1144 /* This is deprecated, FIOSETOWN should be used instead. */
1146 return (fsetown(-(*(int *)data), &mpipe->pipe_sigio));
1148 /* This is deprecated, FIOGETOWN should be used instead. */
1150 *(int *)data = -fgetown(mpipe->pipe_sigio);
1158 pipe_poll(struct file *fp, int events, struct ucred *cred, struct thread *td)
1160 struct pipe *rpipe = (struct pipe *)fp->f_data;
1164 wpipe = rpipe->pipe_peer;
1165 if (events & (POLLIN | POLLRDNORM))
1166 if ((rpipe->pipe_state & PIPE_DIRECTW) ||
1167 (rpipe->pipe_buffer.cnt > 0) ||
1168 (rpipe->pipe_state & PIPE_EOF))
1169 revents |= events & (POLLIN | POLLRDNORM);
1171 if (events & (POLLOUT | POLLWRNORM))
1172 if (wpipe == NULL || (wpipe->pipe_state & PIPE_EOF) ||
1173 (((wpipe->pipe_state & PIPE_DIRECTW) == 0) &&
1174 (wpipe->pipe_buffer.size - wpipe->pipe_buffer.cnt) >= PIPE_BUF))
1175 revents |= events & (POLLOUT | POLLWRNORM);
1177 if ((rpipe->pipe_state & PIPE_EOF) ||
1179 (wpipe->pipe_state & PIPE_EOF))
1183 if (events & (POLLIN | POLLRDNORM)) {
1184 selrecord(td, &rpipe->pipe_sel);
1185 rpipe->pipe_state |= PIPE_SEL;
1188 if (events & (POLLOUT | POLLWRNORM)) {
1189 selrecord(td, &wpipe->pipe_sel);
1190 wpipe->pipe_state |= PIPE_SEL;
1198 pipe_stat(struct file *fp, struct stat *ub, struct thread *td)
1200 struct pipe *pipe = (struct pipe *)fp->f_data;
1202 bzero((caddr_t)ub, sizeof(*ub));
1203 ub->st_mode = S_IFIFO;
1204 ub->st_blksize = pipe->pipe_buffer.size;
1205 ub->st_size = pipe->pipe_buffer.cnt;
1206 ub->st_blocks = (ub->st_size + ub->st_blksize - 1) / ub->st_blksize;
1207 ub->st_atimespec = pipe->pipe_atime;
1208 ub->st_mtimespec = pipe->pipe_mtime;
1209 ub->st_ctimespec = pipe->pipe_ctime;
1211 * Left as 0: st_dev, st_ino, st_nlink, st_uid, st_gid, st_rdev,
1213 * XXX (st_dev, st_ino) should be unique.
1220 pipe_close(struct file *fp, struct thread *td)
1222 struct pipe *cpipe = (struct pipe *)fp->f_data;
1224 fp->f_ops = &badfileops;
1226 funsetown(cpipe->pipe_sigio);
1232 pipe_free_kmem(struct pipe *cpipe)
1234 if (cpipe->pipe_buffer.buffer != NULL) {
1235 if (cpipe->pipe_buffer.size > PIPE_SIZE)
1237 kmem_free(kernel_map,
1238 (vm_offset_t)cpipe->pipe_buffer.buffer,
1239 cpipe->pipe_buffer.size);
1240 cpipe->pipe_buffer.buffer = NULL;
1241 cpipe->pipe_buffer.object = NULL;
1243 #ifndef PIPE_NODIRECT
1244 KKASSERT(cpipe->pipe_map.xio_bytes == 0 &&
1245 cpipe->pipe_map.xio_offset == 0 &&
1246 cpipe->pipe_map.xio_npages == 0);
1254 pipeclose(struct pipe *cpipe)
1262 pipeselwakeup(cpipe);
1265 * If the other side is blocked, wake it up saying that
1266 * we want to close it down.
1268 while (cpipe->pipe_busy) {
1270 cpipe->pipe_state |= PIPE_WANT | PIPE_EOF;
1271 tsleep(cpipe, 0, "pipecl", 0);
1275 * Disconnect from peer
1277 if ((ppipe = cpipe->pipe_peer) != NULL) {
1278 pipeselwakeup(ppipe);
1280 ppipe->pipe_state |= PIPE_EOF;
1282 KNOTE(&ppipe->pipe_sel.si_note, 0);
1283 ppipe->pipe_peer = NULL;
1286 if (cpipe->pipe_kva) {
1287 pmap_qremove(cpipe->pipe_kva, XIO_INTERNAL_PAGES);
1288 kmem_free(kernel_map, cpipe->pipe_kva, XIO_INTERNAL_SIZE);
1289 cpipe->pipe_kva = NULL;
1293 * free or cache resources
1296 if (gd->gd_pipeqcount >= pipe_maxcache ||
1297 cpipe->pipe_buffer.size != PIPE_SIZE
1299 pipe_free_kmem(cpipe);
1300 free(cpipe, M_PIPE);
1302 KKASSERT(cpipe->pipe_map.xio_npages == 0 &&
1303 cpipe->pipe_map.xio_bytes == 0 &&
1304 cpipe->pipe_map.xio_offset == 0);
1305 cpipe->pipe_state = 0;
1306 cpipe->pipe_busy = 0;
1307 cpipe->pipe_peer = gd->gd_pipeq;
1308 gd->gd_pipeq = cpipe;
1309 ++gd->gd_pipeqcount;
1315 pipe_kqfilter(struct file *fp, struct knote *kn)
1317 struct pipe *cpipe = (struct pipe *)kn->kn_fp->f_data;
1319 switch (kn->kn_filter) {
1321 kn->kn_fop = &pipe_rfiltops;
1324 kn->kn_fop = &pipe_wfiltops;
1325 cpipe = cpipe->pipe_peer;
1327 /* other end of pipe has been closed */
1333 kn->kn_hook = (caddr_t)cpipe;
1335 SLIST_INSERT_HEAD(&cpipe->pipe_sel.si_note, kn, kn_selnext);
1340 filt_pipedetach(struct knote *kn)
1342 struct pipe *cpipe = (struct pipe *)kn->kn_hook;
1344 SLIST_REMOVE(&cpipe->pipe_sel.si_note, kn, knote, kn_selnext);
1349 filt_piperead(struct knote *kn, long hint)
1351 struct pipe *rpipe = (struct pipe *)kn->kn_fp->f_data;
1352 struct pipe *wpipe = rpipe->pipe_peer;
1354 kn->kn_data = rpipe->pipe_buffer.cnt;
1355 if ((kn->kn_data == 0) && (rpipe->pipe_state & PIPE_DIRECTW))
1356 kn->kn_data = rpipe->pipe_map.xio_bytes;
1358 if ((rpipe->pipe_state & PIPE_EOF) ||
1359 (wpipe == NULL) || (wpipe->pipe_state & PIPE_EOF)) {
1360 kn->kn_flags |= EV_EOF;
1363 return (kn->kn_data > 0);
1368 filt_pipewrite(struct knote *kn, long hint)
1370 struct pipe *rpipe = (struct pipe *)kn->kn_fp->f_data;
1371 struct pipe *wpipe = rpipe->pipe_peer;
1373 if ((wpipe == NULL) || (wpipe->pipe_state & PIPE_EOF)) {
1375 kn->kn_flags |= EV_EOF;
1378 kn->kn_data = wpipe->pipe_buffer.size - wpipe->pipe_buffer.cnt;
1379 if (wpipe->pipe_state & PIPE_DIRECTW)
1382 return (kn->kn_data >= PIPE_BUF);