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