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