relay.h 8.7 KB

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