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