Home | History | Annotate | Line # | Download | only in kern
sys_pipe.c revision 1.165
      1  1.165        ad /*	$NetBSD: sys_pipe.c,v 1.165 2023/10/13 19:07:08 ad Exp $	*/
      2   1.35        pk 
      3   1.35        pk /*-
      4  1.163        ad  * Copyright (c) 2003, 2007, 2008, 2009, 2023 The NetBSD Foundation, Inc.
      5   1.35        pk  * All rights reserved.
      6   1.35        pk  *
      7   1.35        pk  * This code is derived from software contributed to The NetBSD Foundation
      8   1.80        ad  * by Paul Kranenburg, and by Andrew Doran.
      9   1.35        pk  *
     10   1.35        pk  * Redistribution and use in source and binary forms, with or without
     11   1.35        pk  * modification, are permitted provided that the following conditions
     12   1.35        pk  * are met:
     13   1.35        pk  * 1. Redistributions of source code must retain the above copyright
     14   1.35        pk  *    notice, this list of conditions and the following disclaimer.
     15   1.35        pk  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.35        pk  *    notice, this list of conditions and the following disclaimer in the
     17   1.35        pk  *    documentation and/or other materials provided with the distribution.
     18   1.35        pk  *
     19   1.35        pk  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20   1.35        pk  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21   1.35        pk  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22   1.35        pk  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23   1.35        pk  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24   1.35        pk  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25   1.35        pk  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26   1.35        pk  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27   1.35        pk  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28   1.35        pk  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29   1.35        pk  * POSSIBILITY OF SUCH DAMAGE.
     30   1.35        pk  */
     31    1.2  jdolecek 
     32    1.1  jdolecek /*
     33    1.1  jdolecek  * Copyright (c) 1996 John S. Dyson
     34    1.1  jdolecek  * All rights reserved.
     35    1.1  jdolecek  *
     36    1.1  jdolecek  * Redistribution and use in source and binary forms, with or without
     37    1.1  jdolecek  * modification, are permitted provided that the following conditions
     38    1.1  jdolecek  * are met:
     39    1.1  jdolecek  * 1. Redistributions of source code must retain the above copyright
     40    1.1  jdolecek  *    notice immediately at the beginning of the file, without modification,
     41    1.1  jdolecek  *    this list of conditions, and the following disclaimer.
     42    1.1  jdolecek  * 2. Redistributions in binary form must reproduce the above copyright
     43    1.1  jdolecek  *    notice, this list of conditions and the following disclaimer in the
     44    1.1  jdolecek  *    documentation and/or other materials provided with the distribution.
     45    1.1  jdolecek  * 3. Absolutely no warranty of function or purpose is made by the author
     46    1.1  jdolecek  *    John S. Dyson.
     47    1.1  jdolecek  * 4. Modifications may be freely made to this file if the above conditions
     48    1.1  jdolecek  *    are met.
     49    1.1  jdolecek  */
     50    1.1  jdolecek 
     51    1.1  jdolecek /*
     52    1.1  jdolecek  * This file contains a high-performance replacement for the socket-based
     53  1.106        ad  * pipes scheme originally used.  It does not support all features of
     54  1.106        ad  * sockets, but does do everything that pipes normally do.
     55    1.1  jdolecek  */
     56   1.19     lukem 
     57   1.19     lukem #include <sys/cdefs.h>
     58  1.165        ad __KERNEL_RCSID(0, "$NetBSD: sys_pipe.c,v 1.165 2023/10/13 19:07:08 ad Exp $");
     59    1.2  jdolecek 
     60    1.1  jdolecek #include <sys/param.h>
     61    1.1  jdolecek #include <sys/systm.h>
     62    1.2  jdolecek #include <sys/proc.h>
     63    1.1  jdolecek #include <sys/fcntl.h>
     64    1.1  jdolecek #include <sys/file.h>
     65    1.1  jdolecek #include <sys/filedesc.h>
     66    1.1  jdolecek #include <sys/filio.h>
     67   1.24  jdolecek #include <sys/kernel.h>
     68    1.1  jdolecek #include <sys/ttycom.h>
     69    1.1  jdolecek #include <sys/stat.h>
     70    1.1  jdolecek #include <sys/poll.h>
     71    1.2  jdolecek #include <sys/signalvar.h>
     72    1.2  jdolecek #include <sys/vnode.h>
     73    1.2  jdolecek #include <sys/uio.h>
     74    1.2  jdolecek #include <sys/select.h>
     75    1.2  jdolecek #include <sys/mount.h>
     76    1.2  jdolecek #include <sys/syscallargs.h>
     77    1.2  jdolecek #include <sys/sysctl.h>
     78   1.72      elad #include <sys/kauth.h>
     79   1.90        ad #include <sys/atomic.h>
     80   1.90        ad #include <sys/pipe.h>
     81    1.2  jdolecek 
     82  1.113     rmind static int	pipe_read(file_t *, off_t *, struct uio *, kauth_cred_t, int);
     83  1.113     rmind static int	pipe_write(file_t *, off_t *, struct uio *, kauth_cred_t, int);
     84  1.113     rmind static int	pipe_close(file_t *);
     85  1.113     rmind static int	pipe_poll(file_t *, int);
     86  1.114     rmind static int	pipe_kqfilter(file_t *, struct knote *);
     87  1.113     rmind static int	pipe_stat(file_t *, struct stat *);
     88  1.113     rmind static int	pipe_ioctl(file_t *, u_long, void *);
     89  1.127       dsl static void	pipe_restart(file_t *);
     90  1.159  riastrad static int	pipe_fpathconf(file_t *, int, register_t *);
     91  1.160  riastrad static int	pipe_posix_fadvise(file_t *, off_t, off_t, int);
     92    1.1  jdolecek 
     93   1.62  christos static const struct fileops pipeops = {
     94  1.142  christos 	.fo_name = "pipe",
     95  1.109        ad 	.fo_read = pipe_read,
     96  1.109        ad 	.fo_write = pipe_write,
     97  1.109        ad 	.fo_ioctl = pipe_ioctl,
     98  1.109        ad 	.fo_fcntl = fnullop_fcntl,
     99  1.109        ad 	.fo_poll = pipe_poll,
    100  1.109        ad 	.fo_stat = pipe_stat,
    101  1.109        ad 	.fo_close = pipe_close,
    102  1.109        ad 	.fo_kqfilter = pipe_kqfilter,
    103  1.127       dsl 	.fo_restart = pipe_restart,
    104  1.159  riastrad 	.fo_fpathconf = pipe_fpathconf,
    105  1.160  riastrad 	.fo_posix_fadvise = pipe_posix_fadvise,
    106   1.35        pk };
    107    1.1  jdolecek 
    108    1.1  jdolecek /*
    109    1.1  jdolecek  * Default pipe buffer size(s), this can be kind-of large now because pipe
    110    1.1  jdolecek  * space is pageable.  The pipe code will try to maintain locality of
    111    1.1  jdolecek  * reference for performance reasons, so small amounts of outstanding I/O
    112    1.1  jdolecek  * will not wipe the cache.
    113    1.1  jdolecek  */
    114  1.113     rmind #define	MINPIPESIZE	(PIPE_SIZE / 3)
    115  1.113     rmind #define	MAXPIPESIZE	(2 * PIPE_SIZE / 3)
    116    1.1  jdolecek 
    117    1.1  jdolecek /*
    118    1.1  jdolecek  * Limit the number of "big" pipes
    119    1.1  jdolecek  */
    120  1.113     rmind #define	LIMITBIGPIPES	32
    121  1.163        ad static u_int	maxbigpipes __read_mostly = LIMITBIGPIPES;
    122  1.113     rmind static u_int	nbigpipe = 0;
    123    1.1  jdolecek 
    124    1.2  jdolecek /*
    125    1.2  jdolecek  * Amount of KVA consumed by pipe buffers.
    126    1.2  jdolecek  */
    127  1.113     rmind static u_int	amountpipekva = 0;
    128   1.34   thorpej 
    129  1.165        ad static bool	pipebusy(struct pipe *);
    130  1.165        ad static bool	pipeunbusy(struct pipe *);
    131  1.165        ad static void	pipeselwakeup(struct pipe *, int, int);
    132  1.113     rmind static int	pipe_ctor(void *, void *, int);
    133  1.113     rmind static void	pipe_dtor(void *, void *);
    134    1.2  jdolecek 
    135  1.165        ad static pool_cache_t	pipe_cache __read_mostly;
    136   1.82        ad 
    137   1.82        ad void
    138   1.82        ad pipe_init(void)
    139   1.82        ad {
    140   1.82        ad 
    141  1.165        ad 	pipe_cache = pool_cache_init(sizeof(struct pipe), COHERENCY_UNIT, 0, 0,
    142  1.165        ad 	    "pipe", NULL, IPL_NONE, pipe_ctor, pipe_dtor, NULL);
    143  1.165        ad 	KASSERT(pipe_cache != NULL);
    144   1.90        ad }
    145   1.90        ad 
    146   1.90        ad static int
    147  1.106        ad pipe_ctor(void *arg, void *obj, int flags)
    148   1.90        ad {
    149  1.165        ad 	struct pipe *pipe = obj;
    150   1.90        ad 
    151  1.106        ad 	memset(pipe, 0, sizeof(struct pipe));
    152  1.165        ad 	pipe->pipe_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
    153  1.165        ad 	cv_init(&pipe->pipe_read, "piperd");
    154  1.165        ad 	cv_init(&pipe->pipe_write, "pipewr");
    155  1.165        ad 	cv_init(&pipe->pipe_busy, "pipebusy");
    156  1.165        ad 	selinit(&pipe->pipe_rdsel);
    157  1.165        ad 	selinit(&pipe->pipe_wrsel);
    158  1.165        ad 	pipe->pipe_kmem = uvm_km_alloc(kernel_map, PIPE_SIZE, 0,
    159  1.165        ad 	    UVM_KMF_PAGEABLE | UVM_KMF_WAITVA);
    160  1.165        ad 	pipe->pipe_state = PIPE_SIGNALR | PIPE_RDOPEN | PIPE_WROPEN;
    161  1.165        ad 	pipe->pipe_buffer.buffer = (void *)pipe->pipe_kmem;
    162  1.165        ad 	pipe->pipe_buffer.size = PIPE_SIZE;
    163  1.165        ad 	KASSERT(pipe->pipe_kmem != 0);
    164  1.165        ad 	atomic_add_int(&amountpipekva, PIPE_SIZE);
    165   1.90        ad 
    166   1.90        ad 	return 0;
    167   1.90        ad }
    168   1.90        ad 
    169   1.90        ad static void
    170  1.106        ad pipe_dtor(void *arg, void *obj)
    171   1.90        ad {
    172  1.165        ad 	struct pipe *pipe = obj;
    173   1.90        ad 
    174  1.165        ad 	cv_destroy(&pipe->pipe_read);
    175  1.165        ad 	cv_destroy(&pipe->pipe_write);
    176  1.165        ad 	cv_destroy(&pipe->pipe_busy);
    177  1.165        ad 	seldestroy(&pipe->pipe_rdsel);
    178  1.165        ad 	seldestroy(&pipe->pipe_wrsel);
    179  1.165        ad 	mutex_obj_free(pipe->pipe_lock);
    180  1.165        ad 	uvm_km_free(kernel_map, pipe->pipe_kmem, PIPE_SIZE, UVM_KMF_PAGEABLE);
    181  1.165        ad 	atomic_add_int(&amountpipekva, -PIPE_SIZE);
    182   1.82        ad }
    183   1.82        ad 
    184    1.1  jdolecek /*
    185    1.1  jdolecek  * The pipe system call for the DTYPE_PIPE type of pipes
    186    1.1  jdolecek  */
    187    1.2  jdolecek int
    188  1.143     kamil pipe1(struct lwp *l, int *fildes, int flags)
    189    1.1  jdolecek {
    190  1.165        ad 	struct pipe *pipe;
    191  1.113     rmind 	file_t *rf, *wf;
    192    1.1  jdolecek 	int fd, error;
    193   1.99        ad 	proc_t *p;
    194    1.2  jdolecek 
    195  1.135  christos 	if (flags & ~(O_CLOEXEC|O_NONBLOCK|O_NOSIGPIPE))
    196  1.132  christos 		return EINVAL;
    197   1.99        ad 	p = curproc;
    198  1.165        ad 
    199  1.165        ad 	pipe = pool_cache_get(pipe_cache, PR_WAITOK);
    200  1.165        ad 	getnanotime(&pipe->pipe_atime);
    201  1.165        ad 	pipe->pipe_mtime = pipe->pipe_atime;
    202  1.165        ad 	pipe->pipe_btime = pipe->pipe_atime;
    203    1.6  jdolecek 
    204   1.99        ad 	error = fd_allocfile(&rf, &fd);
    205  1.165        ad 	if (error) {
    206  1.165        ad 		pool_cache_put(pipe_cache, pipe);
    207  1.165        ad 		return error;
    208  1.165        ad 	}
    209  1.143     kamil 	fildes[0] = fd;
    210  1.136    martin 
    211  1.136    martin 	error = fd_allocfile(&wf, &fd);
    212  1.165        ad 	if (error) {
    213  1.165        ad 		fd_abort(p, rf, fildes[0]);
    214  1.165        ad 		pool_cache_put(pipe_cache, pipe);
    215  1.165        ad 		return error;
    216  1.165        ad 	}
    217  1.143     kamil 	fildes[1] = fd;
    218  1.136    martin 
    219  1.130  christos 	rf->f_flag = FREAD | flags;
    220    1.2  jdolecek 	rf->f_type = DTYPE_PIPE;
    221  1.165        ad 	rf->f_pipe = pipe;
    222    1.2  jdolecek 	rf->f_ops = &pipeops;
    223  1.143     kamil 	fd_set_exclose(l, fildes[0], (flags & O_CLOEXEC) != 0);
    224    1.2  jdolecek 
    225  1.130  christos 	wf->f_flag = FWRITE | flags;
    226    1.2  jdolecek 	wf->f_type = DTYPE_PIPE;
    227  1.165        ad 	wf->f_pipe = pipe;
    228    1.2  jdolecek 	wf->f_ops = &pipeops;
    229  1.143     kamil 	fd_set_exclose(l, fildes[1], (flags & O_CLOEXEC) != 0);
    230    1.2  jdolecek 
    231  1.143     kamil 	fd_affix(p, rf, fildes[0]);
    232  1.143     kamil 	fd_affix(p, wf, fildes[1]);
    233  1.165        ad 	return 0;
    234    1.1  jdolecek }
    235    1.1  jdolecek 
    236    1.1  jdolecek /*
    237  1.165        ad  * Busy a pipe for I/O, blocking other access.  Called with pipe lock held.
    238  1.165        ad  * NB: curlwp may already hold the pipe busy.
    239    1.1  jdolecek  */
    240  1.165        ad static bool
    241  1.165        ad pipebusy(struct pipe *pipe)
    242    1.1  jdolecek {
    243  1.165        ad 	struct lwp *l = curlwp;
    244  1.165        ad 	bool blocked = false;
    245  1.106        ad 
    246  1.165        ad 	KASSERT(mutex_owned(pipe->pipe_lock));
    247  1.165        ad 
    248  1.165        ad 	if (pipe->pipe_owner != l) {
    249  1.165        ad 		while (__predict_false(pipe->pipe_owner != NULL)) {
    250  1.165        ad 			cv_wait(&pipe->pipe_busy, pipe->pipe_lock);
    251  1.165        ad 			blocked = true;
    252  1.165        ad 		}
    253  1.165        ad 		pipe->pipe_owner = l;
    254  1.106        ad 	}
    255    1.1  jdolecek 
    256  1.165        ad 	return blocked;
    257    1.1  jdolecek }
    258    1.1  jdolecek 
    259    1.1  jdolecek /*
    260  1.165        ad  * Unbusy a pipe for I/O, if held busy by curlwp.
    261    1.1  jdolecek  */
    262  1.165        ad static bool
    263  1.165        ad pipeunbusy(struct pipe *pipe)
    264    1.1  jdolecek {
    265    1.1  jdolecek 
    266  1.165        ad 	KASSERT(mutex_owned(pipe->pipe_lock));
    267  1.165        ad 
    268  1.165        ad 	if (pipe->pipe_owner == curlwp) {
    269  1.165        ad 		pipe->pipe_owner = NULL;
    270  1.165        ad 		return true;
    271  1.165        ad 	} else
    272  1.165        ad 		return false;
    273    1.1  jdolecek }
    274    1.1  jdolecek 
    275    1.1  jdolecek /*
    276  1.165        ad  * Select/poll wakeup.  This also sends SIGIO to peer.
    277    1.1  jdolecek  */
    278  1.165        ad static void
    279  1.165        ad pipeselwakeup(struct pipe *pipe, int side, int code)
    280    1.1  jdolecek {
    281  1.165        ad 	struct selinfo *selp;
    282  1.165        ad 	int band, flag;
    283  1.165        ad 	pid_t pgid;
    284    1.1  jdolecek 
    285   1.90        ad 	KASSERT(mutex_owned(pipe->pipe_lock));
    286   1.35        pk 
    287  1.165        ad 	if (side == FREAD) {
    288  1.165        ad 		selp = &pipe->pipe_rdsel;
    289  1.165        ad 		pgid = pipe->pipe_rdpgid;
    290  1.165        ad 		flag = PIPE_RDASYNC;
    291  1.165        ad 	} else {
    292  1.165        ad 		selp = &pipe->pipe_wrsel;
    293  1.165        ad 		pgid = pipe->pipe_wrpgid;
    294  1.165        ad 		flag = PIPE_WRASYNC;
    295    1.1  jdolecek 	}
    296   1.67      yamt 
    297   1.43  jdolecek 	switch (code) {
    298   1.42  christos 	case POLL_IN:
    299   1.43  jdolecek 		band = POLLIN|POLLRDNORM;
    300   1.42  christos 		break;
    301   1.42  christos 	case POLL_OUT:
    302   1.43  jdolecek 		band = POLLOUT|POLLWRNORM;
    303   1.42  christos 		break;
    304   1.42  christos 	case POLL_HUP:
    305   1.43  jdolecek 		band = POLLHUP;
    306   1.42  christos 		break;
    307   1.42  christos 	case POLL_ERR:
    308   1.43  jdolecek 		band = POLLERR;
    309   1.42  christos 		break;
    310   1.42  christos 	default:
    311   1.45  christos 		band = 0;
    312   1.42  christos #ifdef DIAGNOSTIC
    313   1.42  christos 		printf("bad siginfo code %d in pipe notification.\n", code);
    314   1.42  christos #endif
    315   1.42  christos 		break;
    316   1.42  christos 	}
    317   1.43  jdolecek 
    318  1.165        ad 	selnotify(selp, band, NOTE_SUBMIT);
    319   1.98     rmind 
    320  1.165        ad 	if (pgid != 0 && (pipe->pipe_state & flag) != 0)
    321  1.165        ad 		fownsignal(pgid, SIGIO, code, band, pipe);
    322    1.1  jdolecek }
    323    1.1  jdolecek 
    324    1.2  jdolecek static int
    325  1.113     rmind pipe_read(file_t *fp, off_t *offset, struct uio *uio, kauth_cred_t cred,
    326   1.77      yamt     int flags)
    327    1.1  jdolecek {
    328  1.165        ad 	struct pipe *pipe = fp->f_pipe;
    329  1.165        ad 	struct pipebuf *bp = &pipe->pipe_buffer;
    330  1.165        ad 	size_t size, cnt, ocnt, nread = 0;
    331  1.165        ad 	kmutex_t *lock = pipe->pipe_lock;
    332  1.165        ad 	int error = 0;
    333  1.165        ad 	bool unbusy;
    334    1.1  jdolecek 
    335  1.161        ad 	/*
    336  1.161        ad 	 * Try to avoid locking the pipe if we have nothing to do.
    337  1.161        ad 	 *
    338  1.161        ad 	 * There are programs which share one pipe amongst multiple processes
    339  1.161        ad 	 * and perform non-blocking reads in parallel, even if the pipe is
    340  1.161        ad 	 * empty.  This in particular is the case with BSD make, which when
    341  1.161        ad 	 * spawned with a high -j number can find itself with over half of the
    342  1.161        ad 	 * calls failing to find anything.
    343  1.161        ad 	 */
    344  1.161        ad 	if ((fp->f_flag & FNONBLOCK) != 0) {
    345  1.161        ad 		if (__predict_false(uio->uio_resid == 0))
    346  1.165        ad 			return 0;
    347  1.161        ad 		if (atomic_load_relaxed(&bp->cnt) == 0 &&
    348  1.165        ad 		    (atomic_load_relaxed(&pipe->pipe_state) & PIPE_EOF) == 0)
    349  1.165        ad 			return EAGAIN;
    350  1.161        ad 	}
    351  1.161        ad 
    352   1.95        ad 	mutex_enter(lock);
    353   1.35        pk 	ocnt = bp->cnt;
    354   1.28  jdolecek 
    355    1.1  jdolecek 	while (uio->uio_resid) {
    356    1.1  jdolecek 		/*
    357  1.113     rmind 		 * Normal pipe buffer receive.
    358    1.1  jdolecek 		 */
    359   1.35        pk 		if (bp->cnt > 0) {
    360  1.165        ad 			/* If pipebusy() blocked then re-validate. */
    361  1.165        ad 			if (pipebusy(pipe))
    362  1.165        ad 				continue;
    363   1.35        pk 			size = bp->size - bp->out;
    364   1.35        pk 			if (size > bp->cnt)
    365   1.35        pk 				size = bp->cnt;
    366    1.2  jdolecek 			if (size > uio->uio_resid)
    367    1.2  jdolecek 				size = uio->uio_resid;
    368    1.1  jdolecek 
    369  1.165        ad 			KASSERT(pipe->pipe_owner == curlwp);
    370   1.95        ad 			mutex_exit(lock);
    371   1.79  christos 			error = uiomove((char *)bp->buffer + bp->out, size, uio);
    372   1.95        ad 			mutex_enter(lock);
    373    1.1  jdolecek 			if (error)
    374    1.1  jdolecek 				break;
    375    1.1  jdolecek 
    376   1.35        pk 			bp->out += size;
    377   1.35        pk 			if (bp->out >= bp->size)
    378   1.35        pk 				bp->out = 0;
    379   1.35        pk 			bp->cnt -= size;
    380    1.1  jdolecek 
    381    1.1  jdolecek 			/*
    382    1.1  jdolecek 			 * If there is no more to read in the pipe, reset
    383    1.1  jdolecek 			 * its pointers to the beginning.  This improves
    384    1.1  jdolecek 			 * cache hit stats.
    385    1.1  jdolecek 			 */
    386   1.35        pk 			if (bp->cnt == 0) {
    387   1.35        pk 				bp->in = 0;
    388   1.35        pk 				bp->out = 0;
    389    1.1  jdolecek 			}
    390    1.1  jdolecek 			nread += size;
    391   1.85        ad 			continue;
    392   1.85        ad 		}
    393   1.85        ad 
    394   1.85        ad 		/*
    395   1.85        ad 		 * Break if some data was read.
    396   1.85        ad 		 */
    397   1.90        ad 		if (nread > 0)
    398   1.85        ad 			break;
    399    1.1  jdolecek 
    400   1.85        ad 		/*
    401  1.113     rmind 		 * Detect EOF condition.
    402  1.113     rmind 		 * Read returns 0 on EOF, no need to set error.
    403   1.85        ad 		 */
    404  1.165        ad 		if ((pipe->pipe_state & PIPE_EOF) != 0)
    405   1.85        ad 			break;
    406   1.36        pk 
    407   1.85        ad 		/*
    408  1.113     rmind 		 * Don't block on non-blocking I/O.
    409   1.85        ad 		 */
    410  1.165        ad 		if ((fp->f_flag & FNONBLOCK) != 0) {
    411   1.85        ad 			error = EAGAIN;
    412   1.85        ad 			break;
    413   1.85        ad 		}
    414    1.1  jdolecek 
    415   1.85        ad 		/*
    416  1.165        ad 		 * Awaken the other side (including select/poll/kqueue)
    417  1.165        ad 		 * then sleep ASAP to minimise contention.
    418   1.85        ad 		 */
    419  1.165        ad 		pipeselwakeup(pipe, FWRITE, POLL_OUT);
    420  1.165        ad 		if (pipeunbusy(pipe))
    421  1.165        ad 			cv_signal(&pipe->pipe_busy);
    422  1.165        ad 		cv_broadcast(&pipe->pipe_write);
    423  1.165        ad 		if ((error = cv_wait_sig(&pipe->pipe_read, lock)) != 0)
    424  1.165        ad 			break;
    425    1.1  jdolecek 	}
    426   1.35        pk 
    427  1.165        ad 	/*
    428  1.165        ad 	 * Update timestamp and drop the long term lock (if held).
    429  1.165        ad 	 */
    430   1.35        pk 	if (error == 0)
    431  1.165        ad 		getnanotime(&pipe->pipe_atime);
    432  1.165        ad 	unbusy = pipeunbusy(pipe);
    433    1.1  jdolecek 
    434    1.2  jdolecek 	/*
    435    1.2  jdolecek 	 * If anything was read off the buffer, signal to the writer it's
    436    1.2  jdolecek 	 * possible to write more data. Also send signal if we are here for the
    437    1.2  jdolecek 	 * first time after last write.
    438    1.2  jdolecek 	 */
    439  1.165        ad 	cnt = bp->cnt;
    440  1.165        ad 	if (bp->size - cnt >= PIPE_BUF
    441  1.165        ad 	    && (ocnt != cnt || (pipe->pipe_state & PIPE_SIGNALR) != 0)) {
    442  1.165        ad 		pipe->pipe_state &= ~PIPE_SIGNALR;
    443  1.165        ad 		pipeselwakeup(pipe, FWRITE, POLL_OUT);
    444    1.2  jdolecek 	}
    445    1.1  jdolecek 
    446  1.165        ad 	/*
    447  1.165        ad 	 * Release the mutex and only then wake the other side, to minimise
    448  1.165        ad 	 * contention.
    449  1.165        ad 	 */
    450   1.95        ad 	mutex_exit(lock);
    451  1.165        ad 	if (unbusy)
    452  1.165        ad 		cv_signal(&pipe->pipe_busy);
    453  1.165        ad 	if (cnt < MINPIPESIZE)
    454  1.165        ad 		cv_broadcast(&pipe->pipe_write);
    455  1.165        ad 
    456  1.165        ad 	return error;
    457    1.1  jdolecek }
    458    1.1  jdolecek 
    459    1.2  jdolecek static int
    460  1.113     rmind pipe_write(file_t *fp, off_t *offset, struct uio *uio, kauth_cred_t cred,
    461   1.77      yamt     int flags)
    462    1.1  jdolecek {
    463  1.165        ad 	struct pipe *pipe = fp->f_pipe;
    464  1.165        ad 	struct pipebuf *bp = &pipe->pipe_buffer;
    465  1.165        ad 	kmutex_t *lock = pipe->pipe_lock;
    466  1.165        ad 	size_t cnt, space, orig_resid = uio->uio_resid;
    467  1.165        ad 	bool unbusy;
    468   1.35        pk 	int error;
    469   1.35        pk 
    470    1.1  jdolecek 	/*
    471   1.35        pk 	 * If it is advantageous to resize the pipe buffer, do so.
    472    1.1  jdolecek 	 */
    473  1.165        ad 	mutex_enter(lock);
    474  1.165        ad 	if (uio->uio_resid > PIPE_SIZE &&
    475  1.165        ad 	    (pipe->pipe_state & PIPE_RESIZED) == 0 &&
    476  1.165        ad 	    nbigpipe < maxbigpipes && bp->cnt == 0) {
    477  1.165        ad 	    	size_t size = round_page(BIG_PIPE_SIZE);
    478  1.165        ad 		void *buffer = (void *)uvm_km_alloc(kernel_map, size,
    479  1.165        ad 		    0, UVM_KMF_PAGEABLE);
    480  1.165        ad 		if (buffer != NULL) {
    481  1.165        ad 			atomic_add_int(&amountpipekva, size);
    482   1.90        ad 			atomic_inc_uint(&nbigpipe);
    483  1.165        ad 			pipe->pipe_buffer.buffer = buffer;
    484  1.165        ad 			pipe->pipe_buffer.size = size;
    485  1.165        ad 			pipe->pipe_buffer.in = 0;
    486  1.165        ad 			pipe->pipe_buffer.out = 0;
    487  1.165        ad 			pipe->pipe_buffer.cnt = 0;
    488  1.165        ad 		}
    489  1.165        ad 		pipe->pipe_state |= PIPE_RESIZED;
    490   1.24  jdolecek 	}
    491    1.1  jdolecek 
    492  1.165        ad 	while (uio->uio_resid > 0) {
    493  1.165        ad 		/*
    494  1.165        ad 		 * If read side has gone away, we just issue a signal to
    495  1.165        ad 		 * ourselves.
    496  1.165        ad 		 */
    497  1.165        ad 		if ((pipe->pipe_state & PIPE_EOF) != 0) {
    498  1.165        ad 			error = EPIPE;
    499  1.165        ad 			break;
    500  1.165        ad 		}
    501    1.1  jdolecek 
    502  1.165        ad 		/* Writes of size <= PIPE_BUF must be atomic. */
    503   1.35        pk 		space = bp->size - bp->cnt;
    504  1.165        ad 		if (space < uio->uio_resid && uio->uio_resid <= PIPE_BUF)
    505    1.1  jdolecek 			space = 0;
    506    1.1  jdolecek 
    507   1.16   mycroft 		if (space > 0) {
    508    1.2  jdolecek 			int size;	/* Transfer size */
    509    1.2  jdolecek 			int segsize;	/* first segment to transfer */
    510    1.2  jdolecek 
    511  1.165        ad 			/* If pipebusy() blocked then re-validate. */
    512  1.165        ad 			if (pipebusy(pipe))
    513  1.165        ad 				continue;
    514  1.165        ad 
    515    1.2  jdolecek 			/*
    516    1.2  jdolecek 			 * Transfer size is minimum of uio transfer
    517    1.2  jdolecek 			 * and free space in pipe buffer.
    518    1.2  jdolecek 			 */
    519    1.2  jdolecek 			if (space > uio->uio_resid)
    520    1.2  jdolecek 				size = uio->uio_resid;
    521    1.2  jdolecek 			else
    522    1.2  jdolecek 				size = space;
    523    1.2  jdolecek 			/*
    524   1.63     perry 			 * First segment to transfer is minimum of
    525    1.2  jdolecek 			 * transfer size and contiguous space in
    526    1.2  jdolecek 			 * pipe buffer.  If first segment to transfer
    527    1.2  jdolecek 			 * is less than the transfer size, we've got
    528    1.2  jdolecek 			 * a wraparound in the buffer.
    529    1.2  jdolecek 			 */
    530   1.35        pk 			segsize = bp->size - bp->in;
    531    1.2  jdolecek 			if (segsize > size)
    532    1.2  jdolecek 				segsize = size;
    533   1.18       chs 
    534    1.2  jdolecek 			/* Transfer first segment */
    535  1.165        ad 			KASSERT(pipe->pipe_owner == curlwp);
    536   1.95        ad 			mutex_exit(lock);
    537   1.79  christos 			error = uiomove((char *)bp->buffer + bp->in, segsize,
    538   1.79  christos 			    uio);
    539   1.18       chs 
    540    1.2  jdolecek 			if (error == 0 && segsize < size) {
    541   1.63     perry 				/*
    542    1.2  jdolecek 				 * Transfer remaining part now, to
    543    1.2  jdolecek 				 * support atomic writes.  Wraparound
    544    1.2  jdolecek 				 * happened.
    545    1.2  jdolecek 				 */
    546  1.113     rmind 				KASSERT(bp->in + segsize == bp->size);
    547   1.79  christos 				error = uiomove(bp->buffer,
    548   1.79  christos 				    size - segsize, uio);
    549    1.2  jdolecek 			}
    550   1.95        ad 			mutex_enter(lock);
    551   1.35        pk 			if (error)
    552   1.35        pk 				break;
    553   1.35        pk 
    554   1.35        pk 			bp->in += size;
    555   1.35        pk 			if (bp->in >= bp->size) {
    556  1.113     rmind 				KASSERT(bp->in == size - segsize + bp->size);
    557   1.35        pk 				bp->in = size - segsize;
    558   1.35        pk 			}
    559   1.18       chs 
    560   1.35        pk 			bp->cnt += size;
    561  1.113     rmind 			KASSERT(bp->cnt <= bp->size);
    562  1.165        ad 			continue;
    563  1.165        ad 		}
    564    1.1  jdolecek 
    565  1.165        ad 		/*
    566  1.165        ad 		 * Don't block on non-blocking I/O.
    567  1.165        ad 		 */
    568  1.165        ad 		if ((fp->f_flag & FNONBLOCK) != 0) {
    569  1.165        ad 			error = EAGAIN;
    570  1.165        ad 			break;
    571    1.1  jdolecek 		}
    572    1.1  jdolecek 
    573  1.165        ad 		/*
    574  1.165        ad 		 * Awaken the other side (including select/poll/kqueue) then
    575  1.165        ad 		 * sleep ASAP to minimise contention.
    576  1.165        ad 		 */
    577  1.165        ad 		pipeselwakeup(pipe, FREAD, POLL_IN);
    578  1.165        ad 		if (pipeunbusy(pipe))
    579  1.165        ad 			cv_signal(&pipe->pipe_busy);
    580  1.165        ad 		cv_broadcast(&pipe->pipe_read);
    581  1.165        ad 		if ((error = cv_wait_sig(&pipe->pipe_write, lock)) != 0)
    582  1.165        ad 			break;
    583    1.1  jdolecek 	}
    584    1.1  jdolecek 
    585    1.1  jdolecek 	/*
    586    1.1  jdolecek 	 * Don't return EPIPE if I/O was successful
    587    1.1  jdolecek 	 */
    588  1.165        ad 	if (error == EPIPE && uio->uio_resid != orig_resid)
    589    1.1  jdolecek 		error = 0;
    590    1.1  jdolecek 
    591  1.165        ad 	/*
    592  1.165        ad 	 * Update timestamp and drop the long term lock (if held).
    593  1.165        ad 	 */
    594    1.1  jdolecek 	if (error == 0)
    595  1.165        ad 		getnanotime(&pipe->pipe_mtime);
    596  1.165        ad 	unbusy = pipeunbusy(pipe);
    597  1.165        ad 
    598  1.165        ad 	/*
    599  1.165        ad 	 * Arrange for next read(2) to do a signal.
    600  1.165        ad 	 */
    601  1.165        ad 	pipe->pipe_state |= PIPE_SIGNALR;
    602    1.1  jdolecek 
    603    1.1  jdolecek 	/*
    604    1.2  jdolecek 	 * We have something to offer, wake up select/poll.
    605    1.1  jdolecek 	 */
    606  1.165        ad 	if ((cnt = bp->cnt) > 0)
    607  1.165        ad 		pipeselwakeup(pipe, FREAD, POLL_IN);
    608    1.1  jdolecek 
    609    1.2  jdolecek 	/*
    610  1.165        ad 	 * Release the mutex then wake other side, to minimise contention.
    611    1.2  jdolecek 	 */
    612  1.165        ad 	mutex_exit(lock);
    613  1.165        ad 	if (unbusy)
    614  1.165        ad 		cv_signal(&pipe->pipe_busy);
    615  1.165        ad 	if (cnt > 0)
    616  1.165        ad 		cv_broadcast(&pipe->pipe_read);
    617    1.2  jdolecek 
    618  1.165        ad 	return error;
    619    1.1  jdolecek }
    620    1.1  jdolecek 
    621    1.1  jdolecek /*
    622  1.113     rmind  * We implement a very minimal set of ioctls for compatibility with sockets.
    623    1.1  jdolecek  */
    624    1.1  jdolecek int
    625  1.113     rmind pipe_ioctl(file_t *fp, u_long cmd, void *data)
    626    1.1  jdolecek {
    627  1.140      matt 	struct pipe *pipe = fp->f_pipe;
    628   1.95        ad 	kmutex_t *lock = pipe->pipe_lock;
    629  1.165        ad 	int flag;
    630    1.1  jdolecek 
    631    1.1  jdolecek 	switch (cmd) {
    632    1.1  jdolecek 	case FIONBIO:
    633  1.165        ad 		return 0;
    634    1.1  jdolecek 
    635    1.1  jdolecek 	case FIOASYNC:
    636  1.165        ad 		flag = (fp->f_flag & FREAD) != 0 ? PIPE_RDASYNC : PIPE_WRASYNC;
    637   1.95        ad 		mutex_enter(lock);
    638  1.165        ad 		if (*(int *)data)
    639  1.165        ad 			pipe->pipe_state |= flag;
    640  1.165        ad 		else
    641  1.165        ad 			pipe->pipe_state &= ~flag;
    642   1.95        ad 		mutex_exit(lock);
    643  1.165        ad 		return 0;
    644    1.1  jdolecek 
    645    1.1  jdolecek 	case FIONREAD:
    646  1.165        ad 		if ((fp->f_flag & FREAD) != 0)
    647  1.165        ad 			*(int *)data =
    648  1.165        ad 			    atomic_load_relaxed(&pipe->pipe_buffer.cnt);
    649  1.165        ad 		else
    650  1.165        ad 			*(int *)data = 0;
    651  1.165        ad 		return 0;
    652    1.1  jdolecek 
    653   1.59  wrstuden 	case FIONWRITE:
    654  1.165        ad 		if ((fp->f_flag & FWRITE) != 0)
    655  1.165        ad 			*(int *)data =
    656  1.165        ad 			    atomic_load_relaxed(&pipe->pipe_buffer.cnt);
    657  1.165        ad 		else
    658  1.148   mlelstv 			*(int *)data = 0;
    659   1.59  wrstuden 		return (0);
    660   1.59  wrstuden 
    661   1.59  wrstuden 	case FIONSPACE:
    662  1.165        ad 		if ((fp->f_flag & FWRITE) != 0) {
    663  1.165        ad 			mutex_enter(lock);
    664   1.59  wrstuden 			*(int *)data = pipe->pipe_buffer.size -
    665   1.82        ad 			    pipe->pipe_buffer.cnt;
    666  1.165        ad 			mutex_exit(lock);
    667  1.165        ad 		} else
    668  1.165        ad 			*(int *)data = 0;
    669   1.59  wrstuden 		return (0);
    670   1.59  wrstuden 
    671    1.2  jdolecek 	case TIOCSPGRP:
    672   1.43  jdolecek 	case FIOSETOWN:
    673  1.165        ad 		return fsetown((fp->f_flag & FREAD) != 0 ?
    674  1.165        ad 		    &pipe->pipe_rdpgid : &pipe->pipe_wrpgid, cmd, data);
    675    1.2  jdolecek 
    676    1.2  jdolecek 	case TIOCGPGRP:
    677   1.43  jdolecek 	case FIOGETOWN:
    678  1.165        ad 		return fgetown((fp->f_flag & FREAD) != 0 ?
    679  1.165        ad 		    pipe->pipe_rdpgid : pipe->pipe_wrpgid, cmd, data);
    680    1.1  jdolecek 
    681  1.165        ad 	default:
    682  1.165        ad 		return EPASSTHROUGH;
    683    1.1  jdolecek 	}
    684    1.1  jdolecek }
    685    1.1  jdolecek 
    686    1.1  jdolecek int
    687  1.113     rmind pipe_poll(file_t *fp, int events)
    688    1.1  jdolecek {
    689  1.165        ad 	struct pipe *pipe = fp->f_pipe;
    690  1.165        ad 	kmutex_t *lock = pipe->pipe_lock;
    691    1.1  jdolecek 	int revents = 0;
    692    1.1  jdolecek 
    693  1.165        ad 	/* Unlocked fast path for make(1). */
    694  1.165        ad 	if ((fp->f_flag & FREAD) != 0 &&
    695  1.165        ad 	    atomic_load_relaxed(&pipe->pipe_buffer.cnt) != 0 &&
    696  1.165        ad 	    (atomic_load_relaxed(&pipe->pipe_state) & PIPE_EOF) == 0 &&
    697  1.165        ad 	    (events & (POLLIN | POLLRDNORM)) != 0 &&
    698  1.165        ad 	    (events & (POLLOUT | POLLWRNORM)) == 0)
    699  1.165        ad 		return events & (POLLIN | POLLRDNORM);
    700   1.35        pk 
    701  1.165        ad 	mutex_enter(lock);
    702    1.1  jdolecek 
    703  1.165        ad 	if ((fp->f_flag & FREAD) != 0) {
    704  1.165        ad 		if ((events & (POLLIN | POLLRDNORM)) != 0) {
    705  1.165        ad 			if (pipe->pipe_buffer.cnt > 0 ||
    706  1.165        ad 			    (pipe->pipe_state & PIPE_EOF) != 0)
    707  1.165        ad 				revents |= events & (POLLIN | POLLRDNORM);
    708  1.165        ad 			selrecord(curlwp, &pipe->pipe_rdsel);
    709  1.165        ad 		}
    710  1.165        ad 	} else if ((events & (POLLOUT | POLLWRNORM)) != 0) {
    711  1.165        ad 		KASSERT((fp->f_flag & FWRITE) != 0);
    712  1.165        ad 		size_t space = pipe->pipe_buffer.size - pipe->pipe_buffer.cnt;
    713  1.165        ad 		if ((pipe->pipe_state & PIPE_EOF) != 0)
    714  1.165        ad 			revents |= events & (POLLOUT | POLLWRNORM);
    715  1.165        ad 		if ((pipe->pipe_state & PIPE_EOF) || space >= PIPE_BUF)
    716  1.165        ad 			revents |= events & (POLLOUT | POLLWRNORM);
    717  1.165        ad 		selrecord(curlwp, &pipe->pipe_wrsel);
    718   1.35        pk 	}
    719   1.35        pk 
    720  1.165        ad 	if ((pipe->pipe_state & PIPE_EOF) != 0)
    721    1.1  jdolecek 		revents |= POLLHUP;
    722    1.1  jdolecek 
    723  1.165        ad 	mutex_exit(lock);
    724    1.1  jdolecek 
    725  1.165        ad 	return revents;
    726    1.1  jdolecek }
    727    1.1  jdolecek 
    728    1.1  jdolecek static int
    729  1.113     rmind pipe_stat(file_t *fp, struct stat *ub)
    730    1.1  jdolecek {
    731  1.140      matt 	struct pipe *pipe = fp->f_pipe;
    732  1.165        ad 	kmutex_t *lock = pipe->pipe_lock;
    733    1.1  jdolecek 
    734  1.110  christos 	memset(ub, 0, sizeof(*ub));
    735  1.165        ad 
    736  1.165        ad 	mutex_enter(lock);
    737   1.32  jdolecek 	ub->st_mode = S_IFIFO | S_IRUSR | S_IWUSR;
    738    1.1  jdolecek 	ub->st_blksize = pipe->pipe_buffer.size;
    739    1.1  jdolecek 	ub->st_size = pipe->pipe_buffer.cnt;
    740    1.2  jdolecek 	ub->st_blocks = (ub->st_size) ? 1 : 0;
    741  1.110  christos 	ub->st_atimespec = pipe->pipe_atime;
    742  1.110  christos 	ub->st_mtimespec = pipe->pipe_mtime;
    743  1.165        ad 	ub->st_ctimespec = pipe->pipe_btime;
    744  1.165        ad 	ub->st_birthtimespec = pipe->pipe_btime;
    745   1.72      elad 	ub->st_uid = kauth_cred_geteuid(fp->f_cred);
    746   1.72      elad 	ub->st_gid = kauth_cred_getegid(fp->f_cred);
    747  1.165        ad 	mutex_exit(lock);
    748   1.82        ad 
    749    1.1  jdolecek 	/*
    750    1.1  jdolecek 	 * Left as 0: st_dev, st_ino, st_nlink, st_rdev, st_flags, st_gen.
    751    1.1  jdolecek 	 * XXX (st_dev, st_ino) should be unique.
    752    1.1  jdolecek 	 */
    753  1.112  christos 	return 0;
    754    1.1  jdolecek }
    755    1.1  jdolecek 
    756    1.1  jdolecek static int
    757  1.113     rmind pipe_close(file_t *fp)
    758    1.1  jdolecek {
    759  1.140      matt 	struct pipe *pipe = fp->f_pipe;
    760  1.165        ad 	kmutex_t *lock = pipe->pipe_lock;
    761  1.165        ad 	u_int state;
    762  1.165        ad 
    763  1.165        ad 	KASSERT(cv_is_valid(&pipe->pipe_read));
    764  1.165        ad 	KASSERT(cv_is_valid(&pipe->pipe_write));
    765  1.165        ad 	KASSERT(cv_is_valid(&pipe->pipe_busy));
    766    1.1  jdolecek 
    767  1.140      matt 	fp->f_pipe = NULL;
    768  1.165        ad 
    769  1.165        ad 	/*
    770  1.165        ad 	 * If the other side is blocked, wake it up.
    771  1.165        ad 	 *
    772  1.165        ad 	 * Any knote objects still left in the list are the one attached by
    773  1.165        ad 	 * peer.  Since no one will traverse this list, we just clear it.
    774  1.165        ad 	 *
    775  1.165        ad 	 * XXX Exposes select/kqueue internals.
    776  1.165        ad 	 */
    777  1.165        ad 	mutex_enter(lock);
    778  1.165        ad 	pipebusy(pipe);
    779  1.165        ad 	state = pipe->pipe_state | PIPE_EOF;
    780  1.165        ad 	if ((fp->f_flag & FREAD) != 0) {
    781  1.165        ad 		KASSERT((state & PIPE_RDOPEN) != 0);
    782  1.165        ad 		SLIST_INIT(&pipe->pipe_rdsel.sel_klist);
    783  1.165        ad 		pipe->pipe_rdpgid = 0;
    784  1.165        ad 		state &= ~(PIPE_RDASYNC | PIPE_RDOPEN);
    785  1.165        ad 		pipeselwakeup(pipe, FWRITE, POLL_HUP);
    786  1.165        ad 		cv_broadcast(&pipe->pipe_write);
    787  1.165        ad 	} else {
    788  1.165        ad 		KASSERT((fp->f_flag & FWRITE) != 0);
    789  1.165        ad 		KASSERT((state & PIPE_WROPEN) != 0);
    790  1.165        ad 		SLIST_INIT(&pipe->pipe_wrsel.sel_klist);
    791  1.165        ad 		pipe->pipe_wrpgid = 0;
    792  1.165        ad 		state &= ~(PIPE_WRASYNC | PIPE_WROPEN);
    793  1.165        ad 		pipeselwakeup(pipe, FREAD, POLL_HUP);
    794  1.165        ad 		cv_broadcast(&pipe->pipe_read);
    795  1.165        ad 	}
    796  1.165        ad 	pipe->pipe_state = state;
    797  1.165        ad 	pipeunbusy(pipe);
    798  1.165        ad 	cv_signal(&pipe->pipe_busy);
    799  1.165        ad 	mutex_exit(lock);
    800  1.165        ad 
    801  1.165        ad 	/*
    802  1.165        ad 	 * NB: now that the mutex is released, we cannot touch "pipe" any
    803  1.165        ad 	 * more unless we are the last guy out, since nothing else is
    804  1.165        ad 	 * keeping the data structure around.  This also means we have to
    805  1.165        ad 	 * wake the other side with the mutex held above.
    806  1.165        ad 	 */
    807  1.165        ad 	if ((state & (PIPE_RDOPEN | PIPE_WROPEN)) != 0)
    808  1.165        ad 		return 0;
    809  1.165        ad 
    810  1.165        ad 	/* Both sides are closed, free resources. */
    811  1.165        ad 	pipe->pipe_state = PIPE_SIGNALR | PIPE_RDOPEN | PIPE_WROPEN;
    812  1.165        ad 	pipe->pipe_buffer.in = 0;
    813  1.165        ad 	pipe->pipe_buffer.out = 0;
    814  1.165        ad 	pipe->pipe_buffer.cnt = 0;
    815  1.165        ad 	if (pipe->pipe_buffer.buffer != (void *)pipe->pipe_kmem) {
    816  1.165        ad 		uvm_km_free(kernel_map, (vaddr_t)pipe->pipe_buffer.buffer,
    817  1.165        ad 		    pipe->pipe_buffer.size, UVM_KMF_PAGEABLE);
    818  1.165        ad 		atomic_add_int(&amountpipekva, -pipe->pipe_buffer.size);
    819  1.165        ad 		atomic_dec_uint(&nbigpipe);
    820  1.165        ad 		pipe->pipe_buffer.buffer = (void *)pipe->pipe_kmem;
    821  1.165        ad 		pipe->pipe_buffer.size = PIPE_SIZE;
    822  1.165        ad 	}
    823  1.165        ad 	pool_cache_put(pipe_cache, pipe);
    824  1.165        ad 
    825  1.165        ad 	return 0;
    826    1.1  jdolecek }
    827    1.1  jdolecek 
    828    1.1  jdolecek static void
    829  1.127       dsl pipe_restart(file_t *fp)
    830  1.123       dsl {
    831  1.140      matt 	struct pipe *pipe = fp->f_pipe;
    832  1.165        ad 	kmutex_t *lock = pipe->pipe_lock;
    833  1.123       dsl 
    834  1.124       dsl 	/*
    835  1.124       dsl 	 * Unblock blocked reads/writes in order to allow close() to complete.
    836  1.127       dsl 	 * System calls return ERESTART so that the fd is revalidated.
    837  1.127       dsl 	 * (Partial writes return the transfer length.)
    838  1.124       dsl 	 */
    839  1.165        ad 	mutex_enter(lock);
    840  1.165        ad 	cv_fdrestart(&pipe->pipe_read);
    841  1.165        ad 	cv_fdrestart(&pipe->pipe_write);
    842  1.165        ad 	cv_fdrestart(&pipe->pipe_busy);
    843  1.165        ad 	mutex_exit(lock);
    844  1.123       dsl }
    845  1.123       dsl 
    846  1.159  riastrad static int
    847  1.159  riastrad pipe_fpathconf(struct file *fp, int name, register_t *retval)
    848  1.159  riastrad {
    849  1.159  riastrad 
    850  1.159  riastrad 	switch (name) {
    851  1.159  riastrad 	case _PC_PIPE_BUF:
    852  1.159  riastrad 		*retval = PIPE_BUF;
    853  1.159  riastrad 		return 0;
    854  1.159  riastrad 	default:
    855  1.159  riastrad 		return EINVAL;
    856  1.159  riastrad 	}
    857  1.159  riastrad }
    858  1.159  riastrad 
    859  1.160  riastrad static int
    860  1.160  riastrad pipe_posix_fadvise(struct file *fp, off_t offset, off_t len, int advice)
    861  1.160  riastrad {
    862  1.160  riastrad 
    863  1.160  riastrad 	return ESPIPE;
    864  1.160  riastrad }
    865  1.160  riastrad 
    866  1.123       dsl static void
    867   1.27  jdolecek filt_pipedetach(struct knote *kn)
    868    1.1  jdolecek {
    869  1.165        ad 	struct file *fp = kn->kn_obj;
    870  1.165        ad 	struct pipe *pipe = fp->f_pipe;
    871  1.165        ad 	kmutex_t *lock = pipe->pipe_lock;
    872    1.1  jdolecek 
    873   1.92        ad 	mutex_enter(lock);
    874   1.82        ad 
    875  1.113     rmind 	KASSERT(kn->kn_hook == pipe);
    876  1.165        ad 	if ((fp->f_flag & FREAD) != 0) {
    877  1.165        ad 		if ((pipe->pipe_state & PIPE_RDOPEN) != 0)
    878  1.165        ad 			selremove_knote(&pipe->pipe_rdsel, kn);
    879  1.165        ad 	} else if ((pipe->pipe_state & PIPE_WROPEN) != 0)
    880  1.165        ad 		selremove_knote(&pipe->pipe_wrsel, kn);
    881   1.92        ad 	mutex_exit(lock);
    882    1.1  jdolecek }
    883    1.1  jdolecek 
    884    1.1  jdolecek static int
    885    1.1  jdolecek filt_piperead(struct knote *kn, long hint)
    886    1.1  jdolecek {
    887  1.165        ad 	struct file *fp = kn->kn_obj;
    888  1.165        ad 	struct pipe *pipe = fp->f_pipe;
    889  1.165        ad 	kmutex_t *lock = pipe->pipe_lock;
    890  1.156   thorpej 	int rv;
    891   1.82        ad 
    892   1.83        ad 	if ((hint & NOTE_SUBMIT) == 0) {
    893  1.165        ad 		mutex_enter(lock);
    894   1.83        ad 	}
    895    1.1  jdolecek 
    896  1.165        ad 	if ((fp->f_flag & FREAD) != 0)
    897  1.165        ad 		kn->kn_data = pipe->pipe_buffer.cnt;
    898  1.165        ad 	else
    899  1.165        ad 		kn->kn_data = 0;
    900  1.165        ad 
    901  1.165        ad 	if ((pipe->pipe_state & PIPE_EOF) != 0) {
    902  1.158   thorpej 		knote_set_eof(kn, 0);
    903  1.156   thorpej 		rv = 1;
    904  1.156   thorpej 	} else {
    905  1.156   thorpej 		rv = kn->kn_data > 0;
    906    1.1  jdolecek 	}
    907   1.83        ad 
    908   1.83        ad 	if ((hint & NOTE_SUBMIT) == 0) {
    909  1.165        ad 		mutex_exit(lock);
    910   1.83        ad 	}
    911  1.156   thorpej 	return rv;
    912    1.1  jdolecek }
    913    1.1  jdolecek 
    914    1.1  jdolecek static int
    915    1.1  jdolecek filt_pipewrite(struct knote *kn, long hint)
    916    1.1  jdolecek {
    917  1.165        ad 	struct file *fp = kn->kn_obj;
    918  1.165        ad 	struct pipe *pipe = fp->f_pipe;
    919  1.165        ad 	kmutex_t *lock = pipe->pipe_lock;
    920  1.156   thorpej 	int rv;
    921   1.82        ad 
    922   1.83        ad 	if ((hint & NOTE_SUBMIT) == 0) {
    923  1.165        ad 		mutex_enter(lock);
    924   1.83        ad 	}
    925    1.1  jdolecek 
    926  1.165        ad 	if ((pipe->pipe_state & PIPE_EOF)) {
    927    1.1  jdolecek 		kn->kn_data = 0;
    928  1.158   thorpej 		knote_set_eof(kn, 0);
    929  1.156   thorpej 		rv = 1;
    930  1.165        ad 	} else if ((fp->f_flag & FWRITE) != 0) {
    931  1.165        ad 		kn->kn_data = pipe->pipe_buffer.size - pipe->pipe_buffer.cnt;
    932  1.165        ad 		rv = kn->kn_data >= PIPE_BUF;
    933  1.156   thorpej 	} else {
    934  1.165        ad 		kn->kn_data = 0;
    935  1.165        ad 		rv = 0;
    936    1.1  jdolecek 	}
    937    1.1  jdolecek 
    938   1.83        ad 	if ((hint & NOTE_SUBMIT) == 0) {
    939  1.165        ad 		mutex_exit(lock);
    940   1.83        ad 	}
    941  1.156   thorpej 	return rv;
    942    1.1  jdolecek }
    943   1.27  jdolecek 
    944  1.141      maya static const struct filterops pipe_rfiltops = {
    945  1.155   thorpej 	.f_flags = FILTEROP_ISFD | FILTEROP_MPSAFE,
    946  1.141      maya 	.f_attach = NULL,
    947  1.141      maya 	.f_detach = filt_pipedetach,
    948  1.141      maya 	.f_event = filt_piperead,
    949  1.141      maya };
    950  1.141      maya 
    951  1.141      maya static const struct filterops pipe_wfiltops = {
    952  1.155   thorpej 	.f_flags = FILTEROP_ISFD | FILTEROP_MPSAFE,
    953  1.141      maya 	.f_attach = NULL,
    954  1.141      maya 	.f_detach = filt_pipedetach,
    955  1.141      maya 	.f_event = filt_pipewrite,
    956  1.141      maya };
    957   1.27  jdolecek 
    958   1.27  jdolecek static int
    959  1.113     rmind pipe_kqfilter(file_t *fp, struct knote *kn)
    960   1.27  jdolecek {
    961  1.165        ad 	struct pipe *pipe = ((file_t *)kn->kn_obj)->f_pipe;
    962  1.165        ad 	kmutex_t *lock = pipe->pipe_lock;
    963   1.82        ad 
    964   1.27  jdolecek 	switch (kn->kn_filter) {
    965   1.27  jdolecek 	case EVFILT_READ:
    966  1.165        ad 		if ((fp->f_flag & FREAD) == 0)
    967  1.165        ad 			return EINVAL;
    968  1.165        ad 		mutex_enter(lock);
    969   1.27  jdolecek 		kn->kn_fop = &pipe_rfiltops;
    970  1.165        ad 		kn->kn_hook = pipe;
    971  1.165        ad 		selrecord_knote(&pipe->pipe_rdsel, kn);
    972  1.165        ad 		mutex_exit(lock);
    973   1.27  jdolecek 		break;
    974   1.27  jdolecek 	case EVFILT_WRITE:
    975  1.165        ad 		if ((fp->f_flag & FWRITE) == 0)
    976  1.165        ad 			return EINVAL;
    977  1.165        ad 		mutex_enter(lock);
    978   1.27  jdolecek 		kn->kn_fop = &pipe_wfiltops;
    979  1.165        ad 		if ((pipe->pipe_state & PIPE_EOF) != 0) {
    980  1.113     rmind 			/* Other end of pipe has been closed. */
    981   1.92        ad 			mutex_exit(lock);
    982  1.165        ad 			return EBADF;
    983   1.27  jdolecek 		}
    984  1.165        ad 		kn->kn_hook = pipe;
    985  1.165        ad 		selrecord_knote(&pipe->pipe_wrsel, kn);
    986  1.165        ad 		mutex_exit(lock);
    987   1.27  jdolecek 		break;
    988   1.27  jdolecek 	default:
    989  1.165        ad 		return EINVAL;
    990   1.27  jdolecek 	}
    991   1.82        ad 
    992   1.27  jdolecek 	return (0);
    993   1.27  jdolecek }
    994    1.2  jdolecek 
    995    1.2  jdolecek /*
    996    1.2  jdolecek  * Handle pipe sysctls.
    997    1.2  jdolecek  */
    998   1.47    atatat SYSCTL_SETUP(sysctl_kern_pipe_setup, "sysctl kern.pipe subtree setup")
    999   1.47    atatat {
   1000   1.47    atatat 
   1001   1.54    atatat 	sysctl_createv(clog, 0, NULL, NULL,
   1002   1.54    atatat 		       CTLFLAG_PERMANENT,
   1003   1.56    atatat 		       CTLTYPE_NODE, "pipe",
   1004   1.56    atatat 		       SYSCTL_DESCR("Pipe settings"),
   1005   1.47    atatat 		       NULL, 0, NULL, 0,
   1006   1.47    atatat 		       CTL_KERN, KERN_PIPE, CTL_EOL);
   1007   1.47    atatat 
   1008   1.54    atatat 	sysctl_createv(clog, 0, NULL, NULL,
   1009   1.54    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1010   1.56    atatat 		       CTLTYPE_INT, "maxbigpipes",
   1011   1.56    atatat 		       SYSCTL_DESCR("Maximum number of \"big\" pipes"),
   1012   1.47    atatat 		       NULL, 0, &maxbigpipes, 0,
   1013   1.47    atatat 		       CTL_KERN, KERN_PIPE, KERN_PIPE_MAXBIGPIPES, CTL_EOL);
   1014   1.54    atatat 	sysctl_createv(clog, 0, NULL, NULL,
   1015   1.54    atatat 		       CTLFLAG_PERMANENT,
   1016   1.56    atatat 		       CTLTYPE_INT, "nbigpipes",
   1017   1.56    atatat 		       SYSCTL_DESCR("Number of \"big\" pipes"),
   1018   1.47    atatat 		       NULL, 0, &nbigpipe, 0,
   1019   1.47    atatat 		       CTL_KERN, KERN_PIPE, KERN_PIPE_NBIGPIPES, CTL_EOL);
   1020   1.54    atatat 	sysctl_createv(clog, 0, NULL, NULL,
   1021   1.54    atatat 		       CTLFLAG_PERMANENT,
   1022   1.56    atatat 		       CTLTYPE_INT, "kvasize",
   1023   1.56    atatat 		       SYSCTL_DESCR("Amount of kernel memory consumed by pipe "
   1024   1.56    atatat 				    "buffers"),
   1025   1.47    atatat 		       NULL, 0, &amountpipekva, 0,
   1026   1.47    atatat 		       CTL_KERN, KERN_PIPE, KERN_PIPE_KVASIZE, CTL_EOL);
   1027    1.2  jdolecek }
   1028