seq_file.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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_has_overflowed - check if the buffer has overflowed
  41. * @m: the seq_file handle
  42. *
  43. * seq_files have a buffer which may overflow. When this happens a larger
  44. * buffer is reallocated and all the data will be printed again.
  45. * The overflow state is true when m->count == m->size.
  46. *
  47. * Returns true if the buffer received more than it can hold.
  48. */
  49. static inline bool seq_has_overflowed(struct seq_file *m)
  50. {
  51. return m->count == m->size;
  52. }
  53. /**
  54. * seq_get_buf - get buffer to write arbitrary data to
  55. * @m: the seq_file handle
  56. * @bufp: the beginning of the buffer is stored here
  57. *
  58. * Return the number of bytes available in the buffer, or zero if
  59. * there's no space.
  60. */
  61. static inline size_t seq_get_buf(struct seq_file *m, char **bufp)
  62. {
  63. BUG_ON(m->count > m->size);
  64. if (m->count < m->size)
  65. *bufp = m->buf + m->count;
  66. else
  67. *bufp = NULL;
  68. return m->size - m->count;
  69. }
  70. /**
  71. * seq_commit - commit data to the buffer
  72. * @m: the seq_file handle
  73. * @num: the number of bytes to commit
  74. *
  75. * Commit @num bytes of data written to a buffer previously acquired
  76. * by seq_buf_get. To signal an error condition, or that the data
  77. * didn't fit in the available space, pass a negative @num value.
  78. */
  79. static inline void seq_commit(struct seq_file *m, int num)
  80. {
  81. if (num < 0) {
  82. m->count = m->size;
  83. } else {
  84. BUG_ON(m->count + num > m->size);
  85. m->count += num;
  86. }
  87. }
  88. /**
  89. * seq_setwidth - set padding width
  90. * @m: the seq_file handle
  91. * @size: the max number of bytes to pad.
  92. *
  93. * Call seq_setwidth() for setting max width, then call seq_printf() etc. and
  94. * finally call seq_pad() to pad the remaining bytes.
  95. */
  96. static inline void seq_setwidth(struct seq_file *m, size_t size)
  97. {
  98. m->pad_until = m->count + size;
  99. }
  100. void seq_pad(struct seq_file *m, char c);
  101. char *mangle_path(char *s, const char *p, const char *esc);
  102. int seq_open(struct file *, const struct seq_operations *);
  103. ssize_t seq_read(struct file *, char __user *, size_t, loff_t *);
  104. loff_t seq_lseek(struct file *, loff_t, int);
  105. int seq_release(struct inode *, struct file *);
  106. int seq_write(struct seq_file *seq, const void *data, size_t len);
  107. __printf(2, 0)
  108. void seq_vprintf(struct seq_file *m, const char *fmt, va_list args);
  109. __printf(2, 3)
  110. void seq_printf(struct seq_file *m, const char *fmt, ...);
  111. void seq_putc(struct seq_file *m, char c);
  112. void seq_puts(struct seq_file *m, const char *s);
  113. void seq_put_decimal_ull(struct seq_file *m, char delimiter,
  114. unsigned long long num);
  115. void seq_put_decimal_ll(struct seq_file *m, char delimiter, long long num);
  116. void seq_escape(struct seq_file *m, const char *s, const char *esc);
  117. void seq_hex_dump(struct seq_file *m, const char *prefix_str, int prefix_type,
  118. int rowsize, int groupsize, const void *buf, size_t len,
  119. bool ascii);
  120. int seq_path(struct seq_file *, const struct path *, const char *);
  121. int seq_file_path(struct seq_file *, struct file *, const char *);
  122. int seq_dentry(struct seq_file *, struct dentry *, const char *);
  123. int seq_path_root(struct seq_file *m, const struct path *path,
  124. const struct path *root, const char *esc);
  125. int single_open(struct file *, int (*)(struct seq_file *, void *), void *);
  126. int single_open_size(struct file *, int (*)(struct seq_file *, void *), void *, size_t);
  127. int single_release(struct inode *, struct file *);
  128. void *__seq_open_private(struct file *, const struct seq_operations *, int);
  129. int seq_open_private(struct file *, const struct seq_operations *, int);
  130. int seq_release_private(struct inode *, struct file *);
  131. static inline struct user_namespace *seq_user_ns(struct seq_file *seq)
  132. {
  133. #ifdef CONFIG_USER_NS
  134. return seq->user_ns;
  135. #else
  136. extern struct user_namespace init_user_ns;
  137. return &init_user_ns;
  138. #endif
  139. }
  140. /**
  141. * seq_show_options - display mount options with appropriate escapes.
  142. * @m: the seq_file handle
  143. * @name: the mount option name
  144. * @value: the mount option name's value, can be NULL
  145. */
  146. static inline void seq_show_option(struct seq_file *m, const char *name,
  147. const char *value)
  148. {
  149. seq_putc(m, ',');
  150. seq_escape(m, name, ",= \t\n\\");
  151. if (value) {
  152. seq_putc(m, '=');
  153. seq_escape(m, value, ", \t\n\\");
  154. }
  155. }
  156. /**
  157. * seq_show_option_n - display mount options with appropriate escapes
  158. * where @value must be a specific length.
  159. * @m: the seq_file handle
  160. * @name: the mount option name
  161. * @value: the mount option name's value, cannot be NULL
  162. * @length: the length of @value to display
  163. *
  164. * This is a macro since this uses "length" to define the size of the
  165. * stack buffer.
  166. */
  167. #define seq_show_option_n(m, name, value, length) { \
  168. char val_buf[length + 1]; \
  169. strncpy(val_buf, value, length); \
  170. val_buf[length] = '\0'; \
  171. seq_show_option(m, name, val_buf); \
  172. }
  173. #define SEQ_START_TOKEN ((void *)1)
  174. /*
  175. * Helpers for iteration over list_head-s in seq_files
  176. */
  177. extern struct list_head *seq_list_start(struct list_head *head,
  178. loff_t pos);
  179. extern struct list_head *seq_list_start_head(struct list_head *head,
  180. loff_t pos);
  181. extern struct list_head *seq_list_next(void *v, struct list_head *head,
  182. loff_t *ppos);
  183. /*
  184. * Helpers for iteration over hlist_head-s in seq_files
  185. */
  186. extern struct hlist_node *seq_hlist_start(struct hlist_head *head,
  187. loff_t pos);
  188. extern struct hlist_node *seq_hlist_start_head(struct hlist_head *head,
  189. loff_t pos);
  190. extern struct hlist_node *seq_hlist_next(void *v, struct hlist_head *head,
  191. loff_t *ppos);
  192. extern struct hlist_node *seq_hlist_start_rcu(struct hlist_head *head,
  193. loff_t pos);
  194. extern struct hlist_node *seq_hlist_start_head_rcu(struct hlist_head *head,
  195. loff_t pos);
  196. extern struct hlist_node *seq_hlist_next_rcu(void *v,
  197. struct hlist_head *head,
  198. loff_t *ppos);
  199. /* Helpers for iterating over per-cpu hlist_head-s in seq_files */
  200. extern struct hlist_node *seq_hlist_start_percpu(struct hlist_head __percpu *head, int *cpu, loff_t pos);
  201. extern struct hlist_node *seq_hlist_next_percpu(void *v, struct hlist_head __percpu *head, int *cpu, loff_t *pos);
  202. #endif