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.15 2004/03/28 08:25:48 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;
158 static int pipe_bcache_alloc;
159 static int pipe_bkmem_alloc;
161 SYSCTL_NODE(_kern, OID_AUTO, pipe, CTLFLAG_RW, 0, "Pipe operation");
162 SYSCTL_INT(_kern_pipe, OID_AUTO, nbig,
163 CTLFLAG_RD, &pipe_nbig, 0, "numer of big pipes allocated");
164 SYSCTL_INT(_kern_pipe, OID_AUTO, maxcache,
165 CTLFLAG_RW, &pipe_maxcache, 0, "max pipes cached per-cpu");
166 SYSCTL_INT(_kern_pipe, OID_AUTO, maxbig,
167 CTLFLAG_RW, &pipe_maxbig, 0, "max number of big pipes");
168 #if !defined(NO_PIPE_SYSCTL_STATS)
169 SYSCTL_INT(_kern_pipe, OID_AUTO, bcache_alloc,
170 CTLFLAG_RW, &pipe_bcache_alloc, 0, "pipe buffer from pcpu cache");
171 SYSCTL_INT(_kern_pipe, OID_AUTO, bkmem_alloc,
172 CTLFLAG_RW, &pipe_bkmem_alloc, 0, "pipe buffer from kmem");
175 static void pipeclose (struct pipe *cpipe);
176 static void pipe_free_kmem (struct pipe *cpipe);
177 static int pipe_create (struct pipe **cpipep);
178 static __inline int pipelock (struct pipe *cpipe, int catch);
179 static __inline void pipeunlock (struct pipe *cpipe);
180 static __inline void pipeselwakeup (struct pipe *cpipe);
181 #ifndef PIPE_NODIRECT
182 static int pipe_build_write_buffer (struct pipe *wpipe, struct uio *uio);
183 static void pipe_destroy_write_buffer (struct pipe *wpipe);
184 static int pipe_direct_write (struct pipe *wpipe, struct uio *uio);
185 static void pipe_clone_write_buffer (struct pipe *wpipe);
187 static int pipespace (struct pipe *cpipe, int size);
190 * The pipe system call for the DTYPE_PIPE type of pipes
192 * pipe_ARgs(int dummy)
197 pipe(struct pipe_args *uap)
199 struct thread *td = curthread;
200 struct proc *p = td->td_proc;
201 struct filedesc *fdp;
202 struct file *rf, *wf;
203 struct pipe *rpipe, *wpipe;
209 rpipe = wpipe = NULL;
210 if (pipe_create(&rpipe) || pipe_create(&wpipe)) {
216 rpipe->pipe_state |= PIPE_DIRECTOK;
217 wpipe->pipe_state |= PIPE_DIRECTOK;
219 error = falloc(p, &rf, &fd1);
226 uap->sysmsg_fds[0] = fd1;
229 * Warning: once we've gotten past allocation of the fd for the
230 * read-side, we can only drop the read side via fdrop() in order
231 * to avoid races against processes which manage to dup() the read
232 * side while we are blocked trying to allocate the write side.
234 rf->f_flag = FREAD | FWRITE;
235 rf->f_type = DTYPE_PIPE;
236 rf->f_data = (caddr_t)rpipe;
237 rf->f_ops = &pipeops;
238 error = falloc(p, &wf, &fd2);
240 if (fdp->fd_ofiles[fd1] == rf) {
241 fdp->fd_ofiles[fd1] = NULL;
245 /* rpipe has been closed by fdrop(). */
249 wf->f_flag = FREAD | FWRITE;
250 wf->f_type = DTYPE_PIPE;
251 wf->f_data = (caddr_t)wpipe;
252 wf->f_ops = &pipeops;
253 uap->sysmsg_fds[1] = fd2;
255 rpipe->pipe_peer = wpipe;
256 wpipe->pipe_peer = rpipe;
263 * Allocate kva for pipe circular buffer, the space is pageable
264 * This routine will 'realloc' the size of a pipe safely, if it fails
265 * it will retain the old buffer.
266 * If it fails it will return ENOMEM.
269 pipespace(struct pipe *cpipe, int size)
271 struct vm_object *object;
275 npages = round_page(size) / PAGE_SIZE;
276 object = cpipe->pipe_buffer.object;
279 * [re]create the object if necessary and reserve space for it
280 * in the kernel_map. The object and memory are pageable. On
281 * success, free the old resources before assigning the new
284 if (object == NULL || object->size != npages) {
285 object = vm_object_allocate(OBJT_DEFAULT, npages);
286 buffer = (caddr_t) vm_map_min(kernel_map);
288 error = vm_map_find(kernel_map, object, 0,
289 (vm_offset_t *) &buffer, size, 1,
290 VM_PROT_ALL, VM_PROT_ALL, 0);
292 if (error != KERN_SUCCESS) {
293 vm_object_deallocate(object);
296 pipe_free_kmem(cpipe);
297 cpipe->pipe_buffer.object = object;
298 cpipe->pipe_buffer.buffer = buffer;
299 cpipe->pipe_buffer.size = size;
304 cpipe->pipe_buffer.in = 0;
305 cpipe->pipe_buffer.out = 0;
306 cpipe->pipe_buffer.cnt = 0;
311 * Initialize and allocate VM and memory for pipe, pulling the pipe from
312 * our per-cpu cache if possible. For now make sure it is sized for the
313 * smaller PIPE_SIZE default.
317 struct pipe **cpipep;
319 globaldata_t gd = mycpu;
323 if ((cpipe = gd->gd_pipeq) != NULL) {
324 gd->gd_pipeq = cpipe->pipe_peer;
326 cpipe->pipe_peer = NULL;
328 cpipe = malloc(sizeof(struct pipe), M_PIPE, M_WAITOK|M_ZERO);
331 if ((error = pipespace(cpipe, PIPE_SIZE)) != 0)
333 vfs_timestamp(&cpipe->pipe_ctime);
334 cpipe->pipe_atime = cpipe->pipe_ctime;
335 cpipe->pipe_mtime = cpipe->pipe_ctime;
341 * lock a pipe for I/O, blocking other access
344 pipelock(cpipe, catch)
350 while (cpipe->pipe_state & PIPE_LOCK) {
351 cpipe->pipe_state |= PIPE_LWANT;
352 error = tsleep(cpipe, (catch ? PCATCH : 0), "pipelk", 0);
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(struct file *fp, struct uio *uio, struct ucred *cred,
392 int flags, struct thread *td)
394 struct pipe *rpipe = (struct pipe *) fp->f_data;
400 error = pipelock(rpipe, 1);
404 while (uio->uio_resid) {
406 * normal pipe buffer receive
408 if (rpipe->pipe_buffer.cnt > 0) {
409 size = rpipe->pipe_buffer.size - rpipe->pipe_buffer.out;
410 if (size > rpipe->pipe_buffer.cnt)
411 size = rpipe->pipe_buffer.cnt;
412 if (size > (u_int) uio->uio_resid)
413 size = (u_int) uio->uio_resid;
415 error = uiomove(&rpipe->pipe_buffer.buffer[rpipe->pipe_buffer.out],
420 rpipe->pipe_buffer.out += size;
421 if (rpipe->pipe_buffer.out >= rpipe->pipe_buffer.size)
422 rpipe->pipe_buffer.out = 0;
424 rpipe->pipe_buffer.cnt -= size;
427 * If there is no more to read in the pipe, reset
428 * its pointers to the beginning. This improves
431 if (rpipe->pipe_buffer.cnt == 0) {
432 rpipe->pipe_buffer.in = 0;
433 rpipe->pipe_buffer.out = 0;
436 #ifndef PIPE_NODIRECT
438 * Direct copy, bypassing a kernel buffer.
440 } else if ((size = rpipe->pipe_map.cnt) &&
441 (rpipe->pipe_state & PIPE_DIRECTW)) {
442 if (size > (u_int) uio->uio_resid)
443 size = (u_int) uio->uio_resid;
444 error = uiomove_fromphys(rpipe->pipe_map.ms,
445 rpipe->pipe_map.pos, size, uio);
449 rpipe->pipe_map.pos += size;
450 rpipe->pipe_map.cnt -= size;
451 if (rpipe->pipe_map.cnt == 0) {
452 rpipe->pipe_state &= ~PIPE_DIRECTW;
458 * detect EOF condition
459 * read returns 0 on EOF, no need to set error
461 if (rpipe->pipe_state & PIPE_EOF)
465 * If the "write-side" has been blocked, wake it up now.
467 if (rpipe->pipe_state & PIPE_WANTW) {
468 rpipe->pipe_state &= ~PIPE_WANTW;
473 * Break if some data was read.
479 * Unlock the pipe buffer for our remaining processing. We
480 * will either break out with an error or we will sleep and
486 * Handle non-blocking mode operation or
487 * wait for more data.
489 if (fp->f_flag & FNONBLOCK) {
492 rpipe->pipe_state |= PIPE_WANTR;
493 if ((error = tsleep(rpipe, PCATCH,
494 "piperd", 0)) == 0) {
495 error = pipelock(rpipe, 1);
505 vfs_timestamp(&rpipe->pipe_atime);
510 * PIPE_WANT processing only makes sense if pipe_busy is 0.
512 if ((rpipe->pipe_busy == 0) && (rpipe->pipe_state & PIPE_WANT)) {
513 rpipe->pipe_state &= ~(PIPE_WANT|PIPE_WANTW);
515 } else if (rpipe->pipe_buffer.cnt < MINPIPESIZE) {
517 * Handle write blocking hysteresis.
519 if (rpipe->pipe_state & PIPE_WANTW) {
520 rpipe->pipe_state &= ~PIPE_WANTW;
525 if ((rpipe->pipe_buffer.size - rpipe->pipe_buffer.cnt) >= PIPE_BUF)
526 pipeselwakeup(rpipe);
531 #ifndef PIPE_NODIRECT
533 * Map the sending processes' buffer into kernel space and wire it.
534 * This is similar to a physical write operation.
537 pipe_build_write_buffer(wpipe, uio)
543 vm_offset_t addr, endaddr;
546 size = (u_int) uio->uio_iov->iov_len;
547 if (size > wpipe->pipe_buffer.size)
548 size = wpipe->pipe_buffer.size;
550 endaddr = round_page((vm_offset_t)uio->uio_iov->iov_base + size);
551 addr = trunc_page((vm_offset_t)uio->uio_iov->iov_base);
552 for (i = 0; addr < endaddr; addr += PAGE_SIZE, i++) {
555 if (vm_fault_quick((caddr_t)addr, VM_PROT_READ) < 0 ||
556 (paddr = pmap_kextract(addr)) == 0) {
559 for (j = 0; j < i; j++)
560 vm_page_unhold(wpipe->pipe_map.ms[j]);
564 m = PHYS_TO_VM_PAGE(paddr);
566 wpipe->pipe_map.ms[i] = m;
570 * set up the control block
572 wpipe->pipe_map.npages = i;
573 wpipe->pipe_map.pos =
574 ((vm_offset_t) uio->uio_iov->iov_base) & PAGE_MASK;
575 wpipe->pipe_map.cnt = size;
578 * and update the uio data
580 uio->uio_iov->iov_len -= size;
581 uio->uio_iov->iov_base += size;
582 if (uio->uio_iov->iov_len == 0)
584 uio->uio_resid -= size;
585 uio->uio_offset += size;
590 * unmap and unwire the process buffer
593 pipe_destroy_write_buffer(wpipe)
598 for (i = 0; i < wpipe->pipe_map.npages; i++) {
599 vm_page_unhold(wpipe->pipe_map.ms[i]);
600 wpipe->pipe_map.ms[i] = NULL; /* sanity */
602 wpipe->pipe_map.npages = 0;
606 * In the case of a signal, the writing process might go away. This
607 * code copies the data into the circular buffer so that the source
608 * pages can be freed without loss of data.
611 pipe_clone_write_buffer(wpipe)
619 size = wpipe->pipe_map.cnt;
620 pos = wpipe->pipe_map.pos;
622 wpipe->pipe_buffer.in = size;
623 wpipe->pipe_buffer.out = 0;
624 wpipe->pipe_buffer.cnt = size;
625 wpipe->pipe_state &= ~PIPE_DIRECTW;
627 iov.iov_base = wpipe->pipe_buffer.buffer;
632 uio.uio_resid = size;
633 uio.uio_segflg = UIO_SYSSPACE;
634 uio.uio_rw = UIO_READ;
635 uio.uio_td = curthread;
636 uiomove_fromphys(wpipe->pipe_map.ms, pos, size, &uio);
638 pipe_destroy_write_buffer(wpipe);
642 * This implements the pipe buffer write mechanism. Note that only
643 * a direct write OR a normal pipe write can be pending at any given time.
644 * If there are any characters in the pipe buffer, the direct write will
645 * be deferred until the receiving process grabs all of the bytes from
646 * the pipe buffer. Then the direct mapping write is set-up.
649 pipe_direct_write(wpipe, uio)
656 while (wpipe->pipe_state & PIPE_DIRECTW) {
657 if (wpipe->pipe_state & PIPE_WANTR) {
658 wpipe->pipe_state &= ~PIPE_WANTR;
661 wpipe->pipe_state |= PIPE_WANTW;
662 error = tsleep(wpipe, PCATCH, "pipdww", 0);
665 if (wpipe->pipe_state & PIPE_EOF) {
670 wpipe->pipe_map.cnt = 0; /* transfer not ready yet */
671 if (wpipe->pipe_buffer.cnt > 0) {
672 if (wpipe->pipe_state & PIPE_WANTR) {
673 wpipe->pipe_state &= ~PIPE_WANTR;
677 wpipe->pipe_state |= PIPE_WANTW;
678 error = tsleep(wpipe, PCATCH, "pipdwc", 0);
681 if (wpipe->pipe_state & PIPE_EOF) {
689 * Build our direct-write buffer
691 wpipe->pipe_state |= PIPE_DIRECTW;
692 error = pipe_build_write_buffer(wpipe, uio);
694 wpipe->pipe_state &= ~PIPE_DIRECTW;
699 * Wait until the receiver has snarfed the data. Since we are likely
700 * going to sleep we optimize the case and yield synchronously,
701 * possibly avoiding the tsleep().
704 while (!error && (wpipe->pipe_state & PIPE_DIRECTW)) {
705 if (wpipe->pipe_state & PIPE_EOF) {
707 pipe_destroy_write_buffer(wpipe);
709 pipeselwakeup(wpipe);
713 if (wpipe->pipe_state & PIPE_WANTR) {
714 wpipe->pipe_state &= ~PIPE_WANTR;
717 pipeselwakeup(wpipe);
718 error = tsleep(wpipe, PCATCH, "pipdwt", 0);
722 if (wpipe->pipe_state & PIPE_DIRECTW) {
724 * this bit of trickery substitutes a kernel buffer for
725 * the process that might be going away.
727 pipe_clone_write_buffer(wpipe);
729 pipe_destroy_write_buffer(wpipe);
741 pipe_write(struct file *fp, struct uio *uio, struct ucred *cred,
742 int flags, struct thread *td)
746 struct pipe *wpipe, *rpipe;
748 rpipe = (struct pipe *) fp->f_data;
749 wpipe = rpipe->pipe_peer;
752 * detect loss of pipe read side, issue SIGPIPE if lost.
754 if ((wpipe == NULL) || (wpipe->pipe_state & PIPE_EOF)) {
760 * If it is advantageous to resize the pipe buffer, do
763 if ((uio->uio_resid > PIPE_SIZE) &&
764 (pipe_nbig < pipe_maxbig) &&
765 (wpipe->pipe_state & PIPE_DIRECTW) == 0 &&
766 (wpipe->pipe_buffer.size <= PIPE_SIZE) &&
767 (wpipe->pipe_buffer.cnt == 0)) {
769 if ((error = pipelock(wpipe,1)) == 0) {
770 if (pipespace(wpipe, BIG_PIPE_SIZE) == 0)
777 * If an early error occured unbusy and return, waking up any pending
782 if ((wpipe->pipe_busy == 0) &&
783 (wpipe->pipe_state & PIPE_WANT)) {
784 wpipe->pipe_state &= ~(PIPE_WANT | PIPE_WANTR);
790 KASSERT(wpipe->pipe_buffer.buffer != NULL, ("pipe buffer gone"));
792 orig_resid = uio->uio_resid;
794 while (uio->uio_resid) {
797 #ifndef PIPE_NODIRECT
799 * If the transfer is large, we can gain performance if
800 * we do process-to-process copies directly.
801 * If the write is non-blocking, we don't use the
802 * direct write mechanism.
804 * The direct write mechanism will detect the reader going
807 if ((uio->uio_iov->iov_len >= PIPE_MINDIRECT) &&
808 (fp->f_flag & FNONBLOCK) == 0) {
809 error = pipe_direct_write( wpipe, uio);
817 * Pipe buffered writes cannot be coincidental with
818 * direct writes. We wait until the currently executing
819 * direct write is completed before we start filling the
820 * pipe buffer. We break out if a signal occurs or the
824 while (wpipe->pipe_state & PIPE_DIRECTW) {
825 if (wpipe->pipe_state & PIPE_WANTR) {
826 wpipe->pipe_state &= ~PIPE_WANTR;
829 error = tsleep(wpipe, PCATCH, "pipbww", 0);
830 if (wpipe->pipe_state & PIPE_EOF)
835 if (wpipe->pipe_state & PIPE_EOF) {
840 space = wpipe->pipe_buffer.size - wpipe->pipe_buffer.cnt;
842 /* Writes of size <= PIPE_BUF must be atomic. */
843 if ((space < uio->uio_resid) && (orig_resid <= PIPE_BUF))
847 * Write to fill, read size handles write hysteresis. Also
848 * additional restrictions can cause select-based non-blocking
852 if ((error = pipelock(wpipe,1)) == 0) {
853 int size; /* Transfer size */
854 int segsize; /* first segment to transfer */
857 * It is possible for a direct write to
858 * slip in on us... handle it here...
860 if (wpipe->pipe_state & PIPE_DIRECTW) {
865 * If a process blocked in uiomove, our
866 * value for space might be bad.
868 * XXX will we be ok if the reader has gone
871 if (space > wpipe->pipe_buffer.size -
872 wpipe->pipe_buffer.cnt) {
878 * Transfer size is minimum of uio transfer
879 * and free space in pipe buffer.
881 if (space > uio->uio_resid)
882 size = uio->uio_resid;
886 * First segment to transfer is minimum of
887 * transfer size and contiguous space in
888 * pipe buffer. If first segment to transfer
889 * is less than the transfer size, we've got
890 * a wraparound in the buffer.
892 segsize = wpipe->pipe_buffer.size -
893 wpipe->pipe_buffer.in;
897 /* Transfer first segment */
899 error = uiomove(&wpipe->pipe_buffer.buffer[wpipe->pipe_buffer.in],
902 if (error == 0 && segsize < size) {
904 * Transfer remaining part now, to
905 * support atomic writes. Wraparound
908 if (wpipe->pipe_buffer.in + segsize !=
909 wpipe->pipe_buffer.size)
910 panic("Expected pipe buffer wraparound disappeared");
912 error = uiomove(&wpipe->pipe_buffer.buffer[0],
913 size - segsize, uio);
916 wpipe->pipe_buffer.in += size;
917 if (wpipe->pipe_buffer.in >=
918 wpipe->pipe_buffer.size) {
919 if (wpipe->pipe_buffer.in != size - segsize + wpipe->pipe_buffer.size)
920 panic("Expected wraparound bad");
921 wpipe->pipe_buffer.in = size - segsize;
924 wpipe->pipe_buffer.cnt += size;
925 if (wpipe->pipe_buffer.cnt > wpipe->pipe_buffer.size)
926 panic("Pipe buffer overflow");
936 * If the "read-side" has been blocked, wake it up now
937 * and yield to let it drain synchronously rather
940 if (wpipe->pipe_state & PIPE_WANTR) {
941 wpipe->pipe_state &= ~PIPE_WANTR;
946 * don't block on non-blocking I/O
948 if (fp->f_flag & FNONBLOCK) {
954 * We have no more space and have something to offer,
955 * wake up select/poll.
957 pipeselwakeup(wpipe);
959 wpipe->pipe_state |= PIPE_WANTW;
960 error = tsleep(wpipe, PCATCH, "pipewr", 0);
964 * If read side wants to go away, we just issue a signal
967 if (wpipe->pipe_state & PIPE_EOF) {
976 if ((wpipe->pipe_busy == 0) && (wpipe->pipe_state & PIPE_WANT)) {
977 wpipe->pipe_state &= ~(PIPE_WANT | PIPE_WANTR);
979 } else if (wpipe->pipe_buffer.cnt > 0) {
981 * If we have put any characters in the buffer, we wake up
984 if (wpipe->pipe_state & PIPE_WANTR) {
985 wpipe->pipe_state &= ~PIPE_WANTR;
991 * Don't return EPIPE if I/O was successful
993 if ((wpipe->pipe_buffer.cnt == 0) &&
994 (uio->uio_resid == 0) &&
1000 vfs_timestamp(&wpipe->pipe_mtime);
1003 * We have something to offer,
1004 * wake up select/poll.
1006 if (wpipe->pipe_buffer.cnt)
1007 pipeselwakeup(wpipe);
1013 * we implement a very minimal set of ioctls for compatibility with sockets.
1016 pipe_ioctl(struct file *fp, u_long cmd, caddr_t data, struct thread *td)
1018 struct pipe *mpipe = (struct pipe *)fp->f_data;
1027 mpipe->pipe_state |= PIPE_ASYNC;
1029 mpipe->pipe_state &= ~PIPE_ASYNC;
1034 if (mpipe->pipe_state & PIPE_DIRECTW)
1035 *(int *)data = mpipe->pipe_map.cnt;
1037 *(int *)data = mpipe->pipe_buffer.cnt;
1041 return (fsetown(*(int *)data, &mpipe->pipe_sigio));
1044 *(int *)data = fgetown(mpipe->pipe_sigio);
1047 /* This is deprecated, FIOSETOWN should be used instead. */
1049 return (fsetown(-(*(int *)data), &mpipe->pipe_sigio));
1051 /* This is deprecated, FIOGETOWN should be used instead. */
1053 *(int *)data = -fgetown(mpipe->pipe_sigio);
1061 pipe_poll(struct file *fp, int events, struct ucred *cred, struct thread *td)
1063 struct pipe *rpipe = (struct pipe *)fp->f_data;
1067 wpipe = rpipe->pipe_peer;
1068 if (events & (POLLIN | POLLRDNORM))
1069 if ((rpipe->pipe_state & PIPE_DIRECTW) ||
1070 (rpipe->pipe_buffer.cnt > 0) ||
1071 (rpipe->pipe_state & PIPE_EOF))
1072 revents |= events & (POLLIN | POLLRDNORM);
1074 if (events & (POLLOUT | POLLWRNORM))
1075 if (wpipe == NULL || (wpipe->pipe_state & PIPE_EOF) ||
1076 (((wpipe->pipe_state & PIPE_DIRECTW) == 0) &&
1077 (wpipe->pipe_buffer.size - wpipe->pipe_buffer.cnt) >= PIPE_BUF))
1078 revents |= events & (POLLOUT | POLLWRNORM);
1080 if ((rpipe->pipe_state & PIPE_EOF) ||
1082 (wpipe->pipe_state & PIPE_EOF))
1086 if (events & (POLLIN | POLLRDNORM)) {
1087 selrecord(td, &rpipe->pipe_sel);
1088 rpipe->pipe_state |= PIPE_SEL;
1091 if (events & (POLLOUT | POLLWRNORM)) {
1092 selrecord(td, &wpipe->pipe_sel);
1093 wpipe->pipe_state |= PIPE_SEL;
1101 pipe_stat(struct file *fp, struct stat *ub, struct thread *td)
1103 struct pipe *pipe = (struct pipe *)fp->f_data;
1105 bzero((caddr_t)ub, sizeof(*ub));
1106 ub->st_mode = S_IFIFO;
1107 ub->st_blksize = pipe->pipe_buffer.size;
1108 ub->st_size = pipe->pipe_buffer.cnt;
1109 ub->st_blocks = (ub->st_size + ub->st_blksize - 1) / ub->st_blksize;
1110 ub->st_atimespec = pipe->pipe_atime;
1111 ub->st_mtimespec = pipe->pipe_mtime;
1112 ub->st_ctimespec = pipe->pipe_ctime;
1114 * Left as 0: st_dev, st_ino, st_nlink, st_uid, st_gid, st_rdev,
1116 * XXX (st_dev, st_ino) should be unique.
1123 pipe_close(struct file *fp, struct thread *td)
1125 struct pipe *cpipe = (struct pipe *)fp->f_data;
1127 fp->f_ops = &badfileops;
1129 funsetown(cpipe->pipe_sigio);
1135 pipe_free_kmem(struct pipe *cpipe)
1137 if (cpipe->pipe_buffer.buffer != NULL) {
1138 if (cpipe->pipe_buffer.size > PIPE_SIZE)
1140 kmem_free(kernel_map,
1141 (vm_offset_t)cpipe->pipe_buffer.buffer,
1142 cpipe->pipe_buffer.size);
1143 cpipe->pipe_buffer.buffer = NULL;
1144 cpipe->pipe_buffer.object = NULL;
1146 #ifndef PIPE_NODIRECT
1147 cpipe->pipe_map.cnt = 0;
1148 cpipe->pipe_map.pos = 0;
1149 cpipe->pipe_map.npages = 0;
1157 pipeclose(struct pipe *cpipe)
1165 pipeselwakeup(cpipe);
1168 * If the other side is blocked, wake it up saying that
1169 * we want to close it down.
1171 while (cpipe->pipe_busy) {
1173 cpipe->pipe_state |= PIPE_WANT | PIPE_EOF;
1174 tsleep(cpipe, 0, "pipecl", 0);
1178 * Disconnect from peer
1180 if ((ppipe = cpipe->pipe_peer) != NULL) {
1181 pipeselwakeup(ppipe);
1183 ppipe->pipe_state |= PIPE_EOF;
1185 KNOTE(&ppipe->pipe_sel.si_note, 0);
1186 ppipe->pipe_peer = NULL;
1190 * free or cache resources
1193 if (gd->gd_pipeqcount >= pipe_maxcache ||
1194 cpipe->pipe_buffer.size != PIPE_SIZE
1196 pipe_free_kmem(cpipe);
1197 free(cpipe, M_PIPE);
1199 KKASSERT(cpipe->pipe_map.npages == 0);
1201 cpipe->pipe_state = 0;
1202 cpipe->pipe_busy = 0;
1203 cpipe->pipe_map.cnt = 0;
1204 cpipe->pipe_map.pos = 0;
1205 cpipe->pipe_peer = gd->gd_pipeq;
1206 gd->gd_pipeq = cpipe;
1207 ++gd->gd_pipeqcount;
1213 pipe_kqfilter(struct file *fp, struct knote *kn)
1215 struct pipe *cpipe = (struct pipe *)kn->kn_fp->f_data;
1217 switch (kn->kn_filter) {
1219 kn->kn_fop = &pipe_rfiltops;
1222 kn->kn_fop = &pipe_wfiltops;
1223 cpipe = cpipe->pipe_peer;
1225 /* other end of pipe has been closed */
1231 kn->kn_hook = (caddr_t)cpipe;
1233 SLIST_INSERT_HEAD(&cpipe->pipe_sel.si_note, kn, kn_selnext);
1238 filt_pipedetach(struct knote *kn)
1240 struct pipe *cpipe = (struct pipe *)kn->kn_hook;
1242 SLIST_REMOVE(&cpipe->pipe_sel.si_note, kn, knote, kn_selnext);
1247 filt_piperead(struct knote *kn, long hint)
1249 struct pipe *rpipe = (struct pipe *)kn->kn_fp->f_data;
1250 struct pipe *wpipe = rpipe->pipe_peer;
1252 kn->kn_data = rpipe->pipe_buffer.cnt;
1253 if ((kn->kn_data == 0) && (rpipe->pipe_state & PIPE_DIRECTW))
1254 kn->kn_data = rpipe->pipe_map.cnt;
1256 if ((rpipe->pipe_state & PIPE_EOF) ||
1257 (wpipe == NULL) || (wpipe->pipe_state & PIPE_EOF)) {
1258 kn->kn_flags |= EV_EOF;
1261 return (kn->kn_data > 0);
1266 filt_pipewrite(struct knote *kn, long hint)
1268 struct pipe *rpipe = (struct pipe *)kn->kn_fp->f_data;
1269 struct pipe *wpipe = rpipe->pipe_peer;
1271 if ((wpipe == NULL) || (wpipe->pipe_state & PIPE_EOF)) {
1273 kn->kn_flags |= EV_EOF;
1276 kn->kn_data = wpipe->pipe_buffer.size - wpipe->pipe_buffer.cnt;
1277 if (wpipe->pipe_state & PIPE_DIRECTW)
1280 return (kn->kn_data >= PIPE_BUF);