relay.h 8.4 KB

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