Make fstat() account for pending direct-write data when run on a pipe.
[dragonfly.git] / sys / kern / sys_pipe.c
CommitLineData
984263bc
MD
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 * $FreeBSD: src/sys/kern/sys_pipe.c,v 1.60.2.13 2002/08/05 15:05:15 des Exp $
739ae7cb 20 * $DragonFly: src/sys/kern/sys_pipe.c,v 1.24 2004/07/24 20:30:00 dillon Exp $
984263bc
MD
21 */
22
23/*
24 * This file contains a high-performance replacement for the socket-based
25 * pipes scheme originally used in FreeBSD/4.4Lite. It does not support
26 * all features of sockets, but does do everything that pipes normally
27 * do.
28 */
29
30/*
31 * This code has two modes of operation, a small write mode and a large
32 * write mode. The small write mode acts like conventional pipes with
33 * a kernel buffer. If the buffer is less than PIPE_MINDIRECT, then the
34 * "normal" pipe buffering is done. If the buffer is between PIPE_MINDIRECT
35 * and PIPE_SIZE in size, it is fully mapped and wired into the kernel, and
36 * the receiving process can copy it directly from the pages in the sending
37 * process.
38 *
39 * If the sending process receives a signal, it is possible that it will
40 * go away, and certainly its address space can change, because control
41 * is returned back to the user-mode side. In that case, the pipe code
42 * arranges to copy the buffer supplied by the user process, to a pageable
43 * kernel buffer, and the receiving process will grab the data from the
44 * pageable kernel buffer. Since signals don't happen all that often,
45 * the copy operation is normally eliminated.
46 *
47 * The constant PIPE_MINDIRECT is chosen to make sure that buffering will
48 * happen for small transfers so that the system will not spend all of
49 * its time context switching. PIPE_SIZE is constrained by the
50 * amount of kernel virtual memory.
51 */
52
53#include <sys/param.h>
54#include <sys/systm.h>
fc7d5181 55#include <sys/kernel.h>
984263bc
MD
56#include <sys/proc.h>
57#include <sys/fcntl.h>
58#include <sys/file.h>
59#include <sys/filedesc.h>
60#include <sys/filio.h>
61#include <sys/ttycom.h>
62#include <sys/stat.h>
63#include <sys/poll.h>
64#include <sys/select.h>
65#include <sys/signalvar.h>
66#include <sys/sysproto.h>
67#include <sys/pipe.h>
68#include <sys/vnode.h>
69#include <sys/uio.h>
70#include <sys/event.h>
fc7d5181
MD
71#include <sys/globaldata.h>
72#include <sys/module.h>
73#include <sys/malloc.h>
74#include <sys/sysctl.h>
984263bc
MD
75
76#include <vm/vm.h>
77#include <vm/vm_param.h>
78#include <sys/lock.h>
79#include <vm/vm_object.h>
80#include <vm/vm_kern.h>
81#include <vm/vm_extern.h>
82#include <vm/pmap.h>
83#include <vm/vm_map.h>
84#include <vm/vm_page.h>
85#include <vm/vm_zone.h>
86
dadab5e9
MD
87#include <sys/file2.h>
88
8100156a 89#include <machine/cpufunc.h>
984263bc
MD
90
91/*
92 * interfaces to the outside world
93 */
402ed7e1
RG
94static int pipe_read (struct file *fp, struct uio *uio,
95 struct ucred *cred, int flags, struct thread *td);
96static int pipe_write (struct file *fp, struct uio *uio,
97 struct ucred *cred, int flags, struct thread *td);
98static int pipe_close (struct file *fp, struct thread *td);
99static int pipe_poll (struct file *fp, int events, struct ucred *cred,
100 struct thread *td);
101static int pipe_kqfilter (struct file *fp, struct knote *kn);
102static int pipe_stat (struct file *fp, struct stat *sb, struct thread *td);
103static int pipe_ioctl (struct file *fp, u_long cmd, caddr_t data, struct thread *td);
984263bc
MD
104
105static struct fileops pipeops = {
f53ede20 106 NULL, /* port */
455fcd7e 107 NULL, /* clone */
984263bc
MD
108 pipe_read, pipe_write, pipe_ioctl, pipe_poll, pipe_kqfilter,
109 pipe_stat, pipe_close
110};
111
112static void filt_pipedetach(struct knote *kn);
113static int filt_piperead(struct knote *kn, long hint);
114static int filt_pipewrite(struct knote *kn, long hint);
115
116static struct filterops pipe_rfiltops =
117 { 1, NULL, filt_pipedetach, filt_piperead };
118static struct filterops pipe_wfiltops =
119 { 1, NULL, filt_pipedetach, filt_pipewrite };
120
fc7d5181 121MALLOC_DEFINE(M_PIPE, "pipe", "pipe structures");
984263bc
MD
122
123/*
124 * Default pipe buffer size(s), this can be kind-of large now because pipe
125 * space is pageable. The pipe code will try to maintain locality of
126 * reference for performance reasons, so small amounts of outstanding I/O
127 * will not wipe the cache.
128 */
129#define MINPIPESIZE (PIPE_SIZE/3)
130#define MAXPIPESIZE (2*PIPE_SIZE/3)
131
132/*
133 * Maximum amount of kva for pipes -- this is kind-of a soft limit, but
134 * is there so that on large systems, we don't exhaust it.
135 */
136#define MAXPIPEKVA (8*1024*1024)
137
138/*
139 * Limit for direct transfers, we cannot, of course limit
140 * the amount of kva for pipes in general though.
141 */
142#define LIMITPIPEKVA (16*1024*1024)
143
144/*
145 * Limit the number of "big" pipes
146 */
147#define LIMITBIGPIPES 32
fc7d5181
MD
148#define PIPEQ_MAX_CACHE 16 /* per-cpu pipe structure cache */
149
150static int pipe_maxbig = LIMITBIGPIPES;
151static int pipe_maxcache = PIPEQ_MAX_CACHE;
152static int pipe_nbig;
fc7d5181
MD
153static int pipe_bcache_alloc;
154static int pipe_bkmem_alloc;
8100156a
MD
155static int pipe_dwrite_enable = 1; /* 0:copy, 1:kmem/sfbuf 2:force */
156static int pipe_dwrite_sfbuf = 1; /* 0:kmem_map 1:sfbufs 2:sfbufs_dmap */
157 /* 3:sfbuf_dmap w/ forced invlpg */
fc7d5181
MD
158
159SYSCTL_NODE(_kern, OID_AUTO, pipe, CTLFLAG_RW, 0, "Pipe operation");
160SYSCTL_INT(_kern_pipe, OID_AUTO, nbig,
161 CTLFLAG_RD, &pipe_nbig, 0, "numer of big pipes allocated");
fc7d5181
MD
162SYSCTL_INT(_kern_pipe, OID_AUTO, maxcache,
163 CTLFLAG_RW, &pipe_maxcache, 0, "max pipes cached per-cpu");
164SYSCTL_INT(_kern_pipe, OID_AUTO, maxbig,
165 CTLFLAG_RW, &pipe_maxbig, 0, "max number of big pipes");
8100156a
MD
166SYSCTL_INT(_kern_pipe, OID_AUTO, dwrite_enable,
167 CTLFLAG_RW, &pipe_dwrite_enable, 0, "1:enable/2:force direct writes");
168SYSCTL_INT(_kern_pipe, OID_AUTO, dwrite_sfbuf,
333ad792
HP
169 CTLFLAG_RW, &pipe_dwrite_sfbuf, 0,
170 "(if dwrite_enable) 0:kmem 1:sfbuf 2:sfbuf_dmap 3:sfbuf_dmap_forceinvlpg");
fc7d5181
MD
171#if !defined(NO_PIPE_SYSCTL_STATS)
172SYSCTL_INT(_kern_pipe, OID_AUTO, bcache_alloc,
173 CTLFLAG_RW, &pipe_bcache_alloc, 0, "pipe buffer from pcpu cache");
fc7d5181
MD
174SYSCTL_INT(_kern_pipe, OID_AUTO, bkmem_alloc,
175 CTLFLAG_RW, &pipe_bkmem_alloc, 0, "pipe buffer from kmem");
fc7d5181 176#endif
984263bc 177
402ed7e1
RG
178static void pipeclose (struct pipe *cpipe);
179static void pipe_free_kmem (struct pipe *cpipe);
180static int pipe_create (struct pipe **cpipep);
181static __inline int pipelock (struct pipe *cpipe, int catch);
182static __inline void pipeunlock (struct pipe *cpipe);
183static __inline void pipeselwakeup (struct pipe *cpipe);
984263bc 184#ifndef PIPE_NODIRECT
402ed7e1 185static int pipe_build_write_buffer (struct pipe *wpipe, struct uio *uio);
402ed7e1
RG
186static int pipe_direct_write (struct pipe *wpipe, struct uio *uio);
187static void pipe_clone_write_buffer (struct pipe *wpipe);
984263bc 188#endif
402ed7e1 189static int pipespace (struct pipe *cpipe, int size);
984263bc 190
984263bc
MD
191/*
192 * The pipe system call for the DTYPE_PIPE type of pipes
41c20dac
MD
193 *
194 * pipe_ARgs(int dummy)
984263bc
MD
195 */
196
197/* ARGSUSED */
198int
41c20dac 199pipe(struct pipe_args *uap)
984263bc 200{
dadab5e9
MD
201 struct thread *td = curthread;
202 struct proc *p = td->td_proc;
203 struct filedesc *fdp;
984263bc
MD
204 struct file *rf, *wf;
205 struct pipe *rpipe, *wpipe;
90b9818c 206 int fd1, fd2, error;
984263bc 207
dadab5e9
MD
208 KKASSERT(p);
209 fdp = p->p_fd;
210
984263bc
MD
211 rpipe = wpipe = NULL;
212 if (pipe_create(&rpipe) || pipe_create(&wpipe)) {
213 pipeclose(rpipe);
214 pipeclose(wpipe);
215 return (ENFILE);
216 }
217
218 rpipe->pipe_state |= PIPE_DIRECTOK;
219 wpipe->pipe_state |= PIPE_DIRECTOK;
220
8100156a
MD
221 /*
222 * Select the direct-map features to use for this pipe. Since the
223 * sysctl's can change on the fly we record the settings when the
224 * pipe is created.
225 *
226 * Generally speaking the system will default to what we consider
227 * to be the best-balanced and most stable option. Right now this
228 * is SFBUF1. Modes 2 and 3 are considered experiemental at the
229 * moment.
230 */
231 wpipe->pipe_feature = PIPE_COPY;
232 if (pipe_dwrite_enable) {
233 switch(pipe_dwrite_sfbuf) {
234 case 0:
235 wpipe->pipe_feature = PIPE_KMEM;
236 break;
237 case 1:
238 wpipe->pipe_feature = PIPE_SFBUF1;
239 break;
240 case 2:
241 case 3:
242 wpipe->pipe_feature = PIPE_SFBUF2;
243 break;
244 }
245 }
246 rpipe->pipe_feature = wpipe->pipe_feature;
247
90b9818c 248 error = falloc(p, &rf, &fd1);
984263bc
MD
249 if (error) {
250 pipeclose(rpipe);
251 pipeclose(wpipe);
252 return (error);
253 }
254 fhold(rf);
c7114eea 255 uap->sysmsg_fds[0] = fd1;
984263bc
MD
256
257 /*
258 * Warning: once we've gotten past allocation of the fd for the
259 * read-side, we can only drop the read side via fdrop() in order
260 * to avoid races against processes which manage to dup() the read
261 * side while we are blocked trying to allocate the write side.
262 */
263 rf->f_flag = FREAD | FWRITE;
264 rf->f_type = DTYPE_PIPE;
265 rf->f_data = (caddr_t)rpipe;
266 rf->f_ops = &pipeops;
90b9818c 267 error = falloc(p, &wf, &fd2);
984263bc 268 if (error) {
90b9818c
MD
269 if (fdp->fd_ofiles[fd1] == rf) {
270 fdp->fd_ofiles[fd1] = NULL;
dadab5e9 271 fdrop(rf, td);
984263bc 272 }
dadab5e9 273 fdrop(rf, td);
984263bc
MD
274 /* rpipe has been closed by fdrop(). */
275 pipeclose(wpipe);
276 return (error);
277 }
278 wf->f_flag = FREAD | FWRITE;
279 wf->f_type = DTYPE_PIPE;
280 wf->f_data = (caddr_t)wpipe;
281 wf->f_ops = &pipeops;
c7114eea 282 uap->sysmsg_fds[1] = fd2;
984263bc
MD
283
284 rpipe->pipe_peer = wpipe;
285 wpipe->pipe_peer = rpipe;
dadab5e9 286 fdrop(rf, td);
984263bc
MD
287
288 return (0);
289}
290
291/*
292 * Allocate kva for pipe circular buffer, the space is pageable
293 * This routine will 'realloc' the size of a pipe safely, if it fails
294 * it will retain the old buffer.
295 * If it fails it will return ENOMEM.
296 */
297static int
fc7d5181 298pipespace(struct pipe *cpipe, int size)
984263bc
MD
299{
300 struct vm_object *object;
301 caddr_t buffer;
302 int npages, error;
303
fc7d5181
MD
304 npages = round_page(size) / PAGE_SIZE;
305 object = cpipe->pipe_buffer.object;
984263bc
MD
306
307 /*
fc7d5181
MD
308 * [re]create the object if necessary and reserve space for it
309 * in the kernel_map. The object and memory are pageable. On
310 * success, free the old resources before assigning the new
311 * ones.
984263bc 312 */
fc7d5181
MD
313 if (object == NULL || object->size != npages) {
314 object = vm_object_allocate(OBJT_DEFAULT, npages);
315 buffer = (caddr_t) vm_map_min(kernel_map);
984263bc 316
fc7d5181
MD
317 error = vm_map_find(kernel_map, object, 0,
318 (vm_offset_t *) &buffer, size, 1,
319 VM_PROT_ALL, VM_PROT_ALL, 0);
984263bc 320
fc7d5181
MD
321 if (error != KERN_SUCCESS) {
322 vm_object_deallocate(object);
323 return (ENOMEM);
324 }
fc7d5181
MD
325 pipe_free_kmem(cpipe);
326 cpipe->pipe_buffer.object = object;
327 cpipe->pipe_buffer.buffer = buffer;
328 cpipe->pipe_buffer.size = size;
329 ++pipe_bkmem_alloc;
330 } else {
331 ++pipe_bcache_alloc;
fc7d5181 332 }
984263bc
MD
333 cpipe->pipe_buffer.in = 0;
334 cpipe->pipe_buffer.out = 0;
335 cpipe->pipe_buffer.cnt = 0;
984263bc
MD
336 return (0);
337}
338
339/*
fc7d5181
MD
340 * Initialize and allocate VM and memory for pipe, pulling the pipe from
341 * our per-cpu cache if possible. For now make sure it is sized for the
342 * smaller PIPE_SIZE default.
984263bc
MD
343 */
344static int
345pipe_create(cpipep)
346 struct pipe **cpipep;
347{
fc7d5181 348 globaldata_t gd = mycpu;
984263bc
MD
349 struct pipe *cpipe;
350 int error;
351
fc7d5181
MD
352 if ((cpipe = gd->gd_pipeq) != NULL) {
353 gd->gd_pipeq = cpipe->pipe_peer;
354 --gd->gd_pipeqcount;
355 cpipe->pipe_peer = NULL;
356 } else {
357 cpipe = malloc(sizeof(struct pipe), M_PIPE, M_WAITOK|M_ZERO);
358 }
359 *cpipep = cpipe;
360 if ((error = pipespace(cpipe, PIPE_SIZE)) != 0)
984263bc 361 return (error);
984263bc
MD
362 vfs_timestamp(&cpipe->pipe_ctime);
363 cpipe->pipe_atime = cpipe->pipe_ctime;
364 cpipe->pipe_mtime = cpipe->pipe_ctime;
984263bc
MD
365 return (0);
366}
367
368
369/*
370 * lock a pipe for I/O, blocking other access
371 */
372static __inline int
373pipelock(cpipe, catch)
374 struct pipe *cpipe;
375 int catch;
376{
377 int error;
378
379 while (cpipe->pipe_state & PIPE_LOCK) {
380 cpipe->pipe_state |= PIPE_LWANT;
377d4740 381 error = tsleep(cpipe, (catch ? PCATCH : 0), "pipelk", 0);
984263bc
MD
382 if (error != 0)
383 return (error);
384 }
385 cpipe->pipe_state |= PIPE_LOCK;
386 return (0);
387}
388
389/*
390 * unlock a pipe I/O lock
391 */
392static __inline void
393pipeunlock(cpipe)
394 struct pipe *cpipe;
395{
396
397 cpipe->pipe_state &= ~PIPE_LOCK;
398 if (cpipe->pipe_state & PIPE_LWANT) {
399 cpipe->pipe_state &= ~PIPE_LWANT;
400 wakeup(cpipe);
401 }
402}
403
404static __inline void
405pipeselwakeup(cpipe)
406 struct pipe *cpipe;
407{
408
409 if (cpipe->pipe_state & PIPE_SEL) {
410 cpipe->pipe_state &= ~PIPE_SEL;
411 selwakeup(&cpipe->pipe_sel);
412 }
413 if ((cpipe->pipe_state & PIPE_ASYNC) && cpipe->pipe_sigio)
414 pgsigio(cpipe->pipe_sigio, SIGIO, 0);
415 KNOTE(&cpipe->pipe_sel.si_note, 0);
416}
417
418/* ARGSUSED */
419static int
dadab5e9
MD
420pipe_read(struct file *fp, struct uio *uio, struct ucred *cred,
421 int flags, struct thread *td)
984263bc
MD
422{
423 struct pipe *rpipe = (struct pipe *) fp->f_data;
424 int error;
425 int nread = 0;
426 u_int size;
427
428 ++rpipe->pipe_busy;
429 error = pipelock(rpipe, 1);
430 if (error)
431 goto unlocked_error;
432
433 while (uio->uio_resid) {
8100156a
MD
434 caddr_t va;
435
984263bc 436 if (rpipe->pipe_buffer.cnt > 0) {
8100156a
MD
437 /*
438 * normal pipe buffer receive
439 */
984263bc
MD
440 size = rpipe->pipe_buffer.size - rpipe->pipe_buffer.out;
441 if (size > rpipe->pipe_buffer.cnt)
442 size = rpipe->pipe_buffer.cnt;
443 if (size > (u_int) uio->uio_resid)
444 size = (u_int) uio->uio_resid;
445
446 error = uiomove(&rpipe->pipe_buffer.buffer[rpipe->pipe_buffer.out],
447 size, uio);
448 if (error)
449 break;
450
451 rpipe->pipe_buffer.out += size;
452 if (rpipe->pipe_buffer.out >= rpipe->pipe_buffer.size)
453 rpipe->pipe_buffer.out = 0;
454
455 rpipe->pipe_buffer.cnt -= size;
456
457 /*
458 * If there is no more to read in the pipe, reset
459 * its pointers to the beginning. This improves
460 * cache hit stats.
461 */
462 if (rpipe->pipe_buffer.cnt == 0) {
463 rpipe->pipe_buffer.in = 0;
464 rpipe->pipe_buffer.out = 0;
465 }
466 nread += size;
467#ifndef PIPE_NODIRECT
8100156a
MD
468 } else if (rpipe->pipe_kva &&
469 rpipe->pipe_feature == PIPE_KMEM &&
470 (rpipe->pipe_state & (PIPE_DIRECTW|PIPE_DIRECTIP))
471 == PIPE_DIRECTW
472 ) {
473 /*
474 * Direct copy using source-side kva mapping
475 */
476 size = rpipe->pipe_map.xio_bytes;
477 if (size > (u_int)uio->uio_resid)
478 size = (u_int)uio->uio_resid;
479 va = (caddr_t)rpipe->pipe_kva + rpipe->pipe_map.xio_offset;
480 error = uiomove(va, size, uio);
481 if (error)
482 break;
483 nread += size;
484 rpipe->pipe_map.xio_offset += size;
485 rpipe->pipe_map.xio_bytes -= size;
486 if (rpipe->pipe_map.xio_bytes == 0) {
487 rpipe->pipe_state |= PIPE_DIRECTIP;
488 rpipe->pipe_state &= ~PIPE_DIRECTW;
489 wakeup(rpipe);
490 }
24712b90 491 } else if (rpipe->pipe_map.xio_bytes &&
8100156a
MD
492 rpipe->pipe_kva &&
493 rpipe->pipe_feature == PIPE_SFBUF2 &&
494 (rpipe->pipe_state & (PIPE_DIRECTW|PIPE_DIRECTIP))
495 == PIPE_DIRECTW
496 ) {
497 /*
498 * Direct copy, bypassing a kernel buffer. We cannot
499 * mess with the direct-write buffer until
500 * PIPE_DIRECTIP is cleared. In order to prevent
501 * the pipe_write code from racing itself in
502 * direct_write, we set DIRECTIP when we clear
503 * DIRECTW after we have exhausted the buffer.
504 */
505 if (pipe_dwrite_sfbuf == 3)
506 rpipe->pipe_kvamask = 0;
507 pmap_qenter2(rpipe->pipe_kva, rpipe->pipe_map.xio_pages,
508 rpipe->pipe_map.xio_npages,
509 &rpipe->pipe_kvamask);
510 size = rpipe->pipe_map.xio_bytes;
511 if (size > (u_int)uio->uio_resid)
512 size = (u_int)uio->uio_resid;
513 va = (caddr_t)rpipe->pipe_kva +
514 rpipe->pipe_map.xio_offset;
515 error = uiomove(va, size, uio);
516 if (error)
517 break;
518 nread += size;
519 rpipe->pipe_map.xio_offset += size;
520 rpipe->pipe_map.xio_bytes -= size;
521 if (rpipe->pipe_map.xio_bytes == 0) {
522 rpipe->pipe_state |= PIPE_DIRECTIP;
523 rpipe->pipe_state &= ~PIPE_DIRECTW;
524 wakeup(rpipe);
525 }
526 } else if (rpipe->pipe_map.xio_bytes &&
527 rpipe->pipe_feature == PIPE_SFBUF1 &&
528 (rpipe->pipe_state & (PIPE_DIRECTW|PIPE_DIRECTIP))
529 == PIPE_DIRECTW
530 ) {
531 /*
532 * Direct copy, bypassing a kernel buffer. We cannot
533 * mess with the direct-write buffer until
534 * PIPE_DIRECTIP is cleared. In order to prevent
535 * the pipe_write code from racing itself in
536 * direct_write, we set DIRECTIP when we clear
537 * DIRECTW after we have exhausted the buffer.
538 */
24712b90 539 error = xio_uio_copy(&rpipe->pipe_map, uio, &size);
984263bc
MD
540 if (error)
541 break;
542 nread += size;
24712b90
MD
543 if (rpipe->pipe_map.xio_bytes == 0) {
544 rpipe->pipe_state |= PIPE_DIRECTIP;
984263bc
MD
545 rpipe->pipe_state &= ~PIPE_DIRECTW;
546 wakeup(rpipe);
547 }
548#endif
549 } else {
550 /*
551 * detect EOF condition
552 * read returns 0 on EOF, no need to set error
553 */
554 if (rpipe->pipe_state & PIPE_EOF)
555 break;
556
557 /*
558 * If the "write-side" has been blocked, wake it up now.
559 */
560 if (rpipe->pipe_state & PIPE_WANTW) {
561 rpipe->pipe_state &= ~PIPE_WANTW;
562 wakeup(rpipe);
563 }
564
565 /*
566 * Break if some data was read.
567 */
568 if (nread > 0)
569 break;
570
571 /*
8100156a
MD
572 * Unlock the pipe buffer for our remaining
573 * processing. We will either break out with an
574 * error or we will sleep and relock to loop.
984263bc
MD
575 */
576 pipeunlock(rpipe);
577
578 /*
579 * Handle non-blocking mode operation or
580 * wait for more data.
581 */
582 if (fp->f_flag & FNONBLOCK) {
583 error = EAGAIN;
584 } else {
585 rpipe->pipe_state |= PIPE_WANTR;
0a3f9b47 586 if ((error = tsleep(rpipe, PCATCH|PNORESCHED,
377d4740 587 "piperd", 0)) == 0) {
984263bc 588 error = pipelock(rpipe, 1);
377d4740 589 }
984263bc
MD
590 }
591 if (error)
592 goto unlocked_error;
593 }
594 }
595 pipeunlock(rpipe);
596
597 if (error == 0)
598 vfs_timestamp(&rpipe->pipe_atime);
599unlocked_error:
600 --rpipe->pipe_busy;
601
602 /*
603 * PIPE_WANT processing only makes sense if pipe_busy is 0.
604 */
605 if ((rpipe->pipe_busy == 0) && (rpipe->pipe_state & PIPE_WANT)) {
606 rpipe->pipe_state &= ~(PIPE_WANT|PIPE_WANTW);
607 wakeup(rpipe);
608 } else if (rpipe->pipe_buffer.cnt < MINPIPESIZE) {
609 /*
610 * Handle write blocking hysteresis.
611 */
612 if (rpipe->pipe_state & PIPE_WANTW) {
613 rpipe->pipe_state &= ~PIPE_WANTW;
614 wakeup(rpipe);
615 }
616 }
617
618 if ((rpipe->pipe_buffer.size - rpipe->pipe_buffer.cnt) >= PIPE_BUF)
619 pipeselwakeup(rpipe);
984263bc
MD
620 return (error);
621}
622
623#ifndef PIPE_NODIRECT
624/*
625 * Map the sending processes' buffer into kernel space and wire it.
626 * This is similar to a physical write operation.
627 */
628static int
629pipe_build_write_buffer(wpipe, uio)
630 struct pipe *wpipe;
631 struct uio *uio;
632{
24712b90 633 int error;
984263bc 634 u_int size;
984263bc
MD
635
636 size = (u_int) uio->uio_iov->iov_len;
637 if (size > wpipe->pipe_buffer.size)
638 size = wpipe->pipe_buffer.size;
639
24712b90
MD
640 error = xio_init_ubuf(&wpipe->pipe_map, uio->uio_iov->iov_base,
641 size, XIOF_READ);
642 if (error)
643 return(error);
984263bc 644
8100156a
MD
645 /*
646 * Create a kernel map for KMEM and SFBUF2 copy modes. SFBUF2 will
647 * map the pages on the target while KMEM maps the pages now.
648 */
649 switch(wpipe->pipe_feature) {
650 case PIPE_KMEM:
651 case PIPE_SFBUF2:
652 if (wpipe->pipe_kva == NULL) {
653 wpipe->pipe_kva =
5986cc14 654 kmem_alloc_nofault(kernel_map, XIO_INTERNAL_SIZE);
8100156a
MD
655 wpipe->pipe_kvamask = 0;
656 }
657 if (wpipe->pipe_feature == PIPE_KMEM) {
658 pmap_qenter(wpipe->pipe_kva, wpipe->pipe_map.xio_pages,
659 wpipe->pipe_map.xio_npages);
660 }
661 break;
662 default:
663 break;
664 }
665
c617bada 666 /*
5986cc14
MD
667 * And update the uio data. The XIO might have loaded fewer bytes
668 * then requested so reload 'size'.
c617bada 669 */
5986cc14 670 size = wpipe->pipe_map.xio_bytes;
984263bc
MD
671 uio->uio_iov->iov_len -= size;
672 uio->uio_iov->iov_base += size;
673 if (uio->uio_iov->iov_len == 0)
674 uio->uio_iov++;
675 uio->uio_resid -= size;
676 uio->uio_offset += size;
677 return (0);
678}
679
984263bc
MD
680/*
681 * In the case of a signal, the writing process might go away. This
682 * code copies the data into the circular buffer so that the source
683 * pages can be freed without loss of data.
684 */
685static void
686pipe_clone_write_buffer(wpipe)
687 struct pipe *wpipe;
688{
689 int size;
984263bc 690
24712b90 691 size = wpipe->pipe_map.xio_bytes;
984263bc 692
abd73068
MD
693 KKASSERT(size <= wpipe->pipe_buffer.size);
694
984263bc
MD
695 wpipe->pipe_buffer.in = size;
696 wpipe->pipe_buffer.out = 0;
697 wpipe->pipe_buffer.cnt = size;
24712b90
MD
698 wpipe->pipe_state &= ~(PIPE_DIRECTW | PIPE_DIRECTIP);
699
700 xio_copy_xtok(&wpipe->pipe_map, wpipe->pipe_buffer.buffer, size);
701 xio_release(&wpipe->pipe_map);
8100156a 702 if (wpipe->pipe_kva) {
910cc692 703 pmap_qremove(wpipe->pipe_kva, XIO_INTERNAL_PAGES);
8100156a
MD
704 kmem_free(kernel_map, wpipe->pipe_kva, XIO_INTERNAL_SIZE);
705 wpipe->pipe_kva = NULL;
706 }
984263bc
MD
707}
708
709/*
710 * This implements the pipe buffer write mechanism. Note that only
711 * a direct write OR a normal pipe write can be pending at any given time.
712 * If there are any characters in the pipe buffer, the direct write will
713 * be deferred until the receiving process grabs all of the bytes from
714 * the pipe buffer. Then the direct mapping write is set-up.
715 */
716static int
717pipe_direct_write(wpipe, uio)
718 struct pipe *wpipe;
719 struct uio *uio;
720{
721 int error;
722
723retry:
24712b90 724 while (wpipe->pipe_state & (PIPE_DIRECTW|PIPE_DIRECTIP)) {
984263bc
MD
725 if (wpipe->pipe_state & PIPE_WANTR) {
726 wpipe->pipe_state &= ~PIPE_WANTR;
727 wakeup(wpipe);
728 }
729 wpipe->pipe_state |= PIPE_WANTW;
377d4740 730 error = tsleep(wpipe, PCATCH, "pipdww", 0);
984263bc 731 if (error)
24712b90 732 goto error2;
984263bc
MD
733 if (wpipe->pipe_state & PIPE_EOF) {
734 error = EPIPE;
24712b90 735 goto error2;
984263bc
MD
736 }
737 }
24712b90 738 KKASSERT(wpipe->pipe_map.xio_bytes == 0);
984263bc
MD
739 if (wpipe->pipe_buffer.cnt > 0) {
740 if (wpipe->pipe_state & PIPE_WANTR) {
741 wpipe->pipe_state &= ~PIPE_WANTR;
742 wakeup(wpipe);
743 }
744
745 wpipe->pipe_state |= PIPE_WANTW;
377d4740 746 error = tsleep(wpipe, PCATCH, "pipdwc", 0);
984263bc 747 if (error)
24712b90 748 goto error2;
984263bc
MD
749 if (wpipe->pipe_state & PIPE_EOF) {
750 error = EPIPE;
24712b90 751 goto error2;
984263bc
MD
752 }
753 goto retry;
754 }
755
c617bada
MD
756 /*
757 * Build our direct-write buffer
758 */
24712b90 759 wpipe->pipe_state |= PIPE_DIRECTW | PIPE_DIRECTIP;
984263bc 760 error = pipe_build_write_buffer(wpipe, uio);
24712b90 761 if (error)
984263bc 762 goto error1;
24712b90 763 wpipe->pipe_state &= ~PIPE_DIRECTIP;
984263bc 764
c617bada
MD
765 /*
766 * Wait until the receiver has snarfed the data. Since we are likely
767 * going to sleep we optimize the case and yield synchronously,
768 * possibly avoiding the tsleep().
769 */
984263bc
MD
770 error = 0;
771 while (!error && (wpipe->pipe_state & PIPE_DIRECTW)) {
772 if (wpipe->pipe_state & PIPE_EOF) {
773 pipelock(wpipe, 0);
24712b90 774 xio_release(&wpipe->pipe_map);
8100156a 775 if (wpipe->pipe_kva) {
910cc692 776 pmap_qremove(wpipe->pipe_kva, XIO_INTERNAL_PAGES);
8100156a
MD
777 kmem_free(kernel_map, wpipe->pipe_kva, XIO_INTERNAL_SIZE);
778 wpipe->pipe_kva = NULL;
779 }
984263bc
MD
780 pipeunlock(wpipe);
781 pipeselwakeup(wpipe);
782 error = EPIPE;
783 goto error1;
784 }
785 if (wpipe->pipe_state & PIPE_WANTR) {
786 wpipe->pipe_state &= ~PIPE_WANTR;
787 wakeup(wpipe);
788 }
789 pipeselwakeup(wpipe);
0a3f9b47 790 error = tsleep(wpipe, PCATCH|PNORESCHED, "pipdwt", 0);
984263bc 791 }
984263bc
MD
792 pipelock(wpipe,0);
793 if (wpipe->pipe_state & PIPE_DIRECTW) {
794 /*
795 * this bit of trickery substitutes a kernel buffer for
796 * the process that might be going away.
797 */
798 pipe_clone_write_buffer(wpipe);
24712b90 799 KKASSERT((wpipe->pipe_state & PIPE_DIRECTIP) == 0);
984263bc 800 } else {
910cc692
MD
801 /*
802 * note: The pipe_kva mapping is not qremove'd here. For
803 * legacy PIPE_KMEM mode this constitutes an improvement
804 * over the original FreeBSD-4 algorithm. For PIPE_SFBUF2
805 * mode the kva mapping must not be removed to get the
806 * caching benefit.
807 *
808 * For testing purposes we will give the original algorithm
809 * the benefit of the doubt 'what it could have been', and
810 * keep the optimization.
811 */
24712b90
MD
812 KKASSERT(wpipe->pipe_state & PIPE_DIRECTIP);
813 xio_release(&wpipe->pipe_map);
814 wpipe->pipe_state &= ~PIPE_DIRECTIP;
984263bc
MD
815 }
816 pipeunlock(wpipe);
817 return (error);
818
24712b90
MD
819 /*
820 * Direct-write error, clear the direct write flags.
821 */
984263bc 822error1:
24712b90
MD
823 wpipe->pipe_state &= ~(PIPE_DIRECTW | PIPE_DIRECTIP);
824 /* fallthrough */
825
826 /*
827 * General error, wakeup the other side if it happens to be sleeping.
828 */
829error2:
984263bc
MD
830 wakeup(wpipe);
831 return (error);
832}
833#endif
834
835static int
dadab5e9
MD
836pipe_write(struct file *fp, struct uio *uio, struct ucred *cred,
837 int flags, struct thread *td)
984263bc
MD
838{
839 int error = 0;
840 int orig_resid;
841 struct pipe *wpipe, *rpipe;
842
843 rpipe = (struct pipe *) fp->f_data;
844 wpipe = rpipe->pipe_peer;
845
846 /*
847 * detect loss of pipe read side, issue SIGPIPE if lost.
848 */
849 if ((wpipe == NULL) || (wpipe->pipe_state & PIPE_EOF)) {
850 return (EPIPE);
851 }
852 ++wpipe->pipe_busy;
853
854 /*
855 * If it is advantageous to resize the pipe buffer, do
856 * so.
857 */
858 if ((uio->uio_resid > PIPE_SIZE) &&
fc7d5181 859 (pipe_nbig < pipe_maxbig) &&
24712b90 860 (wpipe->pipe_state & (PIPE_DIRECTW|PIPE_DIRECTIP)) == 0 &&
984263bc
MD
861 (wpipe->pipe_buffer.size <= PIPE_SIZE) &&
862 (wpipe->pipe_buffer.cnt == 0)) {
863
864 if ((error = pipelock(wpipe,1)) == 0) {
865 if (pipespace(wpipe, BIG_PIPE_SIZE) == 0)
fc7d5181 866 pipe_nbig++;
984263bc
MD
867 pipeunlock(wpipe);
868 }
869 }
870
871 /*
872 * If an early error occured unbusy and return, waking up any pending
873 * readers.
874 */
875 if (error) {
876 --wpipe->pipe_busy;
877 if ((wpipe->pipe_busy == 0) &&
878 (wpipe->pipe_state & PIPE_WANT)) {
879 wpipe->pipe_state &= ~(PIPE_WANT | PIPE_WANTR);
880 wakeup(wpipe);
881 }
882 return(error);
883 }
884
885 KASSERT(wpipe->pipe_buffer.buffer != NULL, ("pipe buffer gone"));
886
887 orig_resid = uio->uio_resid;
888
889 while (uio->uio_resid) {
890 int space;
891
892#ifndef PIPE_NODIRECT
893 /*
894 * If the transfer is large, we can gain performance if
895 * we do process-to-process copies directly.
896 * If the write is non-blocking, we don't use the
897 * direct write mechanism.
898 *
899 * The direct write mechanism will detect the reader going
900 * away on us.
901 */
8100156a
MD
902 if ((uio->uio_iov->iov_len >= PIPE_MINDIRECT ||
903 pipe_dwrite_enable > 1) &&
904 (fp->f_flag & FNONBLOCK) == 0 &&
905 pipe_dwrite_enable) {
984263bc
MD
906 error = pipe_direct_write( wpipe, uio);
907 if (error)
908 break;
909 continue;
910 }
911#endif
912
913 /*
914 * Pipe buffered writes cannot be coincidental with
915 * direct writes. We wait until the currently executing
916 * direct write is completed before we start filling the
917 * pipe buffer. We break out if a signal occurs or the
918 * reader goes away.
919 */
920 retrywrite:
24712b90 921 while (wpipe->pipe_state & (PIPE_DIRECTW|PIPE_DIRECTIP)) {
984263bc
MD
922 if (wpipe->pipe_state & PIPE_WANTR) {
923 wpipe->pipe_state &= ~PIPE_WANTR;
924 wakeup(wpipe);
925 }
377d4740 926 error = tsleep(wpipe, PCATCH, "pipbww", 0);
984263bc
MD
927 if (wpipe->pipe_state & PIPE_EOF)
928 break;
929 if (error)
930 break;
931 }
932 if (wpipe->pipe_state & PIPE_EOF) {
933 error = EPIPE;
934 break;
935 }
936
937 space = wpipe->pipe_buffer.size - wpipe->pipe_buffer.cnt;
938
939 /* Writes of size <= PIPE_BUF must be atomic. */
940 if ((space < uio->uio_resid) && (orig_resid <= PIPE_BUF))
941 space = 0;
942
c617bada
MD
943 /*
944 * Write to fill, read size handles write hysteresis. Also
945 * additional restrictions can cause select-based non-blocking
946 * writes to spin.
947 */
948 if (space > 0) {
984263bc
MD
949 if ((error = pipelock(wpipe,1)) == 0) {
950 int size; /* Transfer size */
951 int segsize; /* first segment to transfer */
952
953 /*
954 * It is possible for a direct write to
955 * slip in on us... handle it here...
956 */
24712b90 957 if (wpipe->pipe_state & (PIPE_DIRECTW|PIPE_DIRECTIP)) {
984263bc
MD
958 pipeunlock(wpipe);
959 goto retrywrite;
960 }
961 /*
962 * If a process blocked in uiomove, our
963 * value for space might be bad.
964 *
965 * XXX will we be ok if the reader has gone
966 * away here?
967 */
968 if (space > wpipe->pipe_buffer.size -
969 wpipe->pipe_buffer.cnt) {
970 pipeunlock(wpipe);
971 goto retrywrite;
972 }
973
974 /*
975 * Transfer size is minimum of uio transfer
976 * and free space in pipe buffer.
977 */
978 if (space > uio->uio_resid)
979 size = uio->uio_resid;
980 else
981 size = space;
982 /*
983 * First segment to transfer is minimum of
984 * transfer size and contiguous space in
985 * pipe buffer. If first segment to transfer
986 * is less than the transfer size, we've got
987 * a wraparound in the buffer.
988 */
989 segsize = wpipe->pipe_buffer.size -
990 wpipe->pipe_buffer.in;
991 if (segsize > size)
992 segsize = size;
993
994 /* Transfer first segment */
995
996 error = uiomove(&wpipe->pipe_buffer.buffer[wpipe->pipe_buffer.in],
997 segsize, uio);
998
999 if (error == 0 && segsize < size) {
1000 /*
1001 * Transfer remaining part now, to
1002 * support atomic writes. Wraparound
1003 * happened.
1004 */
1005 if (wpipe->pipe_buffer.in + segsize !=
1006 wpipe->pipe_buffer.size)
1007 panic("Expected pipe buffer wraparound disappeared");
1008
1009 error = uiomove(&wpipe->pipe_buffer.buffer[0],
1010 size - segsize, uio);
1011 }
1012 if (error == 0) {
1013 wpipe->pipe_buffer.in += size;
1014 if (wpipe->pipe_buffer.in >=
1015 wpipe->pipe_buffer.size) {
1016 if (wpipe->pipe_buffer.in != size - segsize + wpipe->pipe_buffer.size)
1017 panic("Expected wraparound bad");
1018 wpipe->pipe_buffer.in = size - segsize;
1019 }
1020
1021 wpipe->pipe_buffer.cnt += size;
1022 if (wpipe->pipe_buffer.cnt > wpipe->pipe_buffer.size)
1023 panic("Pipe buffer overflow");
1024
1025 }
1026 pipeunlock(wpipe);
1027 }
1028 if (error)
1029 break;
1030
1031 } else {
1032 /*
c617bada
MD
1033 * If the "read-side" has been blocked, wake it up now
1034 * and yield to let it drain synchronously rather
1035 * then block.
984263bc
MD
1036 */
1037 if (wpipe->pipe_state & PIPE_WANTR) {
1038 wpipe->pipe_state &= ~PIPE_WANTR;
1039 wakeup(wpipe);
1040 }
1041
1042 /*
1043 * don't block on non-blocking I/O
1044 */
1045 if (fp->f_flag & FNONBLOCK) {
1046 error = EAGAIN;
1047 break;
1048 }
1049
1050 /*
1051 * We have no more space and have something to offer,
1052 * wake up select/poll.
1053 */
1054 pipeselwakeup(wpipe);
1055
1056 wpipe->pipe_state |= PIPE_WANTW;
0a3f9b47 1057 error = tsleep(wpipe, PCATCH|PNORESCHED, "pipewr", 0);
984263bc
MD
1058 if (error != 0)
1059 break;
1060 /*
1061 * If read side wants to go away, we just issue a signal
1062 * to ourselves.
1063 */
1064 if (wpipe->pipe_state & PIPE_EOF) {
1065 error = EPIPE;
1066 break;
1067 }
1068 }
1069 }
1070
1071 --wpipe->pipe_busy;
1072
1073 if ((wpipe->pipe_busy == 0) && (wpipe->pipe_state & PIPE_WANT)) {
1074 wpipe->pipe_state &= ~(PIPE_WANT | PIPE_WANTR);
1075 wakeup(wpipe);
1076 } else if (wpipe->pipe_buffer.cnt > 0) {
1077 /*
1078 * If we have put any characters in the buffer, we wake up
1079 * the reader.
1080 */
1081 if (wpipe->pipe_state & PIPE_WANTR) {
1082 wpipe->pipe_state &= ~PIPE_WANTR;
1083 wakeup(wpipe);
1084 }
1085 }
1086
1087 /*
1088 * Don't return EPIPE if I/O was successful
1089 */
1090 if ((wpipe->pipe_buffer.cnt == 0) &&
1091 (uio->uio_resid == 0) &&
1092 (error == EPIPE)) {
1093 error = 0;
1094 }
1095
1096 if (error == 0)
1097 vfs_timestamp(&wpipe->pipe_mtime);
1098
1099 /*
1100 * We have something to offer,
1101 * wake up select/poll.
1102 */
1103 if (wpipe->pipe_buffer.cnt)
1104 pipeselwakeup(wpipe);
1105
1106 return (error);
1107}
1108
1109/*
1110 * we implement a very minimal set of ioctls for compatibility with sockets.
1111 */
1112int
dadab5e9 1113pipe_ioctl(struct file *fp, u_long cmd, caddr_t data, struct thread *td)
984263bc
MD
1114{
1115 struct pipe *mpipe = (struct pipe *)fp->f_data;
1116
1117 switch (cmd) {
1118
1119 case FIONBIO:
1120 return (0);
1121
1122 case FIOASYNC:
1123 if (*(int *)data) {
1124 mpipe->pipe_state |= PIPE_ASYNC;
1125 } else {
1126 mpipe->pipe_state &= ~PIPE_ASYNC;
1127 }
1128 return (0);
1129
1130 case FIONREAD:
24712b90
MD
1131 if (mpipe->pipe_state & PIPE_DIRECTW) {
1132 *(int *)data = mpipe->pipe_map.xio_bytes;
1133 } else {
984263bc 1134 *(int *)data = mpipe->pipe_buffer.cnt;
24712b90 1135 }
984263bc
MD
1136 return (0);
1137
1138 case FIOSETOWN:
1139 return (fsetown(*(int *)data, &mpipe->pipe_sigio));
1140
1141 case FIOGETOWN:
1142 *(int *)data = fgetown(mpipe->pipe_sigio);
1143 return (0);
1144
1145 /* This is deprecated, FIOSETOWN should be used instead. */
1146 case TIOCSPGRP:
1147 return (fsetown(-(*(int *)data), &mpipe->pipe_sigio));
1148
1149 /* This is deprecated, FIOGETOWN should be used instead. */
1150 case TIOCGPGRP:
1151 *(int *)data = -fgetown(mpipe->pipe_sigio);
1152 return (0);
1153
1154 }
1155 return (ENOTTY);
1156}
1157
1158int
dadab5e9 1159pipe_poll(struct file *fp, int events, struct ucred *cred, struct thread *td)
984263bc
MD
1160{
1161 struct pipe *rpipe = (struct pipe *)fp->f_data;
1162 struct pipe *wpipe;
1163 int revents = 0;
1164
1165 wpipe = rpipe->pipe_peer;
1166 if (events & (POLLIN | POLLRDNORM))
1167 if ((rpipe->pipe_state & PIPE_DIRECTW) ||
1168 (rpipe->pipe_buffer.cnt > 0) ||
1169 (rpipe->pipe_state & PIPE_EOF))
1170 revents |= events & (POLLIN | POLLRDNORM);
1171
1172 if (events & (POLLOUT | POLLWRNORM))
1173 if (wpipe == NULL || (wpipe->pipe_state & PIPE_EOF) ||
1174 (((wpipe->pipe_state & PIPE_DIRECTW) == 0) &&
1175 (wpipe->pipe_buffer.size - wpipe->pipe_buffer.cnt) >= PIPE_BUF))
1176 revents |= events & (POLLOUT | POLLWRNORM);
1177
1178 if ((rpipe->pipe_state & PIPE_EOF) ||
1179 (wpipe == NULL) ||
1180 (wpipe->pipe_state & PIPE_EOF))
1181 revents |= POLLHUP;
1182
1183 if (revents == 0) {
1184 if (events & (POLLIN | POLLRDNORM)) {
dadab5e9 1185 selrecord(td, &rpipe->pipe_sel);
984263bc
MD
1186 rpipe->pipe_state |= PIPE_SEL;
1187 }
1188
1189 if (events & (POLLOUT | POLLWRNORM)) {
dadab5e9 1190 selrecord(td, &wpipe->pipe_sel);
984263bc
MD
1191 wpipe->pipe_state |= PIPE_SEL;
1192 }
1193 }
1194
1195 return (revents);
1196}
1197
1198static int
dadab5e9 1199pipe_stat(struct file *fp, struct stat *ub, struct thread *td)
984263bc
MD
1200{
1201 struct pipe *pipe = (struct pipe *)fp->f_data;
1202
1203 bzero((caddr_t)ub, sizeof(*ub));
1204 ub->st_mode = S_IFIFO;
1205 ub->st_blksize = pipe->pipe_buffer.size;
1206 ub->st_size = pipe->pipe_buffer.cnt;
739ae7cb
MD
1207 if (ub->st_size == 0 && (pipe->pipe_state & PIPE_DIRECTW))
1208 ub->st_size = pipe->pipe_map.xio_bytes;
984263bc
MD
1209 ub->st_blocks = (ub->st_size + ub->st_blksize - 1) / ub->st_blksize;
1210 ub->st_atimespec = pipe->pipe_atime;
1211 ub->st_mtimespec = pipe->pipe_mtime;
1212 ub->st_ctimespec = pipe->pipe_ctime;
1213 /*
1214 * Left as 0: st_dev, st_ino, st_nlink, st_uid, st_gid, st_rdev,
1215 * st_flags, st_gen.
1216 * XXX (st_dev, st_ino) should be unique.
1217 */
1218 return (0);
1219}
1220
1221/* ARGSUSED */
1222static int
dadab5e9 1223pipe_close(struct file *fp, struct thread *td)
984263bc
MD
1224{
1225 struct pipe *cpipe = (struct pipe *)fp->f_data;
1226
1227 fp->f_ops = &badfileops;
1228 fp->f_data = NULL;
1229 funsetown(cpipe->pipe_sigio);
1230 pipeclose(cpipe);
1231 return (0);
1232}
1233
1234static void
dadab5e9 1235pipe_free_kmem(struct pipe *cpipe)
984263bc 1236{
984263bc
MD
1237 if (cpipe->pipe_buffer.buffer != NULL) {
1238 if (cpipe->pipe_buffer.size > PIPE_SIZE)
fc7d5181 1239 --pipe_nbig;
984263bc
MD
1240 kmem_free(kernel_map,
1241 (vm_offset_t)cpipe->pipe_buffer.buffer,
1242 cpipe->pipe_buffer.size);
1243 cpipe->pipe_buffer.buffer = NULL;
fc7d5181 1244 cpipe->pipe_buffer.object = NULL;
984263bc
MD
1245 }
1246#ifndef PIPE_NODIRECT
24712b90
MD
1247 KKASSERT(cpipe->pipe_map.xio_bytes == 0 &&
1248 cpipe->pipe_map.xio_offset == 0 &&
1249 cpipe->pipe_map.xio_npages == 0);
984263bc
MD
1250#endif
1251}
1252
1253/*
1254 * shutdown the pipe
1255 */
1256static void
dadab5e9 1257pipeclose(struct pipe *cpipe)
984263bc 1258{
fc7d5181 1259 globaldata_t gd;
984263bc
MD
1260 struct pipe *ppipe;
1261
fc7d5181
MD
1262 if (cpipe == NULL)
1263 return;
984263bc 1264
fc7d5181 1265 pipeselwakeup(cpipe);
984263bc 1266
fc7d5181
MD
1267 /*
1268 * If the other side is blocked, wake it up saying that
1269 * we want to close it down.
1270 */
1271 while (cpipe->pipe_busy) {
1272 wakeup(cpipe);
1273 cpipe->pipe_state |= PIPE_WANT | PIPE_EOF;
1274 tsleep(cpipe, 0, "pipecl", 0);
1275 }
984263bc 1276
fc7d5181
MD
1277 /*
1278 * Disconnect from peer
1279 */
1280 if ((ppipe = cpipe->pipe_peer) != NULL) {
1281 pipeselwakeup(ppipe);
1282
1283 ppipe->pipe_state |= PIPE_EOF;
1284 wakeup(ppipe);
1285 KNOTE(&ppipe->pipe_sel.si_note, 0);
1286 ppipe->pipe_peer = NULL;
1287 }
1288
8100156a 1289 if (cpipe->pipe_kva) {
910cc692 1290 pmap_qremove(cpipe->pipe_kva, XIO_INTERNAL_PAGES);
8100156a
MD
1291 kmem_free(kernel_map, cpipe->pipe_kva, XIO_INTERNAL_SIZE);
1292 cpipe->pipe_kva = NULL;
1293 }
1294
fc7d5181
MD
1295 /*
1296 * free or cache resources
1297 */
1298 gd = mycpu;
1299 if (gd->gd_pipeqcount >= pipe_maxcache ||
1300 cpipe->pipe_buffer.size != PIPE_SIZE
1301 ) {
984263bc 1302 pipe_free_kmem(cpipe);
fc7d5181
MD
1303 free(cpipe, M_PIPE);
1304 } else {
24712b90
MD
1305 KKASSERT(cpipe->pipe_map.xio_npages == 0 &&
1306 cpipe->pipe_map.xio_bytes == 0 &&
1307 cpipe->pipe_map.xio_offset == 0);
fc7d5181
MD
1308 cpipe->pipe_state = 0;
1309 cpipe->pipe_busy = 0;
fc7d5181
MD
1310 cpipe->pipe_peer = gd->gd_pipeq;
1311 gd->gd_pipeq = cpipe;
1312 ++gd->gd_pipeqcount;
984263bc
MD
1313 }
1314}
1315
1316/*ARGSUSED*/
1317static int
1318pipe_kqfilter(struct file *fp, struct knote *kn)
1319{
1320 struct pipe *cpipe = (struct pipe *)kn->kn_fp->f_data;
1321
1322 switch (kn->kn_filter) {
1323 case EVFILT_READ:
1324 kn->kn_fop = &pipe_rfiltops;
1325 break;
1326 case EVFILT_WRITE:
1327 kn->kn_fop = &pipe_wfiltops;
1328 cpipe = cpipe->pipe_peer;
1329 if (cpipe == NULL)
1330 /* other end of pipe has been closed */
41f57d15 1331 return (EPIPE);
984263bc
MD
1332 break;
1333 default:
1334 return (1);
1335 }
1336 kn->kn_hook = (caddr_t)cpipe;
1337
1338 SLIST_INSERT_HEAD(&cpipe->pipe_sel.si_note, kn, kn_selnext);
1339 return (0);
1340}
1341
1342static void
1343filt_pipedetach(struct knote *kn)
1344{
1345 struct pipe *cpipe = (struct pipe *)kn->kn_hook;
1346
1347 SLIST_REMOVE(&cpipe->pipe_sel.si_note, kn, knote, kn_selnext);
1348}
1349
1350/*ARGSUSED*/
1351static int
1352filt_piperead(struct knote *kn, long hint)
1353{
1354 struct pipe *rpipe = (struct pipe *)kn->kn_fp->f_data;
1355 struct pipe *wpipe = rpipe->pipe_peer;
1356
1357 kn->kn_data = rpipe->pipe_buffer.cnt;
1358 if ((kn->kn_data == 0) && (rpipe->pipe_state & PIPE_DIRECTW))
24712b90 1359 kn->kn_data = rpipe->pipe_map.xio_bytes;
984263bc
MD
1360
1361 if ((rpipe->pipe_state & PIPE_EOF) ||
1362 (wpipe == NULL) || (wpipe->pipe_state & PIPE_EOF)) {
1363 kn->kn_flags |= EV_EOF;
1364 return (1);
1365 }
1366 return (kn->kn_data > 0);
1367}
1368
1369/*ARGSUSED*/
1370static int
1371filt_pipewrite(struct knote *kn, long hint)
1372{
1373 struct pipe *rpipe = (struct pipe *)kn->kn_fp->f_data;
1374 struct pipe *wpipe = rpipe->pipe_peer;
1375
1376 if ((wpipe == NULL) || (wpipe->pipe_state & PIPE_EOF)) {
1377 kn->kn_data = 0;
1378 kn->kn_flags |= EV_EOF;
1379 return (1);
1380 }
1381 kn->kn_data = wpipe->pipe_buffer.size - wpipe->pipe_buffer.cnt;
1382 if (wpipe->pipe_state & PIPE_DIRECTW)
1383 kn->kn_data = 0;
1384
1385 return (kn->kn_data >= PIPE_BUF);
1386}