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.3 2003/06/23 17:55: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
166 * pipe_ARgs(int dummy)
171 pipe(struct pipe_args *uap)
173 struct proc *p = curproc;
174 struct filedesc *fdp = p->p_fd;
175 struct file *rf, *wf;
176 struct pipe *rpipe, *wpipe;
179 if (pipe_zone == NULL)
180 pipe_zone = zinit("PIPE", sizeof(struct pipe), 0, 0, 4);
182 rpipe = wpipe = NULL;
183 if (pipe_create(&rpipe) || pipe_create(&wpipe)) {
189 rpipe->pipe_state |= PIPE_DIRECTOK;
190 wpipe->pipe_state |= PIPE_DIRECTOK;
192 error = falloc(p, &rf, &fd);
202 * Warning: once we've gotten past allocation of the fd for the
203 * read-side, we can only drop the read side via fdrop() in order
204 * to avoid races against processes which manage to dup() the read
205 * side while we are blocked trying to allocate the write side.
207 rf->f_flag = FREAD | FWRITE;
208 rf->f_type = DTYPE_PIPE;
209 rf->f_data = (caddr_t)rpipe;
210 rf->f_ops = &pipeops;
211 error = falloc(p, &wf, &fd);
213 if (fdp->fd_ofiles[p->p_retval[0]] == rf) {
214 fdp->fd_ofiles[p->p_retval[0]] = NULL;
218 /* rpipe has been closed by fdrop(). */
222 wf->f_flag = FREAD | FWRITE;
223 wf->f_type = DTYPE_PIPE;
224 wf->f_data = (caddr_t)wpipe;
225 wf->f_ops = &pipeops;
228 rpipe->pipe_peer = wpipe;
229 wpipe->pipe_peer = rpipe;
236 * Allocate kva for pipe circular buffer, the space is pageable
237 * This routine will 'realloc' the size of a pipe safely, if it fails
238 * it will retain the old buffer.
239 * If it fails it will return ENOMEM.
242 pipespace(cpipe, size)
246 struct vm_object *object;
250 npages = round_page(size)/PAGE_SIZE;
252 * Create an object, I don't like the idea of paging to/from
254 * XXX -- minor change needed here for NetBSD/OpenBSD VM systems.
256 object = vm_object_allocate(OBJT_DEFAULT, npages);
257 buffer = (caddr_t) vm_map_min(kernel_map);
260 * Insert the object into the kernel map, and allocate kva for it.
261 * The map entry is, by default, pageable.
262 * XXX -- minor change needed here for NetBSD/OpenBSD VM systems.
264 error = vm_map_find(kernel_map, object, 0,
265 (vm_offset_t *) &buffer, size, 1,
266 VM_PROT_ALL, VM_PROT_ALL, 0);
268 if (error != KERN_SUCCESS) {
269 vm_object_deallocate(object);
273 /* free old resources if we're resizing */
274 pipe_free_kmem(cpipe);
275 cpipe->pipe_buffer.object = object;
276 cpipe->pipe_buffer.buffer = buffer;
277 cpipe->pipe_buffer.size = size;
278 cpipe->pipe_buffer.in = 0;
279 cpipe->pipe_buffer.out = 0;
280 cpipe->pipe_buffer.cnt = 0;
281 amountpipekva += cpipe->pipe_buffer.size;
286 * initialize and allocate VM and memory for pipe
290 struct pipe **cpipep;
295 *cpipep = zalloc(pipe_zone);
301 /* so pipespace()->pipe_free_kmem() doesn't follow junk pointer */
302 cpipe->pipe_buffer.object = NULL;
303 #ifndef PIPE_NODIRECT
304 cpipe->pipe_map.kva = NULL;
307 * protect so pipeclose() doesn't follow a junk pointer
308 * if pipespace() fails.
310 bzero(&cpipe->pipe_sel, sizeof(cpipe->pipe_sel));
311 cpipe->pipe_state = 0;
312 cpipe->pipe_peer = NULL;
313 cpipe->pipe_busy = 0;
315 #ifndef PIPE_NODIRECT
317 * pipe data structure initializations to support direct pipe I/O
319 cpipe->pipe_map.cnt = 0;
320 cpipe->pipe_map.kva = 0;
321 cpipe->pipe_map.pos = 0;
322 cpipe->pipe_map.npages = 0;
323 /* cpipe->pipe_map.ms[] = invalid */
326 error = pipespace(cpipe, PIPE_SIZE);
330 vfs_timestamp(&cpipe->pipe_ctime);
331 cpipe->pipe_atime = cpipe->pipe_ctime;
332 cpipe->pipe_mtime = cpipe->pipe_ctime;
339 * lock a pipe for I/O, blocking other access
342 pipelock(cpipe, catch)
348 while (cpipe->pipe_state & PIPE_LOCK) {
349 cpipe->pipe_state |= PIPE_LWANT;
350 error = tsleep(cpipe, catch ? (PRIBIO | PCATCH) : PRIBIO,
355 cpipe->pipe_state |= PIPE_LOCK;
360 * unlock a pipe I/O lock
367 cpipe->pipe_state &= ~PIPE_LOCK;
368 if (cpipe->pipe_state & PIPE_LWANT) {
369 cpipe->pipe_state &= ~PIPE_LWANT;
379 if (cpipe->pipe_state & PIPE_SEL) {
380 cpipe->pipe_state &= ~PIPE_SEL;
381 selwakeup(&cpipe->pipe_sel);
383 if ((cpipe->pipe_state & PIPE_ASYNC) && cpipe->pipe_sigio)
384 pgsigio(cpipe->pipe_sigio, SIGIO, 0);
385 KNOTE(&cpipe->pipe_sel.si_note, 0);
390 pipe_read(fp, uio, cred, flags, p)
397 struct pipe *rpipe = (struct pipe *) fp->f_data;
403 error = pipelock(rpipe, 1);
407 while (uio->uio_resid) {
409 * normal pipe buffer receive
411 if (rpipe->pipe_buffer.cnt > 0) {
412 size = rpipe->pipe_buffer.size - rpipe->pipe_buffer.out;
413 if (size > rpipe->pipe_buffer.cnt)
414 size = rpipe->pipe_buffer.cnt;
415 if (size > (u_int) uio->uio_resid)
416 size = (u_int) uio->uio_resid;
418 error = uiomove(&rpipe->pipe_buffer.buffer[rpipe->pipe_buffer.out],
423 rpipe->pipe_buffer.out += size;
424 if (rpipe->pipe_buffer.out >= rpipe->pipe_buffer.size)
425 rpipe->pipe_buffer.out = 0;
427 rpipe->pipe_buffer.cnt -= size;
430 * If there is no more to read in the pipe, reset
431 * its pointers to the beginning. This improves
434 if (rpipe->pipe_buffer.cnt == 0) {
435 rpipe->pipe_buffer.in = 0;
436 rpipe->pipe_buffer.out = 0;
439 #ifndef PIPE_NODIRECT
441 * Direct copy, bypassing a kernel buffer.
443 } else if ((size = rpipe->pipe_map.cnt) &&
444 (rpipe->pipe_state & PIPE_DIRECTW)) {
446 if (size > (u_int) uio->uio_resid)
447 size = (u_int) uio->uio_resid;
449 va = (caddr_t) rpipe->pipe_map.kva +
451 error = uiomove(va, size, uio);
455 rpipe->pipe_map.pos += size;
456 rpipe->pipe_map.cnt -= size;
457 if (rpipe->pipe_map.cnt == 0) {
458 rpipe->pipe_state &= ~PIPE_DIRECTW;
464 * detect EOF condition
465 * read returns 0 on EOF, no need to set error
467 if (rpipe->pipe_state & PIPE_EOF)
471 * If the "write-side" has been blocked, wake it up now.
473 if (rpipe->pipe_state & PIPE_WANTW) {
474 rpipe->pipe_state &= ~PIPE_WANTW;
479 * Break if some data was read.
485 * Unlock the pipe buffer for our remaining processing. We
486 * will either break out with an error or we will sleep and
492 * Handle non-blocking mode operation or
493 * wait for more data.
495 if (fp->f_flag & FNONBLOCK) {
498 rpipe->pipe_state |= PIPE_WANTR;
499 if ((error = tsleep(rpipe, PRIBIO | PCATCH,
501 error = pipelock(rpipe, 1);
510 vfs_timestamp(&rpipe->pipe_atime);
515 * PIPE_WANT processing only makes sense if pipe_busy is 0.
517 if ((rpipe->pipe_busy == 0) && (rpipe->pipe_state & PIPE_WANT)) {
518 rpipe->pipe_state &= ~(PIPE_WANT|PIPE_WANTW);
520 } else if (rpipe->pipe_buffer.cnt < MINPIPESIZE) {
522 * Handle write blocking hysteresis.
524 if (rpipe->pipe_state & PIPE_WANTW) {
525 rpipe->pipe_state &= ~PIPE_WANTW;
530 if ((rpipe->pipe_buffer.size - rpipe->pipe_buffer.cnt) >= PIPE_BUF)
531 pipeselwakeup(rpipe);
536 #ifndef PIPE_NODIRECT
538 * Map the sending processes' buffer into kernel space and wire it.
539 * This is similar to a physical write operation.
542 pipe_build_write_buffer(wpipe, uio)
548 vm_offset_t addr, endaddr, paddr;
550 size = (u_int) uio->uio_iov->iov_len;
551 if (size > wpipe->pipe_buffer.size)
552 size = wpipe->pipe_buffer.size;
554 endaddr = round_page((vm_offset_t)uio->uio_iov->iov_base + size);
555 addr = trunc_page((vm_offset_t)uio->uio_iov->iov_base);
556 for (i = 0; addr < endaddr; addr += PAGE_SIZE, i++) {
559 if (vm_fault_quick((caddr_t)addr, VM_PROT_READ) < 0 ||
560 (paddr = pmap_kextract(addr)) == 0) {
563 for (j = 0; j < i; j++)
564 vm_page_unwire(wpipe->pipe_map.ms[j], 1);
568 m = PHYS_TO_VM_PAGE(paddr);
570 wpipe->pipe_map.ms[i] = m;
574 * set up the control block
576 wpipe->pipe_map.npages = i;
577 wpipe->pipe_map.pos =
578 ((vm_offset_t) uio->uio_iov->iov_base) & PAGE_MASK;
579 wpipe->pipe_map.cnt = size;
584 if (wpipe->pipe_map.kva == 0) {
586 * We need to allocate space for an extra page because the
587 * address range might (will) span pages at times.
589 wpipe->pipe_map.kva = kmem_alloc_pageable(kernel_map,
590 wpipe->pipe_buffer.size + PAGE_SIZE);
591 amountpipekva += wpipe->pipe_buffer.size + PAGE_SIZE;
593 pmap_qenter(wpipe->pipe_map.kva, wpipe->pipe_map.ms,
594 wpipe->pipe_map.npages);
597 * and update the uio data
600 uio->uio_iov->iov_len -= size;
601 uio->uio_iov->iov_base += size;
602 if (uio->uio_iov->iov_len == 0)
604 uio->uio_resid -= size;
605 uio->uio_offset += size;
610 * unmap and unwire the process buffer
613 pipe_destroy_write_buffer(wpipe)
618 if (wpipe->pipe_map.kva) {
619 pmap_qremove(wpipe->pipe_map.kva, wpipe->pipe_map.npages);
621 if (amountpipekva > MAXPIPEKVA) {
622 vm_offset_t kva = wpipe->pipe_map.kva;
623 wpipe->pipe_map.kva = 0;
624 kmem_free(kernel_map, kva,
625 wpipe->pipe_buffer.size + PAGE_SIZE);
626 amountpipekva -= wpipe->pipe_buffer.size + PAGE_SIZE;
629 for (i = 0; i < wpipe->pipe_map.npages; i++)
630 vm_page_unwire(wpipe->pipe_map.ms[i], 1);
631 wpipe->pipe_map.npages = 0;
635 * In the case of a signal, the writing process might go away. This
636 * code copies the data into the circular buffer so that the source
637 * pages can be freed without loss of data.
640 pipe_clone_write_buffer(wpipe)
646 size = wpipe->pipe_map.cnt;
647 pos = wpipe->pipe_map.pos;
648 bcopy((caddr_t) wpipe->pipe_map.kva + pos,
649 (caddr_t) wpipe->pipe_buffer.buffer, size);
651 wpipe->pipe_buffer.in = size;
652 wpipe->pipe_buffer.out = 0;
653 wpipe->pipe_buffer.cnt = size;
654 wpipe->pipe_state &= ~PIPE_DIRECTW;
656 pipe_destroy_write_buffer(wpipe);
660 * This implements the pipe buffer write mechanism. Note that only
661 * a direct write OR a normal pipe write can be pending at any given time.
662 * If there are any characters in the pipe buffer, the direct write will
663 * be deferred until the receiving process grabs all of the bytes from
664 * the pipe buffer. Then the direct mapping write is set-up.
667 pipe_direct_write(wpipe, uio)
674 while (wpipe->pipe_state & PIPE_DIRECTW) {
675 if (wpipe->pipe_state & PIPE_WANTR) {
676 wpipe->pipe_state &= ~PIPE_WANTR;
679 wpipe->pipe_state |= PIPE_WANTW;
680 error = tsleep(wpipe, PRIBIO | PCATCH, "pipdww", 0);
683 if (wpipe->pipe_state & PIPE_EOF) {
688 wpipe->pipe_map.cnt = 0; /* transfer not ready yet */
689 if (wpipe->pipe_buffer.cnt > 0) {
690 if (wpipe->pipe_state & PIPE_WANTR) {
691 wpipe->pipe_state &= ~PIPE_WANTR;
695 wpipe->pipe_state |= PIPE_WANTW;
696 error = tsleep(wpipe, PRIBIO | PCATCH, "pipdwc", 0);
699 if (wpipe->pipe_state & PIPE_EOF) {
706 wpipe->pipe_state |= PIPE_DIRECTW;
708 error = pipe_build_write_buffer(wpipe, uio);
710 wpipe->pipe_state &= ~PIPE_DIRECTW;
715 while (!error && (wpipe->pipe_state & PIPE_DIRECTW)) {
716 if (wpipe->pipe_state & PIPE_EOF) {
718 pipe_destroy_write_buffer(wpipe);
720 pipeselwakeup(wpipe);
724 if (wpipe->pipe_state & PIPE_WANTR) {
725 wpipe->pipe_state &= ~PIPE_WANTR;
728 pipeselwakeup(wpipe);
729 error = tsleep(wpipe, PRIBIO | PCATCH, "pipdwt", 0);
733 if (wpipe->pipe_state & PIPE_DIRECTW) {
735 * this bit of trickery substitutes a kernel buffer for
736 * the process that might be going away.
738 pipe_clone_write_buffer(wpipe);
740 pipe_destroy_write_buffer(wpipe);
752 pipe_write(fp, uio, cred, flags, p)
761 struct pipe *wpipe, *rpipe;
763 rpipe = (struct pipe *) fp->f_data;
764 wpipe = rpipe->pipe_peer;
767 * detect loss of pipe read side, issue SIGPIPE if lost.
769 if ((wpipe == NULL) || (wpipe->pipe_state & PIPE_EOF)) {
775 * If it is advantageous to resize the pipe buffer, do
778 if ((uio->uio_resid > PIPE_SIZE) &&
779 (nbigpipe < LIMITBIGPIPES) &&
780 (wpipe->pipe_state & PIPE_DIRECTW) == 0 &&
781 (wpipe->pipe_buffer.size <= PIPE_SIZE) &&
782 (wpipe->pipe_buffer.cnt == 0)) {
784 if ((error = pipelock(wpipe,1)) == 0) {
785 if (pipespace(wpipe, BIG_PIPE_SIZE) == 0)
792 * If an early error occured unbusy and return, waking up any pending
797 if ((wpipe->pipe_busy == 0) &&
798 (wpipe->pipe_state & PIPE_WANT)) {
799 wpipe->pipe_state &= ~(PIPE_WANT | PIPE_WANTR);
805 KASSERT(wpipe->pipe_buffer.buffer != NULL, ("pipe buffer gone"));
807 orig_resid = uio->uio_resid;
809 while (uio->uio_resid) {
812 #ifndef PIPE_NODIRECT
814 * If the transfer is large, we can gain performance if
815 * we do process-to-process copies directly.
816 * If the write is non-blocking, we don't use the
817 * direct write mechanism.
819 * The direct write mechanism will detect the reader going
822 if ((uio->uio_iov->iov_len >= PIPE_MINDIRECT) &&
823 (fp->f_flag & FNONBLOCK) == 0 &&
824 (wpipe->pipe_map.kva || (amountpipekva < LIMITPIPEKVA)) &&
825 (uio->uio_iov->iov_len >= PIPE_MINDIRECT)) {
826 error = pipe_direct_write( wpipe, uio);
834 * Pipe buffered writes cannot be coincidental with
835 * direct writes. We wait until the currently executing
836 * direct write is completed before we start filling the
837 * pipe buffer. We break out if a signal occurs or the
841 while (wpipe->pipe_state & PIPE_DIRECTW) {
842 if (wpipe->pipe_state & PIPE_WANTR) {
843 wpipe->pipe_state &= ~PIPE_WANTR;
846 error = tsleep(wpipe, PRIBIO | PCATCH, "pipbww", 0);
847 if (wpipe->pipe_state & PIPE_EOF)
852 if (wpipe->pipe_state & PIPE_EOF) {
857 space = wpipe->pipe_buffer.size - wpipe->pipe_buffer.cnt;
859 /* Writes of size <= PIPE_BUF must be atomic. */
860 if ((space < uio->uio_resid) && (orig_resid <= PIPE_BUF))
863 if (space > 0 && (wpipe->pipe_buffer.cnt < PIPE_SIZE)) {
864 if ((error = pipelock(wpipe,1)) == 0) {
865 int size; /* Transfer size */
866 int segsize; /* first segment to transfer */
869 * It is possible for a direct write to
870 * slip in on us... handle it here...
872 if (wpipe->pipe_state & PIPE_DIRECTW) {
877 * If a process blocked in uiomove, our
878 * value for space might be bad.
880 * XXX will we be ok if the reader has gone
883 if (space > wpipe->pipe_buffer.size -
884 wpipe->pipe_buffer.cnt) {
890 * Transfer size is minimum of uio transfer
891 * and free space in pipe buffer.
893 if (space > uio->uio_resid)
894 size = uio->uio_resid;
898 * First segment to transfer is minimum of
899 * transfer size and contiguous space in
900 * pipe buffer. If first segment to transfer
901 * is less than the transfer size, we've got
902 * a wraparound in the buffer.
904 segsize = wpipe->pipe_buffer.size -
905 wpipe->pipe_buffer.in;
909 /* Transfer first segment */
911 error = uiomove(&wpipe->pipe_buffer.buffer[wpipe->pipe_buffer.in],
914 if (error == 0 && segsize < size) {
916 * Transfer remaining part now, to
917 * support atomic writes. Wraparound
920 if (wpipe->pipe_buffer.in + segsize !=
921 wpipe->pipe_buffer.size)
922 panic("Expected pipe buffer wraparound disappeared");
924 error = uiomove(&wpipe->pipe_buffer.buffer[0],
925 size - segsize, uio);
928 wpipe->pipe_buffer.in += size;
929 if (wpipe->pipe_buffer.in >=
930 wpipe->pipe_buffer.size) {
931 if (wpipe->pipe_buffer.in != size - segsize + wpipe->pipe_buffer.size)
932 panic("Expected wraparound bad");
933 wpipe->pipe_buffer.in = size - segsize;
936 wpipe->pipe_buffer.cnt += size;
937 if (wpipe->pipe_buffer.cnt > wpipe->pipe_buffer.size)
938 panic("Pipe buffer overflow");
948 * If the "read-side" has been blocked, wake it up now.
950 if (wpipe->pipe_state & PIPE_WANTR) {
951 wpipe->pipe_state &= ~PIPE_WANTR;
956 * don't block on non-blocking I/O
958 if (fp->f_flag & FNONBLOCK) {
964 * We have no more space and have something to offer,
965 * wake up select/poll.
967 pipeselwakeup(wpipe);
969 wpipe->pipe_state |= PIPE_WANTW;
970 error = tsleep(wpipe, PRIBIO | PCATCH, "pipewr", 0);
974 * If read side wants to go away, we just issue a signal
977 if (wpipe->pipe_state & PIPE_EOF) {
986 if ((wpipe->pipe_busy == 0) && (wpipe->pipe_state & PIPE_WANT)) {
987 wpipe->pipe_state &= ~(PIPE_WANT | PIPE_WANTR);
989 } else if (wpipe->pipe_buffer.cnt > 0) {
991 * If we have put any characters in the buffer, we wake up
994 if (wpipe->pipe_state & PIPE_WANTR) {
995 wpipe->pipe_state &= ~PIPE_WANTR;
1001 * Don't return EPIPE if I/O was successful
1003 if ((wpipe->pipe_buffer.cnt == 0) &&
1004 (uio->uio_resid == 0) &&
1010 vfs_timestamp(&wpipe->pipe_mtime);
1013 * We have something to offer,
1014 * wake up select/poll.
1016 if (wpipe->pipe_buffer.cnt)
1017 pipeselwakeup(wpipe);
1023 * we implement a very minimal set of ioctls for compatibility with sockets.
1026 pipe_ioctl(fp, cmd, data, p)
1032 struct pipe *mpipe = (struct pipe *)fp->f_data;
1041 mpipe->pipe_state |= PIPE_ASYNC;
1043 mpipe->pipe_state &= ~PIPE_ASYNC;
1048 if (mpipe->pipe_state & PIPE_DIRECTW)
1049 *(int *)data = mpipe->pipe_map.cnt;
1051 *(int *)data = mpipe->pipe_buffer.cnt;
1055 return (fsetown(*(int *)data, &mpipe->pipe_sigio));
1058 *(int *)data = fgetown(mpipe->pipe_sigio);
1061 /* This is deprecated, FIOSETOWN should be used instead. */
1063 return (fsetown(-(*(int *)data), &mpipe->pipe_sigio));
1065 /* This is deprecated, FIOGETOWN should be used instead. */
1067 *(int *)data = -fgetown(mpipe->pipe_sigio);
1075 pipe_poll(fp, events, cred, p)
1081 struct pipe *rpipe = (struct pipe *)fp->f_data;
1085 wpipe = rpipe->pipe_peer;
1086 if (events & (POLLIN | POLLRDNORM))
1087 if ((rpipe->pipe_state & PIPE_DIRECTW) ||
1088 (rpipe->pipe_buffer.cnt > 0) ||
1089 (rpipe->pipe_state & PIPE_EOF))
1090 revents |= events & (POLLIN | POLLRDNORM);
1092 if (events & (POLLOUT | POLLWRNORM))
1093 if (wpipe == NULL || (wpipe->pipe_state & PIPE_EOF) ||
1094 (((wpipe->pipe_state & PIPE_DIRECTW) == 0) &&
1095 (wpipe->pipe_buffer.size - wpipe->pipe_buffer.cnt) >= PIPE_BUF))
1096 revents |= events & (POLLOUT | POLLWRNORM);
1098 if ((rpipe->pipe_state & PIPE_EOF) ||
1100 (wpipe->pipe_state & PIPE_EOF))
1104 if (events & (POLLIN | POLLRDNORM)) {
1105 selrecord(p->p_thread, &rpipe->pipe_sel);
1106 rpipe->pipe_state |= PIPE_SEL;
1109 if (events & (POLLOUT | POLLWRNORM)) {
1110 selrecord(p->p_thread, &wpipe->pipe_sel);
1111 wpipe->pipe_state |= PIPE_SEL;
1119 pipe_stat(fp, ub, p)
1124 struct pipe *pipe = (struct pipe *)fp->f_data;
1126 bzero((caddr_t)ub, sizeof(*ub));
1127 ub->st_mode = S_IFIFO;
1128 ub->st_blksize = pipe->pipe_buffer.size;
1129 ub->st_size = pipe->pipe_buffer.cnt;
1130 ub->st_blocks = (ub->st_size + ub->st_blksize - 1) / ub->st_blksize;
1131 ub->st_atimespec = pipe->pipe_atime;
1132 ub->st_mtimespec = pipe->pipe_mtime;
1133 ub->st_ctimespec = pipe->pipe_ctime;
1135 * Left as 0: st_dev, st_ino, st_nlink, st_uid, st_gid, st_rdev,
1137 * XXX (st_dev, st_ino) should be unique.
1148 struct pipe *cpipe = (struct pipe *)fp->f_data;
1150 fp->f_ops = &badfileops;
1152 funsetown(cpipe->pipe_sigio);
1158 pipe_free_kmem(cpipe)
1162 if (cpipe->pipe_buffer.buffer != NULL) {
1163 if (cpipe->pipe_buffer.size > PIPE_SIZE)
1165 amountpipekva -= cpipe->pipe_buffer.size;
1166 kmem_free(kernel_map,
1167 (vm_offset_t)cpipe->pipe_buffer.buffer,
1168 cpipe->pipe_buffer.size);
1169 cpipe->pipe_buffer.buffer = NULL;
1171 #ifndef PIPE_NODIRECT
1172 if (cpipe->pipe_map.kva != NULL) {
1173 amountpipekva -= cpipe->pipe_buffer.size + PAGE_SIZE;
1174 kmem_free(kernel_map,
1175 cpipe->pipe_map.kva,
1176 cpipe->pipe_buffer.size + PAGE_SIZE);
1177 cpipe->pipe_map.cnt = 0;
1178 cpipe->pipe_map.kva = 0;
1179 cpipe->pipe_map.pos = 0;
1180 cpipe->pipe_map.npages = 0;
1196 pipeselwakeup(cpipe);
1199 * If the other side is blocked, wake it up saying that
1200 * we want to close it down.
1202 while (cpipe->pipe_busy) {
1204 cpipe->pipe_state |= PIPE_WANT | PIPE_EOF;
1205 tsleep(cpipe, PRIBIO, "pipecl", 0);
1209 * Disconnect from peer
1211 if ((ppipe = cpipe->pipe_peer) != NULL) {
1212 pipeselwakeup(ppipe);
1214 ppipe->pipe_state |= PIPE_EOF;
1216 KNOTE(&ppipe->pipe_sel.si_note, 0);
1217 ppipe->pipe_peer = NULL;
1222 pipe_free_kmem(cpipe);
1223 zfree(pipe_zone, cpipe);
1229 pipe_kqfilter(struct file *fp, struct knote *kn)
1231 struct pipe *cpipe = (struct pipe *)kn->kn_fp->f_data;
1233 switch (kn->kn_filter) {
1235 kn->kn_fop = &pipe_rfiltops;
1238 kn->kn_fop = &pipe_wfiltops;
1239 cpipe = cpipe->pipe_peer;
1241 /* other end of pipe has been closed */
1247 kn->kn_hook = (caddr_t)cpipe;
1249 SLIST_INSERT_HEAD(&cpipe->pipe_sel.si_note, kn, kn_selnext);
1254 filt_pipedetach(struct knote *kn)
1256 struct pipe *cpipe = (struct pipe *)kn->kn_hook;
1258 SLIST_REMOVE(&cpipe->pipe_sel.si_note, kn, knote, kn_selnext);
1263 filt_piperead(struct knote *kn, long hint)
1265 struct pipe *rpipe = (struct pipe *)kn->kn_fp->f_data;
1266 struct pipe *wpipe = rpipe->pipe_peer;
1268 kn->kn_data = rpipe->pipe_buffer.cnt;
1269 if ((kn->kn_data == 0) && (rpipe->pipe_state & PIPE_DIRECTW))
1270 kn->kn_data = rpipe->pipe_map.cnt;
1272 if ((rpipe->pipe_state & PIPE_EOF) ||
1273 (wpipe == NULL) || (wpipe->pipe_state & PIPE_EOF)) {
1274 kn->kn_flags |= EV_EOF;
1277 return (kn->kn_data > 0);
1282 filt_pipewrite(struct knote *kn, long hint)
1284 struct pipe *rpipe = (struct pipe *)kn->kn_fp->f_data;
1285 struct pipe *wpipe = rpipe->pipe_peer;
1287 if ((wpipe == NULL) || (wpipe->pipe_state & PIPE_EOF)) {
1289 kn->kn_flags |= EV_EOF;
1292 kn->kn_data = wpipe->pipe_buffer.size - wpipe->pipe_buffer.cnt;
1293 if (wpipe->pipe_state & PIPE_DIRECTW)
1296 return (kn->kn_data >= PIPE_BUF);