blob: 00bbe2bfc6345ec61b3daf04fc52e3e60c2f6348 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/seq_file.c
3 *
4 * helper functions for making synthetic files from sequences of records.
5 * initial implementation -- AV, Oct 2001.
6 */
7
8#include <linux/fs.h>
Paul Gortmaker630d9c42011-11-16 23:57:37 -05009#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/seq_file.h>
Heiko Carstens058504e2014-07-02 15:22:37 -070011#include <linux/vmalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/slab.h>
Eric W. Biedermanadb37c42012-05-23 18:01:20 -060013#include <linux/cred.h>
Heiko Carstens058504e2014-07-02 15:22:37 -070014#include <linux/mm.h>
Andy Shevchenko37607102015-09-09 15:38:33 -070015#include <linux/printk.h>
Andy Shevchenko25c6bb72015-11-06 16:32:40 -080016#include <linux/string_helpers.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
18#include <asm/uaccess.h>
19#include <asm/page.h>
20
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -070021static void seq_set_overflow(struct seq_file *m)
22{
23 m->count = m->size;
24}
25
Heiko Carstens058504e2014-07-02 15:22:37 -070026static void *seq_buf_alloc(unsigned long size)
27{
28 void *buf;
29
David Rientjes5cec38a2014-12-12 16:56:16 -080030 /*
31 * __GFP_NORETRY to avoid oom-killings with high-order allocations -
32 * it's better to fall back to vmalloc() than to kill things.
33 */
34 buf = kmalloc(size, GFP_KERNEL | __GFP_NORETRY | __GFP_NOWARN);
Heiko Carstens058504e2014-07-02 15:22:37 -070035 if (!buf && size > PAGE_SIZE)
36 buf = vmalloc(size);
37 return buf;
38}
39
Linus Torvalds1da177e2005-04-16 15:20:36 -070040/**
41 * seq_open - initialize sequential file
42 * @file: file we initialize
43 * @op: method table describing the sequence
44 *
45 * seq_open() sets @file, associating it with a sequence described
46 * by @op. @op->start() sets the iterator up and returns the first
47 * element of sequence. @op->stop() shuts it down. @op->next()
48 * returns the next element of sequence. @op->show() prints element
49 * into the buffer. In case of error ->start() and ->next() return
50 * ERR_PTR(error). In the end of sequence they return %NULL. ->show()
51 * returns 0 in case of success and negative number in case of error.
Al Viro521b5d02008-03-28 00:46:41 -040052 * Returning SEQ_SKIP means "discard this element and move on".
Yann Droneaud460b8652015-06-30 14:57:36 -070053 * Note: seq_open() will allocate a struct seq_file and store its
54 * pointer in @file->private_data. This pointer should not be modified.
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 */
Helge Deller15ad7cd2006-12-06 20:40:36 -080056int seq_open(struct file *file, const struct seq_operations *op)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057{
Yann Droneaud189f9842015-06-30 14:57:33 -070058 struct seq_file *p;
Al Viro1abe77b2005-11-07 17:15:34 -050059
Yann Droneaud189f9842015-06-30 14:57:33 -070060 WARN_ON(file->private_data);
61
62 p = kzalloc(sizeof(*p), GFP_KERNEL);
63 if (!p)
64 return -ENOMEM;
65
66 file->private_data = p;
67
Ingo Molnar0ac17592006-03-23 03:00:37 -080068 mutex_init(&p->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 p->op = op;
Eric W. Biedermanadb37c42012-05-23 18:01:20 -060070#ifdef CONFIG_USER_NS
71 p->user_ns = file->f_cred->user_ns;
72#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74 /*
75 * Wrappers around seq_open(e.g. swaps_open) need to be
76 * aware of this. If they set f_version themselves, they
77 * should call seq_open first and then set f_version.
78 */
79 file->f_version = 0;
80
Eric Biederman8f19d472009-02-18 14:48:16 -080081 /*
82 * seq_files support lseek() and pread(). They do not implement
83 * write() at all, but we clear FMODE_PWRITE here for historical
84 * reasons.
85 *
86 * If a client of seq_files a) implements file.write() and b) wishes to
87 * support pwrite() then that client will need to implement its own
88 * file.open() which calls seq_open() and then sets FMODE_PWRITE.
89 */
90 file->f_mode &= ~FMODE_PWRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 return 0;
92}
93EXPORT_SYMBOL(seq_open);
94
Eric Biederman33da8892009-02-04 15:12:25 -080095static int traverse(struct seq_file *m, loff_t offset)
96{
97 loff_t pos = 0, index;
98 int error = 0;
99 void *p;
100
101 m->version = 0;
102 index = 0;
103 m->count = m->from = 0;
104 if (!offset) {
105 m->index = index;
106 return 0;
107 }
108 if (!m->buf) {
Heiko Carstens058504e2014-07-02 15:22:37 -0700109 m->buf = seq_buf_alloc(m->size = PAGE_SIZE);
Eric Biederman33da8892009-02-04 15:12:25 -0800110 if (!m->buf)
111 return -ENOMEM;
112 }
113 p = m->op->start(m, &index);
114 while (p) {
115 error = PTR_ERR(p);
116 if (IS_ERR(p))
117 break;
118 error = m->op->show(m, p);
119 if (error < 0)
120 break;
121 if (unlikely(error)) {
122 error = 0;
123 m->count = 0;
124 }
Joe Perches1f33c412014-09-29 16:08:21 -0700125 if (seq_has_overflowed(m))
Eric Biederman33da8892009-02-04 15:12:25 -0800126 goto Eoverflow;
127 if (pos + m->count > offset) {
128 m->from = offset - pos;
129 m->count -= m->from;
130 m->index = index;
131 break;
132 }
133 pos += m->count;
134 m->count = 0;
135 if (pos == offset) {
136 index++;
137 m->index = index;
138 break;
139 }
140 p = m->op->next(m, p, &index);
141 }
142 m->op->stop(m, p);
Alexey Dobriyanf01d1d52009-02-06 00:30:05 +0300143 m->index = index;
Eric Biederman33da8892009-02-04 15:12:25 -0800144 return error;
145
146Eoverflow:
147 m->op->stop(m, p);
Heiko Carstens058504e2014-07-02 15:22:37 -0700148 kvfree(m->buf);
Al Viro801a7602013-11-19 01:20:43 +0000149 m->count = 0;
Heiko Carstens058504e2014-07-02 15:22:37 -0700150 m->buf = seq_buf_alloc(m->size <<= 1);
Eric Biederman33da8892009-02-04 15:12:25 -0800151 return !m->buf ? -ENOMEM : -EAGAIN;
152}
153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154/**
155 * seq_read - ->read() method for sequential files.
Martin Waitz67be2dd2005-05-01 08:59:26 -0700156 * @file: the file to read from
157 * @buf: the buffer to read to
158 * @size: the maximum number of bytes to read
159 * @ppos: the current position in the file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 *
161 * Ready-made ->f_op->read()
162 */
163ssize_t seq_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
164{
Joe Perches8209e2f2010-09-04 18:52:49 -0700165 struct seq_file *m = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 size_t copied = 0;
167 loff_t pos;
168 size_t n;
169 void *p;
170 int err = 0;
171
Ingo Molnar0ac17592006-03-23 03:00:37 -0800172 mutex_lock(&m->lock);
Eric Biederman8f19d472009-02-18 14:48:16 -0800173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 /*
175 * seq_file->op->..m_start/m_stop/m_next may do special actions
176 * or optimisations based on the file->f_version, so we want to
177 * pass the file->f_version to those methods.
178 *
179 * seq_file->version is just copy of f_version, and seq_file
180 * methods can treat it simply as file version.
181 * It is copied in first and copied out after all operations.
182 * It is convenient to have it as part of structure to avoid the
183 * need of passing another argument to all the seq_file methods.
184 */
185 m->version = file->f_version;
Earl Chew7904ac82012-03-21 16:33:43 -0700186
187 /* Don't assume *ppos is where we left it */
188 if (unlikely(*ppos != m->read_pos)) {
189 while ((err = traverse(m, *ppos)) == -EAGAIN)
190 ;
191 if (err) {
192 /* With prejudice... */
193 m->read_pos = 0;
194 m->version = 0;
195 m->index = 0;
196 m->count = 0;
197 goto Done;
198 } else {
199 m->read_pos = *ppos;
200 }
201 }
202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 /* grab buffer if we didn't have one */
204 if (!m->buf) {
Heiko Carstens058504e2014-07-02 15:22:37 -0700205 m->buf = seq_buf_alloc(m->size = PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 if (!m->buf)
207 goto Enomem;
208 }
209 /* if not empty - flush it first */
210 if (m->count) {
211 n = min(m->count, size);
212 err = copy_to_user(buf, m->buf + m->from, n);
213 if (err)
214 goto Efault;
215 m->count -= n;
216 m->from += n;
217 size -= n;
218 buf += n;
219 copied += n;
220 if (!m->count)
221 m->index++;
222 if (!size)
223 goto Done;
224 }
225 /* we need at least one record in buffer */
Al Viro4cdfe842008-08-24 07:45:33 -0400226 pos = m->index;
227 p = m->op->start(m, &pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 while (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 err = PTR_ERR(p);
230 if (!p || IS_ERR(p))
231 break;
232 err = m->op->show(m, p);
Al Viro521b5d02008-03-28 00:46:41 -0400233 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 break;
Al Viro521b5d02008-03-28 00:46:41 -0400235 if (unlikely(err))
236 m->count = 0;
Al Viro4cdfe842008-08-24 07:45:33 -0400237 if (unlikely(!m->count)) {
238 p = m->op->next(m, p, &pos);
239 m->index = pos;
240 continue;
241 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 if (m->count < m->size)
243 goto Fill;
244 m->op->stop(m, p);
Heiko Carstens058504e2014-07-02 15:22:37 -0700245 kvfree(m->buf);
Al Viro801a7602013-11-19 01:20:43 +0000246 m->count = 0;
Heiko Carstens058504e2014-07-02 15:22:37 -0700247 m->buf = seq_buf_alloc(m->size <<= 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 if (!m->buf)
249 goto Enomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 m->version = 0;
Al Viro4cdfe842008-08-24 07:45:33 -0400251 pos = m->index;
252 p = m->op->start(m, &pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 }
254 m->op->stop(m, p);
255 m->count = 0;
256 goto Done;
257Fill:
258 /* they want more? let's try to get some more */
259 while (m->count < size) {
260 size_t offs = m->count;
261 loff_t next = pos;
262 p = m->op->next(m, p, &next);
263 if (!p || IS_ERR(p)) {
264 err = PTR_ERR(p);
265 break;
266 }
267 err = m->op->show(m, p);
Joe Perches1f33c412014-09-29 16:08:21 -0700268 if (seq_has_overflowed(m) || err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 m->count = offs;
Al Viro521b5d02008-03-28 00:46:41 -0400270 if (likely(err <= 0))
271 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 }
273 pos = next;
274 }
275 m->op->stop(m, p);
276 n = min(m->count, size);
277 err = copy_to_user(buf, m->buf, n);
278 if (err)
279 goto Efault;
280 copied += n;
281 m->count -= n;
282 if (m->count)
283 m->from = n;
284 else
285 pos++;
286 m->index = pos;
287Done:
288 if (!copied)
289 copied = err;
Eric Biederman8f19d472009-02-18 14:48:16 -0800290 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 *ppos += copied;
Eric Biederman8f19d472009-02-18 14:48:16 -0800292 m->read_pos += copied;
293 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 file->f_version = m->version;
Ingo Molnar0ac17592006-03-23 03:00:37 -0800295 mutex_unlock(&m->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 return copied;
297Enomem:
298 err = -ENOMEM;
299 goto Done;
300Efault:
301 err = -EFAULT;
302 goto Done;
303}
304EXPORT_SYMBOL(seq_read);
305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306/**
307 * seq_lseek - ->llseek() method for sequential files.
Martin Waitz67be2dd2005-05-01 08:59:26 -0700308 * @file: the file in question
309 * @offset: new position
Randy Dunlap254adaa2013-01-09 17:13:00 -0800310 * @whence: 0 for absolute, 1 for relative position
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 *
312 * Ready-made ->f_op->llseek()
313 */
Andrew Morton965c8e52012-12-17 15:59:39 -0800314loff_t seq_lseek(struct file *file, loff_t offset, int whence)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{
Joe Perches8209e2f2010-09-04 18:52:49 -0700316 struct seq_file *m = file->private_data;
David Sterba16abef02008-04-22 15:09:22 +0200317 loff_t retval = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
Ingo Molnar0ac17592006-03-23 03:00:37 -0800319 mutex_lock(&m->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 m->version = file->f_version;
Andrew Morton965c8e52012-12-17 15:59:39 -0800321 switch (whence) {
Andrew Morton5e62ade2013-02-27 17:03:22 -0800322 case SEEK_CUR:
323 offset += file->f_pos;
324 case SEEK_SET:
325 if (offset < 0)
326 break;
327 retval = offset;
328 if (offset != m->read_pos) {
329 while ((retval = traverse(m, offset)) == -EAGAIN)
330 ;
331 if (retval) {
332 /* with extreme prejudice... */
333 file->f_pos = 0;
334 m->read_pos = 0;
335 m->version = 0;
336 m->index = 0;
337 m->count = 0;
338 } else {
339 m->read_pos = offset;
340 retval = file->f_pos = offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 }
Gu Zheng05e16742013-10-25 18:15:06 +0800342 } else {
343 file->f_pos = offset;
Andrew Morton5e62ade2013-02-27 17:03:22 -0800344 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 file->f_version = m->version;
Alexey Dobriyan00c57462007-07-15 23:40:22 -0700347 mutex_unlock(&m->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 return retval;
349}
350EXPORT_SYMBOL(seq_lseek);
351
352/**
353 * seq_release - free the structures associated with sequential file.
354 * @file: file in question
Al Viro6131ffa2013-02-27 16:59:05 -0500355 * @inode: its inode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 *
357 * Frees the structures associated with sequential file; can be used
358 * as ->f_op->release() if you don't have private data to destroy.
359 */
360int seq_release(struct inode *inode, struct file *file)
361{
Joe Perches8209e2f2010-09-04 18:52:49 -0700362 struct seq_file *m = file->private_data;
Heiko Carstens058504e2014-07-02 15:22:37 -0700363 kvfree(m->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 kfree(m);
365 return 0;
366}
367EXPORT_SYMBOL(seq_release);
368
369/**
370 * seq_escape - print string into buffer, escaping some characters
371 * @m: target buffer
372 * @s: string
373 * @esc: set of characters that need escaping
374 *
375 * Puts string into buffer, replacing each occurrence of character from
Joe Perches6798a8c2015-09-11 13:07:48 -0700376 * @esc with usual octal escape.
377 * Use seq_has_overflowed() to check for errors.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 */
Joe Perches6798a8c2015-09-11 13:07:48 -0700379void seq_escape(struct seq_file *m, const char *s, const char *esc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380{
Andy Shevchenko25c6bb72015-11-06 16:32:40 -0800381 char *buf;
382 size_t size = seq_get_buf(m, &buf);
383 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
Andy Shevchenko25c6bb72015-11-06 16:32:40 -0800385 ret = string_escape_str(s, buf, size, ESCAPE_OCTAL, esc);
386 seq_commit(m, ret < size ? ret : -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387}
388EXPORT_SYMBOL(seq_escape);
389
Joe Perches6798a8c2015-09-11 13:07:48 -0700390void seq_vprintf(struct seq_file *m, const char *f, va_list args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 int len;
393
394 if (m->count < m->size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 len = vsnprintf(m->buf + m->count, m->size - m->count, f, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 if (m->count + len < m->size) {
397 m->count += len;
Joe Perches6798a8c2015-09-11 13:07:48 -0700398 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 }
400 }
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -0700401 seq_set_overflow(m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402}
Steven Whitehousea4808142012-06-11 13:16:35 +0100403EXPORT_SYMBOL(seq_vprintf);
404
Joe Perches6798a8c2015-09-11 13:07:48 -0700405void seq_printf(struct seq_file *m, const char *f, ...)
Steven Whitehousea4808142012-06-11 13:16:35 +0100406{
Steven Whitehousea4808142012-06-11 13:16:35 +0100407 va_list args;
408
409 va_start(args, f);
Joe Perches6798a8c2015-09-11 13:07:48 -0700410 seq_vprintf(m, f, args);
Steven Whitehousea4808142012-06-11 13:16:35 +0100411 va_end(args);
Steven Whitehousea4808142012-06-11 13:16:35 +0100412}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413EXPORT_SYMBOL(seq_printf);
414
Török Edwin74e2f332008-11-22 13:28:48 +0200415/**
Török Edwin958086d2008-11-23 23:24:53 +0200416 * mangle_path - mangle and copy path to buffer beginning
417 * @s: buffer start
418 * @p: beginning of path in above buffer
419 * @esc: set of characters that need escaping
Török Edwin74e2f332008-11-22 13:28:48 +0200420 *
421 * Copy the path from @p to @s, replacing each occurrence of character from
422 * @esc with usual octal escape.
423 * Returns pointer past last written character in @s, or NULL in case of
424 * failure.
425 */
Al Viro8c9379e2011-12-08 20:18:57 -0500426char *mangle_path(char *s, const char *p, const char *esc)
Ram Pai6092d042008-03-27 13:06:20 +0100427{
428 while (s <= p) {
429 char c = *p++;
430 if (!c) {
431 return s;
432 } else if (!strchr(esc, c)) {
433 *s++ = c;
434 } else if (s + 4 > p) {
435 break;
436 } else {
437 *s++ = '\\';
438 *s++ = '0' + ((c & 0300) >> 6);
439 *s++ = '0' + ((c & 070) >> 3);
440 *s++ = '0' + (c & 07);
441 }
442 }
443 return NULL;
444}
Ingo Molnar604094f2008-11-28 18:03:22 +0100445EXPORT_SYMBOL(mangle_path);
Ram Pai6092d042008-03-27 13:06:20 +0100446
Arjan van de Ven52afeef2008-12-01 14:35:00 -0800447/**
448 * seq_path - seq_file interface to print a pathname
449 * @m: the seq_file handle
450 * @path: the struct path to print
451 * @esc: set of characters to escape in the output
452 *
453 * return the absolute path of 'path', as represented by the
454 * dentry / mnt pair in the path parameter.
Ram Pai6092d042008-03-27 13:06:20 +0100455 */
Al Viro8c9379e2011-12-08 20:18:57 -0500456int seq_path(struct seq_file *m, const struct path *path, const char *esc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457{
Miklos Szeredif8439802009-09-21 14:48:36 +0200458 char *buf;
459 size_t size = seq_get_buf(m, &buf);
460 int res = -1;
461
462 if (size) {
463 char *p = d_path(path, buf, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 if (!IS_ERR(p)) {
Miklos Szeredif8439802009-09-21 14:48:36 +0200465 char *end = mangle_path(buf, p, esc);
466 if (end)
467 res = end - buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 }
469 }
Miklos Szeredif8439802009-09-21 14:48:36 +0200470 seq_commit(m, res);
471
472 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473}
474EXPORT_SYMBOL(seq_path);
475
Miklos Szeredi2726d562015-06-19 10:30:28 +0200476/**
477 * seq_file_path - seq_file interface to print a pathname of a file
478 * @m: the seq_file handle
479 * @file: the struct file to print
480 * @esc: set of characters to escape in the output
481 *
482 * return the absolute path to the file.
483 */
484int seq_file_path(struct seq_file *m, struct file *file, const char *esc)
485{
486 return seq_path(m, &file->f_path, esc);
487}
488EXPORT_SYMBOL(seq_file_path);
489
Ram Pai6092d042008-03-27 13:06:20 +0100490/*
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100491 * Same as seq_path, but relative to supplied root.
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100492 */
Al Viro8c9379e2011-12-08 20:18:57 -0500493int seq_path_root(struct seq_file *m, const struct path *path,
494 const struct path *root, const char *esc)
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100495{
Miklos Szeredif8439802009-09-21 14:48:36 +0200496 char *buf;
497 size_t size = seq_get_buf(m, &buf);
498 int res = -ENAMETOOLONG;
499
500 if (size) {
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100501 char *p;
502
Miklos Szeredif8439802009-09-21 14:48:36 +0200503 p = __d_path(path, root, buf, size);
Al Viro02125a82011-12-05 08:43:34 -0500504 if (!p)
505 return SEQ_SKIP;
Miklos Szeredif8439802009-09-21 14:48:36 +0200506 res = PTR_ERR(p);
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100507 if (!IS_ERR(p)) {
Miklos Szeredif8439802009-09-21 14:48:36 +0200508 char *end = mangle_path(buf, p, esc);
509 if (end)
510 res = end - buf;
511 else
512 res = -ENAMETOOLONG;
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100513 }
514 }
Miklos Szeredif8439802009-09-21 14:48:36 +0200515 seq_commit(m, res);
516
Al Viro02125a82011-12-05 08:43:34 -0500517 return res < 0 && res != -ENAMETOOLONG ? res : 0;
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100518}
519
520/*
Ram Pai6092d042008-03-27 13:06:20 +0100521 * returns the path of the 'dentry' from the root of its filesystem.
522 */
Al Viro8c9379e2011-12-08 20:18:57 -0500523int seq_dentry(struct seq_file *m, struct dentry *dentry, const char *esc)
Ram Pai6092d042008-03-27 13:06:20 +0100524{
Miklos Szeredif8439802009-09-21 14:48:36 +0200525 char *buf;
526 size_t size = seq_get_buf(m, &buf);
527 int res = -1;
528
529 if (size) {
530 char *p = dentry_path(dentry, buf, size);
Ram Pai6092d042008-03-27 13:06:20 +0100531 if (!IS_ERR(p)) {
Miklos Szeredif8439802009-09-21 14:48:36 +0200532 char *end = mangle_path(buf, p, esc);
533 if (end)
534 res = end - buf;
Ram Pai6092d042008-03-27 13:06:20 +0100535 }
536 }
Miklos Szeredif8439802009-09-21 14:48:36 +0200537 seq_commit(m, res);
538
539 return res;
Ram Pai6092d042008-03-27 13:06:20 +0100540}
Omar Sandovalc8d3fe02015-05-18 02:16:31 -0700541EXPORT_SYMBOL(seq_dentry);
Ram Pai6092d042008-03-27 13:06:20 +0100542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543static void *single_start(struct seq_file *p, loff_t *pos)
544{
545 return NULL + (*pos == 0);
546}
547
548static void *single_next(struct seq_file *p, void *v, loff_t *pos)
549{
550 ++*pos;
551 return NULL;
552}
553
554static void single_stop(struct seq_file *p, void *v)
555{
556}
557
558int single_open(struct file *file, int (*show)(struct seq_file *, void *),
559 void *data)
560{
561 struct seq_operations *op = kmalloc(sizeof(*op), GFP_KERNEL);
562 int res = -ENOMEM;
563
564 if (op) {
565 op->start = single_start;
566 op->next = single_next;
567 op->stop = single_stop;
568 op->show = show;
569 res = seq_open(file, op);
570 if (!res)
571 ((struct seq_file *)file->private_data)->private = data;
572 else
573 kfree(op);
574 }
575 return res;
576}
577EXPORT_SYMBOL(single_open);
578
Al Viro2043f492013-03-31 13:43:23 -0400579int single_open_size(struct file *file, int (*show)(struct seq_file *, void *),
580 void *data, size_t size)
581{
Heiko Carstens058504e2014-07-02 15:22:37 -0700582 char *buf = seq_buf_alloc(size);
Al Viro2043f492013-03-31 13:43:23 -0400583 int ret;
584 if (!buf)
585 return -ENOMEM;
586 ret = single_open(file, show, data);
587 if (ret) {
Heiko Carstens058504e2014-07-02 15:22:37 -0700588 kvfree(buf);
Al Viro2043f492013-03-31 13:43:23 -0400589 return ret;
590 }
591 ((struct seq_file *)file->private_data)->buf = buf;
592 ((struct seq_file *)file->private_data)->size = size;
593 return 0;
594}
595EXPORT_SYMBOL(single_open_size);
596
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597int single_release(struct inode *inode, struct file *file)
598{
Helge Deller15ad7cd2006-12-06 20:40:36 -0800599 const struct seq_operations *op = ((struct seq_file *)file->private_data)->op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 int res = seq_release(inode, file);
601 kfree(op);
602 return res;
603}
604EXPORT_SYMBOL(single_release);
605
606int seq_release_private(struct inode *inode, struct file *file)
607{
608 struct seq_file *seq = file->private_data;
609
610 kfree(seq->private);
611 seq->private = NULL;
612 return seq_release(inode, file);
613}
614EXPORT_SYMBOL(seq_release_private);
615
Pavel Emelyanov39699032007-10-10 02:28:42 -0700616void *__seq_open_private(struct file *f, const struct seq_operations *ops,
617 int psize)
618{
619 int rc;
620 void *private;
621 struct seq_file *seq;
622
623 private = kzalloc(psize, GFP_KERNEL);
624 if (private == NULL)
625 goto out;
626
627 rc = seq_open(f, ops);
628 if (rc < 0)
629 goto out_free;
630
631 seq = f->private_data;
632 seq->private = private;
633 return private;
634
635out_free:
636 kfree(private);
637out:
638 return NULL;
639}
640EXPORT_SYMBOL(__seq_open_private);
641
642int seq_open_private(struct file *filp, const struct seq_operations *ops,
643 int psize)
644{
645 return __seq_open_private(filp, ops, psize) ? 0 : -ENOMEM;
646}
647EXPORT_SYMBOL(seq_open_private);
648
Joe Perches6798a8c2015-09-11 13:07:48 -0700649void seq_putc(struct seq_file *m, char c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650{
Joe Perches6798a8c2015-09-11 13:07:48 -0700651 if (m->count >= m->size)
652 return;
653
654 m->buf[m->count++] = c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655}
656EXPORT_SYMBOL(seq_putc);
657
Joe Perches6798a8c2015-09-11 13:07:48 -0700658void seq_puts(struct seq_file *m, const char *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659{
660 int len = strlen(s);
Joe Perches6798a8c2015-09-11 13:07:48 -0700661
662 if (m->count + len >= m->size) {
663 seq_set_overflow(m);
664 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 }
Joe Perches6798a8c2015-09-11 13:07:48 -0700666 memcpy(m->buf + m->count, s, len);
667 m->count += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668}
669EXPORT_SYMBOL(seq_puts);
Pavel Emelianovbcf67e12007-07-10 17:22:26 -0700670
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700671/*
672 * A helper routine for putting decimal numbers without rich format of printf().
673 * only 'unsigned long long' is supported.
674 * This routine will put one byte delimiter + number into seq_file.
675 * This routine is very quick when you show lots of numbers.
676 * In usual cases, it will be better to use seq_printf(). It's easier to read.
677 */
Joe Perches6798a8c2015-09-11 13:07:48 -0700678void seq_put_decimal_ull(struct seq_file *m, char delimiter,
679 unsigned long long num)
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700680{
681 int len;
682
683 if (m->count + 2 >= m->size) /* we'll write 2 bytes at least */
684 goto overflow;
685
KAMEZAWA Hiroyukibda7bad2012-03-23 15:02:54 -0700686 if (delimiter)
687 m->buf[m->count++] = delimiter;
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700688
689 if (num < 10) {
690 m->buf[m->count++] = num + '0';
Joe Perches6798a8c2015-09-11 13:07:48 -0700691 return;
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700692 }
693
694 len = num_to_str(m->buf + m->count, m->size - m->count, num);
695 if (!len)
696 goto overflow;
697 m->count += len;
Joe Perches6798a8c2015-09-11 13:07:48 -0700698 return;
699
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700700overflow:
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -0700701 seq_set_overflow(m);
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700702}
703EXPORT_SYMBOL(seq_put_decimal_ull);
704
Joe Perches6798a8c2015-09-11 13:07:48 -0700705void seq_put_decimal_ll(struct seq_file *m, char delimiter, long long num)
KAMEZAWA Hiroyukibda7bad2012-03-23 15:02:54 -0700706{
707 if (num < 0) {
708 if (m->count + 3 >= m->size) {
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -0700709 seq_set_overflow(m);
Joe Perches6798a8c2015-09-11 13:07:48 -0700710 return;
KAMEZAWA Hiroyukibda7bad2012-03-23 15:02:54 -0700711 }
712 if (delimiter)
713 m->buf[m->count++] = delimiter;
714 num = -num;
715 delimiter = '-';
716 }
Joe Perches6798a8c2015-09-11 13:07:48 -0700717 seq_put_decimal_ull(m, delimiter, num);
KAMEZAWA Hiroyukibda7bad2012-03-23 15:02:54 -0700718}
719EXPORT_SYMBOL(seq_put_decimal_ll);
720
Peter Oberparleiter0b923602009-06-17 16:28:05 -0700721/**
722 * seq_write - write arbitrary data to buffer
723 * @seq: seq_file identifying the buffer to which data should be written
724 * @data: data address
725 * @len: number of bytes
726 *
727 * Return 0 on success, non-zero otherwise.
728 */
729int seq_write(struct seq_file *seq, const void *data, size_t len)
730{
731 if (seq->count + len < seq->size) {
732 memcpy(seq->buf + seq->count, data, len);
733 seq->count += len;
734 return 0;
735 }
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -0700736 seq_set_overflow(seq);
Peter Oberparleiter0b923602009-06-17 16:28:05 -0700737 return -1;
738}
739EXPORT_SYMBOL(seq_write);
740
Tetsuo Handa839cc2a2013-11-14 14:31:56 -0800741/**
742 * seq_pad - write padding spaces to buffer
743 * @m: seq_file identifying the buffer to which data should be written
744 * @c: the byte to append after padding if non-zero
745 */
746void seq_pad(struct seq_file *m, char c)
747{
748 int size = m->pad_until - m->count;
749 if (size > 0)
750 seq_printf(m, "%*s", size, "");
751 if (c)
752 seq_putc(m, c);
753}
754EXPORT_SYMBOL(seq_pad);
755
Andy Shevchenko37607102015-09-09 15:38:33 -0700756/* A complete analogue of print_hex_dump() */
757void seq_hex_dump(struct seq_file *m, const char *prefix_str, int prefix_type,
758 int rowsize, int groupsize, const void *buf, size_t len,
759 bool ascii)
760{
761 const u8 *ptr = buf;
762 int i, linelen, remaining = len;
Andy Shevchenko8b91a312015-11-06 16:32:37 -0800763 char *buffer;
764 size_t size;
Andy Shevchenko37607102015-09-09 15:38:33 -0700765 int ret;
766
767 if (rowsize != 16 && rowsize != 32)
768 rowsize = 16;
769
770 for (i = 0; i < len && !seq_has_overflowed(m); i += rowsize) {
771 linelen = min(remaining, rowsize);
772 remaining -= rowsize;
773
774 switch (prefix_type) {
775 case DUMP_PREFIX_ADDRESS:
776 seq_printf(m, "%s%p: ", prefix_str, ptr + i);
777 break;
778 case DUMP_PREFIX_OFFSET:
779 seq_printf(m, "%s%.8x: ", prefix_str, i);
780 break;
781 default:
782 seq_printf(m, "%s", prefix_str);
783 break;
784 }
785
Andy Shevchenko8b91a312015-11-06 16:32:37 -0800786 size = seq_get_buf(m, &buffer);
Andy Shevchenko37607102015-09-09 15:38:33 -0700787 ret = hex_dump_to_buffer(ptr + i, linelen, rowsize, groupsize,
Andy Shevchenko8b91a312015-11-06 16:32:37 -0800788 buffer, size, ascii);
789 seq_commit(m, ret < size ? ret : -1);
790
791 seq_putc(m, '\n');
Andy Shevchenko37607102015-09-09 15:38:33 -0700792 }
793}
794EXPORT_SYMBOL(seq_hex_dump);
795
Pavel Emelianovbcf67e12007-07-10 17:22:26 -0700796struct list_head *seq_list_start(struct list_head *head, loff_t pos)
797{
798 struct list_head *lh;
799
800 list_for_each(lh, head)
801 if (pos-- == 0)
802 return lh;
803
804 return NULL;
805}
Pavel Emelianovbcf67e12007-07-10 17:22:26 -0700806EXPORT_SYMBOL(seq_list_start);
807
808struct list_head *seq_list_start_head(struct list_head *head, loff_t pos)
809{
810 if (!pos)
811 return head;
812
813 return seq_list_start(head, pos - 1);
814}
Pavel Emelianovbcf67e12007-07-10 17:22:26 -0700815EXPORT_SYMBOL(seq_list_start_head);
816
817struct list_head *seq_list_next(void *v, struct list_head *head, loff_t *ppos)
818{
819 struct list_head *lh;
820
821 lh = ((struct list_head *)v)->next;
822 ++*ppos;
823 return lh == head ? NULL : lh;
824}
Pavel Emelianovbcf67e12007-07-10 17:22:26 -0700825EXPORT_SYMBOL(seq_list_next);
Li Zefan66655de2010-02-08 23:18:22 +0000826
827/**
828 * seq_hlist_start - start an iteration of a hlist
829 * @head: the head of the hlist
830 * @pos: the start position of the sequence
831 *
832 * Called at seq_file->op->start().
833 */
834struct hlist_node *seq_hlist_start(struct hlist_head *head, loff_t pos)
835{
836 struct hlist_node *node;
837
838 hlist_for_each(node, head)
839 if (pos-- == 0)
840 return node;
841 return NULL;
842}
843EXPORT_SYMBOL(seq_hlist_start);
844
845/**
846 * seq_hlist_start_head - start an iteration of a hlist
847 * @head: the head of the hlist
848 * @pos: the start position of the sequence
849 *
850 * Called at seq_file->op->start(). Call this function if you want to
851 * print a header at the top of the output.
852 */
853struct hlist_node *seq_hlist_start_head(struct hlist_head *head, loff_t pos)
854{
855 if (!pos)
856 return SEQ_START_TOKEN;
857
858 return seq_hlist_start(head, pos - 1);
859}
860EXPORT_SYMBOL(seq_hlist_start_head);
861
862/**
863 * seq_hlist_next - move to the next position of the hlist
864 * @v: the current iterator
865 * @head: the head of the hlist
Randy Dunlap138860b2010-03-04 09:37:12 -0800866 * @ppos: the current position
Li Zefan66655de2010-02-08 23:18:22 +0000867 *
868 * Called at seq_file->op->next().
869 */
870struct hlist_node *seq_hlist_next(void *v, struct hlist_head *head,
871 loff_t *ppos)
872{
873 struct hlist_node *node = v;
874
875 ++*ppos;
876 if (v == SEQ_START_TOKEN)
877 return head->first;
878 else
879 return node->next;
880}
881EXPORT_SYMBOL(seq_hlist_next);
stephen hemminger1cc52322010-02-22 07:57:17 +0000882
883/**
884 * seq_hlist_start_rcu - start an iteration of a hlist protected by RCU
885 * @head: the head of the hlist
886 * @pos: the start position of the sequence
887 *
888 * Called at seq_file->op->start().
889 *
890 * This list-traversal primitive may safely run concurrently with
891 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
892 * as long as the traversal is guarded by rcu_read_lock().
893 */
894struct hlist_node *seq_hlist_start_rcu(struct hlist_head *head,
895 loff_t pos)
896{
897 struct hlist_node *node;
898
899 __hlist_for_each_rcu(node, head)
900 if (pos-- == 0)
901 return node;
902 return NULL;
903}
904EXPORT_SYMBOL(seq_hlist_start_rcu);
905
906/**
907 * seq_hlist_start_head_rcu - start an iteration of a hlist protected by RCU
908 * @head: the head of the hlist
909 * @pos: the start position of the sequence
910 *
911 * Called at seq_file->op->start(). Call this function if you want to
912 * print a header at the top of the output.
913 *
914 * This list-traversal primitive may safely run concurrently with
915 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
916 * as long as the traversal is guarded by rcu_read_lock().
917 */
918struct hlist_node *seq_hlist_start_head_rcu(struct hlist_head *head,
919 loff_t pos)
920{
921 if (!pos)
922 return SEQ_START_TOKEN;
923
924 return seq_hlist_start_rcu(head, pos - 1);
925}
926EXPORT_SYMBOL(seq_hlist_start_head_rcu);
927
928/**
929 * seq_hlist_next_rcu - move to the next position of the hlist protected by RCU
930 * @v: the current iterator
931 * @head: the head of the hlist
Randy Dunlap138860b2010-03-04 09:37:12 -0800932 * @ppos: the current position
stephen hemminger1cc52322010-02-22 07:57:17 +0000933 *
934 * Called at seq_file->op->next().
935 *
936 * This list-traversal primitive may safely run concurrently with
937 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
938 * as long as the traversal is guarded by rcu_read_lock().
939 */
940struct hlist_node *seq_hlist_next_rcu(void *v,
941 struct hlist_head *head,
942 loff_t *ppos)
943{
944 struct hlist_node *node = v;
945
946 ++*ppos;
947 if (v == SEQ_START_TOKEN)
948 return rcu_dereference(head->first);
949 else
950 return rcu_dereference(node->next);
951}
952EXPORT_SYMBOL(seq_hlist_next_rcu);
Jeff Layton0bc77382013-06-21 08:58:21 -0400953
954/**
955 * seq_hlist_start_precpu - start an iteration of a percpu hlist array
956 * @head: pointer to percpu array of struct hlist_heads
957 * @cpu: pointer to cpu "cursor"
958 * @pos: start position of sequence
959 *
960 * Called at seq_file->op->start().
961 */
962struct hlist_node *
963seq_hlist_start_percpu(struct hlist_head __percpu *head, int *cpu, loff_t pos)
964{
965 struct hlist_node *node;
966
967 for_each_possible_cpu(*cpu) {
968 hlist_for_each(node, per_cpu_ptr(head, *cpu)) {
969 if (pos-- == 0)
970 return node;
971 }
972 }
973 return NULL;
974}
975EXPORT_SYMBOL(seq_hlist_start_percpu);
976
977/**
978 * seq_hlist_next_percpu - move to the next position of the percpu hlist array
979 * @v: pointer to current hlist_node
980 * @head: pointer to percpu array of struct hlist_heads
981 * @cpu: pointer to cpu "cursor"
982 * @pos: start position of sequence
983 *
984 * Called at seq_file->op->next().
985 */
986struct hlist_node *
987seq_hlist_next_percpu(void *v, struct hlist_head __percpu *head,
988 int *cpu, loff_t *pos)
989{
990 struct hlist_node *node = v;
991
992 ++*pos;
993
994 if (node->next)
995 return node->next;
996
997 for (*cpu = cpumask_next(*cpu, cpu_possible_mask); *cpu < nr_cpu_ids;
998 *cpu = cpumask_next(*cpu, cpu_possible_mask)) {
999 struct hlist_head *bucket = per_cpu_ptr(head, *cpu);
1000
1001 if (!hlist_empty(bucket))
1002 return bucket->first;
1003 }
1004 return NULL;
1005}
1006EXPORT_SYMBOL(seq_hlist_next_percpu);