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.2 2003/06/17 04:28:41 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>
56 #include <sys/fcntl.h>
58 #include <sys/filedesc.h>
59 #include <sys/filio.h>
60 #include <sys/ttycom.h>
63 #include <sys/select.h>
64 #include <sys/signalvar.h>
65 #include <sys/sysproto.h>
67 #include <sys/vnode.h>
69 #include <sys/event.h>
72 #include <vm/vm_param.h>
74 #include <vm/vm_object.h>
75 #include <vm/vm_kern.h>
76 #include <vm/vm_extern.h>
78 #include <vm/vm_map.h>
79 #include <vm/vm_page.h>
80 #include <vm/vm_zone.h>
83 * Use this define if you want to disable *fancy* VM things. Expect an
84 * approx 30% decrease in transfer rate. This could be useful for
87 /* #define PIPE_NODIRECT */
90 * interfaces to the outside world
92 static int pipe_read __P((struct file *fp, struct uio *uio,
93 struct ucred *cred, int flags, struct proc *p));
94 static int pipe_write __P((struct file *fp, struct uio *uio,
95 struct ucred *cred, int flags, struct proc *p));
96 static int pipe_close __P((struct file *fp, struct proc *p));
97 static int pipe_poll __P((struct file *fp, int events, struct ucred *cred,
99 static int pipe_kqfilter __P((struct file *fp, struct knote *kn));
100 static int pipe_stat __P((struct file *fp, struct stat *sb, struct proc *p));
101 static int pipe_ioctl __P((struct file *fp, u_long cmd, caddr_t data, struct proc *p));
103 static struct fileops pipeops = {
104 pipe_read, pipe_write, pipe_ioctl, pipe_poll, pipe_kqfilter,
105 pipe_stat, pipe_close
108 static void filt_pipedetach(struct knote *kn);
109 static int filt_piperead(struct knote *kn, long hint);
110 static int filt_pipewrite(struct knote *kn, long hint);
112 static struct filterops pipe_rfiltops =
113 { 1, NULL, filt_pipedetach, filt_piperead };
114 static struct filterops pipe_wfiltops =
115 { 1, NULL, filt_pipedetach, filt_pipewrite };
119 * Default pipe buffer size(s), this can be kind-of large now because pipe
120 * space is pageable. The pipe code will try to maintain locality of
121 * reference for performance reasons, so small amounts of outstanding I/O
122 * will not wipe the cache.
124 #define MINPIPESIZE (PIPE_SIZE/3)
125 #define MAXPIPESIZE (2*PIPE_SIZE/3)
128 * Maximum amount of kva for pipes -- this is kind-of a soft limit, but
129 * is there so that on large systems, we don't exhaust it.
131 #define MAXPIPEKVA (8*1024*1024)
134 * Limit for direct transfers, we cannot, of course limit
135 * the amount of kva for pipes in general though.
137 #define LIMITPIPEKVA (16*1024*1024)
140 * Limit the number of "big" pipes
142 #define LIMITBIGPIPES 32
145 static int amountpipekva;
147 static void pipeclose __P((struct pipe *cpipe));
148 static void pipe_free_kmem __P((struct pipe *cpipe));
149 static int pipe_create __P((struct pipe **cpipep));
150 static __inline int pipelock __P((struct pipe *cpipe, int catch));
151 static __inline void pipeunlock __P((struct pipe *cpipe));
152 static __inline void pipeselwakeup __P((struct pipe *cpipe));
153 #ifndef PIPE_NODIRECT
154 static int pipe_build_write_buffer __P((struct pipe *wpipe, struct uio *uio));
155 static void pipe_destroy_write_buffer __P((struct pipe *wpipe));
156 static int pipe_direct_write __P((struct pipe *wpipe, struct uio *uio));
157 static void pipe_clone_write_buffer __P((struct pipe *wpipe));
159 static int pipespace __P((struct pipe *cpipe, int size));
161 static vm_zone_t pipe_zone;
164 * The pipe system call for the DTYPE_PIPE type of pipes
171 struct pipe_args /* {
175 struct filedesc *fdp = p->p_fd;
176 struct file *rf, *wf;
177 struct pipe *rpipe, *wpipe;
180 if (pipe_zone == NULL)
181 pipe_zone = zinit("PIPE", sizeof(struct pipe), 0, 0, 4);
183 rpipe = wpipe = NULL;
184 if (pipe_create(&rpipe) || pipe_create(&wpipe)) {
190 rpipe->pipe_state |= PIPE_DIRECTOK;
191 wpipe->pipe_state |= PIPE_DIRECTOK;
193 error = falloc(p, &rf, &fd);
203 * Warning: once we've gotten past allocation of the fd for the
204 * read-side, we can only drop the read side via fdrop() in order
205 * to avoid races against processes which manage to dup() the read
206 * side while we are blocked trying to allocate the write side.
208 rf->f_flag = FREAD | FWRITE;
209 rf->f_type = DTYPE_PIPE;
210 rf->f_data = (caddr_t)rpipe;
211 rf->f_ops = &pipeops;
212 error = falloc(p, &wf, &fd);
214 if (fdp->fd_ofiles[p->p_retval[0]] == rf) {
215 fdp->fd_ofiles[p->p_retval[0]] = NULL;
219 /* rpipe has been closed by fdrop(). */
223 wf->f_flag = FREAD | FWRITE;
224 wf->f_type = DTYPE_PIPE;
225 wf->f_data = (caddr_t)wpipe;
226 wf->f_ops = &pipeops;
229 rpipe->pipe_peer = wpipe;
230 wpipe->pipe_peer = rpipe;
237 * Allocate kva for pipe circular buffer, the space is pageable
238 * This routine will 'realloc' the size of a pipe safely, if it fails
239 * it will retain the old buffer.
240 * If it fails it will return ENOMEM.
243 pipespace(cpipe, size)
247 struct vm_object *object;
251 npages = round_page(size)/PAGE_SIZE;
253 * Create an object, I don't like the idea of paging to/from
255 * XXX -- minor change needed here for NetBSD/OpenBSD VM systems.
257 object = vm_object_allocate(OBJT_DEFAULT, npages);
258 buffer = (caddr_t) vm_map_min(kernel_map);
261 * Insert the object into the kernel map, and allocate kva for it.
262 * The map entry is, by default, pageable.
263 * XXX -- minor change needed here for NetBSD/OpenBSD VM systems.
265 error = vm_map_find(kernel_map, object, 0,
266 (vm_offset_t *) &buffer, size, 1,
267 VM_PROT_ALL, VM_PROT_ALL, 0);
269 if (error != KERN_SUCCESS) {
270 vm_object_deallocate(object);
274 /* free old resources if we're resizing */
275 pipe_free_kmem(cpipe);
276 cpipe->pipe_buffer.object = object;
277 cpipe->pipe_buffer.buffer = buffer;
278 cpipe->pipe_buffer.size = size;
279 cpipe->pipe_buffer.in = 0;
280 cpipe->pipe_buffer.out = 0;
281 cpipe->pipe_buffer.cnt = 0;
282 amountpipekva += cpipe->pipe_buffer.size;
287 * initialize and allocate VM and memory for pipe
291 struct pipe **cpipep;
296 *cpipep = zalloc(pipe_zone);
302 /* so pipespace()->pipe_free_kmem() doesn't follow junk pointer */
303 cpipe->pipe_buffer.object = NULL;
304 #ifndef PIPE_NODIRECT
305 cpipe->pipe_map.kva = NULL;
308 * protect so pipeclose() doesn't follow a junk pointer
309 * if pipespace() fails.
311 bzero(&cpipe->pipe_sel, sizeof(cpipe->pipe_sel));
312 cpipe->pipe_state = 0;
313 cpipe->pipe_peer = NULL;
314 cpipe->pipe_busy = 0;
316 #ifndef PIPE_NODIRECT
318 * pipe data structure initializations to support direct pipe I/O
320 cpipe->pipe_map.cnt = 0;
321 cpipe->pipe_map.kva = 0;
322 cpipe->pipe_map.pos = 0;
323 cpipe->pipe_map.npages = 0;
324 /* cpipe->pipe_map.ms[] = invalid */
327 error = pipespace(cpipe, PIPE_SIZE);
331 vfs_timestamp(&cpipe->pipe_ctime);
332 cpipe->pipe_atime = cpipe->pipe_ctime;
333 cpipe->pipe_mtime = cpipe->pipe_ctime;
340 * lock a pipe for I/O, blocking other access
343 pipelock(cpipe, catch)
349 while (cpipe->pipe_state & PIPE_LOCK) {
350 cpipe->pipe_state |= PIPE_LWANT;
351 error = tsleep(cpipe, catch ? (PRIBIO | PCATCH) : PRIBIO,
356 cpipe->pipe_state |= PIPE_LOCK;
361 * unlock a pipe I/O lock
368 cpipe->pipe_state &= ~PIPE_LOCK;
369 if (cpipe->pipe_state & PIPE_LWANT) {
370 cpipe->pipe_state &= ~PIPE_LWANT;
380 if (cpipe->pipe_state & PIPE_SEL) {
381 cpipe->pipe_state &= ~PIPE_SEL;
382 selwakeup(&cpipe->pipe_sel);
384 if ((cpipe->pipe_state & PIPE_ASYNC) && cpipe->pipe_sigio)
385 pgsigio(cpipe->pipe_sigio, SIGIO, 0);
386 KNOTE(&cpipe->pipe_sel.si_note, 0);
391 pipe_read(fp, uio, cred, flags, p)
398 struct pipe *rpipe = (struct pipe *) fp->f_data;
404 error = pipelock(rpipe, 1);
408 while (uio->uio_resid) {
410 * normal pipe buffer receive
412 if (rpipe->pipe_buffer.cnt > 0) {
413 size = rpipe->pipe_buffer.size - rpipe->pipe_buffer.out;
414 if (size > rpipe->pipe_buffer.cnt)
415 size = rpipe->pipe_buffer.cnt;
416 if (size > (u_int) uio->uio_resid)
417 size = (u_int) uio->uio_resid;
419 error = uiomove(&rpipe->pipe_buffer.buffer[rpipe->pipe_buffer.out],
424 rpipe->pipe_buffer.out += size;
425 if (rpipe->pipe_buffer.out >= rpipe->pipe_buffer.size)
426 rpipe->pipe_buffer.out = 0;
428 rpipe->pipe_buffer.cnt -= size;
431 * If there is no more to read in the pipe, reset
432 * its pointers to the beginning. This improves
435 if (rpipe->pipe_buffer.cnt == 0) {
436 rpipe->pipe_buffer.in = 0;
437 rpipe->pipe_buffer.out = 0;
440 #ifndef PIPE_NODIRECT
442 * Direct copy, bypassing a kernel buffer.
444 } else if ((size = rpipe->pipe_map.cnt) &&
445 (rpipe->pipe_state & PIPE_DIRECTW)) {
447 if (size > (u_int) uio->uio_resid)
448 size = (u_int) uio->uio_resid;
450 va = (caddr_t) rpipe->pipe_map.kva +
452 error = uiomove(va, size, uio);
456 rpipe->pipe_map.pos += size;
457 rpipe->pipe_map.cnt -= size;
458 if (rpipe->pipe_map.cnt == 0) {
459 rpipe->pipe_state &= ~PIPE_DIRECTW;
465 * detect EOF condition
466 * read returns 0 on EOF, no need to set error
468 if (rpipe->pipe_state & PIPE_EOF)
472 * If the "write-side" has been blocked, wake it up now.
474 if (rpipe->pipe_state & PIPE_WANTW) {
475 rpipe->pipe_state &= ~PIPE_WANTW;
480 * Break if some data was read.
486 * Unlock the pipe buffer for our remaining processing. We
487 * will either break out with an error or we will sleep and
493 * Handle non-blocking mode operation or
494 * wait for more data.
496 if (fp->f_flag & FNONBLOCK) {
499 rpipe->pipe_state |= PIPE_WANTR;
500 if ((error = tsleep(rpipe, PRIBIO | PCATCH,
502 error = pipelock(rpipe, 1);
511 vfs_timestamp(&rpipe->pipe_atime);
516 * PIPE_WANT processing only makes sense if pipe_busy is 0.
518 if ((rpipe->pipe_busy == 0) && (rpipe->pipe_state & PIPE_WANT)) {
519 rpipe->pipe_state &= ~(PIPE_WANT|PIPE_WANTW);
521 } else if (rpipe->pipe_buffer.cnt < MINPIPESIZE) {
523 * Handle write blocking hysteresis.
525 if (rpipe->pipe_state & PIPE_WANTW) {
526 rpipe->pipe_state &= ~PIPE_WANTW;
531 if ((rpipe->pipe_buffer.size - rpipe->pipe_buffer.cnt) >= PIPE_BUF)
532 pipeselwakeup(rpipe);
537 #ifndef PIPE_NODIRECT
539 * Map the sending processes' buffer into kernel space and wire it.
540 * This is similar to a physical write operation.
543 pipe_build_write_buffer(wpipe, uio)
549 vm_offset_t addr, endaddr, paddr;
551 size = (u_int) uio->uio_iov->iov_len;
552 if (size > wpipe->pipe_buffer.size)
553 size = wpipe->pipe_buffer.size;
555 endaddr = round_page((vm_offset_t)uio->uio_iov->iov_base + size);
556 addr = trunc_page((vm_offset_t)uio->uio_iov->iov_base);
557 for (i = 0; addr < endaddr; addr += PAGE_SIZE, i++) {
560 if (vm_fault_quick((caddr_t)addr, VM_PROT_READ) < 0 ||
561 (paddr = pmap_kextract(addr)) == 0) {
564 for (j = 0; j < i; j++)
565 vm_page_unwire(wpipe->pipe_map.ms[j], 1);
569 m = PHYS_TO_VM_PAGE(paddr);
571 wpipe->pipe_map.ms[i] = m;
575 * set up the control block
577 wpipe->pipe_map.npages = i;
578 wpipe->pipe_map.pos =
579 ((vm_offset_t) uio->uio_iov->iov_base) & PAGE_MASK;
580 wpipe->pipe_map.cnt = size;
585 if (wpipe->pipe_map.kva == 0) {
587 * We need to allocate space for an extra page because the
588 * address range might (will) span pages at times.
590 wpipe->pipe_map.kva = kmem_alloc_pageable(kernel_map,
591 wpipe->pipe_buffer.size + PAGE_SIZE);
592 amountpipekva += wpipe->pipe_buffer.size + PAGE_SIZE;
594 pmap_qenter(wpipe->pipe_map.kva, wpipe->pipe_map.ms,
595 wpipe->pipe_map.npages);
598 * and update the uio data
601 uio->uio_iov->iov_len -= size;
602 uio->uio_iov->iov_base += size;
603 if (uio->uio_iov->iov_len == 0)
605 uio->uio_resid -= size;
606 uio->uio_offset += size;
611 * unmap and unwire the process buffer
614 pipe_destroy_write_buffer(wpipe)
619 if (wpipe->pipe_map.kva) {
620 pmap_qremove(wpipe->pipe_map.kva, wpipe->pipe_map.npages);
622 if (amountpipekva > MAXPIPEKVA) {
623 vm_offset_t kva = wpipe->pipe_map.kva;
624 wpipe->pipe_map.kva = 0;
625 kmem_free(kernel_map, kva,
626 wpipe->pipe_buffer.size + PAGE_SIZE);
627 amountpipekva -= wpipe->pipe_buffer.size + PAGE_SIZE;
630 for (i = 0; i < wpipe->pipe_map.npages; i++)
631 vm_page_unwire(wpipe->pipe_map.ms[i], 1);
632 wpipe->pipe_map.npages = 0;
636 * In the case of a signal, the writing process might go away. This
637 * code copies the data into the circular buffer so that the source
638 * pages can be freed without loss of data.
641 pipe_clone_write_buffer(wpipe)
647 size = wpipe->pipe_map.cnt;
648 pos = wpipe->pipe_map.pos;
649 bcopy((caddr_t) wpipe->pipe_map.kva + pos,
650 (caddr_t) wpipe->pipe_buffer.buffer, size);
652 wpipe->pipe_buffer.in = size;
653 wpipe->pipe_buffer.out = 0;
654 wpipe->pipe_buffer.cnt = size;
655 wpipe->pipe_state &= ~PIPE_DIRECTW;
657 pipe_destroy_write_buffer(wpipe);
661 * This implements the pipe buffer write mechanism. Note that only
662 * a direct write OR a normal pipe write can be pending at any given time.
663 * If there are any characters in the pipe buffer, the direct write will
664 * be deferred until the receiving process grabs all of the bytes from
665 * the pipe buffer. Then the direct mapping write is set-up.
668 pipe_direct_write(wpipe, uio)
675 while (wpipe->pipe_state & PIPE_DIRECTW) {
676 if (wpipe->pipe_state & PIPE_WANTR) {
677 wpipe->pipe_state &= ~PIPE_WANTR;
680 wpipe->pipe_state |= PIPE_WANTW;
681 error = tsleep(wpipe, PRIBIO | PCATCH, "pipdww", 0);
684 if (wpipe->pipe_state & PIPE_EOF) {
689 wpipe->pipe_map.cnt = 0; /* transfer not ready yet */
690 if (wpipe->pipe_buffer.cnt > 0) {
691 if (wpipe->pipe_state & PIPE_WANTR) {
692 wpipe->pipe_state &= ~PIPE_WANTR;
696 wpipe->pipe_state |= PIPE_WANTW;
697 error = tsleep(wpipe, PRIBIO | PCATCH, "pipdwc", 0);
700 if (wpipe->pipe_state & PIPE_EOF) {
707 wpipe->pipe_state |= PIPE_DIRECTW;
709 error = pipe_build_write_buffer(wpipe, uio);
711 wpipe->pipe_state &= ~PIPE_DIRECTW;
716 while (!error && (wpipe->pipe_state & PIPE_DIRECTW)) {
717 if (wpipe->pipe_state & PIPE_EOF) {
719 pipe_destroy_write_buffer(wpipe);
721 pipeselwakeup(wpipe);
725 if (wpipe->pipe_state & PIPE_WANTR) {
726 wpipe->pipe_state &= ~PIPE_WANTR;
729 pipeselwakeup(wpipe);
730 error = tsleep(wpipe, PRIBIO | PCATCH, "pipdwt", 0);
734 if (wpipe->pipe_state & PIPE_DIRECTW) {
736 * this bit of trickery substitutes a kernel buffer for
737 * the process that might be going away.
739 pipe_clone_write_buffer(wpipe);
741 pipe_destroy_write_buffer(wpipe);
753 pipe_write(fp, uio, cred, flags, p)
762 struct pipe *wpipe, *rpipe;
764 rpipe = (struct pipe *) fp->f_data;
765 wpipe = rpipe->pipe_peer;
768 * detect loss of pipe read side, issue SIGPIPE if lost.
770 if ((wpipe == NULL) || (wpipe->pipe_state & PIPE_EOF)) {
776 * If it is advantageous to resize the pipe buffer, do
779 if ((uio->uio_resid > PIPE_SIZE) &&
780 (nbigpipe < LIMITBIGPIPES) &&
781 (wpipe->pipe_state & PIPE_DIRECTW) == 0 &&
782 (wpipe->pipe_buffer.size <= PIPE_SIZE) &&
783 (wpipe->pipe_buffer.cnt == 0)) {
785 if ((error = pipelock(wpipe,1)) == 0) {
786 if (pipespace(wpipe, BIG_PIPE_SIZE) == 0)
793 * If an early error occured unbusy and return, waking up any pending
798 if ((wpipe->pipe_busy == 0) &&
799 (wpipe->pipe_state & PIPE_WANT)) {
800 wpipe->pipe_state &= ~(PIPE_WANT | PIPE_WANTR);
806 KASSERT(wpipe->pipe_buffer.buffer != NULL, ("pipe buffer gone"));
808 orig_resid = uio->uio_resid;
810 while (uio->uio_resid) {
813 #ifndef PIPE_NODIRECT
815 * If the transfer is large, we can gain performance if
816 * we do process-to-process copies directly.
817 * If the write is non-blocking, we don't use the
818 * direct write mechanism.
820 * The direct write mechanism will detect the reader going
823 if ((uio->uio_iov->iov_len >= PIPE_MINDIRECT) &&
824 (fp->f_flag & FNONBLOCK) == 0 &&
825 (wpipe->pipe_map.kva || (amountpipekva < LIMITPIPEKVA)) &&
826 (uio->uio_iov->iov_len >= PIPE_MINDIRECT)) {
827 error = pipe_direct_write( wpipe, uio);
835 * Pipe buffered writes cannot be coincidental with
836 * direct writes. We wait until the currently executing
837 * direct write is completed before we start filling the
838 * pipe buffer. We break out if a signal occurs or the
842 while (wpipe->pipe_state & PIPE_DIRECTW) {
843 if (wpipe->pipe_state & PIPE_WANTR) {
844 wpipe->pipe_state &= ~PIPE_WANTR;
847 error = tsleep(wpipe, PRIBIO | PCATCH, "pipbww", 0);
848 if (wpipe->pipe_state & PIPE_EOF)
853 if (wpipe->pipe_state & PIPE_EOF) {
858 space = wpipe->pipe_buffer.size - wpipe->pipe_buffer.cnt;
860 /* Writes of size <= PIPE_BUF must be atomic. */
861 if ((space < uio->uio_resid) && (orig_resid <= PIPE_BUF))
864 if (space > 0 && (wpipe->pipe_buffer.cnt < PIPE_SIZE)) {
865 if ((error = pipelock(wpipe,1)) == 0) {
866 int size; /* Transfer size */
867 int segsize; /* first segment to transfer */
870 * It is possible for a direct write to
871 * slip in on us... handle it here...
873 if (wpipe->pipe_state & PIPE_DIRECTW) {
878 * If a process blocked in uiomove, our
879 * value for space might be bad.
881 * XXX will we be ok if the reader has gone
884 if (space > wpipe->pipe_buffer.size -
885 wpipe->pipe_buffer.cnt) {
891 * Transfer size is minimum of uio transfer
892 * and free space in pipe buffer.
894 if (space > uio->uio_resid)
895 size = uio->uio_resid;
899 * First segment to transfer is minimum of
900 * transfer size and contiguous space in
901 * pipe buffer. If first segment to transfer
902 * is less than the transfer size, we've got
903 * a wraparound in the buffer.
905 segsize = wpipe->pipe_buffer.size -
906 wpipe->pipe_buffer.in;
910 /* Transfer first segment */
912 error = uiomove(&wpipe->pipe_buffer.buffer[wpipe->pipe_buffer.in],
915 if (error == 0 && segsize < size) {
917 * Transfer remaining part now, to
918 * support atomic writes. Wraparound
921 if (wpipe->pipe_buffer.in + segsize !=
922 wpipe->pipe_buffer.size)
923 panic("Expected pipe buffer wraparound disappeared");
925 error = uiomove(&wpipe->pipe_buffer.buffer[0],
926 size - segsize, uio);
929 wpipe->pipe_buffer.in += size;
930 if (wpipe->pipe_buffer.in >=
931 wpipe->pipe_buffer.size) {
932 if (wpipe->pipe_buffer.in != size - segsize + wpipe->pipe_buffer.size)
933 panic("Expected wraparound bad");
934 wpipe->pipe_buffer.in = size - segsize;
937 wpipe->pipe_buffer.cnt += size;
938 if (wpipe->pipe_buffer.cnt > wpipe->pipe_buffer.size)
939 panic("Pipe buffer overflow");
949 * If the "read-side" has been blocked, wake it up now.
951 if (wpipe->pipe_state & PIPE_WANTR) {
952 wpipe->pipe_state &= ~PIPE_WANTR;
957 * don't block on non-blocking I/O
959 if (fp->f_flag & FNONBLOCK) {
965 * We have no more space and have something to offer,
966 * wake up select/poll.
968 pipeselwakeup(wpipe);
970 wpipe->pipe_state |= PIPE_WANTW;
971 error = tsleep(wpipe, PRIBIO | PCATCH, "pipewr", 0);
975 * If read side wants to go away, we just issue a signal
978 if (wpipe->pipe_state & PIPE_EOF) {
987 if ((wpipe->pipe_busy == 0) && (wpipe->pipe_state & PIPE_WANT)) {
988 wpipe->pipe_state &= ~(PIPE_WANT | PIPE_WANTR);
990 } else if (wpipe->pipe_buffer.cnt > 0) {
992 * If we have put any characters in the buffer, we wake up
995 if (wpipe->pipe_state & PIPE_WANTR) {
996 wpipe->pipe_state &= ~PIPE_WANTR;
1002 * Don't return EPIPE if I/O was successful
1004 if ((wpipe->pipe_buffer.cnt == 0) &&
1005 (uio->uio_resid == 0) &&
1011 vfs_timestamp(&wpipe->pipe_mtime);
1014 * We have something to offer,
1015 * wake up select/poll.
1017 if (wpipe->pipe_buffer.cnt)
1018 pipeselwakeup(wpipe);
1024 * we implement a very minimal set of ioctls for compatibility with sockets.
1027 pipe_ioctl(fp, cmd, data, p)
1033 struct pipe *mpipe = (struct pipe *)fp->f_data;
1042 mpipe->pipe_state |= PIPE_ASYNC;
1044 mpipe->pipe_state &= ~PIPE_ASYNC;
1049 if (mpipe->pipe_state & PIPE_DIRECTW)
1050 *(int *)data = mpipe->pipe_map.cnt;
1052 *(int *)data = mpipe->pipe_buffer.cnt;
1056 return (fsetown(*(int *)data, &mpipe->pipe_sigio));
1059 *(int *)data = fgetown(mpipe->pipe_sigio);
1062 /* This is deprecated, FIOSETOWN should be used instead. */
1064 return (fsetown(-(*(int *)data), &mpipe->pipe_sigio));
1066 /* This is deprecated, FIOGETOWN should be used instead. */
1068 *(int *)data = -fgetown(mpipe->pipe_sigio);
1076 pipe_poll(fp, events, cred, p)
1082 struct pipe *rpipe = (struct pipe *)fp->f_data;
1086 wpipe = rpipe->pipe_peer;
1087 if (events & (POLLIN | POLLRDNORM))
1088 if ((rpipe->pipe_state & PIPE_DIRECTW) ||
1089 (rpipe->pipe_buffer.cnt > 0) ||
1090 (rpipe->pipe_state & PIPE_EOF))
1091 revents |= events & (POLLIN | POLLRDNORM);
1093 if (events & (POLLOUT | POLLWRNORM))
1094 if (wpipe == NULL || (wpipe->pipe_state & PIPE_EOF) ||
1095 (((wpipe->pipe_state & PIPE_DIRECTW) == 0) &&
1096 (wpipe->pipe_buffer.size - wpipe->pipe_buffer.cnt) >= PIPE_BUF))
1097 revents |= events & (POLLOUT | POLLWRNORM);
1099 if ((rpipe->pipe_state & PIPE_EOF) ||
1101 (wpipe->pipe_state & PIPE_EOF))
1105 if (events & (POLLIN | POLLRDNORM)) {
1106 selrecord(p, &rpipe->pipe_sel);
1107 rpipe->pipe_state |= PIPE_SEL;
1110 if (events & (POLLOUT | POLLWRNORM)) {
1111 selrecord(p, &wpipe->pipe_sel);
1112 wpipe->pipe_state |= PIPE_SEL;
1120 pipe_stat(fp, ub, p)
1125 struct pipe *pipe = (struct pipe *)fp->f_data;
1127 bzero((caddr_t)ub, sizeof(*ub));
1128 ub->st_mode = S_IFIFO;
1129 ub->st_blksize = pipe->pipe_buffer.size;
1130 ub->st_size = pipe->pipe_buffer.cnt;
1131 ub->st_blocks = (ub->st_size + ub->st_blksize - 1) / ub->st_blksize;
1132 ub->st_atimespec = pipe->pipe_atime;
1133 ub->st_mtimespec = pipe->pipe_mtime;
1134 ub->st_ctimespec = pipe->pipe_ctime;
1136 * Left as 0: st_dev, st_ino, st_nlink, st_uid, st_gid, st_rdev,
1138 * XXX (st_dev, st_ino) should be unique.
1149 struct pipe *cpipe = (struct pipe *)fp->f_data;
1151 fp->f_ops = &badfileops;
1153 funsetown(cpipe->pipe_sigio);
1159 pipe_free_kmem(cpipe)
1163 if (cpipe->pipe_buffer.buffer != NULL) {
1164 if (cpipe->pipe_buffer.size > PIPE_SIZE)
1166 amountpipekva -= cpipe->pipe_buffer.size;
1167 kmem_free(kernel_map,
1168 (vm_offset_t)cpipe->pipe_buffer.buffer,
1169 cpipe->pipe_buffer.size);
1170 cpipe->pipe_buffer.buffer = NULL;
1172 #ifndef PIPE_NODIRECT
1173 if (cpipe->pipe_map.kva != NULL) {
1174 amountpipekva -= cpipe->pipe_buffer.size + PAGE_SIZE;
1175 kmem_free(kernel_map,
1176 cpipe->pipe_map.kva,
1177 cpipe->pipe_buffer.size + PAGE_SIZE);
1178 cpipe->pipe_map.cnt = 0;
1179 cpipe->pipe_map.kva = 0;
1180 cpipe->pipe_map.pos = 0;
1181 cpipe->pipe_map.npages = 0;
1197 pipeselwakeup(cpipe);
1200 * If the other side is blocked, wake it up saying that
1201 * we want to close it down.
1203 while (cpipe->pipe_busy) {
1205 cpipe->pipe_state |= PIPE_WANT | PIPE_EOF;
1206 tsleep(cpipe, PRIBIO, "pipecl", 0);
1210 * Disconnect from peer
1212 if ((ppipe = cpipe->pipe_peer) != NULL) {
1213 pipeselwakeup(ppipe);
1215 ppipe->pipe_state |= PIPE_EOF;
1217 KNOTE(&ppipe->pipe_sel.si_note, 0);
1218 ppipe->pipe_peer = NULL;
1223 pipe_free_kmem(cpipe);
1224 zfree(pipe_zone, cpipe);
1230 pipe_kqfilter(struct file *fp, struct knote *kn)
1232 struct pipe *cpipe = (struct pipe *)kn->kn_fp->f_data;
1234 switch (kn->kn_filter) {
1236 kn->kn_fop = &pipe_rfiltops;
1239 kn->kn_fop = &pipe_wfiltops;
1240 cpipe = cpipe->pipe_peer;
1242 /* other end of pipe has been closed */
1248 kn->kn_hook = (caddr_t)cpipe;
1250 SLIST_INSERT_HEAD(&cpipe->pipe_sel.si_note, kn, kn_selnext);
1255 filt_pipedetach(struct knote *kn)
1257 struct pipe *cpipe = (struct pipe *)kn->kn_hook;
1259 SLIST_REMOVE(&cpipe->pipe_sel.si_note, kn, knote, kn_selnext);
1264 filt_piperead(struct knote *kn, long hint)
1266 struct pipe *rpipe = (struct pipe *)kn->kn_fp->f_data;
1267 struct pipe *wpipe = rpipe->pipe_peer;
1269 kn->kn_data = rpipe->pipe_buffer.cnt;
1270 if ((kn->kn_data == 0) && (rpipe->pipe_state & PIPE_DIRECTW))
1271 kn->kn_data = rpipe->pipe_map.cnt;
1273 if ((rpipe->pipe_state & PIPE_EOF) ||
1274 (wpipe == NULL) || (wpipe->pipe_state & PIPE_EOF)) {
1275 kn->kn_flags |= EV_EOF;
1278 return (kn->kn_data > 0);
1283 filt_pipewrite(struct knote *kn, long hint)
1285 struct pipe *rpipe = (struct pipe *)kn->kn_fp->f_data;
1286 struct pipe *wpipe = rpipe->pipe_peer;
1288 if ((wpipe == NULL) || (wpipe->pipe_state & PIPE_EOF)) {
1290 kn->kn_flags |= EV_EOF;
1293 kn->kn_data = wpipe->pipe_buffer.size - wpipe->pipe_buffer.cnt;
1294 if (wpipe->pipe_state & PIPE_DIRECTW)
1297 return (kn->kn_data >= PIPE_BUF);