relayfs_fs.h
上传用户:szlgq88
上传日期:2009-04-28
资源大小:48287k
文件大小:7k
源码类别:

嵌入式Linux

开发平台:

Unix_Linux

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