1139825Simp/*-
213675Sdyson * Copyright (c) 1996 John S. Dyson
313675Sdyson * All rights reserved.
413675Sdyson *
513675Sdyson * Redistribution and use in source and binary forms, with or without
613675Sdyson * modification, are permitted provided that the following conditions
713675Sdyson * are met:
813675Sdyson * 1. Redistributions of source code must retain the above copyright
913675Sdyson *    notice immediately at the beginning of the file, without modification,
1013675Sdyson *    this list of conditions, and the following disclaimer.
1113675Sdyson * 2. Redistributions in binary form must reproduce the above copyright
1213675Sdyson *    notice, this list of conditions and the following disclaimer in the
1313675Sdyson *    documentation and/or other materials provided with the distribution.
1413675Sdyson * 3. Absolutely no warranty of function or purpose is made by the author
1513675Sdyson *    John S. Dyson.
1613675Sdyson * 4. This work was done expressly for inclusion into FreeBSD.  Other use
1713675Sdyson *    is allowed if this notation is included.
1813675Sdyson * 5. Modifications may be freely made to this file if the above conditions
1913675Sdyson *    are met.
2013675Sdyson *
2150477Speter * $FreeBSD$
2213675Sdyson */
2313675Sdyson
2415504Sbde#ifndef _SYS_PIPE_H_
2515504Sbde#define _SYS_PIPE_H_
2615504Sbde
2755205Speter#ifndef _KERNEL
28132478Ssilby#error "no user-servicable parts inside"
2915504Sbde#endif
3013675Sdyson
3113675Sdyson/*
3213907Sdyson * Pipe buffer size, keep moderate in value, pipes take kva space.
3313907Sdyson */
3413907Sdyson#ifndef PIPE_SIZE
3515504Sbde#define PIPE_SIZE	16384
3613907Sdyson#endif
3713907Sdyson
3817163Sdyson#ifndef BIG_PIPE_SIZE
3917163Sdyson#define BIG_PIPE_SIZE	(64*1024)
4017163Sdyson#endif
4117163Sdyson
42117325Ssilby#ifndef SMALL_PIPE_SIZE
43118567Ssilby#define SMALL_PIPE_SIZE	PAGE_SIZE
44117325Ssilby#endif
45117325Ssilby
4613907Sdyson/*
4713907Sdyson * PIPE_MINDIRECT MUST be smaller than PIPE_SIZE and MUST be bigger
4815504Sbde * than PIPE_BUF.
4913907Sdyson */
5013907Sdyson#ifndef PIPE_MINDIRECT
5115504Sbde#define PIPE_MINDIRECT	8192
5213907Sdyson#endif
5313907Sdyson
5417163Sdyson#define PIPENPAGES	(BIG_PIPE_SIZE / PAGE_SIZE + 1)
5515504Sbde
5613907Sdyson/*
57117325Ssilby * See sys_pipe.c for info on what these limits mean.
58117325Ssilby */
59189649Sjhbextern long	maxpipekva;
60232055Skmacyextern struct	fileops pipeops;
61117325Ssilby
62117325Ssilby/*
6315504Sbde * Pipe buffer information.
6415504Sbde * Separate in, out, cnt are used to simplify calculations.
6515504Sbde * Buffered write is active when the buffer.cnt field is set.
6613675Sdyson */
6713675Sdysonstruct pipebuf {
6813675Sdyson	u_int	cnt;		/* number of chars currently in buffer */
6913675Sdyson	u_int	in;		/* in pointer */
7013675Sdyson	u_int	out;		/* out pointer */
7113675Sdyson	u_int	size;		/* size of buffer */
7213675Sdyson	caddr_t	buffer;		/* kva of buffer */
7313675Sdyson};
7413675Sdyson
7513675Sdyson/*
7615504Sbde * Information to support direct transfers between processes for pipes.
7713907Sdyson */
7813907Sdysonstruct pipemapping {
7913907Sdyson	vm_size_t	cnt;		/* number of chars in buffer */
8013907Sdyson	vm_size_t	pos;		/* current position of transfer */
8113907Sdyson	int		npages;		/* number of pages */
8213907Sdyson	vm_page_t	ms[PIPENPAGES];	/* pages in source process */
8313907Sdyson};
8413907Sdyson
8513907Sdyson/*
8615504Sbde * Bits in pipe_state.
8713675Sdyson */
8815504Sbde#define PIPE_ASYNC	0x004	/* Async? I/O. */
8915504Sbde#define PIPE_WANTR	0x008	/* Reader wants some characters. */
9015504Sbde#define PIPE_WANTW	0x010	/* Writer wants space to put characters. */
9115504Sbde#define PIPE_WANT	0x020	/* Pipe is wanted to be run-down. */
9215504Sbde#define PIPE_SEL	0x040	/* Pipe has a select active. */
9315504Sbde#define PIPE_EOF	0x080	/* Pipe is in EOF condition. */
9491362Salfred#define PIPE_LOCKFL	0x100	/* Process has exclusive access to pointers/data. */
9515504Sbde#define PIPE_LWANT	0x200	/* Process wants exclusive access to pointers/data. */
9615504Sbde#define PIPE_DIRECTW	0x400	/* Pipe direct write active. */
9715504Sbde#define PIPE_DIRECTOK	0x800	/* Direct mode ok. */
98232055Skmacy#define PIPE_NAMED	0x1000	/* Is a named pipe. */
9913675Sdyson
10013675Sdyson/*
10115504Sbde * Per-pipe data structure.
10215504Sbde * Two of these are linked together to produce bi-directional pipes.
10313907Sdyson */
10413675Sdysonstruct pipe {
10513675Sdyson	struct	pipebuf pipe_buffer;	/* data storage */
10615504Sbde	struct	pipemapping pipe_map;	/* pipe mapping for direct I/O */
10713675Sdyson	struct	selinfo pipe_sel;	/* for compat with select */
10834901Sphk	struct	timespec pipe_atime;	/* time of last access */
10934901Sphk	struct	timespec pipe_mtime;	/* time of last modify */
11034901Sphk	struct	timespec pipe_ctime;	/* time of status change */
11141087Struckman	struct	sigio *pipe_sigio;	/* information for async I/O */
11215504Sbde	struct	pipe *pipe_peer;	/* link with other direction */
113125293Srwatson	struct	pipepair *pipe_pair;	/* container structure pointer */
11413675Sdyson	u_int	pipe_state;		/* pipe status info */
11513675Sdyson	int	pipe_busy;		/* busy flag, mostly to handle rundown sanely */
116125293Srwatson	int	pipe_present;		/* still present? */
117238936Sdavidxu	int	pipe_wgen;		/* writer generation for named pipe */
118226042Skib	ino_t	pipe_ino;		/* fake inode for stat(2) */
11913675Sdyson};
12013675Sdyson
121125293Srwatson/*
122179243Skib * Values for the pipe_present.
123179243Skib */
124179243Skib#define PIPE_ACTIVE		1
125179243Skib#define	PIPE_CLOSING		2
126179243Skib#define	PIPE_FINALIZED		3
127179243Skib
128179243Skib/*
129125293Srwatson * Container structure to hold the two pipe endpoints, mutex, and label
130125293Srwatson * pointer.
131125293Srwatson */
132125293Srwatsonstruct pipepair {
133125293Srwatson	struct pipe	pp_rpipe;
134125293Srwatson	struct pipe	pp_wpipe;
135125293Srwatson	struct mtx	pp_mtx;
136125293Srwatson	struct label	*pp_label;
137125293Srwatson};
138125293Srwatson
139125293Srwatson#define PIPE_MTX(pipe)		(&(pipe)->pipe_pair->pp_mtx)
14091362Salfred#define PIPE_LOCK(pipe)		mtx_lock(PIPE_MTX(pipe))
14191362Salfred#define PIPE_UNLOCK(pipe)	mtx_unlock(PIPE_MTX(pipe))
14291362Salfred#define PIPE_LOCK_ASSERT(pipe, type)  mtx_assert(PIPE_MTX(pipe), (type))
14391362Salfred
144232055Skmacyvoid	pipe_dtor(struct pipe *dpipe);
145268335Smjgvoid	pipe_named_ctor(struct pipe **ppipe, struct thread *td);
146238928Sdavidxuvoid	pipeselwakeup(struct pipe *cpipe);
14715504Sbde#endif /* !_SYS_PIPE_H_ */
148