Home | History | Annotate | Line # | Download | only in kern
sys_pipe.c revision 1.92
      1  1.92        ad /*	$NetBSD: sys_pipe.c,v 1.92 2007/12/28 13:11:16 ad Exp $	*/
      2  1.35        pk 
      3  1.35        pk /*-
      4  1.80        ad  * Copyright (c) 2003, 2007 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  * 3. All advertising materials mentioning features or use of this software
     19  1.35        pk  *    must display the following acknowledgement:
     20  1.35        pk  *        This product includes software developed by the NetBSD
     21  1.35        pk  *        Foundation, Inc. and its contributors.
     22  1.35        pk  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.35        pk  *    contributors may be used to endorse or promote products derived
     24  1.35        pk  *    from this software without specific prior written permission.
     25  1.35        pk  *
     26  1.35        pk  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.35        pk  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.35        pk  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.35        pk  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.35        pk  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.35        pk  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.35        pk  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.35        pk  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.35        pk  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.35        pk  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.35        pk  * POSSIBILITY OF SUCH DAMAGE.
     37  1.35        pk  */
     38   1.2  jdolecek 
     39   1.1  jdolecek /*
     40   1.1  jdolecek  * Copyright (c) 1996 John S. Dyson
     41   1.1  jdolecek  * All rights reserved.
     42   1.1  jdolecek  *
     43   1.1  jdolecek  * Redistribution and use in source and binary forms, with or without
     44   1.1  jdolecek  * modification, are permitted provided that the following conditions
     45   1.1  jdolecek  * are met:
     46   1.1  jdolecek  * 1. Redistributions of source code must retain the above copyright
     47   1.1  jdolecek  *    notice immediately at the beginning of the file, without modification,
     48   1.1  jdolecek  *    this list of conditions, and the following disclaimer.
     49   1.1  jdolecek  * 2. Redistributions in binary form must reproduce the above copyright
     50   1.1  jdolecek  *    notice, this list of conditions and the following disclaimer in the
     51   1.1  jdolecek  *    documentation and/or other materials provided with the distribution.
     52   1.1  jdolecek  * 3. Absolutely no warranty of function or purpose is made by the author
     53   1.1  jdolecek  *    John S. Dyson.
     54   1.1  jdolecek  * 4. Modifications may be freely made to this file if the above conditions
     55   1.1  jdolecek  *    are met.
     56   1.1  jdolecek  *
     57  1.24  jdolecek  * $FreeBSD: src/sys/kern/sys_pipe.c,v 1.95 2002/03/09 22:06:31 alfred Exp $
     58   1.1  jdolecek  */
     59   1.1  jdolecek 
     60   1.1  jdolecek /*
     61   1.1  jdolecek  * This file contains a high-performance replacement for the socket-based
     62   1.1  jdolecek  * pipes scheme originally used in FreeBSD/4.4Lite.  It does not support
     63   1.1  jdolecek  * all features of sockets, but does do everything that pipes normally
     64   1.1  jdolecek  * do.
     65   1.2  jdolecek  *
     66   1.2  jdolecek  * Adaption for NetBSD UVM, including uvm_loan() based direct write, was
     67   1.2  jdolecek  * written by Jaromir Dolecek.
     68   1.1  jdolecek  */
     69   1.1  jdolecek 
     70   1.1  jdolecek /*
     71   1.1  jdolecek  * This code has two modes of operation, a small write mode and a large
     72   1.1  jdolecek  * write mode.  The small write mode acts like conventional pipes with
     73   1.1  jdolecek  * a kernel buffer.  If the buffer is less than PIPE_MINDIRECT, then the
     74   1.1  jdolecek  * "normal" pipe buffering is done.  If the buffer is between PIPE_MINDIRECT
     75  1.35        pk  * and PIPE_SIZE in size it is mapped read-only into the kernel address space
     76  1.35        pk  * using the UVM page loan facility from where the receiving process can copy
     77  1.35        pk  * the data directly from the pages in the sending process.
     78   1.1  jdolecek  *
     79   1.1  jdolecek  * The constant PIPE_MINDIRECT is chosen to make sure that buffering will
     80   1.1  jdolecek  * happen for small transfers so that the system will not spend all of
     81   1.1  jdolecek  * its time context switching.  PIPE_SIZE is constrained by the
     82   1.1  jdolecek  * amount of kernel virtual memory.
     83   1.1  jdolecek  */
     84  1.19     lukem 
     85  1.19     lukem #include <sys/cdefs.h>
     86  1.92        ad __KERNEL_RCSID(0, "$NetBSD: sys_pipe.c,v 1.92 2007/12/28 13:11:16 ad Exp $");
     87   1.2  jdolecek 
     88   1.1  jdolecek #include <sys/param.h>
     89   1.1  jdolecek #include <sys/systm.h>
     90   1.2  jdolecek #include <sys/proc.h>
     91   1.1  jdolecek #include <sys/fcntl.h>
     92   1.1  jdolecek #include <sys/file.h>
     93   1.1  jdolecek #include <sys/filedesc.h>
     94   1.1  jdolecek #include <sys/filio.h>
     95  1.24  jdolecek #include <sys/kernel.h>
     96   1.1  jdolecek #include <sys/ttycom.h>
     97   1.1  jdolecek #include <sys/stat.h>
     98  1.24  jdolecek #include <sys/malloc.h>
     99   1.1  jdolecek #include <sys/poll.h>
    100   1.2  jdolecek #include <sys/signalvar.h>
    101   1.2  jdolecek #include <sys/vnode.h>
    102   1.2  jdolecek #include <sys/uio.h>
    103   1.2  jdolecek #include <sys/lock.h>
    104   1.2  jdolecek #include <sys/select.h>
    105   1.2  jdolecek #include <sys/mount.h>
    106   1.2  jdolecek #include <sys/syscallargs.h>
    107   1.2  jdolecek #include <sys/sysctl.h>
    108  1.72      elad #include <sys/kauth.h>
    109  1.90        ad #include <sys/atomic.h>
    110  1.90        ad #include <sys/pipe.h>
    111   1.2  jdolecek 
    112  1.90        ad #include <uvm/uvm.h>
    113   1.1  jdolecek 
    114  1.17  jdolecek /*
    115   1.1  jdolecek  * Use this define if you want to disable *fancy* VM things.  Expect an
    116  1.35        pk  * approx 30% decrease in transfer rate.
    117   1.1  jdolecek  */
    118   1.1  jdolecek /* #define PIPE_NODIRECT */
    119   1.1  jdolecek 
    120   1.1  jdolecek /*
    121   1.1  jdolecek  * interfaces to the outside world
    122   1.1  jdolecek  */
    123  1.63     perry static int pipe_read(struct file *fp, off_t *offset, struct uio *uio,
    124  1.72      elad 		kauth_cred_t cred, int flags);
    125  1.63     perry static int pipe_write(struct file *fp, off_t *offset, struct uio *uio,
    126  1.72      elad 		kauth_cred_t cred, int flags);
    127  1.69  christos static int pipe_close(struct file *fp, struct lwp *l);
    128  1.69  christos static int pipe_poll(struct file *fp, int events, struct lwp *l);
    129  1.27  jdolecek static int pipe_kqfilter(struct file *fp, struct knote *kn);
    130  1.69  christos static int pipe_stat(struct file *fp, struct stat *sb, struct lwp *l);
    131  1.38       dsl static int pipe_ioctl(struct file *fp, u_long cmd, void *data,
    132  1.69  christos 		struct lwp *l);
    133   1.1  jdolecek 
    134  1.62  christos static const struct fileops pipeops = {
    135  1.62  christos 	pipe_read, pipe_write, pipe_ioctl, fnullop_fcntl, pipe_poll,
    136  1.35        pk 	pipe_stat, pipe_close, pipe_kqfilter
    137  1.35        pk };
    138   1.1  jdolecek 
    139   1.1  jdolecek /*
    140  1.90        ad  * Single mutex shared between both ends of the pipe.
    141  1.90        ad  */
    142  1.90        ad 
    143  1.90        ad struct pipe_mutex {
    144  1.90        ad 	kmutex_t	pm_mutex;
    145  1.90        ad 	u_int		pm_refcnt;
    146  1.90        ad };
    147  1.90        ad 
    148  1.90        ad /*
    149   1.1  jdolecek  * Default pipe buffer size(s), this can be kind-of large now because pipe
    150   1.1  jdolecek  * space is pageable.  The pipe code will try to maintain locality of
    151   1.1  jdolecek  * reference for performance reasons, so small amounts of outstanding I/O
    152   1.1  jdolecek  * will not wipe the cache.
    153   1.1  jdolecek  */
    154   1.1  jdolecek #define MINPIPESIZE (PIPE_SIZE/3)
    155   1.1  jdolecek #define MAXPIPESIZE (2*PIPE_SIZE/3)
    156   1.1  jdolecek 
    157   1.1  jdolecek /*
    158   1.1  jdolecek  * Maximum amount of kva for pipes -- this is kind-of a soft limit, but
    159   1.1  jdolecek  * is there so that on large systems, we don't exhaust it.
    160   1.1  jdolecek  */
    161   1.1  jdolecek #define MAXPIPEKVA (8*1024*1024)
    162  1.90        ad static u_int maxpipekva = MAXPIPEKVA;
    163   1.1  jdolecek 
    164   1.1  jdolecek /*
    165   1.1  jdolecek  * Limit for direct transfers, we cannot, of course limit
    166   1.1  jdolecek  * the amount of kva for pipes in general though.
    167   1.1  jdolecek  */
    168   1.1  jdolecek #define LIMITPIPEKVA (16*1024*1024)
    169  1.90        ad static u_int limitpipekva = LIMITPIPEKVA;
    170   1.1  jdolecek 
    171   1.1  jdolecek /*
    172   1.1  jdolecek  * Limit the number of "big" pipes
    173   1.1  jdolecek  */
    174   1.2  jdolecek #define LIMITBIGPIPES  32
    175  1.90        ad static u_int maxbigpipes = LIMITBIGPIPES;
    176  1.90        ad static u_int nbigpipe = 0;
    177   1.1  jdolecek 
    178   1.2  jdolecek /*
    179   1.2  jdolecek  * Amount of KVA consumed by pipe buffers.
    180   1.2  jdolecek  */
    181  1.90        ad static u_int amountpipekva = 0;
    182  1.34   thorpej 
    183  1.34   thorpej MALLOC_DEFINE(M_PIPE, "pipe", "Pipe structures");
    184   1.1  jdolecek 
    185  1.42  christos static void pipeclose(struct file *fp, struct pipe *pipe);
    186  1.35        pk static void pipe_free_kmem(struct pipe *pipe);
    187  1.90        ad static int pipe_create(struct pipe **pipep, int allockva, struct pipe_mutex *);
    188  1.35        pk static int pipelock(struct pipe *pipe, int catch);
    189  1.70     perry static inline void pipeunlock(struct pipe *pipe);
    190  1.66  christos static void pipeselwakeup(struct pipe *pipe, struct pipe *sigp, int code);
    191   1.1  jdolecek #ifndef PIPE_NODIRECT
    192  1.42  christos static int pipe_direct_write(struct file *fp, struct pipe *wpipe,
    193  1.42  christos     struct uio *uio);
    194   1.1  jdolecek #endif
    195  1.35        pk static int pipespace(struct pipe *pipe, int size);
    196   1.2  jdolecek 
    197   1.2  jdolecek #ifndef PIPE_NODIRECT
    198  1.24  jdolecek static int pipe_loan_alloc(struct pipe *, int);
    199  1.24  jdolecek static void pipe_loan_free(struct pipe *);
    200   1.2  jdolecek #endif /* PIPE_NODIRECT */
    201   1.2  jdolecek 
    202  1.90        ad static int pipe_mutex_ctor(void *, void *, int);
    203  1.90        ad static void pipe_mutex_dtor(void *, void *);
    204  1.90        ad 
    205  1.87        ad static pool_cache_t pipe_cache;
    206  1.90        ad static pool_cache_t pipe_mutex_cache;
    207  1.82        ad 
    208  1.82        ad void
    209  1.82        ad pipe_init(void)
    210  1.82        ad {
    211  1.90        ad 	size_t size;
    212  1.82        ad 
    213  1.87        ad 	pipe_cache = pool_cache_init(sizeof(struct pipe), 0, 0, 0, "pipepl",
    214  1.87        ad 	    NULL, IPL_NONE, NULL, NULL, NULL);
    215  1.87        ad 	KASSERT(pipe_cache != NULL);
    216  1.90        ad 
    217  1.90        ad 	size = (sizeof(struct pipe_mutex) + (CACHE_LINE_SIZE - 1)) &
    218  1.90        ad 	    (CACHE_LINE_SIZE - 1);
    219  1.90        ad 	pipe_mutex_cache = pool_cache_init(size, CACHE_LINE_SIZE,
    220  1.90        ad 	    0, 0, "pipemtxpl", NULL, IPL_NONE, pipe_mutex_ctor,
    221  1.90        ad 	    pipe_mutex_dtor, NULL);
    222  1.90        ad 	KASSERT(pipe_cache != NULL);
    223  1.90        ad }
    224  1.90        ad 
    225  1.90        ad static int
    226  1.90        ad pipe_mutex_ctor(void *arg, void *obj, int flag)
    227  1.90        ad {
    228  1.90        ad 	struct pipe_mutex *pm = obj;
    229  1.90        ad 
    230  1.90        ad 	mutex_init(&pm->pm_mutex, MUTEX_DEFAULT, IPL_NONE);
    231  1.90        ad 	pm->pm_refcnt = 0;
    232  1.90        ad 
    233  1.90        ad 	return 0;
    234  1.90        ad }
    235  1.90        ad 
    236  1.90        ad static void
    237  1.90        ad pipe_mutex_dtor(void *arg, void *obj)
    238  1.90        ad {
    239  1.90        ad 	struct pipe_mutex *pm = obj;
    240  1.90        ad 
    241  1.90        ad 	KASSERT(pm->pm_refcnt == 0);
    242  1.90        ad 
    243  1.90        ad 	mutex_destroy(&pm->pm_mutex);
    244  1.82        ad }
    245  1.82        ad 
    246   1.1  jdolecek /*
    247   1.1  jdolecek  * The pipe system call for the DTYPE_PIPE type of pipes
    248   1.1  jdolecek  */
    249   1.1  jdolecek 
    250   1.1  jdolecek /* ARGSUSED */
    251   1.2  jdolecek int
    252  1.89       dsl sys_pipe(struct lwp *l, const void *v, register_t *retval)
    253   1.1  jdolecek {
    254   1.1  jdolecek 	struct file *rf, *wf;
    255  1.53       dsl 	struct pipe *rpipe, *wpipe;
    256  1.90        ad 	struct pipe_mutex *mutex;
    257   1.1  jdolecek 	int fd, error;
    258   1.2  jdolecek 
    259   1.6  jdolecek 	rpipe = wpipe = NULL;
    260  1.90        ad 	mutex = pool_cache_get(pipe_mutex_cache, PR_WAITOK);
    261  1.90        ad 	if (mutex == NULL)
    262  1.90        ad 		return (ENOMEM);
    263  1.90        ad 	if (pipe_create(&rpipe, 1, mutex) || pipe_create(&wpipe, 0, mutex)) {
    264  1.42  christos 		pipeclose(NULL, rpipe);
    265  1.42  christos 		pipeclose(NULL, wpipe);
    266   1.6  jdolecek 		return (ENFILE);
    267   1.6  jdolecek 	}
    268   1.6  jdolecek 
    269   1.2  jdolecek 	/*
    270   1.2  jdolecek 	 * Note: the file structure returned from falloc() is marked
    271   1.2  jdolecek 	 * as 'larval' initially. Unless we mark it as 'mature' by
    272   1.2  jdolecek 	 * FILE_SET_MATURE(), any attempt to do anything with it would
    273   1.2  jdolecek 	 * return EBADF, including e.g. dup(2) or close(2). This avoids
    274   1.2  jdolecek 	 * file descriptor races if we block in the second falloc().
    275   1.2  jdolecek 	 */
    276   1.2  jdolecek 
    277  1.74        ad 	error = falloc(l, &rf, &fd);
    278   1.2  jdolecek 	if (error)
    279   1.2  jdolecek 		goto free2;
    280   1.2  jdolecek 	retval[0] = fd;
    281   1.2  jdolecek 	rf->f_flag = FREAD;
    282   1.2  jdolecek 	rf->f_type = DTYPE_PIPE;
    283  1.79  christos 	rf->f_data = (void *)rpipe;
    284   1.2  jdolecek 	rf->f_ops = &pipeops;
    285   1.2  jdolecek 
    286  1.74        ad 	error = falloc(l, &wf, &fd);
    287   1.2  jdolecek 	if (error)
    288   1.2  jdolecek 		goto free3;
    289   1.2  jdolecek 	retval[1] = fd;
    290   1.2  jdolecek 	wf->f_flag = FWRITE;
    291   1.2  jdolecek 	wf->f_type = DTYPE_PIPE;
    292  1.79  christos 	wf->f_data = (void *)wpipe;
    293   1.2  jdolecek 	wf->f_ops = &pipeops;
    294   1.2  jdolecek 
    295   1.2  jdolecek 	rpipe->pipe_peer = wpipe;
    296   1.2  jdolecek 	wpipe->pipe_peer = rpipe;
    297   1.1  jdolecek 
    298   1.2  jdolecek 	FILE_SET_MATURE(rf);
    299   1.2  jdolecek 	FILE_SET_MATURE(wf);
    300  1.69  christos 	FILE_UNUSE(rf, l);
    301  1.69  christos 	FILE_UNUSE(wf, l);
    302   1.1  jdolecek 	return (0);
    303   1.2  jdolecek free3:
    304  1.69  christos 	FILE_UNUSE(rf, l);
    305   1.2  jdolecek 	ffree(rf);
    306  1.74        ad 	fdremove(l->l_proc->p_fd, retval[0]);
    307   1.2  jdolecek free2:
    308  1.42  christos 	pipeclose(NULL, wpipe);
    309  1.42  christos 	pipeclose(NULL, rpipe);
    310   1.2  jdolecek 
    311   1.2  jdolecek 	return (error);
    312   1.1  jdolecek }
    313   1.1  jdolecek 
    314   1.1  jdolecek /*
    315   1.1  jdolecek  * Allocate kva for pipe circular buffer, the space is pageable
    316   1.1  jdolecek  * This routine will 'realloc' the size of a pipe safely, if it fails
    317   1.1  jdolecek  * it will retain the old buffer.
    318   1.1  jdolecek  * If it fails it will return ENOMEM.
    319   1.1  jdolecek  */
    320   1.1  jdolecek static int
    321  1.68   thorpej pipespace(struct pipe *pipe, int size)
    322   1.1  jdolecek {
    323  1.79  christos 	void *buffer;
    324   1.2  jdolecek 	/*
    325  1.35        pk 	 * Allocate pageable virtual address space. Physical memory is
    326  1.35        pk 	 * allocated on demand.
    327   1.2  jdolecek 	 */
    328  1.79  christos 	buffer = (void *) uvm_km_alloc(kernel_map, round_page(size), 0,
    329  1.65      yamt 	    UVM_KMF_PAGEABLE);
    330   1.2  jdolecek 	if (buffer == NULL)
    331   1.2  jdolecek 		return (ENOMEM);
    332   1.1  jdolecek 
    333   1.1  jdolecek 	/* free old resources if we're resizing */
    334  1.35        pk 	pipe_free_kmem(pipe);
    335  1.35        pk 	pipe->pipe_buffer.buffer = buffer;
    336  1.35        pk 	pipe->pipe_buffer.size = size;
    337  1.35        pk 	pipe->pipe_buffer.in = 0;
    338  1.35        pk 	pipe->pipe_buffer.out = 0;
    339  1.35        pk 	pipe->pipe_buffer.cnt = 0;
    340  1.90        ad 	atomic_add_int(&amountpipekva, pipe->pipe_buffer.size);
    341   1.1  jdolecek 	return (0);
    342   1.1  jdolecek }
    343   1.1  jdolecek 
    344   1.1  jdolecek /*
    345  1.35        pk  * Initialize and allocate VM and memory for pipe.
    346   1.1  jdolecek  */
    347   1.1  jdolecek static int
    348  1.90        ad pipe_create(struct pipe **pipep, int allockva, struct pipe_mutex *mutex)
    349   1.1  jdolecek {
    350  1.35        pk 	struct pipe *pipe;
    351   1.1  jdolecek 	int error;
    352   1.1  jdolecek 
    353  1.87        ad 	pipe = *pipep = pool_cache_get(pipe_cache, PR_WAITOK);
    354  1.90        ad 	mutex->pm_refcnt++;
    355   1.1  jdolecek 
    356  1.63     perry 	/* Initialize */
    357  1.35        pk 	memset(pipe, 0, sizeof(struct pipe));
    358  1.35        pk 	pipe->pipe_state = PIPE_SIGNALR;
    359   1.1  jdolecek 
    360  1.73    kardel 	getmicrotime(&pipe->pipe_ctime);
    361  1.35        pk 	pipe->pipe_atime = pipe->pipe_ctime;
    362  1.35        pk 	pipe->pipe_mtime = pipe->pipe_ctime;
    363  1.90        ad 	pipe->pipe_lock = &mutex->pm_mutex;
    364  1.80        ad 	cv_init(&pipe->pipe_cv, "pipe");
    365  1.80        ad 	cv_init(&pipe->pipe_lkcv, "pipelk");
    366  1.86        ad 	selinit(&pipe->pipe_sel);
    367   1.1  jdolecek 
    368  1.53       dsl 	if (allockva && (error = pipespace(pipe, PIPE_SIZE)))
    369  1.53       dsl 		return (error);
    370  1.53       dsl 
    371   1.1  jdolecek 	return (0);
    372   1.1  jdolecek }
    373   1.1  jdolecek 
    374   1.1  jdolecek 
    375   1.1  jdolecek /*
    376  1.35        pk  * Lock a pipe for I/O, blocking other access
    377  1.35        pk  * Called with pipe spin lock held.
    378  1.35        pk  * Return with pipe spin lock released on success.
    379   1.1  jdolecek  */
    380  1.35        pk static int
    381  1.68   thorpej pipelock(struct pipe *pipe, int catch)
    382   1.1  jdolecek {
    383  1.80        ad 	int error;
    384   1.1  jdolecek 
    385  1.90        ad 	KASSERT(mutex_owned(pipe->pipe_lock));
    386  1.35        pk 
    387  1.67      yamt 	while (pipe->pipe_state & PIPE_LOCKFL) {
    388  1.67      yamt 		pipe->pipe_state |= PIPE_LWANT;
    389  1.80        ad 		if (catch) {
    390  1.90        ad 			error = cv_wait_sig(&pipe->pipe_lkcv, pipe->pipe_lock);
    391  1.80        ad 			if (error != 0)
    392  1.80        ad 				return error;
    393  1.80        ad 		} else
    394  1.90        ad 			cv_wait(&pipe->pipe_lkcv, pipe->pipe_lock);
    395   1.1  jdolecek 	}
    396  1.67      yamt 
    397  1.67      yamt 	pipe->pipe_state |= PIPE_LOCKFL;
    398  1.67      yamt 
    399  1.67      yamt 	return 0;
    400   1.1  jdolecek }
    401   1.1  jdolecek 
    402   1.1  jdolecek /*
    403   1.1  jdolecek  * unlock a pipe I/O lock
    404   1.1  jdolecek  */
    405  1.70     perry static inline void
    406  1.68   thorpej pipeunlock(struct pipe *pipe)
    407   1.1  jdolecek {
    408  1.24  jdolecek 
    409  1.67      yamt 	KASSERT(pipe->pipe_state & PIPE_LOCKFL);
    410  1.67      yamt 
    411  1.67      yamt 	pipe->pipe_state &= ~PIPE_LOCKFL;
    412  1.67      yamt 	if (pipe->pipe_state & PIPE_LWANT) {
    413  1.67      yamt 		pipe->pipe_state &= ~PIPE_LWANT;
    414  1.80        ad 		cv_broadcast(&pipe->pipe_lkcv);
    415  1.67      yamt 	}
    416   1.1  jdolecek }
    417   1.1  jdolecek 
    418   1.2  jdolecek /*
    419   1.2  jdolecek  * Select/poll wakup. This also sends SIGIO to peer connected to
    420   1.2  jdolecek  * 'sigpipe' side of pipe.
    421   1.2  jdolecek  */
    422  1.35        pk static void
    423  1.68   thorpej pipeselwakeup(struct pipe *selp, struct pipe *sigp, int code)
    424   1.1  jdolecek {
    425  1.43  jdolecek 	int band;
    426  1.27  jdolecek 
    427  1.48  jdolecek 	selnotify(&selp->pipe_sel, NOTE_SUBMIT);
    428  1.43  jdolecek 
    429  1.35        pk 	if (sigp == NULL || (sigp->pipe_state & PIPE_ASYNC) == 0)
    430  1.35        pk 		return;
    431  1.35        pk 
    432  1.43  jdolecek 	switch (code) {
    433  1.42  christos 	case POLL_IN:
    434  1.43  jdolecek 		band = POLLIN|POLLRDNORM;
    435  1.42  christos 		break;
    436  1.42  christos 	case POLL_OUT:
    437  1.43  jdolecek 		band = POLLOUT|POLLWRNORM;
    438  1.42  christos 		break;
    439  1.42  christos 	case POLL_HUP:
    440  1.43  jdolecek 		band = POLLHUP;
    441  1.42  christos 		break;
    442  1.42  christos #if POLL_HUP != POLL_ERR
    443  1.42  christos 	case POLL_ERR:
    444  1.43  jdolecek 		band = POLLERR;
    445  1.42  christos 		break;
    446  1.42  christos #endif
    447  1.42  christos 	default:
    448  1.45  christos 		band = 0;
    449  1.42  christos #ifdef DIAGNOSTIC
    450  1.42  christos 		printf("bad siginfo code %d in pipe notification.\n", code);
    451  1.42  christos #endif
    452  1.42  christos 		break;
    453  1.42  christos 	}
    454  1.43  jdolecek 
    455  1.44  christos 	fownsignal(sigp->pipe_pgid, SIGIO, code, band, selp);
    456   1.1  jdolecek }
    457   1.1  jdolecek 
    458   1.1  jdolecek /* ARGSUSED */
    459   1.2  jdolecek static int
    460  1.77      yamt pipe_read(struct file *fp, off_t *offset, struct uio *uio, kauth_cred_t cred,
    461  1.77      yamt     int flags)
    462   1.1  jdolecek {
    463   1.1  jdolecek 	struct pipe *rpipe = (struct pipe *) fp->f_data;
    464  1.35        pk 	struct pipebuf *bp = &rpipe->pipe_buffer;
    465   1.1  jdolecek 	int error;
    466   1.2  jdolecek 	size_t nread = 0;
    467   1.2  jdolecek 	size_t size;
    468   1.2  jdolecek 	size_t ocnt;
    469   1.1  jdolecek 
    470  1.90        ad 	mutex_enter(rpipe->pipe_lock);
    471   1.1  jdolecek 	++rpipe->pipe_busy;
    472  1.35        pk 	ocnt = bp->cnt;
    473  1.28  jdolecek 
    474  1.35        pk again:
    475   1.1  jdolecek 	error = pipelock(rpipe, 1);
    476   1.1  jdolecek 	if (error)
    477   1.1  jdolecek 		goto unlocked_error;
    478   1.2  jdolecek 
    479   1.1  jdolecek 	while (uio->uio_resid) {
    480   1.1  jdolecek 		/*
    481   1.1  jdolecek 		 * normal pipe buffer receive
    482   1.1  jdolecek 		 */
    483  1.35        pk 		if (bp->cnt > 0) {
    484  1.35        pk 			size = bp->size - bp->out;
    485  1.35        pk 			if (size > bp->cnt)
    486  1.35        pk 				size = bp->cnt;
    487   1.2  jdolecek 			if (size > uio->uio_resid)
    488   1.2  jdolecek 				size = uio->uio_resid;
    489   1.1  jdolecek 
    490  1.90        ad 			mutex_exit(rpipe->pipe_lock);
    491  1.79  christos 			error = uiomove((char *)bp->buffer + bp->out, size, uio);
    492  1.90        ad 			mutex_enter(rpipe->pipe_lock);
    493   1.1  jdolecek 			if (error)
    494   1.1  jdolecek 				break;
    495   1.1  jdolecek 
    496  1.35        pk 			bp->out += size;
    497  1.35        pk 			if (bp->out >= bp->size)
    498  1.35        pk 				bp->out = 0;
    499   1.1  jdolecek 
    500  1.35        pk 			bp->cnt -= size;
    501   1.1  jdolecek 
    502   1.1  jdolecek 			/*
    503   1.1  jdolecek 			 * If there is no more to read in the pipe, reset
    504   1.1  jdolecek 			 * its pointers to the beginning.  This improves
    505   1.1  jdolecek 			 * cache hit stats.
    506   1.1  jdolecek 			 */
    507  1.35        pk 			if (bp->cnt == 0) {
    508  1.35        pk 				bp->in = 0;
    509  1.35        pk 				bp->out = 0;
    510   1.1  jdolecek 			}
    511   1.1  jdolecek 			nread += size;
    512  1.85        ad 			continue;
    513  1.85        ad 		}
    514  1.85        ad 
    515   1.1  jdolecek #ifndef PIPE_NODIRECT
    516  1.85        ad 		if ((rpipe->pipe_state & PIPE_DIRECTR) != 0) {
    517  1.35        pk 			/*
    518  1.35        pk 			 * Direct copy, bypassing a kernel buffer.
    519  1.35        pk 			 */
    520  1.79  christos 			void *	va;
    521  1.35        pk 
    522  1.35        pk 			KASSERT(rpipe->pipe_state & PIPE_DIRECTW);
    523  1.35        pk 
    524  1.35        pk 			size = rpipe->pipe_map.cnt;
    525   1.2  jdolecek 			if (size > uio->uio_resid)
    526   1.2  jdolecek 				size = uio->uio_resid;
    527   1.1  jdolecek 
    528  1.79  christos 			va = (char *)rpipe->pipe_map.kva + rpipe->pipe_map.pos;
    529  1.90        ad 			mutex_exit(rpipe->pipe_lock);
    530   1.1  jdolecek 			error = uiomove(va, size, uio);
    531  1.90        ad 			mutex_enter(rpipe->pipe_lock);
    532   1.1  jdolecek 			if (error)
    533   1.1  jdolecek 				break;
    534   1.1  jdolecek 			nread += size;
    535   1.1  jdolecek 			rpipe->pipe_map.pos += size;
    536   1.1  jdolecek 			rpipe->pipe_map.cnt -= size;
    537   1.1  jdolecek 			if (rpipe->pipe_map.cnt == 0) {
    538  1.35        pk 				rpipe->pipe_state &= ~PIPE_DIRECTR;
    539  1.80        ad 				cv_broadcast(&rpipe->pipe_cv);
    540   1.1  jdolecek 			}
    541  1.85        ad 			continue;
    542  1.85        ad 		}
    543   1.1  jdolecek #endif
    544  1.85        ad 		/*
    545  1.85        ad 		 * Break if some data was read.
    546  1.85        ad 		 */
    547  1.90        ad 		if (nread > 0)
    548  1.85        ad 			break;
    549   1.1  jdolecek 
    550  1.85        ad 		/*
    551  1.85        ad 		 * detect EOF condition
    552  1.85        ad 		 * read returns 0 on EOF, no need to set error
    553  1.85        ad 		 */
    554  1.90        ad 		if (rpipe->pipe_state & PIPE_EOF)
    555  1.85        ad 			break;
    556  1.36        pk 
    557  1.85        ad 		/*
    558  1.85        ad 		 * don't block on non-blocking I/O
    559  1.85        ad 		 */
    560  1.85        ad 		if (fp->f_flag & FNONBLOCK) {
    561  1.85        ad 			error = EAGAIN;
    562  1.85        ad 			break;
    563  1.85        ad 		}
    564   1.1  jdolecek 
    565  1.85        ad 		/*
    566  1.85        ad 		 * Unlock the pipe buffer for our remaining processing.
    567  1.85        ad 		 * We will either break out with an error or we will
    568  1.85        ad 		 * sleep and relock to loop.
    569  1.85        ad 		 */
    570  1.85        ad 		pipeunlock(rpipe);
    571   1.2  jdolecek 
    572  1.85        ad 		/*
    573  1.85        ad 		 * Re-check to see if more direct writes are pending.
    574  1.85        ad 		 */
    575  1.85        ad 		if ((rpipe->pipe_state & PIPE_DIRECTR) != 0)
    576  1.85        ad 			goto again;
    577   1.1  jdolecek 
    578  1.85        ad 		/*
    579  1.85        ad 		 * We want to read more, wake up select/poll.
    580  1.85        ad 		 */
    581  1.85        ad 		pipeselwakeup(rpipe, rpipe->pipe_peer, POLL_IN);
    582  1.35        pk 
    583  1.85        ad 		/*
    584  1.85        ad 		 * If the "write-side" is blocked, wake it up now.
    585  1.85        ad 		 */
    586  1.85        ad 		if (rpipe->pipe_state & PIPE_WANTW) {
    587  1.85        ad 			rpipe->pipe_state &= ~PIPE_WANTW;
    588  1.85        ad 			cv_broadcast(&rpipe->pipe_cv);
    589  1.85        ad 		}
    590   1.2  jdolecek 
    591  1.85        ad 		/* Now wait until the pipe is filled */
    592  1.85        ad 		rpipe->pipe_state |= PIPE_WANTR;
    593  1.90        ad 		error = cv_wait_sig(&rpipe->pipe_cv, rpipe->pipe_lock);
    594  1.85        ad 		if (error != 0)
    595  1.85        ad 			goto unlocked_error;
    596  1.85        ad 		goto again;
    597   1.1  jdolecek 	}
    598  1.35        pk 
    599  1.35        pk 	if (error == 0)
    600  1.73    kardel 		getmicrotime(&rpipe->pipe_atime);
    601   1.1  jdolecek 	pipeunlock(rpipe);
    602   1.1  jdolecek 
    603   1.1  jdolecek unlocked_error:
    604   1.1  jdolecek 	--rpipe->pipe_busy;
    605   1.1  jdolecek 
    606   1.1  jdolecek 	/*
    607   1.2  jdolecek 	 * PIPE_WANTCLOSE processing only makes sense if pipe_busy is 0.
    608   1.1  jdolecek 	 */
    609   1.2  jdolecek 	if ((rpipe->pipe_busy == 0) && (rpipe->pipe_state & PIPE_WANTCLOSE)) {
    610   1.2  jdolecek 		rpipe->pipe_state &= ~(PIPE_WANTCLOSE|PIPE_WANTW);
    611  1.80        ad 		cv_broadcast(&rpipe->pipe_cv);
    612  1.35        pk 	} else if (bp->cnt < MINPIPESIZE) {
    613   1.1  jdolecek 		/*
    614   1.1  jdolecek 		 * Handle write blocking hysteresis.
    615   1.1  jdolecek 		 */
    616   1.1  jdolecek 		if (rpipe->pipe_state & PIPE_WANTW) {
    617   1.1  jdolecek 			rpipe->pipe_state &= ~PIPE_WANTW;
    618  1.80        ad 			cv_broadcast(&rpipe->pipe_cv);
    619   1.1  jdolecek 		}
    620   1.1  jdolecek 	}
    621   1.1  jdolecek 
    622   1.2  jdolecek 	/*
    623   1.2  jdolecek 	 * If anything was read off the buffer, signal to the writer it's
    624   1.2  jdolecek 	 * possible to write more data. Also send signal if we are here for the
    625   1.2  jdolecek 	 * first time after last write.
    626   1.2  jdolecek 	 */
    627  1.35        pk 	if ((bp->size - bp->cnt) >= PIPE_BUF
    628  1.35        pk 	    && (ocnt != bp->cnt || (rpipe->pipe_state & PIPE_SIGNALR))) {
    629  1.66  christos 		pipeselwakeup(rpipe, rpipe->pipe_peer, POLL_OUT);
    630   1.2  jdolecek 		rpipe->pipe_state &= ~PIPE_SIGNALR;
    631   1.2  jdolecek 	}
    632   1.1  jdolecek 
    633  1.90        ad 	mutex_exit(rpipe->pipe_lock);
    634   1.1  jdolecek 	return (error);
    635   1.1  jdolecek }
    636   1.1  jdolecek 
    637   1.2  jdolecek #ifndef PIPE_NODIRECT
    638   1.2  jdolecek /*
    639   1.2  jdolecek  * Allocate structure for loan transfer.
    640   1.2  jdolecek  */
    641  1.18       chs static int
    642  1.68   thorpej pipe_loan_alloc(struct pipe *wpipe, int npages)
    643   1.2  jdolecek {
    644  1.18       chs 	vsize_t len;
    645  1.18       chs 
    646  1.18       chs 	len = (vsize_t)npages << PAGE_SHIFT;
    647  1.65      yamt 	wpipe->pipe_map.kva = uvm_km_alloc(kernel_map, len, 0,
    648  1.65      yamt 	    UVM_KMF_VAONLY | UVM_KMF_WAITVA);
    649  1.22   thorpej 	if (wpipe->pipe_map.kva == 0)
    650   1.2  jdolecek 		return (ENOMEM);
    651   1.2  jdolecek 
    652  1.90        ad 	atomic_add_int(&amountpipekva, len);
    653   1.2  jdolecek 	wpipe->pipe_map.npages = npages;
    654  1.18       chs 	wpipe->pipe_map.pgs = malloc(npages * sizeof(struct vm_page *), M_PIPE,
    655  1.18       chs 	    M_WAITOK);
    656   1.2  jdolecek 	return (0);
    657   1.2  jdolecek }
    658   1.2  jdolecek 
    659   1.2  jdolecek /*
    660   1.2  jdolecek  * Free resources allocated for loan transfer.
    661   1.2  jdolecek  */
    662   1.2  jdolecek static void
    663  1.68   thorpej pipe_loan_free(struct pipe *wpipe)
    664   1.2  jdolecek {
    665  1.18       chs 	vsize_t len;
    666  1.18       chs 
    667  1.18       chs 	len = (vsize_t)wpipe->pipe_map.npages << PAGE_SHIFT;
    668  1.65      yamt 	uvm_km_free(kernel_map, wpipe->pipe_map.kva, len, UVM_KMF_VAONLY);
    669  1.22   thorpej 	wpipe->pipe_map.kva = 0;
    670  1.90        ad 	atomic_add_int(&amountpipekva, -len);
    671  1.18       chs 	free(wpipe->pipe_map.pgs, M_PIPE);
    672  1.18       chs 	wpipe->pipe_map.pgs = NULL;
    673   1.2  jdolecek }
    674   1.2  jdolecek 
    675   1.2  jdolecek /*
    676   1.2  jdolecek  * NetBSD direct write, using uvm_loan() mechanism.
    677   1.2  jdolecek  * This implements the pipe buffer write mechanism.  Note that only
    678   1.2  jdolecek  * a direct write OR a normal pipe write can be pending at any given time.
    679   1.2  jdolecek  * If there are any characters in the pipe buffer, the direct write will
    680   1.2  jdolecek  * be deferred until the receiving process grabs all of the bytes from
    681   1.2  jdolecek  * the pipe buffer.  Then the direct mapping write is set-up.
    682  1.35        pk  *
    683  1.35        pk  * Called with the long-term pipe lock held.
    684   1.2  jdolecek  */
    685  1.18       chs static int
    686  1.77      yamt pipe_direct_write(struct file *fp, struct pipe *wpipe, struct uio *uio)
    687   1.2  jdolecek {
    688   1.5  jdolecek 	int error, npages, j;
    689  1.18       chs 	struct vm_page **pgs;
    690   1.2  jdolecek 	vaddr_t bbase, kva, base, bend;
    691   1.2  jdolecek 	vsize_t blen, bcnt;
    692   1.5  jdolecek 	voff_t bpos;
    693   1.5  jdolecek 
    694  1.90        ad 	KASSERT(mutex_owned(wpipe->pipe_lock));
    695  1.35        pk 	KASSERT(wpipe->pipe_map.cnt == 0);
    696   1.2  jdolecek 
    697  1.90        ad 	mutex_exit(wpipe->pipe_lock);
    698  1.90        ad 
    699   1.2  jdolecek 	/*
    700  1.14  jdolecek 	 * Handle first PIPE_CHUNK_SIZE bytes of buffer. Deal with buffers
    701  1.14  jdolecek 	 * not aligned to PAGE_SIZE.
    702   1.5  jdolecek 	 */
    703  1.14  jdolecek 	bbase = (vaddr_t)uio->uio_iov->iov_base;
    704   1.5  jdolecek 	base = trunc_page(bbase);
    705  1.14  jdolecek 	bend = round_page(bbase + uio->uio_iov->iov_len);
    706   1.5  jdolecek 	blen = bend - base;
    707   1.5  jdolecek 	bpos = bbase - base;
    708   1.5  jdolecek 
    709   1.5  jdolecek 	if (blen > PIPE_DIRECT_CHUNK) {
    710   1.5  jdolecek 		blen = PIPE_DIRECT_CHUNK;
    711   1.5  jdolecek 		bend = base + blen;
    712   1.5  jdolecek 		bcnt = PIPE_DIRECT_CHUNK - bpos;
    713  1.18       chs 	} else {
    714  1.14  jdolecek 		bcnt = uio->uio_iov->iov_len;
    715  1.18       chs 	}
    716  1.18       chs 	npages = blen >> PAGE_SHIFT;
    717   1.5  jdolecek 
    718   1.5  jdolecek 	/*
    719   1.5  jdolecek 	 * Free the old kva if we need more pages than we have
    720   1.5  jdolecek 	 * allocated.
    721   1.2  jdolecek 	 */
    722  1.35        pk 	if (wpipe->pipe_map.kva != 0 && npages > wpipe->pipe_map.npages)
    723   1.5  jdolecek 		pipe_loan_free(wpipe);
    724   1.2  jdolecek 
    725   1.5  jdolecek 	/* Allocate new kva. */
    726  1.22   thorpej 	if (wpipe->pipe_map.kva == 0) {
    727  1.18       chs 		error = pipe_loan_alloc(wpipe, npages);
    728  1.90        ad 		if (error) {
    729  1.90        ad 			mutex_enter(wpipe->pipe_lock);
    730  1.35        pk 			return (error);
    731  1.90        ad 		}
    732  1.18       chs 	}
    733  1.18       chs 
    734   1.5  jdolecek 	/* Loan the write buffer memory from writer process */
    735  1.18       chs 	pgs = wpipe->pipe_map.pgs;
    736  1.71      yamt 	error = uvm_loan(&uio->uio_vmspace->vm_map, base, blen,
    737  1.35        pk 			 pgs, UVM_LOAN_TOPAGE);
    738  1.18       chs 	if (error) {
    739  1.35        pk 		pipe_loan_free(wpipe);
    740  1.90        ad 		mutex_enter(wpipe->pipe_lock);
    741  1.61      yamt 		return (ENOMEM); /* so that caller fallback to ordinary write */
    742  1.18       chs 	}
    743  1.18       chs 
    744   1.5  jdolecek 	/* Enter the loaned pages to kva */
    745   1.5  jdolecek 	kva = wpipe->pipe_map.kva;
    746  1.18       chs 	for (j = 0; j < npages; j++, kva += PAGE_SIZE) {
    747  1.18       chs 		pmap_kenter_pa(kva, VM_PAGE_TO_PHYS(pgs[j]), VM_PROT_READ);
    748  1.18       chs 	}
    749  1.12  jdolecek 	pmap_update(pmap_kernel());
    750   1.2  jdolecek 
    751  1.35        pk 	/* Now we can put the pipe in direct write mode */
    752  1.35        pk 	wpipe->pipe_map.pos = bpos;
    753  1.35        pk 	wpipe->pipe_map.cnt = bcnt;
    754  1.35        pk 
    755  1.35        pk 	/*
    756  1.85        ad 	 * But before we can let someone do a direct read, we
    757  1.85        ad 	 * have to wait until the pipe is drained.  Release the
    758  1.85        ad 	 * pipe lock while we wait.
    759  1.35        pk 	 */
    760  1.90        ad 	mutex_enter(wpipe->pipe_lock);
    761  1.85        ad 	wpipe->pipe_state |= PIPE_DIRECTW;
    762  1.35        pk 	pipeunlock(wpipe);
    763  1.35        pk 
    764  1.35        pk 	while (error == 0 && wpipe->pipe_buffer.cnt > 0) {
    765  1.35        pk 		if (wpipe->pipe_state & PIPE_WANTR) {
    766  1.35        pk 			wpipe->pipe_state &= ~PIPE_WANTR;
    767  1.80        ad 			cv_broadcast(&wpipe->pipe_cv);
    768  1.35        pk 		}
    769  1.35        pk 
    770  1.35        pk 		wpipe->pipe_state |= PIPE_WANTW;
    771  1.90        ad 		error = cv_wait_sig(&wpipe->pipe_cv, wpipe->pipe_lock);
    772  1.35        pk 		if (error == 0 && wpipe->pipe_state & PIPE_EOF)
    773   1.5  jdolecek 			error = EPIPE;
    774  1.35        pk 	}
    775  1.35        pk 
    776  1.35        pk 	/* Pipe is drained; next read will off the direct buffer */
    777  1.35        pk 	wpipe->pipe_state |= PIPE_DIRECTR;
    778  1.35        pk 
    779  1.35        pk 	/* Wait until the reader is done */
    780  1.35        pk 	while (error == 0 && (wpipe->pipe_state & PIPE_DIRECTR)) {
    781   1.5  jdolecek 		if (wpipe->pipe_state & PIPE_WANTR) {
    782   1.5  jdolecek 			wpipe->pipe_state &= ~PIPE_WANTR;
    783  1.80        ad 			cv_broadcast(&wpipe->pipe_cv);
    784   1.2  jdolecek 		}
    785  1.66  christos 		pipeselwakeup(wpipe, wpipe, POLL_IN);
    786  1.90        ad 		error = cv_wait_sig(&wpipe->pipe_cv, wpipe->pipe_lock);
    787  1.35        pk 		if (error == 0 && wpipe->pipe_state & PIPE_EOF)
    788  1.35        pk 			error = EPIPE;
    789   1.5  jdolecek 	}
    790   1.5  jdolecek 
    791  1.35        pk 	/* Take pipe out of direct write mode */
    792  1.35        pk 	wpipe->pipe_state &= ~(PIPE_DIRECTW | PIPE_DIRECTR);
    793   1.2  jdolecek 
    794  1.35        pk 	/* Acquire the pipe lock and cleanup */
    795  1.35        pk 	(void)pipelock(wpipe, 0);
    796  1.90        ad 	mutex_exit(wpipe->pipe_lock);
    797  1.85        ad 
    798  1.21       chs 	if (pgs != NULL) {
    799  1.21       chs 		pmap_kremove(wpipe->pipe_map.kva, blen);
    800  1.18       chs 		uvm_unloan(pgs, npages, UVM_LOAN_TOPAGE);
    801  1.21       chs 	}
    802   1.5  jdolecek 	if (error || amountpipekva > maxpipekva)
    803   1.5  jdolecek 		pipe_loan_free(wpipe);
    804   1.5  jdolecek 
    805  1.90        ad 	mutex_enter(wpipe->pipe_lock);
    806  1.15  jdolecek 	if (error) {
    807  1.66  christos 		pipeselwakeup(wpipe, wpipe, POLL_ERR);
    808   1.2  jdolecek 
    809   1.5  jdolecek 		/*
    810  1.15  jdolecek 		 * If nothing was read from what we offered, return error
    811  1.18       chs 		 * straight on. Otherwise update uio resid first. Caller
    812  1.15  jdolecek 		 * will deal with the error condition, returning short
    813  1.15  jdolecek 		 * write, error, or restarting the write(2) as appropriate.
    814   1.5  jdolecek 		 */
    815  1.15  jdolecek 		if (wpipe->pipe_map.cnt == bcnt) {
    816  1.35        pk 			wpipe->pipe_map.cnt = 0;
    817  1.80        ad 			cv_broadcast(&wpipe->pipe_cv);
    818  1.15  jdolecek 			return (error);
    819   1.2  jdolecek 		}
    820   1.2  jdolecek 
    821  1.15  jdolecek 		bcnt -= wpipe->pipe_map.cnt;
    822   1.5  jdolecek 	}
    823   1.2  jdolecek 
    824  1.18       chs 	uio->uio_resid -= bcnt;
    825   1.8  jdolecek 	/* uio_offset not updated, not set/used for write(2) */
    826  1.18       chs 	uio->uio_iov->iov_base = (char *)uio->uio_iov->iov_base + bcnt;
    827  1.14  jdolecek 	uio->uio_iov->iov_len -= bcnt;
    828  1.14  jdolecek 	if (uio->uio_iov->iov_len == 0) {
    829  1.14  jdolecek 		uio->uio_iov++;
    830  1.14  jdolecek 		uio->uio_iovcnt--;
    831  1.14  jdolecek 	}
    832   1.2  jdolecek 
    833  1.35        pk 	wpipe->pipe_map.cnt = 0;
    834  1.15  jdolecek 	return (error);
    835   1.2  jdolecek }
    836   1.2  jdolecek #endif /* !PIPE_NODIRECT */
    837   1.2  jdolecek 
    838   1.2  jdolecek static int
    839  1.77      yamt pipe_write(struct file *fp, off_t *offset, struct uio *uio, kauth_cred_t cred,
    840  1.77      yamt     int flags)
    841   1.1  jdolecek {
    842   1.1  jdolecek 	struct pipe *wpipe, *rpipe;
    843  1.35        pk 	struct pipebuf *bp;
    844  1.35        pk 	int error;
    845   1.1  jdolecek 
    846  1.35        pk 	/* We want to write to our peer */
    847   1.1  jdolecek 	rpipe = (struct pipe *) fp->f_data;
    848  1.90        ad 	error = 0;
    849  1.35        pk 
    850  1.90        ad 	mutex_enter(rpipe->pipe_lock);
    851   1.1  jdolecek 	wpipe = rpipe->pipe_peer;
    852   1.1  jdolecek 
    853   1.1  jdolecek 	/*
    854  1.35        pk 	 * Detect loss of pipe read side, issue SIGPIPE if lost.
    855   1.1  jdolecek 	 */
    856  1.90        ad 	if (wpipe == NULL) {
    857  1.90        ad 		mutex_exit(rpipe->pipe_lock);
    858  1.90        ad 		return EPIPE;
    859  1.35        pk 	} else if ((wpipe->pipe_state & PIPE_EOF) != 0) {
    860  1.90        ad 		mutex_exit(rpipe->pipe_lock);
    861  1.90        ad 		return EPIPE;
    862  1.24  jdolecek 	}
    863   1.1  jdolecek 	++wpipe->pipe_busy;
    864   1.1  jdolecek 
    865  1.35        pk 	/* Aquire the long-term pipe lock */
    866  1.35        pk 	if ((error = pipelock(wpipe,1)) != 0) {
    867  1.35        pk 		--wpipe->pipe_busy;
    868  1.35        pk 		if (wpipe->pipe_busy == 0
    869  1.35        pk 		    && (wpipe->pipe_state & PIPE_WANTCLOSE)) {
    870  1.35        pk 			wpipe->pipe_state &= ~(PIPE_WANTCLOSE | PIPE_WANTR);
    871  1.80        ad 			cv_broadcast(&wpipe->pipe_cv);
    872  1.35        pk 		}
    873  1.90        ad 		mutex_exit(rpipe->pipe_lock);
    874  1.35        pk 		return (error);
    875  1.35        pk 	}
    876  1.35        pk 
    877  1.35        pk 	bp = &wpipe->pipe_buffer;
    878  1.35        pk 
    879   1.1  jdolecek 	/*
    880  1.35        pk 	 * If it is advantageous to resize the pipe buffer, do so.
    881   1.1  jdolecek 	 */
    882   1.1  jdolecek 	if ((uio->uio_resid > PIPE_SIZE) &&
    883  1.35        pk 	    (nbigpipe < maxbigpipes) &&
    884   1.2  jdolecek #ifndef PIPE_NODIRECT
    885  1.35        pk 	    (wpipe->pipe_state & PIPE_DIRECTW) == 0 &&
    886   1.2  jdolecek #endif
    887  1.35        pk 	    (bp->size <= PIPE_SIZE) && (bp->cnt == 0)) {
    888   1.1  jdolecek 
    889  1.35        pk 		if (pipespace(wpipe, BIG_PIPE_SIZE) == 0)
    890  1.90        ad 			atomic_inc_uint(&nbigpipe);
    891  1.24  jdolecek 	}
    892   1.1  jdolecek 
    893   1.1  jdolecek 	while (uio->uio_resid) {
    894  1.26   thorpej 		size_t space;
    895   1.1  jdolecek 
    896   1.1  jdolecek #ifndef PIPE_NODIRECT
    897   1.1  jdolecek 		/*
    898  1.35        pk 		 * Pipe buffered writes cannot be coincidental with
    899  1.35        pk 		 * direct writes.  Also, only one direct write can be
    900  1.35        pk 		 * in progress at any one time.  We wait until the currently
    901  1.35        pk 		 * executing direct write is completed before continuing.
    902  1.35        pk 		 *
    903  1.35        pk 		 * We break out if a signal occurs or the reader goes away.
    904  1.35        pk 		 */
    905  1.35        pk 		while (error == 0 && wpipe->pipe_state & PIPE_DIRECTW) {
    906  1.35        pk 			if (wpipe->pipe_state & PIPE_WANTR) {
    907  1.35        pk 				wpipe->pipe_state &= ~PIPE_WANTR;
    908  1.80        ad 				cv_broadcast(&wpipe->pipe_cv);
    909  1.35        pk 			}
    910  1.35        pk 			pipeunlock(wpipe);
    911  1.80        ad 			error = cv_wait_sig(&wpipe->pipe_cv,
    912  1.90        ad 			    wpipe->pipe_lock);
    913  1.35        pk 			(void)pipelock(wpipe, 0);
    914  1.35        pk 			if (wpipe->pipe_state & PIPE_EOF)
    915  1.35        pk 				error = EPIPE;
    916  1.35        pk 		}
    917  1.35        pk 		if (error)
    918  1.35        pk 			break;
    919  1.35        pk 
    920  1.35        pk 		/*
    921   1.1  jdolecek 		 * If the transfer is large, we can gain performance if
    922   1.1  jdolecek 		 * we do process-to-process copies directly.
    923   1.1  jdolecek 		 * If the write is non-blocking, we don't use the
    924   1.1  jdolecek 		 * direct write mechanism.
    925   1.1  jdolecek 		 *
    926   1.1  jdolecek 		 * The direct write mechanism will detect the reader going
    927   1.1  jdolecek 		 * away on us.
    928   1.1  jdolecek 		 */
    929  1.14  jdolecek 		if ((uio->uio_iov->iov_len >= PIPE_MINDIRECT) &&
    930   1.1  jdolecek 		    (fp->f_flag & FNONBLOCK) == 0 &&
    931   1.2  jdolecek 		    (wpipe->pipe_map.kva || (amountpipekva < limitpipekva))) {
    932  1.42  christos 			error = pipe_direct_write(fp, wpipe, uio);
    933   1.5  jdolecek 
    934   1.5  jdolecek 			/*
    935  1.49       wiz 			 * Break out if error occurred, unless it's ENOMEM.
    936  1.14  jdolecek 			 * ENOMEM means we failed to allocate some resources
    937  1.14  jdolecek 			 * for direct write, so we just fallback to ordinary
    938  1.14  jdolecek 			 * write. If the direct write was successful,
    939  1.14  jdolecek 			 * process rest of data via ordinary write.
    940   1.5  jdolecek 			 */
    941  1.35        pk 			if (error == 0)
    942  1.14  jdolecek 				continue;
    943  1.14  jdolecek 
    944   1.5  jdolecek 			if (error != ENOMEM)
    945   1.1  jdolecek 				break;
    946   1.1  jdolecek 		}
    947   1.2  jdolecek #endif /* PIPE_NODIRECT */
    948   1.1  jdolecek 
    949  1.35        pk 		space = bp->size - bp->cnt;
    950   1.1  jdolecek 
    951   1.1  jdolecek 		/* Writes of size <= PIPE_BUF must be atomic. */
    952  1.14  jdolecek 		if ((space < uio->uio_resid) && (uio->uio_resid <= PIPE_BUF))
    953   1.1  jdolecek 			space = 0;
    954   1.1  jdolecek 
    955  1.16   mycroft 		if (space > 0) {
    956   1.2  jdolecek 			int size;	/* Transfer size */
    957   1.2  jdolecek 			int segsize;	/* first segment to transfer */
    958   1.2  jdolecek 
    959   1.2  jdolecek 			/*
    960   1.2  jdolecek 			 * Transfer size is minimum of uio transfer
    961   1.2  jdolecek 			 * and free space in pipe buffer.
    962   1.2  jdolecek 			 */
    963   1.2  jdolecek 			if (space > uio->uio_resid)
    964   1.2  jdolecek 				size = uio->uio_resid;
    965   1.2  jdolecek 			else
    966   1.2  jdolecek 				size = space;
    967   1.2  jdolecek 			/*
    968  1.63     perry 			 * First segment to transfer is minimum of
    969   1.2  jdolecek 			 * transfer size and contiguous space in
    970   1.2  jdolecek 			 * pipe buffer.  If first segment to transfer
    971   1.2  jdolecek 			 * is less than the transfer size, we've got
    972   1.2  jdolecek 			 * a wraparound in the buffer.
    973   1.2  jdolecek 			 */
    974  1.35        pk 			segsize = bp->size - bp->in;
    975   1.2  jdolecek 			if (segsize > size)
    976   1.2  jdolecek 				segsize = size;
    977  1.18       chs 
    978   1.2  jdolecek 			/* Transfer first segment */
    979  1.90        ad 			mutex_exit(wpipe->pipe_lock);
    980  1.79  christos 			error = uiomove((char *)bp->buffer + bp->in, segsize,
    981  1.79  christos 			    uio);
    982  1.18       chs 
    983   1.2  jdolecek 			if (error == 0 && segsize < size) {
    984  1.63     perry 				/*
    985   1.2  jdolecek 				 * Transfer remaining part now, to
    986   1.2  jdolecek 				 * support atomic writes.  Wraparound
    987   1.2  jdolecek 				 * happened.
    988   1.2  jdolecek 				 */
    989   1.2  jdolecek #ifdef DEBUG
    990  1.35        pk 				if (bp->in + segsize != bp->size)
    991   1.2  jdolecek 					panic("Expected pipe buffer wraparound disappeared");
    992   1.2  jdolecek #endif
    993  1.18       chs 
    994  1.79  christos 				error = uiomove(bp->buffer,
    995  1.79  christos 				    size - segsize, uio);
    996   1.2  jdolecek 			}
    997  1.90        ad 			mutex_enter(wpipe->pipe_lock);
    998  1.35        pk 			if (error)
    999  1.35        pk 				break;
   1000  1.35        pk 
   1001  1.35        pk 			bp->in += size;
   1002  1.35        pk 			if (bp->in >= bp->size) {
   1003   1.2  jdolecek #ifdef DEBUG
   1004  1.35        pk 				if (bp->in != size - segsize + bp->size)
   1005  1.35        pk 					panic("Expected wraparound bad");
   1006   1.2  jdolecek #endif
   1007  1.35        pk 				bp->in = size - segsize;
   1008  1.35        pk 			}
   1009  1.18       chs 
   1010  1.35        pk 			bp->cnt += size;
   1011   1.2  jdolecek #ifdef DEBUG
   1012  1.35        pk 			if (bp->cnt > bp->size)
   1013  1.35        pk 				panic("Pipe buffer overflow");
   1014   1.2  jdolecek #endif
   1015   1.1  jdolecek 		} else {
   1016   1.1  jdolecek 			/*
   1017   1.1  jdolecek 			 * If the "read-side" has been blocked, wake it up now.
   1018   1.1  jdolecek 			 */
   1019   1.1  jdolecek 			if (wpipe->pipe_state & PIPE_WANTR) {
   1020   1.1  jdolecek 				wpipe->pipe_state &= ~PIPE_WANTR;
   1021  1.80        ad 				cv_broadcast(&wpipe->pipe_cv);
   1022   1.1  jdolecek 			}
   1023   1.1  jdolecek 
   1024   1.1  jdolecek 			/*
   1025   1.1  jdolecek 			 * don't block on non-blocking I/O
   1026   1.1  jdolecek 			 */
   1027   1.1  jdolecek 			if (fp->f_flag & FNONBLOCK) {
   1028   1.1  jdolecek 				error = EAGAIN;
   1029   1.1  jdolecek 				break;
   1030   1.1  jdolecek 			}
   1031   1.1  jdolecek 
   1032   1.1  jdolecek 			/*
   1033   1.1  jdolecek 			 * We have no more space and have something to offer,
   1034   1.1  jdolecek 			 * wake up select/poll.
   1035   1.1  jdolecek 			 */
   1036  1.35        pk 			if (bp->cnt)
   1037  1.66  christos 				pipeselwakeup(wpipe, wpipe, POLL_OUT);
   1038   1.1  jdolecek 
   1039  1.35        pk 			pipeunlock(wpipe);
   1040   1.1  jdolecek 			wpipe->pipe_state |= PIPE_WANTW;
   1041  1.90        ad 			error = cv_wait_sig(&wpipe->pipe_cv, wpipe->pipe_lock);
   1042  1.35        pk 			(void)pipelock(wpipe, 0);
   1043   1.1  jdolecek 			if (error != 0)
   1044   1.1  jdolecek 				break;
   1045   1.1  jdolecek 			/*
   1046   1.1  jdolecek 			 * If read side wants to go away, we just issue a signal
   1047   1.1  jdolecek 			 * to ourselves.
   1048   1.1  jdolecek 			 */
   1049   1.1  jdolecek 			if (wpipe->pipe_state & PIPE_EOF) {
   1050   1.1  jdolecek 				error = EPIPE;
   1051   1.1  jdolecek 				break;
   1052  1.18       chs 			}
   1053   1.1  jdolecek 		}
   1054   1.1  jdolecek 	}
   1055   1.1  jdolecek 
   1056   1.1  jdolecek 	--wpipe->pipe_busy;
   1057   1.2  jdolecek 	if ((wpipe->pipe_busy == 0) && (wpipe->pipe_state & PIPE_WANTCLOSE)) {
   1058   1.2  jdolecek 		wpipe->pipe_state &= ~(PIPE_WANTCLOSE | PIPE_WANTR);
   1059  1.80        ad 		cv_broadcast(&wpipe->pipe_cv);
   1060  1.35        pk 	} else if (bp->cnt > 0) {
   1061   1.1  jdolecek 		/*
   1062   1.1  jdolecek 		 * If we have put any characters in the buffer, we wake up
   1063   1.1  jdolecek 		 * the reader.
   1064   1.1  jdolecek 		 */
   1065   1.1  jdolecek 		if (wpipe->pipe_state & PIPE_WANTR) {
   1066   1.1  jdolecek 			wpipe->pipe_state &= ~PIPE_WANTR;
   1067  1.80        ad 			cv_broadcast(&wpipe->pipe_cv);
   1068   1.1  jdolecek 		}
   1069   1.1  jdolecek 	}
   1070   1.1  jdolecek 
   1071   1.1  jdolecek 	/*
   1072   1.1  jdolecek 	 * Don't return EPIPE if I/O was successful
   1073   1.1  jdolecek 	 */
   1074  1.35        pk 	if (error == EPIPE && bp->cnt == 0 && uio->uio_resid == 0)
   1075   1.1  jdolecek 		error = 0;
   1076   1.1  jdolecek 
   1077   1.1  jdolecek 	if (error == 0)
   1078  1.73    kardel 		getmicrotime(&wpipe->pipe_mtime);
   1079   1.1  jdolecek 
   1080   1.1  jdolecek 	/*
   1081   1.2  jdolecek 	 * We have something to offer, wake up select/poll.
   1082   1.2  jdolecek 	 * wpipe->pipe_map.cnt is always 0 in this point (direct write
   1083  1.14  jdolecek 	 * is only done synchronously), so check only wpipe->pipe_buffer.cnt
   1084   1.1  jdolecek 	 */
   1085  1.35        pk 	if (bp->cnt)
   1086  1.66  christos 		pipeselwakeup(wpipe, wpipe, POLL_OUT);
   1087   1.1  jdolecek 
   1088   1.2  jdolecek 	/*
   1089   1.2  jdolecek 	 * Arrange for next read(2) to do a signal.
   1090   1.2  jdolecek 	 */
   1091   1.2  jdolecek 	wpipe->pipe_state |= PIPE_SIGNALR;
   1092   1.2  jdolecek 
   1093  1.35        pk 	pipeunlock(wpipe);
   1094  1.90        ad 	mutex_exit(wpipe->pipe_lock);
   1095   1.1  jdolecek 	return (error);
   1096   1.1  jdolecek }
   1097   1.1  jdolecek 
   1098   1.1  jdolecek /*
   1099   1.1  jdolecek  * we implement a very minimal set of ioctls for compatibility with sockets.
   1100   1.1  jdolecek  */
   1101   1.1  jdolecek int
   1102  1.69  christos pipe_ioctl(struct file *fp, u_long cmd, void *data, struct lwp *l)
   1103   1.1  jdolecek {
   1104  1.35        pk 	struct pipe *pipe = (struct pipe *)fp->f_data;
   1105  1.69  christos 	struct proc *p = l->l_proc;
   1106   1.1  jdolecek 
   1107   1.1  jdolecek 	switch (cmd) {
   1108   1.1  jdolecek 
   1109   1.1  jdolecek 	case FIONBIO:
   1110   1.1  jdolecek 		return (0);
   1111   1.1  jdolecek 
   1112   1.1  jdolecek 	case FIOASYNC:
   1113  1.90        ad 		mutex_enter(pipe->pipe_lock);
   1114   1.1  jdolecek 		if (*(int *)data) {
   1115  1.35        pk 			pipe->pipe_state |= PIPE_ASYNC;
   1116   1.1  jdolecek 		} else {
   1117  1.35        pk 			pipe->pipe_state &= ~PIPE_ASYNC;
   1118   1.1  jdolecek 		}
   1119  1.90        ad 		mutex_exit(pipe->pipe_lock);
   1120   1.1  jdolecek 		return (0);
   1121   1.1  jdolecek 
   1122   1.1  jdolecek 	case FIONREAD:
   1123  1.90        ad 		mutex_enter(pipe->pipe_lock);
   1124   1.2  jdolecek #ifndef PIPE_NODIRECT
   1125  1.35        pk 		if (pipe->pipe_state & PIPE_DIRECTW)
   1126  1.35        pk 			*(int *)data = pipe->pipe_map.cnt;
   1127   1.1  jdolecek 		else
   1128   1.2  jdolecek #endif
   1129  1.35        pk 			*(int *)data = pipe->pipe_buffer.cnt;
   1130  1.90        ad 		mutex_exit(pipe->pipe_lock);
   1131   1.1  jdolecek 		return (0);
   1132   1.1  jdolecek 
   1133  1.59  wrstuden 	case FIONWRITE:
   1134  1.59  wrstuden 		/* Look at other side */
   1135  1.59  wrstuden 		pipe = pipe->pipe_peer;
   1136  1.90        ad 		mutex_enter(pipe->pipe_lock);
   1137  1.59  wrstuden #ifndef PIPE_NODIRECT
   1138  1.59  wrstuden 		if (pipe->pipe_state & PIPE_DIRECTW)
   1139  1.59  wrstuden 			*(int *)data = pipe->pipe_map.cnt;
   1140  1.59  wrstuden 		else
   1141  1.59  wrstuden #endif
   1142  1.59  wrstuden 			*(int *)data = pipe->pipe_buffer.cnt;
   1143  1.90        ad 		mutex_exit(pipe->pipe_lock);
   1144  1.59  wrstuden 		return (0);
   1145  1.59  wrstuden 
   1146  1.59  wrstuden 	case FIONSPACE:
   1147  1.59  wrstuden 		/* Look at other side */
   1148  1.59  wrstuden 		pipe = pipe->pipe_peer;
   1149  1.90        ad 		mutex_enter(pipe->pipe_lock);
   1150  1.59  wrstuden #ifndef PIPE_NODIRECT
   1151  1.59  wrstuden 		/*
   1152  1.59  wrstuden 		 * If we're in direct-mode, we don't really have a
   1153  1.59  wrstuden 		 * send queue, and any other write will block. Thus
   1154  1.59  wrstuden 		 * zero seems like the best answer.
   1155  1.59  wrstuden 		 */
   1156  1.59  wrstuden 		if (pipe->pipe_state & PIPE_DIRECTW)
   1157  1.59  wrstuden 			*(int *)data = 0;
   1158  1.59  wrstuden 		else
   1159  1.59  wrstuden #endif
   1160  1.59  wrstuden 			*(int *)data = pipe->pipe_buffer.size -
   1161  1.82        ad 			    pipe->pipe_buffer.cnt;
   1162  1.90        ad 		mutex_exit(pipe->pipe_lock);
   1163  1.59  wrstuden 		return (0);
   1164  1.59  wrstuden 
   1165   1.2  jdolecek 	case TIOCSPGRP:
   1166  1.43  jdolecek 	case FIOSETOWN:
   1167  1.43  jdolecek 		return fsetown(p, &pipe->pipe_pgid, cmd, data);
   1168   1.2  jdolecek 
   1169   1.2  jdolecek 	case TIOCGPGRP:
   1170  1.43  jdolecek 	case FIOGETOWN:
   1171  1.43  jdolecek 		return fgetown(p, pipe->pipe_pgid, cmd, data);
   1172   1.1  jdolecek 
   1173   1.1  jdolecek 	}
   1174  1.25    atatat 	return (EPASSTHROUGH);
   1175   1.1  jdolecek }
   1176   1.1  jdolecek 
   1177   1.1  jdolecek int
   1178  1.69  christos pipe_poll(struct file *fp, int events, struct lwp *l)
   1179   1.1  jdolecek {
   1180   1.1  jdolecek 	struct pipe *rpipe = (struct pipe *)fp->f_data;
   1181   1.1  jdolecek 	struct pipe *wpipe;
   1182  1.35        pk 	int eof = 0;
   1183   1.1  jdolecek 	int revents = 0;
   1184   1.1  jdolecek 
   1185  1.90        ad 	mutex_enter(rpipe->pipe_lock);
   1186   1.1  jdolecek 	wpipe = rpipe->pipe_peer;
   1187  1.35        pk 
   1188   1.1  jdolecek 	if (events & (POLLIN | POLLRDNORM))
   1189   1.2  jdolecek 		if ((rpipe->pipe_buffer.cnt > 0) ||
   1190   1.2  jdolecek #ifndef PIPE_NODIRECT
   1191  1.35        pk 		    (rpipe->pipe_state & PIPE_DIRECTR) ||
   1192   1.2  jdolecek #endif
   1193   1.1  jdolecek 		    (rpipe->pipe_state & PIPE_EOF))
   1194   1.1  jdolecek 			revents |= events & (POLLIN | POLLRDNORM);
   1195   1.1  jdolecek 
   1196  1.35        pk 	eof |= (rpipe->pipe_state & PIPE_EOF);
   1197  1.35        pk 
   1198  1.35        pk 	if (wpipe == NULL)
   1199  1.35        pk 		revents |= events & (POLLOUT | POLLWRNORM);
   1200  1.35        pk 	else {
   1201  1.35        pk 		if (events & (POLLOUT | POLLWRNORM))
   1202  1.35        pk 			if ((wpipe->pipe_state & PIPE_EOF) || (
   1203   1.2  jdolecek #ifndef PIPE_NODIRECT
   1204  1.35        pk 			     (wpipe->pipe_state & PIPE_DIRECTW) == 0 &&
   1205   1.2  jdolecek #endif
   1206  1.35        pk 			     (wpipe->pipe_buffer.size - wpipe->pipe_buffer.cnt) >= PIPE_BUF))
   1207  1.35        pk 				revents |= events & (POLLOUT | POLLWRNORM);
   1208   1.1  jdolecek 
   1209  1.35        pk 		eof |= (wpipe->pipe_state & PIPE_EOF);
   1210  1.35        pk 	}
   1211  1.35        pk 
   1212  1.35        pk 	if (wpipe == NULL || eof)
   1213   1.1  jdolecek 		revents |= POLLHUP;
   1214   1.1  jdolecek 
   1215   1.1  jdolecek 	if (revents == 0) {
   1216  1.35        pk 		if (events & (POLLIN | POLLRDNORM))
   1217  1.69  christos 			selrecord(l, &rpipe->pipe_sel);
   1218   1.1  jdolecek 
   1219  1.35        pk 		if (events & (POLLOUT | POLLWRNORM))
   1220  1.69  christos 			selrecord(l, &wpipe->pipe_sel);
   1221   1.1  jdolecek 	}
   1222  1.90        ad 	mutex_exit(rpipe->pipe_lock);
   1223   1.1  jdolecek 
   1224   1.1  jdolecek 	return (revents);
   1225   1.1  jdolecek }
   1226   1.1  jdolecek 
   1227   1.1  jdolecek static int
   1228  1.77      yamt pipe_stat(struct file *fp, struct stat *ub, struct lwp *l)
   1229   1.1  jdolecek {
   1230   1.1  jdolecek 	struct pipe *pipe = (struct pipe *)fp->f_data;
   1231   1.1  jdolecek 
   1232  1.79  christos 	memset((void *)ub, 0, sizeof(*ub));
   1233  1.32  jdolecek 	ub->st_mode = S_IFIFO | S_IRUSR | S_IWUSR;
   1234   1.1  jdolecek 	ub->st_blksize = pipe->pipe_buffer.size;
   1235  1.64  christos 	if (ub->st_blksize == 0 && pipe->pipe_peer)
   1236  1.64  christos 		ub->st_blksize = pipe->pipe_peer->pipe_buffer.size;
   1237   1.1  jdolecek 	ub->st_size = pipe->pipe_buffer.cnt;
   1238   1.2  jdolecek 	ub->st_blocks = (ub->st_size) ? 1 : 0;
   1239  1.60    atatat 	TIMEVAL_TO_TIMESPEC(&pipe->pipe_atime, &ub->st_atimespec);
   1240   1.2  jdolecek 	TIMEVAL_TO_TIMESPEC(&pipe->pipe_mtime, &ub->st_mtimespec);
   1241   1.2  jdolecek 	TIMEVAL_TO_TIMESPEC(&pipe->pipe_ctime, &ub->st_ctimespec);
   1242  1.72      elad 	ub->st_uid = kauth_cred_geteuid(fp->f_cred);
   1243  1.72      elad 	ub->st_gid = kauth_cred_getegid(fp->f_cred);
   1244  1.82        ad 
   1245   1.1  jdolecek 	/*
   1246   1.1  jdolecek 	 * Left as 0: st_dev, st_ino, st_nlink, st_rdev, st_flags, st_gen.
   1247   1.1  jdolecek 	 * XXX (st_dev, st_ino) should be unique.
   1248   1.1  jdolecek 	 */
   1249   1.1  jdolecek 	return (0);
   1250   1.1  jdolecek }
   1251   1.1  jdolecek 
   1252   1.1  jdolecek /* ARGSUSED */
   1253   1.1  jdolecek static int
   1254  1.77      yamt pipe_close(struct file *fp, struct lwp *l)
   1255   1.1  jdolecek {
   1256  1.35        pk 	struct pipe *pipe = (struct pipe *)fp->f_data;
   1257   1.1  jdolecek 
   1258   1.1  jdolecek 	fp->f_data = NULL;
   1259  1.42  christos 	pipeclose(fp, pipe);
   1260   1.1  jdolecek 	return (0);
   1261   1.1  jdolecek }
   1262   1.1  jdolecek 
   1263   1.1  jdolecek static void
   1264  1.68   thorpej pipe_free_kmem(struct pipe *pipe)
   1265   1.1  jdolecek {
   1266   1.1  jdolecek 
   1267  1.35        pk 	if (pipe->pipe_buffer.buffer != NULL) {
   1268  1.35        pk 		if (pipe->pipe_buffer.size > PIPE_SIZE)
   1269  1.90        ad 			atomic_dec_uint(&nbigpipe);
   1270  1.90        ad 		atomic_add_int(&amountpipekva, -pipe->pipe_buffer.size);
   1271   1.2  jdolecek 		uvm_km_free(kernel_map,
   1272  1.35        pk 			(vaddr_t)pipe->pipe_buffer.buffer,
   1273  1.65      yamt 			pipe->pipe_buffer.size, UVM_KMF_PAGEABLE);
   1274  1.35        pk 		pipe->pipe_buffer.buffer = NULL;
   1275   1.1  jdolecek 	}
   1276   1.1  jdolecek #ifndef PIPE_NODIRECT
   1277  1.35        pk 	if (pipe->pipe_map.kva != 0) {
   1278  1.35        pk 		pipe_loan_free(pipe);
   1279  1.35        pk 		pipe->pipe_map.cnt = 0;
   1280  1.35        pk 		pipe->pipe_map.kva = 0;
   1281  1.35        pk 		pipe->pipe_map.pos = 0;
   1282  1.35        pk 		pipe->pipe_map.npages = 0;
   1283   1.1  jdolecek 	}
   1284   1.2  jdolecek #endif /* !PIPE_NODIRECT */
   1285   1.1  jdolecek }
   1286   1.1  jdolecek 
   1287   1.1  jdolecek /*
   1288   1.1  jdolecek  * shutdown the pipe
   1289   1.1  jdolecek  */
   1290   1.1  jdolecek static void
   1291  1.77      yamt pipeclose(struct file *fp, struct pipe *pipe)
   1292   1.1  jdolecek {
   1293  1.90        ad 	struct pipe_mutex *mutex;
   1294   1.1  jdolecek 	struct pipe *ppipe;
   1295  1.90        ad 	u_int refcnt;
   1296   1.1  jdolecek 
   1297  1.35        pk 	if (pipe == NULL)
   1298   1.2  jdolecek 		return;
   1299   1.2  jdolecek 
   1300  1.90        ad 	mutex_enter(pipe->pipe_lock);
   1301  1.66  christos 	pipeselwakeup(pipe, pipe, POLL_HUP);
   1302   1.1  jdolecek 
   1303   1.2  jdolecek 	/*
   1304   1.2  jdolecek 	 * If the other side is blocked, wake it up saying that
   1305   1.2  jdolecek 	 * we want to close it down.
   1306   1.2  jdolecek 	 */
   1307  1.66  christos 	pipe->pipe_state |= PIPE_EOF;
   1308  1.82        ad 	if (pipe->pipe_busy) {
   1309  1.82        ad 		while (pipe->pipe_busy) {
   1310  1.82        ad 			cv_broadcast(&pipe->pipe_cv);
   1311  1.82        ad 			pipe->pipe_state |= PIPE_WANTCLOSE;
   1312  1.90        ad 			cv_wait_sig(&pipe->pipe_cv, pipe->pipe_lock);
   1313  1.82        ad 		}
   1314   1.2  jdolecek 	}
   1315   1.1  jdolecek 
   1316   1.2  jdolecek 	/*
   1317   1.2  jdolecek 	 * Disconnect from peer
   1318   1.2  jdolecek 	 */
   1319  1.35        pk 	if ((ppipe = pipe->pipe_peer) != NULL) {
   1320  1.66  christos 		pipeselwakeup(ppipe, ppipe, POLL_HUP);
   1321   1.2  jdolecek 		ppipe->pipe_state |= PIPE_EOF;
   1322  1.80        ad 		cv_broadcast(&ppipe->pipe_cv);
   1323   1.2  jdolecek 		ppipe->pipe_peer = NULL;
   1324   1.1  jdolecek 	}
   1325  1.35        pk 
   1326  1.67      yamt 	KASSERT((pipe->pipe_state & PIPE_LOCKFL) == 0);
   1327  1.67      yamt 
   1328  1.90        ad 	mutex = (struct pipe_mutex *)pipe->pipe_lock;
   1329  1.90        ad 	refcnt = --(mutex->pm_refcnt);
   1330  1.90        ad 	KASSERT(refcnt == 0 || refcnt == 1);
   1331  1.90        ad 	mutex_exit(pipe->pipe_lock);
   1332  1.35        pk 
   1333   1.2  jdolecek 	/*
   1334   1.2  jdolecek 	 * free resources
   1335   1.2  jdolecek 	 */
   1336  1.35        pk 	pipe_free_kmem(pipe);
   1337  1.80        ad 	cv_destroy(&pipe->pipe_cv);
   1338  1.80        ad 	cv_destroy(&pipe->pipe_lkcv);
   1339  1.86        ad 	seldestroy(&pipe->pipe_sel);
   1340  1.87        ad 	pool_cache_put(pipe_cache, pipe);
   1341  1.90        ad 	if (refcnt == 0)
   1342  1.90        ad 		pool_cache_put(pipe_mutex_cache, mutex);
   1343   1.1  jdolecek }
   1344   1.1  jdolecek 
   1345  1.27  jdolecek static void
   1346  1.27  jdolecek filt_pipedetach(struct knote *kn)
   1347   1.1  jdolecek {
   1348  1.92        ad 	struct pipe *pipe;
   1349  1.92        ad 	kmutex_t *lock;
   1350  1.92        ad 
   1351  1.92        ad 	pipe = (struct pipe *)kn->kn_fp->f_data;
   1352  1.92        ad 	lock = pipe->pipe_lock;
   1353   1.1  jdolecek 
   1354  1.92        ad 	mutex_enter(lock);
   1355  1.82        ad 
   1356  1.27  jdolecek 	switch(kn->kn_filter) {
   1357   1.1  jdolecek 	case EVFILT_WRITE:
   1358  1.27  jdolecek 		/* need the peer structure, not our own */
   1359  1.35        pk 		pipe = pipe->pipe_peer;
   1360  1.27  jdolecek 
   1361  1.27  jdolecek 		/* if reader end already closed, just return */
   1362  1.82        ad 		if (pipe == NULL) {
   1363  1.92        ad 			mutex_exit(lock);
   1364  1.27  jdolecek 			return;
   1365  1.82        ad 		}
   1366  1.27  jdolecek 
   1367   1.1  jdolecek 		break;
   1368   1.1  jdolecek 	default:
   1369  1.27  jdolecek 		/* nothing to do */
   1370  1.29  kristerw 		break;
   1371   1.1  jdolecek 	}
   1372  1.24  jdolecek 
   1373  1.27  jdolecek #ifdef DIAGNOSTIC
   1374  1.35        pk 	if (kn->kn_hook != pipe)
   1375  1.27  jdolecek 		panic("filt_pipedetach: inconsistent knote");
   1376  1.27  jdolecek #endif
   1377   1.1  jdolecek 
   1378  1.35        pk 	SLIST_REMOVE(&pipe->pipe_sel.sel_klist, kn, knote, kn_selnext);
   1379  1.92        ad 	mutex_exit(lock);
   1380   1.1  jdolecek }
   1381   1.1  jdolecek 
   1382   1.1  jdolecek /*ARGSUSED*/
   1383   1.1  jdolecek static int
   1384   1.1  jdolecek filt_piperead(struct knote *kn, long hint)
   1385   1.1  jdolecek {
   1386   1.1  jdolecek 	struct pipe *rpipe = (struct pipe *)kn->kn_fp->f_data;
   1387  1.82        ad 	struct pipe *wpipe;
   1388  1.82        ad 
   1389  1.83        ad 	if ((hint & NOTE_SUBMIT) == 0) {
   1390  1.90        ad 		mutex_enter(rpipe->pipe_lock);
   1391  1.83        ad 	}
   1392  1.82        ad 	wpipe = rpipe->pipe_peer;
   1393  1.83        ad 	kn->kn_data = rpipe->pipe_buffer.cnt;
   1394   1.1  jdolecek 
   1395   1.1  jdolecek 	if ((kn->kn_data == 0) && (rpipe->pipe_state & PIPE_DIRECTW))
   1396   1.1  jdolecek 		kn->kn_data = rpipe->pipe_map.cnt;
   1397   1.1  jdolecek 
   1398   1.1  jdolecek 	if ((rpipe->pipe_state & PIPE_EOF) ||
   1399   1.1  jdolecek 	    (wpipe == NULL) || (wpipe->pipe_state & PIPE_EOF)) {
   1400  1.24  jdolecek 		kn->kn_flags |= EV_EOF;
   1401  1.83        ad 		if ((hint & NOTE_SUBMIT) == 0) {
   1402  1.90        ad 			mutex_exit(rpipe->pipe_lock);
   1403  1.83        ad 		}
   1404   1.1  jdolecek 		return (1);
   1405   1.1  jdolecek 	}
   1406  1.83        ad 
   1407  1.83        ad 	if ((hint & NOTE_SUBMIT) == 0) {
   1408  1.90        ad 		mutex_exit(rpipe->pipe_lock);
   1409  1.83        ad 	}
   1410   1.1  jdolecek 	return (kn->kn_data > 0);
   1411   1.1  jdolecek }
   1412   1.1  jdolecek 
   1413   1.1  jdolecek /*ARGSUSED*/
   1414   1.1  jdolecek static int
   1415   1.1  jdolecek filt_pipewrite(struct knote *kn, long hint)
   1416   1.1  jdolecek {
   1417   1.1  jdolecek 	struct pipe *rpipe = (struct pipe *)kn->kn_fp->f_data;
   1418  1.82        ad 	struct pipe *wpipe;
   1419  1.82        ad 
   1420  1.83        ad 	if ((hint & NOTE_SUBMIT) == 0) {
   1421  1.90        ad 		mutex_enter(rpipe->pipe_lock);
   1422  1.83        ad 	}
   1423  1.82        ad 	wpipe = rpipe->pipe_peer;
   1424   1.1  jdolecek 
   1425   1.1  jdolecek 	if ((wpipe == NULL) || (wpipe->pipe_state & PIPE_EOF)) {
   1426   1.1  jdolecek 		kn->kn_data = 0;
   1427  1.63     perry 		kn->kn_flags |= EV_EOF;
   1428  1.83        ad 		if ((hint & NOTE_SUBMIT) == 0) {
   1429  1.90        ad 			mutex_exit(rpipe->pipe_lock);
   1430  1.83        ad 		}
   1431   1.1  jdolecek 		return (1);
   1432   1.1  jdolecek 	}
   1433   1.1  jdolecek 	kn->kn_data = wpipe->pipe_buffer.size - wpipe->pipe_buffer.cnt;
   1434   1.1  jdolecek 	if (wpipe->pipe_state & PIPE_DIRECTW)
   1435   1.1  jdolecek 		kn->kn_data = 0;
   1436   1.1  jdolecek 
   1437  1.83        ad 	if ((hint & NOTE_SUBMIT) == 0) {
   1438  1.90        ad 		mutex_exit(rpipe->pipe_lock);
   1439  1.83        ad 	}
   1440   1.1  jdolecek 	return (kn->kn_data >= PIPE_BUF);
   1441   1.1  jdolecek }
   1442  1.27  jdolecek 
   1443  1.27  jdolecek static const struct filterops pipe_rfiltops =
   1444  1.27  jdolecek 	{ 1, NULL, filt_pipedetach, filt_piperead };
   1445  1.27  jdolecek static const struct filterops pipe_wfiltops =
   1446  1.27  jdolecek 	{ 1, NULL, filt_pipedetach, filt_pipewrite };
   1447  1.27  jdolecek 
   1448  1.27  jdolecek /*ARGSUSED*/
   1449  1.27  jdolecek static int
   1450  1.77      yamt pipe_kqfilter(struct file *fp, struct knote *kn)
   1451  1.27  jdolecek {
   1452  1.35        pk 	struct pipe *pipe;
   1453  1.92        ad 	kmutex_t *lock;
   1454  1.27  jdolecek 
   1455  1.35        pk 	pipe = (struct pipe *)kn->kn_fp->f_data;
   1456  1.92        ad 	lock = pipe->pipe_lock;
   1457  1.92        ad 
   1458  1.92        ad 	mutex_enter(lock);
   1459  1.82        ad 
   1460  1.27  jdolecek 	switch (kn->kn_filter) {
   1461  1.27  jdolecek 	case EVFILT_READ:
   1462  1.27  jdolecek 		kn->kn_fop = &pipe_rfiltops;
   1463  1.27  jdolecek 		break;
   1464  1.27  jdolecek 	case EVFILT_WRITE:
   1465  1.27  jdolecek 		kn->kn_fop = &pipe_wfiltops;
   1466  1.35        pk 		pipe = pipe->pipe_peer;
   1467  1.35        pk 		if (pipe == NULL) {
   1468  1.27  jdolecek 			/* other end of pipe has been closed */
   1469  1.92        ad 			mutex_exit(lock);
   1470  1.27  jdolecek 			return (EBADF);
   1471  1.27  jdolecek 		}
   1472  1.27  jdolecek 		break;
   1473  1.27  jdolecek 	default:
   1474  1.92        ad 		mutex_exit(lock);
   1475  1.88     pooka 		return (EINVAL);
   1476  1.27  jdolecek 	}
   1477  1.82        ad 
   1478  1.35        pk 	kn->kn_hook = pipe;
   1479  1.35        pk 	SLIST_INSERT_HEAD(&pipe->pipe_sel.sel_klist, kn, kn_selnext);
   1480  1.92        ad 	mutex_exit(lock);
   1481  1.82        ad 
   1482  1.27  jdolecek 	return (0);
   1483  1.27  jdolecek }
   1484   1.2  jdolecek 
   1485   1.2  jdolecek /*
   1486   1.2  jdolecek  * Handle pipe sysctls.
   1487   1.2  jdolecek  */
   1488  1.47    atatat SYSCTL_SETUP(sysctl_kern_pipe_setup, "sysctl kern.pipe subtree setup")
   1489  1.47    atatat {
   1490  1.47    atatat 
   1491  1.54    atatat 	sysctl_createv(clog, 0, NULL, NULL,
   1492  1.54    atatat 		       CTLFLAG_PERMANENT,
   1493  1.47    atatat 		       CTLTYPE_NODE, "kern", NULL,
   1494  1.47    atatat 		       NULL, 0, NULL, 0,
   1495  1.47    atatat 		       CTL_KERN, CTL_EOL);
   1496  1.54    atatat 	sysctl_createv(clog, 0, NULL, NULL,
   1497  1.54    atatat 		       CTLFLAG_PERMANENT,
   1498  1.56    atatat 		       CTLTYPE_NODE, "pipe",
   1499  1.56    atatat 		       SYSCTL_DESCR("Pipe settings"),
   1500  1.47    atatat 		       NULL, 0, NULL, 0,
   1501  1.47    atatat 		       CTL_KERN, KERN_PIPE, CTL_EOL);
   1502  1.47    atatat 
   1503  1.54    atatat 	sysctl_createv(clog, 0, NULL, NULL,
   1504  1.54    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1505  1.56    atatat 		       CTLTYPE_INT, "maxkvasz",
   1506  1.56    atatat 		       SYSCTL_DESCR("Maximum amount of kernel memory to be "
   1507  1.56    atatat 				    "used for pipes"),
   1508  1.47    atatat 		       NULL, 0, &maxpipekva, 0,
   1509  1.47    atatat 		       CTL_KERN, KERN_PIPE, KERN_PIPE_MAXKVASZ, CTL_EOL);
   1510  1.54    atatat 	sysctl_createv(clog, 0, NULL, NULL,
   1511  1.54    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1512  1.56    atatat 		       CTLTYPE_INT, "maxloankvasz",
   1513  1.56    atatat 		       SYSCTL_DESCR("Limit for direct transfers via page loan"),
   1514  1.47    atatat 		       NULL, 0, &limitpipekva, 0,
   1515  1.47    atatat 		       CTL_KERN, KERN_PIPE, KERN_PIPE_LIMITKVA, CTL_EOL);
   1516  1.54    atatat 	sysctl_createv(clog, 0, NULL, NULL,
   1517  1.54    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1518  1.56    atatat 		       CTLTYPE_INT, "maxbigpipes",
   1519  1.56    atatat 		       SYSCTL_DESCR("Maximum number of \"big\" pipes"),
   1520  1.47    atatat 		       NULL, 0, &maxbigpipes, 0,
   1521  1.47    atatat 		       CTL_KERN, KERN_PIPE, KERN_PIPE_MAXBIGPIPES, CTL_EOL);
   1522  1.54    atatat 	sysctl_createv(clog, 0, NULL, NULL,
   1523  1.54    atatat 		       CTLFLAG_PERMANENT,
   1524  1.56    atatat 		       CTLTYPE_INT, "nbigpipes",
   1525  1.56    atatat 		       SYSCTL_DESCR("Number of \"big\" pipes"),
   1526  1.47    atatat 		       NULL, 0, &nbigpipe, 0,
   1527  1.47    atatat 		       CTL_KERN, KERN_PIPE, KERN_PIPE_NBIGPIPES, CTL_EOL);
   1528  1.54    atatat 	sysctl_createv(clog, 0, NULL, NULL,
   1529  1.54    atatat 		       CTLFLAG_PERMANENT,
   1530  1.56    atatat 		       CTLTYPE_INT, "kvasize",
   1531  1.56    atatat 		       SYSCTL_DESCR("Amount of kernel memory consumed by pipe "
   1532  1.56    atatat 				    "buffers"),
   1533  1.47    atatat 		       NULL, 0, &amountpipekva, 0,
   1534  1.47    atatat 		       CTL_KERN, KERN_PIPE, KERN_PIPE_KVASIZE, CTL_EOL);
   1535   1.2  jdolecek }
   1536