pipe_fs_i.h 8.2 KB

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