relay.h 8.4 KB

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