seq_file.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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_escape(struct seq_file *, const char *, const char *);
  107. int seq_putc(struct seq_file *m, char c);
  108. int seq_puts(struct seq_file *m, const char *s);
  109. int seq_write(struct seq_file *seq, const void *data, size_t len);
  110. __printf(2, 3) int seq_printf(struct seq_file *, const char *, ...);
  111. __printf(2, 0) int seq_vprintf(struct seq_file *, const char *, va_list args);
  112. int seq_path(struct seq_file *, const struct path *, const char *);
  113. int seq_file_path(struct seq_file *, struct file *, const char *);
  114. int seq_dentry(struct seq_file *, struct dentry *, const char *);
  115. int seq_path_root(struct seq_file *m, const struct path *path,
  116. const struct path *root, const char *esc);
  117. int single_open(struct file *, int (*)(struct seq_file *, void *), void *);
  118. int single_open_size(struct file *, int (*)(struct seq_file *, void *), void *, size_t);
  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. static inline struct user_namespace *seq_user_ns(struct seq_file *seq)
  128. {
  129. #ifdef CONFIG_USER_NS
  130. return seq->user_ns;
  131. #else
  132. extern struct user_namespace init_user_ns;
  133. return &init_user_ns;
  134. #endif
  135. }
  136. #define SEQ_START_TOKEN ((void *)1)
  137. /*
  138. * Helpers for iteration over list_head-s in seq_files
  139. */
  140. extern struct list_head *seq_list_start(struct list_head *head,
  141. loff_t pos);
  142. extern struct list_head *seq_list_start_head(struct list_head *head,
  143. loff_t pos);
  144. extern struct list_head *seq_list_next(void *v, struct list_head *head,
  145. loff_t *ppos);
  146. /*
  147. * Helpers for iteration over hlist_head-s in seq_files
  148. */
  149. extern struct hlist_node *seq_hlist_start(struct hlist_head *head,
  150. loff_t pos);
  151. extern struct hlist_node *seq_hlist_start_head(struct hlist_head *head,
  152. loff_t pos);
  153. extern struct hlist_node *seq_hlist_next(void *v, struct hlist_head *head,
  154. loff_t *ppos);
  155. extern struct hlist_node *seq_hlist_start_rcu(struct hlist_head *head,
  156. loff_t pos);
  157. extern struct hlist_node *seq_hlist_start_head_rcu(struct hlist_head *head,
  158. loff_t pos);
  159. extern struct hlist_node *seq_hlist_next_rcu(void *v,
  160. struct hlist_head *head,
  161. loff_t *ppos);
  162. /* Helpers for iterating over per-cpu hlist_head-s in seq_files */
  163. extern struct hlist_node *seq_hlist_start_percpu(struct hlist_head __percpu *head, int *cpu, loff_t pos);
  164. extern struct hlist_node *seq_hlist_next_percpu(void *v, struct hlist_head __percpu *head, int *cpu, loff_t *pos);
  165. #endif