seq_file.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. #ifndef _LINUX_SEQ_FILE_H
  2. #define _LINUX_SEQ_FILE_H
  3. #include <linux/types.h>
  4. #include <linux/string.h>
  5. #include <linux/bug.h>
  6. #include <linux/mutex.h>
  7. #include <linux/cpumask.h>
  8. #include <linux/nodemask.h>
  9. struct seq_operations;
  10. struct file;
  11. struct path;
  12. struct inode;
  13. struct dentry;
  14. struct user_namespace;
  15. struct seq_file {
  16. char *buf;
  17. size_t size;
  18. size_t from;
  19. size_t count;
  20. size_t pad_until;
  21. loff_t index;
  22. loff_t read_pos;
  23. u64 version;
  24. struct mutex lock;
  25. const struct seq_operations *op;
  26. int poll_event;
  27. #ifdef CONFIG_USER_NS
  28. struct user_namespace *user_ns;
  29. #endif
  30. void *private;
  31. };
  32. struct seq_operations {
  33. void * (*start) (struct seq_file *m, loff_t *pos);
  34. void (*stop) (struct seq_file *m, void *v);
  35. void * (*next) (struct seq_file *m, void *v, loff_t *pos);
  36. int (*show) (struct seq_file *m, void *v);
  37. };
  38. #define SEQ_SKIP 1
  39. /**
  40. * seq_get_buf - get buffer to write arbitrary data to
  41. * @m: the seq_file handle
  42. * @bufp: the beginning of the buffer is stored here
  43. *
  44. * Return the number of bytes available in the buffer, or zero if
  45. * there's no space.
  46. */
  47. static inline size_t seq_get_buf(struct seq_file *m, char **bufp)
  48. {
  49. BUG_ON(m->count > m->size);
  50. if (m->count < m->size)
  51. *bufp = m->buf + m->count;
  52. else
  53. *bufp = NULL;
  54. return m->size - m->count;
  55. }
  56. /**
  57. * seq_commit - commit data to the buffer
  58. * @m: the seq_file handle
  59. * @num: the number of bytes to commit
  60. *
  61. * Commit @num bytes of data written to a buffer previously acquired
  62. * by seq_buf_get. To signal an error condition, or that the data
  63. * didn't fit in the available space, pass a negative @num value.
  64. */
  65. static inline void seq_commit(struct seq_file *m, int num)
  66. {
  67. if (num < 0) {
  68. m->count = m->size;
  69. } else {
  70. BUG_ON(m->count + num > m->size);
  71. m->count += num;
  72. }
  73. }
  74. /**
  75. * seq_setwidth - set padding width
  76. * @m: the seq_file handle
  77. * @size: the max number of bytes to pad.
  78. *
  79. * Call seq_setwidth() for setting max width, then call seq_printf() etc. and
  80. * finally call seq_pad() to pad the remaining bytes.
  81. */
  82. static inline void seq_setwidth(struct seq_file *m, size_t size)
  83. {
  84. m->pad_until = m->count + size;
  85. }
  86. void seq_pad(struct seq_file *m, char c);
  87. char *mangle_path(char *s, const char *p, const char *esc);
  88. int seq_open(struct file *, const struct seq_operations *);
  89. ssize_t seq_read(struct file *, char __user *, size_t, loff_t *);
  90. loff_t seq_lseek(struct file *, loff_t, int);
  91. int seq_release(struct inode *, struct file *);
  92. int seq_escape(struct seq_file *, const char *, const char *);
  93. int seq_putc(struct seq_file *m, char c);
  94. int seq_puts(struct seq_file *m, const char *s);
  95. int seq_write(struct seq_file *seq, const void *data, size_t len);
  96. __printf(2, 3) int seq_printf(struct seq_file *, const char *, ...);
  97. __printf(2, 0) int seq_vprintf(struct seq_file *, const char *, va_list args);
  98. int seq_path(struct seq_file *, const struct path *, const char *);
  99. int seq_dentry(struct seq_file *, struct dentry *, const char *);
  100. int seq_path_root(struct seq_file *m, const struct path *path,
  101. const struct path *root, const char *esc);
  102. int seq_bitmap(struct seq_file *m, const unsigned long *bits,
  103. unsigned int nr_bits);
  104. static inline int seq_cpumask(struct seq_file *m, const struct cpumask *mask)
  105. {
  106. return seq_bitmap(m, cpumask_bits(mask), nr_cpu_ids);
  107. }
  108. static inline int seq_nodemask(struct seq_file *m, nodemask_t *mask)
  109. {
  110. return seq_bitmap(m, mask->bits, MAX_NUMNODES);
  111. }
  112. int seq_bitmap_list(struct seq_file *m, const unsigned long *bits,
  113. unsigned int nr_bits);
  114. static inline int seq_cpumask_list(struct seq_file *m,
  115. const struct cpumask *mask)
  116. {
  117. return seq_bitmap_list(m, cpumask_bits(mask), nr_cpu_ids);
  118. }
  119. static inline int seq_nodemask_list(struct seq_file *m, nodemask_t *mask)
  120. {
  121. return seq_bitmap_list(m, mask->bits, MAX_NUMNODES);
  122. }
  123. int single_open(struct file *, int (*)(struct seq_file *, void *), void *);
  124. int single_open_size(struct file *, int (*)(struct seq_file *, void *), void *, size_t);
  125. int single_release(struct inode *, struct file *);
  126. void *__seq_open_private(struct file *, const struct seq_operations *, int);
  127. int seq_open_private(struct file *, const struct seq_operations *, int);
  128. int seq_release_private(struct inode *, struct file *);
  129. int seq_put_decimal_ull(struct seq_file *m, char delimiter,
  130. unsigned long long num);
  131. int seq_put_decimal_ll(struct seq_file *m, char delimiter,
  132. long long num);
  133. static inline struct user_namespace *seq_user_ns(struct seq_file *seq)
  134. {
  135. #ifdef CONFIG_USER_NS
  136. return seq->user_ns;
  137. #else
  138. extern struct user_namespace init_user_ns;
  139. return &init_user_ns;
  140. #endif
  141. }
  142. #define SEQ_START_TOKEN ((void *)1)
  143. /*
  144. * Helpers for iteration over list_head-s in seq_files
  145. */
  146. extern struct list_head *seq_list_start(struct list_head *head,
  147. loff_t pos);
  148. extern struct list_head *seq_list_start_head(struct list_head *head,
  149. loff_t pos);
  150. extern struct list_head *seq_list_next(void *v, struct list_head *head,
  151. loff_t *ppos);
  152. /*
  153. * Helpers for iteration over hlist_head-s in seq_files
  154. */
  155. extern struct hlist_node *seq_hlist_start(struct hlist_head *head,
  156. loff_t pos);
  157. extern struct hlist_node *seq_hlist_start_head(struct hlist_head *head,
  158. loff_t pos);
  159. extern struct hlist_node *seq_hlist_next(void *v, struct hlist_head *head,
  160. loff_t *ppos);
  161. extern struct hlist_node *seq_hlist_start_rcu(struct hlist_head *head,
  162. loff_t pos);
  163. extern struct hlist_node *seq_hlist_start_head_rcu(struct hlist_head *head,
  164. loff_t pos);
  165. extern struct hlist_node *seq_hlist_next_rcu(void *v,
  166. struct hlist_head *head,
  167. loff_t *ppos);
  168. /* Helpers for iterating over per-cpu hlist_head-s in seq_files */
  169. extern struct hlist_node *seq_hlist_start_percpu(struct hlist_head __percpu *head, int *cpu, loff_t pos);
  170. extern struct hlist_node *seq_hlist_next_percpu(void *v, struct hlist_head __percpu *head, int *cpu, loff_t *pos);
  171. #endif