sys_pipe.c revision 1.79 1 /* $NetBSD: sys_pipe.c,v 1.79 2007/03/04 06:03:09 christos Exp $ */
2
3 /*-
4 * Copyright (c) 2003 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Paul Kranenburg.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * Copyright (c) 1996 John S. Dyson
41 * All rights reserved.
42 *
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
45 * are met:
46 * 1. Redistributions of source code must retain the above copyright
47 * notice immediately at the beginning of the file, without modification,
48 * this list of conditions, and the following disclaimer.
49 * 2. Redistributions in binary form must reproduce the above copyright
50 * notice, this list of conditions and the following disclaimer in the
51 * documentation and/or other materials provided with the distribution.
52 * 3. Absolutely no warranty of function or purpose is made by the author
53 * John S. Dyson.
54 * 4. Modifications may be freely made to this file if the above conditions
55 * are met.
56 *
57 * $FreeBSD: src/sys/kern/sys_pipe.c,v 1.95 2002/03/09 22:06:31 alfred Exp $
58 */
59
60 /*
61 * This file contains a high-performance replacement for the socket-based
62 * pipes scheme originally used in FreeBSD/4.4Lite. It does not support
63 * all features of sockets, but does do everything that pipes normally
64 * do.
65 *
66 * Adaption for NetBSD UVM, including uvm_loan() based direct write, was
67 * written by Jaromir Dolecek.
68 */
69
70 /*
71 * This code has two modes of operation, a small write mode and a large
72 * write mode. The small write mode acts like conventional pipes with
73 * a kernel buffer. If the buffer is less than PIPE_MINDIRECT, then the
74 * "normal" pipe buffering is done. If the buffer is between PIPE_MINDIRECT
75 * and PIPE_SIZE in size it is mapped read-only into the kernel address space
76 * using the UVM page loan facility from where the receiving process can copy
77 * the data directly from the pages in the sending process.
78 *
79 * The constant PIPE_MINDIRECT is chosen to make sure that buffering will
80 * happen for small transfers so that the system will not spend all of
81 * its time context switching. PIPE_SIZE is constrained by the
82 * amount of kernel virtual memory.
83 */
84
85 #include <sys/cdefs.h>
86 __KERNEL_RCSID(0, "$NetBSD: sys_pipe.c,v 1.79 2007/03/04 06:03:09 christos 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 simple_lock_init(&pipe->pipe_slock);
309
310 if (allockva && (error = pipespace(pipe, PIPE_SIZE)))
311 return (error);
312
313 return (0);
314 }
315
316
317 /*
318 * Lock a pipe for I/O, blocking other access
319 * Called with pipe spin lock held.
320 * Return with pipe spin lock released on success.
321 */
322 static int
323 pipelock(struct pipe *pipe, int catch)
324 {
325
326 LOCK_ASSERT(simple_lock_held(&pipe->pipe_slock));
327
328 while (pipe->pipe_state & PIPE_LOCKFL) {
329 int error;
330 const int pcatch = catch ? PCATCH : 0;
331
332 pipe->pipe_state |= PIPE_LWANT;
333 error = ltsleep(pipe, PSOCK | pcatch, "pipelk", 0,
334 &pipe->pipe_slock);
335 if (error != 0)
336 return error;
337 }
338
339 pipe->pipe_state |= PIPE_LOCKFL;
340 simple_unlock(&pipe->pipe_slock);
341
342 return 0;
343 }
344
345 /*
346 * unlock a pipe I/O lock
347 */
348 static inline void
349 pipeunlock(struct pipe *pipe)
350 {
351
352 KASSERT(pipe->pipe_state & PIPE_LOCKFL);
353
354 pipe->pipe_state &= ~PIPE_LOCKFL;
355 if (pipe->pipe_state & PIPE_LWANT) {
356 pipe->pipe_state &= ~PIPE_LWANT;
357 wakeup(pipe);
358 }
359 }
360
361 /*
362 * Select/poll wakup. This also sends SIGIO to peer connected to
363 * 'sigpipe' side of pipe.
364 */
365 static void
366 pipeselwakeup(struct pipe *selp, struct pipe *sigp, int code)
367 {
368 int band;
369
370 selnotify(&selp->pipe_sel, NOTE_SUBMIT);
371
372 if (sigp == NULL || (sigp->pipe_state & PIPE_ASYNC) == 0)
373 return;
374
375 switch (code) {
376 case POLL_IN:
377 band = POLLIN|POLLRDNORM;
378 break;
379 case POLL_OUT:
380 band = POLLOUT|POLLWRNORM;
381 break;
382 case POLL_HUP:
383 band = POLLHUP;
384 break;
385 #if POLL_HUP != POLL_ERR
386 case POLL_ERR:
387 band = POLLERR;
388 break;
389 #endif
390 default:
391 band = 0;
392 #ifdef DIAGNOSTIC
393 printf("bad siginfo code %d in pipe notification.\n", code);
394 #endif
395 break;
396 }
397
398 fownsignal(sigp->pipe_pgid, SIGIO, code, band, selp);
399 }
400
401 /* ARGSUSED */
402 static int
403 pipe_read(struct file *fp, off_t *offset, struct uio *uio, kauth_cred_t cred,
404 int flags)
405 {
406 struct pipe *rpipe = (struct pipe *) fp->f_data;
407 struct pipebuf *bp = &rpipe->pipe_buffer;
408 int error;
409 size_t nread = 0;
410 size_t size;
411 size_t ocnt;
412
413 PIPE_LOCK(rpipe);
414 ++rpipe->pipe_busy;
415 ocnt = bp->cnt;
416
417 again:
418 error = pipelock(rpipe, 1);
419 if (error)
420 goto unlocked_error;
421
422 while (uio->uio_resid) {
423 /*
424 * normal pipe buffer receive
425 */
426 if (bp->cnt > 0) {
427 size = bp->size - bp->out;
428 if (size > bp->cnt)
429 size = bp->cnt;
430 if (size > uio->uio_resid)
431 size = uio->uio_resid;
432
433 error = uiomove((char *)bp->buffer + bp->out, size, uio);
434 if (error)
435 break;
436
437 bp->out += size;
438 if (bp->out >= bp->size)
439 bp->out = 0;
440
441 bp->cnt -= size;
442
443 /*
444 * If there is no more to read in the pipe, reset
445 * its pointers to the beginning. This improves
446 * cache hit stats.
447 */
448 if (bp->cnt == 0) {
449 bp->in = 0;
450 bp->out = 0;
451 }
452 nread += size;
453 #ifndef PIPE_NODIRECT
454 } else if ((rpipe->pipe_state & PIPE_DIRECTR) != 0) {
455 /*
456 * Direct copy, bypassing a kernel buffer.
457 */
458 void * va;
459
460 KASSERT(rpipe->pipe_state & PIPE_DIRECTW);
461
462 size = rpipe->pipe_map.cnt;
463 if (size > uio->uio_resid)
464 size = uio->uio_resid;
465
466 va = (char *)rpipe->pipe_map.kva + rpipe->pipe_map.pos;
467 error = uiomove(va, size, uio);
468 if (error)
469 break;
470 nread += size;
471 rpipe->pipe_map.pos += size;
472 rpipe->pipe_map.cnt -= size;
473 if (rpipe->pipe_map.cnt == 0) {
474 PIPE_LOCK(rpipe);
475 rpipe->pipe_state &= ~PIPE_DIRECTR;
476 wakeup(rpipe);
477 PIPE_UNLOCK(rpipe);
478 }
479 #endif
480 } else {
481 /*
482 * Break if some data was read.
483 */
484 if (nread > 0)
485 break;
486
487 PIPE_LOCK(rpipe);
488
489 /*
490 * detect EOF condition
491 * read returns 0 on EOF, no need to set error
492 */
493 if (rpipe->pipe_state & PIPE_EOF) {
494 PIPE_UNLOCK(rpipe);
495 break;
496 }
497
498 /*
499 * don't block on non-blocking I/O
500 */
501 if (fp->f_flag & FNONBLOCK) {
502 PIPE_UNLOCK(rpipe);
503 error = EAGAIN;
504 break;
505 }
506
507 /*
508 * Unlock the pipe buffer for our remaining processing.
509 * We will either break out with an error or we will
510 * sleep and relock to loop.
511 */
512 pipeunlock(rpipe);
513
514 /*
515 * The PIPE_DIRECTR flag is not under the control
516 * of the long-term lock (see pipe_direct_write()),
517 * so re-check now while holding the spin lock.
518 */
519 if ((rpipe->pipe_state & PIPE_DIRECTR) != 0)
520 goto again;
521
522 /*
523 * We want to read more, wake up select/poll.
524 */
525 pipeselwakeup(rpipe, rpipe->pipe_peer, POLL_IN);
526
527 /*
528 * If the "write-side" is blocked, wake it up now.
529 */
530 if (rpipe->pipe_state & PIPE_WANTW) {
531 rpipe->pipe_state &= ~PIPE_WANTW;
532 wakeup(rpipe);
533 }
534
535 /* Now wait until the pipe is filled */
536 rpipe->pipe_state |= PIPE_WANTR;
537 error = ltsleep(rpipe, PSOCK | PCATCH,
538 "piperd", 0, &rpipe->pipe_slock);
539 if (error != 0)
540 goto unlocked_error;
541 goto again;
542 }
543 }
544
545 if (error == 0)
546 getmicrotime(&rpipe->pipe_atime);
547
548 PIPE_LOCK(rpipe);
549 pipeunlock(rpipe);
550
551 unlocked_error:
552 --rpipe->pipe_busy;
553
554 /*
555 * PIPE_WANTCLOSE processing only makes sense if pipe_busy is 0.
556 */
557 if ((rpipe->pipe_busy == 0) && (rpipe->pipe_state & PIPE_WANTCLOSE)) {
558 rpipe->pipe_state &= ~(PIPE_WANTCLOSE|PIPE_WANTW);
559 wakeup(rpipe);
560 } else if (bp->cnt < MINPIPESIZE) {
561 /*
562 * Handle write blocking hysteresis.
563 */
564 if (rpipe->pipe_state & PIPE_WANTW) {
565 rpipe->pipe_state &= ~PIPE_WANTW;
566 wakeup(rpipe);
567 }
568 }
569
570 /*
571 * If anything was read off the buffer, signal to the writer it's
572 * possible to write more data. Also send signal if we are here for the
573 * first time after last write.
574 */
575 if ((bp->size - bp->cnt) >= PIPE_BUF
576 && (ocnt != bp->cnt || (rpipe->pipe_state & PIPE_SIGNALR))) {
577 pipeselwakeup(rpipe, rpipe->pipe_peer, POLL_OUT);
578 rpipe->pipe_state &= ~PIPE_SIGNALR;
579 }
580
581 PIPE_UNLOCK(rpipe);
582 return (error);
583 }
584
585 #ifndef PIPE_NODIRECT
586 /*
587 * Allocate structure for loan transfer.
588 */
589 static int
590 pipe_loan_alloc(struct pipe *wpipe, int npages)
591 {
592 vsize_t len;
593
594 len = (vsize_t)npages << PAGE_SHIFT;
595 wpipe->pipe_map.kva = uvm_km_alloc(kernel_map, len, 0,
596 UVM_KMF_VAONLY | UVM_KMF_WAITVA);
597 if (wpipe->pipe_map.kva == 0)
598 return (ENOMEM);
599
600 amountpipekva += len;
601 wpipe->pipe_map.npages = npages;
602 wpipe->pipe_map.pgs = malloc(npages * sizeof(struct vm_page *), M_PIPE,
603 M_WAITOK);
604 return (0);
605 }
606
607 /*
608 * Free resources allocated for loan transfer.
609 */
610 static void
611 pipe_loan_free(struct pipe *wpipe)
612 {
613 vsize_t len;
614
615 len = (vsize_t)wpipe->pipe_map.npages << PAGE_SHIFT;
616 uvm_km_free(kernel_map, wpipe->pipe_map.kva, len, UVM_KMF_VAONLY);
617 wpipe->pipe_map.kva = 0;
618 amountpipekva -= len;
619 free(wpipe->pipe_map.pgs, M_PIPE);
620 wpipe->pipe_map.pgs = NULL;
621 }
622
623 /*
624 * NetBSD direct write, using uvm_loan() mechanism.
625 * This implements the pipe buffer write mechanism. Note that only
626 * a direct write OR a normal pipe write can be pending at any given time.
627 * If there are any characters in the pipe buffer, the direct write will
628 * be deferred until the receiving process grabs all of the bytes from
629 * the pipe buffer. Then the direct mapping write is set-up.
630 *
631 * Called with the long-term pipe lock held.
632 */
633 static int
634 pipe_direct_write(struct file *fp, struct pipe *wpipe, struct uio *uio)
635 {
636 int error, npages, j;
637 struct vm_page **pgs;
638 vaddr_t bbase, kva, base, bend;
639 vsize_t blen, bcnt;
640 voff_t bpos;
641
642 KASSERT(wpipe->pipe_map.cnt == 0);
643
644 /*
645 * Handle first PIPE_CHUNK_SIZE bytes of buffer. Deal with buffers
646 * not aligned to PAGE_SIZE.
647 */
648 bbase = (vaddr_t)uio->uio_iov->iov_base;
649 base = trunc_page(bbase);
650 bend = round_page(bbase + uio->uio_iov->iov_len);
651 blen = bend - base;
652 bpos = bbase - base;
653
654 if (blen > PIPE_DIRECT_CHUNK) {
655 blen = PIPE_DIRECT_CHUNK;
656 bend = base + blen;
657 bcnt = PIPE_DIRECT_CHUNK - bpos;
658 } else {
659 bcnt = uio->uio_iov->iov_len;
660 }
661 npages = blen >> PAGE_SHIFT;
662
663 /*
664 * Free the old kva if we need more pages than we have
665 * allocated.
666 */
667 if (wpipe->pipe_map.kva != 0 && npages > wpipe->pipe_map.npages)
668 pipe_loan_free(wpipe);
669
670 /* Allocate new kva. */
671 if (wpipe->pipe_map.kva == 0) {
672 error = pipe_loan_alloc(wpipe, npages);
673 if (error)
674 return (error);
675 }
676
677 /* Loan the write buffer memory from writer process */
678 pgs = wpipe->pipe_map.pgs;
679 error = uvm_loan(&uio->uio_vmspace->vm_map, base, blen,
680 pgs, UVM_LOAN_TOPAGE);
681 if (error) {
682 pipe_loan_free(wpipe);
683 return (ENOMEM); /* so that caller fallback to ordinary write */
684 }
685
686 /* Enter the loaned pages to kva */
687 kva = wpipe->pipe_map.kva;
688 for (j = 0; j < npages; j++, kva += PAGE_SIZE) {
689 pmap_kenter_pa(kva, VM_PAGE_TO_PHYS(pgs[j]), VM_PROT_READ);
690 }
691 pmap_update(pmap_kernel());
692
693 /* Now we can put the pipe in direct write mode */
694 wpipe->pipe_map.pos = bpos;
695 wpipe->pipe_map.cnt = bcnt;
696 wpipe->pipe_state |= PIPE_DIRECTW;
697
698 /*
699 * But before we can let someone do a direct read,
700 * we have to wait until the pipe is drained.
701 */
702
703 /* Relase the pipe lock while we wait */
704 PIPE_LOCK(wpipe);
705 pipeunlock(wpipe);
706
707 while (error == 0 && wpipe->pipe_buffer.cnt > 0) {
708 if (wpipe->pipe_state & PIPE_WANTR) {
709 wpipe->pipe_state &= ~PIPE_WANTR;
710 wakeup(wpipe);
711 }
712
713 wpipe->pipe_state |= PIPE_WANTW;
714 error = ltsleep(wpipe, PSOCK | PCATCH, "pipdwc", 0,
715 &wpipe->pipe_slock);
716 if (error == 0 && wpipe->pipe_state & PIPE_EOF)
717 error = EPIPE;
718 }
719
720 /* Pipe is drained; next read will off the direct buffer */
721 wpipe->pipe_state |= PIPE_DIRECTR;
722
723 /* Wait until the reader is done */
724 while (error == 0 && (wpipe->pipe_state & PIPE_DIRECTR)) {
725 if (wpipe->pipe_state & PIPE_WANTR) {
726 wpipe->pipe_state &= ~PIPE_WANTR;
727 wakeup(wpipe);
728 }
729 pipeselwakeup(wpipe, wpipe, POLL_IN);
730 error = ltsleep(wpipe, PSOCK | PCATCH, "pipdwt", 0,
731 &wpipe->pipe_slock);
732 if (error == 0 && wpipe->pipe_state & PIPE_EOF)
733 error = EPIPE;
734 }
735
736 /* Take pipe out of direct write mode */
737 wpipe->pipe_state &= ~(PIPE_DIRECTW | PIPE_DIRECTR);
738
739 /* Acquire the pipe lock and cleanup */
740 (void)pipelock(wpipe, 0);
741 if (pgs != NULL) {
742 pmap_kremove(wpipe->pipe_map.kva, blen);
743 uvm_unloan(pgs, npages, UVM_LOAN_TOPAGE);
744 }
745 if (error || amountpipekva > maxpipekva)
746 pipe_loan_free(wpipe);
747
748 if (error) {
749 pipeselwakeup(wpipe, wpipe, POLL_ERR);
750
751 /*
752 * If nothing was read from what we offered, return error
753 * straight on. Otherwise update uio resid first. Caller
754 * will deal with the error condition, returning short
755 * write, error, or restarting the write(2) as appropriate.
756 */
757 if (wpipe->pipe_map.cnt == bcnt) {
758 wpipe->pipe_map.cnt = 0;
759 wakeup(wpipe);
760 return (error);
761 }
762
763 bcnt -= wpipe->pipe_map.cnt;
764 }
765
766 uio->uio_resid -= bcnt;
767 /* uio_offset not updated, not set/used for write(2) */
768 uio->uio_iov->iov_base = (char *)uio->uio_iov->iov_base + bcnt;
769 uio->uio_iov->iov_len -= bcnt;
770 if (uio->uio_iov->iov_len == 0) {
771 uio->uio_iov++;
772 uio->uio_iovcnt--;
773 }
774
775 wpipe->pipe_map.cnt = 0;
776 return (error);
777 }
778 #endif /* !PIPE_NODIRECT */
779
780 static int
781 pipe_write(struct file *fp, off_t *offset, struct uio *uio, kauth_cred_t cred,
782 int flags)
783 {
784 struct pipe *wpipe, *rpipe;
785 struct pipebuf *bp;
786 int error;
787
788 /* We want to write to our peer */
789 rpipe = (struct pipe *) fp->f_data;
790
791 retry:
792 error = 0;
793 PIPE_LOCK(rpipe);
794 wpipe = rpipe->pipe_peer;
795
796 /*
797 * Detect loss of pipe read side, issue SIGPIPE if lost.
798 */
799 if (wpipe == NULL)
800 error = EPIPE;
801 else if (simple_lock_try(&wpipe->pipe_slock) == 0) {
802 /* Deal with race for peer */
803 PIPE_UNLOCK(rpipe);
804 goto retry;
805 } else if ((wpipe->pipe_state & PIPE_EOF) != 0) {
806 PIPE_UNLOCK(wpipe);
807 error = EPIPE;
808 }
809
810 PIPE_UNLOCK(rpipe);
811 if (error != 0)
812 return (error);
813
814 ++wpipe->pipe_busy;
815
816 /* Aquire the long-term pipe lock */
817 if ((error = pipelock(wpipe,1)) != 0) {
818 --wpipe->pipe_busy;
819 if (wpipe->pipe_busy == 0
820 && (wpipe->pipe_state & PIPE_WANTCLOSE)) {
821 wpipe->pipe_state &= ~(PIPE_WANTCLOSE | PIPE_WANTR);
822 wakeup(wpipe);
823 }
824 PIPE_UNLOCK(wpipe);
825 return (error);
826 }
827
828 bp = &wpipe->pipe_buffer;
829
830 /*
831 * If it is advantageous to resize the pipe buffer, do so.
832 */
833 if ((uio->uio_resid > PIPE_SIZE) &&
834 (nbigpipe < maxbigpipes) &&
835 #ifndef PIPE_NODIRECT
836 (wpipe->pipe_state & PIPE_DIRECTW) == 0 &&
837 #endif
838 (bp->size <= PIPE_SIZE) && (bp->cnt == 0)) {
839
840 if (pipespace(wpipe, BIG_PIPE_SIZE) == 0)
841 nbigpipe++;
842 }
843
844 while (uio->uio_resid) {
845 size_t space;
846
847 #ifndef PIPE_NODIRECT
848 /*
849 * Pipe buffered writes cannot be coincidental with
850 * direct writes. Also, only one direct write can be
851 * in progress at any one time. We wait until the currently
852 * executing direct write is completed before continuing.
853 *
854 * We break out if a signal occurs or the reader goes away.
855 */
856 while (error == 0 && wpipe->pipe_state & PIPE_DIRECTW) {
857 PIPE_LOCK(wpipe);
858 if (wpipe->pipe_state & PIPE_WANTR) {
859 wpipe->pipe_state &= ~PIPE_WANTR;
860 wakeup(wpipe);
861 }
862 pipeunlock(wpipe);
863 error = ltsleep(wpipe, PSOCK | PCATCH,
864 "pipbww", 0, &wpipe->pipe_slock);
865
866 (void)pipelock(wpipe, 0);
867 if (wpipe->pipe_state & PIPE_EOF)
868 error = EPIPE;
869 }
870 if (error)
871 break;
872
873 /*
874 * If the transfer is large, we can gain performance if
875 * we do process-to-process copies directly.
876 * If the write is non-blocking, we don't use the
877 * direct write mechanism.
878 *
879 * The direct write mechanism will detect the reader going
880 * away on us.
881 */
882 if ((uio->uio_iov->iov_len >= PIPE_MINDIRECT) &&
883 (fp->f_flag & FNONBLOCK) == 0 &&
884 (wpipe->pipe_map.kva || (amountpipekva < limitpipekva))) {
885 error = pipe_direct_write(fp, wpipe, uio);
886
887 /*
888 * Break out if error occurred, unless it's ENOMEM.
889 * ENOMEM means we failed to allocate some resources
890 * for direct write, so we just fallback to ordinary
891 * write. If the direct write was successful,
892 * process rest of data via ordinary write.
893 */
894 if (error == 0)
895 continue;
896
897 if (error != ENOMEM)
898 break;
899 }
900 #endif /* PIPE_NODIRECT */
901
902 space = bp->size - bp->cnt;
903
904 /* Writes of size <= PIPE_BUF must be atomic. */
905 if ((space < uio->uio_resid) && (uio->uio_resid <= PIPE_BUF))
906 space = 0;
907
908 if (space > 0) {
909 int size; /* Transfer size */
910 int segsize; /* first segment to transfer */
911
912 /*
913 * Transfer size is minimum of uio transfer
914 * and free space in pipe buffer.
915 */
916 if (space > uio->uio_resid)
917 size = uio->uio_resid;
918 else
919 size = space;
920 /*
921 * First segment to transfer is minimum of
922 * transfer size and contiguous space in
923 * pipe buffer. If first segment to transfer
924 * is less than the transfer size, we've got
925 * a wraparound in the buffer.
926 */
927 segsize = bp->size - bp->in;
928 if (segsize > size)
929 segsize = size;
930
931 /* Transfer first segment */
932 error = uiomove((char *)bp->buffer + bp->in, segsize,
933 uio);
934
935 if (error == 0 && segsize < size) {
936 /*
937 * Transfer remaining part now, to
938 * support atomic writes. Wraparound
939 * happened.
940 */
941 #ifdef DEBUG
942 if (bp->in + segsize != bp->size)
943 panic("Expected pipe buffer wraparound disappeared");
944 #endif
945
946 error = uiomove(bp->buffer,
947 size - segsize, uio);
948 }
949 if (error)
950 break;
951
952 bp->in += size;
953 if (bp->in >= bp->size) {
954 #ifdef DEBUG
955 if (bp->in != size - segsize + bp->size)
956 panic("Expected wraparound bad");
957 #endif
958 bp->in = size - segsize;
959 }
960
961 bp->cnt += size;
962 #ifdef DEBUG
963 if (bp->cnt > bp->size)
964 panic("Pipe buffer overflow");
965 #endif
966 } else {
967 /*
968 * If the "read-side" has been blocked, wake it up now.
969 */
970 PIPE_LOCK(wpipe);
971 if (wpipe->pipe_state & PIPE_WANTR) {
972 wpipe->pipe_state &= ~PIPE_WANTR;
973 wakeup(wpipe);
974 }
975 PIPE_UNLOCK(wpipe);
976
977 /*
978 * don't block on non-blocking I/O
979 */
980 if (fp->f_flag & FNONBLOCK) {
981 error = EAGAIN;
982 break;
983 }
984
985 /*
986 * We have no more space and have something to offer,
987 * wake up select/poll.
988 */
989 if (bp->cnt)
990 pipeselwakeup(wpipe, wpipe, POLL_OUT);
991
992 PIPE_LOCK(wpipe);
993 pipeunlock(wpipe);
994 wpipe->pipe_state |= PIPE_WANTW;
995 error = ltsleep(wpipe, PSOCK | PCATCH, "pipewr", 0,
996 &wpipe->pipe_slock);
997 (void)pipelock(wpipe, 0);
998 if (error != 0)
999 break;
1000 /*
1001 * If read side wants to go away, we just issue a signal
1002 * to ourselves.
1003 */
1004 if (wpipe->pipe_state & PIPE_EOF) {
1005 error = EPIPE;
1006 break;
1007 }
1008 }
1009 }
1010
1011 PIPE_LOCK(wpipe);
1012 --wpipe->pipe_busy;
1013 if ((wpipe->pipe_busy == 0) && (wpipe->pipe_state & PIPE_WANTCLOSE)) {
1014 wpipe->pipe_state &= ~(PIPE_WANTCLOSE | PIPE_WANTR);
1015 wakeup(wpipe);
1016 } else if (bp->cnt > 0) {
1017 /*
1018 * If we have put any characters in the buffer, we wake up
1019 * the reader.
1020 */
1021 if (wpipe->pipe_state & PIPE_WANTR) {
1022 wpipe->pipe_state &= ~PIPE_WANTR;
1023 wakeup(wpipe);
1024 }
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 PIPE_UNLOCK(wpipe);
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 PIPE_LOCK(pipe);
1070 if (*(int *)data) {
1071 pipe->pipe_state |= PIPE_ASYNC;
1072 } else {
1073 pipe->pipe_state &= ~PIPE_ASYNC;
1074 }
1075 PIPE_UNLOCK(pipe);
1076 return (0);
1077
1078 case FIONREAD:
1079 PIPE_LOCK(pipe);
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 PIPE_UNLOCK(pipe);
1087 return (0);
1088
1089 case FIONWRITE:
1090 /* Look at other side */
1091 pipe = pipe->pipe_peer;
1092 PIPE_LOCK(pipe);
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 PIPE_UNLOCK(pipe);
1100 return (0);
1101
1102 case FIONSPACE:
1103 /* Look at other side */
1104 pipe = pipe->pipe_peer;
1105 PIPE_LOCK(pipe);
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 PIPE_UNLOCK(pipe);
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 retry:
1142 PIPE_LOCK(rpipe);
1143 wpipe = rpipe->pipe_peer;
1144 if (wpipe != NULL && simple_lock_try(&wpipe->pipe_slock) == 0) {
1145 /* Deal with race for peer */
1146 PIPE_UNLOCK(rpipe);
1147 goto retry;
1148 }
1149
1150 if (events & (POLLIN | POLLRDNORM))
1151 if ((rpipe->pipe_buffer.cnt > 0) ||
1152 #ifndef PIPE_NODIRECT
1153 (rpipe->pipe_state & PIPE_DIRECTR) ||
1154 #endif
1155 (rpipe->pipe_state & PIPE_EOF))
1156 revents |= events & (POLLIN | POLLRDNORM);
1157
1158 eof |= (rpipe->pipe_state & PIPE_EOF);
1159 PIPE_UNLOCK(rpipe);
1160
1161 if (wpipe == NULL)
1162 revents |= events & (POLLOUT | POLLWRNORM);
1163 else {
1164 if (events & (POLLOUT | POLLWRNORM))
1165 if ((wpipe->pipe_state & PIPE_EOF) || (
1166 #ifndef PIPE_NODIRECT
1167 (wpipe->pipe_state & PIPE_DIRECTW) == 0 &&
1168 #endif
1169 (wpipe->pipe_buffer.size - wpipe->pipe_buffer.cnt) >= PIPE_BUF))
1170 revents |= events & (POLLOUT | POLLWRNORM);
1171
1172 eof |= (wpipe->pipe_state & PIPE_EOF);
1173 PIPE_UNLOCK(wpipe);
1174 }
1175
1176 if (wpipe == NULL || eof)
1177 revents |= POLLHUP;
1178
1179 if (revents == 0) {
1180 if (events & (POLLIN | POLLRDNORM))
1181 selrecord(l, &rpipe->pipe_sel);
1182
1183 if (events & (POLLOUT | POLLWRNORM))
1184 selrecord(l, &wpipe->pipe_sel);
1185 }
1186
1187 return (revents);
1188 }
1189
1190 static int
1191 pipe_stat(struct file *fp, struct stat *ub, struct lwp *l)
1192 {
1193 struct pipe *pipe = (struct pipe *)fp->f_data;
1194
1195 memset((void *)ub, 0, sizeof(*ub));
1196 ub->st_mode = S_IFIFO | S_IRUSR | S_IWUSR;
1197 ub->st_blksize = pipe->pipe_buffer.size;
1198 if (ub->st_blksize == 0 && pipe->pipe_peer)
1199 ub->st_blksize = pipe->pipe_peer->pipe_buffer.size;
1200 ub->st_size = pipe->pipe_buffer.cnt;
1201 ub->st_blocks = (ub->st_size) ? 1 : 0;
1202 TIMEVAL_TO_TIMESPEC(&pipe->pipe_atime, &ub->st_atimespec);
1203 TIMEVAL_TO_TIMESPEC(&pipe->pipe_mtime, &ub->st_mtimespec);
1204 TIMEVAL_TO_TIMESPEC(&pipe->pipe_ctime, &ub->st_ctimespec);
1205 ub->st_uid = kauth_cred_geteuid(fp->f_cred);
1206 ub->st_gid = kauth_cred_getegid(fp->f_cred);
1207 /*
1208 * Left as 0: st_dev, st_ino, st_nlink, st_rdev, st_flags, st_gen.
1209 * XXX (st_dev, st_ino) should be unique.
1210 */
1211 return (0);
1212 }
1213
1214 /* ARGSUSED */
1215 static int
1216 pipe_close(struct file *fp, struct lwp *l)
1217 {
1218 struct pipe *pipe = (struct pipe *)fp->f_data;
1219
1220 fp->f_data = NULL;
1221 pipeclose(fp, pipe);
1222 return (0);
1223 }
1224
1225 static void
1226 pipe_free_kmem(struct pipe *pipe)
1227 {
1228
1229 if (pipe->pipe_buffer.buffer != NULL) {
1230 if (pipe->pipe_buffer.size > PIPE_SIZE)
1231 --nbigpipe;
1232 amountpipekva -= pipe->pipe_buffer.size;
1233 uvm_km_free(kernel_map,
1234 (vaddr_t)pipe->pipe_buffer.buffer,
1235 pipe->pipe_buffer.size, UVM_KMF_PAGEABLE);
1236 pipe->pipe_buffer.buffer = NULL;
1237 }
1238 #ifndef PIPE_NODIRECT
1239 if (pipe->pipe_map.kva != 0) {
1240 pipe_loan_free(pipe);
1241 pipe->pipe_map.cnt = 0;
1242 pipe->pipe_map.kva = 0;
1243 pipe->pipe_map.pos = 0;
1244 pipe->pipe_map.npages = 0;
1245 }
1246 #endif /* !PIPE_NODIRECT */
1247 }
1248
1249 /*
1250 * shutdown the pipe
1251 */
1252 static void
1253 pipeclose(struct file *fp, struct pipe *pipe)
1254 {
1255 struct pipe *ppipe;
1256
1257 if (pipe == NULL)
1258 return;
1259
1260 retry:
1261 PIPE_LOCK(pipe);
1262
1263 pipeselwakeup(pipe, pipe, POLL_HUP);
1264
1265 /*
1266 * If the other side is blocked, wake it up saying that
1267 * we want to close it down.
1268 */
1269 pipe->pipe_state |= PIPE_EOF;
1270 while (pipe->pipe_busy) {
1271 wakeup(pipe);
1272 pipe->pipe_state |= PIPE_WANTCLOSE;
1273 ltsleep(pipe, PSOCK, "pipecl", 0, &pipe->pipe_slock);
1274 }
1275
1276 /*
1277 * Disconnect from peer
1278 */
1279 if ((ppipe = pipe->pipe_peer) != NULL) {
1280 /* Deal with race for peer */
1281 if (simple_lock_try(&ppipe->pipe_slock) == 0) {
1282 PIPE_UNLOCK(pipe);
1283 goto retry;
1284 }
1285 pipeselwakeup(ppipe, ppipe, POLL_HUP);
1286
1287 ppipe->pipe_state |= PIPE_EOF;
1288 wakeup(ppipe);
1289 ppipe->pipe_peer = NULL;
1290 PIPE_UNLOCK(ppipe);
1291 }
1292
1293 KASSERT((pipe->pipe_state & PIPE_LOCKFL) == 0);
1294
1295 PIPE_UNLOCK(pipe);
1296
1297 /*
1298 * free resources
1299 */
1300 pipe_free_kmem(pipe);
1301 pool_put(&pipe_pool, pipe);
1302 }
1303
1304 static void
1305 filt_pipedetach(struct knote *kn)
1306 {
1307 struct pipe *pipe = (struct pipe *)kn->kn_fp->f_data;
1308
1309 switch(kn->kn_filter) {
1310 case EVFILT_WRITE:
1311 /* need the peer structure, not our own */
1312 pipe = pipe->pipe_peer;
1313 /* XXXSMP: race for peer */
1314
1315 /* if reader end already closed, just return */
1316 if (pipe == NULL)
1317 return;
1318
1319 break;
1320 default:
1321 /* nothing to do */
1322 break;
1323 }
1324
1325 #ifdef DIAGNOSTIC
1326 if (kn->kn_hook != pipe)
1327 panic("filt_pipedetach: inconsistent knote");
1328 #endif
1329
1330 PIPE_LOCK(pipe);
1331 SLIST_REMOVE(&pipe->pipe_sel.sel_klist, kn, knote, kn_selnext);
1332 PIPE_UNLOCK(pipe);
1333 }
1334
1335 /*ARGSUSED*/
1336 static int
1337 filt_piperead(struct knote *kn, long hint)
1338 {
1339 struct pipe *rpipe = (struct pipe *)kn->kn_fp->f_data;
1340 struct pipe *wpipe = rpipe->pipe_peer;
1341
1342 if ((hint & NOTE_SUBMIT) == 0)
1343 PIPE_LOCK(rpipe);
1344 kn->kn_data = rpipe->pipe_buffer.cnt;
1345 if ((kn->kn_data == 0) && (rpipe->pipe_state & PIPE_DIRECTW))
1346 kn->kn_data = rpipe->pipe_map.cnt;
1347
1348 /* XXXSMP: race for peer */
1349 if ((rpipe->pipe_state & PIPE_EOF) ||
1350 (wpipe == NULL) || (wpipe->pipe_state & PIPE_EOF)) {
1351 kn->kn_flags |= EV_EOF;
1352 if ((hint & NOTE_SUBMIT) == 0)
1353 PIPE_UNLOCK(rpipe);
1354 return (1);
1355 }
1356 if ((hint & NOTE_SUBMIT) == 0)
1357 PIPE_UNLOCK(rpipe);
1358 return (kn->kn_data > 0);
1359 }
1360
1361 /*ARGSUSED*/
1362 static int
1363 filt_pipewrite(struct knote *kn, long hint)
1364 {
1365 struct pipe *rpipe = (struct pipe *)kn->kn_fp->f_data;
1366 struct pipe *wpipe = rpipe->pipe_peer;
1367
1368 if ((hint & NOTE_SUBMIT) == 0)
1369 PIPE_LOCK(rpipe);
1370 /* XXXSMP: race for peer */
1371 if ((wpipe == NULL) || (wpipe->pipe_state & PIPE_EOF)) {
1372 kn->kn_data = 0;
1373 kn->kn_flags |= EV_EOF;
1374 if ((hint & NOTE_SUBMIT) == 0)
1375 PIPE_UNLOCK(rpipe);
1376 return (1);
1377 }
1378 kn->kn_data = wpipe->pipe_buffer.size - wpipe->pipe_buffer.cnt;
1379 if (wpipe->pipe_state & PIPE_DIRECTW)
1380 kn->kn_data = 0;
1381
1382 if ((hint & NOTE_SUBMIT) == 0)
1383 PIPE_UNLOCK(rpipe);
1384 return (kn->kn_data >= PIPE_BUF);
1385 }
1386
1387 static const struct filterops pipe_rfiltops =
1388 { 1, NULL, filt_pipedetach, filt_piperead };
1389 static const struct filterops pipe_wfiltops =
1390 { 1, NULL, filt_pipedetach, filt_pipewrite };
1391
1392 /*ARGSUSED*/
1393 static int
1394 pipe_kqfilter(struct file *fp, struct knote *kn)
1395 {
1396 struct pipe *pipe;
1397
1398 pipe = (struct pipe *)kn->kn_fp->f_data;
1399 switch (kn->kn_filter) {
1400 case EVFILT_READ:
1401 kn->kn_fop = &pipe_rfiltops;
1402 break;
1403 case EVFILT_WRITE:
1404 kn->kn_fop = &pipe_wfiltops;
1405 /* XXXSMP: race for peer */
1406 pipe = pipe->pipe_peer;
1407 if (pipe == NULL) {
1408 /* other end of pipe has been closed */
1409 return (EBADF);
1410 }
1411 break;
1412 default:
1413 return (1);
1414 }
1415 kn->kn_hook = pipe;
1416
1417 PIPE_LOCK(pipe);
1418 SLIST_INSERT_HEAD(&pipe->pipe_sel.sel_klist, kn, kn_selnext);
1419 PIPE_UNLOCK(pipe);
1420 return (0);
1421 }
1422
1423 /*
1424 * Handle pipe sysctls.
1425 */
1426 SYSCTL_SETUP(sysctl_kern_pipe_setup, "sysctl kern.pipe subtree setup")
1427 {
1428
1429 sysctl_createv(clog, 0, NULL, NULL,
1430 CTLFLAG_PERMANENT,
1431 CTLTYPE_NODE, "kern", NULL,
1432 NULL, 0, NULL, 0,
1433 CTL_KERN, CTL_EOL);
1434 sysctl_createv(clog, 0, NULL, NULL,
1435 CTLFLAG_PERMANENT,
1436 CTLTYPE_NODE, "pipe",
1437 SYSCTL_DESCR("Pipe settings"),
1438 NULL, 0, NULL, 0,
1439 CTL_KERN, KERN_PIPE, CTL_EOL);
1440
1441 sysctl_createv(clog, 0, NULL, NULL,
1442 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1443 CTLTYPE_INT, "maxkvasz",
1444 SYSCTL_DESCR("Maximum amount of kernel memory to be "
1445 "used for pipes"),
1446 NULL, 0, &maxpipekva, 0,
1447 CTL_KERN, KERN_PIPE, KERN_PIPE_MAXKVASZ, CTL_EOL);
1448 sysctl_createv(clog, 0, NULL, NULL,
1449 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1450 CTLTYPE_INT, "maxloankvasz",
1451 SYSCTL_DESCR("Limit for direct transfers via page loan"),
1452 NULL, 0, &limitpipekva, 0,
1453 CTL_KERN, KERN_PIPE, KERN_PIPE_LIMITKVA, CTL_EOL);
1454 sysctl_createv(clog, 0, NULL, NULL,
1455 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1456 CTLTYPE_INT, "maxbigpipes",
1457 SYSCTL_DESCR("Maximum number of \"big\" pipes"),
1458 NULL, 0, &maxbigpipes, 0,
1459 CTL_KERN, KERN_PIPE, KERN_PIPE_MAXBIGPIPES, CTL_EOL);
1460 sysctl_createv(clog, 0, NULL, NULL,
1461 CTLFLAG_PERMANENT,
1462 CTLTYPE_INT, "nbigpipes",
1463 SYSCTL_DESCR("Number of \"big\" pipes"),
1464 NULL, 0, &nbigpipe, 0,
1465 CTL_KERN, KERN_PIPE, KERN_PIPE_NBIGPIPES, CTL_EOL);
1466 sysctl_createv(clog, 0, NULL, NULL,
1467 CTLFLAG_PERMANENT,
1468 CTLTYPE_INT, "kvasize",
1469 SYSCTL_DESCR("Amount of kernel memory consumed by pipe "
1470 "buffers"),
1471 NULL, 0, &amountpipekva, 0,
1472 CTL_KERN, KERN_PIPE, KERN_PIPE_KVASIZE, CTL_EOL);
1473 }
1474