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