xref: /netbsd-src/sys/kern/sys_pipe.c (revision 33cd1faa348fe1cb7947ea59b0556b9bad76cee9)
1 /*	$NetBSD: sys_pipe.c,v 1.55 2004/03/24 20:25:28 pooka Exp $	*/
2 
3 /*-
4  * Copyright (c) 2003 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Paul Kranenburg.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *        This product includes software developed by the NetBSD
21  *        Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 /*
40  * Copyright (c) 1996 John S. Dyson
41  * All rights reserved.
42  *
43  * Redistribution and use in source and binary forms, with or without
44  * modification, are permitted provided that the following conditions
45  * are met:
46  * 1. Redistributions of source code must retain the above copyright
47  *    notice immediately at the beginning of the file, without modification,
48  *    this list of conditions, and the following disclaimer.
49  * 2. Redistributions in binary form must reproduce the above copyright
50  *    notice, this list of conditions and the following disclaimer in the
51  *    documentation and/or other materials provided with the distribution.
52  * 3. Absolutely no warranty of function or purpose is made by the author
53  *    John S. Dyson.
54  * 4. Modifications may be freely made to this file if the above conditions
55  *    are met.
56  *
57  * $FreeBSD: src/sys/kern/sys_pipe.c,v 1.95 2002/03/09 22:06:31 alfred Exp $
58  */
59 
60 /*
61  * This file contains a high-performance replacement for the socket-based
62  * pipes scheme originally used in FreeBSD/4.4Lite.  It does not support
63  * all features of sockets, but does do everything that pipes normally
64  * do.
65  *
66  * Adaption for NetBSD UVM, including uvm_loan() based direct write, was
67  * written by Jaromir Dolecek.
68  */
69 
70 /*
71  * This code has two modes of operation, a small write mode and a large
72  * write mode.  The small write mode acts like conventional pipes with
73  * a kernel buffer.  If the buffer is less than PIPE_MINDIRECT, then the
74  * "normal" pipe buffering is done.  If the buffer is between PIPE_MINDIRECT
75  * and PIPE_SIZE in size it is mapped read-only into the kernel address space
76  * using the UVM page loan facility from where the receiving process can copy
77  * the data directly from the pages in the sending process.
78  *
79  * The constant PIPE_MINDIRECT is chosen to make sure that buffering will
80  * happen for small transfers so that the system will not spend all of
81  * its time context switching.  PIPE_SIZE is constrained by the
82  * amount of kernel virtual memory.
83  */
84 
85 #include <sys/cdefs.h>
86 __KERNEL_RCSID(0, "$NetBSD: sys_pipe.c,v 1.55 2004/03/24 20:25:28 pooka Exp $");
87 
88 #include <sys/param.h>
89 #include <sys/systm.h>
90 #include <sys/proc.h>
91 #include <sys/fcntl.h>
92 #include <sys/file.h>
93 #include <sys/filedesc.h>
94 #include <sys/filio.h>
95 #include <sys/kernel.h>
96 #include <sys/lock.h>
97 #include <sys/ttycom.h>
98 #include <sys/stat.h>
99 #include <sys/malloc.h>
100 #include <sys/poll.h>
101 #include <sys/signalvar.h>
102 #include <sys/vnode.h>
103 #include <sys/uio.h>
104 #include <sys/lock.h>
105 #include <sys/select.h>
106 #include <sys/mount.h>
107 #include <sys/sa.h>
108 #include <sys/syscallargs.h>
109 #include <uvm/uvm.h>
110 #include <sys/sysctl.h>
111 #include <sys/kernel.h>
112 
113 #include <sys/pipe.h>
114 
115 /*
116  * Avoid microtime(9), it's slow. We don't guard the read from time(9)
117  * with splclock(9) since we don't actually need to be THAT sure the access
118  * is atomic.
119  */
120 #define PIPE_TIMESTAMP(tvp)	(*(tvp) = time)
121 
122 
123 /*
124  * Use this define if you want to disable *fancy* VM things.  Expect an
125  * approx 30% decrease in transfer rate.
126  */
127 /* #define PIPE_NODIRECT */
128 
129 /*
130  * interfaces to the outside world
131  */
132 static int pipe_read(struct file *fp, off_t *offset, struct uio *uio,
133 		struct ucred *cred, int flags);
134 static int pipe_write(struct file *fp, off_t *offset, struct uio *uio,
135 		struct ucred *cred, int flags);
136 static int pipe_close(struct file *fp, struct proc *p);
137 static int pipe_poll(struct file *fp, int events, struct proc *p);
138 static int pipe_fcntl(struct file *fp, u_int com, void *data,
139 		struct proc *p);
140 static int pipe_kqfilter(struct file *fp, struct knote *kn);
141 static int pipe_stat(struct file *fp, struct stat *sb, struct proc *p);
142 static int pipe_ioctl(struct file *fp, u_long cmd, void *data,
143 		struct proc *p);
144 
145 static struct fileops pipeops = {
146 	pipe_read, pipe_write, pipe_ioctl, pipe_fcntl, pipe_poll,
147 	pipe_stat, pipe_close, pipe_kqfilter
148 };
149 
150 /*
151  * Default pipe buffer size(s), this can be kind-of large now because pipe
152  * space is pageable.  The pipe code will try to maintain locality of
153  * reference for performance reasons, so small amounts of outstanding I/O
154  * will not wipe the cache.
155  */
156 #define MINPIPESIZE (PIPE_SIZE/3)
157 #define MAXPIPESIZE (2*PIPE_SIZE/3)
158 
159 /*
160  * Maximum amount of kva for pipes -- this is kind-of a soft limit, but
161  * is there so that on large systems, we don't exhaust it.
162  */
163 #define MAXPIPEKVA (8*1024*1024)
164 static int maxpipekva = MAXPIPEKVA;
165 
166 /*
167  * Limit for direct transfers, we cannot, of course limit
168  * the amount of kva for pipes in general though.
169  */
170 #define LIMITPIPEKVA (16*1024*1024)
171 static int limitpipekva = LIMITPIPEKVA;
172 
173 /*
174  * Limit the number of "big" pipes
175  */
176 #define LIMITBIGPIPES  32
177 static int maxbigpipes = LIMITBIGPIPES;
178 static int nbigpipe = 0;
179 
180 /*
181  * Amount of KVA consumed by pipe buffers.
182  */
183 static int amountpipekva = 0;
184 
185 MALLOC_DEFINE(M_PIPE, "pipe", "Pipe structures");
186 
187 static void pipeclose(struct file *fp, struct pipe *pipe);
188 static void pipe_free_kmem(struct pipe *pipe);
189 static int pipe_create(struct pipe **pipep, int allockva);
190 static int pipelock(struct pipe *pipe, int catch);
191 static __inline void pipeunlock(struct pipe *pipe);
192 static void pipeselwakeup(struct pipe *pipe, struct pipe *sigp, void *data,
193     int code);
194 #ifndef PIPE_NODIRECT
195 static int pipe_direct_write(struct file *fp, struct pipe *wpipe,
196     struct uio *uio);
197 #endif
198 static int pipespace(struct pipe *pipe, int size);
199 
200 #ifndef PIPE_NODIRECT
201 static int pipe_loan_alloc(struct pipe *, int);
202 static void pipe_loan_free(struct pipe *);
203 #endif /* PIPE_NODIRECT */
204 
205 static struct pool pipe_pool;
206 
207 /*
208  * The pipe system call for the DTYPE_PIPE type of pipes
209  */
210 
211 /* ARGSUSED */
212 int
213 sys_pipe(l, v, retval)
214 	struct lwp *l;
215 	void *v;
216 	register_t *retval;
217 {
218 	struct file *rf, *wf;
219 	struct pipe *rpipe, *wpipe;
220 	int fd, error;
221 	struct proc *p;
222 
223 	p = l->l_proc;
224 	rpipe = wpipe = NULL;
225 	if (pipe_create(&rpipe, 1) || pipe_create(&wpipe, 0)) {
226 		pipeclose(NULL, rpipe);
227 		pipeclose(NULL, wpipe);
228 		return (ENFILE);
229 	}
230 
231 	/*
232 	 * Note: the file structure returned from falloc() is marked
233 	 * as 'larval' initially. Unless we mark it as 'mature' by
234 	 * FILE_SET_MATURE(), any attempt to do anything with it would
235 	 * return EBADF, including e.g. dup(2) or close(2). This avoids
236 	 * file descriptor races if we block in the second falloc().
237 	 */
238 
239 	error = falloc(p, &rf, &fd);
240 	if (error)
241 		goto free2;
242 	retval[0] = fd;
243 	rf->f_flag = FREAD;
244 	rf->f_type = DTYPE_PIPE;
245 	rf->f_data = (caddr_t)rpipe;
246 	rf->f_ops = &pipeops;
247 
248 	error = falloc(p, &wf, &fd);
249 	if (error)
250 		goto free3;
251 	retval[1] = fd;
252 	wf->f_flag = FWRITE;
253 	wf->f_type = DTYPE_PIPE;
254 	wf->f_data = (caddr_t)wpipe;
255 	wf->f_ops = &pipeops;
256 
257 	rpipe->pipe_peer = wpipe;
258 	wpipe->pipe_peer = rpipe;
259 
260 	FILE_SET_MATURE(rf);
261 	FILE_SET_MATURE(wf);
262 	FILE_UNUSE(rf, p);
263 	FILE_UNUSE(wf, p);
264 	return (0);
265 free3:
266 	FILE_UNUSE(rf, p);
267 	ffree(rf);
268 	fdremove(p->p_fd, retval[0]);
269 free2:
270 	pipeclose(NULL, wpipe);
271 	pipeclose(NULL, rpipe);
272 
273 	return (error);
274 }
275 
276 /*
277  * Allocate kva for pipe circular buffer, the space is pageable
278  * This routine will 'realloc' the size of a pipe safely, if it fails
279  * it will retain the old buffer.
280  * If it fails it will return ENOMEM.
281  */
282 static int
283 pipespace(pipe, size)
284 	struct pipe *pipe;
285 	int size;
286 {
287 	caddr_t buffer;
288 	/*
289 	 * Allocate pageable virtual address space. Physical memory is
290 	 * allocated on demand.
291 	 */
292 	buffer = (caddr_t) uvm_km_valloc(kernel_map, round_page(size));
293 	if (buffer == NULL)
294 		return (ENOMEM);
295 
296 	/* free old resources if we're resizing */
297 	pipe_free_kmem(pipe);
298 	pipe->pipe_buffer.buffer = buffer;
299 	pipe->pipe_buffer.size = size;
300 	pipe->pipe_buffer.in = 0;
301 	pipe->pipe_buffer.out = 0;
302 	pipe->pipe_buffer.cnt = 0;
303 	amountpipekva += pipe->pipe_buffer.size;
304 	return (0);
305 }
306 
307 /*
308  * Initialize and allocate VM and memory for pipe.
309  */
310 static int
311 pipe_create(pipep, allockva)
312 	struct pipe **pipep;
313 	int allockva;
314 {
315 	struct pipe *pipe;
316 	int error;
317 
318 	pipe = *pipep = pool_get(&pipe_pool, PR_WAITOK);
319 
320 	/* Initialize */
321 	memset(pipe, 0, sizeof(struct pipe));
322 	pipe->pipe_state = PIPE_SIGNALR;
323 
324 	PIPE_TIMESTAMP(&pipe->pipe_ctime);
325 	pipe->pipe_atime = pipe->pipe_ctime;
326 	pipe->pipe_mtime = pipe->pipe_ctime;
327 	simple_lock_init(&pipe->pipe_slock);
328 	lockinit(&pipe->pipe_lock, PRIBIO | PCATCH, "pipelk", 0, 0);
329 
330 	if (allockva && (error = pipespace(pipe, PIPE_SIZE)))
331 		return (error);
332 
333 	return (0);
334 }
335 
336 
337 /*
338  * Lock a pipe for I/O, blocking other access
339  * Called with pipe spin lock held.
340  * Return with pipe spin lock released on success.
341  */
342 static int
343 pipelock(pipe, catch)
344 	struct pipe *pipe;
345 	int catch;
346 {
347 	int error;
348 
349 	LOCK_ASSERT(simple_lock_held(&pipe->pipe_slock));
350 
351 	while (1) {
352 		error = lockmgr(&pipe->pipe_lock, LK_EXCLUSIVE | LK_INTERLOCK,
353 				&pipe->pipe_slock);
354 		if (error == 0)
355 			break;
356 
357 		simple_lock(&pipe->pipe_slock);
358 		if (catch || (error != EINTR && error != ERESTART))
359 			break;
360 		/*
361 		 * XXX XXX XXX
362 		 * The pipe lock is initialised with PCATCH on and we cannot
363 		 * override this in a lockmgr() call. Thus a pending signal
364 		 * will cause lockmgr() to return with EINTR or ERESTART.
365 		 * We cannot simply re-enter lockmgr() at this point since
366 		 * the pending signals have not yet been posted and would
367 		 * cause an immediate EINTR/ERESTART return again.
368 		 * As a workaround we pause for a while here, giving the lock
369 		 * a chance to drain, before trying again.
370 		 * XXX XXX XXX
371 		 *
372 		 * NOTE: Consider dropping PCATCH from this lock; in practice
373 		 * it is never held for long enough periods for having it
374 		 * interruptable at the start of pipe_read/pipe_write to be
375 		 * beneficial.
376 		 */
377 		(void) ltsleep(&lbolt, PRIBIO, "rstrtpipelock", hz,
378 		    &pipe->pipe_slock);
379 	}
380 	return (error);
381 }
382 
383 /*
384  * unlock a pipe I/O lock
385  */
386 static __inline void
387 pipeunlock(pipe)
388 	struct pipe *pipe;
389 {
390 
391 	lockmgr(&pipe->pipe_lock, LK_RELEASE, NULL);
392 }
393 
394 /*
395  * Select/poll wakup. This also sends SIGIO to peer connected to
396  * 'sigpipe' side of pipe.
397  */
398 static void
399 pipeselwakeup(selp, sigp, data, code)
400 	struct pipe *selp, *sigp;
401 	void *data;
402 	int code;
403 {
404 	int band;
405 
406 	selnotify(&selp->pipe_sel, NOTE_SUBMIT);
407 
408 	if (sigp == NULL || (sigp->pipe_state & PIPE_ASYNC) == 0)
409 		return;
410 
411 	switch (code) {
412 	case POLL_IN:
413 		band = POLLIN|POLLRDNORM;
414 		break;
415 	case POLL_OUT:
416 		band = POLLOUT|POLLWRNORM;
417 		break;
418 	case POLL_HUP:
419 		band = POLLHUP;
420 		break;
421 #if POLL_HUP != POLL_ERR
422 	case POLL_ERR:
423 		band = POLLERR;
424 		break;
425 #endif
426 	default:
427 		band = 0;
428 #ifdef DIAGNOSTIC
429 		printf("bad siginfo code %d in pipe notification.\n", code);
430 #endif
431 		break;
432 	}
433 
434 	fownsignal(sigp->pipe_pgid, SIGIO, code, band, selp);
435 }
436 
437 /* ARGSUSED */
438 static int
439 pipe_read(fp, offset, uio, cred, flags)
440 	struct file *fp;
441 	off_t *offset;
442 	struct uio *uio;
443 	struct ucred *cred;
444 	int flags;
445 {
446 	struct pipe *rpipe = (struct pipe *) fp->f_data;
447 	struct pipebuf *bp = &rpipe->pipe_buffer;
448 	int error;
449 	size_t nread = 0;
450 	size_t size;
451 	size_t ocnt;
452 
453 	PIPE_LOCK(rpipe);
454 	++rpipe->pipe_busy;
455 	ocnt = bp->cnt;
456 
457 again:
458 	error = pipelock(rpipe, 1);
459 	if (error)
460 		goto unlocked_error;
461 
462 	while (uio->uio_resid) {
463 		/*
464 		 * normal pipe buffer receive
465 		 */
466 		if (bp->cnt > 0) {
467 			size = bp->size - bp->out;
468 			if (size > bp->cnt)
469 				size = bp->cnt;
470 			if (size > uio->uio_resid)
471 				size = uio->uio_resid;
472 
473 			error = uiomove(&bp->buffer[bp->out], size, uio);
474 			if (error)
475 				break;
476 
477 			bp->out += size;
478 			if (bp->out >= bp->size)
479 				bp->out = 0;
480 
481 			bp->cnt -= size;
482 
483 			/*
484 			 * If there is no more to read in the pipe, reset
485 			 * its pointers to the beginning.  This improves
486 			 * cache hit stats.
487 			 */
488 			if (bp->cnt == 0) {
489 				bp->in = 0;
490 				bp->out = 0;
491 			}
492 			nread += size;
493 #ifndef PIPE_NODIRECT
494 		} else if ((rpipe->pipe_state & PIPE_DIRECTR) != 0) {
495 			/*
496 			 * Direct copy, bypassing a kernel buffer.
497 			 */
498 			caddr_t	va;
499 
500 			KASSERT(rpipe->pipe_state & PIPE_DIRECTW);
501 
502 			size = rpipe->pipe_map.cnt;
503 			if (size > uio->uio_resid)
504 				size = uio->uio_resid;
505 
506 			va = (caddr_t) rpipe->pipe_map.kva +
507 			    rpipe->pipe_map.pos;
508 			error = uiomove(va, size, uio);
509 			if (error)
510 				break;
511 			nread += size;
512 			rpipe->pipe_map.pos += size;
513 			rpipe->pipe_map.cnt -= size;
514 			if (rpipe->pipe_map.cnt == 0) {
515 				PIPE_LOCK(rpipe);
516 				rpipe->pipe_state &= ~PIPE_DIRECTR;
517 				wakeup(rpipe);
518 				PIPE_UNLOCK(rpipe);
519 			}
520 #endif
521 		} else {
522 			/*
523 			 * Break if some data was read.
524 			 */
525 			if (nread > 0)
526 				break;
527 
528 			PIPE_LOCK(rpipe);
529 
530 			/*
531 			 * detect EOF condition
532 			 * read returns 0 on EOF, no need to set error
533 			 */
534 			if (rpipe->pipe_state & PIPE_EOF) {
535 				PIPE_UNLOCK(rpipe);
536 				break;
537 			}
538 
539 			/*
540 			 * don't block on non-blocking I/O
541 			 */
542 			if (fp->f_flag & FNONBLOCK) {
543 				PIPE_UNLOCK(rpipe);
544 				error = EAGAIN;
545 				break;
546 			}
547 
548 			/*
549 			 * Unlock the pipe buffer for our remaining processing.
550 			 * We will either break out with an error or we will
551 			 * sleep and relock to loop.
552 			 */
553 			pipeunlock(rpipe);
554 
555 			/*
556 			 * The PIPE_DIRECTR flag is not under the control
557 			 * of the long-term lock (see pipe_direct_write()),
558 			 * so re-check now while holding the spin lock.
559 			 */
560 			if ((rpipe->pipe_state & PIPE_DIRECTR) != 0)
561 				goto again;
562 
563 			/*
564 			 * We want to read more, wake up select/poll.
565 			 */
566 			pipeselwakeup(rpipe, rpipe->pipe_peer, fp->f_data,
567 			    POLL_IN);
568 
569 			/*
570 			 * If the "write-side" is blocked, wake it up now.
571 			 */
572 			if (rpipe->pipe_state & PIPE_WANTW) {
573 				rpipe->pipe_state &= ~PIPE_WANTW;
574 				wakeup(rpipe);
575 			}
576 
577 			/* Now wait until the pipe is filled */
578 			rpipe->pipe_state |= PIPE_WANTR;
579 			error = ltsleep(rpipe, PRIBIO | PCATCH,
580 					"piperd", 0, &rpipe->pipe_slock);
581 			if (error != 0)
582 				goto unlocked_error;
583 			goto again;
584 		}
585 	}
586 
587 	if (error == 0)
588 		PIPE_TIMESTAMP(&rpipe->pipe_atime);
589 
590 	PIPE_LOCK(rpipe);
591 	pipeunlock(rpipe);
592 
593 unlocked_error:
594 	--rpipe->pipe_busy;
595 
596 	/*
597 	 * PIPE_WANTCLOSE processing only makes sense if pipe_busy is 0.
598 	 */
599 	if ((rpipe->pipe_busy == 0) && (rpipe->pipe_state & PIPE_WANTCLOSE)) {
600 		rpipe->pipe_state &= ~(PIPE_WANTCLOSE|PIPE_WANTW);
601 		wakeup(rpipe);
602 	} else if (bp->cnt < MINPIPESIZE) {
603 		/*
604 		 * Handle write blocking hysteresis.
605 		 */
606 		if (rpipe->pipe_state & PIPE_WANTW) {
607 			rpipe->pipe_state &= ~PIPE_WANTW;
608 			wakeup(rpipe);
609 		}
610 	}
611 
612 	/*
613 	 * If anything was read off the buffer, signal to the writer it's
614 	 * possible to write more data. Also send signal if we are here for the
615 	 * first time after last write.
616 	 */
617 	if ((bp->size - bp->cnt) >= PIPE_BUF
618 	    && (ocnt != bp->cnt || (rpipe->pipe_state & PIPE_SIGNALR))) {
619 		pipeselwakeup(rpipe, rpipe->pipe_peer, fp->f_data, POLL_OUT);
620 		rpipe->pipe_state &= ~PIPE_SIGNALR;
621 	}
622 
623 	PIPE_UNLOCK(rpipe);
624 	return (error);
625 }
626 
627 #ifndef PIPE_NODIRECT
628 /*
629  * Allocate structure for loan transfer.
630  */
631 static int
632 pipe_loan_alloc(wpipe, npages)
633 	struct pipe *wpipe;
634 	int npages;
635 {
636 	vsize_t len;
637 
638 	len = (vsize_t)npages << PAGE_SHIFT;
639 	wpipe->pipe_map.kva = uvm_km_valloc_wait(kernel_map, len);
640 	if (wpipe->pipe_map.kva == 0)
641 		return (ENOMEM);
642 
643 	amountpipekva += len;
644 	wpipe->pipe_map.npages = npages;
645 	wpipe->pipe_map.pgs = malloc(npages * sizeof(struct vm_page *), M_PIPE,
646 	    M_WAITOK);
647 	return (0);
648 }
649 
650 /*
651  * Free resources allocated for loan transfer.
652  */
653 static void
654 pipe_loan_free(wpipe)
655 	struct pipe *wpipe;
656 {
657 	vsize_t len;
658 
659 	len = (vsize_t)wpipe->pipe_map.npages << PAGE_SHIFT;
660 	uvm_km_free(kernel_map, wpipe->pipe_map.kva, len);
661 	wpipe->pipe_map.kva = 0;
662 	amountpipekva -= len;
663 	free(wpipe->pipe_map.pgs, M_PIPE);
664 	wpipe->pipe_map.pgs = NULL;
665 }
666 
667 /*
668  * NetBSD direct write, using uvm_loan() mechanism.
669  * This implements the pipe buffer write mechanism.  Note that only
670  * a direct write OR a normal pipe write can be pending at any given time.
671  * If there are any characters in the pipe buffer, the direct write will
672  * be deferred until the receiving process grabs all of the bytes from
673  * the pipe buffer.  Then the direct mapping write is set-up.
674  *
675  * Called with the long-term pipe lock held.
676  */
677 static int
678 pipe_direct_write(fp, wpipe, uio)
679 	struct file *fp;
680 	struct pipe *wpipe;
681 	struct uio *uio;
682 {
683 	int error, npages, j;
684 	struct vm_page **pgs;
685 	vaddr_t bbase, kva, base, bend;
686 	vsize_t blen, bcnt;
687 	voff_t bpos;
688 
689 	KASSERT(wpipe->pipe_map.cnt == 0);
690 
691 	/*
692 	 * Handle first PIPE_CHUNK_SIZE bytes of buffer. Deal with buffers
693 	 * not aligned to PAGE_SIZE.
694 	 */
695 	bbase = (vaddr_t)uio->uio_iov->iov_base;
696 	base = trunc_page(bbase);
697 	bend = round_page(bbase + uio->uio_iov->iov_len);
698 	blen = bend - base;
699 	bpos = bbase - base;
700 
701 	if (blen > PIPE_DIRECT_CHUNK) {
702 		blen = PIPE_DIRECT_CHUNK;
703 		bend = base + blen;
704 		bcnt = PIPE_DIRECT_CHUNK - bpos;
705 	} else {
706 		bcnt = uio->uio_iov->iov_len;
707 	}
708 	npages = blen >> PAGE_SHIFT;
709 
710 	/*
711 	 * Free the old kva if we need more pages than we have
712 	 * allocated.
713 	 */
714 	if (wpipe->pipe_map.kva != 0 && npages > wpipe->pipe_map.npages)
715 		pipe_loan_free(wpipe);
716 
717 	/* Allocate new kva. */
718 	if (wpipe->pipe_map.kva == 0) {
719 		error = pipe_loan_alloc(wpipe, npages);
720 		if (error)
721 			return (error);
722 	}
723 
724 	/* Loan the write buffer memory from writer process */
725 	pgs = wpipe->pipe_map.pgs;
726 	error = uvm_loan(&uio->uio_procp->p_vmspace->vm_map, base, blen,
727 			 pgs, UVM_LOAN_TOPAGE);
728 	if (error) {
729 		pipe_loan_free(wpipe);
730 		return (error);
731 	}
732 
733 	/* Enter the loaned pages to kva */
734 	kva = wpipe->pipe_map.kva;
735 	for (j = 0; j < npages; j++, kva += PAGE_SIZE) {
736 		pmap_kenter_pa(kva, VM_PAGE_TO_PHYS(pgs[j]), VM_PROT_READ);
737 	}
738 	pmap_update(pmap_kernel());
739 
740 	/* Now we can put the pipe in direct write mode */
741 	wpipe->pipe_map.pos = bpos;
742 	wpipe->pipe_map.cnt = bcnt;
743 	wpipe->pipe_state |= PIPE_DIRECTW;
744 
745 	/*
746 	 * But before we can let someone do a direct read,
747 	 * we have to wait until the pipe is drained.
748 	 */
749 
750 	/* Relase the pipe lock while we wait */
751 	PIPE_LOCK(wpipe);
752 	pipeunlock(wpipe);
753 
754 	while (error == 0 && wpipe->pipe_buffer.cnt > 0) {
755 		if (wpipe->pipe_state & PIPE_WANTR) {
756 			wpipe->pipe_state &= ~PIPE_WANTR;
757 			wakeup(wpipe);
758 		}
759 
760 		wpipe->pipe_state |= PIPE_WANTW;
761 		error = ltsleep(wpipe, PRIBIO | PCATCH, "pipdwc", 0,
762 				&wpipe->pipe_slock);
763 		if (error == 0 && wpipe->pipe_state & PIPE_EOF)
764 			error = EPIPE;
765 	}
766 
767 	/* Pipe is drained; next read will off the direct buffer */
768 	wpipe->pipe_state |= PIPE_DIRECTR;
769 
770 	/* Wait until the reader is done */
771 	while (error == 0 && (wpipe->pipe_state & PIPE_DIRECTR)) {
772 		if (wpipe->pipe_state & PIPE_WANTR) {
773 			wpipe->pipe_state &= ~PIPE_WANTR;
774 			wakeup(wpipe);
775 		}
776 		pipeselwakeup(wpipe, wpipe, fp->f_data, POLL_IN);
777 		error = ltsleep(wpipe, PRIBIO | PCATCH, "pipdwt", 0,
778 				&wpipe->pipe_slock);
779 		if (error == 0 && wpipe->pipe_state & PIPE_EOF)
780 			error = EPIPE;
781 	}
782 
783 	/* Take pipe out of direct write mode */
784 	wpipe->pipe_state &= ~(PIPE_DIRECTW | PIPE_DIRECTR);
785 
786 	/* Acquire the pipe lock and cleanup */
787 	(void)pipelock(wpipe, 0);
788 	if (pgs != NULL) {
789 		pmap_kremove(wpipe->pipe_map.kva, blen);
790 		uvm_unloan(pgs, npages, UVM_LOAN_TOPAGE);
791 	}
792 	if (error || amountpipekva > maxpipekva)
793 		pipe_loan_free(wpipe);
794 
795 	if (error) {
796 		pipeselwakeup(wpipe, wpipe, fp->f_data, POLL_ERR);
797 
798 		/*
799 		 * If nothing was read from what we offered, return error
800 		 * straight on. Otherwise update uio resid first. Caller
801 		 * will deal with the error condition, returning short
802 		 * write, error, or restarting the write(2) as appropriate.
803 		 */
804 		if (wpipe->pipe_map.cnt == bcnt) {
805 			wpipe->pipe_map.cnt = 0;
806 			wakeup(wpipe);
807 			return (error);
808 		}
809 
810 		bcnt -= wpipe->pipe_map.cnt;
811 	}
812 
813 	uio->uio_resid -= bcnt;
814 	/* uio_offset not updated, not set/used for write(2) */
815 	uio->uio_iov->iov_base = (char *)uio->uio_iov->iov_base + bcnt;
816 	uio->uio_iov->iov_len -= bcnt;
817 	if (uio->uio_iov->iov_len == 0) {
818 		uio->uio_iov++;
819 		uio->uio_iovcnt--;
820 	}
821 
822 	wpipe->pipe_map.cnt = 0;
823 	return (error);
824 }
825 #endif /* !PIPE_NODIRECT */
826 
827 static int
828 pipe_write(fp, offset, uio, cred, flags)
829 	struct file *fp;
830 	off_t *offset;
831 	struct uio *uio;
832 	struct ucred *cred;
833 	int flags;
834 {
835 	struct pipe *wpipe, *rpipe;
836 	struct pipebuf *bp;
837 	int error;
838 
839 	/* We want to write to our peer */
840 	rpipe = (struct pipe *) fp->f_data;
841 
842 retry:
843 	error = 0;
844 	PIPE_LOCK(rpipe);
845 	wpipe = rpipe->pipe_peer;
846 
847 	/*
848 	 * Detect loss of pipe read side, issue SIGPIPE if lost.
849 	 */
850 	if (wpipe == NULL)
851 		error = EPIPE;
852 	else if (simple_lock_try(&wpipe->pipe_slock) == 0) {
853 		/* Deal with race for peer */
854 		PIPE_UNLOCK(rpipe);
855 		goto retry;
856 	} else if ((wpipe->pipe_state & PIPE_EOF) != 0) {
857 		PIPE_UNLOCK(wpipe);
858 		error = EPIPE;
859 	}
860 
861 	PIPE_UNLOCK(rpipe);
862 	if (error != 0)
863 		return (error);
864 
865 	++wpipe->pipe_busy;
866 
867 	/* Aquire the long-term pipe lock */
868 	if ((error = pipelock(wpipe,1)) != 0) {
869 		--wpipe->pipe_busy;
870 		if (wpipe->pipe_busy == 0
871 		    && (wpipe->pipe_state & PIPE_WANTCLOSE)) {
872 			wpipe->pipe_state &= ~(PIPE_WANTCLOSE | PIPE_WANTR);
873 			wakeup(wpipe);
874 		}
875 		PIPE_UNLOCK(wpipe);
876 		return (error);
877 	}
878 
879 	bp = &wpipe->pipe_buffer;
880 
881 	/*
882 	 * If it is advantageous to resize the pipe buffer, do so.
883 	 */
884 	if ((uio->uio_resid > PIPE_SIZE) &&
885 	    (nbigpipe < maxbigpipes) &&
886 #ifndef PIPE_NODIRECT
887 	    (wpipe->pipe_state & PIPE_DIRECTW) == 0 &&
888 #endif
889 	    (bp->size <= PIPE_SIZE) && (bp->cnt == 0)) {
890 
891 		if (pipespace(wpipe, BIG_PIPE_SIZE) == 0)
892 			nbigpipe++;
893 	}
894 
895 	while (uio->uio_resid) {
896 		size_t space;
897 
898 #ifndef PIPE_NODIRECT
899 		/*
900 		 * Pipe buffered writes cannot be coincidental with
901 		 * direct writes.  Also, only one direct write can be
902 		 * in progress at any one time.  We wait until the currently
903 		 * executing direct write is completed before continuing.
904 		 *
905 		 * We break out if a signal occurs or the reader goes away.
906 		 */
907 		while (error == 0 && wpipe->pipe_state & PIPE_DIRECTW) {
908 			PIPE_LOCK(wpipe);
909 			if (wpipe->pipe_state & PIPE_WANTR) {
910 				wpipe->pipe_state &= ~PIPE_WANTR;
911 				wakeup(wpipe);
912 			}
913 			pipeunlock(wpipe);
914 			error = ltsleep(wpipe, PRIBIO | PCATCH,
915 					"pipbww", 0, &wpipe->pipe_slock);
916 
917 			(void)pipelock(wpipe, 0);
918 			if (wpipe->pipe_state & PIPE_EOF)
919 				error = EPIPE;
920 		}
921 		if (error)
922 			break;
923 
924 		/*
925 		 * If the transfer is large, we can gain performance if
926 		 * we do process-to-process copies directly.
927 		 * If the write is non-blocking, we don't use the
928 		 * direct write mechanism.
929 		 *
930 		 * The direct write mechanism will detect the reader going
931 		 * away on us.
932 		 */
933 		if ((uio->uio_iov->iov_len >= PIPE_MINDIRECT) &&
934 		    (fp->f_flag & FNONBLOCK) == 0 &&
935 		    (wpipe->pipe_map.kva || (amountpipekva < limitpipekva))) {
936 			error = pipe_direct_write(fp, wpipe, uio);
937 
938 			/*
939 			 * Break out if error occurred, unless it's ENOMEM.
940 			 * ENOMEM means we failed to allocate some resources
941 			 * for direct write, so we just fallback to ordinary
942 			 * write. If the direct write was successful,
943 			 * process rest of data via ordinary write.
944 			 */
945 			if (error == 0)
946 				continue;
947 
948 			if (error != ENOMEM)
949 				break;
950 		}
951 #endif /* PIPE_NODIRECT */
952 
953 		space = bp->size - bp->cnt;
954 
955 		/* Writes of size <= PIPE_BUF must be atomic. */
956 		if ((space < uio->uio_resid) && (uio->uio_resid <= PIPE_BUF))
957 			space = 0;
958 
959 		if (space > 0) {
960 			int size;	/* Transfer size */
961 			int segsize;	/* first segment to transfer */
962 
963 			/*
964 			 * Transfer size is minimum of uio transfer
965 			 * and free space in pipe buffer.
966 			 */
967 			if (space > uio->uio_resid)
968 				size = uio->uio_resid;
969 			else
970 				size = space;
971 			/*
972 			 * First segment to transfer is minimum of
973 			 * transfer size and contiguous space in
974 			 * pipe buffer.  If first segment to transfer
975 			 * is less than the transfer size, we've got
976 			 * a wraparound in the buffer.
977 			 */
978 			segsize = bp->size - bp->in;
979 			if (segsize > size)
980 				segsize = size;
981 
982 			/* Transfer first segment */
983 			error = uiomove(&bp->buffer[bp->in], segsize, uio);
984 
985 			if (error == 0 && segsize < size) {
986 				/*
987 				 * Transfer remaining part now, to
988 				 * support atomic writes.  Wraparound
989 				 * happened.
990 				 */
991 #ifdef DEBUG
992 				if (bp->in + segsize != bp->size)
993 					panic("Expected pipe buffer wraparound disappeared");
994 #endif
995 
996 				error = uiomove(&bp->buffer[0],
997 						size - segsize, uio);
998 			}
999 			if (error)
1000 				break;
1001 
1002 			bp->in += size;
1003 			if (bp->in >= bp->size) {
1004 #ifdef DEBUG
1005 				if (bp->in != size - segsize + bp->size)
1006 					panic("Expected wraparound bad");
1007 #endif
1008 				bp->in = size - segsize;
1009 			}
1010 
1011 			bp->cnt += size;
1012 #ifdef DEBUG
1013 			if (bp->cnt > bp->size)
1014 				panic("Pipe buffer overflow");
1015 #endif
1016 		} else {
1017 			/*
1018 			 * If the "read-side" has been blocked, wake it up now.
1019 			 */
1020 			PIPE_LOCK(wpipe);
1021 			if (wpipe->pipe_state & PIPE_WANTR) {
1022 				wpipe->pipe_state &= ~PIPE_WANTR;
1023 				wakeup(wpipe);
1024 			}
1025 			PIPE_UNLOCK(wpipe);
1026 
1027 			/*
1028 			 * don't block on non-blocking I/O
1029 			 */
1030 			if (fp->f_flag & FNONBLOCK) {
1031 				error = EAGAIN;
1032 				break;
1033 			}
1034 
1035 			/*
1036 			 * We have no more space and have something to offer,
1037 			 * wake up select/poll.
1038 			 */
1039 			if (bp->cnt)
1040 				pipeselwakeup(wpipe, wpipe, fp->f_data,
1041 				    POLL_OUT);
1042 
1043 			PIPE_LOCK(wpipe);
1044 			pipeunlock(wpipe);
1045 			wpipe->pipe_state |= PIPE_WANTW;
1046 			error = ltsleep(wpipe, PRIBIO | PCATCH, "pipewr", 0,
1047 					&wpipe->pipe_slock);
1048 			(void)pipelock(wpipe, 0);
1049 			if (error != 0)
1050 				break;
1051 			/*
1052 			 * If read side wants to go away, we just issue a signal
1053 			 * to ourselves.
1054 			 */
1055 			if (wpipe->pipe_state & PIPE_EOF) {
1056 				error = EPIPE;
1057 				break;
1058 			}
1059 		}
1060 	}
1061 
1062 	PIPE_LOCK(wpipe);
1063 	--wpipe->pipe_busy;
1064 	if ((wpipe->pipe_busy == 0) && (wpipe->pipe_state & PIPE_WANTCLOSE)) {
1065 		wpipe->pipe_state &= ~(PIPE_WANTCLOSE | PIPE_WANTR);
1066 		wakeup(wpipe);
1067 	} else if (bp->cnt > 0) {
1068 		/*
1069 		 * If we have put any characters in the buffer, we wake up
1070 		 * the reader.
1071 		 */
1072 		if (wpipe->pipe_state & PIPE_WANTR) {
1073 			wpipe->pipe_state &= ~PIPE_WANTR;
1074 			wakeup(wpipe);
1075 		}
1076 	}
1077 
1078 	/*
1079 	 * Don't return EPIPE if I/O was successful
1080 	 */
1081 	if (error == EPIPE && bp->cnt == 0 && uio->uio_resid == 0)
1082 		error = 0;
1083 
1084 	if (error == 0)
1085 		PIPE_TIMESTAMP(&wpipe->pipe_mtime);
1086 
1087 	/*
1088 	 * We have something to offer, wake up select/poll.
1089 	 * wpipe->pipe_map.cnt is always 0 in this point (direct write
1090 	 * is only done synchronously), so check only wpipe->pipe_buffer.cnt
1091 	 */
1092 	if (bp->cnt)
1093 		pipeselwakeup(wpipe, wpipe, fp->f_data, POLL_OUT);
1094 
1095 	/*
1096 	 * Arrange for next read(2) to do a signal.
1097 	 */
1098 	wpipe->pipe_state |= PIPE_SIGNALR;
1099 
1100 	pipeunlock(wpipe);
1101 	PIPE_UNLOCK(wpipe);
1102 	return (error);
1103 }
1104 
1105 /*
1106  * we implement a very minimal set of ioctls for compatibility with sockets.
1107  */
1108 int
1109 pipe_ioctl(fp, cmd, data, p)
1110 	struct file *fp;
1111 	u_long cmd;
1112 	void *data;
1113 	struct proc *p;
1114 {
1115 	struct pipe *pipe = (struct pipe *)fp->f_data;
1116 
1117 	switch (cmd) {
1118 
1119 	case FIONBIO:
1120 		return (0);
1121 
1122 	case FIOASYNC:
1123 		PIPE_LOCK(pipe);
1124 		if (*(int *)data) {
1125 			pipe->pipe_state |= PIPE_ASYNC;
1126 		} else {
1127 			pipe->pipe_state &= ~PIPE_ASYNC;
1128 		}
1129 		PIPE_UNLOCK(pipe);
1130 		return (0);
1131 
1132 	case FIONREAD:
1133 		PIPE_LOCK(pipe);
1134 #ifndef PIPE_NODIRECT
1135 		if (pipe->pipe_state & PIPE_DIRECTW)
1136 			*(int *)data = pipe->pipe_map.cnt;
1137 		else
1138 #endif
1139 			*(int *)data = pipe->pipe_buffer.cnt;
1140 		PIPE_UNLOCK(pipe);
1141 		return (0);
1142 
1143 	case TIOCSPGRP:
1144 	case FIOSETOWN:
1145 		return fsetown(p, &pipe->pipe_pgid, cmd, data);
1146 
1147 	case TIOCGPGRP:
1148 	case FIOGETOWN:
1149 		return fgetown(p, pipe->pipe_pgid, cmd, data);
1150 
1151 	}
1152 	return (EPASSTHROUGH);
1153 }
1154 
1155 int
1156 pipe_poll(fp, events, td)
1157 	struct file *fp;
1158 	int events;
1159 	struct proc *td;
1160 {
1161 	struct pipe *rpipe = (struct pipe *)fp->f_data;
1162 	struct pipe *wpipe;
1163 	int eof = 0;
1164 	int revents = 0;
1165 
1166 retry:
1167 	PIPE_LOCK(rpipe);
1168 	wpipe = rpipe->pipe_peer;
1169 	if (wpipe != NULL && simple_lock_try(&wpipe->pipe_slock) == 0) {
1170 		/* Deal with race for peer */
1171 		PIPE_UNLOCK(rpipe);
1172 		goto retry;
1173 	}
1174 
1175 	if (events & (POLLIN | POLLRDNORM))
1176 		if ((rpipe->pipe_buffer.cnt > 0) ||
1177 #ifndef PIPE_NODIRECT
1178 		    (rpipe->pipe_state & PIPE_DIRECTR) ||
1179 #endif
1180 		    (rpipe->pipe_state & PIPE_EOF))
1181 			revents |= events & (POLLIN | POLLRDNORM);
1182 
1183 	eof |= (rpipe->pipe_state & PIPE_EOF);
1184 	PIPE_UNLOCK(rpipe);
1185 
1186 	if (wpipe == NULL)
1187 		revents |= events & (POLLOUT | POLLWRNORM);
1188 	else {
1189 		if (events & (POLLOUT | POLLWRNORM))
1190 			if ((wpipe->pipe_state & PIPE_EOF) || (
1191 #ifndef PIPE_NODIRECT
1192 			     (wpipe->pipe_state & PIPE_DIRECTW) == 0 &&
1193 #endif
1194 			     (wpipe->pipe_buffer.size - wpipe->pipe_buffer.cnt) >= PIPE_BUF))
1195 				revents |= events & (POLLOUT | POLLWRNORM);
1196 
1197 		eof |= (wpipe->pipe_state & PIPE_EOF);
1198 		PIPE_UNLOCK(wpipe);
1199 	}
1200 
1201 	if (wpipe == NULL || eof)
1202 		revents |= POLLHUP;
1203 
1204 	if (revents == 0) {
1205 		if (events & (POLLIN | POLLRDNORM))
1206 			selrecord(td, &rpipe->pipe_sel);
1207 
1208 		if (events & (POLLOUT | POLLWRNORM))
1209 			selrecord(td, &wpipe->pipe_sel);
1210 	}
1211 
1212 	return (revents);
1213 }
1214 
1215 static int
1216 pipe_stat(fp, ub, td)
1217 	struct file *fp;
1218 	struct stat *ub;
1219 	struct proc *td;
1220 {
1221 	struct pipe *pipe = (struct pipe *)fp->f_data;
1222 
1223 	memset((caddr_t)ub, 0, sizeof(*ub));
1224 	ub->st_mode = S_IFIFO | S_IRUSR | S_IWUSR;
1225 	ub->st_blksize = pipe->pipe_buffer.size;
1226 	ub->st_size = pipe->pipe_buffer.cnt;
1227 	ub->st_blocks = (ub->st_size) ? 1 : 0;
1228 	TIMEVAL_TO_TIMESPEC(&pipe->pipe_atime, &ub->st_atimespec)
1229 	TIMEVAL_TO_TIMESPEC(&pipe->pipe_mtime, &ub->st_mtimespec);
1230 	TIMEVAL_TO_TIMESPEC(&pipe->pipe_ctime, &ub->st_ctimespec);
1231 	ub->st_uid = fp->f_cred->cr_uid;
1232 	ub->st_gid = fp->f_cred->cr_gid;
1233 	/*
1234 	 * Left as 0: st_dev, st_ino, st_nlink, st_rdev, st_flags, st_gen.
1235 	 * XXX (st_dev, st_ino) should be unique.
1236 	 */
1237 	return (0);
1238 }
1239 
1240 /* ARGSUSED */
1241 static int
1242 pipe_close(fp, td)
1243 	struct file *fp;
1244 	struct proc *td;
1245 {
1246 	struct pipe *pipe = (struct pipe *)fp->f_data;
1247 
1248 	fp->f_data = NULL;
1249 	pipeclose(fp, pipe);
1250 	return (0);
1251 }
1252 
1253 static void
1254 pipe_free_kmem(pipe)
1255 	struct pipe *pipe;
1256 {
1257 
1258 	if (pipe->pipe_buffer.buffer != NULL) {
1259 		if (pipe->pipe_buffer.size > PIPE_SIZE)
1260 			--nbigpipe;
1261 		amountpipekva -= pipe->pipe_buffer.size;
1262 		uvm_km_free(kernel_map,
1263 			(vaddr_t)pipe->pipe_buffer.buffer,
1264 			pipe->pipe_buffer.size);
1265 		pipe->pipe_buffer.buffer = NULL;
1266 	}
1267 #ifndef PIPE_NODIRECT
1268 	if (pipe->pipe_map.kva != 0) {
1269 		pipe_loan_free(pipe);
1270 		pipe->pipe_map.cnt = 0;
1271 		pipe->pipe_map.kva = 0;
1272 		pipe->pipe_map.pos = 0;
1273 		pipe->pipe_map.npages = 0;
1274 	}
1275 #endif /* !PIPE_NODIRECT */
1276 }
1277 
1278 /*
1279  * shutdown the pipe
1280  */
1281 static void
1282 pipeclose(fp, pipe)
1283 	struct file *fp;
1284 	struct pipe *pipe;
1285 {
1286 	struct pipe *ppipe;
1287 
1288 	if (pipe == NULL)
1289 		return;
1290 
1291 retry:
1292 	PIPE_LOCK(pipe);
1293 
1294 	if (fp)
1295 		pipeselwakeup(pipe, pipe, fp->f_data, POLL_HUP);
1296 
1297 	/*
1298 	 * If the other side is blocked, wake it up saying that
1299 	 * we want to close it down.
1300 	 */
1301 	while (pipe->pipe_busy) {
1302 		wakeup(pipe);
1303 		pipe->pipe_state |= PIPE_WANTCLOSE | PIPE_EOF;
1304 		ltsleep(pipe, PRIBIO, "pipecl", 0, &pipe->pipe_slock);
1305 	}
1306 
1307 	/*
1308 	 * Disconnect from peer
1309 	 */
1310 	if ((ppipe = pipe->pipe_peer) != NULL) {
1311 		/* Deal with race for peer */
1312 		if (simple_lock_try(&ppipe->pipe_slock) == 0) {
1313 			PIPE_UNLOCK(pipe);
1314 			goto retry;
1315 		}
1316 		if (fp)
1317 			pipeselwakeup(ppipe, ppipe, fp->f_data, POLL_HUP);
1318 
1319 		ppipe->pipe_state |= PIPE_EOF;
1320 		wakeup(ppipe);
1321 		ppipe->pipe_peer = NULL;
1322 		PIPE_UNLOCK(ppipe);
1323 	}
1324 
1325 	(void)lockmgr(&pipe->pipe_lock, LK_DRAIN | LK_INTERLOCK,
1326 			&pipe->pipe_slock);
1327 
1328 	/*
1329 	 * free resources
1330 	 */
1331 	pipe_free_kmem(pipe);
1332 	pool_put(&pipe_pool, pipe);
1333 }
1334 
1335 static void
1336 filt_pipedetach(struct knote *kn)
1337 {
1338 	struct pipe *pipe = (struct pipe *)kn->kn_fp->f_data;
1339 
1340 	switch(kn->kn_filter) {
1341 	case EVFILT_WRITE:
1342 		/* need the peer structure, not our own */
1343 		pipe = pipe->pipe_peer;
1344 		/* XXXSMP: race for peer */
1345 
1346 		/* if reader end already closed, just return */
1347 		if (pipe == NULL)
1348 			return;
1349 
1350 		break;
1351 	default:
1352 		/* nothing to do */
1353 		break;
1354 	}
1355 
1356 #ifdef DIAGNOSTIC
1357 	if (kn->kn_hook != pipe)
1358 		panic("filt_pipedetach: inconsistent knote");
1359 #endif
1360 
1361 	PIPE_LOCK(pipe);
1362 	SLIST_REMOVE(&pipe->pipe_sel.sel_klist, kn, knote, kn_selnext);
1363 	PIPE_UNLOCK(pipe);
1364 }
1365 
1366 /*ARGSUSED*/
1367 static int
1368 filt_piperead(struct knote *kn, long hint)
1369 {
1370 	struct pipe *rpipe = (struct pipe *)kn->kn_fp->f_data;
1371 	struct pipe *wpipe = rpipe->pipe_peer;
1372 
1373 	if ((hint & NOTE_SUBMIT) == 0)
1374 		PIPE_LOCK(rpipe);
1375 	kn->kn_data = rpipe->pipe_buffer.cnt;
1376 	if ((kn->kn_data == 0) && (rpipe->pipe_state & PIPE_DIRECTW))
1377 		kn->kn_data = rpipe->pipe_map.cnt;
1378 
1379 	/* XXXSMP: race for peer */
1380 	if ((rpipe->pipe_state & PIPE_EOF) ||
1381 	    (wpipe == NULL) || (wpipe->pipe_state & PIPE_EOF)) {
1382 		kn->kn_flags |= EV_EOF;
1383 		if ((hint & NOTE_SUBMIT) == 0)
1384 			PIPE_UNLOCK(rpipe);
1385 		return (1);
1386 	}
1387 	if ((hint & NOTE_SUBMIT) == 0)
1388 		PIPE_UNLOCK(rpipe);
1389 	return (kn->kn_data > 0);
1390 }
1391 
1392 /*ARGSUSED*/
1393 static int
1394 filt_pipewrite(struct knote *kn, long hint)
1395 {
1396 	struct pipe *rpipe = (struct pipe *)kn->kn_fp->f_data;
1397 	struct pipe *wpipe = rpipe->pipe_peer;
1398 
1399 	if ((hint & NOTE_SUBMIT) == 0)
1400 		PIPE_LOCK(rpipe);
1401 	/* XXXSMP: race for peer */
1402 	if ((wpipe == NULL) || (wpipe->pipe_state & PIPE_EOF)) {
1403 		kn->kn_data = 0;
1404 		kn->kn_flags |= EV_EOF;
1405 		if ((hint & NOTE_SUBMIT) == 0)
1406 			PIPE_UNLOCK(rpipe);
1407 		return (1);
1408 	}
1409 	kn->kn_data = wpipe->pipe_buffer.size - wpipe->pipe_buffer.cnt;
1410 	if (wpipe->pipe_state & PIPE_DIRECTW)
1411 		kn->kn_data = 0;
1412 
1413 	if ((hint & NOTE_SUBMIT) == 0)
1414 		PIPE_UNLOCK(rpipe);
1415 	return (kn->kn_data >= PIPE_BUF);
1416 }
1417 
1418 static const struct filterops pipe_rfiltops =
1419 	{ 1, NULL, filt_pipedetach, filt_piperead };
1420 static const struct filterops pipe_wfiltops =
1421 	{ 1, NULL, filt_pipedetach, filt_pipewrite };
1422 
1423 /*ARGSUSED*/
1424 static int
1425 pipe_kqfilter(struct file *fp, struct knote *kn)
1426 {
1427 	struct pipe *pipe;
1428 
1429 	pipe = (struct pipe *)kn->kn_fp->f_data;
1430 	switch (kn->kn_filter) {
1431 	case EVFILT_READ:
1432 		kn->kn_fop = &pipe_rfiltops;
1433 		break;
1434 	case EVFILT_WRITE:
1435 		kn->kn_fop = &pipe_wfiltops;
1436 		/* XXXSMP: race for peer */
1437 		pipe = pipe->pipe_peer;
1438 		if (pipe == NULL) {
1439 			/* other end of pipe has been closed */
1440 			return (EBADF);
1441 		}
1442 		break;
1443 	default:
1444 		return (1);
1445 	}
1446 	kn->kn_hook = pipe;
1447 
1448 	PIPE_LOCK(pipe);
1449 	SLIST_INSERT_HEAD(&pipe->pipe_sel.sel_klist, kn, kn_selnext);
1450 	PIPE_UNLOCK(pipe);
1451 	return (0);
1452 }
1453 
1454 static int
1455 pipe_fcntl(fp, cmd, data, p)
1456 	struct file *fp;
1457 	u_int cmd;
1458 	void *data;
1459 	struct proc *p;
1460 {
1461 	if (cmd == F_SETFL)
1462 		return (0);
1463 	else
1464 		return (EOPNOTSUPP);
1465 }
1466 
1467 /*
1468  * Handle pipe sysctls.
1469  */
1470 SYSCTL_SETUP(sysctl_kern_pipe_setup, "sysctl kern.pipe subtree setup")
1471 {
1472 
1473 	sysctl_createv(clog, 0, NULL, NULL,
1474 		       CTLFLAG_PERMANENT,
1475 		       CTLTYPE_NODE, "kern", NULL,
1476 		       NULL, 0, NULL, 0,
1477 		       CTL_KERN, CTL_EOL);
1478 	sysctl_createv(clog, 0, NULL, NULL,
1479 		       CTLFLAG_PERMANENT,
1480 		       CTLTYPE_NODE, "pipe", NULL,
1481 		       NULL, 0, NULL, 0,
1482 		       CTL_KERN, KERN_PIPE, CTL_EOL);
1483 
1484 	sysctl_createv(clog, 0, NULL, NULL,
1485 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1486 		       CTLTYPE_INT, "maxkvasz", NULL,
1487 		       NULL, 0, &maxpipekva, 0,
1488 		       CTL_KERN, KERN_PIPE, KERN_PIPE_MAXKVASZ, CTL_EOL);
1489 	sysctl_createv(clog, 0, NULL, NULL,
1490 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1491 		       CTLTYPE_INT, "maxloankvasz", NULL,
1492 		       NULL, 0, &limitpipekva, 0,
1493 		       CTL_KERN, KERN_PIPE, KERN_PIPE_LIMITKVA, CTL_EOL);
1494 	sysctl_createv(clog, 0, NULL, NULL,
1495 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1496 		       CTLTYPE_INT, "maxbigpipes", NULL,
1497 		       NULL, 0, &maxbigpipes, 0,
1498 		       CTL_KERN, KERN_PIPE, KERN_PIPE_MAXBIGPIPES, CTL_EOL);
1499 	sysctl_createv(clog, 0, NULL, NULL,
1500 		       CTLFLAG_PERMANENT,
1501 		       CTLTYPE_INT, "nbigpipes", NULL,
1502 		       NULL, 0, &nbigpipe, 0,
1503 		       CTL_KERN, KERN_PIPE, KERN_PIPE_NBIGPIPES, CTL_EOL);
1504 	sysctl_createv(clog, 0, NULL, NULL,
1505 		       CTLFLAG_PERMANENT,
1506 		       CTLTYPE_INT, "kvasize", NULL,
1507 		       NULL, 0, &amountpipekva, 0,
1508 		       CTL_KERN, KERN_PIPE, KERN_PIPE_KVASIZE, CTL_EOL);
1509 }
1510 
1511 /*
1512  * Initialize pipe structs.
1513  */
1514 void
1515 pipe_init(void)
1516 {
1517 
1518 	pool_init(&pipe_pool, sizeof(struct pipe), 0, 0, 0, "pipepl",
1519 	    &pool_allocator_nointr);
1520 }
1521