xref: /freebsd/sys/kern/sys_pipe.c (revision 6af83ee0d2941d18880b6aaa2b4facd1d30c6106)
1 /*-
2  * Copyright (c) 1996 John S. Dyson
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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
15  *    John S. Dyson.
16  * 4. Modifications may be freely made to this file if the above conditions
17  *    are met.
18  */
19 
20 /*
21  * This file contains a high-performance replacement for the socket-based
22  * pipes scheme originally used in FreeBSD/4.4Lite.  It does not support
23  * all features of sockets, but does do everything that pipes normally
24  * do.
25  */
26 
27 /*
28  * This code has two modes of operation, a small write mode and a large
29  * write mode.  The small write mode acts like conventional pipes with
30  * a kernel buffer.  If the buffer is less than PIPE_MINDIRECT, then the
31  * "normal" pipe buffering is done.  If the buffer is between PIPE_MINDIRECT
32  * and PIPE_SIZE in size, it is fully mapped and wired into the kernel, and
33  * the receiving process can copy it directly from the pages in the sending
34  * process.
35  *
36  * If the sending process receives a signal, it is possible that it will
37  * go away, and certainly its address space can change, because control
38  * is returned back to the user-mode side.  In that case, the pipe code
39  * arranges to copy the buffer supplied by the user process, to a pageable
40  * kernel buffer, and the receiving process will grab the data from the
41  * pageable kernel buffer.  Since signals don't happen all that often,
42  * the copy operation is normally eliminated.
43  *
44  * The constant PIPE_MINDIRECT is chosen to make sure that buffering will
45  * happen for small transfers so that the system will not spend all of
46  * its time context switching.
47  *
48  * In order to limit the resource use of pipes, two sysctls exist:
49  *
50  * kern.ipc.maxpipekva - This is a hard limit on the amount of pageable
51  * address space available to us in pipe_map. This value is normally
52  * autotuned, but may also be loader tuned.
53  *
54  * kern.ipc.pipekva - This read-only sysctl tracks the current amount of
55  * memory in use by pipes.
56  *
57  * Based on how large pipekva is relative to maxpipekva, the following
58  * will happen:
59  *
60  * 0% - 50%:
61  *     New pipes are given 16K of memory backing, pipes may dynamically
62  *     grow to as large as 64K where needed.
63  * 50% - 75%:
64  *     New pipes are given 4K (or PAGE_SIZE) of memory backing,
65  *     existing pipes may NOT grow.
66  * 75% - 100%:
67  *     New pipes are given 4K (or PAGE_SIZE) of memory backing,
68  *     existing pipes will be shrunk down to 4K whenever possible.
69  *
70  * Resizing may be disabled by setting kern.ipc.piperesizeallowed=0.  If
71  * that is set,  the only resize that will occur is the 0 -> SMALL_PIPE_SIZE
72  * resize which MUST occur for reverse-direction pipes when they are
73  * first used.
74  *
75  * Additional information about the current state of pipes may be obtained
76  * from kern.ipc.pipes, kern.ipc.pipefragretry, kern.ipc.pipeallocfail,
77  * and kern.ipc.piperesizefail.
78  *
79  * Locking rules:  There are two locks present here:  A mutex, used via
80  * PIPE_LOCK, and a flag, used via pipelock().  All locking is done via
81  * the flag, as mutexes can not persist over uiomove.  The mutex
82  * exists only to guard access to the flag, and is not in itself a
83  * locking mechanism.  Also note that there is only a single mutex for
84  * both directions of a pipe.
85  *
86  * As pipelock() may have to sleep before it can acquire the flag, it
87  * is important to reread all data after a call to pipelock(); everything
88  * in the structure may have changed.
89  */
90 
91 #include <sys/cdefs.h>
92 __FBSDID("$FreeBSD$");
93 
94 #include "opt_mac.h"
95 
96 #include <sys/param.h>
97 #include <sys/systm.h>
98 #include <sys/fcntl.h>
99 #include <sys/file.h>
100 #include <sys/filedesc.h>
101 #include <sys/filio.h>
102 #include <sys/kernel.h>
103 #include <sys/lock.h>
104 #include <sys/mac.h>
105 #include <sys/mutex.h>
106 #include <sys/ttycom.h>
107 #include <sys/stat.h>
108 #include <sys/malloc.h>
109 #include <sys/poll.h>
110 #include <sys/selinfo.h>
111 #include <sys/signalvar.h>
112 #include <sys/sysctl.h>
113 #include <sys/sysproto.h>
114 #include <sys/pipe.h>
115 #include <sys/proc.h>
116 #include <sys/vnode.h>
117 #include <sys/uio.h>
118 #include <sys/event.h>
119 
120 #include <vm/vm.h>
121 #include <vm/vm_param.h>
122 #include <vm/vm_object.h>
123 #include <vm/vm_kern.h>
124 #include <vm/vm_extern.h>
125 #include <vm/pmap.h>
126 #include <vm/vm_map.h>
127 #include <vm/vm_page.h>
128 #include <vm/uma.h>
129 
130 /*
131  * Use this define if you want to disable *fancy* VM things.  Expect an
132  * approx 30% decrease in transfer rate.  This could be useful for
133  * NetBSD or OpenBSD.
134  */
135 /* #define PIPE_NODIRECT */
136 
137 /*
138  * interfaces to the outside world
139  */
140 static fo_rdwr_t	pipe_read;
141 static fo_rdwr_t	pipe_write;
142 static fo_ioctl_t	pipe_ioctl;
143 static fo_poll_t	pipe_poll;
144 static fo_kqfilter_t	pipe_kqfilter;
145 static fo_stat_t	pipe_stat;
146 static fo_close_t	pipe_close;
147 
148 static struct fileops pipeops = {
149 	.fo_read = pipe_read,
150 	.fo_write = pipe_write,
151 	.fo_ioctl = pipe_ioctl,
152 	.fo_poll = pipe_poll,
153 	.fo_kqfilter = pipe_kqfilter,
154 	.fo_stat = pipe_stat,
155 	.fo_close = pipe_close,
156 	.fo_flags = DFLAG_PASSABLE
157 };
158 
159 static void	filt_pipedetach(struct knote *kn);
160 static int	filt_piperead(struct knote *kn, long hint);
161 static int	filt_pipewrite(struct knote *kn, long hint);
162 
163 static struct filterops pipe_rfiltops =
164 	{ 1, NULL, filt_pipedetach, filt_piperead };
165 static struct filterops pipe_wfiltops =
166 	{ 1, NULL, filt_pipedetach, filt_pipewrite };
167 
168 /*
169  * Default pipe buffer size(s), this can be kind-of large now because pipe
170  * space is pageable.  The pipe code will try to maintain locality of
171  * reference for performance reasons, so small amounts of outstanding I/O
172  * will not wipe the cache.
173  */
174 #define MINPIPESIZE (PIPE_SIZE/3)
175 #define MAXPIPESIZE (2*PIPE_SIZE/3)
176 
177 static int amountpipes;
178 static int amountpipekva;
179 static int pipefragretry;
180 static int pipeallocfail;
181 static int piperesizefail;
182 static int piperesizeallowed = 1;
183 
184 SYSCTL_DECL(_kern_ipc);
185 
186 SYSCTL_INT(_kern_ipc, OID_AUTO, maxpipekva, CTLFLAG_RDTUN,
187 	   &maxpipekva, 0, "Pipe KVA limit");
188 SYSCTL_INT(_kern_ipc, OID_AUTO, pipes, CTLFLAG_RD,
189 	   &amountpipes, 0, "Current # of pipes");
190 SYSCTL_INT(_kern_ipc, OID_AUTO, pipekva, CTLFLAG_RD,
191 	   &amountpipekva, 0, "Pipe KVA usage");
192 SYSCTL_INT(_kern_ipc, OID_AUTO, pipefragretry, CTLFLAG_RD,
193 	  &pipefragretry, 0, "Pipe allocation retries due to fragmentation");
194 SYSCTL_INT(_kern_ipc, OID_AUTO, pipeallocfail, CTLFLAG_RD,
195 	  &pipeallocfail, 0, "Pipe allocation failures");
196 SYSCTL_INT(_kern_ipc, OID_AUTO, piperesizefail, CTLFLAG_RD,
197 	  &piperesizefail, 0, "Pipe resize failures");
198 SYSCTL_INT(_kern_ipc, OID_AUTO, piperesizeallowed, CTLFLAG_RW,
199 	  &piperesizeallowed, 0, "Pipe resizing allowed");
200 
201 static void pipeinit(void *dummy __unused);
202 static void pipeclose(struct pipe *cpipe);
203 static void pipe_free_kmem(struct pipe *cpipe);
204 static int pipe_create(struct pipe *pipe, int backing);
205 static __inline int pipelock(struct pipe *cpipe, int catch);
206 static __inline void pipeunlock(struct pipe *cpipe);
207 static __inline void pipeselwakeup(struct pipe *cpipe);
208 #ifndef PIPE_NODIRECT
209 static int pipe_build_write_buffer(struct pipe *wpipe, struct uio *uio);
210 static void pipe_destroy_write_buffer(struct pipe *wpipe);
211 static int pipe_direct_write(struct pipe *wpipe, struct uio *uio);
212 static void pipe_clone_write_buffer(struct pipe *wpipe);
213 #endif
214 static int pipespace(struct pipe *cpipe, int size);
215 static int pipespace_new(struct pipe *cpipe, int size);
216 
217 static int	pipe_zone_ctor(void *mem, int size, void *arg, int flags);
218 static void	pipe_zone_dtor(void *mem, int size, void *arg);
219 static int	pipe_zone_init(void *mem, int size, int flags);
220 static void	pipe_zone_fini(void *mem, int size);
221 
222 static uma_zone_t pipe_zone;
223 
224 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_ANY, pipeinit, NULL);
225 
226 static void
227 pipeinit(void *dummy __unused)
228 {
229 
230 	pipe_zone = uma_zcreate("PIPE", sizeof(struct pipepair),
231 	    pipe_zone_ctor, pipe_zone_dtor, pipe_zone_init, pipe_zone_fini,
232 	    UMA_ALIGN_PTR, 0);
233 	KASSERT(pipe_zone != NULL, ("pipe_zone not initialized"));
234 }
235 
236 static int
237 pipe_zone_ctor(void *mem, int size, void *arg, int flags)
238 {
239 	struct pipepair *pp;
240 	struct pipe *rpipe, *wpipe;
241 
242 	KASSERT(size == sizeof(*pp), ("pipe_zone_ctor: wrong size"));
243 
244 	pp = (struct pipepair *)mem;
245 
246 	/*
247 	 * We zero both pipe endpoints to make sure all the kmem pointers
248 	 * are NULL, flag fields are zero'd, etc.  We timestamp both
249 	 * endpoints with the same time.
250 	 */
251 	rpipe = &pp->pp_rpipe;
252 	bzero(rpipe, sizeof(*rpipe));
253 	vfs_timestamp(&rpipe->pipe_ctime);
254 	rpipe->pipe_atime = rpipe->pipe_mtime = rpipe->pipe_ctime;
255 
256 	wpipe = &pp->pp_wpipe;
257 	bzero(wpipe, sizeof(*wpipe));
258 	wpipe->pipe_ctime = rpipe->pipe_ctime;
259 	wpipe->pipe_atime = wpipe->pipe_mtime = rpipe->pipe_ctime;
260 
261 	rpipe->pipe_peer = wpipe;
262 	rpipe->pipe_pair = pp;
263 	wpipe->pipe_peer = rpipe;
264 	wpipe->pipe_pair = pp;
265 
266 	/*
267 	 * Mark both endpoints as present; they will later get free'd
268 	 * one at a time.  When both are free'd, then the whole pair
269 	 * is released.
270 	 */
271 	rpipe->pipe_present = 1;
272 	wpipe->pipe_present = 1;
273 
274 	/*
275 	 * Eventually, the MAC Framework may initialize the label
276 	 * in ctor or init, but for now we do it elswhere to avoid
277 	 * blocking in ctor or init.
278 	 */
279 	pp->pp_label = NULL;
280 
281 	atomic_add_int(&amountpipes, 2);
282 	return (0);
283 }
284 
285 static void
286 pipe_zone_dtor(void *mem, int size, void *arg)
287 {
288 	struct pipepair *pp;
289 
290 	KASSERT(size == sizeof(*pp), ("pipe_zone_dtor: wrong size"));
291 
292 	pp = (struct pipepair *)mem;
293 
294 	atomic_subtract_int(&amountpipes, 2);
295 }
296 
297 static int
298 pipe_zone_init(void *mem, int size, int flags)
299 {
300 	struct pipepair *pp;
301 
302 	KASSERT(size == sizeof(*pp), ("pipe_zone_init: wrong size"));
303 
304 	pp = (struct pipepair *)mem;
305 
306 	mtx_init(&pp->pp_mtx, "pipe mutex", NULL, MTX_DEF | MTX_RECURSE);
307 	return (0);
308 }
309 
310 static void
311 pipe_zone_fini(void *mem, int size)
312 {
313 	struct pipepair *pp;
314 
315 	KASSERT(size == sizeof(*pp), ("pipe_zone_fini: wrong size"));
316 
317 	pp = (struct pipepair *)mem;
318 
319 	mtx_destroy(&pp->pp_mtx);
320 }
321 
322 /*
323  * The pipe system call for the DTYPE_PIPE type of pipes.  If we fail,
324  * let the zone pick up the pieces via pipeclose().
325  */
326 
327 /* ARGSUSED */
328 int
329 pipe(td, uap)
330 	struct thread *td;
331 	struct pipe_args /* {
332 		int	dummy;
333 	} */ *uap;
334 {
335 	struct filedesc *fdp = td->td_proc->p_fd;
336 	struct file *rf, *wf;
337 	struct pipepair *pp;
338 	struct pipe *rpipe, *wpipe;
339 	int fd, error;
340 
341 	pp = uma_zalloc(pipe_zone, M_WAITOK);
342 #ifdef MAC
343 	/*
344 	 * The MAC label is shared between the connected endpoints.  As a
345 	 * result mac_init_pipe() and mac_create_pipe() are called once
346 	 * for the pair, and not on the endpoints.
347 	 */
348 	mac_init_pipe(pp);
349 	mac_create_pipe(td->td_ucred, pp);
350 #endif
351 	rpipe = &pp->pp_rpipe;
352 	wpipe = &pp->pp_wpipe;
353 
354 	knlist_init(&rpipe->pipe_sel.si_note, PIPE_MTX(rpipe));
355 	knlist_init(&wpipe->pipe_sel.si_note, PIPE_MTX(wpipe));
356 
357 	/* Only the forward direction pipe is backed by default */
358 	if (pipe_create(rpipe, 1) || pipe_create(wpipe, 0)) {
359 		pipeclose(rpipe);
360 		pipeclose(wpipe);
361 		return (ENFILE);
362 	}
363 
364 	rpipe->pipe_state |= PIPE_DIRECTOK;
365 	wpipe->pipe_state |= PIPE_DIRECTOK;
366 
367 	error = falloc(td, &rf, &fd);
368 	if (error) {
369 		pipeclose(rpipe);
370 		pipeclose(wpipe);
371 		return (error);
372 	}
373 	/* An extra reference on `rf' has been held for us by falloc(). */
374 	td->td_retval[0] = fd;
375 
376 	/*
377 	 * Warning: once we've gotten past allocation of the fd for the
378 	 * read-side, we can only drop the read side via fdrop() in order
379 	 * to avoid races against processes which manage to dup() the read
380 	 * side while we are blocked trying to allocate the write side.
381 	 */
382 	FILE_LOCK(rf);
383 	rf->f_flag = FREAD | FWRITE;
384 	rf->f_type = DTYPE_PIPE;
385 	rf->f_data = rpipe;
386 	rf->f_ops = &pipeops;
387 	FILE_UNLOCK(rf);
388 	error = falloc(td, &wf, &fd);
389 	if (error) {
390 		fdclose(fdp, rf, td->td_retval[0], td);
391 		fdrop(rf, td);
392 		/* rpipe has been closed by fdrop(). */
393 		pipeclose(wpipe);
394 		return (error);
395 	}
396 	/* An extra reference on `wf' has been held for us by falloc(). */
397 	FILE_LOCK(wf);
398 	wf->f_flag = FREAD | FWRITE;
399 	wf->f_type = DTYPE_PIPE;
400 	wf->f_data = wpipe;
401 	wf->f_ops = &pipeops;
402 	FILE_UNLOCK(wf);
403 	fdrop(wf, td);
404 	td->td_retval[1] = fd;
405 	fdrop(rf, td);
406 
407 	return (0);
408 }
409 
410 /*
411  * Allocate kva for pipe circular buffer, the space is pageable
412  * This routine will 'realloc' the size of a pipe safely, if it fails
413  * it will retain the old buffer.
414  * If it fails it will return ENOMEM.
415  */
416 static int
417 pipespace_new(cpipe, size)
418 	struct pipe *cpipe;
419 	int size;
420 {
421 	caddr_t buffer;
422 	int error, cnt, firstseg;
423 	static int curfail = 0;
424 	static struct timeval lastfail;
425 
426 	KASSERT(!mtx_owned(PIPE_MTX(cpipe)), ("pipespace: pipe mutex locked"));
427 	KASSERT(!(cpipe->pipe_state & PIPE_DIRECTW),
428 		("pipespace: resize of direct writes not allowed"));
429 retry:
430 	cnt = cpipe->pipe_buffer.cnt;
431 	if (cnt > size)
432 		size = cnt;
433 
434 	size = round_page(size);
435 	buffer = (caddr_t) vm_map_min(pipe_map);
436 
437 	error = vm_map_find(pipe_map, NULL, 0,
438 		(vm_offset_t *) &buffer, size, 1,
439 		VM_PROT_ALL, VM_PROT_ALL, 0);
440 	if (error != KERN_SUCCESS) {
441 		if ((cpipe->pipe_buffer.buffer == NULL) &&
442 			(size > SMALL_PIPE_SIZE)) {
443 			size = SMALL_PIPE_SIZE;
444 			pipefragretry++;
445 			goto retry;
446 		}
447 		if (cpipe->pipe_buffer.buffer == NULL) {
448 			pipeallocfail++;
449 			if (ppsratecheck(&lastfail, &curfail, 1))
450 				printf("kern.ipc.maxpipekva exceeded; see tuning(7)\n");
451 		} else {
452 			piperesizefail++;
453 		}
454 		return (ENOMEM);
455 	}
456 
457 	/* copy data, then free old resources if we're resizing */
458 	if (cnt > 0) {
459 		if (cpipe->pipe_buffer.in <= cpipe->pipe_buffer.out) {
460 			firstseg = cpipe->pipe_buffer.size - cpipe->pipe_buffer.out;
461 			bcopy(&cpipe->pipe_buffer.buffer[cpipe->pipe_buffer.out],
462 				buffer, firstseg);
463 			if ((cnt - firstseg) > 0)
464 				bcopy(cpipe->pipe_buffer.buffer, &buffer[firstseg],
465 					cpipe->pipe_buffer.in);
466 		} else {
467 			bcopy(&cpipe->pipe_buffer.buffer[cpipe->pipe_buffer.out],
468 				buffer, cnt);
469 		}
470 	}
471 	pipe_free_kmem(cpipe);
472 	cpipe->pipe_buffer.buffer = buffer;
473 	cpipe->pipe_buffer.size = size;
474 	cpipe->pipe_buffer.in = cnt;
475 	cpipe->pipe_buffer.out = 0;
476 	cpipe->pipe_buffer.cnt = cnt;
477 	atomic_add_int(&amountpipekva, cpipe->pipe_buffer.size);
478 	return (0);
479 }
480 
481 /*
482  * Wrapper for pipespace_new() that performs locking assertions.
483  */
484 static int
485 pipespace(cpipe, size)
486 	struct pipe *cpipe;
487 	int size;
488 {
489 
490 	KASSERT(cpipe->pipe_state & PIPE_LOCKFL,
491 		("Unlocked pipe passed to pipespace"));
492 	return (pipespace_new(cpipe, size));
493 }
494 
495 /*
496  * lock a pipe for I/O, blocking other access
497  */
498 static __inline int
499 pipelock(cpipe, catch)
500 	struct pipe *cpipe;
501 	int catch;
502 {
503 	int error;
504 
505 	PIPE_LOCK_ASSERT(cpipe, MA_OWNED);
506 	while (cpipe->pipe_state & PIPE_LOCKFL) {
507 		cpipe->pipe_state |= PIPE_LWANT;
508 		error = msleep(cpipe, PIPE_MTX(cpipe),
509 		    catch ? (PRIBIO | PCATCH) : PRIBIO,
510 		    "pipelk", 0);
511 		if (error != 0)
512 			return (error);
513 	}
514 	cpipe->pipe_state |= PIPE_LOCKFL;
515 	return (0);
516 }
517 
518 /*
519  * unlock a pipe I/O lock
520  */
521 static __inline void
522 pipeunlock(cpipe)
523 	struct pipe *cpipe;
524 {
525 
526 	PIPE_LOCK_ASSERT(cpipe, MA_OWNED);
527 	KASSERT(cpipe->pipe_state & PIPE_LOCKFL,
528 		("Unlocked pipe passed to pipeunlock"));
529 	cpipe->pipe_state &= ~PIPE_LOCKFL;
530 	if (cpipe->pipe_state & PIPE_LWANT) {
531 		cpipe->pipe_state &= ~PIPE_LWANT;
532 		wakeup(cpipe);
533 	}
534 }
535 
536 static __inline void
537 pipeselwakeup(cpipe)
538 	struct pipe *cpipe;
539 {
540 
541 	PIPE_LOCK_ASSERT(cpipe, MA_OWNED);
542 	if (cpipe->pipe_state & PIPE_SEL) {
543 		cpipe->pipe_state &= ~PIPE_SEL;
544 		selwakeuppri(&cpipe->pipe_sel, PSOCK);
545 	}
546 	if ((cpipe->pipe_state & PIPE_ASYNC) && cpipe->pipe_sigio)
547 		pgsigio(&cpipe->pipe_sigio, SIGIO, 0);
548 	KNOTE_LOCKED(&cpipe->pipe_sel.si_note, 0);
549 }
550 
551 /*
552  * Initialize and allocate VM and memory for pipe.  The structure
553  * will start out zero'd from the ctor, so we just manage the kmem.
554  */
555 static int
556 pipe_create(pipe, backing)
557 	struct pipe *pipe;
558 	int backing;
559 {
560 	int error;
561 
562 	if (backing) {
563 		if (amountpipekva > maxpipekva / 2)
564 			error = pipespace_new(pipe, SMALL_PIPE_SIZE);
565 		else
566 			error = pipespace_new(pipe, PIPE_SIZE);
567 	} else {
568 		/* If we're not backing this pipe, no need to do anything. */
569 		error = 0;
570 	}
571 	return (error);
572 }
573 
574 /* ARGSUSED */
575 static int
576 pipe_read(fp, uio, active_cred, flags, td)
577 	struct file *fp;
578 	struct uio *uio;
579 	struct ucred *active_cred;
580 	struct thread *td;
581 	int flags;
582 {
583 	struct pipe *rpipe = fp->f_data;
584 	int error;
585 	int nread = 0;
586 	u_int size;
587 
588 	PIPE_LOCK(rpipe);
589 	++rpipe->pipe_busy;
590 	error = pipelock(rpipe, 1);
591 	if (error)
592 		goto unlocked_error;
593 
594 #ifdef MAC
595 	error = mac_check_pipe_read(active_cred, rpipe->pipe_pair);
596 	if (error)
597 		goto locked_error;
598 #endif
599 	if (amountpipekva > (3 * maxpipekva) / 4) {
600 		if (!(rpipe->pipe_state & PIPE_DIRECTW) &&
601 			(rpipe->pipe_buffer.size > SMALL_PIPE_SIZE) &&
602 			(rpipe->pipe_buffer.cnt <= SMALL_PIPE_SIZE) &&
603 			(piperesizeallowed == 1)) {
604 			PIPE_UNLOCK(rpipe);
605 			pipespace(rpipe, SMALL_PIPE_SIZE);
606 			PIPE_LOCK(rpipe);
607 		}
608 	}
609 
610 	while (uio->uio_resid) {
611 		/*
612 		 * normal pipe buffer receive
613 		 */
614 		if (rpipe->pipe_buffer.cnt > 0) {
615 			size = rpipe->pipe_buffer.size - rpipe->pipe_buffer.out;
616 			if (size > rpipe->pipe_buffer.cnt)
617 				size = rpipe->pipe_buffer.cnt;
618 			if (size > (u_int) uio->uio_resid)
619 				size = (u_int) uio->uio_resid;
620 
621 			PIPE_UNLOCK(rpipe);
622 			error = uiomove(
623 			    &rpipe->pipe_buffer.buffer[rpipe->pipe_buffer.out],
624 			    size, uio);
625 			PIPE_LOCK(rpipe);
626 			if (error)
627 				break;
628 
629 			rpipe->pipe_buffer.out += size;
630 			if (rpipe->pipe_buffer.out >= rpipe->pipe_buffer.size)
631 				rpipe->pipe_buffer.out = 0;
632 
633 			rpipe->pipe_buffer.cnt -= size;
634 
635 			/*
636 			 * If there is no more to read in the pipe, reset
637 			 * its pointers to the beginning.  This improves
638 			 * cache hit stats.
639 			 */
640 			if (rpipe->pipe_buffer.cnt == 0) {
641 				rpipe->pipe_buffer.in = 0;
642 				rpipe->pipe_buffer.out = 0;
643 			}
644 			nread += size;
645 #ifndef PIPE_NODIRECT
646 		/*
647 		 * Direct copy, bypassing a kernel buffer.
648 		 */
649 		} else if ((size = rpipe->pipe_map.cnt) &&
650 			   (rpipe->pipe_state & PIPE_DIRECTW)) {
651 			if (size > (u_int) uio->uio_resid)
652 				size = (u_int) uio->uio_resid;
653 
654 			PIPE_UNLOCK(rpipe);
655 			error = uiomove_fromphys(rpipe->pipe_map.ms,
656 			    rpipe->pipe_map.pos, size, uio);
657 			PIPE_LOCK(rpipe);
658 			if (error)
659 				break;
660 			nread += size;
661 			rpipe->pipe_map.pos += size;
662 			rpipe->pipe_map.cnt -= size;
663 			if (rpipe->pipe_map.cnt == 0) {
664 				rpipe->pipe_state &= ~PIPE_DIRECTW;
665 				wakeup(rpipe);
666 			}
667 #endif
668 		} else {
669 			/*
670 			 * detect EOF condition
671 			 * read returns 0 on EOF, no need to set error
672 			 */
673 			if (rpipe->pipe_state & PIPE_EOF)
674 				break;
675 
676 			/*
677 			 * If the "write-side" has been blocked, wake it up now.
678 			 */
679 			if (rpipe->pipe_state & PIPE_WANTW) {
680 				rpipe->pipe_state &= ~PIPE_WANTW;
681 				wakeup(rpipe);
682 			}
683 
684 			/*
685 			 * Break if some data was read.
686 			 */
687 			if (nread > 0)
688 				break;
689 
690 			/*
691 			 * Unlock the pipe buffer for our remaining processing.
692 			 * We will either break out with an error or we will
693 			 * sleep and relock to loop.
694 			 */
695 			pipeunlock(rpipe);
696 
697 			/*
698 			 * Handle non-blocking mode operation or
699 			 * wait for more data.
700 			 */
701 			if (fp->f_flag & FNONBLOCK) {
702 				error = EAGAIN;
703 			} else {
704 				rpipe->pipe_state |= PIPE_WANTR;
705 				if ((error = msleep(rpipe, PIPE_MTX(rpipe),
706 				    PRIBIO | PCATCH,
707 				    "piperd", 0)) == 0)
708 					error = pipelock(rpipe, 1);
709 			}
710 			if (error)
711 				goto unlocked_error;
712 		}
713 	}
714 #ifdef MAC
715 locked_error:
716 #endif
717 	pipeunlock(rpipe);
718 
719 	/* XXX: should probably do this before getting any locks. */
720 	if (error == 0)
721 		vfs_timestamp(&rpipe->pipe_atime);
722 unlocked_error:
723 	--rpipe->pipe_busy;
724 
725 	/*
726 	 * PIPE_WANT processing only makes sense if pipe_busy is 0.
727 	 */
728 	if ((rpipe->pipe_busy == 0) && (rpipe->pipe_state & PIPE_WANT)) {
729 		rpipe->pipe_state &= ~(PIPE_WANT|PIPE_WANTW);
730 		wakeup(rpipe);
731 	} else if (rpipe->pipe_buffer.cnt < MINPIPESIZE) {
732 		/*
733 		 * Handle write blocking hysteresis.
734 		 */
735 		if (rpipe->pipe_state & PIPE_WANTW) {
736 			rpipe->pipe_state &= ~PIPE_WANTW;
737 			wakeup(rpipe);
738 		}
739 	}
740 
741 	if ((rpipe->pipe_buffer.size - rpipe->pipe_buffer.cnt) >= PIPE_BUF)
742 		pipeselwakeup(rpipe);
743 
744 	PIPE_UNLOCK(rpipe);
745 	return (error);
746 }
747 
748 #ifndef PIPE_NODIRECT
749 /*
750  * Map the sending processes' buffer into kernel space and wire it.
751  * This is similar to a physical write operation.
752  */
753 static int
754 pipe_build_write_buffer(wpipe, uio)
755 	struct pipe *wpipe;
756 	struct uio *uio;
757 {
758 	pmap_t pmap;
759 	u_int size;
760 	int i, j;
761 	vm_offset_t addr, endaddr;
762 
763 	PIPE_LOCK_ASSERT(wpipe, MA_NOTOWNED);
764 	KASSERT(wpipe->pipe_state & PIPE_DIRECTW,
765 		("Clone attempt on non-direct write pipe!"));
766 
767 	size = (u_int) uio->uio_iov->iov_len;
768 	if (size > wpipe->pipe_buffer.size)
769 		size = wpipe->pipe_buffer.size;
770 
771 	pmap = vmspace_pmap(curproc->p_vmspace);
772 	endaddr = round_page((vm_offset_t)uio->uio_iov->iov_base + size);
773 	addr = trunc_page((vm_offset_t)uio->uio_iov->iov_base);
774 	for (i = 0; addr < endaddr; addr += PAGE_SIZE, i++) {
775 		/*
776 		 * vm_fault_quick() can sleep.  Consequently,
777 		 * vm_page_lock_queue() and vm_page_unlock_queue()
778 		 * should not be performed outside of this loop.
779 		 */
780 	race:
781 		if (vm_fault_quick((caddr_t)addr, VM_PROT_READ) < 0) {
782 			vm_page_lock_queues();
783 			for (j = 0; j < i; j++)
784 				vm_page_unhold(wpipe->pipe_map.ms[j]);
785 			vm_page_unlock_queues();
786 			return (EFAULT);
787 		}
788 		wpipe->pipe_map.ms[i] = pmap_extract_and_hold(pmap, addr,
789 		    VM_PROT_READ);
790 		if (wpipe->pipe_map.ms[i] == NULL)
791 			goto race;
792 	}
793 
794 /*
795  * set up the control block
796  */
797 	wpipe->pipe_map.npages = i;
798 	wpipe->pipe_map.pos =
799 	    ((vm_offset_t) uio->uio_iov->iov_base) & PAGE_MASK;
800 	wpipe->pipe_map.cnt = size;
801 
802 /*
803  * and update the uio data
804  */
805 
806 	uio->uio_iov->iov_len -= size;
807 	uio->uio_iov->iov_base = (char *)uio->uio_iov->iov_base + size;
808 	if (uio->uio_iov->iov_len == 0)
809 		uio->uio_iov++;
810 	uio->uio_resid -= size;
811 	uio->uio_offset += size;
812 	return (0);
813 }
814 
815 /*
816  * unmap and unwire the process buffer
817  */
818 static void
819 pipe_destroy_write_buffer(wpipe)
820 	struct pipe *wpipe;
821 {
822 	int i;
823 
824 	PIPE_LOCK_ASSERT(wpipe, MA_OWNED);
825 	vm_page_lock_queues();
826 	for (i = 0; i < wpipe->pipe_map.npages; i++) {
827 		vm_page_unhold(wpipe->pipe_map.ms[i]);
828 	}
829 	vm_page_unlock_queues();
830 	wpipe->pipe_map.npages = 0;
831 }
832 
833 /*
834  * In the case of a signal, the writing process might go away.  This
835  * code copies the data into the circular buffer so that the source
836  * pages can be freed without loss of data.
837  */
838 static void
839 pipe_clone_write_buffer(wpipe)
840 	struct pipe *wpipe;
841 {
842 	struct uio uio;
843 	struct iovec iov;
844 	int size;
845 	int pos;
846 
847 	PIPE_LOCK_ASSERT(wpipe, MA_OWNED);
848 	size = wpipe->pipe_map.cnt;
849 	pos = wpipe->pipe_map.pos;
850 
851 	wpipe->pipe_buffer.in = size;
852 	wpipe->pipe_buffer.out = 0;
853 	wpipe->pipe_buffer.cnt = size;
854 	wpipe->pipe_state &= ~PIPE_DIRECTW;
855 
856 	PIPE_UNLOCK(wpipe);
857 	iov.iov_base = wpipe->pipe_buffer.buffer;
858 	iov.iov_len = size;
859 	uio.uio_iov = &iov;
860 	uio.uio_iovcnt = 1;
861 	uio.uio_offset = 0;
862 	uio.uio_resid = size;
863 	uio.uio_segflg = UIO_SYSSPACE;
864 	uio.uio_rw = UIO_READ;
865 	uio.uio_td = curthread;
866 	uiomove_fromphys(wpipe->pipe_map.ms, pos, size, &uio);
867 	PIPE_LOCK(wpipe);
868 	pipe_destroy_write_buffer(wpipe);
869 }
870 
871 /*
872  * This implements the pipe buffer write mechanism.  Note that only
873  * a direct write OR a normal pipe write can be pending at any given time.
874  * If there are any characters in the pipe buffer, the direct write will
875  * be deferred until the receiving process grabs all of the bytes from
876  * the pipe buffer.  Then the direct mapping write is set-up.
877  */
878 static int
879 pipe_direct_write(wpipe, uio)
880 	struct pipe *wpipe;
881 	struct uio *uio;
882 {
883 	int error;
884 
885 retry:
886 	PIPE_LOCK_ASSERT(wpipe, MA_OWNED);
887 	error = pipelock(wpipe, 1);
888 	if (wpipe->pipe_state & PIPE_EOF)
889 		error = EPIPE;
890 	if (error) {
891 		pipeunlock(wpipe);
892 		goto error1;
893 	}
894 	while (wpipe->pipe_state & PIPE_DIRECTW) {
895 		if (wpipe->pipe_state & PIPE_WANTR) {
896 			wpipe->pipe_state &= ~PIPE_WANTR;
897 			wakeup(wpipe);
898 		}
899 		wpipe->pipe_state |= PIPE_WANTW;
900 		pipeunlock(wpipe);
901 		error = msleep(wpipe, PIPE_MTX(wpipe),
902 		    PRIBIO | PCATCH, "pipdww", 0);
903 		if (error)
904 			goto error1;
905 		else
906 			goto retry;
907 	}
908 	wpipe->pipe_map.cnt = 0;	/* transfer not ready yet */
909 	if (wpipe->pipe_buffer.cnt > 0) {
910 		if (wpipe->pipe_state & PIPE_WANTR) {
911 			wpipe->pipe_state &= ~PIPE_WANTR;
912 			wakeup(wpipe);
913 		}
914 		wpipe->pipe_state |= PIPE_WANTW;
915 		pipeunlock(wpipe);
916 		error = msleep(wpipe, PIPE_MTX(wpipe),
917 		    PRIBIO | PCATCH, "pipdwc", 0);
918 		if (error)
919 			goto error1;
920 		else
921 			goto retry;
922 	}
923 
924 	wpipe->pipe_state |= PIPE_DIRECTW;
925 
926 	PIPE_UNLOCK(wpipe);
927 	error = pipe_build_write_buffer(wpipe, uio);
928 	PIPE_LOCK(wpipe);
929 	if (error) {
930 		wpipe->pipe_state &= ~PIPE_DIRECTW;
931 		pipeunlock(wpipe);
932 		goto error1;
933 	}
934 
935 	error = 0;
936 	while (!error && (wpipe->pipe_state & PIPE_DIRECTW)) {
937 		if (wpipe->pipe_state & PIPE_EOF) {
938 			pipe_destroy_write_buffer(wpipe);
939 			pipeselwakeup(wpipe);
940 			pipeunlock(wpipe);
941 			error = EPIPE;
942 			goto error1;
943 		}
944 		if (wpipe->pipe_state & PIPE_WANTR) {
945 			wpipe->pipe_state &= ~PIPE_WANTR;
946 			wakeup(wpipe);
947 		}
948 		pipeselwakeup(wpipe);
949 		pipeunlock(wpipe);
950 		error = msleep(wpipe, PIPE_MTX(wpipe), PRIBIO | PCATCH,
951 		    "pipdwt", 0);
952 		pipelock(wpipe, 0);
953 	}
954 
955 	if (wpipe->pipe_state & PIPE_EOF)
956 		error = EPIPE;
957 	if (wpipe->pipe_state & PIPE_DIRECTW) {
958 		/*
959 		 * this bit of trickery substitutes a kernel buffer for
960 		 * the process that might be going away.
961 		 */
962 		pipe_clone_write_buffer(wpipe);
963 	} else {
964 		pipe_destroy_write_buffer(wpipe);
965 	}
966 	pipeunlock(wpipe);
967 	return (error);
968 
969 error1:
970 	wakeup(wpipe);
971 	return (error);
972 }
973 #endif
974 
975 static int
976 pipe_write(fp, uio, active_cred, flags, td)
977 	struct file *fp;
978 	struct uio *uio;
979 	struct ucred *active_cred;
980 	struct thread *td;
981 	int flags;
982 {
983 	int error = 0;
984 	int desiredsize, orig_resid;
985 	struct pipe *wpipe, *rpipe;
986 
987 	rpipe = fp->f_data;
988 	wpipe = rpipe->pipe_peer;
989 
990 	PIPE_LOCK(rpipe);
991 	error = pipelock(wpipe, 1);
992 	if (error) {
993 		PIPE_UNLOCK(rpipe);
994 		return (error);
995 	}
996 	/*
997 	 * detect loss of pipe read side, issue SIGPIPE if lost.
998 	 */
999 	if ((!wpipe->pipe_present) || (wpipe->pipe_state & PIPE_EOF)) {
1000 		pipeunlock(wpipe);
1001 		PIPE_UNLOCK(rpipe);
1002 		return (EPIPE);
1003 	}
1004 #ifdef MAC
1005 	error = mac_check_pipe_write(active_cred, wpipe->pipe_pair);
1006 	if (error) {
1007 		pipeunlock(wpipe);
1008 		PIPE_UNLOCK(rpipe);
1009 		return (error);
1010 	}
1011 #endif
1012 	++wpipe->pipe_busy;
1013 
1014 	/* Choose a larger size if it's advantageous */
1015 	desiredsize = max(SMALL_PIPE_SIZE, wpipe->pipe_buffer.size);
1016 	while (desiredsize < wpipe->pipe_buffer.cnt + uio->uio_resid) {
1017 		if (piperesizeallowed != 1)
1018 			break;
1019 		if (amountpipekva > maxpipekva / 2)
1020 			break;
1021 		if (desiredsize == BIG_PIPE_SIZE)
1022 			break;
1023 		desiredsize = desiredsize * 2;
1024 	}
1025 
1026 	/* Choose a smaller size if we're in a OOM situation */
1027 	if ((amountpipekva > (3 * maxpipekva) / 4) &&
1028 		(wpipe->pipe_buffer.size > SMALL_PIPE_SIZE) &&
1029 		(wpipe->pipe_buffer.cnt <= SMALL_PIPE_SIZE) &&
1030 		(piperesizeallowed == 1))
1031 		desiredsize = SMALL_PIPE_SIZE;
1032 
1033 	/* Resize if the above determined that a new size was necessary */
1034 	if ((desiredsize != wpipe->pipe_buffer.size) &&
1035 		((wpipe->pipe_state & PIPE_DIRECTW) == 0)) {
1036 		PIPE_UNLOCK(wpipe);
1037 		pipespace(wpipe, desiredsize);
1038 		PIPE_LOCK(wpipe);
1039 	}
1040 	if (wpipe->pipe_buffer.size == 0) {
1041 		/*
1042 		 * This can only happen for reverse direction use of pipes
1043 		 * in a complete OOM situation.
1044 		 */
1045 		error = ENOMEM;
1046 		--wpipe->pipe_busy;
1047 		pipeunlock(wpipe);
1048 		PIPE_UNLOCK(wpipe);
1049 		return (error);
1050 	}
1051 
1052 	pipeunlock(wpipe);
1053 
1054 	orig_resid = uio->uio_resid;
1055 
1056 	while (uio->uio_resid) {
1057 		int space;
1058 
1059 		pipelock(wpipe, 0);
1060 		if (wpipe->pipe_state & PIPE_EOF) {
1061 			pipeunlock(wpipe);
1062 			error = EPIPE;
1063 			break;
1064 		}
1065 #ifndef PIPE_NODIRECT
1066 		/*
1067 		 * If the transfer is large, we can gain performance if
1068 		 * we do process-to-process copies directly.
1069 		 * If the write is non-blocking, we don't use the
1070 		 * direct write mechanism.
1071 		 *
1072 		 * The direct write mechanism will detect the reader going
1073 		 * away on us.
1074 		 */
1075 		if ((uio->uio_iov->iov_len >= PIPE_MINDIRECT) &&
1076 		    (wpipe->pipe_buffer.size >= PIPE_MINDIRECT) &&
1077 		    (fp->f_flag & FNONBLOCK) == 0) {
1078 			pipeunlock(wpipe);
1079 			error = pipe_direct_write(wpipe, uio);
1080 			if (error)
1081 				break;
1082 			continue;
1083 		}
1084 #endif
1085 
1086 		/*
1087 		 * Pipe buffered writes cannot be coincidental with
1088 		 * direct writes.  We wait until the currently executing
1089 		 * direct write is completed before we start filling the
1090 		 * pipe buffer.  We break out if a signal occurs or the
1091 		 * reader goes away.
1092 		 */
1093 		if (wpipe->pipe_state & PIPE_DIRECTW) {
1094 			if (wpipe->pipe_state & PIPE_WANTR) {
1095 				wpipe->pipe_state &= ~PIPE_WANTR;
1096 				wakeup(wpipe);
1097 			}
1098 			pipeunlock(wpipe);
1099 			error = msleep(wpipe, PIPE_MTX(rpipe), PRIBIO | PCATCH,
1100 			    "pipbww", 0);
1101 			if (error)
1102 				break;
1103 			else
1104 				continue;
1105 		}
1106 
1107 		space = wpipe->pipe_buffer.size - wpipe->pipe_buffer.cnt;
1108 
1109 		/* Writes of size <= PIPE_BUF must be atomic. */
1110 		if ((space < uio->uio_resid) && (orig_resid <= PIPE_BUF))
1111 			space = 0;
1112 
1113 		if (space > 0) {
1114 			int size;	/* Transfer size */
1115 			int segsize;	/* first segment to transfer */
1116 
1117 			/*
1118 			 * Transfer size is minimum of uio transfer
1119 			 * and free space in pipe buffer.
1120 			 */
1121 			if (space > uio->uio_resid)
1122 				size = uio->uio_resid;
1123 			else
1124 				size = space;
1125 			/*
1126 			 * First segment to transfer is minimum of
1127 			 * transfer size and contiguous space in
1128 			 * pipe buffer.  If first segment to transfer
1129 			 * is less than the transfer size, we've got
1130 			 * a wraparound in the buffer.
1131 			 */
1132 			segsize = wpipe->pipe_buffer.size -
1133 				wpipe->pipe_buffer.in;
1134 			if (segsize > size)
1135 				segsize = size;
1136 
1137 			/* Transfer first segment */
1138 
1139 			PIPE_UNLOCK(rpipe);
1140 			error = uiomove(&wpipe->pipe_buffer.buffer[wpipe->pipe_buffer.in],
1141 					segsize, uio);
1142 			PIPE_LOCK(rpipe);
1143 
1144 			if (error == 0 && segsize < size) {
1145 				KASSERT(wpipe->pipe_buffer.in + segsize ==
1146 					wpipe->pipe_buffer.size,
1147 					("Pipe buffer wraparound disappeared"));
1148 				/*
1149 				 * Transfer remaining part now, to
1150 				 * support atomic writes.  Wraparound
1151 				 * happened.
1152 				 */
1153 
1154 				PIPE_UNLOCK(rpipe);
1155 				error = uiomove(
1156 				    &wpipe->pipe_buffer.buffer[0],
1157 				    size - segsize, uio);
1158 				PIPE_LOCK(rpipe);
1159 			}
1160 			if (error == 0) {
1161 				wpipe->pipe_buffer.in += size;
1162 				if (wpipe->pipe_buffer.in >=
1163 				    wpipe->pipe_buffer.size) {
1164 					KASSERT(wpipe->pipe_buffer.in ==
1165 						size - segsize +
1166 						wpipe->pipe_buffer.size,
1167 						("Expected wraparound bad"));
1168 					wpipe->pipe_buffer.in = size - segsize;
1169 				}
1170 
1171 				wpipe->pipe_buffer.cnt += size;
1172 				KASSERT(wpipe->pipe_buffer.cnt <=
1173 					wpipe->pipe_buffer.size,
1174 					("Pipe buffer overflow"));
1175 			}
1176 			pipeunlock(wpipe);
1177 		} else {
1178 			/*
1179 			 * If the "read-side" has been blocked, wake it up now.
1180 			 */
1181 			if (wpipe->pipe_state & PIPE_WANTR) {
1182 				wpipe->pipe_state &= ~PIPE_WANTR;
1183 				wakeup(wpipe);
1184 			}
1185 
1186 			/*
1187 			 * don't block on non-blocking I/O
1188 			 */
1189 			if (fp->f_flag & FNONBLOCK) {
1190 				error = EAGAIN;
1191 				pipeunlock(wpipe);
1192 				break;
1193 			}
1194 
1195 			/*
1196 			 * We have no more space and have something to offer,
1197 			 * wake up select/poll.
1198 			 */
1199 			pipeselwakeup(wpipe);
1200 
1201 			wpipe->pipe_state |= PIPE_WANTW;
1202 			pipeunlock(wpipe);
1203 			error = msleep(wpipe, PIPE_MTX(rpipe),
1204 			    PRIBIO | PCATCH, "pipewr", 0);
1205 			if (error != 0)
1206 				break;
1207 		}
1208 	}
1209 
1210 	pipelock(wpipe, 0);
1211 	--wpipe->pipe_busy;
1212 
1213 	if ((wpipe->pipe_busy == 0) && (wpipe->pipe_state & PIPE_WANT)) {
1214 		wpipe->pipe_state &= ~(PIPE_WANT | PIPE_WANTR);
1215 		wakeup(wpipe);
1216 	} else if (wpipe->pipe_buffer.cnt > 0) {
1217 		/*
1218 		 * If we have put any characters in the buffer, we wake up
1219 		 * the reader.
1220 		 */
1221 		if (wpipe->pipe_state & PIPE_WANTR) {
1222 			wpipe->pipe_state &= ~PIPE_WANTR;
1223 			wakeup(wpipe);
1224 		}
1225 	}
1226 
1227 	/*
1228 	 * Don't return EPIPE if I/O was successful
1229 	 */
1230 	if ((wpipe->pipe_buffer.cnt == 0) &&
1231 	    (uio->uio_resid == 0) &&
1232 	    (error == EPIPE)) {
1233 		error = 0;
1234 	}
1235 
1236 	if (error == 0)
1237 		vfs_timestamp(&wpipe->pipe_mtime);
1238 
1239 	/*
1240 	 * We have something to offer,
1241 	 * wake up select/poll.
1242 	 */
1243 	if (wpipe->pipe_buffer.cnt)
1244 		pipeselwakeup(wpipe);
1245 
1246 	pipeunlock(wpipe);
1247 	PIPE_UNLOCK(rpipe);
1248 	return (error);
1249 }
1250 
1251 /*
1252  * we implement a very minimal set of ioctls for compatibility with sockets.
1253  */
1254 static int
1255 pipe_ioctl(fp, cmd, data, active_cred, td)
1256 	struct file *fp;
1257 	u_long cmd;
1258 	void *data;
1259 	struct ucred *active_cred;
1260 	struct thread *td;
1261 {
1262 	struct pipe *mpipe = fp->f_data;
1263 	int error;
1264 
1265 	PIPE_LOCK(mpipe);
1266 
1267 #ifdef MAC
1268 	error = mac_check_pipe_ioctl(active_cred, mpipe->pipe_pair, cmd, data);
1269 	if (error) {
1270 		PIPE_UNLOCK(mpipe);
1271 		return (error);
1272 	}
1273 #endif
1274 
1275 	error = 0;
1276 	switch (cmd) {
1277 
1278 	case FIONBIO:
1279 		break;
1280 
1281 	case FIOASYNC:
1282 		if (*(int *)data) {
1283 			mpipe->pipe_state |= PIPE_ASYNC;
1284 		} else {
1285 			mpipe->pipe_state &= ~PIPE_ASYNC;
1286 		}
1287 		break;
1288 
1289 	case FIONREAD:
1290 		if (mpipe->pipe_state & PIPE_DIRECTW)
1291 			*(int *)data = mpipe->pipe_map.cnt;
1292 		else
1293 			*(int *)data = mpipe->pipe_buffer.cnt;
1294 		break;
1295 
1296 	case FIOSETOWN:
1297 		PIPE_UNLOCK(mpipe);
1298 		error = fsetown(*(int *)data, &mpipe->pipe_sigio);
1299 		goto out_unlocked;
1300 
1301 	case FIOGETOWN:
1302 		*(int *)data = fgetown(&mpipe->pipe_sigio);
1303 		break;
1304 
1305 	/* This is deprecated, FIOSETOWN should be used instead. */
1306 	case TIOCSPGRP:
1307 		PIPE_UNLOCK(mpipe);
1308 		error = fsetown(-(*(int *)data), &mpipe->pipe_sigio);
1309 		goto out_unlocked;
1310 
1311 	/* This is deprecated, FIOGETOWN should be used instead. */
1312 	case TIOCGPGRP:
1313 		*(int *)data = -fgetown(&mpipe->pipe_sigio);
1314 		break;
1315 
1316 	default:
1317 		error = ENOTTY;
1318 		break;
1319 	}
1320 	PIPE_UNLOCK(mpipe);
1321 out_unlocked:
1322 	return (error);
1323 }
1324 
1325 static int
1326 pipe_poll(fp, events, active_cred, td)
1327 	struct file *fp;
1328 	int events;
1329 	struct ucred *active_cred;
1330 	struct thread *td;
1331 {
1332 	struct pipe *rpipe = fp->f_data;
1333 	struct pipe *wpipe;
1334 	int revents = 0;
1335 #ifdef MAC
1336 	int error;
1337 #endif
1338 
1339 	wpipe = rpipe->pipe_peer;
1340 	PIPE_LOCK(rpipe);
1341 #ifdef MAC
1342 	error = mac_check_pipe_poll(active_cred, rpipe->pipe_pair);
1343 	if (error)
1344 		goto locked_error;
1345 #endif
1346 	if (events & (POLLIN | POLLRDNORM))
1347 		if ((rpipe->pipe_state & PIPE_DIRECTW) ||
1348 		    (rpipe->pipe_buffer.cnt > 0) ||
1349 		    (rpipe->pipe_state & PIPE_EOF))
1350 			revents |= events & (POLLIN | POLLRDNORM);
1351 
1352 	if (events & (POLLOUT | POLLWRNORM))
1353 		if (!wpipe->pipe_present || (wpipe->pipe_state & PIPE_EOF) ||
1354 		    (((wpipe->pipe_state & PIPE_DIRECTW) == 0) &&
1355 		     (wpipe->pipe_buffer.size - wpipe->pipe_buffer.cnt) >= PIPE_BUF))
1356 			revents |= events & (POLLOUT | POLLWRNORM);
1357 
1358 	if ((rpipe->pipe_state & PIPE_EOF) ||
1359 	    (!wpipe->pipe_present) ||
1360 	    (wpipe->pipe_state & PIPE_EOF))
1361 		revents |= POLLHUP;
1362 
1363 	if (revents == 0) {
1364 		if (events & (POLLIN | POLLRDNORM)) {
1365 			selrecord(td, &rpipe->pipe_sel);
1366 			rpipe->pipe_state |= PIPE_SEL;
1367 		}
1368 
1369 		if (events & (POLLOUT | POLLWRNORM)) {
1370 			selrecord(td, &wpipe->pipe_sel);
1371 			wpipe->pipe_state |= PIPE_SEL;
1372 		}
1373 	}
1374 #ifdef MAC
1375 locked_error:
1376 #endif
1377 	PIPE_UNLOCK(rpipe);
1378 
1379 	return (revents);
1380 }
1381 
1382 /*
1383  * We shouldn't need locks here as we're doing a read and this should
1384  * be a natural race.
1385  */
1386 static int
1387 pipe_stat(fp, ub, active_cred, td)
1388 	struct file *fp;
1389 	struct stat *ub;
1390 	struct ucred *active_cred;
1391 	struct thread *td;
1392 {
1393 	struct pipe *pipe = fp->f_data;
1394 #ifdef MAC
1395 	int error;
1396 
1397 	PIPE_LOCK(pipe);
1398 	error = mac_check_pipe_stat(active_cred, pipe->pipe_pair);
1399 	PIPE_UNLOCK(pipe);
1400 	if (error)
1401 		return (error);
1402 #endif
1403 	bzero(ub, sizeof(*ub));
1404 	ub->st_mode = S_IFIFO;
1405 	ub->st_blksize = PAGE_SIZE;
1406 	if (pipe->pipe_state & PIPE_DIRECTW)
1407 		ub->st_size = pipe->pipe_map.cnt;
1408 	else
1409 		ub->st_size = pipe->pipe_buffer.cnt;
1410 	ub->st_blocks = (ub->st_size + ub->st_blksize - 1) / ub->st_blksize;
1411 	ub->st_atimespec = pipe->pipe_atime;
1412 	ub->st_mtimespec = pipe->pipe_mtime;
1413 	ub->st_ctimespec = pipe->pipe_ctime;
1414 	ub->st_uid = fp->f_cred->cr_uid;
1415 	ub->st_gid = fp->f_cred->cr_gid;
1416 	/*
1417 	 * Left as 0: st_dev, st_ino, st_nlink, st_rdev, st_flags, st_gen.
1418 	 * XXX (st_dev, st_ino) should be unique.
1419 	 */
1420 	return (0);
1421 }
1422 
1423 /* ARGSUSED */
1424 static int
1425 pipe_close(fp, td)
1426 	struct file *fp;
1427 	struct thread *td;
1428 {
1429 	struct pipe *cpipe = fp->f_data;
1430 
1431 	fp->f_ops = &badfileops;
1432 	fp->f_data = NULL;
1433 	funsetown(&cpipe->pipe_sigio);
1434 	pipeclose(cpipe);
1435 	return (0);
1436 }
1437 
1438 static void
1439 pipe_free_kmem(cpipe)
1440 	struct pipe *cpipe;
1441 {
1442 
1443 	KASSERT(!mtx_owned(PIPE_MTX(cpipe)),
1444 	    ("pipe_free_kmem: pipe mutex locked"));
1445 
1446 	if (cpipe->pipe_buffer.buffer != NULL) {
1447 		atomic_subtract_int(&amountpipekva, cpipe->pipe_buffer.size);
1448 		vm_map_remove(pipe_map,
1449 		    (vm_offset_t)cpipe->pipe_buffer.buffer,
1450 		    (vm_offset_t)cpipe->pipe_buffer.buffer + cpipe->pipe_buffer.size);
1451 		cpipe->pipe_buffer.buffer = NULL;
1452 	}
1453 #ifndef PIPE_NODIRECT
1454 	{
1455 		cpipe->pipe_map.cnt = 0;
1456 		cpipe->pipe_map.pos = 0;
1457 		cpipe->pipe_map.npages = 0;
1458 	}
1459 #endif
1460 }
1461 
1462 /*
1463  * shutdown the pipe
1464  */
1465 static void
1466 pipeclose(cpipe)
1467 	struct pipe *cpipe;
1468 {
1469 	struct pipepair *pp;
1470 	struct pipe *ppipe;
1471 
1472 	KASSERT(cpipe != NULL, ("pipeclose: cpipe == NULL"));
1473 
1474 	PIPE_LOCK(cpipe);
1475 	pipelock(cpipe, 0);
1476 	pp = cpipe->pipe_pair;
1477 
1478 	pipeselwakeup(cpipe);
1479 
1480 	/*
1481 	 * If the other side is blocked, wake it up saying that
1482 	 * we want to close it down.
1483 	 */
1484 	cpipe->pipe_state |= PIPE_EOF;
1485 	while (cpipe->pipe_busy) {
1486 		wakeup(cpipe);
1487 		cpipe->pipe_state |= PIPE_WANT;
1488 		pipeunlock(cpipe);
1489 		msleep(cpipe, PIPE_MTX(cpipe), PRIBIO, "pipecl", 0);
1490 		pipelock(cpipe, 0);
1491 	}
1492 
1493 
1494 	/*
1495 	 * Disconnect from peer, if any.
1496 	 */
1497 	ppipe = cpipe->pipe_peer;
1498 	if (ppipe->pipe_present != 0) {
1499 		pipeselwakeup(ppipe);
1500 
1501 		ppipe->pipe_state |= PIPE_EOF;
1502 		wakeup(ppipe);
1503 		KNOTE_LOCKED(&ppipe->pipe_sel.si_note, 0);
1504 	}
1505 
1506 	/*
1507 	 * Mark this endpoint as free.  Release kmem resources.  We
1508 	 * don't mark this endpoint as unused until we've finished
1509 	 * doing that, or the pipe might disappear out from under
1510 	 * us.
1511 	 */
1512 	PIPE_UNLOCK(cpipe);
1513 	pipe_free_kmem(cpipe);
1514 	PIPE_LOCK(cpipe);
1515 	cpipe->pipe_present = 0;
1516 	pipeunlock(cpipe);
1517 	knlist_clear(&cpipe->pipe_sel.si_note, 1);
1518 	knlist_destroy(&cpipe->pipe_sel.si_note);
1519 
1520 	/*
1521 	 * If both endpoints are now closed, release the memory for the
1522 	 * pipe pair.  If not, unlock.
1523 	 */
1524 	if (ppipe->pipe_present == 0) {
1525 		PIPE_UNLOCK(cpipe);
1526 #ifdef MAC
1527 		mac_destroy_pipe(pp);
1528 #endif
1529 		uma_zfree(pipe_zone, cpipe->pipe_pair);
1530 	} else
1531 		PIPE_UNLOCK(cpipe);
1532 }
1533 
1534 /*ARGSUSED*/
1535 static int
1536 pipe_kqfilter(struct file *fp, struct knote *kn)
1537 {
1538 	struct pipe *cpipe;
1539 
1540 	cpipe = kn->kn_fp->f_data;
1541 	PIPE_LOCK(cpipe);
1542 	switch (kn->kn_filter) {
1543 	case EVFILT_READ:
1544 		kn->kn_fop = &pipe_rfiltops;
1545 		break;
1546 	case EVFILT_WRITE:
1547 		kn->kn_fop = &pipe_wfiltops;
1548 		if (!cpipe->pipe_peer->pipe_present) {
1549 			/* other end of pipe has been closed */
1550 			PIPE_UNLOCK(cpipe);
1551 			return (EPIPE);
1552 		}
1553 		cpipe = cpipe->pipe_peer;
1554 		break;
1555 	default:
1556 		PIPE_UNLOCK(cpipe);
1557 		return (EINVAL);
1558 	}
1559 
1560 	knlist_add(&cpipe->pipe_sel.si_note, kn, 1);
1561 	PIPE_UNLOCK(cpipe);
1562 	return (0);
1563 }
1564 
1565 static void
1566 filt_pipedetach(struct knote *kn)
1567 {
1568 	struct pipe *cpipe = (struct pipe *)kn->kn_fp->f_data;
1569 
1570 	PIPE_LOCK(cpipe);
1571 	if (kn->kn_filter == EVFILT_WRITE) {
1572 		if (!cpipe->pipe_peer->pipe_present) {
1573 			PIPE_UNLOCK(cpipe);
1574 			return;
1575 		}
1576 		cpipe = cpipe->pipe_peer;
1577 	}
1578 	knlist_remove(&cpipe->pipe_sel.si_note, kn, 1);
1579 	PIPE_UNLOCK(cpipe);
1580 }
1581 
1582 /*ARGSUSED*/
1583 static int
1584 filt_piperead(struct knote *kn, long hint)
1585 {
1586 	struct pipe *rpipe = kn->kn_fp->f_data;
1587 	struct pipe *wpipe = rpipe->pipe_peer;
1588 	int ret;
1589 
1590 	PIPE_LOCK(rpipe);
1591 	kn->kn_data = rpipe->pipe_buffer.cnt;
1592 	if ((kn->kn_data == 0) && (rpipe->pipe_state & PIPE_DIRECTW))
1593 		kn->kn_data = rpipe->pipe_map.cnt;
1594 
1595 	if ((rpipe->pipe_state & PIPE_EOF) ||
1596 	    (!wpipe->pipe_present) || (wpipe->pipe_state & PIPE_EOF)) {
1597 		kn->kn_flags |= EV_EOF;
1598 		PIPE_UNLOCK(rpipe);
1599 		return (1);
1600 	}
1601 	ret = kn->kn_data > 0;
1602 	PIPE_UNLOCK(rpipe);
1603 	return ret;
1604 }
1605 
1606 /*ARGSUSED*/
1607 static int
1608 filt_pipewrite(struct knote *kn, long hint)
1609 {
1610 	struct pipe *rpipe = kn->kn_fp->f_data;
1611 	struct pipe *wpipe = rpipe->pipe_peer;
1612 
1613 	PIPE_LOCK(rpipe);
1614 	if ((!wpipe->pipe_present) || (wpipe->pipe_state & PIPE_EOF)) {
1615 		kn->kn_data = 0;
1616 		kn->kn_flags |= EV_EOF;
1617 		PIPE_UNLOCK(rpipe);
1618 		return (1);
1619 	}
1620 	kn->kn_data = wpipe->pipe_buffer.size - wpipe->pipe_buffer.cnt;
1621 	if (wpipe->pipe_state & PIPE_DIRECTW)
1622 		kn->kn_data = 0;
1623 
1624 	PIPE_UNLOCK(rpipe);
1625 	return (kn->kn_data >= PIPE_BUF);
1626 }
1627