sys_pipe.c revision 1.4.2.10 1 /* $NetBSD: sys_pipe.c,v 1.4.2.10 2002/02/28 23:48:50 nathanw Exp $ */
2
3 /*
4 * Copyright (c) 1996 John S. Dyson
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice immediately at the beginning of the file, without modification,
12 * this list of conditions, and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Absolutely no warranty of function or purpose is made by the author
17 * John S. Dyson.
18 * 4. Modifications may be freely made to this file if the above conditions
19 * are met.
20 *
21 * $FreeBSD: src/sys/kern/sys_pipe.c,v 1.82 2001/06/15 20:45:01 jlemon Exp $
22 */
23
24 /*
25 * This file contains a high-performance replacement for the socket-based
26 * pipes scheme originally used in FreeBSD/4.4Lite. It does not support
27 * all features of sockets, but does do everything that pipes normally
28 * do.
29 *
30 * Adaption for NetBSD UVM, including uvm_loan() based direct write, was
31 * written by Jaromir Dolecek.
32 */
33
34 /*
35 * This code has two modes of operation, a small write mode and a large
36 * write mode. The small write mode acts like conventional pipes with
37 * a kernel buffer. If the buffer is less than PIPE_MINDIRECT, then the
38 * "normal" pipe buffering is done. If the buffer is between PIPE_MINDIRECT
39 * and PIPE_SIZE in size, it is fully mapped into the kernel (on FreeBSD,
40 * those pages are also wired), and the receiving process can copy it directly
41 * from the pages in the sending process.
42 *
43 * If the sending process receives a signal, it is possible that it will
44 * go away, and certainly its address space can change, because control
45 * is returned back to the user-mode side. In that case, the pipe code
46 * arranges to copy the buffer supplied by the user process on FreeBSD, to
47 * a pageable kernel buffer, and the receiving process will grab the data
48 * from the pageable kernel buffer. Since signals don't happen all that often,
49 * the copy operation is normally eliminated.
50 * For NetBSD, the pages are mapped read-only, COW for kernel by uvm_loan(),
51 * so no explicit handling need to be done, all is handled by standard VM
52 * facilities.
53 *
54 * The constant PIPE_MINDIRECT is chosen to make sure that buffering will
55 * happen for small transfers so that the system will not spend all of
56 * its time context switching. PIPE_SIZE is constrained by the
57 * amount of kernel virtual memory.
58 */
59
60 #include <sys/cdefs.h>
61 __KERNEL_RCSID(0, "$NetBSD: sys_pipe.c,v 1.4.2.10 2002/02/28 23:48:50 nathanw Exp $");
62
63 #include <sys/param.h>
64 #include <sys/systm.h>
65 #include <sys/proc.h>
66 #include <sys/fcntl.h>
67 #include <sys/file.h>
68 #include <sys/filedesc.h>
69 #include <sys/filio.h>
70 #include <sys/ttycom.h>
71 #include <sys/stat.h>
72 #include <sys/poll.h>
73 #include <sys/signalvar.h>
74 #include <sys/vnode.h>
75 #include <sys/uio.h>
76 #include <sys/lock.h>
77 #ifdef __FreeBSD__
78 #include <sys/mutex.h>
79 #include <sys/selinfo.h>
80 #include <sys/sysproto.h>
81 #elif defined(__NetBSD__)
82 #include <sys/select.h>
83 #include <sys/malloc.h>
84 #include <sys/mount.h>
85 #include <sys/syscallargs.h>
86 #include <uvm/uvm.h>
87 #include <sys/sysctl.h>
88 #include <sys/kernel.h>
89 #endif /* NetBSD, FreeBSD */
90
91 #include <sys/pipe.h>
92
93 #ifdef __NetBSD__
94 /*
95 * Avoid microtime(9), it's slow. We don't guard the read from time(9)
96 * with splclock(9) since we don't actually need to be THAT sure the access
97 * is atomic.
98 */
99 #define vfs_timestamp(tv) (*(tv) = time)
100 #endif
101
102 /*
103 * Use this define if you want to disable *fancy* VM things. Expect an
104 * approx 30% decrease in transfer rate. This could be useful for
105 * OpenBSD.
106 */
107 /* #define PIPE_NODIRECT */
108
109 /*
110 * interfaces to the outside world
111 */
112 #ifdef __FreeBSD__
113 static int pipe_read __P((struct file *fp, struct uio *uio,
114 struct ucred *cred, int flags, struct proc *p));
115 static int pipe_write __P((struct file *fp, struct uio *uio,
116 struct ucred *cred, int flags, struct proc *p));
117 static int pipe_close __P((struct file *fp, struct proc *p));
118 static int pipe_poll __P((struct file *fp, int events, struct ucred *cred,
119 struct proc *p));
120 static int pipe_kqfilter __P((struct file *fp, struct knote *kn));
121 static int pipe_stat __P((struct file *fp, struct stat *sb, struct proc *p));
122 static int pipe_ioctl __P((struct file *fp, u_long cmd, caddr_t data, struct proc *p));
123
124 static struct fileops pipeops = {
125 pipe_read, pipe_write, pipe_ioctl, pipe_poll, pipe_kqfilter,
126 pipe_stat, pipe_close
127 };
128
129 static void filt_pipedetach(struct knote *kn);
130 static int filt_piperead(struct knote *kn, long hint);
131 static int filt_pipewrite(struct knote *kn, long hint);
132
133 static struct filterops pipe_rfiltops =
134 { 1, NULL, filt_pipedetach, filt_piperead };
135 static struct filterops pipe_wfiltops =
136 { 1, NULL, filt_pipedetach, filt_pipewrite };
137 #endif /* FreeBSD */
138
139 #ifdef __NetBSD__
140 static int pipe_read __P((struct file *fp, off_t *offset, struct uio *uio,
141 struct ucred *cred, int flags));
142 static int pipe_write __P((struct file *fp, off_t *offset, struct uio *uio,
143 struct ucred *cred, int flags));
144 static int pipe_close __P((struct file *fp, struct proc *p));
145 static int pipe_poll __P((struct file *fp, int events, struct proc *p));
146 static int pipe_fcntl __P((struct file *fp, u_int com, caddr_t data,
147 struct proc *p));
148 static int pipe_stat __P((struct file *fp, struct stat *sb, struct proc *p));
149 static int pipe_ioctl __P((struct file *fp, u_long cmd, caddr_t data, struct proc *p));
150
151 static struct fileops pipeops =
152 { pipe_read, pipe_write, pipe_ioctl, pipe_fcntl, pipe_poll,
153 pipe_stat, pipe_close };
154 #endif /* NetBSD */
155
156 /*
157 * Default pipe buffer size(s), this can be kind-of large now because pipe
158 * space is pageable. The pipe code will try to maintain locality of
159 * reference for performance reasons, so small amounts of outstanding I/O
160 * will not wipe the cache.
161 */
162 #define MINPIPESIZE (PIPE_SIZE/3)
163 #define MAXPIPESIZE (2*PIPE_SIZE/3)
164
165 /*
166 * Maximum amount of kva for pipes -- this is kind-of a soft limit, but
167 * is there so that on large systems, we don't exhaust it.
168 */
169 #define MAXPIPEKVA (8*1024*1024)
170 static int maxpipekva = MAXPIPEKVA;
171
172 /*
173 * Limit for direct transfers, we cannot, of course limit
174 * the amount of kva for pipes in general though.
175 */
176 #define LIMITPIPEKVA (16*1024*1024)
177 static int limitpipekva = LIMITPIPEKVA;
178
179 /*
180 * Limit the number of "big" pipes
181 */
182 #define LIMITBIGPIPES 32
183 static int maxbigpipes = LIMITBIGPIPES;
184 static int nbigpipe = 0;
185
186 /*
187 * Amount of KVA consumed by pipe buffers.
188 */
189 static int amountpipekva = 0;
190
191 static void pipeclose __P((struct pipe *));
192 static void pipe_free_kmem __P((struct pipe *));
193 static int pipe_create __P((struct pipe **, int));
194 static __inline int pipelock __P((struct pipe *, int));
195 static __inline void pipeunlock __P((struct pipe *));
196 static __inline void pipeselwakeup __P((struct pipe *, struct pipe *));
197 static int pipespace __P((struct pipe *, int));
198
199 #ifdef __FreeBSD__
200 #ifndef PIPE_NODIRECT
201 static int pipe_build_write_buffer __P((struct pipe *wpipe, struct uio *uio));
202 static void pipe_destroy_write_buffer __P((struct pipe *wpipe));
203 static int pipe_direct_write __P((struct pipe *wpipe, struct uio *uio));
204 static void pipe_clone_write_buffer __P((struct pipe *wpipe));
205 #endif
206
207 static vm_zone_t pipe_zone;
208 #endif /* FreeBSD */
209
210 #ifdef __NetBSD__
211 #ifndef PIPE_NODIRECT
212 static int pipe_direct_write __P((struct pipe *, struct uio *));
213 static int pipe_loan_alloc __P((struct pipe *, int));
214 static void pipe_loan_free __P((struct pipe *));
215 #endif /* PIPE_NODIRECT */
216
217 static struct pool pipe_pool;
218 #endif /* NetBSD */
219
220 /*
221 * The pipe system call for the DTYPE_PIPE type of pipes
222 */
223
224 /* ARGSUSED */
225 #ifdef __FreeBSD__
226 int
227 pipe(p, uap)
228 struct proc *p;
229 struct pipe_args /* {
230 int dummy;
231 } */ *uap;
232 #elif defined(__NetBSD__)
233 int
234 sys_pipe(l, v, retval)
235 struct lwp *l;
236 void *v;
237 register_t *retval;
238 #endif
239 {
240 struct file *rf, *wf;
241 struct pipe *rpipe, *wpipe;
242 int fd, error;
243 #ifdef __NetBSD__
244 struct proc *p;
245 #endif
246
247 #ifdef __FreeBSD__
248 if (pipe_zone == NULL)
249 pipe_zone = zinit("PIPE", sizeof(struct pipe), 0, 0, 4);
250
251 rpipe = wpipe = NULL;
252 if (pipe_create(&rpipe, 1) || pipe_create(&wpipe, 1)) {
253 pipeclose(rpipe);
254 pipeclose(wpipe);
255 return (ENFILE);
256 }
257
258 error = falloc(p, &rf, &fd);
259 if (error) {
260 pipeclose(rpipe);
261 pipeclose(wpipe);
262 return (error);
263 }
264 fhold(rf);
265 p->p_retval[0] = fd;
266
267 /*
268 * Warning: once we've gotten past allocation of the fd for the
269 * read-side, we can only drop the read side via fdrop() in order
270 * to avoid races against processes which manage to dup() the read
271 * side while we are blocked trying to allocate the write side.
272 */
273 rf->f_flag = FREAD | FWRITE;
274 rf->f_type = DTYPE_PIPE;
275 rf->f_data = (caddr_t)rpipe;
276 rf->f_ops = &pipeops;
277 error = falloc(p, &wf, &fd);
278 if (error) {
279 struct filedesc *fdp = p->p_fd;
280
281 if (fdp->fd_ofiles[p->p_retval[0]] == rf) {
282 fdp->fd_ofiles[p->p_retval[0]] = NULL;
283 fdrop(rf, p);
284 }
285 fdrop(rf, p);
286 /* rpipe has been closed by fdrop(). */
287 pipeclose(wpipe);
288 return (error);
289 }
290 wf->f_flag = FREAD | FWRITE;
291 wf->f_type = DTYPE_PIPE;
292 wf->f_data = (caddr_t)wpipe;
293 wf->f_ops = &pipeops;
294 p->p_retval[1] = fd;
295
296 rpipe->pipe_peer = wpipe;
297 wpipe->pipe_peer = rpipe;
298 fdrop(rf, p);
299 #endif /* FreeBSD */
300
301 #ifdef __NetBSD__
302 p = l->l_proc;
303 rpipe = wpipe = NULL;
304 if (pipe_create(&rpipe, 1) || pipe_create(&wpipe, 0)) {
305 pipeclose(rpipe);
306 pipeclose(wpipe);
307 return (ENFILE);
308 }
309
310 /*
311 * Note: the file structure returned from falloc() is marked
312 * as 'larval' initially. Unless we mark it as 'mature' by
313 * FILE_SET_MATURE(), any attempt to do anything with it would
314 * return EBADF, including e.g. dup(2) or close(2). This avoids
315 * file descriptor races if we block in the second falloc().
316 */
317
318 error = falloc(p, &rf, &fd);
319 if (error)
320 goto free2;
321 retval[0] = fd;
322 rf->f_flag = FREAD;
323 rf->f_type = DTYPE_PIPE;
324 rf->f_data = (caddr_t)rpipe;
325 rf->f_ops = &pipeops;
326
327 error = falloc(p, &wf, &fd);
328 if (error)
329 goto free3;
330 retval[1] = fd;
331 wf->f_flag = FWRITE;
332 wf->f_type = DTYPE_PIPE;
333 wf->f_data = (caddr_t)wpipe;
334 wf->f_ops = &pipeops;
335
336 rpipe->pipe_peer = wpipe;
337 wpipe->pipe_peer = rpipe;
338
339 FILE_SET_MATURE(rf);
340 FILE_SET_MATURE(wf);
341 FILE_UNUSE(rf, p);
342 FILE_UNUSE(wf, p);
343 return (0);
344 free3:
345 FILE_UNUSE(rf, p);
346 ffree(rf);
347 fdremove(p->p_fd, retval[0]);
348 free2:
349 pipeclose(wpipe);
350 pipeclose(rpipe);
351 #endif /* NetBSD */
352
353 return (error);
354 }
355
356 /*
357 * Allocate kva for pipe circular buffer, the space is pageable
358 * This routine will 'realloc' the size of a pipe safely, if it fails
359 * it will retain the old buffer.
360 * If it fails it will return ENOMEM.
361 */
362 static int
363 pipespace(cpipe, size)
364 struct pipe *cpipe;
365 int size;
366 {
367 caddr_t buffer;
368 #ifdef __FreeBSD__
369 struct vm_object *object;
370 int npages, error;
371
372 npages = round_page(size)/PAGE_SIZE;
373 /*
374 * Create an object, I don't like the idea of paging to/from
375 * kernel_object.
376 */
377 mtx_lock(&vm_mtx);
378 object = vm_object_allocate(OBJT_DEFAULT, npages);
379 buffer = (caddr_t) vm_map_min(kernel_map);
380
381 /*
382 * Insert the object into the kernel map, and allocate kva for it.
383 * The map entry is, by default, pageable.
384 */
385 error = vm_map_find(kernel_map, object, 0,
386 (vm_offset_t *) &buffer, size, 1,
387 VM_PROT_ALL, VM_PROT_ALL, 0);
388
389 if (error != KERN_SUCCESS) {
390 vm_object_deallocate(object);
391 mtx_unlock(&vm_mtx);
392 return (ENOMEM);
393 }
394 #endif /* FreeBSD */
395
396 #ifdef __NetBSD__
397 /*
398 * Allocate pageable virtual address space. Physical memory is allocated
399 * on demand.
400 */
401 buffer = (caddr_t) uvm_km_valloc(kernel_map, round_page(size));
402 if (buffer == NULL)
403 return (ENOMEM);
404 #endif /* NetBSD */
405
406 /* free old resources if we're resizing */
407 pipe_free_kmem(cpipe);
408 #ifdef __FreeBSD__
409 mtx_unlock(&vm_mtx);
410 cpipe->pipe_buffer.object = object;
411 #endif
412 cpipe->pipe_buffer.buffer = buffer;
413 cpipe->pipe_buffer.size = size;
414 cpipe->pipe_buffer.in = 0;
415 cpipe->pipe_buffer.out = 0;
416 cpipe->pipe_buffer.cnt = 0;
417 amountpipekva += cpipe->pipe_buffer.size;
418 return (0);
419 }
420
421 /*
422 * initialize and allocate VM and memory for pipe
423 */
424 static int
425 pipe_create(cpipep, allockva)
426 struct pipe **cpipep;
427 int allockva;
428 {
429 struct pipe *cpipe;
430 int error;
431
432 #ifdef __FreeBSD__
433 *cpipep = zalloc(pipe_zone);
434 #endif
435 #ifdef __NetBSD__
436 *cpipep = pool_get(&pipe_pool, M_WAITOK);
437 #endif
438 if (*cpipep == NULL)
439 return (ENOMEM);
440
441 cpipe = *cpipep;
442
443 /* Initialize */
444 memset(cpipe, 0, sizeof(*cpipe));
445 cpipe->pipe_state = PIPE_SIGNALR;
446
447 if (allockva && (error = pipespace(cpipe, PIPE_SIZE)))
448 return (error);
449
450 vfs_timestamp(&cpipe->pipe_ctime);
451 cpipe->pipe_atime = cpipe->pipe_ctime;
452 cpipe->pipe_mtime = cpipe->pipe_ctime;
453 #ifdef __NetBSD__
454 cpipe->pipe_pgid = NO_PID;
455 lockinit(&cpipe->pipe_lock, PRIBIO | PCATCH, "pipelk", 0, 0);
456 #endif
457
458 return (0);
459 }
460
461
462 /*
463 * lock a pipe for I/O, blocking other access
464 */
465 static __inline int
466 pipelock(cpipe, catch)
467 struct pipe *cpipe;
468 int catch;
469 {
470 int error;
471
472 #ifdef __FreeBSD__
473 while (cpipe->pipe_state & PIPE_LOCK) {
474 cpipe->pipe_state |= PIPE_LWANT;
475 error = tsleep(cpipe, catch ? (PRIBIO | PCATCH) : PRIBIO,
476 "pipelk", 0);
477 if (error != 0)
478 return (error);
479 }
480 cpipe->pipe_state |= PIPE_LOCK;
481 return (0);
482 #endif
483
484 #ifdef __NetBSD__
485 do {
486 error = lockmgr(&cpipe->pipe_lock, LK_EXCLUSIVE, NULL);
487 } while (!catch && (error == EINTR || error == ERESTART));
488 return (error);
489 #endif
490 }
491
492 /*
493 * unlock a pipe I/O lock
494 */
495 static __inline void
496 pipeunlock(cpipe)
497 struct pipe *cpipe;
498 {
499 #ifdef __FreeBSD__
500 cpipe->pipe_state &= ~PIPE_LOCK;
501 if (cpipe->pipe_state & PIPE_LWANT) {
502 cpipe->pipe_state &= ~PIPE_LWANT;
503 wakeup(cpipe);
504 }
505 #endif
506
507 #ifdef __NetBSD__
508 lockmgr(&cpipe->pipe_lock, LK_RELEASE, NULL);
509 #endif
510 }
511
512 /*
513 * Select/poll wakup. This also sends SIGIO to peer connected to
514 * 'sigpipe' side of pipe.
515 */
516 static __inline void
517 pipeselwakeup(selp, sigp)
518 struct pipe *selp, *sigp;
519 {
520 if (selp->pipe_state & PIPE_SEL) {
521 selp->pipe_state &= ~PIPE_SEL;
522 selwakeup(&selp->pipe_sel);
523 }
524 #ifdef __FreeBSD__
525 if (sigp && (sigp->pipe_state & PIPE_ASYNC) && sigp->pipe_sigio)
526 pgsigio(sigp->pipe_sigio, SIGIO, 0);
527 KNOTE(&selp->pipe_sel.si_note, 0);
528 #endif
529
530 #ifdef __NetBSD__
531 if (sigp && (sigp->pipe_state & PIPE_ASYNC)
532 && sigp->pipe_pgid != NO_PID){
533 struct proc *p;
534
535 if (sigp->pipe_pgid < 0)
536 gsignal(-sigp->pipe_pgid, SIGIO);
537 else if (sigp->pipe_pgid > 0 && (p = pfind(sigp->pipe_pgid)) != 0)
538 psignal(p, SIGIO);
539 }
540 #endif /* NetBSD */
541 }
542
543 /* ARGSUSED */
544 #ifdef __FreeBSD__
545 static int
546 pipe_read(fp, uio, cred, flags, p)
547 struct file *fp;
548 struct uio *uio;
549 struct ucred *cred;
550 int flags;
551 struct proc *p;
552 #elif defined(__NetBSD__)
553 static int
554 pipe_read(fp, offset, uio, cred, flags)
555 struct file *fp;
556 off_t *offset;
557 struct uio *uio;
558 struct ucred *cred;
559 int flags;
560 #endif
561 {
562 struct pipe *rpipe = (struct pipe *) fp->f_data;
563 int error;
564 size_t nread = 0;
565 size_t size;
566 size_t ocnt;
567
568 ++rpipe->pipe_busy;
569 error = pipelock(rpipe, 1);
570 if (error)
571 goto unlocked_error;
572
573 ocnt = rpipe->pipe_buffer.cnt;
574
575 while (uio->uio_resid) {
576 /*
577 * normal pipe buffer receive
578 */
579 if (rpipe->pipe_buffer.cnt > 0) {
580 size = rpipe->pipe_buffer.size - rpipe->pipe_buffer.out;
581 if (size > rpipe->pipe_buffer.cnt)
582 size = rpipe->pipe_buffer.cnt;
583 if (size > uio->uio_resid)
584 size = uio->uio_resid;
585
586 error = uiomove(&rpipe->pipe_buffer.buffer[rpipe->pipe_buffer.out],
587 size, uio);
588 if (error)
589 break;
590
591 rpipe->pipe_buffer.out += size;
592 if (rpipe->pipe_buffer.out >= rpipe->pipe_buffer.size)
593 rpipe->pipe_buffer.out = 0;
594
595 rpipe->pipe_buffer.cnt -= size;
596
597 /*
598 * If there is no more to read in the pipe, reset
599 * its pointers to the beginning. This improves
600 * cache hit stats.
601 */
602 if (rpipe->pipe_buffer.cnt == 0) {
603 rpipe->pipe_buffer.in = 0;
604 rpipe->pipe_buffer.out = 0;
605 }
606 nread += size;
607 #ifndef PIPE_NODIRECT
608 /*
609 * Direct copy, bypassing a kernel buffer.
610 */
611 } else if ((size = rpipe->pipe_map.cnt) &&
612 (rpipe->pipe_state & PIPE_DIRECTW)) {
613 caddr_t va;
614 if (size > uio->uio_resid)
615 size = uio->uio_resid;
616
617 va = (caddr_t) rpipe->pipe_map.kva +
618 rpipe->pipe_map.pos;
619 error = uiomove(va, size, uio);
620 if (error)
621 break;
622 nread += size;
623 rpipe->pipe_map.pos += size;
624 rpipe->pipe_map.cnt -= size;
625 if (rpipe->pipe_map.cnt == 0) {
626 rpipe->pipe_state &= ~PIPE_DIRECTW;
627 wakeup(rpipe);
628 }
629 #endif
630 } else {
631 /*
632 * detect EOF condition
633 * read returns 0 on EOF, no need to set error
634 */
635 if (rpipe->pipe_state & PIPE_EOF)
636 break;
637
638 /*
639 * If the "write-side" has been blocked, wake it up now.
640 */
641 if (rpipe->pipe_state & PIPE_WANTW) {
642 rpipe->pipe_state &= ~PIPE_WANTW;
643 wakeup(rpipe);
644 }
645
646 /*
647 * Break if some data was read.
648 */
649 if (nread > 0)
650 break;
651
652 /*
653 * don't block on non-blocking I/O
654 */
655 if (fp->f_flag & FNONBLOCK) {
656 error = EAGAIN;
657 break;
658 }
659
660 /*
661 * Unlock the pipe buffer for our remaining processing.
662 * We will either break out with an error or we will
663 * sleep and relock to loop.
664 */
665 pipeunlock(rpipe);
666
667 /*
668 * We want to read more, wake up select/poll.
669 */
670 pipeselwakeup(rpipe, rpipe->pipe_peer);
671
672 rpipe->pipe_state |= PIPE_WANTR;
673 error = tsleep(rpipe, PRIBIO | PCATCH, "piperd", 0);
674 if (error != 0 || (error = pipelock(rpipe, 1)))
675 goto unlocked_error;
676 }
677 }
678 pipeunlock(rpipe);
679
680 if (error == 0)
681 vfs_timestamp(&rpipe->pipe_atime);
682 unlocked_error:
683 --rpipe->pipe_busy;
684
685 /*
686 * PIPE_WANTCLOSE processing only makes sense if pipe_busy is 0.
687 */
688 if ((rpipe->pipe_busy == 0) && (rpipe->pipe_state & PIPE_WANTCLOSE)) {
689 rpipe->pipe_state &= ~(PIPE_WANTCLOSE|PIPE_WANTW);
690 wakeup(rpipe);
691 } else if (rpipe->pipe_buffer.cnt < MINPIPESIZE) {
692 /*
693 * Handle write blocking hysteresis.
694 */
695 if (rpipe->pipe_state & PIPE_WANTW) {
696 rpipe->pipe_state &= ~PIPE_WANTW;
697 wakeup(rpipe);
698 }
699 }
700
701 /*
702 * If anything was read off the buffer, signal to the writer it's
703 * possible to write more data. Also send signal if we are here for the
704 * first time after last write.
705 */
706 if ((rpipe->pipe_buffer.size - rpipe->pipe_buffer.cnt) >= PIPE_BUF
707 && (ocnt != rpipe->pipe_buffer.cnt || (rpipe->pipe_state & PIPE_SIGNALR))) {
708 pipeselwakeup(rpipe, rpipe->pipe_peer);
709 rpipe->pipe_state &= ~PIPE_SIGNALR;
710 }
711
712 return (error);
713 }
714
715 #ifdef __FreeBSD__
716 #ifndef PIPE_NODIRECT
717 /*
718 * Map the sending processes' buffer into kernel space and wire it.
719 * This is similar to a physical write operation.
720 */
721 static int
722 pipe_build_write_buffer(wpipe, uio)
723 struct pipe *wpipe;
724 struct uio *uio;
725 {
726 size_t size;
727 int i;
728 vm_offset_t addr, endaddr, paddr;
729
730 size = uio->uio_iov->iov_len;
731 if (size > wpipe->pipe_buffer.size)
732 size = wpipe->pipe_buffer.size;
733
734 endaddr = round_page((vm_offset_t)uio->uio_iov->iov_base + size);
735 mtx_lock(&vm_mtx);
736 addr = trunc_page((vm_offset_t)uio->uio_iov->iov_base);
737 for (i = 0; addr < endaddr; addr += PAGE_SIZE, i++) {
738 vm_page_t m;
739
740 if (vm_fault_quick((caddr_t)addr, VM_PROT_READ) < 0 ||
741 (paddr = pmap_kextract(addr)) == 0) {
742 int j;
743
744 for (j = 0; j < i; j++)
745 vm_page_unwire(wpipe->pipe_map.ms[j], 1);
746 mtx_unlock(&vm_mtx);
747 return (EFAULT);
748 }
749
750 m = PHYS_TO_VM_PAGE(paddr);
751 vm_page_wire(m);
752 wpipe->pipe_map.ms[i] = m;
753 }
754
755 /*
756 * set up the control block
757 */
758 wpipe->pipe_map.npages = i;
759 wpipe->pipe_map.pos =
760 ((vm_offset_t) uio->uio_iov->iov_base) & PAGE_MASK;
761 wpipe->pipe_map.cnt = size;
762
763 /*
764 * and map the buffer
765 */
766 if (wpipe->pipe_map.kva == 0) {
767 /*
768 * We need to allocate space for an extra page because the
769 * address range might (will) span pages at times.
770 */
771 wpipe->pipe_map.kva = kmem_alloc_pageable(kernel_map,
772 wpipe->pipe_buffer.size + PAGE_SIZE);
773 amountpipekva += wpipe->pipe_buffer.size + PAGE_SIZE;
774 }
775 pmap_qenter(wpipe->pipe_map.kva, wpipe->pipe_map.ms,
776 wpipe->pipe_map.npages);
777
778 mtx_unlock(&vm_mtx);
779 /*
780 * and update the uio data
781 */
782
783 uio->uio_iov->iov_len -= size;
784 uio->uio_iov->iov_base += size;
785 if (uio->uio_iov->iov_len == 0)
786 uio->uio_iov++;
787 uio->uio_resid -= size;
788 uio->uio_offset += size;
789 return (0);
790 }
791
792 /*
793 * unmap and unwire the process buffer
794 */
795 static void
796 pipe_destroy_write_buffer(wpipe)
797 struct pipe *wpipe;
798 {
799 int i;
800
801 mtx_lock(&vm_mtx);
802 if (wpipe->pipe_map.kva) {
803 pmap_qremove(wpipe->pipe_map.kva, wpipe->pipe_map.npages);
804
805 if (amountpipekva > maxpipekva) {
806 vm_offset_t kva = wpipe->pipe_map.kva;
807 wpipe->pipe_map.kva = 0;
808 kmem_free(kernel_map, kva,
809 wpipe->pipe_buffer.size + PAGE_SIZE);
810 amountpipekva -= wpipe->pipe_buffer.size + PAGE_SIZE;
811 }
812 }
813 for (i = 0; i < wpipe->pipe_map.npages; i++)
814 vm_page_unwire(wpipe->pipe_map.ms[i], 1);
815 mtx_unlock(&vm_mtx);
816 }
817
818 /*
819 * In the case of a signal, the writing process might go away. This
820 * code copies the data into the circular buffer so that the source
821 * pages can be freed without loss of data.
822 */
823 static void
824 pipe_clone_write_buffer(wpipe)
825 struct pipe *wpipe;
826 {
827 int size;
828 int pos;
829
830 size = wpipe->pipe_map.cnt;
831 pos = wpipe->pipe_map.pos;
832 memcpy((caddr_t) wpipe->pipe_buffer.buffer,
833 (caddr_t) wpipe->pipe_map.kva + pos, size);
834
835 wpipe->pipe_buffer.in = size;
836 wpipe->pipe_buffer.out = 0;
837 wpipe->pipe_buffer.cnt = size;
838 wpipe->pipe_state &= ~PIPE_DIRECTW;
839
840 pipe_destroy_write_buffer(wpipe);
841 }
842
843 /*
844 * This implements the pipe buffer write mechanism. Note that only
845 * a direct write OR a normal pipe write can be pending at any given time.
846 * If there are any characters in the pipe buffer, the direct write will
847 * be deferred until the receiving process grabs all of the bytes from
848 * the pipe buffer. Then the direct mapping write is set-up.
849 */
850 static int
851 pipe_direct_write(wpipe, uio)
852 struct pipe *wpipe;
853 struct uio *uio;
854 {
855 int error;
856
857 retry:
858 while (wpipe->pipe_state & PIPE_DIRECTW) {
859 if (wpipe->pipe_state & PIPE_WANTR) {
860 wpipe->pipe_state &= ~PIPE_WANTR;
861 wakeup(wpipe);
862 }
863 wpipe->pipe_state |= PIPE_WANTW;
864 error = tsleep(wpipe, PRIBIO | PCATCH, "pipdww", 0);
865 if (error)
866 goto error1;
867 if (wpipe->pipe_state & PIPE_EOF) {
868 error = EPIPE;
869 goto error1;
870 }
871 }
872 wpipe->pipe_map.cnt = 0; /* transfer not ready yet */
873 if (wpipe->pipe_buffer.cnt > 0) {
874 if (wpipe->pipe_state & PIPE_WANTR) {
875 wpipe->pipe_state &= ~PIPE_WANTR;
876 wakeup(wpipe);
877 }
878
879 wpipe->pipe_state |= PIPE_WANTW;
880 error = tsleep(wpipe, PRIBIO | PCATCH, "pipdwc", 0);
881 if (error)
882 goto error1;
883 if (wpipe->pipe_state & PIPE_EOF) {
884 error = EPIPE;
885 goto error1;
886 }
887 goto retry;
888 }
889
890 wpipe->pipe_state |= PIPE_DIRECTW;
891
892 error = pipe_build_write_buffer(wpipe, uio);
893 if (error) {
894 wpipe->pipe_state &= ~PIPE_DIRECTW;
895 goto error1;
896 }
897
898 error = 0;
899 while (!error && (wpipe->pipe_state & PIPE_DIRECTW)) {
900 if (wpipe->pipe_state & PIPE_EOF) {
901 pipelock(wpipe, 0);
902 pipe_destroy_write_buffer(wpipe);
903 pipeunlock(wpipe);
904 pipeselwakeup(wpipe, wpipe);
905 error = EPIPE;
906 goto error1;
907 }
908 if (wpipe->pipe_state & PIPE_WANTR) {
909 wpipe->pipe_state &= ~PIPE_WANTR;
910 wakeup(wpipe);
911 }
912 pipeselwakeup(wpipe, wpipe);
913 error = tsleep(wpipe, PRIBIO | PCATCH, "pipdwt", 0);
914 }
915
916 pipelock(wpipe,0);
917 if (wpipe->pipe_state & PIPE_DIRECTW) {
918 /*
919 * this bit of trickery substitutes a kernel buffer for
920 * the process that might be going away.
921 */
922 pipe_clone_write_buffer(wpipe);
923 } else {
924 pipe_destroy_write_buffer(wpipe);
925 }
926 pipeunlock(wpipe);
927 return (error);
928
929 error1:
930 wakeup(wpipe);
931 return (error);
932 }
933 #endif /* !PIPE_NODIRECT */
934 #endif /* FreeBSD */
935
936 #ifdef __NetBSD__
937 #ifndef PIPE_NODIRECT
938 /*
939 * Allocate structure for loan transfer.
940 */
941 static int
942 pipe_loan_alloc(wpipe, npages)
943 struct pipe *wpipe;
944 int npages;
945 {
946 vsize_t len;
947
948 len = (vsize_t)npages << PAGE_SHIFT;
949 wpipe->pipe_map.kva = uvm_km_valloc_wait(kernel_map, len);
950 if (wpipe->pipe_map.kva == NULL)
951 return (ENOMEM);
952
953 amountpipekva += len;
954 wpipe->pipe_map.npages = npages;
955 wpipe->pipe_map.pgs = malloc(npages * sizeof(struct vm_page *), M_PIPE,
956 M_WAITOK);
957 return (0);
958 }
959
960 /*
961 * Free resources allocated for loan transfer.
962 */
963 static void
964 pipe_loan_free(wpipe)
965 struct pipe *wpipe;
966 {
967 vsize_t len;
968
969 len = (vsize_t)wpipe->pipe_map.npages << PAGE_SHIFT;
970 uvm_km_free(kernel_map, wpipe->pipe_map.kva, len);
971 wpipe->pipe_map.kva = NULL;
972 amountpipekva -= len;
973 free(wpipe->pipe_map.pgs, M_PIPE);
974 wpipe->pipe_map.pgs = NULL;
975 }
976
977 /*
978 * NetBSD direct write, using uvm_loan() mechanism.
979 * This implements the pipe buffer write mechanism. Note that only
980 * a direct write OR a normal pipe write can be pending at any given time.
981 * If there are any characters in the pipe buffer, the direct write will
982 * be deferred until the receiving process grabs all of the bytes from
983 * the pipe buffer. Then the direct mapping write is set-up.
984 */
985 static int
986 pipe_direct_write(wpipe, uio)
987 struct pipe *wpipe;
988 struct uio *uio;
989 {
990 int error, npages, j;
991 struct vm_page **pgs;
992 vaddr_t bbase, kva, base, bend;
993 vsize_t blen, bcnt;
994 voff_t bpos;
995
996 retry:
997 while (wpipe->pipe_state & PIPE_DIRECTW) {
998 if (wpipe->pipe_state & PIPE_WANTR) {
999 wpipe->pipe_state &= ~PIPE_WANTR;
1000 wakeup(wpipe);
1001 }
1002 wpipe->pipe_state |= PIPE_WANTW;
1003 error = tsleep(wpipe, PRIBIO | PCATCH, "pipdww", 0);
1004 if (error)
1005 goto error;
1006 if (wpipe->pipe_state & PIPE_EOF) {
1007 error = EPIPE;
1008 goto error;
1009 }
1010 }
1011 wpipe->pipe_map.cnt = 0; /* transfer not ready yet */
1012 if (wpipe->pipe_buffer.cnt > 0) {
1013 if (wpipe->pipe_state & PIPE_WANTR) {
1014 wpipe->pipe_state &= ~PIPE_WANTR;
1015 wakeup(wpipe);
1016 }
1017
1018 wpipe->pipe_state |= PIPE_WANTW;
1019 error = tsleep(wpipe, PRIBIO | PCATCH, "pipdwc", 0);
1020 if (error)
1021 goto error;
1022 if (wpipe->pipe_state & PIPE_EOF) {
1023 error = EPIPE;
1024 goto error;
1025 }
1026 goto retry;
1027 }
1028
1029 /*
1030 * Handle first PIPE_CHUNK_SIZE bytes of buffer. Deal with buffers
1031 * not aligned to PAGE_SIZE.
1032 */
1033 bbase = (vaddr_t)uio->uio_iov->iov_base;
1034 base = trunc_page(bbase);
1035 bend = round_page(bbase + uio->uio_iov->iov_len);
1036 blen = bend - base;
1037 bpos = bbase - base;
1038
1039 if (blen > PIPE_DIRECT_CHUNK) {
1040 blen = PIPE_DIRECT_CHUNK;
1041 bend = base + blen;
1042 bcnt = PIPE_DIRECT_CHUNK - bpos;
1043 } else {
1044 bcnt = uio->uio_iov->iov_len;
1045 }
1046 npages = blen >> PAGE_SHIFT;
1047
1048 wpipe->pipe_map.pos = bpos;
1049 wpipe->pipe_map.cnt = bcnt;
1050
1051 /*
1052 * Free the old kva if we need more pages than we have
1053 * allocated.
1054 */
1055 if (wpipe->pipe_map.kva && npages > wpipe->pipe_map.npages)
1056 pipe_loan_free(wpipe);
1057
1058 /* Allocate new kva. */
1059 if (wpipe->pipe_map.kva == NULL) {
1060 error = pipe_loan_alloc(wpipe, npages);
1061 if (error) {
1062 goto error;
1063 }
1064 }
1065
1066 /* Loan the write buffer memory from writer process */
1067 pgs = wpipe->pipe_map.pgs;
1068 error = uvm_loan(&uio->uio_procp->p_vmspace->vm_map, base, blen,
1069 pgs, UVM_LOAN_TOPAGE);
1070 if (error) {
1071 pgs = NULL;
1072 goto cleanup;
1073 }
1074
1075 /* Enter the loaned pages to kva */
1076 kva = wpipe->pipe_map.kva;
1077 for (j = 0; j < npages; j++, kva += PAGE_SIZE) {
1078 pmap_kenter_pa(kva, VM_PAGE_TO_PHYS(pgs[j]), VM_PROT_READ);
1079 }
1080 pmap_update(pmap_kernel());
1081
1082 wpipe->pipe_state |= PIPE_DIRECTW;
1083 while (!error && (wpipe->pipe_state & PIPE_DIRECTW)) {
1084 if (wpipe->pipe_state & PIPE_EOF) {
1085 error = EPIPE;
1086 break;
1087 }
1088 if (wpipe->pipe_state & PIPE_WANTR) {
1089 wpipe->pipe_state &= ~PIPE_WANTR;
1090 wakeup(wpipe);
1091 }
1092 pipeselwakeup(wpipe, wpipe);
1093 error = tsleep(wpipe, PRIBIO | PCATCH, "pipdwt", 0);
1094 }
1095
1096 if (error)
1097 wpipe->pipe_state &= ~PIPE_DIRECTW;
1098
1099 cleanup:
1100 pipelock(wpipe, 0);
1101 if (pgs != NULL) {
1102 pmap_kremove(wpipe->pipe_map.kva, blen);
1103 uvm_unloan(pgs, npages, UVM_LOAN_TOPAGE);
1104 }
1105 if (error || amountpipekva > maxpipekva)
1106 pipe_loan_free(wpipe);
1107 pipeunlock(wpipe);
1108
1109 if (error) {
1110 pipeselwakeup(wpipe, wpipe);
1111
1112 /*
1113 * If nothing was read from what we offered, return error
1114 * straight on. Otherwise update uio resid first. Caller
1115 * will deal with the error condition, returning short
1116 * write, error, or restarting the write(2) as appropriate.
1117 */
1118 if (wpipe->pipe_map.cnt == bcnt) {
1119 error:
1120 wakeup(wpipe);
1121 return (error);
1122 }
1123
1124 bcnt -= wpipe->pipe_map.cnt;
1125 }
1126
1127 uio->uio_resid -= bcnt;
1128 /* uio_offset not updated, not set/used for write(2) */
1129 uio->uio_iov->iov_base = (char *)uio->uio_iov->iov_base + bcnt;
1130 uio->uio_iov->iov_len -= bcnt;
1131 if (uio->uio_iov->iov_len == 0) {
1132 uio->uio_iov++;
1133 uio->uio_iovcnt--;
1134 }
1135
1136 return (error);
1137 }
1138 #endif /* !PIPE_NODIRECT */
1139 #endif /* NetBSD */
1140
1141 #ifdef __FreeBSD__
1142 static int
1143 pipe_write(fp, uio, cred, flags, p)
1144 struct file *fp;
1145 off_t *offset;
1146 struct uio *uio;
1147 struct ucred *cred;
1148 int flags;
1149 struct proc *p;
1150 #elif defined(__NetBSD__)
1151 static int
1152 pipe_write(fp, offset, uio, cred, flags)
1153 struct file *fp;
1154 off_t *offset;
1155 struct uio *uio;
1156 struct ucred *cred;
1157 int flags;
1158 #endif
1159 {
1160 int error = 0;
1161 struct pipe *wpipe, *rpipe;
1162
1163 rpipe = (struct pipe *) fp->f_data;
1164 wpipe = rpipe->pipe_peer;
1165
1166 /*
1167 * detect loss of pipe read side, issue SIGPIPE if lost.
1168 */
1169 if ((wpipe == NULL) || (wpipe->pipe_state & PIPE_EOF))
1170 return (EPIPE);
1171
1172 ++wpipe->pipe_busy;
1173
1174 /*
1175 * If it is advantageous to resize the pipe buffer, do
1176 * so.
1177 */
1178 if ((uio->uio_resid > PIPE_SIZE) &&
1179 (nbigpipe < maxbigpipes) &&
1180 #ifndef PIPE_NODIRECT
1181 (wpipe->pipe_state & PIPE_DIRECTW) == 0 &&
1182 #endif
1183 (wpipe->pipe_buffer.size <= PIPE_SIZE) &&
1184 (wpipe->pipe_buffer.cnt == 0)) {
1185
1186 if ((error = pipelock(wpipe,1)) == 0) {
1187 if (pipespace(wpipe, BIG_PIPE_SIZE) == 0)
1188 nbigpipe++;
1189 pipeunlock(wpipe);
1190 } else {
1191 /*
1192 * If an error occurred, unbusy and return, waking up
1193 * any waiting readers.
1194 */
1195 --wpipe->pipe_busy;
1196 if (wpipe->pipe_busy == 0
1197 && (wpipe->pipe_state & PIPE_WANTCLOSE)) {
1198 wpipe->pipe_state &=
1199 ~(PIPE_WANTCLOSE | PIPE_WANTR);
1200 wakeup(wpipe);
1201 }
1202
1203 return (error);
1204 }
1205 }
1206
1207 #ifdef __FreeBSD__
1208 KASSERT(wpipe->pipe_buffer.buffer != NULL, ("pipe buffer gone"));
1209 #endif
1210
1211 while (uio->uio_resid) {
1212 int space;
1213
1214 #ifndef PIPE_NODIRECT
1215 /*
1216 * If the transfer is large, we can gain performance if
1217 * we do process-to-process copies directly.
1218 * If the write is non-blocking, we don't use the
1219 * direct write mechanism.
1220 *
1221 * The direct write mechanism will detect the reader going
1222 * away on us.
1223 */
1224 if ((uio->uio_iov->iov_len >= PIPE_MINDIRECT) &&
1225 (fp->f_flag & FNONBLOCK) == 0 &&
1226 (wpipe->pipe_map.kva || (amountpipekva < limitpipekva))) {
1227 error = pipe_direct_write(wpipe, uio);
1228
1229 /*
1230 * Break out if error occured, unless it's ENOMEM.
1231 * ENOMEM means we failed to allocate some resources
1232 * for direct write, so we just fallback to ordinary
1233 * write. If the direct write was successful,
1234 * process rest of data via ordinary write.
1235 */
1236 if (!error)
1237 continue;
1238
1239 if (error != ENOMEM)
1240 break;
1241 }
1242 #endif /* PIPE_NODIRECT */
1243
1244 /*
1245 * Pipe buffered writes cannot be coincidental with
1246 * direct writes. We wait until the currently executing
1247 * direct write is completed before we start filling the
1248 * pipe buffer. We break out if a signal occurs or the
1249 * reader goes away.
1250 */
1251 retrywrite:
1252 while (wpipe->pipe_state & PIPE_DIRECTW) {
1253 if (wpipe->pipe_state & PIPE_WANTR) {
1254 wpipe->pipe_state &= ~PIPE_WANTR;
1255 wakeup(wpipe);
1256 }
1257 error = tsleep(wpipe, PRIBIO | PCATCH, "pipbww", 0);
1258 if (wpipe->pipe_state & PIPE_EOF)
1259 break;
1260 if (error)
1261 break;
1262 }
1263 if (wpipe->pipe_state & PIPE_EOF) {
1264 error = EPIPE;
1265 break;
1266 }
1267
1268 space = wpipe->pipe_buffer.size - wpipe->pipe_buffer.cnt;
1269
1270 /* Writes of size <= PIPE_BUF must be atomic. */
1271 if ((space < uio->uio_resid) && (uio->uio_resid <= PIPE_BUF))
1272 space = 0;
1273
1274 if (space > 0) {
1275 int size; /* Transfer size */
1276 int segsize; /* first segment to transfer */
1277
1278 if ((error = pipelock(wpipe,1)) != 0)
1279 break;
1280
1281 /*
1282 * It is possible for a direct write to
1283 * slip in on us... handle it here...
1284 */
1285 if (wpipe->pipe_state & PIPE_DIRECTW) {
1286 pipeunlock(wpipe);
1287 goto retrywrite;
1288 }
1289 /*
1290 * If a process blocked in uiomove, our
1291 * value for space might be bad.
1292 *
1293 * XXX will we be ok if the reader has gone
1294 * away here?
1295 */
1296 if (space > wpipe->pipe_buffer.size -
1297 wpipe->pipe_buffer.cnt) {
1298 pipeunlock(wpipe);
1299 goto retrywrite;
1300 }
1301
1302 /*
1303 * Transfer size is minimum of uio transfer
1304 * and free space in pipe buffer.
1305 */
1306 if (space > uio->uio_resid)
1307 size = uio->uio_resid;
1308 else
1309 size = space;
1310 /*
1311 * First segment to transfer is minimum of
1312 * transfer size and contiguous space in
1313 * pipe buffer. If first segment to transfer
1314 * is less than the transfer size, we've got
1315 * a wraparound in the buffer.
1316 */
1317 segsize = wpipe->pipe_buffer.size -
1318 wpipe->pipe_buffer.in;
1319 if (segsize > size)
1320 segsize = size;
1321
1322 /* Transfer first segment */
1323
1324 error = uiomove(&wpipe->pipe_buffer.buffer[wpipe->pipe_buffer.in],
1325 segsize, uio);
1326
1327 if (error == 0 && segsize < size) {
1328 /*
1329 * Transfer remaining part now, to
1330 * support atomic writes. Wraparound
1331 * happened.
1332 */
1333 #ifdef DEBUG
1334 if (wpipe->pipe_buffer.in + segsize !=
1335 wpipe->pipe_buffer.size)
1336 panic("Expected pipe buffer wraparound disappeared");
1337 #endif
1338
1339 error = uiomove(&wpipe->pipe_buffer.buffer[0],
1340 size - segsize, uio);
1341 }
1342 if (error == 0) {
1343 wpipe->pipe_buffer.in += size;
1344 if (wpipe->pipe_buffer.in >=
1345 wpipe->pipe_buffer.size) {
1346 #ifdef DEBUG
1347 if (wpipe->pipe_buffer.in != size - segsize + wpipe->pipe_buffer.size)
1348 panic("Expected wraparound bad");
1349 #endif
1350 wpipe->pipe_buffer.in = size - segsize;
1351 }
1352
1353 wpipe->pipe_buffer.cnt += size;
1354 #ifdef DEBUG
1355 if (wpipe->pipe_buffer.cnt > wpipe->pipe_buffer.size)
1356 panic("Pipe buffer overflow");
1357 #endif
1358 }
1359 pipeunlock(wpipe);
1360 if (error)
1361 break;
1362 } else {
1363 /*
1364 * If the "read-side" has been blocked, wake it up now.
1365 */
1366 if (wpipe->pipe_state & PIPE_WANTR) {
1367 wpipe->pipe_state &= ~PIPE_WANTR;
1368 wakeup(wpipe);
1369 }
1370
1371 /*
1372 * don't block on non-blocking I/O
1373 */
1374 if (fp->f_flag & FNONBLOCK) {
1375 error = EAGAIN;
1376 break;
1377 }
1378
1379 /*
1380 * We have no more space and have something to offer,
1381 * wake up select/poll.
1382 */
1383 pipeselwakeup(wpipe, wpipe);
1384
1385 wpipe->pipe_state |= PIPE_WANTW;
1386 error = tsleep(wpipe, PRIBIO | PCATCH, "pipewr", 0);
1387 if (error != 0)
1388 break;
1389 /*
1390 * If read side wants to go away, we just issue a signal
1391 * to ourselves.
1392 */
1393 if (wpipe->pipe_state & PIPE_EOF) {
1394 error = EPIPE;
1395 break;
1396 }
1397 }
1398 }
1399
1400 --wpipe->pipe_busy;
1401 if ((wpipe->pipe_busy == 0) && (wpipe->pipe_state & PIPE_WANTCLOSE)) {
1402 wpipe->pipe_state &= ~(PIPE_WANTCLOSE | PIPE_WANTR);
1403 wakeup(wpipe);
1404 } else if (wpipe->pipe_buffer.cnt > 0) {
1405 /*
1406 * If we have put any characters in the buffer, we wake up
1407 * the reader.
1408 */
1409 if (wpipe->pipe_state & PIPE_WANTR) {
1410 wpipe->pipe_state &= ~PIPE_WANTR;
1411 wakeup(wpipe);
1412 }
1413 }
1414
1415 /*
1416 * Don't return EPIPE if I/O was successful
1417 */
1418 if ((error == EPIPE) && (wpipe->pipe_buffer.cnt == 0)
1419 && (uio->uio_resid == 0))
1420 error = 0;
1421
1422 if (error == 0)
1423 vfs_timestamp(&wpipe->pipe_mtime);
1424
1425 /*
1426 * We have something to offer, wake up select/poll.
1427 * wpipe->pipe_map.cnt is always 0 in this point (direct write
1428 * is only done synchronously), so check only wpipe->pipe_buffer.cnt
1429 */
1430 if (wpipe->pipe_buffer.cnt)
1431 pipeselwakeup(wpipe, wpipe);
1432
1433 /*
1434 * Arrange for next read(2) to do a signal.
1435 */
1436 wpipe->pipe_state |= PIPE_SIGNALR;
1437
1438 return (error);
1439 }
1440
1441 /*
1442 * we implement a very minimal set of ioctls for compatibility with sockets.
1443 */
1444 int
1445 pipe_ioctl(fp, cmd, data, p)
1446 struct file *fp;
1447 u_long cmd;
1448 caddr_t data;
1449 struct proc *p;
1450 {
1451 struct pipe *mpipe = (struct pipe *)fp->f_data;
1452
1453 switch (cmd) {
1454
1455 case FIONBIO:
1456 return (0);
1457
1458 case FIOASYNC:
1459 if (*(int *)data) {
1460 mpipe->pipe_state |= PIPE_ASYNC;
1461 } else {
1462 mpipe->pipe_state &= ~PIPE_ASYNC;
1463 }
1464 return (0);
1465
1466 case FIONREAD:
1467 #ifndef PIPE_NODIRECT
1468 if (mpipe->pipe_state & PIPE_DIRECTW)
1469 *(int *)data = mpipe->pipe_map.cnt;
1470 else
1471 #endif
1472 *(int *)data = mpipe->pipe_buffer.cnt;
1473 return (0);
1474
1475 #ifdef __FreeBSD__
1476 case FIOSETOWN:
1477 return (fsetown(*(int *)data, &mpipe->pipe_sigio));
1478
1479 case FIOGETOWN:
1480 *(int *)data = fgetown(mpipe->pipe_sigio);
1481 return (0);
1482
1483 /* This is deprecated, FIOSETOWN should be used instead. */
1484 case TIOCSPGRP:
1485 return (fsetown(-(*(int *)data), &mpipe->pipe_sigio));
1486
1487 /* This is deprecated, FIOGETOWN should be used instead. */
1488 case TIOCGPGRP:
1489 *(int *)data = -fgetown(mpipe->pipe_sigio);
1490 return (0);
1491 #endif /* FreeBSD */
1492 #ifdef __NetBSD__
1493 case TIOCSPGRP:
1494 mpipe->pipe_pgid = *(int *)data;
1495 return (0);
1496
1497 case TIOCGPGRP:
1498 *(int *)data = mpipe->pipe_pgid;
1499 return (0);
1500 #endif /* NetBSD */
1501
1502 }
1503 return (ENOTTY);
1504 }
1505
1506 int
1507 pipe_poll(fp, events, p)
1508 struct file *fp;
1509 int events;
1510 struct proc *p;
1511 {
1512 struct pipe *rpipe = (struct pipe *)fp->f_data;
1513 struct pipe *wpipe;
1514 int revents = 0;
1515
1516 wpipe = rpipe->pipe_peer;
1517 if (events & (POLLIN | POLLRDNORM))
1518 if ((rpipe->pipe_buffer.cnt > 0) ||
1519 #ifndef PIPE_NODIRECT
1520 (rpipe->pipe_state & PIPE_DIRECTW) ||
1521 #endif
1522 (rpipe->pipe_state & PIPE_EOF))
1523 revents |= events & (POLLIN | POLLRDNORM);
1524
1525 if (events & (POLLOUT | POLLWRNORM))
1526 if (wpipe == NULL || (wpipe->pipe_state & PIPE_EOF)
1527 || (
1528 #ifndef PIPE_NODIRECT
1529 ((wpipe->pipe_state & PIPE_DIRECTW) == 0) &&
1530 #endif
1531 (wpipe->pipe_buffer.size - wpipe->pipe_buffer.cnt) >= PIPE_BUF))
1532 revents |= events & (POLLOUT | POLLWRNORM);
1533
1534 if ((rpipe->pipe_state & PIPE_EOF) ||
1535 (wpipe == NULL) ||
1536 (wpipe->pipe_state & PIPE_EOF))
1537 revents |= POLLHUP;
1538
1539 if (revents == 0) {
1540 if (events & (POLLIN | POLLRDNORM)) {
1541 selrecord(p, &rpipe->pipe_sel);
1542 rpipe->pipe_state |= PIPE_SEL;
1543 }
1544
1545 if (events & (POLLOUT | POLLWRNORM)) {
1546 selrecord(p, &wpipe->pipe_sel);
1547 wpipe->pipe_state |= PIPE_SEL;
1548 }
1549 }
1550
1551 return (revents);
1552 }
1553
1554 static int
1555 pipe_stat(fp, ub, p)
1556 struct file *fp;
1557 struct stat *ub;
1558 struct proc *p;
1559 {
1560 struct pipe *pipe = (struct pipe *)fp->f_data;
1561
1562 memset((caddr_t)ub, 0, sizeof(*ub));
1563 ub->st_mode = S_IFIFO;
1564 ub->st_blksize = pipe->pipe_buffer.size;
1565 ub->st_size = pipe->pipe_buffer.cnt;
1566 ub->st_blocks = (ub->st_size) ? 1 : 0;
1567 #ifdef __FreeBSD__
1568 ub->st_atimespec = pipe->pipe_atime;
1569 ub->st_mtimespec = pipe->pipe_mtime;
1570 ub->st_ctimespec = pipe->pipe_ctime;
1571 #endif /* FreeBSD */
1572 #ifdef __NetBSD__
1573 TIMEVAL_TO_TIMESPEC(&pipe->pipe_atime, &ub->st_atimespec)
1574 TIMEVAL_TO_TIMESPEC(&pipe->pipe_mtime, &ub->st_mtimespec);
1575 TIMEVAL_TO_TIMESPEC(&pipe->pipe_ctime, &ub->st_ctimespec);
1576 #endif /* NetBSD */
1577 ub->st_uid = fp->f_cred->cr_uid;
1578 ub->st_gid = fp->f_cred->cr_gid;
1579 /*
1580 * Left as 0: st_dev, st_ino, st_nlink, st_rdev, st_flags, st_gen.
1581 * XXX (st_dev, st_ino) should be unique.
1582 */
1583 return (0);
1584 }
1585
1586 /* ARGSUSED */
1587 static int
1588 pipe_close(fp, p)
1589 struct file *fp;
1590 struct proc *p;
1591 {
1592 struct pipe *cpipe = (struct pipe *)fp->f_data;
1593
1594 #ifdef __FreeBSD__
1595 fp->f_ops = &badfileops;
1596 funsetown(cpipe->pipe_sigio);
1597 #endif
1598 fp->f_data = NULL;
1599 pipeclose(cpipe);
1600 return (0);
1601 }
1602
1603 static void
1604 pipe_free_kmem(cpipe)
1605 struct pipe *cpipe;
1606 {
1607
1608 #ifdef __FreeBSD__
1609 mtx_assert(&vm_mtx, MA_OWNED);
1610 #endif
1611 if (cpipe->pipe_buffer.buffer != NULL) {
1612 if (cpipe->pipe_buffer.size > PIPE_SIZE)
1613 --nbigpipe;
1614 amountpipekva -= cpipe->pipe_buffer.size;
1615 #ifdef __FreeBSD__
1616 kmem_free(kernel_map,
1617 (vm_offset_t)cpipe->pipe_buffer.buffer,
1618 cpipe->pipe_buffer.size);
1619 #elif defined(__NetBSD__)
1620 uvm_km_free(kernel_map,
1621 (vaddr_t)cpipe->pipe_buffer.buffer,
1622 cpipe->pipe_buffer.size);
1623 #endif /* NetBSD */
1624 cpipe->pipe_buffer.buffer = NULL;
1625 }
1626 #ifndef PIPE_NODIRECT
1627 if (cpipe->pipe_map.kva != NULL) {
1628 #ifdef __FreeBSD__
1629 amountpipekva -= cpipe->pipe_buffer.size + PAGE_SIZE;
1630 kmem_free(kernel_map,
1631 cpipe->pipe_map.kva,
1632 cpipe->pipe_buffer.size + PAGE_SIZE);
1633 #elif defined(__NetBSD__)
1634 pipe_loan_free(cpipe);
1635 #endif /* NetBSD */
1636 cpipe->pipe_map.cnt = 0;
1637 cpipe->pipe_map.kva = NULL;
1638 cpipe->pipe_map.pos = 0;
1639 cpipe->pipe_map.npages = 0;
1640 }
1641 #endif /* !PIPE_NODIRECT */
1642 }
1643
1644 /*
1645 * shutdown the pipe
1646 */
1647 static void
1648 pipeclose(cpipe)
1649 struct pipe *cpipe;
1650 {
1651 struct pipe *ppipe;
1652
1653 if (!cpipe)
1654 return;
1655
1656 pipeselwakeup(cpipe, cpipe);
1657
1658 /*
1659 * If the other side is blocked, wake it up saying that
1660 * we want to close it down.
1661 */
1662 while (cpipe->pipe_busy) {
1663 wakeup(cpipe);
1664 cpipe->pipe_state |= PIPE_WANTCLOSE | PIPE_EOF;
1665 tsleep(cpipe, PRIBIO, "pipecl", 0);
1666 }
1667
1668 /*
1669 * Disconnect from peer
1670 */
1671 if ((ppipe = cpipe->pipe_peer) != NULL) {
1672 pipeselwakeup(ppipe, ppipe);
1673
1674 ppipe->pipe_state |= PIPE_EOF;
1675 wakeup(ppipe);
1676 ppipe->pipe_peer = NULL;
1677 }
1678
1679 /*
1680 * free resources
1681 */
1682 #ifdef __FreeBSD__
1683 mtx_lock(&vm_mtx);
1684 pipe_free_kmem(cpipe);
1685 /* XXX: erm, doesn't zalloc already have its own locks and
1686 * not need the giant vm lock?
1687 */
1688 zfree(pipe_zone, cpipe);
1689 mtx_unlock(&vm_mtx);
1690 #endif /* FreeBSD */
1691
1692 #ifdef __NetBSD__
1693 pipe_free_kmem(cpipe);
1694 (void) lockmgr(&cpipe->pipe_lock, LK_DRAIN, NULL);
1695 pool_put(&pipe_pool, cpipe);
1696 #endif
1697 }
1698
1699 #ifdef __FreeBSD__
1700 /*ARGSUSED*/
1701 static int
1702 pipe_kqfilter(struct file *fp, struct knote *kn)
1703 {
1704 struct pipe *cpipe = (struct pipe *)kn->kn_fp->f_data;
1705
1706 switch (kn->kn_filter) {
1707 case EVFILT_READ:
1708 kn->kn_fop = &pipe_rfiltops;
1709 break;
1710 case EVFILT_WRITE:
1711 kn->kn_fop = &pipe_wfiltops;
1712 cpipe = cpipe->pipe_peer;
1713 break;
1714 default:
1715 return (1);
1716 }
1717 kn->kn_hook = (caddr_t)cpipe;
1718 SLIST_INSERT_HEAD(&cpipe->pipe_sel.si_note, kn, kn_selnext);
1719 return (0);
1720 }
1721
1722 static void
1723 filt_pipedetach(struct knote *kn)
1724 {
1725 struct pipe *cpipe = (struct pipe *)kn->kn_fp->f_data;
1726
1727 SLIST_REMOVE(&cpipe->pipe_sel.si_note, kn, knote, kn_selnext);
1728 }
1729
1730 /*ARGSUSED*/
1731 static int
1732 filt_piperead(struct knote *kn, long hint)
1733 {
1734 struct pipe *rpipe = (struct pipe *)kn->kn_fp->f_data;
1735 struct pipe *wpipe = rpipe->pipe_peer;
1736
1737 kn->kn_data = rpipe->pipe_buffer.cnt;
1738 if ((kn->kn_data == 0) && (rpipe->pipe_state & PIPE_DIRECTW))
1739 kn->kn_data = rpipe->pipe_map.cnt;
1740
1741 if ((rpipe->pipe_state & PIPE_EOF) ||
1742 (wpipe == NULL) || (wpipe->pipe_state & PIPE_EOF)) {
1743 kn->kn_flags |= EV_EOF;
1744 return (1);
1745 }
1746 return (kn->kn_data > 0);
1747 }
1748
1749 /*ARGSUSED*/
1750 static int
1751 filt_pipewrite(struct knote *kn, long hint)
1752 {
1753 struct pipe *rpipe = (struct pipe *)kn->kn_fp->f_data;
1754 struct pipe *wpipe = rpipe->pipe_peer;
1755
1756 if ((wpipe == NULL) || (wpipe->pipe_state & PIPE_EOF)) {
1757 kn->kn_data = 0;
1758 kn->kn_flags |= EV_EOF;
1759 return (1);
1760 }
1761 kn->kn_data = wpipe->pipe_buffer.size - wpipe->pipe_buffer.cnt;
1762 if (wpipe->pipe_state & PIPE_DIRECTW)
1763 kn->kn_data = 0;
1764
1765 return (kn->kn_data >= PIPE_BUF);
1766 }
1767 #endif /* FreeBSD */
1768
1769 #ifdef __NetBSD__
1770 static int
1771 pipe_fcntl(fp, cmd, data, p)
1772 struct file *fp;
1773 u_int cmd;
1774 caddr_t data;
1775 struct proc *p;
1776 {
1777 if (cmd == F_SETFL)
1778 return (0);
1779 else
1780 return (EOPNOTSUPP);
1781 }
1782
1783 /*
1784 * Handle pipe sysctls.
1785 */
1786 int
1787 sysctl_dopipe(name, namelen, oldp, oldlenp, newp, newlen)
1788 int *name;
1789 u_int namelen;
1790 void *oldp;
1791 size_t *oldlenp;
1792 void *newp;
1793 size_t newlen;
1794 {
1795 /* All sysctl names at this level are terminal. */
1796 if (namelen != 1)
1797 return (ENOTDIR); /* overloaded */
1798
1799 switch (name[0]) {
1800 case KERN_PIPE_MAXKVASZ:
1801 return (sysctl_int(oldp, oldlenp, newp, newlen, &maxpipekva));
1802 case KERN_PIPE_LIMITKVA:
1803 return (sysctl_int(oldp, oldlenp, newp, newlen, &limitpipekva));
1804 case KERN_PIPE_MAXBIGPIPES:
1805 return (sysctl_int(oldp, oldlenp, newp, newlen, &maxbigpipes));
1806 case KERN_PIPE_NBIGPIPES:
1807 return (sysctl_rdint(oldp, oldlenp, newp, nbigpipe));
1808 case KERN_PIPE_KVASIZE:
1809 return (sysctl_rdint(oldp, oldlenp, newp, amountpipekva));
1810 default:
1811 return (EOPNOTSUPP);
1812 }
1813 /* NOTREACHED */
1814 }
1815
1816 /*
1817 * Initialize pipe structs.
1818 */
1819 void
1820 pipe_init(void)
1821 {
1822 pool_init(&pipe_pool, sizeof(struct pipe), 0, 0, 0, "pipepl",
1823 0, NULL, NULL, M_PIPE);
1824 }
1825
1826 #endif /* __NetBSD __ */
1827