relayfs_fs.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. * linux/include/linux/relayfs_fs.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. * RelayFS definitions and declarations
  8. */
  9. #ifndef _LINUX_RELAYFS_FS_H
  10. #define _LINUX_RELAYFS_FS_H
  11. #include <linux/config.h>
  12. #include <linux/types.h>
  13. #include <linux/sched.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. /*
  20. * Tracks changes to rchan/rchan_buf structs
  21. */
  22. #define RELAYFS_CHANNEL_VERSION 6
  23. /*
  24. * Per-cpu relay channel buffer
  25. */
  26. struct rchan_buf
  27. {
  28. void *start; /* start of channel buffer */
  29. void *data; /* start of current sub-buffer */
  30. size_t offset; /* current offset into sub-buffer */
  31. size_t subbufs_produced; /* count of sub-buffers produced */
  32. size_t subbufs_consumed; /* count of sub-buffers consumed */
  33. struct rchan *chan; /* associated channel */
  34. wait_queue_head_t read_wait; /* reader wait queue */
  35. struct work_struct wake_readers; /* reader wake-up work struct */
  36. struct dentry *dentry; /* channel file dentry */
  37. struct kref kref; /* channel buffer refcount */
  38. struct page **page_array; /* array of current buffer pages */
  39. unsigned int page_count; /* number of current buffer pages */
  40. unsigned int finalized; /* buffer has been finalized */
  41. size_t *padding; /* padding counts per sub-buffer */
  42. size_t prev_padding; /* temporary variable */
  43. size_t bytes_consumed; /* bytes consumed in cur read subbuf */
  44. unsigned int cpu; /* this buf's cpu */
  45. } ____cacheline_aligned;
  46. /*
  47. * Relay channel data structure
  48. */
  49. struct rchan
  50. {
  51. u32 version; /* the version of this struct */
  52. size_t subbuf_size; /* sub-buffer size */
  53. size_t n_subbufs; /* number of sub-buffers per buffer */
  54. size_t alloc_size; /* total buffer size allocated */
  55. struct rchan_callbacks *cb; /* client callbacks */
  56. struct kref kref; /* channel refcount */
  57. void *private_data; /* for user-defined data */
  58. size_t last_toobig; /* tried to log event > subbuf size */
  59. struct rchan_buf *buf[NR_CPUS]; /* per-cpu channel buffers */
  60. };
  61. /*
  62. * Relayfs inode
  63. */
  64. struct relayfs_inode_info
  65. {
  66. struct inode vfs_inode;
  67. struct rchan_buf *buf;
  68. };
  69. static inline struct relayfs_inode_info *RELAYFS_I(struct inode *inode)
  70. {
  71. return container_of(inode, struct relayfs_inode_info, vfs_inode);
  72. }
  73. /*
  74. * Relay channel client callbacks
  75. */
  76. struct rchan_callbacks
  77. {
  78. /*
  79. * subbuf_start - called on buffer-switch to a new sub-buffer
  80. * @buf: the channel buffer containing the new sub-buffer
  81. * @subbuf: the start of the new sub-buffer
  82. * @prev_subbuf: the start of the previous sub-buffer
  83. * @prev_padding: unused space at the end of previous sub-buffer
  84. *
  85. * The client should return 1 to continue logging, 0 to stop
  86. * logging.
  87. *
  88. * NOTE: subbuf_start will also be invoked when the buffer is
  89. * created, so that the first sub-buffer can be initialized
  90. * if necessary. In this case, prev_subbuf will be NULL.
  91. *
  92. * NOTE: the client can reserve bytes at the beginning of the new
  93. * sub-buffer by calling subbuf_start_reserve() in this callback.
  94. */
  95. int (*subbuf_start) (struct rchan_buf *buf,
  96. void *subbuf,
  97. void *prev_subbuf,
  98. size_t prev_padding);
  99. /*
  100. * buf_mapped - relayfs buffer mmap notification
  101. * @buf: the channel buffer
  102. * @filp: relayfs file pointer
  103. *
  104. * Called when a relayfs file is successfully mmapped
  105. */
  106. void (*buf_mapped)(struct rchan_buf *buf,
  107. struct file *filp);
  108. /*
  109. * buf_unmapped - relayfs buffer unmap notification
  110. * @buf: the channel buffer
  111. * @filp: relayfs file pointer
  112. *
  113. * Called when a relayfs file is successfully unmapped
  114. */
  115. void (*buf_unmapped)(struct rchan_buf *buf,
  116. struct file *filp);
  117. };
  118. /*
  119. * relayfs kernel API, fs/relayfs/relay.c
  120. */
  121. struct rchan *relay_open(const char *base_filename,
  122. struct dentry *parent,
  123. size_t subbuf_size,
  124. size_t n_subbufs,
  125. struct rchan_callbacks *cb);
  126. extern void relay_close(struct rchan *chan);
  127. extern void relay_flush(struct rchan *chan);
  128. extern void relay_subbufs_consumed(struct rchan *chan,
  129. unsigned int cpu,
  130. size_t consumed);
  131. extern void relay_reset(struct rchan *chan);
  132. extern int relay_buf_full(struct rchan_buf *buf);
  133. extern size_t relay_switch_subbuf(struct rchan_buf *buf,
  134. size_t length);
  135. extern struct dentry *relayfs_create_dir(const char *name,
  136. struct dentry *parent);
  137. extern int relayfs_remove_dir(struct dentry *dentry);
  138. /**
  139. * relay_write - write data into the channel
  140. * @chan: relay channel
  141. * @data: data to be written
  142. * @length: number of bytes to write
  143. *
  144. * Writes data into the current cpu's channel buffer.
  145. *
  146. * Protects the buffer by disabling interrupts. Use this
  147. * if you might be logging from interrupt context. Try
  148. * __relay_write() if you know you won't be logging from
  149. * interrupt context.
  150. */
  151. static inline void relay_write(struct rchan *chan,
  152. const void *data,
  153. size_t length)
  154. {
  155. unsigned long flags;
  156. struct rchan_buf *buf;
  157. local_irq_save(flags);
  158. buf = chan->buf[smp_processor_id()];
  159. if (unlikely(buf->offset + length > chan->subbuf_size))
  160. length = relay_switch_subbuf(buf, length);
  161. memcpy(buf->data + buf->offset, data, length);
  162. buf->offset += length;
  163. local_irq_restore(flags);
  164. }
  165. /**
  166. * __relay_write - write data into the channel
  167. * @chan: relay channel
  168. * @data: data to be written
  169. * @length: number of bytes to write
  170. *
  171. * Writes data into the current cpu's channel buffer.
  172. *
  173. * Protects the buffer by disabling preemption. Use
  174. * relay_write() if you might be logging from interrupt
  175. * context.
  176. */
  177. static inline void __relay_write(struct rchan *chan,
  178. const void *data,
  179. size_t length)
  180. {
  181. struct rchan_buf *buf;
  182. buf = chan->buf[get_cpu()];
  183. if (unlikely(buf->offset + length > buf->chan->subbuf_size))
  184. length = relay_switch_subbuf(buf, length);
  185. memcpy(buf->data + buf->offset, data, length);
  186. buf->offset += length;
  187. put_cpu();
  188. }
  189. /**
  190. * relay_reserve - reserve slot in channel buffer
  191. * @chan: relay channel
  192. * @length: number of bytes to reserve
  193. *
  194. * Returns pointer to reserved slot, NULL if full.
  195. *
  196. * Reserves a slot in the current cpu's channel buffer.
  197. * Does not protect the buffer at all - caller must provide
  198. * appropriate synchronization.
  199. */
  200. static inline void *relay_reserve(struct rchan *chan, size_t length)
  201. {
  202. void *reserved;
  203. struct rchan_buf *buf = chan->buf[smp_processor_id()];
  204. if (unlikely(buf->offset + length > buf->chan->subbuf_size)) {
  205. length = relay_switch_subbuf(buf, length);
  206. if (!length)
  207. return NULL;
  208. }
  209. reserved = buf->data + buf->offset;
  210. buf->offset += length;
  211. return reserved;
  212. }
  213. /**
  214. * subbuf_start_reserve - reserve bytes at the start of a sub-buffer
  215. * @buf: relay channel buffer
  216. * @length: number of bytes to reserve
  217. *
  218. * Helper function used to reserve bytes at the beginning of
  219. * a sub-buffer in the subbuf_start() callback.
  220. */
  221. static inline void subbuf_start_reserve(struct rchan_buf *buf,
  222. size_t length)
  223. {
  224. BUG_ON(length >= buf->chan->subbuf_size - 1);
  225. buf->offset = length;
  226. }
  227. /*
  228. * exported relayfs file operations, fs/relayfs/inode.c
  229. */
  230. extern struct file_operations relayfs_file_operations;
  231. #endif /* _LINUX_RELAYFS_FS_H */