seq_file.h 6.0 KB

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