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