sys_pipe.c revision 1.5.2.4 1 /* $NetBSD: sys_pipe.c,v 1.5.2.4 2002/01/10 20:00:06 thorpej 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.5.2.4 2002/01/10 20:00:06 thorpej 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 #endif /* FreeBSD */
129
130 static void filt_pipedetach(struct knote *kn);
131 static int filt_piperead(struct knote *kn, long hint);
132 static int filt_pipewrite(struct knote *kn, long hint);
133
134 static const struct filterops pipe_rfiltops =
135 { 1, NULL, filt_pipedetach, filt_piperead };
136 static const struct filterops pipe_wfiltops =
137 { 1, NULL, filt_pipedetach, filt_pipewrite };
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_kqfilter __P((struct file *fp, struct knote *kn));
149 static int pipe_stat __P((struct file *fp, struct stat *sb, struct proc *p));
150 static int pipe_ioctl __P((struct file *fp, u_long cmd, caddr_t data, struct proc *p));
151
152 static struct fileops pipeops =
153 { pipe_read, pipe_write, pipe_ioctl, pipe_fcntl, pipe_poll,
154 pipe_stat, pipe_close, pipe_kqfilter };
155 #endif /* NetBSD */
156
157 /*
158 * Default pipe buffer size(s), this can be kind-of large now because pipe
159 * space is pageable. The pipe code will try to maintain locality of
160 * reference for performance reasons, so small amounts of outstanding I/O
161 * will not wipe the cache.
162 */
163 #define MINPIPESIZE (PIPE_SIZE/3)
164 #define MAXPIPESIZE (2*PIPE_SIZE/3)
165
166 /*
167 * Maximum amount of kva for pipes -- this is kind-of a soft limit, but
168 * is there so that on large systems, we don't exhaust it.
169 */
170 #define MAXPIPEKVA (8*1024*1024)
171 static int maxpipekva = MAXPIPEKVA;
172
173 /*
174 * Limit for direct transfers, we cannot, of course limit
175 * the amount of kva for pipes in general though.
176 */
177 #define LIMITPIPEKVA (16*1024*1024)
178 static int limitpipekva = LIMITPIPEKVA;
179
180 /*
181 * Limit the number of "big" pipes
182 */
183 #define LIMITBIGPIPES 32
184 static int maxbigpipes = LIMITBIGPIPES;
185 static int nbigpipe = 0;
186
187 /*
188 * Amount of KVA consumed by pipe buffers.
189 */
190 static int amountpipekva = 0;
191
192 static void pipeclose __P((struct pipe *));
193 static void pipe_free_kmem __P((struct pipe *));
194 static int pipe_create __P((struct pipe **, int));
195 static __inline int pipelock __P((struct pipe *, int));
196 static __inline void pipeunlock __P((struct pipe *));
197 static __inline void pipeselwakeup __P((struct pipe *, struct pipe *));
198 static int pipespace __P((struct pipe *, int));
199
200 #ifdef __FreeBSD__
201 #ifndef PIPE_NODIRECT
202 static int pipe_build_write_buffer __P((struct pipe *wpipe, struct uio *uio));
203 static void pipe_destroy_write_buffer __P((struct pipe *wpipe));
204 static int pipe_direct_write __P((struct pipe *wpipe, struct uio *uio));
205 static void pipe_clone_write_buffer __P((struct pipe *wpipe));
206 #endif
207
208 static vm_zone_t pipe_zone;
209 #endif /* FreeBSD */
210
211 #ifdef __NetBSD__
212 #ifndef PIPE_NODIRECT
213 static int pipe_direct_write __P((struct pipe *, struct uio *));
214 static int pipe_loan_alloc __P((struct pipe *, int));
215 static void pipe_loan_free __P((struct pipe *));
216 #endif /* PIPE_NODIRECT */
217
218 static struct pool pipe_pool;
219 #endif /* NetBSD */
220
221 /*
222 * The pipe system call for the DTYPE_PIPE type of pipes
223 */
224
225 /* ARGSUSED */
226 #ifdef __FreeBSD__
227 int
228 pipe(p, uap)
229 struct proc *p;
230 struct pipe_args /* {
231 int dummy;
232 } */ *uap;
233 #elif defined(__NetBSD__)
234 int
235 sys_pipe(p, v, retval)
236 struct proc *p;
237 void *v;
238 register_t *retval;
239 #endif
240 {
241 struct file *rf, *wf;
242 struct pipe *rpipe, *wpipe;
243 int fd, error;
244
245 #ifdef __FreeBSD__
246 if (pipe_zone == NULL)
247 pipe_zone = zinit("PIPE", sizeof(struct pipe), 0, 0, 4);
248
249 rpipe = wpipe = NULL;
250 if (pipe_create(&rpipe, 1) || pipe_create(&wpipe, 1)) {
251 pipeclose(rpipe);
252 pipeclose(wpipe);
253 return (ENFILE);
254 }
255
256 error = falloc(p, &rf, &fd);
257 if (error) {
258 pipeclose(rpipe);
259 pipeclose(wpipe);
260 return (error);
261 }
262 fhold(rf);
263 p->p_retval[0] = fd;
264
265 /*
266 * Warning: once we've gotten past allocation of the fd for the
267 * read-side, we can only drop the read side via fdrop() in order
268 * to avoid races against processes which manage to dup() the read
269 * side while we are blocked trying to allocate the write side.
270 */
271 rf->f_flag = FREAD | FWRITE;
272 rf->f_type = DTYPE_PIPE;
273 rf->f_data = (caddr_t)rpipe;
274 rf->f_ops = &pipeops;
275 error = falloc(p, &wf, &fd);
276 if (error) {
277 struct filedesc *fdp = p->p_fd;
278
279 if (fdp->fd_ofiles[p->p_retval[0]] == rf) {
280 fdp->fd_ofiles[p->p_retval[0]] = NULL;
281 fdrop(rf, p);
282 }
283 fdrop(rf, p);
284 /* rpipe has been closed by fdrop(). */
285 pipeclose(wpipe);
286 return (error);
287 }
288 wf->f_flag = FREAD | FWRITE;
289 wf->f_type = DTYPE_PIPE;
290 wf->f_data = (caddr_t)wpipe;
291 wf->f_ops = &pipeops;
292 p->p_retval[1] = fd;
293
294 rpipe->pipe_peer = wpipe;
295 wpipe->pipe_peer = rpipe;
296 fdrop(rf, p);
297 #endif /* FreeBSD */
298
299 #ifdef __NetBSD__
300 rpipe = wpipe = NULL;
301 if (pipe_create(&rpipe, 1) || pipe_create(&wpipe, 0)) {
302 pipeclose(rpipe);
303 pipeclose(wpipe);
304 return (ENFILE);
305 }
306
307 /*
308 * Note: the file structure returned from falloc() is marked
309 * as 'larval' initially. Unless we mark it as 'mature' by
310 * FILE_SET_MATURE(), any attempt to do anything with it would
311 * return EBADF, including e.g. dup(2) or close(2). This avoids
312 * file descriptor races if we block in the second falloc().
313 */
314
315 error = falloc(p, &rf, &fd);
316 if (error)
317 goto free2;
318 retval[0] = fd;
319 rf->f_flag = FREAD;
320 rf->f_type = DTYPE_PIPE;
321 rf->f_data = (caddr_t)rpipe;
322 rf->f_ops = &pipeops;
323
324 error = falloc(p, &wf, &fd);
325 if (error)
326 goto free3;
327 retval[1] = fd;
328 wf->f_flag = FWRITE;
329 wf->f_type = DTYPE_PIPE;
330 wf->f_data = (caddr_t)wpipe;
331 wf->f_ops = &pipeops;
332
333 rpipe->pipe_peer = wpipe;
334 wpipe->pipe_peer = rpipe;
335
336 FILE_SET_MATURE(rf);
337 FILE_SET_MATURE(wf);
338 FILE_UNUSE(rf, p);
339 FILE_UNUSE(wf, p);
340 return (0);
341 free3:
342 FILE_UNUSE(rf, p);
343 ffree(rf);
344 fdremove(p->p_fd, retval[0]);
345 free2:
346 pipeclose(wpipe);
347 pipeclose(rpipe);
348 #endif /* NetBSD */
349
350 return (error);
351 }
352
353 /*
354 * Allocate kva for pipe circular buffer, the space is pageable
355 * This routine will 'realloc' the size of a pipe safely, if it fails
356 * it will retain the old buffer.
357 * If it fails it will return ENOMEM.
358 */
359 static int
360 pipespace(cpipe, size)
361 struct pipe *cpipe;
362 int size;
363 {
364 caddr_t buffer;
365 #ifdef __FreeBSD__
366 struct vm_object *object;
367 int npages, error;
368
369 npages = round_page(size)/PAGE_SIZE;
370 /*
371 * Create an object, I don't like the idea of paging to/from
372 * kernel_object.
373 */
374 mtx_lock(&vm_mtx);
375 object = vm_object_allocate(OBJT_DEFAULT, npages);
376 buffer = (caddr_t) vm_map_min(kernel_map);
377
378 /*
379 * Insert the object into the kernel map, and allocate kva for it.
380 * The map entry is, by default, pageable.
381 */
382 error = vm_map_find(kernel_map, object, 0,
383 (vm_offset_t *) &buffer, size, 1,
384 VM_PROT_ALL, VM_PROT_ALL, 0);
385
386 if (error != KERN_SUCCESS) {
387 vm_object_deallocate(object);
388 mtx_unlock(&vm_mtx);
389 return (ENOMEM);
390 }
391 #endif /* FreeBSD */
392
393 #ifdef __NetBSD__
394 /*
395 * Allocate pageable virtual address space. Physical memory is allocated
396 * on demand.
397 */
398 buffer = (caddr_t) uvm_km_valloc(kernel_map, round_page(size));
399 if (buffer == NULL)
400 return (ENOMEM);
401 #endif /* NetBSD */
402
403 /* free old resources if we're resizing */
404 pipe_free_kmem(cpipe);
405 #ifdef __FreeBSD__
406 mtx_unlock(&vm_mtx);
407 cpipe->pipe_buffer.object = object;
408 #endif
409 cpipe->pipe_buffer.buffer = buffer;
410 cpipe->pipe_buffer.size = size;
411 cpipe->pipe_buffer.in = 0;
412 cpipe->pipe_buffer.out = 0;
413 cpipe->pipe_buffer.cnt = 0;
414 amountpipekva += cpipe->pipe_buffer.size;
415 return (0);
416 }
417
418 /*
419 * initialize and allocate VM and memory for pipe
420 */
421 static int
422 pipe_create(cpipep, allockva)
423 struct pipe **cpipep;
424 int allockva;
425 {
426 struct pipe *cpipe;
427 int error;
428
429 #ifdef __FreeBSD__
430 *cpipep = zalloc(pipe_zone);
431 #endif
432 #ifdef __NetBSD__
433 *cpipep = pool_get(&pipe_pool, M_WAITOK);
434 #endif
435 if (*cpipep == NULL)
436 return (ENOMEM);
437
438 cpipe = *cpipep;
439
440 /* Initialize */
441 memset(cpipe, 0, sizeof(*cpipe));
442 cpipe->pipe_state = PIPE_SIGNALR;
443
444 if (allockva && (error = pipespace(cpipe, PIPE_SIZE)))
445 return (error);
446
447 vfs_timestamp(&cpipe->pipe_ctime);
448 cpipe->pipe_atime = cpipe->pipe_ctime;
449 cpipe->pipe_mtime = cpipe->pipe_ctime;
450 #ifdef __NetBSD__
451 cpipe->pipe_pgid = NO_PID;
452 lockinit(&cpipe->pipe_lock, PRIBIO | PCATCH, "pipelk", 0, 0);
453 #endif
454
455 return (0);
456 }
457
458
459 /*
460 * lock a pipe for I/O, blocking other access
461 */
462 static __inline int
463 pipelock(cpipe, catch)
464 struct pipe *cpipe;
465 int catch;
466 {
467 int error;
468
469 #ifdef __FreeBSD__
470 while (cpipe->pipe_state & PIPE_LOCK) {
471 cpipe->pipe_state |= PIPE_LWANT;
472 error = tsleep(cpipe, catch ? (PRIBIO | PCATCH) : PRIBIO,
473 "pipelk", 0);
474 if (error != 0)
475 return (error);
476 }
477 cpipe->pipe_state |= PIPE_LOCK;
478 return (0);
479 #endif
480
481 #ifdef __NetBSD__
482 do {
483 error = lockmgr(&cpipe->pipe_lock, LK_EXCLUSIVE, NULL);
484 } while (!catch && (error == EINTR || error == ERESTART));
485 return (error);
486 #endif
487 }
488
489 /*
490 * unlock a pipe I/O lock
491 */
492 static __inline void
493 pipeunlock(cpipe)
494 struct pipe *cpipe;
495 {
496 #ifdef __FreeBSD__
497 cpipe->pipe_state &= ~PIPE_LOCK;
498 if (cpipe->pipe_state & PIPE_LWANT) {
499 cpipe->pipe_state &= ~PIPE_LWANT;
500 wakeup(cpipe);
501 }
502 #endif
503
504 #ifdef __NetBSD__
505 lockmgr(&cpipe->pipe_lock, LK_RELEASE, NULL);
506 #endif
507 }
508
509 /*
510 * Select/poll wakup. This also sends SIGIO to peer connected to
511 * 'sigpipe' side of pipe.
512 */
513 static __inline void
514 pipeselwakeup(selp, sigp)
515 struct pipe *selp, *sigp;
516 {
517
518 #ifdef __FreeBSD__
519 if (selp->pipe_state & PIPE_SEL) {
520 selp->pipe_state &= ~PIPE_SEL;
521 selwakeup(&selp->pipe_sel);
522 }
523 if (sigp && (sigp->pipe_state & PIPE_ASYNC) && sigp->pipe_sigio)
524 pgsigio(sigp->pipe_sigio, SIGIO, 0);
525 KNOTE(&selp->pipe_sel.si_note, 0);
526 #endif
527
528 #ifdef __NetBSD__
529 selnotify(&selp->pipe_sel, 0);
530 if (sigp && (sigp->pipe_state & PIPE_ASYNC) &&
531 sigp->pipe_pgid != NO_PID) {
532 struct proc *p;
533
534 if (sigp->pipe_pgid < 0)
535 gsignal(-sigp->pipe_pgid, SIGIO);
536 else if (sigp->pipe_pgid > 0 &&
537 (p = pfind(sigp->pipe_pgid)) != NULL)
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 #ifdef __FreeBSD__
1543 rpipe->pipe_state |= PIPE_SEL;
1544 #endif
1545 }
1546
1547 if (events & (POLLOUT | POLLWRNORM)) {
1548 selrecord(p, &wpipe->pipe_sel);
1549 #ifdef __FreeBSD__
1550 wpipe->pipe_state |= PIPE_SEL;
1551 #endif
1552 }
1553 }
1554
1555 return (revents);
1556 }
1557
1558 static int
1559 pipe_stat(fp, ub, p)
1560 struct file *fp;
1561 struct stat *ub;
1562 struct proc *p;
1563 {
1564 struct pipe *pipe = (struct pipe *)fp->f_data;
1565
1566 memset((caddr_t)ub, 0, sizeof(*ub));
1567 ub->st_mode = S_IFIFO;
1568 ub->st_blksize = pipe->pipe_buffer.size;
1569 ub->st_size = pipe->pipe_buffer.cnt;
1570 ub->st_blocks = (ub->st_size) ? 1 : 0;
1571 #ifdef __FreeBSD__
1572 ub->st_atimespec = pipe->pipe_atime;
1573 ub->st_mtimespec = pipe->pipe_mtime;
1574 ub->st_ctimespec = pipe->pipe_ctime;
1575 #endif /* FreeBSD */
1576 #ifdef __NetBSD__
1577 TIMEVAL_TO_TIMESPEC(&pipe->pipe_atime, &ub->st_atimespec)
1578 TIMEVAL_TO_TIMESPEC(&pipe->pipe_mtime, &ub->st_mtimespec);
1579 TIMEVAL_TO_TIMESPEC(&pipe->pipe_ctime, &ub->st_ctimespec);
1580 #endif /* NetBSD */
1581 ub->st_uid = fp->f_cred->cr_uid;
1582 ub->st_gid = fp->f_cred->cr_gid;
1583 /*
1584 * Left as 0: st_dev, st_ino, st_nlink, st_rdev, st_flags, st_gen.
1585 * XXX (st_dev, st_ino) should be unique.
1586 */
1587 return (0);
1588 }
1589
1590 /* ARGSUSED */
1591 static int
1592 pipe_close(fp, p)
1593 struct file *fp;
1594 struct proc *p;
1595 {
1596 struct pipe *cpipe = (struct pipe *)fp->f_data;
1597
1598 #ifdef __FreeBSD__
1599 fp->f_ops = &badfileops;
1600 funsetown(cpipe->pipe_sigio);
1601 #endif
1602 fp->f_data = NULL;
1603 pipeclose(cpipe);
1604 return (0);
1605 }
1606
1607 static void
1608 pipe_free_kmem(cpipe)
1609 struct pipe *cpipe;
1610 {
1611
1612 #ifdef __FreeBSD__
1613 mtx_assert(&vm_mtx, MA_OWNED);
1614 #endif
1615 if (cpipe->pipe_buffer.buffer != NULL) {
1616 if (cpipe->pipe_buffer.size > PIPE_SIZE)
1617 --nbigpipe;
1618 amountpipekva -= cpipe->pipe_buffer.size;
1619 #ifdef __FreeBSD__
1620 kmem_free(kernel_map,
1621 (vm_offset_t)cpipe->pipe_buffer.buffer,
1622 cpipe->pipe_buffer.size);
1623 #elif defined(__NetBSD__)
1624 uvm_km_free(kernel_map,
1625 (vaddr_t)cpipe->pipe_buffer.buffer,
1626 cpipe->pipe_buffer.size);
1627 #endif /* NetBSD */
1628 cpipe->pipe_buffer.buffer = NULL;
1629 }
1630 #ifndef PIPE_NODIRECT
1631 if (cpipe->pipe_map.kva != NULL) {
1632 #ifdef __FreeBSD__
1633 amountpipekva -= cpipe->pipe_buffer.size + PAGE_SIZE;
1634 kmem_free(kernel_map,
1635 cpipe->pipe_map.kva,
1636 cpipe->pipe_buffer.size + PAGE_SIZE);
1637 #elif defined(__NetBSD__)
1638 pipe_loan_free(cpipe);
1639 #endif /* NetBSD */
1640 cpipe->pipe_map.cnt = 0;
1641 cpipe->pipe_map.kva = NULL;
1642 cpipe->pipe_map.pos = 0;
1643 cpipe->pipe_map.npages = 0;
1644 }
1645 #endif /* !PIPE_NODIRECT */
1646 }
1647
1648 /*
1649 * shutdown the pipe
1650 */
1651 static void
1652 pipeclose(cpipe)
1653 struct pipe *cpipe;
1654 {
1655 struct pipe *ppipe;
1656
1657 if (!cpipe)
1658 return;
1659
1660 pipeselwakeup(cpipe, cpipe);
1661
1662 /*
1663 * If the other side is blocked, wake it up saying that
1664 * we want to close it down.
1665 */
1666 while (cpipe->pipe_busy) {
1667 wakeup(cpipe);
1668 cpipe->pipe_state |= PIPE_WANTCLOSE | PIPE_EOF;
1669 tsleep(cpipe, PRIBIO, "pipecl", 0);
1670 }
1671
1672 /*
1673 * Disconnect from peer
1674 */
1675 if ((ppipe = cpipe->pipe_peer) != NULL) {
1676 pipeselwakeup(ppipe, ppipe);
1677
1678 ppipe->pipe_state |= PIPE_EOF;
1679 wakeup(ppipe);
1680 ppipe->pipe_peer = NULL;
1681 }
1682
1683 /*
1684 * free resources
1685 */
1686 #ifdef __FreeBSD__
1687 mtx_lock(&vm_mtx);
1688 pipe_free_kmem(cpipe);
1689 /* XXX: erm, doesn't zalloc already have its own locks and
1690 * not need the giant vm lock?
1691 */
1692 zfree(pipe_zone, cpipe);
1693 mtx_unlock(&vm_mtx);
1694 #endif /* FreeBSD */
1695
1696 #ifdef __NetBSD__
1697 pipe_free_kmem(cpipe);
1698 (void) lockmgr(&cpipe->pipe_lock, LK_DRAIN, NULL);
1699 pool_put(&pipe_pool, cpipe);
1700 #endif
1701 }
1702
1703 /*ARGSUSED*/
1704 static int
1705 pipe_kqfilter(struct file *fp, struct knote *kn)
1706 {
1707 struct pipe *cpipe = (struct pipe *)kn->kn_fp->f_data;
1708
1709 switch (kn->kn_filter) {
1710 case EVFILT_READ:
1711 kn->kn_fop = &pipe_rfiltops;
1712 break;
1713 case EVFILT_WRITE:
1714 kn->kn_fop = &pipe_wfiltops;
1715 cpipe = cpipe->pipe_peer;
1716 break;
1717 default:
1718 return (1);
1719 }
1720 kn->kn_hook = (caddr_t)cpipe;
1721
1722 #ifdef __FreeBSD__
1723 SLIST_INSERT_HEAD(&cpipe->pipe_sel.si_note, kn, kn_selnext);
1724 #else
1725 SLIST_INSERT_HEAD(&cpipe->pipe_sel.si_klist, kn, kn_selnext);
1726 #endif /* __FreeBSD__ */
1727 return (0);
1728 }
1729
1730 static void
1731 filt_pipedetach(struct knote *kn)
1732 {
1733 struct pipe *cpipe = (struct pipe *)kn->kn_fp->f_data;
1734
1735 #ifdef __FreeBSD__
1736 SLIST_REMOVE(&cpipe->pipe_sel.si_note, kn, knote, kn_selnext);
1737 #else
1738 SLIST_REMOVE(&cpipe->pipe_sel.si_klist, kn, knote, kn_selnext);
1739 #endif /* __FreeBSD__ */
1740 }
1741
1742 /*ARGSUSED*/
1743 static int
1744 filt_piperead(struct knote *kn, long hint)
1745 {
1746 struct pipe *rpipe = (struct pipe *)kn->kn_fp->f_data;
1747 struct pipe *wpipe = rpipe->pipe_peer;
1748
1749 kn->kn_data = rpipe->pipe_buffer.cnt;
1750 if ((kn->kn_data == 0) && (rpipe->pipe_state & PIPE_DIRECTW))
1751 kn->kn_data = rpipe->pipe_map.cnt;
1752
1753 if ((rpipe->pipe_state & PIPE_EOF) ||
1754 (wpipe == NULL) || (wpipe->pipe_state & PIPE_EOF)) {
1755 kn->kn_flags |= EV_EOF;
1756 return (1);
1757 }
1758 return (kn->kn_data > 0);
1759 }
1760
1761 /*ARGSUSED*/
1762 static int
1763 filt_pipewrite(struct knote *kn, long hint)
1764 {
1765 struct pipe *rpipe = (struct pipe *)kn->kn_fp->f_data;
1766 struct pipe *wpipe = rpipe->pipe_peer;
1767
1768 if ((wpipe == NULL) || (wpipe->pipe_state & PIPE_EOF)) {
1769 kn->kn_data = 0;
1770 kn->kn_flags |= EV_EOF;
1771 return (1);
1772 }
1773 kn->kn_data = wpipe->pipe_buffer.size - wpipe->pipe_buffer.cnt;
1774 if (wpipe->pipe_state & PIPE_DIRECTW)
1775 kn->kn_data = 0;
1776
1777 return (kn->kn_data >= PIPE_BUF);
1778 }
1779
1780 #ifdef __NetBSD__
1781 static int
1782 pipe_fcntl(fp, cmd, data, p)
1783 struct file *fp;
1784 u_int cmd;
1785 caddr_t data;
1786 struct proc *p;
1787 {
1788 if (cmd == F_SETFL)
1789 return (0);
1790 else
1791 return (EOPNOTSUPP);
1792 }
1793
1794 /*
1795 * Handle pipe sysctls.
1796 */
1797 int
1798 sysctl_dopipe(name, namelen, oldp, oldlenp, newp, newlen)
1799 int *name;
1800 u_int namelen;
1801 void *oldp;
1802 size_t *oldlenp;
1803 void *newp;
1804 size_t newlen;
1805 {
1806 /* All sysctl names at this level are terminal. */
1807 if (namelen != 1)
1808 return (ENOTDIR); /* overloaded */
1809
1810 switch (name[0]) {
1811 case KERN_PIPE_MAXKVASZ:
1812 return (sysctl_int(oldp, oldlenp, newp, newlen, &maxpipekva));
1813 case KERN_PIPE_LIMITKVA:
1814 return (sysctl_int(oldp, oldlenp, newp, newlen, &limitpipekva));
1815 case KERN_PIPE_MAXBIGPIPES:
1816 return (sysctl_int(oldp, oldlenp, newp, newlen, &maxbigpipes));
1817 case KERN_PIPE_NBIGPIPES:
1818 return (sysctl_rdint(oldp, oldlenp, newp, nbigpipe));
1819 case KERN_PIPE_KVASIZE:
1820 return (sysctl_rdint(oldp, oldlenp, newp, amountpipekva));
1821 default:
1822 return (EOPNOTSUPP);
1823 }
1824 /* NOTREACHED */
1825 }
1826
1827 /*
1828 * Initialize pipe structs.
1829 */
1830 void
1831 pipe_init(void)
1832 {
1833 pool_init(&pipe_pool, sizeof(struct pipe), 0, 0, 0, "pipepl",
1834 0, NULL, NULL, M_PIPE);
1835 }
1836
1837 #endif /* __NetBSD __ */
1838