xref: /openbsd-src/sys/kern/sys_pipe.c (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1 /*	$OpenBSD: sys_pipe.c,v 1.37 2001/07/05 07:15:07 art Exp $	*/
2 
3 /*
4  * Copyright (c) 1996 John S. Dyson
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice immediately at the beginning of the file, without modification,
12  *    this list of conditions, and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Absolutely no warranty of function or purpose is made by the author
17  *    John S. Dyson.
18  * 4. Modifications may be freely made to this file if the above conditions
19  *    are met.
20  */
21 
22 /*
23  * This file contains a high-performance replacement for the socket-based
24  * pipes scheme originally used in FreeBSD/4.4Lite.  It does not support
25  * all features of sockets, but does do everything that pipes normally
26  * do.
27  */
28 
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/proc.h>
32 #include <sys/file.h>
33 #include <sys/protosw.h>
34 #include <sys/stat.h>
35 #include <sys/filedesc.h>
36 #include <sys/malloc.h>
37 #include <sys/pool.h>
38 #include <sys/ioctl.h>
39 #include <sys/stat.h>
40 #include <sys/select.h>
41 #include <sys/signalvar.h>
42 #include <sys/errno.h>
43 #include <sys/queue.h>
44 #include <sys/kernel.h>
45 #include <sys/mount.h>
46 #include <sys/syscallargs.h>
47 #include <sys/event.h>
48 #include <sys/lock.h>
49 
50 #include <vm/vm.h>
51 #include <vm/vm_kern.h>
52 
53 #include <uvm/uvm_extern.h>
54 
55 #include <sys/pipe.h>
56 
57 /*
58  * interfaces to the outside world
59  */
60 int	pipe_read __P((struct file *, off_t *, struct uio *, struct ucred *));
61 int	pipe_write __P((struct file *, off_t *, struct uio *, struct ucred *));
62 int	pipe_close __P((struct file *, struct proc *));
63 int	pipe_select __P((struct file *, int which, struct proc *));
64 int	pipe_kqfilter __P((struct file *fp, struct knote *kn));
65 int	pipe_ioctl __P((struct file *, u_long, caddr_t, struct proc *));
66 int	pipe_stat __P((struct file *fp, struct stat *ub, struct proc *p));
67 
68 static struct fileops pipeops = {
69 	pipe_read, pipe_write, pipe_ioctl, pipe_select, pipe_kqfilter,
70 	pipe_stat, pipe_close
71 };
72 
73 void	filt_pipedetach(struct knote *kn);
74 int	filt_piperead(struct knote *kn, long hint);
75 int	filt_pipewrite(struct knote *kn, long hint);
76 
77 struct filterops pipe_rfiltops =
78 	{ 1, NULL, filt_pipedetach, filt_piperead };
79 struct filterops pipe_wfiltops =
80 	{ 1, NULL, filt_pipedetach, filt_pipewrite };
81 
82 /*
83  * Default pipe buffer size(s), this can be kind-of large now because pipe
84  * space is pageable.  The pipe code will try to maintain locality of
85  * reference for performance reasons, so small amounts of outstanding I/O
86  * will not wipe the cache.
87  */
88 #define MINPIPESIZE (PIPE_SIZE/3)
89 
90 /*
91  * Limit the number of "big" pipes
92  */
93 #define LIMITBIGPIPES	32
94 int nbigpipe;
95 static int amountpipekva;
96 
97 struct pool pipe_pool;
98 
99 void	pipeclose __P((struct pipe *));
100 void	pipeinit __P((struct pipe *));
101 static __inline int pipelock __P((struct pipe *));
102 static __inline void pipeunlock __P((struct pipe *));
103 static __inline void pipeselwakeup __P((struct pipe *));
104 void	pipespace __P((struct pipe *));
105 
106 /*
107  * The pipe system call for the DTYPE_PIPE type of pipes
108  */
109 
110 /* ARGSUSED */
111 int
112 sys_opipe(p, v, retval)
113 	struct proc *p;
114 	void *v;
115 	register_t *retval;
116 {
117 	struct filedesc *fdp = p->p_fd;
118 	struct file *rf, *wf;
119 	struct pipe *rpipe, *wpipe;
120 	int fd, error;
121 
122 	rpipe = pool_get(&pipe_pool, PR_WAITOK);
123 	pipeinit(rpipe);
124 	wpipe = pool_get(&pipe_pool, PR_WAITOK);
125 	pipeinit(wpipe);
126 
127 	error = falloc(p, &rf, &fd);
128 	if (error)
129 		goto free2;
130 	rf->f_flag = FREAD | FWRITE;
131 	rf->f_type = DTYPE_PIPE;
132 	rf->f_ops = &pipeops;
133 	rf->f_data = (caddr_t)rpipe;
134 	retval[0] = fd;
135 
136 	error = falloc(p, &wf, &fd);
137 	if (error)
138 		goto free3;
139 	wf->f_flag = FREAD | FWRITE;
140 	wf->f_type = DTYPE_PIPE;
141 	wf->f_ops = &pipeops;
142 	wf->f_data = (caddr_t)wpipe;
143 	retval[1] = fd;
144 
145 	rpipe->pipe_peer = wpipe;
146 	wpipe->pipe_peer = rpipe;
147 
148 	return (0);
149 free3:
150 	ffree(rf);
151 	fdremove(fdp, retval[0]);
152 free2:
153 	(void)pipeclose(wpipe);
154 	(void)pipeclose(rpipe);
155 	return (error);
156 }
157 
158 /*
159  * Allocate kva for pipe circular buffer, the space is pageable
160  */
161 void
162 pipespace(cpipe)
163 	struct pipe *cpipe;
164 {
165 	cpipe->pipe_buffer.buffer = (caddr_t) uvm_km_valloc(kernel_map,
166 						cpipe->pipe_buffer.size);
167 	if (cpipe->pipe_buffer.buffer == NULL)
168 		panic("pipespace: out of kvm");
169 
170 	amountpipekva += cpipe->pipe_buffer.size;
171 }
172 
173 /*
174  * initialize and allocate VM and memory for pipe
175  */
176 void
177 pipeinit(cpipe)
178 	struct pipe *cpipe;
179 {
180 
181 	cpipe->pipe_buffer.in = 0;
182 	cpipe->pipe_buffer.out = 0;
183 	cpipe->pipe_buffer.cnt = 0;
184 	cpipe->pipe_buffer.size = PIPE_SIZE;
185 
186 	/* Buffer kva gets dynamically allocated */
187 	cpipe->pipe_buffer.buffer = NULL;
188 	/* cpipe->pipe_buffer.object = invalid */
189 
190 	cpipe->pipe_state = 0;
191 	cpipe->pipe_peer = NULL;
192 	cpipe->pipe_busy = 0;
193 	microtime(&cpipe->pipe_ctime);
194 	cpipe->pipe_atime = cpipe->pipe_ctime;
195 	cpipe->pipe_mtime = cpipe->pipe_ctime;
196 	bzero(&cpipe->pipe_sel, sizeof cpipe->pipe_sel);
197 	cpipe->pipe_pgid = NO_PID;
198 }
199 
200 
201 /*
202  * lock a pipe for I/O, blocking other access
203  */
204 static __inline int
205 pipelock(cpipe)
206 	struct pipe *cpipe;
207 {
208 	int error;
209 	while (cpipe->pipe_state & PIPE_LOCK) {
210 		cpipe->pipe_state |= PIPE_LWANT;
211 		if ((error = tsleep(cpipe, PRIBIO|PCATCH, "pipelk", 0)))
212 			return error;
213 	}
214 	cpipe->pipe_state |= PIPE_LOCK;
215 	return 0;
216 }
217 
218 /*
219  * unlock a pipe I/O lock
220  */
221 static __inline void
222 pipeunlock(cpipe)
223 	struct pipe *cpipe;
224 {
225 	cpipe->pipe_state &= ~PIPE_LOCK;
226 	if (cpipe->pipe_state & PIPE_LWANT) {
227 		cpipe->pipe_state &= ~PIPE_LWANT;
228 		wakeup(cpipe);
229 	}
230 }
231 
232 static __inline void
233 pipeselwakeup(cpipe)
234 	struct pipe *cpipe;
235 {
236 	if (cpipe->pipe_state & PIPE_SEL) {
237 		cpipe->pipe_state &= ~PIPE_SEL;
238 		selwakeup(&cpipe->pipe_sel);
239 	}
240 	if ((cpipe->pipe_state & PIPE_ASYNC) && cpipe->pipe_pgid != NO_PID)
241 		gsignal(cpipe->pipe_pgid, SIGIO);
242 	KNOTE(&cpipe->pipe_sel.si_note, 0);
243 }
244 
245 /* ARGSUSED */
246 int
247 pipe_read(fp, poff, uio, cred)
248 	struct file *fp;
249 	off_t *poff;
250 	struct uio *uio;
251 	struct ucred *cred;
252 {
253 	struct pipe *rpipe = (struct pipe *) fp->f_data;
254 	int error;
255 	int nread = 0;
256 	int size;
257 
258 	error = pipelock(rpipe);
259 	if (error)
260 		goto unlocked_error;
261 
262 	++rpipe->pipe_busy;
263 
264 	while (uio->uio_resid) {
265 		/*
266 		 * normal pipe buffer receive
267 		 */
268 		if (rpipe->pipe_buffer.cnt > 0) {
269 			size = rpipe->pipe_buffer.size - rpipe->pipe_buffer.out;
270 			if (size > rpipe->pipe_buffer.cnt)
271 				size = rpipe->pipe_buffer.cnt;
272 			if (size > uio->uio_resid)
273 				size = uio->uio_resid;
274 			error = uiomove(&rpipe->pipe_buffer.buffer[rpipe->pipe_buffer.out],
275 					size, uio);
276 			if (error) {
277 				break;
278 			}
279 			rpipe->pipe_buffer.out += size;
280 			if (rpipe->pipe_buffer.out >= rpipe->pipe_buffer.size)
281 				rpipe->pipe_buffer.out = 0;
282 
283 			rpipe->pipe_buffer.cnt -= size;
284 
285 			/*
286 			 * If there is no more to read in the pipe, reset
287 			 * its pointers to the beginning.  This improves
288 			 * cache hit stats.
289 			 */
290 			if (rpipe->pipe_buffer.cnt == 0) {
291 				rpipe->pipe_buffer.in = 0;
292 				rpipe->pipe_buffer.out = 0;
293 			}
294 			nread += size;
295 		} else {
296 			/*
297 			 * detect EOF condition
298 			 */
299 			if (rpipe->pipe_state & PIPE_EOF) {
300 				/* XXX error = ? */
301 				break;
302 			}
303 
304 			/*
305 			 * If the "write-side" has been blocked, wake it up now.
306 			 */
307 			if (rpipe->pipe_state & PIPE_WANTW) {
308 				rpipe->pipe_state &= ~PIPE_WANTW;
309 				wakeup(rpipe);
310 			}
311 
312 			/*
313 			 * Break if some data was read.
314 			 */
315 			if (nread > 0)
316 				break;
317 
318 			/*
319 			 * Unlock the pipe buffer for our remaining processing.
320 			 * We will either break out with an error or we will
321 			 * sleep and relock to loop.
322 			 */
323 			pipeunlock(rpipe);
324 
325 			/*
326 			 * Handle non-blocking mode operation or
327 			 * wait for more data.
328 			 */
329 			if (fp->f_flag & FNONBLOCK)
330 				error = EAGAIN;
331 			else {
332 				rpipe->pipe_state |= PIPE_WANTR;
333 				if ((error = tsleep(rpipe, PRIBIO|PCATCH, "piperd", 0)) == 0)
334 					error = pipelock(rpipe);
335 			}
336 			if (error)
337 				goto unlocked_error;
338 		}
339 	}
340 	pipeunlock(rpipe);
341 
342 	if (error == 0)
343 		microtime(&rpipe->pipe_atime);
344 unlocked_error:
345 	--rpipe->pipe_busy;
346 
347 	/*
348 	 * PIPE_WANT processing only makes sense if pipe_busy is 0.
349 	 */
350 	if ((rpipe->pipe_busy == 0) && (rpipe->pipe_state & PIPE_WANT)) {
351 		rpipe->pipe_state &= ~(PIPE_WANT|PIPE_WANTW);
352 		wakeup(rpipe);
353 	} else if (rpipe->pipe_buffer.cnt < MINPIPESIZE) {
354 		/*
355 		 * Handle write blocking hysteresis.
356 		 */
357 		if (rpipe->pipe_state & PIPE_WANTW) {
358 			rpipe->pipe_state &= ~PIPE_WANTW;
359 			wakeup(rpipe);
360 		}
361 	}
362 
363 	if ((rpipe->pipe_buffer.size - rpipe->pipe_buffer.cnt) >= PIPE_BUF)
364 		pipeselwakeup(rpipe);
365 
366 	return error;
367 }
368 
369 int
370 pipe_write(fp, poff, uio, cred)
371 	struct file *fp;
372 	off_t *poff;
373 	struct uio *uio;
374 	struct ucred *cred;
375 {
376 	int error = 0;
377 	int orig_resid;
378 
379 	struct pipe *wpipe, *rpipe;
380 
381 	rpipe = (struct pipe *) fp->f_data;
382 	wpipe = rpipe->pipe_peer;
383 
384 	/*
385 	 * detect loss of pipe read side, issue SIGPIPE if lost.
386 	 */
387 	if ((wpipe == NULL) || (wpipe->pipe_state & PIPE_EOF)) {
388 		return EPIPE;
389 	}
390 
391 	/*
392 	 * If it is advantageous to resize the pipe buffer, do
393 	 * so.
394 	 */
395 	if ((uio->uio_resid > PIPE_SIZE) &&
396 	    (nbigpipe < LIMITBIGPIPES) &&
397 	    (wpipe->pipe_buffer.size <= PIPE_SIZE) &&
398 	    (wpipe->pipe_buffer.cnt == 0)) {
399 
400 		if (wpipe->pipe_buffer.buffer) {
401 			amountpipekva -= wpipe->pipe_buffer.size;
402 			uvm_km_free(kernel_map,
403 				(vaddr_t)wpipe->pipe_buffer.buffer,
404 				wpipe->pipe_buffer.size);
405 		}
406 
407 		wpipe->pipe_buffer.in = 0;
408 		wpipe->pipe_buffer.out = 0;
409 		wpipe->pipe_buffer.cnt = 0;
410 		wpipe->pipe_buffer.size = BIG_PIPE_SIZE;
411 		wpipe->pipe_buffer.buffer = NULL;
412 		++nbigpipe;
413 	}
414 
415 
416 	if (wpipe->pipe_buffer.buffer == NULL) {
417 		if ((error = pipelock(wpipe)) == 0) {
418 			pipespace(wpipe);
419 			pipeunlock(wpipe);
420 		} else {
421 			return error;
422 		}
423 	}
424 
425 	++wpipe->pipe_busy;
426 	orig_resid = uio->uio_resid;
427 
428 retrywrite:
429 	while (uio->uio_resid) {
430 		int space;
431 
432 		if (wpipe->pipe_state & PIPE_EOF) {
433 			error = EPIPE;
434 			break;
435 		}
436 
437 		space = wpipe->pipe_buffer.size - wpipe->pipe_buffer.cnt;
438 
439 		/* Writes of size <= PIPE_BUF must be atomic. */
440 		if ((space < uio->uio_resid) && (orig_resid <= PIPE_BUF))
441 			space = 0;
442 
443 		if (space > 0 &&
444 		    (wpipe->pipe_buffer.cnt < wpipe->pipe_buffer.size)) {
445 			if ((error = pipelock(wpipe)) == 0) {
446 				int size;	/* Transfer size */
447 				int segsize;	/* first segment to transfer */
448 
449 				/*
450 				 * If a process blocked in uiomove, our
451 				 * value for space might be bad.
452 				 *
453 				 * XXX will we be ok if the reader has gone
454 				 * away here?
455 				 */
456 				if (space > wpipe->pipe_buffer.size -
457 				    wpipe->pipe_buffer.cnt) {
458 					pipeunlock(wpipe);
459 					goto retrywrite;
460 				}
461 
462 				/*
463 				 * Transfer size is minimum of uio transfer
464 				 * and free space in pipe buffer.
465 				 */
466 				if (space > uio->uio_resid)
467 					size = uio->uio_resid;
468 				else
469 					size = space;
470 				/*
471 				 * First segment to transfer is minimum of
472 				 * transfer size and contiguous space in
473 				 * pipe buffer.  If first segment to transfer
474 				 * is less than the transfer size, we've got
475 				 * a wraparound in the buffer.
476 				 */
477 				segsize = wpipe->pipe_buffer.size -
478 					wpipe->pipe_buffer.in;
479 				if (segsize > size)
480 					segsize = size;
481 
482 				/* Transfer first segment */
483 
484 				error = uiomove(&wpipe->pipe_buffer.buffer[wpipe->pipe_buffer.in],
485 						segsize, uio);
486 
487 				if (error == 0 && segsize < size) {
488 					/*
489 					 * Transfer remaining part now, to
490 					 * support atomic writes.  Wraparound
491 					 * happened.
492 					 */
493 #ifdef DIAGNOSTIC
494 					if (wpipe->pipe_buffer.in + segsize !=
495 					    wpipe->pipe_buffer.size)
496 						panic("Expected pipe buffer wraparound disappeared");
497 #endif
498 
499 					error = uiomove(&wpipe->pipe_buffer.buffer[0],
500 							size - segsize, uio);
501 				}
502 				if (error == 0) {
503 					wpipe->pipe_buffer.in += size;
504 					if (wpipe->pipe_buffer.in >=
505 					    wpipe->pipe_buffer.size) {
506 #ifdef DIAGNOSTIC
507 						if (wpipe->pipe_buffer.in != size - segsize + wpipe->pipe_buffer.size)
508 							panic("Expected wraparound bad");
509 #endif
510 						wpipe->pipe_buffer.in = size - segsize;
511 					}
512 
513 					wpipe->pipe_buffer.cnt += size;
514 #ifdef DIAGNOSTIC
515 					if (wpipe->pipe_buffer.cnt > wpipe->pipe_buffer.size)
516 						panic("Pipe buffer overflow");
517 #endif
518 				}
519 				pipeunlock(wpipe);
520 			}
521 			if (error)
522 				break;
523 		} else {
524 			/*
525 			 * If the "read-side" has been blocked, wake it up now.
526 			 */
527 			if (wpipe->pipe_state & PIPE_WANTR) {
528 				wpipe->pipe_state &= ~PIPE_WANTR;
529 				wakeup(wpipe);
530 			}
531 
532 			/*
533 			 * don't block on non-blocking I/O
534 			 */
535 			if (fp->f_flag & FNONBLOCK) {
536 				error = EAGAIN;
537 				break;
538 			}
539 
540 			/*
541 			 * We have no more space and have something to offer,
542 			 * wake up selects.
543 			 */
544 			pipeselwakeup(wpipe);
545 
546 			wpipe->pipe_state |= PIPE_WANTW;
547 			error = tsleep(wpipe, (PRIBIO + 1)|PCATCH,
548 			    "pipewr", 0);
549 			if (error)
550 				break;
551 			/*
552 			 * If read side wants to go away, we just issue a
553 			 * signal to ourselves.
554 			 */
555 			if (wpipe->pipe_state & PIPE_EOF) {
556 				error = EPIPE;
557 				break;
558 			}
559 		}
560 	}
561 
562 	--wpipe->pipe_busy;
563 	if ((wpipe->pipe_busy == 0) &&
564 	    (wpipe->pipe_state & PIPE_WANT)) {
565 		wpipe->pipe_state &= ~(PIPE_WANT|PIPE_WANTR);
566 		wakeup(wpipe);
567 	} else if (wpipe->pipe_buffer.cnt > 0) {
568 		/*
569 		 * If we have put any characters in the buffer, we wake up
570 		 * the reader.
571 		 */
572 		if (wpipe->pipe_state & PIPE_WANTR) {
573 			wpipe->pipe_state &= ~PIPE_WANTR;
574 			wakeup(wpipe);
575 		}
576 	}
577 
578 	/*
579 	 * Don't return EPIPE if I/O was successful
580 	 */
581 	if ((wpipe->pipe_buffer.cnt == 0) &&
582 	    (uio->uio_resid == 0) &&
583 	    (error == EPIPE))
584 		error = 0;
585 
586 	if (error == 0)
587 		microtime(&wpipe->pipe_mtime);
588 	/*
589 	 * We have something to offer,
590 	 * wake up select.
591 	 */
592 	if (wpipe->pipe_buffer.cnt)
593 		pipeselwakeup(wpipe);
594 
595 	return error;
596 }
597 
598 /*
599  * we implement a very minimal set of ioctls for compatibility with sockets.
600  */
601 int
602 pipe_ioctl(fp, cmd, data, p)
603 	struct file *fp;
604 	u_long cmd;
605 	caddr_t data;
606 	struct proc *p;
607 {
608 	struct pipe *mpipe = (struct pipe *)fp->f_data;
609 
610 	switch (cmd) {
611 
612 	case FIONBIO:
613 		return (0);
614 
615 	case FIOASYNC:
616 		if (*(int *)data) {
617 			mpipe->pipe_state |= PIPE_ASYNC;
618 		} else {
619 			mpipe->pipe_state &= ~PIPE_ASYNC;
620 		}
621 		return (0);
622 
623 	case FIONREAD:
624 		*(int *)data = mpipe->pipe_buffer.cnt;
625 		return (0);
626 
627 	case SIOCSPGRP:
628 		mpipe->pipe_pgid = *(int *)data;
629 		return (0);
630 
631 	case SIOCGPGRP:
632 		*(int *)data = mpipe->pipe_pgid;
633 		return (0);
634 
635 	}
636 	return (ENOTTY);
637 }
638 
639 int
640 pipe_select(fp, which, p)
641 	struct file *fp;
642 	int which;
643 	struct proc *p;
644 {
645 	struct pipe *rpipe = (struct pipe *)fp->f_data;
646 	struct pipe *wpipe;
647 
648 	wpipe = rpipe->pipe_peer;
649 	switch (which) {
650 
651 	case FREAD:
652 		if ((rpipe->pipe_buffer.cnt > 0) ||
653 		    (rpipe->pipe_state & PIPE_EOF)) {
654 			return (1);
655 		}
656 		selrecord(p, &rpipe->pipe_sel);
657 		rpipe->pipe_state |= PIPE_SEL;
658 		break;
659 
660 	case FWRITE:
661 		if ((wpipe == NULL) ||
662 		    (wpipe->pipe_state & PIPE_EOF) ||
663 		    ((wpipe->pipe_buffer.size - wpipe->pipe_buffer.cnt) >= PIPE_BUF)) {
664 			return (1);
665 		}
666 		selrecord(p, &wpipe->pipe_sel);
667 		wpipe->pipe_state |= PIPE_SEL;
668 		break;
669 
670 	case 0:
671 		if ((rpipe->pipe_state & PIPE_EOF) ||
672 		    (wpipe == NULL) ||
673 		    (wpipe->pipe_state & PIPE_EOF)) {
674 			return (1);
675 		}
676 
677 		selrecord(p, &rpipe->pipe_sel);
678 		rpipe->pipe_state |= PIPE_SEL;
679 		break;
680 	}
681 	return (0);
682 }
683 
684 int
685 pipe_stat(fp, ub, p)
686 	struct file *fp;
687 	struct stat *ub;
688 	struct proc *p;
689 {
690 	struct pipe *pipe = (struct pipe *)fp->f_data;
691 
692 	bzero((caddr_t)ub, sizeof (*ub));
693 	ub->st_mode = S_IFIFO;
694 	ub->st_blksize = pipe->pipe_buffer.size;
695 	ub->st_size = pipe->pipe_buffer.cnt;
696 	ub->st_blocks = (ub->st_size + ub->st_blksize - 1) / ub->st_blksize;
697 	TIMEVAL_TO_TIMESPEC(&pipe->pipe_atime, &ub->st_atimespec);
698 	TIMEVAL_TO_TIMESPEC(&pipe->pipe_mtime, &ub->st_mtimespec);
699 	TIMEVAL_TO_TIMESPEC(&pipe->pipe_ctime, &ub->st_ctimespec);
700 	ub->st_uid = fp->f_cred->cr_uid;
701 	ub->st_gid = fp->f_cred->cr_gid;
702 	/*
703 	 * Left as 0: st_dev, st_ino, st_nlink, st_rdev, st_flags, st_gen.
704 	 * XXX (st_dev, st_ino) should be unique.
705 	 */
706 	return 0;
707 }
708 
709 /* ARGSUSED */
710 int
711 pipe_close(fp, p)
712 	struct file *fp;
713 	struct proc *p;
714 {
715 	struct pipe *cpipe = (struct pipe *)fp->f_data;
716 
717 	pipeclose(cpipe);
718 	fp->f_data = NULL;
719 	return 0;
720 }
721 
722 /*
723  * shutdown the pipe
724  */
725 void
726 pipeclose(cpipe)
727 	struct pipe *cpipe;
728 {
729 	struct pipe *ppipe;
730 	if (cpipe) {
731 
732 		pipeselwakeup(cpipe);
733 
734 		/*
735 		 * If the other side is blocked, wake it up saying that
736 		 * we want to close it down.
737 		 */
738 		while (cpipe->pipe_busy) {
739 			wakeup(cpipe);
740 			cpipe->pipe_state |= PIPE_WANT|PIPE_EOF;
741 			tsleep(cpipe, PRIBIO, "pipecl", 0);
742 		}
743 
744 		/*
745 		 * Disconnect from peer
746 		 */
747 		if ((ppipe = cpipe->pipe_peer) != NULL) {
748 			pipeselwakeup(ppipe);
749 
750 			ppipe->pipe_state |= PIPE_EOF;
751 			wakeup(ppipe);
752 			ppipe->pipe_peer = NULL;
753 		}
754 
755 		/*
756 		 * free resources
757 		 */
758 		if (cpipe->pipe_buffer.buffer) {
759 			if (cpipe->pipe_buffer.size > PIPE_SIZE)
760 				--nbigpipe;
761 			amountpipekva -= cpipe->pipe_buffer.size;
762 			uvm_km_free(kernel_map,
763 				(vaddr_t)cpipe->pipe_buffer.buffer,
764 				cpipe->pipe_buffer.size);
765 		}
766 		pool_put(&pipe_pool, cpipe);
767 	}
768 }
769 
770 int
771 pipe_kqfilter(struct file *fp, struct knote *kn)
772 {
773 	struct pipe *rpipe = (struct pipe *)kn->kn_fp->f_data;
774 	struct pipe *wpipe = rpipe->pipe_peer;
775 
776 	switch (kn->kn_filter) {
777 	case EVFILT_READ:
778 		kn->kn_fop = &pipe_rfiltops;
779 		SLIST_INSERT_HEAD(&rpipe->pipe_sel.si_note, kn, kn_selnext);
780 		break;
781 	case EVFILT_WRITE:
782 		if (wpipe == NULL)
783 			return (1);
784 		kn->kn_fop = &pipe_wfiltops;
785 		SLIST_INSERT_HEAD(&wpipe->pipe_sel.si_note, kn, kn_selnext);
786 		break;
787 	default:
788 		return (1);
789 	}
790 
791 	return (0);
792 }
793 
794 void
795 filt_pipedetach(struct knote *kn)
796 {
797 	struct pipe *rpipe = (struct pipe *)kn->kn_fp->f_data;
798 	struct pipe *wpipe = rpipe->pipe_peer;
799 
800 	switch (kn->kn_filter) {
801 	case EVFILT_READ:
802 		SLIST_REMOVE(&rpipe->pipe_sel.si_note, kn, knote, kn_selnext);
803 		break;
804 	case EVFILT_WRITE:
805 		if (wpipe == NULL)
806 			return;
807 		SLIST_REMOVE(&wpipe->pipe_sel.si_note, kn, knote, kn_selnext);
808 		break;
809 	}
810 }
811 
812 /*ARGSUSED*/
813 int
814 filt_piperead(struct knote *kn, long hint)
815 {
816 	struct pipe *rpipe = (struct pipe *)kn->kn_fp->f_data;
817 	struct pipe *wpipe = rpipe->pipe_peer;
818 
819 	kn->kn_data = rpipe->pipe_buffer.cnt;
820 
821 	if ((rpipe->pipe_state & PIPE_EOF) ||
822 	    (wpipe == NULL) || (wpipe->pipe_state & PIPE_EOF)) {
823 		kn->kn_flags |= EV_EOF;
824 		return (1);
825 	}
826 	return (kn->kn_data > 0);
827 }
828 
829 /*ARGSUSED*/
830 int
831 filt_pipewrite(struct knote *kn, long hint)
832 {
833 	struct pipe *rpipe = (struct pipe *)kn->kn_fp->f_data;
834 	struct pipe *wpipe = rpipe->pipe_peer;
835 
836 	if ((wpipe == NULL) || (wpipe->pipe_state & PIPE_EOF)) {
837 		kn->kn_data = 0;
838 		kn->kn_flags |= EV_EOF;
839 		return (1);
840 	}
841 	kn->kn_data = wpipe->pipe_buffer.size - wpipe->pipe_buffer.cnt;
842 
843 	return (kn->kn_data >= PIPE_BUF);
844 }
845 
846 void
847 pipe_init()
848 {
849 	pool_init(&pipe_pool, sizeof(struct pipe), 0, 0, 0, "pipepl",
850 		0, pool_page_alloc_nointr, pool_page_free_nointr,
851 		M_PIPE);
852 }
853 
854