xref: /freebsd/sys/sys/pipe.h (revision 95ee2897)
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  */
2110c5615cSJohn Dyson 
223da92a61SBruce Evans #ifndef _SYS_PIPE_H_
233da92a61SBruce Evans #define _SYS_PIPE_H_
243da92a61SBruce Evans 
2510c5615cSJohn Dyson /*
262834ceecSJohn Dyson  * Pipe buffer size, keep moderate in value, pipes take kva space.
272834ceecSJohn Dyson  */
282834ceecSJohn Dyson #ifndef PIPE_SIZE
293da92a61SBruce Evans #define PIPE_SIZE	16384
302834ceecSJohn Dyson #endif
312834ceecSJohn Dyson 
32d1a5be10SJohn Dyson #ifndef BIG_PIPE_SIZE
33d1a5be10SJohn Dyson #define BIG_PIPE_SIZE	(64*1024)
34d1a5be10SJohn Dyson #endif
35d1a5be10SJohn Dyson 
36289016f2SMike Silbersack #ifndef SMALL_PIPE_SIZE
37b7fbac18SMike Silbersack #define SMALL_PIPE_SIZE	PAGE_SIZE
38289016f2SMike Silbersack #endif
39289016f2SMike Silbersack 
402834ceecSJohn Dyson /*
412834ceecSJohn Dyson  * PIPE_MINDIRECT MUST be smaller than PIPE_SIZE and MUST be bigger
423da92a61SBruce Evans  * than PIPE_BUF.
432834ceecSJohn Dyson  */
442834ceecSJohn Dyson #ifndef PIPE_MINDIRECT
453da92a61SBruce Evans #define PIPE_MINDIRECT	8192
462834ceecSJohn Dyson #endif
472834ceecSJohn Dyson 
48d1a5be10SJohn Dyson #define PIPENPAGES	(BIG_PIPE_SIZE / PAGE_SIZE + 1)
493da92a61SBruce Evans 
50d485c77fSKonstantin Belousov #ifdef _KERNEL
512834ceecSJohn Dyson /*
52289016f2SMike Silbersack  * See sys_pipe.c for info on what these limits mean.
53289016f2SMike Silbersack  */
5464ecd139SJohn Baldwin extern long	maxpipekva;
5511ac7ec0SKip Macy extern struct	fileops pipeops;
56d485c77fSKonstantin Belousov #endif
57289016f2SMike Silbersack 
58289016f2SMike Silbersack /*
593da92a61SBruce Evans  * Pipe buffer information.
603da92a61SBruce Evans  * Separate in, out, cnt are used to simplify calculations.
613da92a61SBruce Evans  * Buffered write is active when the buffer.cnt field is set.
6210c5615cSJohn Dyson  */
6310c5615cSJohn Dyson struct pipebuf {
6410c5615cSJohn Dyson 	u_int	cnt;		/* number of chars currently in buffer */
6510c5615cSJohn Dyson 	u_int	in;		/* in pointer */
6610c5615cSJohn Dyson 	u_int	out;		/* out pointer */
6710c5615cSJohn Dyson 	u_int	size;		/* size of buffer */
6810c5615cSJohn Dyson 	caddr_t	buffer;		/* kva of buffer */
6910c5615cSJohn Dyson };
7010c5615cSJohn Dyson 
7110c5615cSJohn Dyson /*
723da92a61SBruce Evans  * Information to support direct transfers between processes for pipes.
732834ceecSJohn Dyson  */
742834ceecSJohn Dyson struct pipemapping {
754c2ef8c1SMateusz Guzik 	u_int		cnt;		/* number of chars in buffer */
764c2ef8c1SMateusz Guzik 	u_int		pos;		/* current position of transfer */
772834ceecSJohn Dyson 	int		npages;		/* number of pages */
782834ceecSJohn Dyson 	vm_page_t	ms[PIPENPAGES];	/* pages in source process */
792834ceecSJohn Dyson };
802834ceecSJohn Dyson 
812834ceecSJohn Dyson /*
823da92a61SBruce Evans  * Bits in pipe_state.
8310c5615cSJohn Dyson  */
843da92a61SBruce Evans #define PIPE_ASYNC	0x004	/* Async? I/O. */
853da92a61SBruce Evans #define PIPE_WANTR	0x008	/* Reader wants some characters. */
863da92a61SBruce Evans #define PIPE_WANTW	0x010	/* Writer wants space to put characters. */
873da92a61SBruce Evans #define PIPE_WANT	0x020	/* Pipe is wanted to be run-down. */
883da92a61SBruce Evans #define PIPE_SEL	0x040	/* Pipe has a select active. */
893da92a61SBruce Evans #define PIPE_EOF	0x080	/* Pipe is in EOF condition. */
90f81b04d9SAlfred Perlstein #define PIPE_LOCKFL	0x100	/* Process has exclusive access to pointers/data. */
913da92a61SBruce Evans #define PIPE_LWANT	0x200	/* Process wants exclusive access to pointers/data. */
923da92a61SBruce Evans #define PIPE_DIRECTW	0x400	/* Pipe direct write active. */
933da92a61SBruce Evans #define PIPE_DIRECTOK	0x800	/* Direct mode ok. */
9489744405SMateusz Guzik 
9589744405SMateusz Guzik /*
9689744405SMateusz Guzik  * Bits in pipe_type.
9789744405SMateusz Guzik  */
9889744405SMateusz Guzik #define PIPE_TYPE_NAMED	0x001	/* Is a named pipe. */
992834ceecSJohn Dyson 
1002834ceecSJohn Dyson /*
1013da92a61SBruce Evans  * Per-pipe data structure.
1023da92a61SBruce Evans  * Two of these are linked together to produce bi-directional pipes.
10310c5615cSJohn Dyson  */
10410c5615cSJohn Dyson struct pipe {
10510c5615cSJohn Dyson 	struct	pipebuf pipe_buffer;	/* data storage */
10685232c2fSMark Johnston 	struct	pipemapping pipe_pages;	/* wired pages for direct I/O */
10710c5615cSJohn Dyson 	struct	selinfo pipe_sel;	/* for compat with select */
108a0502b19SPoul-Henning Kamp 	struct	timespec pipe_atime;	/* time of last access */
109a0502b19SPoul-Henning Kamp 	struct	timespec pipe_mtime;	/* time of last modify */
110a0502b19SPoul-Henning Kamp 	struct	timespec pipe_ctime;	/* time of status change */
11162d6ce3aSDon Lewis 	struct	sigio *pipe_sigio;	/* information for async I/O */
11210c5615cSJohn Dyson 	struct	pipe *pipe_peer;	/* link with other direction */
1134795b82cSRobert Watson 	struct	pipepair *pipe_pair;	/* container structure pointer */
11489744405SMateusz Guzik 	u_short	pipe_state;		/* pipe status info */
115f9fe7b28SMateusz Guzik 	u_char	pipe_type;		/* pipe type info */
116f9fe7b28SMateusz Guzik 	u_char	pipe_present;		/* still present? */
117f9fe7b28SMateusz Guzik 	int	pipe_waiters;		/* pipelock waiters */
11810c5615cSJohn Dyson 	int	pipe_busy;		/* busy flag, mostly to handle rundown sanely */
1195ff2bb52SDavid Xu 	int	pipe_wgen;		/* writer generation for named pipe */
120a101072dSKonstantin Belousov 	ino_t	pipe_ino;		/* fake inode for stat(2) */
12110c5615cSJohn Dyson };
12210c5615cSJohn Dyson 
1234795b82cSRobert Watson /*
124741b6cf8SKonstantin Belousov  * Values for the pipe_present.
125741b6cf8SKonstantin Belousov  */
126741b6cf8SKonstantin Belousov #define PIPE_ACTIVE		1
127741b6cf8SKonstantin Belousov #define	PIPE_CLOSING		2
128741b6cf8SKonstantin Belousov #define	PIPE_FINALIZED		3
129741b6cf8SKonstantin Belousov 
130741b6cf8SKonstantin Belousov /*
1314795b82cSRobert Watson  * Container structure to hold the two pipe endpoints, mutex, and label
1324795b82cSRobert Watson  * pointer.
1334795b82cSRobert Watson  */
1344795b82cSRobert Watson struct pipepair {
1354795b82cSRobert Watson 	struct pipe	pp_rpipe;
1364795b82cSRobert Watson 	struct pipe	pp_wpipe;
1374795b82cSRobert Watson 	struct mtx	pp_mtx;
1384795b82cSRobert Watson 	struct label	*pp_label;
1394795b82cSRobert Watson };
1404795b82cSRobert Watson 
1414795b82cSRobert Watson #define PIPE_MTX(pipe)		(&(pipe)->pipe_pair->pp_mtx)
142f81b04d9SAlfred Perlstein #define PIPE_LOCK(pipe)		mtx_lock(PIPE_MTX(pipe))
143f81b04d9SAlfred Perlstein #define PIPE_UNLOCK(pipe)	mtx_unlock(PIPE_MTX(pipe))
144f81b04d9SAlfred Perlstein #define PIPE_LOCK_ASSERT(pipe, type)  mtx_assert(PIPE_MTX(pipe), (type))
145f81b04d9SAlfred Perlstein 
146d485c77fSKonstantin Belousov #ifdef _KERNEL
14711ac7ec0SKip Macy void	pipe_dtor(struct pipe *dpipe);
148c6d3d601SKonstantin Belousov int	pipe_named_ctor(struct pipe **ppipe, struct thread *td);
14912a480faSDavid Xu void	pipeselwakeup(struct pipe *cpipe);
150d485c77fSKonstantin Belousov #endif
1513da92a61SBruce Evans #endif /* !_SYS_PIPE_H_ */
152