pipe_fs_i.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. * @fasync_readers: reader side fasync
  48. * @fasync_writers: writer side fasync
  49. * @bufs: the circular array of pipe buffers
  50. * @user: the user who created this pipe
  51. * @watch_queue: If this pipe is a watch_queue, this is the stuff for that
  52. **/
  53. struct pipe_inode_info {
  54. struct mutex mutex;
  55. wait_queue_head_t rd_wait, wr_wait;
  56. unsigned int head;
  57. unsigned int tail;
  58. unsigned int max_usage;
  59. unsigned int ring_size;
  60. #ifdef CONFIG_WATCH_QUEUE
  61. bool note_loss;
  62. #endif
  63. unsigned int nr_accounted;
  64. unsigned int readers;
  65. unsigned int writers;
  66. unsigned int files;
  67. unsigned int r_counter;
  68. unsigned int w_counter;
  69. struct page *tmp_page;
  70. struct fasync_struct *fasync_readers;
  71. struct fasync_struct *fasync_writers;
  72. struct pipe_buffer *bufs;
  73. struct user_struct *user;
  74. #ifdef CONFIG_WATCH_QUEUE
  75. struct watch_queue *watch_queue;
  76. #endif
  77. };
  78. /*
  79. * Note on the nesting of these functions:
  80. *
  81. * ->confirm()
  82. * ->try_steal()
  83. *
  84. * That is, ->try_steal() must be called on a confirmed buffer. See below for
  85. * the meaning of each operation. Also see the kerneldoc in fs/pipe.c for the
  86. * pipe and generic variants of these hooks.
  87. */
  88. struct pipe_buf_operations {
  89. /*
  90. * ->confirm() verifies that the data in the pipe buffer is there
  91. * and that the contents are good. If the pages in the pipe belong
  92. * to a file system, we may need to wait for IO completion in this
  93. * hook. Returns 0 for good, or a negative error value in case of
  94. * error. If not present all pages are considered good.
  95. */
  96. int (*confirm)(struct pipe_inode_info *, struct pipe_buffer *);
  97. /*
  98. * When the contents of this pipe buffer has been completely
  99. * consumed by a reader, ->release() is called.
  100. */
  101. void (*release)(struct pipe_inode_info *, struct pipe_buffer *);
  102. /*
  103. * Attempt to take ownership of the pipe buffer and its contents.
  104. * ->try_steal() returns %true for success, in which case the contents
  105. * of the pipe (the buf->page) is locked and now completely owned by the
  106. * caller. The page may then be transferred to a different mapping, the
  107. * most often used case is insertion into different file address space
  108. * cache.
  109. */
  110. bool (*try_steal)(struct pipe_inode_info *, struct pipe_buffer *);
  111. /*
  112. * Get a reference to the pipe buffer.
  113. */
  114. bool (*get)(struct pipe_inode_info *, struct pipe_buffer *);
  115. };
  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. */
  121. static inline bool pipe_empty(unsigned int head, unsigned int tail)
  122. {
  123. return head == tail;
  124. }
  125. /**
  126. * pipe_occupancy - Return number of slots used in the pipe
  127. * @head: The pipe ring head pointer
  128. * @tail: The pipe ring tail pointer
  129. */
  130. static inline unsigned int pipe_occupancy(unsigned int head, unsigned int tail)
  131. {
  132. return head - tail;
  133. }
  134. /**
  135. * pipe_full - Return true if the pipe is full
  136. * @head: The pipe ring head pointer
  137. * @tail: The pipe ring tail pointer
  138. * @limit: The maximum amount of slots available.
  139. */
  140. static inline bool pipe_full(unsigned int head, unsigned int tail,
  141. unsigned int limit)
  142. {
  143. return pipe_occupancy(head, tail) >= limit;
  144. }
  145. /**
  146. * pipe_space_for_user - Return number of slots available to userspace
  147. * @head: The pipe ring head pointer
  148. * @tail: The pipe ring tail pointer
  149. * @pipe: The pipe info structure
  150. */
  151. static inline unsigned int pipe_space_for_user(unsigned int head, unsigned int tail,
  152. struct pipe_inode_info *pipe)
  153. {
  154. unsigned int p_occupancy, p_space;
  155. p_occupancy = pipe_occupancy(head, tail);
  156. if (p_occupancy >= pipe->max_usage)
  157. return 0;
  158. p_space = pipe->ring_size - p_occupancy;
  159. if (p_space > pipe->max_usage)
  160. p_space = pipe->max_usage;
  161. return p_space;
  162. }
  163. /**
  164. * pipe_buf_get - get a reference to a pipe_buffer
  165. * @pipe: the pipe that the buffer belongs to
  166. * @buf: the buffer to get a reference to
  167. *
  168. * Return: %true if the reference was successfully obtained.
  169. */
  170. static inline __must_check bool pipe_buf_get(struct pipe_inode_info *pipe,
  171. struct pipe_buffer *buf)
  172. {
  173. return buf->ops->get(pipe, buf);
  174. }
  175. /**
  176. * pipe_buf_release - put a reference to a pipe_buffer
  177. * @pipe: the pipe that the buffer belongs to
  178. * @buf: the buffer to put a reference to
  179. */
  180. static inline void pipe_buf_release(struct pipe_inode_info *pipe,
  181. struct pipe_buffer *buf)
  182. {
  183. const struct pipe_buf_operations *ops = buf->ops;
  184. buf->ops = NULL;
  185. ops->release(pipe, buf);
  186. }
  187. /**
  188. * pipe_buf_confirm - verify contents of the pipe buffer
  189. * @pipe: the pipe that the buffer belongs to
  190. * @buf: the buffer to confirm
  191. */
  192. static inline int pipe_buf_confirm(struct pipe_inode_info *pipe,
  193. struct pipe_buffer *buf)
  194. {
  195. if (!buf->ops->confirm)
  196. return 0;
  197. return buf->ops->confirm(pipe, buf);
  198. }
  199. /**
  200. * pipe_buf_try_steal - attempt to take ownership of a pipe_buffer
  201. * @pipe: the pipe that the buffer belongs to
  202. * @buf: the buffer to attempt to steal
  203. */
  204. static inline bool pipe_buf_try_steal(struct pipe_inode_info *pipe,
  205. struct pipe_buffer *buf)
  206. {
  207. if (!buf->ops->try_steal)
  208. return false;
  209. return buf->ops->try_steal(pipe, buf);
  210. }
  211. /* Differs from PIPE_BUF in that PIPE_SIZE is the length of the actual
  212. memory allocation, whereas PIPE_BUF makes atomicity guarantees. */
  213. #define PIPE_SIZE PAGE_SIZE
  214. /* Pipe lock and unlock operations */
  215. void pipe_lock(struct pipe_inode_info *);
  216. void pipe_unlock(struct pipe_inode_info *);
  217. void pipe_double_lock(struct pipe_inode_info *, struct pipe_inode_info *);
  218. extern unsigned int pipe_max_size;
  219. extern unsigned long pipe_user_pages_hard;
  220. extern unsigned long pipe_user_pages_soft;
  221. /* Wait for a pipe to be readable/writable while dropping the pipe lock */
  222. void pipe_wait_readable(struct pipe_inode_info *);
  223. void pipe_wait_writable(struct pipe_inode_info *);
  224. struct pipe_inode_info *alloc_pipe_info(void);
  225. void free_pipe_info(struct pipe_inode_info *);
  226. /* Generic pipe buffer ops functions */
  227. bool generic_pipe_buf_get(struct pipe_inode_info *, struct pipe_buffer *);
  228. bool generic_pipe_buf_try_steal(struct pipe_inode_info *, struct pipe_buffer *);
  229. void generic_pipe_buf_release(struct pipe_inode_info *, struct pipe_buffer *);
  230. extern const struct pipe_buf_operations nosteal_pipe_buf_ops;
  231. #ifdef CONFIG_WATCH_QUEUE
  232. unsigned long account_pipe_buffers(struct user_struct *user,
  233. unsigned long old, unsigned long new);
  234. bool too_many_pipe_buffers_soft(unsigned long user_bufs);
  235. bool too_many_pipe_buffers_hard(unsigned long user_bufs);
  236. bool pipe_is_unprivileged_user(void);
  237. #endif
  238. /* for F_SETPIPE_SZ and F_GETPIPE_SZ */
  239. #ifdef CONFIG_WATCH_QUEUE
  240. int pipe_resize_ring(struct pipe_inode_info *pipe, unsigned int nr_slots);
  241. #endif
  242. long pipe_fcntl(struct file *, unsigned int, unsigned long arg);
  243. struct pipe_inode_info *get_pipe_info(struct file *file, bool for_splice);
  244. int create_pipe_files(struct file **, int);
  245. unsigned int round_pipe_size(unsigned long size);
  246. #endif