relay.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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/wait.h>
  14. #include <linux/list.h>
  15. #include <linux/fs.h>
  16. #include <linux/poll.h>
  17. #include <linux/kref.h>
  18. /* Needs a _much_ better name... */
  19. #define FIX_SIZE(x) ((((x) - 1) & PAGE_MASK) + PAGE_SIZE)
  20. /*
  21. * Tracks changes to rchan/rchan_buf structs
  22. */
  23. #define RELAYFS_CHANNEL_VERSION 6
  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 work_struct wake_readers; /* reader wake-up work struct */
  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. unsigned int cpu; /* this buf's cpu */
  46. } ____cacheline_aligned;
  47. /*
  48. * Relay channel data structure
  49. */
  50. struct rchan
  51. {
  52. u32 version; /* the version of this struct */
  53. size_t subbuf_size; /* sub-buffer size */
  54. size_t n_subbufs; /* number of sub-buffers per buffer */
  55. size_t alloc_size; /* total buffer size allocated */
  56. struct rchan_callbacks *cb; /* client callbacks */
  57. struct kref kref; /* channel refcount */
  58. void *private_data; /* for user-defined data */
  59. size_t last_toobig; /* tried to log event > subbuf size */
  60. struct rchan_buf *buf[NR_CPUS]; /* per-cpu channel buffers */
  61. };
  62. /*
  63. * Relay channel client callbacks
  64. */
  65. struct rchan_callbacks
  66. {
  67. /*
  68. * subbuf_start - called on buffer-switch to a new sub-buffer
  69. * @buf: the channel buffer containing the new sub-buffer
  70. * @subbuf: the start of the new sub-buffer
  71. * @prev_subbuf: the start of the previous sub-buffer
  72. * @prev_padding: unused space at the end of previous sub-buffer
  73. *
  74. * The client should return 1 to continue logging, 0 to stop
  75. * logging.
  76. *
  77. * NOTE: subbuf_start will also be invoked when the buffer is
  78. * created, so that the first sub-buffer can be initialized
  79. * if necessary. In this case, prev_subbuf will be NULL.
  80. *
  81. * NOTE: the client can reserve bytes at the beginning of the new
  82. * sub-buffer by calling subbuf_start_reserve() in this callback.
  83. */
  84. int (*subbuf_start) (struct rchan_buf *buf,
  85. void *subbuf,
  86. void *prev_subbuf,
  87. size_t prev_padding);
  88. /*
  89. * buf_mapped - relay buffer mmap notification
  90. * @buf: the channel buffer
  91. * @filp: relay file pointer
  92. *
  93. * Called when a relay file is successfully mmapped
  94. */
  95. void (*buf_mapped)(struct rchan_buf *buf,
  96. struct file *filp);
  97. /*
  98. * buf_unmapped - relay buffer unmap notification
  99. * @buf: the channel buffer
  100. * @filp: relay file pointer
  101. *
  102. * Called when a relay file is successfully unmapped
  103. */
  104. void (*buf_unmapped)(struct rchan_buf *buf,
  105. struct file *filp);
  106. /*
  107. * create_buf_file - create file to represent a relay channel buffer
  108. * @filename: the name of the file to create
  109. * @parent: the parent of the file to create
  110. * @mode: the mode of the file to create
  111. * @buf: the channel buffer
  112. * @is_global: outparam - set non-zero if the buffer should be global
  113. *
  114. * Called during relay_open(), once for each per-cpu buffer,
  115. * to allow the client to create a file to be used to
  116. * represent the corresponding channel buffer. If the file is
  117. * created outside of relay, the parent must also exist in
  118. * that filesystem.
  119. *
  120. * The callback should return the dentry of the file created
  121. * to represent the relay buffer.
  122. *
  123. * Setting the is_global outparam to a non-zero value will
  124. * cause relay_open() to create a single global buffer rather
  125. * than the default set of per-cpu buffers.
  126. *
  127. * See Documentation/filesystems/relayfs.txt for more info.
  128. */
  129. struct dentry *(*create_buf_file)(const char *filename,
  130. struct dentry *parent,
  131. int mode,
  132. struct rchan_buf *buf,
  133. int *is_global);
  134. /*
  135. * remove_buf_file - remove file representing a relay channel buffer
  136. * @dentry: the dentry of the file to remove
  137. *
  138. * Called during relay_close(), once for each per-cpu buffer,
  139. * to allow the client to remove a file used to represent a
  140. * channel buffer.
  141. *
  142. * The callback should return 0 if successful, negative if not.
  143. */
  144. int (*remove_buf_file)(struct dentry *dentry);
  145. };
  146. /*
  147. * CONFIG_RELAY kernel API, kernel/relay.c
  148. */
  149. struct rchan *relay_open(const char *base_filename,
  150. struct dentry *parent,
  151. size_t subbuf_size,
  152. size_t n_subbufs,
  153. struct rchan_callbacks *cb);
  154. extern void relay_close(struct rchan *chan);
  155. extern void relay_flush(struct rchan *chan);
  156. extern void relay_subbufs_consumed(struct rchan *chan,
  157. unsigned int cpu,
  158. size_t consumed);
  159. extern void relay_reset(struct rchan *chan);
  160. extern int relay_buf_full(struct rchan_buf *buf);
  161. extern size_t relay_switch_subbuf(struct rchan_buf *buf,
  162. size_t length);
  163. /**
  164. * relay_write - write data into the channel
  165. * @chan: relay channel
  166. * @data: data to be written
  167. * @length: number of bytes to write
  168. *
  169. * Writes data into the current cpu's channel buffer.
  170. *
  171. * Protects the buffer by disabling interrupts. Use this
  172. * if you might be logging from interrupt context. Try
  173. * __relay_write() if you know you won't be logging from
  174. * interrupt context.
  175. */
  176. static inline void relay_write(struct rchan *chan,
  177. const void *data,
  178. size_t length)
  179. {
  180. unsigned long flags;
  181. struct rchan_buf *buf;
  182. local_irq_save(flags);
  183. buf = chan->buf[smp_processor_id()];
  184. if (unlikely(buf->offset + length > chan->subbuf_size))
  185. length = relay_switch_subbuf(buf, length);
  186. memcpy(buf->data + buf->offset, data, length);
  187. buf->offset += length;
  188. local_irq_restore(flags);
  189. }
  190. /**
  191. * __relay_write - write data into the channel
  192. * @chan: relay channel
  193. * @data: data to be written
  194. * @length: number of bytes to write
  195. *
  196. * Writes data into the current cpu's channel buffer.
  197. *
  198. * Protects the buffer by disabling preemption. Use
  199. * relay_write() if you might be logging from interrupt
  200. * context.
  201. */
  202. static inline void __relay_write(struct rchan *chan,
  203. const void *data,
  204. size_t length)
  205. {
  206. struct rchan_buf *buf;
  207. buf = chan->buf[get_cpu()];
  208. if (unlikely(buf->offset + length > buf->chan->subbuf_size))
  209. length = relay_switch_subbuf(buf, length);
  210. memcpy(buf->data + buf->offset, data, length);
  211. buf->offset += length;
  212. put_cpu();
  213. }
  214. /**
  215. * relay_reserve - reserve slot in channel buffer
  216. * @chan: relay channel
  217. * @length: number of bytes to reserve
  218. *
  219. * Returns pointer to reserved slot, NULL if full.
  220. *
  221. * Reserves a slot in the current cpu's channel buffer.
  222. * Does not protect the buffer at all - caller must provide
  223. * appropriate synchronization.
  224. */
  225. static inline void *relay_reserve(struct rchan *chan, size_t length)
  226. {
  227. void *reserved;
  228. struct rchan_buf *buf = chan->buf[smp_processor_id()];
  229. if (unlikely(buf->offset + length > buf->chan->subbuf_size)) {
  230. length = relay_switch_subbuf(buf, length);
  231. if (!length)
  232. return NULL;
  233. }
  234. reserved = buf->data + buf->offset;
  235. buf->offset += length;
  236. return reserved;
  237. }
  238. /**
  239. * subbuf_start_reserve - reserve bytes at the start of a sub-buffer
  240. * @buf: relay channel buffer
  241. * @length: number of bytes to reserve
  242. *
  243. * Helper function used to reserve bytes at the beginning of
  244. * a sub-buffer in the subbuf_start() callback.
  245. */
  246. static inline void subbuf_start_reserve(struct rchan_buf *buf,
  247. size_t length)
  248. {
  249. BUG_ON(length >= buf->chan->subbuf_size - 1);
  250. buf->offset = length;
  251. }
  252. /*
  253. * exported relay file operations, kernel/relay.c
  254. */
  255. extern struct file_operations relay_file_operations;
  256. #endif /* _LINUX_RELAY_H */