relayfs_fs.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. void *data;
  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. extern struct dentry *relayfs_create_file(const char *name,
  139. struct dentry *parent,
  140. int mode,
  141. struct file_operations *fops,
  142. void *data);
  143. /**
  144. * relay_write - write data into the channel
  145. * @chan: relay channel
  146. * @data: data to be written
  147. * @length: number of bytes to write
  148. *
  149. * Writes data into the current cpu's channel buffer.
  150. *
  151. * Protects the buffer by disabling interrupts. Use this
  152. * if you might be logging from interrupt context. Try
  153. * __relay_write() if you know you won't be logging from
  154. * interrupt context.
  155. */
  156. static inline void relay_write(struct rchan *chan,
  157. const void *data,
  158. size_t length)
  159. {
  160. unsigned long flags;
  161. struct rchan_buf *buf;
  162. local_irq_save(flags);
  163. buf = chan->buf[smp_processor_id()];
  164. if (unlikely(buf->offset + length > chan->subbuf_size))
  165. length = relay_switch_subbuf(buf, length);
  166. memcpy(buf->data + buf->offset, data, length);
  167. buf->offset += length;
  168. local_irq_restore(flags);
  169. }
  170. /**
  171. * __relay_write - write data into the channel
  172. * @chan: relay channel
  173. * @data: data to be written
  174. * @length: number of bytes to write
  175. *
  176. * Writes data into the current cpu's channel buffer.
  177. *
  178. * Protects the buffer by disabling preemption. Use
  179. * relay_write() if you might be logging from interrupt
  180. * context.
  181. */
  182. static inline void __relay_write(struct rchan *chan,
  183. const void *data,
  184. size_t length)
  185. {
  186. struct rchan_buf *buf;
  187. buf = chan->buf[get_cpu()];
  188. if (unlikely(buf->offset + length > buf->chan->subbuf_size))
  189. length = relay_switch_subbuf(buf, length);
  190. memcpy(buf->data + buf->offset, data, length);
  191. buf->offset += length;
  192. put_cpu();
  193. }
  194. /**
  195. * relay_reserve - reserve slot in channel buffer
  196. * @chan: relay channel
  197. * @length: number of bytes to reserve
  198. *
  199. * Returns pointer to reserved slot, NULL if full.
  200. *
  201. * Reserves a slot in the current cpu's channel buffer.
  202. * Does not protect the buffer at all - caller must provide
  203. * appropriate synchronization.
  204. */
  205. static inline void *relay_reserve(struct rchan *chan, size_t length)
  206. {
  207. void *reserved;
  208. struct rchan_buf *buf = chan->buf[smp_processor_id()];
  209. if (unlikely(buf->offset + length > buf->chan->subbuf_size)) {
  210. length = relay_switch_subbuf(buf, length);
  211. if (!length)
  212. return NULL;
  213. }
  214. reserved = buf->data + buf->offset;
  215. buf->offset += length;
  216. return reserved;
  217. }
  218. /**
  219. * subbuf_start_reserve - reserve bytes at the start of a sub-buffer
  220. * @buf: relay channel buffer
  221. * @length: number of bytes to reserve
  222. *
  223. * Helper function used to reserve bytes at the beginning of
  224. * a sub-buffer in the subbuf_start() callback.
  225. */
  226. static inline void subbuf_start_reserve(struct rchan_buf *buf,
  227. size_t length)
  228. {
  229. BUG_ON(length >= buf->chan->subbuf_size - 1);
  230. buf->offset = length;
  231. }
  232. /*
  233. * exported relayfs file operations, fs/relayfs/inode.c
  234. */
  235. extern struct file_operations relayfs_file_operations;
  236. #endif /* _LINUX_RELAYFS_FS_H */