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.4 2003/06/25 03:55:57 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>
82 #include <sys/file2.h>
85 * Use this define if you want to disable *fancy* VM things. Expect an
86 * approx 30% decrease in transfer rate. This could be useful for
89 /* #define PIPE_NODIRECT */
92 * interfaces to the outside world
94 static int pipe_read __P((struct file *fp, struct uio *uio,
95 struct ucred *cred, int flags, struct thread *td));
96 static int pipe_write __P((struct file *fp, struct uio *uio,
97 struct ucred *cred, int flags, struct thread *td));
98 static int pipe_close __P((struct file *fp, struct thread *td));
99 static int pipe_poll __P((struct file *fp, int events, struct ucred *cred,
101 static int pipe_kqfilter __P((struct file *fp, struct knote *kn));
102 static int pipe_stat __P((struct file *fp, struct stat *sb, struct thread *td));
103 static int pipe_ioctl __P((struct file *fp, u_long cmd, caddr_t data, struct thread *td));
105 static struct fileops pipeops = {
106 pipe_read, pipe_write, pipe_ioctl, pipe_poll, pipe_kqfilter,
107 pipe_stat, pipe_close
110 static void filt_pipedetach(struct knote *kn);
111 static int filt_piperead(struct knote *kn, long hint);
112 static int filt_pipewrite(struct knote *kn, long hint);
114 static struct filterops pipe_rfiltops =
115 { 1, NULL, filt_pipedetach, filt_piperead };
116 static struct filterops pipe_wfiltops =
117 { 1, NULL, filt_pipedetach, filt_pipewrite };
121 * Default pipe buffer size(s), this can be kind-of large now because pipe
122 * space is pageable. The pipe code will try to maintain locality of
123 * reference for performance reasons, so small amounts of outstanding I/O
124 * will not wipe the cache.
126 #define MINPIPESIZE (PIPE_SIZE/3)
127 #define MAXPIPESIZE (2*PIPE_SIZE/3)
130 * Maximum amount of kva for pipes -- this is kind-of a soft limit, but
131 * is there so that on large systems, we don't exhaust it.
133 #define MAXPIPEKVA (8*1024*1024)
136 * Limit for direct transfers, we cannot, of course limit
137 * the amount of kva for pipes in general though.
139 #define LIMITPIPEKVA (16*1024*1024)
142 * Limit the number of "big" pipes
144 #define LIMITBIGPIPES 32
147 static int amountpipekva;
149 static void pipeclose __P((struct pipe *cpipe));
150 static void pipe_free_kmem __P((struct pipe *cpipe));
151 static int pipe_create __P((struct pipe **cpipep));
152 static __inline int pipelock __P((struct pipe *cpipe, int catch));
153 static __inline void pipeunlock __P((struct pipe *cpipe));
154 static __inline void pipeselwakeup __P((struct pipe *cpipe));
155 #ifndef PIPE_NODIRECT
156 static int pipe_build_write_buffer __P((struct pipe *wpipe, struct uio *uio));
157 static void pipe_destroy_write_buffer __P((struct pipe *wpipe));
158 static int pipe_direct_write __P((struct pipe *wpipe, struct uio *uio));
159 static void pipe_clone_write_buffer __P((struct pipe *wpipe));
161 static int pipespace __P((struct pipe *cpipe, int size));
163 static vm_zone_t pipe_zone;
166 * The pipe system call for the DTYPE_PIPE type of pipes
168 * pipe_ARgs(int dummy)
173 pipe(struct pipe_args *uap)
175 struct thread *td = curthread;
176 struct proc *p = td->td_proc;
177 struct filedesc *fdp;
178 struct file *rf, *wf;
179 struct pipe *rpipe, *wpipe;
185 if (pipe_zone == NULL)
186 pipe_zone = zinit("PIPE", sizeof(struct pipe), 0, 0, 4);
188 rpipe = wpipe = NULL;
189 if (pipe_create(&rpipe) || pipe_create(&wpipe)) {
195 rpipe->pipe_state |= PIPE_DIRECTOK;
196 wpipe->pipe_state |= PIPE_DIRECTOK;
198 error = falloc(p, &rf, &fd);
208 * Warning: once we've gotten past allocation of the fd for the
209 * read-side, we can only drop the read side via fdrop() in order
210 * to avoid races against processes which manage to dup() the read
211 * side while we are blocked trying to allocate the write side.
213 rf->f_flag = FREAD | FWRITE;
214 rf->f_type = DTYPE_PIPE;
215 rf->f_data = (caddr_t)rpipe;
216 rf->f_ops = &pipeops;
217 error = falloc(p, &wf, &fd);
219 if (fdp->fd_ofiles[p->p_retval[0]] == rf) {
220 fdp->fd_ofiles[p->p_retval[0]] = NULL;
224 /* rpipe has been closed by fdrop(). */
228 wf->f_flag = FREAD | FWRITE;
229 wf->f_type = DTYPE_PIPE;
230 wf->f_data = (caddr_t)wpipe;
231 wf->f_ops = &pipeops;
234 rpipe->pipe_peer = wpipe;
235 wpipe->pipe_peer = rpipe;
242 * Allocate kva for pipe circular buffer, the space is pageable
243 * This routine will 'realloc' the size of a pipe safely, if it fails
244 * it will retain the old buffer.
245 * If it fails it will return ENOMEM.
248 pipespace(cpipe, size)
252 struct vm_object *object;
256 npages = round_page(size)/PAGE_SIZE;
258 * Create an object, I don't like the idea of paging to/from
260 * XXX -- minor change needed here for NetBSD/OpenBSD VM systems.
262 object = vm_object_allocate(OBJT_DEFAULT, npages);
263 buffer = (caddr_t) vm_map_min(kernel_map);
266 * Insert the object into the kernel map, and allocate kva for it.
267 * The map entry is, by default, pageable.
268 * XXX -- minor change needed here for NetBSD/OpenBSD VM systems.
270 error = vm_map_find(kernel_map, object, 0,
271 (vm_offset_t *) &buffer, size, 1,
272 VM_PROT_ALL, VM_PROT_ALL, 0);
274 if (error != KERN_SUCCESS) {
275 vm_object_deallocate(object);
279 /* free old resources if we're resizing */
280 pipe_free_kmem(cpipe);
281 cpipe->pipe_buffer.object = object;
282 cpipe->pipe_buffer.buffer = buffer;
283 cpipe->pipe_buffer.size = size;
284 cpipe->pipe_buffer.in = 0;
285 cpipe->pipe_buffer.out = 0;
286 cpipe->pipe_buffer.cnt = 0;
287 amountpipekva += cpipe->pipe_buffer.size;
292 * initialize and allocate VM and memory for pipe
296 struct pipe **cpipep;
301 *cpipep = zalloc(pipe_zone);
307 /* so pipespace()->pipe_free_kmem() doesn't follow junk pointer */
308 cpipe->pipe_buffer.object = NULL;
309 #ifndef PIPE_NODIRECT
310 cpipe->pipe_map.kva = NULL;
313 * protect so pipeclose() doesn't follow a junk pointer
314 * if pipespace() fails.
316 bzero(&cpipe->pipe_sel, sizeof(cpipe->pipe_sel));
317 cpipe->pipe_state = 0;
318 cpipe->pipe_peer = NULL;
319 cpipe->pipe_busy = 0;
321 #ifndef PIPE_NODIRECT
323 * pipe data structure initializations to support direct pipe I/O
325 cpipe->pipe_map.cnt = 0;
326 cpipe->pipe_map.kva = 0;
327 cpipe->pipe_map.pos = 0;
328 cpipe->pipe_map.npages = 0;
329 /* cpipe->pipe_map.ms[] = invalid */
332 error = pipespace(cpipe, PIPE_SIZE);
336 vfs_timestamp(&cpipe->pipe_ctime);
337 cpipe->pipe_atime = cpipe->pipe_ctime;
338 cpipe->pipe_mtime = cpipe->pipe_ctime;
345 * lock a pipe for I/O, blocking other access
348 pipelock(cpipe, catch)
354 while (cpipe->pipe_state & PIPE_LOCK) {
355 cpipe->pipe_state |= PIPE_LWANT;
356 error = tsleep(cpipe, catch ? (PRIBIO | PCATCH) : PRIBIO,
361 cpipe->pipe_state |= PIPE_LOCK;
366 * unlock a pipe I/O lock
373 cpipe->pipe_state &= ~PIPE_LOCK;
374 if (cpipe->pipe_state & PIPE_LWANT) {
375 cpipe->pipe_state &= ~PIPE_LWANT;
385 if (cpipe->pipe_state & PIPE_SEL) {
386 cpipe->pipe_state &= ~PIPE_SEL;
387 selwakeup(&cpipe->pipe_sel);
389 if ((cpipe->pipe_state & PIPE_ASYNC) && cpipe->pipe_sigio)
390 pgsigio(cpipe->pipe_sigio, SIGIO, 0);
391 KNOTE(&cpipe->pipe_sel.si_note, 0);
396 pipe_read(struct file *fp, struct uio *uio, struct ucred *cred,
397 int flags, struct thread *td)
399 struct pipe *rpipe = (struct pipe *) fp->f_data;
405 error = pipelock(rpipe, 1);
409 while (uio->uio_resid) {
411 * normal pipe buffer receive
413 if (rpipe->pipe_buffer.cnt > 0) {
414 size = rpipe->pipe_buffer.size - rpipe->pipe_buffer.out;
415 if (size > rpipe->pipe_buffer.cnt)
416 size = rpipe->pipe_buffer.cnt;
417 if (size > (u_int) uio->uio_resid)
418 size = (u_int) uio->uio_resid;
420 error = uiomove(&rpipe->pipe_buffer.buffer[rpipe->pipe_buffer.out],
425 rpipe->pipe_buffer.out += size;
426 if (rpipe->pipe_buffer.out >= rpipe->pipe_buffer.size)
427 rpipe->pipe_buffer.out = 0;
429 rpipe->pipe_buffer.cnt -= size;
432 * If there is no more to read in the pipe, reset
433 * its pointers to the beginning. This improves
436 if (rpipe->pipe_buffer.cnt == 0) {
437 rpipe->pipe_buffer.in = 0;
438 rpipe->pipe_buffer.out = 0;
441 #ifndef PIPE_NODIRECT
443 * Direct copy, bypassing a kernel buffer.
445 } else if ((size = rpipe->pipe_map.cnt) &&
446 (rpipe->pipe_state & PIPE_DIRECTW)) {
448 if (size > (u_int) uio->uio_resid)
449 size = (u_int) uio->uio_resid;
451 va = (caddr_t) rpipe->pipe_map.kva +
453 error = uiomove(va, size, uio);
457 rpipe->pipe_map.pos += size;
458 rpipe->pipe_map.cnt -= size;
459 if (rpipe->pipe_map.cnt == 0) {
460 rpipe->pipe_state &= ~PIPE_DIRECTW;
466 * detect EOF condition
467 * read returns 0 on EOF, no need to set error
469 if (rpipe->pipe_state & PIPE_EOF)
473 * If the "write-side" has been blocked, wake it up now.
475 if (rpipe->pipe_state & PIPE_WANTW) {
476 rpipe->pipe_state &= ~PIPE_WANTW;
481 * Break if some data was read.
487 * Unlock the pipe buffer for our remaining processing. We
488 * will either break out with an error or we will sleep and
494 * Handle non-blocking mode operation or
495 * wait for more data.
497 if (fp->f_flag & FNONBLOCK) {
500 rpipe->pipe_state |= PIPE_WANTR;
501 if ((error = tsleep(rpipe, PRIBIO | PCATCH,
503 error = pipelock(rpipe, 1);
512 vfs_timestamp(&rpipe->pipe_atime);
517 * PIPE_WANT processing only makes sense if pipe_busy is 0.
519 if ((rpipe->pipe_busy == 0) && (rpipe->pipe_state & PIPE_WANT)) {
520 rpipe->pipe_state &= ~(PIPE_WANT|PIPE_WANTW);
522 } else if (rpipe->pipe_buffer.cnt < MINPIPESIZE) {
524 * Handle write blocking hysteresis.
526 if (rpipe->pipe_state & PIPE_WANTW) {
527 rpipe->pipe_state &= ~PIPE_WANTW;
532 if ((rpipe->pipe_buffer.size - rpipe->pipe_buffer.cnt) >= PIPE_BUF)
533 pipeselwakeup(rpipe);
538 #ifndef PIPE_NODIRECT
540 * Map the sending processes' buffer into kernel space and wire it.
541 * This is similar to a physical write operation.
544 pipe_build_write_buffer(wpipe, uio)
550 vm_offset_t addr, endaddr, paddr;
552 size = (u_int) uio->uio_iov->iov_len;
553 if (size > wpipe->pipe_buffer.size)
554 size = wpipe->pipe_buffer.size;
556 endaddr = round_page((vm_offset_t)uio->uio_iov->iov_base + size);
557 addr = trunc_page((vm_offset_t)uio->uio_iov->iov_base);
558 for (i = 0; addr < endaddr; addr += PAGE_SIZE, i++) {
561 if (vm_fault_quick((caddr_t)addr, VM_PROT_READ) < 0 ||
562 (paddr = pmap_kextract(addr)) == 0) {
565 for (j = 0; j < i; j++)
566 vm_page_unwire(wpipe->pipe_map.ms[j], 1);
570 m = PHYS_TO_VM_PAGE(paddr);
572 wpipe->pipe_map.ms[i] = m;
576 * set up the control block
578 wpipe->pipe_map.npages = i;
579 wpipe->pipe_map.pos =
580 ((vm_offset_t) uio->uio_iov->iov_base) & PAGE_MASK;
581 wpipe->pipe_map.cnt = size;
586 if (wpipe->pipe_map.kva == 0) {
588 * We need to allocate space for an extra page because the
589 * address range might (will) span pages at times.
591 wpipe->pipe_map.kva = kmem_alloc_pageable(kernel_map,
592 wpipe->pipe_buffer.size + PAGE_SIZE);
593 amountpipekva += wpipe->pipe_buffer.size + PAGE_SIZE;
595 pmap_qenter(wpipe->pipe_map.kva, wpipe->pipe_map.ms,
596 wpipe->pipe_map.npages);
599 * and update the uio data
602 uio->uio_iov->iov_len -= size;
603 uio->uio_iov->iov_base += size;
604 if (uio->uio_iov->iov_len == 0)
606 uio->uio_resid -= size;
607 uio->uio_offset += size;
612 * unmap and unwire the process buffer
615 pipe_destroy_write_buffer(wpipe)
620 if (wpipe->pipe_map.kva) {
621 pmap_qremove(wpipe->pipe_map.kva, wpipe->pipe_map.npages);
623 if (amountpipekva > MAXPIPEKVA) {
624 vm_offset_t kva = wpipe->pipe_map.kva;
625 wpipe->pipe_map.kva = 0;
626 kmem_free(kernel_map, kva,
627 wpipe->pipe_buffer.size + PAGE_SIZE);
628 amountpipekva -= wpipe->pipe_buffer.size + PAGE_SIZE;
631 for (i = 0; i < wpipe->pipe_map.npages; i++)
632 vm_page_unwire(wpipe->pipe_map.ms[i], 1);
633 wpipe->pipe_map.npages = 0;
637 * In the case of a signal, the writing process might go away. This
638 * code copies the data into the circular buffer so that the source
639 * pages can be freed without loss of data.
642 pipe_clone_write_buffer(wpipe)
648 size = wpipe->pipe_map.cnt;
649 pos = wpipe->pipe_map.pos;
650 bcopy((caddr_t) wpipe->pipe_map.kva + pos,
651 (caddr_t) wpipe->pipe_buffer.buffer, size);
653 wpipe->pipe_buffer.in = size;
654 wpipe->pipe_buffer.out = 0;
655 wpipe->pipe_buffer.cnt = size;
656 wpipe->pipe_state &= ~PIPE_DIRECTW;
658 pipe_destroy_write_buffer(wpipe);
662 * This implements the pipe buffer write mechanism. Note that only
663 * a direct write OR a normal pipe write can be pending at any given time.
664 * If there are any characters in the pipe buffer, the direct write will
665 * be deferred until the receiving process grabs all of the bytes from
666 * the pipe buffer. Then the direct mapping write is set-up.
669 pipe_direct_write(wpipe, uio)
676 while (wpipe->pipe_state & PIPE_DIRECTW) {
677 if (wpipe->pipe_state & PIPE_WANTR) {
678 wpipe->pipe_state &= ~PIPE_WANTR;
681 wpipe->pipe_state |= PIPE_WANTW;
682 error = tsleep(wpipe, PRIBIO | PCATCH, "pipdww", 0);
685 if (wpipe->pipe_state & PIPE_EOF) {
690 wpipe->pipe_map.cnt = 0; /* transfer not ready yet */
691 if (wpipe->pipe_buffer.cnt > 0) {
692 if (wpipe->pipe_state & PIPE_WANTR) {
693 wpipe->pipe_state &= ~PIPE_WANTR;
697 wpipe->pipe_state |= PIPE_WANTW;
698 error = tsleep(wpipe, PRIBIO | PCATCH, "pipdwc", 0);
701 if (wpipe->pipe_state & PIPE_EOF) {
708 wpipe->pipe_state |= PIPE_DIRECTW;
710 error = pipe_build_write_buffer(wpipe, uio);
712 wpipe->pipe_state &= ~PIPE_DIRECTW;
717 while (!error && (wpipe->pipe_state & PIPE_DIRECTW)) {
718 if (wpipe->pipe_state & PIPE_EOF) {
720 pipe_destroy_write_buffer(wpipe);
722 pipeselwakeup(wpipe);
726 if (wpipe->pipe_state & PIPE_WANTR) {
727 wpipe->pipe_state &= ~PIPE_WANTR;
730 pipeselwakeup(wpipe);
731 error = tsleep(wpipe, PRIBIO | PCATCH, "pipdwt", 0);
735 if (wpipe->pipe_state & PIPE_DIRECTW) {
737 * this bit of trickery substitutes a kernel buffer for
738 * the process that might be going away.
740 pipe_clone_write_buffer(wpipe);
742 pipe_destroy_write_buffer(wpipe);
754 pipe_write(struct file *fp, struct uio *uio, struct ucred *cred,
755 int flags, struct thread *td)
759 struct pipe *wpipe, *rpipe;
761 rpipe = (struct pipe *) fp->f_data;
762 wpipe = rpipe->pipe_peer;
765 * detect loss of pipe read side, issue SIGPIPE if lost.
767 if ((wpipe == NULL) || (wpipe->pipe_state & PIPE_EOF)) {
773 * If it is advantageous to resize the pipe buffer, do
776 if ((uio->uio_resid > PIPE_SIZE) &&
777 (nbigpipe < LIMITBIGPIPES) &&
778 (wpipe->pipe_state & PIPE_DIRECTW) == 0 &&
779 (wpipe->pipe_buffer.size <= PIPE_SIZE) &&
780 (wpipe->pipe_buffer.cnt == 0)) {
782 if ((error = pipelock(wpipe,1)) == 0) {
783 if (pipespace(wpipe, BIG_PIPE_SIZE) == 0)
790 * If an early error occured unbusy and return, waking up any pending
795 if ((wpipe->pipe_busy == 0) &&
796 (wpipe->pipe_state & PIPE_WANT)) {
797 wpipe->pipe_state &= ~(PIPE_WANT | PIPE_WANTR);
803 KASSERT(wpipe->pipe_buffer.buffer != NULL, ("pipe buffer gone"));
805 orig_resid = uio->uio_resid;
807 while (uio->uio_resid) {
810 #ifndef PIPE_NODIRECT
812 * If the transfer is large, we can gain performance if
813 * we do process-to-process copies directly.
814 * If the write is non-blocking, we don't use the
815 * direct write mechanism.
817 * The direct write mechanism will detect the reader going
820 if ((uio->uio_iov->iov_len >= PIPE_MINDIRECT) &&
821 (fp->f_flag & FNONBLOCK) == 0 &&
822 (wpipe->pipe_map.kva || (amountpipekva < LIMITPIPEKVA)) &&
823 (uio->uio_iov->iov_len >= PIPE_MINDIRECT)) {
824 error = pipe_direct_write( wpipe, uio);
832 * Pipe buffered writes cannot be coincidental with
833 * direct writes. We wait until the currently executing
834 * direct write is completed before we start filling the
835 * pipe buffer. We break out if a signal occurs or the
839 while (wpipe->pipe_state & PIPE_DIRECTW) {
840 if (wpipe->pipe_state & PIPE_WANTR) {
841 wpipe->pipe_state &= ~PIPE_WANTR;
844 error = tsleep(wpipe, PRIBIO | PCATCH, "pipbww", 0);
845 if (wpipe->pipe_state & PIPE_EOF)
850 if (wpipe->pipe_state & PIPE_EOF) {
855 space = wpipe->pipe_buffer.size - wpipe->pipe_buffer.cnt;
857 /* Writes of size <= PIPE_BUF must be atomic. */
858 if ((space < uio->uio_resid) && (orig_resid <= PIPE_BUF))
861 if (space > 0 && (wpipe->pipe_buffer.cnt < PIPE_SIZE)) {
862 if ((error = pipelock(wpipe,1)) == 0) {
863 int size; /* Transfer size */
864 int segsize; /* first segment to transfer */
867 * It is possible for a direct write to
868 * slip in on us... handle it here...
870 if (wpipe->pipe_state & PIPE_DIRECTW) {
875 * If a process blocked in uiomove, our
876 * value for space might be bad.
878 * XXX will we be ok if the reader has gone
881 if (space > wpipe->pipe_buffer.size -
882 wpipe->pipe_buffer.cnt) {
888 * Transfer size is minimum of uio transfer
889 * and free space in pipe buffer.
891 if (space > uio->uio_resid)
892 size = uio->uio_resid;
896 * First segment to transfer is minimum of
897 * transfer size and contiguous space in
898 * pipe buffer. If first segment to transfer
899 * is less than the transfer size, we've got
900 * a wraparound in the buffer.
902 segsize = wpipe->pipe_buffer.size -
903 wpipe->pipe_buffer.in;
907 /* Transfer first segment */
909 error = uiomove(&wpipe->pipe_buffer.buffer[wpipe->pipe_buffer.in],
912 if (error == 0 && segsize < size) {
914 * Transfer remaining part now, to
915 * support atomic writes. Wraparound
918 if (wpipe->pipe_buffer.in + segsize !=
919 wpipe->pipe_buffer.size)
920 panic("Expected pipe buffer wraparound disappeared");
922 error = uiomove(&wpipe->pipe_buffer.buffer[0],
923 size - segsize, uio);
926 wpipe->pipe_buffer.in += size;
927 if (wpipe->pipe_buffer.in >=
928 wpipe->pipe_buffer.size) {
929 if (wpipe->pipe_buffer.in != size - segsize + wpipe->pipe_buffer.size)
930 panic("Expected wraparound bad");
931 wpipe->pipe_buffer.in = size - segsize;
934 wpipe->pipe_buffer.cnt += size;
935 if (wpipe->pipe_buffer.cnt > wpipe->pipe_buffer.size)
936 panic("Pipe buffer overflow");
946 * If the "read-side" has been blocked, wake it up now.
948 if (wpipe->pipe_state & PIPE_WANTR) {
949 wpipe->pipe_state &= ~PIPE_WANTR;
954 * don't block on non-blocking I/O
956 if (fp->f_flag & FNONBLOCK) {
962 * We have no more space and have something to offer,
963 * wake up select/poll.
965 pipeselwakeup(wpipe);
967 wpipe->pipe_state |= PIPE_WANTW;
968 error = tsleep(wpipe, PRIBIO | PCATCH, "pipewr", 0);
972 * If read side wants to go away, we just issue a signal
975 if (wpipe->pipe_state & PIPE_EOF) {
984 if ((wpipe->pipe_busy == 0) && (wpipe->pipe_state & PIPE_WANT)) {
985 wpipe->pipe_state &= ~(PIPE_WANT | PIPE_WANTR);
987 } else if (wpipe->pipe_buffer.cnt > 0) {
989 * If we have put any characters in the buffer, we wake up
992 if (wpipe->pipe_state & PIPE_WANTR) {
993 wpipe->pipe_state &= ~PIPE_WANTR;
999 * Don't return EPIPE if I/O was successful
1001 if ((wpipe->pipe_buffer.cnt == 0) &&
1002 (uio->uio_resid == 0) &&
1008 vfs_timestamp(&wpipe->pipe_mtime);
1011 * We have something to offer,
1012 * wake up select/poll.
1014 if (wpipe->pipe_buffer.cnt)
1015 pipeselwakeup(wpipe);
1021 * we implement a very minimal set of ioctls for compatibility with sockets.
1024 pipe_ioctl(struct file *fp, u_long cmd, caddr_t data, struct thread *td)
1026 struct pipe *mpipe = (struct pipe *)fp->f_data;
1035 mpipe->pipe_state |= PIPE_ASYNC;
1037 mpipe->pipe_state &= ~PIPE_ASYNC;
1042 if (mpipe->pipe_state & PIPE_DIRECTW)
1043 *(int *)data = mpipe->pipe_map.cnt;
1045 *(int *)data = mpipe->pipe_buffer.cnt;
1049 return (fsetown(*(int *)data, &mpipe->pipe_sigio));
1052 *(int *)data = fgetown(mpipe->pipe_sigio);
1055 /* This is deprecated, FIOSETOWN should be used instead. */
1057 return (fsetown(-(*(int *)data), &mpipe->pipe_sigio));
1059 /* This is deprecated, FIOGETOWN should be used instead. */
1061 *(int *)data = -fgetown(mpipe->pipe_sigio);
1069 pipe_poll(struct file *fp, int events, struct ucred *cred, struct thread *td)
1071 struct pipe *rpipe = (struct pipe *)fp->f_data;
1075 wpipe = rpipe->pipe_peer;
1076 if (events & (POLLIN | POLLRDNORM))
1077 if ((rpipe->pipe_state & PIPE_DIRECTW) ||
1078 (rpipe->pipe_buffer.cnt > 0) ||
1079 (rpipe->pipe_state & PIPE_EOF))
1080 revents |= events & (POLLIN | POLLRDNORM);
1082 if (events & (POLLOUT | POLLWRNORM))
1083 if (wpipe == NULL || (wpipe->pipe_state & PIPE_EOF) ||
1084 (((wpipe->pipe_state & PIPE_DIRECTW) == 0) &&
1085 (wpipe->pipe_buffer.size - wpipe->pipe_buffer.cnt) >= PIPE_BUF))
1086 revents |= events & (POLLOUT | POLLWRNORM);
1088 if ((rpipe->pipe_state & PIPE_EOF) ||
1090 (wpipe->pipe_state & PIPE_EOF))
1094 if (events & (POLLIN | POLLRDNORM)) {
1095 selrecord(td, &rpipe->pipe_sel);
1096 rpipe->pipe_state |= PIPE_SEL;
1099 if (events & (POLLOUT | POLLWRNORM)) {
1100 selrecord(td, &wpipe->pipe_sel);
1101 wpipe->pipe_state |= PIPE_SEL;
1109 pipe_stat(struct file *fp, struct stat *ub, struct thread *td)
1111 struct pipe *pipe = (struct pipe *)fp->f_data;
1113 bzero((caddr_t)ub, sizeof(*ub));
1114 ub->st_mode = S_IFIFO;
1115 ub->st_blksize = pipe->pipe_buffer.size;
1116 ub->st_size = pipe->pipe_buffer.cnt;
1117 ub->st_blocks = (ub->st_size + ub->st_blksize - 1) / ub->st_blksize;
1118 ub->st_atimespec = pipe->pipe_atime;
1119 ub->st_mtimespec = pipe->pipe_mtime;
1120 ub->st_ctimespec = pipe->pipe_ctime;
1122 * Left as 0: st_dev, st_ino, st_nlink, st_uid, st_gid, st_rdev,
1124 * XXX (st_dev, st_ino) should be unique.
1131 pipe_close(struct file *fp, struct thread *td)
1133 struct pipe *cpipe = (struct pipe *)fp->f_data;
1135 fp->f_ops = &badfileops;
1137 funsetown(cpipe->pipe_sigio);
1143 pipe_free_kmem(struct pipe *cpipe)
1146 if (cpipe->pipe_buffer.buffer != NULL) {
1147 if (cpipe->pipe_buffer.size > PIPE_SIZE)
1149 amountpipekva -= cpipe->pipe_buffer.size;
1150 kmem_free(kernel_map,
1151 (vm_offset_t)cpipe->pipe_buffer.buffer,
1152 cpipe->pipe_buffer.size);
1153 cpipe->pipe_buffer.buffer = NULL;
1155 #ifndef PIPE_NODIRECT
1156 if (cpipe->pipe_map.kva != NULL) {
1157 amountpipekva -= cpipe->pipe_buffer.size + PAGE_SIZE;
1158 kmem_free(kernel_map,
1159 cpipe->pipe_map.kva,
1160 cpipe->pipe_buffer.size + PAGE_SIZE);
1161 cpipe->pipe_map.cnt = 0;
1162 cpipe->pipe_map.kva = 0;
1163 cpipe->pipe_map.pos = 0;
1164 cpipe->pipe_map.npages = 0;
1173 pipeclose(struct pipe *cpipe)
1179 pipeselwakeup(cpipe);
1182 * If the other side is blocked, wake it up saying that
1183 * we want to close it down.
1185 while (cpipe->pipe_busy) {
1187 cpipe->pipe_state |= PIPE_WANT | PIPE_EOF;
1188 tsleep(cpipe, PRIBIO, "pipecl", 0);
1192 * Disconnect from peer
1194 if ((ppipe = cpipe->pipe_peer) != NULL) {
1195 pipeselwakeup(ppipe);
1197 ppipe->pipe_state |= PIPE_EOF;
1199 KNOTE(&ppipe->pipe_sel.si_note, 0);
1200 ppipe->pipe_peer = NULL;
1205 pipe_free_kmem(cpipe);
1206 zfree(pipe_zone, cpipe);
1212 pipe_kqfilter(struct file *fp, struct knote *kn)
1214 struct pipe *cpipe = (struct pipe *)kn->kn_fp->f_data;
1216 switch (kn->kn_filter) {
1218 kn->kn_fop = &pipe_rfiltops;
1221 kn->kn_fop = &pipe_wfiltops;
1222 cpipe = cpipe->pipe_peer;
1224 /* other end of pipe has been closed */
1230 kn->kn_hook = (caddr_t)cpipe;
1232 SLIST_INSERT_HEAD(&cpipe->pipe_sel.si_note, kn, kn_selnext);
1237 filt_pipedetach(struct knote *kn)
1239 struct pipe *cpipe = (struct pipe *)kn->kn_hook;
1241 SLIST_REMOVE(&cpipe->pipe_sel.si_note, kn, knote, kn_selnext);
1246 filt_piperead(struct knote *kn, long hint)
1248 struct pipe *rpipe = (struct pipe *)kn->kn_fp->f_data;
1249 struct pipe *wpipe = rpipe->pipe_peer;
1251 kn->kn_data = rpipe->pipe_buffer.cnt;
1252 if ((kn->kn_data == 0) && (rpipe->pipe_state & PIPE_DIRECTW))
1253 kn->kn_data = rpipe->pipe_map.cnt;
1255 if ((rpipe->pipe_state & PIPE_EOF) ||
1256 (wpipe == NULL) || (wpipe->pipe_state & PIPE_EOF)) {
1257 kn->kn_flags |= EV_EOF;
1260 return (kn->kn_data > 0);
1265 filt_pipewrite(struct knote *kn, long hint)
1267 struct pipe *rpipe = (struct pipe *)kn->kn_fp->f_data;
1268 struct pipe *wpipe = rpipe->pipe_peer;
1270 if ((wpipe == NULL) || (wpipe->pipe_state & PIPE_EOF)) {
1272 kn->kn_flags |= EV_EOF;
1275 kn->kn_data = wpipe->pipe_buffer.size - wpipe->pipe_buffer.cnt;
1276 if (wpipe->pipe_state & PIPE_DIRECTW)
1279 return (kn->kn_data >= PIPE_BUF);