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