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,
358 1, VM_MAPTYPE_NORMAL,
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, 1);
408 lwkt_token_init(&cpipe->pipe_wlock, 1);
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 */
430 if (uio->uio_resid == 0)
434 * Setup locks, calculate nbio
436 pipe_get_mplock(&mpsave);
437 rpipe = (struct pipe *)fp->f_data;
438 lwkt_gettoken(&rpipe->pipe_rlock);
440 if (fflags & O_FBLOCKING)
442 else if (fflags & O_FNONBLOCKING)
444 else if (fp->f_flag & O_NONBLOCK)
450 * Reads are serialized. Note howeverthat pipe_buffer.buffer and
451 * pipe_buffer.size can change out from under us when the number
452 * of bytes in the buffer are zero due to the write-side doing a
455 error = pipe_start_uio(rpipe, &rpipe->pipe_rip);
457 pipe_rel_mplock(&mpsave);
458 lwkt_reltoken(&rpipe->pipe_rlock);
463 bigread = (uio->uio_resid > 10 * 1024 * 1024);
466 while (uio->uio_resid) {
470 if (bigread && --bigcount == 0) {
473 if (CURSIG(curthread->td_lwp)) {
479 size = rpipe->pipe_buffer.windex - rpipe->pipe_buffer.rindex;
482 rindex = rpipe->pipe_buffer.rindex &
483 (rpipe->pipe_buffer.size - 1);
485 if (nsize > rpipe->pipe_buffer.size - rindex)
486 nsize = rpipe->pipe_buffer.size - rindex;
487 nsize = szmin(nsize, uio->uio_resid);
489 error = uiomove(&rpipe->pipe_buffer.buffer[rindex],
494 rpipe->pipe_buffer.rindex += nsize;
498 * If the FIFO is still over half full just continue
499 * and do not try to notify the writer yet.
501 if (size - nsize >= (rpipe->pipe_buffer.size >> 1)) {
507 * When the FIFO is less then half full notify any
508 * waiting writer. WANTW can be checked while
509 * holding just the rlock.
512 if ((rpipe->pipe_state & PIPE_WANTW) == 0)
517 * If the "write-side" was blocked we wake it up. This code
518 * is reached either when the buffer is completely emptied
519 * or if it becomes more then half-empty.
521 * Pipe_state can only be modified if both the rlock and
524 if (rpipe->pipe_state & PIPE_WANTW) {
525 lwkt_gettoken(&rpipe->pipe_wlock);
526 if (rpipe->pipe_state & PIPE_WANTW) {
528 rpipe->pipe_state &= ~PIPE_WANTW;
529 lwkt_reltoken(&rpipe->pipe_wlock);
532 lwkt_reltoken(&rpipe->pipe_wlock);
537 * Pick up our copy loop again if the writer sent data to
538 * us while we were messing around.
540 * On a SMP box poll up to pipe_delay nanoseconds for new
541 * data. Typically a value of 2000 to 4000 is sufficient
542 * to eradicate most IPIs/tsleeps/wakeups when a pipe
543 * is used for synchronous communications with small packets,
544 * and 8000 or so (8uS) will pipeline large buffer xfers
545 * between cpus over a pipe.
547 * For synchronous communications a hit means doing a
548 * full Awrite-Bread-Bwrite-Aread cycle in less then 2uS,
549 * where as miss requiring a tsleep/wakeup sequence
550 * will take 7uS or more.
552 if (rpipe->pipe_buffer.windex != rpipe->pipe_buffer.rindex)
555 #if defined(SMP) && defined(_RDTSC_SUPPORTED_)
560 tsc_target = tsc_get_target(pipe_delay);
561 while (tsc_test_target(tsc_target) == 0) {
562 if (rpipe->pipe_buffer.windex !=
563 rpipe->pipe_buffer.rindex) {
574 * Detect EOF condition, do not set error.
576 if (rpipe->pipe_state & PIPE_REOF)
580 * Break if some data was read, or if this was a non-blocking
592 * Last chance, interlock with WANTR.
594 lwkt_gettoken(&rpipe->pipe_wlock);
595 size = rpipe->pipe_buffer.windex - rpipe->pipe_buffer.rindex;
597 lwkt_reltoken(&rpipe->pipe_wlock);
602 * Retest EOF - acquiring a new token can temporarily release
603 * tokens already held.
605 if (rpipe->pipe_state & PIPE_REOF) {
606 lwkt_reltoken(&rpipe->pipe_wlock);
611 * If there is no more to read in the pipe, reset its
612 * pointers to the beginning. This improves cache hit
615 * We need both locks to modify both pointers, and there
616 * must also not be a write in progress or the uiomove()
617 * in the write might block and temporarily release
618 * its wlock, then reacquire and update windex. We are
619 * only serialized against reads, not writes.
621 * XXX should we even bother resetting the indices? It
622 * might actually be more cache efficient not to.
624 if (rpipe->pipe_buffer.rindex == rpipe->pipe_buffer.windex &&
625 rpipe->pipe_wip == 0) {
626 rpipe->pipe_buffer.rindex = 0;
627 rpipe->pipe_buffer.windex = 0;
631 * Wait for more data.
633 * Pipe_state can only be set if both the rlock and wlock
636 rpipe->pipe_state |= PIPE_WANTR;
637 tsleep_interlock(rpipe, PCATCH);
638 lwkt_reltoken(&rpipe->pipe_wlock);
639 error = tsleep(rpipe, PCATCH | PINTERLOCKED, "piperd", 0);
640 ++pipe_rblocked_count;
644 pipe_end_uio(rpipe, &rpipe->pipe_rip);
647 * Uptime last access time
649 if (error == 0 && nread)
650 vfs_timestamp(&rpipe->pipe_atime);
653 * If we drained the FIFO more then half way then handle
654 * write blocking hysteresis.
656 * Note that PIPE_WANTW cannot be set by the writer without
657 * it holding both rlock and wlock, so we can test it
658 * while holding just rlock.
661 if (rpipe->pipe_state & PIPE_WANTW) {
662 lwkt_gettoken(&rpipe->pipe_wlock);
663 if (rpipe->pipe_state & PIPE_WANTW) {
664 rpipe->pipe_state &= ~PIPE_WANTW;
665 lwkt_reltoken(&rpipe->pipe_wlock);
668 lwkt_reltoken(&rpipe->pipe_wlock);
671 if (pipeseltest(rpipe)) {
672 lwkt_gettoken(&rpipe->pipe_wlock);
673 pipeselwakeup(rpipe);
674 lwkt_reltoken(&rpipe->pipe_wlock);
677 /*size = rpipe->pipe_buffer.windex - rpipe->pipe_buffer.rindex;*/
678 lwkt_reltoken(&rpipe->pipe_rlock);
680 pipe_rel_mplock(&mpsave);
685 * MPALMOSTSAFE - acquires mplock
688 pipe_write(struct file *fp, struct uio *uio, struct ucred *cred, int fflags)
693 struct pipe *wpipe, *rpipe;
701 pipe_get_mplock(&mpsave);
704 * Writes go to the peer. The peer will always exist.
706 rpipe = (struct pipe *) fp->f_data;
707 wpipe = rpipe->pipe_peer;
708 lwkt_gettoken(&wpipe->pipe_wlock);
709 if (wpipe->pipe_state & PIPE_WEOF) {
710 pipe_rel_mplock(&mpsave);
711 lwkt_reltoken(&wpipe->pipe_wlock);
716 * Degenerate case (EPIPE takes prec)
718 if (uio->uio_resid == 0) {
719 pipe_rel_mplock(&mpsave);
720 lwkt_reltoken(&wpipe->pipe_wlock);
725 * Writes are serialized (start_uio must be called with wlock)
727 error = pipe_start_uio(wpipe, &wpipe->pipe_wip);
729 pipe_rel_mplock(&mpsave);
730 lwkt_reltoken(&wpipe->pipe_wlock);
734 if (fflags & O_FBLOCKING)
736 else if (fflags & O_FNONBLOCKING)
738 else if (fp->f_flag & O_NONBLOCK)
744 * If it is advantageous to resize the pipe buffer, do
745 * so. We are write-serialized so we can block safely.
747 if ((wpipe->pipe_buffer.size <= PIPE_SIZE) &&
748 (pipe_nbig < pipe_maxbig) &&
749 wpipe->pipe_wantwcnt > 4 &&
750 (wpipe->pipe_buffer.rindex == wpipe->pipe_buffer.windex)) {
752 * Recheck after lock.
754 lwkt_gettoken(&wpipe->pipe_rlock);
755 if ((wpipe->pipe_buffer.size <= PIPE_SIZE) &&
756 (pipe_nbig < pipe_maxbig) &&
757 (wpipe->pipe_buffer.rindex == wpipe->pipe_buffer.windex)) {
758 atomic_add_int(&pipe_nbig, 1);
759 if (pipespace(wpipe, BIG_PIPE_SIZE) == 0)
762 atomic_subtract_int(&pipe_nbig, 1);
764 lwkt_reltoken(&wpipe->pipe_rlock);
767 orig_resid = uio->uio_resid;
770 bigwrite = (uio->uio_resid > 10 * 1024 * 1024);
773 while (uio->uio_resid) {
774 if (wpipe->pipe_state & PIPE_WEOF) {
782 if (bigwrite && --bigcount == 0) {
785 if (CURSIG(curthread->td_lwp)) {
791 windex = wpipe->pipe_buffer.windex &
792 (wpipe->pipe_buffer.size - 1);
793 space = wpipe->pipe_buffer.size -
794 (wpipe->pipe_buffer.windex - wpipe->pipe_buffer.rindex);
797 /* Writes of size <= PIPE_BUF must be atomic. */
798 if ((space < uio->uio_resid) && (orig_resid <= PIPE_BUF))
802 * Write to fill, read size handles write hysteresis. Also
803 * additional restrictions can cause select-based non-blocking
810 * Transfer size is minimum of uio transfer
811 * and free space in pipe buffer.
813 * Limit each uiocopy to no more then PIPE_SIZE
814 * so we can keep the gravy train going on a
815 * SMP box. This doubles the performance for
816 * write sizes > 16K. Otherwise large writes
817 * wind up doing an inefficient synchronous
820 space = szmin(space, uio->uio_resid);
821 if (space > PIPE_SIZE)
825 * First segment to transfer is minimum of
826 * transfer size and contiguous space in
827 * pipe buffer. If first segment to transfer
828 * is less than the transfer size, we've got
829 * a wraparound in the buffer.
831 segsize = wpipe->pipe_buffer.size - windex;
837 * If this is the first loop and the reader is
838 * blocked, do a preemptive wakeup of the reader.
840 * On SMP the IPI latency plus the wlock interlock
841 * on the reader side is the fastest way to get the
842 * reader going. (The scheduler will hard loop on
845 * NOTE: We can't clear WANTR here without acquiring
846 * the rlock, which we don't want to do here!
848 if ((wpipe->pipe_state & PIPE_WANTR) && pipe_mpsafe > 1)
853 * Transfer segment, which may include a wrap-around.
854 * Update windex to account for both all in one go
855 * so the reader can read() the data atomically.
857 error = uiomove(&wpipe->pipe_buffer.buffer[windex],
859 if (error == 0 && segsize < space) {
860 segsize = space - segsize;
861 error = uiomove(&wpipe->pipe_buffer.buffer[0],
867 wpipe->pipe_buffer.windex += space;
873 * We need both the rlock and the wlock to interlock against
874 * the EOF, WANTW, and size checks, and to modify pipe_state.
876 * These are token locks so we do not have to worry about
879 lwkt_gettoken(&wpipe->pipe_rlock);
882 * If the "read-side" has been blocked, wake it up now
883 * and yield to let it drain synchronously rather
886 if (wpipe->pipe_state & PIPE_WANTR) {
887 wpipe->pipe_state &= ~PIPE_WANTR;
892 * don't block on non-blocking I/O
895 lwkt_reltoken(&wpipe->pipe_rlock);
901 * re-test whether we have to block in the writer after
902 * acquiring both locks, in case the reader opened up
905 space = wpipe->pipe_buffer.size -
906 (wpipe->pipe_buffer.windex - wpipe->pipe_buffer.rindex);
908 if ((space < uio->uio_resid) && (orig_resid <= PIPE_BUF))
912 * Retest EOF - acquiring a new token can temporarily release
913 * tokens already held.
915 if (wpipe->pipe_state & PIPE_WEOF) {
916 lwkt_reltoken(&wpipe->pipe_rlock);
922 * We have no more space and have something to offer,
923 * wake up select/poll.
926 wpipe->pipe_state |= PIPE_WANTW;
927 ++wpipe->pipe_wantwcnt;
928 pipeselwakeup(wpipe);
929 if (wpipe->pipe_state & PIPE_WANTW)
930 error = tsleep(wpipe, PCATCH, "pipewr", 0);
931 ++pipe_wblocked_count;
933 lwkt_reltoken(&wpipe->pipe_rlock);
936 * Break out if we errored or the read side wants us to go
941 if (wpipe->pipe_state & PIPE_WEOF) {
946 pipe_end_uio(wpipe, &wpipe->pipe_wip);
949 * If we have put any characters in the buffer, we wake up
952 * Both rlock and wlock are required to be able to modify pipe_state.
954 if (wpipe->pipe_buffer.windex != wpipe->pipe_buffer.rindex) {
955 if (wpipe->pipe_state & PIPE_WANTR) {
956 lwkt_gettoken(&wpipe->pipe_rlock);
957 if (wpipe->pipe_state & PIPE_WANTR) {
958 wpipe->pipe_state &= ~PIPE_WANTR;
959 lwkt_reltoken(&wpipe->pipe_rlock);
962 lwkt_reltoken(&wpipe->pipe_rlock);
965 if (pipeseltest(wpipe)) {
966 lwkt_gettoken(&wpipe->pipe_rlock);
967 pipeselwakeup(wpipe);
968 lwkt_reltoken(&wpipe->pipe_rlock);
973 * Don't return EPIPE if I/O was successful
975 if ((wpipe->pipe_buffer.rindex == wpipe->pipe_buffer.windex) &&
976 (uio->uio_resid == 0) &&
982 vfs_timestamp(&wpipe->pipe_mtime);
985 * We have something to offer,
986 * wake up select/poll.
988 /*space = wpipe->pipe_buffer.windex - wpipe->pipe_buffer.rindex;*/
989 lwkt_reltoken(&wpipe->pipe_wlock);
990 pipe_rel_mplock(&mpsave);
995 * MPALMOSTSAFE - acquires mplock
997 * we implement a very minimal set of ioctls for compatibility with sockets.
1000 pipe_ioctl(struct file *fp, u_long cmd, caddr_t data,
1001 struct ucred *cred, struct sysmsg *msg)
1007 pipe_get_mplock(&mpsave);
1008 mpipe = (struct pipe *)fp->f_data;
1010 lwkt_gettoken(&mpipe->pipe_rlock);
1011 lwkt_gettoken(&mpipe->pipe_wlock);
1016 mpipe->pipe_state |= PIPE_ASYNC;
1018 mpipe->pipe_state &= ~PIPE_ASYNC;
1023 *(int *)data = mpipe->pipe_buffer.windex -
1024 mpipe->pipe_buffer.rindex;
1029 error = fsetown(*(int *)data, &mpipe->pipe_sigio);
1033 *(int *)data = fgetown(mpipe->pipe_sigio);
1037 /* This is deprecated, FIOSETOWN should be used instead. */
1039 error = fsetown(-(*(int *)data), &mpipe->pipe_sigio);
1044 /* This is deprecated, FIOGETOWN should be used instead. */
1045 *(int *)data = -fgetown(mpipe->pipe_sigio);
1052 lwkt_reltoken(&mpipe->pipe_wlock);
1053 lwkt_reltoken(&mpipe->pipe_rlock);
1054 pipe_rel_mplock(&mpsave);
1060 * MPALMOSTSAFE - acquires mplock
1062 * poll for events (helper)
1065 pipe_poll_events(struct pipe *rpipe, struct pipe *wpipe, int events)
1070 if (events & (POLLIN | POLLRDNORM)) {
1071 if ((rpipe->pipe_buffer.windex != rpipe->pipe_buffer.rindex) ||
1072 (rpipe->pipe_state & PIPE_REOF)) {
1073 revents |= events & (POLLIN | POLLRDNORM);
1077 if (events & (POLLOUT | POLLWRNORM)) {
1078 if (wpipe == NULL || (wpipe->pipe_state & PIPE_WEOF)) {
1079 revents |= events & (POLLOUT | POLLWRNORM);
1081 space = wpipe->pipe_buffer.windex -
1082 wpipe->pipe_buffer.rindex;
1083 space = wpipe->pipe_buffer.size - space;
1084 if (space >= PIPE_BUF)
1085 revents |= events & (POLLOUT | POLLWRNORM);
1089 if ((rpipe->pipe_state & PIPE_REOF) ||
1091 (wpipe->pipe_state & PIPE_WEOF)) {
1098 * Poll for events from file pointer.
1101 pipe_poll(struct file *fp, int events, struct ucred *cred)
1108 pipe_get_mplock(&mpsave);
1109 rpipe = (struct pipe *)fp->f_data;
1110 wpipe = rpipe->pipe_peer;
1112 revents = pipe_poll_events(rpipe, wpipe, events);
1114 if (events & (POLLIN | POLLRDNORM)) {
1115 lwkt_gettoken(&rpipe->pipe_rlock);
1116 lwkt_gettoken(&rpipe->pipe_wlock);
1118 if (events & (POLLOUT | POLLWRNORM)) {
1119 lwkt_gettoken(&wpipe->pipe_rlock);
1120 lwkt_gettoken(&wpipe->pipe_wlock);
1122 revents = pipe_poll_events(rpipe, wpipe, events);
1124 if (events & (POLLIN | POLLRDNORM)) {
1125 selrecord(curthread, &rpipe->pipe_sel);
1126 rpipe->pipe_state |= PIPE_SEL;
1129 if (events & (POLLOUT | POLLWRNORM)) {
1130 selrecord(curthread, &wpipe->pipe_sel);
1131 wpipe->pipe_state |= PIPE_SEL;
1134 if (events & (POLLOUT | POLLWRNORM)) {
1135 lwkt_reltoken(&wpipe->pipe_wlock);
1136 lwkt_reltoken(&wpipe->pipe_rlock);
1138 if (events & (POLLIN | POLLRDNORM)) {
1139 lwkt_reltoken(&rpipe->pipe_wlock);
1140 lwkt_reltoken(&rpipe->pipe_rlock);
1143 pipe_rel_mplock(&mpsave);
1151 pipe_stat(struct file *fp, struct stat *ub, struct ucred *cred)
1156 pipe_get_mplock(&mpsave);
1157 pipe = (struct pipe *)fp->f_data;
1159 bzero((caddr_t)ub, sizeof(*ub));
1160 ub->st_mode = S_IFIFO;
1161 ub->st_blksize = pipe->pipe_buffer.size;
1162 ub->st_size = pipe->pipe_buffer.windex - pipe->pipe_buffer.rindex;
1163 ub->st_blocks = (ub->st_size + ub->st_blksize - 1) / ub->st_blksize;
1164 ub->st_atimespec = pipe->pipe_atime;
1165 ub->st_mtimespec = pipe->pipe_mtime;
1166 ub->st_ctimespec = pipe->pipe_ctime;
1168 * Left as 0: st_dev, st_ino, st_nlink, st_uid, st_gid, st_rdev,
1170 * XXX (st_dev, st_ino) should be unique.
1172 pipe_rel_mplock(&mpsave);
1177 * MPALMOSTSAFE - acquires mplock
1180 pipe_close(struct file *fp)
1185 cpipe = (struct pipe *)fp->f_data;
1186 fp->f_ops = &badfileops;
1188 funsetown(cpipe->pipe_sigio);
1195 * Shutdown one or both directions of a full-duplex pipe.
1197 * MPALMOSTSAFE - acquires mplock
1200 pipe_shutdown(struct file *fp, int how)
1207 pipe_get_mplock(&mpsave);
1208 rpipe = (struct pipe *)fp->f_data;
1209 wpipe = rpipe->pipe_peer;
1212 * We modify pipe_state on both pipes, which means we need
1215 lwkt_gettoken(&rpipe->pipe_rlock);
1216 lwkt_gettoken(&rpipe->pipe_wlock);
1217 lwkt_gettoken(&wpipe->pipe_rlock);
1218 lwkt_gettoken(&wpipe->pipe_wlock);
1223 rpipe->pipe_state |= PIPE_REOF; /* my reads */
1224 rpipe->pipe_state |= PIPE_WEOF; /* peer writes */
1225 if (rpipe->pipe_state & PIPE_WANTR) {
1226 rpipe->pipe_state &= ~PIPE_WANTR;
1229 if (rpipe->pipe_state & PIPE_WANTW) {
1230 rpipe->pipe_state &= ~PIPE_WANTW;
1238 wpipe->pipe_state |= PIPE_REOF; /* peer reads */
1239 wpipe->pipe_state |= PIPE_WEOF; /* my writes */
1240 if (wpipe->pipe_state & PIPE_WANTR) {
1241 wpipe->pipe_state &= ~PIPE_WANTR;
1244 if (wpipe->pipe_state & PIPE_WANTW) {
1245 wpipe->pipe_state &= ~PIPE_WANTW;
1251 pipeselwakeup(rpipe);
1252 pipeselwakeup(wpipe);
1254 lwkt_reltoken(&wpipe->pipe_wlock);
1255 lwkt_reltoken(&wpipe->pipe_rlock);
1256 lwkt_reltoken(&rpipe->pipe_wlock);
1257 lwkt_reltoken(&rpipe->pipe_rlock);
1259 pipe_rel_mplock(&mpsave);
1264 pipe_free_kmem(struct pipe *cpipe)
1266 if (cpipe->pipe_buffer.buffer != NULL) {
1267 if (cpipe->pipe_buffer.size > PIPE_SIZE)
1268 atomic_subtract_int(&pipe_nbig, 1);
1269 kmem_free(&kernel_map,
1270 (vm_offset_t)cpipe->pipe_buffer.buffer,
1271 cpipe->pipe_buffer.size);
1272 cpipe->pipe_buffer.buffer = NULL;
1273 cpipe->pipe_buffer.object = NULL;
1278 * Close the pipe. The slock must be held to interlock against simultanious
1279 * closes. The rlock and wlock must be held to adjust the pipe_state.
1282 pipeclose(struct pipe *cpipe)
1291 * The slock may not have been allocated yet (close during
1294 * We need both the read and write tokens to modify pipe_state.
1296 if (cpipe->pipe_slock)
1297 lockmgr(cpipe->pipe_slock, LK_EXCLUSIVE);
1298 lwkt_gettoken(&cpipe->pipe_rlock);
1299 lwkt_gettoken(&cpipe->pipe_wlock);
1302 * Set our state, wakeup anyone waiting in select, and
1303 * wakeup anyone blocked on our pipe.
1305 cpipe->pipe_state |= PIPE_CLOSED | PIPE_REOF | PIPE_WEOF;
1306 pipeselwakeup(cpipe);
1307 if (cpipe->pipe_state & (PIPE_WANTR | PIPE_WANTW)) {
1308 cpipe->pipe_state &= ~(PIPE_WANTR | PIPE_WANTW);
1313 * Disconnect from peer.
1315 if ((ppipe = cpipe->pipe_peer) != NULL) {
1316 lwkt_gettoken(&ppipe->pipe_rlock);
1317 lwkt_gettoken(&ppipe->pipe_wlock);
1318 ppipe->pipe_state |= PIPE_REOF | PIPE_WEOF;
1319 pipeselwakeup(ppipe);
1320 if (ppipe->pipe_state & (PIPE_WANTR | PIPE_WANTW)) {
1321 ppipe->pipe_state &= ~(PIPE_WANTR | PIPE_WANTW);
1324 if (SLIST_FIRST(&ppipe->pipe_sel.si_note)) {
1326 KNOTE(&ppipe->pipe_sel.si_note, 0);
1329 lwkt_reltoken(&ppipe->pipe_wlock);
1330 lwkt_reltoken(&ppipe->pipe_rlock);
1334 * If the peer is also closed we can free resources for both
1335 * sides, otherwise we leave our side intact to deal with any
1336 * races (since we only have the slock).
1338 if (ppipe && (ppipe->pipe_state & PIPE_CLOSED)) {
1339 cpipe->pipe_peer = NULL;
1340 ppipe->pipe_peer = NULL;
1341 ppipe->pipe_slock = NULL; /* we will free the slock */
1346 lwkt_reltoken(&cpipe->pipe_wlock);
1347 lwkt_reltoken(&cpipe->pipe_rlock);
1348 if (cpipe->pipe_slock)
1349 lockmgr(cpipe->pipe_slock, LK_RELEASE);
1352 * If we disassociated from our peer we can free resources
1354 if (ppipe == NULL) {
1356 if (cpipe->pipe_slock) {
1357 kfree(cpipe->pipe_slock, M_PIPE);
1358 cpipe->pipe_slock = NULL;
1360 if (gd->gd_pipeqcount >= pipe_maxcache ||
1361 cpipe->pipe_buffer.size != PIPE_SIZE
1363 pipe_free_kmem(cpipe);
1364 kfree(cpipe, M_PIPE);
1366 cpipe->pipe_state = 0;
1367 cpipe->pipe_peer = gd->gd_pipeq;
1368 gd->gd_pipeq = cpipe;
1369 ++gd->gd_pipeqcount;
1375 * MPALMOSTSAFE - acquires mplock
1378 pipe_kqfilter(struct file *fp, struct knote *kn)
1383 cpipe = (struct pipe *)kn->kn_fp->f_data;
1385 switch (kn->kn_filter) {
1387 kn->kn_fop = &pipe_rfiltops;
1390 kn->kn_fop = &pipe_wfiltops;
1391 cpipe = cpipe->pipe_peer;
1392 if (cpipe == NULL) {
1393 /* other end of pipe has been closed */
1402 kn->kn_hook = (caddr_t)cpipe;
1404 SLIST_INSERT_HEAD(&cpipe->pipe_sel.si_note, kn, kn_selnext);
1410 filt_pipedetach(struct knote *kn)
1412 struct pipe *cpipe = (struct pipe *)kn->kn_hook;
1414 SLIST_REMOVE(&cpipe->pipe_sel.si_note, kn, knote, kn_selnext);
1419 filt_piperead(struct knote *kn, long hint)
1421 struct pipe *rpipe = (struct pipe *)kn->kn_fp->f_data;
1423 kn->kn_data = rpipe->pipe_buffer.windex - rpipe->pipe_buffer.rindex;
1426 if (rpipe->pipe_state & PIPE_REOF) {
1427 kn->kn_flags |= EV_EOF;
1430 return (kn->kn_data > 0);
1435 filt_pipewrite(struct knote *kn, long hint)
1437 struct pipe *rpipe = (struct pipe *)kn->kn_fp->f_data;
1438 struct pipe *wpipe = rpipe->pipe_peer;
1442 if ((wpipe == NULL) || (wpipe->pipe_state & PIPE_WEOF)) {
1444 kn->kn_flags |= EV_EOF;
1447 space = wpipe->pipe_buffer.windex -
1448 wpipe->pipe_buffer.rindex;
1449 space = wpipe->pipe_buffer.size - space;
1450 kn->kn_data = space;
1451 return (kn->kn_data >= PIPE_BUF);