Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 #ifndef _LINUX_PIPE_FS_I_H
0003 #define _LINUX_PIPE_FS_I_H
0004 
0005 #define PIPE_DEF_BUFFERS    16
0006 
0007 #define PIPE_BUF_FLAG_LRU   0x01    /* page is on the LRU */
0008 #define PIPE_BUF_FLAG_ATOMIC    0x02    /* was atomically mapped */
0009 #define PIPE_BUF_FLAG_GIFT  0x04    /* page is a gift */
0010 #define PIPE_BUF_FLAG_PACKET    0x08    /* read() as a packet */
0011 #define PIPE_BUF_FLAG_CAN_MERGE 0x10    /* can merge buffers */
0012 #define PIPE_BUF_FLAG_WHOLE 0x20    /* read() must return entire buffer or error */
0013 #ifdef CONFIG_WATCH_QUEUE
0014 #define PIPE_BUF_FLAG_LOSS  0x40    /* Message loss happened after this buffer */
0015 #endif
0016 
0017 /**
0018  *  struct pipe_buffer - a linux kernel pipe buffer
0019  *  @page: the page containing the data for the pipe buffer
0020  *  @offset: offset of data inside the @page
0021  *  @len: length of data inside the @page
0022  *  @ops: operations associated with this buffer. See @pipe_buf_operations.
0023  *  @flags: pipe buffer flags. See above.
0024  *  @private: private data owned by the ops.
0025  **/
0026 struct pipe_buffer {
0027     struct page *page;
0028     unsigned int offset, len;
0029     const struct pipe_buf_operations *ops;
0030     unsigned int flags;
0031     unsigned long private;
0032 };
0033 
0034 /**
0035  *  struct pipe_inode_info - a linux kernel pipe
0036  *  @mutex: mutex protecting the whole thing
0037  *  @rd_wait: reader wait point in case of empty pipe
0038  *  @wr_wait: writer wait point in case of full pipe
0039  *  @head: The point of buffer production
0040  *  @tail: The point of buffer consumption
0041  *  @note_loss: The next read() should insert a data-lost message
0042  *  @max_usage: The maximum number of slots that may be used in the ring
0043  *  @ring_size: total number of buffers (should be a power of 2)
0044  *  @nr_accounted: The amount this pipe accounts for in user->pipe_bufs
0045  *  @tmp_page: cached released page
0046  *  @readers: number of current readers of this pipe
0047  *  @writers: number of current writers of this pipe
0048  *  @files: number of struct file referring this pipe (protected by ->i_lock)
0049  *  @r_counter: reader counter
0050  *  @w_counter: writer counter
0051  *  @poll_usage: is this pipe used for epoll, which has crazy wakeups?
0052  *  @fasync_readers: reader side fasync
0053  *  @fasync_writers: writer side fasync
0054  *  @bufs: the circular array of pipe buffers
0055  *  @user: the user who created this pipe
0056  *  @watch_queue: If this pipe is a watch_queue, this is the stuff for that
0057  **/
0058 struct pipe_inode_info {
0059     struct mutex mutex;
0060     wait_queue_head_t rd_wait, wr_wait;
0061     unsigned int head;
0062     unsigned int tail;
0063     unsigned int max_usage;
0064     unsigned int ring_size;
0065 #ifdef CONFIG_WATCH_QUEUE
0066     bool note_loss;
0067 #endif
0068     unsigned int nr_accounted;
0069     unsigned int readers;
0070     unsigned int writers;
0071     unsigned int files;
0072     unsigned int r_counter;
0073     unsigned int w_counter;
0074     bool poll_usage;
0075     struct page *tmp_page;
0076     struct fasync_struct *fasync_readers;
0077     struct fasync_struct *fasync_writers;
0078     struct pipe_buffer *bufs;
0079     struct user_struct *user;
0080 #ifdef CONFIG_WATCH_QUEUE
0081     struct watch_queue *watch_queue;
0082 #endif
0083 };
0084 
0085 /*
0086  * Note on the nesting of these functions:
0087  *
0088  * ->confirm()
0089  *  ->try_steal()
0090  *
0091  * That is, ->try_steal() must be called on a confirmed buffer.  See below for
0092  * the meaning of each operation.  Also see the kerneldoc in fs/pipe.c for the
0093  * pipe and generic variants of these hooks.
0094  */
0095 struct pipe_buf_operations {
0096     /*
0097      * ->confirm() verifies that the data in the pipe buffer is there
0098      * and that the contents are good. If the pages in the pipe belong
0099      * to a file system, we may need to wait for IO completion in this
0100      * hook. Returns 0 for good, or a negative error value in case of
0101      * error.  If not present all pages are considered good.
0102      */
0103     int (*confirm)(struct pipe_inode_info *, struct pipe_buffer *);
0104 
0105     /*
0106      * When the contents of this pipe buffer has been completely
0107      * consumed by a reader, ->release() is called.
0108      */
0109     void (*release)(struct pipe_inode_info *, struct pipe_buffer *);
0110 
0111     /*
0112      * Attempt to take ownership of the pipe buffer and its contents.
0113      * ->try_steal() returns %true for success, in which case the contents
0114      * of the pipe (the buf->page) is locked and now completely owned by the
0115      * caller. The page may then be transferred to a different mapping, the
0116      * most often used case is insertion into different file address space
0117      * cache.
0118      */
0119     bool (*try_steal)(struct pipe_inode_info *, struct pipe_buffer *);
0120 
0121     /*
0122      * Get a reference to the pipe buffer.
0123      */
0124     bool (*get)(struct pipe_inode_info *, struct pipe_buffer *);
0125 };
0126 
0127 /**
0128  * pipe_empty - Return true if the pipe is empty
0129  * @head: The pipe ring head pointer
0130  * @tail: The pipe ring tail pointer
0131  */
0132 static inline bool pipe_empty(unsigned int head, unsigned int tail)
0133 {
0134     return head == tail;
0135 }
0136 
0137 /**
0138  * pipe_occupancy - Return number of slots used in the pipe
0139  * @head: The pipe ring head pointer
0140  * @tail: The pipe ring tail pointer
0141  */
0142 static inline unsigned int pipe_occupancy(unsigned int head, unsigned int tail)
0143 {
0144     return head - tail;
0145 }
0146 
0147 /**
0148  * pipe_full - Return true if the pipe is full
0149  * @head: The pipe ring head pointer
0150  * @tail: The pipe ring tail pointer
0151  * @limit: The maximum amount of slots available.
0152  */
0153 static inline bool pipe_full(unsigned int head, unsigned int tail,
0154                  unsigned int limit)
0155 {
0156     return pipe_occupancy(head, tail) >= limit;
0157 }
0158 
0159 /**
0160  * pipe_buf_get - get a reference to a pipe_buffer
0161  * @pipe:   the pipe that the buffer belongs to
0162  * @buf:    the buffer to get a reference to
0163  *
0164  * Return: %true if the reference was successfully obtained.
0165  */
0166 static inline __must_check bool pipe_buf_get(struct pipe_inode_info *pipe,
0167                 struct pipe_buffer *buf)
0168 {
0169     return buf->ops->get(pipe, buf);
0170 }
0171 
0172 /**
0173  * pipe_buf_release - put a reference to a pipe_buffer
0174  * @pipe:   the pipe that the buffer belongs to
0175  * @buf:    the buffer to put a reference to
0176  */
0177 static inline void pipe_buf_release(struct pipe_inode_info *pipe,
0178                     struct pipe_buffer *buf)
0179 {
0180     const struct pipe_buf_operations *ops = buf->ops;
0181 
0182     buf->ops = NULL;
0183     ops->release(pipe, buf);
0184 }
0185 
0186 /**
0187  * pipe_buf_confirm - verify contents of the pipe buffer
0188  * @pipe:   the pipe that the buffer belongs to
0189  * @buf:    the buffer to confirm
0190  */
0191 static inline int pipe_buf_confirm(struct pipe_inode_info *pipe,
0192                    struct pipe_buffer *buf)
0193 {
0194     if (!buf->ops->confirm)
0195         return 0;
0196     return buf->ops->confirm(pipe, buf);
0197 }
0198 
0199 /**
0200  * pipe_buf_try_steal - attempt to take ownership of a pipe_buffer
0201  * @pipe:   the pipe that the buffer belongs to
0202  * @buf:    the buffer to attempt to steal
0203  */
0204 static inline bool pipe_buf_try_steal(struct pipe_inode_info *pipe,
0205         struct pipe_buffer *buf)
0206 {
0207     if (!buf->ops->try_steal)
0208         return false;
0209     return buf->ops->try_steal(pipe, buf);
0210 }
0211 
0212 static inline void pipe_discard_from(struct pipe_inode_info *pipe,
0213         unsigned int old_head)
0214 {
0215     unsigned int mask = pipe->ring_size - 1;
0216 
0217     while (pipe->head > old_head)
0218         pipe_buf_release(pipe, &pipe->bufs[--pipe->head & mask]);
0219 }
0220 
0221 /* Differs from PIPE_BUF in that PIPE_SIZE is the length of the actual
0222    memory allocation, whereas PIPE_BUF makes atomicity guarantees.  */
0223 #define PIPE_SIZE       PAGE_SIZE
0224 
0225 /* Pipe lock and unlock operations */
0226 void pipe_lock(struct pipe_inode_info *);
0227 void pipe_unlock(struct pipe_inode_info *);
0228 void pipe_double_lock(struct pipe_inode_info *, struct pipe_inode_info *);
0229 
0230 /* Wait for a pipe to be readable/writable while dropping the pipe lock */
0231 void pipe_wait_readable(struct pipe_inode_info *);
0232 void pipe_wait_writable(struct pipe_inode_info *);
0233 
0234 struct pipe_inode_info *alloc_pipe_info(void);
0235 void free_pipe_info(struct pipe_inode_info *);
0236 
0237 /* Generic pipe buffer ops functions */
0238 bool generic_pipe_buf_get(struct pipe_inode_info *, struct pipe_buffer *);
0239 bool generic_pipe_buf_try_steal(struct pipe_inode_info *, struct pipe_buffer *);
0240 void generic_pipe_buf_release(struct pipe_inode_info *, struct pipe_buffer *);
0241 
0242 extern const struct pipe_buf_operations nosteal_pipe_buf_ops;
0243 
0244 #ifdef CONFIG_WATCH_QUEUE
0245 unsigned long account_pipe_buffers(struct user_struct *user,
0246                    unsigned long old, unsigned long new);
0247 bool too_many_pipe_buffers_soft(unsigned long user_bufs);
0248 bool too_many_pipe_buffers_hard(unsigned long user_bufs);
0249 bool pipe_is_unprivileged_user(void);
0250 #endif
0251 
0252 /* for F_SETPIPE_SZ and F_GETPIPE_SZ */
0253 #ifdef CONFIG_WATCH_QUEUE
0254 int pipe_resize_ring(struct pipe_inode_info *pipe, unsigned int nr_slots);
0255 #endif
0256 long pipe_fcntl(struct file *, unsigned int, unsigned long arg);
0257 struct pipe_inode_info *get_pipe_info(struct file *file, bool for_splice);
0258 
0259 int create_pipe_files(struct file **, int);
0260 unsigned int round_pipe_size(unsigned long size);
0261 
0262 #endif