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