FreeBSD/Linux Kernel Cross Reference
sys/kern/sys_pipe.c
1 /*-
2 * Copyright (c) 1996 John S. Dyson
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice immediately at the beginning of the file, without modification,
10 * this list of conditions, and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Absolutely no warranty of function or purpose is made by the author
15 * John S. Dyson.
16 * 4. Modifications may be freely made to this file if the above conditions
17 * are met.
18 */
19
20 /*
21 * This file contains a high-performance replacement for the socket-based
22 * pipes scheme originally used in FreeBSD/4.4Lite. It does not support
23 * all features of sockets, but does do everything that pipes normally
24 * do.
25 */
26
27 /*
28 * This code has two modes of operation, a small write mode and a large
29 * write mode. The small write mode acts like conventional pipes with
30 * a kernel buffer. If the buffer is less than PIPE_MINDIRECT, then the
31 * "normal" pipe buffering is done. If the buffer is between PIPE_MINDIRECT
32 * and PIPE_SIZE in size, it is fully mapped and wired into the kernel, and
33 * the receiving process can copy it directly from the pages in the sending
34 * process.
35 *
36 * If the sending process receives a signal, it is possible that it will
37 * go away, and certainly its address space can change, because control
38 * is returned back to the user-mode side. In that case, the pipe code
39 * arranges to copy the buffer supplied by the user process, to a pageable
40 * kernel buffer, and the receiving process will grab the data from the
41 * pageable kernel buffer. Since signals don't happen all that often,
42 * the copy operation is normally eliminated.
43 *
44 * The constant PIPE_MINDIRECT is chosen to make sure that buffering will
45 * happen for small transfers so that the system will not spend all of
46 * its time context switching.
47 *
48 * In order to limit the resource use of pipes, two sysctls exist:
49 *
50 * kern.ipc.maxpipekva - This is a hard limit on the amount of pageable
51 * address space available to us in pipe_map. This value is normally
52 * autotuned, but may also be loader tuned.
53 *
54 * kern.ipc.pipekva - This read-only sysctl tracks the current amount of
55 * memory in use by pipes.
56 *
57 * Based on how large pipekva is relative to maxpipekva, the following
58 * will happen:
59 *
60 * 0% - 50%:
61 * New pipes are given 16K of memory backing, pipes may dynamically
62 * grow to as large as 64K where needed.
63 * 50% - 75%:
64 * New pipes are given 4K (or PAGE_SIZE) of memory backing,
65 * existing pipes may NOT grow.
66 * 75% - 100%:
67 * New pipes are given 4K (or PAGE_SIZE) of memory backing,
68 * existing pipes will be shrunk down to 4K whenever possible.
69 *
70 * Resizing may be disabled by setting kern.ipc.piperesizeallowed=0. If
71 * that is set, the only resize that will occur is the 0 -> SMALL_PIPE_SIZE
72 * resize which MUST occur for reverse-direction pipes when they are
73 * first used.
74 *
75 * Additional information about the current state of pipes may be obtained
76 * from kern.ipc.pipes, kern.ipc.pipefragretry, kern.ipc.pipeallocfail,
77 * and kern.ipc.piperesizefail.
78 *
79 * Locking rules: There are two locks present here: A mutex, used via
80 * PIPE_LOCK, and a flag, used via pipelock(). All locking is done via
81 * the flag, as mutexes can not persist over uiomove. The mutex
82 * exists only to guard access to the flag, and is not in itself a
83 * locking mechanism. Also note that there is only a single mutex for
84 * both directions of a pipe.
85 *
86 * As pipelock() may have to sleep before it can acquire the flag, it
87 * is important to reread all data after a call to pipelock(); everything
88 * in the structure may have changed.
89 */
90
91 #include <sys/cdefs.h>
92 __FBSDID("$FreeBSD$");
93
94 #include "opt_mac.h"
95
96 #include <sys/param.h>
97 #include <sys/systm.h>
98 #include <sys/fcntl.h>
99 #include <sys/file.h>
100 #include <sys/filedesc.h>
101 #include <sys/filio.h>
102 #include <sys/kernel.h>
103 #include <sys/lock.h>
104 #include <sys/mac.h>
105 #include <sys/mutex.h>
106 #include <sys/ttycom.h>
107 #include <sys/stat.h>
108 #include <sys/malloc.h>
109 #include <sys/poll.h>
110 #include <sys/selinfo.h>
111 #include <sys/signalvar.h>
112 #include <sys/sysctl.h>
113 #include <sys/sysproto.h>
114 #include <sys/pipe.h>
115 #include <sys/proc.h>
116 #include <sys/vnode.h>
117 #include <sys/uio.h>
118 #include <sys/event.h>
119
120 #include <vm/vm.h>
121 #include <vm/vm_param.h>
122 #include <vm/vm_object.h>
123 #include <vm/vm_kern.h>
124 #include <vm/vm_extern.h>
125 #include <vm/pmap.h>
126 #include <vm/vm_map.h>
127 #include <vm/vm_page.h>
128 #include <vm/uma.h>
129
130 /*
131 * Use this define if you want to disable *fancy* VM things. Expect an
132 * approx 30% decrease in transfer rate. This could be useful for
133 * NetBSD or OpenBSD.
134 */
135 /* #define PIPE_NODIRECT */
136
137 /*
138 * interfaces to the outside world
139 */
140 static fo_rdwr_t pipe_read;
141 static fo_rdwr_t pipe_write;
142 static fo_ioctl_t pipe_ioctl;
143 static fo_poll_t pipe_poll;
144 static fo_kqfilter_t pipe_kqfilter;
145 static fo_stat_t pipe_stat;
146 static fo_close_t pipe_close;
147
148 static struct fileops pipeops = {
149 .fo_read = pipe_read,
150 .fo_write = pipe_write,
151 .fo_ioctl = pipe_ioctl,
152 .fo_poll = pipe_poll,
153 .fo_kqfilter = pipe_kqfilter,
154 .fo_stat = pipe_stat,
155 .fo_close = pipe_close,
156 .fo_flags = DFLAG_PASSABLE
157 };
158
159 static void filt_pipedetach(struct knote *kn);
160 static int filt_piperead(struct knote *kn, long hint);
161 static int filt_pipewrite(struct knote *kn, long hint);
162
163 static struct filterops pipe_rfiltops =
164 { 1, NULL, filt_pipedetach, filt_piperead };
165 static struct filterops pipe_wfiltops =
166 { 1, NULL, filt_pipedetach, filt_pipewrite };
167
168 /*
169 * Default pipe buffer size(s), this can be kind-of large now because pipe
170 * space is pageable. The pipe code will try to maintain locality of
171 * reference for performance reasons, so small amounts of outstanding I/O
172 * will not wipe the cache.
173 */
174 #define MINPIPESIZE (PIPE_SIZE/3)
175 #define MAXPIPESIZE (2*PIPE_SIZE/3)
176
177 static int amountpipes;
178 static int amountpipekva;
179 static int pipefragretry;
180 static int pipeallocfail;
181 static int piperesizefail;
182 static int piperesizeallowed = 1;
183
184 SYSCTL_DECL(_kern_ipc);
185
186 SYSCTL_INT(_kern_ipc, OID_AUTO, maxpipekva, CTLFLAG_RDTUN,
187 &maxpipekva, 0, "Pipe KVA limit");
188 SYSCTL_INT(_kern_ipc, OID_AUTO, pipes, CTLFLAG_RD,
189 &amountpipes, 0, "Current # of pipes");
190 SYSCTL_INT(_kern_ipc, OID_AUTO, pipekva, CTLFLAG_RD,
191 &amountpipekva, 0, "Pipe KVA usage");
192 SYSCTL_INT(_kern_ipc, OID_AUTO, pipefragretry, CTLFLAG_RD,
193 &pipefragretry, 0, "Pipe allocation retries due to fragmentation");
194 SYSCTL_INT(_kern_ipc, OID_AUTO, pipeallocfail, CTLFLAG_RD,
195 &pipeallocfail, 0, "Pipe allocation failures");
196 SYSCTL_INT(_kern_ipc, OID_AUTO, piperesizefail, CTLFLAG_RD,
197 &piperesizefail, 0, "Pipe resize failures");
198 SYSCTL_INT(_kern_ipc, OID_AUTO, piperesizeallowed, CTLFLAG_RW,
199 &piperesizeallowed, 0, "Pipe resizing allowed");
200
201 static void pipeinit(void *dummy __unused);
202 static void pipeclose(struct pipe *cpipe);
203 static void pipe_free_kmem(struct pipe *cpipe);
204 static int pipe_create(struct pipe *pipe, int backing);
205 static __inline int pipelock(struct pipe *cpipe, int catch);
206 static __inline void pipeunlock(struct pipe *cpipe);
207 static __inline void pipeselwakeup(struct pipe *cpipe);
208 #ifndef PIPE_NODIRECT
209 static int pipe_build_write_buffer(struct pipe *wpipe, struct uio *uio);
210 static void pipe_destroy_write_buffer(struct pipe *wpipe);
211 static int pipe_direct_write(struct pipe *wpipe, struct uio *uio);
212 static void pipe_clone_write_buffer(struct pipe *wpipe);
213 #endif
214 static int pipespace(struct pipe *cpipe, int size);
215 static int pipespace_new(struct pipe *cpipe, int size);
216
217 static int pipe_zone_ctor(void *mem, int size, void *arg, int flags);
218 static void pipe_zone_dtor(void *mem, int size, void *arg);
219 static int pipe_zone_init(void *mem, int size, int flags);
220 static void pipe_zone_fini(void *mem, int size);
221
222 static uma_zone_t pipe_zone;
223
224 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_ANY, pipeinit, NULL);
225
226 static void
227 pipeinit(void *dummy __unused)
228 {
229
230 pipe_zone = uma_zcreate("PIPE", sizeof(struct pipepair),
231 pipe_zone_ctor, pipe_zone_dtor, pipe_zone_init, pipe_zone_fini,
232 UMA_ALIGN_PTR, 0);
233 KASSERT(pipe_zone != NULL, ("pipe_zone not initialized"));
234 }
235
236 static int
237 pipe_zone_ctor(void *mem, int size, void *arg, int flags)
238 {
239 struct pipepair *pp;
240 struct pipe *rpipe, *wpipe;
241
242 KASSERT(size == sizeof(*pp), ("pipe_zone_ctor: wrong size"));
243
244 pp = (struct pipepair *)mem;
245
246 /*
247 * We zero both pipe endpoints to make sure all the kmem pointers
248 * are NULL, flag fields are zero'd, etc. We timestamp both
249 * endpoints with the same time.
250 */
251 rpipe = &pp->pp_rpipe;
252 bzero(rpipe, sizeof(*rpipe));
253 vfs_timestamp(&rpipe->pipe_ctime);
254 rpipe->pipe_atime = rpipe->pipe_mtime = rpipe->pipe_ctime;
255
256 wpipe = &pp->pp_wpipe;
257 bzero(wpipe, sizeof(*wpipe));
258 wpipe->pipe_ctime = rpipe->pipe_ctime;
259 wpipe->pipe_atime = wpipe->pipe_mtime = rpipe->pipe_ctime;
260
261 rpipe->pipe_peer = wpipe;
262 rpipe->pipe_pair = pp;
263 wpipe->pipe_peer = rpipe;
264 wpipe->pipe_pair = pp;
265
266 /*
267 * Mark both endpoints as present; they will later get free'd
268 * one at a time. When both are free'd, then the whole pair
269 * is released.
270 */
271 rpipe->pipe_present = PIPE_ACTIVE;
272 wpipe->pipe_present = PIPE_ACTIVE;
273
274 /*
275 * Eventually, the MAC Framework may initialize the label
276 * in ctor or init, but for now we do it elswhere to avoid
277 * blocking in ctor or init.
278 */
279 pp->pp_label = NULL;
280
281 atomic_add_int(&amountpipes, 2);
282 return (0);
283 }
284
285 static void
286 pipe_zone_dtor(void *mem, int size, void *arg)
287 {
288 struct pipepair *pp;
289
290 KASSERT(size == sizeof(*pp), ("pipe_zone_dtor: wrong size"));
291
292 pp = (struct pipepair *)mem;
293
294 atomic_subtract_int(&amountpipes, 2);
295 }
296
297 static int
298 pipe_zone_init(void *mem, int size, int flags)
299 {
300 struct pipepair *pp;
301
302 KASSERT(size == sizeof(*pp), ("pipe_zone_init: wrong size"));
303
304 pp = (struct pipepair *)mem;
305
306 mtx_init(&pp->pp_mtx, "pipe mutex", NULL, MTX_DEF | MTX_RECURSE);
307 return (0);
308 }
309
310 static void
311 pipe_zone_fini(void *mem, int size)
312 {
313 struct pipepair *pp;
314
315 KASSERT(size == sizeof(*pp), ("pipe_zone_fini: wrong size"));
316
317 pp = (struct pipepair *)mem;
318
319 mtx_destroy(&pp->pp_mtx);
320 }
321
322 /*
323 * The pipe system call for the DTYPE_PIPE type of pipes. If we fail,
324 * let the zone pick up the pieces via pipeclose().
325 */
326
327 /* ARGSUSED */
328 int
329 pipe(td, uap)
330 struct thread *td;
331 struct pipe_args /* {
332 int dummy;
333 } */ *uap;
334 {
335 struct filedesc *fdp = td->td_proc->p_fd;
336 struct file *rf, *wf;
337 struct pipepair *pp;
338 struct pipe *rpipe, *wpipe;
339 int fd, error;
340
341 pp = uma_zalloc(pipe_zone, M_WAITOK);
342 #ifdef MAC
343 /*
344 * The MAC label is shared between the connected endpoints. As a
345 * result mac_init_pipe() and mac_create_pipe() are called once
346 * for the pair, and not on the endpoints.
347 */
348 mac_init_pipe(pp);
349 mac_create_pipe(td->td_ucred, pp);
350 #endif
351 rpipe = &pp->pp_rpipe;
352 wpipe = &pp->pp_wpipe;
353
354 knlist_init(&rpipe->pipe_sel.si_note, PIPE_MTX(rpipe), NULL, NULL,
355 NULL);
356 knlist_init(&wpipe->pipe_sel.si_note, PIPE_MTX(wpipe), NULL, NULL,
357 NULL);
358
359 /* Only the forward direction pipe is backed by default */
360 if ((error = pipe_create(rpipe, 1)) != 0 ||
361 (error = pipe_create(wpipe, 0)) != 0) {
362 pipeclose(rpipe);
363 pipeclose(wpipe);
364 return (error);
365 }
366
367 rpipe->pipe_state |= PIPE_DIRECTOK;
368 wpipe->pipe_state |= PIPE_DIRECTOK;
369
370 error = falloc(td, &rf, &fd);
371 if (error) {
372 pipeclose(rpipe);
373 pipeclose(wpipe);
374 return (error);
375 }
376 /* An extra reference on `rf' has been held for us by falloc(). */
377 td->td_retval[0] = fd;
378
379 /*
380 * Warning: once we've gotten past allocation of the fd for the
381 * read-side, we can only drop the read side via fdrop() in order
382 * to avoid races against processes which manage to dup() the read
383 * side while we are blocked trying to allocate the write side.
384 */
385 FILE_LOCK(rf);
386 rf->f_flag = FREAD | FWRITE;
387 rf->f_type = DTYPE_PIPE;
388 rf->f_data = rpipe;
389 rf->f_ops = &pipeops;
390 FILE_UNLOCK(rf);
391 error = falloc(td, &wf, &fd);
392 if (error) {
393 fdclose(fdp, rf, td->td_retval[0], td);
394 fdrop(rf, td);
395 /* rpipe has been closed by fdrop(). */
396 pipeclose(wpipe);
397 return (error);
398 }
399 /* An extra reference on `wf' has been held for us by falloc(). */
400 FILE_LOCK(wf);
401 wf->f_flag = FREAD | FWRITE;
402 wf->f_type = DTYPE_PIPE;
403 wf->f_data = wpipe;
404 wf->f_ops = &pipeops;
405 FILE_UNLOCK(wf);
406 fdrop(wf, td);
407 td->td_retval[1] = fd;
408 fdrop(rf, td);
409
410 return (0);
411 }
412
413 /*
414 * Allocate kva for pipe circular buffer, the space is pageable
415 * This routine will 'realloc' the size of a pipe safely, if it fails
416 * it will retain the old buffer.
417 * If it fails it will return ENOMEM.
418 */
419 static int
420 pipespace_new(cpipe, size)
421 struct pipe *cpipe;
422 int size;
423 {
424 caddr_t buffer;
425 int error, cnt, firstseg;
426 static int curfail = 0;
427 static struct timeval lastfail;
428
429 KASSERT(!mtx_owned(PIPE_MTX(cpipe)), ("pipespace: pipe mutex locked"));
430 KASSERT(!(cpipe->pipe_state & PIPE_DIRECTW),
431 ("pipespace: resize of direct writes not allowed"));
432 retry:
433 cnt = cpipe->pipe_buffer.cnt;
434 if (cnt > size)
435 size = cnt;
436
437 size = round_page(size);
438 buffer = (caddr_t) vm_map_min(pipe_map);
439
440 error = vm_map_find(pipe_map, NULL, 0,
441 (vm_offset_t *) &buffer, size, 1,
442 VM_PROT_ALL, VM_PROT_ALL, 0);
443 if (error != KERN_SUCCESS) {
444 if ((cpipe->pipe_buffer.buffer == NULL) &&
445 (size > SMALL_PIPE_SIZE)) {
446 size = SMALL_PIPE_SIZE;
447 pipefragretry++;
448 goto retry;
449 }
450 if (cpipe->pipe_buffer.buffer == NULL) {
451 pipeallocfail++;
452 if (ppsratecheck(&lastfail, &curfail, 1))
453 printf("kern.ipc.maxpipekva exceeded; see tuning(7)\n");
454 } else {
455 piperesizefail++;
456 }
457 return (ENOMEM);
458 }
459
460 /* copy data, then free old resources if we're resizing */
461 if (cnt > 0) {
462 if (cpipe->pipe_buffer.in <= cpipe->pipe_buffer.out) {
463 firstseg = cpipe->pipe_buffer.size - cpipe->pipe_buffer.out;
464 bcopy(&cpipe->pipe_buffer.buffer[cpipe->pipe_buffer.out],
465 buffer, firstseg);
466 if ((cnt - firstseg) > 0)
467 bcopy(cpipe->pipe_buffer.buffer, &buffer[firstseg],
468 cpipe->pipe_buffer.in);
469 } else {
470 bcopy(&cpipe->pipe_buffer.buffer[cpipe->pipe_buffer.out],
471 buffer, cnt);
472 }
473 }
474 pipe_free_kmem(cpipe);
475 cpipe->pipe_buffer.buffer = buffer;
476 cpipe->pipe_buffer.size = size;
477 cpipe->pipe_buffer.in = cnt;
478 cpipe->pipe_buffer.out = 0;
479 cpipe->pipe_buffer.cnt = cnt;
480 atomic_add_int(&amountpipekva, cpipe->pipe_buffer.size);
481 return (0);
482 }
483
484 /*
485 * Wrapper for pipespace_new() that performs locking assertions.
486 */
487 static int
488 pipespace(cpipe, size)
489 struct pipe *cpipe;
490 int size;
491 {
492
493 KASSERT(cpipe->pipe_state & PIPE_LOCKFL,
494 ("Unlocked pipe passed to pipespace"));
495 return (pipespace_new(cpipe, size));
496 }
497
498 /*
499 * lock a pipe for I/O, blocking other access
500 */
501 static __inline int
502 pipelock(cpipe, catch)
503 struct pipe *cpipe;
504 int catch;
505 {
506 int error;
507
508 PIPE_LOCK_ASSERT(cpipe, MA_OWNED);
509 while (cpipe->pipe_state & PIPE_LOCKFL) {
510 cpipe->pipe_state |= PIPE_LWANT;
511 error = msleep(cpipe, PIPE_MTX(cpipe),
512 catch ? (PRIBIO | PCATCH) : PRIBIO,
513 "pipelk", 0);
514 if (error != 0)
515 return (error);
516 }
517 cpipe->pipe_state |= PIPE_LOCKFL;
518 return (0);
519 }
520
521 /*
522 * unlock a pipe I/O lock
523 */
524 static __inline void
525 pipeunlock(cpipe)
526 struct pipe *cpipe;
527 {
528
529 PIPE_LOCK_ASSERT(cpipe, MA_OWNED);
530 KASSERT(cpipe->pipe_state & PIPE_LOCKFL,
531 ("Unlocked pipe passed to pipeunlock"));
532 cpipe->pipe_state &= ~PIPE_LOCKFL;
533 if (cpipe->pipe_state & PIPE_LWANT) {
534 cpipe->pipe_state &= ~PIPE_LWANT;
535 wakeup(cpipe);
536 }
537 }
538
539 static __inline void
540 pipeselwakeup(cpipe)
541 struct pipe *cpipe;
542 {
543
544 PIPE_LOCK_ASSERT(cpipe, MA_OWNED);
545 if (cpipe->pipe_state & PIPE_SEL) {
546 cpipe->pipe_state &= ~PIPE_SEL;
547 selwakeuppri(&cpipe->pipe_sel, PSOCK);
548 }
549 if ((cpipe->pipe_state & PIPE_ASYNC) && cpipe->pipe_sigio)
550 pgsigio(&cpipe->pipe_sigio, SIGIO, 0);
551 KNOTE_LOCKED(&cpipe->pipe_sel.si_note, 0);
552 }
553
554 /*
555 * Initialize and allocate VM and memory for pipe. The structure
556 * will start out zero'd from the ctor, so we just manage the kmem.
557 */
558 static int
559 pipe_create(pipe, backing)
560 struct pipe *pipe;
561 int backing;
562 {
563 int error;
564
565 if (backing) {
566 if (amountpipekva > maxpipekva / 2)
567 error = pipespace_new(pipe, SMALL_PIPE_SIZE);
568 else
569 error = pipespace_new(pipe, PIPE_SIZE);
570 } else {
571 /* If we're not backing this pipe, no need to do anything. */
572 error = 0;
573 }
574 return (error);
575 }
576
577 /* ARGSUSED */
578 static int
579 pipe_read(fp, uio, active_cred, flags, td)
580 struct file *fp;
581 struct uio *uio;
582 struct ucred *active_cred;
583 struct thread *td;
584 int flags;
585 {
586 struct pipe *rpipe = fp->f_data;
587 int error;
588 int nread = 0;
589 u_int size;
590
591 PIPE_LOCK(rpipe);
592 ++rpipe->pipe_busy;
593 error = pipelock(rpipe, 1);
594 if (error)
595 goto unlocked_error;
596
597 #ifdef MAC
598 error = mac_check_pipe_read(active_cred, rpipe->pipe_pair);
599 if (error)
600 goto locked_error;
601 #endif
602 if (amountpipekva > (3 * maxpipekva) / 4) {
603 if (!(rpipe->pipe_state & PIPE_DIRECTW) &&
604 (rpipe->pipe_buffer.size > SMALL_PIPE_SIZE) &&
605 (rpipe->pipe_buffer.cnt <= SMALL_PIPE_SIZE) &&
606 (piperesizeallowed == 1)) {
607 PIPE_UNLOCK(rpipe);
608 pipespace(rpipe, SMALL_PIPE_SIZE);
609 PIPE_LOCK(rpipe);
610 }
611 }
612
613 while (uio->uio_resid) {
614 /*
615 * normal pipe buffer receive
616 */
617 if (rpipe->pipe_buffer.cnt > 0) {
618 size = rpipe->pipe_buffer.size - rpipe->pipe_buffer.out;
619 if (size > rpipe->pipe_buffer.cnt)
620 size = rpipe->pipe_buffer.cnt;
621 if (size > (u_int) uio->uio_resid)
622 size = (u_int) uio->uio_resid;
623
624 PIPE_UNLOCK(rpipe);
625 error = uiomove(
626 &rpipe->pipe_buffer.buffer[rpipe->pipe_buffer.out],
627 size, uio);
628 PIPE_LOCK(rpipe);
629 if (error)
630 break;
631
632 rpipe->pipe_buffer.out += size;
633 if (rpipe->pipe_buffer.out >= rpipe->pipe_buffer.size)
634 rpipe->pipe_buffer.out = 0;
635
636 rpipe->pipe_buffer.cnt -= size;
637
638 /*
639 * If there is no more to read in the pipe, reset
640 * its pointers to the beginning. This improves
641 * cache hit stats.
642 */
643 if (rpipe->pipe_buffer.cnt == 0) {
644 rpipe->pipe_buffer.in = 0;
645 rpipe->pipe_buffer.out = 0;
646 }
647 nread += size;
648 #ifndef PIPE_NODIRECT
649 /*
650 * Direct copy, bypassing a kernel buffer.
651 */
652 } else if ((size = rpipe->pipe_map.cnt) &&
653 (rpipe->pipe_state & PIPE_DIRECTW)) {
654 if (size > (u_int) uio->uio_resid)
655 size = (u_int) uio->uio_resid;
656
657 PIPE_UNLOCK(rpipe);
658 error = uiomove_fromphys(rpipe->pipe_map.ms,
659 rpipe->pipe_map.pos, size, uio);
660 PIPE_LOCK(rpipe);
661 if (error)
662 break;
663 nread += size;
664 rpipe->pipe_map.pos += size;
665 rpipe->pipe_map.cnt -= size;
666 if (rpipe->pipe_map.cnt == 0) {
667 rpipe->pipe_state &= ~PIPE_DIRECTW;
668 wakeup(rpipe);
669 }
670 #endif
671 } else {
672 /*
673 * detect EOF condition
674 * read returns 0 on EOF, no need to set error
675 */
676 if (rpipe->pipe_state & PIPE_EOF)
677 break;
678
679 /*
680 * If the "write-side" has been blocked, wake it up now.
681 */
682 if (rpipe->pipe_state & PIPE_WANTW) {
683 rpipe->pipe_state &= ~PIPE_WANTW;
684 wakeup(rpipe);
685 }
686
687 /*
688 * Break if some data was read.
689 */
690 if (nread > 0)
691 break;
692
693 /*
694 * Unlock the pipe buffer for our remaining processing.
695 * We will either break out with an error or we will
696 * sleep and relock to loop.
697 */
698 pipeunlock(rpipe);
699
700 /*
701 * Handle non-blocking mode operation or
702 * wait for more data.
703 */
704 if (fp->f_flag & FNONBLOCK) {
705 error = EAGAIN;
706 } else {
707 rpipe->pipe_state |= PIPE_WANTR;
708 if ((error = msleep(rpipe, PIPE_MTX(rpipe),
709 PRIBIO | PCATCH,
710 "piperd", 0)) == 0)
711 error = pipelock(rpipe, 1);
712 }
713 if (error)
714 goto unlocked_error;
715 }
716 }
717 #ifdef MAC
718 locked_error:
719 #endif
720 pipeunlock(rpipe);
721
722 /* XXX: should probably do this before getting any locks. */
723 if (error == 0)
724 vfs_timestamp(&rpipe->pipe_atime);
725 unlocked_error:
726 --rpipe->pipe_busy;
727
728 /*
729 * PIPE_WANT processing only makes sense if pipe_busy is 0.
730 */
731 if ((rpipe->pipe_busy == 0) && (rpipe->pipe_state & PIPE_WANT)) {
732 rpipe->pipe_state &= ~(PIPE_WANT|PIPE_WANTW);
733 wakeup(rpipe);
734 } else if (rpipe->pipe_buffer.cnt < MINPIPESIZE) {
735 /*
736 * Handle write blocking hysteresis.
737 */
738 if (rpipe->pipe_state & PIPE_WANTW) {
739 rpipe->pipe_state &= ~PIPE_WANTW;
740 wakeup(rpipe);
741 }
742 }
743
744 if ((rpipe->pipe_buffer.size - rpipe->pipe_buffer.cnt) >= PIPE_BUF)
745 pipeselwakeup(rpipe);
746
747 PIPE_UNLOCK(rpipe);
748 return (error);
749 }
750
751 #ifndef PIPE_NODIRECT
752 /*
753 * Map the sending processes' buffer into kernel space and wire it.
754 * This is similar to a physical write operation.
755 */
756 static int
757 pipe_build_write_buffer(wpipe, uio)
758 struct pipe *wpipe;
759 struct uio *uio;
760 {
761 pmap_t pmap;
762 u_int size;
763 int i, j;
764 vm_offset_t addr, endaddr;
765
766 PIPE_LOCK_ASSERT(wpipe, MA_NOTOWNED);
767 KASSERT(wpipe->pipe_state & PIPE_DIRECTW,
768 ("Clone attempt on non-direct write pipe!"));
769
770 size = (u_int) uio->uio_iov->iov_len;
771 if (size > wpipe->pipe_buffer.size)
772 size = wpipe->pipe_buffer.size;
773
774 pmap = vmspace_pmap(curproc->p_vmspace);
775 endaddr = round_page((vm_offset_t)uio->uio_iov->iov_base + size);
776 addr = trunc_page((vm_offset_t)uio->uio_iov->iov_base);
777 if (endaddr < addr)
778 return (EFAULT);
779 for (i = 0; addr < endaddr; addr += PAGE_SIZE, i++) {
780 /*
781 * vm_fault_quick() can sleep. Consequently,
782 * vm_page_lock_queue() and vm_page_unlock_queue()
783 * should not be performed outside of this loop.
784 */
785 race:
786 if (vm_fault_quick((caddr_t)addr, VM_PROT_READ) < 0) {
787 vm_page_lock_queues();
788 for (j = 0; j < i; j++)
789 vm_page_unhold(wpipe->pipe_map.ms[j]);
790 vm_page_unlock_queues();
791 return (EFAULT);
792 }
793 wpipe->pipe_map.ms[i] = pmap_extract_and_hold(pmap, addr,
794 VM_PROT_READ);
795 if (wpipe->pipe_map.ms[i] == NULL)
796 goto race;
797 }
798
799 /*
800 * set up the control block
801 */
802 wpipe->pipe_map.npages = i;
803 wpipe->pipe_map.pos =
804 ((vm_offset_t) uio->uio_iov->iov_base) & PAGE_MASK;
805 wpipe->pipe_map.cnt = size;
806
807 /*
808 * and update the uio data
809 */
810
811 uio->uio_iov->iov_len -= size;
812 uio->uio_iov->iov_base = (char *)uio->uio_iov->iov_base + size;
813 if (uio->uio_iov->iov_len == 0)
814 uio->uio_iov++;
815 uio->uio_resid -= size;
816 uio->uio_offset += size;
817 return (0);
818 }
819
820 /*
821 * unmap and unwire the process buffer
822 */
823 static void
824 pipe_destroy_write_buffer(wpipe)
825 struct pipe *wpipe;
826 {
827 int i;
828
829 PIPE_LOCK_ASSERT(wpipe, MA_OWNED);
830 vm_page_lock_queues();
831 for (i = 0; i < wpipe->pipe_map.npages; i++) {
832 vm_page_unhold(wpipe->pipe_map.ms[i]);
833 }
834 vm_page_unlock_queues();
835 wpipe->pipe_map.npages = 0;
836 }
837
838 /*
839 * In the case of a signal, the writing process might go away. This
840 * code copies the data into the circular buffer so that the source
841 * pages can be freed without loss of data.
842 */
843 static void
844 pipe_clone_write_buffer(wpipe)
845 struct pipe *wpipe;
846 {
847 struct uio uio;
848 struct iovec iov;
849 int size;
850 int pos;
851
852 PIPE_LOCK_ASSERT(wpipe, MA_OWNED);
853 size = wpipe->pipe_map.cnt;
854 pos = wpipe->pipe_map.pos;
855
856 wpipe->pipe_buffer.in = size;
857 wpipe->pipe_buffer.out = 0;
858 wpipe->pipe_buffer.cnt = size;
859 wpipe->pipe_state &= ~PIPE_DIRECTW;
860
861 PIPE_UNLOCK(wpipe);
862 iov.iov_base = wpipe->pipe_buffer.buffer;
863 iov.iov_len = size;
864 uio.uio_iov = &iov;
865 uio.uio_iovcnt = 1;
866 uio.uio_offset = 0;
867 uio.uio_resid = size;
868 uio.uio_segflg = UIO_SYSSPACE;
869 uio.uio_rw = UIO_READ;
870 uio.uio_td = curthread;
871 uiomove_fromphys(wpipe->pipe_map.ms, pos, size, &uio);
872 PIPE_LOCK(wpipe);
873 pipe_destroy_write_buffer(wpipe);
874 }
875
876 /*
877 * This implements the pipe buffer write mechanism. Note that only
878 * a direct write OR a normal pipe write can be pending at any given time.
879 * If there are any characters in the pipe buffer, the direct write will
880 * be deferred until the receiving process grabs all of the bytes from
881 * the pipe buffer. Then the direct mapping write is set-up.
882 */
883 static int
884 pipe_direct_write(wpipe, uio)
885 struct pipe *wpipe;
886 struct uio *uio;
887 {
888 int error;
889
890 retry:
891 PIPE_LOCK_ASSERT(wpipe, MA_OWNED);
892 error = pipelock(wpipe, 1);
893 if (wpipe->pipe_state & PIPE_EOF)
894 error = EPIPE;
895 if (error) {
896 pipeunlock(wpipe);
897 goto error1;
898 }
899 while (wpipe->pipe_state & PIPE_DIRECTW) {
900 if (wpipe->pipe_state & PIPE_WANTR) {
901 wpipe->pipe_state &= ~PIPE_WANTR;
902 wakeup(wpipe);
903 }
904 pipeselwakeup(wpipe);
905 wpipe->pipe_state |= PIPE_WANTW;
906 pipeunlock(wpipe);
907 error = msleep(wpipe, PIPE_MTX(wpipe),
908 PRIBIO | PCATCH, "pipdww", 0);
909 if (error)
910 goto error1;
911 else
912 goto retry;
913 }
914 wpipe->pipe_map.cnt = 0; /* transfer not ready yet */
915 if (wpipe->pipe_buffer.cnt > 0) {
916 if (wpipe->pipe_state & PIPE_WANTR) {
917 wpipe->pipe_state &= ~PIPE_WANTR;
918 wakeup(wpipe);
919 }
920 pipeselwakeup(wpipe);
921 wpipe->pipe_state |= PIPE_WANTW;
922 pipeunlock(wpipe);
923 error = msleep(wpipe, PIPE_MTX(wpipe),
924 PRIBIO | PCATCH, "pipdwc", 0);
925 if (error)
926 goto error1;
927 else
928 goto retry;
929 }
930
931 wpipe->pipe_state |= PIPE_DIRECTW;
932
933 PIPE_UNLOCK(wpipe);
934 error = pipe_build_write_buffer(wpipe, uio);
935 PIPE_LOCK(wpipe);
936 if (error) {
937 wpipe->pipe_state &= ~PIPE_DIRECTW;
938 pipeunlock(wpipe);
939 goto error1;
940 }
941
942 error = 0;
943 while (!error && (wpipe->pipe_state & PIPE_DIRECTW)) {
944 if (wpipe->pipe_state & PIPE_EOF) {
945 pipe_destroy_write_buffer(wpipe);
946 pipeselwakeup(wpipe);
947 pipeunlock(wpipe);
948 error = EPIPE;
949 goto error1;
950 }
951 if (wpipe->pipe_state & PIPE_WANTR) {
952 wpipe->pipe_state &= ~PIPE_WANTR;
953 wakeup(wpipe);
954 }
955 pipeselwakeup(wpipe);
956 pipeunlock(wpipe);
957 error = msleep(wpipe, PIPE_MTX(wpipe), PRIBIO | PCATCH,
958 "pipdwt", 0);
959 pipelock(wpipe, 0);
960 }
961
962 if (wpipe->pipe_state & PIPE_EOF)
963 error = EPIPE;
964 if (wpipe->pipe_state & PIPE_DIRECTW) {
965 /*
966 * this bit of trickery substitutes a kernel buffer for
967 * the process that might be going away.
968 */
969 pipe_clone_write_buffer(wpipe);
970 } else {
971 pipe_destroy_write_buffer(wpipe);
972 }
973 pipeunlock(wpipe);
974 return (error);
975
976 error1:
977 wakeup(wpipe);
978 return (error);
979 }
980 #endif
981
982 static int
983 pipe_write(fp, uio, active_cred, flags, td)
984 struct file *fp;
985 struct uio *uio;
986 struct ucred *active_cred;
987 struct thread *td;
988 int flags;
989 {
990 int error = 0;
991 int desiredsize, orig_resid;
992 struct pipe *wpipe, *rpipe;
993
994 rpipe = fp->f_data;
995 wpipe = rpipe->pipe_peer;
996
997 PIPE_LOCK(rpipe);
998 error = pipelock(wpipe, 1);
999 if (error) {
1000 PIPE_UNLOCK(rpipe);
1001 return (error);
1002 }
1003 /*
1004 * detect loss of pipe read side, issue SIGPIPE if lost.
1005 */
1006 if (wpipe->pipe_present != PIPE_ACTIVE ||
1007 (wpipe->pipe_state & PIPE_EOF)) {
1008 pipeunlock(wpipe);
1009 PIPE_UNLOCK(rpipe);
1010 return (EPIPE);
1011 }
1012 #ifdef MAC
1013 error = mac_check_pipe_write(active_cred, wpipe->pipe_pair);
1014 if (error) {
1015 pipeunlock(wpipe);
1016 PIPE_UNLOCK(rpipe);
1017 return (error);
1018 }
1019 #endif
1020 ++wpipe->pipe_busy;
1021
1022 /* Choose a larger size if it's advantageous */
1023 desiredsize = max(SMALL_PIPE_SIZE, wpipe->pipe_buffer.size);
1024 while (desiredsize < wpipe->pipe_buffer.cnt + uio->uio_resid) {
1025 if (piperesizeallowed != 1)
1026 break;
1027 if (amountpipekva > maxpipekva / 2)
1028 break;
1029 if (desiredsize == BIG_PIPE_SIZE)
1030 break;
1031 desiredsize = desiredsize * 2;
1032 }
1033
1034 /* Choose a smaller size if we're in a OOM situation */
1035 if ((amountpipekva > (3 * maxpipekva) / 4) &&
1036 (wpipe->pipe_buffer.size > SMALL_PIPE_SIZE) &&
1037 (wpipe->pipe_buffer.cnt <= SMALL_PIPE_SIZE) &&
1038 (piperesizeallowed == 1))
1039 desiredsize = SMALL_PIPE_SIZE;
1040
1041 /* Resize if the above determined that a new size was necessary */
1042 if ((desiredsize != wpipe->pipe_buffer.size) &&
1043 ((wpipe->pipe_state & PIPE_DIRECTW) == 0)) {
1044 PIPE_UNLOCK(wpipe);
1045 pipespace(wpipe, desiredsize);
1046 PIPE_LOCK(wpipe);
1047 }
1048 if (wpipe->pipe_buffer.size == 0) {
1049 /*
1050 * This can only happen for reverse direction use of pipes
1051 * in a complete OOM situation.
1052 */
1053 error = ENOMEM;
1054 --wpipe->pipe_busy;
1055 pipeunlock(wpipe);
1056 PIPE_UNLOCK(wpipe);
1057 return (error);
1058 }
1059
1060 pipeunlock(wpipe);
1061
1062 orig_resid = uio->uio_resid;
1063
1064 while (uio->uio_resid) {
1065 int space;
1066
1067 pipelock(wpipe, 0);
1068 if (wpipe->pipe_state & PIPE_EOF) {
1069 pipeunlock(wpipe);
1070 error = EPIPE;
1071 break;
1072 }
1073 #ifndef PIPE_NODIRECT
1074 /*
1075 * If the transfer is large, we can gain performance if
1076 * we do process-to-process copies directly.
1077 * If the write is non-blocking, we don't use the
1078 * direct write mechanism.
1079 *
1080 * The direct write mechanism will detect the reader going
1081 * away on us.
1082 */
1083 if ((uio->uio_iov->iov_len >= PIPE_MINDIRECT) &&
1084 (wpipe->pipe_buffer.size >= PIPE_MINDIRECT) &&
1085 (fp->f_flag & FNONBLOCK) == 0) {
1086 pipeunlock(wpipe);
1087 error = pipe_direct_write(wpipe, uio);
1088 if (error)
1089 break;
1090 continue;
1091 }
1092 #endif
1093
1094 /*
1095 * Pipe buffered writes cannot be coincidental with
1096 * direct writes. We wait until the currently executing
1097 * direct write is completed before we start filling the
1098 * pipe buffer. We break out if a signal occurs or the
1099 * reader goes away.
1100 */
1101 if (wpipe->pipe_state & PIPE_DIRECTW) {
1102 if (wpipe->pipe_state & PIPE_WANTR) {
1103 wpipe->pipe_state &= ~PIPE_WANTR;
1104 wakeup(wpipe);
1105 }
1106 pipeselwakeup(wpipe);
1107 wpipe->pipe_state |= PIPE_WANTW;
1108 pipeunlock(wpipe);
1109 error = msleep(wpipe, PIPE_MTX(rpipe), PRIBIO | PCATCH,
1110 "pipbww", 0);
1111 if (error)
1112 break;
1113 else
1114 continue;
1115 }
1116
1117 space = wpipe->pipe_buffer.size - wpipe->pipe_buffer.cnt;
1118
1119 /* Writes of size <= PIPE_BUF must be atomic. */
1120 if ((space < uio->uio_resid) && (orig_resid <= PIPE_BUF))
1121 space = 0;
1122
1123 if (space > 0) {
1124 int size; /* Transfer size */
1125 int segsize; /* first segment to transfer */
1126
1127 /*
1128 * Transfer size is minimum of uio transfer
1129 * and free space in pipe buffer.
1130 */
1131 if (space > uio->uio_resid)
1132 size = uio->uio_resid;
1133 else
1134 size = space;
1135 /*
1136 * First segment to transfer is minimum of
1137 * transfer size and contiguous space in
1138 * pipe buffer. If first segment to transfer
1139 * is less than the transfer size, we've got
1140 * a wraparound in the buffer.
1141 */
1142 segsize = wpipe->pipe_buffer.size -
1143 wpipe->pipe_buffer.in;
1144 if (segsize > size)
1145 segsize = size;
1146
1147 /* Transfer first segment */
1148
1149 PIPE_UNLOCK(rpipe);
1150 error = uiomove(&wpipe->pipe_buffer.buffer[wpipe->pipe_buffer.in],
1151 segsize, uio);
1152 PIPE_LOCK(rpipe);
1153
1154 if (error == 0 && segsize < size) {
1155 KASSERT(wpipe->pipe_buffer.in + segsize ==
1156 wpipe->pipe_buffer.size,
1157 ("Pipe buffer wraparound disappeared"));
1158 /*
1159 * Transfer remaining part now, to
1160 * support atomic writes. Wraparound
1161 * happened.
1162 */
1163
1164 PIPE_UNLOCK(rpipe);
1165 error = uiomove(
1166 &wpipe->pipe_buffer.buffer[0],
1167 size - segsize, uio);
1168 PIPE_LOCK(rpipe);
1169 }
1170 if (error == 0) {
1171 wpipe->pipe_buffer.in += size;
1172 if (wpipe->pipe_buffer.in >=
1173 wpipe->pipe_buffer.size) {
1174 KASSERT(wpipe->pipe_buffer.in ==
1175 size - segsize +
1176 wpipe->pipe_buffer.size,
1177 ("Expected wraparound bad"));
1178 wpipe->pipe_buffer.in = size - segsize;
1179 }
1180
1181 wpipe->pipe_buffer.cnt += size;
1182 KASSERT(wpipe->pipe_buffer.cnt <=
1183 wpipe->pipe_buffer.size,
1184 ("Pipe buffer overflow"));
1185 }
1186 pipeunlock(wpipe);
1187 if (error != 0)
1188 break;
1189 } else {
1190 /*
1191 * If the "read-side" has been blocked, wake it up now.
1192 */
1193 if (wpipe->pipe_state & PIPE_WANTR) {
1194 wpipe->pipe_state &= ~PIPE_WANTR;
1195 wakeup(wpipe);
1196 }
1197
1198 /*
1199 * don't block on non-blocking I/O
1200 */
1201 if (fp->f_flag & FNONBLOCK) {
1202 error = EAGAIN;
1203 pipeunlock(wpipe);
1204 break;
1205 }
1206
1207 /*
1208 * We have no more space and have something to offer,
1209 * wake up select/poll.
1210 */
1211 pipeselwakeup(wpipe);
1212
1213 wpipe->pipe_state |= PIPE_WANTW;
1214 pipeunlock(wpipe);
1215 error = msleep(wpipe, PIPE_MTX(rpipe),
1216 PRIBIO | PCATCH, "pipewr", 0);
1217 if (error != 0)
1218 break;
1219 }
1220 }
1221
1222 pipelock(wpipe, 0);
1223 --wpipe->pipe_busy;
1224
1225 if ((wpipe->pipe_busy == 0) && (wpipe->pipe_state & PIPE_WANT)) {
1226 wpipe->pipe_state &= ~(PIPE_WANT | PIPE_WANTR);
1227 wakeup(wpipe);
1228 } else if (wpipe->pipe_buffer.cnt > 0) {
1229 /*
1230 * If we have put any characters in the buffer, we wake up
1231 * the reader.
1232 */
1233 if (wpipe->pipe_state & PIPE_WANTR) {
1234 wpipe->pipe_state &= ~PIPE_WANTR;
1235 wakeup(wpipe);
1236 }
1237 }
1238
1239 /*
1240 * Don't return EPIPE if I/O was successful
1241 */
1242 if ((wpipe->pipe_buffer.cnt == 0) &&
1243 (uio->uio_resid == 0) &&
1244 (error == EPIPE)) {
1245 error = 0;
1246 }
1247
1248 if (error == 0)
1249 vfs_timestamp(&wpipe->pipe_mtime);
1250
1251 /*
1252 * We have something to offer,
1253 * wake up select/poll.
1254 */
1255 if (wpipe->pipe_buffer.cnt)
1256 pipeselwakeup(wpipe);
1257
1258 pipeunlock(wpipe);
1259 PIPE_UNLOCK(rpipe);
1260 return (error);
1261 }
1262
1263 /*
1264 * we implement a very minimal set of ioctls for compatibility with sockets.
1265 */
1266 static int
1267 pipe_ioctl(fp, cmd, data, active_cred, td)
1268 struct file *fp;
1269 u_long cmd;
1270 void *data;
1271 struct ucred *active_cred;
1272 struct thread *td;
1273 {
1274 struct pipe *mpipe = fp->f_data;
1275 int error;
1276
1277 PIPE_LOCK(mpipe);
1278
1279 #ifdef MAC
1280 error = mac_check_pipe_ioctl(active_cred, mpipe->pipe_pair, cmd, data);
1281 if (error) {
1282 PIPE_UNLOCK(mpipe);
1283 return (error);
1284 }
1285 #endif
1286
1287 error = 0;
1288 switch (cmd) {
1289
1290 case FIONBIO:
1291 break;
1292
1293 case FIOASYNC:
1294 if (*(int *)data) {
1295 mpipe->pipe_state |= PIPE_ASYNC;
1296 } else {
1297 mpipe->pipe_state &= ~PIPE_ASYNC;
1298 }
1299 break;
1300
1301 case FIONREAD:
1302 if (mpipe->pipe_state & PIPE_DIRECTW)
1303 *(int *)data = mpipe->pipe_map.cnt;
1304 else
1305 *(int *)data = mpipe->pipe_buffer.cnt;
1306 break;
1307
1308 case FIOSETOWN:
1309 PIPE_UNLOCK(mpipe);
1310 error = fsetown(*(int *)data, &mpipe->pipe_sigio);
1311 goto out_unlocked;
1312
1313 case FIOGETOWN:
1314 *(int *)data = fgetown(&mpipe->pipe_sigio);
1315 break;
1316
1317 /* This is deprecated, FIOSETOWN should be used instead. */
1318 case TIOCSPGRP:
1319 PIPE_UNLOCK(mpipe);
1320 error = fsetown(-(*(int *)data), &mpipe->pipe_sigio);
1321 goto out_unlocked;
1322
1323 /* This is deprecated, FIOGETOWN should be used instead. */
1324 case TIOCGPGRP:
1325 *(int *)data = -fgetown(&mpipe->pipe_sigio);
1326 break;
1327
1328 default:
1329 error = ENOTTY;
1330 break;
1331 }
1332 PIPE_UNLOCK(mpipe);
1333 out_unlocked:
1334 return (error);
1335 }
1336
1337 static int
1338 pipe_poll(fp, events, active_cred, td)
1339 struct file *fp;
1340 int events;
1341 struct ucred *active_cred;
1342 struct thread *td;
1343 {
1344 struct pipe *rpipe = fp->f_data;
1345 struct pipe *wpipe;
1346 int revents = 0;
1347 #ifdef MAC
1348 int error;
1349 #endif
1350
1351 wpipe = rpipe->pipe_peer;
1352 PIPE_LOCK(rpipe);
1353 #ifdef MAC
1354 error = mac_check_pipe_poll(active_cred, rpipe->pipe_pair);
1355 if (error)
1356 goto locked_error;
1357 #endif
1358 if (events & (POLLIN | POLLRDNORM))
1359 if ((rpipe->pipe_state & PIPE_DIRECTW) ||
1360 (rpipe->pipe_buffer.cnt > 0) ||
1361 (rpipe->pipe_state & PIPE_EOF))
1362 revents |= events & (POLLIN | POLLRDNORM);
1363
1364 if (events & (POLLOUT | POLLWRNORM))
1365 if (wpipe->pipe_present != PIPE_ACTIVE ||
1366 (wpipe->pipe_state & PIPE_EOF) ||
1367 (((wpipe->pipe_state & PIPE_DIRECTW) == 0) &&
1368 (wpipe->pipe_buffer.size - wpipe->pipe_buffer.cnt) >= PIPE_BUF))
1369 revents |= events & (POLLOUT | POLLWRNORM);
1370
1371 if ((rpipe->pipe_state & PIPE_EOF) ||
1372 wpipe->pipe_present != PIPE_ACTIVE ||
1373 (wpipe->pipe_state & PIPE_EOF))
1374 revents |= POLLHUP;
1375
1376 if (revents == 0) {
1377 if (events & (POLLIN | POLLRDNORM)) {
1378 selrecord(td, &rpipe->pipe_sel);
1379 rpipe->pipe_state |= PIPE_SEL;
1380 }
1381
1382 if (events & (POLLOUT | POLLWRNORM)) {
1383 selrecord(td, &wpipe->pipe_sel);
1384 wpipe->pipe_state |= PIPE_SEL;
1385 }
1386 }
1387 #ifdef MAC
1388 locked_error:
1389 #endif
1390 PIPE_UNLOCK(rpipe);
1391
1392 return (revents);
1393 }
1394
1395 /*
1396 * We shouldn't need locks here as we're doing a read and this should
1397 * be a natural race.
1398 */
1399 static int
1400 pipe_stat(fp, ub, active_cred, td)
1401 struct file *fp;
1402 struct stat *ub;
1403 struct ucred *active_cred;
1404 struct thread *td;
1405 {
1406 struct pipe *pipe = fp->f_data;
1407 #ifdef MAC
1408 int error;
1409
1410 PIPE_LOCK(pipe);
1411 error = mac_check_pipe_stat(active_cred, pipe->pipe_pair);
1412 PIPE_UNLOCK(pipe);
1413 if (error)
1414 return (error);
1415 #endif
1416 bzero(ub, sizeof(*ub));
1417 ub->st_mode = S_IFIFO;
1418 ub->st_blksize = PAGE_SIZE;
1419 if (pipe->pipe_state & PIPE_DIRECTW)
1420 ub->st_size = pipe->pipe_map.cnt;
1421 else
1422 ub->st_size = pipe->pipe_buffer.cnt;
1423 ub->st_blocks = (ub->st_size + ub->st_blksize - 1) / ub->st_blksize;
1424 ub->st_atimespec = pipe->pipe_atime;
1425 ub->st_mtimespec = pipe->pipe_mtime;
1426 ub->st_ctimespec = pipe->pipe_ctime;
1427 ub->st_uid = fp->f_cred->cr_uid;
1428 ub->st_gid = fp->f_cred->cr_gid;
1429 /*
1430 * Left as 0: st_dev, st_ino, st_nlink, st_rdev, st_flags, st_gen.
1431 * XXX (st_dev, st_ino) should be unique.
1432 */
1433 return (0);
1434 }
1435
1436 /* ARGSUSED */
1437 static int
1438 pipe_close(fp, td)
1439 struct file *fp;
1440 struct thread *td;
1441 {
1442 struct pipe *cpipe = fp->f_data;
1443
1444 fp->f_ops = &badfileops;
1445 fp->f_data = NULL;
1446 funsetown(&cpipe->pipe_sigio);
1447 pipeclose(cpipe);
1448 return (0);
1449 }
1450
1451 static void
1452 pipe_free_kmem(cpipe)
1453 struct pipe *cpipe;
1454 {
1455
1456 KASSERT(!mtx_owned(PIPE_MTX(cpipe)),
1457 ("pipe_free_kmem: pipe mutex locked"));
1458
1459 if (cpipe->pipe_buffer.buffer != NULL) {
1460 atomic_subtract_int(&amountpipekva, cpipe->pipe_buffer.size);
1461 vm_map_remove(pipe_map,
1462 (vm_offset_t)cpipe->pipe_buffer.buffer,
1463 (vm_offset_t)cpipe->pipe_buffer.buffer + cpipe->pipe_buffer.size);
1464 cpipe->pipe_buffer.buffer = NULL;
1465 }
1466 #ifndef PIPE_NODIRECT
1467 {
1468 cpipe->pipe_map.cnt = 0;
1469 cpipe->pipe_map.pos = 0;
1470 cpipe->pipe_map.npages = 0;
1471 }
1472 #endif
1473 }
1474
1475 /*
1476 * shutdown the pipe
1477 */
1478 static void
1479 pipeclose(cpipe)
1480 struct pipe *cpipe;
1481 {
1482 struct pipepair *pp;
1483 struct pipe *ppipe;
1484
1485 KASSERT(cpipe != NULL, ("pipeclose: cpipe == NULL"));
1486
1487 PIPE_LOCK(cpipe);
1488 pipelock(cpipe, 0);
1489 pp = cpipe->pipe_pair;
1490
1491 pipeselwakeup(cpipe);
1492
1493 /*
1494 * If the other side is blocked, wake it up saying that
1495 * we want to close it down.
1496 */
1497 cpipe->pipe_state |= PIPE_EOF;
1498 while (cpipe->pipe_busy) {
1499 wakeup(cpipe);
1500 cpipe->pipe_state |= PIPE_WANT;
1501 pipeunlock(cpipe);
1502 msleep(cpipe, PIPE_MTX(cpipe), PRIBIO, "pipecl", 0);
1503 pipelock(cpipe, 0);
1504 }
1505
1506
1507 /*
1508 * Disconnect from peer, if any.
1509 */
1510 ppipe = cpipe->pipe_peer;
1511 if (ppipe->pipe_present == PIPE_ACTIVE) {
1512 pipeselwakeup(ppipe);
1513
1514 ppipe->pipe_state |= PIPE_EOF;
1515 wakeup(ppipe);
1516 KNOTE_LOCKED(&ppipe->pipe_sel.si_note, 0);
1517 }
1518
1519 /*
1520 * Mark this endpoint as free. Release kmem resources. We
1521 * don't mark this endpoint as unused until we've finished
1522 * doing that, or the pipe might disappear out from under
1523 * us.
1524 */
1525 PIPE_UNLOCK(cpipe);
1526 pipe_free_kmem(cpipe);
1527 PIPE_LOCK(cpipe);
1528 cpipe->pipe_present = PIPE_CLOSING;
1529 pipeunlock(cpipe);
1530
1531 /*
1532 * knlist_clear() may sleep dropping the PIPE_MTX. Set the
1533 * PIPE_FINALIZED, that allows other end to free the
1534 * pipe_pair, only after the knotes are completely dismantled.
1535 */
1536 knlist_clear(&cpipe->pipe_sel.si_note, 1);
1537 cpipe->pipe_present = PIPE_FINALIZED;
1538 knlist_destroy(&cpipe->pipe_sel.si_note);
1539
1540 /*
1541 * If both endpoints are now closed, release the memory for the
1542 * pipe pair. If not, unlock.
1543 */
1544 if (ppipe->pipe_present == PIPE_FINALIZED) {
1545 PIPE_UNLOCK(cpipe);
1546 #ifdef MAC
1547 mac_destroy_pipe(pp);
1548 #endif
1549 uma_zfree(pipe_zone, cpipe->pipe_pair);
1550 } else
1551 PIPE_UNLOCK(cpipe);
1552 }
1553
1554 /*ARGSUSED*/
1555 static int
1556 pipe_kqfilter(struct file *fp, struct knote *kn)
1557 {
1558 struct pipe *cpipe;
1559
1560 cpipe = kn->kn_fp->f_data;
1561 PIPE_LOCK(cpipe);
1562 switch (kn->kn_filter) {
1563 case EVFILT_READ:
1564 kn->kn_fop = &pipe_rfiltops;
1565 break;
1566 case EVFILT_WRITE:
1567 kn->kn_fop = &pipe_wfiltops;
1568 if (cpipe->pipe_peer->pipe_present != PIPE_ACTIVE) {
1569 /* other end of pipe has been closed */
1570 PIPE_UNLOCK(cpipe);
1571 return (EPIPE);
1572 }
1573 cpipe = cpipe->pipe_peer;
1574 break;
1575 default:
1576 PIPE_UNLOCK(cpipe);
1577 return (EINVAL);
1578 }
1579
1580 knlist_add(&cpipe->pipe_sel.si_note, kn, 1);
1581 PIPE_UNLOCK(cpipe);
1582 return (0);
1583 }
1584
1585 static void
1586 filt_pipedetach(struct knote *kn)
1587 {
1588 struct pipe *cpipe = (struct pipe *)kn->kn_fp->f_data;
1589
1590 PIPE_LOCK(cpipe);
1591 if (kn->kn_filter == EVFILT_WRITE)
1592 cpipe = cpipe->pipe_peer;
1593 knlist_remove(&cpipe->pipe_sel.si_note, kn, 1);
1594 PIPE_UNLOCK(cpipe);
1595 }
1596
1597 /*ARGSUSED*/
1598 static int
1599 filt_piperead(struct knote *kn, long hint)
1600 {
1601 struct pipe *rpipe = kn->kn_fp->f_data;
1602 struct pipe *wpipe = rpipe->pipe_peer;
1603 int ret;
1604
1605 PIPE_LOCK(rpipe);
1606 kn->kn_data = rpipe->pipe_buffer.cnt;
1607 if ((kn->kn_data == 0) && (rpipe->pipe_state & PIPE_DIRECTW))
1608 kn->kn_data = rpipe->pipe_map.cnt;
1609
1610 if ((rpipe->pipe_state & PIPE_EOF) ||
1611 wpipe->pipe_present != PIPE_ACTIVE ||
1612 (wpipe->pipe_state & PIPE_EOF)) {
1613 kn->kn_flags |= EV_EOF;
1614 PIPE_UNLOCK(rpipe);
1615 return (1);
1616 }
1617 ret = kn->kn_data > 0;
1618 PIPE_UNLOCK(rpipe);
1619 return ret;
1620 }
1621
1622 /*ARGSUSED*/
1623 static int
1624 filt_pipewrite(struct knote *kn, long hint)
1625 {
1626 struct pipe *rpipe = kn->kn_fp->f_data;
1627 struct pipe *wpipe = rpipe->pipe_peer;
1628
1629 PIPE_LOCK(rpipe);
1630 if (wpipe->pipe_present != PIPE_ACTIVE ||
1631 (wpipe->pipe_state & PIPE_EOF)) {
1632 kn->kn_data = 0;
1633 kn->kn_flags |= EV_EOF;
1634 PIPE_UNLOCK(rpipe);
1635 return (1);
1636 }
1637 kn->kn_data = wpipe->pipe_buffer.size - wpipe->pipe_buffer.cnt;
1638 if (wpipe->pipe_state & PIPE_DIRECTW)
1639 kn->kn_data = 0;
1640
1641 PIPE_UNLOCK(rpipe);
1642 return (kn->kn_data >= PIPE_BUF);
1643 }
Cache object: f6c9fa4ef829faf49e934395147f37ee
|