sys_pipe.c revision 43311
113675Sdyson/* 213675Sdyson * Copyright (c) 1996 John S. Dyson 313675Sdyson * All rights reserved. 413675Sdyson * 513675Sdyson * Redistribution and use in source and binary forms, with or without 613675Sdyson * modification, are permitted provided that the following conditions 713675Sdyson * are met: 813675Sdyson * 1. Redistributions of source code must retain the above copyright 913675Sdyson * notice immediately at the beginning of the file, without modification, 1013675Sdyson * this list of conditions, and the following disclaimer. 1113675Sdyson * 2. Redistributions in binary form must reproduce the above copyright 1213675Sdyson * notice, this list of conditions and the following disclaimer in the 1313675Sdyson * documentation and/or other materials provided with the distribution. 1413675Sdyson * 3. Absolutely no warranty of function or purpose is made by the author 1513675Sdyson * John S. Dyson. 1614037Sdyson * 4. Modifications may be freely made to this file if the above conditions 1713675Sdyson * are met. 1813675Sdyson * 1943311Sdillon * $Id: sys_pipe.c,v 1.48 1999/01/27 21:49:57 dillon Exp $ 2013675Sdyson */ 2113675Sdyson 2213675Sdyson/* 2313675Sdyson * This file contains a high-performance replacement for the socket-based 2413675Sdyson * pipes scheme originally used in FreeBSD/4.4Lite. It does not support 2513675Sdyson * all features of sockets, but does do everything that pipes normally 2613675Sdyson * do. 2713675Sdyson */ 2813675Sdyson 2913907Sdyson/* 3013907Sdyson * This code has two modes of operation, a small write mode and a large 3113907Sdyson * write mode. The small write mode acts like conventional pipes with 3213907Sdyson * a kernel buffer. If the buffer is less than PIPE_MINDIRECT, then the 3313907Sdyson * "normal" pipe buffering is done. If the buffer is between PIPE_MINDIRECT 3413907Sdyson * and PIPE_SIZE in size, it is fully mapped and wired into the kernel, and 3513907Sdyson * the receiving process can copy it directly from the pages in the sending 3613907Sdyson * process. 3713907Sdyson * 3813907Sdyson * If the sending process receives a signal, it is possible that it will 3913913Sdyson * go away, and certainly its address space can change, because control 4013907Sdyson * is returned back to the user-mode side. In that case, the pipe code 4113907Sdyson * arranges to copy the buffer supplied by the user process, to a pageable 4213907Sdyson * kernel buffer, and the receiving process will grab the data from the 4313907Sdyson * pageable kernel buffer. Since signals don't happen all that often, 4413907Sdyson * the copy operation is normally eliminated. 4513907Sdyson * 4613907Sdyson * The constant PIPE_MINDIRECT is chosen to make sure that buffering will 4713907Sdyson * happen for small transfers so that the system will not spend all of 4813913Sdyson * its time context switching. PIPE_SIZE is constrained by the 4913907Sdyson * amount of kernel virtual memory. 5013907Sdyson */ 5113907Sdyson 5213675Sdyson#include <sys/param.h> 5313675Sdyson#include <sys/systm.h> 5413675Sdyson#include <sys/proc.h> 5524131Sbde#include <sys/fcntl.h> 5613675Sdyson#include <sys/file.h> 5713675Sdyson#include <sys/filedesc.h> 5824206Sbde#include <sys/filio.h> 5924206Sbde#include <sys/ttycom.h> 6013675Sdyson#include <sys/stat.h> 6129356Speter#include <sys/poll.h> 6243278Sbde#include <sys/select.h> 6313675Sdyson#include <sys/signalvar.h> 6413675Sdyson#include <sys/sysproto.h> 6513675Sdyson#include <sys/pipe.h> 6634924Sbde#include <sys/uio.h> 6713675Sdyson 6813675Sdyson#include <vm/vm.h> 6913675Sdyson#include <vm/vm_prot.h> 7013675Sdyson#include <vm/vm_param.h> 7122521Sdyson#include <sys/lock.h> 7213675Sdyson#include <vm/vm_object.h> 7313675Sdyson#include <vm/vm_kern.h> 7413675Sdyson#include <vm/vm_extern.h> 7513675Sdyson#include <vm/pmap.h> 7613675Sdyson#include <vm/vm_map.h> 7713907Sdyson#include <vm/vm_page.h> 7827899Sdyson#include <vm/vm_zone.h> 7913675Sdyson 8014037Sdyson/* 8114037Sdyson * Use this define if you want to disable *fancy* VM things. Expect an 8214037Sdyson * approx 30% decrease in transfer rate. This could be useful for 8314037Sdyson * NetBSD or OpenBSD. 8414037Sdyson */ 8514037Sdyson/* #define PIPE_NODIRECT */ 8614037Sdyson 8714037Sdyson/* 8814037Sdyson * interfaces to the outside world 8914037Sdyson */ 9013675Sdysonstatic int pipe_read __P((struct file *fp, struct uio *uio, 9113675Sdyson struct ucred *cred)); 9213675Sdysonstatic int pipe_write __P((struct file *fp, struct uio *uio, 9313675Sdyson struct ucred *cred)); 9413675Sdysonstatic int pipe_close __P((struct file *fp, struct proc *p)); 9529356Speterstatic int pipe_poll __P((struct file *fp, int events, struct ucred *cred, 9629356Speter struct proc *p)); 9736735Sdfrstatic int pipe_ioctl __P((struct file *fp, u_long cmd, caddr_t data, struct proc *p)); 9813675Sdyson 9913675Sdysonstatic struct fileops pipeops = 10029356Speter { pipe_read, pipe_write, pipe_ioctl, pipe_poll, pipe_close }; 10113675Sdyson 10213675Sdyson/* 10313675Sdyson * Default pipe buffer size(s), this can be kind-of large now because pipe 10413675Sdyson * space is pageable. The pipe code will try to maintain locality of 10513675Sdyson * reference for performance reasons, so small amounts of outstanding I/O 10613675Sdyson * will not wipe the cache. 10713675Sdyson */ 10813907Sdyson#define MINPIPESIZE (PIPE_SIZE/3) 10913907Sdyson#define MAXPIPESIZE (2*PIPE_SIZE/3) 11013675Sdyson 11113907Sdyson/* 11213907Sdyson * Maximum amount of kva for pipes -- this is kind-of a soft limit, but 11313907Sdyson * is there so that on large systems, we don't exhaust it. 11413907Sdyson */ 11513907Sdyson#define MAXPIPEKVA (8*1024*1024) 11613907Sdyson 11713907Sdyson/* 11813907Sdyson * Limit for direct transfers, we cannot, of course limit 11913907Sdyson * the amount of kva for pipes in general though. 12013907Sdyson */ 12113907Sdyson#define LIMITPIPEKVA (16*1024*1024) 12217163Sdyson 12317163Sdyson/* 12417163Sdyson * Limit the number of "big" pipes 12517163Sdyson */ 12617163Sdyson#define LIMITBIGPIPES 32 12733181Seivindstatic int nbigpipe; 12817163Sdyson 12917124Sbdestatic int amountpipekva; 13013907Sdyson 13113675Sdysonstatic void pipeclose __P((struct pipe *cpipe)); 13213675Sdysonstatic void pipeinit __P((struct pipe *cpipe)); 13313907Sdysonstatic __inline int pipelock __P((struct pipe *cpipe, int catch)); 13413675Sdysonstatic __inline void pipeunlock __P((struct pipe *cpipe)); 13514122Speterstatic __inline void pipeselwakeup __P((struct pipe *cpipe)); 13614037Sdyson#ifndef PIPE_NODIRECT 13713907Sdysonstatic int pipe_build_write_buffer __P((struct pipe *wpipe, struct uio *uio)); 13813907Sdysonstatic void pipe_destroy_write_buffer __P((struct pipe *wpipe)); 13913907Sdysonstatic int pipe_direct_write __P((struct pipe *wpipe, struct uio *uio)); 14013907Sdysonstatic void pipe_clone_write_buffer __P((struct pipe *wpipe)); 14114037Sdyson#endif 14213907Sdysonstatic void pipespace __P((struct pipe *cpipe)); 14313675Sdyson 14433181Seivindstatic vm_zone_t pipe_zone; 14527899Sdyson 14613675Sdyson/* 14713675Sdyson * The pipe system call for the DTYPE_PIPE type of pipes 14813675Sdyson */ 14913675Sdyson 15013675Sdyson/* ARGSUSED */ 15113675Sdysonint 15230994Sphkpipe(p, uap) 15313675Sdyson struct proc *p; 15413675Sdyson struct pipe_args /* { 15513675Sdyson int dummy; 15613675Sdyson } */ *uap; 15713675Sdyson{ 15813675Sdyson register struct filedesc *fdp = p->p_fd; 15913675Sdyson struct file *rf, *wf; 16013675Sdyson struct pipe *rpipe, *wpipe; 16113675Sdyson int fd, error; 16213675Sdyson 16327899Sdyson if (pipe_zone == NULL) 16427923Sdyson pipe_zone = zinit("PIPE", sizeof (struct pipe), 0, 0, 4); 16527899Sdyson 16627899Sdyson rpipe = zalloc( pipe_zone); 16713675Sdyson pipeinit(rpipe); 16813907Sdyson rpipe->pipe_state |= PIPE_DIRECTOK; 16927899Sdyson wpipe = zalloc( pipe_zone); 17013675Sdyson pipeinit(wpipe); 17113907Sdyson wpipe->pipe_state |= PIPE_DIRECTOK; 17213675Sdyson 17313675Sdyson error = falloc(p, &rf, &fd); 17413675Sdyson if (error) 17513675Sdyson goto free2; 17630994Sphk p->p_retval[0] = fd; 17713675Sdyson rf->f_flag = FREAD | FWRITE; 17813675Sdyson rf->f_type = DTYPE_PIPE; 17913675Sdyson rf->f_ops = &pipeops; 18013675Sdyson rf->f_data = (caddr_t)rpipe; 18113675Sdyson error = falloc(p, &wf, &fd); 18213675Sdyson if (error) 18313675Sdyson goto free3; 18413675Sdyson wf->f_flag = FREAD | FWRITE; 18513675Sdyson wf->f_type = DTYPE_PIPE; 18613675Sdyson wf->f_ops = &pipeops; 18713675Sdyson wf->f_data = (caddr_t)wpipe; 18830994Sphk p->p_retval[1] = fd; 18913675Sdyson 19013675Sdyson rpipe->pipe_peer = wpipe; 19113675Sdyson wpipe->pipe_peer = rpipe; 19213675Sdyson 19313675Sdyson return (0); 19413675Sdysonfree3: 19513675Sdyson ffree(rf); 19630994Sphk fdp->fd_ofiles[p->p_retval[0]] = 0; 19713675Sdysonfree2: 19813675Sdyson (void)pipeclose(wpipe); 19913675Sdyson (void)pipeclose(rpipe); 20013675Sdyson return (error); 20113675Sdyson} 20213675Sdyson 20313909Sdyson/* 20413909Sdyson * Allocate kva for pipe circular buffer, the space is pageable 20513909Sdyson */ 20613675Sdysonstatic void 20713907Sdysonpipespace(cpipe) 20813675Sdyson struct pipe *cpipe; 20913675Sdyson{ 21013688Sdyson int npages, error; 21113675Sdyson 21213907Sdyson npages = round_page(cpipe->pipe_buffer.size)/PAGE_SIZE; 21313675Sdyson /* 21413675Sdyson * Create an object, I don't like the idea of paging to/from 21513675Sdyson * kernel_object. 21614037Sdyson * XXX -- minor change needed here for NetBSD/OpenBSD VM systems. 21713675Sdyson */ 21813675Sdyson cpipe->pipe_buffer.object = vm_object_allocate(OBJT_DEFAULT, npages); 21913688Sdyson cpipe->pipe_buffer.buffer = (caddr_t) vm_map_min(kernel_map); 22013675Sdyson 22113675Sdyson /* 22213675Sdyson * Insert the object into the kernel map, and allocate kva for it. 22313675Sdyson * The map entry is, by default, pageable. 22414037Sdyson * XXX -- minor change needed here for NetBSD/OpenBSD VM systems. 22513675Sdyson */ 22613688Sdyson error = vm_map_find(kernel_map, cpipe->pipe_buffer.object, 0, 22713907Sdyson (vm_offset_t *) &cpipe->pipe_buffer.buffer, 22813907Sdyson cpipe->pipe_buffer.size, 1, 22913688Sdyson VM_PROT_ALL, VM_PROT_ALL, 0); 23013675Sdyson 23113688Sdyson if (error != KERN_SUCCESS) 23213688Sdyson panic("pipeinit: cannot allocate pipe -- out of kvm -- code = %d", error); 23313907Sdyson amountpipekva += cpipe->pipe_buffer.size; 23413907Sdyson} 23513688Sdyson 23613907Sdyson/* 23713907Sdyson * initialize and allocate VM and memory for pipe 23813907Sdyson */ 23913907Sdysonstatic void 24013907Sdysonpipeinit(cpipe) 24113907Sdyson struct pipe *cpipe; 24213907Sdyson{ 24313907Sdyson 24413675Sdyson cpipe->pipe_buffer.in = 0; 24513675Sdyson cpipe->pipe_buffer.out = 0; 24613675Sdyson cpipe->pipe_buffer.cnt = 0; 24713907Sdyson cpipe->pipe_buffer.size = PIPE_SIZE; 24817163Sdyson 24913907Sdyson /* Buffer kva gets dynamically allocated */ 25013907Sdyson cpipe->pipe_buffer.buffer = NULL; 25117124Sbde /* cpipe->pipe_buffer.object = invalid */ 25213675Sdyson 25313675Sdyson cpipe->pipe_state = 0; 25413675Sdyson cpipe->pipe_peer = NULL; 25513675Sdyson cpipe->pipe_busy = 0; 25634901Sphk getnanotime(&cpipe->pipe_ctime); 25724101Sbde cpipe->pipe_atime = cpipe->pipe_ctime; 25824101Sbde cpipe->pipe_mtime = cpipe->pipe_ctime; 25913675Sdyson bzero(&cpipe->pipe_sel, sizeof cpipe->pipe_sel); 26013907Sdyson 26114037Sdyson#ifndef PIPE_NODIRECT 26213907Sdyson /* 26313907Sdyson * pipe data structure initializations to support direct pipe I/O 26413907Sdyson */ 26513907Sdyson cpipe->pipe_map.cnt = 0; 26613907Sdyson cpipe->pipe_map.kva = 0; 26713907Sdyson cpipe->pipe_map.pos = 0; 26813907Sdyson cpipe->pipe_map.npages = 0; 26917124Sbde /* cpipe->pipe_map.ms[] = invalid */ 27014037Sdyson#endif 27113675Sdyson} 27213675Sdyson 27313675Sdyson 27413675Sdyson/* 27513675Sdyson * lock a pipe for I/O, blocking other access 27613675Sdyson */ 27713675Sdysonstatic __inline int 27813907Sdysonpipelock(cpipe, catch) 27913675Sdyson struct pipe *cpipe; 28013907Sdyson int catch; 28113675Sdyson{ 28213776Sdyson int error; 28313675Sdyson while (cpipe->pipe_state & PIPE_LOCK) { 28413675Sdyson cpipe->pipe_state |= PIPE_LWANT; 28543301Sdillon if ((error = tsleep( cpipe, 28643301Sdillon catch?(PRIBIO|PCATCH):PRIBIO, "pipelk", 0)) != 0) { 28713776Sdyson return error; 28813675Sdyson } 28913675Sdyson } 29013675Sdyson cpipe->pipe_state |= PIPE_LOCK; 29113675Sdyson return 0; 29213675Sdyson} 29313675Sdyson 29413675Sdyson/* 29513675Sdyson * unlock a pipe I/O lock 29613675Sdyson */ 29713675Sdysonstatic __inline void 29813675Sdysonpipeunlock(cpipe) 29913675Sdyson struct pipe *cpipe; 30013675Sdyson{ 30113675Sdyson cpipe->pipe_state &= ~PIPE_LOCK; 30213675Sdyson if (cpipe->pipe_state & PIPE_LWANT) { 30313675Sdyson cpipe->pipe_state &= ~PIPE_LWANT; 30414177Sdyson wakeup(cpipe); 30513675Sdyson } 30613675Sdyson} 30713675Sdyson 30814037Sdysonstatic __inline void 30914037Sdysonpipeselwakeup(cpipe) 31014037Sdyson struct pipe *cpipe; 31114037Sdyson{ 31214037Sdyson if (cpipe->pipe_state & PIPE_SEL) { 31314037Sdyson cpipe->pipe_state &= ~PIPE_SEL; 31414037Sdyson selwakeup(&cpipe->pipe_sel); 31514037Sdyson } 31641086Struckman if ((cpipe->pipe_state & PIPE_ASYNC) && cpipe->pipe_sigio) 31741086Struckman pgsigio(cpipe->pipe_sigio, SIGIO, 0); 31814037Sdyson} 31914037Sdyson 32013675Sdyson/* ARGSUSED */ 32113675Sdysonstatic int 32213675Sdysonpipe_read(fp, uio, cred) 32313675Sdyson struct file *fp; 32413675Sdyson struct uio *uio; 32513675Sdyson struct ucred *cred; 32613675Sdyson{ 32713675Sdyson 32813675Sdyson struct pipe *rpipe = (struct pipe *) fp->f_data; 32913675Sdyson int error = 0; 33013675Sdyson int nread = 0; 33118863Sdyson u_int size; 33213675Sdyson 33313675Sdyson ++rpipe->pipe_busy; 33413675Sdyson while (uio->uio_resid) { 33513907Sdyson /* 33613907Sdyson * normal pipe buffer receive 33713907Sdyson */ 33813675Sdyson if (rpipe->pipe_buffer.cnt > 0) { 33918863Sdyson size = rpipe->pipe_buffer.size - rpipe->pipe_buffer.out; 34013675Sdyson if (size > rpipe->pipe_buffer.cnt) 34113675Sdyson size = rpipe->pipe_buffer.cnt; 34218863Sdyson if (size > (u_int) uio->uio_resid) 34318863Sdyson size = (u_int) uio->uio_resid; 34413907Sdyson if ((error = pipelock(rpipe,1)) == 0) { 34513675Sdyson error = uiomove( &rpipe->pipe_buffer.buffer[rpipe->pipe_buffer.out], 34613675Sdyson size, uio); 34713675Sdyson pipeunlock(rpipe); 34813675Sdyson } 34913675Sdyson if (error) { 35013675Sdyson break; 35113675Sdyson } 35213675Sdyson rpipe->pipe_buffer.out += size; 35313675Sdyson if (rpipe->pipe_buffer.out >= rpipe->pipe_buffer.size) 35413675Sdyson rpipe->pipe_buffer.out = 0; 35513675Sdyson 35613675Sdyson rpipe->pipe_buffer.cnt -= size; 35713675Sdyson nread += size; 35814037Sdyson#ifndef PIPE_NODIRECT 35913907Sdyson /* 36013907Sdyson * Direct copy, bypassing a kernel buffer. 36113907Sdyson */ 36213907Sdyson } else if ((size = rpipe->pipe_map.cnt) && 36313907Sdyson (rpipe->pipe_state & PIPE_DIRECTW)) { 36413907Sdyson caddr_t va; 36518863Sdyson if (size > (u_int) uio->uio_resid) 36618863Sdyson size = (u_int) uio->uio_resid; 36713907Sdyson if ((error = pipelock(rpipe,1)) == 0) { 36813907Sdyson va = (caddr_t) rpipe->pipe_map.kva + rpipe->pipe_map.pos; 36913907Sdyson error = uiomove(va, size, uio); 37013907Sdyson pipeunlock(rpipe); 37113907Sdyson } 37213907Sdyson if (error) 37313907Sdyson break; 37413907Sdyson nread += size; 37513907Sdyson rpipe->pipe_map.pos += size; 37613907Sdyson rpipe->pipe_map.cnt -= size; 37713907Sdyson if (rpipe->pipe_map.cnt == 0) { 37813907Sdyson rpipe->pipe_state &= ~PIPE_DIRECTW; 37913907Sdyson wakeup(rpipe); 38013907Sdyson } 38114037Sdyson#endif 38213675Sdyson } else { 38313675Sdyson /* 38413675Sdyson * detect EOF condition 38513675Sdyson */ 38613675Sdyson if (rpipe->pipe_state & PIPE_EOF) { 38714802Sdyson /* XXX error = ? */ 38813675Sdyson break; 38913675Sdyson } 39013675Sdyson /* 39113675Sdyson * If the "write-side" has been blocked, wake it up now. 39213675Sdyson */ 39313675Sdyson if (rpipe->pipe_state & PIPE_WANTW) { 39413675Sdyson rpipe->pipe_state &= ~PIPE_WANTW; 39513675Sdyson wakeup(rpipe); 39613675Sdyson } 39713774Sdyson if (nread > 0) 39813675Sdyson break; 39916960Sdyson 40016960Sdyson if (fp->f_flag & FNONBLOCK) { 40113774Sdyson error = EAGAIN; 40213774Sdyson break; 40313774Sdyson } 40413675Sdyson 40513675Sdyson /* 40613675Sdyson * If there is no more to read in the pipe, reset 40713913Sdyson * its pointers to the beginning. This improves 40813675Sdyson * cache hit stats. 40913675Sdyson */ 41013675Sdyson 41113907Sdyson if ((error = pipelock(rpipe,1)) == 0) { 41213675Sdyson if (rpipe->pipe_buffer.cnt == 0) { 41313675Sdyson rpipe->pipe_buffer.in = 0; 41413675Sdyson rpipe->pipe_buffer.out = 0; 41513675Sdyson } 41613675Sdyson pipeunlock(rpipe); 41713675Sdyson } else { 41813675Sdyson break; 41913675Sdyson } 42014177Sdyson 42114177Sdyson if (rpipe->pipe_state & PIPE_WANTW) { 42214177Sdyson rpipe->pipe_state &= ~PIPE_WANTW; 42314177Sdyson wakeup(rpipe); 42414177Sdyson } 42514177Sdyson 42613675Sdyson rpipe->pipe_state |= PIPE_WANTR; 42743301Sdillon if ((error = tsleep(rpipe, PRIBIO|PCATCH, "piperd", 0)) != 0) { 42813675Sdyson break; 42913675Sdyson } 43013675Sdyson } 43113675Sdyson } 43213675Sdyson 43324101Sbde if (error == 0) 43434901Sphk getnanotime(&rpipe->pipe_atime); 43513913Sdyson 43613675Sdyson --rpipe->pipe_busy; 43713675Sdyson if ((rpipe->pipe_busy == 0) && (rpipe->pipe_state & PIPE_WANT)) { 43813675Sdyson rpipe->pipe_state &= ~(PIPE_WANT|PIPE_WANTW); 43913675Sdyson wakeup(rpipe); 44013675Sdyson } else if (rpipe->pipe_buffer.cnt < MINPIPESIZE) { 44113675Sdyson /* 44213675Sdyson * If there is no more to read in the pipe, reset 44313913Sdyson * its pointers to the beginning. This improves 44413675Sdyson * cache hit stats. 44513675Sdyson */ 44617163Sdyson if (rpipe->pipe_buffer.cnt == 0) { 44717163Sdyson if ((error == 0) && (error = pipelock(rpipe,1)) == 0) { 44813675Sdyson rpipe->pipe_buffer.in = 0; 44913675Sdyson rpipe->pipe_buffer.out = 0; 45017163Sdyson pipeunlock(rpipe); 45113675Sdyson } 45213675Sdyson } 45313675Sdyson 45413675Sdyson /* 45513675Sdyson * If the "write-side" has been blocked, wake it up now. 45613675Sdyson */ 45713675Sdyson if (rpipe->pipe_state & PIPE_WANTW) { 45813675Sdyson rpipe->pipe_state &= ~PIPE_WANTW; 45913675Sdyson wakeup(rpipe); 46013675Sdyson } 46113675Sdyson } 46214037Sdyson 46314802Sdyson if ((rpipe->pipe_buffer.size - rpipe->pipe_buffer.cnt) >= PIPE_BUF) 46414037Sdyson pipeselwakeup(rpipe); 46514037Sdyson 46613675Sdyson return error; 46713675Sdyson} 46813675Sdyson 46914037Sdyson#ifndef PIPE_NODIRECT 47013907Sdyson/* 47113907Sdyson * Map the sending processes' buffer into kernel space and wire it. 47213907Sdyson * This is similar to a physical write operation. 47313907Sdyson */ 47413675Sdysonstatic int 47513907Sdysonpipe_build_write_buffer(wpipe, uio) 47613907Sdyson struct pipe *wpipe; 47713675Sdyson struct uio *uio; 47813675Sdyson{ 47918863Sdyson u_int size; 48013907Sdyson int i; 48113907Sdyson vm_offset_t addr, endaddr, paddr; 48213907Sdyson 48318863Sdyson size = (u_int) uio->uio_iov->iov_len; 48413907Sdyson if (size > wpipe->pipe_buffer.size) 48513907Sdyson size = wpipe->pipe_buffer.size; 48613907Sdyson 48740286Sdg endaddr = round_page((vm_offset_t)uio->uio_iov->iov_base + size); 48840286Sdg for(i = 0, addr = trunc_page((vm_offset_t)uio->uio_iov->iov_base); 48913907Sdyson addr < endaddr; 49013907Sdyson addr += PAGE_SIZE, i+=1) { 49113907Sdyson 49213907Sdyson vm_page_t m; 49313907Sdyson 49413909Sdyson vm_fault_quick( (caddr_t) addr, VM_PROT_READ); 49513907Sdyson paddr = pmap_kextract(addr); 49613907Sdyson if (!paddr) { 49713907Sdyson int j; 49813907Sdyson for(j=0;j<i;j++) 49940700Sdg vm_page_unwire(wpipe->pipe_map.ms[j], 1); 50013907Sdyson return EFAULT; 50113907Sdyson } 50213907Sdyson 50313907Sdyson m = PHYS_TO_VM_PAGE(paddr); 50413907Sdyson vm_page_wire(m); 50513907Sdyson wpipe->pipe_map.ms[i] = m; 50613907Sdyson } 50713907Sdyson 50813907Sdyson/* 50913907Sdyson * set up the control block 51013907Sdyson */ 51113907Sdyson wpipe->pipe_map.npages = i; 51213907Sdyson wpipe->pipe_map.pos = ((vm_offset_t) uio->uio_iov->iov_base) & PAGE_MASK; 51313907Sdyson wpipe->pipe_map.cnt = size; 51413907Sdyson 51513907Sdyson/* 51613907Sdyson * and map the buffer 51713907Sdyson */ 51813907Sdyson if (wpipe->pipe_map.kva == 0) { 51913912Sdyson /* 52013912Sdyson * We need to allocate space for an extra page because the 52113912Sdyson * address range might (will) span pages at times. 52213912Sdyson */ 52313907Sdyson wpipe->pipe_map.kva = kmem_alloc_pageable(kernel_map, 52413912Sdyson wpipe->pipe_buffer.size + PAGE_SIZE); 52513912Sdyson amountpipekva += wpipe->pipe_buffer.size + PAGE_SIZE; 52613907Sdyson } 52713907Sdyson pmap_qenter(wpipe->pipe_map.kva, wpipe->pipe_map.ms, 52813907Sdyson wpipe->pipe_map.npages); 52913907Sdyson 53013907Sdyson/* 53113907Sdyson * and update the uio data 53213907Sdyson */ 53313907Sdyson 53413907Sdyson uio->uio_iov->iov_len -= size; 53513907Sdyson uio->uio_iov->iov_base += size; 53613907Sdyson if (uio->uio_iov->iov_len == 0) 53713907Sdyson uio->uio_iov++; 53813907Sdyson uio->uio_resid -= size; 53913907Sdyson uio->uio_offset += size; 54013907Sdyson return 0; 54113907Sdyson} 54213907Sdyson 54313907Sdyson/* 54413907Sdyson * unmap and unwire the process buffer 54513907Sdyson */ 54613907Sdysonstatic void 54713907Sdysonpipe_destroy_write_buffer(wpipe) 54813907Sdysonstruct pipe *wpipe; 54913907Sdyson{ 55013907Sdyson int i; 55117163Sdyson if (wpipe->pipe_map.kva) { 55217163Sdyson pmap_qremove(wpipe->pipe_map.kva, wpipe->pipe_map.npages); 55313907Sdyson 55413907Sdyson if (amountpipekva > MAXPIPEKVA) { 55513907Sdyson vm_offset_t kva = wpipe->pipe_map.kva; 55613907Sdyson wpipe->pipe_map.kva = 0; 55713907Sdyson kmem_free(kernel_map, kva, 55813912Sdyson wpipe->pipe_buffer.size + PAGE_SIZE); 55913912Sdyson amountpipekva -= wpipe->pipe_buffer.size + PAGE_SIZE; 56013907Sdyson } 56113907Sdyson } 56213907Sdyson for (i=0;i<wpipe->pipe_map.npages;i++) 56340700Sdg vm_page_unwire(wpipe->pipe_map.ms[i], 1); 56413907Sdyson} 56513907Sdyson 56613907Sdyson/* 56713907Sdyson * In the case of a signal, the writing process might go away. This 56813907Sdyson * code copies the data into the circular buffer so that the source 56913907Sdyson * pages can be freed without loss of data. 57013907Sdyson */ 57113907Sdysonstatic void 57213907Sdysonpipe_clone_write_buffer(wpipe) 57313907Sdysonstruct pipe *wpipe; 57413907Sdyson{ 57513907Sdyson int size; 57613907Sdyson int pos; 57713907Sdyson 57813907Sdyson size = wpipe->pipe_map.cnt; 57913907Sdyson pos = wpipe->pipe_map.pos; 58013907Sdyson bcopy((caddr_t) wpipe->pipe_map.kva+pos, 58113907Sdyson (caddr_t) wpipe->pipe_buffer.buffer, 58213907Sdyson size); 58313907Sdyson 58413907Sdyson wpipe->pipe_buffer.in = size; 58513907Sdyson wpipe->pipe_buffer.out = 0; 58613907Sdyson wpipe->pipe_buffer.cnt = size; 58713907Sdyson wpipe->pipe_state &= ~PIPE_DIRECTW; 58813907Sdyson 58913907Sdyson pipe_destroy_write_buffer(wpipe); 59013907Sdyson} 59113907Sdyson 59213907Sdyson/* 59313907Sdyson * This implements the pipe buffer write mechanism. Note that only 59413907Sdyson * a direct write OR a normal pipe write can be pending at any given time. 59513907Sdyson * If there are any characters in the pipe buffer, the direct write will 59613907Sdyson * be deferred until the receiving process grabs all of the bytes from 59713907Sdyson * the pipe buffer. Then the direct mapping write is set-up. 59813907Sdyson */ 59913907Sdysonstatic int 60013907Sdysonpipe_direct_write(wpipe, uio) 60113907Sdyson struct pipe *wpipe; 60213907Sdyson struct uio *uio; 60313907Sdyson{ 60413907Sdyson int error; 60513951Sdysonretry: 60613907Sdyson while (wpipe->pipe_state & PIPE_DIRECTW) { 60713951Sdyson if ( wpipe->pipe_state & PIPE_WANTR) { 60813951Sdyson wpipe->pipe_state &= ~PIPE_WANTR; 60913951Sdyson wakeup(wpipe); 61013951Sdyson } 61113992Sdyson wpipe->pipe_state |= PIPE_WANTW; 61213907Sdyson error = tsleep(wpipe, 61313907Sdyson PRIBIO|PCATCH, "pipdww", 0); 61414802Sdyson if (error) 61513907Sdyson goto error1; 61614802Sdyson if (wpipe->pipe_state & PIPE_EOF) { 61714802Sdyson error = EPIPE; 61814802Sdyson goto error1; 61914802Sdyson } 62013907Sdyson } 62113907Sdyson wpipe->pipe_map.cnt = 0; /* transfer not ready yet */ 62213951Sdyson if (wpipe->pipe_buffer.cnt > 0) { 62313951Sdyson if ( wpipe->pipe_state & PIPE_WANTR) { 62413951Sdyson wpipe->pipe_state &= ~PIPE_WANTR; 62513951Sdyson wakeup(wpipe); 62613951Sdyson } 62713951Sdyson 62813992Sdyson wpipe->pipe_state |= PIPE_WANTW; 62913907Sdyson error = tsleep(wpipe, 63013907Sdyson PRIBIO|PCATCH, "pipdwc", 0); 63114802Sdyson if (error) 63213907Sdyson goto error1; 63314802Sdyson if (wpipe->pipe_state & PIPE_EOF) { 63414802Sdyson error = EPIPE; 63514802Sdyson goto error1; 63613907Sdyson } 63713951Sdyson goto retry; 63813907Sdyson } 63913907Sdyson 64013951Sdyson wpipe->pipe_state |= PIPE_DIRECTW; 64113951Sdyson 64213907Sdyson error = pipe_build_write_buffer(wpipe, uio); 64313907Sdyson if (error) { 64413907Sdyson wpipe->pipe_state &= ~PIPE_DIRECTW; 64513907Sdyson goto error1; 64613907Sdyson } 64713907Sdyson 64813907Sdyson error = 0; 64913907Sdyson while (!error && (wpipe->pipe_state & PIPE_DIRECTW)) { 65013907Sdyson if (wpipe->pipe_state & PIPE_EOF) { 65113907Sdyson pipelock(wpipe, 0); 65213907Sdyson pipe_destroy_write_buffer(wpipe); 65313907Sdyson pipeunlock(wpipe); 65414037Sdyson pipeselwakeup(wpipe); 65514802Sdyson error = EPIPE; 65614802Sdyson goto error1; 65713907Sdyson } 65813992Sdyson if (wpipe->pipe_state & PIPE_WANTR) { 65913992Sdyson wpipe->pipe_state &= ~PIPE_WANTR; 66013992Sdyson wakeup(wpipe); 66113992Sdyson } 66214037Sdyson pipeselwakeup(wpipe); 66313907Sdyson error = tsleep(wpipe, PRIBIO|PCATCH, "pipdwt", 0); 66413907Sdyson } 66513907Sdyson 66613907Sdyson pipelock(wpipe,0); 66713907Sdyson if (wpipe->pipe_state & PIPE_DIRECTW) { 66813907Sdyson /* 66913907Sdyson * this bit of trickery substitutes a kernel buffer for 67013907Sdyson * the process that might be going away. 67113907Sdyson */ 67213907Sdyson pipe_clone_write_buffer(wpipe); 67313907Sdyson } else { 67413907Sdyson pipe_destroy_write_buffer(wpipe); 67513907Sdyson } 67613907Sdyson pipeunlock(wpipe); 67713907Sdyson return error; 67813907Sdyson 67913907Sdysonerror1: 68013907Sdyson wakeup(wpipe); 68113907Sdyson return error; 68213907Sdyson} 68314037Sdyson#endif 68413907Sdyson 68516960Sdysonstatic int 68616960Sdysonpipe_write(fp, uio, cred) 68716960Sdyson struct file *fp; 68813907Sdyson struct uio *uio; 68916960Sdyson struct ucred *cred; 69013907Sdyson{ 69113675Sdyson int error = 0; 69213913Sdyson int orig_resid; 69313675Sdyson 69416960Sdyson struct pipe *wpipe, *rpipe; 69516960Sdyson 69616960Sdyson rpipe = (struct pipe *) fp->f_data; 69716960Sdyson wpipe = rpipe->pipe_peer; 69816960Sdyson 69913675Sdyson /* 70013675Sdyson * detect loss of pipe read side, issue SIGPIPE if lost. 70113675Sdyson */ 70216960Sdyson if ((wpipe == NULL) || (wpipe->pipe_state & PIPE_EOF)) { 70313774Sdyson return EPIPE; 70413675Sdyson } 70513675Sdyson 70617163Sdyson /* 70717163Sdyson * If it is advantageous to resize the pipe buffer, do 70817163Sdyson * so. 70917163Sdyson */ 71017163Sdyson if ((uio->uio_resid > PIPE_SIZE) && 71117163Sdyson (nbigpipe < LIMITBIGPIPES) && 71217163Sdyson (wpipe->pipe_state & PIPE_DIRECTW) == 0 && 71317163Sdyson (wpipe->pipe_buffer.size <= PIPE_SIZE) && 71417163Sdyson (wpipe->pipe_buffer.cnt == 0)) { 71517163Sdyson 71617163Sdyson if (wpipe->pipe_buffer.buffer) { 71717163Sdyson amountpipekva -= wpipe->pipe_buffer.size; 71817163Sdyson kmem_free(kernel_map, 71917163Sdyson (vm_offset_t)wpipe->pipe_buffer.buffer, 72017163Sdyson wpipe->pipe_buffer.size); 72117163Sdyson } 72217163Sdyson 72317163Sdyson#ifndef PIPE_NODIRECT 72417163Sdyson if (wpipe->pipe_map.kva) { 72517163Sdyson amountpipekva -= wpipe->pipe_buffer.size + PAGE_SIZE; 72617163Sdyson kmem_free(kernel_map, 72717163Sdyson wpipe->pipe_map.kva, 72817163Sdyson wpipe->pipe_buffer.size + PAGE_SIZE); 72917163Sdyson } 73017163Sdyson#endif 73117163Sdyson 73217163Sdyson wpipe->pipe_buffer.in = 0; 73317163Sdyson wpipe->pipe_buffer.out = 0; 73417163Sdyson wpipe->pipe_buffer.cnt = 0; 73517163Sdyson wpipe->pipe_buffer.size = BIG_PIPE_SIZE; 73617163Sdyson wpipe->pipe_buffer.buffer = NULL; 73717163Sdyson ++nbigpipe; 73817163Sdyson 73917163Sdyson#ifndef PIPE_NODIRECT 74017163Sdyson wpipe->pipe_map.cnt = 0; 74117163Sdyson wpipe->pipe_map.kva = 0; 74217163Sdyson wpipe->pipe_map.pos = 0; 74317163Sdyson wpipe->pipe_map.npages = 0; 74417163Sdyson#endif 74517163Sdyson 74617163Sdyson } 74717163Sdyson 74817163Sdyson 74913907Sdyson if( wpipe->pipe_buffer.buffer == NULL) { 75013907Sdyson if ((error = pipelock(wpipe,1)) == 0) { 75113907Sdyson pipespace(wpipe); 75213907Sdyson pipeunlock(wpipe); 75313907Sdyson } else { 75413907Sdyson return error; 75513907Sdyson } 75613907Sdyson } 75713907Sdyson 75813675Sdyson ++wpipe->pipe_busy; 75913913Sdyson orig_resid = uio->uio_resid; 76013675Sdyson while (uio->uio_resid) { 76113907Sdyson int space; 76214037Sdyson#ifndef PIPE_NODIRECT 76313907Sdyson /* 76413907Sdyson * If the transfer is large, we can gain performance if 76513907Sdyson * we do process-to-process copies directly. 76616416Sdyson * If the write is non-blocking, we don't use the 76716416Sdyson * direct write mechanism. 76813907Sdyson */ 76917163Sdyson if ((uio->uio_iov->iov_len >= PIPE_MINDIRECT) && 77017163Sdyson (fp->f_flag & FNONBLOCK) == 0 && 77117163Sdyson (wpipe->pipe_map.kva || (amountpipekva < LIMITPIPEKVA)) && 77213907Sdyson (uio->uio_iov->iov_len >= PIPE_MINDIRECT)) { 77313907Sdyson error = pipe_direct_write( wpipe, uio); 77413907Sdyson if (error) { 77513907Sdyson break; 77613907Sdyson } 77713907Sdyson continue; 77813907Sdyson } 77914037Sdyson#endif 78013907Sdyson 78113907Sdyson /* 78213907Sdyson * Pipe buffered writes cannot be coincidental with 78313907Sdyson * direct writes. We wait until the currently executing 78413907Sdyson * direct write is completed before we start filling the 78513907Sdyson * pipe buffer. 78613907Sdyson */ 78713907Sdyson retrywrite: 78813907Sdyson while (wpipe->pipe_state & PIPE_DIRECTW) { 78913992Sdyson if (wpipe->pipe_state & PIPE_WANTR) { 79013992Sdyson wpipe->pipe_state &= ~PIPE_WANTR; 79113992Sdyson wakeup(wpipe); 79213992Sdyson } 79313907Sdyson error = tsleep(wpipe, 79413907Sdyson PRIBIO|PCATCH, "pipbww", 0); 79513907Sdyson if (error) 79613907Sdyson break; 79713907Sdyson } 79813907Sdyson 79913907Sdyson space = wpipe->pipe_buffer.size - wpipe->pipe_buffer.cnt; 80014644Sdyson 80114644Sdyson /* Writes of size <= PIPE_BUF must be atomic. */ 80214644Sdyson /* XXX perhaps they need to be contiguous to be atomic? */ 80313913Sdyson if ((space < uio->uio_resid) && (orig_resid <= PIPE_BUF)) 80413913Sdyson space = 0; 80513907Sdyson 80617163Sdyson if (space > 0 && (wpipe->pipe_buffer.cnt < PIPE_SIZE)) { 80717163Sdyson /* 80817163Sdyson * This set the maximum transfer as a segment of 80917163Sdyson * the buffer. 81017163Sdyson */ 81113675Sdyson int size = wpipe->pipe_buffer.size - wpipe->pipe_buffer.in; 81217163Sdyson /* 81317163Sdyson * space is the size left in the buffer 81417163Sdyson */ 81513675Sdyson if (size > space) 81613675Sdyson size = space; 81717163Sdyson /* 81817163Sdyson * now limit it to the size of the uio transfer 81917163Sdyson */ 82013675Sdyson if (size > uio->uio_resid) 82113675Sdyson size = uio->uio_resid; 82213907Sdyson if ((error = pipelock(wpipe,1)) == 0) { 82313907Sdyson /* 82413907Sdyson * It is possible for a direct write to 82513907Sdyson * slip in on us... handle it here... 82613907Sdyson */ 82713907Sdyson if (wpipe->pipe_state & PIPE_DIRECTW) { 82813907Sdyson pipeunlock(wpipe); 82913907Sdyson goto retrywrite; 83013907Sdyson } 83113675Sdyson error = uiomove( &wpipe->pipe_buffer.buffer[wpipe->pipe_buffer.in], 83213675Sdyson size, uio); 83313675Sdyson pipeunlock(wpipe); 83413675Sdyson } 83513675Sdyson if (error) 83613675Sdyson break; 83713675Sdyson 83813675Sdyson wpipe->pipe_buffer.in += size; 83913675Sdyson if (wpipe->pipe_buffer.in >= wpipe->pipe_buffer.size) 84013675Sdyson wpipe->pipe_buffer.in = 0; 84113675Sdyson 84213675Sdyson wpipe->pipe_buffer.cnt += size; 84313675Sdyson } else { 84413675Sdyson /* 84513675Sdyson * If the "read-side" has been blocked, wake it up now. 84613675Sdyson */ 84713675Sdyson if (wpipe->pipe_state & PIPE_WANTR) { 84813675Sdyson wpipe->pipe_state &= ~PIPE_WANTR; 84913675Sdyson wakeup(wpipe); 85013675Sdyson } 85114037Sdyson 85213675Sdyson /* 85313675Sdyson * don't block on non-blocking I/O 85413675Sdyson */ 85516960Sdyson if (fp->f_flag & FNONBLOCK) { 85613907Sdyson error = EAGAIN; 85713675Sdyson break; 85813675Sdyson } 85913907Sdyson 86014037Sdyson /* 86114037Sdyson * We have no more space and have something to offer, 86229356Speter * wake up select/poll. 86314037Sdyson */ 86414037Sdyson pipeselwakeup(wpipe); 86514037Sdyson 86613675Sdyson wpipe->pipe_state |= PIPE_WANTW; 86743301Sdillon if ((error = tsleep(wpipe, (PRIBIO+1)|PCATCH, "pipewr", 0)) != 0) { 86813675Sdyson break; 86913675Sdyson } 87013675Sdyson /* 87113675Sdyson * If read side wants to go away, we just issue a signal 87213675Sdyson * to ourselves. 87313675Sdyson */ 87413675Sdyson if (wpipe->pipe_state & PIPE_EOF) { 87513774Sdyson error = EPIPE; 87613907Sdyson break; 87713675Sdyson } 87813675Sdyson } 87913675Sdyson } 88013675Sdyson 88114644Sdyson --wpipe->pipe_busy; 88213675Sdyson if ((wpipe->pipe_busy == 0) && 88313675Sdyson (wpipe->pipe_state & PIPE_WANT)) { 88413675Sdyson wpipe->pipe_state &= ~(PIPE_WANT|PIPE_WANTR); 88513675Sdyson wakeup(wpipe); 88613675Sdyson } else if (wpipe->pipe_buffer.cnt > 0) { 88713675Sdyson /* 88813675Sdyson * If we have put any characters in the buffer, we wake up 88913675Sdyson * the reader. 89013675Sdyson */ 89113675Sdyson if (wpipe->pipe_state & PIPE_WANTR) { 89213675Sdyson wpipe->pipe_state &= ~PIPE_WANTR; 89313675Sdyson wakeup(wpipe); 89413675Sdyson } 89513675Sdyson } 89613909Sdyson 89713909Sdyson /* 89813909Sdyson * Don't return EPIPE if I/O was successful 89913909Sdyson */ 90013907Sdyson if ((wpipe->pipe_buffer.cnt == 0) && 90113907Sdyson (uio->uio_resid == 0) && 90213907Sdyson (error == EPIPE)) 90313907Sdyson error = 0; 90413913Sdyson 90524101Sbde if (error == 0) 90634901Sphk getnanotime(&wpipe->pipe_mtime); 90724101Sbde 90814037Sdyson /* 90914037Sdyson * We have something to offer, 91029356Speter * wake up select/poll. 91114037Sdyson */ 91214177Sdyson if (wpipe->pipe_buffer.cnt) 91314037Sdyson pipeselwakeup(wpipe); 91413907Sdyson 91513675Sdyson return error; 91613675Sdyson} 91713675Sdyson 91813675Sdyson/* 91913675Sdyson * we implement a very minimal set of ioctls for compatibility with sockets. 92013675Sdyson */ 92113675Sdysonint 92213675Sdysonpipe_ioctl(fp, cmd, data, p) 92313675Sdyson struct file *fp; 92436735Sdfr u_long cmd; 92513675Sdyson register caddr_t data; 92613675Sdyson struct proc *p; 92713675Sdyson{ 92813675Sdyson register struct pipe *mpipe = (struct pipe *)fp->f_data; 92913675Sdyson 93013675Sdyson switch (cmd) { 93113675Sdyson 93213675Sdyson case FIONBIO: 93313675Sdyson return (0); 93413675Sdyson 93513675Sdyson case FIOASYNC: 93613675Sdyson if (*(int *)data) { 93713675Sdyson mpipe->pipe_state |= PIPE_ASYNC; 93813675Sdyson } else { 93913675Sdyson mpipe->pipe_state &= ~PIPE_ASYNC; 94013675Sdyson } 94113675Sdyson return (0); 94213675Sdyson 94313675Sdyson case FIONREAD: 94414037Sdyson if (mpipe->pipe_state & PIPE_DIRECTW) 94514037Sdyson *(int *)data = mpipe->pipe_map.cnt; 94614037Sdyson else 94714037Sdyson *(int *)data = mpipe->pipe_buffer.cnt; 94813675Sdyson return (0); 94913675Sdyson 95041086Struckman case FIOSETOWN: 95141086Struckman return (fsetown(*(int *)data, &mpipe->pipe_sigio)); 95241086Struckman 95341086Struckman case FIOGETOWN: 95441086Struckman *(int *)data = fgetown(mpipe->pipe_sigio); 95513675Sdyson return (0); 95613675Sdyson 95741086Struckman /* This is deprecated, FIOSETOWN should be used instead. */ 95841086Struckman case TIOCSPGRP: 95941086Struckman return (fsetown(-(*(int *)data), &mpipe->pipe_sigio)); 96041086Struckman 96141086Struckman /* This is deprecated, FIOGETOWN should be used instead. */ 96218863Sdyson case TIOCGPGRP: 96341086Struckman *(int *)data = -fgetown(mpipe->pipe_sigio); 96413675Sdyson return (0); 96513675Sdyson 96613675Sdyson } 96717124Sbde return (ENOTTY); 96813675Sdyson} 96913675Sdyson 97013675Sdysonint 97129356Speterpipe_poll(fp, events, cred, p) 97213675Sdyson struct file *fp; 97329356Speter int events; 97429356Speter struct ucred *cred; 97513675Sdyson struct proc *p; 97613675Sdyson{ 97713675Sdyson register struct pipe *rpipe = (struct pipe *)fp->f_data; 97813675Sdyson struct pipe *wpipe; 97929356Speter int revents = 0; 98013675Sdyson 98113675Sdyson wpipe = rpipe->pipe_peer; 98229356Speter if (events & (POLLIN | POLLRDNORM)) 98329356Speter if ((rpipe->pipe_state & PIPE_DIRECTW) || 98429356Speter (rpipe->pipe_buffer.cnt > 0) || 98529356Speter (rpipe->pipe_state & PIPE_EOF)) 98629356Speter revents |= events & (POLLIN | POLLRDNORM); 98713675Sdyson 98829356Speter if (events & (POLLOUT | POLLWRNORM)) 98929356Speter if (wpipe == NULL || (wpipe->pipe_state & PIPE_EOF) || 99043311Sdillon (((wpipe->pipe_state & PIPE_DIRECTW) == 0) && 99143311Sdillon (wpipe->pipe_buffer.size - wpipe->pipe_buffer.cnt) >= PIPE_BUF)) 99229356Speter revents |= events & (POLLOUT | POLLWRNORM); 99313675Sdyson 99429356Speter if ((rpipe->pipe_state & PIPE_EOF) || 99529356Speter (wpipe == NULL) || 99629356Speter (wpipe->pipe_state & PIPE_EOF)) 99729356Speter revents |= POLLHUP; 99829356Speter 99929356Speter if (revents == 0) { 100029356Speter if (events & (POLLIN | POLLRDNORM)) { 100129356Speter selrecord(p, &rpipe->pipe_sel); 100229356Speter rpipe->pipe_state |= PIPE_SEL; 100313675Sdyson } 100413675Sdyson 100529356Speter if (events & (POLLOUT | POLLWRNORM)) { 100630164Speter selrecord(p, &wpipe->pipe_sel); 100730164Speter wpipe->pipe_state |= PIPE_SEL; 100813907Sdyson } 100913675Sdyson } 101029356Speter 101129356Speter return (revents); 101213675Sdyson} 101313675Sdyson 101413675Sdysonint 101513675Sdysonpipe_stat(pipe, ub) 101613675Sdyson register struct pipe *pipe; 101713675Sdyson register struct stat *ub; 101813675Sdyson{ 101913675Sdyson bzero((caddr_t)ub, sizeof (*ub)); 102017124Sbde ub->st_mode = S_IFIFO; 102113907Sdyson ub->st_blksize = pipe->pipe_buffer.size; 102213675Sdyson ub->st_size = pipe->pipe_buffer.cnt; 102313675Sdyson ub->st_blocks = (ub->st_size + ub->st_blksize - 1) / ub->st_blksize; 102434901Sphk ub->st_atimespec = pipe->pipe_atime; 102534901Sphk ub->st_mtimespec = pipe->pipe_mtime; 102634901Sphk ub->st_ctimespec = pipe->pipe_ctime; 102717124Sbde /* 102817124Sbde * Left as 0: st_dev, st_ino, st_nlink, st_uid, st_gid, st_rdev, 102917124Sbde * st_flags, st_gen. 103017124Sbde * XXX (st_dev, st_ino) should be unique. 103117124Sbde */ 103213675Sdyson return 0; 103313675Sdyson} 103413675Sdyson 103513675Sdyson/* ARGSUSED */ 103613675Sdysonstatic int 103713675Sdysonpipe_close(fp, p) 103813675Sdyson struct file *fp; 103913675Sdyson struct proc *p; 104013675Sdyson{ 104113675Sdyson struct pipe *cpipe = (struct pipe *)fp->f_data; 104216322Sgpalmer 104341086Struckman funsetown(cpipe->pipe_sigio); 104413675Sdyson pipeclose(cpipe); 104513675Sdyson fp->f_data = NULL; 104613675Sdyson return 0; 104713675Sdyson} 104813675Sdyson 104913675Sdyson/* 105013675Sdyson * shutdown the pipe 105113675Sdyson */ 105213675Sdysonstatic void 105313675Sdysonpipeclose(cpipe) 105413675Sdyson struct pipe *cpipe; 105513675Sdyson{ 105613907Sdyson struct pipe *ppipe; 105713675Sdyson if (cpipe) { 105813907Sdyson 105914037Sdyson pipeselwakeup(cpipe); 106013907Sdyson 106113675Sdyson /* 106213675Sdyson * If the other side is blocked, wake it up saying that 106313675Sdyson * we want to close it down. 106413675Sdyson */ 106513675Sdyson while (cpipe->pipe_busy) { 106613675Sdyson wakeup(cpipe); 106713675Sdyson cpipe->pipe_state |= PIPE_WANT|PIPE_EOF; 106813675Sdyson tsleep(cpipe, PRIBIO, "pipecl", 0); 106913675Sdyson } 107013675Sdyson 107113675Sdyson /* 107213675Sdyson * Disconnect from peer 107313675Sdyson */ 107443301Sdillon if ((ppipe = cpipe->pipe_peer) != NULL) { 107514037Sdyson pipeselwakeup(ppipe); 107613907Sdyson 107713907Sdyson ppipe->pipe_state |= PIPE_EOF; 107813907Sdyson wakeup(ppipe); 107913907Sdyson ppipe->pipe_peer = NULL; 108013675Sdyson } 108113675Sdyson 108213675Sdyson /* 108313675Sdyson * free resources 108413675Sdyson */ 108513907Sdyson if (cpipe->pipe_buffer.buffer) { 108617163Sdyson if (cpipe->pipe_buffer.size > PIPE_SIZE) 108717163Sdyson --nbigpipe; 108813907Sdyson amountpipekva -= cpipe->pipe_buffer.size; 108913907Sdyson kmem_free(kernel_map, 109013907Sdyson (vm_offset_t)cpipe->pipe_buffer.buffer, 109113907Sdyson cpipe->pipe_buffer.size); 109213907Sdyson } 109314037Sdyson#ifndef PIPE_NODIRECT 109413907Sdyson if (cpipe->pipe_map.kva) { 109513912Sdyson amountpipekva -= cpipe->pipe_buffer.size + PAGE_SIZE; 109613907Sdyson kmem_free(kernel_map, 109713907Sdyson cpipe->pipe_map.kva, 109813912Sdyson cpipe->pipe_buffer.size + PAGE_SIZE); 109913907Sdyson } 110014037Sdyson#endif 110127899Sdyson zfree(pipe_zone, cpipe); 110213675Sdyson } 110313675Sdyson} 1104