sys_pipe.c revision 34924
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 * $Id: sys_pipe.c,v 1.40 1998/03/26 20:51:47 phk Exp $
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/*
30 * This code has two modes of operation, a small write mode and a large
31 * write mode.  The small write mode acts like conventional pipes with
32 * a kernel buffer.  If the buffer is less than PIPE_MINDIRECT, then the
33 * "normal" pipe buffering is done.  If the buffer is between PIPE_MINDIRECT
34 * and PIPE_SIZE in size, it is fully mapped and wired into the kernel, and
35 * the receiving process can copy it directly from the pages in the sending
36 * process.
37 *
38 * If the sending process receives a signal, it is possible that it will
39 * go away, and certainly its address space can change, because control
40 * is returned back to the user-mode side.  In that case, the pipe code
41 * arranges to copy the buffer supplied by the user process, to a pageable
42 * kernel buffer, and the receiving process will grab the data from the
43 * pageable kernel buffer.  Since signals don't happen all that often,
44 * the copy operation is normally eliminated.
45 *
46 * The constant PIPE_MINDIRECT is chosen to make sure that buffering will
47 * happen for small transfers so that the system will not spend all of
48 * its time context switching.  PIPE_SIZE is constrained by the
49 * amount of kernel virtual memory.
50 */
51
52#include <sys/param.h>
53#include <sys/systm.h>
54#include <sys/proc.h>
55#include <sys/fcntl.h>
56#include <sys/file.h>
57#include <sys/filedesc.h>
58#include <sys/filio.h>
59#include <sys/ttycom.h>
60#include <sys/stat.h>
61#include <sys/poll.h>
62#include <sys/signalvar.h>
63#include <sys/sysproto.h>
64#include <sys/pipe.h>
65#include <sys/uio.h>
66
67#include <vm/vm.h>
68#include <vm/vm_prot.h>
69#include <vm/vm_param.h>
70#include <sys/lock.h>
71#include <vm/vm_object.h>
72#include <vm/vm_kern.h>
73#include <vm/vm_extern.h>
74#include <vm/pmap.h>
75#include <vm/vm_map.h>
76#include <vm/vm_page.h>
77#include <vm/vm_zone.h>
78
79/*
80 * Use this define if you want to disable *fancy* VM things.  Expect an
81 * approx 30% decrease in transfer rate.  This could be useful for
82 * NetBSD or OpenBSD.
83 */
84/* #define PIPE_NODIRECT */
85
86/*
87 * interfaces to the outside world
88 */
89static int pipe_read __P((struct file *fp, struct uio *uio,
90		struct ucred *cred));
91static int pipe_write __P((struct file *fp, struct uio *uio,
92		struct ucred *cred));
93static int pipe_close __P((struct file *fp, struct proc *p));
94static int pipe_poll __P((struct file *fp, int events, struct ucred *cred,
95		struct proc *p));
96static int pipe_ioctl __P((struct file *fp, int cmd, caddr_t data, struct proc *p));
97
98static struct fileops pipeops =
99    { pipe_read, pipe_write, pipe_ioctl, pipe_poll, pipe_close };
100
101/*
102 * Default pipe buffer size(s), this can be kind-of large now because pipe
103 * space is pageable.  The pipe code will try to maintain locality of
104 * reference for performance reasons, so small amounts of outstanding I/O
105 * will not wipe the cache.
106 */
107#define MINPIPESIZE (PIPE_SIZE/3)
108#define MAXPIPESIZE (2*PIPE_SIZE/3)
109
110/*
111 * Maximum amount of kva for pipes -- this is kind-of a soft limit, but
112 * is there so that on large systems, we don't exhaust it.
113 */
114#define MAXPIPEKVA (8*1024*1024)
115
116/*
117 * Limit for direct transfers, we cannot, of course limit
118 * the amount of kva for pipes in general though.
119 */
120#define LIMITPIPEKVA (16*1024*1024)
121
122/*
123 * Limit the number of "big" pipes
124 */
125#define LIMITBIGPIPES	32
126static int nbigpipe;
127
128static int amountpipekva;
129
130static void pipeclose __P((struct pipe *cpipe));
131static void pipeinit __P((struct pipe *cpipe));
132static __inline int pipelock __P((struct pipe *cpipe, int catch));
133static __inline void pipeunlock __P((struct pipe *cpipe));
134static __inline void pipeselwakeup __P((struct pipe *cpipe));
135#ifndef PIPE_NODIRECT
136static int pipe_build_write_buffer __P((struct pipe *wpipe, struct uio *uio));
137static void pipe_destroy_write_buffer __P((struct pipe *wpipe));
138static int pipe_direct_write __P((struct pipe *wpipe, struct uio *uio));
139static void pipe_clone_write_buffer __P((struct pipe *wpipe));
140#endif
141static void pipespace __P((struct pipe *cpipe));
142
143static vm_zone_t pipe_zone;
144
145/*
146 * The pipe system call for the DTYPE_PIPE type of pipes
147 */
148
149/* ARGSUSED */
150int
151pipe(p, uap)
152	struct proc *p;
153	struct pipe_args /* {
154		int	dummy;
155	} */ *uap;
156{
157	register struct filedesc *fdp = p->p_fd;
158	struct file *rf, *wf;
159	struct pipe *rpipe, *wpipe;
160	int fd, error;
161
162	if (pipe_zone == NULL)
163		pipe_zone = zinit("PIPE", sizeof (struct pipe), 0, 0, 4);
164
165	rpipe = zalloc( pipe_zone);
166	pipeinit(rpipe);
167	rpipe->pipe_state |= PIPE_DIRECTOK;
168	wpipe = zalloc( pipe_zone);
169	pipeinit(wpipe);
170	wpipe->pipe_state |= PIPE_DIRECTOK;
171
172	error = falloc(p, &rf, &fd);
173	if (error)
174		goto free2;
175	p->p_retval[0] = fd;
176	rf->f_flag = FREAD | FWRITE;
177	rf->f_type = DTYPE_PIPE;
178	rf->f_ops = &pipeops;
179	rf->f_data = (caddr_t)rpipe;
180	error = falloc(p, &wf, &fd);
181	if (error)
182		goto free3;
183	wf->f_flag = FREAD | FWRITE;
184	wf->f_type = DTYPE_PIPE;
185	wf->f_ops = &pipeops;
186	wf->f_data = (caddr_t)wpipe;
187	p->p_retval[1] = fd;
188
189	rpipe->pipe_peer = wpipe;
190	wpipe->pipe_peer = rpipe;
191
192	return (0);
193free3:
194	ffree(rf);
195	fdp->fd_ofiles[p->p_retval[0]] = 0;
196free2:
197	(void)pipeclose(wpipe);
198	(void)pipeclose(rpipe);
199	return (error);
200}
201
202/*
203 * Allocate kva for pipe circular buffer, the space is pageable
204 */
205static void
206pipespace(cpipe)
207	struct pipe *cpipe;
208{
209	int npages, error;
210
211	npages = round_page(cpipe->pipe_buffer.size)/PAGE_SIZE;
212	/*
213	 * Create an object, I don't like the idea of paging to/from
214	 * kernel_object.
215	 * XXX -- minor change needed here for NetBSD/OpenBSD VM systems.
216	 */
217	cpipe->pipe_buffer.object = vm_object_allocate(OBJT_DEFAULT, npages);
218	cpipe->pipe_buffer.buffer = (caddr_t) vm_map_min(kernel_map);
219
220	/*
221	 * Insert the object into the kernel map, and allocate kva for it.
222	 * The map entry is, by default, pageable.
223	 * XXX -- minor change needed here for NetBSD/OpenBSD VM systems.
224	 */
225	error = vm_map_find(kernel_map, cpipe->pipe_buffer.object, 0,
226		(vm_offset_t *) &cpipe->pipe_buffer.buffer,
227		cpipe->pipe_buffer.size, 1,
228		VM_PROT_ALL, VM_PROT_ALL, 0);
229
230	if (error != KERN_SUCCESS)
231		panic("pipeinit: cannot allocate pipe -- out of kvm -- code = %d", error);
232	amountpipekva += cpipe->pipe_buffer.size;
233}
234
235/*
236 * initialize and allocate VM and memory for pipe
237 */
238static void
239pipeinit(cpipe)
240	struct pipe *cpipe;
241{
242
243	cpipe->pipe_buffer.in = 0;
244	cpipe->pipe_buffer.out = 0;
245	cpipe->pipe_buffer.cnt = 0;
246	cpipe->pipe_buffer.size = PIPE_SIZE;
247
248	/* Buffer kva gets dynamically allocated */
249	cpipe->pipe_buffer.buffer = NULL;
250	/* cpipe->pipe_buffer.object = invalid */
251
252	cpipe->pipe_state = 0;
253	cpipe->pipe_peer = NULL;
254	cpipe->pipe_busy = 0;
255	getnanotime(&cpipe->pipe_ctime);
256	cpipe->pipe_atime = cpipe->pipe_ctime;
257	cpipe->pipe_mtime = cpipe->pipe_ctime;
258	bzero(&cpipe->pipe_sel, sizeof cpipe->pipe_sel);
259	cpipe->pipe_pgid = NO_PID;
260
261#ifndef PIPE_NODIRECT
262	/*
263	 * pipe data structure initializations to support direct pipe I/O
264	 */
265	cpipe->pipe_map.cnt = 0;
266	cpipe->pipe_map.kva = 0;
267	cpipe->pipe_map.pos = 0;
268	cpipe->pipe_map.npages = 0;
269	/* cpipe->pipe_map.ms[] = invalid */
270#endif
271}
272
273
274/*
275 * lock a pipe for I/O, blocking other access
276 */
277static __inline int
278pipelock(cpipe, catch)
279	struct pipe *cpipe;
280	int catch;
281{
282	int error;
283	while (cpipe->pipe_state & PIPE_LOCK) {
284		cpipe->pipe_state |= PIPE_LWANT;
285		if (error = tsleep( cpipe,
286			catch?(PRIBIO|PCATCH):PRIBIO, "pipelk", 0)) {
287			return error;
288		}
289	}
290	cpipe->pipe_state |= PIPE_LOCK;
291	return 0;
292}
293
294/*
295 * unlock a pipe I/O lock
296 */
297static __inline void
298pipeunlock(cpipe)
299	struct pipe *cpipe;
300{
301	cpipe->pipe_state &= ~PIPE_LOCK;
302	if (cpipe->pipe_state & PIPE_LWANT) {
303		cpipe->pipe_state &= ~PIPE_LWANT;
304		wakeup(cpipe);
305	}
306}
307
308static __inline void
309pipeselwakeup(cpipe)
310	struct pipe *cpipe;
311{
312	struct proc *p;
313
314	if (cpipe->pipe_state & PIPE_SEL) {
315		cpipe->pipe_state &= ~PIPE_SEL;
316		selwakeup(&cpipe->pipe_sel);
317	}
318	if (cpipe->pipe_state & PIPE_ASYNC) {
319		if (cpipe->pipe_pgid < 0)
320			gsignal(-cpipe->pipe_pgid, SIGIO);
321		else if ((p = pfind(cpipe->pipe_pgid)) != NULL)
322			psignal(p, SIGIO);
323	}
324}
325
326/* ARGSUSED */
327static int
328pipe_read(fp, uio, cred)
329	struct file *fp;
330	struct uio *uio;
331	struct ucred *cred;
332{
333
334	struct pipe *rpipe = (struct pipe *) fp->f_data;
335	int error = 0;
336	int nread = 0;
337	u_int size;
338
339	++rpipe->pipe_busy;
340	while (uio->uio_resid) {
341		/*
342		 * normal pipe buffer receive
343		 */
344		if (rpipe->pipe_buffer.cnt > 0) {
345			size = rpipe->pipe_buffer.size - rpipe->pipe_buffer.out;
346			if (size > rpipe->pipe_buffer.cnt)
347				size = rpipe->pipe_buffer.cnt;
348			if (size > (u_int) uio->uio_resid)
349				size = (u_int) uio->uio_resid;
350			if ((error = pipelock(rpipe,1)) == 0) {
351				error = uiomove( &rpipe->pipe_buffer.buffer[rpipe->pipe_buffer.out],
352					size, uio);
353				pipeunlock(rpipe);
354			}
355			if (error) {
356				break;
357			}
358			rpipe->pipe_buffer.out += size;
359			if (rpipe->pipe_buffer.out >= rpipe->pipe_buffer.size)
360				rpipe->pipe_buffer.out = 0;
361
362			rpipe->pipe_buffer.cnt -= size;
363			nread += size;
364#ifndef PIPE_NODIRECT
365		/*
366		 * Direct copy, bypassing a kernel buffer.
367		 */
368		} else if ((size = rpipe->pipe_map.cnt) &&
369			(rpipe->pipe_state & PIPE_DIRECTW)) {
370			caddr_t va;
371			if (size > (u_int) uio->uio_resid)
372				size = (u_int) uio->uio_resid;
373			if ((error = pipelock(rpipe,1)) == 0) {
374				va = (caddr_t) rpipe->pipe_map.kva + rpipe->pipe_map.pos;
375				error = uiomove(va, size, uio);
376				pipeunlock(rpipe);
377			}
378			if (error)
379				break;
380			nread += size;
381			rpipe->pipe_map.pos += size;
382			rpipe->pipe_map.cnt -= size;
383			if (rpipe->pipe_map.cnt == 0) {
384				rpipe->pipe_state &= ~PIPE_DIRECTW;
385				wakeup(rpipe);
386			}
387#endif
388		} else {
389			/*
390			 * detect EOF condition
391			 */
392			if (rpipe->pipe_state & PIPE_EOF) {
393				/* XXX error = ? */
394				break;
395			}
396			/*
397			 * If the "write-side" has been blocked, wake it up now.
398			 */
399			if (rpipe->pipe_state & PIPE_WANTW) {
400				rpipe->pipe_state &= ~PIPE_WANTW;
401				wakeup(rpipe);
402			}
403			if (nread > 0)
404				break;
405
406			if (fp->f_flag & FNONBLOCK) {
407				error = EAGAIN;
408				break;
409			}
410
411			/*
412			 * If there is no more to read in the pipe, reset
413			 * its pointers to the beginning.  This improves
414			 * cache hit stats.
415			 */
416
417			if ((error = pipelock(rpipe,1)) == 0) {
418				if (rpipe->pipe_buffer.cnt == 0) {
419					rpipe->pipe_buffer.in = 0;
420					rpipe->pipe_buffer.out = 0;
421				}
422				pipeunlock(rpipe);
423			} else {
424				break;
425			}
426
427			if (rpipe->pipe_state & PIPE_WANTW) {
428				rpipe->pipe_state &= ~PIPE_WANTW;
429				wakeup(rpipe);
430			}
431
432			rpipe->pipe_state |= PIPE_WANTR;
433			if (error = tsleep(rpipe, PRIBIO|PCATCH, "piperd", 0)) {
434				break;
435			}
436		}
437	}
438
439	if (error == 0)
440		getnanotime(&rpipe->pipe_atime);
441
442	--rpipe->pipe_busy;
443	if ((rpipe->pipe_busy == 0) && (rpipe->pipe_state & PIPE_WANT)) {
444		rpipe->pipe_state &= ~(PIPE_WANT|PIPE_WANTW);
445		wakeup(rpipe);
446	} else if (rpipe->pipe_buffer.cnt < MINPIPESIZE) {
447		/*
448		 * If there is no more to read in the pipe, reset
449		 * its pointers to the beginning.  This improves
450		 * cache hit stats.
451		 */
452		if (rpipe->pipe_buffer.cnt == 0) {
453			if ((error == 0) && (error = pipelock(rpipe,1)) == 0) {
454				rpipe->pipe_buffer.in = 0;
455				rpipe->pipe_buffer.out = 0;
456				pipeunlock(rpipe);
457			}
458		}
459
460		/*
461		 * If the "write-side" has been blocked, wake it up now.
462		 */
463		if (rpipe->pipe_state & PIPE_WANTW) {
464			rpipe->pipe_state &= ~PIPE_WANTW;
465			wakeup(rpipe);
466		}
467	}
468
469	if ((rpipe->pipe_buffer.size - rpipe->pipe_buffer.cnt) >= PIPE_BUF)
470		pipeselwakeup(rpipe);
471
472	return error;
473}
474
475#ifndef PIPE_NODIRECT
476/*
477 * Map the sending processes' buffer into kernel space and wire it.
478 * This is similar to a physical write operation.
479 */
480static int
481pipe_build_write_buffer(wpipe, uio)
482	struct pipe *wpipe;
483	struct uio *uio;
484{
485	u_int size;
486	int i;
487	vm_offset_t addr, endaddr, paddr;
488
489	size = (u_int) uio->uio_iov->iov_len;
490	if (size > wpipe->pipe_buffer.size)
491		size = wpipe->pipe_buffer.size;
492
493	endaddr = round_page(uio->uio_iov->iov_base + size);
494	for(i = 0, addr = trunc_page(uio->uio_iov->iov_base);
495		addr < endaddr;
496		addr += PAGE_SIZE, i+=1) {
497
498		vm_page_t m;
499
500		vm_fault_quick( (caddr_t) addr, VM_PROT_READ);
501		paddr = pmap_kextract(addr);
502		if (!paddr) {
503			int j;
504			for(j=0;j<i;j++)
505				vm_page_unwire(wpipe->pipe_map.ms[j]);
506			return EFAULT;
507		}
508
509		m = PHYS_TO_VM_PAGE(paddr);
510		vm_page_wire(m);
511		wpipe->pipe_map.ms[i] = m;
512	}
513
514/*
515 * set up the control block
516 */
517	wpipe->pipe_map.npages = i;
518	wpipe->pipe_map.pos = ((vm_offset_t) uio->uio_iov->iov_base) & PAGE_MASK;
519	wpipe->pipe_map.cnt = size;
520
521/*
522 * and map the buffer
523 */
524	if (wpipe->pipe_map.kva == 0) {
525		/*
526		 * We need to allocate space for an extra page because the
527		 * address range might (will) span pages at times.
528		 */
529		wpipe->pipe_map.kva = kmem_alloc_pageable(kernel_map,
530			wpipe->pipe_buffer.size + PAGE_SIZE);
531		amountpipekva += wpipe->pipe_buffer.size + PAGE_SIZE;
532	}
533	pmap_qenter(wpipe->pipe_map.kva, wpipe->pipe_map.ms,
534		wpipe->pipe_map.npages);
535
536/*
537 * and update the uio data
538 */
539
540	uio->uio_iov->iov_len -= size;
541	uio->uio_iov->iov_base += size;
542	if (uio->uio_iov->iov_len == 0)
543		uio->uio_iov++;
544	uio->uio_resid -= size;
545	uio->uio_offset += size;
546	return 0;
547}
548
549/*
550 * unmap and unwire the process buffer
551 */
552static void
553pipe_destroy_write_buffer(wpipe)
554struct pipe *wpipe;
555{
556	int i;
557	if (wpipe->pipe_map.kva) {
558		pmap_qremove(wpipe->pipe_map.kva, wpipe->pipe_map.npages);
559
560		if (amountpipekva > MAXPIPEKVA) {
561			vm_offset_t kva = wpipe->pipe_map.kva;
562			wpipe->pipe_map.kva = 0;
563			kmem_free(kernel_map, kva,
564				wpipe->pipe_buffer.size + PAGE_SIZE);
565			amountpipekva -= wpipe->pipe_buffer.size + PAGE_SIZE;
566		}
567	}
568	for (i=0;i<wpipe->pipe_map.npages;i++)
569		vm_page_unwire(wpipe->pipe_map.ms[i]);
570}
571
572/*
573 * In the case of a signal, the writing process might go away.  This
574 * code copies the data into the circular buffer so that the source
575 * pages can be freed without loss of data.
576 */
577static void
578pipe_clone_write_buffer(wpipe)
579struct pipe *wpipe;
580{
581	int size;
582	int pos;
583
584	size = wpipe->pipe_map.cnt;
585	pos = wpipe->pipe_map.pos;
586	bcopy((caddr_t) wpipe->pipe_map.kva+pos,
587			(caddr_t) wpipe->pipe_buffer.buffer,
588			size);
589
590	wpipe->pipe_buffer.in = size;
591	wpipe->pipe_buffer.out = 0;
592	wpipe->pipe_buffer.cnt = size;
593	wpipe->pipe_state &= ~PIPE_DIRECTW;
594
595	pipe_destroy_write_buffer(wpipe);
596}
597
598/*
599 * This implements the pipe buffer write mechanism.  Note that only
600 * a direct write OR a normal pipe write can be pending at any given time.
601 * If there are any characters in the pipe buffer, the direct write will
602 * be deferred until the receiving process grabs all of the bytes from
603 * the pipe buffer.  Then the direct mapping write is set-up.
604 */
605static int
606pipe_direct_write(wpipe, uio)
607	struct pipe *wpipe;
608	struct uio *uio;
609{
610	int error;
611retry:
612	while (wpipe->pipe_state & PIPE_DIRECTW) {
613		if ( wpipe->pipe_state & PIPE_WANTR) {
614			wpipe->pipe_state &= ~PIPE_WANTR;
615			wakeup(wpipe);
616		}
617		wpipe->pipe_state |= PIPE_WANTW;
618		error = tsleep(wpipe,
619				PRIBIO|PCATCH, "pipdww", 0);
620		if (error)
621			goto error1;
622		if (wpipe->pipe_state & PIPE_EOF) {
623			error = EPIPE;
624			goto error1;
625		}
626	}
627	wpipe->pipe_map.cnt = 0;	/* transfer not ready yet */
628	if (wpipe->pipe_buffer.cnt > 0) {
629		if ( wpipe->pipe_state & PIPE_WANTR) {
630			wpipe->pipe_state &= ~PIPE_WANTR;
631			wakeup(wpipe);
632		}
633
634		wpipe->pipe_state |= PIPE_WANTW;
635		error = tsleep(wpipe,
636				PRIBIO|PCATCH, "pipdwc", 0);
637		if (error)
638			goto error1;
639		if (wpipe->pipe_state & PIPE_EOF) {
640			error = EPIPE;
641			goto error1;
642		}
643		goto retry;
644	}
645
646	wpipe->pipe_state |= PIPE_DIRECTW;
647
648	error = pipe_build_write_buffer(wpipe, uio);
649	if (error) {
650		wpipe->pipe_state &= ~PIPE_DIRECTW;
651		goto error1;
652	}
653
654	error = 0;
655	while (!error && (wpipe->pipe_state & PIPE_DIRECTW)) {
656		if (wpipe->pipe_state & PIPE_EOF) {
657			pipelock(wpipe, 0);
658			pipe_destroy_write_buffer(wpipe);
659			pipeunlock(wpipe);
660			pipeselwakeup(wpipe);
661			error = EPIPE;
662			goto error1;
663		}
664		if (wpipe->pipe_state & PIPE_WANTR) {
665			wpipe->pipe_state &= ~PIPE_WANTR;
666			wakeup(wpipe);
667		}
668		pipeselwakeup(wpipe);
669		error = tsleep(wpipe, PRIBIO|PCATCH, "pipdwt", 0);
670	}
671
672	pipelock(wpipe,0);
673	if (wpipe->pipe_state & PIPE_DIRECTW) {
674		/*
675		 * this bit of trickery substitutes a kernel buffer for
676		 * the process that might be going away.
677		 */
678		pipe_clone_write_buffer(wpipe);
679	} else {
680		pipe_destroy_write_buffer(wpipe);
681	}
682	pipeunlock(wpipe);
683	return error;
684
685error1:
686	wakeup(wpipe);
687	return error;
688}
689#endif
690
691static int
692pipe_write(fp, uio, cred)
693	struct file *fp;
694	struct uio *uio;
695	struct ucred *cred;
696{
697	int error = 0;
698	int orig_resid;
699
700	struct pipe *wpipe, *rpipe;
701
702	rpipe = (struct pipe *) fp->f_data;
703	wpipe = rpipe->pipe_peer;
704
705	/*
706	 * detect loss of pipe read side, issue SIGPIPE if lost.
707	 */
708	if ((wpipe == NULL) || (wpipe->pipe_state & PIPE_EOF)) {
709		return EPIPE;
710	}
711
712	/*
713	 * If it is advantageous to resize the pipe buffer, do
714	 * so.
715	 */
716	if ((uio->uio_resid > PIPE_SIZE) &&
717		(nbigpipe < LIMITBIGPIPES) &&
718		(wpipe->pipe_state & PIPE_DIRECTW) == 0 &&
719		(wpipe->pipe_buffer.size <= PIPE_SIZE) &&
720		(wpipe->pipe_buffer.cnt == 0)) {
721
722		if (wpipe->pipe_buffer.buffer) {
723			amountpipekva -= wpipe->pipe_buffer.size;
724			kmem_free(kernel_map,
725				(vm_offset_t)wpipe->pipe_buffer.buffer,
726				wpipe->pipe_buffer.size);
727		}
728
729#ifndef PIPE_NODIRECT
730		if (wpipe->pipe_map.kva) {
731			amountpipekva -= wpipe->pipe_buffer.size + PAGE_SIZE;
732			kmem_free(kernel_map,
733				wpipe->pipe_map.kva,
734				wpipe->pipe_buffer.size + PAGE_SIZE);
735		}
736#endif
737
738		wpipe->pipe_buffer.in = 0;
739		wpipe->pipe_buffer.out = 0;
740		wpipe->pipe_buffer.cnt = 0;
741		wpipe->pipe_buffer.size = BIG_PIPE_SIZE;
742		wpipe->pipe_buffer.buffer = NULL;
743		++nbigpipe;
744
745#ifndef PIPE_NODIRECT
746		wpipe->pipe_map.cnt = 0;
747		wpipe->pipe_map.kva = 0;
748		wpipe->pipe_map.pos = 0;
749		wpipe->pipe_map.npages = 0;
750#endif
751
752	}
753
754
755	if( wpipe->pipe_buffer.buffer == NULL) {
756		if ((error = pipelock(wpipe,1)) == 0) {
757			pipespace(wpipe);
758			pipeunlock(wpipe);
759		} else {
760			return error;
761		}
762	}
763
764	++wpipe->pipe_busy;
765	orig_resid = uio->uio_resid;
766	while (uio->uio_resid) {
767		int space;
768#ifndef PIPE_NODIRECT
769		/*
770		 * If the transfer is large, we can gain performance if
771		 * we do process-to-process copies directly.
772		 * If the write is non-blocking, we don't use the
773		 * direct write mechanism.
774		 */
775		if ((uio->uio_iov->iov_len >= PIPE_MINDIRECT) &&
776		    (fp->f_flag & FNONBLOCK) == 0 &&
777			(wpipe->pipe_map.kva || (amountpipekva < LIMITPIPEKVA)) &&
778			(uio->uio_iov->iov_len >= PIPE_MINDIRECT)) {
779			error = pipe_direct_write( wpipe, uio);
780			if (error) {
781				break;
782			}
783			continue;
784		}
785#endif
786
787		/*
788		 * Pipe buffered writes cannot be coincidental with
789		 * direct writes.  We wait until the currently executing
790		 * direct write is completed before we start filling the
791		 * pipe buffer.
792		 */
793	retrywrite:
794		while (wpipe->pipe_state & PIPE_DIRECTW) {
795			if (wpipe->pipe_state & PIPE_WANTR) {
796				wpipe->pipe_state &= ~PIPE_WANTR;
797				wakeup(wpipe);
798			}
799			error = tsleep(wpipe,
800					PRIBIO|PCATCH, "pipbww", 0);
801			if (error)
802				break;
803		}
804
805		space = wpipe->pipe_buffer.size - wpipe->pipe_buffer.cnt;
806
807		/* Writes of size <= PIPE_BUF must be atomic. */
808		/* XXX perhaps they need to be contiguous to be atomic? */
809		if ((space < uio->uio_resid) && (orig_resid <= PIPE_BUF))
810			space = 0;
811
812		if (space > 0 && (wpipe->pipe_buffer.cnt < PIPE_SIZE)) {
813			/*
814			 * This set the maximum transfer as a segment of
815			 * the buffer.
816			 */
817			int size = wpipe->pipe_buffer.size - wpipe->pipe_buffer.in;
818			/*
819			 * space is the size left in the buffer
820			 */
821			if (size > space)
822				size = space;
823			/*
824			 * now limit it to the size of the uio transfer
825			 */
826			if (size > uio->uio_resid)
827				size = uio->uio_resid;
828			if ((error = pipelock(wpipe,1)) == 0) {
829				/*
830				 * It is possible for a direct write to
831				 * slip in on us... handle it here...
832				 */
833				if (wpipe->pipe_state & PIPE_DIRECTW) {
834					pipeunlock(wpipe);
835					goto retrywrite;
836				}
837				error = uiomove( &wpipe->pipe_buffer.buffer[wpipe->pipe_buffer.in],
838					size, uio);
839				pipeunlock(wpipe);
840			}
841			if (error)
842				break;
843
844			wpipe->pipe_buffer.in += size;
845			if (wpipe->pipe_buffer.in >= wpipe->pipe_buffer.size)
846				wpipe->pipe_buffer.in = 0;
847
848			wpipe->pipe_buffer.cnt += size;
849		} else {
850			/*
851			 * If the "read-side" has been blocked, wake it up now.
852			 */
853			if (wpipe->pipe_state & PIPE_WANTR) {
854				wpipe->pipe_state &= ~PIPE_WANTR;
855				wakeup(wpipe);
856			}
857
858			/*
859			 * don't block on non-blocking I/O
860			 */
861			if (fp->f_flag & FNONBLOCK) {
862				error = EAGAIN;
863				break;
864			}
865
866			/*
867			 * We have no more space and have something to offer,
868			 * wake up select/poll.
869			 */
870			pipeselwakeup(wpipe);
871
872			wpipe->pipe_state |= PIPE_WANTW;
873			if (error = tsleep(wpipe, (PRIBIO+1)|PCATCH, "pipewr", 0)) {
874				break;
875			}
876			/*
877			 * If read side wants to go away, we just issue a signal
878			 * to ourselves.
879			 */
880			if (wpipe->pipe_state & PIPE_EOF) {
881				error = EPIPE;
882				break;
883			}
884		}
885	}
886
887	--wpipe->pipe_busy;
888	if ((wpipe->pipe_busy == 0) &&
889		(wpipe->pipe_state & PIPE_WANT)) {
890		wpipe->pipe_state &= ~(PIPE_WANT|PIPE_WANTR);
891		wakeup(wpipe);
892	} else if (wpipe->pipe_buffer.cnt > 0) {
893		/*
894		 * If we have put any characters in the buffer, we wake up
895		 * the reader.
896		 */
897		if (wpipe->pipe_state & PIPE_WANTR) {
898			wpipe->pipe_state &= ~PIPE_WANTR;
899			wakeup(wpipe);
900		}
901	}
902
903	/*
904	 * Don't return EPIPE if I/O was successful
905	 */
906	if ((wpipe->pipe_buffer.cnt == 0) &&
907		(uio->uio_resid == 0) &&
908		(error == EPIPE))
909		error = 0;
910
911	if (error == 0)
912		getnanotime(&wpipe->pipe_mtime);
913
914	/*
915	 * We have something to offer,
916	 * wake up select/poll.
917	 */
918	if (wpipe->pipe_buffer.cnt)
919		pipeselwakeup(wpipe);
920
921	return error;
922}
923
924/*
925 * we implement a very minimal set of ioctls for compatibility with sockets.
926 */
927int
928pipe_ioctl(fp, cmd, data, p)
929	struct file *fp;
930	int cmd;
931	register caddr_t data;
932	struct proc *p;
933{
934	register struct pipe *mpipe = (struct pipe *)fp->f_data;
935
936	switch (cmd) {
937
938	case FIONBIO:
939		return (0);
940
941	case FIOASYNC:
942		if (*(int *)data) {
943			mpipe->pipe_state |= PIPE_ASYNC;
944		} else {
945			mpipe->pipe_state &= ~PIPE_ASYNC;
946		}
947		return (0);
948
949	case FIONREAD:
950		if (mpipe->pipe_state & PIPE_DIRECTW)
951			*(int *)data = mpipe->pipe_map.cnt;
952		else
953			*(int *)data = mpipe->pipe_buffer.cnt;
954		return (0);
955
956	case TIOCSPGRP:
957		mpipe->pipe_pgid = *(int *)data;
958		return (0);
959
960	case TIOCGPGRP:
961		*(int *)data = mpipe->pipe_pgid;
962		return (0);
963
964	}
965	return (ENOTTY);
966}
967
968int
969pipe_poll(fp, events, cred, p)
970	struct file *fp;
971	int events;
972	struct ucred *cred;
973	struct proc *p;
974{
975	register struct pipe *rpipe = (struct pipe *)fp->f_data;
976	struct pipe *wpipe;
977	int revents = 0;
978
979	wpipe = rpipe->pipe_peer;
980	if (events & (POLLIN | POLLRDNORM))
981		if ((rpipe->pipe_state & PIPE_DIRECTW) ||
982		    (rpipe->pipe_buffer.cnt > 0) ||
983		    (rpipe->pipe_state & PIPE_EOF))
984			revents |= events & (POLLIN | POLLRDNORM);
985
986	if (events & (POLLOUT | POLLWRNORM))
987		if (wpipe == NULL || (wpipe->pipe_state & PIPE_EOF) ||
988		    ((wpipe->pipe_state & PIPE_DIRECTW) == 0) &&
989		     (wpipe->pipe_buffer.size - wpipe->pipe_buffer.cnt) >= PIPE_BUF)
990			revents |= events & (POLLOUT | POLLWRNORM);
991
992	if ((rpipe->pipe_state & PIPE_EOF) ||
993	    (wpipe == NULL) ||
994	    (wpipe->pipe_state & PIPE_EOF))
995		revents |= POLLHUP;
996
997	if (revents == 0) {
998		if (events & (POLLIN | POLLRDNORM)) {
999			selrecord(p, &rpipe->pipe_sel);
1000			rpipe->pipe_state |= PIPE_SEL;
1001		}
1002
1003		if (events & (POLLOUT | POLLWRNORM)) {
1004			selrecord(p, &wpipe->pipe_sel);
1005			wpipe->pipe_state |= PIPE_SEL;
1006		}
1007	}
1008
1009	return (revents);
1010}
1011
1012int
1013pipe_stat(pipe, ub)
1014	register struct pipe *pipe;
1015	register struct stat *ub;
1016{
1017	bzero((caddr_t)ub, sizeof (*ub));
1018	ub->st_mode = S_IFIFO;
1019	ub->st_blksize = pipe->pipe_buffer.size;
1020	ub->st_size = pipe->pipe_buffer.cnt;
1021	ub->st_blocks = (ub->st_size + ub->st_blksize - 1) / ub->st_blksize;
1022	ub->st_atimespec = pipe->pipe_atime;
1023	ub->st_mtimespec = pipe->pipe_mtime;
1024	ub->st_ctimespec = pipe->pipe_ctime;
1025	/*
1026	 * Left as 0: st_dev, st_ino, st_nlink, st_uid, st_gid, st_rdev,
1027	 * st_flags, st_gen.
1028	 * XXX (st_dev, st_ino) should be unique.
1029	 */
1030	return 0;
1031}
1032
1033/* ARGSUSED */
1034static int
1035pipe_close(fp, p)
1036	struct file *fp;
1037	struct proc *p;
1038{
1039	struct pipe *cpipe = (struct pipe *)fp->f_data;
1040
1041	pipeclose(cpipe);
1042	fp->f_data = NULL;
1043	return 0;
1044}
1045
1046/*
1047 * shutdown the pipe
1048 */
1049static void
1050pipeclose(cpipe)
1051	struct pipe *cpipe;
1052{
1053	struct pipe *ppipe;
1054	if (cpipe) {
1055
1056		pipeselwakeup(cpipe);
1057
1058		/*
1059		 * If the other side is blocked, wake it up saying that
1060		 * we want to close it down.
1061		 */
1062		while (cpipe->pipe_busy) {
1063			wakeup(cpipe);
1064			cpipe->pipe_state |= PIPE_WANT|PIPE_EOF;
1065			tsleep(cpipe, PRIBIO, "pipecl", 0);
1066		}
1067
1068		/*
1069		 * Disconnect from peer
1070		 */
1071		if (ppipe = cpipe->pipe_peer) {
1072			pipeselwakeup(ppipe);
1073
1074			ppipe->pipe_state |= PIPE_EOF;
1075			wakeup(ppipe);
1076			ppipe->pipe_peer = NULL;
1077		}
1078
1079		/*
1080		 * free resources
1081		 */
1082		if (cpipe->pipe_buffer.buffer) {
1083			if (cpipe->pipe_buffer.size > PIPE_SIZE)
1084				--nbigpipe;
1085			amountpipekva -= cpipe->pipe_buffer.size;
1086			kmem_free(kernel_map,
1087				(vm_offset_t)cpipe->pipe_buffer.buffer,
1088				cpipe->pipe_buffer.size);
1089		}
1090#ifndef PIPE_NODIRECT
1091		if (cpipe->pipe_map.kva) {
1092			amountpipekva -= cpipe->pipe_buffer.size + PAGE_SIZE;
1093			kmem_free(kernel_map,
1094				cpipe->pipe_map.kva,
1095				cpipe->pipe_buffer.size + PAGE_SIZE);
1096		}
1097#endif
1098		zfree(pipe_zone, cpipe);
1099	}
1100}
1101