seq_file.h 6.6 KB

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