blob: c00c618ef290d726874d18c00c174e71d215a4ae [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002#ifndef _LINUX_PIPE_FS_I_H
3#define _LINUX_PIPE_FS_I_H
4
Jens Axboe35f3d142010-05-20 10:43:18 +02005#define PIPE_DEF_BUFFERS 16
Linus Torvalds1da177e2005-04-16 15:20:36 -07006
Jens Axboe14328732006-05-03 10:35:26 +02007#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 */
Linus Torvalds9883035a2012-04-29 13:12:42 -070010#define PIPE_BUF_FLAG_PACKET 0x08 /* read() as a packet */
Christoph Hellwigf6dd9752020-05-20 17:58:12 +020011#define PIPE_BUF_FLAG_CAN_MERGE 0x10 /* can merge buffers */
Linus Torvalds6c329782020-06-13 09:56:21 -070012#define PIPE_BUF_FLAG_WHOLE 0x20 /* read() must return entire buffer or error */
David Howellse7d553d2020-01-14 17:07:12 +000013#ifdef CONFIG_WATCH_QUEUE
Linus Torvalds6c329782020-06-13 09:56:21 -070014#define PIPE_BUF_FLAG_LOSS 0x40 /* Message loss happened after this buffer */
David Howellse7d553d2020-01-14 17:07:12 +000015#endif
Jens Axboe3e7ee3e2006-04-02 23:11:04 +020016
Jens Axboe08457182007-06-12 20:51:32 +020017/**
18 * struct pipe_buffer - a linux kernel pipe buffer
19 * @page: the page containing the data for the pipe buffer
20 * @offset: offset of data inside the @page
21 * @len: length of data inside the @page
22 * @ops: operations associated with this buffer. See @pipe_buf_operations.
23 * @flags: pipe buffer flags. See above.
24 * @private: private data owned by the ops.
25 **/
Linus Torvalds1da177e2005-04-16 15:20:36 -070026struct pipe_buffer {
27 struct page *page;
28 unsigned int offset, len;
Eric Dumazetd4c3cca2006-12-13 00:34:04 -080029 const struct pipe_buf_operations *ops;
Jens Axboe3e7ee3e2006-04-02 23:11:04 +020030 unsigned int flags;
Jens Axboe497f9622007-06-11 12:00:45 +020031 unsigned long private;
Linus Torvalds1da177e2005-04-16 15:20:36 -070032};
33
Jens Axboe08457182007-06-12 20:51:32 +020034/**
35 * struct pipe_inode_info - a linux kernel pipe
Al Viro72b0d9a2013-03-21 02:32:24 -040036 * @mutex: mutex protecting the whole thing
Randy Dunlap0bf999f2020-02-09 19:36:14 -080037 * @rd_wait: reader wait point in case of empty pipe
38 * @wr_wait: writer wait point in case of full pipe
David Howells8cefc102019-11-15 13:30:32 +000039 * @head: The point of buffer production
40 * @tail: The point of buffer consumption
David Howellse7d553d2020-01-14 17:07:12 +000041 * @note_loss: The next read() should insert a data-lost message
David Howells6718b6f2019-10-16 16:47:32 +010042 * @max_usage: The maximum number of slots that may be used in the ring
David Howells8cefc102019-11-15 13:30:32 +000043 * @ring_size: total number of buffers (should be a power of 2)
David Howellsc73be612020-01-14 17:07:11 +000044 * @nr_accounted: The amount this pipe accounts for in user->pipe_bufs
Jens Axboe08457182007-06-12 20:51:32 +020045 * @tmp_page: cached released page
46 * @readers: number of current readers of this pipe
47 * @writers: number of current writers of this pipe
Masanari Iidae2278672014-02-18 22:54:36 +090048 * @files: number of struct file referring this pipe (protected by ->i_lock)
Jens Axboe08457182007-06-12 20:51:32 +020049 * @r_counter: reader counter
50 * @w_counter: writer counter
Linus Torvalds3b844822021-08-05 10:04:43 -070051 * @poll_usage: is this pipe used for epoll, which has crazy wakeups?
Jens Axboe08457182007-06-12 20:51:32 +020052 * @fasync_readers: reader side fasync
53 * @fasync_writers: writer side fasync
Jens Axboe08457182007-06-12 20:51:32 +020054 * @bufs: the circular array of pipe buffers
Willy Tarreau759c0112016-01-18 16:36:09 +010055 * @user: the user who created this pipe
David Howellsc73be612020-01-14 17:07:11 +000056 * @watch_queue: If this pipe is a watch_queue, this is the stuff for that
Jens Axboe08457182007-06-12 20:51:32 +020057 **/
Jens Axboe17374ff2007-06-04 15:03:12 +020058struct pipe_inode_info {
Al Viro72b0d9a2013-03-21 02:32:24 -040059 struct mutex mutex;
Linus Torvalds0ddad212019-12-09 09:48:27 -080060 wait_queue_head_t rd_wait, wr_wait;
David Howells8cefc102019-11-15 13:30:32 +000061 unsigned int head;
62 unsigned int tail;
David Howells6718b6f2019-10-16 16:47:32 +010063 unsigned int max_usage;
David Howells8cefc102019-11-15 13:30:32 +000064 unsigned int ring_size;
David Howellse7d553d2020-01-14 17:07:12 +000065#ifdef CONFIG_WATCH_QUEUE
66 bool note_loss;
67#endif
David Howellsc73be612020-01-14 17:07:11 +000068 unsigned int nr_accounted;
Jens Axboe17374ff2007-06-04 15:03:12 +020069 unsigned int readers;
70 unsigned int writers;
Al Viroba5bb142013-03-21 02:21:19 -040071 unsigned int files;
Jens Axboe17374ff2007-06-04 15:03:12 +020072 unsigned int r_counter;
73 unsigned int w_counter;
Linus Torvalds3b844822021-08-05 10:04:43 -070074 unsigned int poll_usage;
Jens Axboe35f3d142010-05-20 10:43:18 +020075 struct page *tmp_page;
Jens Axboe17374ff2007-06-04 15:03:12 +020076 struct fasync_struct *fasync_readers;
77 struct fasync_struct *fasync_writers;
Jens Axboe35f3d142010-05-20 10:43:18 +020078 struct pipe_buffer *bufs;
Willy Tarreau759c0112016-01-18 16:36:09 +010079 struct user_struct *user;
David Howellsc73be612020-01-14 17:07:11 +000080#ifdef CONFIG_WATCH_QUEUE
81 struct watch_queue *watch_queue;
82#endif
Jens Axboe17374ff2007-06-04 15:03:12 +020083};
84
Jens Axboef84d7512006-05-01 19:59:03 +020085/*
86 * Note on the nesting of these functions:
87 *
Jens Axboecac36bb02007-06-14 13:10:48 +020088 * ->confirm()
Christoph Hellwigc928f642020-05-20 17:58:16 +020089 * ->try_steal()
Jens Axboef84d7512006-05-01 19:59:03 +020090 *
Christoph Hellwigc928f642020-05-20 17:58:16 +020091 * That is, ->try_steal() must be called on a confirmed buffer. See below for
92 * the meaning of each operation. Also see the kerneldoc in fs/pipe.c for the
93 * pipe and generic variants of these hooks.
Jens Axboef84d7512006-05-01 19:59:03 +020094 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070095struct pipe_buf_operations {
Jens Axboe08457182007-06-12 20:51:32 +020096 /*
Jens Axboe08457182007-06-12 20:51:32 +020097 * ->confirm() verifies that the data in the pipe buffer is there
98 * and that the contents are good. If the pages in the pipe belong
99 * to a file system, we may need to wait for IO completion in this
100 * hook. Returns 0 for good, or a negative error value in case of
Christoph Hellwigb8d9e7f2020-05-20 17:58:15 +0200101 * error. If not present all pages are considered good.
Jens Axboe08457182007-06-12 20:51:32 +0200102 */
Jens Axboecac36bb02007-06-14 13:10:48 +0200103 int (*confirm)(struct pipe_inode_info *, struct pipe_buffer *);
Jens Axboe08457182007-06-12 20:51:32 +0200104
105 /*
106 * When the contents of this pipe buffer has been completely
107 * consumed by a reader, ->release() is called.
108 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 void (*release)(struct pipe_inode_info *, struct pipe_buffer *);
Jens Axboe08457182007-06-12 20:51:32 +0200110
111 /*
112 * Attempt to take ownership of the pipe buffer and its contents.
Christoph Hellwigc928f642020-05-20 17:58:16 +0200113 * ->try_steal() returns %true for success, in which case the contents
114 * of the pipe (the buf->page) is locked and now completely owned by the
115 * caller. The page may then be transferred to a different mapping, the
116 * most often used case is insertion into different file address space
117 * cache.
Jens Axboe08457182007-06-12 20:51:32 +0200118 */
Christoph Hellwigc928f642020-05-20 17:58:16 +0200119 bool (*try_steal)(struct pipe_inode_info *, struct pipe_buffer *);
Jens Axboe08457182007-06-12 20:51:32 +0200120
121 /*
122 * Get a reference to the pipe buffer.
123 */
Matthew Wilcox15fab632019-04-05 14:02:10 -0700124 bool (*get)(struct pipe_inode_info *, struct pipe_buffer *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125};
126
Miklos Szeredi7bf2d1d2016-09-27 10:45:12 +0200127/**
David Howells8cefc102019-11-15 13:30:32 +0000128 * pipe_empty - Return true if the pipe is empty
129 * @head: The pipe ring head pointer
130 * @tail: The pipe ring tail pointer
131 */
132static inline bool pipe_empty(unsigned int head, unsigned int tail)
133{
134 return head == tail;
135}
136
137/**
138 * pipe_occupancy - Return number of slots used in the pipe
139 * @head: The pipe ring head pointer
140 * @tail: The pipe ring tail pointer
141 */
142static inline unsigned int pipe_occupancy(unsigned int head, unsigned int tail)
143{
144 return head - tail;
145}
146
147/**
148 * pipe_full - Return true if the pipe is full
149 * @head: The pipe ring head pointer
150 * @tail: The pipe ring tail pointer
151 * @limit: The maximum amount of slots available.
152 */
153static inline bool pipe_full(unsigned int head, unsigned int tail,
154 unsigned int limit)
155{
156 return pipe_occupancy(head, tail) >= limit;
157}
158
159/**
160 * pipe_space_for_user - Return number of slots available to userspace
161 * @head: The pipe ring head pointer
162 * @tail: The pipe ring tail pointer
163 * @pipe: The pipe info structure
164 */
165static inline unsigned int pipe_space_for_user(unsigned int head, unsigned int tail,
166 struct pipe_inode_info *pipe)
167{
168 unsigned int p_occupancy, p_space;
169
170 p_occupancy = pipe_occupancy(head, tail);
David Howells6718b6f2019-10-16 16:47:32 +0100171 if (p_occupancy >= pipe->max_usage)
David Howells8cefc102019-11-15 13:30:32 +0000172 return 0;
173 p_space = pipe->ring_size - p_occupancy;
David Howells6718b6f2019-10-16 16:47:32 +0100174 if (p_space > pipe->max_usage)
175 p_space = pipe->max_usage;
David Howells8cefc102019-11-15 13:30:32 +0000176 return p_space;
177}
178
179/**
Miklos Szeredi7bf2d1d2016-09-27 10:45:12 +0200180 * pipe_buf_get - get a reference to a pipe_buffer
181 * @pipe: the pipe that the buffer belongs to
182 * @buf: the buffer to get a reference to
Matthew Wilcox15fab632019-04-05 14:02:10 -0700183 *
184 * Return: %true if the reference was successfully obtained.
Miklos Szeredi7bf2d1d2016-09-27 10:45:12 +0200185 */
Matthew Wilcox15fab632019-04-05 14:02:10 -0700186static inline __must_check bool pipe_buf_get(struct pipe_inode_info *pipe,
Miklos Szeredi7bf2d1d2016-09-27 10:45:12 +0200187 struct pipe_buffer *buf)
188{
Matthew Wilcox15fab632019-04-05 14:02:10 -0700189 return buf->ops->get(pipe, buf);
Miklos Szeredi7bf2d1d2016-09-27 10:45:12 +0200190}
191
Miklos Szeredia7796382016-09-27 10:45:12 +0200192/**
193 * pipe_buf_release - put a reference to a pipe_buffer
194 * @pipe: the pipe that the buffer belongs to
195 * @buf: the buffer to put a reference to
196 */
197static inline void pipe_buf_release(struct pipe_inode_info *pipe,
198 struct pipe_buffer *buf)
199{
200 const struct pipe_buf_operations *ops = buf->ops;
201
202 buf->ops = NULL;
203 ops->release(pipe, buf);
204}
205
Miklos Szeredifba597d2016-09-27 10:45:12 +0200206/**
207 * pipe_buf_confirm - verify contents of the pipe buffer
208 * @pipe: the pipe that the buffer belongs to
209 * @buf: the buffer to confirm
210 */
211static inline int pipe_buf_confirm(struct pipe_inode_info *pipe,
212 struct pipe_buffer *buf)
213{
Christoph Hellwigb8d9e7f2020-05-20 17:58:15 +0200214 if (!buf->ops->confirm)
215 return 0;
Miklos Szeredifba597d2016-09-27 10:45:12 +0200216 return buf->ops->confirm(pipe, buf);
217}
218
Miklos Szeredica76f5b2016-09-27 10:45:12 +0200219/**
Christoph Hellwigc928f642020-05-20 17:58:16 +0200220 * pipe_buf_try_steal - attempt to take ownership of a pipe_buffer
Miklos Szeredica76f5b2016-09-27 10:45:12 +0200221 * @pipe: the pipe that the buffer belongs to
222 * @buf: the buffer to attempt to steal
223 */
Christoph Hellwigc928f642020-05-20 17:58:16 +0200224static inline bool pipe_buf_try_steal(struct pipe_inode_info *pipe,
225 struct pipe_buffer *buf)
Miklos Szeredica76f5b2016-09-27 10:45:12 +0200226{
Christoph Hellwigc928f642020-05-20 17:58:16 +0200227 if (!buf->ops->try_steal)
228 return false;
229 return buf->ops->try_steal(pipe, buf);
Miklos Szeredica76f5b2016-09-27 10:45:12 +0200230}
231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232/* Differs from PIPE_BUF in that PIPE_SIZE is the length of the actual
233 memory allocation, whereas PIPE_BUF makes atomicity guarantees. */
234#define PIPE_SIZE PAGE_SIZE
235
Miklos Szeredi61e0d472009-04-14 19:48:41 +0200236/* Pipe lock and unlock operations */
237void pipe_lock(struct pipe_inode_info *);
238void pipe_unlock(struct pipe_inode_info *);
239void pipe_double_lock(struct pipe_inode_info *, struct pipe_inode_info *);
240
Linus Torvalds472e5b02020-10-01 19:14:36 -0700241/* Wait for a pipe to be readable/writable while dropping the pipe lock */
242void pipe_wait_readable(struct pipe_inode_info *);
243void pipe_wait_writable(struct pipe_inode_info *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Al Viro7bee1302013-03-21 11:04:15 -0400245struct pipe_inode_info *alloc_pipe_info(void);
Al Viro4b8a8f12013-03-21 11:06:46 -0400246void free_pipe_info(struct pipe_inode_info *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Jens Axboef84d7512006-05-01 19:59:03 +0200248/* Generic pipe buffer ops functions */
Matthew Wilcox15fab632019-04-05 14:02:10 -0700249bool generic_pipe_buf_get(struct pipe_inode_info *, struct pipe_buffer *);
Christoph Hellwigc928f642020-05-20 17:58:16 +0200250bool generic_pipe_buf_try_steal(struct pipe_inode_info *, struct pipe_buffer *);
Miklos Szeredi6818173b2009-05-07 15:37:36 +0200251void generic_pipe_buf_release(struct pipe_inode_info *, struct pipe_buffer *);
Jens Axboef84d7512006-05-01 19:59:03 +0200252
Miklos Szeredi28a625c2014-01-22 19:36:57 +0100253extern const struct pipe_buf_operations nosteal_pipe_buf_ops;
254
David Howellsc73be612020-01-14 17:07:11 +0000255#ifdef CONFIG_WATCH_QUEUE
256unsigned long account_pipe_buffers(struct user_struct *user,
257 unsigned long old, unsigned long new);
258bool too_many_pipe_buffers_soft(unsigned long user_bufs);
259bool too_many_pipe_buffers_hard(unsigned long user_bufs);
260bool pipe_is_unprivileged_user(void);
261#endif
262
Jens Axboe35f3d142010-05-20 10:43:18 +0200263/* for F_SETPIPE_SZ and F_GETPIPE_SZ */
David Howellsc73be612020-01-14 17:07:11 +0000264#ifdef CONFIG_WATCH_QUEUE
265int pipe_resize_ring(struct pipe_inode_info *pipe, unsigned int nr_slots);
266#endif
Jens Axboe35f3d142010-05-20 10:43:18 +0200267long pipe_fcntl(struct file *, unsigned int, unsigned long arg);
David Howellsc73be612020-01-14 17:07:11 +0000268struct pipe_inode_info *get_pipe_info(struct file *file, bool for_splice);
Linus Torvaldsc66fb342010-11-28 14:09:57 -0800269
Al Viroe4fad8e2012-07-21 15:33:25 +0400270int create_pipe_files(struct file **, int);
Eric Biggers96e99be402018-02-06 15:42:00 -0800271unsigned int round_pipe_size(unsigned long size);
Al Viroe4fad8e2012-07-21 15:33:25 +0400272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273#endif