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 $
23 * This file contains a high-performance replacement for the socket-based
24 * pipes scheme originally used in FreeBSD/4.4Lite. It does not support
25 * all features of sockets, but does do everything that pipes normally
28 #include <sys/param.h>
29 #include <sys/systm.h>
30 #include <sys/kernel.h>
32 #include <sys/fcntl.h>
34 #include <sys/filedesc.h>
35 #include <sys/filio.h>
36 #include <sys/ttycom.h>
38 #include <sys/signalvar.h>
39 #include <sys/sysproto.h>
41 #include <sys/vnode.h>
43 #include <sys/event.h>
44 #include <sys/globaldata.h>
45 #include <sys/module.h>
46 #include <sys/malloc.h>
47 #include <sys/sysctl.h>
48 #include <sys/socket.h>
51 #include <vm/vm_param.h>
53 #include <vm/vm_object.h>
54 #include <vm/vm_kern.h>
55 #include <vm/vm_extern.h>
57 #include <vm/vm_map.h>
58 #include <vm/vm_page.h>
59 #include <vm/vm_zone.h>
61 #include <sys/file2.h>
62 #include <sys/signal2.h>
64 #include <machine/cpufunc.h>
67 * interfaces to the outside world
69 static int pipe_read (struct file *fp, struct uio *uio,
70 struct ucred *cred, int flags);
71 static int pipe_write (struct file *fp, struct uio *uio,
72 struct ucred *cred, int flags);
73 static int pipe_close (struct file *fp);
74 static int pipe_shutdown (struct file *fp, int how);
75 static int pipe_kqfilter (struct file *fp, struct knote *kn);
76 static int pipe_stat (struct file *fp, struct stat *sb, struct ucred *cred);
77 static int pipe_ioctl (struct file *fp, u_long cmd, caddr_t data,
78 struct ucred *cred, struct sysmsg *msg);
80 static struct fileops pipeops = {
82 .fo_write = pipe_write,
83 .fo_ioctl = pipe_ioctl,
84 .fo_kqfilter = pipe_kqfilter,
86 .fo_close = pipe_close,
87 .fo_shutdown = pipe_shutdown
90 static void filt_pipedetach(struct knote *kn);
91 static int filt_piperead(struct knote *kn, long hint);
92 static int filt_pipewrite(struct knote *kn, long hint);
94 static struct filterops pipe_rfiltops =
95 { FILTEROP_ISFD|FILTEROP_MPSAFE, NULL, filt_pipedetach, filt_piperead };
96 static struct filterops pipe_wfiltops =
97 { FILTEROP_ISFD|FILTEROP_MPSAFE, NULL, filt_pipedetach, filt_pipewrite };
99 MALLOC_DEFINE(M_PIPE, "pipe", "pipe structures");
102 * Default pipe buffer size(s), this can be kind-of large now because pipe
103 * space is pageable. The pipe code will try to maintain locality of
104 * reference for performance reasons, so small amounts of outstanding I/O
105 * will not wipe the cache.
107 #define MINPIPESIZE (PIPE_SIZE/3)
108 #define MAXPIPESIZE (2*PIPE_SIZE/3)
111 * Limit the number of "big" pipes
113 #define LIMITBIGPIPES 64
114 #define PIPEQ_MAX_CACHE 16 /* per-cpu pipe structure cache */
116 static int pipe_maxbig = LIMITBIGPIPES;
117 static int pipe_maxcache = PIPEQ_MAX_CACHE;
118 static int pipe_bigcount;
119 static int pipe_nbig;
120 static int pipe_bcache_alloc;
121 static int pipe_bkmem_alloc;
122 static int pipe_rblocked_count;
123 static int pipe_wblocked_count;
125 SYSCTL_NODE(_kern, OID_AUTO, pipe, CTLFLAG_RW, 0, "Pipe operation");
126 SYSCTL_INT(_kern_pipe, OID_AUTO, nbig,
127 CTLFLAG_RD, &pipe_nbig, 0, "number of big pipes allocated");
128 SYSCTL_INT(_kern_pipe, OID_AUTO, bigcount,
129 CTLFLAG_RW, &pipe_bigcount, 0, "number of times pipe expanded");
130 SYSCTL_INT(_kern_pipe, OID_AUTO, rblocked,
131 CTLFLAG_RW, &pipe_rblocked_count, 0, "number of times pipe expanded");
132 SYSCTL_INT(_kern_pipe, OID_AUTO, wblocked,
133 CTLFLAG_RW, &pipe_wblocked_count, 0, "number of times pipe expanded");
134 SYSCTL_INT(_kern_pipe, OID_AUTO, maxcache,
135 CTLFLAG_RW, &pipe_maxcache, 0, "max pipes cached per-cpu");
136 SYSCTL_INT(_kern_pipe, OID_AUTO, maxbig,
137 CTLFLAG_RW, &pipe_maxbig, 0, "max number of big pipes");
138 static int pipe_delay = 5000; /* 5uS default */
139 SYSCTL_INT(_kern_pipe, OID_AUTO, delay,
140 CTLFLAG_RW, &pipe_delay, 0, "SMP delay optimization in ns");
141 #if !defined(NO_PIPE_SYSCTL_STATS)
142 SYSCTL_INT(_kern_pipe, OID_AUTO, bcache_alloc,
143 CTLFLAG_RW, &pipe_bcache_alloc, 0, "pipe buffer from pcpu cache");
144 SYSCTL_INT(_kern_pipe, OID_AUTO, bkmem_alloc,
145 CTLFLAG_RW, &pipe_bkmem_alloc, 0, "pipe buffer from kmem");
149 * Auto-size pipe cache to reduce kmem allocations and frees.
153 pipeinit(void *dummy)
155 size_t mbytes = kmem_lim_size();
157 if (pipe_maxbig == LIMITBIGPIPES) {
158 if (mbytes >= 7 * 1024)
160 if (mbytes >= 15 * 1024)
163 if (pipe_maxcache == PIPEQ_MAX_CACHE) {
164 if (mbytes >= 7 * 1024)
166 if (mbytes >= 15 * 1024)
170 SYSINIT(kmem, SI_BOOT2_MACHDEP, SI_ORDER_ANY, pipeinit, NULL)
172 static void pipeclose (struct pipe *cpipe);
173 static void pipe_free_kmem (struct pipe *cpipe);
174 static int pipe_create (struct pipe **cpipep);
175 static int pipespace (struct pipe *cpipe, int size);
178 pipewakeup(struct pipe *cpipe, int dosigio)
180 if (dosigio && (cpipe->pipe_state & PIPE_ASYNC) && cpipe->pipe_sigio) {
181 lwkt_gettoken(&sigio_token);
182 pgsigio(cpipe->pipe_sigio, SIGIO, 0);
183 lwkt_reltoken(&sigio_token);
185 KNOTE(&cpipe->pipe_kq.ki_note, 0);
189 * These routines are called before and after a UIO. The UIO
190 * may block, causing our held tokens to be lost temporarily.
192 * We use these routines to serialize reads against other reads
193 * and writes against other writes.
195 * The read token is held on entry so *ipp does not race.
198 pipe_start_uio(struct pipe *cpipe, int *ipp)
204 error = tsleep(ipp, PCATCH, "pipexx", 0);
213 pipe_end_uio(struct pipe *cpipe, int *ipp)
225 * The pipe system call for the DTYPE_PIPE type of pipes
227 * pipe_args(int dummy)
232 sys_pipe(struct pipe_args *uap)
234 struct thread *td = curthread;
235 struct filedesc *fdp = td->td_proc->p_fd;
236 struct file *rf, *wf;
237 struct pipe *rpipe, *wpipe;
240 rpipe = wpipe = NULL;
241 if (pipe_create(&rpipe) || pipe_create(&wpipe)) {
247 error = falloc(td->td_lwp, &rf, &fd1);
253 uap->sysmsg_fds[0] = fd1;
256 * Warning: once we've gotten past allocation of the fd for the
257 * read-side, we can only drop the read side via fdrop() in order
258 * to avoid races against processes which manage to dup() the read
259 * side while we are blocked trying to allocate the write side.
261 rf->f_type = DTYPE_PIPE;
262 rf->f_flag = FREAD | FWRITE;
263 rf->f_ops = &pipeops;
265 error = falloc(td->td_lwp, &wf, &fd2);
267 fsetfd(fdp, NULL, fd1);
269 /* rpipe has been closed by fdrop(). */
273 wf->f_type = DTYPE_PIPE;
274 wf->f_flag = FREAD | FWRITE;
275 wf->f_ops = &pipeops;
277 uap->sysmsg_fds[1] = fd2;
279 rpipe->pipe_slock = kmalloc(sizeof(struct lock),
280 M_PIPE, M_WAITOK|M_ZERO);
281 wpipe->pipe_slock = rpipe->pipe_slock;
282 rpipe->pipe_peer = wpipe;
283 wpipe->pipe_peer = rpipe;
284 lockinit(rpipe->pipe_slock, "pipecl", 0, 0);
287 * Once activated the peer relationship remains valid until
288 * both sides are closed.
290 fsetfd(fdp, rf, fd1);
291 fsetfd(fdp, wf, fd2);
299 * Allocate kva for pipe circular buffer, the space is pageable
300 * This routine will 'realloc' the size of a pipe safely, if it fails
301 * it will retain the old buffer.
302 * If it fails it will return ENOMEM.
305 pipespace(struct pipe *cpipe, int size)
307 struct vm_object *object;
311 npages = round_page(size) / PAGE_SIZE;
312 object = cpipe->pipe_buffer.object;
315 * [re]create the object if necessary and reserve space for it
316 * in the kernel_map. The object and memory are pageable. On
317 * success, free the old resources before assigning the new
320 if (object == NULL || object->size != npages) {
321 object = vm_object_allocate(OBJT_DEFAULT, npages);
322 buffer = (caddr_t)vm_map_min(&kernel_map);
324 error = vm_map_find(&kernel_map, object, NULL,
325 0, (vm_offset_t *)&buffer, size,
327 1, VM_MAPTYPE_NORMAL,
328 VM_PROT_ALL, VM_PROT_ALL, 0);
330 if (error != KERN_SUCCESS) {
331 vm_object_deallocate(object);
334 pipe_free_kmem(cpipe);
335 cpipe->pipe_buffer.object = object;
336 cpipe->pipe_buffer.buffer = buffer;
337 cpipe->pipe_buffer.size = size;
342 cpipe->pipe_buffer.rindex = 0;
343 cpipe->pipe_buffer.windex = 0;
348 * Initialize and allocate VM and memory for pipe, pulling the pipe from
349 * our per-cpu cache if possible. For now make sure it is sized for the
350 * smaller PIPE_SIZE default.
353 pipe_create(struct pipe **cpipep)
355 globaldata_t gd = mycpu;
359 if ((cpipe = gd->gd_pipeq) != NULL) {
360 gd->gd_pipeq = cpipe->pipe_peer;
362 cpipe->pipe_peer = NULL;
363 cpipe->pipe_wantwcnt = 0;
365 cpipe = kmalloc(sizeof(struct pipe), M_PIPE, M_WAITOK|M_ZERO);
368 if ((error = pipespace(cpipe, PIPE_SIZE)) != 0)
370 vfs_timestamp(&cpipe->pipe_ctime);
371 cpipe->pipe_atime = cpipe->pipe_ctime;
372 cpipe->pipe_mtime = cpipe->pipe_ctime;
373 lwkt_token_init(&cpipe->pipe_rlock, "piper");
374 lwkt_token_init(&cpipe->pipe_wlock, "pipew");
379 pipe_read(struct file *fp, struct uio *uio, struct ucred *cred, int fflags)
386 u_int size; /* total bytes available */
387 u_int nsize; /* total bytes to read */
388 u_int rindex; /* contiguous bytes available */
393 atomic_set_int(&curthread->td_mpflags, TDF_MP_BATCH_DEMARC);
395 if (uio->uio_resid == 0)
399 * Setup locks, calculate nbio
401 rpipe = (struct pipe *)fp->f_data;
402 wpipe = rpipe->pipe_peer;
403 lwkt_gettoken(&rpipe->pipe_rlock);
405 if (fflags & O_FBLOCKING)
407 else if (fflags & O_FNONBLOCKING)
409 else if (fp->f_flag & O_NONBLOCK)
415 * Reads are serialized. Note however that pipe_buffer.buffer and
416 * pipe_buffer.size can change out from under us when the number
417 * of bytes in the buffer are zero due to the write-side doing a
420 error = pipe_start_uio(rpipe, &rpipe->pipe_rip);
422 lwkt_reltoken(&rpipe->pipe_rlock);
427 bigread = (uio->uio_resid > 10 * 1024 * 1024);
430 while (uio->uio_resid) {
434 if (bigread && --bigcount == 0) {
437 if (CURSIG(curthread->td_lwp)) {
443 size = rpipe->pipe_buffer.windex - rpipe->pipe_buffer.rindex;
446 rindex = rpipe->pipe_buffer.rindex &
447 (rpipe->pipe_buffer.size - 1);
449 if (nsize > rpipe->pipe_buffer.size - rindex)
450 nsize = rpipe->pipe_buffer.size - rindex;
451 nsize = szmin(nsize, uio->uio_resid);
453 error = uiomove(&rpipe->pipe_buffer.buffer[rindex],
458 rpipe->pipe_buffer.rindex += nsize;
462 * If the FIFO is still over half full just continue
463 * and do not try to notify the writer yet.
465 if (size - nsize >= (rpipe->pipe_buffer.size >> 1)) {
471 * When the FIFO is less then half full notify any
472 * waiting writer. WANTW can be checked while
473 * holding just the rlock.
476 if ((rpipe->pipe_state & PIPE_WANTW) == 0)
481 * If the "write-side" was blocked we wake it up. This code
482 * is reached either when the buffer is completely emptied
483 * or if it becomes more then half-empty.
485 * Pipe_state can only be modified if both the rlock and
488 if (rpipe->pipe_state & PIPE_WANTW) {
489 lwkt_gettoken(&rpipe->pipe_wlock);
490 if (rpipe->pipe_state & PIPE_WANTW) {
491 rpipe->pipe_state &= ~PIPE_WANTW;
492 lwkt_reltoken(&rpipe->pipe_wlock);
495 lwkt_reltoken(&rpipe->pipe_wlock);
500 * Pick up our copy loop again if the writer sent data to
501 * us while we were messing around.
503 * On a SMP box poll up to pipe_delay nanoseconds for new
504 * data. Typically a value of 2000 to 4000 is sufficient
505 * to eradicate most IPIs/tsleeps/wakeups when a pipe
506 * is used for synchronous communications with small packets,
507 * and 8000 or so (8uS) will pipeline large buffer xfers
508 * between cpus over a pipe.
510 * For synchronous communications a hit means doing a
511 * full Awrite-Bread-Bwrite-Aread cycle in less then 2uS,
512 * where as miss requiring a tsleep/wakeup sequence
513 * will take 7uS or more.
515 if (rpipe->pipe_buffer.windex != rpipe->pipe_buffer.rindex)
518 #ifdef _RDTSC_SUPPORTED_
523 tsc_target = tsc_get_target(pipe_delay);
524 while (tsc_test_target(tsc_target) == 0) {
525 if (rpipe->pipe_buffer.windex !=
526 rpipe->pipe_buffer.rindex) {
537 * Detect EOF condition, do not set error.
539 if (rpipe->pipe_state & PIPE_REOF)
543 * Break if some data was read, or if this was a non-blocking
555 * Last chance, interlock with WANTR.
557 lwkt_gettoken(&rpipe->pipe_wlock);
558 size = rpipe->pipe_buffer.windex - rpipe->pipe_buffer.rindex;
560 lwkt_reltoken(&rpipe->pipe_wlock);
565 * Retest EOF - acquiring a new token can temporarily release
566 * tokens already held.
568 if (rpipe->pipe_state & PIPE_REOF) {
569 lwkt_reltoken(&rpipe->pipe_wlock);
574 * If there is no more to read in the pipe, reset its
575 * pointers to the beginning. This improves cache hit
578 * We need both locks to modify both pointers, and there
579 * must also not be a write in progress or the uiomove()
580 * in the write might block and temporarily release
581 * its wlock, then reacquire and update windex. We are
582 * only serialized against reads, not writes.
584 * XXX should we even bother resetting the indices? It
585 * might actually be more cache efficient not to.
587 if (rpipe->pipe_buffer.rindex == rpipe->pipe_buffer.windex &&
588 rpipe->pipe_wip == 0) {
589 rpipe->pipe_buffer.rindex = 0;
590 rpipe->pipe_buffer.windex = 0;
594 * Wait for more data.
596 * Pipe_state can only be set if both the rlock and wlock
599 rpipe->pipe_state |= PIPE_WANTR;
600 tsleep_interlock(rpipe, PCATCH);
601 lwkt_reltoken(&rpipe->pipe_wlock);
602 error = tsleep(rpipe, PCATCH | PINTERLOCKED, "piperd", 0);
603 ++pipe_rblocked_count;
607 pipe_end_uio(rpipe, &rpipe->pipe_rip);
610 * Uptime last access time
612 if (error == 0 && nread)
613 vfs_timestamp(&rpipe->pipe_atime);
616 * If we drained the FIFO more then half way then handle
617 * write blocking hysteresis.
619 * Note that PIPE_WANTW cannot be set by the writer without
620 * it holding both rlock and wlock, so we can test it
621 * while holding just rlock.
625 * Synchronous blocking is done on the pipe involved
627 if (rpipe->pipe_state & PIPE_WANTW) {
628 lwkt_gettoken(&rpipe->pipe_wlock);
629 if (rpipe->pipe_state & PIPE_WANTW) {
630 rpipe->pipe_state &= ~PIPE_WANTW;
631 lwkt_reltoken(&rpipe->pipe_wlock);
634 lwkt_reltoken(&rpipe->pipe_wlock);
639 * But we may also have to deal with a kqueue which is
640 * stored on the same pipe as its descriptor, so a
641 * EVFILT_WRITE event waiting for our side to drain will
642 * be on the other side.
644 lwkt_gettoken(&wpipe->pipe_wlock);
645 pipewakeup(wpipe, 0);
646 lwkt_reltoken(&wpipe->pipe_wlock);
648 /*size = rpipe->pipe_buffer.windex - rpipe->pipe_buffer.rindex;*/
649 lwkt_reltoken(&rpipe->pipe_rlock);
655 pipe_write(struct file *fp, struct uio *uio, struct ucred *cred, int fflags)
669 * Writes go to the peer. The peer will always exist.
671 rpipe = (struct pipe *) fp->f_data;
672 wpipe = rpipe->pipe_peer;
673 lwkt_gettoken(&wpipe->pipe_wlock);
674 if (wpipe->pipe_state & PIPE_WEOF) {
675 lwkt_reltoken(&wpipe->pipe_wlock);
680 * Degenerate case (EPIPE takes prec)
682 if (uio->uio_resid == 0) {
683 lwkt_reltoken(&wpipe->pipe_wlock);
688 * Writes are serialized (start_uio must be called with wlock)
690 error = pipe_start_uio(wpipe, &wpipe->pipe_wip);
692 lwkt_reltoken(&wpipe->pipe_wlock);
696 if (fflags & O_FBLOCKING)
698 else if (fflags & O_FNONBLOCKING)
700 else if (fp->f_flag & O_NONBLOCK)
706 * If it is advantageous to resize the pipe buffer, do
707 * so. We are write-serialized so we can block safely.
709 if ((wpipe->pipe_buffer.size <= PIPE_SIZE) &&
710 (pipe_nbig < pipe_maxbig) &&
711 wpipe->pipe_wantwcnt > 4 &&
712 (wpipe->pipe_buffer.rindex == wpipe->pipe_buffer.windex)) {
714 * Recheck after lock.
716 lwkt_gettoken(&wpipe->pipe_rlock);
717 if ((wpipe->pipe_buffer.size <= PIPE_SIZE) &&
718 (pipe_nbig < pipe_maxbig) &&
719 (wpipe->pipe_buffer.rindex == wpipe->pipe_buffer.windex)) {
720 atomic_add_int(&pipe_nbig, 1);
721 if (pipespace(wpipe, BIG_PIPE_SIZE) == 0)
724 atomic_subtract_int(&pipe_nbig, 1);
726 lwkt_reltoken(&wpipe->pipe_rlock);
729 orig_resid = uio->uio_resid;
732 bigwrite = (uio->uio_resid > 10 * 1024 * 1024);
735 while (uio->uio_resid) {
736 if (wpipe->pipe_state & PIPE_WEOF) {
744 if (bigwrite && --bigcount == 0) {
747 if (CURSIG(curthread->td_lwp)) {
753 windex = wpipe->pipe_buffer.windex &
754 (wpipe->pipe_buffer.size - 1);
755 space = wpipe->pipe_buffer.size -
756 (wpipe->pipe_buffer.windex - wpipe->pipe_buffer.rindex);
759 /* Writes of size <= PIPE_BUF must be atomic. */
760 if ((space < uio->uio_resid) && (orig_resid <= PIPE_BUF))
764 * Write to fill, read size handles write hysteresis. Also
765 * additional restrictions can cause select-based non-blocking
772 * Transfer size is minimum of uio transfer
773 * and free space in pipe buffer.
775 * Limit each uiocopy to no more then PIPE_SIZE
776 * so we can keep the gravy train going on a
777 * SMP box. This doubles the performance for
778 * write sizes > 16K. Otherwise large writes
779 * wind up doing an inefficient synchronous
782 space = szmin(space, uio->uio_resid);
783 if (space > PIPE_SIZE)
787 * First segment to transfer is minimum of
788 * transfer size and contiguous space in
789 * pipe buffer. If first segment to transfer
790 * is less than the transfer size, we've got
791 * a wraparound in the buffer.
793 segsize = wpipe->pipe_buffer.size - windex;
798 * If this is the first loop and the reader is
799 * blocked, do a preemptive wakeup of the reader.
801 * On SMP the IPI latency plus the wlock interlock
802 * on the reader side is the fastest way to get the
803 * reader going. (The scheduler will hard loop on
806 * NOTE: We can't clear WANTR here without acquiring
807 * the rlock, which we don't want to do here!
809 if ((wpipe->pipe_state & PIPE_WANTR))
813 * Transfer segment, which may include a wrap-around.
814 * Update windex to account for both all in one go
815 * so the reader can read() the data atomically.
817 error = uiomove(&wpipe->pipe_buffer.buffer[windex],
819 if (error == 0 && segsize < space) {
820 segsize = space - segsize;
821 error = uiomove(&wpipe->pipe_buffer.buffer[0],
827 wpipe->pipe_buffer.windex += space;
833 * We need both the rlock and the wlock to interlock against
834 * the EOF, WANTW, and size checks, and to modify pipe_state.
836 * These are token locks so we do not have to worry about
839 lwkt_gettoken(&wpipe->pipe_rlock);
842 * If the "read-side" has been blocked, wake it up now
843 * and yield to let it drain synchronously rather
846 if (wpipe->pipe_state & PIPE_WANTR) {
847 wpipe->pipe_state &= ~PIPE_WANTR;
852 * don't block on non-blocking I/O
855 lwkt_reltoken(&wpipe->pipe_rlock);
861 * re-test whether we have to block in the writer after
862 * acquiring both locks, in case the reader opened up
865 space = wpipe->pipe_buffer.size -
866 (wpipe->pipe_buffer.windex - wpipe->pipe_buffer.rindex);
868 if ((space < uio->uio_resid) && (orig_resid <= PIPE_BUF))
872 * Retest EOF - acquiring a new token can temporarily release
873 * tokens already held.
875 if (wpipe->pipe_state & PIPE_WEOF) {
876 lwkt_reltoken(&wpipe->pipe_rlock);
882 * We have no more space and have something to offer,
883 * wake up select/poll/kq.
886 wpipe->pipe_state |= PIPE_WANTW;
887 ++wpipe->pipe_wantwcnt;
888 pipewakeup(wpipe, 1);
889 if (wpipe->pipe_state & PIPE_WANTW)
890 error = tsleep(wpipe, PCATCH, "pipewr", 0);
891 ++pipe_wblocked_count;
893 lwkt_reltoken(&wpipe->pipe_rlock);
896 * Break out if we errored or the read side wants us to go
901 if (wpipe->pipe_state & PIPE_WEOF) {
906 pipe_end_uio(wpipe, &wpipe->pipe_wip);
909 * If we have put any characters in the buffer, we wake up
912 * Both rlock and wlock are required to be able to modify pipe_state.
914 if (wpipe->pipe_buffer.windex != wpipe->pipe_buffer.rindex) {
915 if (wpipe->pipe_state & PIPE_WANTR) {
916 lwkt_gettoken(&wpipe->pipe_rlock);
917 if (wpipe->pipe_state & PIPE_WANTR) {
918 wpipe->pipe_state &= ~PIPE_WANTR;
919 lwkt_reltoken(&wpipe->pipe_rlock);
922 lwkt_reltoken(&wpipe->pipe_rlock);
925 lwkt_gettoken(&wpipe->pipe_rlock);
926 pipewakeup(wpipe, 1);
927 lwkt_reltoken(&wpipe->pipe_rlock);
931 * Don't return EPIPE if I/O was successful
933 if ((wpipe->pipe_buffer.rindex == wpipe->pipe_buffer.windex) &&
934 (uio->uio_resid == 0) &&
940 vfs_timestamp(&wpipe->pipe_mtime);
943 * We have something to offer,
944 * wake up select/poll/kq.
946 /*space = wpipe->pipe_buffer.windex - wpipe->pipe_buffer.rindex;*/
947 lwkt_reltoken(&wpipe->pipe_wlock);
952 * we implement a very minimal set of ioctls for compatibility with sockets.
955 pipe_ioctl(struct file *fp, u_long cmd, caddr_t data,
956 struct ucred *cred, struct sysmsg *msg)
961 mpipe = (struct pipe *)fp->f_data;
963 lwkt_gettoken(&mpipe->pipe_rlock);
964 lwkt_gettoken(&mpipe->pipe_wlock);
969 mpipe->pipe_state |= PIPE_ASYNC;
971 mpipe->pipe_state &= ~PIPE_ASYNC;
976 *(int *)data = mpipe->pipe_buffer.windex -
977 mpipe->pipe_buffer.rindex;
981 error = fsetown(*(int *)data, &mpipe->pipe_sigio);
984 *(int *)data = fgetown(&mpipe->pipe_sigio);
988 /* This is deprecated, FIOSETOWN should be used instead. */
989 error = fsetown(-(*(int *)data), &mpipe->pipe_sigio);
993 /* This is deprecated, FIOGETOWN should be used instead. */
994 *(int *)data = -fgetown(&mpipe->pipe_sigio);
1001 lwkt_reltoken(&mpipe->pipe_wlock);
1002 lwkt_reltoken(&mpipe->pipe_rlock);
1011 pipe_stat(struct file *fp, struct stat *ub, struct ucred *cred)
1015 pipe = (struct pipe *)fp->f_data;
1017 bzero((caddr_t)ub, sizeof(*ub));
1018 ub->st_mode = S_IFIFO;
1019 ub->st_blksize = pipe->pipe_buffer.size;
1020 ub->st_size = pipe->pipe_buffer.windex - pipe->pipe_buffer.rindex;
1021 ub->st_blocks = (ub->st_size + ub->st_blksize - 1) / ub->st_blksize;
1022 ub->st_atimespec = pipe->pipe_atime;
1023 ub->st_mtimespec = pipe->pipe_mtime;
1024 ub->st_ctimespec = pipe->pipe_ctime;
1026 * Left as 0: st_dev, st_ino, st_nlink, st_uid, st_gid, st_rdev,
1028 * XXX (st_dev, st_ino) should be unique.
1034 pipe_close(struct file *fp)
1038 cpipe = (struct pipe *)fp->f_data;
1039 fp->f_ops = &badfileops;
1041 funsetown(&cpipe->pipe_sigio);
1047 * Shutdown one or both directions of a full-duplex pipe.
1050 pipe_shutdown(struct file *fp, int how)
1056 rpipe = (struct pipe *)fp->f_data;
1057 wpipe = rpipe->pipe_peer;
1060 * We modify pipe_state on both pipes, which means we need
1063 lwkt_gettoken(&rpipe->pipe_rlock);
1064 lwkt_gettoken(&rpipe->pipe_wlock);
1065 lwkt_gettoken(&wpipe->pipe_rlock);
1066 lwkt_gettoken(&wpipe->pipe_wlock);
1071 rpipe->pipe_state |= PIPE_REOF; /* my reads */
1072 rpipe->pipe_state |= PIPE_WEOF; /* peer writes */
1073 if (rpipe->pipe_state & PIPE_WANTR) {
1074 rpipe->pipe_state &= ~PIPE_WANTR;
1077 if (rpipe->pipe_state & PIPE_WANTW) {
1078 rpipe->pipe_state &= ~PIPE_WANTW;
1086 wpipe->pipe_state |= PIPE_REOF; /* peer reads */
1087 wpipe->pipe_state |= PIPE_WEOF; /* my writes */
1088 if (wpipe->pipe_state & PIPE_WANTR) {
1089 wpipe->pipe_state &= ~PIPE_WANTR;
1092 if (wpipe->pipe_state & PIPE_WANTW) {
1093 wpipe->pipe_state &= ~PIPE_WANTW;
1099 pipewakeup(rpipe, 1);
1100 pipewakeup(wpipe, 1);
1102 lwkt_reltoken(&wpipe->pipe_wlock);
1103 lwkt_reltoken(&wpipe->pipe_rlock);
1104 lwkt_reltoken(&rpipe->pipe_wlock);
1105 lwkt_reltoken(&rpipe->pipe_rlock);
1111 pipe_free_kmem(struct pipe *cpipe)
1113 if (cpipe->pipe_buffer.buffer != NULL) {
1114 if (cpipe->pipe_buffer.size > PIPE_SIZE)
1115 atomic_subtract_int(&pipe_nbig, 1);
1116 kmem_free(&kernel_map,
1117 (vm_offset_t)cpipe->pipe_buffer.buffer,
1118 cpipe->pipe_buffer.size);
1119 cpipe->pipe_buffer.buffer = NULL;
1120 cpipe->pipe_buffer.object = NULL;
1125 * Close the pipe. The slock must be held to interlock against simultanious
1126 * closes. The rlock and wlock must be held to adjust the pipe_state.
1129 pipeclose(struct pipe *cpipe)
1138 * The slock may not have been allocated yet (close during
1141 * We need both the read and write tokens to modify pipe_state.
1143 if (cpipe->pipe_slock)
1144 lockmgr(cpipe->pipe_slock, LK_EXCLUSIVE);
1145 lwkt_gettoken(&cpipe->pipe_rlock);
1146 lwkt_gettoken(&cpipe->pipe_wlock);
1149 * Set our state, wakeup anyone waiting in select/poll/kq, and
1150 * wakeup anyone blocked on our pipe.
1152 cpipe->pipe_state |= PIPE_CLOSED | PIPE_REOF | PIPE_WEOF;
1153 pipewakeup(cpipe, 1);
1154 if (cpipe->pipe_state & (PIPE_WANTR | PIPE_WANTW)) {
1155 cpipe->pipe_state &= ~(PIPE_WANTR | PIPE_WANTW);
1160 * Disconnect from peer.
1162 if ((ppipe = cpipe->pipe_peer) != NULL) {
1163 lwkt_gettoken(&ppipe->pipe_rlock);
1164 lwkt_gettoken(&ppipe->pipe_wlock);
1165 ppipe->pipe_state |= PIPE_REOF | PIPE_WEOF;
1166 pipewakeup(ppipe, 1);
1167 if (ppipe->pipe_state & (PIPE_WANTR | PIPE_WANTW)) {
1168 ppipe->pipe_state &= ~(PIPE_WANTR | PIPE_WANTW);
1171 if (SLIST_FIRST(&ppipe->pipe_kq.ki_note))
1172 KNOTE(&ppipe->pipe_kq.ki_note, 0);
1173 lwkt_reltoken(&ppipe->pipe_wlock);
1174 lwkt_reltoken(&ppipe->pipe_rlock);
1178 * If the peer is also closed we can free resources for both
1179 * sides, otherwise we leave our side intact to deal with any
1180 * races (since we only have the slock).
1182 if (ppipe && (ppipe->pipe_state & PIPE_CLOSED)) {
1183 cpipe->pipe_peer = NULL;
1184 ppipe->pipe_peer = NULL;
1185 ppipe->pipe_slock = NULL; /* we will free the slock */
1190 lwkt_reltoken(&cpipe->pipe_wlock);
1191 lwkt_reltoken(&cpipe->pipe_rlock);
1192 if (cpipe->pipe_slock)
1193 lockmgr(cpipe->pipe_slock, LK_RELEASE);
1196 * If we disassociated from our peer we can free resources
1198 if (ppipe == NULL) {
1200 if (cpipe->pipe_slock) {
1201 kfree(cpipe->pipe_slock, M_PIPE);
1202 cpipe->pipe_slock = NULL;
1204 if (gd->gd_pipeqcount >= pipe_maxcache ||
1205 cpipe->pipe_buffer.size != PIPE_SIZE
1207 pipe_free_kmem(cpipe);
1208 kfree(cpipe, M_PIPE);
1210 cpipe->pipe_state = 0;
1211 cpipe->pipe_peer = gd->gd_pipeq;
1212 gd->gd_pipeq = cpipe;
1213 ++gd->gd_pipeqcount;
1219 pipe_kqfilter(struct file *fp, struct knote *kn)
1223 cpipe = (struct pipe *)kn->kn_fp->f_data;
1225 switch (kn->kn_filter) {
1227 kn->kn_fop = &pipe_rfiltops;
1230 kn->kn_fop = &pipe_wfiltops;
1231 if (cpipe->pipe_peer == NULL) {
1232 /* other end of pipe has been closed */
1237 return (EOPNOTSUPP);
1239 kn->kn_hook = (caddr_t)cpipe;
1241 knote_insert(&cpipe->pipe_kq.ki_note, kn);
1247 filt_pipedetach(struct knote *kn)
1249 struct pipe *cpipe = (struct pipe *)kn->kn_hook;
1251 knote_remove(&cpipe->pipe_kq.ki_note, kn);
1256 filt_piperead(struct knote *kn, long hint)
1258 struct pipe *rpipe = (struct pipe *)kn->kn_fp->f_data;
1261 lwkt_gettoken(&rpipe->pipe_rlock);
1262 lwkt_gettoken(&rpipe->pipe_wlock);
1264 kn->kn_data = rpipe->pipe_buffer.windex - rpipe->pipe_buffer.rindex;
1266 if (rpipe->pipe_state & PIPE_REOF) {
1268 * Only set NODATA if all data has been exhausted
1270 if (kn->kn_data == 0)
1271 kn->kn_flags |= EV_NODATA;
1272 kn->kn_flags |= EV_EOF;
1276 lwkt_reltoken(&rpipe->pipe_wlock);
1277 lwkt_reltoken(&rpipe->pipe_rlock);
1280 ready = kn->kn_data > 0;
1287 filt_pipewrite(struct knote *kn, long hint)
1289 struct pipe *rpipe = (struct pipe *)kn->kn_fp->f_data;
1290 struct pipe *wpipe = rpipe->pipe_peer;
1294 if (wpipe == NULL) {
1295 kn->kn_flags |= (EV_EOF | EV_NODATA);
1299 lwkt_gettoken(&wpipe->pipe_rlock);
1300 lwkt_gettoken(&wpipe->pipe_wlock);
1302 if (wpipe->pipe_state & PIPE_WEOF) {
1303 kn->kn_flags |= (EV_EOF | EV_NODATA);
1308 kn->kn_data = wpipe->pipe_buffer.size -
1309 (wpipe->pipe_buffer.windex -
1310 wpipe->pipe_buffer.rindex);
1312 lwkt_reltoken(&wpipe->pipe_wlock);
1313 lwkt_reltoken(&wpipe->pipe_rlock);
1316 ready = kn->kn_data >= PIPE_BUF;