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.14 2004/02/20 17:11:07 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>
90 * Use this define if you want to disable *fancy* VM things. Expect an
91 * approx 30% decrease in transfer rate. This could be useful for
94 /* #define PIPE_NODIRECT */
97 * interfaces to the outside world
99 static int pipe_read (struct file *fp, struct uio *uio,
100 struct ucred *cred, int flags, struct thread *td);
101 static int pipe_write (struct file *fp, struct uio *uio,
102 struct ucred *cred, int flags, struct thread *td);
103 static int pipe_close (struct file *fp, struct thread *td);
104 static int pipe_poll (struct file *fp, int events, struct ucred *cred,
106 static int pipe_kqfilter (struct file *fp, struct knote *kn);
107 static int pipe_stat (struct file *fp, struct stat *sb, struct thread *td);
108 static int pipe_ioctl (struct file *fp, u_long cmd, caddr_t data, struct thread *td);
110 static struct fileops pipeops = {
113 pipe_read, pipe_write, pipe_ioctl, pipe_poll, pipe_kqfilter,
114 pipe_stat, pipe_close
117 static void filt_pipedetach(struct knote *kn);
118 static int filt_piperead(struct knote *kn, long hint);
119 static int filt_pipewrite(struct knote *kn, long hint);
121 static struct filterops pipe_rfiltops =
122 { 1, NULL, filt_pipedetach, filt_piperead };
123 static struct filterops pipe_wfiltops =
124 { 1, NULL, filt_pipedetach, filt_pipewrite };
126 MALLOC_DEFINE(M_PIPE, "pipe", "pipe structures");
129 * Default pipe buffer size(s), this can be kind-of large now because pipe
130 * space is pageable. The pipe code will try to maintain locality of
131 * reference for performance reasons, so small amounts of outstanding I/O
132 * will not wipe the cache.
134 #define MINPIPESIZE (PIPE_SIZE/3)
135 #define MAXPIPESIZE (2*PIPE_SIZE/3)
138 * Maximum amount of kva for pipes -- this is kind-of a soft limit, but
139 * is there so that on large systems, we don't exhaust it.
141 #define MAXPIPEKVA (8*1024*1024)
144 * Limit for direct transfers, we cannot, of course limit
145 * the amount of kva for pipes in general though.
147 #define LIMITPIPEKVA (16*1024*1024)
150 * Limit the number of "big" pipes
152 #define LIMITBIGPIPES 32
153 #define PIPEQ_MAX_CACHE 16 /* per-cpu pipe structure cache */
155 static int pipe_maxbig = LIMITBIGPIPES;
156 static int pipe_maxcache = PIPEQ_MAX_CACHE;
157 static int pipe_nbig;
159 static int pipe_bcache_alloc;
160 static int pipe_bkmem_alloc;
161 static int pipe_dcache_alloc;
162 static int pipe_dkmem_alloc;
164 SYSCTL_NODE(_kern, OID_AUTO, pipe, CTLFLAG_RW, 0, "Pipe operation");
165 SYSCTL_INT(_kern_pipe, OID_AUTO, nbig,
166 CTLFLAG_RD, &pipe_nbig, 0, "numer of big pipes allocated");
167 SYSCTL_INT(_kern_pipe, OID_AUTO, kva,
168 CTLFLAG_RD, &pipe_kva, 0, "kva reserved by pipes");
169 SYSCTL_INT(_kern_pipe, OID_AUTO, maxcache,
170 CTLFLAG_RW, &pipe_maxcache, 0, "max pipes cached per-cpu");
171 SYSCTL_INT(_kern_pipe, OID_AUTO, maxbig,
172 CTLFLAG_RW, &pipe_maxbig, 0, "max number of big pipes");
173 #if !defined(NO_PIPE_SYSCTL_STATS)
174 SYSCTL_INT(_kern_pipe, OID_AUTO, bcache_alloc,
175 CTLFLAG_RW, &pipe_bcache_alloc, 0, "pipe buffer from pcpu cache");
176 SYSCTL_INT(_kern_pipe, OID_AUTO, dcache_alloc,
177 CTLFLAG_RW, &pipe_dcache_alloc, 0, "pipe direct buf from pcpu cache");
178 SYSCTL_INT(_kern_pipe, OID_AUTO, bkmem_alloc,
179 CTLFLAG_RW, &pipe_bkmem_alloc, 0, "pipe buffer from kmem");
180 SYSCTL_INT(_kern_pipe, OID_AUTO, dkmem_alloc,
181 CTLFLAG_RW, &pipe_dkmem_alloc, 0, "pipe direct buf from kmem");
184 static void pipeclose (struct pipe *cpipe);
185 static void pipe_free_kmem (struct pipe *cpipe);
186 static int pipe_create (struct pipe **cpipep);
187 static __inline int pipelock (struct pipe *cpipe, int catch);
188 static __inline void pipeunlock (struct pipe *cpipe);
189 static __inline void pipeselwakeup (struct pipe *cpipe);
190 #ifndef PIPE_NODIRECT
191 static int pipe_build_write_buffer (struct pipe *wpipe, struct uio *uio);
192 static void pipe_destroy_write_buffer (struct pipe *wpipe);
193 static int pipe_direct_write (struct pipe *wpipe, struct uio *uio);
194 static void pipe_clone_write_buffer (struct pipe *wpipe);
196 static int pipespace (struct pipe *cpipe, int size);
199 * The pipe system call for the DTYPE_PIPE type of pipes
201 * pipe_ARgs(int dummy)
206 pipe(struct pipe_args *uap)
208 struct thread *td = curthread;
209 struct proc *p = td->td_proc;
210 struct filedesc *fdp;
211 struct file *rf, *wf;
212 struct pipe *rpipe, *wpipe;
218 rpipe = wpipe = NULL;
219 if (pipe_create(&rpipe) || pipe_create(&wpipe)) {
225 rpipe->pipe_state |= PIPE_DIRECTOK;
226 wpipe->pipe_state |= PIPE_DIRECTOK;
228 error = falloc(p, &rf, &fd1);
235 uap->sysmsg_fds[0] = fd1;
238 * Warning: once we've gotten past allocation of the fd for the
239 * read-side, we can only drop the read side via fdrop() in order
240 * to avoid races against processes which manage to dup() the read
241 * side while we are blocked trying to allocate the write side.
243 rf->f_flag = FREAD | FWRITE;
244 rf->f_type = DTYPE_PIPE;
245 rf->f_data = (caddr_t)rpipe;
246 rf->f_ops = &pipeops;
247 error = falloc(p, &wf, &fd2);
249 if (fdp->fd_ofiles[fd1] == rf) {
250 fdp->fd_ofiles[fd1] = NULL;
254 /* rpipe has been closed by fdrop(). */
258 wf->f_flag = FREAD | FWRITE;
259 wf->f_type = DTYPE_PIPE;
260 wf->f_data = (caddr_t)wpipe;
261 wf->f_ops = &pipeops;
262 uap->sysmsg_fds[1] = fd2;
264 rpipe->pipe_peer = wpipe;
265 wpipe->pipe_peer = rpipe;
272 * Allocate kva for pipe circular buffer, the space is pageable
273 * This routine will 'realloc' the size of a pipe safely, if it fails
274 * it will retain the old buffer.
275 * If it fails it will return ENOMEM.
278 pipespace(struct pipe *cpipe, int size)
280 struct vm_object *object;
284 npages = round_page(size) / PAGE_SIZE;
285 object = cpipe->pipe_buffer.object;
288 * [re]create the object if necessary and reserve space for it
289 * in the kernel_map. The object and memory are pageable. On
290 * success, free the old resources before assigning the new
293 if (object == NULL || object->size != npages) {
294 object = vm_object_allocate(OBJT_DEFAULT, npages);
295 buffer = (caddr_t) vm_map_min(kernel_map);
297 error = vm_map_find(kernel_map, object, 0,
298 (vm_offset_t *) &buffer, size, 1,
299 VM_PROT_ALL, VM_PROT_ALL, 0);
301 if (error != KERN_SUCCESS) {
302 vm_object_deallocate(object);
306 pipe_free_kmem(cpipe);
307 cpipe->pipe_buffer.object = object;
308 cpipe->pipe_buffer.buffer = buffer;
309 cpipe->pipe_buffer.size = size;
313 if (cpipe->pipe_map.kva)
316 cpipe->pipe_buffer.in = 0;
317 cpipe->pipe_buffer.out = 0;
318 cpipe->pipe_buffer.cnt = 0;
323 * Initialize and allocate VM and memory for pipe, pulling the pipe from
324 * our per-cpu cache if possible. For now make sure it is sized for the
325 * smaller PIPE_SIZE default.
329 struct pipe **cpipep;
331 globaldata_t gd = mycpu;
335 if ((cpipe = gd->gd_pipeq) != NULL) {
336 gd->gd_pipeq = cpipe->pipe_peer;
338 cpipe->pipe_peer = NULL;
340 cpipe = malloc(sizeof(struct pipe), M_PIPE, M_WAITOK|M_ZERO);
343 if ((error = pipespace(cpipe, PIPE_SIZE)) != 0)
345 vfs_timestamp(&cpipe->pipe_ctime);
346 cpipe->pipe_atime = cpipe->pipe_ctime;
347 cpipe->pipe_mtime = cpipe->pipe_ctime;
353 * lock a pipe for I/O, blocking other access
356 pipelock(cpipe, catch)
362 while (cpipe->pipe_state & PIPE_LOCK) {
363 cpipe->pipe_state |= PIPE_LWANT;
364 error = tsleep(cpipe, (catch ? PCATCH : 0), "pipelk", 0);
368 cpipe->pipe_state |= PIPE_LOCK;
373 * unlock a pipe I/O lock
380 cpipe->pipe_state &= ~PIPE_LOCK;
381 if (cpipe->pipe_state & PIPE_LWANT) {
382 cpipe->pipe_state &= ~PIPE_LWANT;
392 if (cpipe->pipe_state & PIPE_SEL) {
393 cpipe->pipe_state &= ~PIPE_SEL;
394 selwakeup(&cpipe->pipe_sel);
396 if ((cpipe->pipe_state & PIPE_ASYNC) && cpipe->pipe_sigio)
397 pgsigio(cpipe->pipe_sigio, SIGIO, 0);
398 KNOTE(&cpipe->pipe_sel.si_note, 0);
403 pipe_read(struct file *fp, struct uio *uio, struct ucred *cred,
404 int flags, struct thread *td)
406 struct pipe *rpipe = (struct pipe *) fp->f_data;
412 error = pipelock(rpipe, 1);
416 while (uio->uio_resid) {
418 * normal pipe buffer receive
420 if (rpipe->pipe_buffer.cnt > 0) {
421 size = rpipe->pipe_buffer.size - rpipe->pipe_buffer.out;
422 if (size > rpipe->pipe_buffer.cnt)
423 size = rpipe->pipe_buffer.cnt;
424 if (size > (u_int) uio->uio_resid)
425 size = (u_int) uio->uio_resid;
427 error = uiomove(&rpipe->pipe_buffer.buffer[rpipe->pipe_buffer.out],
432 rpipe->pipe_buffer.out += size;
433 if (rpipe->pipe_buffer.out >= rpipe->pipe_buffer.size)
434 rpipe->pipe_buffer.out = 0;
436 rpipe->pipe_buffer.cnt -= size;
439 * If there is no more to read in the pipe, reset
440 * its pointers to the beginning. This improves
443 if (rpipe->pipe_buffer.cnt == 0) {
444 rpipe->pipe_buffer.in = 0;
445 rpipe->pipe_buffer.out = 0;
448 #ifndef PIPE_NODIRECT
450 * Direct copy, bypassing a kernel buffer.
452 } else if ((size = rpipe->pipe_map.cnt) &&
453 (rpipe->pipe_state & PIPE_DIRECTW)) {
455 if (size > (u_int) uio->uio_resid)
456 size = (u_int) uio->uio_resid;
458 va = (caddr_t) rpipe->pipe_map.kva +
460 error = uiomove(va, size, uio);
464 rpipe->pipe_map.pos += size;
465 rpipe->pipe_map.cnt -= size;
466 if (rpipe->pipe_map.cnt == 0) {
467 rpipe->pipe_state &= ~PIPE_DIRECTW;
473 * detect EOF condition
474 * read returns 0 on EOF, no need to set error
476 if (rpipe->pipe_state & PIPE_EOF)
480 * If the "write-side" has been blocked, wake it up now.
482 if (rpipe->pipe_state & PIPE_WANTW) {
483 rpipe->pipe_state &= ~PIPE_WANTW;
488 * Break if some data was read.
494 * Unlock the pipe buffer for our remaining processing. We
495 * will either break out with an error or we will sleep and
501 * Handle non-blocking mode operation or
502 * wait for more data.
504 if (fp->f_flag & FNONBLOCK) {
507 rpipe->pipe_state |= PIPE_WANTR;
508 if ((error = tsleep(rpipe, PCATCH,
509 "piperd", 0)) == 0) {
510 error = pipelock(rpipe, 1);
520 vfs_timestamp(&rpipe->pipe_atime);
525 * PIPE_WANT processing only makes sense if pipe_busy is 0.
527 if ((rpipe->pipe_busy == 0) && (rpipe->pipe_state & PIPE_WANT)) {
528 rpipe->pipe_state &= ~(PIPE_WANT|PIPE_WANTW);
530 } else if (rpipe->pipe_buffer.cnt < MINPIPESIZE) {
532 * Handle write blocking hysteresis.
534 if (rpipe->pipe_state & PIPE_WANTW) {
535 rpipe->pipe_state &= ~PIPE_WANTW;
540 if ((rpipe->pipe_buffer.size - rpipe->pipe_buffer.cnt) >= PIPE_BUF)
541 pipeselwakeup(rpipe);
546 #ifndef PIPE_NODIRECT
548 * Map the sending processes' buffer into kernel space and wire it.
549 * This is similar to a physical write operation.
552 pipe_build_write_buffer(wpipe, uio)
558 vm_offset_t addr, endaddr;
561 size = (u_int) uio->uio_iov->iov_len;
562 if (size > wpipe->pipe_buffer.size)
563 size = wpipe->pipe_buffer.size;
565 endaddr = round_page((vm_offset_t)uio->uio_iov->iov_base + size);
566 addr = trunc_page((vm_offset_t)uio->uio_iov->iov_base);
567 for (i = 0; addr < endaddr; addr += PAGE_SIZE, i++) {
570 if (vm_fault_quick((caddr_t)addr, VM_PROT_READ) < 0 ||
571 (paddr = pmap_kextract(addr)) == 0) {
574 for (j = 0; j < i; j++)
575 vm_page_unhold(wpipe->pipe_map.ms[j]);
579 m = PHYS_TO_VM_PAGE(paddr);
581 wpipe->pipe_map.ms[i] = m;
585 * set up the control block
587 wpipe->pipe_map.npages = i;
588 wpipe->pipe_map.pos =
589 ((vm_offset_t) uio->uio_iov->iov_base) & PAGE_MASK;
590 wpipe->pipe_map.cnt = size;
595 if (wpipe->pipe_map.kva == 0) {
597 * We need to allocate space for an extra page because the
598 * address range might (will) span pages at times.
600 wpipe->pipe_map.kva = kmem_alloc_pageable(kernel_map,
601 wpipe->pipe_buffer.size + PAGE_SIZE);
602 pipe_kva += wpipe->pipe_buffer.size + PAGE_SIZE;
605 pmap_qenter(wpipe->pipe_map.kva, wpipe->pipe_map.ms,
606 wpipe->pipe_map.npages);
609 * and update the uio data
612 uio->uio_iov->iov_len -= size;
613 uio->uio_iov->iov_base += size;
614 if (uio->uio_iov->iov_len == 0)
616 uio->uio_resid -= size;
617 uio->uio_offset += size;
622 * unmap and unwire the process buffer
625 pipe_destroy_write_buffer(wpipe)
630 if (wpipe->pipe_map.kva) {
631 pmap_qremove(wpipe->pipe_map.kva, wpipe->pipe_map.npages);
633 if (pipe_kva > MAXPIPEKVA) {
634 vm_offset_t kva = wpipe->pipe_map.kva;
635 wpipe->pipe_map.kva = 0;
636 kmem_free(kernel_map, kva,
637 wpipe->pipe_buffer.size + PAGE_SIZE);
638 pipe_kva -= wpipe->pipe_buffer.size + PAGE_SIZE;
641 for (i = 0; i < wpipe->pipe_map.npages; i++)
642 vm_page_unhold(wpipe->pipe_map.ms[i]);
643 wpipe->pipe_map.npages = 0;
647 * In the case of a signal, the writing process might go away. This
648 * code copies the data into the circular buffer so that the source
649 * pages can be freed without loss of data.
652 pipe_clone_write_buffer(wpipe)
658 size = wpipe->pipe_map.cnt;
659 pos = wpipe->pipe_map.pos;
660 bcopy((caddr_t) wpipe->pipe_map.kva + pos,
661 (caddr_t) wpipe->pipe_buffer.buffer, size);
663 wpipe->pipe_buffer.in = size;
664 wpipe->pipe_buffer.out = 0;
665 wpipe->pipe_buffer.cnt = size;
666 wpipe->pipe_state &= ~PIPE_DIRECTW;
668 pipe_destroy_write_buffer(wpipe);
672 * This implements the pipe buffer write mechanism. Note that only
673 * a direct write OR a normal pipe write can be pending at any given time.
674 * If there are any characters in the pipe buffer, the direct write will
675 * be deferred until the receiving process grabs all of the bytes from
676 * the pipe buffer. Then the direct mapping write is set-up.
679 pipe_direct_write(wpipe, uio)
686 while (wpipe->pipe_state & PIPE_DIRECTW) {
687 if (wpipe->pipe_state & PIPE_WANTR) {
688 wpipe->pipe_state &= ~PIPE_WANTR;
691 wpipe->pipe_state |= PIPE_WANTW;
692 error = tsleep(wpipe, PCATCH, "pipdww", 0);
695 if (wpipe->pipe_state & PIPE_EOF) {
700 wpipe->pipe_map.cnt = 0; /* transfer not ready yet */
701 if (wpipe->pipe_buffer.cnt > 0) {
702 if (wpipe->pipe_state & PIPE_WANTR) {
703 wpipe->pipe_state &= ~PIPE_WANTR;
707 wpipe->pipe_state |= PIPE_WANTW;
708 error = tsleep(wpipe, PCATCH, "pipdwc", 0);
711 if (wpipe->pipe_state & PIPE_EOF) {
718 wpipe->pipe_state |= PIPE_DIRECTW;
720 error = pipe_build_write_buffer(wpipe, uio);
722 wpipe->pipe_state &= ~PIPE_DIRECTW;
727 while (!error && (wpipe->pipe_state & PIPE_DIRECTW)) {
728 if (wpipe->pipe_state & PIPE_EOF) {
730 pipe_destroy_write_buffer(wpipe);
732 pipeselwakeup(wpipe);
736 if (wpipe->pipe_state & PIPE_WANTR) {
737 wpipe->pipe_state &= ~PIPE_WANTR;
740 pipeselwakeup(wpipe);
741 error = tsleep(wpipe, PCATCH, "pipdwt", 0);
745 if (wpipe->pipe_state & PIPE_DIRECTW) {
747 * this bit of trickery substitutes a kernel buffer for
748 * the process that might be going away.
750 pipe_clone_write_buffer(wpipe);
752 pipe_destroy_write_buffer(wpipe);
764 pipe_write(struct file *fp, struct uio *uio, struct ucred *cred,
765 int flags, struct thread *td)
769 struct pipe *wpipe, *rpipe;
771 rpipe = (struct pipe *) fp->f_data;
772 wpipe = rpipe->pipe_peer;
775 * detect loss of pipe read side, issue SIGPIPE if lost.
777 if ((wpipe == NULL) || (wpipe->pipe_state & PIPE_EOF)) {
783 * If it is advantageous to resize the pipe buffer, do
786 if ((uio->uio_resid > PIPE_SIZE) &&
787 (pipe_nbig < pipe_maxbig) &&
788 (wpipe->pipe_state & PIPE_DIRECTW) == 0 &&
789 (wpipe->pipe_buffer.size <= PIPE_SIZE) &&
790 (wpipe->pipe_buffer.cnt == 0)) {
792 if ((error = pipelock(wpipe,1)) == 0) {
793 if (pipespace(wpipe, BIG_PIPE_SIZE) == 0)
800 * If an early error occured unbusy and return, waking up any pending
805 if ((wpipe->pipe_busy == 0) &&
806 (wpipe->pipe_state & PIPE_WANT)) {
807 wpipe->pipe_state &= ~(PIPE_WANT | PIPE_WANTR);
813 KASSERT(wpipe->pipe_buffer.buffer != NULL, ("pipe buffer gone"));
815 orig_resid = uio->uio_resid;
817 while (uio->uio_resid) {
820 #ifndef PIPE_NODIRECT
822 * If the transfer is large, we can gain performance if
823 * we do process-to-process copies directly.
824 * If the write is non-blocking, we don't use the
825 * direct write mechanism.
827 * The direct write mechanism will detect the reader going
830 if ((uio->uio_iov->iov_len >= PIPE_MINDIRECT) &&
831 (fp->f_flag & FNONBLOCK) == 0 &&
832 (wpipe->pipe_map.kva || (pipe_kva < LIMITPIPEKVA)) &&
833 (uio->uio_iov->iov_len >= PIPE_MINDIRECT)) {
834 error = pipe_direct_write( wpipe, uio);
842 * Pipe buffered writes cannot be coincidental with
843 * direct writes. We wait until the currently executing
844 * direct write is completed before we start filling the
845 * pipe buffer. We break out if a signal occurs or the
849 while (wpipe->pipe_state & PIPE_DIRECTW) {
850 if (wpipe->pipe_state & PIPE_WANTR) {
851 wpipe->pipe_state &= ~PIPE_WANTR;
854 error = tsleep(wpipe, PCATCH, "pipbww", 0);
855 if (wpipe->pipe_state & PIPE_EOF)
860 if (wpipe->pipe_state & PIPE_EOF) {
865 space = wpipe->pipe_buffer.size - wpipe->pipe_buffer.cnt;
867 /* Writes of size <= PIPE_BUF must be atomic. */
868 if ((space < uio->uio_resid) && (orig_resid <= PIPE_BUF))
871 if (space > 0 && (wpipe->pipe_buffer.cnt < PIPE_SIZE)) {
872 if ((error = pipelock(wpipe,1)) == 0) {
873 int size; /* Transfer size */
874 int segsize; /* first segment to transfer */
877 * It is possible for a direct write to
878 * slip in on us... handle it here...
880 if (wpipe->pipe_state & PIPE_DIRECTW) {
885 * If a process blocked in uiomove, our
886 * value for space might be bad.
888 * XXX will we be ok if the reader has gone
891 if (space > wpipe->pipe_buffer.size -
892 wpipe->pipe_buffer.cnt) {
898 * Transfer size is minimum of uio transfer
899 * and free space in pipe buffer.
901 if (space > uio->uio_resid)
902 size = uio->uio_resid;
906 * First segment to transfer is minimum of
907 * transfer size and contiguous space in
908 * pipe buffer. If first segment to transfer
909 * is less than the transfer size, we've got
910 * a wraparound in the buffer.
912 segsize = wpipe->pipe_buffer.size -
913 wpipe->pipe_buffer.in;
917 /* Transfer first segment */
919 error = uiomove(&wpipe->pipe_buffer.buffer[wpipe->pipe_buffer.in],
922 if (error == 0 && segsize < size) {
924 * Transfer remaining part now, to
925 * support atomic writes. Wraparound
928 if (wpipe->pipe_buffer.in + segsize !=
929 wpipe->pipe_buffer.size)
930 panic("Expected pipe buffer wraparound disappeared");
932 error = uiomove(&wpipe->pipe_buffer.buffer[0],
933 size - segsize, uio);
936 wpipe->pipe_buffer.in += size;
937 if (wpipe->pipe_buffer.in >=
938 wpipe->pipe_buffer.size) {
939 if (wpipe->pipe_buffer.in != size - segsize + wpipe->pipe_buffer.size)
940 panic("Expected wraparound bad");
941 wpipe->pipe_buffer.in = size - segsize;
944 wpipe->pipe_buffer.cnt += size;
945 if (wpipe->pipe_buffer.cnt > wpipe->pipe_buffer.size)
946 panic("Pipe buffer overflow");
956 * If the "read-side" has been blocked, wake it up now.
958 if (wpipe->pipe_state & PIPE_WANTR) {
959 wpipe->pipe_state &= ~PIPE_WANTR;
964 * don't block on non-blocking I/O
966 if (fp->f_flag & FNONBLOCK) {
972 * We have no more space and have something to offer,
973 * wake up select/poll.
975 pipeselwakeup(wpipe);
977 wpipe->pipe_state |= PIPE_WANTW;
978 error = tsleep(wpipe, PCATCH, "pipewr", 0);
982 * If read side wants to go away, we just issue a signal
985 if (wpipe->pipe_state & PIPE_EOF) {
994 if ((wpipe->pipe_busy == 0) && (wpipe->pipe_state & PIPE_WANT)) {
995 wpipe->pipe_state &= ~(PIPE_WANT | PIPE_WANTR);
997 } else if (wpipe->pipe_buffer.cnt > 0) {
999 * If we have put any characters in the buffer, we wake up
1002 if (wpipe->pipe_state & PIPE_WANTR) {
1003 wpipe->pipe_state &= ~PIPE_WANTR;
1009 * Don't return EPIPE if I/O was successful
1011 if ((wpipe->pipe_buffer.cnt == 0) &&
1012 (uio->uio_resid == 0) &&
1018 vfs_timestamp(&wpipe->pipe_mtime);
1021 * We have something to offer,
1022 * wake up select/poll.
1024 if (wpipe->pipe_buffer.cnt)
1025 pipeselwakeup(wpipe);
1031 * we implement a very minimal set of ioctls for compatibility with sockets.
1034 pipe_ioctl(struct file *fp, u_long cmd, caddr_t data, struct thread *td)
1036 struct pipe *mpipe = (struct pipe *)fp->f_data;
1045 mpipe->pipe_state |= PIPE_ASYNC;
1047 mpipe->pipe_state &= ~PIPE_ASYNC;
1052 if (mpipe->pipe_state & PIPE_DIRECTW)
1053 *(int *)data = mpipe->pipe_map.cnt;
1055 *(int *)data = mpipe->pipe_buffer.cnt;
1059 return (fsetown(*(int *)data, &mpipe->pipe_sigio));
1062 *(int *)data = fgetown(mpipe->pipe_sigio);
1065 /* This is deprecated, FIOSETOWN should be used instead. */
1067 return (fsetown(-(*(int *)data), &mpipe->pipe_sigio));
1069 /* This is deprecated, FIOGETOWN should be used instead. */
1071 *(int *)data = -fgetown(mpipe->pipe_sigio);
1079 pipe_poll(struct file *fp, int events, struct ucred *cred, struct thread *td)
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(td, &rpipe->pipe_sel);
1106 rpipe->pipe_state |= PIPE_SEL;
1109 if (events & (POLLOUT | POLLWRNORM)) {
1110 selrecord(td, &wpipe->pipe_sel);
1111 wpipe->pipe_state |= PIPE_SEL;
1119 pipe_stat(struct file *fp, struct stat *ub, struct thread *td)
1121 struct pipe *pipe = (struct pipe *)fp->f_data;
1123 bzero((caddr_t)ub, sizeof(*ub));
1124 ub->st_mode = S_IFIFO;
1125 ub->st_blksize = pipe->pipe_buffer.size;
1126 ub->st_size = pipe->pipe_buffer.cnt;
1127 ub->st_blocks = (ub->st_size + ub->st_blksize - 1) / ub->st_blksize;
1128 ub->st_atimespec = pipe->pipe_atime;
1129 ub->st_mtimespec = pipe->pipe_mtime;
1130 ub->st_ctimespec = pipe->pipe_ctime;
1132 * Left as 0: st_dev, st_ino, st_nlink, st_uid, st_gid, st_rdev,
1134 * XXX (st_dev, st_ino) should be unique.
1141 pipe_close(struct file *fp, struct thread *td)
1143 struct pipe *cpipe = (struct pipe *)fp->f_data;
1145 fp->f_ops = &badfileops;
1147 funsetown(cpipe->pipe_sigio);
1153 pipe_free_kmem(struct pipe *cpipe)
1155 if (cpipe->pipe_buffer.buffer != NULL) {
1156 if (cpipe->pipe_buffer.size > PIPE_SIZE)
1158 pipe_kva -= cpipe->pipe_buffer.size;
1159 kmem_free(kernel_map,
1160 (vm_offset_t)cpipe->pipe_buffer.buffer,
1161 cpipe->pipe_buffer.size);
1162 cpipe->pipe_buffer.buffer = NULL;
1163 cpipe->pipe_buffer.object = NULL;
1165 #ifndef PIPE_NODIRECT
1166 if (cpipe->pipe_map.kva != NULL) {
1167 pipe_kva -= cpipe->pipe_buffer.size + PAGE_SIZE;
1168 kmem_free(kernel_map,
1169 cpipe->pipe_map.kva,
1170 cpipe->pipe_buffer.size + PAGE_SIZE);
1171 cpipe->pipe_map.cnt = 0;
1172 cpipe->pipe_map.kva = 0;
1173 cpipe->pipe_map.pos = 0;
1174 cpipe->pipe_map.npages = 0;
1183 pipeclose(struct pipe *cpipe)
1191 pipeselwakeup(cpipe);
1194 * If the other side is blocked, wake it up saying that
1195 * we want to close it down.
1197 while (cpipe->pipe_busy) {
1199 cpipe->pipe_state |= PIPE_WANT | PIPE_EOF;
1200 tsleep(cpipe, 0, "pipecl", 0);
1204 * Disconnect from peer
1206 if ((ppipe = cpipe->pipe_peer) != NULL) {
1207 pipeselwakeup(ppipe);
1209 ppipe->pipe_state |= PIPE_EOF;
1211 KNOTE(&ppipe->pipe_sel.si_note, 0);
1212 ppipe->pipe_peer = NULL;
1216 * free or cache resources
1219 if (gd->gd_pipeqcount >= pipe_maxcache ||
1220 cpipe->pipe_buffer.size != PIPE_SIZE
1222 pipe_free_kmem(cpipe);
1223 free(cpipe, M_PIPE);
1225 KKASSERT(cpipe->pipe_map.npages == 0);
1227 cpipe->pipe_state = 0;
1228 cpipe->pipe_busy = 0;
1229 cpipe->pipe_map.cnt = 0;
1230 cpipe->pipe_map.pos = 0;
1231 cpipe->pipe_peer = gd->gd_pipeq;
1232 gd->gd_pipeq = cpipe;
1233 ++gd->gd_pipeqcount;
1239 pipe_kqfilter(struct file *fp, struct knote *kn)
1241 struct pipe *cpipe = (struct pipe *)kn->kn_fp->f_data;
1243 switch (kn->kn_filter) {
1245 kn->kn_fop = &pipe_rfiltops;
1248 kn->kn_fop = &pipe_wfiltops;
1249 cpipe = cpipe->pipe_peer;
1251 /* other end of pipe has been closed */
1257 kn->kn_hook = (caddr_t)cpipe;
1259 SLIST_INSERT_HEAD(&cpipe->pipe_sel.si_note, kn, kn_selnext);
1264 filt_pipedetach(struct knote *kn)
1266 struct pipe *cpipe = (struct pipe *)kn->kn_hook;
1268 SLIST_REMOVE(&cpipe->pipe_sel.si_note, kn, knote, kn_selnext);
1273 filt_piperead(struct knote *kn, long hint)
1275 struct pipe *rpipe = (struct pipe *)kn->kn_fp->f_data;
1276 struct pipe *wpipe = rpipe->pipe_peer;
1278 kn->kn_data = rpipe->pipe_buffer.cnt;
1279 if ((kn->kn_data == 0) && (rpipe->pipe_state & PIPE_DIRECTW))
1280 kn->kn_data = rpipe->pipe_map.cnt;
1282 if ((rpipe->pipe_state & PIPE_EOF) ||
1283 (wpipe == NULL) || (wpipe->pipe_state & PIPE_EOF)) {
1284 kn->kn_flags |= EV_EOF;
1287 return (kn->kn_data > 0);
1292 filt_pipewrite(struct knote *kn, long hint)
1294 struct pipe *rpipe = (struct pipe *)kn->kn_fp->f_data;
1295 struct pipe *wpipe = rpipe->pipe_peer;
1297 if ((wpipe == NULL) || (wpipe->pipe_state & PIPE_EOF)) {
1299 kn->kn_flags |= EV_EOF;
1302 kn->kn_data = wpipe->pipe_buffer.size - wpipe->pipe_buffer.cnt;
1303 if (wpipe->pipe_state & PIPE_DIRECTW)
1306 return (kn->kn_data >= PIPE_BUF);