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.50 2008/09/09 04:06:13 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
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/kernel.h>
33 #include <sys/fcntl.h>
35 #include <sys/filedesc.h>
36 #include <sys/filio.h>
37 #include <sys/ttycom.h>
40 #include <sys/select.h>
41 #include <sys/signalvar.h>
42 #include <sys/sysproto.h>
44 #include <sys/vnode.h>
46 #include <sys/event.h>
47 #include <sys/globaldata.h>
48 #include <sys/module.h>
49 #include <sys/malloc.h>
50 #include <sys/sysctl.h>
51 #include <sys/socket.h>
54 #include <vm/vm_param.h>
56 #include <vm/vm_object.h>
57 #include <vm/vm_kern.h>
58 #include <vm/vm_extern.h>
60 #include <vm/vm_map.h>
61 #include <vm/vm_page.h>
62 #include <vm/vm_zone.h>
64 #include <sys/file2.h>
65 #include <sys/signal2.h>
66 #include <sys/mplock2.h>
68 #include <machine/cpufunc.h>
71 * interfaces to the outside world
73 static int pipe_read (struct file
*fp
, struct uio
*uio
,
74 struct ucred
*cred
, int flags
);
75 static int pipe_write (struct file
*fp
, struct uio
*uio
,
76 struct ucred
*cred
, int flags
);
77 static int pipe_close (struct file
*fp
);
78 static int pipe_shutdown (struct file
*fp
, int how
);
79 static int pipe_poll (struct file
*fp
, int events
, struct ucred
*cred
);
80 static int pipe_kqfilter (struct file
*fp
, struct knote
*kn
);
81 static int pipe_stat (struct file
*fp
, struct stat
*sb
, struct ucred
*cred
);
82 static int pipe_ioctl (struct file
*fp
, u_long cmd
, caddr_t data
,
83 struct ucred
*cred
, struct sysmsg
*msg
);
85 static struct fileops pipeops
= {
87 .fo_write
= pipe_write
,
88 .fo_ioctl
= pipe_ioctl
,
90 .fo_kqfilter
= pipe_kqfilter
,
92 .fo_close
= pipe_close
,
93 .fo_shutdown
= pipe_shutdown
96 static void filt_pipedetach(struct knote
*kn
);
97 static int filt_piperead(struct knote
*kn
, long hint
);
98 static int filt_pipewrite(struct knote
*kn
, long hint
);
100 static struct filterops pipe_rfiltops
=
101 { 1, NULL
, filt_pipedetach
, filt_piperead
};
102 static struct filterops pipe_wfiltops
=
103 { 1, NULL
, filt_pipedetach
, filt_pipewrite
};
105 MALLOC_DEFINE(M_PIPE
, "pipe", "pipe structures");
108 * Default pipe buffer size(s), this can be kind-of large now because pipe
109 * space is pageable. The pipe code will try to maintain locality of
110 * reference for performance reasons, so small amounts of outstanding I/O
111 * will not wipe the cache.
113 #define MINPIPESIZE (PIPE_SIZE/3)
114 #define MAXPIPESIZE (2*PIPE_SIZE/3)
117 * Limit the number of "big" pipes
119 #define LIMITBIGPIPES 64
120 #define PIPEQ_MAX_CACHE 16 /* per-cpu pipe structure cache */
122 static int pipe_maxbig
= LIMITBIGPIPES
;
123 static int pipe_maxcache
= PIPEQ_MAX_CACHE
;
124 static int pipe_bigcount
;
125 static int pipe_nbig
;
126 static int pipe_bcache_alloc
;
127 static int pipe_bkmem_alloc
;
128 static int pipe_rblocked_count
;
129 static int pipe_wblocked_count
;
131 SYSCTL_NODE(_kern
, OID_AUTO
, pipe
, CTLFLAG_RW
, 0, "Pipe operation");
132 SYSCTL_INT(_kern_pipe
, OID_AUTO
, nbig
,
133 CTLFLAG_RD
, &pipe_nbig
, 0, "numer of big pipes allocated");
134 SYSCTL_INT(_kern_pipe
, OID_AUTO
, bigcount
,
135 CTLFLAG_RW
, &pipe_bigcount
, 0, "number of times pipe expanded");
136 SYSCTL_INT(_kern_pipe
, OID_AUTO
, rblocked
,
137 CTLFLAG_RW
, &pipe_rblocked_count
, 0, "number of times pipe expanded");
138 SYSCTL_INT(_kern_pipe
, OID_AUTO
, wblocked
,
139 CTLFLAG_RW
, &pipe_wblocked_count
, 0, "number of times pipe expanded");
140 SYSCTL_INT(_kern_pipe
, OID_AUTO
, maxcache
,
141 CTLFLAG_RW
, &pipe_maxcache
, 0, "max pipes cached per-cpu");
142 SYSCTL_INT(_kern_pipe
, OID_AUTO
, maxbig
,
143 CTLFLAG_RW
, &pipe_maxbig
, 0, "max number of big pipes");
145 static int pipe_delay
= 5000; /* 5uS default */
146 SYSCTL_INT(_kern_pipe
, OID_AUTO
, delay
,
147 CTLFLAG_RW
, &pipe_delay
, 0, "SMP delay optimization in ns");
148 static int pipe_mpsafe
= 1;
149 SYSCTL_INT(_kern_pipe
, OID_AUTO
, mpsafe
,
150 CTLFLAG_RW
, &pipe_mpsafe
, 0, "");
152 #if !defined(NO_PIPE_SYSCTL_STATS)
153 SYSCTL_INT(_kern_pipe
, OID_AUTO
, bcache_alloc
,
154 CTLFLAG_RW
, &pipe_bcache_alloc
, 0, "pipe buffer from pcpu cache");
155 SYSCTL_INT(_kern_pipe
, OID_AUTO
, bkmem_alloc
,
156 CTLFLAG_RW
, &pipe_bkmem_alloc
, 0, "pipe buffer from kmem");
159 static void pipeclose (struct pipe
*cpipe
);
160 static void pipe_free_kmem (struct pipe
*cpipe
);
161 static int pipe_create (struct pipe
**cpipep
);
162 static __inline
void pipeselwakeup (struct pipe
*cpipe
);
163 static int pipespace (struct pipe
*cpipe
, int size
);
166 pipeseltest(struct pipe
*cpipe
)
168 return ((cpipe
->pipe_state
& PIPE_SEL
) ||
169 ((cpipe
->pipe_state
& PIPE_ASYNC
) && cpipe
->pipe_sigio
) ||
170 SLIST_FIRST(&cpipe
->pipe_sel
.si_note
));
174 pipeselwakeup(struct pipe
*cpipe
)
176 if (cpipe
->pipe_state
& PIPE_SEL
) {
178 cpipe
->pipe_state
&= ~PIPE_SEL
;
179 selwakeup(&cpipe
->pipe_sel
);
182 if ((cpipe
->pipe_state
& PIPE_ASYNC
) && cpipe
->pipe_sigio
) {
184 pgsigio(cpipe
->pipe_sigio
, SIGIO
, 0);
187 if (SLIST_FIRST(&cpipe
->pipe_sel
.si_note
)) {
189 KNOTE(&cpipe
->pipe_sel
.si_note
, 0);
195 * These routines are called before and after a UIO. The UIO
196 * may block, causing our held tokens to be lost temporarily.
198 * We use these routines to serialize reads against other reads
199 * and writes against other writes.
201 * The read token is held on entry so *ipp does not race.
204 pipe_start_uio(struct pipe
*cpipe
, int *ipp
)
210 error
= tsleep(ipp
, PCATCH
, "pipexx", 0);
219 pipe_end_uio(struct pipe
*cpipe
, int *ipp
)
231 pipe_get_mplock(int *save
)
234 if (pipe_mpsafe
== 0) {
245 pipe_rel_mplock(int *save
)
255 * The pipe system call for the DTYPE_PIPE type of pipes
257 * pipe_args(int dummy)
262 sys_pipe(struct pipe_args
*uap
)
264 struct thread
*td
= curthread
;
265 struct filedesc
*fdp
= td
->td_proc
->p_fd
;
266 struct file
*rf
, *wf
;
267 struct pipe
*rpipe
, *wpipe
;
270 rpipe
= wpipe
= NULL
;
271 if (pipe_create(&rpipe
) || pipe_create(&wpipe
)) {
277 error
= falloc(td
->td_lwp
, &rf
, &fd1
);
283 uap
->sysmsg_fds
[0] = fd1
;
286 * Warning: once we've gotten past allocation of the fd for the
287 * read-side, we can only drop the read side via fdrop() in order
288 * to avoid races against processes which manage to dup() the read
289 * side while we are blocked trying to allocate the write side.
291 rf
->f_type
= DTYPE_PIPE
;
292 rf
->f_flag
= FREAD
| FWRITE
;
293 rf
->f_ops
= &pipeops
;
295 error
= falloc(td
->td_lwp
, &wf
, &fd2
);
297 fsetfd(fdp
, NULL
, fd1
);
299 /* rpipe has been closed by fdrop(). */
303 wf
->f_type
= DTYPE_PIPE
;
304 wf
->f_flag
= FREAD
| FWRITE
;
305 wf
->f_ops
= &pipeops
;
307 uap
->sysmsg_fds
[1] = fd2
;
309 rpipe
->pipe_slock
= kmalloc(sizeof(struct lock
),
310 M_PIPE
, M_WAITOK
|M_ZERO
);
311 wpipe
->pipe_slock
= rpipe
->pipe_slock
;
312 rpipe
->pipe_peer
= wpipe
;
313 wpipe
->pipe_peer
= rpipe
;
314 lockinit(rpipe
->pipe_slock
, "pipecl", 0, 0);
317 * Once activated the peer relationship remains valid until
318 * both sides are closed.
320 fsetfd(fdp
, rf
, fd1
);
321 fsetfd(fdp
, wf
, fd2
);
329 * Allocate kva for pipe circular buffer, the space is pageable
330 * This routine will 'realloc' the size of a pipe safely, if it fails
331 * it will retain the old buffer.
332 * If it fails it will return ENOMEM.
335 pipespace(struct pipe
*cpipe
, int size
)
337 struct vm_object
*object
;
341 npages
= round_page(size
) / PAGE_SIZE
;
342 object
= cpipe
->pipe_buffer
.object
;
345 * [re]create the object if necessary and reserve space for it
346 * in the kernel_map. The object and memory are pageable. On
347 * success, free the old resources before assigning the new
350 if (object
== NULL
|| object
->size
!= npages
) {
352 object
= vm_object_allocate(OBJT_DEFAULT
, npages
);
353 buffer
= (caddr_t
)vm_map_min(&kernel_map
);
355 error
= vm_map_find(&kernel_map
, object
, 0,
356 (vm_offset_t
*)&buffer
, size
,
359 VM_PROT_ALL
, VM_PROT_ALL
,
362 if (error
!= KERN_SUCCESS
) {
363 vm_object_deallocate(object
);
367 pipe_free_kmem(cpipe
);
369 cpipe
->pipe_buffer
.object
= object
;
370 cpipe
->pipe_buffer
.buffer
= buffer
;
371 cpipe
->pipe_buffer
.size
= size
;
376 cpipe
->pipe_buffer
.rindex
= 0;
377 cpipe
->pipe_buffer
.windex
= 0;
382 * Initialize and allocate VM and memory for pipe, pulling the pipe from
383 * our per-cpu cache if possible. For now make sure it is sized for the
384 * smaller PIPE_SIZE default.
387 pipe_create(struct pipe
**cpipep
)
389 globaldata_t gd
= mycpu
;
393 if ((cpipe
= gd
->gd_pipeq
) != NULL
) {
394 gd
->gd_pipeq
= cpipe
->pipe_peer
;
396 cpipe
->pipe_peer
= NULL
;
397 cpipe
->pipe_wantwcnt
= 0;
399 cpipe
= kmalloc(sizeof(struct pipe
), M_PIPE
, M_WAITOK
|M_ZERO
);
402 if ((error
= pipespace(cpipe
, PIPE_SIZE
)) != 0)
404 vfs_timestamp(&cpipe
->pipe_ctime
);
405 cpipe
->pipe_atime
= cpipe
->pipe_ctime
;
406 cpipe
->pipe_mtime
= cpipe
->pipe_ctime
;
407 lwkt_token_init(&cpipe
->pipe_rlock
);
408 lwkt_token_init(&cpipe
->pipe_wlock
);
413 * MPALMOSTSAFE (acquires mplock)
416 pipe_read(struct file
*fp
, struct uio
*uio
, struct ucred
*cred
, int fflags
)
422 u_int size
; /* total bytes available */
423 u_int nsize
; /* total bytes to read */
424 u_int rindex
; /* contiguous bytes available */
432 if (uio
->uio_resid
== 0)
436 * Setup locks, calculate nbio
438 pipe_get_mplock(&mpsave
);
439 rpipe
= (struct pipe
*)fp
->f_data
;
440 lwkt_gettoken(&rlock
, &rpipe
->pipe_rlock
);
442 if (fflags
& O_FBLOCKING
)
444 else if (fflags
& O_FNONBLOCKING
)
446 else if (fp
->f_flag
& O_NONBLOCK
)
452 * Reads are serialized. Note howeverthat pipe_buffer.buffer and
453 * pipe_buffer.size can change out from under us when the number
454 * of bytes in the buffer are zero due to the write-side doing a
457 error
= pipe_start_uio(rpipe
, &rpipe
->pipe_rip
);
459 pipe_rel_mplock(&mpsave
);
460 lwkt_reltoken(&rlock
);
465 bigread
= (uio
->uio_resid
> 10 * 1024 * 1024);
468 while (uio
->uio_resid
) {
472 if (bigread
&& --bigcount
== 0) {
475 if (CURSIG(curthread
->td_lwp
)) {
481 size
= rpipe
->pipe_buffer
.windex
- rpipe
->pipe_buffer
.rindex
;
484 rindex
= rpipe
->pipe_buffer
.rindex
&
485 (rpipe
->pipe_buffer
.size
- 1);
487 if (nsize
> rpipe
->pipe_buffer
.size
- rindex
)
488 nsize
= rpipe
->pipe_buffer
.size
- rindex
;
489 nsize
= szmin(nsize
, uio
->uio_resid
);
491 error
= uiomove(&rpipe
->pipe_buffer
.buffer
[rindex
],
496 rpipe
->pipe_buffer
.rindex
+= nsize
;
500 * If the FIFO is still over half full just continue
501 * and do not try to notify the writer yet.
503 if (size
- nsize
>= (rpipe
->pipe_buffer
.size
>> 1)) {
509 * When the FIFO is less then half full notify any
510 * waiting writer. WANTW can be checked while
511 * holding just the rlock.
514 if ((rpipe
->pipe_state
& PIPE_WANTW
) == 0)
519 * If the "write-side" was blocked we wake it up. This code
520 * is reached either when the buffer is completely emptied
521 * or if it becomes more then half-empty.
523 * Pipe_state can only be modified if both the rlock and
526 if (rpipe
->pipe_state
& PIPE_WANTW
) {
527 lwkt_gettoken(&wlock
, &rpipe
->pipe_wlock
);
528 if (rpipe
->pipe_state
& PIPE_WANTW
) {
530 rpipe
->pipe_state
&= ~PIPE_WANTW
;
531 lwkt_reltoken(&wlock
);
534 lwkt_reltoken(&wlock
);
539 * Pick up our copy loop again if the writer sent data to
540 * us while we were messing around.
542 * On a SMP box poll up to pipe_delay nanoseconds for new
543 * data. Typically a value of 2000 to 4000 is sufficient
544 * to eradicate most IPIs/tsleeps/wakeups when a pipe
545 * is used for synchronous communications with small packets,
546 * and 8000 or so (8uS) will pipeline large buffer xfers
547 * between cpus over a pipe.
549 * For synchronous communications a hit means doing a
550 * full Awrite-Bread-Bwrite-Aread cycle in less then 2uS,
551 * where as miss requiring a tsleep/wakeup sequence
552 * will take 7uS or more.
554 if (rpipe
->pipe_buffer
.windex
!= rpipe
->pipe_buffer
.rindex
)
557 #if defined(SMP) && defined(_RDTSC_SUPPORTED_)
562 tsc_target
= tsc_get_target(pipe_delay
);
563 while (tsc_test_target(tsc_target
) == 0) {
564 if (rpipe
->pipe_buffer
.windex
!=
565 rpipe
->pipe_buffer
.rindex
) {
576 * Detect EOF condition, do not set error.
578 if (rpipe
->pipe_state
& PIPE_REOF
)
582 * Break if some data was read, or if this was a non-blocking
594 * Last chance, interlock with WANTR.
596 lwkt_gettoken(&wlock
, &rpipe
->pipe_wlock
);
597 size
= rpipe
->pipe_buffer
.windex
- rpipe
->pipe_buffer
.rindex
;
599 lwkt_reltoken(&wlock
);
604 * Retest EOF - acquiring a new token can temporarily release
605 * tokens already held.
607 if (rpipe
->pipe_state
& PIPE_REOF
) {
608 lwkt_reltoken(&wlock
);
613 * If there is no more to read in the pipe, reset its
614 * pointers to the beginning. This improves cache hit
617 * We need both locks to modify both pointers, and there
618 * must also not be a write in progress or the uiomove()
619 * in the write might block and temporarily release
620 * its wlock, then reacquire and update windex. We are
621 * only serialized against reads, not writes.
623 * XXX should we even bother resetting the indices? It
624 * might actually be more cache efficient not to.
626 if (rpipe
->pipe_buffer
.rindex
== rpipe
->pipe_buffer
.windex
&&
627 rpipe
->pipe_wip
== 0) {
628 rpipe
->pipe_buffer
.rindex
= 0;
629 rpipe
->pipe_buffer
.windex
= 0;
633 * Wait for more data.
635 * Pipe_state can only be set if both the rlock and wlock
638 rpipe
->pipe_state
|= PIPE_WANTR
;
639 tsleep_interlock(rpipe
, PCATCH
);
640 lwkt_reltoken(&wlock
);
641 error
= tsleep(rpipe
, PCATCH
| PINTERLOCKED
, "piperd", 0);
642 ++pipe_rblocked_count
;
646 pipe_end_uio(rpipe
, &rpipe
->pipe_rip
);
649 * Uptime last access time
651 if (error
== 0 && nread
)
652 vfs_timestamp(&rpipe
->pipe_atime
);
655 * If we drained the FIFO more then half way then handle
656 * write blocking hysteresis.
658 * Note that PIPE_WANTW cannot be set by the writer without
659 * it holding both rlock and wlock, so we can test it
660 * while holding just rlock.
663 if (rpipe
->pipe_state
& PIPE_WANTW
) {
664 lwkt_gettoken(&wlock
, &rpipe
->pipe_wlock
);
665 if (rpipe
->pipe_state
& PIPE_WANTW
) {
666 rpipe
->pipe_state
&= ~PIPE_WANTW
;
667 lwkt_reltoken(&wlock
);
670 lwkt_reltoken(&wlock
);
673 if (pipeseltest(rpipe
)) {
674 lwkt_gettoken(&wlock
, &rpipe
->pipe_wlock
);
675 pipeselwakeup(rpipe
);
676 lwkt_reltoken(&wlock
);
679 /*size = rpipe->pipe_buffer.windex - rpipe->pipe_buffer.rindex;*/
680 lwkt_reltoken(&rlock
);
682 pipe_rel_mplock(&mpsave
);
687 * MPALMOSTSAFE - acquires mplock
690 pipe_write(struct file
*fp
, struct uio
*uio
, struct ucred
*cred
, int fflags
)
695 struct pipe
*wpipe
, *rpipe
;
705 pipe_get_mplock(&mpsave
);
708 * Writes go to the peer. The peer will always exist.
710 rpipe
= (struct pipe
*) fp
->f_data
;
711 wpipe
= rpipe
->pipe_peer
;
712 lwkt_gettoken(&wlock
, &wpipe
->pipe_wlock
);
713 if (wpipe
->pipe_state
& PIPE_WEOF
) {
714 pipe_rel_mplock(&mpsave
);
715 lwkt_reltoken(&wlock
);
720 * Degenerate case (EPIPE takes prec)
722 if (uio
->uio_resid
== 0) {
723 pipe_rel_mplock(&mpsave
);
724 lwkt_reltoken(&wlock
);
729 * Writes are serialized (start_uio must be called with wlock)
731 error
= pipe_start_uio(wpipe
, &wpipe
->pipe_wip
);
733 pipe_rel_mplock(&mpsave
);
734 lwkt_reltoken(&wlock
);
738 if (fflags
& O_FBLOCKING
)
740 else if (fflags
& O_FNONBLOCKING
)
742 else if (fp
->f_flag
& O_NONBLOCK
)
748 * If it is advantageous to resize the pipe buffer, do
749 * so. We are write-serialized so we can block safely.
751 if ((wpipe
->pipe_buffer
.size
<= PIPE_SIZE
) &&
752 (pipe_nbig
< pipe_maxbig
) &&
753 wpipe
->pipe_wantwcnt
> 4 &&
754 (wpipe
->pipe_buffer
.rindex
== wpipe
->pipe_buffer
.windex
)) {
756 * Recheck after lock.
758 lwkt_gettoken(&rlock
, &wpipe
->pipe_rlock
);
759 if ((wpipe
->pipe_buffer
.size
<= PIPE_SIZE
) &&
760 (pipe_nbig
< pipe_maxbig
) &&
761 (wpipe
->pipe_buffer
.rindex
== wpipe
->pipe_buffer
.windex
)) {
762 atomic_add_int(&pipe_nbig
, 1);
763 if (pipespace(wpipe
, BIG_PIPE_SIZE
) == 0)
766 atomic_subtract_int(&pipe_nbig
, 1);
768 lwkt_reltoken(&rlock
);
771 orig_resid
= uio
->uio_resid
;
774 bigwrite
= (uio
->uio_resid
> 10 * 1024 * 1024);
777 while (uio
->uio_resid
) {
778 if (wpipe
->pipe_state
& PIPE_WEOF
) {
786 if (bigwrite
&& --bigcount
== 0) {
789 if (CURSIG(curthread
->td_lwp
)) {
795 windex
= wpipe
->pipe_buffer
.windex
&
796 (wpipe
->pipe_buffer
.size
- 1);
797 space
= wpipe
->pipe_buffer
.size
-
798 (wpipe
->pipe_buffer
.windex
- wpipe
->pipe_buffer
.rindex
);
801 /* Writes of size <= PIPE_BUF must be atomic. */
802 if ((space
< uio
->uio_resid
) && (orig_resid
<= PIPE_BUF
))
806 * Write to fill, read size handles write hysteresis. Also
807 * additional restrictions can cause select-based non-blocking
814 * Transfer size is minimum of uio transfer
815 * and free space in pipe buffer.
817 * Limit each uiocopy to no more then PIPE_SIZE
818 * so we can keep the gravy train going on a
819 * SMP box. This doubles the performance for
820 * write sizes > 16K. Otherwise large writes
821 * wind up doing an inefficient synchronous
824 space
= szmin(space
, uio
->uio_resid
);
825 if (space
> PIPE_SIZE
)
829 * First segment to transfer is minimum of
830 * transfer size and contiguous space in
831 * pipe buffer. If first segment to transfer
832 * is less than the transfer size, we've got
833 * a wraparound in the buffer.
835 segsize
= wpipe
->pipe_buffer
.size
- windex
;
841 * If this is the first loop and the reader is
842 * blocked, do a preemptive wakeup of the reader.
844 * On SMP the IPI latency plus the wlock interlock
845 * on the reader side is the fastest way to get the
846 * reader going. (The scheduler will hard loop on
849 * NOTE: We can't clear WANTR here without acquiring
850 * the rlock, which we don't want to do here!
852 if ((wpipe
->pipe_state
& PIPE_WANTR
) && pipe_mpsafe
> 1)
857 * Transfer segment, which may include a wrap-around.
858 * Update windex to account for both all in one go
859 * so the reader can read() the data atomically.
861 error
= uiomove(&wpipe
->pipe_buffer
.buffer
[windex
],
863 if (error
== 0 && segsize
< space
) {
864 segsize
= space
- segsize
;
865 error
= uiomove(&wpipe
->pipe_buffer
.buffer
[0],
871 wpipe
->pipe_buffer
.windex
+= space
;
877 * We need both the rlock and the wlock to interlock against
878 * the EOF, WANTW, and size checks, and to modify pipe_state.
880 * These are token locks so we do not have to worry about
883 lwkt_gettoken(&rlock
, &wpipe
->pipe_rlock
);
886 * If the "read-side" has been blocked, wake it up now
887 * and yield to let it drain synchronously rather
890 if (wpipe
->pipe_state
& PIPE_WANTR
) {
891 wpipe
->pipe_state
&= ~PIPE_WANTR
;
896 * don't block on non-blocking I/O
899 lwkt_reltoken(&rlock
);
905 * re-test whether we have to block in the writer after
906 * acquiring both locks, in case the reader opened up
909 space
= wpipe
->pipe_buffer
.size
-
910 (wpipe
->pipe_buffer
.windex
- wpipe
->pipe_buffer
.rindex
);
912 if ((space
< uio
->uio_resid
) && (orig_resid
<= PIPE_BUF
))
916 * Retest EOF - acquiring a new token can temporarily release
917 * tokens already held.
919 if (wpipe
->pipe_state
& PIPE_WEOF
) {
920 lwkt_reltoken(&rlock
);
926 * We have no more space and have something to offer,
927 * wake up select/poll.
930 wpipe
->pipe_state
|= PIPE_WANTW
;
931 ++wpipe
->pipe_wantwcnt
;
932 pipeselwakeup(wpipe
);
933 if (wpipe
->pipe_state
& PIPE_WANTW
)
934 error
= tsleep(wpipe
, PCATCH
, "pipewr", 0);
935 ++pipe_wblocked_count
;
937 lwkt_reltoken(&rlock
);
940 * Break out if we errored or the read side wants us to go
945 if (wpipe
->pipe_state
& PIPE_WEOF
) {
950 pipe_end_uio(wpipe
, &wpipe
->pipe_wip
);
953 * If we have put any characters in the buffer, we wake up
956 * Both rlock and wlock are required to be able to modify pipe_state.
958 if (wpipe
->pipe_buffer
.windex
!= wpipe
->pipe_buffer
.rindex
) {
959 if (wpipe
->pipe_state
& PIPE_WANTR
) {
960 lwkt_gettoken(&rlock
, &wpipe
->pipe_rlock
);
961 if (wpipe
->pipe_state
& PIPE_WANTR
) {
962 wpipe
->pipe_state
&= ~PIPE_WANTR
;
963 lwkt_reltoken(&rlock
);
966 lwkt_reltoken(&rlock
);
969 if (pipeseltest(wpipe
)) {
970 lwkt_gettoken(&rlock
, &wpipe
->pipe_rlock
);
971 pipeselwakeup(wpipe
);
972 lwkt_reltoken(&rlock
);
977 * Don't return EPIPE if I/O was successful
979 if ((wpipe
->pipe_buffer
.rindex
== wpipe
->pipe_buffer
.windex
) &&
980 (uio
->uio_resid
== 0) &&
986 vfs_timestamp(&wpipe
->pipe_mtime
);
989 * We have something to offer,
990 * wake up select/poll.
992 /*space = wpipe->pipe_buffer.windex - wpipe->pipe_buffer.rindex;*/
993 lwkt_reltoken(&wlock
);
994 pipe_rel_mplock(&mpsave
);
999 * MPALMOSTSAFE - acquires mplock
1001 * we implement a very minimal set of ioctls for compatibility with sockets.
1004 pipe_ioctl(struct file
*fp
, u_long cmd
, caddr_t data
,
1005 struct ucred
*cred
, struct sysmsg
*msg
)
1013 pipe_get_mplock(&mpsave
);
1014 mpipe
= (struct pipe
*)fp
->f_data
;
1016 lwkt_gettoken(&rlock
, &mpipe
->pipe_rlock
);
1017 lwkt_gettoken(&wlock
, &mpipe
->pipe_wlock
);
1022 mpipe
->pipe_state
|= PIPE_ASYNC
;
1024 mpipe
->pipe_state
&= ~PIPE_ASYNC
;
1029 *(int *)data
= mpipe
->pipe_buffer
.windex
-
1030 mpipe
->pipe_buffer
.rindex
;
1035 error
= fsetown(*(int *)data
, &mpipe
->pipe_sigio
);
1039 *(int *)data
= fgetown(mpipe
->pipe_sigio
);
1043 /* This is deprecated, FIOSETOWN should be used instead. */
1045 error
= fsetown(-(*(int *)data
), &mpipe
->pipe_sigio
);
1050 /* This is deprecated, FIOGETOWN should be used instead. */
1051 *(int *)data
= -fgetown(mpipe
->pipe_sigio
);
1058 lwkt_reltoken(&rlock
);
1059 lwkt_reltoken(&wlock
);
1060 pipe_rel_mplock(&mpsave
);
1066 * MPALMOSTSAFE - acquires mplock
1068 * poll for events (helper)
1071 pipe_poll_events(struct pipe
*rpipe
, struct pipe
*wpipe
, int events
)
1076 if (events
& (POLLIN
| POLLRDNORM
)) {
1077 if ((rpipe
->pipe_buffer
.windex
!= rpipe
->pipe_buffer
.rindex
) ||
1078 (rpipe
->pipe_state
& PIPE_REOF
)) {
1079 revents
|= events
& (POLLIN
| POLLRDNORM
);
1083 if (events
& (POLLOUT
| POLLWRNORM
)) {
1084 if (wpipe
== NULL
|| (wpipe
->pipe_state
& PIPE_WEOF
)) {
1085 revents
|= events
& (POLLOUT
| POLLWRNORM
);
1087 space
= wpipe
->pipe_buffer
.windex
-
1088 wpipe
->pipe_buffer
.rindex
;
1089 space
= wpipe
->pipe_buffer
.size
- space
;
1090 if (space
>= PIPE_BUF
)
1091 revents
|= events
& (POLLOUT
| POLLWRNORM
);
1095 if ((rpipe
->pipe_state
& PIPE_REOF
) ||
1097 (wpipe
->pipe_state
& PIPE_WEOF
)) {
1104 * Poll for events from file pointer.
1107 pipe_poll(struct file
*fp
, int events
, struct ucred
*cred
)
1109 lwkt_tokref rpipe_rlock
;
1110 lwkt_tokref rpipe_wlock
;
1111 lwkt_tokref wpipe_rlock
;
1112 lwkt_tokref wpipe_wlock
;
1118 pipe_get_mplock(&mpsave
);
1119 rpipe
= (struct pipe
*)fp
->f_data
;
1120 wpipe
= rpipe
->pipe_peer
;
1122 revents
= pipe_poll_events(rpipe
, wpipe
, events
);
1124 if (events
& (POLLIN
| POLLRDNORM
)) {
1125 lwkt_gettoken(&rpipe_rlock
, &rpipe
->pipe_rlock
);
1126 lwkt_gettoken(&rpipe_wlock
, &rpipe
->pipe_wlock
);
1128 if (events
& (POLLOUT
| POLLWRNORM
)) {
1129 lwkt_gettoken(&wpipe_rlock
, &wpipe
->pipe_rlock
);
1130 lwkt_gettoken(&wpipe_wlock
, &wpipe
->pipe_wlock
);
1132 revents
= pipe_poll_events(rpipe
, wpipe
, events
);
1134 if (events
& (POLLIN
| POLLRDNORM
)) {
1135 selrecord(curthread
, &rpipe
->pipe_sel
);
1136 rpipe
->pipe_state
|= PIPE_SEL
;
1139 if (events
& (POLLOUT
| POLLWRNORM
)) {
1140 selrecord(curthread
, &wpipe
->pipe_sel
);
1141 wpipe
->pipe_state
|= PIPE_SEL
;
1144 if (events
& (POLLIN
| POLLRDNORM
)) {
1145 lwkt_reltoken(&rpipe_rlock
);
1146 lwkt_reltoken(&rpipe_wlock
);
1148 if (events
& (POLLOUT
| POLLWRNORM
)) {
1149 lwkt_reltoken(&wpipe_rlock
);
1150 lwkt_reltoken(&wpipe_wlock
);
1153 pipe_rel_mplock(&mpsave
);
1161 pipe_stat(struct file
*fp
, struct stat
*ub
, struct ucred
*cred
)
1166 pipe_get_mplock(&mpsave
);
1167 pipe
= (struct pipe
*)fp
->f_data
;
1169 bzero((caddr_t
)ub
, sizeof(*ub
));
1170 ub
->st_mode
= S_IFIFO
;
1171 ub
->st_blksize
= pipe
->pipe_buffer
.size
;
1172 ub
->st_size
= pipe
->pipe_buffer
.windex
- pipe
->pipe_buffer
.rindex
;
1173 ub
->st_blocks
= (ub
->st_size
+ ub
->st_blksize
- 1) / ub
->st_blksize
;
1174 ub
->st_atimespec
= pipe
->pipe_atime
;
1175 ub
->st_mtimespec
= pipe
->pipe_mtime
;
1176 ub
->st_ctimespec
= pipe
->pipe_ctime
;
1178 * Left as 0: st_dev, st_ino, st_nlink, st_uid, st_gid, st_rdev,
1180 * XXX (st_dev, st_ino) should be unique.
1182 pipe_rel_mplock(&mpsave
);
1187 * MPALMOSTSAFE - acquires mplock
1190 pipe_close(struct file
*fp
)
1195 cpipe
= (struct pipe
*)fp
->f_data
;
1196 fp
->f_ops
= &badfileops
;
1198 funsetown(cpipe
->pipe_sigio
);
1205 * Shutdown one or both directions of a full-duplex pipe.
1207 * MPALMOSTSAFE - acquires mplock
1210 pipe_shutdown(struct file
*fp
, int how
)
1215 lwkt_tokref rpipe_rlock
;
1216 lwkt_tokref rpipe_wlock
;
1217 lwkt_tokref wpipe_rlock
;
1218 lwkt_tokref wpipe_wlock
;
1221 pipe_get_mplock(&mpsave
);
1222 rpipe
= (struct pipe
*)fp
->f_data
;
1223 wpipe
= rpipe
->pipe_peer
;
1226 * We modify pipe_state on both pipes, which means we need
1229 lwkt_gettoken(&rpipe_rlock
, &rpipe
->pipe_rlock
);
1230 lwkt_gettoken(&rpipe_wlock
, &rpipe
->pipe_wlock
);
1231 lwkt_gettoken(&wpipe_rlock
, &wpipe
->pipe_rlock
);
1232 lwkt_gettoken(&wpipe_wlock
, &wpipe
->pipe_wlock
);
1237 rpipe
->pipe_state
|= PIPE_REOF
; /* my reads */
1238 rpipe
->pipe_state
|= PIPE_WEOF
; /* peer writes */
1239 if (rpipe
->pipe_state
& PIPE_WANTR
) {
1240 rpipe
->pipe_state
&= ~PIPE_WANTR
;
1243 if (rpipe
->pipe_state
& PIPE_WANTW
) {
1244 rpipe
->pipe_state
&= ~PIPE_WANTW
;
1252 wpipe
->pipe_state
|= PIPE_REOF
; /* peer reads */
1253 wpipe
->pipe_state
|= PIPE_WEOF
; /* my writes */
1254 if (wpipe
->pipe_state
& PIPE_WANTR
) {
1255 wpipe
->pipe_state
&= ~PIPE_WANTR
;
1258 if (wpipe
->pipe_state
& PIPE_WANTW
) {
1259 wpipe
->pipe_state
&= ~PIPE_WANTW
;
1265 pipeselwakeup(rpipe
);
1266 pipeselwakeup(wpipe
);
1268 lwkt_reltoken(&rpipe_rlock
);
1269 lwkt_reltoken(&rpipe_wlock
);
1270 lwkt_reltoken(&wpipe_rlock
);
1271 lwkt_reltoken(&wpipe_wlock
);
1273 pipe_rel_mplock(&mpsave
);
1278 pipe_free_kmem(struct pipe
*cpipe
)
1280 if (cpipe
->pipe_buffer
.buffer
!= NULL
) {
1281 if (cpipe
->pipe_buffer
.size
> PIPE_SIZE
)
1282 atomic_subtract_int(&pipe_nbig
, 1);
1283 kmem_free(&kernel_map
,
1284 (vm_offset_t
)cpipe
->pipe_buffer
.buffer
,
1285 cpipe
->pipe_buffer
.size
);
1286 cpipe
->pipe_buffer
.buffer
= NULL
;
1287 cpipe
->pipe_buffer
.object
= NULL
;
1292 * Close the pipe. The slock must be held to interlock against simultanious
1293 * closes. The rlock and wlock must be held to adjust the pipe_state.
1296 pipeclose(struct pipe
*cpipe
)
1300 lwkt_tokref cpipe_rlock
;
1301 lwkt_tokref cpipe_wlock
;
1302 lwkt_tokref ppipe_rlock
;
1303 lwkt_tokref ppipe_wlock
;
1309 * The slock may not have been allocated yet (close during
1312 * We need both the read and write tokens to modify pipe_state.
1314 if (cpipe
->pipe_slock
)
1315 lockmgr(cpipe
->pipe_slock
, LK_EXCLUSIVE
);
1316 lwkt_gettoken(&cpipe_rlock
, &cpipe
->pipe_rlock
);
1317 lwkt_gettoken(&cpipe_wlock
, &cpipe
->pipe_wlock
);
1320 * Set our state, wakeup anyone waiting in select, and
1321 * wakeup anyone blocked on our pipe.
1323 cpipe
->pipe_state
|= PIPE_CLOSED
| PIPE_REOF
| PIPE_WEOF
;
1324 pipeselwakeup(cpipe
);
1325 if (cpipe
->pipe_state
& (PIPE_WANTR
| PIPE_WANTW
)) {
1326 cpipe
->pipe_state
&= ~(PIPE_WANTR
| PIPE_WANTW
);
1331 * Disconnect from peer.
1333 if ((ppipe
= cpipe
->pipe_peer
) != NULL
) {
1334 lwkt_gettoken(&ppipe_rlock
, &ppipe
->pipe_rlock
);
1335 lwkt_gettoken(&ppipe_wlock
, &ppipe
->pipe_wlock
);
1336 ppipe
->pipe_state
|= PIPE_REOF
| PIPE_WEOF
;
1337 pipeselwakeup(ppipe
);
1338 if (ppipe
->pipe_state
& (PIPE_WANTR
| PIPE_WANTW
)) {
1339 ppipe
->pipe_state
&= ~(PIPE_WANTR
| PIPE_WANTW
);
1342 if (SLIST_FIRST(&ppipe
->pipe_sel
.si_note
)) {
1344 KNOTE(&ppipe
->pipe_sel
.si_note
, 0);
1347 lwkt_reltoken(&ppipe_rlock
);
1348 lwkt_reltoken(&ppipe_wlock
);
1352 * If the peer is also closed we can free resources for both
1353 * sides, otherwise we leave our side intact to deal with any
1354 * races (since we only have the slock).
1356 if (ppipe
&& (ppipe
->pipe_state
& PIPE_CLOSED
)) {
1357 cpipe
->pipe_peer
= NULL
;
1358 ppipe
->pipe_peer
= NULL
;
1359 ppipe
->pipe_slock
= NULL
; /* we will free the slock */
1364 lwkt_reltoken(&cpipe_rlock
);
1365 lwkt_reltoken(&cpipe_wlock
);
1366 if (cpipe
->pipe_slock
)
1367 lockmgr(cpipe
->pipe_slock
, LK_RELEASE
);
1370 * If we disassociated from our peer we can free resources
1372 if (ppipe
== NULL
) {
1374 if (cpipe
->pipe_slock
) {
1375 kfree(cpipe
->pipe_slock
, M_PIPE
);
1376 cpipe
->pipe_slock
= NULL
;
1378 if (gd
->gd_pipeqcount
>= pipe_maxcache
||
1379 cpipe
->pipe_buffer
.size
!= PIPE_SIZE
1381 pipe_free_kmem(cpipe
);
1382 kfree(cpipe
, M_PIPE
);
1384 cpipe
->pipe_state
= 0;
1385 cpipe
->pipe_peer
= gd
->gd_pipeq
;
1386 gd
->gd_pipeq
= cpipe
;
1387 ++gd
->gd_pipeqcount
;
1393 * MPALMOSTSAFE - acquires mplock
1396 pipe_kqfilter(struct file
*fp
, struct knote
*kn
)
1401 cpipe
= (struct pipe
*)kn
->kn_fp
->f_data
;
1403 switch (kn
->kn_filter
) {
1405 kn
->kn_fop
= &pipe_rfiltops
;
1408 kn
->kn_fop
= &pipe_wfiltops
;
1409 cpipe
= cpipe
->pipe_peer
;
1410 if (cpipe
== NULL
) {
1411 /* other end of pipe has been closed */
1419 kn
->kn_hook
= (caddr_t
)cpipe
;
1421 SLIST_INSERT_HEAD(&cpipe
->pipe_sel
.si_note
, kn
, kn_selnext
);
1427 filt_pipedetach(struct knote
*kn
)
1429 struct pipe
*cpipe
= (struct pipe
*)kn
->kn_hook
;
1431 SLIST_REMOVE(&cpipe
->pipe_sel
.si_note
, kn
, knote
, kn_selnext
);
1436 filt_piperead(struct knote
*kn
, long hint
)
1438 struct pipe
*rpipe
= (struct pipe
*)kn
->kn_fp
->f_data
;
1440 kn
->kn_data
= rpipe
->pipe_buffer
.windex
- rpipe
->pipe_buffer
.rindex
;
1443 if (rpipe
->pipe_state
& PIPE_REOF
) {
1444 kn
->kn_flags
|= EV_EOF
;
1447 return (kn
->kn_data
> 0);
1452 filt_pipewrite(struct knote
*kn
, long hint
)
1454 struct pipe
*rpipe
= (struct pipe
*)kn
->kn_fp
->f_data
;
1455 struct pipe
*wpipe
= rpipe
->pipe_peer
;
1459 if ((wpipe
== NULL
) || (wpipe
->pipe_state
& PIPE_WEOF
)) {
1461 kn
->kn_flags
|= EV_EOF
;
1464 space
= wpipe
->pipe_buffer
.windex
-
1465 wpipe
->pipe_buffer
.rindex
;
1466 space
= wpipe
->pipe_buffer
.size
- space
;
1467 kn
->kn_data
= space
;
1468 return (kn
->kn_data
>= PIPE_BUF
);