pipe: Add general notification queue support
[linux-2.6-block.git] / include / linux / pipe_fs_i.h
CommitLineData
b2441318 1/* SPDX-License-Identifier: GPL-2.0 */
1da177e4
LT
2#ifndef _LINUX_PIPE_FS_I_H
3#define _LINUX_PIPE_FS_I_H
4
35f3d14d 5#define PIPE_DEF_BUFFERS 16
1da177e4 6
1432873a
JA
7#define PIPE_BUF_FLAG_LRU 0x01 /* page is on the LRU */
8#define PIPE_BUF_FLAG_ATOMIC 0x02 /* was atomically mapped */
9#define PIPE_BUF_FLAG_GIFT 0x04 /* page is a gift */
9883035a 10#define PIPE_BUF_FLAG_PACKET 0x08 /* read() as a packet */
3e7ee3e7 11
0845718d
JA
12/**
13 * struct pipe_buffer - a linux kernel pipe buffer
14 * @page: the page containing the data for the pipe buffer
15 * @offset: offset of data inside the @page
16 * @len: length of data inside the @page
17 * @ops: operations associated with this buffer. See @pipe_buf_operations.
18 * @flags: pipe buffer flags. See above.
19 * @private: private data owned by the ops.
20 **/
1da177e4
LT
21struct pipe_buffer {
22 struct page *page;
23 unsigned int offset, len;
d4c3cca9 24 const struct pipe_buf_operations *ops;
3e7ee3e7 25 unsigned int flags;
497f9625 26 unsigned long private;
1da177e4
LT
27};
28
0845718d
JA
29/**
30 * struct pipe_inode_info - a linux kernel pipe
72b0d9aa 31 * @mutex: mutex protecting the whole thing
0bf999f9
RD
32 * @rd_wait: reader wait point in case of empty pipe
33 * @wr_wait: writer wait point in case of full pipe
8cefc107
DH
34 * @head: The point of buffer production
35 * @tail: The point of buffer consumption
6718b6f8 36 * @max_usage: The maximum number of slots that may be used in the ring
8cefc107 37 * @ring_size: total number of buffers (should be a power of 2)
c73be61c 38 * @nr_accounted: The amount this pipe accounts for in user->pipe_bufs
0845718d
JA
39 * @tmp_page: cached released page
40 * @readers: number of current readers of this pipe
41 * @writers: number of current writers of this pipe
e227867f 42 * @files: number of struct file referring this pipe (protected by ->i_lock)
0845718d
JA
43 * @r_counter: reader counter
44 * @w_counter: writer counter
45 * @fasync_readers: reader side fasync
46 * @fasync_writers: writer side fasync
0845718d 47 * @bufs: the circular array of pipe buffers
759c0114 48 * @user: the user who created this pipe
c73be61c 49 * @watch_queue: If this pipe is a watch_queue, this is the stuff for that
0845718d 50 **/
17374ff1 51struct pipe_inode_info {
72b0d9aa 52 struct mutex mutex;
0ddad21d 53 wait_queue_head_t rd_wait, wr_wait;
8cefc107
DH
54 unsigned int head;
55 unsigned int tail;
6718b6f8 56 unsigned int max_usage;
8cefc107 57 unsigned int ring_size;
c73be61c 58 unsigned int nr_accounted;
17374ff1
JA
59 unsigned int readers;
60 unsigned int writers;
ba5bb147 61 unsigned int files;
17374ff1
JA
62 unsigned int r_counter;
63 unsigned int w_counter;
35f3d14d 64 struct page *tmp_page;
17374ff1
JA
65 struct fasync_struct *fasync_readers;
66 struct fasync_struct *fasync_writers;
35f3d14d 67 struct pipe_buffer *bufs;
759c0114 68 struct user_struct *user;
c73be61c
DH
69#ifdef CONFIG_WATCH_QUEUE
70 struct watch_queue *watch_queue;
71#endif
17374ff1
JA
72};
73
f84d7519
JA
74/*
75 * Note on the nesting of these functions:
76 *
cac36bb0 77 * ->confirm()
f84d7519 78 * ->steal()
f84d7519 79 *
a949e639
MS
80 * That is, ->steal() must be called on a confirmed buffer.
81 * See below for the meaning of each operation. Also see kerneldoc
82 * in fs/pipe.c for the pipe and generic variants of these hooks.
f84d7519 83 */
1da177e4 84struct pipe_buf_operations {
0845718d
JA
85 /*
86 * ->confirm() verifies that the data in the pipe buffer is there
87 * and that the contents are good. If the pages in the pipe belong
88 * to a file system, we may need to wait for IO completion in this
89 * hook. Returns 0 for good, or a negative error value in case of
90 * error.
91 */
cac36bb0 92 int (*confirm)(struct pipe_inode_info *, struct pipe_buffer *);
0845718d
JA
93
94 /*
95 * When the contents of this pipe buffer has been completely
96 * consumed by a reader, ->release() is called.
97 */
1da177e4 98 void (*release)(struct pipe_inode_info *, struct pipe_buffer *);
0845718d
JA
99
100 /*
101 * Attempt to take ownership of the pipe buffer and its contents.
102 * ->steal() returns 0 for success, in which case the contents
103 * of the pipe (the buf->page) is locked and now completely owned
104 * by the caller. The page may then be transferred to a different
105 * mapping, the most often used case is insertion into different
106 * file address space cache.
107 */
5abc97aa 108 int (*steal)(struct pipe_inode_info *, struct pipe_buffer *);
0845718d
JA
109
110 /*
111 * Get a reference to the pipe buffer.
112 */
15fab63e 113 bool (*get)(struct pipe_inode_info *, struct pipe_buffer *);
1da177e4
LT
114};
115
8cefc107
DH
116/**
117 * pipe_empty - Return true if the pipe is empty
118 * @head: The pipe ring head pointer
119 * @tail: The pipe ring tail pointer
120 */
121static inline bool pipe_empty(unsigned int head, unsigned int tail)
122{
123 return head == tail;
124}
125
126/**
127 * pipe_occupancy - Return number of slots used in the pipe
128 * @head: The pipe ring head pointer
129 * @tail: The pipe ring tail pointer
130 */
131static inline unsigned int pipe_occupancy(unsigned int head, unsigned int tail)
132{
133 return head - tail;
134}
135
136/**
137 * pipe_full - Return true if the pipe is full
138 * @head: The pipe ring head pointer
139 * @tail: The pipe ring tail pointer
140 * @limit: The maximum amount of slots available.
141 */
142static inline bool pipe_full(unsigned int head, unsigned int tail,
143 unsigned int limit)
144{
145 return pipe_occupancy(head, tail) >= limit;
146}
147
148/**
149 * pipe_space_for_user - Return number of slots available to userspace
150 * @head: The pipe ring head pointer
151 * @tail: The pipe ring tail pointer
152 * @pipe: The pipe info structure
153 */
154static inline unsigned int pipe_space_for_user(unsigned int head, unsigned int tail,
155 struct pipe_inode_info *pipe)
156{
157 unsigned int p_occupancy, p_space;
158
159 p_occupancy = pipe_occupancy(head, tail);
6718b6f8 160 if (p_occupancy >= pipe->max_usage)
8cefc107
DH
161 return 0;
162 p_space = pipe->ring_size - p_occupancy;
6718b6f8
DH
163 if (p_space > pipe->max_usage)
164 p_space = pipe->max_usage;
8cefc107
DH
165 return p_space;
166}
167
7bf2d1df
MS
168/**
169 * pipe_buf_get - get a reference to a pipe_buffer
170 * @pipe: the pipe that the buffer belongs to
171 * @buf: the buffer to get a reference to
15fab63e
MW
172 *
173 * Return: %true if the reference was successfully obtained.
7bf2d1df 174 */
15fab63e 175static inline __must_check bool pipe_buf_get(struct pipe_inode_info *pipe,
7bf2d1df
MS
176 struct pipe_buffer *buf)
177{
15fab63e 178 return buf->ops->get(pipe, buf);
7bf2d1df
MS
179}
180
a779638c
MS
181/**
182 * pipe_buf_release - put a reference to a pipe_buffer
183 * @pipe: the pipe that the buffer belongs to
184 * @buf: the buffer to put a reference to
185 */
186static inline void pipe_buf_release(struct pipe_inode_info *pipe,
187 struct pipe_buffer *buf)
188{
189 const struct pipe_buf_operations *ops = buf->ops;
190
191 buf->ops = NULL;
192 ops->release(pipe, buf);
193}
194
fba597db
MS
195/**
196 * pipe_buf_confirm - verify contents of the pipe buffer
197 * @pipe: the pipe that the buffer belongs to
198 * @buf: the buffer to confirm
199 */
200static inline int pipe_buf_confirm(struct pipe_inode_info *pipe,
201 struct pipe_buffer *buf)
202{
203 return buf->ops->confirm(pipe, buf);
204}
205
ca76f5b6
MS
206/**
207 * pipe_buf_steal - attempt to take ownership of a pipe_buffer
208 * @pipe: the pipe that the buffer belongs to
209 * @buf: the buffer to attempt to steal
210 */
211static inline int pipe_buf_steal(struct pipe_inode_info *pipe,
212 struct pipe_buffer *buf)
213{
214 return buf->ops->steal(pipe, buf);
215}
216
1da177e4
LT
217/* Differs from PIPE_BUF in that PIPE_SIZE is the length of the actual
218 memory allocation, whereas PIPE_BUF makes atomicity guarantees. */
219#define PIPE_SIZE PAGE_SIZE
220
61e0d47c
MS
221/* Pipe lock and unlock operations */
222void pipe_lock(struct pipe_inode_info *);
223void pipe_unlock(struct pipe_inode_info *);
224void pipe_double_lock(struct pipe_inode_info *, struct pipe_inode_info *);
225
4c2e4bef 226extern unsigned int pipe_max_size;
759c0114
WT
227extern unsigned long pipe_user_pages_hard;
228extern unsigned long pipe_user_pages_soft;
ff9da691 229
1da177e4 230/* Drop the inode semaphore and wait for a pipe event, atomically */
3a326a2c 231void pipe_wait(struct pipe_inode_info *pipe);
1da177e4 232
7bee130e 233struct pipe_inode_info *alloc_pipe_info(void);
4b8a8f1e 234void free_pipe_info(struct pipe_inode_info *);
1da177e4 235
f84d7519 236/* Generic pipe buffer ops functions */
15fab63e 237bool generic_pipe_buf_get(struct pipe_inode_info *, struct pipe_buffer *);
cac36bb0 238int generic_pipe_buf_confirm(struct pipe_inode_info *, struct pipe_buffer *);
330ab716 239int generic_pipe_buf_steal(struct pipe_inode_info *, struct pipe_buffer *);
b9872226 240int generic_pipe_buf_nosteal(struct pipe_inode_info *, struct pipe_buffer *);
6818173b 241void generic_pipe_buf_release(struct pipe_inode_info *, struct pipe_buffer *);
a0ce2f0a 242void pipe_buf_mark_unmergeable(struct pipe_buffer *buf);
f84d7519 243
28a625cb
MS
244extern const struct pipe_buf_operations nosteal_pipe_buf_ops;
245
c73be61c
DH
246#ifdef CONFIG_WATCH_QUEUE
247unsigned long account_pipe_buffers(struct user_struct *user,
248 unsigned long old, unsigned long new);
249bool too_many_pipe_buffers_soft(unsigned long user_bufs);
250bool too_many_pipe_buffers_hard(unsigned long user_bufs);
251bool pipe_is_unprivileged_user(void);
252#endif
253
35f3d14d 254/* for F_SETPIPE_SZ and F_GETPIPE_SZ */
c73be61c
DH
255#ifdef CONFIG_WATCH_QUEUE
256int pipe_resize_ring(struct pipe_inode_info *pipe, unsigned int nr_slots);
257#endif
35f3d14d 258long pipe_fcntl(struct file *, unsigned int, unsigned long arg);
c73be61c 259struct pipe_inode_info *get_pipe_info(struct file *file, bool for_splice);
c66fb347 260
e4fad8e5 261int create_pipe_files(struct file **, int);
96e99be4 262unsigned int round_pipe_size(unsigned long size);
e4fad8e5 263
1da177e4 264#endif