relayfs_fs.h 8.3 KB

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