sys_pipe.c revision 47748
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 * 1947748Salc * $Id: sys_pipe.c,v 1.51 1999/04/04 21:41:15 dt 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, 9145311Sdt struct ucred *cred, int flags)); 9213675Sdysonstatic int pipe_write __P((struct file *fp, struct uio *uio, 9345311Sdt struct ucred *cred, int flags)); 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 32245311Sdtpipe_read(fp, uio, cred, flags) 32313675Sdyson struct file *fp; 32413675Sdyson struct uio *uio; 32513675Sdyson struct ucred *cred; 32645311Sdt int flags; 32713675Sdyson{ 32813675Sdyson 32913675Sdyson struct pipe *rpipe = (struct pipe *) fp->f_data; 33047748Salc int error; 33113675Sdyson int nread = 0; 33218863Sdyson u_int size; 33313675Sdyson 33413675Sdyson ++rpipe->pipe_busy; 33547748Salc error = pipelock(rpipe, 1); 33647748Salc if (error) 33747748Salc goto unlocked_error; 33847748Salc 33913675Sdyson while (uio->uio_resid) { 34013907Sdyson /* 34113907Sdyson * normal pipe buffer receive 34213907Sdyson */ 34313675Sdyson if (rpipe->pipe_buffer.cnt > 0) { 34418863Sdyson size = rpipe->pipe_buffer.size - rpipe->pipe_buffer.out; 34513675Sdyson if (size > rpipe->pipe_buffer.cnt) 34613675Sdyson size = rpipe->pipe_buffer.cnt; 34718863Sdyson if (size > (u_int) uio->uio_resid) 34818863Sdyson size = (u_int) uio->uio_resid; 34947748Salc 35047748Salc error = uiomove(&rpipe->pipe_buffer.buffer[rpipe->pipe_buffer.out], 35113675Sdyson size, uio); 35213675Sdyson if (error) { 35313675Sdyson break; 35413675Sdyson } 35513675Sdyson rpipe->pipe_buffer.out += size; 35613675Sdyson if (rpipe->pipe_buffer.out >= rpipe->pipe_buffer.size) 35713675Sdyson rpipe->pipe_buffer.out = 0; 35813675Sdyson 35913675Sdyson rpipe->pipe_buffer.cnt -= size; 36047748Salc 36147748Salc /* 36247748Salc * If there is no more to read in the pipe, reset 36347748Salc * its pointers to the beginning. This improves 36447748Salc * cache hit stats. 36547748Salc */ 36647748Salc if (rpipe->pipe_buffer.cnt == 0) { 36747748Salc rpipe->pipe_buffer.in = 0; 36847748Salc rpipe->pipe_buffer.out = 0; 36947748Salc } 37013675Sdyson nread += size; 37114037Sdyson#ifndef PIPE_NODIRECT 37213907Sdyson /* 37313907Sdyson * Direct copy, bypassing a kernel buffer. 37413907Sdyson */ 37513907Sdyson } else if ((size = rpipe->pipe_map.cnt) && 37647748Salc (rpipe->pipe_state & PIPE_DIRECTW)) { 37747748Salc caddr_t va; 37818863Sdyson if (size > (u_int) uio->uio_resid) 37918863Sdyson size = (u_int) uio->uio_resid; 38047748Salc 38147748Salc va = (caddr_t) rpipe->pipe_map.kva + rpipe->pipe_map.pos; 38247748Salc error = uiomove(va, size, uio); 38313907Sdyson if (error) 38413907Sdyson break; 38513907Sdyson nread += size; 38613907Sdyson rpipe->pipe_map.pos += size; 38713907Sdyson rpipe->pipe_map.cnt -= size; 38813907Sdyson if (rpipe->pipe_map.cnt == 0) { 38913907Sdyson rpipe->pipe_state &= ~PIPE_DIRECTW; 39013907Sdyson wakeup(rpipe); 39113907Sdyson } 39214037Sdyson#endif 39313675Sdyson } else { 39413675Sdyson /* 39513675Sdyson * detect EOF condition 39613675Sdyson */ 39713675Sdyson if (rpipe->pipe_state & PIPE_EOF) { 39814802Sdyson /* XXX error = ? */ 39913675Sdyson break; 40013675Sdyson } 40143623Sdillon 40213675Sdyson /* 40313675Sdyson * If the "write-side" has been blocked, wake it up now. 40413675Sdyson */ 40513675Sdyson if (rpipe->pipe_state & PIPE_WANTW) { 40613675Sdyson rpipe->pipe_state &= ~PIPE_WANTW; 40713675Sdyson wakeup(rpipe); 40813675Sdyson } 40943623Sdillon 41043623Sdillon /* 41147748Salc * Break if some data was read. 41243623Sdillon */ 41347748Salc if (nread > 0) 41413675Sdyson break; 41516960Sdyson 41643623Sdillon /* 41747748Salc * Unlock the pipe buffer for our remaining processing. We 41847748Salc * will either break out with an error or we will sleep and 41947748Salc * relock to loop. 42043623Sdillon */ 42147748Salc pipeunlock(rpipe); 42243623Sdillon 42313675Sdyson /* 42447748Salc * Handle non-blocking mode operation or 42547748Salc * wait for more data. 42613675Sdyson */ 42747748Salc if (fp->f_flag & FNONBLOCK) 42847748Salc error = EAGAIN; 42947748Salc else { 43047748Salc rpipe->pipe_state |= PIPE_WANTR; 43147748Salc if ((error = tsleep(rpipe, PRIBIO|PCATCH, "piperd", 0)) == 0) 43247748Salc error = pipelock(rpipe, 1); 43313675Sdyson } 43447748Salc if (error) 43547748Salc goto unlocked_error; 43613675Sdyson } 43713675Sdyson } 43847748Salc pipeunlock(rpipe); 43913675Sdyson 44024101Sbde if (error == 0) 44134901Sphk getnanotime(&rpipe->pipe_atime); 44247748Salcunlocked_error: 44347748Salc --rpipe->pipe_busy; 44413913Sdyson 44547748Salc /* 44647748Salc * PIPE_WANT processing only makes sense if pipe_busy is 0. 44747748Salc */ 44813675Sdyson if ((rpipe->pipe_busy == 0) && (rpipe->pipe_state & PIPE_WANT)) { 44913675Sdyson rpipe->pipe_state &= ~(PIPE_WANT|PIPE_WANTW); 45013675Sdyson wakeup(rpipe); 45113675Sdyson } else if (rpipe->pipe_buffer.cnt < MINPIPESIZE) { 45213675Sdyson /* 45347748Salc * Handle write blocking hysteresis. 45413675Sdyson */ 45513675Sdyson if (rpipe->pipe_state & PIPE_WANTW) { 45613675Sdyson rpipe->pipe_state &= ~PIPE_WANTW; 45713675Sdyson wakeup(rpipe); 45813675Sdyson } 45913675Sdyson } 46014037Sdyson 46114802Sdyson if ((rpipe->pipe_buffer.size - rpipe->pipe_buffer.cnt) >= PIPE_BUF) 46214037Sdyson pipeselwakeup(rpipe); 46314037Sdyson 46413675Sdyson return error; 46513675Sdyson} 46613675Sdyson 46714037Sdyson#ifndef PIPE_NODIRECT 46813907Sdyson/* 46913907Sdyson * Map the sending processes' buffer into kernel space and wire it. 47013907Sdyson * This is similar to a physical write operation. 47113907Sdyson */ 47213675Sdysonstatic int 47313907Sdysonpipe_build_write_buffer(wpipe, uio) 47413907Sdyson struct pipe *wpipe; 47513675Sdyson struct uio *uio; 47613675Sdyson{ 47718863Sdyson u_int size; 47813907Sdyson int i; 47913907Sdyson vm_offset_t addr, endaddr, paddr; 48013907Sdyson 48118863Sdyson size = (u_int) uio->uio_iov->iov_len; 48213907Sdyson if (size > wpipe->pipe_buffer.size) 48313907Sdyson size = wpipe->pipe_buffer.size; 48413907Sdyson 48540286Sdg endaddr = round_page((vm_offset_t)uio->uio_iov->iov_base + size); 48640286Sdg for(i = 0, addr = trunc_page((vm_offset_t)uio->uio_iov->iov_base); 48713907Sdyson addr < endaddr; 48813907Sdyson addr += PAGE_SIZE, i+=1) { 48913907Sdyson 49013907Sdyson vm_page_t m; 49113907Sdyson 49213909Sdyson vm_fault_quick( (caddr_t) addr, VM_PROT_READ); 49313907Sdyson paddr = pmap_kextract(addr); 49413907Sdyson if (!paddr) { 49513907Sdyson int j; 49613907Sdyson for(j=0;j<i;j++) 49740700Sdg vm_page_unwire(wpipe->pipe_map.ms[j], 1); 49813907Sdyson return EFAULT; 49913907Sdyson } 50013907Sdyson 50113907Sdyson m = PHYS_TO_VM_PAGE(paddr); 50213907Sdyson vm_page_wire(m); 50313907Sdyson wpipe->pipe_map.ms[i] = m; 50413907Sdyson } 50513907Sdyson 50613907Sdyson/* 50713907Sdyson * set up the control block 50813907Sdyson */ 50913907Sdyson wpipe->pipe_map.npages = i; 51013907Sdyson wpipe->pipe_map.pos = ((vm_offset_t) uio->uio_iov->iov_base) & PAGE_MASK; 51113907Sdyson wpipe->pipe_map.cnt = size; 51213907Sdyson 51313907Sdyson/* 51413907Sdyson * and map the buffer 51513907Sdyson */ 51613907Sdyson if (wpipe->pipe_map.kva == 0) { 51713912Sdyson /* 51813912Sdyson * We need to allocate space for an extra page because the 51913912Sdyson * address range might (will) span pages at times. 52013912Sdyson */ 52113907Sdyson wpipe->pipe_map.kva = kmem_alloc_pageable(kernel_map, 52213912Sdyson wpipe->pipe_buffer.size + PAGE_SIZE); 52313912Sdyson amountpipekva += wpipe->pipe_buffer.size + PAGE_SIZE; 52413907Sdyson } 52513907Sdyson pmap_qenter(wpipe->pipe_map.kva, wpipe->pipe_map.ms, 52613907Sdyson wpipe->pipe_map.npages); 52713907Sdyson 52813907Sdyson/* 52913907Sdyson * and update the uio data 53013907Sdyson */ 53113907Sdyson 53213907Sdyson uio->uio_iov->iov_len -= size; 53313907Sdyson uio->uio_iov->iov_base += size; 53413907Sdyson if (uio->uio_iov->iov_len == 0) 53513907Sdyson uio->uio_iov++; 53613907Sdyson uio->uio_resid -= size; 53713907Sdyson uio->uio_offset += size; 53813907Sdyson return 0; 53913907Sdyson} 54013907Sdyson 54113907Sdyson/* 54213907Sdyson * unmap and unwire the process buffer 54313907Sdyson */ 54413907Sdysonstatic void 54513907Sdysonpipe_destroy_write_buffer(wpipe) 54613907Sdysonstruct pipe *wpipe; 54713907Sdyson{ 54813907Sdyson int i; 54917163Sdyson if (wpipe->pipe_map.kva) { 55017163Sdyson pmap_qremove(wpipe->pipe_map.kva, wpipe->pipe_map.npages); 55113907Sdyson 55213907Sdyson if (amountpipekva > MAXPIPEKVA) { 55313907Sdyson vm_offset_t kva = wpipe->pipe_map.kva; 55413907Sdyson wpipe->pipe_map.kva = 0; 55513907Sdyson kmem_free(kernel_map, kva, 55613912Sdyson wpipe->pipe_buffer.size + PAGE_SIZE); 55713912Sdyson amountpipekva -= wpipe->pipe_buffer.size + PAGE_SIZE; 55813907Sdyson } 55913907Sdyson } 56013907Sdyson for (i=0;i<wpipe->pipe_map.npages;i++) 56140700Sdg vm_page_unwire(wpipe->pipe_map.ms[i], 1); 56213907Sdyson} 56313907Sdyson 56413907Sdyson/* 56513907Sdyson * In the case of a signal, the writing process might go away. This 56613907Sdyson * code copies the data into the circular buffer so that the source 56713907Sdyson * pages can be freed without loss of data. 56813907Sdyson */ 56913907Sdysonstatic void 57013907Sdysonpipe_clone_write_buffer(wpipe) 57113907Sdysonstruct pipe *wpipe; 57213907Sdyson{ 57313907Sdyson int size; 57413907Sdyson int pos; 57513907Sdyson 57613907Sdyson size = wpipe->pipe_map.cnt; 57713907Sdyson pos = wpipe->pipe_map.pos; 57813907Sdyson bcopy((caddr_t) wpipe->pipe_map.kva+pos, 57913907Sdyson (caddr_t) wpipe->pipe_buffer.buffer, 58013907Sdyson size); 58113907Sdyson 58213907Sdyson wpipe->pipe_buffer.in = size; 58313907Sdyson wpipe->pipe_buffer.out = 0; 58413907Sdyson wpipe->pipe_buffer.cnt = size; 58513907Sdyson wpipe->pipe_state &= ~PIPE_DIRECTW; 58613907Sdyson 58713907Sdyson pipe_destroy_write_buffer(wpipe); 58813907Sdyson} 58913907Sdyson 59013907Sdyson/* 59113907Sdyson * This implements the pipe buffer write mechanism. Note that only 59213907Sdyson * a direct write OR a normal pipe write can be pending at any given time. 59313907Sdyson * If there are any characters in the pipe buffer, the direct write will 59413907Sdyson * be deferred until the receiving process grabs all of the bytes from 59513907Sdyson * the pipe buffer. Then the direct mapping write is set-up. 59613907Sdyson */ 59713907Sdysonstatic int 59813907Sdysonpipe_direct_write(wpipe, uio) 59913907Sdyson struct pipe *wpipe; 60013907Sdyson struct uio *uio; 60113907Sdyson{ 60213907Sdyson int error; 60313951Sdysonretry: 60413907Sdyson while (wpipe->pipe_state & PIPE_DIRECTW) { 60513951Sdyson if ( wpipe->pipe_state & PIPE_WANTR) { 60613951Sdyson wpipe->pipe_state &= ~PIPE_WANTR; 60713951Sdyson wakeup(wpipe); 60813951Sdyson } 60913992Sdyson wpipe->pipe_state |= PIPE_WANTW; 61013907Sdyson error = tsleep(wpipe, 61113907Sdyson PRIBIO|PCATCH, "pipdww", 0); 61214802Sdyson if (error) 61313907Sdyson goto error1; 61414802Sdyson if (wpipe->pipe_state & PIPE_EOF) { 61514802Sdyson error = EPIPE; 61614802Sdyson goto error1; 61714802Sdyson } 61813907Sdyson } 61913907Sdyson wpipe->pipe_map.cnt = 0; /* transfer not ready yet */ 62013951Sdyson if (wpipe->pipe_buffer.cnt > 0) { 62113951Sdyson if ( wpipe->pipe_state & PIPE_WANTR) { 62213951Sdyson wpipe->pipe_state &= ~PIPE_WANTR; 62313951Sdyson wakeup(wpipe); 62413951Sdyson } 62513951Sdyson 62613992Sdyson wpipe->pipe_state |= PIPE_WANTW; 62713907Sdyson error = tsleep(wpipe, 62813907Sdyson PRIBIO|PCATCH, "pipdwc", 0); 62914802Sdyson if (error) 63013907Sdyson goto error1; 63114802Sdyson if (wpipe->pipe_state & PIPE_EOF) { 63214802Sdyson error = EPIPE; 63314802Sdyson goto error1; 63413907Sdyson } 63513951Sdyson goto retry; 63613907Sdyson } 63713907Sdyson 63813951Sdyson wpipe->pipe_state |= PIPE_DIRECTW; 63913951Sdyson 64013907Sdyson error = pipe_build_write_buffer(wpipe, uio); 64113907Sdyson if (error) { 64213907Sdyson wpipe->pipe_state &= ~PIPE_DIRECTW; 64313907Sdyson goto error1; 64413907Sdyson } 64513907Sdyson 64613907Sdyson error = 0; 64713907Sdyson while (!error && (wpipe->pipe_state & PIPE_DIRECTW)) { 64813907Sdyson if (wpipe->pipe_state & PIPE_EOF) { 64913907Sdyson pipelock(wpipe, 0); 65013907Sdyson pipe_destroy_write_buffer(wpipe); 65113907Sdyson pipeunlock(wpipe); 65214037Sdyson pipeselwakeup(wpipe); 65314802Sdyson error = EPIPE; 65414802Sdyson goto error1; 65513907Sdyson } 65613992Sdyson if (wpipe->pipe_state & PIPE_WANTR) { 65713992Sdyson wpipe->pipe_state &= ~PIPE_WANTR; 65813992Sdyson wakeup(wpipe); 65913992Sdyson } 66014037Sdyson pipeselwakeup(wpipe); 66113907Sdyson error = tsleep(wpipe, PRIBIO|PCATCH, "pipdwt", 0); 66213907Sdyson } 66313907Sdyson 66413907Sdyson pipelock(wpipe,0); 66513907Sdyson if (wpipe->pipe_state & PIPE_DIRECTW) { 66613907Sdyson /* 66713907Sdyson * this bit of trickery substitutes a kernel buffer for 66813907Sdyson * the process that might be going away. 66913907Sdyson */ 67013907Sdyson pipe_clone_write_buffer(wpipe); 67113907Sdyson } else { 67213907Sdyson pipe_destroy_write_buffer(wpipe); 67313907Sdyson } 67413907Sdyson pipeunlock(wpipe); 67513907Sdyson return error; 67613907Sdyson 67713907Sdysonerror1: 67813907Sdyson wakeup(wpipe); 67913907Sdyson return error; 68013907Sdyson} 68114037Sdyson#endif 68213907Sdyson 68316960Sdysonstatic int 68445311Sdtpipe_write(fp, uio, cred, flags) 68516960Sdyson struct file *fp; 68613907Sdyson struct uio *uio; 68716960Sdyson struct ucred *cred; 68845311Sdt int flags; 68913907Sdyson{ 69013675Sdyson int error = 0; 69113913Sdyson int orig_resid; 69213675Sdyson 69316960Sdyson struct pipe *wpipe, *rpipe; 69416960Sdyson 69516960Sdyson rpipe = (struct pipe *) fp->f_data; 69616960Sdyson wpipe = rpipe->pipe_peer; 69716960Sdyson 69813675Sdyson /* 69913675Sdyson * detect loss of pipe read side, issue SIGPIPE if lost. 70013675Sdyson */ 70116960Sdyson if ((wpipe == NULL) || (wpipe->pipe_state & PIPE_EOF)) { 70213774Sdyson return EPIPE; 70313675Sdyson } 70413675Sdyson 70517163Sdyson /* 70617163Sdyson * If it is advantageous to resize the pipe buffer, do 70717163Sdyson * so. 70817163Sdyson */ 70917163Sdyson if ((uio->uio_resid > PIPE_SIZE) && 71017163Sdyson (nbigpipe < LIMITBIGPIPES) && 71117163Sdyson (wpipe->pipe_state & PIPE_DIRECTW) == 0 && 71217163Sdyson (wpipe->pipe_buffer.size <= PIPE_SIZE) && 71317163Sdyson (wpipe->pipe_buffer.cnt == 0)) { 71417163Sdyson 71517163Sdyson if (wpipe->pipe_buffer.buffer) { 71617163Sdyson amountpipekva -= wpipe->pipe_buffer.size; 71717163Sdyson kmem_free(kernel_map, 71817163Sdyson (vm_offset_t)wpipe->pipe_buffer.buffer, 71917163Sdyson wpipe->pipe_buffer.size); 72017163Sdyson } 72117163Sdyson 72217163Sdyson#ifndef PIPE_NODIRECT 72317163Sdyson if (wpipe->pipe_map.kva) { 72417163Sdyson amountpipekva -= wpipe->pipe_buffer.size + PAGE_SIZE; 72517163Sdyson kmem_free(kernel_map, 72617163Sdyson wpipe->pipe_map.kva, 72717163Sdyson wpipe->pipe_buffer.size + PAGE_SIZE); 72817163Sdyson } 72917163Sdyson#endif 73017163Sdyson 73117163Sdyson wpipe->pipe_buffer.in = 0; 73217163Sdyson wpipe->pipe_buffer.out = 0; 73317163Sdyson wpipe->pipe_buffer.cnt = 0; 73417163Sdyson wpipe->pipe_buffer.size = BIG_PIPE_SIZE; 73517163Sdyson wpipe->pipe_buffer.buffer = NULL; 73617163Sdyson ++nbigpipe; 73717163Sdyson 73817163Sdyson#ifndef PIPE_NODIRECT 73917163Sdyson wpipe->pipe_map.cnt = 0; 74017163Sdyson wpipe->pipe_map.kva = 0; 74117163Sdyson wpipe->pipe_map.pos = 0; 74217163Sdyson wpipe->pipe_map.npages = 0; 74317163Sdyson#endif 74417163Sdyson 74517163Sdyson } 74617163Sdyson 74717163Sdyson 74813907Sdyson if( wpipe->pipe_buffer.buffer == NULL) { 74913907Sdyson if ((error = pipelock(wpipe,1)) == 0) { 75013907Sdyson pipespace(wpipe); 75113907Sdyson pipeunlock(wpipe); 75213907Sdyson } else { 75313907Sdyson return error; 75413907Sdyson } 75513907Sdyson } 75613907Sdyson 75713675Sdyson ++wpipe->pipe_busy; 75813913Sdyson orig_resid = uio->uio_resid; 75913675Sdyson while (uio->uio_resid) { 76013907Sdyson int space; 76114037Sdyson#ifndef PIPE_NODIRECT 76213907Sdyson /* 76313907Sdyson * If the transfer is large, we can gain performance if 76413907Sdyson * we do process-to-process copies directly. 76516416Sdyson * If the write is non-blocking, we don't use the 76616416Sdyson * direct write mechanism. 76713907Sdyson */ 76817163Sdyson if ((uio->uio_iov->iov_len >= PIPE_MINDIRECT) && 76917163Sdyson (fp->f_flag & FNONBLOCK) == 0 && 77017163Sdyson (wpipe->pipe_map.kva || (amountpipekva < LIMITPIPEKVA)) && 77113907Sdyson (uio->uio_iov->iov_len >= PIPE_MINDIRECT)) { 77213907Sdyson error = pipe_direct_write( wpipe, uio); 77313907Sdyson if (error) { 77413907Sdyson break; 77513907Sdyson } 77613907Sdyson continue; 77713907Sdyson } 77814037Sdyson#endif 77913907Sdyson 78013907Sdyson /* 78113907Sdyson * Pipe buffered writes cannot be coincidental with 78213907Sdyson * direct writes. We wait until the currently executing 78313907Sdyson * direct write is completed before we start filling the 78413907Sdyson * pipe buffer. 78513907Sdyson */ 78613907Sdyson retrywrite: 78713907Sdyson while (wpipe->pipe_state & PIPE_DIRECTW) { 78813992Sdyson if (wpipe->pipe_state & PIPE_WANTR) { 78913992Sdyson wpipe->pipe_state &= ~PIPE_WANTR; 79013992Sdyson wakeup(wpipe); 79113992Sdyson } 79213907Sdyson error = tsleep(wpipe, 79313907Sdyson PRIBIO|PCATCH, "pipbww", 0); 79413907Sdyson if (error) 79513907Sdyson break; 79613907Sdyson } 79713907Sdyson 79813907Sdyson space = wpipe->pipe_buffer.size - wpipe->pipe_buffer.cnt; 79914644Sdyson 80014644Sdyson /* Writes of size <= PIPE_BUF must be atomic. */ 80114644Sdyson /* XXX perhaps they need to be contiguous to be atomic? */ 80213913Sdyson if ((space < uio->uio_resid) && (orig_resid <= PIPE_BUF)) 80313913Sdyson space = 0; 80413907Sdyson 80517163Sdyson if (space > 0 && (wpipe->pipe_buffer.cnt < PIPE_SIZE)) { 80617163Sdyson /* 80717163Sdyson * This set the maximum transfer as a segment of 80817163Sdyson * the buffer. 80917163Sdyson */ 81013675Sdyson int size = wpipe->pipe_buffer.size - wpipe->pipe_buffer.in; 81117163Sdyson /* 81217163Sdyson * space is the size left in the buffer 81317163Sdyson */ 81413675Sdyson if (size > space) 81513675Sdyson size = space; 81617163Sdyson /* 81717163Sdyson * now limit it to the size of the uio transfer 81817163Sdyson */ 81913675Sdyson if (size > uio->uio_resid) 82013675Sdyson size = uio->uio_resid; 82113907Sdyson if ((error = pipelock(wpipe,1)) == 0) { 82213907Sdyson /* 82313907Sdyson * It is possible for a direct write to 82413907Sdyson * slip in on us... handle it here... 82513907Sdyson */ 82613907Sdyson if (wpipe->pipe_state & PIPE_DIRECTW) { 82713907Sdyson pipeunlock(wpipe); 82813907Sdyson goto retrywrite; 82913907Sdyson } 83013675Sdyson error = uiomove( &wpipe->pipe_buffer.buffer[wpipe->pipe_buffer.in], 83113675Sdyson size, uio); 83213675Sdyson pipeunlock(wpipe); 83313675Sdyson } 83413675Sdyson if (error) 83513675Sdyson break; 83613675Sdyson 83713675Sdyson wpipe->pipe_buffer.in += size; 83813675Sdyson if (wpipe->pipe_buffer.in >= wpipe->pipe_buffer.size) 83913675Sdyson wpipe->pipe_buffer.in = 0; 84013675Sdyson 84113675Sdyson wpipe->pipe_buffer.cnt += size; 84213675Sdyson } else { 84313675Sdyson /* 84413675Sdyson * If the "read-side" has been blocked, wake it up now. 84513675Sdyson */ 84613675Sdyson if (wpipe->pipe_state & PIPE_WANTR) { 84713675Sdyson wpipe->pipe_state &= ~PIPE_WANTR; 84813675Sdyson wakeup(wpipe); 84913675Sdyson } 85014037Sdyson 85113675Sdyson /* 85213675Sdyson * don't block on non-blocking I/O 85313675Sdyson */ 85416960Sdyson if (fp->f_flag & FNONBLOCK) { 85513907Sdyson error = EAGAIN; 85613675Sdyson break; 85713675Sdyson } 85813907Sdyson 85914037Sdyson /* 86014037Sdyson * We have no more space and have something to offer, 86129356Speter * wake up select/poll. 86214037Sdyson */ 86314037Sdyson pipeselwakeup(wpipe); 86414037Sdyson 86513675Sdyson wpipe->pipe_state |= PIPE_WANTW; 86643301Sdillon if ((error = tsleep(wpipe, (PRIBIO+1)|PCATCH, "pipewr", 0)) != 0) { 86713675Sdyson break; 86813675Sdyson } 86913675Sdyson /* 87013675Sdyson * If read side wants to go away, we just issue a signal 87113675Sdyson * to ourselves. 87213675Sdyson */ 87313675Sdyson if (wpipe->pipe_state & PIPE_EOF) { 87413774Sdyson error = EPIPE; 87513907Sdyson break; 87613675Sdyson } 87713675Sdyson } 87813675Sdyson } 87913675Sdyson 88014644Sdyson --wpipe->pipe_busy; 88113675Sdyson if ((wpipe->pipe_busy == 0) && 88213675Sdyson (wpipe->pipe_state & PIPE_WANT)) { 88313675Sdyson wpipe->pipe_state &= ~(PIPE_WANT|PIPE_WANTR); 88413675Sdyson wakeup(wpipe); 88513675Sdyson } else if (wpipe->pipe_buffer.cnt > 0) { 88613675Sdyson /* 88713675Sdyson * If we have put any characters in the buffer, we wake up 88813675Sdyson * the reader. 88913675Sdyson */ 89013675Sdyson if (wpipe->pipe_state & PIPE_WANTR) { 89113675Sdyson wpipe->pipe_state &= ~PIPE_WANTR; 89213675Sdyson wakeup(wpipe); 89313675Sdyson } 89413675Sdyson } 89513909Sdyson 89613909Sdyson /* 89713909Sdyson * Don't return EPIPE if I/O was successful 89813909Sdyson */ 89913907Sdyson if ((wpipe->pipe_buffer.cnt == 0) && 90013907Sdyson (uio->uio_resid == 0) && 90113907Sdyson (error == EPIPE)) 90213907Sdyson error = 0; 90313913Sdyson 90424101Sbde if (error == 0) 90534901Sphk getnanotime(&wpipe->pipe_mtime); 90624101Sbde 90714037Sdyson /* 90814037Sdyson * We have something to offer, 90929356Speter * wake up select/poll. 91014037Sdyson */ 91114177Sdyson if (wpipe->pipe_buffer.cnt) 91214037Sdyson pipeselwakeup(wpipe); 91313907Sdyson 91413675Sdyson return error; 91513675Sdyson} 91613675Sdyson 91713675Sdyson/* 91813675Sdyson * we implement a very minimal set of ioctls for compatibility with sockets. 91913675Sdyson */ 92013675Sdysonint 92113675Sdysonpipe_ioctl(fp, cmd, data, p) 92213675Sdyson struct file *fp; 92336735Sdfr u_long cmd; 92413675Sdyson register caddr_t data; 92513675Sdyson struct proc *p; 92613675Sdyson{ 92713675Sdyson register struct pipe *mpipe = (struct pipe *)fp->f_data; 92813675Sdyson 92913675Sdyson switch (cmd) { 93013675Sdyson 93113675Sdyson case FIONBIO: 93213675Sdyson return (0); 93313675Sdyson 93413675Sdyson case FIOASYNC: 93513675Sdyson if (*(int *)data) { 93613675Sdyson mpipe->pipe_state |= PIPE_ASYNC; 93713675Sdyson } else { 93813675Sdyson mpipe->pipe_state &= ~PIPE_ASYNC; 93913675Sdyson } 94013675Sdyson return (0); 94113675Sdyson 94213675Sdyson case FIONREAD: 94314037Sdyson if (mpipe->pipe_state & PIPE_DIRECTW) 94414037Sdyson *(int *)data = mpipe->pipe_map.cnt; 94514037Sdyson else 94614037Sdyson *(int *)data = mpipe->pipe_buffer.cnt; 94713675Sdyson return (0); 94813675Sdyson 94941086Struckman case FIOSETOWN: 95041086Struckman return (fsetown(*(int *)data, &mpipe->pipe_sigio)); 95141086Struckman 95241086Struckman case FIOGETOWN: 95341086Struckman *(int *)data = fgetown(mpipe->pipe_sigio); 95413675Sdyson return (0); 95513675Sdyson 95641086Struckman /* This is deprecated, FIOSETOWN should be used instead. */ 95741086Struckman case TIOCSPGRP: 95841086Struckman return (fsetown(-(*(int *)data), &mpipe->pipe_sigio)); 95941086Struckman 96041086Struckman /* This is deprecated, FIOGETOWN should be used instead. */ 96118863Sdyson case TIOCGPGRP: 96241086Struckman *(int *)data = -fgetown(mpipe->pipe_sigio); 96313675Sdyson return (0); 96413675Sdyson 96513675Sdyson } 96617124Sbde return (ENOTTY); 96713675Sdyson} 96813675Sdyson 96913675Sdysonint 97029356Speterpipe_poll(fp, events, cred, p) 97113675Sdyson struct file *fp; 97229356Speter int events; 97329356Speter struct ucred *cred; 97413675Sdyson struct proc *p; 97513675Sdyson{ 97613675Sdyson register struct pipe *rpipe = (struct pipe *)fp->f_data; 97713675Sdyson struct pipe *wpipe; 97829356Speter int revents = 0; 97913675Sdyson 98013675Sdyson wpipe = rpipe->pipe_peer; 98129356Speter if (events & (POLLIN | POLLRDNORM)) 98229356Speter if ((rpipe->pipe_state & PIPE_DIRECTW) || 98329356Speter (rpipe->pipe_buffer.cnt > 0) || 98429356Speter (rpipe->pipe_state & PIPE_EOF)) 98529356Speter revents |= events & (POLLIN | POLLRDNORM); 98613675Sdyson 98729356Speter if (events & (POLLOUT | POLLWRNORM)) 98829356Speter if (wpipe == NULL || (wpipe->pipe_state & PIPE_EOF) || 98943311Sdillon (((wpipe->pipe_state & PIPE_DIRECTW) == 0) && 99043311Sdillon (wpipe->pipe_buffer.size - wpipe->pipe_buffer.cnt) >= PIPE_BUF)) 99129356Speter revents |= events & (POLLOUT | POLLWRNORM); 99213675Sdyson 99329356Speter if ((rpipe->pipe_state & PIPE_EOF) || 99429356Speter (wpipe == NULL) || 99529356Speter (wpipe->pipe_state & PIPE_EOF)) 99629356Speter revents |= POLLHUP; 99729356Speter 99829356Speter if (revents == 0) { 99929356Speter if (events & (POLLIN | POLLRDNORM)) { 100029356Speter selrecord(p, &rpipe->pipe_sel); 100129356Speter rpipe->pipe_state |= PIPE_SEL; 100213675Sdyson } 100313675Sdyson 100429356Speter if (events & (POLLOUT | POLLWRNORM)) { 100530164Speter selrecord(p, &wpipe->pipe_sel); 100630164Speter wpipe->pipe_state |= PIPE_SEL; 100713907Sdyson } 100813675Sdyson } 100929356Speter 101029356Speter return (revents); 101113675Sdyson} 101213675Sdyson 101313675Sdysonint 101413675Sdysonpipe_stat(pipe, ub) 101513675Sdyson register struct pipe *pipe; 101613675Sdyson register struct stat *ub; 101713675Sdyson{ 101813675Sdyson bzero((caddr_t)ub, sizeof (*ub)); 101917124Sbde ub->st_mode = S_IFIFO; 102013907Sdyson ub->st_blksize = pipe->pipe_buffer.size; 102113675Sdyson ub->st_size = pipe->pipe_buffer.cnt; 102213675Sdyson ub->st_blocks = (ub->st_size + ub->st_blksize - 1) / ub->st_blksize; 102334901Sphk ub->st_atimespec = pipe->pipe_atime; 102434901Sphk ub->st_mtimespec = pipe->pipe_mtime; 102534901Sphk ub->st_ctimespec = pipe->pipe_ctime; 102617124Sbde /* 102717124Sbde * Left as 0: st_dev, st_ino, st_nlink, st_uid, st_gid, st_rdev, 102817124Sbde * st_flags, st_gen. 102917124Sbde * XXX (st_dev, st_ino) should be unique. 103017124Sbde */ 103113675Sdyson return 0; 103213675Sdyson} 103313675Sdyson 103413675Sdyson/* ARGSUSED */ 103513675Sdysonstatic int 103613675Sdysonpipe_close(fp, p) 103713675Sdyson struct file *fp; 103813675Sdyson struct proc *p; 103913675Sdyson{ 104013675Sdyson struct pipe *cpipe = (struct pipe *)fp->f_data; 104116322Sgpalmer 104241086Struckman funsetown(cpipe->pipe_sigio); 104313675Sdyson pipeclose(cpipe); 104413675Sdyson fp->f_data = NULL; 104513675Sdyson return 0; 104613675Sdyson} 104713675Sdyson 104813675Sdyson/* 104913675Sdyson * shutdown the pipe 105013675Sdyson */ 105113675Sdysonstatic void 105213675Sdysonpipeclose(cpipe) 105313675Sdyson struct pipe *cpipe; 105413675Sdyson{ 105513907Sdyson struct pipe *ppipe; 105613675Sdyson if (cpipe) { 105713907Sdyson 105814037Sdyson pipeselwakeup(cpipe); 105913907Sdyson 106013675Sdyson /* 106113675Sdyson * If the other side is blocked, wake it up saying that 106213675Sdyson * we want to close it down. 106313675Sdyson */ 106413675Sdyson while (cpipe->pipe_busy) { 106513675Sdyson wakeup(cpipe); 106613675Sdyson cpipe->pipe_state |= PIPE_WANT|PIPE_EOF; 106713675Sdyson tsleep(cpipe, PRIBIO, "pipecl", 0); 106813675Sdyson } 106913675Sdyson 107013675Sdyson /* 107113675Sdyson * Disconnect from peer 107213675Sdyson */ 107343301Sdillon if ((ppipe = cpipe->pipe_peer) != NULL) { 107414037Sdyson pipeselwakeup(ppipe); 107513907Sdyson 107613907Sdyson ppipe->pipe_state |= PIPE_EOF; 107713907Sdyson wakeup(ppipe); 107813907Sdyson ppipe->pipe_peer = NULL; 107913675Sdyson } 108013675Sdyson 108113675Sdyson /* 108213675Sdyson * free resources 108313675Sdyson */ 108413907Sdyson if (cpipe->pipe_buffer.buffer) { 108517163Sdyson if (cpipe->pipe_buffer.size > PIPE_SIZE) 108617163Sdyson --nbigpipe; 108713907Sdyson amountpipekva -= cpipe->pipe_buffer.size; 108813907Sdyson kmem_free(kernel_map, 108913907Sdyson (vm_offset_t)cpipe->pipe_buffer.buffer, 109013907Sdyson cpipe->pipe_buffer.size); 109113907Sdyson } 109214037Sdyson#ifndef PIPE_NODIRECT 109313907Sdyson if (cpipe->pipe_map.kva) { 109413912Sdyson amountpipekva -= cpipe->pipe_buffer.size + PAGE_SIZE; 109513907Sdyson kmem_free(kernel_map, 109613907Sdyson cpipe->pipe_map.kva, 109713912Sdyson cpipe->pipe_buffer.size + PAGE_SIZE); 109813907Sdyson } 109914037Sdyson#endif 110027899Sdyson zfree(pipe_zone, cpipe); 110113675Sdyson } 110213675Sdyson} 1103