blob: 953e584c94aa7f07b11efe56aefe4b95f0b6eda1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#ifndef _LINUX_SEQ_FILE_H
2#define _LINUX_SEQ_FILE_H
Linus Torvalds1da177e2005-04-16 15:20:36 -07003
4#include <linux/types.h>
5#include <linux/string.h>
Paul Gortmaker187f1882011-11-23 20:12:59 -05006#include <linux/bug.h>
Ingo Molnar0ac17592006-03-23 03:00:37 -08007#include <linux/mutex.h>
Alexey Dobriyan50ac2d62008-08-12 15:09:02 -07008#include <linux/cpumask.h>
9#include <linux/nodemask.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010
11struct seq_operations;
12struct file;
Jan Blunckc32c2f62008-02-14 19:38:43 -080013struct path;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014struct inode;
Ram Pai6092d042008-03-27 13:06:20 +010015struct dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
17struct seq_file {
18 char *buf;
19 size_t size;
20 size_t from;
21 size_t count;
Tetsuo Handa8ca5b062013-11-14 14:31:56 -080022 size_t pad_until;
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 loff_t index;
Eric Biederman8f19d472009-02-18 14:48:16 -080024 loff_t read_pos;
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -070025 u64 version;
Ingo Molnar0ac17592006-03-23 03:00:37 -080026 struct mutex lock;
Helge Deller15ad7cd2006-12-06 20:40:36 -080027 const struct seq_operations *op;
Kay Sieversf1514632011-07-12 20:48:39 +020028 int poll_event;
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 void *private;
30};
31
32struct 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
Al Viro521b5d02008-03-28 00:46:41 -040039#define SEQ_SKIP 1
40
Miklos Szeredif8439802009-09-21 14:48:36 +020041/**
42 * seq_get_buf - get buffer to write arbitrary data to
43 * @m: the seq_file handle
44 * @bufp: the beginning of the buffer is stored here
45 *
46 * Return the number of bytes available in the buffer, or zero if
47 * there's no space.
48 */
49static inline size_t seq_get_buf(struct seq_file *m, char **bufp)
50{
51 BUG_ON(m->count > m->size);
52 if (m->count < m->size)
53 *bufp = m->buf + m->count;
54 else
55 *bufp = NULL;
56
57 return m->size - m->count;
58}
59
60/**
61 * seq_commit - commit data to the buffer
62 * @m: the seq_file handle
63 * @num: the number of bytes to commit
64 *
65 * Commit @num bytes of data written to a buffer previously acquired
66 * by seq_buf_get. To signal an error condition, or that the data
67 * didn't fit in the available space, pass a negative @num value.
68 */
69static inline void seq_commit(struct seq_file *m, int num)
70{
71 if (num < 0) {
72 m->count = m->size;
73 } else {
74 BUG_ON(m->count + num > m->size);
75 m->count += num;
76 }
77}
78
Tetsuo Handa8ca5b062013-11-14 14:31:56 -080079/**
80 * seq_setwidth - set padding width
81 * @m: the seq_file handle
82 * @size: the max number of bytes to pad.
83 *
84 * Call seq_setwidth() for setting max width, then call seq_printf() etc. and
85 * finally call seq_pad() to pad the remaining bytes.
86 */
87static inline void seq_setwidth(struct seq_file *m, size_t size)
88{
89 m->pad_until = m->count + size;
90}
91void seq_pad(struct seq_file *m, char c);
92
Al Viro8c9379e2011-12-08 20:18:57 -050093char *mangle_path(char *s, const char *p, const char *esc);
Helge Deller15ad7cd2006-12-06 20:40:36 -080094int seq_open(struct file *, const struct seq_operations *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095ssize_t seq_read(struct file *, char __user *, size_t, loff_t *);
96loff_t seq_lseek(struct file *, loff_t, int);
97int seq_release(struct inode *, struct file *);
98int seq_escape(struct seq_file *, const char *, const char *);
99int seq_putc(struct seq_file *m, char c);
100int seq_puts(struct seq_file *m, const char *s);
Peter Oberparleiter0b923602009-06-17 16:28:05 -0700101int seq_write(struct seq_file *seq, const void *data, size_t len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Joe Perchesb9075fa2011-10-31 17:11:33 -0700103__printf(2, 3) int seq_printf(struct seq_file *, const char *, ...);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Al Viro8c9379e2011-12-08 20:18:57 -0500105int seq_path(struct seq_file *, const struct path *, const char *);
106int seq_dentry(struct seq_file *, struct dentry *, const char *);
107int seq_path_root(struct seq_file *m, const struct path *path,
108 const struct path *root, const char *esc);
Rusty Russellcb78a0c2008-12-30 09:05:14 +1030109int seq_bitmap(struct seq_file *m, const unsigned long *bits,
110 unsigned int nr_bits);
111static inline int seq_cpumask(struct seq_file *m, const struct cpumask *mask)
Alexey Dobriyan50ac2d62008-08-12 15:09:02 -0700112{
Rusty Russellaf76aba2009-03-30 22:05:11 -0600113 return seq_bitmap(m, cpumask_bits(mask), nr_cpu_ids);
Alexey Dobriyan50ac2d62008-08-12 15:09:02 -0700114}
115
116static inline int seq_nodemask(struct seq_file *m, nodemask_t *mask)
117{
118 return seq_bitmap(m, mask->bits, MAX_NUMNODES);
119}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Rusty Russellaf76aba2009-03-30 22:05:11 -0600121int seq_bitmap_list(struct seq_file *m, const unsigned long *bits,
Lai Jiangshan3eda2012008-10-18 20:28:19 -0700122 unsigned int nr_bits);
123
Rusty Russellaf76aba2009-03-30 22:05:11 -0600124static inline int seq_cpumask_list(struct seq_file *m,
125 const struct cpumask *mask)
Lai Jiangshan3eda2012008-10-18 20:28:19 -0700126{
Rusty Russellaf76aba2009-03-30 22:05:11 -0600127 return seq_bitmap_list(m, cpumask_bits(mask), nr_cpu_ids);
Lai Jiangshan3eda2012008-10-18 20:28:19 -0700128}
129
130static inline int seq_nodemask_list(struct seq_file *m, nodemask_t *mask)
131{
132 return seq_bitmap_list(m, mask->bits, MAX_NUMNODES);
133}
134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135int single_open(struct file *, int (*)(struct seq_file *, void *), void *);
136int single_release(struct inode *, struct file *);
Pavel Emelyanov39699032007-10-10 02:28:42 -0700137void *__seq_open_private(struct file *, const struct seq_operations *, int);
138int seq_open_private(struct file *, const struct seq_operations *, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139int seq_release_private(struct inode *, struct file *);
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700140int seq_put_decimal_ull(struct seq_file *m, char delimiter,
141 unsigned long long num);
KAMEZAWA Hiroyukibda7bad2012-03-23 15:02:54 -0700142int seq_put_decimal_ll(struct seq_file *m, char delimiter,
143 long long num);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
flintman9892c402015-09-23 06:14:10 -0400145static inline struct user_namespace *seq_user_ns(struct seq_file *seq)
146{
147#ifdef CONFIG_USER_NS
148 return seq->user_ns;
149#else
150 extern struct user_namespace init_user_ns;
151 return &init_user_ns;
152#endif
William Bellavance14deb7a2016-05-17 10:56:05 -0400153}
Ivan Grinko766ce4e2016-04-28 22:06:41 +0300154
155/**
156 * seq_show_options - display mount options with appropriate escapes.
157 * @m: the seq_file handle
158 * @name: the mount option name
159 * @value: the mount option name's value, can be NULL
160 */
161static inline void seq_show_option(struct seq_file *m, const char *name,
162 const char *value)
163{
164 seq_putc(m, ',');
165 seq_escape(m, name, ",= \t\n\\");
166 if (value) {
167 seq_putc(m, '=');
168 seq_escape(m, value, ", \t\n\\");
169 }
170}
171
172/**
173 * seq_show_option_n - display mount options with appropriate escapes
174 * where @value must be a specific length.
175 * @m: the seq_file handle
176 * @name: the mount option name
177 * @value: the mount option name's value, cannot be NULL
178 * @length: the length of @value to display
179 *
180 * This is a macro since this uses "length" to define the size of the
181 * stack buffer.
182 */
183#define seq_show_option_n(m, name, value, length) { \
184 char val_buf[length + 1]; \
185 strncpy(val_buf, value, length); \
186 val_buf[length] = '\0'; \
187 seq_show_option(m, name, val_buf); \
flintman9892c402015-09-23 06:14:10 -0400188}
189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190#define SEQ_START_TOKEN ((void *)1)
Pavel Emelianovbcf67e12007-07-10 17:22:26 -0700191/*
192 * Helpers for iteration over list_head-s in seq_files
193 */
194
195extern struct list_head *seq_list_start(struct list_head *head,
196 loff_t pos);
197extern struct list_head *seq_list_start_head(struct list_head *head,
198 loff_t pos);
199extern struct list_head *seq_list_next(void *v, struct list_head *head,
200 loff_t *ppos);
201
Li Zefan66655de2010-02-08 23:18:22 +0000202/*
203 * Helpers for iteration over hlist_head-s in seq_files
204 */
205
206extern struct hlist_node *seq_hlist_start(struct hlist_head *head,
stephen hemminger1cc52322010-02-22 07:57:17 +0000207 loff_t pos);
Li Zefan66655de2010-02-08 23:18:22 +0000208extern struct hlist_node *seq_hlist_start_head(struct hlist_head *head,
stephen hemminger1cc52322010-02-22 07:57:17 +0000209 loff_t pos);
Li Zefan66655de2010-02-08 23:18:22 +0000210extern struct hlist_node *seq_hlist_next(void *v, struct hlist_head *head,
stephen hemminger1cc52322010-02-22 07:57:17 +0000211 loff_t *ppos);
Li Zefan66655de2010-02-08 23:18:22 +0000212
stephen hemminger1cc52322010-02-22 07:57:17 +0000213extern struct hlist_node *seq_hlist_start_rcu(struct hlist_head *head,
214 loff_t pos);
215extern struct hlist_node *seq_hlist_start_head_rcu(struct hlist_head *head,
216 loff_t pos);
217extern struct hlist_node *seq_hlist_next_rcu(void *v,
218 struct hlist_head *head,
219 loff_t *ppos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220#endif