sys/kern: Adjust some function declaration vs. definition mismatches.
[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 $
20 */
21
22/*
23 * This file contains a high-performance replacement for the socket-based
24 * pipes scheme originally used in FreeBSD/4.4Lite. It does not support
25 * all features of sockets, but does do everything that pipes normally
26 * do.
27 */
984263bc
MD
28#include <sys/param.h>
29#include <sys/systm.h>
fc7d5181 30#include <sys/kernel.h>
984263bc
MD
31#include <sys/proc.h>
32#include <sys/fcntl.h>
33#include <sys/file.h>
34#include <sys/filedesc.h>
35#include <sys/filio.h>
36#include <sys/ttycom.h>
37#include <sys/stat.h>
984263bc
MD
38#include <sys/signalvar.h>
39#include <sys/sysproto.h>
40#include <sys/pipe.h>
41#include <sys/vnode.h>
42#include <sys/uio.h>
43#include <sys/event.h>
fc7d5181
MD
44#include <sys/globaldata.h>
45#include <sys/module.h>
46#include <sys/malloc.h>
47#include <sys/sysctl.h>
004d2de5 48#include <sys/socket.h>
984263bc
MD
49
50#include <vm/vm.h>
51#include <vm/vm_param.h>
52#include <sys/lock.h>
53#include <vm/vm_object.h>
54#include <vm/vm_kern.h>
55#include <vm/vm_extern.h>
56#include <vm/pmap.h>
57#include <vm/vm_map.h>
58#include <vm/vm_page.h>
59#include <vm/vm_zone.h>
60
dadab5e9 61#include <sys/file2.h>
607b0ed9 62#include <sys/signal2.h>
dadab5e9 63
8100156a 64#include <machine/cpufunc.h>
984263bc
MD
65
66/*
67 * interfaces to the outside world
68 */
402ed7e1 69static int pipe_read (struct file *fp, struct uio *uio,
87de5057 70 struct ucred *cred, int flags);
402ed7e1 71static int pipe_write (struct file *fp, struct uio *uio,
87de5057
MD
72 struct ucred *cred, int flags);
73static int pipe_close (struct file *fp);
74static int pipe_shutdown (struct file *fp, int how);
402ed7e1 75static int pipe_kqfilter (struct file *fp, struct knote *kn);
87de5057 76static int pipe_stat (struct file *fp, struct stat *sb, struct ucred *cred);
87baaf0c
MD
77static int pipe_ioctl (struct file *fp, u_long cmd, caddr_t data,
78 struct ucred *cred, struct sysmsg *msg);
984263bc
MD
79
80static struct fileops pipeops = {
b2d248cb
MD
81 .fo_read = pipe_read,
82 .fo_write = pipe_write,
83 .fo_ioctl = pipe_ioctl,
b2d248cb
MD
84 .fo_kqfilter = pipe_kqfilter,
85 .fo_stat = pipe_stat,
86 .fo_close = pipe_close,
87 .fo_shutdown = pipe_shutdown
984263bc
MD
88};
89
90static void filt_pipedetach(struct knote *kn);
91static int filt_piperead(struct knote *kn, long hint);
92static int filt_pipewrite(struct knote *kn, long hint);
93
94static struct filterops pipe_rfiltops =
a081e067 95 { FILTEROP_ISFD|FILTEROP_MPSAFE, NULL, filt_pipedetach, filt_piperead };
984263bc 96static struct filterops pipe_wfiltops =
a081e067 97 { FILTEROP_ISFD|FILTEROP_MPSAFE, NULL, filt_pipedetach, filt_pipewrite };
984263bc 98
fc7d5181 99MALLOC_DEFINE(M_PIPE, "pipe", "pipe structures");
984263bc
MD
100
101/*
102 * Default pipe buffer size(s), this can be kind-of large now because pipe
103 * space is pageable. The pipe code will try to maintain locality of
104 * reference for performance reasons, so small amounts of outstanding I/O
105 * will not wipe the cache.
106 */
107#define MINPIPESIZE (PIPE_SIZE/3)
108#define MAXPIPESIZE (2*PIPE_SIZE/3)
109
984263bc
MD
110/*
111 * Limit the number of "big" pipes
112 */
08593aa1 113#define LIMITBIGPIPES 64
fc7d5181
MD
114#define PIPEQ_MAX_CACHE 16 /* per-cpu pipe structure cache */
115
116static int pipe_maxbig = LIMITBIGPIPES;
117static int pipe_maxcache = PIPEQ_MAX_CACHE;
39b0a1af 118static int pipe_bigcount;
fc7d5181 119static int pipe_nbig;
fc7d5181
MD
120static int pipe_bcache_alloc;
121static int pipe_bkmem_alloc;
880ffa3a
MD
122static int pipe_rblocked_count;
123static int pipe_wblocked_count;
fc7d5181
MD
124
125SYSCTL_NODE(_kern, OID_AUTO, pipe, CTLFLAG_RW, 0, "Pipe operation");
126SYSCTL_INT(_kern_pipe, OID_AUTO, nbig,
8d67cbb3 127 CTLFLAG_RD, &pipe_nbig, 0, "number of big pipes allocated");
39b0a1af
MD
128SYSCTL_INT(_kern_pipe, OID_AUTO, bigcount,
129 CTLFLAG_RW, &pipe_bigcount, 0, "number of times pipe expanded");
880ffa3a
MD
130SYSCTL_INT(_kern_pipe, OID_AUTO, rblocked,
131 CTLFLAG_RW, &pipe_rblocked_count, 0, "number of times pipe expanded");
132SYSCTL_INT(_kern_pipe, OID_AUTO, wblocked,
133 CTLFLAG_RW, &pipe_wblocked_count, 0, "number of times pipe expanded");
fc7d5181
MD
134SYSCTL_INT(_kern_pipe, OID_AUTO, maxcache,
135 CTLFLAG_RW, &pipe_maxcache, 0, "max pipes cached per-cpu");
136SYSCTL_INT(_kern_pipe, OID_AUTO, maxbig,
137 CTLFLAG_RW, &pipe_maxbig, 0, "max number of big pipes");
880ffa3a
MD
138static int pipe_delay = 5000; /* 5uS default */
139SYSCTL_INT(_kern_pipe, OID_AUTO, delay,
140 CTLFLAG_RW, &pipe_delay, 0, "SMP delay optimization in ns");
fc7d5181
MD
141#if !defined(NO_PIPE_SYSCTL_STATS)
142SYSCTL_INT(_kern_pipe, OID_AUTO, bcache_alloc,
143 CTLFLAG_RW, &pipe_bcache_alloc, 0, "pipe buffer from pcpu cache");
fc7d5181
MD
144SYSCTL_INT(_kern_pipe, OID_AUTO, bkmem_alloc,
145 CTLFLAG_RW, &pipe_bkmem_alloc, 0, "pipe buffer from kmem");
fc7d5181 146#endif
984263bc 147
b12defdc
MD
148/*
149 * Auto-size pipe cache to reduce kmem allocations and frees.
150 */
151static
152void
153pipeinit(void *dummy)
154{
155 size_t mbytes = kmem_lim_size();
156
157 if (pipe_maxbig == LIMITBIGPIPES) {
158 if (mbytes >= 7 * 1024)
159 pipe_maxbig *= 2;
160 if (mbytes >= 15 * 1024)
161 pipe_maxbig *= 2;
162 }
163 if (pipe_maxcache == PIPEQ_MAX_CACHE) {
164 if (mbytes >= 7 * 1024)
165 pipe_maxcache *= 2;
166 if (mbytes >= 15 * 1024)
167 pipe_maxcache *= 2;
168 }
169}
170SYSINIT(kmem, SI_BOOT2_MACHDEP, SI_ORDER_ANY, pipeinit, NULL)
171
402ed7e1
RG
172static void pipeclose (struct pipe *cpipe);
173static void pipe_free_kmem (struct pipe *cpipe);
174static int pipe_create (struct pipe **cpipep);
402ed7e1 175static int pipespace (struct pipe *cpipe, int size);
984263bc 176
1ae37239 177static __inline void
8315ba5b 178pipewakeup(struct pipe *cpipe, int dosigio)
1ae37239 179{
8315ba5b 180 if (dosigio && (cpipe->pipe_state & PIPE_ASYNC) && cpipe->pipe_sigio) {
a8d3ab53 181 lwkt_gettoken(&sigio_token);
1ae37239 182 pgsigio(cpipe->pipe_sigio, SIGIO, 0);
a8d3ab53 183 lwkt_reltoken(&sigio_token);
1ae37239 184 }
13b050b0 185 KNOTE(&cpipe->pipe_kq.ki_note, 0);
1ae37239
MD
186}
187
188/*
189 * These routines are called before and after a UIO. The UIO
190 * may block, causing our held tokens to be lost temporarily.
191 *
192 * We use these routines to serialize reads against other reads
193 * and writes against other writes.
194 *
195 * The read token is held on entry so *ipp does not race.
196 */
197static __inline int
930bd151 198pipe_start_uio(struct pipe *cpipe, int *ipp)
1ae37239
MD
199{
200 int error;
201
202 while (*ipp) {
203 *ipp = -1;
204 error = tsleep(ipp, PCATCH, "pipexx", 0);
205 if (error)
206 return (error);
207 }
208 *ipp = 1;
209 return (0);
210}
211
212static __inline void
930bd151 213pipe_end_uio(struct pipe *cpipe, int *ipp)
1ae37239
MD
214{
215 if (*ipp < 0) {
216 *ipp = 0;
217 wakeup(ipp);
218 } else {
930bd151 219 KKASSERT(*ipp > 0);
1ae37239
MD
220 *ipp = 0;
221 }
222}
223
984263bc
MD
224/*
225 * The pipe system call for the DTYPE_PIPE type of pipes
41c20dac 226 *
3919ced0
MD
227 * pipe_args(int dummy)
228 *
229 * MPSAFE
984263bc 230 */
984263bc 231int
753fd850 232sys_pipe(struct pipe_args *uap)
984263bc 233{
dadab5e9 234 struct thread *td = curthread;
f3a2d8c4 235 struct filedesc *fdp = td->td_proc->p_fd;
984263bc
MD
236 struct file *rf, *wf;
237 struct pipe *rpipe, *wpipe;
90b9818c 238 int fd1, fd2, error;
984263bc 239
984263bc
MD
240 rpipe = wpipe = NULL;
241 if (pipe_create(&rpipe) || pipe_create(&wpipe)) {
242 pipeclose(rpipe);
243 pipeclose(wpipe);
244 return (ENFILE);
245 }
246
f3a2d8c4 247 error = falloc(td->td_lwp, &rf, &fd1);
984263bc
MD
248 if (error) {
249 pipeclose(rpipe);
250 pipeclose(wpipe);
251 return (error);
252 }
c7114eea 253 uap->sysmsg_fds[0] = fd1;
984263bc
MD
254
255 /*
256 * Warning: once we've gotten past allocation of the fd for the
257 * read-side, we can only drop the read side via fdrop() in order
258 * to avoid races against processes which manage to dup() the read
259 * side while we are blocked trying to allocate the write side.
260 */
984263bc 261 rf->f_type = DTYPE_PIPE;
fbb4eeab 262 rf->f_flag = FREAD | FWRITE;
984263bc 263 rf->f_ops = &pipeops;
fbb4eeab 264 rf->f_data = rpipe;
f3a2d8c4 265 error = falloc(td->td_lwp, &wf, &fd2);
984263bc 266 if (error) {
f3a2d8c4 267 fsetfd(fdp, NULL, fd1);
9f87144f 268 fdrop(rf);
984263bc
MD
269 /* rpipe has been closed by fdrop(). */
270 pipeclose(wpipe);
271 return (error);
272 }
984263bc 273 wf->f_type = DTYPE_PIPE;
fbb4eeab 274 wf->f_flag = FREAD | FWRITE;
984263bc 275 wf->f_ops = &pipeops;
fbb4eeab 276 wf->f_data = wpipe;
c7114eea 277 uap->sysmsg_fds[1] = fd2;
984263bc 278
1ae37239
MD
279 rpipe->pipe_slock = kmalloc(sizeof(struct lock),
280 M_PIPE, M_WAITOK|M_ZERO);
281 wpipe->pipe_slock = rpipe->pipe_slock;
984263bc
MD
282 rpipe->pipe_peer = wpipe;
283 wpipe->pipe_peer = rpipe;
1ae37239 284 lockinit(rpipe->pipe_slock, "pipecl", 0, 0);
259b8ea0 285
1ae37239
MD
286 /*
287 * Once activated the peer relationship remains valid until
288 * both sides are closed.
289 */
f3a2d8c4
MD
290 fsetfd(fdp, rf, fd1);
291 fsetfd(fdp, wf, fd2);
9f87144f
MD
292 fdrop(rf);
293 fdrop(wf);
984263bc
MD
294
295 return (0);
296}
297
298/*
299 * Allocate kva for pipe circular buffer, the space is pageable
300 * This routine will 'realloc' the size of a pipe safely, if it fails
301 * it will retain the old buffer.
302 * If it fails it will return ENOMEM.
303 */
304static int
fc7d5181 305pipespace(struct pipe *cpipe, int size)
984263bc
MD
306{
307 struct vm_object *object;
308 caddr_t buffer;
309 int npages, error;
310
fc7d5181
MD
311 npages = round_page(size) / PAGE_SIZE;
312 object = cpipe->pipe_buffer.object;
984263bc
MD
313
314 /*
fc7d5181
MD
315 * [re]create the object if necessary and reserve space for it
316 * in the kernel_map. The object and memory are pageable. On
317 * success, free the old resources before assigning the new
318 * ones.
984263bc 319 */
fc7d5181
MD
320 if (object == NULL || object->size != npages) {
321 object = vm_object_allocate(OBJT_DEFAULT, npages);
e4846942 322 buffer = (caddr_t)vm_map_min(&kernel_map);
984263bc 323
0adbcbd6
MD
324 error = vm_map_find(&kernel_map, object, NULL,
325 0, (vm_offset_t *)&buffer, size,
326 PAGE_SIZE,
9388fcaa 327 1, VM_MAPTYPE_NORMAL,
0adbcbd6 328 VM_PROT_ALL, VM_PROT_ALL, 0);
984263bc 329
fc7d5181
MD
330 if (error != KERN_SUCCESS) {
331 vm_object_deallocate(object);
332 return (ENOMEM);
333 }
fc7d5181
MD
334 pipe_free_kmem(cpipe);
335 cpipe->pipe_buffer.object = object;
336 cpipe->pipe_buffer.buffer = buffer;
337 cpipe->pipe_buffer.size = size;
338 ++pipe_bkmem_alloc;
339 } else {
340 ++pipe_bcache_alloc;
fc7d5181 341 }
c600838f
MD
342 cpipe->pipe_buffer.rindex = 0;
343 cpipe->pipe_buffer.windex = 0;
984263bc
MD
344 return (0);
345}
346
347/*
fc7d5181
MD
348 * Initialize and allocate VM and memory for pipe, pulling the pipe from
349 * our per-cpu cache if possible. For now make sure it is sized for the
350 * smaller PIPE_SIZE default.
984263bc
MD
351 */
352static int
c972a82f 353pipe_create(struct pipe **cpipep)
984263bc 354{
fc7d5181 355 globaldata_t gd = mycpu;
984263bc
MD
356 struct pipe *cpipe;
357 int error;
358
fc7d5181
MD
359 if ((cpipe = gd->gd_pipeq) != NULL) {
360 gd->gd_pipeq = cpipe->pipe_peer;
361 --gd->gd_pipeqcount;
362 cpipe->pipe_peer = NULL;
1ae37239 363 cpipe->pipe_wantwcnt = 0;
fc7d5181 364 } else {
efda3bd0 365 cpipe = kmalloc(sizeof(struct pipe), M_PIPE, M_WAITOK|M_ZERO);
fc7d5181
MD
366 }
367 *cpipep = cpipe;
368 if ((error = pipespace(cpipe, PIPE_SIZE)) != 0)
984263bc 369 return (error);
984263bc
MD
370 vfs_timestamp(&cpipe->pipe_ctime);
371 cpipe->pipe_atime = cpipe->pipe_ctime;
372 cpipe->pipe_mtime = cpipe->pipe_ctime;
a3c18566
MD
373 lwkt_token_init(&cpipe->pipe_rlock, "piper");
374 lwkt_token_init(&cpipe->pipe_wlock, "pipew");
984263bc
MD
375 return (0);
376}
377
984263bc 378static int
9ba76b73 379pipe_read(struct file *fp, struct uio *uio, struct ucred *cred, int fflags)
984263bc 380{
d9b2033e 381 struct pipe *rpipe;
8315ba5b 382 struct pipe *wpipe;
984263bc 383 int error;
607b0ed9 384 size_t nread = 0;
9ba76b73 385 int nbio;
c600838f 386 u_int size; /* total bytes available */
1ae37239 387 u_int nsize; /* total bytes to read */
c600838f 388 u_int rindex; /* contiguous bytes available */
b20720b5 389 int notify_writer;
607b0ed9
MD
390 int bigread;
391 int bigcount;
984263bc 392
a3ef5f2e
MD
393 atomic_set_int(&curthread->td_mpflags, TDF_MP_BATCH_DEMARC);
394
607b0ed9 395 if (uio->uio_resid == 0)
1ae37239
MD
396 return(0);
397
398 /*
399 * Setup locks, calculate nbio
400 */
1ae37239 401 rpipe = (struct pipe *)fp->f_data;
8315ba5b 402 wpipe = rpipe->pipe_peer;
3b998fa9 403 lwkt_gettoken(&rpipe->pipe_rlock);
984263bc 404
9ba76b73
MD
405 if (fflags & O_FBLOCKING)
406 nbio = 0;
407 else if (fflags & O_FNONBLOCKING)
408 nbio = 1;
409 else if (fp->f_flag & O_NONBLOCK)
410 nbio = 1;
411 else
412 nbio = 0;
413
1ae37239 414 /*
3720cffa 415 * Reads are serialized. Note however that pipe_buffer.buffer and
1ae37239
MD
416 * pipe_buffer.size can change out from under us when the number
417 * of bytes in the buffer are zero due to the write-side doing a
418 * pipespace().
419 */
420 error = pipe_start_uio(rpipe, &rpipe->pipe_rip);
421 if (error) {
3b998fa9 422 lwkt_reltoken(&rpipe->pipe_rlock);
1ae37239
MD
423 return (error);
424 }
b20720b5 425 notify_writer = 0;
607b0ed9
MD
426
427 bigread = (uio->uio_resid > 10 * 1024 * 1024);
428 bigcount = 10;
429
984263bc 430 while (uio->uio_resid) {
607b0ed9
MD
431 /*
432 * Don't hog the cpu.
433 */
434 if (bigread && --bigcount == 0) {
435 lwkt_user_yield();
436 bigcount = 10;
437 if (CURSIG(curthread->td_lwp)) {
438 error = EINTR;
439 break;
440 }
441 }
442
c600838f 443 size = rpipe->pipe_buffer.windex - rpipe->pipe_buffer.rindex;
1ae37239 444 cpu_lfence();
c600838f
MD
445 if (size) {
446 rindex = rpipe->pipe_buffer.rindex &
447 (rpipe->pipe_buffer.size - 1);
1ae37239
MD
448 nsize = size;
449 if (nsize > rpipe->pipe_buffer.size - rindex)
450 nsize = rpipe->pipe_buffer.size - rindex;
607b0ed9 451 nsize = szmin(nsize, uio->uio_resid);
c600838f
MD
452
453 error = uiomove(&rpipe->pipe_buffer.buffer[rindex],
1ae37239 454 nsize, uio);
984263bc
MD
455 if (error)
456 break;
1ae37239
MD
457 cpu_mfence();
458 rpipe->pipe_buffer.rindex += nsize;
459 nread += nsize;
984263bc
MD
460
461 /*
880ffa3a
MD
462 * If the FIFO is still over half full just continue
463 * and do not try to notify the writer yet.
984263bc 464 */
b20720b5
MD
465 if (size - nsize >= (rpipe->pipe_buffer.size >> 1)) {
466 notify_writer = 0;
1ae37239 467 continue;
984263bc 468 }
b20720b5
MD
469
470 /*
880ffa3a
MD
471 * When the FIFO is less then half full notify any
472 * waiting writer. WANTW can be checked while
473 * holding just the rlock.
b20720b5
MD
474 */
475 notify_writer = 1;
476 if ((rpipe->pipe_state & PIPE_WANTW) == 0)
477 continue;
1ae37239 478 }
984263bc 479
1ae37239
MD
480 /*
481 * If the "write-side" was blocked we wake it up. This code
482 * is reached either when the buffer is completely emptied
483 * or if it becomes more then half-empty.
484 *
485 * Pipe_state can only be modified if both the rlock and
486 * wlock are held.
487 */
488 if (rpipe->pipe_state & PIPE_WANTW) {
3b998fa9 489 lwkt_gettoken(&rpipe->pipe_wlock);
984263bc
MD
490 if (rpipe->pipe_state & PIPE_WANTW) {
491 rpipe->pipe_state &= ~PIPE_WANTW;
3b998fa9 492 lwkt_reltoken(&rpipe->pipe_wlock);
984263bc 493 wakeup(rpipe);
1ae37239 494 } else {
3b998fa9 495 lwkt_reltoken(&rpipe->pipe_wlock);
984263bc 496 }
1ae37239 497 }
984263bc 498
1ae37239
MD
499 /*
500 * Pick up our copy loop again if the writer sent data to
880ffa3a
MD
501 * us while we were messing around.
502 *
503 * On a SMP box poll up to pipe_delay nanoseconds for new
504 * data. Typically a value of 2000 to 4000 is sufficient
505 * to eradicate most IPIs/tsleeps/wakeups when a pipe
506 * is used for synchronous communications with small packets,
507 * and 8000 or so (8uS) will pipeline large buffer xfers
508 * between cpus over a pipe.
509 *
510 * For synchronous communications a hit means doing a
511 * full Awrite-Bread-Bwrite-Aread cycle in less then 2uS,
512 * where as miss requiring a tsleep/wakeup sequence
513 * will take 7uS or more.
1ae37239 514 */
880ffa3a 515 if (rpipe->pipe_buffer.windex != rpipe->pipe_buffer.rindex)
1ae37239 516 continue;
984263bc 517
1918fc5c 518#ifdef _RDTSC_SUPPORTED_
880ffa3a
MD
519 if (pipe_delay) {
520 int64_t tsc_target;
521 int good = 0;
522
523 tsc_target = tsc_get_target(pipe_delay);
524 while (tsc_test_target(tsc_target) == 0) {
525 if (rpipe->pipe_buffer.windex !=
526 rpipe->pipe_buffer.rindex) {
527 good = 1;
528 break;
529 }
530 }
531 if (good)
532 continue;
533 }
534#endif
535
1ae37239
MD
536 /*
537 * Detect EOF condition, do not set error.
538 */
539 if (rpipe->pipe_state & PIPE_REOF)
540 break;
984263bc 541
1ae37239
MD
542 /*
543 * Break if some data was read, or if this was a non-blocking
544 * read.
545 */
546 if (nread > 0)
547 break;
548
549 if (nbio) {
550 error = EAGAIN;
551 break;
552 }
553
554 /*
555 * Last chance, interlock with WANTR.
556 */
3b998fa9 557 lwkt_gettoken(&rpipe->pipe_wlock);
1ae37239
MD
558 size = rpipe->pipe_buffer.windex - rpipe->pipe_buffer.rindex;
559 if (size) {
3b998fa9 560 lwkt_reltoken(&rpipe->pipe_wlock);
1ae37239 561 continue;
984263bc 562 }
1ae37239 563
1bfdcce2
MD
564 /*
565 * Retest EOF - acquiring a new token can temporarily release
566 * tokens already held.
567 */
9aff1d71 568 if (rpipe->pipe_state & PIPE_REOF) {
3b998fa9 569 lwkt_reltoken(&rpipe->pipe_wlock);
1bfdcce2 570 break;
9aff1d71 571 }
1bfdcce2 572
1ae37239
MD
573 /*
574 * If there is no more to read in the pipe, reset its
575 * pointers to the beginning. This improves cache hit
576 * stats.
577 *
578 * We need both locks to modify both pointers, and there
579 * must also not be a write in progress or the uiomove()
580 * in the write might block and temporarily release
581 * its wlock, then reacquire and update windex. We are
582 * only serialized against reads, not writes.
583 *
584 * XXX should we even bother resetting the indices? It
585 * might actually be more cache efficient not to.
586 */
587 if (rpipe->pipe_buffer.rindex == rpipe->pipe_buffer.windex &&
588 rpipe->pipe_wip == 0) {
589 rpipe->pipe_buffer.rindex = 0;
590 rpipe->pipe_buffer.windex = 0;
591 }
592
593 /*
594 * Wait for more data.
595 *
596 * Pipe_state can only be set if both the rlock and wlock
597 * are held.
598 */
599 rpipe->pipe_state |= PIPE_WANTR;
ae8e83e6 600 tsleep_interlock(rpipe, PCATCH);
3b998fa9 601 lwkt_reltoken(&rpipe->pipe_wlock);
d9345d3a 602 error = tsleep(rpipe, PCATCH | PINTERLOCKED, "piperd", 0);
880ffa3a 603 ++pipe_rblocked_count;
1ae37239
MD
604 if (error)
605 break;
984263bc 606 }
1ae37239 607 pipe_end_uio(rpipe, &rpipe->pipe_rip);
984263bc 608
1ae37239
MD
609 /*
610 * Uptime last access time
611 */
612 if (error == 0 && nread)
984263bc 613 vfs_timestamp(&rpipe->pipe_atime);
984263bc
MD
614
615 /*
b20720b5
MD
616 * If we drained the FIFO more then half way then handle
617 * write blocking hysteresis.
1ae37239 618 *
b20720b5
MD
619 * Note that PIPE_WANTW cannot be set by the writer without
620 * it holding both rlock and wlock, so we can test it
621 * while holding just rlock.
984263bc 622 */
b20720b5 623 if (notify_writer) {
8315ba5b
MD
624 /*
625 * Synchronous blocking is done on the pipe involved
626 */
b20720b5 627 if (rpipe->pipe_state & PIPE_WANTW) {
3b998fa9 628 lwkt_gettoken(&rpipe->pipe_wlock);
1ae37239
MD
629 if (rpipe->pipe_state & PIPE_WANTW) {
630 rpipe->pipe_state &= ~PIPE_WANTW;
3b998fa9 631 lwkt_reltoken(&rpipe->pipe_wlock);
1ae37239
MD
632 wakeup(rpipe);
633 } else {
3b998fa9 634 lwkt_reltoken(&rpipe->pipe_wlock);
1ae37239 635 }
984263bc 636 }
8315ba5b
MD
637
638 /*
639 * But we may also have to deal with a kqueue which is
640 * stored on the same pipe as its descriptor, so a
641 * EVFILT_WRITE event waiting for our side to drain will
642 * be on the other side.
643 */
644 lwkt_gettoken(&wpipe->pipe_wlock);
645 pipewakeup(wpipe, 0);
646 lwkt_reltoken(&wpipe->pipe_wlock);
984263bc 647 }
1bfdcce2 648 /*size = rpipe->pipe_buffer.windex - rpipe->pipe_buffer.rindex;*/
3b998fa9 649 lwkt_reltoken(&rpipe->pipe_rlock);
984263bc 650
984263bc
MD
651 return (error);
652}
653
984263bc 654static int
9ba76b73 655pipe_write(struct file *fp, struct uio *uio, struct ucred *cred, int fflags)
984263bc 656{
1ae37239 657 int error;
984263bc 658 int orig_resid;
9ba76b73 659 int nbio;
8315ba5b
MD
660 struct pipe *wpipe;
661 struct pipe *rpipe;
c600838f
MD
662 u_int windex;
663 u_int space;
1ae37239 664 u_int wcount;
607b0ed9
MD
665 int bigwrite;
666 int bigcount;
984263bc 667
1ae37239
MD
668 /*
669 * Writes go to the peer. The peer will always exist.
670 */
984263bc
MD
671 rpipe = (struct pipe *) fp->f_data;
672 wpipe = rpipe->pipe_peer;
3b998fa9 673 lwkt_gettoken(&wpipe->pipe_wlock);
1ae37239 674 if (wpipe->pipe_state & PIPE_WEOF) {
3b998fa9 675 lwkt_reltoken(&wpipe->pipe_wlock);
1ae37239
MD
676 return (EPIPE);
677 }
984263bc
MD
678
679 /*
1ae37239 680 * Degenerate case (EPIPE takes prec)
984263bc 681 */
1ae37239 682 if (uio->uio_resid == 0) {
3b998fa9 683 lwkt_reltoken(&wpipe->pipe_wlock);
1ae37239
MD
684 return(0);
685 }
686
687 /*
688 * Writes are serialized (start_uio must be called with wlock)
689 */
690 error = pipe_start_uio(wpipe, &wpipe->pipe_wip);
691 if (error) {
3b998fa9 692 lwkt_reltoken(&wpipe->pipe_wlock);
1ae37239 693 return (error);
984263bc 694 }
984263bc 695
9ba76b73
MD
696 if (fflags & O_FBLOCKING)
697 nbio = 0;
698 else if (fflags & O_FNONBLOCKING)
699 nbio = 1;
700 else if (fp->f_flag & O_NONBLOCK)
701 nbio = 1;
702 else
703 nbio = 0;
704
984263bc
MD
705 /*
706 * If it is advantageous to resize the pipe buffer, do
1ae37239 707 * so. We are write-serialized so we can block safely.
984263bc 708 */
1ae37239 709 if ((wpipe->pipe_buffer.size <= PIPE_SIZE) &&
39b0a1af 710 (pipe_nbig < pipe_maxbig) &&
1ae37239
MD
711 wpipe->pipe_wantwcnt > 4 &&
712 (wpipe->pipe_buffer.rindex == wpipe->pipe_buffer.windex)) {
39b0a1af
MD
713 /*
714 * Recheck after lock.
715 */
3b998fa9 716 lwkt_gettoken(&wpipe->pipe_rlock);
1ae37239
MD
717 if ((wpipe->pipe_buffer.size <= PIPE_SIZE) &&
718 (pipe_nbig < pipe_maxbig) &&
c600838f 719 (wpipe->pipe_buffer.rindex == wpipe->pipe_buffer.windex)) {
880ffa3a
MD
720 atomic_add_int(&pipe_nbig, 1);
721 if (pipespace(wpipe, BIG_PIPE_SIZE) == 0)
39b0a1af 722 ++pipe_bigcount;
880ffa3a
MD
723 else
724 atomic_subtract_int(&pipe_nbig, 1);
984263bc 725 }
3b998fa9 726 lwkt_reltoken(&wpipe->pipe_rlock);
984263bc 727 }
984263bc
MD
728
729 orig_resid = uio->uio_resid;
1ae37239 730 wcount = 0;
984263bc 731
607b0ed9
MD
732 bigwrite = (uio->uio_resid > 10 * 1024 * 1024);
733 bigcount = 10;
734
984263bc 735 while (uio->uio_resid) {
1ae37239 736 if (wpipe->pipe_state & PIPE_WEOF) {
984263bc
MD
737 error = EPIPE;
738 break;
739 }
740
607b0ed9
MD
741 /*
742 * Don't hog the cpu.
743 */
744 if (bigwrite && --bigcount == 0) {
745 lwkt_user_yield();
746 bigcount = 10;
747 if (CURSIG(curthread->td_lwp)) {
748 error = EINTR;
749 break;
750 }
751 }
752
c600838f
MD
753 windex = wpipe->pipe_buffer.windex &
754 (wpipe->pipe_buffer.size - 1);
755 space = wpipe->pipe_buffer.size -
756 (wpipe->pipe_buffer.windex - wpipe->pipe_buffer.rindex);
1ae37239 757 cpu_lfence();
984263bc
MD
758
759 /* Writes of size <= PIPE_BUF must be atomic. */
760 if ((space < uio->uio_resid) && (orig_resid <= PIPE_BUF))
761 space = 0;
762
c617bada
MD
763 /*
764 * Write to fill, read size handles write hysteresis. Also
765 * additional restrictions can cause select-based non-blocking
766 * writes to spin.
767 */
768 if (space > 0) {
1ae37239 769 u_int segsize;
984263bc 770
984263bc 771 /*
1ae37239
MD
772 * Transfer size is minimum of uio transfer
773 * and free space in pipe buffer.
774 *
775 * Limit each uiocopy to no more then PIPE_SIZE
776 * so we can keep the gravy train going on a
777 * SMP box. This doubles the performance for
778 * write sizes > 16K. Otherwise large writes
779 * wind up doing an inefficient synchronous
780 * ping-pong.
984263bc 781 */
607b0ed9 782 space = szmin(space, uio->uio_resid);
1ae37239
MD
783 if (space > PIPE_SIZE)
784 space = PIPE_SIZE;
984263bc
MD
785
786 /*
1ae37239
MD
787 * First segment to transfer is minimum of
788 * transfer size and contiguous space in
789 * pipe buffer. If first segment to transfer
790 * is less than the transfer size, we've got
791 * a wraparound in the buffer.
984263bc 792 */
1ae37239
MD
793 segsize = wpipe->pipe_buffer.size - windex;
794 if (segsize > space)
795 segsize = space;
984263bc
MD
796
797 /*
1ae37239
MD
798 * If this is the first loop and the reader is
799 * blocked, do a preemptive wakeup of the reader.
800 *
39055880
MD
801 * On SMP the IPI latency plus the wlock interlock
802 * on the reader side is the fastest way to get the
803 * reader going. (The scheduler will hard loop on
804 * lock tokens).
1ae37239
MD
805 *
806 * NOTE: We can't clear WANTR here without acquiring
807 * the rlock, which we don't want to do here!
984263bc 808 */
394d8e4b 809 if ((wpipe->pipe_state & PIPE_WANTR))
1ae37239 810 wakeup(wpipe);
984263bc 811
984263bc 812 /*
880ffa3a
MD
813 * Transfer segment, which may include a wrap-around.
814 * Update windex to account for both all in one go
815 * so the reader can read() the data atomically.
984263bc 816 */
1ae37239
MD
817 error = uiomove(&wpipe->pipe_buffer.buffer[windex],
818 segsize, uio);
1ae37239 819 if (error == 0 && segsize < space) {
1ae37239
MD
820 segsize = space - segsize;
821 error = uiomove(&wpipe->pipe_buffer.buffer[0],
822 segsize, uio);
1ae37239
MD
823 }
824 if (error)
984263bc 825 break;
880ffa3a
MD
826 cpu_mfence();
827 wpipe->pipe_buffer.windex += space;
1ae37239
MD
828 wcount += space;
829 continue;
984263bc 830 }
984263bc 831
1ae37239
MD
832 /*
833 * We need both the rlock and the wlock to interlock against
834 * the EOF, WANTW, and size checks, and to modify pipe_state.
835 *
836 * These are token locks so we do not have to worry about
837 * deadlocks.
838 */
3b998fa9 839 lwkt_gettoken(&wpipe->pipe_rlock);
984263bc 840
984263bc 841 /*
1ae37239
MD
842 * If the "read-side" has been blocked, wake it up now
843 * and yield to let it drain synchronously rather
844 * then block.
984263bc
MD
845 */
846 if (wpipe->pipe_state & PIPE_WANTR) {
847 wpipe->pipe_state &= ~PIPE_WANTR;
848 wakeup(wpipe);
849 }
1ae37239
MD
850
851 /*
852 * don't block on non-blocking I/O
853 */
854 if (nbio) {
3b998fa9 855 lwkt_reltoken(&wpipe->pipe_rlock);
1ae37239
MD
856 error = EAGAIN;
857 break;
858 }
859
b20720b5
MD
860 /*
861 * re-test whether we have to block in the writer after
862 * acquiring both locks, in case the reader opened up
863 * some space.
864 */
865 space = wpipe->pipe_buffer.size -
866 (wpipe->pipe_buffer.windex - wpipe->pipe_buffer.rindex);
867 cpu_lfence();
868 if ((space < uio->uio_resid) && (orig_resid <= PIPE_BUF))
869 space = 0;
870
1bfdcce2
MD
871 /*
872 * Retest EOF - acquiring a new token can temporarily release
873 * tokens already held.
874 */
875 if (wpipe->pipe_state & PIPE_WEOF) {
3b998fa9 876 lwkt_reltoken(&wpipe->pipe_rlock);
1bfdcce2
MD
877 error = EPIPE;
878 break;
879 }
880
1ae37239
MD
881 /*
882 * We have no more space and have something to offer,
5b22f1a7 883 * wake up select/poll/kq.
1ae37239 884 */
b20720b5 885 if (space == 0) {
b20720b5 886 wpipe->pipe_state |= PIPE_WANTW;
958de305 887 ++wpipe->pipe_wantwcnt;
8315ba5b 888 pipewakeup(wpipe, 1);
958de305
MD
889 if (wpipe->pipe_state & PIPE_WANTW)
890 error = tsleep(wpipe, PCATCH, "pipewr", 0);
880ffa3a 891 ++pipe_wblocked_count;
b20720b5 892 }
3b998fa9 893 lwkt_reltoken(&wpipe->pipe_rlock);
1ae37239
MD
894
895 /*
896 * Break out if we errored or the read side wants us to go
897 * away.
898 */
899 if (error)
900 break;
901 if (wpipe->pipe_state & PIPE_WEOF) {
902 error = EPIPE;
903 break;
904 }
905 }
906 pipe_end_uio(wpipe, &wpipe->pipe_wip);
907
908 /*
909 * If we have put any characters in the buffer, we wake up
910 * the reader.
911 *
912 * Both rlock and wlock are required to be able to modify pipe_state.
913 */
914 if (wpipe->pipe_buffer.windex != wpipe->pipe_buffer.rindex) {
915 if (wpipe->pipe_state & PIPE_WANTR) {
3b998fa9 916 lwkt_gettoken(&wpipe->pipe_rlock);
1ae37239
MD
917 if (wpipe->pipe_state & PIPE_WANTR) {
918 wpipe->pipe_state &= ~PIPE_WANTR;
3b998fa9 919 lwkt_reltoken(&wpipe->pipe_rlock);
1ae37239
MD
920 wakeup(wpipe);
921 } else {
3b998fa9 922 lwkt_reltoken(&wpipe->pipe_rlock);
1ae37239
MD
923 }
924 }
3720cffa 925 lwkt_gettoken(&wpipe->pipe_rlock);
8315ba5b 926 pipewakeup(wpipe, 1);
3720cffa 927 lwkt_reltoken(&wpipe->pipe_rlock);
984263bc
MD
928 }
929
930 /*
931 * Don't return EPIPE if I/O was successful
932 */
c600838f 933 if ((wpipe->pipe_buffer.rindex == wpipe->pipe_buffer.windex) &&
984263bc
MD
934 (uio->uio_resid == 0) &&
935 (error == EPIPE)) {
936 error = 0;
937 }
938
939 if (error == 0)
940 vfs_timestamp(&wpipe->pipe_mtime);
941
942 /*
943 * We have something to offer,
5b22f1a7 944 * wake up select/poll/kq.
984263bc 945 */
1bfdcce2 946 /*space = wpipe->pipe_buffer.windex - wpipe->pipe_buffer.rindex;*/
3b998fa9 947 lwkt_reltoken(&wpipe->pipe_wlock);
984263bc
MD
948 return (error);
949}
950
951/*
952 * we implement a very minimal set of ioctls for compatibility with sockets.
953 */
59b728a7 954static int
87baaf0c
MD
955pipe_ioctl(struct file *fp, u_long cmd, caddr_t data,
956 struct ucred *cred, struct sysmsg *msg)
984263bc 957{
d9b2033e
MD
958 struct pipe *mpipe;
959 int error;
984263bc 960
d9b2033e 961 mpipe = (struct pipe *)fp->f_data;
984263bc 962
3b998fa9
MD
963 lwkt_gettoken(&mpipe->pipe_rlock);
964 lwkt_gettoken(&mpipe->pipe_wlock);
880ffa3a 965
d9b2033e 966 switch (cmd) {
984263bc
MD
967 case FIOASYNC:
968 if (*(int *)data) {
969 mpipe->pipe_state |= PIPE_ASYNC;
970 } else {
971 mpipe->pipe_state &= ~PIPE_ASYNC;
972 }
d9b2033e
MD
973 error = 0;
974 break;
984263bc 975 case FIONREAD:
c600838f
MD
976 *(int *)data = mpipe->pipe_buffer.windex -
977 mpipe->pipe_buffer.rindex;
d9b2033e
MD
978 error = 0;
979 break;
984263bc 980 case FIOSETOWN:
d9b2033e
MD
981 error = fsetown(*(int *)data, &mpipe->pipe_sigio);
982 break;
984263bc 983 case FIOGETOWN:
b5c4d81f 984 *(int *)data = fgetown(&mpipe->pipe_sigio);
d9b2033e
MD
985 error = 0;
986 break;
984263bc 987 case TIOCSPGRP:
d9b2033e
MD
988 /* This is deprecated, FIOSETOWN should be used instead. */
989 error = fsetown(-(*(int *)data), &mpipe->pipe_sigio);
990 break;
984263bc 991
984263bc 992 case TIOCGPGRP:
d9b2033e 993 /* This is deprecated, FIOGETOWN should be used instead. */
b5c4d81f 994 *(int *)data = -fgetown(&mpipe->pipe_sigio);
d9b2033e
MD
995 error = 0;
996 break;
997 default:
998 error = ENOTTY;
999 break;
984263bc 1000 }
3b998fa9
MD
1001 lwkt_reltoken(&mpipe->pipe_wlock);
1002 lwkt_reltoken(&mpipe->pipe_rlock);
880ffa3a 1003
d9b2033e 1004 return (error);
984263bc
MD
1005}
1006
d9b2033e 1007/*
1ee6e3c6 1008 * MPSAFE
d9b2033e 1009 */
984263bc 1010static int
87de5057 1011pipe_stat(struct file *fp, struct stat *ub, struct ucred *cred)
984263bc 1012{
d9b2033e
MD
1013 struct pipe *pipe;
1014
d9b2033e 1015 pipe = (struct pipe *)fp->f_data;
984263bc
MD
1016
1017 bzero((caddr_t)ub, sizeof(*ub));
1018 ub->st_mode = S_IFIFO;
1019 ub->st_blksize = pipe->pipe_buffer.size;
c600838f 1020 ub->st_size = pipe->pipe_buffer.windex - pipe->pipe_buffer.rindex;
984263bc
MD
1021 ub->st_blocks = (ub->st_size + ub->st_blksize - 1) / ub->st_blksize;
1022 ub->st_atimespec = pipe->pipe_atime;
1023 ub->st_mtimespec = pipe->pipe_mtime;
1024 ub->st_ctimespec = pipe->pipe_ctime;
1025 /*
1026 * Left as 0: st_dev, st_ino, st_nlink, st_uid, st_gid, st_rdev,
1027 * st_flags, st_gen.
1028 * XXX (st_dev, st_ino) should be unique.
1029 */
1030 return (0);
1031}
1032
984263bc 1033static int
87de5057 1034pipe_close(struct file *fp)
984263bc 1035{
39b0a1af 1036 struct pipe *cpipe;
984263bc 1037
39b0a1af 1038 cpipe = (struct pipe *)fp->f_data;
984263bc
MD
1039 fp->f_ops = &badfileops;
1040 fp->f_data = NULL;
58c2553a 1041 funsetown(&cpipe->pipe_sigio);
984263bc
MD
1042 pipeclose(cpipe);
1043 return (0);
1044}
1045
004d2de5
MD
1046/*
1047 * Shutdown one or both directions of a full-duplex pipe.
1048 */
004d2de5 1049static int
87de5057 1050pipe_shutdown(struct file *fp, int how)
004d2de5 1051{
d9b2033e 1052 struct pipe *rpipe;
004d2de5
MD
1053 struct pipe *wpipe;
1054 int error = EPIPE;
1055
d9b2033e 1056 rpipe = (struct pipe *)fp->f_data;
1ae37239
MD
1057 wpipe = rpipe->pipe_peer;
1058
1059 /*
1060 * We modify pipe_state on both pipes, which means we need
1061 * all four tokens!
1062 */
3b998fa9
MD
1063 lwkt_gettoken(&rpipe->pipe_rlock);
1064 lwkt_gettoken(&rpipe->pipe_wlock);
1065 lwkt_gettoken(&wpipe->pipe_rlock);
1066 lwkt_gettoken(&wpipe->pipe_wlock);
d9b2033e 1067
004d2de5
MD
1068 switch(how) {
1069 case SHUT_RDWR:
1070 case SHUT_RD:
930bd151
MD
1071 rpipe->pipe_state |= PIPE_REOF; /* my reads */
1072 rpipe->pipe_state |= PIPE_WEOF; /* peer writes */
1ae37239
MD
1073 if (rpipe->pipe_state & PIPE_WANTR) {
1074 rpipe->pipe_state &= ~PIPE_WANTR;
1075 wakeup(rpipe);
004d2de5 1076 }
930bd151
MD
1077 if (rpipe->pipe_state & PIPE_WANTW) {
1078 rpipe->pipe_state &= ~PIPE_WANTW;
1079 wakeup(rpipe);
1ae37239 1080 }
1ae37239 1081 error = 0;
004d2de5
MD
1082 if (how == SHUT_RD)
1083 break;
1084 /* fall through */
1085 case SHUT_WR:
930bd151
MD
1086 wpipe->pipe_state |= PIPE_REOF; /* peer reads */
1087 wpipe->pipe_state |= PIPE_WEOF; /* my writes */
1088 if (wpipe->pipe_state & PIPE_WANTR) {
1089 wpipe->pipe_state &= ~PIPE_WANTR;
1090 wakeup(wpipe);
1091 }
1ae37239
MD
1092 if (wpipe->pipe_state & PIPE_WANTW) {
1093 wpipe->pipe_state &= ~PIPE_WANTW;
1094 wakeup(wpipe);
1095 }
1ae37239
MD
1096 error = 0;
1097 break;
004d2de5 1098 }
8315ba5b
MD
1099 pipewakeup(rpipe, 1);
1100 pipewakeup(wpipe, 1);
1ae37239 1101
3b998fa9
MD
1102 lwkt_reltoken(&wpipe->pipe_wlock);
1103 lwkt_reltoken(&wpipe->pipe_rlock);
1104 lwkt_reltoken(&rpipe->pipe_wlock);
1105 lwkt_reltoken(&rpipe->pipe_rlock);
1ae37239 1106
004d2de5
MD
1107 return (error);
1108}
1109
984263bc 1110static void
dadab5e9 1111pipe_free_kmem(struct pipe *cpipe)
984263bc 1112{
984263bc
MD
1113 if (cpipe->pipe_buffer.buffer != NULL) {
1114 if (cpipe->pipe_buffer.size > PIPE_SIZE)
880ffa3a 1115 atomic_subtract_int(&pipe_nbig, 1);
e4846942 1116 kmem_free(&kernel_map,
984263bc
MD
1117 (vm_offset_t)cpipe->pipe_buffer.buffer,
1118 cpipe->pipe_buffer.size);
1119 cpipe->pipe_buffer.buffer = NULL;
fc7d5181 1120 cpipe->pipe_buffer.object = NULL;
984263bc 1121 }
984263bc
MD
1122}
1123
1124/*
1ae37239
MD
1125 * Close the pipe. The slock must be held to interlock against simultanious
1126 * closes. The rlock and wlock must be held to adjust the pipe_state.
984263bc
MD
1127 */
1128static void
dadab5e9 1129pipeclose(struct pipe *cpipe)
984263bc 1130{
fc7d5181 1131 globaldata_t gd;
984263bc
MD
1132 struct pipe *ppipe;
1133
fc7d5181
MD
1134 if (cpipe == NULL)
1135 return;
984263bc 1136
1ae37239
MD
1137 /*
1138 * The slock may not have been allocated yet (close during
1139 * initialization)
1140 *
1141 * We need both the read and write tokens to modify pipe_state.
1142 */
1143 if (cpipe->pipe_slock)
1144 lockmgr(cpipe->pipe_slock, LK_EXCLUSIVE);
3b998fa9
MD
1145 lwkt_gettoken(&cpipe->pipe_rlock);
1146 lwkt_gettoken(&cpipe->pipe_wlock);
984263bc 1147
fc7d5181 1148 /*
5b22f1a7 1149 * Set our state, wakeup anyone waiting in select/poll/kq, and
1ae37239 1150 * wakeup anyone blocked on our pipe.
fc7d5181 1151 */
1ae37239 1152 cpipe->pipe_state |= PIPE_CLOSED | PIPE_REOF | PIPE_WEOF;
8315ba5b 1153 pipewakeup(cpipe, 1);
1ae37239
MD
1154 if (cpipe->pipe_state & (PIPE_WANTR | PIPE_WANTW)) {
1155 cpipe->pipe_state &= ~(PIPE_WANTR | PIPE_WANTW);
fc7d5181 1156 wakeup(cpipe);
fc7d5181 1157 }
984263bc 1158
fc7d5181 1159 /*
930bd151 1160 * Disconnect from peer.
fc7d5181
MD
1161 */
1162 if ((ppipe = cpipe->pipe_peer) != NULL) {
3b998fa9
MD
1163 lwkt_gettoken(&ppipe->pipe_rlock);
1164 lwkt_gettoken(&ppipe->pipe_wlock);
930bd151 1165 ppipe->pipe_state |= PIPE_REOF | PIPE_WEOF;
8315ba5b 1166 pipewakeup(ppipe, 1);
1ae37239
MD
1167 if (ppipe->pipe_state & (PIPE_WANTR | PIPE_WANTW)) {
1168 ppipe->pipe_state &= ~(PIPE_WANTR | PIPE_WANTW);
1169 wakeup(ppipe);
1170 }
5b22f1a7
SG
1171 if (SLIST_FIRST(&ppipe->pipe_kq.ki_note))
1172 KNOTE(&ppipe->pipe_kq.ki_note, 0);
3b998fa9
MD
1173 lwkt_reltoken(&ppipe->pipe_wlock);
1174 lwkt_reltoken(&ppipe->pipe_rlock);
1ae37239 1175 }
fc7d5181 1176
1ae37239
MD
1177 /*
1178 * If the peer is also closed we can free resources for both
1179 * sides, otherwise we leave our side intact to deal with any
1180 * races (since we only have the slock).
1181 */
1182 if (ppipe && (ppipe->pipe_state & PIPE_CLOSED)) {
1183 cpipe->pipe_peer = NULL;
fc7d5181 1184 ppipe->pipe_peer = NULL;
1ae37239
MD
1185 ppipe->pipe_slock = NULL; /* we will free the slock */
1186 pipeclose(ppipe);
1187 ppipe = NULL;
fc7d5181
MD
1188 }
1189
3b998fa9
MD
1190 lwkt_reltoken(&cpipe->pipe_wlock);
1191 lwkt_reltoken(&cpipe->pipe_rlock);
1ae37239
MD
1192 if (cpipe->pipe_slock)
1193 lockmgr(cpipe->pipe_slock, LK_RELEASE);
8100156a 1194
fc7d5181 1195 /*
1ae37239 1196 * If we disassociated from our peer we can free resources
fc7d5181 1197 */
1ae37239
MD
1198 if (ppipe == NULL) {
1199 gd = mycpu;
1200 if (cpipe->pipe_slock) {
1201 kfree(cpipe->pipe_slock, M_PIPE);
1202 cpipe->pipe_slock = NULL;
1203 }
1204 if (gd->gd_pipeqcount >= pipe_maxcache ||
1205 cpipe->pipe_buffer.size != PIPE_SIZE
1206 ) {
1207 pipe_free_kmem(cpipe);
1208 kfree(cpipe, M_PIPE);
1209 } else {
1210 cpipe->pipe_state = 0;
1211 cpipe->pipe_peer = gd->gd_pipeq;
1212 gd->gd_pipeq = cpipe;
1213 ++gd->gd_pipeqcount;
1214 }
984263bc
MD
1215 }
1216}
1217
984263bc
MD
1218static int
1219pipe_kqfilter(struct file *fp, struct knote *kn)
1220{
d9b2033e
MD
1221 struct pipe *cpipe;
1222
d9b2033e 1223 cpipe = (struct pipe *)kn->kn_fp->f_data;
984263bc
MD
1224
1225 switch (kn->kn_filter) {
1226 case EVFILT_READ:
1227 kn->kn_fop = &pipe_rfiltops;
1228 break;
1229 case EVFILT_WRITE:
1230 kn->kn_fop = &pipe_wfiltops;
3720cffa 1231 if (cpipe->pipe_peer == NULL) {
984263bc 1232 /* other end of pipe has been closed */
41f57d15 1233 return (EPIPE);
d9b2033e 1234 }
984263bc
MD
1235 break;
1236 default:
b287d649 1237 return (EOPNOTSUPP);
984263bc
MD
1238 }
1239 kn->kn_hook = (caddr_t)cpipe;
1240
5b22f1a7 1241 knote_insert(&cpipe->pipe_kq.ki_note, kn);
3720cffa 1242
984263bc
MD
1243 return (0);
1244}
1245
1246static void
1247filt_pipedetach(struct knote *kn)
1248{
1249 struct pipe *cpipe = (struct pipe *)kn->kn_hook;
1250
5b22f1a7 1251 knote_remove(&cpipe->pipe_kq.ki_note, kn);
984263bc
MD
1252}
1253
1254/*ARGSUSED*/
1255static int
1256filt_piperead(struct knote *kn, long hint)
1257{
1258 struct pipe *rpipe = (struct pipe *)kn->kn_fp->f_data;
13b050b0
SG
1259 int ready = 0;
1260
1261 lwkt_gettoken(&rpipe->pipe_rlock);
1262 lwkt_gettoken(&rpipe->pipe_wlock);
984263bc 1263
3720cffa 1264 kn->kn_data = rpipe->pipe_buffer.windex - rpipe->pipe_buffer.rindex;
8c4ed426 1265
3bcb6e5e
SZ
1266 if (rpipe->pipe_state & PIPE_REOF) {
1267 /*
1268 * Only set NODATA if all data has been exhausted
1269 */
1270 if (kn->kn_data == 0)
1271 kn->kn_flags |= EV_NODATA;
984263bc 1272 kn->kn_flags |= EV_EOF;
13b050b0 1273 ready = 1;
984263bc 1274 }
3720cffa 1275
13b050b0
SG
1276 lwkt_reltoken(&rpipe->pipe_wlock);
1277 lwkt_reltoken(&rpipe->pipe_rlock);
1278
1279 if (!ready)
1280 ready = kn->kn_data > 0;
1281
1282 return (ready);
984263bc
MD
1283}
1284
1285/*ARGSUSED*/
1286static int
1287filt_pipewrite(struct knote *kn, long hint)
1288{
13b050b0
SG
1289 struct pipe *rpipe = (struct pipe *)kn->kn_fp->f_data;
1290 struct pipe *wpipe = rpipe->pipe_peer;
1291 int ready = 0;
1292
045ef32c
SG
1293 kn->kn_data = 0;
1294 if (wpipe == NULL) {
3bcb6e5e 1295 kn->kn_flags |= (EV_EOF | EV_NODATA);
045ef32c
SG
1296 return (1);
1297 }
1298
13b050b0
SG
1299 lwkt_gettoken(&wpipe->pipe_rlock);
1300 lwkt_gettoken(&wpipe->pipe_wlock);
1301
045ef32c 1302 if (wpipe->pipe_state & PIPE_WEOF) {
3bcb6e5e 1303 kn->kn_flags |= (EV_EOF | EV_NODATA);
13b050b0 1304 ready = 1;
984263bc 1305 }
3720cffa 1306
13b050b0
SG
1307 if (!ready)
1308 kn->kn_data = wpipe->pipe_buffer.size -
1309 (wpipe->pipe_buffer.windex -
1310 wpipe->pipe_buffer.rindex);
1311
1312 lwkt_reltoken(&wpipe->pipe_wlock);
1313 lwkt_reltoken(&wpipe->pipe_rlock);
1314
1315 if (!ready)
1316 ready = kn->kn_data >= PIPE_BUF;
1317
1318 return (ready);
984263bc 1319}