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>
39 #include <sys/signalvar.h>
40 #include <sys/sysproto.h>
42 #include <sys/vnode.h>
44 #include <sys/event.h>
45 #include <sys/globaldata.h>
46 #include <sys/module.h>
47 #include <sys/malloc.h>
48 #include <sys/sysctl.h>
49 #include <sys/socket.h>
52 #include <vm/vm_param.h>
54 #include <vm/vm_object.h>
55 #include <vm/vm_kern.h>
56 #include <vm/vm_extern.h>
58 #include <vm/vm_map.h>
59 #include <vm/vm_page.h>
60 #include <vm/vm_zone.h>
62 #include <sys/file2.h>
63 #include <sys/signal2.h>
65 #include <machine/cpufunc.h>
68 * interfaces to the outside world
70 static int pipe_read (struct file
*fp
, struct uio
*uio
,
71 struct ucred
*cred
, int flags
);
72 static int pipe_write (struct file
*fp
, struct uio
*uio
,
73 struct ucred
*cred
, int flags
);
74 static int pipe_close (struct file
*fp
);
75 static int pipe_shutdown (struct file
*fp
, int how
);
76 static int pipe_kqfilter (struct file
*fp
, struct knote
*kn
);
77 static int pipe_stat (struct file
*fp
, struct stat
*sb
, struct ucred
*cred
);
78 static int pipe_ioctl (struct file
*fp
, u_long cmd
, caddr_t data
,
79 struct ucred
*cred
, struct sysmsg
*msg
);
81 static struct fileops pipeops
= {
83 .fo_write
= pipe_write
,
84 .fo_ioctl
= pipe_ioctl
,
85 .fo_kqfilter
= pipe_kqfilter
,
87 .fo_close
= pipe_close
,
88 .fo_shutdown
= pipe_shutdown
91 static void filt_pipedetach(struct knote
*kn
);
92 static int filt_piperead(struct knote
*kn
, long hint
);
93 static int filt_pipewrite(struct knote
*kn
, long hint
);
95 static struct filterops pipe_rfiltops
=
96 { FILTEROP_ISFD
, NULL
, filt_pipedetach
, filt_piperead
};
97 static struct filterops pipe_wfiltops
=
98 { FILTEROP_ISFD
, NULL
, filt_pipedetach
, filt_pipewrite
};
100 MALLOC_DEFINE(M_PIPE
, "pipe", "pipe structures");
103 * Default pipe buffer size(s), this can be kind-of large now because pipe
104 * space is pageable. The pipe code will try to maintain locality of
105 * reference for performance reasons, so small amounts of outstanding I/O
106 * will not wipe the cache.
108 #define MINPIPESIZE (PIPE_SIZE/3)
109 #define MAXPIPESIZE (2*PIPE_SIZE/3)
112 * Limit the number of "big" pipes
114 #define LIMITBIGPIPES 64
115 #define PIPEQ_MAX_CACHE 16 /* per-cpu pipe structure cache */
117 static int pipe_maxbig
= LIMITBIGPIPES
;
118 static int pipe_maxcache
= PIPEQ_MAX_CACHE
;
119 static int pipe_bigcount
;
120 static int pipe_nbig
;
121 static int pipe_bcache_alloc
;
122 static int pipe_bkmem_alloc
;
123 static int pipe_rblocked_count
;
124 static int pipe_wblocked_count
;
126 SYSCTL_NODE(_kern
, OID_AUTO
, pipe
, CTLFLAG_RW
, 0, "Pipe operation");
127 SYSCTL_INT(_kern_pipe
, OID_AUTO
, nbig
,
128 CTLFLAG_RD
, &pipe_nbig
, 0, "numer of big pipes allocated");
129 SYSCTL_INT(_kern_pipe
, OID_AUTO
, bigcount
,
130 CTLFLAG_RW
, &pipe_bigcount
, 0, "number of times pipe expanded");
131 SYSCTL_INT(_kern_pipe
, OID_AUTO
, rblocked
,
132 CTLFLAG_RW
, &pipe_rblocked_count
, 0, "number of times pipe expanded");
133 SYSCTL_INT(_kern_pipe
, OID_AUTO
, wblocked
,
134 CTLFLAG_RW
, &pipe_wblocked_count
, 0, "number of times pipe expanded");
135 SYSCTL_INT(_kern_pipe
, OID_AUTO
, maxcache
,
136 CTLFLAG_RW
, &pipe_maxcache
, 0, "max pipes cached per-cpu");
137 SYSCTL_INT(_kern_pipe
, OID_AUTO
, maxbig
,
138 CTLFLAG_RW
, &pipe_maxbig
, 0, "max number of big pipes");
140 static int pipe_delay
= 5000; /* 5uS default */
141 SYSCTL_INT(_kern_pipe
, OID_AUTO
, delay
,
142 CTLFLAG_RW
, &pipe_delay
, 0, "SMP delay optimization in ns");
144 #if !defined(NO_PIPE_SYSCTL_STATS)
145 SYSCTL_INT(_kern_pipe
, OID_AUTO
, bcache_alloc
,
146 CTLFLAG_RW
, &pipe_bcache_alloc
, 0, "pipe buffer from pcpu cache");
147 SYSCTL_INT(_kern_pipe
, OID_AUTO
, bkmem_alloc
,
148 CTLFLAG_RW
, &pipe_bkmem_alloc
, 0, "pipe buffer from kmem");
151 static void pipeclose (struct pipe
*cpipe
);
152 static void pipe_free_kmem (struct pipe
*cpipe
);
153 static int pipe_create (struct pipe
**cpipep
);
154 static int pipespace (struct pipe
*cpipe
, int size
);
157 pipewakeup(struct pipe
*cpipe
, int dosigio
)
159 if (dosigio
&& (cpipe
->pipe_state
& PIPE_ASYNC
) && cpipe
->pipe_sigio
) {
160 lwkt_gettoken(&proc_token
);
161 pgsigio(cpipe
->pipe_sigio
, SIGIO
, 0);
162 lwkt_reltoken(&proc_token
);
164 KNOTE(&cpipe
->pipe_kq
.ki_note
, 0);
168 * These routines are called before and after a UIO. The UIO
169 * may block, causing our held tokens to be lost temporarily.
171 * We use these routines to serialize reads against other reads
172 * and writes against other writes.
174 * The read token is held on entry so *ipp does not race.
177 pipe_start_uio(struct pipe
*cpipe
, int *ipp
)
183 error
= tsleep(ipp
, PCATCH
, "pipexx", 0);
192 pipe_end_uio(struct pipe
*cpipe
, int *ipp
)
204 * The pipe system call for the DTYPE_PIPE type of pipes
206 * pipe_args(int dummy)
211 sys_pipe(struct pipe_args
*uap
)
213 struct thread
*td
= curthread
;
214 struct filedesc
*fdp
= td
->td_proc
->p_fd
;
215 struct file
*rf
, *wf
;
216 struct pipe
*rpipe
, *wpipe
;
219 rpipe
= wpipe
= NULL
;
220 if (pipe_create(&rpipe
) || pipe_create(&wpipe
)) {
226 error
= falloc(td
->td_lwp
, &rf
, &fd1
);
232 uap
->sysmsg_fds
[0] = fd1
;
235 * Warning: once we've gotten past allocation of the fd for the
236 * read-side, we can only drop the read side via fdrop() in order
237 * to avoid races against processes which manage to dup() the read
238 * side while we are blocked trying to allocate the write side.
240 rf
->f_type
= DTYPE_PIPE
;
241 rf
->f_flag
= FREAD
| FWRITE
;
242 rf
->f_ops
= &pipeops
;
244 error
= falloc(td
->td_lwp
, &wf
, &fd2
);
246 fsetfd(fdp
, NULL
, fd1
);
248 /* rpipe has been closed by fdrop(). */
252 wf
->f_type
= DTYPE_PIPE
;
253 wf
->f_flag
= FREAD
| FWRITE
;
254 wf
->f_ops
= &pipeops
;
256 uap
->sysmsg_fds
[1] = fd2
;
258 rpipe
->pipe_slock
= kmalloc(sizeof(struct lock
),
259 M_PIPE
, M_WAITOK
|M_ZERO
);
260 wpipe
->pipe_slock
= rpipe
->pipe_slock
;
261 rpipe
->pipe_peer
= wpipe
;
262 wpipe
->pipe_peer
= rpipe
;
263 lockinit(rpipe
->pipe_slock
, "pipecl", 0, 0);
266 * Once activated the peer relationship remains valid until
267 * both sides are closed.
269 fsetfd(fdp
, rf
, fd1
);
270 fsetfd(fdp
, wf
, fd2
);
278 * Allocate kva for pipe circular buffer, the space is pageable
279 * This routine will 'realloc' the size of a pipe safely, if it fails
280 * it will retain the old buffer.
281 * If it fails it will return ENOMEM.
284 pipespace(struct pipe
*cpipe
, int size
)
286 struct vm_object
*object
;
290 npages
= round_page(size
) / PAGE_SIZE
;
291 object
= cpipe
->pipe_buffer
.object
;
294 * [re]create the object if necessary and reserve space for it
295 * in the kernel_map. The object and memory are pageable. On
296 * success, free the old resources before assigning the new
299 if (object
== NULL
|| object
->size
!= npages
) {
300 object
= vm_object_allocate(OBJT_DEFAULT
, npages
);
301 buffer
= (caddr_t
)vm_map_min(&kernel_map
);
303 error
= vm_map_find(&kernel_map
, object
, 0,
304 (vm_offset_t
*)&buffer
,
306 1, VM_MAPTYPE_NORMAL
,
307 VM_PROT_ALL
, VM_PROT_ALL
,
310 if (error
!= KERN_SUCCESS
) {
311 vm_object_deallocate(object
);
314 pipe_free_kmem(cpipe
);
315 cpipe
->pipe_buffer
.object
= object
;
316 cpipe
->pipe_buffer
.buffer
= buffer
;
317 cpipe
->pipe_buffer
.size
= size
;
322 cpipe
->pipe_buffer
.rindex
= 0;
323 cpipe
->pipe_buffer
.windex
= 0;
328 * Initialize and allocate VM and memory for pipe, pulling the pipe from
329 * our per-cpu cache if possible. For now make sure it is sized for the
330 * smaller PIPE_SIZE default.
333 pipe_create(struct pipe
**cpipep
)
335 globaldata_t gd
= mycpu
;
339 if ((cpipe
= gd
->gd_pipeq
) != NULL
) {
340 gd
->gd_pipeq
= cpipe
->pipe_peer
;
342 cpipe
->pipe_peer
= NULL
;
343 cpipe
->pipe_wantwcnt
= 0;
345 cpipe
= kmalloc(sizeof(struct pipe
), M_PIPE
, M_WAITOK
|M_ZERO
);
348 if ((error
= pipespace(cpipe
, PIPE_SIZE
)) != 0)
350 vfs_timestamp(&cpipe
->pipe_ctime
);
351 cpipe
->pipe_atime
= cpipe
->pipe_ctime
;
352 cpipe
->pipe_mtime
= cpipe
->pipe_ctime
;
353 lwkt_token_init(&cpipe
->pipe_rlock
, 1, "piper");
354 lwkt_token_init(&cpipe
->pipe_wlock
, 1, "pipew");
359 pipe_read(struct file
*fp
, struct uio
*uio
, struct ucred
*cred
, int fflags
)
366 u_int size
; /* total bytes available */
367 u_int nsize
; /* total bytes to read */
368 u_int rindex
; /* contiguous bytes available */
373 if (uio
->uio_resid
== 0)
377 * Setup locks, calculate nbio
379 rpipe
= (struct pipe
*)fp
->f_data
;
380 wpipe
= rpipe
->pipe_peer
;
381 lwkt_gettoken(&rpipe
->pipe_rlock
);
383 if (fflags
& O_FBLOCKING
)
385 else if (fflags
& O_FNONBLOCKING
)
387 else if (fp
->f_flag
& O_NONBLOCK
)
393 * Reads are serialized. Note however that pipe_buffer.buffer and
394 * pipe_buffer.size can change out from under us when the number
395 * of bytes in the buffer are zero due to the write-side doing a
398 error
= pipe_start_uio(rpipe
, &rpipe
->pipe_rip
);
400 lwkt_reltoken(&rpipe
->pipe_rlock
);
405 bigread
= (uio
->uio_resid
> 10 * 1024 * 1024);
408 while (uio
->uio_resid
) {
412 if (bigread
&& --bigcount
== 0) {
415 if (CURSIG(curthread
->td_lwp
)) {
421 size
= rpipe
->pipe_buffer
.windex
- rpipe
->pipe_buffer
.rindex
;
424 rindex
= rpipe
->pipe_buffer
.rindex
&
425 (rpipe
->pipe_buffer
.size
- 1);
427 if (nsize
> rpipe
->pipe_buffer
.size
- rindex
)
428 nsize
= rpipe
->pipe_buffer
.size
- rindex
;
429 nsize
= szmin(nsize
, uio
->uio_resid
);
431 error
= uiomove(&rpipe
->pipe_buffer
.buffer
[rindex
],
436 rpipe
->pipe_buffer
.rindex
+= nsize
;
440 * If the FIFO is still over half full just continue
441 * and do not try to notify the writer yet.
443 if (size
- nsize
>= (rpipe
->pipe_buffer
.size
>> 1)) {
449 * When the FIFO is less then half full notify any
450 * waiting writer. WANTW can be checked while
451 * holding just the rlock.
454 if ((rpipe
->pipe_state
& PIPE_WANTW
) == 0)
459 * If the "write-side" was blocked we wake it up. This code
460 * is reached either when the buffer is completely emptied
461 * or if it becomes more then half-empty.
463 * Pipe_state can only be modified if both the rlock and
466 if (rpipe
->pipe_state
& PIPE_WANTW
) {
467 lwkt_gettoken(&rpipe
->pipe_wlock
);
468 if (rpipe
->pipe_state
& PIPE_WANTW
) {
469 rpipe
->pipe_state
&= ~PIPE_WANTW
;
470 lwkt_reltoken(&rpipe
->pipe_wlock
);
473 lwkt_reltoken(&rpipe
->pipe_wlock
);
478 * Pick up our copy loop again if the writer sent data to
479 * us while we were messing around.
481 * On a SMP box poll up to pipe_delay nanoseconds for new
482 * data. Typically a value of 2000 to 4000 is sufficient
483 * to eradicate most IPIs/tsleeps/wakeups when a pipe
484 * is used for synchronous communications with small packets,
485 * and 8000 or so (8uS) will pipeline large buffer xfers
486 * between cpus over a pipe.
488 * For synchronous communications a hit means doing a
489 * full Awrite-Bread-Bwrite-Aread cycle in less then 2uS,
490 * where as miss requiring a tsleep/wakeup sequence
491 * will take 7uS or more.
493 if (rpipe
->pipe_buffer
.windex
!= rpipe
->pipe_buffer
.rindex
)
496 #if defined(SMP) && defined(_RDTSC_SUPPORTED_)
501 tsc_target
= tsc_get_target(pipe_delay
);
502 while (tsc_test_target(tsc_target
) == 0) {
503 if (rpipe
->pipe_buffer
.windex
!=
504 rpipe
->pipe_buffer
.rindex
) {
515 * Detect EOF condition, do not set error.
517 if (rpipe
->pipe_state
& PIPE_REOF
)
521 * Break if some data was read, or if this was a non-blocking
533 * Last chance, interlock with WANTR.
535 lwkt_gettoken(&rpipe
->pipe_wlock
);
536 size
= rpipe
->pipe_buffer
.windex
- rpipe
->pipe_buffer
.rindex
;
538 lwkt_reltoken(&rpipe
->pipe_wlock
);
543 * Retest EOF - acquiring a new token can temporarily release
544 * tokens already held.
546 if (rpipe
->pipe_state
& PIPE_REOF
) {
547 lwkt_reltoken(&rpipe
->pipe_wlock
);
552 * If there is no more to read in the pipe, reset its
553 * pointers to the beginning. This improves cache hit
556 * We need both locks to modify both pointers, and there
557 * must also not be a write in progress or the uiomove()
558 * in the write might block and temporarily release
559 * its wlock, then reacquire and update windex. We are
560 * only serialized against reads, not writes.
562 * XXX should we even bother resetting the indices? It
563 * might actually be more cache efficient not to.
565 if (rpipe
->pipe_buffer
.rindex
== rpipe
->pipe_buffer
.windex
&&
566 rpipe
->pipe_wip
== 0) {
567 rpipe
->pipe_buffer
.rindex
= 0;
568 rpipe
->pipe_buffer
.windex
= 0;
572 * Wait for more data.
574 * Pipe_state can only be set if both the rlock and wlock
577 rpipe
->pipe_state
|= PIPE_WANTR
;
578 tsleep_interlock(rpipe
, PCATCH
);
579 lwkt_reltoken(&rpipe
->pipe_wlock
);
580 error
= tsleep(rpipe
, PCATCH
| PINTERLOCKED
, "piperd", 0);
581 ++pipe_rblocked_count
;
585 pipe_end_uio(rpipe
, &rpipe
->pipe_rip
);
588 * Uptime last access time
590 if (error
== 0 && nread
)
591 vfs_timestamp(&rpipe
->pipe_atime
);
594 * If we drained the FIFO more then half way then handle
595 * write blocking hysteresis.
597 * Note that PIPE_WANTW cannot be set by the writer without
598 * it holding both rlock and wlock, so we can test it
599 * while holding just rlock.
603 * Synchronous blocking is done on the pipe involved
605 if (rpipe
->pipe_state
& PIPE_WANTW
) {
606 lwkt_gettoken(&rpipe
->pipe_wlock
);
607 if (rpipe
->pipe_state
& PIPE_WANTW
) {
608 rpipe
->pipe_state
&= ~PIPE_WANTW
;
609 lwkt_reltoken(&rpipe
->pipe_wlock
);
612 lwkt_reltoken(&rpipe
->pipe_wlock
);
617 * But we may also have to deal with a kqueue which is
618 * stored on the same pipe as its descriptor, so a
619 * EVFILT_WRITE event waiting for our side to drain will
620 * be on the other side.
622 lwkt_gettoken(&wpipe
->pipe_wlock
);
623 pipewakeup(wpipe
, 0);
624 lwkt_reltoken(&wpipe
->pipe_wlock
);
626 /*size = rpipe->pipe_buffer.windex - rpipe->pipe_buffer.rindex;*/
627 lwkt_reltoken(&rpipe
->pipe_rlock
);
633 pipe_write(struct file
*fp
, struct uio
*uio
, struct ucred
*cred
, int fflags
)
647 * Writes go to the peer. The peer will always exist.
649 rpipe
= (struct pipe
*) fp
->f_data
;
650 wpipe
= rpipe
->pipe_peer
;
651 lwkt_gettoken(&wpipe
->pipe_wlock
);
652 if (wpipe
->pipe_state
& PIPE_WEOF
) {
653 lwkt_reltoken(&wpipe
->pipe_wlock
);
658 * Degenerate case (EPIPE takes prec)
660 if (uio
->uio_resid
== 0) {
661 lwkt_reltoken(&wpipe
->pipe_wlock
);
666 * Writes are serialized (start_uio must be called with wlock)
668 error
= pipe_start_uio(wpipe
, &wpipe
->pipe_wip
);
670 lwkt_reltoken(&wpipe
->pipe_wlock
);
674 if (fflags
& O_FBLOCKING
)
676 else if (fflags
& O_FNONBLOCKING
)
678 else if (fp
->f_flag
& O_NONBLOCK
)
684 * If it is advantageous to resize the pipe buffer, do
685 * so. We are write-serialized so we can block safely.
687 if ((wpipe
->pipe_buffer
.size
<= PIPE_SIZE
) &&
688 (pipe_nbig
< pipe_maxbig
) &&
689 wpipe
->pipe_wantwcnt
> 4 &&
690 (wpipe
->pipe_buffer
.rindex
== wpipe
->pipe_buffer
.windex
)) {
692 * Recheck after lock.
694 lwkt_gettoken(&wpipe
->pipe_rlock
);
695 if ((wpipe
->pipe_buffer
.size
<= PIPE_SIZE
) &&
696 (pipe_nbig
< pipe_maxbig
) &&
697 (wpipe
->pipe_buffer
.rindex
== wpipe
->pipe_buffer
.windex
)) {
698 atomic_add_int(&pipe_nbig
, 1);
699 if (pipespace(wpipe
, BIG_PIPE_SIZE
) == 0)
702 atomic_subtract_int(&pipe_nbig
, 1);
704 lwkt_reltoken(&wpipe
->pipe_rlock
);
707 orig_resid
= uio
->uio_resid
;
710 bigwrite
= (uio
->uio_resid
> 10 * 1024 * 1024);
713 while (uio
->uio_resid
) {
714 if (wpipe
->pipe_state
& PIPE_WEOF
) {
722 if (bigwrite
&& --bigcount
== 0) {
725 if (CURSIG(curthread
->td_lwp
)) {
731 windex
= wpipe
->pipe_buffer
.windex
&
732 (wpipe
->pipe_buffer
.size
- 1);
733 space
= wpipe
->pipe_buffer
.size
-
734 (wpipe
->pipe_buffer
.windex
- wpipe
->pipe_buffer
.rindex
);
737 /* Writes of size <= PIPE_BUF must be atomic. */
738 if ((space
< uio
->uio_resid
) && (orig_resid
<= PIPE_BUF
))
742 * Write to fill, read size handles write hysteresis. Also
743 * additional restrictions can cause select-based non-blocking
750 * Transfer size is minimum of uio transfer
751 * and free space in pipe buffer.
753 * Limit each uiocopy to no more then PIPE_SIZE
754 * so we can keep the gravy train going on a
755 * SMP box. This doubles the performance for
756 * write sizes > 16K. Otherwise large writes
757 * wind up doing an inefficient synchronous
760 space
= szmin(space
, uio
->uio_resid
);
761 if (space
> PIPE_SIZE
)
765 * First segment to transfer is minimum of
766 * transfer size and contiguous space in
767 * pipe buffer. If first segment to transfer
768 * is less than the transfer size, we've got
769 * a wraparound in the buffer.
771 segsize
= wpipe
->pipe_buffer
.size
- windex
;
777 * If this is the first loop and the reader is
778 * blocked, do a preemptive wakeup of the reader.
780 * On SMP the IPI latency plus the wlock interlock
781 * on the reader side is the fastest way to get the
782 * reader going. (The scheduler will hard loop on
785 * NOTE: We can't clear WANTR here without acquiring
786 * the rlock, which we don't want to do here!
788 if ((wpipe
->pipe_state
& PIPE_WANTR
))
793 * Transfer segment, which may include a wrap-around.
794 * Update windex to account for both all in one go
795 * so the reader can read() the data atomically.
797 error
= uiomove(&wpipe
->pipe_buffer
.buffer
[windex
],
799 if (error
== 0 && segsize
< space
) {
800 segsize
= space
- segsize
;
801 error
= uiomove(&wpipe
->pipe_buffer
.buffer
[0],
807 wpipe
->pipe_buffer
.windex
+= space
;
813 * We need both the rlock and the wlock to interlock against
814 * the EOF, WANTW, and size checks, and to modify pipe_state.
816 * These are token locks so we do not have to worry about
819 lwkt_gettoken(&wpipe
->pipe_rlock
);
822 * If the "read-side" has been blocked, wake it up now
823 * and yield to let it drain synchronously rather
826 if (wpipe
->pipe_state
& PIPE_WANTR
) {
827 wpipe
->pipe_state
&= ~PIPE_WANTR
;
832 * don't block on non-blocking I/O
835 lwkt_reltoken(&wpipe
->pipe_rlock
);
841 * re-test whether we have to block in the writer after
842 * acquiring both locks, in case the reader opened up
845 space
= wpipe
->pipe_buffer
.size
-
846 (wpipe
->pipe_buffer
.windex
- wpipe
->pipe_buffer
.rindex
);
848 if ((space
< uio
->uio_resid
) && (orig_resid
<= PIPE_BUF
))
852 * Retest EOF - acquiring a new token can temporarily release
853 * tokens already held.
855 if (wpipe
->pipe_state
& PIPE_WEOF
) {
856 lwkt_reltoken(&wpipe
->pipe_rlock
);
862 * We have no more space and have something to offer,
863 * wake up select/poll/kq.
866 wpipe
->pipe_state
|= PIPE_WANTW
;
867 ++wpipe
->pipe_wantwcnt
;
868 pipewakeup(wpipe
, 1);
869 if (wpipe
->pipe_state
& PIPE_WANTW
)
870 error
= tsleep(wpipe
, PCATCH
, "pipewr", 0);
871 ++pipe_wblocked_count
;
873 lwkt_reltoken(&wpipe
->pipe_rlock
);
876 * Break out if we errored or the read side wants us to go
881 if (wpipe
->pipe_state
& PIPE_WEOF
) {
886 pipe_end_uio(wpipe
, &wpipe
->pipe_wip
);
889 * If we have put any characters in the buffer, we wake up
892 * Both rlock and wlock are required to be able to modify pipe_state.
894 if (wpipe
->pipe_buffer
.windex
!= wpipe
->pipe_buffer
.rindex
) {
895 if (wpipe
->pipe_state
& PIPE_WANTR
) {
896 lwkt_gettoken(&wpipe
->pipe_rlock
);
897 if (wpipe
->pipe_state
& PIPE_WANTR
) {
898 wpipe
->pipe_state
&= ~PIPE_WANTR
;
899 lwkt_reltoken(&wpipe
->pipe_rlock
);
902 lwkt_reltoken(&wpipe
->pipe_rlock
);
905 lwkt_gettoken(&wpipe
->pipe_rlock
);
906 pipewakeup(wpipe
, 1);
907 lwkt_reltoken(&wpipe
->pipe_rlock
);
911 * Don't return EPIPE if I/O was successful
913 if ((wpipe
->pipe_buffer
.rindex
== wpipe
->pipe_buffer
.windex
) &&
914 (uio
->uio_resid
== 0) &&
920 vfs_timestamp(&wpipe
->pipe_mtime
);
923 * We have something to offer,
924 * wake up select/poll/kq.
926 /*space = wpipe->pipe_buffer.windex - wpipe->pipe_buffer.rindex;*/
927 lwkt_reltoken(&wpipe
->pipe_wlock
);
932 * we implement a very minimal set of ioctls for compatibility with sockets.
935 pipe_ioctl(struct file
*fp
, u_long cmd
, caddr_t data
,
936 struct ucred
*cred
, struct sysmsg
*msg
)
941 mpipe
= (struct pipe
*)fp
->f_data
;
943 lwkt_gettoken(&mpipe
->pipe_rlock
);
944 lwkt_gettoken(&mpipe
->pipe_wlock
);
949 mpipe
->pipe_state
|= PIPE_ASYNC
;
951 mpipe
->pipe_state
&= ~PIPE_ASYNC
;
956 *(int *)data
= mpipe
->pipe_buffer
.windex
-
957 mpipe
->pipe_buffer
.rindex
;
961 lwkt_gettoken(&proc_token
);
962 error
= fsetown(*(int *)data
, &mpipe
->pipe_sigio
);
963 lwkt_reltoken(&proc_token
);
966 *(int *)data
= fgetown(mpipe
->pipe_sigio
);
970 /* This is deprecated, FIOSETOWN should be used instead. */
971 lwkt_gettoken(&proc_token
);
972 error
= fsetown(-(*(int *)data
), &mpipe
->pipe_sigio
);
973 lwkt_reltoken(&proc_token
);
977 /* This is deprecated, FIOGETOWN should be used instead. */
978 *(int *)data
= -fgetown(mpipe
->pipe_sigio
);
985 lwkt_reltoken(&mpipe
->pipe_wlock
);
986 lwkt_reltoken(&mpipe
->pipe_rlock
);
995 pipe_stat(struct file
*fp
, struct stat
*ub
, struct ucred
*cred
)
999 pipe
= (struct pipe
*)fp
->f_data
;
1001 bzero((caddr_t
)ub
, sizeof(*ub
));
1002 ub
->st_mode
= S_IFIFO
;
1003 ub
->st_blksize
= pipe
->pipe_buffer
.size
;
1004 ub
->st_size
= pipe
->pipe_buffer
.windex
- pipe
->pipe_buffer
.rindex
;
1005 ub
->st_blocks
= (ub
->st_size
+ ub
->st_blksize
- 1) / ub
->st_blksize
;
1006 ub
->st_atimespec
= pipe
->pipe_atime
;
1007 ub
->st_mtimespec
= pipe
->pipe_mtime
;
1008 ub
->st_ctimespec
= pipe
->pipe_ctime
;
1010 * Left as 0: st_dev, st_ino, st_nlink, st_uid, st_gid, st_rdev,
1012 * XXX (st_dev, st_ino) should be unique.
1018 pipe_close(struct file
*fp
)
1022 cpipe
= (struct pipe
*)fp
->f_data
;
1023 fp
->f_ops
= &badfileops
;
1025 lwkt_gettoken(&proc_token
);
1026 funsetown(cpipe
->pipe_sigio
);
1027 lwkt_reltoken(&proc_token
);
1033 * Shutdown one or both directions of a full-duplex pipe.
1036 pipe_shutdown(struct file
*fp
, int how
)
1042 rpipe
= (struct pipe
*)fp
->f_data
;
1043 wpipe
= rpipe
->pipe_peer
;
1046 * We modify pipe_state on both pipes, which means we need
1049 lwkt_gettoken(&rpipe
->pipe_rlock
);
1050 lwkt_gettoken(&rpipe
->pipe_wlock
);
1051 lwkt_gettoken(&wpipe
->pipe_rlock
);
1052 lwkt_gettoken(&wpipe
->pipe_wlock
);
1057 rpipe
->pipe_state
|= PIPE_REOF
; /* my reads */
1058 rpipe
->pipe_state
|= PIPE_WEOF
; /* peer writes */
1059 if (rpipe
->pipe_state
& PIPE_WANTR
) {
1060 rpipe
->pipe_state
&= ~PIPE_WANTR
;
1063 if (rpipe
->pipe_state
& PIPE_WANTW
) {
1064 rpipe
->pipe_state
&= ~PIPE_WANTW
;
1072 wpipe
->pipe_state
|= PIPE_REOF
; /* peer reads */
1073 wpipe
->pipe_state
|= PIPE_WEOF
; /* my writes */
1074 if (wpipe
->pipe_state
& PIPE_WANTR
) {
1075 wpipe
->pipe_state
&= ~PIPE_WANTR
;
1078 if (wpipe
->pipe_state
& PIPE_WANTW
) {
1079 wpipe
->pipe_state
&= ~PIPE_WANTW
;
1085 pipewakeup(rpipe
, 1);
1086 pipewakeup(wpipe
, 1);
1088 lwkt_reltoken(&wpipe
->pipe_wlock
);
1089 lwkt_reltoken(&wpipe
->pipe_rlock
);
1090 lwkt_reltoken(&rpipe
->pipe_wlock
);
1091 lwkt_reltoken(&rpipe
->pipe_rlock
);
1097 pipe_free_kmem(struct pipe
*cpipe
)
1099 if (cpipe
->pipe_buffer
.buffer
!= NULL
) {
1100 if (cpipe
->pipe_buffer
.size
> PIPE_SIZE
)
1101 atomic_subtract_int(&pipe_nbig
, 1);
1102 kmem_free(&kernel_map
,
1103 (vm_offset_t
)cpipe
->pipe_buffer
.buffer
,
1104 cpipe
->pipe_buffer
.size
);
1105 cpipe
->pipe_buffer
.buffer
= NULL
;
1106 cpipe
->pipe_buffer
.object
= NULL
;
1111 * Close the pipe. The slock must be held to interlock against simultanious
1112 * closes. The rlock and wlock must be held to adjust the pipe_state.
1115 pipeclose(struct pipe
*cpipe
)
1124 * The slock may not have been allocated yet (close during
1127 * We need both the read and write tokens to modify pipe_state.
1129 if (cpipe
->pipe_slock
)
1130 lockmgr(cpipe
->pipe_slock
, LK_EXCLUSIVE
);
1131 lwkt_gettoken(&cpipe
->pipe_rlock
);
1132 lwkt_gettoken(&cpipe
->pipe_wlock
);
1135 * Set our state, wakeup anyone waiting in select/poll/kq, and
1136 * wakeup anyone blocked on our pipe.
1138 cpipe
->pipe_state
|= PIPE_CLOSED
| PIPE_REOF
| PIPE_WEOF
;
1139 pipewakeup(cpipe
, 1);
1140 if (cpipe
->pipe_state
& (PIPE_WANTR
| PIPE_WANTW
)) {
1141 cpipe
->pipe_state
&= ~(PIPE_WANTR
| PIPE_WANTW
);
1146 * Disconnect from peer.
1148 if ((ppipe
= cpipe
->pipe_peer
) != NULL
) {
1149 lwkt_gettoken(&ppipe
->pipe_rlock
);
1150 lwkt_gettoken(&ppipe
->pipe_wlock
);
1151 ppipe
->pipe_state
|= PIPE_REOF
| PIPE_WEOF
;
1152 pipewakeup(ppipe
, 1);
1153 if (ppipe
->pipe_state
& (PIPE_WANTR
| PIPE_WANTW
)) {
1154 ppipe
->pipe_state
&= ~(PIPE_WANTR
| PIPE_WANTW
);
1157 if (SLIST_FIRST(&ppipe
->pipe_kq
.ki_note
))
1158 KNOTE(&ppipe
->pipe_kq
.ki_note
, 0);
1159 lwkt_reltoken(&ppipe
->pipe_wlock
);
1160 lwkt_reltoken(&ppipe
->pipe_rlock
);
1164 * If the peer is also closed we can free resources for both
1165 * sides, otherwise we leave our side intact to deal with any
1166 * races (since we only have the slock).
1168 if (ppipe
&& (ppipe
->pipe_state
& PIPE_CLOSED
)) {
1169 cpipe
->pipe_peer
= NULL
;
1170 ppipe
->pipe_peer
= NULL
;
1171 ppipe
->pipe_slock
= NULL
; /* we will free the slock */
1176 lwkt_reltoken(&cpipe
->pipe_wlock
);
1177 lwkt_reltoken(&cpipe
->pipe_rlock
);
1178 if (cpipe
->pipe_slock
)
1179 lockmgr(cpipe
->pipe_slock
, LK_RELEASE
);
1182 * If we disassociated from our peer we can free resources
1184 if (ppipe
== NULL
) {
1186 if (cpipe
->pipe_slock
) {
1187 kfree(cpipe
->pipe_slock
, M_PIPE
);
1188 cpipe
->pipe_slock
= NULL
;
1190 if (gd
->gd_pipeqcount
>= pipe_maxcache
||
1191 cpipe
->pipe_buffer
.size
!= PIPE_SIZE
1193 pipe_free_kmem(cpipe
);
1194 kfree(cpipe
, M_PIPE
);
1196 cpipe
->pipe_state
= 0;
1197 cpipe
->pipe_peer
= gd
->gd_pipeq
;
1198 gd
->gd_pipeq
= cpipe
;
1199 ++gd
->gd_pipeqcount
;
1205 pipe_kqfilter(struct file
*fp
, struct knote
*kn
)
1209 cpipe
= (struct pipe
*)kn
->kn_fp
->f_data
;
1211 switch (kn
->kn_filter
) {
1213 kn
->kn_fop
= &pipe_rfiltops
;
1216 kn
->kn_fop
= &pipe_wfiltops
;
1217 if (cpipe
->pipe_peer
== NULL
) {
1218 /* other end of pipe has been closed */
1223 return (EOPNOTSUPP
);
1225 kn
->kn_hook
= (caddr_t
)cpipe
;
1227 knote_insert(&cpipe
->pipe_kq
.ki_note
, kn
);
1233 filt_pipedetach(struct knote
*kn
)
1235 struct pipe
*cpipe
= (struct pipe
*)kn
->kn_hook
;
1237 knote_remove(&cpipe
->pipe_kq
.ki_note
, kn
);
1242 filt_piperead(struct knote
*kn
, long hint
)
1244 struct pipe
*rpipe
= (struct pipe
*)kn
->kn_fp
->f_data
;
1247 lwkt_gettoken(&rpipe
->pipe_rlock
);
1248 lwkt_gettoken(&rpipe
->pipe_wlock
);
1250 kn
->kn_data
= rpipe
->pipe_buffer
.windex
- rpipe
->pipe_buffer
.rindex
;
1253 * Only set EOF if all data has been exhausted
1255 if ((rpipe
->pipe_state
& PIPE_REOF
) && kn
->kn_data
== 0) {
1256 kn
->kn_flags
|= EV_EOF
;
1260 lwkt_reltoken(&rpipe
->pipe_wlock
);
1261 lwkt_reltoken(&rpipe
->pipe_rlock
);
1264 ready
= kn
->kn_data
> 0;
1271 filt_pipewrite(struct knote
*kn
, long hint
)
1273 struct pipe
*rpipe
= (struct pipe
*)kn
->kn_fp
->f_data
;
1274 struct pipe
*wpipe
= rpipe
->pipe_peer
;
1278 if (wpipe
== NULL
) {
1279 kn
->kn_flags
|= EV_EOF
;
1283 lwkt_gettoken(&wpipe
->pipe_rlock
);
1284 lwkt_gettoken(&wpipe
->pipe_wlock
);
1286 if (wpipe
->pipe_state
& PIPE_WEOF
) {
1287 kn
->kn_flags
|= EV_EOF
;
1292 kn
->kn_data
= wpipe
->pipe_buffer
.size
-
1293 (wpipe
->pipe_buffer
.windex
-
1294 wpipe
->pipe_buffer
.rindex
);
1296 lwkt_reltoken(&wpipe
->pipe_wlock
);
1297 lwkt_reltoken(&wpipe
->pipe_rlock
);
1300 ready
= kn
->kn_data
>= PIPE_BUF
;