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>
66 #include <machine/cpufunc.h>
69 * interfaces to the outside world
71 static int pipe_read (struct file *fp, struct uio *uio,
72 struct ucred *cred, int flags);
73 static int pipe_write (struct file *fp, struct uio *uio,
74 struct ucred *cred, int flags);
75 static int pipe_close (struct file *fp);
76 static int pipe_shutdown (struct file *fp, int how);
77 static int pipe_poll (struct file *fp, int events, struct ucred *cred);
78 static int pipe_kqfilter (struct file *fp, struct knote *kn);
79 static int pipe_stat (struct file *fp, struct stat *sb, struct ucred *cred);
80 static int pipe_ioctl (struct file *fp, u_long cmd, caddr_t data, struct ucred *cred);
82 static struct fileops pipeops = {
84 .fo_write = pipe_write,
85 .fo_ioctl = pipe_ioctl,
87 .fo_kqfilter = pipe_kqfilter,
89 .fo_close = pipe_close,
90 .fo_shutdown = pipe_shutdown
93 static void filt_pipedetach(struct knote *kn);
94 static int filt_piperead(struct knote *kn, long hint);
95 static int filt_pipewrite(struct knote *kn, long hint);
97 static struct filterops pipe_rfiltops =
98 { 1, NULL, filt_pipedetach, filt_piperead };
99 static struct filterops pipe_wfiltops =
100 { 1, NULL, filt_pipedetach, filt_pipewrite };
102 MALLOC_DEFINE(M_PIPE, "pipe", "pipe structures");
105 * Default pipe buffer size(s), this can be kind-of large now because pipe
106 * space is pageable. The pipe code will try to maintain locality of
107 * reference for performance reasons, so small amounts of outstanding I/O
108 * will not wipe the cache.
110 #define MINPIPESIZE (PIPE_SIZE/3)
111 #define MAXPIPESIZE (2*PIPE_SIZE/3)
114 * Limit the number of "big" pipes
116 #define LIMITBIGPIPES 64
117 #define PIPEQ_MAX_CACHE 16 /* per-cpu pipe structure cache */
119 static int pipe_maxbig = LIMITBIGPIPES;
120 static int pipe_maxcache = PIPEQ_MAX_CACHE;
121 static int pipe_bigcount;
122 static int pipe_nbig;
123 static int pipe_bcache_alloc;
124 static int pipe_bkmem_alloc;
125 static int pipe_rblocked_count;
126 static int pipe_wblocked_count;
128 SYSCTL_NODE(_kern, OID_AUTO, pipe, CTLFLAG_RW, 0, "Pipe operation");
129 SYSCTL_INT(_kern_pipe, OID_AUTO, nbig,
130 CTLFLAG_RD, &pipe_nbig, 0, "numer of big pipes allocated");
131 SYSCTL_INT(_kern_pipe, OID_AUTO, bigcount,
132 CTLFLAG_RW, &pipe_bigcount, 0, "number of times pipe expanded");
133 SYSCTL_INT(_kern_pipe, OID_AUTO, rblocked,
134 CTLFLAG_RW, &pipe_rblocked_count, 0, "number of times pipe expanded");
135 SYSCTL_INT(_kern_pipe, OID_AUTO, wblocked,
136 CTLFLAG_RW, &pipe_wblocked_count, 0, "number of times pipe expanded");
137 SYSCTL_INT(_kern_pipe, OID_AUTO, maxcache,
138 CTLFLAG_RW, &pipe_maxcache, 0, "max pipes cached per-cpu");
139 SYSCTL_INT(_kern_pipe, OID_AUTO, maxbig,
140 CTLFLAG_RW, &pipe_maxbig, 0, "max number of big pipes");
142 static int pipe_delay = 5000; /* 5uS default */
143 SYSCTL_INT(_kern_pipe, OID_AUTO, delay,
144 CTLFLAG_RW, &pipe_delay, 0, "SMP delay optimization in ns");
145 static int pipe_mpsafe = 0;
146 SYSCTL_INT(_kern_pipe, OID_AUTO, mpsafe,
147 CTLFLAG_RW, &pipe_mpsafe, 0, "");
149 #if !defined(NO_PIPE_SYSCTL_STATS)
150 SYSCTL_INT(_kern_pipe, OID_AUTO, bcache_alloc,
151 CTLFLAG_RW, &pipe_bcache_alloc, 0, "pipe buffer from pcpu cache");
152 SYSCTL_INT(_kern_pipe, OID_AUTO, bkmem_alloc,
153 CTLFLAG_RW, &pipe_bkmem_alloc, 0, "pipe buffer from kmem");
156 static void pipeclose (struct pipe *cpipe);
157 static void pipe_free_kmem (struct pipe *cpipe);
158 static int pipe_create (struct pipe **cpipep);
159 static __inline void pipeselwakeup (struct pipe *cpipe);
160 static int pipespace (struct pipe *cpipe, int size);
163 pipeselwakeup(struct pipe *cpipe)
165 if (cpipe->pipe_state & PIPE_SEL) {
167 cpipe->pipe_state &= ~PIPE_SEL;
168 selwakeup(&cpipe->pipe_sel);
171 if ((cpipe->pipe_state & PIPE_ASYNC) && cpipe->pipe_sigio) {
173 pgsigio(cpipe->pipe_sigio, SIGIO, 0);
176 if (SLIST_FIRST(&cpipe->pipe_sel.si_note)) {
178 KNOTE(&cpipe->pipe_sel.si_note, 0);
184 * These routines are called before and after a UIO. The UIO
185 * may block, causing our held tokens to be lost temporarily.
187 * We use these routines to serialize reads against other reads
188 * and writes against other writes.
190 * The read token is held on entry so *ipp does not race.
193 pipe_start_uio(struct pipe *cpipe, u_int *ipp)
199 error = tsleep(ipp, PCATCH, "pipexx", 0);
208 pipe_end_uio(struct pipe *cpipe, u_int *ipp)
219 pipe_get_mplock(int *save)
222 if (pipe_mpsafe == 0) {
233 pipe_rel_mplock(int *save)
243 * The pipe system call for the DTYPE_PIPE type of pipes
245 * pipe_ARgs(int dummy)
250 sys_pipe(struct pipe_args *uap)
252 struct thread *td = curthread;
253 struct proc *p = td->td_proc;
254 struct file *rf, *wf;
255 struct pipe *rpipe, *wpipe;
260 rpipe = wpipe = NULL;
261 if (pipe_create(&rpipe) || pipe_create(&wpipe)) {
267 error = falloc(p, &rf, &fd1);
273 uap->sysmsg_fds[0] = fd1;
276 * Warning: once we've gotten past allocation of the fd for the
277 * read-side, we can only drop the read side via fdrop() in order
278 * to avoid races against processes which manage to dup() the read
279 * side while we are blocked trying to allocate the write side.
281 rf->f_type = DTYPE_PIPE;
282 rf->f_flag = FREAD | FWRITE;
283 rf->f_ops = &pipeops;
285 error = falloc(p, &wf, &fd2);
287 fsetfd(p, NULL, fd1);
289 /* rpipe has been closed by fdrop(). */
293 wf->f_type = DTYPE_PIPE;
294 wf->f_flag = FREAD | FWRITE;
295 wf->f_ops = &pipeops;
297 uap->sysmsg_fds[1] = fd2;
299 rpipe->pipe_slock = kmalloc(sizeof(struct lock),
300 M_PIPE, M_WAITOK|M_ZERO);
301 wpipe->pipe_slock = rpipe->pipe_slock;
302 rpipe->pipe_peer = wpipe;
303 wpipe->pipe_peer = rpipe;
304 lockinit(rpipe->pipe_slock, "pipecl", 0, 0);
307 * Once activated the peer relationship remains valid until
308 * both sides are closed.
319 * Allocate kva for pipe circular buffer, the space is pageable
320 * This routine will 'realloc' the size of a pipe safely, if it fails
321 * it will retain the old buffer.
322 * If it fails it will return ENOMEM.
325 pipespace(struct pipe *cpipe, int size)
327 struct vm_object *object;
331 npages = round_page(size) / PAGE_SIZE;
332 object = cpipe->pipe_buffer.object;
335 * [re]create the object if necessary and reserve space for it
336 * in the kernel_map. The object and memory are pageable. On
337 * success, free the old resources before assigning the new
340 if (object == NULL || object->size != npages) {
342 object = vm_object_allocate(OBJT_DEFAULT, npages);
343 buffer = (caddr_t)vm_map_min(&kernel_map);
345 error = vm_map_find(&kernel_map, object, 0,
346 (vm_offset_t *)&buffer, size,
349 VM_PROT_ALL, VM_PROT_ALL,
352 if (error != KERN_SUCCESS) {
353 vm_object_deallocate(object);
357 pipe_free_kmem(cpipe);
359 cpipe->pipe_buffer.object = object;
360 cpipe->pipe_buffer.buffer = buffer;
361 cpipe->pipe_buffer.size = size;
366 cpipe->pipe_buffer.rindex = 0;
367 cpipe->pipe_buffer.windex = 0;
372 * Initialize and allocate VM and memory for pipe, pulling the pipe from
373 * our per-cpu cache if possible. For now make sure it is sized for the
374 * smaller PIPE_SIZE default.
377 pipe_create(struct pipe **cpipep)
379 globaldata_t gd = mycpu;
383 if ((cpipe = gd->gd_pipeq) != NULL) {
384 gd->gd_pipeq = cpipe->pipe_peer;
386 cpipe->pipe_peer = NULL;
387 cpipe->pipe_wantwcnt = 0;
389 cpipe = kmalloc(sizeof(struct pipe), M_PIPE, M_WAITOK|M_ZERO);
392 if ((error = pipespace(cpipe, PIPE_SIZE)) != 0)
394 vfs_timestamp(&cpipe->pipe_ctime);
395 cpipe->pipe_atime = cpipe->pipe_ctime;
396 cpipe->pipe_mtime = cpipe->pipe_ctime;
397 lwkt_token_init(&cpipe->pipe_rlock);
398 lwkt_token_init(&cpipe->pipe_wlock);
403 * MPALMOSTSAFE (acquires mplock)
406 pipe_read(struct file *fp, struct uio *uio, struct ucred *cred, int fflags)
413 u_int size; /* total bytes available */
414 u_int nsize; /* total bytes to read */
415 u_int rindex; /* contiguous bytes available */
424 orig_resid = uio->uio_resid;
429 * Setup locks, calculate nbio
431 pipe_get_mplock(&mpsave);
432 rpipe = (struct pipe *)fp->f_data;
433 lwkt_gettoken(&rlock, &rpipe->pipe_rlock);
435 if (fflags & O_FBLOCKING)
437 else if (fflags & O_FNONBLOCKING)
439 else if (fp->f_flag & O_NONBLOCK)
445 * Reads are serialized. Note howeverthat pipe_buffer.buffer and
446 * pipe_buffer.size can change out from under us when the number
447 * of bytes in the buffer are zero due to the write-side doing a
450 error = pipe_start_uio(rpipe, &rpipe->pipe_rip);
452 pipe_rel_mplock(&mpsave);
453 lwkt_reltoken(&rlock);
457 while (uio->uio_resid) {
458 size = rpipe->pipe_buffer.windex - rpipe->pipe_buffer.rindex;
461 rindex = rpipe->pipe_buffer.rindex &
462 (rpipe->pipe_buffer.size - 1);
464 if (nsize > rpipe->pipe_buffer.size - rindex)
465 nsize = rpipe->pipe_buffer.size - rindex;
466 if (nsize > (u_int)uio->uio_resid)
467 nsize = (u_int)uio->uio_resid;
469 error = uiomove(&rpipe->pipe_buffer.buffer[rindex],
474 rpipe->pipe_buffer.rindex += nsize;
478 * If the FIFO is still over half full just continue
479 * and do not try to notify the writer yet.
481 if (size - nsize >= (rpipe->pipe_buffer.size >> 1)) {
487 * When the FIFO is less then half full notify any
488 * waiting writer. WANTW can be checked while
489 * holding just the rlock.
492 if ((rpipe->pipe_state & PIPE_WANTW) == 0)
497 * If the "write-side" was blocked we wake it up. This code
498 * is reached either when the buffer is completely emptied
499 * or if it becomes more then half-empty.
501 * Pipe_state can only be modified if both the rlock and
504 if (rpipe->pipe_state & PIPE_WANTW) {
505 lwkt_gettoken(&wlock, &rpipe->pipe_wlock);
506 if (rpipe->pipe_state & PIPE_WANTW) {
508 rpipe->pipe_state &= ~PIPE_WANTW;
509 lwkt_reltoken(&wlock);
512 lwkt_reltoken(&wlock);
517 * Pick up our copy loop again if the writer sent data to
518 * us while we were messing around.
520 * On a SMP box poll up to pipe_delay nanoseconds for new
521 * data. Typically a value of 2000 to 4000 is sufficient
522 * to eradicate most IPIs/tsleeps/wakeups when a pipe
523 * is used for synchronous communications with small packets,
524 * and 8000 or so (8uS) will pipeline large buffer xfers
525 * between cpus over a pipe.
527 * For synchronous communications a hit means doing a
528 * full Awrite-Bread-Bwrite-Aread cycle in less then 2uS,
529 * where as miss requiring a tsleep/wakeup sequence
530 * will take 7uS or more.
532 if (rpipe->pipe_buffer.windex != rpipe->pipe_buffer.rindex)
535 #if defined(SMP) && defined(_RDTSC_SUPPORTED_)
540 tsc_target = tsc_get_target(pipe_delay);
541 while (tsc_test_target(tsc_target) == 0) {
542 if (rpipe->pipe_buffer.windex !=
543 rpipe->pipe_buffer.rindex) {
554 * Detect EOF condition, do not set error.
556 if (rpipe->pipe_state & PIPE_REOF)
560 * Break if some data was read, or if this was a non-blocking
572 * Last chance, interlock with WANTR.
574 lwkt_gettoken(&wlock, &rpipe->pipe_wlock);
575 size = rpipe->pipe_buffer.windex - rpipe->pipe_buffer.rindex;
577 lwkt_reltoken(&wlock);
582 * If there is no more to read in the pipe, reset its
583 * pointers to the beginning. This improves cache hit
586 * We need both locks to modify both pointers, and there
587 * must also not be a write in progress or the uiomove()
588 * in the write might block and temporarily release
589 * its wlock, then reacquire and update windex. We are
590 * only serialized against reads, not writes.
592 * XXX should we even bother resetting the indices? It
593 * might actually be more cache efficient not to.
595 if (rpipe->pipe_buffer.rindex == rpipe->pipe_buffer.windex &&
596 rpipe->pipe_wip == 0) {
597 rpipe->pipe_buffer.rindex = 0;
598 rpipe->pipe_buffer.windex = 0;
602 * Wait for more data.
604 * Pipe_state can only be set if both the rlock and wlock
607 rpipe->pipe_state |= PIPE_WANTR;
608 tsleep_interlock(rpipe);
609 lwkt_reltoken(&wlock);
610 error = tsleep(rpipe, PCATCH, "piperd", 0);
611 ++pipe_rblocked_count;
615 pipe_end_uio(rpipe, &rpipe->pipe_rip);
618 * Uptime last access time
620 if (error == 0 && nread)
621 vfs_timestamp(&rpipe->pipe_atime);
624 * If we drained the FIFO more then half way then handle
625 * write blocking hysteresis.
627 * Note that PIPE_WANTW cannot be set by the writer without
628 * it holding both rlock and wlock, so we can test it
629 * while holding just rlock.
632 if (rpipe->pipe_state & PIPE_WANTW) {
633 lwkt_gettoken(&wlock, &rpipe->pipe_wlock);
634 if (rpipe->pipe_state & PIPE_WANTW) {
635 rpipe->pipe_state &= ~PIPE_WANTW;
636 lwkt_reltoken(&wlock);
639 lwkt_reltoken(&wlock);
643 size = rpipe->pipe_buffer.windex - rpipe->pipe_buffer.rindex;
644 lwkt_reltoken(&rlock);
647 * If enough space is available in buffer then wakeup sel writers?
649 if ((rpipe->pipe_buffer.size - size) >= PIPE_BUF)
650 pipeselwakeup(rpipe);
651 pipe_rel_mplock(&mpsave);
656 * MPALMOSTSAFE - acquires mplock
659 pipe_write(struct file *fp, struct uio *uio, struct ucred *cred, int fflags)
664 struct pipe *wpipe, *rpipe;
672 pipe_get_mplock(&mpsave);
675 * Writes go to the peer. The peer will always exist.
677 rpipe = (struct pipe *) fp->f_data;
678 wpipe = rpipe->pipe_peer;
679 lwkt_gettoken(&wlock, &wpipe->pipe_wlock);
680 if (wpipe->pipe_state & PIPE_WEOF) {
681 pipe_rel_mplock(&mpsave);
682 lwkt_reltoken(&wlock);
687 * Degenerate case (EPIPE takes prec)
689 if (uio->uio_resid == 0) {
690 pipe_rel_mplock(&mpsave);
691 lwkt_reltoken(&wlock);
696 * Writes are serialized (start_uio must be called with wlock)
698 error = pipe_start_uio(wpipe, &wpipe->pipe_wip);
700 pipe_rel_mplock(&mpsave);
701 lwkt_reltoken(&wlock);
705 if (fflags & O_FBLOCKING)
707 else if (fflags & O_FNONBLOCKING)
709 else if (fp->f_flag & O_NONBLOCK)
715 * If it is advantageous to resize the pipe buffer, do
716 * so. We are write-serialized so we can block safely.
718 if ((wpipe->pipe_buffer.size <= PIPE_SIZE) &&
719 (pipe_nbig < pipe_maxbig) &&
720 wpipe->pipe_wantwcnt > 4 &&
721 (wpipe->pipe_buffer.rindex == wpipe->pipe_buffer.windex)) {
723 * Recheck after lock.
725 lwkt_gettoken(&rlock, &wpipe->pipe_rlock);
726 if ((wpipe->pipe_buffer.size <= PIPE_SIZE) &&
727 (pipe_nbig < pipe_maxbig) &&
728 (wpipe->pipe_buffer.rindex == wpipe->pipe_buffer.windex)) {
729 atomic_add_int(&pipe_nbig, 1);
730 if (pipespace(wpipe, BIG_PIPE_SIZE) == 0)
733 atomic_subtract_int(&pipe_nbig, 1);
735 lwkt_reltoken(&rlock);
738 orig_resid = uio->uio_resid;
741 while (uio->uio_resid) {
742 if (wpipe->pipe_state & PIPE_WEOF) {
747 windex = wpipe->pipe_buffer.windex &
748 (wpipe->pipe_buffer.size - 1);
749 space = wpipe->pipe_buffer.size -
750 (wpipe->pipe_buffer.windex - wpipe->pipe_buffer.rindex);
753 /* Writes of size <= PIPE_BUF must be atomic. */
754 if ((space < uio->uio_resid) && (orig_resid <= PIPE_BUF))
758 * Write to fill, read size handles write hysteresis. Also
759 * additional restrictions can cause select-based non-blocking
766 * Transfer size is minimum of uio transfer
767 * and free space in pipe buffer.
769 * Limit each uiocopy to no more then PIPE_SIZE
770 * so we can keep the gravy train going on a
771 * SMP box. This doubles the performance for
772 * write sizes > 16K. Otherwise large writes
773 * wind up doing an inefficient synchronous
776 if (space > (u_int)uio->uio_resid)
777 space = (u_int)uio->uio_resid;
778 if (space > PIPE_SIZE)
782 * First segment to transfer is minimum of
783 * transfer size and contiguous space in
784 * pipe buffer. If first segment to transfer
785 * is less than the transfer size, we've got
786 * a wraparound in the buffer.
788 segsize = wpipe->pipe_buffer.size - windex;
793 * If this is the first loop and the reader is
794 * blocked, do a preemptive wakeup of the reader.
796 * This works for both SMP and UP. On SMP the IPI
797 * latency plus the wlock interlock on the reader
798 * side is the fastest way to get the reader going.
799 * (The scheduler will hard loop on lock tokens).
801 * NOTE: We can't clear WANTR here without acquiring
802 * the rlock, which we don't want to do here!
804 if ((wpipe->pipe_state & PIPE_WANTR) && pipe_mpsafe > 1)
808 * Transfer segment, which may include a wrap-around.
809 * Update windex to account for both all in one go
810 * so the reader can read() the data atomically.
812 error = uiomove(&wpipe->pipe_buffer.buffer[windex],
814 if (error == 0 && segsize < space) {
815 segsize = space - segsize;
816 error = uiomove(&wpipe->pipe_buffer.buffer[0],
822 wpipe->pipe_buffer.windex += space;
828 * We need both the rlock and the wlock to interlock against
829 * the EOF, WANTW, and size checks, and to modify pipe_state.
831 * These are token locks so we do not have to worry about
834 lwkt_gettoken(&rlock, &wpipe->pipe_rlock);
837 * If the "read-side" has been blocked, wake it up now
838 * and yield to let it drain synchronously rather
841 if (wpipe->pipe_state & PIPE_WANTR) {
842 wpipe->pipe_state &= ~PIPE_WANTR;
847 * don't block on non-blocking I/O
850 lwkt_reltoken(&rlock);
856 * re-test whether we have to block in the writer after
857 * acquiring both locks, in case the reader opened up
860 space = wpipe->pipe_buffer.size -
861 (wpipe->pipe_buffer.windex - wpipe->pipe_buffer.rindex);
863 if ((space < uio->uio_resid) && (orig_resid <= PIPE_BUF))
867 * We have no more space and have something to offer,
868 * wake up select/poll.
871 pipeselwakeup(wpipe);
872 ++wpipe->pipe_wantwcnt;
873 wpipe->pipe_state |= PIPE_WANTW;
874 error = tsleep(wpipe, PCATCH, "pipewr", 0);
875 ++pipe_wblocked_count;
877 lwkt_reltoken(&rlock);
880 * Break out if we errored or the read side wants us to go
885 if (wpipe->pipe_state & PIPE_WEOF) {
890 pipe_end_uio(wpipe, &wpipe->pipe_wip);
893 * If we have put any characters in the buffer, we wake up
896 * Both rlock and wlock are required to be able to modify pipe_state.
898 if (wpipe->pipe_buffer.windex != wpipe->pipe_buffer.rindex) {
899 if (wpipe->pipe_state & PIPE_WANTR) {
900 lwkt_gettoken(&rlock, &wpipe->pipe_rlock);
901 if (wpipe->pipe_state & PIPE_WANTR) {
902 wpipe->pipe_state &= ~PIPE_WANTR;
903 lwkt_reltoken(&rlock);
906 lwkt_reltoken(&rlock);
912 * Don't return EPIPE if I/O was successful
914 if ((wpipe->pipe_buffer.rindex == wpipe->pipe_buffer.windex) &&
915 (uio->uio_resid == 0) &&
921 vfs_timestamp(&wpipe->pipe_mtime);
924 * We have something to offer,
925 * wake up select/poll.
927 space = wpipe->pipe_buffer.windex - wpipe->pipe_buffer.rindex;
928 lwkt_reltoken(&wlock);
930 pipeselwakeup(wpipe);
931 pipe_rel_mplock(&mpsave);
936 * MPALMOSTSAFE - acquires mplock
938 * we implement a very minimal set of ioctls for compatibility with sockets.
941 pipe_ioctl(struct file *fp, u_long cmd, caddr_t data, struct ucred *cred)
949 pipe_get_mplock(&mpsave);
950 mpipe = (struct pipe *)fp->f_data;
952 lwkt_gettoken(&rlock, &mpipe->pipe_rlock);
953 lwkt_gettoken(&wlock, &mpipe->pipe_wlock);
958 mpipe->pipe_state |= PIPE_ASYNC;
960 mpipe->pipe_state &= ~PIPE_ASYNC;
965 *(int *)data = mpipe->pipe_buffer.windex -
966 mpipe->pipe_buffer.rindex;
971 error = fsetown(*(int *)data, &mpipe->pipe_sigio);
975 *(int *)data = fgetown(mpipe->pipe_sigio);
979 /* This is deprecated, FIOSETOWN should be used instead. */
981 error = fsetown(-(*(int *)data), &mpipe->pipe_sigio);
986 /* This is deprecated, FIOGETOWN should be used instead. */
987 *(int *)data = -fgetown(mpipe->pipe_sigio);
994 lwkt_reltoken(&rlock);
995 lwkt_reltoken(&wlock);
996 pipe_rel_mplock(&mpsave);
1002 * MPALMOSTSAFE - acquires mplock
1005 pipe_poll(struct file *fp, int events, struct ucred *cred)
1013 pipe_get_mplock(&mpsave);
1014 rpipe = (struct pipe *)fp->f_data;
1015 wpipe = rpipe->pipe_peer;
1016 if (events & (POLLIN | POLLRDNORM)) {
1017 if ((rpipe->pipe_buffer.windex != rpipe->pipe_buffer.rindex) ||
1018 (rpipe->pipe_state & PIPE_REOF)) {
1019 revents |= events & (POLLIN | POLLRDNORM);
1023 if (events & (POLLOUT | POLLWRNORM)) {
1024 if (wpipe == NULL || (wpipe->pipe_state & PIPE_WEOF)) {
1025 revents |= events & (POLLOUT | POLLWRNORM);
1027 space = wpipe->pipe_buffer.windex -
1028 wpipe->pipe_buffer.rindex;
1029 space = wpipe->pipe_buffer.size - space;
1030 if (space >= PIPE_BUF)
1031 revents |= events & (POLLOUT | POLLWRNORM);
1035 if ((rpipe->pipe_state & PIPE_REOF) ||
1037 (wpipe->pipe_state & PIPE_WEOF))
1041 if (events & (POLLIN | POLLRDNORM)) {
1042 selrecord(curthread, &rpipe->pipe_sel);
1043 rpipe->pipe_state |= PIPE_SEL;
1046 if (events & (POLLOUT | POLLWRNORM)) {
1047 selrecord(curthread, &wpipe->pipe_sel);
1048 wpipe->pipe_state |= PIPE_SEL;
1051 pipe_rel_mplock(&mpsave);
1056 * MPALMOSTSAFE - acquires mplock
1059 pipe_stat(struct file *fp, struct stat *ub, struct ucred *cred)
1064 pipe_get_mplock(&mpsave);
1065 pipe = (struct pipe *)fp->f_data;
1067 bzero((caddr_t)ub, sizeof(*ub));
1068 ub->st_mode = S_IFIFO;
1069 ub->st_blksize = pipe->pipe_buffer.size;
1070 ub->st_size = pipe->pipe_buffer.windex - pipe->pipe_buffer.rindex;
1071 ub->st_blocks = (ub->st_size + ub->st_blksize - 1) / ub->st_blksize;
1072 ub->st_atimespec = pipe->pipe_atime;
1073 ub->st_mtimespec = pipe->pipe_mtime;
1074 ub->st_ctimespec = pipe->pipe_ctime;
1076 * Left as 0: st_dev, st_ino, st_nlink, st_uid, st_gid, st_rdev,
1078 * XXX (st_dev, st_ino) should be unique.
1080 pipe_rel_mplock(&mpsave);
1085 * MPALMOSTSAFE - acquires mplock
1088 pipe_close(struct file *fp)
1093 cpipe = (struct pipe *)fp->f_data;
1094 fp->f_ops = &badfileops;
1096 funsetown(cpipe->pipe_sigio);
1103 * Shutdown one or both directions of a full-duplex pipe.
1105 * MPALMOSTSAFE - acquires mplock
1108 pipe_shutdown(struct file *fp, int how)
1113 lwkt_tokref rpipe_rlock;
1114 lwkt_tokref rpipe_wlock;
1115 lwkt_tokref wpipe_rlock;
1116 lwkt_tokref wpipe_wlock;
1119 pipe_get_mplock(&mpsave);
1120 rpipe = (struct pipe *)fp->f_data;
1121 wpipe = rpipe->pipe_peer;
1124 * We modify pipe_state on both pipes, which means we need
1127 lwkt_gettoken(&rpipe_rlock, &rpipe->pipe_rlock);
1128 lwkt_gettoken(&rpipe_wlock, &rpipe->pipe_wlock);
1129 lwkt_gettoken(&wpipe_rlock, &wpipe->pipe_rlock);
1130 lwkt_gettoken(&wpipe_wlock, &wpipe->pipe_wlock);
1135 rpipe->pipe_state |= PIPE_REOF;
1136 wpipe->pipe_state |= PIPE_WEOF;
1137 if (rpipe->pipe_state & PIPE_WANTR) {
1138 rpipe->pipe_state &= ~PIPE_WANTR;
1141 if (wpipe->pipe_state & PIPE_WANTW) {
1142 wpipe->pipe_state &= ~PIPE_WANTW;
1145 pipeselwakeup(rpipe);
1151 wpipe->pipe_state |= PIPE_WEOF;
1152 rpipe->pipe_state |= PIPE_REOF;
1153 if (wpipe->pipe_state & PIPE_WANTW) {
1154 wpipe->pipe_state &= ~PIPE_WANTW;
1157 if (rpipe->pipe_state & PIPE_WANTR) {
1158 rpipe->pipe_state &= ~PIPE_WANTR;
1161 pipeselwakeup(wpipe);
1166 lwkt_reltoken(&rpipe_rlock);
1167 lwkt_reltoken(&rpipe_wlock);
1168 lwkt_reltoken(&wpipe_rlock);
1169 lwkt_reltoken(&wpipe_wlock);
1171 pipe_rel_mplock(&mpsave);
1176 pipe_free_kmem(struct pipe *cpipe)
1178 if (cpipe->pipe_buffer.buffer != NULL) {
1179 if (cpipe->pipe_buffer.size > PIPE_SIZE)
1180 atomic_subtract_int(&pipe_nbig, 1);
1181 kmem_free(&kernel_map,
1182 (vm_offset_t)cpipe->pipe_buffer.buffer,
1183 cpipe->pipe_buffer.size);
1184 cpipe->pipe_buffer.buffer = NULL;
1185 cpipe->pipe_buffer.object = NULL;
1190 * Close the pipe. The slock must be held to interlock against simultanious
1191 * closes. The rlock and wlock must be held to adjust the pipe_state.
1194 pipeclose(struct pipe *cpipe)
1198 lwkt_tokref cpipe_rlock;
1199 lwkt_tokref cpipe_wlock;
1200 lwkt_tokref ppipe_rlock;
1201 lwkt_tokref ppipe_wlock;
1207 * The slock may not have been allocated yet (close during
1210 * We need both the read and write tokens to modify pipe_state.
1212 if (cpipe->pipe_slock)
1213 lockmgr(cpipe->pipe_slock, LK_EXCLUSIVE);
1214 lwkt_gettoken(&cpipe_rlock, &cpipe->pipe_rlock);
1215 lwkt_gettoken(&cpipe_wlock, &cpipe->pipe_wlock);
1218 * Set our state, wakeup anyone waiting in select, and
1219 * wakeup anyone blocked on our pipe.
1221 cpipe->pipe_state |= PIPE_CLOSED | PIPE_REOF | PIPE_WEOF;
1222 pipeselwakeup(cpipe);
1223 if (cpipe->pipe_state & (PIPE_WANTR | PIPE_WANTW)) {
1224 cpipe->pipe_state &= ~(PIPE_WANTR | PIPE_WANTW);
1229 * Disconnect from peer
1231 if ((ppipe = cpipe->pipe_peer) != NULL) {
1232 lwkt_gettoken(&ppipe_rlock, &ppipe->pipe_rlock);
1233 lwkt_gettoken(&ppipe_wlock, &ppipe->pipe_wlock);
1234 ppipe->pipe_state |= PIPE_REOF;
1235 pipeselwakeup(ppipe);
1236 if (ppipe->pipe_state & (PIPE_WANTR | PIPE_WANTW)) {
1237 ppipe->pipe_state &= ~(PIPE_WANTR | PIPE_WANTW);
1240 if (SLIST_FIRST(&ppipe->pipe_sel.si_note)) {
1242 KNOTE(&ppipe->pipe_sel.si_note, 0);
1245 lwkt_reltoken(&ppipe_rlock);
1246 lwkt_reltoken(&ppipe_wlock);
1250 * If the peer is also closed we can free resources for both
1251 * sides, otherwise we leave our side intact to deal with any
1252 * races (since we only have the slock).
1254 if (ppipe && (ppipe->pipe_state & PIPE_CLOSED)) {
1255 cpipe->pipe_peer = NULL;
1256 ppipe->pipe_peer = NULL;
1257 ppipe->pipe_slock = NULL; /* we will free the slock */
1262 lwkt_reltoken(&cpipe_rlock);
1263 lwkt_reltoken(&cpipe_wlock);
1264 if (cpipe->pipe_slock)
1265 lockmgr(cpipe->pipe_slock, LK_RELEASE);
1268 * If we disassociated from our peer we can free resources
1270 if (ppipe == NULL) {
1272 if (cpipe->pipe_slock) {
1273 kfree(cpipe->pipe_slock, M_PIPE);
1274 cpipe->pipe_slock = NULL;
1276 if (gd->gd_pipeqcount >= pipe_maxcache ||
1277 cpipe->pipe_buffer.size != PIPE_SIZE
1279 pipe_free_kmem(cpipe);
1280 kfree(cpipe, M_PIPE);
1282 cpipe->pipe_state = 0;
1283 cpipe->pipe_peer = gd->gd_pipeq;
1284 gd->gd_pipeq = cpipe;
1285 ++gd->gd_pipeqcount;
1291 * MPALMOSTSAFE - acquires mplock
1294 pipe_kqfilter(struct file *fp, struct knote *kn)
1299 cpipe = (struct pipe *)kn->kn_fp->f_data;
1301 switch (kn->kn_filter) {
1303 kn->kn_fop = &pipe_rfiltops;
1306 kn->kn_fop = &pipe_wfiltops;
1307 cpipe = cpipe->pipe_peer;
1308 if (cpipe == NULL) {
1309 /* other end of pipe has been closed */
1317 kn->kn_hook = (caddr_t)cpipe;
1319 SLIST_INSERT_HEAD(&cpipe->pipe_sel.si_note, kn, kn_selnext);
1325 filt_pipedetach(struct knote *kn)
1327 struct pipe *cpipe = (struct pipe *)kn->kn_hook;
1329 SLIST_REMOVE(&cpipe->pipe_sel.si_note, kn, knote, kn_selnext);
1334 filt_piperead(struct knote *kn, long hint)
1336 struct pipe *rpipe = (struct pipe *)kn->kn_fp->f_data;
1338 kn->kn_data = rpipe->pipe_buffer.windex - rpipe->pipe_buffer.rindex;
1341 if (rpipe->pipe_state & PIPE_REOF) {
1342 kn->kn_flags |= EV_EOF;
1345 return (kn->kn_data > 0);
1350 filt_pipewrite(struct knote *kn, long hint)
1352 struct pipe *rpipe = (struct pipe *)kn->kn_fp->f_data;
1353 struct pipe *wpipe = rpipe->pipe_peer;
1357 if ((wpipe == NULL) || (wpipe->pipe_state & PIPE_WEOF)) {
1359 kn->kn_flags |= EV_EOF;
1362 space = wpipe->pipe_buffer.windex -
1363 wpipe->pipe_buffer.rindex;
1364 space = wpipe->pipe_buffer.size - space;
1365 kn->kn_data = space;
1366 return (kn->kn_data >= PIPE_BUF);