1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0 */
2*4882a593Smuzhiyun #ifndef _LINUX_SEQ_FILE_H
3*4882a593Smuzhiyun #define _LINUX_SEQ_FILE_H
4*4882a593Smuzhiyun
5*4882a593Smuzhiyun #include <linux/types.h>
6*4882a593Smuzhiyun #include <linux/string.h>
7*4882a593Smuzhiyun #include <linux/bug.h>
8*4882a593Smuzhiyun #include <linux/mutex.h>
9*4882a593Smuzhiyun #include <linux/cpumask.h>
10*4882a593Smuzhiyun #include <linux/nodemask.h>
11*4882a593Smuzhiyun #include <linux/fs.h>
12*4882a593Smuzhiyun #include <linux/cred.h>
13*4882a593Smuzhiyun
14*4882a593Smuzhiyun struct seq_operations;
15*4882a593Smuzhiyun
16*4882a593Smuzhiyun struct seq_file {
17*4882a593Smuzhiyun char *buf;
18*4882a593Smuzhiyun size_t size;
19*4882a593Smuzhiyun size_t from;
20*4882a593Smuzhiyun size_t count;
21*4882a593Smuzhiyun size_t pad_until;
22*4882a593Smuzhiyun loff_t index;
23*4882a593Smuzhiyun loff_t read_pos;
24*4882a593Smuzhiyun struct mutex lock;
25*4882a593Smuzhiyun const struct seq_operations *op;
26*4882a593Smuzhiyun int poll_event;
27*4882a593Smuzhiyun const struct file *file;
28*4882a593Smuzhiyun void *private;
29*4882a593Smuzhiyun };
30*4882a593Smuzhiyun
31*4882a593Smuzhiyun struct seq_operations {
32*4882a593Smuzhiyun void * (*start) (struct seq_file *m, loff_t *pos);
33*4882a593Smuzhiyun void (*stop) (struct seq_file *m, void *v);
34*4882a593Smuzhiyun void * (*next) (struct seq_file *m, void *v, loff_t *pos);
35*4882a593Smuzhiyun int (*show) (struct seq_file *m, void *v);
36*4882a593Smuzhiyun };
37*4882a593Smuzhiyun
38*4882a593Smuzhiyun #define SEQ_SKIP 1
39*4882a593Smuzhiyun
40*4882a593Smuzhiyun /**
41*4882a593Smuzhiyun * seq_has_overflowed - check if the buffer has overflowed
42*4882a593Smuzhiyun * @m: the seq_file handle
43*4882a593Smuzhiyun *
44*4882a593Smuzhiyun * seq_files have a buffer which may overflow. When this happens a larger
45*4882a593Smuzhiyun * buffer is reallocated and all the data will be printed again.
46*4882a593Smuzhiyun * The overflow state is true when m->count == m->size.
47*4882a593Smuzhiyun *
48*4882a593Smuzhiyun * Returns true if the buffer received more than it can hold.
49*4882a593Smuzhiyun */
seq_has_overflowed(struct seq_file * m)50*4882a593Smuzhiyun static inline bool seq_has_overflowed(struct seq_file *m)
51*4882a593Smuzhiyun {
52*4882a593Smuzhiyun return m->count == m->size;
53*4882a593Smuzhiyun }
54*4882a593Smuzhiyun
55*4882a593Smuzhiyun /**
56*4882a593Smuzhiyun * seq_get_buf - get buffer to write arbitrary data to
57*4882a593Smuzhiyun * @m: the seq_file handle
58*4882a593Smuzhiyun * @bufp: the beginning of the buffer is stored here
59*4882a593Smuzhiyun *
60*4882a593Smuzhiyun * Return the number of bytes available in the buffer, or zero if
61*4882a593Smuzhiyun * there's no space.
62*4882a593Smuzhiyun */
seq_get_buf(struct seq_file * m,char ** bufp)63*4882a593Smuzhiyun static inline size_t seq_get_buf(struct seq_file *m, char **bufp)
64*4882a593Smuzhiyun {
65*4882a593Smuzhiyun BUG_ON(m->count > m->size);
66*4882a593Smuzhiyun if (m->count < m->size)
67*4882a593Smuzhiyun *bufp = m->buf + m->count;
68*4882a593Smuzhiyun else
69*4882a593Smuzhiyun *bufp = NULL;
70*4882a593Smuzhiyun
71*4882a593Smuzhiyun return m->size - m->count;
72*4882a593Smuzhiyun }
73*4882a593Smuzhiyun
74*4882a593Smuzhiyun /**
75*4882a593Smuzhiyun * seq_commit - commit data to the buffer
76*4882a593Smuzhiyun * @m: the seq_file handle
77*4882a593Smuzhiyun * @num: the number of bytes to commit
78*4882a593Smuzhiyun *
79*4882a593Smuzhiyun * Commit @num bytes of data written to a buffer previously acquired
80*4882a593Smuzhiyun * by seq_buf_get. To signal an error condition, or that the data
81*4882a593Smuzhiyun * didn't fit in the available space, pass a negative @num value.
82*4882a593Smuzhiyun */
seq_commit(struct seq_file * m,int num)83*4882a593Smuzhiyun static inline void seq_commit(struct seq_file *m, int num)
84*4882a593Smuzhiyun {
85*4882a593Smuzhiyun if (num < 0) {
86*4882a593Smuzhiyun m->count = m->size;
87*4882a593Smuzhiyun } else {
88*4882a593Smuzhiyun BUG_ON(m->count + num > m->size);
89*4882a593Smuzhiyun m->count += num;
90*4882a593Smuzhiyun }
91*4882a593Smuzhiyun }
92*4882a593Smuzhiyun
93*4882a593Smuzhiyun /**
94*4882a593Smuzhiyun * seq_setwidth - set padding width
95*4882a593Smuzhiyun * @m: the seq_file handle
96*4882a593Smuzhiyun * @size: the max number of bytes to pad.
97*4882a593Smuzhiyun *
98*4882a593Smuzhiyun * Call seq_setwidth() for setting max width, then call seq_printf() etc. and
99*4882a593Smuzhiyun * finally call seq_pad() to pad the remaining bytes.
100*4882a593Smuzhiyun */
seq_setwidth(struct seq_file * m,size_t size)101*4882a593Smuzhiyun static inline void seq_setwidth(struct seq_file *m, size_t size)
102*4882a593Smuzhiyun {
103*4882a593Smuzhiyun m->pad_until = m->count + size;
104*4882a593Smuzhiyun }
105*4882a593Smuzhiyun void seq_pad(struct seq_file *m, char c);
106*4882a593Smuzhiyun
107*4882a593Smuzhiyun char *mangle_path(char *s, const char *p, const char *esc);
108*4882a593Smuzhiyun int seq_open(struct file *, const struct seq_operations *);
109*4882a593Smuzhiyun ssize_t seq_read(struct file *, char __user *, size_t, loff_t *);
110*4882a593Smuzhiyun ssize_t seq_read_iter(struct kiocb *iocb, struct iov_iter *iter);
111*4882a593Smuzhiyun loff_t seq_lseek(struct file *, loff_t, int);
112*4882a593Smuzhiyun int seq_release(struct inode *, struct file *);
113*4882a593Smuzhiyun int seq_write(struct seq_file *seq, const void *data, size_t len);
114*4882a593Smuzhiyun
115*4882a593Smuzhiyun __printf(2, 0)
116*4882a593Smuzhiyun void seq_vprintf(struct seq_file *m, const char *fmt, va_list args);
117*4882a593Smuzhiyun __printf(2, 3)
118*4882a593Smuzhiyun void seq_printf(struct seq_file *m, const char *fmt, ...);
119*4882a593Smuzhiyun void seq_putc(struct seq_file *m, char c);
120*4882a593Smuzhiyun void seq_puts(struct seq_file *m, const char *s);
121*4882a593Smuzhiyun void seq_put_decimal_ull_width(struct seq_file *m, const char *delimiter,
122*4882a593Smuzhiyun unsigned long long num, unsigned int width);
123*4882a593Smuzhiyun void seq_put_decimal_ull(struct seq_file *m, const char *delimiter,
124*4882a593Smuzhiyun unsigned long long num);
125*4882a593Smuzhiyun void seq_put_decimal_ll(struct seq_file *m, const char *delimiter, long long num);
126*4882a593Smuzhiyun void seq_put_hex_ll(struct seq_file *m, const char *delimiter,
127*4882a593Smuzhiyun unsigned long long v, unsigned int width);
128*4882a593Smuzhiyun
129*4882a593Smuzhiyun void seq_escape(struct seq_file *m, const char *s, const char *esc);
130*4882a593Smuzhiyun void seq_escape_mem_ascii(struct seq_file *m, const char *src, size_t isz);
131*4882a593Smuzhiyun
132*4882a593Smuzhiyun void seq_hex_dump(struct seq_file *m, const char *prefix_str, int prefix_type,
133*4882a593Smuzhiyun int rowsize, int groupsize, const void *buf, size_t len,
134*4882a593Smuzhiyun bool ascii);
135*4882a593Smuzhiyun
136*4882a593Smuzhiyun int seq_path(struct seq_file *, const struct path *, const char *);
137*4882a593Smuzhiyun int seq_file_path(struct seq_file *, struct file *, const char *);
138*4882a593Smuzhiyun int seq_dentry(struct seq_file *, struct dentry *, const char *);
139*4882a593Smuzhiyun int seq_path_root(struct seq_file *m, const struct path *path,
140*4882a593Smuzhiyun const struct path *root, const char *esc);
141*4882a593Smuzhiyun
142*4882a593Smuzhiyun int single_open(struct file *, int (*)(struct seq_file *, void *), void *);
143*4882a593Smuzhiyun int single_open_size(struct file *, int (*)(struct seq_file *, void *), void *, size_t);
144*4882a593Smuzhiyun int single_release(struct inode *, struct file *);
145*4882a593Smuzhiyun void *__seq_open_private(struct file *, const struct seq_operations *, int);
146*4882a593Smuzhiyun int seq_open_private(struct file *, const struct seq_operations *, int);
147*4882a593Smuzhiyun int seq_release_private(struct inode *, struct file *);
148*4882a593Smuzhiyun
149*4882a593Smuzhiyun #define DEFINE_SEQ_ATTRIBUTE(__name) \
150*4882a593Smuzhiyun static int __name ## _open(struct inode *inode, struct file *file) \
151*4882a593Smuzhiyun { \
152*4882a593Smuzhiyun int ret = seq_open(file, &__name ## _sops); \
153*4882a593Smuzhiyun if (!ret && inode->i_private) { \
154*4882a593Smuzhiyun struct seq_file *seq_f = file->private_data; \
155*4882a593Smuzhiyun seq_f->private = inode->i_private; \
156*4882a593Smuzhiyun } \
157*4882a593Smuzhiyun return ret; \
158*4882a593Smuzhiyun } \
159*4882a593Smuzhiyun \
160*4882a593Smuzhiyun static const struct file_operations __name ## _fops = { \
161*4882a593Smuzhiyun .owner = THIS_MODULE, \
162*4882a593Smuzhiyun .open = __name ## _open, \
163*4882a593Smuzhiyun .read = seq_read, \
164*4882a593Smuzhiyun .llseek = seq_lseek, \
165*4882a593Smuzhiyun .release = seq_release, \
166*4882a593Smuzhiyun }
167*4882a593Smuzhiyun
168*4882a593Smuzhiyun #define DEFINE_SHOW_ATTRIBUTE(__name) \
169*4882a593Smuzhiyun static int __name ## _open(struct inode *inode, struct file *file) \
170*4882a593Smuzhiyun { \
171*4882a593Smuzhiyun return single_open(file, __name ## _show, inode->i_private); \
172*4882a593Smuzhiyun } \
173*4882a593Smuzhiyun \
174*4882a593Smuzhiyun static const struct file_operations __name ## _fops = { \
175*4882a593Smuzhiyun .owner = THIS_MODULE, \
176*4882a593Smuzhiyun .open = __name ## _open, \
177*4882a593Smuzhiyun .read = seq_read, \
178*4882a593Smuzhiyun .llseek = seq_lseek, \
179*4882a593Smuzhiyun .release = single_release, \
180*4882a593Smuzhiyun }
181*4882a593Smuzhiyun
182*4882a593Smuzhiyun #define DEFINE_PROC_SHOW_ATTRIBUTE(__name) \
183*4882a593Smuzhiyun static int __name ## _open(struct inode *inode, struct file *file) \
184*4882a593Smuzhiyun { \
185*4882a593Smuzhiyun return single_open(file, __name ## _show, PDE_DATA(inode)); \
186*4882a593Smuzhiyun } \
187*4882a593Smuzhiyun \
188*4882a593Smuzhiyun static const struct proc_ops __name ## _proc_ops = { \
189*4882a593Smuzhiyun .proc_open = __name ## _open, \
190*4882a593Smuzhiyun .proc_read = seq_read, \
191*4882a593Smuzhiyun .proc_lseek = seq_lseek, \
192*4882a593Smuzhiyun .proc_release = single_release, \
193*4882a593Smuzhiyun }
194*4882a593Smuzhiyun
seq_user_ns(struct seq_file * seq)195*4882a593Smuzhiyun static inline struct user_namespace *seq_user_ns(struct seq_file *seq)
196*4882a593Smuzhiyun {
197*4882a593Smuzhiyun #ifdef CONFIG_USER_NS
198*4882a593Smuzhiyun return seq->file->f_cred->user_ns;
199*4882a593Smuzhiyun #else
200*4882a593Smuzhiyun extern struct user_namespace init_user_ns;
201*4882a593Smuzhiyun return &init_user_ns;
202*4882a593Smuzhiyun #endif
203*4882a593Smuzhiyun }
204*4882a593Smuzhiyun
205*4882a593Smuzhiyun /**
206*4882a593Smuzhiyun * seq_show_options - display mount options with appropriate escapes.
207*4882a593Smuzhiyun * @m: the seq_file handle
208*4882a593Smuzhiyun * @name: the mount option name
209*4882a593Smuzhiyun * @value: the mount option name's value, can be NULL
210*4882a593Smuzhiyun */
seq_show_option(struct seq_file * m,const char * name,const char * value)211*4882a593Smuzhiyun static inline void seq_show_option(struct seq_file *m, const char *name,
212*4882a593Smuzhiyun const char *value)
213*4882a593Smuzhiyun {
214*4882a593Smuzhiyun seq_putc(m, ',');
215*4882a593Smuzhiyun seq_escape(m, name, ",= \t\n\\");
216*4882a593Smuzhiyun if (value) {
217*4882a593Smuzhiyun seq_putc(m, '=');
218*4882a593Smuzhiyun seq_escape(m, value, ", \t\n\\");
219*4882a593Smuzhiyun }
220*4882a593Smuzhiyun }
221*4882a593Smuzhiyun
222*4882a593Smuzhiyun /**
223*4882a593Smuzhiyun * seq_show_option_n - display mount options with appropriate escapes
224*4882a593Smuzhiyun * where @value must be a specific length.
225*4882a593Smuzhiyun * @m: the seq_file handle
226*4882a593Smuzhiyun * @name: the mount option name
227*4882a593Smuzhiyun * @value: the mount option name's value, cannot be NULL
228*4882a593Smuzhiyun * @length: the length of @value to display
229*4882a593Smuzhiyun *
230*4882a593Smuzhiyun * This is a macro since this uses "length" to define the size of the
231*4882a593Smuzhiyun * stack buffer.
232*4882a593Smuzhiyun */
233*4882a593Smuzhiyun #define seq_show_option_n(m, name, value, length) { \
234*4882a593Smuzhiyun char val_buf[length + 1]; \
235*4882a593Smuzhiyun strncpy(val_buf, value, length); \
236*4882a593Smuzhiyun val_buf[length] = '\0'; \
237*4882a593Smuzhiyun seq_show_option(m, name, val_buf); \
238*4882a593Smuzhiyun }
239*4882a593Smuzhiyun
240*4882a593Smuzhiyun #define SEQ_START_TOKEN ((void *)1)
241*4882a593Smuzhiyun /*
242*4882a593Smuzhiyun * Helpers for iteration over list_head-s in seq_files
243*4882a593Smuzhiyun */
244*4882a593Smuzhiyun
245*4882a593Smuzhiyun extern struct list_head *seq_list_start(struct list_head *head,
246*4882a593Smuzhiyun loff_t pos);
247*4882a593Smuzhiyun extern struct list_head *seq_list_start_head(struct list_head *head,
248*4882a593Smuzhiyun loff_t pos);
249*4882a593Smuzhiyun extern struct list_head *seq_list_next(void *v, struct list_head *head,
250*4882a593Smuzhiyun loff_t *ppos);
251*4882a593Smuzhiyun
252*4882a593Smuzhiyun /*
253*4882a593Smuzhiyun * Helpers for iteration over hlist_head-s in seq_files
254*4882a593Smuzhiyun */
255*4882a593Smuzhiyun
256*4882a593Smuzhiyun extern struct hlist_node *seq_hlist_start(struct hlist_head *head,
257*4882a593Smuzhiyun loff_t pos);
258*4882a593Smuzhiyun extern struct hlist_node *seq_hlist_start_head(struct hlist_head *head,
259*4882a593Smuzhiyun loff_t pos);
260*4882a593Smuzhiyun extern struct hlist_node *seq_hlist_next(void *v, struct hlist_head *head,
261*4882a593Smuzhiyun loff_t *ppos);
262*4882a593Smuzhiyun
263*4882a593Smuzhiyun extern struct hlist_node *seq_hlist_start_rcu(struct hlist_head *head,
264*4882a593Smuzhiyun loff_t pos);
265*4882a593Smuzhiyun extern struct hlist_node *seq_hlist_start_head_rcu(struct hlist_head *head,
266*4882a593Smuzhiyun loff_t pos);
267*4882a593Smuzhiyun extern struct hlist_node *seq_hlist_next_rcu(void *v,
268*4882a593Smuzhiyun struct hlist_head *head,
269*4882a593Smuzhiyun loff_t *ppos);
270*4882a593Smuzhiyun
271*4882a593Smuzhiyun /* Helpers for iterating over per-cpu hlist_head-s in seq_files */
272*4882a593Smuzhiyun extern struct hlist_node *seq_hlist_start_percpu(struct hlist_head __percpu *head, int *cpu, loff_t pos);
273*4882a593Smuzhiyun
274*4882a593Smuzhiyun extern struct hlist_node *seq_hlist_next_percpu(void *v, struct hlist_head __percpu *head, int *cpu, loff_t *pos);
275*4882a593Smuzhiyun
276*4882a593Smuzhiyun void seq_file_init(void);
277*4882a593Smuzhiyun #endif
278