relayfs_fs.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. * @is_global: outparam - set non-zero if the buffer should be global
  112. *
  113. * Called during relay_open(), once for each per-cpu buffer,
  114. * to allow the client to create a file to be used to
  115. * represent the corresponding channel buffer. If the file is
  116. * created outside of relayfs, the parent must also exist in
  117. * that filesystem.
  118. *
  119. * The callback should return the dentry of the file created
  120. * to represent the relay buffer.
  121. *
  122. * Setting the is_global outparam to a non-zero value will
  123. * cause relay_open() to create a single global buffer rather
  124. * than the default set of per-cpu buffers.
  125. *
  126. * See Documentation/filesystems/relayfs.txt for more info.
  127. */
  128. struct dentry *(*create_buf_file)(const char *filename,
  129. struct dentry *parent,
  130. int mode,
  131. struct rchan_buf *buf,
  132. int *is_global);
  133. /*
  134. * remove_buf_file - remove file representing a relayfs channel buffer
  135. * @dentry: the dentry of the file to remove
  136. *
  137. * Called during relay_close(), once for each per-cpu buffer,
  138. * to allow the client to remove a file used to represent a
  139. * channel buffer.
  140. *
  141. * The callback should return 0 if successful, negative if not.
  142. */
  143. int (*remove_buf_file)(struct dentry *dentry);
  144. };
  145. /*
  146. * relayfs kernel API, fs/relayfs/relay.c
  147. */
  148. struct rchan *relay_open(const char *base_filename,
  149. struct dentry *parent,
  150. size_t subbuf_size,
  151. size_t n_subbufs,
  152. struct rchan_callbacks *cb);
  153. extern void relay_close(struct rchan *chan);
  154. extern void relay_flush(struct rchan *chan);
  155. extern void relay_subbufs_consumed(struct rchan *chan,
  156. unsigned int cpu,
  157. size_t consumed);
  158. extern void relay_reset(struct rchan *chan);
  159. extern int relay_buf_full(struct rchan_buf *buf);
  160. extern size_t relay_switch_subbuf(struct rchan_buf *buf,
  161. size_t length);
  162. extern struct dentry *relayfs_create_dir(const char *name,
  163. struct dentry *parent);
  164. extern int relayfs_remove_dir(struct dentry *dentry);
  165. extern struct dentry *relayfs_create_file(const char *name,
  166. struct dentry *parent,
  167. int mode,
  168. struct file_operations *fops,
  169. void *data);
  170. extern int relayfs_remove_file(struct dentry *dentry);
  171. /**
  172. * relay_write - write data into the channel
  173. * @chan: relay channel
  174. * @data: data to be written
  175. * @length: number of bytes to write
  176. *
  177. * Writes data into the current cpu's channel buffer.
  178. *
  179. * Protects the buffer by disabling interrupts. Use this
  180. * if you might be logging from interrupt context. Try
  181. * __relay_write() if you know you won't be logging from
  182. * interrupt context.
  183. */
  184. static inline void relay_write(struct rchan *chan,
  185. const void *data,
  186. size_t length)
  187. {
  188. unsigned long flags;
  189. struct rchan_buf *buf;
  190. local_irq_save(flags);
  191. buf = chan->buf[smp_processor_id()];
  192. if (unlikely(buf->offset + length > chan->subbuf_size))
  193. length = relay_switch_subbuf(buf, length);
  194. memcpy(buf->data + buf->offset, data, length);
  195. buf->offset += length;
  196. local_irq_restore(flags);
  197. }
  198. /**
  199. * __relay_write - write data into the channel
  200. * @chan: relay channel
  201. * @data: data to be written
  202. * @length: number of bytes to write
  203. *
  204. * Writes data into the current cpu's channel buffer.
  205. *
  206. * Protects the buffer by disabling preemption. Use
  207. * relay_write() if you might be logging from interrupt
  208. * context.
  209. */
  210. static inline void __relay_write(struct rchan *chan,
  211. const void *data,
  212. size_t length)
  213. {
  214. struct rchan_buf *buf;
  215. buf = chan->buf[get_cpu()];
  216. if (unlikely(buf->offset + length > buf->chan->subbuf_size))
  217. length = relay_switch_subbuf(buf, length);
  218. memcpy(buf->data + buf->offset, data, length);
  219. buf->offset += length;
  220. put_cpu();
  221. }
  222. /**
  223. * relay_reserve - reserve slot in channel buffer
  224. * @chan: relay channel
  225. * @length: number of bytes to reserve
  226. *
  227. * Returns pointer to reserved slot, NULL if full.
  228. *
  229. * Reserves a slot in the current cpu's channel buffer.
  230. * Does not protect the buffer at all - caller must provide
  231. * appropriate synchronization.
  232. */
  233. static inline void *relay_reserve(struct rchan *chan, size_t length)
  234. {
  235. void *reserved;
  236. struct rchan_buf *buf = chan->buf[smp_processor_id()];
  237. if (unlikely(buf->offset + length > buf->chan->subbuf_size)) {
  238. length = relay_switch_subbuf(buf, length);
  239. if (!length)
  240. return NULL;
  241. }
  242. reserved = buf->data + buf->offset;
  243. buf->offset += length;
  244. return reserved;
  245. }
  246. /**
  247. * subbuf_start_reserve - reserve bytes at the start of a sub-buffer
  248. * @buf: relay channel buffer
  249. * @length: number of bytes to reserve
  250. *
  251. * Helper function used to reserve bytes at the beginning of
  252. * a sub-buffer in the subbuf_start() callback.
  253. */
  254. static inline void subbuf_start_reserve(struct rchan_buf *buf,
  255. size_t length)
  256. {
  257. BUG_ON(length >= buf->chan->subbuf_size - 1);
  258. buf->offset = length;
  259. }
  260. /*
  261. * exported relay file operations, fs/relayfs/inode.c
  262. */
  263. extern struct file_operations relay_file_operations;
  264. #endif /* _LINUX_RELAYFS_FS_H */