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