pipe_fs_i.h 8.2 KB
Newer Older
1
/* SPDX-License-Identifier: GPL-2.0 */
L
Linus Torvalds 已提交
2 3 4
#ifndef _LINUX_PIPE_FS_I_H
#define _LINUX_PIPE_FS_I_H

5
#define PIPE_DEF_BUFFERS	16
L
Linus Torvalds 已提交
6

J
Jens Axboe 已提交
7 8 9
#define PIPE_BUF_FLAG_LRU	0x01	/* page is on the LRU */
#define PIPE_BUF_FLAG_ATOMIC	0x02	/* was atomically mapped */
#define PIPE_BUF_FLAG_GIFT	0x04	/* page is a gift */
10
#define PIPE_BUF_FLAG_PACKET	0x08	/* read() as a packet */
11
#define PIPE_BUF_FLAG_CAN_MERGE	0x10	/* can merge buffers */
12
#define PIPE_BUF_FLAG_WHOLE	0x20	/* read() must return entire buffer or error */
13
#ifdef CONFIG_WATCH_QUEUE
14
#define PIPE_BUF_FLAG_LOSS	0x40	/* Message loss happened after this buffer */
15
#endif
16

17 18 19 20 21 22 23 24 25
/**
 *	struct pipe_buffer - a linux kernel pipe buffer
 *	@page: the page containing the data for the pipe buffer
 *	@offset: offset of data inside the @page
 *	@len: length of data inside the @page
 *	@ops: operations associated with this buffer. See @pipe_buf_operations.
 *	@flags: pipe buffer flags. See above.
 *	@private: private data owned by the ops.
 **/
L
Linus Torvalds 已提交
26 27 28
struct pipe_buffer {
	struct page *page;
	unsigned int offset, len;
29
	const struct pipe_buf_operations *ops;
30
	unsigned int flags;
31
	unsigned long private;
L
Linus Torvalds 已提交
32 33
};

34 35
/**
 *	struct pipe_inode_info - a linux kernel pipe
A
Al Viro 已提交
36
 *	@mutex: mutex protecting the whole thing
37 38
 *	@rd_wait: reader wait point in case of empty pipe
 *	@wr_wait: writer wait point in case of full pipe
39 40
 *	@head: The point of buffer production
 *	@tail: The point of buffer consumption
41
 *	@note_loss: The next read() should insert a data-lost message
42
 *	@max_usage: The maximum number of slots that may be used in the ring
43
 *	@ring_size: total number of buffers (should be a power of 2)
44
 *	@nr_accounted: The amount this pipe accounts for in user->pipe_bufs
45 46 47
 *	@tmp_page: cached released page
 *	@readers: number of current readers of this pipe
 *	@writers: number of current writers of this pipe
48
 *	@files: number of struct file referring this pipe (protected by ->i_lock)
49 50
 *	@r_counter: reader counter
 *	@w_counter: writer counter
51
 *	@poll_usage: is this pipe used for epoll, which has crazy wakeups?
52 53 54
 *	@fasync_readers: reader side fasync
 *	@fasync_writers: writer side fasync
 *	@bufs: the circular array of pipe buffers
55
 *	@user: the user who created this pipe
56
 *	@watch_queue: If this pipe is a watch_queue, this is the stuff for that
57
 **/
58
struct pipe_inode_info {
A
Al Viro 已提交
59
	struct mutex mutex;
60
	wait_queue_head_t rd_wait, wr_wait;
61 62
	unsigned int head;
	unsigned int tail;
63
	unsigned int max_usage;
64
	unsigned int ring_size;
65 66 67
#ifdef CONFIG_WATCH_QUEUE
	bool note_loss;
#endif
68
	unsigned int nr_accounted;
69 70
	unsigned int readers;
	unsigned int writers;
71
	unsigned int files;
72 73
	unsigned int r_counter;
	unsigned int w_counter;
74
	bool poll_usage;
75
	struct page *tmp_page;
76 77
	struct fasync_struct *fasync_readers;
	struct fasync_struct *fasync_writers;
78
	struct pipe_buffer *bufs;
79
	struct user_struct *user;
80 81 82
#ifdef CONFIG_WATCH_QUEUE
	struct watch_queue *watch_queue;
#endif
83 84
};

85 86 87
/*
 * Note on the nesting of these functions:
 *
88
 * ->confirm()
89
 *	->try_steal()
90
 *
91 92 93
 * That is, ->try_steal() must be called on a confirmed buffer.  See below for
 * the meaning of each operation.  Also see the kerneldoc in fs/pipe.c for the
 * pipe and generic variants of these hooks.
94
 */
L
Linus Torvalds 已提交
95
struct pipe_buf_operations {
96 97 98 99 100
	/*
	 * ->confirm() verifies that the data in the pipe buffer is there
	 * and that the contents are good. If the pages in the pipe belong
	 * to a file system, we may need to wait for IO completion in this
	 * hook. Returns 0 for good, or a negative error value in case of
101
	 * error.  If not present all pages are considered good.
102
	 */
103
	int (*confirm)(struct pipe_inode_info *, struct pipe_buffer *);
104 105 106 107 108

	/*
	 * When the contents of this pipe buffer has been completely
	 * consumed by a reader, ->release() is called.
	 */
L
Linus Torvalds 已提交
109
	void (*release)(struct pipe_inode_info *, struct pipe_buffer *);
110 111 112

	/*
	 * Attempt to take ownership of the pipe buffer and its contents.
113 114 115 116 117
	 * ->try_steal() returns %true for success, in which case the contents
	 * of the pipe (the buf->page) is locked and now completely owned by the
	 * caller. The page may then be transferred to a different mapping, the
	 * most often used case is insertion into different file address space
	 * cache.
118
	 */
119
	bool (*try_steal)(struct pipe_inode_info *, struct pipe_buffer *);
120 121 122 123

	/*
	 * Get a reference to the pipe buffer.
	 */
124
	bool (*get)(struct pipe_inode_info *, struct pipe_buffer *);
L
Linus Torvalds 已提交
125 126
};

127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
/**
 * pipe_empty - Return true if the pipe is empty
 * @head: The pipe ring head pointer
 * @tail: The pipe ring tail pointer
 */
static inline bool pipe_empty(unsigned int head, unsigned int tail)
{
	return head == tail;
}

/**
 * pipe_occupancy - Return number of slots used in the pipe
 * @head: The pipe ring head pointer
 * @tail: The pipe ring tail pointer
 */
static inline unsigned int pipe_occupancy(unsigned int head, unsigned int tail)
{
	return head - tail;
}

/**
 * pipe_full - Return true if the pipe is full
 * @head: The pipe ring head pointer
 * @tail: The pipe ring tail pointer
 * @limit: The maximum amount of slots available.
 */
static inline bool pipe_full(unsigned int head, unsigned int tail,
			     unsigned int limit)
{
	return pipe_occupancy(head, tail) >= limit;
}

M
Miklos Szeredi 已提交
159 160 161 162
/**
 * pipe_buf_get - get a reference to a pipe_buffer
 * @pipe:	the pipe that the buffer belongs to
 * @buf:	the buffer to get a reference to
163 164
 *
 * Return: %true if the reference was successfully obtained.
M
Miklos Szeredi 已提交
165
 */
166
static inline __must_check bool pipe_buf_get(struct pipe_inode_info *pipe,
M
Miklos Szeredi 已提交
167 168
				struct pipe_buffer *buf)
{
169
	return buf->ops->get(pipe, buf);
M
Miklos Szeredi 已提交
170 171
}

172 173 174 175 176 177 178 179 180 181 182 183 184 185
/**
 * pipe_buf_release - put a reference to a pipe_buffer
 * @pipe:	the pipe that the buffer belongs to
 * @buf:	the buffer to put a reference to
 */
static inline void pipe_buf_release(struct pipe_inode_info *pipe,
				    struct pipe_buffer *buf)
{
	const struct pipe_buf_operations *ops = buf->ops;

	buf->ops = NULL;
	ops->release(pipe, buf);
}

186 187 188 189 190 191 192 193
/**
 * pipe_buf_confirm - verify contents of the pipe buffer
 * @pipe:	the pipe that the buffer belongs to
 * @buf:	the buffer to confirm
 */
static inline int pipe_buf_confirm(struct pipe_inode_info *pipe,
				   struct pipe_buffer *buf)
{
194 195
	if (!buf->ops->confirm)
		return 0;
196 197 198
	return buf->ops->confirm(pipe, buf);
}

M
Miklos Szeredi 已提交
199
/**
200
 * pipe_buf_try_steal - attempt to take ownership of a pipe_buffer
M
Miklos Szeredi 已提交
201 202 203
 * @pipe:	the pipe that the buffer belongs to
 * @buf:	the buffer to attempt to steal
 */
204 205
static inline bool pipe_buf_try_steal(struct pipe_inode_info *pipe,
		struct pipe_buffer *buf)
M
Miklos Szeredi 已提交
206
{
207 208 209
	if (!buf->ops->try_steal)
		return false;
	return buf->ops->try_steal(pipe, buf);
M
Miklos Szeredi 已提交
210 211
}

212 213 214 215 216 217 218 219 220
static inline void pipe_discard_from(struct pipe_inode_info *pipe,
		unsigned int old_head)
{
	unsigned int mask = pipe->ring_size - 1;

	while (pipe->head > old_head)
		pipe_buf_release(pipe, &pipe->bufs[--pipe->head & mask]);
}

L
Linus Torvalds 已提交
221 222 223 224
/* Differs from PIPE_BUF in that PIPE_SIZE is the length of the actual
   memory allocation, whereas PIPE_BUF makes atomicity guarantees.  */
#define PIPE_SIZE		PAGE_SIZE

225 226 227 228 229
/* Pipe lock and unlock operations */
void pipe_lock(struct pipe_inode_info *);
void pipe_unlock(struct pipe_inode_info *);
void pipe_double_lock(struct pipe_inode_info *, struct pipe_inode_info *);

230 231 232
/* Wait for a pipe to be readable/writable while dropping the pipe lock */
void pipe_wait_readable(struct pipe_inode_info *);
void pipe_wait_writable(struct pipe_inode_info *);
L
Linus Torvalds 已提交
233

A
Al Viro 已提交
234
struct pipe_inode_info *alloc_pipe_info(void);
235
void free_pipe_info(struct pipe_inode_info *);
L
Linus Torvalds 已提交
236

237
/* Generic pipe buffer ops functions */
238
bool generic_pipe_buf_get(struct pipe_inode_info *, struct pipe_buffer *);
239
bool generic_pipe_buf_try_steal(struct pipe_inode_info *, struct pipe_buffer *);
240
void generic_pipe_buf_release(struct pipe_inode_info *, struct pipe_buffer *);
241

M
Miklos Szeredi 已提交
242 243
extern const struct pipe_buf_operations nosteal_pipe_buf_ops;

244 245 246 247 248 249 250 251
#ifdef CONFIG_WATCH_QUEUE
unsigned long account_pipe_buffers(struct user_struct *user,
				   unsigned long old, unsigned long new);
bool too_many_pipe_buffers_soft(unsigned long user_bufs);
bool too_many_pipe_buffers_hard(unsigned long user_bufs);
bool pipe_is_unprivileged_user(void);
#endif

252
/* for F_SETPIPE_SZ and F_GETPIPE_SZ */
253 254 255
#ifdef CONFIG_WATCH_QUEUE
int pipe_resize_ring(struct pipe_inode_info *pipe, unsigned int nr_slots);
#endif
256
long pipe_fcntl(struct file *, unsigned int, unsigned long arg);
257
struct pipe_inode_info *get_pipe_info(struct file *file, bool for_splice);
258

A
Al Viro 已提交
259
int create_pipe_files(struct file **, int);
260
unsigned int round_pipe_size(unsigned long size);
A
Al Viro 已提交
261

L
Linus Torvalds 已提交
262
#endif