160727d8bSWarner Losh /*- 210c5615cSJohn Dyson * Copyright (c) 1996 John S. Dyson 310c5615cSJohn Dyson * All rights reserved. 410c5615cSJohn Dyson * 510c5615cSJohn Dyson * Redistribution and use in source and binary forms, with or without 610c5615cSJohn Dyson * modification, are permitted provided that the following conditions 710c5615cSJohn Dyson * are met: 810c5615cSJohn Dyson * 1. Redistributions of source code must retain the above copyright 910c5615cSJohn Dyson * notice immediately at the beginning of the file, without modification, 1010c5615cSJohn Dyson * this list of conditions, and the following disclaimer. 1110c5615cSJohn Dyson * 2. Redistributions in binary form must reproduce the above copyright 1210c5615cSJohn Dyson * notice, this list of conditions and the following disclaimer in the 1310c5615cSJohn Dyson * documentation and/or other materials provided with the distribution. 1410c5615cSJohn Dyson * 3. Absolutely no warranty of function or purpose is made by the author 1510c5615cSJohn Dyson * John S. Dyson. 1610c5615cSJohn Dyson * 4. This work was done expressly for inclusion into FreeBSD. Other use 1710c5615cSJohn Dyson * is allowed if this notation is included. 1810c5615cSJohn Dyson * 5. Modifications may be freely made to this file if the above conditions 1910c5615cSJohn Dyson * are met. 2010c5615cSJohn Dyson * 21c3aac50fSPeter Wemm * $FreeBSD$ 2210c5615cSJohn Dyson */ 2310c5615cSJohn Dyson 243da92a61SBruce Evans #ifndef _SYS_PIPE_H_ 253da92a61SBruce Evans #define _SYS_PIPE_H_ 263da92a61SBruce Evans 2710c5615cSJohn Dyson /* 282834ceecSJohn Dyson * Pipe buffer size, keep moderate in value, pipes take kva space. 292834ceecSJohn Dyson */ 302834ceecSJohn Dyson #ifndef PIPE_SIZE 313da92a61SBruce Evans #define PIPE_SIZE 16384 322834ceecSJohn Dyson #endif 332834ceecSJohn Dyson 34d1a5be10SJohn Dyson #ifndef BIG_PIPE_SIZE 35d1a5be10SJohn Dyson #define BIG_PIPE_SIZE (64*1024) 36d1a5be10SJohn Dyson #endif 37d1a5be10SJohn Dyson 38289016f2SMike Silbersack #ifndef SMALL_PIPE_SIZE 39b7fbac18SMike Silbersack #define SMALL_PIPE_SIZE PAGE_SIZE 40289016f2SMike Silbersack #endif 41289016f2SMike Silbersack 422834ceecSJohn Dyson /* 432834ceecSJohn Dyson * PIPE_MINDIRECT MUST be smaller than PIPE_SIZE and MUST be bigger 443da92a61SBruce Evans * than PIPE_BUF. 452834ceecSJohn Dyson */ 462834ceecSJohn Dyson #ifndef PIPE_MINDIRECT 473da92a61SBruce Evans #define PIPE_MINDIRECT 8192 482834ceecSJohn Dyson #endif 492834ceecSJohn Dyson 50d1a5be10SJohn Dyson #define PIPENPAGES (BIG_PIPE_SIZE / PAGE_SIZE + 1) 513da92a61SBruce Evans 52d485c77fSKonstantin Belousov #ifdef _KERNEL 532834ceecSJohn Dyson /* 54289016f2SMike Silbersack * See sys_pipe.c for info on what these limits mean. 55289016f2SMike Silbersack */ 5664ecd139SJohn Baldwin extern long maxpipekva; 5711ac7ec0SKip Macy extern struct fileops pipeops; 58d485c77fSKonstantin Belousov #endif 59289016f2SMike Silbersack 60289016f2SMike Silbersack /* 613da92a61SBruce Evans * Pipe buffer information. 623da92a61SBruce Evans * Separate in, out, cnt are used to simplify calculations. 633da92a61SBruce Evans * Buffered write is active when the buffer.cnt field is set. 6410c5615cSJohn Dyson */ 6510c5615cSJohn Dyson struct pipebuf { 6610c5615cSJohn Dyson u_int cnt; /* number of chars currently in buffer */ 6710c5615cSJohn Dyson u_int in; /* in pointer */ 6810c5615cSJohn Dyson u_int out; /* out pointer */ 6910c5615cSJohn Dyson u_int size; /* size of buffer */ 7010c5615cSJohn Dyson caddr_t buffer; /* kva of buffer */ 7110c5615cSJohn Dyson }; 7210c5615cSJohn Dyson 7310c5615cSJohn Dyson /* 743da92a61SBruce Evans * Information to support direct transfers between processes for pipes. 752834ceecSJohn Dyson */ 762834ceecSJohn Dyson struct pipemapping { 772834ceecSJohn Dyson vm_size_t cnt; /* number of chars in buffer */ 782834ceecSJohn Dyson vm_size_t pos; /* current position of transfer */ 792834ceecSJohn Dyson int npages; /* number of pages */ 802834ceecSJohn Dyson vm_page_t ms[PIPENPAGES]; /* pages in source process */ 812834ceecSJohn Dyson }; 822834ceecSJohn Dyson 832834ceecSJohn Dyson /* 843da92a61SBruce Evans * Bits in pipe_state. 8510c5615cSJohn Dyson */ 863da92a61SBruce Evans #define PIPE_ASYNC 0x004 /* Async? I/O. */ 873da92a61SBruce Evans #define PIPE_WANTR 0x008 /* Reader wants some characters. */ 883da92a61SBruce Evans #define PIPE_WANTW 0x010 /* Writer wants space to put characters. */ 893da92a61SBruce Evans #define PIPE_WANT 0x020 /* Pipe is wanted to be run-down. */ 903da92a61SBruce Evans #define PIPE_SEL 0x040 /* Pipe has a select active. */ 913da92a61SBruce Evans #define PIPE_EOF 0x080 /* Pipe is in EOF condition. */ 92f81b04d9SAlfred Perlstein #define PIPE_LOCKFL 0x100 /* Process has exclusive access to pointers/data. */ 933da92a61SBruce Evans #define PIPE_LWANT 0x200 /* Process wants exclusive access to pointers/data. */ 943da92a61SBruce Evans #define PIPE_DIRECTW 0x400 /* Pipe direct write active. */ 953da92a61SBruce Evans #define PIPE_DIRECTOK 0x800 /* Direct mode ok. */ 9689744405SMateusz Guzik 9789744405SMateusz Guzik /* 9889744405SMateusz Guzik * Bits in pipe_type. 9989744405SMateusz Guzik */ 10089744405SMateusz Guzik #define PIPE_TYPE_NAMED 0x001 /* Is a named pipe. */ 1012834ceecSJohn Dyson 1022834ceecSJohn Dyson /* 1033da92a61SBruce Evans * Per-pipe data structure. 1043da92a61SBruce Evans * Two of these are linked together to produce bi-directional pipes. 10510c5615cSJohn Dyson */ 10610c5615cSJohn Dyson struct pipe { 10710c5615cSJohn Dyson struct pipebuf pipe_buffer; /* data storage */ 10885232c2fSMark Johnston struct pipemapping pipe_pages; /* wired pages for direct I/O */ 10910c5615cSJohn Dyson struct selinfo pipe_sel; /* for compat with select */ 110a0502b19SPoul-Henning Kamp struct timespec pipe_atime; /* time of last access */ 111a0502b19SPoul-Henning Kamp struct timespec pipe_mtime; /* time of last modify */ 112a0502b19SPoul-Henning Kamp struct timespec pipe_ctime; /* time of status change */ 11362d6ce3aSDon Lewis struct sigio *pipe_sigio; /* information for async I/O */ 11410c5615cSJohn Dyson struct pipe *pipe_peer; /* link with other direction */ 1154795b82cSRobert Watson struct pipepair *pipe_pair; /* container structure pointer */ 11689744405SMateusz Guzik u_short pipe_state; /* pipe status info */ 117f9fe7b28SMateusz Guzik u_char pipe_type; /* pipe type info */ 118f9fe7b28SMateusz Guzik u_char pipe_present; /* still present? */ 119f9fe7b28SMateusz Guzik int pipe_waiters; /* pipelock waiters */ 12010c5615cSJohn Dyson int pipe_busy; /* busy flag, mostly to handle rundown sanely */ 1215ff2bb52SDavid Xu int pipe_wgen; /* writer generation for named pipe */ 122a101072dSKonstantin Belousov ino_t pipe_ino; /* fake inode for stat(2) */ 12310c5615cSJohn Dyson }; 12410c5615cSJohn Dyson 1254795b82cSRobert Watson /* 126741b6cf8SKonstantin Belousov * Values for the pipe_present. 127741b6cf8SKonstantin Belousov */ 128741b6cf8SKonstantin Belousov #define PIPE_ACTIVE 1 129741b6cf8SKonstantin Belousov #define PIPE_CLOSING 2 130741b6cf8SKonstantin Belousov #define PIPE_FINALIZED 3 131741b6cf8SKonstantin Belousov 132741b6cf8SKonstantin Belousov /* 1334795b82cSRobert Watson * Container structure to hold the two pipe endpoints, mutex, and label 1344795b82cSRobert Watson * pointer. 1354795b82cSRobert Watson */ 1364795b82cSRobert Watson struct pipepair { 1374795b82cSRobert Watson struct pipe pp_rpipe; 1384795b82cSRobert Watson struct pipe pp_wpipe; 1394795b82cSRobert Watson struct mtx pp_mtx; 1404795b82cSRobert Watson struct label *pp_label; 1414795b82cSRobert Watson }; 1424795b82cSRobert Watson 1434795b82cSRobert Watson #define PIPE_MTX(pipe) (&(pipe)->pipe_pair->pp_mtx) 144f81b04d9SAlfred Perlstein #define PIPE_LOCK(pipe) mtx_lock(PIPE_MTX(pipe)) 145f81b04d9SAlfred Perlstein #define PIPE_UNLOCK(pipe) mtx_unlock(PIPE_MTX(pipe)) 146f81b04d9SAlfred Perlstein #define PIPE_LOCK_ASSERT(pipe, type) mtx_assert(PIPE_MTX(pipe), (type)) 147f81b04d9SAlfred Perlstein 148d485c77fSKonstantin Belousov #ifdef _KERNEL 14911ac7ec0SKip Macy void pipe_dtor(struct pipe *dpipe); 150c6d3d601SKonstantin Belousov int pipe_named_ctor(struct pipe **ppipe, struct thread *td); 15112a480faSDavid Xu void pipeselwakeup(struct pipe *cpipe); 152d485c77fSKonstantin Belousov #endif 1533da92a61SBruce Evans #endif /* !_SYS_PIPE_H_ */ 154