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