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