relay.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /*
  2. * linux/include/linux/relay.h
  3. *
  4. * Copyright (C) 2002, 2003 - Tom Zanussi (zanussi@us.ibm.com), IBM Corp
  5. * Copyright (C) 1999, 2000, 2001, 2002 - Karim Yaghmour (karim@opersys.com)
  6. *
  7. * CONFIG_RELAY definitions and declarations
  8. */
  9. #ifndef _LINUX_RELAY_H
  10. #define _LINUX_RELAY_H
  11. #include <linux/types.h>
  12. #include <linux/sched.h>
  13. #include <linux/timer.h>
  14. #include <linux/wait.h>
  15. #include <linux/list.h>
  16. #include <linux/bug.h>
  17. #include <linux/fs.h>
  18. #include <linux/poll.h>
  19. #include <linux/kref.h>
  20. /*
  21. * Tracks changes to rchan/rchan_buf structs
  22. */
  23. #define RELAYFS_CHANNEL_VERSION 7
  24. /*
  25. * Per-cpu relay channel buffer
  26. */
  27. struct rchan_buf
  28. {
  29. void *start; /* start of channel buffer */
  30. void *data; /* start of current sub-buffer */
  31. size_t offset; /* current offset into sub-buffer */
  32. size_t subbufs_produced; /* count of sub-buffers produced */
  33. size_t subbufs_consumed; /* count of sub-buffers consumed */
  34. struct rchan *chan; /* associated channel */
  35. wait_queue_head_t read_wait; /* reader wait queue */
  36. struct timer_list timer; /* reader wake-up timer */
  37. struct dentry *dentry; /* channel file dentry */
  38. struct kref kref; /* channel buffer refcount */
  39. struct page **page_array; /* array of current buffer pages */
  40. unsigned int page_count; /* number of current buffer pages */
  41. unsigned int finalized; /* buffer has been finalized */
  42. size_t *padding; /* padding counts per sub-buffer */
  43. size_t prev_padding; /* temporary variable */
  44. size_t bytes_consumed; /* bytes consumed in cur read subbuf */
  45. size_t early_bytes; /* bytes consumed before VFS inited */
  46. unsigned int cpu; /* this buf's cpu */
  47. } ____cacheline_aligned;
  48. /*
  49. * Relay channel data structure
  50. */
  51. struct rchan
  52. {
  53. u32 version; /* the version of this struct */
  54. size_t subbuf_size; /* sub-buffer size */
  55. size_t n_subbufs; /* number of sub-buffers per buffer */
  56. size_t alloc_size; /* total buffer size allocated */
  57. struct rchan_callbacks *cb; /* client callbacks */
  58. struct kref kref; /* channel refcount */
  59. void *private_data; /* for user-defined data */
  60. size_t last_toobig; /* tried to log event > subbuf size */
  61. struct rchan_buf *buf[NR_CPUS]; /* per-cpu channel buffers */
  62. int is_global; /* One global buffer ? */
  63. struct list_head list; /* for channel list */
  64. struct dentry *parent; /* parent dentry passed to open */
  65. int has_base_filename; /* has a filename associated? */
  66. char base_filename[NAME_MAX]; /* saved base filename */
  67. };
  68. /*
  69. * Relay channel client callbacks
  70. */
  71. struct rchan_callbacks
  72. {
  73. /*
  74. * subbuf_start - called on buffer-switch to a new sub-buffer
  75. * @buf: the channel buffer containing the new sub-buffer
  76. * @subbuf: the start of the new sub-buffer
  77. * @prev_subbuf: the start of the previous sub-buffer
  78. * @prev_padding: unused space at the end of previous sub-buffer
  79. *
  80. * The client should return 1 to continue logging, 0 to stop
  81. * logging.
  82. *
  83. * NOTE: subbuf_start will also be invoked when the buffer is
  84. * created, so that the first sub-buffer can be initialized
  85. * if necessary. In this case, prev_subbuf will be NULL.
  86. *
  87. * NOTE: the client can reserve bytes at the beginning of the new
  88. * sub-buffer by calling subbuf_start_reserve() in this callback.
  89. */
  90. int (*subbuf_start) (struct rchan_buf *buf,
  91. void *subbuf,
  92. void *prev_subbuf,
  93. size_t prev_padding);
  94. /*
  95. * buf_mapped - relay buffer mmap notification
  96. * @buf: the channel buffer
  97. * @filp: relay file pointer
  98. *
  99. * Called when a relay file is successfully mmapped
  100. */
  101. void (*buf_mapped)(struct rchan_buf *buf,
  102. struct file *filp);
  103. /*
  104. * buf_unmapped - relay buffer unmap notification
  105. * @buf: the channel buffer
  106. * @filp: relay file pointer
  107. *
  108. * Called when a relay file is successfully unmapped
  109. */
  110. void (*buf_unmapped)(struct rchan_buf *buf,
  111. struct file *filp);
  112. /*
  113. * create_buf_file - create file to represent a relay channel buffer
  114. * @filename: the name of the file to create
  115. * @parent: the parent of the file to create
  116. * @mode: the mode of the file to create
  117. * @buf: the channel buffer
  118. * @is_global: outparam - set non-zero if the buffer should be global
  119. *
  120. * Called during relay_open(), once for each per-cpu buffer,
  121. * to allow the client to create a file to be used to
  122. * represent the corresponding channel buffer. If the file is
  123. * created outside of relay, the parent must also exist in
  124. * that filesystem.
  125. *
  126. * The callback should return the dentry of the file created
  127. * to represent the relay buffer.
  128. *
  129. * Setting the is_global outparam to a non-zero value will
  130. * cause relay_open() to create a single global buffer rather
  131. * than the default set of per-cpu buffers.
  132. *
  133. * See Documentation/filesystems/relay.txt for more info.
  134. */
  135. struct dentry *(*create_buf_file)(const char *filename,
  136. struct dentry *parent,
  137. umode_t mode,
  138. struct rchan_buf *buf,
  139. int *is_global);
  140. /*
  141. * remove_buf_file - remove file representing a relay channel buffer
  142. * @dentry: the dentry of the file to remove
  143. *
  144. * Called during relay_close(), once for each per-cpu buffer,
  145. * to allow the client to remove a file used to represent a
  146. * channel buffer.
  147. *
  148. * The callback should return 0 if successful, negative if not.
  149. */
  150. int (*remove_buf_file)(struct dentry *dentry);
  151. };
  152. /*
  153. * CONFIG_RELAY kernel API, kernel/relay.c
  154. */
  155. struct rchan *relay_open(const char *base_filename,
  156. struct dentry *parent,
  157. size_t subbuf_size,
  158. size_t n_subbufs,
  159. struct rchan_callbacks *cb,
  160. void *private_data);
  161. extern int relay_late_setup_files(struct rchan *chan,
  162. const char *base_filename,
  163. struct dentry *parent);
  164. extern void relay_close(struct rchan *chan);
  165. extern void relay_flush(struct rchan *chan);
  166. extern void relay_subbufs_consumed(struct rchan *chan,
  167. unsigned int cpu,
  168. size_t consumed);
  169. extern void relay_reset(struct rchan *chan);
  170. extern int relay_buf_full(struct rchan_buf *buf);
  171. extern size_t relay_switch_subbuf(struct rchan_buf *buf,
  172. size_t length);
  173. /**
  174. * relay_write - write data into the channel
  175. * @chan: relay channel
  176. * @data: data to be written
  177. * @length: number of bytes to write
  178. *
  179. * Writes data into the current cpu's channel buffer.
  180. *
  181. * Protects the buffer by disabling interrupts. Use this
  182. * if you might be logging from interrupt context. Try
  183. * __relay_write() if you know you won't be logging from
  184. * interrupt context.
  185. */
  186. static inline void relay_write(struct rchan *chan,
  187. const void *data,
  188. size_t length)
  189. {
  190. unsigned long flags;
  191. struct rchan_buf *buf;
  192. local_irq_save(flags);
  193. buf = chan->buf[smp_processor_id()];
  194. if (unlikely(buf->offset + length > chan->subbuf_size))
  195. length = relay_switch_subbuf(buf, length);
  196. memcpy(buf->data + buf->offset, data, length);
  197. buf->offset += length;
  198. local_irq_restore(flags);
  199. }
  200. /**
  201. * __relay_write - write data into the channel
  202. * @chan: relay channel
  203. * @data: data to be written
  204. * @length: number of bytes to write
  205. *
  206. * Writes data into the current cpu's channel buffer.
  207. *
  208. * Protects the buffer by disabling preemption. Use
  209. * relay_write() if you might be logging from interrupt
  210. * context.
  211. */
  212. static inline void __relay_write(struct rchan *chan,
  213. const void *data,
  214. size_t length)
  215. {
  216. struct rchan_buf *buf;
  217. buf = chan->buf[get_cpu()];
  218. if (unlikely(buf->offset + length > buf->chan->subbuf_size))
  219. length = relay_switch_subbuf(buf, length);
  220. memcpy(buf->data + buf->offset, data, length);
  221. buf->offset += length;
  222. put_cpu();
  223. }
  224. /**
  225. * relay_reserve - reserve slot in channel buffer
  226. * @chan: relay channel
  227. * @length: number of bytes to reserve
  228. *
  229. * Returns pointer to reserved slot, NULL if full.
  230. *
  231. * Reserves a slot in the current cpu's channel buffer.
  232. * Does not protect the buffer at all - caller must provide
  233. * appropriate synchronization.
  234. */
  235. static inline void *relay_reserve(struct rchan *chan, size_t length)
  236. {
  237. void *reserved;
  238. struct rchan_buf *buf = chan->buf[smp_processor_id()];
  239. if (unlikely(buf->offset + length > buf->chan->subbuf_size)) {
  240. length = relay_switch_subbuf(buf, length);
  241. if (!length)
  242. return NULL;
  243. }
  244. reserved = buf->data + buf->offset;
  245. buf->offset += length;
  246. return reserved;
  247. }
  248. /**
  249. * subbuf_start_reserve - reserve bytes at the start of a sub-buffer
  250. * @buf: relay channel buffer
  251. * @length: number of bytes to reserve
  252. *
  253. * Helper function used to reserve bytes at the beginning of
  254. * a sub-buffer in the subbuf_start() callback.
  255. */
  256. static inline void subbuf_start_reserve(struct rchan_buf *buf,
  257. size_t length)
  258. {
  259. BUG_ON(length >= buf->chan->subbuf_size - 1);
  260. buf->offset = length;
  261. }
  262. /*
  263. * exported relay file operations, kernel/relay.c
  264. */
  265. extern const struct file_operations relay_file_operations;
  266. #endif /* _LINUX_RELAY_H */