xref: /freebsd/sys/sys/pipe.h (revision 5ff2bb52)
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 
27664a31e4SPeter Wemm #ifndef _KERNEL
28a0c03350SMike Silbersack #error "no user-servicable parts inside"
293da92a61SBruce Evans #endif
3010c5615cSJohn Dyson 
3110c5615cSJohn Dyson /*
322834ceecSJohn Dyson  * Pipe buffer size, keep moderate in value, pipes take kva space.
332834ceecSJohn Dyson  */
342834ceecSJohn Dyson #ifndef PIPE_SIZE
353da92a61SBruce Evans #define PIPE_SIZE	16384
362834ceecSJohn Dyson #endif
372834ceecSJohn Dyson 
38d1a5be10SJohn Dyson #ifndef BIG_PIPE_SIZE
39d1a5be10SJohn Dyson #define BIG_PIPE_SIZE	(64*1024)
40d1a5be10SJohn Dyson #endif
41d1a5be10SJohn Dyson 
42289016f2SMike Silbersack #ifndef SMALL_PIPE_SIZE
43b7fbac18SMike Silbersack #define SMALL_PIPE_SIZE	PAGE_SIZE
44289016f2SMike Silbersack #endif
45289016f2SMike Silbersack 
462834ceecSJohn Dyson /*
472834ceecSJohn Dyson  * PIPE_MINDIRECT MUST be smaller than PIPE_SIZE and MUST be bigger
483da92a61SBruce Evans  * than PIPE_BUF.
492834ceecSJohn Dyson  */
502834ceecSJohn Dyson #ifndef PIPE_MINDIRECT
513da92a61SBruce Evans #define PIPE_MINDIRECT	8192
522834ceecSJohn Dyson #endif
532834ceecSJohn Dyson 
54d1a5be10SJohn Dyson #define PIPENPAGES	(BIG_PIPE_SIZE / PAGE_SIZE + 1)
553da92a61SBruce Evans 
562834ceecSJohn Dyson /*
57289016f2SMike Silbersack  * See sys_pipe.c for info on what these limits mean.
58289016f2SMike Silbersack  */
5964ecd139SJohn Baldwin extern long	maxpipekva;
6011ac7ec0SKip Macy extern struct	fileops pipeops;
61289016f2SMike Silbersack 
62289016f2SMike Silbersack /*
633da92a61SBruce Evans  * Pipe buffer information.
643da92a61SBruce Evans  * Separate in, out, cnt are used to simplify calculations.
653da92a61SBruce Evans  * Buffered write is active when the buffer.cnt field is set.
6610c5615cSJohn Dyson  */
6710c5615cSJohn Dyson struct pipebuf {
6810c5615cSJohn Dyson 	u_int	cnt;		/* number of chars currently in buffer */
6910c5615cSJohn Dyson 	u_int	in;		/* in pointer */
7010c5615cSJohn Dyson 	u_int	out;		/* out pointer */
7110c5615cSJohn Dyson 	u_int	size;		/* size of buffer */
7210c5615cSJohn Dyson 	caddr_t	buffer;		/* kva of buffer */
7310c5615cSJohn Dyson };
7410c5615cSJohn Dyson 
7510c5615cSJohn Dyson /*
763da92a61SBruce Evans  * Information to support direct transfers between processes for pipes.
772834ceecSJohn Dyson  */
782834ceecSJohn Dyson struct pipemapping {
792834ceecSJohn Dyson 	vm_size_t	cnt;		/* number of chars in buffer */
802834ceecSJohn Dyson 	vm_size_t	pos;		/* current position of transfer */
812834ceecSJohn Dyson 	int		npages;		/* number of pages */
822834ceecSJohn Dyson 	vm_page_t	ms[PIPENPAGES];	/* pages in source process */
832834ceecSJohn Dyson };
842834ceecSJohn Dyson 
852834ceecSJohn Dyson /*
863da92a61SBruce Evans  * Bits in pipe_state.
8710c5615cSJohn Dyson  */
883da92a61SBruce Evans #define PIPE_ASYNC	0x004	/* Async? I/O. */
893da92a61SBruce Evans #define PIPE_WANTR	0x008	/* Reader wants some characters. */
903da92a61SBruce Evans #define PIPE_WANTW	0x010	/* Writer wants space to put characters. */
913da92a61SBruce Evans #define PIPE_WANT	0x020	/* Pipe is wanted to be run-down. */
923da92a61SBruce Evans #define PIPE_SEL	0x040	/* Pipe has a select active. */
933da92a61SBruce Evans #define PIPE_EOF	0x080	/* Pipe is in EOF condition. */
94f81b04d9SAlfred Perlstein #define PIPE_LOCKFL	0x100	/* Process has exclusive access to pointers/data. */
953da92a61SBruce Evans #define PIPE_LWANT	0x200	/* Process wants exclusive access to pointers/data. */
963da92a61SBruce Evans #define PIPE_DIRECTW	0x400	/* Pipe direct write active. */
973da92a61SBruce Evans #define PIPE_DIRECTOK	0x800	/* Direct mode ok. */
9811ac7ec0SKip Macy #define PIPE_NAMED	0x1000	/* 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 */
1063da92a61SBruce Evans 	struct	pipemapping pipe_map;	/* pipe mapping 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 */
11410c5615cSJohn Dyson 	u_int	pipe_state;		/* pipe status info */
11510c5615cSJohn Dyson 	int	pipe_busy;		/* busy flag, mostly to handle rundown sanely */
1164795b82cSRobert Watson 	int	pipe_present;		/* still present? */
1175ff2bb52SDavid Xu 	int	pipe_wgen;		/* writer generation for named pipe */
118a101072dSKonstantin Belousov 	ino_t	pipe_ino;		/* fake inode for stat(2) */
11910c5615cSJohn Dyson };
12010c5615cSJohn Dyson 
1214795b82cSRobert Watson /*
122741b6cf8SKonstantin Belousov  * Values for the pipe_present.
123741b6cf8SKonstantin Belousov  */
124741b6cf8SKonstantin Belousov #define PIPE_ACTIVE		1
125741b6cf8SKonstantin Belousov #define	PIPE_CLOSING		2
126741b6cf8SKonstantin Belousov #define	PIPE_FINALIZED		3
127741b6cf8SKonstantin Belousov 
128741b6cf8SKonstantin Belousov /*
1294795b82cSRobert Watson  * Container structure to hold the two pipe endpoints, mutex, and label
1304795b82cSRobert Watson  * pointer.
1314795b82cSRobert Watson  */
1324795b82cSRobert Watson struct pipepair {
1334795b82cSRobert Watson 	struct pipe	pp_rpipe;
1344795b82cSRobert Watson 	struct pipe	pp_wpipe;
1354795b82cSRobert Watson 	struct mtx	pp_mtx;
1364795b82cSRobert Watson 	struct label	*pp_label;
1374795b82cSRobert Watson };
1384795b82cSRobert Watson 
1394795b82cSRobert Watson #define PIPE_MTX(pipe)		(&(pipe)->pipe_pair->pp_mtx)
140f81b04d9SAlfred Perlstein #define PIPE_LOCK(pipe)		mtx_lock(PIPE_MTX(pipe))
141f81b04d9SAlfred Perlstein #define PIPE_UNLOCK(pipe)	mtx_unlock(PIPE_MTX(pipe))
142f81b04d9SAlfred Perlstein #define PIPE_LOCK_ASSERT(pipe, type)  mtx_assert(PIPE_MTX(pipe), (type))
143f81b04d9SAlfred Perlstein 
14411ac7ec0SKip Macy void	pipe_dtor(struct pipe *dpipe);
14511ac7ec0SKip Macy int	pipe_named_ctor(struct pipe **ppipe, struct thread *td);
14612a480faSDavid Xu void	pipeselwakeup(struct pipe *cpipe);
1473da92a61SBruce Evans #endif /* !_SYS_PIPE_H_ */
148