sys_pipe.c revision 52983
113675Sdyson/*
213675Sdyson * Copyright (c) 1996 John S. Dyson
313675Sdyson * All rights reserved.
413675Sdyson *
513675Sdyson * Redistribution and use in source and binary forms, with or without
613675Sdyson * modification, are permitted provided that the following conditions
713675Sdyson * are met:
813675Sdyson * 1. Redistributions of source code must retain the above copyright
913675Sdyson *    notice immediately at the beginning of the file, without modification,
1013675Sdyson *    this list of conditions, and the following disclaimer.
1113675Sdyson * 2. Redistributions in binary form must reproduce the above copyright
1213675Sdyson *    notice, this list of conditions and the following disclaimer in the
1313675Sdyson *    documentation and/or other materials provided with the distribution.
1413675Sdyson * 3. Absolutely no warranty of function or purpose is made by the author
1513675Sdyson *    John S. Dyson.
1614037Sdyson * 4. Modifications may be freely made to this file if the above conditions
1713675Sdyson *    are met.
1813675Sdyson *
1950477Speter * $FreeBSD: head/sys/kern/sys_pipe.c 52983 1999-11-08 03:28:49Z peter $
2013675Sdyson */
2113675Sdyson
2213675Sdyson/*
2313675Sdyson * This file contains a high-performance replacement for the socket-based
2413675Sdyson * pipes scheme originally used in FreeBSD/4.4Lite.  It does not support
2513675Sdyson * all features of sockets, but does do everything that pipes normally
2613675Sdyson * do.
2713675Sdyson */
2813675Sdyson
2913907Sdyson/*
3013907Sdyson * This code has two modes of operation, a small write mode and a large
3113907Sdyson * write mode.  The small write mode acts like conventional pipes with
3213907Sdyson * a kernel buffer.  If the buffer is less than PIPE_MINDIRECT, then the
3313907Sdyson * "normal" pipe buffering is done.  If the buffer is between PIPE_MINDIRECT
3413907Sdyson * and PIPE_SIZE in size, it is fully mapped and wired into the kernel, and
3513907Sdyson * the receiving process can copy it directly from the pages in the sending
3613907Sdyson * process.
3713907Sdyson *
3813907Sdyson * If the sending process receives a signal, it is possible that it will
3913913Sdyson * go away, and certainly its address space can change, because control
4013907Sdyson * is returned back to the user-mode side.  In that case, the pipe code
4113907Sdyson * arranges to copy the buffer supplied by the user process, to a pageable
4213907Sdyson * kernel buffer, and the receiving process will grab the data from the
4313907Sdyson * pageable kernel buffer.  Since signals don't happen all that often,
4413907Sdyson * the copy operation is normally eliminated.
4513907Sdyson *
4613907Sdyson * The constant PIPE_MINDIRECT is chosen to make sure that buffering will
4713907Sdyson * happen for small transfers so that the system will not spend all of
4813913Sdyson * its time context switching.  PIPE_SIZE is constrained by the
4913907Sdyson * amount of kernel virtual memory.
5013907Sdyson */
5113907Sdyson
5213675Sdyson#include <sys/param.h>
5313675Sdyson#include <sys/systm.h>
5413675Sdyson#include <sys/proc.h>
5524131Sbde#include <sys/fcntl.h>
5613675Sdyson#include <sys/file.h>
5713675Sdyson#include <sys/filedesc.h>
5824206Sbde#include <sys/filio.h>
5924206Sbde#include <sys/ttycom.h>
6013675Sdyson#include <sys/stat.h>
6129356Speter#include <sys/poll.h>
6243278Sbde#include <sys/select.h>
6313675Sdyson#include <sys/signalvar.h>
6413675Sdyson#include <sys/sysproto.h>
6513675Sdyson#include <sys/pipe.h>
6634924Sbde#include <sys/uio.h>
6713675Sdyson
6813675Sdyson#include <vm/vm.h>
6913675Sdyson#include <vm/vm_param.h>
7022521Sdyson#include <sys/lock.h>
7113675Sdyson#include <vm/vm_object.h>
7213675Sdyson#include <vm/vm_kern.h>
7313675Sdyson#include <vm/vm_extern.h>
7413675Sdyson#include <vm/pmap.h>
7513675Sdyson#include <vm/vm_map.h>
7613907Sdyson#include <vm/vm_page.h>
7727899Sdyson#include <vm/vm_zone.h>
7813675Sdyson
7914037Sdyson/*
8014037Sdyson * Use this define if you want to disable *fancy* VM things.  Expect an
8114037Sdyson * approx 30% decrease in transfer rate.  This could be useful for
8214037Sdyson * NetBSD or OpenBSD.
8314037Sdyson */
8414037Sdyson/* #define PIPE_NODIRECT */
8514037Sdyson
8614037Sdyson/*
8714037Sdyson * interfaces to the outside world
8814037Sdyson */
8913675Sdysonstatic int pipe_read __P((struct file *fp, struct uio *uio,
9051418Sgreen		struct ucred *cred, int flags, struct proc *p));
9113675Sdysonstatic int pipe_write __P((struct file *fp, struct uio *uio,
9251418Sgreen		struct ucred *cred, int flags, struct proc *p));
9313675Sdysonstatic int pipe_close __P((struct file *fp, struct proc *p));
9429356Speterstatic int pipe_poll __P((struct file *fp, int events, struct ucred *cred,
9529356Speter		struct proc *p));
9652983Speterstatic int pipe_stat __P((struct file *fp, struct stat *sb, struct proc *p));
9736735Sdfrstatic int pipe_ioctl __P((struct file *fp, u_long cmd, caddr_t data, struct proc *p));
9813675Sdyson
9913675Sdysonstatic struct fileops pipeops =
10052983Speter    { pipe_read, pipe_write, pipe_ioctl, pipe_poll, pipe_stat, pipe_close };
10113675Sdyson
10213675Sdyson/*
10313675Sdyson * Default pipe buffer size(s), this can be kind-of large now because pipe
10413675Sdyson * space is pageable.  The pipe code will try to maintain locality of
10513675Sdyson * reference for performance reasons, so small amounts of outstanding I/O
10613675Sdyson * will not wipe the cache.
10713675Sdyson */
10813907Sdyson#define MINPIPESIZE (PIPE_SIZE/3)
10913907Sdyson#define MAXPIPESIZE (2*PIPE_SIZE/3)
11013675Sdyson
11113907Sdyson/*
11213907Sdyson * Maximum amount of kva for pipes -- this is kind-of a soft limit, but
11313907Sdyson * is there so that on large systems, we don't exhaust it.
11413907Sdyson */
11513907Sdyson#define MAXPIPEKVA (8*1024*1024)
11613907Sdyson
11713907Sdyson/*
11813907Sdyson * Limit for direct transfers, we cannot, of course limit
11913907Sdyson * the amount of kva for pipes in general though.
12013907Sdyson */
12113907Sdyson#define LIMITPIPEKVA (16*1024*1024)
12217163Sdyson
12317163Sdyson/*
12417163Sdyson * Limit the number of "big" pipes
12517163Sdyson */
12617163Sdyson#define LIMITBIGPIPES	32
12733181Seivindstatic int nbigpipe;
12817163Sdyson
12917124Sbdestatic int amountpipekva;
13013907Sdyson
13113675Sdysonstatic void pipeclose __P((struct pipe *cpipe));
13213675Sdysonstatic void pipeinit __P((struct pipe *cpipe));
13313907Sdysonstatic __inline int pipelock __P((struct pipe *cpipe, int catch));
13413675Sdysonstatic __inline void pipeunlock __P((struct pipe *cpipe));
13514122Speterstatic __inline void pipeselwakeup __P((struct pipe *cpipe));
13614037Sdyson#ifndef PIPE_NODIRECT
13713907Sdysonstatic int pipe_build_write_buffer __P((struct pipe *wpipe, struct uio *uio));
13813907Sdysonstatic void pipe_destroy_write_buffer __P((struct pipe *wpipe));
13913907Sdysonstatic int pipe_direct_write __P((struct pipe *wpipe, struct uio *uio));
14013907Sdysonstatic void pipe_clone_write_buffer __P((struct pipe *wpipe));
14114037Sdyson#endif
14213907Sdysonstatic void pipespace __P((struct pipe *cpipe));
14313675Sdyson
14433181Seivindstatic vm_zone_t pipe_zone;
14527899Sdyson
14613675Sdyson/*
14713675Sdyson * The pipe system call for the DTYPE_PIPE type of pipes
14813675Sdyson */
14913675Sdyson
15013675Sdyson/* ARGSUSED */
15113675Sdysonint
15230994Sphkpipe(p, uap)
15313675Sdyson	struct proc *p;
15413675Sdyson	struct pipe_args /* {
15513675Sdyson		int	dummy;
15613675Sdyson	} */ *uap;
15713675Sdyson{
15813675Sdyson	register struct filedesc *fdp = p->p_fd;
15913675Sdyson	struct file *rf, *wf;
16013675Sdyson	struct pipe *rpipe, *wpipe;
16113675Sdyson	int fd, error;
16213675Sdyson
16327899Sdyson	if (pipe_zone == NULL)
16427923Sdyson		pipe_zone = zinit("PIPE", sizeof (struct pipe), 0, 0, 4);
16527899Sdyson
16627899Sdyson	rpipe = zalloc( pipe_zone);
16713675Sdyson	pipeinit(rpipe);
16813907Sdyson	rpipe->pipe_state |= PIPE_DIRECTOK;
16927899Sdyson	wpipe = zalloc( pipe_zone);
17013675Sdyson	pipeinit(wpipe);
17113907Sdyson	wpipe->pipe_state |= PIPE_DIRECTOK;
17213675Sdyson
17313675Sdyson	error = falloc(p, &rf, &fd);
17413675Sdyson	if (error)
17513675Sdyson		goto free2;
17630994Sphk	p->p_retval[0] = fd;
17713675Sdyson	rf->f_flag = FREAD | FWRITE;
17813675Sdyson	rf->f_type = DTYPE_PIPE;
17949413Sgreen	rf->f_data = (caddr_t)rpipe;
18013675Sdyson	rf->f_ops = &pipeops;
18113675Sdyson	error = falloc(p, &wf, &fd);
18213675Sdyson	if (error)
18313675Sdyson		goto free3;
18413675Sdyson	wf->f_flag = FREAD | FWRITE;
18513675Sdyson	wf->f_type = DTYPE_PIPE;
18649413Sgreen	wf->f_data = (caddr_t)wpipe;
18713675Sdyson	wf->f_ops = &pipeops;
18830994Sphk	p->p_retval[1] = fd;
18913675Sdyson
19013675Sdyson	rpipe->pipe_peer = wpipe;
19113675Sdyson	wpipe->pipe_peer = rpipe;
19213675Sdyson
19313675Sdyson	return (0);
19413675Sdysonfree3:
19549413Sgreen	fdp->fd_ofiles[p->p_retval[0]] = 0;
19613675Sdyson	ffree(rf);
19713675Sdysonfree2:
19813675Sdyson	(void)pipeclose(wpipe);
19913675Sdyson	(void)pipeclose(rpipe);
20013675Sdyson	return (error);
20113675Sdyson}
20213675Sdyson
20313909Sdyson/*
20413909Sdyson * Allocate kva for pipe circular buffer, the space is pageable
20513909Sdyson */
20613675Sdysonstatic void
20713907Sdysonpipespace(cpipe)
20813675Sdyson	struct pipe *cpipe;
20913675Sdyson{
21013688Sdyson	int npages, error;
21113675Sdyson
21213907Sdyson	npages = round_page(cpipe->pipe_buffer.size)/PAGE_SIZE;
21313675Sdyson	/*
21413675Sdyson	 * Create an object, I don't like the idea of paging to/from
21513675Sdyson	 * kernel_object.
21614037Sdyson	 * XXX -- minor change needed here for NetBSD/OpenBSD VM systems.
21713675Sdyson	 */
21813675Sdyson	cpipe->pipe_buffer.object = vm_object_allocate(OBJT_DEFAULT, npages);
21913688Sdyson	cpipe->pipe_buffer.buffer = (caddr_t) vm_map_min(kernel_map);
22013675Sdyson
22113675Sdyson	/*
22213675Sdyson	 * Insert the object into the kernel map, and allocate kva for it.
22313675Sdyson	 * The map entry is, by default, pageable.
22414037Sdyson	 * XXX -- minor change needed here for NetBSD/OpenBSD VM systems.
22513675Sdyson	 */
22613688Sdyson	error = vm_map_find(kernel_map, cpipe->pipe_buffer.object, 0,
22713907Sdyson		(vm_offset_t *) &cpipe->pipe_buffer.buffer,
22813907Sdyson		cpipe->pipe_buffer.size, 1,
22913688Sdyson		VM_PROT_ALL, VM_PROT_ALL, 0);
23013675Sdyson
23113688Sdyson	if (error != KERN_SUCCESS)
23213688Sdyson		panic("pipeinit: cannot allocate pipe -- out of kvm -- code = %d", error);
23313907Sdyson	amountpipekva += cpipe->pipe_buffer.size;
23413907Sdyson}
23513688Sdyson
23613907Sdyson/*
23713907Sdyson * initialize and allocate VM and memory for pipe
23813907Sdyson */
23913907Sdysonstatic void
24013907Sdysonpipeinit(cpipe)
24113907Sdyson	struct pipe *cpipe;
24213907Sdyson{
24313907Sdyson
24413675Sdyson	cpipe->pipe_buffer.in = 0;
24513675Sdyson	cpipe->pipe_buffer.out = 0;
24613675Sdyson	cpipe->pipe_buffer.cnt = 0;
24713907Sdyson	cpipe->pipe_buffer.size = PIPE_SIZE;
24817163Sdyson
24913907Sdyson	/* Buffer kva gets dynamically allocated */
25013907Sdyson	cpipe->pipe_buffer.buffer = NULL;
25117124Sbde	/* cpipe->pipe_buffer.object = invalid */
25213675Sdyson
25313675Sdyson	cpipe->pipe_state = 0;
25413675Sdyson	cpipe->pipe_peer = NULL;
25513675Sdyson	cpipe->pipe_busy = 0;
25634901Sphk	getnanotime(&cpipe->pipe_ctime);
25724101Sbde	cpipe->pipe_atime = cpipe->pipe_ctime;
25824101Sbde	cpipe->pipe_mtime = cpipe->pipe_ctime;
25913675Sdyson	bzero(&cpipe->pipe_sel, sizeof cpipe->pipe_sel);
26013907Sdyson
26114037Sdyson#ifndef PIPE_NODIRECT
26213907Sdyson	/*
26313907Sdyson	 * pipe data structure initializations to support direct pipe I/O
26413907Sdyson	 */
26513907Sdyson	cpipe->pipe_map.cnt = 0;
26613907Sdyson	cpipe->pipe_map.kva = 0;
26713907Sdyson	cpipe->pipe_map.pos = 0;
26813907Sdyson	cpipe->pipe_map.npages = 0;
26917124Sbde	/* cpipe->pipe_map.ms[] = invalid */
27014037Sdyson#endif
27113675Sdyson}
27213675Sdyson
27313675Sdyson
27413675Sdyson/*
27513675Sdyson * lock a pipe for I/O, blocking other access
27613675Sdyson */
27713675Sdysonstatic __inline int
27813907Sdysonpipelock(cpipe, catch)
27913675Sdyson	struct pipe *cpipe;
28013907Sdyson	int catch;
28113675Sdyson{
28213776Sdyson	int error;
28313675Sdyson	while (cpipe->pipe_state & PIPE_LOCK) {
28413675Sdyson		cpipe->pipe_state |= PIPE_LWANT;
28543301Sdillon		if ((error = tsleep( cpipe,
28643301Sdillon			catch?(PRIBIO|PCATCH):PRIBIO, "pipelk", 0)) != 0) {
28713776Sdyson			return error;
28813675Sdyson		}
28913675Sdyson	}
29013675Sdyson	cpipe->pipe_state |= PIPE_LOCK;
29113675Sdyson	return 0;
29213675Sdyson}
29313675Sdyson
29413675Sdyson/*
29513675Sdyson * unlock a pipe I/O lock
29613675Sdyson */
29713675Sdysonstatic __inline void
29813675Sdysonpipeunlock(cpipe)
29913675Sdyson	struct pipe *cpipe;
30013675Sdyson{
30113675Sdyson	cpipe->pipe_state &= ~PIPE_LOCK;
30213675Sdyson	if (cpipe->pipe_state & PIPE_LWANT) {
30313675Sdyson		cpipe->pipe_state &= ~PIPE_LWANT;
30414177Sdyson		wakeup(cpipe);
30513675Sdyson	}
30613675Sdyson}
30713675Sdyson
30814037Sdysonstatic __inline void
30914037Sdysonpipeselwakeup(cpipe)
31014037Sdyson	struct pipe *cpipe;
31114037Sdyson{
31214037Sdyson	if (cpipe->pipe_state & PIPE_SEL) {
31314037Sdyson		cpipe->pipe_state &= ~PIPE_SEL;
31414037Sdyson		selwakeup(&cpipe->pipe_sel);
31514037Sdyson	}
31641086Struckman	if ((cpipe->pipe_state & PIPE_ASYNC) && cpipe->pipe_sigio)
31741086Struckman		pgsigio(cpipe->pipe_sigio, SIGIO, 0);
31814037Sdyson}
31914037Sdyson
32013675Sdyson/* ARGSUSED */
32113675Sdysonstatic int
32251418Sgreenpipe_read(fp, uio, cred, flags, p)
32313675Sdyson	struct file *fp;
32413675Sdyson	struct uio *uio;
32513675Sdyson	struct ucred *cred;
32651418Sgreen	struct proc *p;
32745311Sdt	int flags;
32813675Sdyson{
32913675Sdyson
33013675Sdyson	struct pipe *rpipe = (struct pipe *) fp->f_data;
33147748Salc	int error;
33213675Sdyson	int nread = 0;
33318863Sdyson	u_int size;
33413675Sdyson
33513675Sdyson	++rpipe->pipe_busy;
33647748Salc	error = pipelock(rpipe, 1);
33747748Salc	if (error)
33847748Salc		goto unlocked_error;
33947748Salc
34013675Sdyson	while (uio->uio_resid) {
34113907Sdyson		/*
34213907Sdyson		 * normal pipe buffer receive
34313907Sdyson		 */
34413675Sdyson		if (rpipe->pipe_buffer.cnt > 0) {
34518863Sdyson			size = rpipe->pipe_buffer.size - rpipe->pipe_buffer.out;
34613675Sdyson			if (size > rpipe->pipe_buffer.cnt)
34713675Sdyson				size = rpipe->pipe_buffer.cnt;
34818863Sdyson			if (size > (u_int) uio->uio_resid)
34918863Sdyson				size = (u_int) uio->uio_resid;
35047748Salc
35147748Salc			error = uiomove(&rpipe->pipe_buffer.buffer[rpipe->pipe_buffer.out],
35213675Sdyson					size, uio);
35313675Sdyson			if (error) {
35413675Sdyson				break;
35513675Sdyson			}
35613675Sdyson			rpipe->pipe_buffer.out += size;
35713675Sdyson			if (rpipe->pipe_buffer.out >= rpipe->pipe_buffer.size)
35813675Sdyson				rpipe->pipe_buffer.out = 0;
35913675Sdyson
36013675Sdyson			rpipe->pipe_buffer.cnt -= size;
36147748Salc
36247748Salc			/*
36347748Salc			 * If there is no more to read in the pipe, reset
36447748Salc			 * its pointers to the beginning.  This improves
36547748Salc			 * cache hit stats.
36647748Salc			 */
36747748Salc			if (rpipe->pipe_buffer.cnt == 0) {
36847748Salc				rpipe->pipe_buffer.in = 0;
36947748Salc				rpipe->pipe_buffer.out = 0;
37047748Salc			}
37113675Sdyson			nread += size;
37214037Sdyson#ifndef PIPE_NODIRECT
37313907Sdyson		/*
37413907Sdyson		 * Direct copy, bypassing a kernel buffer.
37513907Sdyson		 */
37613907Sdyson		} else if ((size = rpipe->pipe_map.cnt) &&
37747748Salc			   (rpipe->pipe_state & PIPE_DIRECTW)) {
37847748Salc			caddr_t	va;
37918863Sdyson			if (size > (u_int) uio->uio_resid)
38018863Sdyson				size = (u_int) uio->uio_resid;
38147748Salc
38247748Salc			va = (caddr_t) rpipe->pipe_map.kva + rpipe->pipe_map.pos;
38347748Salc			error = uiomove(va, size, uio);
38413907Sdyson			if (error)
38513907Sdyson				break;
38613907Sdyson			nread += size;
38713907Sdyson			rpipe->pipe_map.pos += size;
38813907Sdyson			rpipe->pipe_map.cnt -= size;
38913907Sdyson			if (rpipe->pipe_map.cnt == 0) {
39013907Sdyson				rpipe->pipe_state &= ~PIPE_DIRECTW;
39113907Sdyson				wakeup(rpipe);
39213907Sdyson			}
39314037Sdyson#endif
39413675Sdyson		} else {
39513675Sdyson			/*
39613675Sdyson			 * detect EOF condition
39713675Sdyson			 */
39813675Sdyson			if (rpipe->pipe_state & PIPE_EOF) {
39914802Sdyson				/* XXX error = ? */
40013675Sdyson				break;
40113675Sdyson			}
40243623Sdillon
40313675Sdyson			/*
40413675Sdyson			 * If the "write-side" has been blocked, wake it up now.
40513675Sdyson			 */
40613675Sdyson			if (rpipe->pipe_state & PIPE_WANTW) {
40713675Sdyson				rpipe->pipe_state &= ~PIPE_WANTW;
40813675Sdyson				wakeup(rpipe);
40913675Sdyson			}
41043623Sdillon
41143623Sdillon			/*
41247748Salc			 * Break if some data was read.
41343623Sdillon			 */
41447748Salc			if (nread > 0)
41513675Sdyson				break;
41616960Sdyson
41743623Sdillon			/*
41847748Salc			 * Unlock the pipe buffer for our remaining processing.  We
41947748Salc			 * will either break out with an error or we will sleep and
42047748Salc			 * relock to loop.
42143623Sdillon			 */
42247748Salc			pipeunlock(rpipe);
42343623Sdillon
42413675Sdyson			/*
42547748Salc			 * Handle non-blocking mode operation or
42647748Salc			 * wait for more data.
42713675Sdyson			 */
42847748Salc			if (fp->f_flag & FNONBLOCK)
42947748Salc				error = EAGAIN;
43047748Salc			else {
43147748Salc				rpipe->pipe_state |= PIPE_WANTR;
43247748Salc				if ((error = tsleep(rpipe, PRIBIO|PCATCH, "piperd", 0)) == 0)
43347748Salc					error = pipelock(rpipe, 1);
43413675Sdyson			}
43547748Salc			if (error)
43647748Salc				goto unlocked_error;
43713675Sdyson		}
43813675Sdyson	}
43947748Salc	pipeunlock(rpipe);
44013675Sdyson
44124101Sbde	if (error == 0)
44234901Sphk		getnanotime(&rpipe->pipe_atime);
44347748Salcunlocked_error:
44447748Salc	--rpipe->pipe_busy;
44513913Sdyson
44647748Salc	/*
44747748Salc	 * PIPE_WANT processing only makes sense if pipe_busy is 0.
44847748Salc	 */
44913675Sdyson	if ((rpipe->pipe_busy == 0) && (rpipe->pipe_state & PIPE_WANT)) {
45013675Sdyson		rpipe->pipe_state &= ~(PIPE_WANT|PIPE_WANTW);
45113675Sdyson		wakeup(rpipe);
45213675Sdyson	} else if (rpipe->pipe_buffer.cnt < MINPIPESIZE) {
45313675Sdyson		/*
45447748Salc		 * Handle write blocking hysteresis.
45513675Sdyson		 */
45613675Sdyson		if (rpipe->pipe_state & PIPE_WANTW) {
45713675Sdyson			rpipe->pipe_state &= ~PIPE_WANTW;
45813675Sdyson			wakeup(rpipe);
45913675Sdyson		}
46013675Sdyson	}
46114037Sdyson
46214802Sdyson	if ((rpipe->pipe_buffer.size - rpipe->pipe_buffer.cnt) >= PIPE_BUF)
46314037Sdyson		pipeselwakeup(rpipe);
46414037Sdyson
46513675Sdyson	return error;
46613675Sdyson}
46713675Sdyson
46814037Sdyson#ifndef PIPE_NODIRECT
46913907Sdyson/*
47013907Sdyson * Map the sending processes' buffer into kernel space and wire it.
47113907Sdyson * This is similar to a physical write operation.
47213907Sdyson */
47313675Sdysonstatic int
47413907Sdysonpipe_build_write_buffer(wpipe, uio)
47513907Sdyson	struct pipe *wpipe;
47613675Sdyson	struct uio *uio;
47713675Sdyson{
47818863Sdyson	u_int size;
47913907Sdyson	int i;
48013907Sdyson	vm_offset_t addr, endaddr, paddr;
48113907Sdyson
48218863Sdyson	size = (u_int) uio->uio_iov->iov_len;
48313907Sdyson	if (size > wpipe->pipe_buffer.size)
48413907Sdyson		size = wpipe->pipe_buffer.size;
48513907Sdyson
48640286Sdg	endaddr = round_page((vm_offset_t)uio->uio_iov->iov_base + size);
48740286Sdg	for(i = 0, addr = trunc_page((vm_offset_t)uio->uio_iov->iov_base);
48813907Sdyson		addr < endaddr;
48913907Sdyson		addr += PAGE_SIZE, i+=1) {
49013907Sdyson
49113907Sdyson		vm_page_t m;
49213907Sdyson
49351474Sdillon		if (vm_fault_quick((caddr_t)addr, VM_PROT_READ) < 0 ||
49451474Sdillon		    (paddr = pmap_kextract(addr)) == 0) {
49513907Sdyson			int j;
49613907Sdyson			for(j=0;j<i;j++)
49740700Sdg				vm_page_unwire(wpipe->pipe_map.ms[j], 1);
49813907Sdyson			return EFAULT;
49913907Sdyson		}
50013907Sdyson
50113907Sdyson		m = PHYS_TO_VM_PAGE(paddr);
50213907Sdyson		vm_page_wire(m);
50313907Sdyson		wpipe->pipe_map.ms[i] = m;
50413907Sdyson	}
50513907Sdyson
50613907Sdyson/*
50713907Sdyson * set up the control block
50813907Sdyson */
50913907Sdyson	wpipe->pipe_map.npages = i;
51013907Sdyson	wpipe->pipe_map.pos = ((vm_offset_t) uio->uio_iov->iov_base) & PAGE_MASK;
51113907Sdyson	wpipe->pipe_map.cnt = size;
51213907Sdyson
51313907Sdyson/*
51413907Sdyson * and map the buffer
51513907Sdyson */
51613907Sdyson	if (wpipe->pipe_map.kva == 0) {
51713912Sdyson		/*
51813912Sdyson		 * We need to allocate space for an extra page because the
51913912Sdyson		 * address range might (will) span pages at times.
52013912Sdyson		 */
52113907Sdyson		wpipe->pipe_map.kva = kmem_alloc_pageable(kernel_map,
52213912Sdyson			wpipe->pipe_buffer.size + PAGE_SIZE);
52313912Sdyson		amountpipekva += wpipe->pipe_buffer.size + PAGE_SIZE;
52413907Sdyson	}
52513907Sdyson	pmap_qenter(wpipe->pipe_map.kva, wpipe->pipe_map.ms,
52613907Sdyson		wpipe->pipe_map.npages);
52713907Sdyson
52813907Sdyson/*
52913907Sdyson * and update the uio data
53013907Sdyson */
53113907Sdyson
53213907Sdyson	uio->uio_iov->iov_len -= size;
53313907Sdyson	uio->uio_iov->iov_base += size;
53413907Sdyson	if (uio->uio_iov->iov_len == 0)
53513907Sdyson		uio->uio_iov++;
53613907Sdyson	uio->uio_resid -= size;
53713907Sdyson	uio->uio_offset += size;
53813907Sdyson	return 0;
53913907Sdyson}
54013907Sdyson
54113907Sdyson/*
54213907Sdyson * unmap and unwire the process buffer
54313907Sdyson */
54413907Sdysonstatic void
54513907Sdysonpipe_destroy_write_buffer(wpipe)
54613907Sdysonstruct pipe *wpipe;
54713907Sdyson{
54813907Sdyson	int i;
54917163Sdyson	if (wpipe->pipe_map.kva) {
55017163Sdyson		pmap_qremove(wpipe->pipe_map.kva, wpipe->pipe_map.npages);
55113907Sdyson
55213907Sdyson		if (amountpipekva > MAXPIPEKVA) {
55313907Sdyson			vm_offset_t kva = wpipe->pipe_map.kva;
55413907Sdyson			wpipe->pipe_map.kva = 0;
55513907Sdyson			kmem_free(kernel_map, kva,
55613912Sdyson				wpipe->pipe_buffer.size + PAGE_SIZE);
55713912Sdyson			amountpipekva -= wpipe->pipe_buffer.size + PAGE_SIZE;
55813907Sdyson		}
55913907Sdyson	}
56013907Sdyson	for (i=0;i<wpipe->pipe_map.npages;i++)
56140700Sdg		vm_page_unwire(wpipe->pipe_map.ms[i], 1);
56213907Sdyson}
56313907Sdyson
56413907Sdyson/*
56513907Sdyson * In the case of a signal, the writing process might go away.  This
56613907Sdyson * code copies the data into the circular buffer so that the source
56713907Sdyson * pages can be freed without loss of data.
56813907Sdyson */
56913907Sdysonstatic void
57013907Sdysonpipe_clone_write_buffer(wpipe)
57113907Sdysonstruct pipe *wpipe;
57213907Sdyson{
57313907Sdyson	int size;
57413907Sdyson	int pos;
57513907Sdyson
57613907Sdyson	size = wpipe->pipe_map.cnt;
57713907Sdyson	pos = wpipe->pipe_map.pos;
57813907Sdyson	bcopy((caddr_t) wpipe->pipe_map.kva+pos,
57913907Sdyson			(caddr_t) wpipe->pipe_buffer.buffer,
58013907Sdyson			size);
58113907Sdyson
58213907Sdyson	wpipe->pipe_buffer.in = size;
58313907Sdyson	wpipe->pipe_buffer.out = 0;
58413907Sdyson	wpipe->pipe_buffer.cnt = size;
58513907Sdyson	wpipe->pipe_state &= ~PIPE_DIRECTW;
58613907Sdyson
58713907Sdyson	pipe_destroy_write_buffer(wpipe);
58813907Sdyson}
58913907Sdyson
59013907Sdyson/*
59113907Sdyson * This implements the pipe buffer write mechanism.  Note that only
59213907Sdyson * a direct write OR a normal pipe write can be pending at any given time.
59313907Sdyson * If there are any characters in the pipe buffer, the direct write will
59413907Sdyson * be deferred until the receiving process grabs all of the bytes from
59513907Sdyson * the pipe buffer.  Then the direct mapping write is set-up.
59613907Sdyson */
59713907Sdysonstatic int
59813907Sdysonpipe_direct_write(wpipe, uio)
59913907Sdyson	struct pipe *wpipe;
60013907Sdyson	struct uio *uio;
60113907Sdyson{
60213907Sdyson	int error;
60313951Sdysonretry:
60413907Sdyson	while (wpipe->pipe_state & PIPE_DIRECTW) {
60513951Sdyson		if ( wpipe->pipe_state & PIPE_WANTR) {
60613951Sdyson			wpipe->pipe_state &= ~PIPE_WANTR;
60713951Sdyson			wakeup(wpipe);
60813951Sdyson		}
60913992Sdyson		wpipe->pipe_state |= PIPE_WANTW;
61013907Sdyson		error = tsleep(wpipe,
61113907Sdyson				PRIBIO|PCATCH, "pipdww", 0);
61214802Sdyson		if (error)
61313907Sdyson			goto error1;
61414802Sdyson		if (wpipe->pipe_state & PIPE_EOF) {
61514802Sdyson			error = EPIPE;
61614802Sdyson			goto error1;
61714802Sdyson		}
61813907Sdyson	}
61913907Sdyson	wpipe->pipe_map.cnt = 0;	/* transfer not ready yet */
62013951Sdyson	if (wpipe->pipe_buffer.cnt > 0) {
62113951Sdyson		if ( wpipe->pipe_state & PIPE_WANTR) {
62213951Sdyson			wpipe->pipe_state &= ~PIPE_WANTR;
62313951Sdyson			wakeup(wpipe);
62413951Sdyson		}
62513951Sdyson
62613992Sdyson		wpipe->pipe_state |= PIPE_WANTW;
62713907Sdyson		error = tsleep(wpipe,
62813907Sdyson				PRIBIO|PCATCH, "pipdwc", 0);
62914802Sdyson		if (error)
63013907Sdyson			goto error1;
63114802Sdyson		if (wpipe->pipe_state & PIPE_EOF) {
63214802Sdyson			error = EPIPE;
63314802Sdyson			goto error1;
63413907Sdyson		}
63513951Sdyson		goto retry;
63613907Sdyson	}
63713907Sdyson
63813951Sdyson	wpipe->pipe_state |= PIPE_DIRECTW;
63913951Sdyson
64013907Sdyson	error = pipe_build_write_buffer(wpipe, uio);
64113907Sdyson	if (error) {
64213907Sdyson		wpipe->pipe_state &= ~PIPE_DIRECTW;
64313907Sdyson		goto error1;
64413907Sdyson	}
64513907Sdyson
64613907Sdyson	error = 0;
64713907Sdyson	while (!error && (wpipe->pipe_state & PIPE_DIRECTW)) {
64813907Sdyson		if (wpipe->pipe_state & PIPE_EOF) {
64913907Sdyson			pipelock(wpipe, 0);
65013907Sdyson			pipe_destroy_write_buffer(wpipe);
65113907Sdyson			pipeunlock(wpipe);
65214037Sdyson			pipeselwakeup(wpipe);
65314802Sdyson			error = EPIPE;
65414802Sdyson			goto error1;
65513907Sdyson		}
65613992Sdyson		if (wpipe->pipe_state & PIPE_WANTR) {
65713992Sdyson			wpipe->pipe_state &= ~PIPE_WANTR;
65813992Sdyson			wakeup(wpipe);
65913992Sdyson		}
66014037Sdyson		pipeselwakeup(wpipe);
66113907Sdyson		error = tsleep(wpipe, PRIBIO|PCATCH, "pipdwt", 0);
66213907Sdyson	}
66313907Sdyson
66413907Sdyson	pipelock(wpipe,0);
66513907Sdyson	if (wpipe->pipe_state & PIPE_DIRECTW) {
66613907Sdyson		/*
66713907Sdyson		 * this bit of trickery substitutes a kernel buffer for
66813907Sdyson		 * the process that might be going away.
66913907Sdyson		 */
67013907Sdyson		pipe_clone_write_buffer(wpipe);
67113907Sdyson	} else {
67213907Sdyson		pipe_destroy_write_buffer(wpipe);
67313907Sdyson	}
67413907Sdyson	pipeunlock(wpipe);
67513907Sdyson	return error;
67613907Sdyson
67713907Sdysonerror1:
67813907Sdyson	wakeup(wpipe);
67913907Sdyson	return error;
68013907Sdyson}
68114037Sdyson#endif
68213907Sdyson
68316960Sdysonstatic int
68451418Sgreenpipe_write(fp, uio, cred, flags, p)
68516960Sdyson	struct file *fp;
68613907Sdyson	struct uio *uio;
68716960Sdyson	struct ucred *cred;
68851418Sgreen	struct proc *p;
68945311Sdt	int flags;
69013907Sdyson{
69113675Sdyson	int error = 0;
69213913Sdyson	int orig_resid;
69313675Sdyson
69416960Sdyson	struct pipe *wpipe, *rpipe;
69516960Sdyson
69616960Sdyson	rpipe = (struct pipe *) fp->f_data;
69716960Sdyson	wpipe = rpipe->pipe_peer;
69816960Sdyson
69913675Sdyson	/*
70013675Sdyson	 * detect loss of pipe read side, issue SIGPIPE if lost.
70113675Sdyson	 */
70216960Sdyson	if ((wpipe == NULL) || (wpipe->pipe_state & PIPE_EOF)) {
70313774Sdyson		return EPIPE;
70413675Sdyson	}
70513675Sdyson
70617163Sdyson	/*
70717163Sdyson	 * If it is advantageous to resize the pipe buffer, do
70817163Sdyson	 * so.
70917163Sdyson	 */
71017163Sdyson	if ((uio->uio_resid > PIPE_SIZE) &&
71117163Sdyson		(nbigpipe < LIMITBIGPIPES) &&
71217163Sdyson		(wpipe->pipe_state & PIPE_DIRECTW) == 0 &&
71317163Sdyson		(wpipe->pipe_buffer.size <= PIPE_SIZE) &&
71417163Sdyson		(wpipe->pipe_buffer.cnt == 0)) {
71517163Sdyson
71617163Sdyson		if (wpipe->pipe_buffer.buffer) {
71717163Sdyson			amountpipekva -= wpipe->pipe_buffer.size;
71817163Sdyson			kmem_free(kernel_map,
71917163Sdyson				(vm_offset_t)wpipe->pipe_buffer.buffer,
72017163Sdyson				wpipe->pipe_buffer.size);
72117163Sdyson		}
72217163Sdyson
72317163Sdyson#ifndef PIPE_NODIRECT
72417163Sdyson		if (wpipe->pipe_map.kva) {
72517163Sdyson			amountpipekva -= wpipe->pipe_buffer.size + PAGE_SIZE;
72617163Sdyson			kmem_free(kernel_map,
72717163Sdyson				wpipe->pipe_map.kva,
72817163Sdyson				wpipe->pipe_buffer.size + PAGE_SIZE);
72917163Sdyson		}
73017163Sdyson#endif
73117163Sdyson
73217163Sdyson		wpipe->pipe_buffer.in = 0;
73317163Sdyson		wpipe->pipe_buffer.out = 0;
73417163Sdyson		wpipe->pipe_buffer.cnt = 0;
73517163Sdyson		wpipe->pipe_buffer.size = BIG_PIPE_SIZE;
73617163Sdyson		wpipe->pipe_buffer.buffer = NULL;
73717163Sdyson		++nbigpipe;
73817163Sdyson
73917163Sdyson#ifndef PIPE_NODIRECT
74017163Sdyson		wpipe->pipe_map.cnt = 0;
74117163Sdyson		wpipe->pipe_map.kva = 0;
74217163Sdyson		wpipe->pipe_map.pos = 0;
74317163Sdyson		wpipe->pipe_map.npages = 0;
74417163Sdyson#endif
74517163Sdyson
74617163Sdyson	}
74717163Sdyson
74817163Sdyson
74913907Sdyson	if( wpipe->pipe_buffer.buffer == NULL) {
75013907Sdyson		if ((error = pipelock(wpipe,1)) == 0) {
75113907Sdyson			pipespace(wpipe);
75213907Sdyson			pipeunlock(wpipe);
75313907Sdyson		} else {
75413907Sdyson			return error;
75513907Sdyson		}
75613907Sdyson	}
75713907Sdyson
75813675Sdyson	++wpipe->pipe_busy;
75913913Sdyson	orig_resid = uio->uio_resid;
76013675Sdyson	while (uio->uio_resid) {
76113907Sdyson		int space;
76214037Sdyson#ifndef PIPE_NODIRECT
76313907Sdyson		/*
76413907Sdyson		 * If the transfer is large, we can gain performance if
76513907Sdyson		 * we do process-to-process copies directly.
76616416Sdyson		 * If the write is non-blocking, we don't use the
76716416Sdyson		 * direct write mechanism.
76813907Sdyson		 */
76917163Sdyson		if ((uio->uio_iov->iov_len >= PIPE_MINDIRECT) &&
77017163Sdyson		    (fp->f_flag & FNONBLOCK) == 0 &&
77117163Sdyson			(wpipe->pipe_map.kva || (amountpipekva < LIMITPIPEKVA)) &&
77213907Sdyson			(uio->uio_iov->iov_len >= PIPE_MINDIRECT)) {
77313907Sdyson			error = pipe_direct_write( wpipe, uio);
77413907Sdyson			if (error) {
77513907Sdyson				break;
77613907Sdyson			}
77713907Sdyson			continue;
77813907Sdyson		}
77914037Sdyson#endif
78013907Sdyson
78113907Sdyson		/*
78213907Sdyson		 * Pipe buffered writes cannot be coincidental with
78313907Sdyson		 * direct writes.  We wait until the currently executing
78413907Sdyson		 * direct write is completed before we start filling the
78513907Sdyson		 * pipe buffer.
78613907Sdyson		 */
78713907Sdyson	retrywrite:
78813907Sdyson		while (wpipe->pipe_state & PIPE_DIRECTW) {
78913992Sdyson			if (wpipe->pipe_state & PIPE_WANTR) {
79013992Sdyson				wpipe->pipe_state &= ~PIPE_WANTR;
79113992Sdyson				wakeup(wpipe);
79213992Sdyson			}
79313907Sdyson			error = tsleep(wpipe,
79413907Sdyson					PRIBIO|PCATCH, "pipbww", 0);
79513907Sdyson			if (error)
79613907Sdyson				break;
79713907Sdyson		}
79813907Sdyson
79913907Sdyson		space = wpipe->pipe_buffer.size - wpipe->pipe_buffer.cnt;
80014644Sdyson
80114644Sdyson		/* Writes of size <= PIPE_BUF must be atomic. */
80214644Sdyson		/* XXX perhaps they need to be contiguous to be atomic? */
80313913Sdyson		if ((space < uio->uio_resid) && (orig_resid <= PIPE_BUF))
80413913Sdyson			space = 0;
80513907Sdyson
80617163Sdyson		if (space > 0 && (wpipe->pipe_buffer.cnt < PIPE_SIZE)) {
80717163Sdyson			/*
80817163Sdyson			 * This set the maximum transfer as a segment of
80917163Sdyson			 * the buffer.
81017163Sdyson			 */
81113675Sdyson			int size = wpipe->pipe_buffer.size - wpipe->pipe_buffer.in;
81217163Sdyson			/*
81317163Sdyson			 * space is the size left in the buffer
81417163Sdyson			 */
81513675Sdyson			if (size > space)
81613675Sdyson				size = space;
81717163Sdyson			/*
81817163Sdyson			 * now limit it to the size of the uio transfer
81917163Sdyson			 */
82013675Sdyson			if (size > uio->uio_resid)
82113675Sdyson				size = uio->uio_resid;
82213907Sdyson			if ((error = pipelock(wpipe,1)) == 0) {
82313907Sdyson				/*
82413907Sdyson				 * It is possible for a direct write to
82513907Sdyson				 * slip in on us... handle it here...
82613907Sdyson				 */
82713907Sdyson				if (wpipe->pipe_state & PIPE_DIRECTW) {
82813907Sdyson					pipeunlock(wpipe);
82913907Sdyson					goto retrywrite;
83013907Sdyson				}
83113675Sdyson				error = uiomove( &wpipe->pipe_buffer.buffer[wpipe->pipe_buffer.in],
83213675Sdyson					size, uio);
83313675Sdyson				pipeunlock(wpipe);
83413675Sdyson			}
83513675Sdyson			if (error)
83613675Sdyson				break;
83713675Sdyson
83813675Sdyson			wpipe->pipe_buffer.in += size;
83913675Sdyson			if (wpipe->pipe_buffer.in >= wpipe->pipe_buffer.size)
84013675Sdyson				wpipe->pipe_buffer.in = 0;
84113675Sdyson
84213675Sdyson			wpipe->pipe_buffer.cnt += size;
84313675Sdyson		} else {
84413675Sdyson			/*
84513675Sdyson			 * If the "read-side" has been blocked, wake it up now.
84613675Sdyson			 */
84713675Sdyson			if (wpipe->pipe_state & PIPE_WANTR) {
84813675Sdyson				wpipe->pipe_state &= ~PIPE_WANTR;
84913675Sdyson				wakeup(wpipe);
85013675Sdyson			}
85114037Sdyson
85213675Sdyson			/*
85313675Sdyson			 * don't block on non-blocking I/O
85413675Sdyson			 */
85516960Sdyson			if (fp->f_flag & FNONBLOCK) {
85613907Sdyson				error = EAGAIN;
85713675Sdyson				break;
85813675Sdyson			}
85913907Sdyson
86014037Sdyson			/*
86114037Sdyson			 * We have no more space and have something to offer,
86229356Speter			 * wake up select/poll.
86314037Sdyson			 */
86414037Sdyson			pipeselwakeup(wpipe);
86514037Sdyson
86613675Sdyson			wpipe->pipe_state |= PIPE_WANTW;
86743301Sdillon			if ((error = tsleep(wpipe, (PRIBIO+1)|PCATCH, "pipewr", 0)) != 0) {
86813675Sdyson				break;
86913675Sdyson			}
87013675Sdyson			/*
87113675Sdyson			 * If read side wants to go away, we just issue a signal
87213675Sdyson			 * to ourselves.
87313675Sdyson			 */
87413675Sdyson			if (wpipe->pipe_state & PIPE_EOF) {
87513774Sdyson				error = EPIPE;
87613907Sdyson				break;
87713675Sdyson			}
87813675Sdyson		}
87913675Sdyson	}
88013675Sdyson
88114644Sdyson	--wpipe->pipe_busy;
88213675Sdyson	if ((wpipe->pipe_busy == 0) &&
88313675Sdyson		(wpipe->pipe_state & PIPE_WANT)) {
88413675Sdyson		wpipe->pipe_state &= ~(PIPE_WANT|PIPE_WANTR);
88513675Sdyson		wakeup(wpipe);
88613675Sdyson	} else if (wpipe->pipe_buffer.cnt > 0) {
88713675Sdyson		/*
88813675Sdyson		 * If we have put any characters in the buffer, we wake up
88913675Sdyson		 * the reader.
89013675Sdyson		 */
89113675Sdyson		if (wpipe->pipe_state & PIPE_WANTR) {
89213675Sdyson			wpipe->pipe_state &= ~PIPE_WANTR;
89313675Sdyson			wakeup(wpipe);
89413675Sdyson		}
89513675Sdyson	}
89613909Sdyson
89713909Sdyson	/*
89813909Sdyson	 * Don't return EPIPE if I/O was successful
89913909Sdyson	 */
90013907Sdyson	if ((wpipe->pipe_buffer.cnt == 0) &&
90113907Sdyson		(uio->uio_resid == 0) &&
90213907Sdyson		(error == EPIPE))
90313907Sdyson		error = 0;
90413913Sdyson
90524101Sbde	if (error == 0)
90634901Sphk		getnanotime(&wpipe->pipe_mtime);
90724101Sbde
90814037Sdyson	/*
90914037Sdyson	 * We have something to offer,
91029356Speter	 * wake up select/poll.
91114037Sdyson	 */
91214177Sdyson	if (wpipe->pipe_buffer.cnt)
91314037Sdyson		pipeselwakeup(wpipe);
91413907Sdyson
91513675Sdyson	return error;
91613675Sdyson}
91713675Sdyson
91813675Sdyson/*
91913675Sdyson * we implement a very minimal set of ioctls for compatibility with sockets.
92013675Sdyson */
92113675Sdysonint
92213675Sdysonpipe_ioctl(fp, cmd, data, p)
92313675Sdyson	struct file *fp;
92436735Sdfr	u_long cmd;
92513675Sdyson	register caddr_t data;
92613675Sdyson	struct proc *p;
92713675Sdyson{
92813675Sdyson	register struct pipe *mpipe = (struct pipe *)fp->f_data;
92913675Sdyson
93013675Sdyson	switch (cmd) {
93113675Sdyson
93213675Sdyson	case FIONBIO:
93313675Sdyson		return (0);
93413675Sdyson
93513675Sdyson	case FIOASYNC:
93613675Sdyson		if (*(int *)data) {
93713675Sdyson			mpipe->pipe_state |= PIPE_ASYNC;
93813675Sdyson		} else {
93913675Sdyson			mpipe->pipe_state &= ~PIPE_ASYNC;
94013675Sdyson		}
94113675Sdyson		return (0);
94213675Sdyson
94313675Sdyson	case FIONREAD:
94414037Sdyson		if (mpipe->pipe_state & PIPE_DIRECTW)
94514037Sdyson			*(int *)data = mpipe->pipe_map.cnt;
94614037Sdyson		else
94714037Sdyson			*(int *)data = mpipe->pipe_buffer.cnt;
94813675Sdyson		return (0);
94913675Sdyson
95041086Struckman	case FIOSETOWN:
95141086Struckman		return (fsetown(*(int *)data, &mpipe->pipe_sigio));
95241086Struckman
95341086Struckman	case FIOGETOWN:
95441086Struckman		*(int *)data = fgetown(mpipe->pipe_sigio);
95513675Sdyson		return (0);
95613675Sdyson
95741086Struckman	/* This is deprecated, FIOSETOWN should be used instead. */
95841086Struckman	case TIOCSPGRP:
95941086Struckman		return (fsetown(-(*(int *)data), &mpipe->pipe_sigio));
96041086Struckman
96141086Struckman	/* This is deprecated, FIOGETOWN should be used instead. */
96218863Sdyson	case TIOCGPGRP:
96341086Struckman		*(int *)data = -fgetown(mpipe->pipe_sigio);
96413675Sdyson		return (0);
96513675Sdyson
96613675Sdyson	}
96717124Sbde	return (ENOTTY);
96813675Sdyson}
96913675Sdyson
97013675Sdysonint
97129356Speterpipe_poll(fp, events, cred, p)
97213675Sdyson	struct file *fp;
97329356Speter	int events;
97429356Speter	struct ucred *cred;
97513675Sdyson	struct proc *p;
97613675Sdyson{
97713675Sdyson	register struct pipe *rpipe = (struct pipe *)fp->f_data;
97813675Sdyson	struct pipe *wpipe;
97929356Speter	int revents = 0;
98013675Sdyson
98113675Sdyson	wpipe = rpipe->pipe_peer;
98229356Speter	if (events & (POLLIN | POLLRDNORM))
98329356Speter		if ((rpipe->pipe_state & PIPE_DIRECTW) ||
98429356Speter		    (rpipe->pipe_buffer.cnt > 0) ||
98529356Speter		    (rpipe->pipe_state & PIPE_EOF))
98629356Speter			revents |= events & (POLLIN | POLLRDNORM);
98713675Sdyson
98829356Speter	if (events & (POLLOUT | POLLWRNORM))
98929356Speter		if (wpipe == NULL || (wpipe->pipe_state & PIPE_EOF) ||
99043311Sdillon		    (((wpipe->pipe_state & PIPE_DIRECTW) == 0) &&
99143311Sdillon		     (wpipe->pipe_buffer.size - wpipe->pipe_buffer.cnt) >= PIPE_BUF))
99229356Speter			revents |= events & (POLLOUT | POLLWRNORM);
99313675Sdyson
99429356Speter	if ((rpipe->pipe_state & PIPE_EOF) ||
99529356Speter	    (wpipe == NULL) ||
99629356Speter	    (wpipe->pipe_state & PIPE_EOF))
99729356Speter		revents |= POLLHUP;
99829356Speter
99929356Speter	if (revents == 0) {
100029356Speter		if (events & (POLLIN | POLLRDNORM)) {
100129356Speter			selrecord(p, &rpipe->pipe_sel);
100229356Speter			rpipe->pipe_state |= PIPE_SEL;
100313675Sdyson		}
100413675Sdyson
100529356Speter		if (events & (POLLOUT | POLLWRNORM)) {
100630164Speter			selrecord(p, &wpipe->pipe_sel);
100730164Speter			wpipe->pipe_state |= PIPE_SEL;
100813907Sdyson		}
100913675Sdyson	}
101029356Speter
101129356Speter	return (revents);
101213675Sdyson}
101313675Sdyson
101452983Speterstatic int
101552983Speterpipe_stat(fp, ub, p)
101652983Speter	struct file *fp;
101752983Speter	struct stat *ub;
101852983Speter	struct proc *p;
101913675Sdyson{
102052983Speter	struct pipe *pipe = (struct pipe *)fp->f_data;
102152983Speter
102213675Sdyson	bzero((caddr_t)ub, sizeof (*ub));
102317124Sbde	ub->st_mode = S_IFIFO;
102413907Sdyson	ub->st_blksize = pipe->pipe_buffer.size;
102513675Sdyson	ub->st_size = pipe->pipe_buffer.cnt;
102613675Sdyson	ub->st_blocks = (ub->st_size + ub->st_blksize - 1) / ub->st_blksize;
102734901Sphk	ub->st_atimespec = pipe->pipe_atime;
102834901Sphk	ub->st_mtimespec = pipe->pipe_mtime;
102934901Sphk	ub->st_ctimespec = pipe->pipe_ctime;
103017124Sbde	/*
103117124Sbde	 * Left as 0: st_dev, st_ino, st_nlink, st_uid, st_gid, st_rdev,
103217124Sbde	 * st_flags, st_gen.
103317124Sbde	 * XXX (st_dev, st_ino) should be unique.
103417124Sbde	 */
103513675Sdyson	return 0;
103613675Sdyson}
103713675Sdyson
103813675Sdyson/* ARGSUSED */
103913675Sdysonstatic int
104013675Sdysonpipe_close(fp, p)
104113675Sdyson	struct file *fp;
104213675Sdyson	struct proc *p;
104313675Sdyson{
104413675Sdyson	struct pipe *cpipe = (struct pipe *)fp->f_data;
104516322Sgpalmer
104649413Sgreen	fp->f_ops = &badfileops;
104749413Sgreen	fp->f_data = NULL;
104841086Struckman	funsetown(cpipe->pipe_sigio);
104913675Sdyson	pipeclose(cpipe);
105013675Sdyson	return 0;
105113675Sdyson}
105213675Sdyson
105313675Sdyson/*
105413675Sdyson * shutdown the pipe
105513675Sdyson */
105613675Sdysonstatic void
105713675Sdysonpipeclose(cpipe)
105813675Sdyson	struct pipe *cpipe;
105913675Sdyson{
106013907Sdyson	struct pipe *ppipe;
106113675Sdyson	if (cpipe) {
106213907Sdyson
106314037Sdyson		pipeselwakeup(cpipe);
106413907Sdyson
106513675Sdyson		/*
106613675Sdyson		 * If the other side is blocked, wake it up saying that
106713675Sdyson		 * we want to close it down.
106813675Sdyson		 */
106913675Sdyson		while (cpipe->pipe_busy) {
107013675Sdyson			wakeup(cpipe);
107113675Sdyson			cpipe->pipe_state |= PIPE_WANT|PIPE_EOF;
107213675Sdyson			tsleep(cpipe, PRIBIO, "pipecl", 0);
107313675Sdyson		}
107413675Sdyson
107513675Sdyson		/*
107613675Sdyson		 * Disconnect from peer
107713675Sdyson		 */
107843301Sdillon		if ((ppipe = cpipe->pipe_peer) != NULL) {
107914037Sdyson			pipeselwakeup(ppipe);
108013907Sdyson
108113907Sdyson			ppipe->pipe_state |= PIPE_EOF;
108213907Sdyson			wakeup(ppipe);
108313907Sdyson			ppipe->pipe_peer = NULL;
108413675Sdyson		}
108513675Sdyson
108613675Sdyson		/*
108713675Sdyson		 * free resources
108813675Sdyson		 */
108913907Sdyson		if (cpipe->pipe_buffer.buffer) {
109017163Sdyson			if (cpipe->pipe_buffer.size > PIPE_SIZE)
109117163Sdyson				--nbigpipe;
109213907Sdyson			amountpipekva -= cpipe->pipe_buffer.size;
109313907Sdyson			kmem_free(kernel_map,
109413907Sdyson				(vm_offset_t)cpipe->pipe_buffer.buffer,
109513907Sdyson				cpipe->pipe_buffer.size);
109613907Sdyson		}
109714037Sdyson#ifndef PIPE_NODIRECT
109813907Sdyson		if (cpipe->pipe_map.kva) {
109913912Sdyson			amountpipekva -= cpipe->pipe_buffer.size + PAGE_SIZE;
110013907Sdyson			kmem_free(kernel_map,
110113907Sdyson				cpipe->pipe_map.kva,
110213912Sdyson				cpipe->pipe_buffer.size + PAGE_SIZE);
110313907Sdyson		}
110414037Sdyson#endif
110527899Sdyson		zfree(pipe_zone, cpipe);
110613675Sdyson	}
110713675Sdyson}
1108