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.44 2006/12/28 21:24:01 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>
75 #include <sys/socket.h>
78 #include <vm/vm_param.h>
80 #include <vm/vm_object.h>
81 #include <vm/vm_kern.h>
82 #include <vm/vm_extern.h>
84 #include <vm/vm_map.h>
85 #include <vm/vm_page.h>
86 #include <vm/vm_zone.h>
88 #include <sys/file2.h>
90 #include <machine/cpufunc.h>
93 * interfaces to the outside world
95 static int pipe_read (struct file
*fp
, struct uio
*uio
,
96 struct ucred
*cred
, int flags
);
97 static int pipe_write (struct file
*fp
, struct uio
*uio
,
98 struct ucred
*cred
, int flags
);
99 static int pipe_close (struct file
*fp
);
100 static int pipe_shutdown (struct file
*fp
, int how
);
101 static int pipe_poll (struct file
*fp
, int events
, struct ucred
*cred
);
102 static int pipe_kqfilter (struct file
*fp
, struct knote
*kn
);
103 static int pipe_stat (struct file
*fp
, struct stat
*sb
, struct ucred
*cred
);
104 static int pipe_ioctl (struct file
*fp
, u_long cmd
, caddr_t data
, struct ucred
*cred
);
106 static struct fileops pipeops
= {
107 .fo_read
= pipe_read
,
108 .fo_write
= pipe_write
,
109 .fo_ioctl
= pipe_ioctl
,
110 .fo_poll
= pipe_poll
,
111 .fo_kqfilter
= pipe_kqfilter
,
112 .fo_stat
= pipe_stat
,
113 .fo_close
= pipe_close
,
114 .fo_shutdown
= pipe_shutdown
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
;
160 static int pipe_dwrite_enable
= 1; /* 0:copy, 1:kmem/sfbuf 2:force */
161 static int pipe_dwrite_sfbuf
= 1; /* 0:kmem_map 1:sfbufs 2:sfbufs_dmap */
162 /* 3:sfbuf_dmap w/ forced invlpg */
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
, maxcache
,
168 CTLFLAG_RW
, &pipe_maxcache
, 0, "max pipes cached per-cpu");
169 SYSCTL_INT(_kern_pipe
, OID_AUTO
, maxbig
,
170 CTLFLAG_RW
, &pipe_maxbig
, 0, "max number of big pipes");
171 SYSCTL_INT(_kern_pipe
, OID_AUTO
, dwrite_enable
,
172 CTLFLAG_RW
, &pipe_dwrite_enable
, 0, "1:enable/2:force direct writes");
173 SYSCTL_INT(_kern_pipe
, OID_AUTO
, dwrite_sfbuf
,
174 CTLFLAG_RW
, &pipe_dwrite_sfbuf
, 0,
175 "(if dwrite_enable) 0:kmem 1:sfbuf 2:sfbuf_dmap 3:sfbuf_dmap_forceinvlpg");
176 #if !defined(NO_PIPE_SYSCTL_STATS)
177 SYSCTL_INT(_kern_pipe
, OID_AUTO
, bcache_alloc
,
178 CTLFLAG_RW
, &pipe_bcache_alloc
, 0, "pipe buffer from pcpu cache");
179 SYSCTL_INT(_kern_pipe
, OID_AUTO
, bkmem_alloc
,
180 CTLFLAG_RW
, &pipe_bkmem_alloc
, 0, "pipe buffer from kmem");
183 static void pipeclose (struct pipe
*cpipe
);
184 static void pipe_free_kmem (struct pipe
*cpipe
);
185 static int pipe_create (struct pipe
**cpipep
);
186 static __inline
int pipelock (struct pipe
*cpipe
, int catch);
187 static __inline
void pipeunlock (struct pipe
*cpipe
);
188 static __inline
void pipeselwakeup (struct pipe
*cpipe
);
189 #ifndef PIPE_NODIRECT
190 static int pipe_build_write_buffer (struct pipe
*wpipe
, struct uio
*uio
);
191 static int pipe_direct_write (struct pipe
*wpipe
, struct uio
*uio
);
192 static void pipe_clone_write_buffer (struct pipe
*wpipe
);
194 static int pipespace (struct pipe
*cpipe
, int size
);
197 * The pipe system call for the DTYPE_PIPE type of pipes
199 * pipe_ARgs(int dummy)
204 sys_pipe(struct pipe_args
*uap
)
206 struct thread
*td
= curthread
;
207 struct proc
*p
= td
->td_proc
;
208 struct file
*rf
, *wf
;
209 struct pipe
*rpipe
, *wpipe
;
214 rpipe
= wpipe
= NULL
;
215 if (pipe_create(&rpipe
) || pipe_create(&wpipe
)) {
221 rpipe
->pipe_state
|= PIPE_DIRECTOK
;
222 wpipe
->pipe_state
|= PIPE_DIRECTOK
;
225 * Select the direct-map features to use for this pipe. Since the
226 * sysctl's can change on the fly we record the settings when the
229 * Generally speaking the system will default to what we consider
230 * to be the best-balanced and most stable option. Right now this
231 * is SFBUF1. Modes 2 and 3 are considered experiemental at the
234 wpipe
->pipe_feature
= PIPE_COPY
;
235 if (pipe_dwrite_enable
) {
236 switch(pipe_dwrite_sfbuf
) {
238 wpipe
->pipe_feature
= PIPE_KMEM
;
241 wpipe
->pipe_feature
= PIPE_SFBUF1
;
245 wpipe
->pipe_feature
= PIPE_SFBUF2
;
249 rpipe
->pipe_feature
= wpipe
->pipe_feature
;
251 error
= falloc(p
, &rf
, &fd1
);
257 uap
->sysmsg_fds
[0] = fd1
;
260 * Warning: once we've gotten past allocation of the fd for the
261 * read-side, we can only drop the read side via fdrop() in order
262 * to avoid races against processes which manage to dup() the read
263 * side while we are blocked trying to allocate the write side.
265 rf
->f_type
= DTYPE_PIPE
;
266 rf
->f_flag
= FREAD
| FWRITE
;
267 rf
->f_ops
= &pipeops
;
269 error
= falloc(p
, &wf
, &fd2
);
271 fsetfd(p
, NULL
, fd1
);
273 /* rpipe has been closed by fdrop(). */
277 wf
->f_type
= DTYPE_PIPE
;
278 wf
->f_flag
= FREAD
| FWRITE
;
279 wf
->f_ops
= &pipeops
;
281 uap
->sysmsg_fds
[1] = fd2
;
283 rpipe
->pipe_peer
= wpipe
;
284 wpipe
->pipe_peer
= rpipe
;
295 * Allocate kva for pipe circular buffer, the space is pageable
296 * This routine will 'realloc' the size of a pipe safely, if it fails
297 * it will retain the old buffer.
298 * If it fails it will return ENOMEM.
301 pipespace(struct pipe
*cpipe
, int size
)
303 struct vm_object
*object
;
307 npages
= round_page(size
) / PAGE_SIZE
;
308 object
= cpipe
->pipe_buffer
.object
;
311 * [re]create the object if necessary and reserve space for it
312 * in the kernel_map. The object and memory are pageable. On
313 * success, free the old resources before assigning the new
316 if (object
== NULL
|| object
->size
!= npages
) {
317 object
= vm_object_allocate(OBJT_DEFAULT
, npages
);
318 buffer
= (caddr_t
)vm_map_min(&kernel_map
);
320 error
= vm_map_find(&kernel_map
, object
, 0,
321 (vm_offset_t
*)&buffer
, size
,
324 VM_PROT_ALL
, VM_PROT_ALL
,
327 if (error
!= KERN_SUCCESS
) {
328 vm_object_deallocate(object
);
331 pipe_free_kmem(cpipe
);
332 cpipe
->pipe_buffer
.object
= object
;
333 cpipe
->pipe_buffer
.buffer
= buffer
;
334 cpipe
->pipe_buffer
.size
= size
;
339 cpipe
->pipe_buffer
.in
= 0;
340 cpipe
->pipe_buffer
.out
= 0;
341 cpipe
->pipe_buffer
.cnt
= 0;
346 * Initialize and allocate VM and memory for pipe, pulling the pipe from
347 * our per-cpu cache if possible. For now make sure it is sized for the
348 * smaller PIPE_SIZE default.
351 pipe_create(struct pipe
**cpipep
)
353 globaldata_t gd
= mycpu
;
357 if ((cpipe
= gd
->gd_pipeq
) != NULL
) {
358 gd
->gd_pipeq
= cpipe
->pipe_peer
;
360 cpipe
->pipe_peer
= NULL
;
362 cpipe
= kmalloc(sizeof(struct pipe
), M_PIPE
, M_WAITOK
|M_ZERO
);
365 if ((error
= pipespace(cpipe
, PIPE_SIZE
)) != 0)
367 vfs_timestamp(&cpipe
->pipe_ctime
);
368 cpipe
->pipe_atime
= cpipe
->pipe_ctime
;
369 cpipe
->pipe_mtime
= cpipe
->pipe_ctime
;
375 * lock a pipe for I/O, blocking other access
378 pipelock(struct pipe
*cpipe
, int catch)
382 while (cpipe
->pipe_state
& PIPE_LOCK
) {
383 cpipe
->pipe_state
|= PIPE_LWANT
;
384 error
= tsleep(cpipe
, (catch ? PCATCH
: 0), "pipelk", 0);
388 cpipe
->pipe_state
|= PIPE_LOCK
;
393 * unlock a pipe I/O lock
396 pipeunlock(struct pipe
*cpipe
)
399 cpipe
->pipe_state
&= ~PIPE_LOCK
;
400 if (cpipe
->pipe_state
& PIPE_LWANT
) {
401 cpipe
->pipe_state
&= ~PIPE_LWANT
;
407 pipeselwakeup(struct pipe
*cpipe
)
410 if (cpipe
->pipe_state
& PIPE_SEL
) {
411 cpipe
->pipe_state
&= ~PIPE_SEL
;
412 selwakeup(&cpipe
->pipe_sel
);
414 if ((cpipe
->pipe_state
& PIPE_ASYNC
) && cpipe
->pipe_sigio
)
415 pgsigio(cpipe
->pipe_sigio
, SIGIO
, 0);
416 KNOTE(&cpipe
->pipe_sel
.si_note
, 0);
420 * MPALMOSTSAFE (acquires mplock)
423 pipe_read(struct file
*fp
, struct uio
*uio
, struct ucred
*cred
, int fflags
)
432 rpipe
= (struct pipe
*) fp
->f_data
;
434 error
= pipelock(rpipe
, 1);
438 if (fflags
& O_FBLOCKING
)
440 else if (fflags
& O_FNONBLOCKING
)
442 else if (fp
->f_flag
& O_NONBLOCK
)
447 while (uio
->uio_resid
) {
450 if (rpipe
->pipe_buffer
.cnt
> 0) {
452 * normal pipe buffer receive
454 size
= rpipe
->pipe_buffer
.size
- rpipe
->pipe_buffer
.out
;
455 if (size
> rpipe
->pipe_buffer
.cnt
)
456 size
= rpipe
->pipe_buffer
.cnt
;
457 if (size
> (u_int
) uio
->uio_resid
)
458 size
= (u_int
) uio
->uio_resid
;
460 error
= uiomove(&rpipe
->pipe_buffer
.buffer
[rpipe
->pipe_buffer
.out
],
465 rpipe
->pipe_buffer
.out
+= size
;
466 if (rpipe
->pipe_buffer
.out
>= rpipe
->pipe_buffer
.size
)
467 rpipe
->pipe_buffer
.out
= 0;
469 rpipe
->pipe_buffer
.cnt
-= size
;
472 * If there is no more to read in the pipe, reset
473 * its pointers to the beginning. This improves
476 if (rpipe
->pipe_buffer
.cnt
== 0) {
477 rpipe
->pipe_buffer
.in
= 0;
478 rpipe
->pipe_buffer
.out
= 0;
481 #ifndef PIPE_NODIRECT
482 } else if (rpipe
->pipe_kva
&&
483 rpipe
->pipe_feature
== PIPE_KMEM
&&
484 (rpipe
->pipe_state
& (PIPE_DIRECTW
|PIPE_DIRECTIP
))
488 * Direct copy using source-side kva mapping
490 size
= rpipe
->pipe_map
.xio_bytes
-
491 rpipe
->pipe_buffer
.out
;
492 if (size
> (u_int
)uio
->uio_resid
)
493 size
= (u_int
)uio
->uio_resid
;
494 va
= (caddr_t
)rpipe
->pipe_kva
+
495 xio_kvaoffset(&rpipe
->pipe_map
, rpipe
->pipe_buffer
.out
);
496 error
= uiomove(va
, size
, uio
);
500 rpipe
->pipe_buffer
.out
+= size
;
501 if (rpipe
->pipe_buffer
.out
== rpipe
->pipe_map
.xio_bytes
) {
502 rpipe
->pipe_state
|= PIPE_DIRECTIP
;
503 rpipe
->pipe_state
&= ~PIPE_DIRECTW
;
504 /* reset out index for copy mode */
505 rpipe
->pipe_buffer
.out
= 0;
508 } else if (rpipe
->pipe_buffer
.out
!= rpipe
->pipe_map
.xio_bytes
&&
510 rpipe
->pipe_feature
== PIPE_SFBUF2
&&
511 (rpipe
->pipe_state
& (PIPE_DIRECTW
|PIPE_DIRECTIP
))
515 * Direct copy, bypassing a kernel buffer. We cannot
516 * mess with the direct-write buffer until
517 * PIPE_DIRECTIP is cleared. In order to prevent
518 * the pipe_write code from racing itself in
519 * direct_write, we set DIRECTIP when we clear
520 * DIRECTW after we have exhausted the buffer.
522 if (pipe_dwrite_sfbuf
== 3)
523 rpipe
->pipe_kvamask
= 0;
524 pmap_qenter2(rpipe
->pipe_kva
, rpipe
->pipe_map
.xio_pages
,
525 rpipe
->pipe_map
.xio_npages
,
526 &rpipe
->pipe_kvamask
);
527 size
= rpipe
->pipe_map
.xio_bytes
-
528 rpipe
->pipe_buffer
.out
;
529 if (size
> (u_int
)uio
->uio_resid
)
530 size
= (u_int
)uio
->uio_resid
;
531 va
= (caddr_t
)rpipe
->pipe_kva
+ xio_kvaoffset(&rpipe
->pipe_map
, rpipe
->pipe_buffer
.out
);
532 error
= uiomove(va
, size
, uio
);
536 rpipe
->pipe_buffer
.out
+= size
;
537 if (rpipe
->pipe_buffer
.out
== rpipe
->pipe_map
.xio_bytes
) {
538 rpipe
->pipe_state
|= PIPE_DIRECTIP
;
539 rpipe
->pipe_state
&= ~PIPE_DIRECTW
;
540 /* reset out index for copy mode */
541 rpipe
->pipe_buffer
.out
= 0;
544 } else if (rpipe
->pipe_buffer
.out
!= rpipe
->pipe_map
.xio_bytes
&&
545 rpipe
->pipe_feature
== PIPE_SFBUF1
&&
546 (rpipe
->pipe_state
& (PIPE_DIRECTW
|PIPE_DIRECTIP
))
550 * Direct copy, bypassing a kernel buffer. We cannot
551 * mess with the direct-write buffer until
552 * PIPE_DIRECTIP is cleared. In order to prevent
553 * the pipe_write code from racing itself in
554 * direct_write, we set DIRECTIP when we clear
555 * DIRECTW after we have exhausted the buffer.
557 error
= xio_uio_copy(&rpipe
->pipe_map
, rpipe
->pipe_buffer
.out
, uio
, &size
);
561 rpipe
->pipe_buffer
.out
+= size
;
562 if (rpipe
->pipe_buffer
.out
== rpipe
->pipe_map
.xio_bytes
) {
563 rpipe
->pipe_state
|= PIPE_DIRECTIP
;
564 rpipe
->pipe_state
&= ~PIPE_DIRECTW
;
565 /* reset out index for copy mode */
566 rpipe
->pipe_buffer
.out
= 0;
572 * detect EOF condition
573 * read returns 0 on EOF, no need to set error
575 if (rpipe
->pipe_state
& PIPE_EOF
)
579 * If the "write-side" has been blocked, wake it up now.
581 if (rpipe
->pipe_state
& PIPE_WANTW
) {
582 rpipe
->pipe_state
&= ~PIPE_WANTW
;
587 * Break if some data was read.
593 * Unlock the pipe buffer for our remaining
594 * processing. We will either break out with an
595 * error or we will sleep and relock to loop.
600 * Handle non-blocking mode operation or
601 * wait for more data.
606 rpipe
->pipe_state
|= PIPE_WANTR
;
607 if ((error
= tsleep(rpipe
, PCATCH
|PNORESCHED
,
608 "piperd", 0)) == 0) {
609 error
= pipelock(rpipe
, 1);
619 vfs_timestamp(&rpipe
->pipe_atime
);
624 * PIPE_WANT processing only makes sense if pipe_busy is 0.
626 if ((rpipe
->pipe_busy
== 0) && (rpipe
->pipe_state
& PIPE_WANT
)) {
627 rpipe
->pipe_state
&= ~(PIPE_WANT
|PIPE_WANTW
);
629 } else if (rpipe
->pipe_buffer
.cnt
< MINPIPESIZE
) {
631 * Handle write blocking hysteresis.
633 if (rpipe
->pipe_state
& PIPE_WANTW
) {
634 rpipe
->pipe_state
&= ~PIPE_WANTW
;
639 if ((rpipe
->pipe_buffer
.size
- rpipe
->pipe_buffer
.cnt
) >= PIPE_BUF
)
640 pipeselwakeup(rpipe
);
645 #ifndef PIPE_NODIRECT
647 * Map the sending processes' buffer into kernel space and wire it.
648 * This is similar to a physical write operation.
651 pipe_build_write_buffer(struct pipe
*wpipe
, struct uio
*uio
)
656 size
= (u_int
) uio
->uio_iov
->iov_len
;
657 if (size
> wpipe
->pipe_buffer
.size
)
658 size
= wpipe
->pipe_buffer
.size
;
660 if (uio
->uio_segflg
== UIO_SYSSPACE
) {
661 error
= xio_init_kbuf(&wpipe
->pipe_map
, uio
->uio_iov
->iov_base
,
664 error
= xio_init_ubuf(&wpipe
->pipe_map
, uio
->uio_iov
->iov_base
,
667 wpipe
->pipe_buffer
.out
= 0;
672 * Create a kernel map for KMEM and SFBUF2 copy modes. SFBUF2 will
673 * map the pages on the target while KMEM maps the pages now.
675 switch(wpipe
->pipe_feature
) {
678 if (wpipe
->pipe_kva
== NULL
) {
680 kmem_alloc_nofault(&kernel_map
, XIO_INTERNAL_SIZE
);
681 wpipe
->pipe_kvamask
= 0;
683 if (wpipe
->pipe_feature
== PIPE_KMEM
) {
684 pmap_qenter(wpipe
->pipe_kva
, wpipe
->pipe_map
.xio_pages
,
685 wpipe
->pipe_map
.xio_npages
);
693 * And update the uio data. The XIO might have loaded fewer bytes
694 * then requested so reload 'size'.
696 size
= wpipe
->pipe_map
.xio_bytes
;
697 uio
->uio_iov
->iov_len
-= size
;
698 uio
->uio_iov
->iov_base
+= size
;
699 if (uio
->uio_iov
->iov_len
== 0)
701 uio
->uio_resid
-= size
;
702 uio
->uio_offset
+= size
;
707 * In the case of a signal, the writing process might go away. This
708 * code copies the data into the circular buffer so that the source
709 * pages can be freed without loss of data.
711 * Note that in direct mode pipe_buffer.out is used to track the
712 * XIO offset. We are converting the direct mode into buffered mode
713 * which changes the meaning of pipe_buffer.out.
716 pipe_clone_write_buffer(struct pipe
*wpipe
)
721 offset
= wpipe
->pipe_buffer
.out
;
722 size
= wpipe
->pipe_map
.xio_bytes
- offset
;
724 KKASSERT(size
<= wpipe
->pipe_buffer
.size
);
726 wpipe
->pipe_buffer
.in
= size
;
727 wpipe
->pipe_buffer
.out
= 0;
728 wpipe
->pipe_buffer
.cnt
= size
;
729 wpipe
->pipe_state
&= ~(PIPE_DIRECTW
| PIPE_DIRECTIP
);
731 xio_copy_xtok(&wpipe
->pipe_map
, offset
, wpipe
->pipe_buffer
.buffer
, size
);
732 xio_release(&wpipe
->pipe_map
);
733 if (wpipe
->pipe_kva
) {
734 pmap_qremove(wpipe
->pipe_kva
, XIO_INTERNAL_PAGES
);
735 kmem_free(&kernel_map
, wpipe
->pipe_kva
, XIO_INTERNAL_SIZE
);
736 wpipe
->pipe_kva
= NULL
;
741 * This implements the pipe buffer write mechanism. Note that only
742 * a direct write OR a normal pipe write can be pending at any given time.
743 * If there are any characters in the pipe buffer, the direct write will
744 * be deferred until the receiving process grabs all of the bytes from
745 * the pipe buffer. Then the direct mapping write is set-up.
748 pipe_direct_write(struct pipe
*wpipe
, struct uio
*uio
)
753 while (wpipe
->pipe_state
& (PIPE_DIRECTW
|PIPE_DIRECTIP
)) {
754 if (wpipe
->pipe_state
& PIPE_WANTR
) {
755 wpipe
->pipe_state
&= ~PIPE_WANTR
;
758 wpipe
->pipe_state
|= PIPE_WANTW
;
759 error
= tsleep(wpipe
, PCATCH
, "pipdww", 0);
762 if (wpipe
->pipe_state
& PIPE_EOF
) {
767 KKASSERT(wpipe
->pipe_map
.xio_bytes
== 0);
768 if (wpipe
->pipe_buffer
.cnt
> 0) {
769 if (wpipe
->pipe_state
& PIPE_WANTR
) {
770 wpipe
->pipe_state
&= ~PIPE_WANTR
;
774 wpipe
->pipe_state
|= PIPE_WANTW
;
775 error
= tsleep(wpipe
, PCATCH
, "pipdwc", 0);
778 if (wpipe
->pipe_state
& PIPE_EOF
) {
786 * Build our direct-write buffer
788 wpipe
->pipe_state
|= PIPE_DIRECTW
| PIPE_DIRECTIP
;
789 error
= pipe_build_write_buffer(wpipe
, uio
);
792 wpipe
->pipe_state
&= ~PIPE_DIRECTIP
;
795 * Wait until the receiver has snarfed the data. Since we are likely
796 * going to sleep we optimize the case and yield synchronously,
797 * possibly avoiding the tsleep().
800 while (!error
&& (wpipe
->pipe_state
& PIPE_DIRECTW
)) {
801 if (wpipe
->pipe_state
& PIPE_EOF
) {
803 xio_release(&wpipe
->pipe_map
);
804 if (wpipe
->pipe_kva
) {
805 pmap_qremove(wpipe
->pipe_kva
, XIO_INTERNAL_PAGES
);
806 kmem_free(&kernel_map
, wpipe
->pipe_kva
, XIO_INTERNAL_SIZE
);
807 wpipe
->pipe_kva
= NULL
;
810 pipeselwakeup(wpipe
);
814 if (wpipe
->pipe_state
& PIPE_WANTR
) {
815 wpipe
->pipe_state
&= ~PIPE_WANTR
;
818 pipeselwakeup(wpipe
);
819 error
= tsleep(wpipe
, PCATCH
|PNORESCHED
, "pipdwt", 0);
822 if (wpipe
->pipe_state
& PIPE_DIRECTW
) {
824 * this bit of trickery substitutes a kernel buffer for
825 * the process that might be going away.
827 pipe_clone_write_buffer(wpipe
);
828 KKASSERT((wpipe
->pipe_state
& PIPE_DIRECTIP
) == 0);
831 * note: The pipe_kva mapping is not qremove'd here. For
832 * legacy PIPE_KMEM mode this constitutes an improvement
833 * over the original FreeBSD-4 algorithm. For PIPE_SFBUF2
834 * mode the kva mapping must not be removed to get the
837 * For testing purposes we will give the original algorithm
838 * the benefit of the doubt 'what it could have been', and
839 * keep the optimization.
841 KKASSERT(wpipe
->pipe_state
& PIPE_DIRECTIP
);
842 xio_release(&wpipe
->pipe_map
);
843 wpipe
->pipe_state
&= ~PIPE_DIRECTIP
;
849 * Direct-write error, clear the direct write flags.
852 wpipe
->pipe_state
&= ~(PIPE_DIRECTW
| PIPE_DIRECTIP
);
856 * General error, wakeup the other side if it happens to be sleeping.
865 * MPALMOSTSAFE - acquires mplock
868 pipe_write(struct file
*fp
, struct uio
*uio
, struct ucred
*cred
, int fflags
)
873 struct pipe
*wpipe
, *rpipe
;
876 rpipe
= (struct pipe
*) fp
->f_data
;
877 wpipe
= rpipe
->pipe_peer
;
880 * detect loss of pipe read side, issue SIGPIPE if lost.
882 if ((wpipe
== NULL
) || (wpipe
->pipe_state
& PIPE_EOF
)) {
888 if (fflags
& O_FBLOCKING
)
890 else if (fflags
& O_FNONBLOCKING
)
892 else if (fp
->f_flag
& O_NONBLOCK
)
898 * If it is advantageous to resize the pipe buffer, do
901 if ((uio
->uio_resid
> PIPE_SIZE
) &&
902 (pipe_nbig
< pipe_maxbig
) &&
903 (wpipe
->pipe_state
& (PIPE_DIRECTW
|PIPE_DIRECTIP
)) == 0 &&
904 (wpipe
->pipe_buffer
.size
<= PIPE_SIZE
) &&
905 (wpipe
->pipe_buffer
.cnt
== 0)) {
907 if ((error
= pipelock(wpipe
,1)) == 0) {
908 if (pipespace(wpipe
, BIG_PIPE_SIZE
) == 0)
915 * If an early error occured unbusy and return, waking up any pending
920 if ((wpipe
->pipe_busy
== 0) &&
921 (wpipe
->pipe_state
& PIPE_WANT
)) {
922 wpipe
->pipe_state
&= ~(PIPE_WANT
| PIPE_WANTR
);
929 KASSERT(wpipe
->pipe_buffer
.buffer
!= NULL
, ("pipe buffer gone"));
931 orig_resid
= uio
->uio_resid
;
933 while (uio
->uio_resid
) {
936 #ifndef PIPE_NODIRECT
938 * If the transfer is large, we can gain performance if
939 * we do process-to-process copies directly.
940 * If the write is non-blocking, we don't use the
941 * direct write mechanism.
943 * The direct write mechanism will detect the reader going
946 if ((uio
->uio_iov
->iov_len
>= PIPE_MINDIRECT
||
947 pipe_dwrite_enable
> 1) &&
949 pipe_dwrite_enable
) {
950 error
= pipe_direct_write( wpipe
, uio
);
958 * Pipe buffered writes cannot be coincidental with
959 * direct writes. We wait until the currently executing
960 * direct write is completed before we start filling the
961 * pipe buffer. We break out if a signal occurs or the
965 while (wpipe
->pipe_state
& (PIPE_DIRECTW
|PIPE_DIRECTIP
)) {
966 if (wpipe
->pipe_state
& PIPE_WANTR
) {
967 wpipe
->pipe_state
&= ~PIPE_WANTR
;
970 error
= tsleep(wpipe
, PCATCH
, "pipbww", 0);
971 if (wpipe
->pipe_state
& PIPE_EOF
)
976 if (wpipe
->pipe_state
& PIPE_EOF
) {
981 space
= wpipe
->pipe_buffer
.size
- wpipe
->pipe_buffer
.cnt
;
983 /* Writes of size <= PIPE_BUF must be atomic. */
984 if ((space
< uio
->uio_resid
) && (orig_resid
<= PIPE_BUF
))
988 * Write to fill, read size handles write hysteresis. Also
989 * additional restrictions can cause select-based non-blocking
993 if ((error
= pipelock(wpipe
,1)) == 0) {
994 int size
; /* Transfer size */
995 int segsize
; /* first segment to transfer */
998 * It is possible for a direct write to
999 * slip in on us... handle it here...
1001 if (wpipe
->pipe_state
& (PIPE_DIRECTW
|PIPE_DIRECTIP
)) {
1006 * If a process blocked in uiomove, our
1007 * value for space might be bad.
1009 * XXX will we be ok if the reader has gone
1012 if (space
> wpipe
->pipe_buffer
.size
-
1013 wpipe
->pipe_buffer
.cnt
) {
1019 * Transfer size is minimum of uio transfer
1020 * and free space in pipe buffer.
1022 if (space
> uio
->uio_resid
)
1023 size
= uio
->uio_resid
;
1027 * First segment to transfer is minimum of
1028 * transfer size and contiguous space in
1029 * pipe buffer. If first segment to transfer
1030 * is less than the transfer size, we've got
1031 * a wraparound in the buffer.
1033 segsize
= wpipe
->pipe_buffer
.size
-
1034 wpipe
->pipe_buffer
.in
;
1038 /* Transfer first segment */
1040 error
= uiomove(&wpipe
->pipe_buffer
.buffer
[wpipe
->pipe_buffer
.in
],
1043 if (error
== 0 && segsize
< size
) {
1045 * Transfer remaining part now, to
1046 * support atomic writes. Wraparound
1049 if (wpipe
->pipe_buffer
.in
+ segsize
!=
1050 wpipe
->pipe_buffer
.size
)
1051 panic("Expected pipe buffer wraparound disappeared");
1053 error
= uiomove(&wpipe
->pipe_buffer
.buffer
[0],
1054 size
- segsize
, uio
);
1057 wpipe
->pipe_buffer
.in
+= size
;
1058 if (wpipe
->pipe_buffer
.in
>=
1059 wpipe
->pipe_buffer
.size
) {
1060 if (wpipe
->pipe_buffer
.in
!= size
- segsize
+ wpipe
->pipe_buffer
.size
)
1061 panic("Expected wraparound bad");
1062 wpipe
->pipe_buffer
.in
= size
- segsize
;
1065 wpipe
->pipe_buffer
.cnt
+= size
;
1066 if (wpipe
->pipe_buffer
.cnt
> wpipe
->pipe_buffer
.size
)
1067 panic("Pipe buffer overflow");
1077 * If the "read-side" has been blocked, wake it up now
1078 * and yield to let it drain synchronously rather
1081 if (wpipe
->pipe_state
& PIPE_WANTR
) {
1082 wpipe
->pipe_state
&= ~PIPE_WANTR
;
1087 * don't block on non-blocking I/O
1095 * We have no more space and have something to offer,
1096 * wake up select/poll.
1098 pipeselwakeup(wpipe
);
1100 wpipe
->pipe_state
|= PIPE_WANTW
;
1101 error
= tsleep(wpipe
, PCATCH
|PNORESCHED
, "pipewr", 0);
1105 * If read side wants to go away, we just issue a signal
1108 if (wpipe
->pipe_state
& PIPE_EOF
) {
1117 if ((wpipe
->pipe_busy
== 0) && (wpipe
->pipe_state
& PIPE_WANT
)) {
1118 wpipe
->pipe_state
&= ~(PIPE_WANT
| PIPE_WANTR
);
1120 } else if (wpipe
->pipe_buffer
.cnt
> 0) {
1122 * If we have put any characters in the buffer, we wake up
1125 if (wpipe
->pipe_state
& PIPE_WANTR
) {
1126 wpipe
->pipe_state
&= ~PIPE_WANTR
;
1132 * Don't return EPIPE if I/O was successful
1134 if ((wpipe
->pipe_buffer
.cnt
== 0) &&
1135 (uio
->uio_resid
== 0) &&
1141 vfs_timestamp(&wpipe
->pipe_mtime
);
1144 * We have something to offer,
1145 * wake up select/poll.
1147 if (wpipe
->pipe_buffer
.cnt
)
1148 pipeselwakeup(wpipe
);
1154 * MPALMOSTSAFE - acquires mplock
1156 * we implement a very minimal set of ioctls for compatibility with sockets.
1159 pipe_ioctl(struct file
*fp
, u_long cmd
, caddr_t data
, struct ucred
*cred
)
1165 mpipe
= (struct pipe
*)fp
->f_data
;
1170 mpipe
->pipe_state
|= PIPE_ASYNC
;
1172 mpipe
->pipe_state
&= ~PIPE_ASYNC
;
1177 if (mpipe
->pipe_state
& PIPE_DIRECTW
) {
1178 *(int *)data
= mpipe
->pipe_map
.xio_bytes
-
1179 mpipe
->pipe_buffer
.out
;
1181 *(int *)data
= mpipe
->pipe_buffer
.cnt
;
1186 error
= fsetown(*(int *)data
, &mpipe
->pipe_sigio
);
1189 *(int *)data
= fgetown(mpipe
->pipe_sigio
);
1193 /* This is deprecated, FIOSETOWN should be used instead. */
1194 error
= fsetown(-(*(int *)data
), &mpipe
->pipe_sigio
);
1198 /* This is deprecated, FIOGETOWN should be used instead. */
1199 *(int *)data
= -fgetown(mpipe
->pipe_sigio
);
1211 * MPALMOSTSAFE - acquires mplock
1214 pipe_poll(struct file
*fp
, int events
, struct ucred
*cred
)
1221 rpipe
= (struct pipe
*)fp
->f_data
;
1222 wpipe
= rpipe
->pipe_peer
;
1223 if (events
& (POLLIN
| POLLRDNORM
))
1224 if ((rpipe
->pipe_state
& PIPE_DIRECTW
) ||
1225 (rpipe
->pipe_buffer
.cnt
> 0) ||
1226 (rpipe
->pipe_state
& PIPE_EOF
))
1227 revents
|= events
& (POLLIN
| POLLRDNORM
);
1229 if (events
& (POLLOUT
| POLLWRNORM
))
1230 if (wpipe
== NULL
|| (wpipe
->pipe_state
& PIPE_EOF
) ||
1231 (((wpipe
->pipe_state
& PIPE_DIRECTW
) == 0) &&
1232 (wpipe
->pipe_buffer
.size
- wpipe
->pipe_buffer
.cnt
) >= PIPE_BUF
))
1233 revents
|= events
& (POLLOUT
| POLLWRNORM
);
1235 if ((rpipe
->pipe_state
& PIPE_EOF
) ||
1237 (wpipe
->pipe_state
& PIPE_EOF
))
1241 if (events
& (POLLIN
| POLLRDNORM
)) {
1242 selrecord(curthread
, &rpipe
->pipe_sel
);
1243 rpipe
->pipe_state
|= PIPE_SEL
;
1246 if (events
& (POLLOUT
| POLLWRNORM
)) {
1247 selrecord(curthread
, &wpipe
->pipe_sel
);
1248 wpipe
->pipe_state
|= PIPE_SEL
;
1256 * MPALMOSTSAFE - acquires mplock
1259 pipe_stat(struct file
*fp
, struct stat
*ub
, struct ucred
*cred
)
1264 pipe
= (struct pipe
*)fp
->f_data
;
1266 bzero((caddr_t
)ub
, sizeof(*ub
));
1267 ub
->st_mode
= S_IFIFO
;
1268 ub
->st_blksize
= pipe
->pipe_buffer
.size
;
1269 ub
->st_size
= pipe
->pipe_buffer
.cnt
;
1270 if (ub
->st_size
== 0 && (pipe
->pipe_state
& PIPE_DIRECTW
)) {
1271 ub
->st_size
= pipe
->pipe_map
.xio_bytes
-
1272 pipe
->pipe_buffer
.out
;
1274 ub
->st_blocks
= (ub
->st_size
+ ub
->st_blksize
- 1) / ub
->st_blksize
;
1275 ub
->st_atimespec
= pipe
->pipe_atime
;
1276 ub
->st_mtimespec
= pipe
->pipe_mtime
;
1277 ub
->st_ctimespec
= pipe
->pipe_ctime
;
1279 * Left as 0: st_dev, st_ino, st_nlink, st_uid, st_gid, st_rdev,
1281 * XXX (st_dev, st_ino) should be unique.
1288 * MPALMOSTSAFE - acquires mplock
1291 pipe_close(struct file
*fp
)
1293 struct pipe
*cpipe
= (struct pipe
*)fp
->f_data
;
1296 fp
->f_ops
= &badfileops
;
1298 funsetown(cpipe
->pipe_sigio
);
1305 * Shutdown one or both directions of a full-duplex pipe.
1307 * MPALMOSTSAFE - acquires mplock
1310 pipe_shutdown(struct file
*fp
, int how
)
1317 rpipe
= (struct pipe
*)fp
->f_data
;
1323 rpipe
->pipe_state
|= PIPE_EOF
;
1324 pipeselwakeup(rpipe
);
1325 if (rpipe
->pipe_busy
)
1333 if (rpipe
&& (wpipe
= rpipe
->pipe_peer
) != NULL
) {
1334 wpipe
->pipe_state
|= PIPE_EOF
;
1335 pipeselwakeup(wpipe
);
1336 if (wpipe
->pipe_busy
)
1346 pipe_free_kmem(struct pipe
*cpipe
)
1348 if (cpipe
->pipe_buffer
.buffer
!= NULL
) {
1349 if (cpipe
->pipe_buffer
.size
> PIPE_SIZE
)
1351 kmem_free(&kernel_map
,
1352 (vm_offset_t
)cpipe
->pipe_buffer
.buffer
,
1353 cpipe
->pipe_buffer
.size
);
1354 cpipe
->pipe_buffer
.buffer
= NULL
;
1355 cpipe
->pipe_buffer
.object
= NULL
;
1357 #ifndef PIPE_NODIRECT
1358 KKASSERT(cpipe
->pipe_map
.xio_bytes
== 0 &&
1359 cpipe
->pipe_map
.xio_offset
== 0 &&
1360 cpipe
->pipe_map
.xio_npages
== 0);
1368 pipeclose(struct pipe
*cpipe
)
1376 pipeselwakeup(cpipe
);
1379 * If the other side is blocked, wake it up saying that
1380 * we want to close it down.
1382 while (cpipe
->pipe_busy
) {
1384 cpipe
->pipe_state
|= PIPE_WANT
| PIPE_EOF
;
1385 tsleep(cpipe
, 0, "pipecl", 0);
1389 * Disconnect from peer
1391 if ((ppipe
= cpipe
->pipe_peer
) != NULL
) {
1392 pipeselwakeup(ppipe
);
1394 ppipe
->pipe_state
|= PIPE_EOF
;
1396 KNOTE(&ppipe
->pipe_sel
.si_note
, 0);
1397 ppipe
->pipe_peer
= NULL
;
1400 if (cpipe
->pipe_kva
) {
1401 pmap_qremove(cpipe
->pipe_kva
, XIO_INTERNAL_PAGES
);
1402 kmem_free(&kernel_map
, cpipe
->pipe_kva
, XIO_INTERNAL_SIZE
);
1403 cpipe
->pipe_kva
= NULL
;
1407 * free or cache resources
1410 if (gd
->gd_pipeqcount
>= pipe_maxcache
||
1411 cpipe
->pipe_buffer
.size
!= PIPE_SIZE
1413 pipe_free_kmem(cpipe
);
1414 kfree(cpipe
, M_PIPE
);
1416 KKASSERT(cpipe
->pipe_map
.xio_npages
== 0 &&
1417 cpipe
->pipe_map
.xio_bytes
== 0 &&
1418 cpipe
->pipe_map
.xio_offset
== 0);
1419 cpipe
->pipe_state
= 0;
1420 cpipe
->pipe_busy
= 0;
1421 cpipe
->pipe_peer
= gd
->gd_pipeq
;
1422 gd
->gd_pipeq
= cpipe
;
1423 ++gd
->gd_pipeqcount
;
1428 * MPALMOSTSAFE - acquires mplock
1431 pipe_kqfilter(struct file
*fp
, struct knote
*kn
)
1436 cpipe
= (struct pipe
*)kn
->kn_fp
->f_data
;
1438 switch (kn
->kn_filter
) {
1440 kn
->kn_fop
= &pipe_rfiltops
;
1443 kn
->kn_fop
= &pipe_wfiltops
;
1444 cpipe
= cpipe
->pipe_peer
;
1445 if (cpipe
== NULL
) {
1446 /* other end of pipe has been closed */
1454 kn
->kn_hook
= (caddr_t
)cpipe
;
1456 SLIST_INSERT_HEAD(&cpipe
->pipe_sel
.si_note
, kn
, kn_selnext
);
1462 filt_pipedetach(struct knote
*kn
)
1464 struct pipe
*cpipe
= (struct pipe
*)kn
->kn_hook
;
1466 SLIST_REMOVE(&cpipe
->pipe_sel
.si_note
, kn
, knote
, kn_selnext
);
1471 filt_piperead(struct knote
*kn
, long hint
)
1473 struct pipe
*rpipe
= (struct pipe
*)kn
->kn_fp
->f_data
;
1474 struct pipe
*wpipe
= rpipe
->pipe_peer
;
1476 kn
->kn_data
= rpipe
->pipe_buffer
.cnt
;
1477 if ((kn
->kn_data
== 0) && (rpipe
->pipe_state
& PIPE_DIRECTW
)) {
1478 kn
->kn_data
= rpipe
->pipe_map
.xio_bytes
-
1479 rpipe
->pipe_buffer
.out
;
1482 if ((rpipe
->pipe_state
& PIPE_EOF
) ||
1483 (wpipe
== NULL
) || (wpipe
->pipe_state
& PIPE_EOF
)) {
1484 kn
->kn_flags
|= EV_EOF
;
1487 return (kn
->kn_data
> 0);
1492 filt_pipewrite(struct knote
*kn
, long hint
)
1494 struct pipe
*rpipe
= (struct pipe
*)kn
->kn_fp
->f_data
;
1495 struct pipe
*wpipe
= rpipe
->pipe_peer
;
1497 if ((wpipe
== NULL
) || (wpipe
->pipe_state
& PIPE_EOF
)) {
1499 kn
->kn_flags
|= EV_EOF
;
1502 kn
->kn_data
= wpipe
->pipe_buffer
.size
- wpipe
->pipe_buffer
.cnt
;
1503 if (wpipe
->pipe_state
& PIPE_DIRECTW
)
1506 return (kn
->kn_data
>= PIPE_BUF
);