xref: /openbmc/linux/include/linux/seq_file.h (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
21da177e4SLinus Torvalds #ifndef _LINUX_SEQ_FILE_H
31da177e4SLinus Torvalds #define _LINUX_SEQ_FILE_H
41da177e4SLinus Torvalds 
51da177e4SLinus Torvalds #include <linux/types.h>
61da177e4SLinus Torvalds #include <linux/string.h>
7372904c0SAndy Shevchenko #include <linux/string_helpers.h>
8187f1882SPaul Gortmaker #include <linux/bug.h>
90ac1759aSIngo Molnar #include <linux/mutex.h>
1050ac2d69SAlexey Dobriyan #include <linux/cpumask.h>
1150ac2d69SAlexey Dobriyan #include <linux/nodemask.h>
1234dbbcdbSLinus Torvalds #include <linux/fs.h>
1334dbbcdbSLinus Torvalds #include <linux/cred.h>
141da177e4SLinus Torvalds 
151da177e4SLinus Torvalds struct seq_operations;
161da177e4SLinus Torvalds 
171da177e4SLinus Torvalds struct seq_file {
181da177e4SLinus Torvalds 	char *buf;
191da177e4SLinus Torvalds 	size_t size;
201da177e4SLinus Torvalds 	size_t from;
211da177e4SLinus Torvalds 	size_t count;
22839cc2a9STetsuo Handa 	size_t pad_until;
231da177e4SLinus Torvalds 	loff_t index;
248f19d472SEric Biederman 	loff_t read_pos;
250ac1759aSIngo Molnar 	struct mutex lock;
2615ad7cdcSHelge Deller 	const struct seq_operations *op;
27f1514638SKay Sievers 	int poll_event;
2834dbbcdbSLinus Torvalds 	const struct file *file;
291da177e4SLinus Torvalds 	void *private;
301da177e4SLinus Torvalds };
311da177e4SLinus Torvalds 
321da177e4SLinus Torvalds struct seq_operations {
331da177e4SLinus Torvalds 	void * (*start) (struct seq_file *m, loff_t *pos);
341da177e4SLinus Torvalds 	void (*stop) (struct seq_file *m, void *v);
351da177e4SLinus Torvalds 	void * (*next) (struct seq_file *m, void *v, loff_t *pos);
361da177e4SLinus Torvalds 	int (*show) (struct seq_file *m, void *v);
371da177e4SLinus Torvalds };
381da177e4SLinus Torvalds 
39521b5d0cSAl Viro #define SEQ_SKIP 1
40521b5d0cSAl Viro 
41f8439806SMiklos Szeredi /**
421f33c41cSJoe Perches  * seq_has_overflowed - check if the buffer has overflowed
431f33c41cSJoe Perches  * @m: the seq_file handle
441f33c41cSJoe Perches  *
451f33c41cSJoe Perches  * seq_files have a buffer which may overflow. When this happens a larger
461f33c41cSJoe Perches  * buffer is reallocated and all the data will be printed again.
471f33c41cSJoe Perches  * The overflow state is true when m->count == m->size.
481f33c41cSJoe Perches  *
491f33c41cSJoe Perches  * Returns true if the buffer received more than it can hold.
501f33c41cSJoe Perches  */
seq_has_overflowed(struct seq_file * m)511f33c41cSJoe Perches static inline bool seq_has_overflowed(struct seq_file *m)
521f33c41cSJoe Perches {
531f33c41cSJoe Perches 	return m->count == m->size;
541f33c41cSJoe Perches }
551f33c41cSJoe Perches 
561f33c41cSJoe Perches /**
57f8439806SMiklos Szeredi  * seq_get_buf - get buffer to write arbitrary data to
58f8439806SMiklos Szeredi  * @m: the seq_file handle
59f8439806SMiklos Szeredi  * @bufp: the beginning of the buffer is stored here
60f8439806SMiklos Szeredi  *
61f8439806SMiklos Szeredi  * Return the number of bytes available in the buffer, or zero if
62f8439806SMiklos Szeredi  * there's no space.
63f8439806SMiklos Szeredi  */
seq_get_buf(struct seq_file * m,char ** bufp)64f8439806SMiklos Szeredi static inline size_t seq_get_buf(struct seq_file *m, char **bufp)
65f8439806SMiklos Szeredi {
66f8439806SMiklos Szeredi 	BUG_ON(m->count > m->size);
67f8439806SMiklos Szeredi 	if (m->count < m->size)
68f8439806SMiklos Szeredi 		*bufp = m->buf + m->count;
69f8439806SMiklos Szeredi 	else
70f8439806SMiklos Szeredi 		*bufp = NULL;
71f8439806SMiklos Szeredi 
72f8439806SMiklos Szeredi 	return m->size - m->count;
73f8439806SMiklos Szeredi }
74f8439806SMiklos Szeredi 
75f8439806SMiklos Szeredi /**
76f8439806SMiklos Szeredi  * seq_commit - commit data to the buffer
77f8439806SMiklos Szeredi  * @m: the seq_file handle
78f8439806SMiklos Szeredi  * @num: the number of bytes to commit
79f8439806SMiklos Szeredi  *
80f8439806SMiklos Szeredi  * Commit @num bytes of data written to a buffer previously acquired
81f8439806SMiklos Szeredi  * by seq_buf_get.  To signal an error condition, or that the data
82f8439806SMiklos Szeredi  * didn't fit in the available space, pass a negative @num value.
83f8439806SMiklos Szeredi  */
seq_commit(struct seq_file * m,int num)84f8439806SMiklos Szeredi static inline void seq_commit(struct seq_file *m, int num)
85f8439806SMiklos Szeredi {
86f8439806SMiklos Szeredi 	if (num < 0) {
87f8439806SMiklos Szeredi 		m->count = m->size;
88f8439806SMiklos Szeredi 	} else {
89f8439806SMiklos Szeredi 		BUG_ON(m->count + num > m->size);
90f8439806SMiklos Szeredi 		m->count += num;
91f8439806SMiklos Szeredi 	}
92f8439806SMiklos Szeredi }
93f8439806SMiklos Szeredi 
94839cc2a9STetsuo Handa /**
95839cc2a9STetsuo Handa  * seq_setwidth - set padding width
96839cc2a9STetsuo Handa  * @m: the seq_file handle
97839cc2a9STetsuo Handa  * @size: the max number of bytes to pad.
98839cc2a9STetsuo Handa  *
99839cc2a9STetsuo Handa  * Call seq_setwidth() for setting max width, then call seq_printf() etc. and
100839cc2a9STetsuo Handa  * finally call seq_pad() to pad the remaining bytes.
101839cc2a9STetsuo Handa  */
seq_setwidth(struct seq_file * m,size_t size)102839cc2a9STetsuo Handa static inline void seq_setwidth(struct seq_file *m, size_t size)
103839cc2a9STetsuo Handa {
104839cc2a9STetsuo Handa 	m->pad_until = m->count + size;
105839cc2a9STetsuo Handa }
106839cc2a9STetsuo Handa void seq_pad(struct seq_file *m, char c);
107839cc2a9STetsuo Handa 
1088c9379e9SAl Viro char *mangle_path(char *s, const char *p, const char *esc);
10915ad7cdcSHelge Deller int seq_open(struct file *, const struct seq_operations *);
1101da177e4SLinus Torvalds ssize_t seq_read(struct file *, char __user *, size_t, loff_t *);
111d4d50710SChristoph Hellwig ssize_t seq_read_iter(struct kiocb *iocb, struct iov_iter *iter);
1121da177e4SLinus Torvalds loff_t seq_lseek(struct file *, loff_t, int);
1131da177e4SLinus Torvalds int seq_release(struct inode *, struct file *);
1140b923606SPeter Oberparleiter int seq_write(struct seq_file *seq, const void *data, size_t len);
1151da177e4SLinus Torvalds 
1166798a8caSJoe Perches __printf(2, 0)
1176798a8caSJoe Perches void seq_vprintf(struct seq_file *m, const char *fmt, va_list args);
1186798a8caSJoe Perches __printf(2, 3)
1196798a8caSJoe Perches void seq_printf(struct seq_file *m, const char *fmt, ...);
1206798a8caSJoe Perches void seq_putc(struct seq_file *m, char c);
1216798a8caSJoe Perches void seq_puts(struct seq_file *m, const char *s);
122d1be35cbSAndrei Vagin void seq_put_decimal_ull_width(struct seq_file *m, const char *delimiter,
123d1be35cbSAndrei Vagin 			       unsigned long long num, unsigned int width);
12475ba1d07SJoe Perches void seq_put_decimal_ull(struct seq_file *m, const char *delimiter,
1256798a8caSJoe Perches 			 unsigned long long num);
12675ba1d07SJoe Perches void seq_put_decimal_ll(struct seq_file *m, const char *delimiter, long long num);
1270e3dc019SAndrei Vagin void seq_put_hex_ll(struct seq_file *m, const char *delimiter,
1280e3dc019SAndrei Vagin 		    unsigned long long v, unsigned int width);
1290e3dc019SAndrei Vagin 
1301d31aa17SAndy Shevchenko void seq_escape_mem(struct seq_file *m, const char *src, size_t len,
1311d31aa17SAndy Shevchenko 		    unsigned int flags, const char *esc);
132e7ed4a3bSAndy Shevchenko 
seq_escape_str(struct seq_file * m,const char * src,unsigned int flags,const char * esc)133e7ed4a3bSAndy Shevchenko static inline void seq_escape_str(struct seq_file *m, const char *src,
134e7ed4a3bSAndy Shevchenko 				  unsigned int flags, const char *esc)
135e7ed4a3bSAndy Shevchenko {
136e7ed4a3bSAndy Shevchenko 	seq_escape_mem(m, src, strlen(src), flags, esc);
137e7ed4a3bSAndy Shevchenko }
138e7ed4a3bSAndy Shevchenko 
139372904c0SAndy Shevchenko /**
140372904c0SAndy Shevchenko  * seq_escape - print string into buffer, escaping some characters
141372904c0SAndy Shevchenko  * @m: target buffer
142372904c0SAndy Shevchenko  * @s: NULL-terminated string
143372904c0SAndy Shevchenko  * @esc: set of characters that need escaping
144372904c0SAndy Shevchenko  *
145372904c0SAndy Shevchenko  * Puts string into buffer, replacing each occurrence of character from
146372904c0SAndy Shevchenko  * @esc with usual octal escape.
147372904c0SAndy Shevchenko  *
148372904c0SAndy Shevchenko  * Use seq_has_overflowed() to check for errors.
149372904c0SAndy Shevchenko  */
seq_escape(struct seq_file * m,const char * s,const char * esc)150372904c0SAndy Shevchenko static inline void seq_escape(struct seq_file *m, const char *s, const char *esc)
151372904c0SAndy Shevchenko {
152372904c0SAndy Shevchenko 	seq_escape_str(m, s, ESCAPE_OCTAL, esc);
153372904c0SAndy Shevchenko }
1541da177e4SLinus Torvalds 
15537607102SAndy Shevchenko void seq_hex_dump(struct seq_file *m, const char *prefix_str, int prefix_type,
15637607102SAndy Shevchenko 		  int rowsize, int groupsize, const void *buf, size_t len,
15737607102SAndy Shevchenko 		  bool ascii);
15837607102SAndy Shevchenko 
1598c9379e9SAl Viro int seq_path(struct seq_file *, const struct path *, const char *);
1602726d566SMiklos Szeredi int seq_file_path(struct seq_file *, struct file *, const char *);
1618c9379e9SAl Viro int seq_dentry(struct seq_file *, struct dentry *, const char *);
1628c9379e9SAl Viro int seq_path_root(struct seq_file *m, const struct path *path,
1638c9379e9SAl Viro 		  const struct path *root, const char *esc);
1643eda2011SLai Jiangshan 
16590b2433eSMaíra Canal void *single_start(struct seq_file *, loff_t *);
1661da177e4SLinus Torvalds int single_open(struct file *, int (*)(struct seq_file *, void *), void *);
1672043f495SAl Viro int single_open_size(struct file *, int (*)(struct seq_file *, void *), void *, size_t);
1681da177e4SLinus Torvalds int single_release(struct inode *, struct file *);
16939699037SPavel Emelyanov void *__seq_open_private(struct file *, const struct seq_operations *, int);
17039699037SPavel Emelyanov int seq_open_private(struct file *, const struct seq_operations *, int);
1711da177e4SLinus Torvalds int seq_release_private(struct inode *, struct file *);
1721da177e4SLinus Torvalds 
17376d6a133SFlorent Revest #ifdef CONFIG_BINARY_PRINTF
17476d6a133SFlorent Revest void seq_bprintf(struct seq_file *m, const char *f, const u32 *binary);
17576d6a133SFlorent Revest #endif
17676d6a133SFlorent Revest 
177d2c0e6e9SKefeng Wang #define DEFINE_SEQ_ATTRIBUTE(__name)					\
178d2c0e6e9SKefeng Wang static int __name ## _open(struct inode *inode, struct file *file)	\
179d2c0e6e9SKefeng Wang {									\
180d2c0e6e9SKefeng Wang 	int ret = seq_open(file, &__name ## _sops);			\
181d2c0e6e9SKefeng Wang 	if (!ret && inode->i_private) {					\
182d2c0e6e9SKefeng Wang 		struct seq_file *seq_f = file->private_data;		\
183d2c0e6e9SKefeng Wang 		seq_f->private = inode->i_private;			\
184d2c0e6e9SKefeng Wang 	}								\
185d2c0e6e9SKefeng Wang 	return ret;							\
186d2c0e6e9SKefeng Wang }									\
187d2c0e6e9SKefeng Wang 									\
188d2c0e6e9SKefeng Wang static const struct file_operations __name ## _fops = {			\
189d2c0e6e9SKefeng Wang 	.owner		= THIS_MODULE,					\
190d2c0e6e9SKefeng Wang 	.open		= __name ## _open,				\
191d2c0e6e9SKefeng Wang 	.read		= seq_read,					\
192d2c0e6e9SKefeng Wang 	.llseek		= seq_lseek,					\
193d2c0e6e9SKefeng Wang 	.release	= seq_release,					\
194d2c0e6e9SKefeng Wang }
195d2c0e6e9SKefeng Wang 
196a08f06bbSAndy Shevchenko #define DEFINE_SHOW_ATTRIBUTE(__name)					\
197a08f06bbSAndy Shevchenko static int __name ## _open(struct inode *inode, struct file *file)	\
198a08f06bbSAndy Shevchenko {									\
199a08f06bbSAndy Shevchenko 	return single_open(file, __name ## _show, inode->i_private);	\
200a08f06bbSAndy Shevchenko }									\
201a08f06bbSAndy Shevchenko 									\
202a08f06bbSAndy Shevchenko static const struct file_operations __name ## _fops = {			\
203a08f06bbSAndy Shevchenko 	.owner		= THIS_MODULE,					\
204a08f06bbSAndy Shevchenko 	.open		= __name ## _open,				\
205a08f06bbSAndy Shevchenko 	.read		= seq_read,					\
206a08f06bbSAndy Shevchenko 	.llseek		= seq_lseek,					\
207a08f06bbSAndy Shevchenko 	.release	= single_release,				\
208a08f06bbSAndy Shevchenko }
209a08f06bbSAndy Shevchenko 
21097a32539SAlexey Dobriyan #define DEFINE_PROC_SHOW_ATTRIBUTE(__name)				\
21197a32539SAlexey Dobriyan static int __name ## _open(struct inode *inode, struct file *file)	\
21297a32539SAlexey Dobriyan {									\
213359745d7SMuchun Song 	return single_open(file, __name ## _show, pde_data(inode));	\
21497a32539SAlexey Dobriyan }									\
21597a32539SAlexey Dobriyan 									\
21697a32539SAlexey Dobriyan static const struct proc_ops __name ## _proc_ops = {			\
21797a32539SAlexey Dobriyan 	.proc_open	= __name ## _open,				\
21897a32539SAlexey Dobriyan 	.proc_read	= seq_read,					\
21997a32539SAlexey Dobriyan 	.proc_lseek	= seq_lseek,					\
22097a32539SAlexey Dobriyan 	.proc_release	= single_release,				\
22197a32539SAlexey Dobriyan }
22297a32539SAlexey Dobriyan 
seq_user_ns(struct seq_file * seq)223adb37c4cSEric W. Biederman static inline struct user_namespace *seq_user_ns(struct seq_file *seq)
224adb37c4cSEric W. Biederman {
225adb37c4cSEric W. Biederman #ifdef CONFIG_USER_NS
22634dbbcdbSLinus Torvalds 	return seq->file->f_cred->user_ns;
227adb37c4cSEric W. Biederman #else
228adb37c4cSEric W. Biederman 	extern struct user_namespace init_user_ns;
229adb37c4cSEric W. Biederman 	return &init_user_ns;
230adb37c4cSEric W. Biederman #endif
231adb37c4cSEric W. Biederman }
232adb37c4cSEric W. Biederman 
233a068acf2SKees Cook /**
234a068acf2SKees Cook  * seq_show_options - display mount options with appropriate escapes.
235a068acf2SKees Cook  * @m: the seq_file handle
236a068acf2SKees Cook  * @name: the mount option name
237a068acf2SKees Cook  * @value: the mount option name's value, can be NULL
238a068acf2SKees Cook  */
seq_show_option(struct seq_file * m,const char * name,const char * value)239a068acf2SKees Cook static inline void seq_show_option(struct seq_file *m, const char *name,
240a068acf2SKees Cook 				   const char *value)
241a068acf2SKees Cook {
242a068acf2SKees Cook 	seq_putc(m, ',');
243a068acf2SKees Cook 	seq_escape(m, name, ",= \t\n\\");
244a068acf2SKees Cook 	if (value) {
245a068acf2SKees Cook 		seq_putc(m, '=');
246a068acf2SKees Cook 		seq_escape(m, value, ", \t\n\\");
247a068acf2SKees Cook 	}
248a068acf2SKees Cook }
249a068acf2SKees Cook 
250a068acf2SKees Cook /**
251a068acf2SKees Cook  * seq_show_option_n - display mount options with appropriate escapes
252*630fdd59SKees Cook  *		       where @value must be a specific length (i.e.
253*630fdd59SKees Cook  *		       not NUL-terminated).
254a068acf2SKees Cook  * @m: the seq_file handle
255a068acf2SKees Cook  * @name: the mount option name
256a068acf2SKees Cook  * @value: the mount option name's value, cannot be NULL
257*630fdd59SKees Cook  * @length: the exact length of @value to display, must be constant expression
258a068acf2SKees Cook  *
259a068acf2SKees Cook  * This is a macro since this uses "length" to define the size of the
260a068acf2SKees Cook  * stack buffer.
261a068acf2SKees Cook  */
262a068acf2SKees Cook #define seq_show_option_n(m, name, value, length) {	\
263a068acf2SKees Cook 	char val_buf[length + 1];			\
264*630fdd59SKees Cook 	memcpy(val_buf, value, length);			\
265a068acf2SKees Cook 	val_buf[length] = '\0';				\
266a068acf2SKees Cook 	seq_show_option(m, name, val_buf);		\
267a068acf2SKees Cook }
268a068acf2SKees Cook 
2691da177e4SLinus Torvalds #define SEQ_START_TOKEN ((void *)1)
270bcf67e16SPavel Emelianov /*
271bcf67e16SPavel Emelianov  * Helpers for iteration over list_head-s in seq_files
272bcf67e16SPavel Emelianov  */
273bcf67e16SPavel Emelianov 
274bcf67e16SPavel Emelianov extern struct list_head *seq_list_start(struct list_head *head,
275bcf67e16SPavel Emelianov 		loff_t pos);
276bcf67e16SPavel Emelianov extern struct list_head *seq_list_start_head(struct list_head *head,
277bcf67e16SPavel Emelianov 		loff_t pos);
278bcf67e16SPavel Emelianov extern struct list_head *seq_list_next(void *v, struct list_head *head,
279bcf67e16SPavel Emelianov 		loff_t *ppos);
280bcf67e16SPavel Emelianov 
281ad25f5cbSDavid Howells extern struct list_head *seq_list_start_rcu(struct list_head *head, loff_t pos);
282ad25f5cbSDavid Howells extern struct list_head *seq_list_start_head_rcu(struct list_head *head, loff_t pos);
283ad25f5cbSDavid Howells extern struct list_head *seq_list_next_rcu(void *v, struct list_head *head, loff_t *ppos);
284ad25f5cbSDavid Howells 
28566655de6SLi Zefan /*
28666655de6SLi Zefan  * Helpers for iteration over hlist_head-s in seq_files
28766655de6SLi Zefan  */
28866655de6SLi Zefan 
28966655de6SLi Zefan extern struct hlist_node *seq_hlist_start(struct hlist_head *head,
29066655de6SLi Zefan 					  loff_t pos);
29166655de6SLi Zefan extern struct hlist_node *seq_hlist_start_head(struct hlist_head *head,
29266655de6SLi Zefan 					       loff_t pos);
29366655de6SLi Zefan extern struct hlist_node *seq_hlist_next(void *v, struct hlist_head *head,
29466655de6SLi Zefan 					 loff_t *ppos);
29566655de6SLi Zefan 
2961cc52327Sstephen hemminger extern struct hlist_node *seq_hlist_start_rcu(struct hlist_head *head,
2971cc52327Sstephen hemminger 					      loff_t pos);
2981cc52327Sstephen hemminger extern struct hlist_node *seq_hlist_start_head_rcu(struct hlist_head *head,
2991cc52327Sstephen hemminger 						   loff_t pos);
3001cc52327Sstephen hemminger extern struct hlist_node *seq_hlist_next_rcu(void *v,
3011cc52327Sstephen hemminger 						   struct hlist_head *head,
3021cc52327Sstephen hemminger 						   loff_t *ppos);
3030bc77381SJeff Layton 
3040bc77381SJeff Layton /* Helpers for iterating over per-cpu hlist_head-s in seq_files */
3050bc77381SJeff Layton extern struct hlist_node *seq_hlist_start_percpu(struct hlist_head __percpu *head, int *cpu, loff_t pos);
3060bc77381SJeff Layton 
3070bc77381SJeff Layton extern struct hlist_node *seq_hlist_next_percpu(void *v, struct hlist_head __percpu *head, int *cpu, loff_t *pos);
3080bc77381SJeff Layton 
30909652320SAlexey Dobriyan void seq_file_init(void);
3101da177e4SLinus Torvalds #endif
311