relay.h 8.7 KB

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