blob: 33638bdae28289905892e698d12975be66a6c820 [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>
11#include <linux/slab.h>
Eric W. Biederman1c3a18f2012-05-23 18:01:20 -060012#include <linux/cred.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013
14#include <asm/uaccess.h>
15#include <asm/page.h>
16
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -070017
18/*
19 * seq_files have a buffer which can may overflow. When this happens a larger
20 * buffer is reallocated and all the data will be printed again.
21 * The overflow state is true when m->count == m->size.
22 */
23static bool seq_overflow(struct seq_file *m)
24{
25 return m->count == m->size;
26}
27
28static void seq_set_overflow(struct seq_file *m)
29{
30 m->count = m->size;
31}
32
Linus Torvalds1da177e2005-04-16 15:20:36 -070033/**
34 * seq_open - initialize sequential file
35 * @file: file we initialize
36 * @op: method table describing the sequence
37 *
38 * seq_open() sets @file, associating it with a sequence described
39 * by @op. @op->start() sets the iterator up and returns the first
40 * element of sequence. @op->stop() shuts it down. @op->next()
41 * returns the next element of sequence. @op->show() prints element
42 * into the buffer. In case of error ->start() and ->next() return
43 * ERR_PTR(error). In the end of sequence they return %NULL. ->show()
44 * returns 0 in case of success and negative number in case of error.
Al Viro521b5d02008-03-28 00:46:41 -040045 * Returning SEQ_SKIP means "discard this element and move on".
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 */
Helge Deller15ad7cd2006-12-06 20:40:36 -080047int seq_open(struct file *file, const struct seq_operations *op)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048{
Al Viro1abe77b2005-11-07 17:15:34 -050049 struct seq_file *p = file->private_data;
50
51 if (!p) {
52 p = kmalloc(sizeof(*p), GFP_KERNEL);
53 if (!p)
54 return -ENOMEM;
55 file->private_data = p;
56 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 memset(p, 0, sizeof(*p));
Ingo Molnar0ac17592006-03-23 03:00:37 -080058 mutex_init(&p->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 p->op = op;
Linus Torvalds043a9472016-04-14 11:22:00 -070060
61 // No refcounting: the lifetime of 'p' is constrained
62 // to the lifetime of the file.
63 p->file = file;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65 /*
66 * Wrappers around seq_open(e.g. swaps_open) need to be
67 * aware of this. If they set f_version themselves, they
68 * should call seq_open first and then set f_version.
69 */
70 file->f_version = 0;
71
Eric Biederman8f19d472009-02-18 14:48:16 -080072 /*
73 * seq_files support lseek() and pread(). They do not implement
74 * write() at all, but we clear FMODE_PWRITE here for historical
75 * reasons.
76 *
77 * If a client of seq_files a) implements file.write() and b) wishes to
78 * support pwrite() then that client will need to implement its own
79 * file.open() which calls seq_open() and then sets FMODE_PWRITE.
80 */
81 file->f_mode &= ~FMODE_PWRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 return 0;
83}
84EXPORT_SYMBOL(seq_open);
85
Eric Biederman33da8892009-02-04 15:12:25 -080086static int traverse(struct seq_file *m, loff_t offset)
87{
88 loff_t pos = 0, index;
89 int error = 0;
90 void *p;
91
92 m->version = 0;
93 index = 0;
94 m->count = m->from = 0;
95 if (!offset) {
96 m->index = index;
97 return 0;
98 }
99 if (!m->buf) {
100 m->buf = kmalloc(m->size = PAGE_SIZE, GFP_KERNEL);
101 if (!m->buf)
102 return -ENOMEM;
103 }
104 p = m->op->start(m, &index);
105 while (p) {
106 error = PTR_ERR(p);
107 if (IS_ERR(p))
108 break;
109 error = m->op->show(m, p);
110 if (error < 0)
111 break;
112 if (unlikely(error)) {
113 error = 0;
114 m->count = 0;
115 }
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -0700116 if (seq_overflow(m))
Eric Biederman33da8892009-02-04 15:12:25 -0800117 goto Eoverflow;
118 if (pos + m->count > offset) {
119 m->from = offset - pos;
120 m->count -= m->from;
121 m->index = index;
122 break;
123 }
124 pos += m->count;
125 m->count = 0;
126 if (pos == offset) {
127 index++;
128 m->index = index;
129 break;
130 }
131 p = m->op->next(m, p, &index);
132 }
133 m->op->stop(m, p);
Alexey Dobriyanf01d1d52009-02-06 00:30:05 +0300134 m->index = index;
Eric Biederman33da8892009-02-04 15:12:25 -0800135 return error;
136
137Eoverflow:
138 m->op->stop(m, p);
139 kfree(m->buf);
140 m->buf = kmalloc(m->size <<= 1, GFP_KERNEL);
141 return !m->buf ? -ENOMEM : -EAGAIN;
142}
143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144/**
145 * seq_read - ->read() method for sequential files.
Martin Waitz67be2dd2005-05-01 08:59:26 -0700146 * @file: the file to read from
147 * @buf: the buffer to read to
148 * @size: the maximum number of bytes to read
149 * @ppos: the current position in the file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 *
151 * Ready-made ->f_op->read()
152 */
153ssize_t seq_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
154{
Joe Perches8209e2f2010-09-04 18:52:49 -0700155 struct seq_file *m = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 size_t copied = 0;
157 loff_t pos;
158 size_t n;
159 void *p;
160 int err = 0;
161
Ingo Molnar0ac17592006-03-23 03:00:37 -0800162 mutex_lock(&m->lock);
Eric Biederman8f19d472009-02-18 14:48:16 -0800163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 /*
165 * seq_file->op->..m_start/m_stop/m_next may do special actions
166 * or optimisations based on the file->f_version, so we want to
167 * pass the file->f_version to those methods.
168 *
169 * seq_file->version is just copy of f_version, and seq_file
170 * methods can treat it simply as file version.
171 * It is copied in first and copied out after all operations.
172 * It is convenient to have it as part of structure to avoid the
173 * need of passing another argument to all the seq_file methods.
174 */
175 m->version = file->f_version;
Earl Chew7904ac82012-03-21 16:33:43 -0700176
177 /* Don't assume *ppos is where we left it */
178 if (unlikely(*ppos != m->read_pos)) {
179 while ((err = traverse(m, *ppos)) == -EAGAIN)
180 ;
181 if (err) {
182 /* With prejudice... */
183 m->read_pos = 0;
184 m->version = 0;
185 m->index = 0;
186 m->count = 0;
187 goto Done;
188 } else {
189 m->read_pos = *ppos;
190 }
191 }
192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 /* grab buffer if we didn't have one */
194 if (!m->buf) {
195 m->buf = kmalloc(m->size = PAGE_SIZE, GFP_KERNEL);
196 if (!m->buf)
197 goto Enomem;
198 }
199 /* if not empty - flush it first */
200 if (m->count) {
201 n = min(m->count, size);
202 err = copy_to_user(buf, m->buf + m->from, n);
203 if (err)
204 goto Efault;
205 m->count -= n;
206 m->from += n;
207 size -= n;
208 buf += n;
209 copied += n;
210 if (!m->count)
211 m->index++;
212 if (!size)
213 goto Done;
214 }
215 /* we need at least one record in buffer */
Al Viro4cdfe842008-08-24 07:45:33 -0400216 pos = m->index;
217 p = m->op->start(m, &pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 while (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 err = PTR_ERR(p);
220 if (!p || IS_ERR(p))
221 break;
222 err = m->op->show(m, p);
Al Viro521b5d02008-03-28 00:46:41 -0400223 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 break;
Al Viro521b5d02008-03-28 00:46:41 -0400225 if (unlikely(err))
226 m->count = 0;
Al Viro4cdfe842008-08-24 07:45:33 -0400227 if (unlikely(!m->count)) {
228 p = m->op->next(m, p, &pos);
229 m->index = pos;
230 continue;
231 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 if (m->count < m->size)
233 goto Fill;
234 m->op->stop(m, p);
235 kfree(m->buf);
236 m->buf = kmalloc(m->size <<= 1, GFP_KERNEL);
237 if (!m->buf)
238 goto Enomem;
239 m->count = 0;
240 m->version = 0;
Al Viro4cdfe842008-08-24 07:45:33 -0400241 pos = m->index;
242 p = m->op->start(m, &pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 }
244 m->op->stop(m, p);
245 m->count = 0;
246 goto Done;
247Fill:
248 /* they want more? let's try to get some more */
249 while (m->count < size) {
250 size_t offs = m->count;
251 loff_t next = pos;
252 p = m->op->next(m, p, &next);
253 if (!p || IS_ERR(p)) {
254 err = PTR_ERR(p);
255 break;
256 }
257 err = m->op->show(m, p);
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -0700258 if (seq_overflow(m) || err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 m->count = offs;
Al Viro521b5d02008-03-28 00:46:41 -0400260 if (likely(err <= 0))
261 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 }
263 pos = next;
264 }
265 m->op->stop(m, p);
266 n = min(m->count, size);
267 err = copy_to_user(buf, m->buf, n);
268 if (err)
269 goto Efault;
270 copied += n;
271 m->count -= n;
272 if (m->count)
273 m->from = n;
274 else
275 pos++;
276 m->index = pos;
277Done:
278 if (!copied)
279 copied = err;
Eric Biederman8f19d472009-02-18 14:48:16 -0800280 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 *ppos += copied;
Eric Biederman8f19d472009-02-18 14:48:16 -0800282 m->read_pos += copied;
283 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 file->f_version = m->version;
Ingo Molnar0ac17592006-03-23 03:00:37 -0800285 mutex_unlock(&m->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 return copied;
287Enomem:
288 err = -ENOMEM;
289 goto Done;
290Efault:
291 err = -EFAULT;
292 goto Done;
293}
294EXPORT_SYMBOL(seq_read);
295
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296/**
297 * seq_lseek - ->llseek() method for sequential files.
Martin Waitz67be2dd2005-05-01 08:59:26 -0700298 * @file: the file in question
299 * @offset: new position
300 * @origin: 0 for absolute, 1 for relative position
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 *
302 * Ready-made ->f_op->llseek()
303 */
304loff_t seq_lseek(struct file *file, loff_t offset, int origin)
305{
Joe Perches8209e2f2010-09-04 18:52:49 -0700306 struct seq_file *m = file->private_data;
David Sterba16abef02008-04-22 15:09:22 +0200307 loff_t retval = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Ingo Molnar0ac17592006-03-23 03:00:37 -0800309 mutex_lock(&m->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 m->version = file->f_version;
311 switch (origin) {
312 case 1:
313 offset += file->f_pos;
314 case 0:
315 if (offset < 0)
316 break;
317 retval = offset;
Eric Biederman8f19d472009-02-18 14:48:16 -0800318 if (offset != m->read_pos) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 while ((retval=traverse(m, offset)) == -EAGAIN)
320 ;
321 if (retval) {
322 /* with extreme prejudice... */
323 file->f_pos = 0;
Eric Biederman8f19d472009-02-18 14:48:16 -0800324 m->read_pos = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 m->version = 0;
326 m->index = 0;
327 m->count = 0;
328 } else {
Eric Biederman8f19d472009-02-18 14:48:16 -0800329 m->read_pos = offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 retval = file->f_pos = offset;
331 }
332 }
333 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 file->f_version = m->version;
Alexey Dobriyan00c57462007-07-15 23:40:22 -0700335 mutex_unlock(&m->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 return retval;
337}
338EXPORT_SYMBOL(seq_lseek);
339
340/**
341 * seq_release - free the structures associated with sequential file.
342 * @file: file in question
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800343 * @inode: file->f_path.dentry->d_inode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 *
345 * Frees the structures associated with sequential file; can be used
346 * as ->f_op->release() if you don't have private data to destroy.
347 */
348int seq_release(struct inode *inode, struct file *file)
349{
Joe Perches8209e2f2010-09-04 18:52:49 -0700350 struct seq_file *m = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 kfree(m->buf);
352 kfree(m);
353 return 0;
354}
355EXPORT_SYMBOL(seq_release);
356
357/**
358 * seq_escape - print string into buffer, escaping some characters
359 * @m: target buffer
360 * @s: string
361 * @esc: set of characters that need escaping
362 *
363 * Puts string into buffer, replacing each occurrence of character from
364 * @esc with usual octal escape. Returns 0 in case of success, -1 - in
365 * case of overflow.
366 */
367int seq_escape(struct seq_file *m, const char *s, const char *esc)
368{
369 char *end = m->buf + m->size;
370 char *p;
371 char c;
372
373 for (p = m->buf + m->count; (c = *s) != '\0' && p < end; s++) {
374 if (!strchr(esc, c)) {
375 *p++ = c;
376 continue;
377 }
378 if (p + 3 < end) {
379 *p++ = '\\';
380 *p++ = '0' + ((c & 0300) >> 6);
381 *p++ = '0' + ((c & 070) >> 3);
382 *p++ = '0' + (c & 07);
383 continue;
384 }
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -0700385 seq_set_overflow(m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 return -1;
387 }
388 m->count = p - m->buf;
389 return 0;
390}
391EXPORT_SYMBOL(seq_escape);
392
393int seq_printf(struct seq_file *m, const char *f, ...)
394{
395 va_list args;
396 int len;
397
398 if (m->count < m->size) {
399 va_start(args, f);
400 len = vsnprintf(m->buf + m->count, m->size - m->count, f, args);
401 va_end(args);
402 if (m->count + len < m->size) {
403 m->count += len;
404 return 0;
405 }
406 }
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -0700407 seq_set_overflow(m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 return -1;
409}
410EXPORT_SYMBOL(seq_printf);
411
Török Edwin74e2f332008-11-22 13:28:48 +0200412/**
Török Edwin958086d2008-11-23 23:24:53 +0200413 * mangle_path - mangle and copy path to buffer beginning
414 * @s: buffer start
415 * @p: beginning of path in above buffer
416 * @esc: set of characters that need escaping
Török Edwin74e2f332008-11-22 13:28:48 +0200417 *
418 * Copy the path from @p to @s, replacing each occurrence of character from
419 * @esc with usual octal escape.
420 * Returns pointer past last written character in @s, or NULL in case of
421 * failure.
422 */
Al Viro8c9379e2011-12-08 20:18:57 -0500423char *mangle_path(char *s, const char *p, const char *esc)
Ram Pai6092d042008-03-27 13:06:20 +0100424{
425 while (s <= p) {
426 char c = *p++;
427 if (!c) {
428 return s;
429 } else if (!strchr(esc, c)) {
430 *s++ = c;
431 } else if (s + 4 > p) {
432 break;
433 } else {
434 *s++ = '\\';
435 *s++ = '0' + ((c & 0300) >> 6);
436 *s++ = '0' + ((c & 070) >> 3);
437 *s++ = '0' + (c & 07);
438 }
439 }
440 return NULL;
441}
Ingo Molnar604094f2008-11-28 18:03:22 +0100442EXPORT_SYMBOL(mangle_path);
Ram Pai6092d042008-03-27 13:06:20 +0100443
Arjan van de Ven52afeef2008-12-01 14:35:00 -0800444/**
445 * seq_path - seq_file interface to print a pathname
446 * @m: the seq_file handle
447 * @path: the struct path to print
448 * @esc: set of characters to escape in the output
449 *
450 * return the absolute path of 'path', as represented by the
451 * dentry / mnt pair in the path parameter.
Ram Pai6092d042008-03-27 13:06:20 +0100452 */
Al Viro8c9379e2011-12-08 20:18:57 -0500453int seq_path(struct seq_file *m, const struct path *path, const char *esc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454{
Miklos Szeredif8439802009-09-21 14:48:36 +0200455 char *buf;
456 size_t size = seq_get_buf(m, &buf);
457 int res = -1;
458
459 if (size) {
460 char *p = d_path(path, buf, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 if (!IS_ERR(p)) {
Miklos Szeredif8439802009-09-21 14:48:36 +0200462 char *end = mangle_path(buf, p, esc);
463 if (end)
464 res = end - buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 }
466 }
Miklos Szeredif8439802009-09-21 14:48:36 +0200467 seq_commit(m, res);
468
469 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470}
471EXPORT_SYMBOL(seq_path);
472
Ram Pai6092d042008-03-27 13:06:20 +0100473/*
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100474 * Same as seq_path, but relative to supplied root.
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100475 */
Al Viro8c9379e2011-12-08 20:18:57 -0500476int seq_path_root(struct seq_file *m, const struct path *path,
477 const struct path *root, const char *esc)
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100478{
Miklos Szeredif8439802009-09-21 14:48:36 +0200479 char *buf;
480 size_t size = seq_get_buf(m, &buf);
481 int res = -ENAMETOOLONG;
482
483 if (size) {
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100484 char *p;
485
Miklos Szeredif8439802009-09-21 14:48:36 +0200486 p = __d_path(path, root, buf, size);
Al Viro02125a82011-12-05 08:43:34 -0500487 if (!p)
488 return SEQ_SKIP;
Miklos Szeredif8439802009-09-21 14:48:36 +0200489 res = PTR_ERR(p);
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100490 if (!IS_ERR(p)) {
Miklos Szeredif8439802009-09-21 14:48:36 +0200491 char *end = mangle_path(buf, p, esc);
492 if (end)
493 res = end - buf;
494 else
495 res = -ENAMETOOLONG;
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100496 }
497 }
Miklos Szeredif8439802009-09-21 14:48:36 +0200498 seq_commit(m, res);
499
Al Viro02125a82011-12-05 08:43:34 -0500500 return res < 0 && res != -ENAMETOOLONG ? res : 0;
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100501}
502
503/*
Ram Pai6092d042008-03-27 13:06:20 +0100504 * returns the path of the 'dentry' from the root of its filesystem.
505 */
Al Viro8c9379e2011-12-08 20:18:57 -0500506int seq_dentry(struct seq_file *m, struct dentry *dentry, const char *esc)
Ram Pai6092d042008-03-27 13:06:20 +0100507{
Miklos Szeredif8439802009-09-21 14:48:36 +0200508 char *buf;
509 size_t size = seq_get_buf(m, &buf);
510 int res = -1;
511
512 if (size) {
513 char *p = dentry_path(dentry, buf, size);
Ram Pai6092d042008-03-27 13:06:20 +0100514 if (!IS_ERR(p)) {
Miklos Szeredif8439802009-09-21 14:48:36 +0200515 char *end = mangle_path(buf, p, esc);
516 if (end)
517 res = end - buf;
Ram Pai6092d042008-03-27 13:06:20 +0100518 }
519 }
Miklos Szeredif8439802009-09-21 14:48:36 +0200520 seq_commit(m, res);
521
522 return res;
Ram Pai6092d042008-03-27 13:06:20 +0100523}
524
Rusty Russellcb78a0c2008-12-30 09:05:14 +1030525int seq_bitmap(struct seq_file *m, const unsigned long *bits,
526 unsigned int nr_bits)
Alexey Dobriyan50ac2d62008-08-12 15:09:02 -0700527{
Lai Jiangshan85dd0302008-10-18 20:28:18 -0700528 if (m->count < m->size) {
529 int len = bitmap_scnprintf(m->buf + m->count,
530 m->size - m->count, bits, nr_bits);
531 if (m->count + len < m->size) {
532 m->count += len;
533 return 0;
534 }
Alexey Dobriyan50ac2d62008-08-12 15:09:02 -0700535 }
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -0700536 seq_set_overflow(m);
Alexey Dobriyan50ac2d62008-08-12 15:09:02 -0700537 return -1;
538}
Lai Jiangshan85dd0302008-10-18 20:28:18 -0700539EXPORT_SYMBOL(seq_bitmap);
Alexey Dobriyan50ac2d62008-08-12 15:09:02 -0700540
Rusty Russellaf76aba2009-03-30 22:05:11 -0600541int seq_bitmap_list(struct seq_file *m, const unsigned long *bits,
Lai Jiangshan3eda2012008-10-18 20:28:19 -0700542 unsigned int nr_bits)
543{
544 if (m->count < m->size) {
545 int len = bitmap_scnlistprintf(m->buf + m->count,
546 m->size - m->count, bits, nr_bits);
547 if (m->count + len < m->size) {
548 m->count += len;
549 return 0;
550 }
551 }
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -0700552 seq_set_overflow(m);
Lai Jiangshan3eda2012008-10-18 20:28:19 -0700553 return -1;
554}
555EXPORT_SYMBOL(seq_bitmap_list);
556
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557static void *single_start(struct seq_file *p, loff_t *pos)
558{
559 return NULL + (*pos == 0);
560}
561
562static void *single_next(struct seq_file *p, void *v, loff_t *pos)
563{
564 ++*pos;
565 return NULL;
566}
567
568static void single_stop(struct seq_file *p, void *v)
569{
570}
571
572int single_open(struct file *file, int (*show)(struct seq_file *, void *),
573 void *data)
574{
575 struct seq_operations *op = kmalloc(sizeof(*op), GFP_KERNEL);
576 int res = -ENOMEM;
577
578 if (op) {
579 op->start = single_start;
580 op->next = single_next;
581 op->stop = single_stop;
582 op->show = show;
583 res = seq_open(file, op);
584 if (!res)
585 ((struct seq_file *)file->private_data)->private = data;
586 else
587 kfree(op);
588 }
589 return res;
590}
591EXPORT_SYMBOL(single_open);
592
593int single_release(struct inode *inode, struct file *file)
594{
Helge Deller15ad7cd2006-12-06 20:40:36 -0800595 const struct seq_operations *op = ((struct seq_file *)file->private_data)->op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 int res = seq_release(inode, file);
597 kfree(op);
598 return res;
599}
600EXPORT_SYMBOL(single_release);
601
602int seq_release_private(struct inode *inode, struct file *file)
603{
604 struct seq_file *seq = file->private_data;
605
606 kfree(seq->private);
607 seq->private = NULL;
608 return seq_release(inode, file);
609}
610EXPORT_SYMBOL(seq_release_private);
611
Pavel Emelyanov39699032007-10-10 02:28:42 -0700612void *__seq_open_private(struct file *f, const struct seq_operations *ops,
613 int psize)
614{
615 int rc;
616 void *private;
617 struct seq_file *seq;
618
619 private = kzalloc(psize, GFP_KERNEL);
620 if (private == NULL)
621 goto out;
622
623 rc = seq_open(f, ops);
624 if (rc < 0)
625 goto out_free;
626
627 seq = f->private_data;
628 seq->private = private;
629 return private;
630
631out_free:
632 kfree(private);
633out:
634 return NULL;
635}
636EXPORT_SYMBOL(__seq_open_private);
637
638int seq_open_private(struct file *filp, const struct seq_operations *ops,
639 int psize)
640{
641 return __seq_open_private(filp, ops, psize) ? 0 : -ENOMEM;
642}
643EXPORT_SYMBOL(seq_open_private);
644
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645int seq_putc(struct seq_file *m, char c)
646{
647 if (m->count < m->size) {
648 m->buf[m->count++] = c;
649 return 0;
650 }
651 return -1;
652}
653EXPORT_SYMBOL(seq_putc);
654
655int seq_puts(struct seq_file *m, const char *s)
656{
657 int len = strlen(s);
658 if (m->count + len < m->size) {
659 memcpy(m->buf + m->count, s, len);
660 m->count += len;
661 return 0;
662 }
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -0700663 seq_set_overflow(m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 return -1;
665}
666EXPORT_SYMBOL(seq_puts);
Pavel Emelianovbcf67e12007-07-10 17:22:26 -0700667
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700668/*
669 * A helper routine for putting decimal numbers without rich format of printf().
670 * only 'unsigned long long' is supported.
671 * This routine will put one byte delimiter + number into seq_file.
672 * This routine is very quick when you show lots of numbers.
673 * In usual cases, it will be better to use seq_printf(). It's easier to read.
674 */
675int seq_put_decimal_ull(struct seq_file *m, char delimiter,
676 unsigned long long num)
677{
678 int len;
679
680 if (m->count + 2 >= m->size) /* we'll write 2 bytes at least */
681 goto overflow;
682
KAMEZAWA Hiroyukibda7bad2012-03-23 15:02:54 -0700683 if (delimiter)
684 m->buf[m->count++] = delimiter;
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700685
686 if (num < 10) {
687 m->buf[m->count++] = num + '0';
688 return 0;
689 }
690
691 len = num_to_str(m->buf + m->count, m->size - m->count, num);
692 if (!len)
693 goto overflow;
694 m->count += len;
695 return 0;
696overflow:
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -0700697 seq_set_overflow(m);
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700698 return -1;
699}
700EXPORT_SYMBOL(seq_put_decimal_ull);
701
KAMEZAWA Hiroyukibda7bad2012-03-23 15:02:54 -0700702int seq_put_decimal_ll(struct seq_file *m, char delimiter,
703 long long num)
704{
705 if (num < 0) {
706 if (m->count + 3 >= m->size) {
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -0700707 seq_set_overflow(m);
KAMEZAWA Hiroyukibda7bad2012-03-23 15:02:54 -0700708 return -1;
709 }
710 if (delimiter)
711 m->buf[m->count++] = delimiter;
712 num = -num;
713 delimiter = '-';
714 }
715 return seq_put_decimal_ull(m, delimiter, num);
716
717}
718EXPORT_SYMBOL(seq_put_decimal_ll);
719
Peter Oberparleiter0b923602009-06-17 16:28:05 -0700720/**
721 * seq_write - write arbitrary data to buffer
722 * @seq: seq_file identifying the buffer to which data should be written
723 * @data: data address
724 * @len: number of bytes
725 *
726 * Return 0 on success, non-zero otherwise.
727 */
728int seq_write(struct seq_file *seq, const void *data, size_t len)
729{
730 if (seq->count + len < seq->size) {
731 memcpy(seq->buf + seq->count, data, len);
732 seq->count += len;
733 return 0;
734 }
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -0700735 seq_set_overflow(seq);
Peter Oberparleiter0b923602009-06-17 16:28:05 -0700736 return -1;
737}
738EXPORT_SYMBOL(seq_write);
739
Tetsuo Handae7704c22013-11-14 14:31:56 -0800740/**
741 * seq_pad - write padding spaces to buffer
742 * @m: seq_file identifying the buffer to which data should be written
743 * @c: the byte to append after padding if non-zero
744 */
745void seq_pad(struct seq_file *m, char c)
746{
747 int size = m->pad_until - m->count;
748 if (size > 0)
749 seq_printf(m, "%*s", size, "");
750 if (c)
751 seq_putc(m, c);
752}
753EXPORT_SYMBOL(seq_pad);
754
Pavel Emelianovbcf67e12007-07-10 17:22:26 -0700755struct list_head *seq_list_start(struct list_head *head, loff_t pos)
756{
757 struct list_head *lh;
758
759 list_for_each(lh, head)
760 if (pos-- == 0)
761 return lh;
762
763 return NULL;
764}
Pavel Emelianovbcf67e12007-07-10 17:22:26 -0700765EXPORT_SYMBOL(seq_list_start);
766
767struct list_head *seq_list_start_head(struct list_head *head, loff_t pos)
768{
769 if (!pos)
770 return head;
771
772 return seq_list_start(head, pos - 1);
773}
Pavel Emelianovbcf67e12007-07-10 17:22:26 -0700774EXPORT_SYMBOL(seq_list_start_head);
775
776struct list_head *seq_list_next(void *v, struct list_head *head, loff_t *ppos)
777{
778 struct list_head *lh;
779
780 lh = ((struct list_head *)v)->next;
781 ++*ppos;
782 return lh == head ? NULL : lh;
783}
Pavel Emelianovbcf67e12007-07-10 17:22:26 -0700784EXPORT_SYMBOL(seq_list_next);
Li Zefan66655de2010-02-08 23:18:22 +0000785
786/**
787 * seq_hlist_start - start an iteration of a hlist
788 * @head: the head of the hlist
789 * @pos: the start position of the sequence
790 *
791 * Called at seq_file->op->start().
792 */
793struct hlist_node *seq_hlist_start(struct hlist_head *head, loff_t pos)
794{
795 struct hlist_node *node;
796
797 hlist_for_each(node, head)
798 if (pos-- == 0)
799 return node;
800 return NULL;
801}
802EXPORT_SYMBOL(seq_hlist_start);
803
804/**
805 * seq_hlist_start_head - start an iteration of a hlist
806 * @head: the head of the hlist
807 * @pos: the start position of the sequence
808 *
809 * Called at seq_file->op->start(). Call this function if you want to
810 * print a header at the top of the output.
811 */
812struct hlist_node *seq_hlist_start_head(struct hlist_head *head, loff_t pos)
813{
814 if (!pos)
815 return SEQ_START_TOKEN;
816
817 return seq_hlist_start(head, pos - 1);
818}
819EXPORT_SYMBOL(seq_hlist_start_head);
820
821/**
822 * seq_hlist_next - move to the next position of the hlist
823 * @v: the current iterator
824 * @head: the head of the hlist
Randy Dunlap138860b2010-03-04 09:37:12 -0800825 * @ppos: the current position
Li Zefan66655de2010-02-08 23:18:22 +0000826 *
827 * Called at seq_file->op->next().
828 */
829struct hlist_node *seq_hlist_next(void *v, struct hlist_head *head,
830 loff_t *ppos)
831{
832 struct hlist_node *node = v;
833
834 ++*ppos;
835 if (v == SEQ_START_TOKEN)
836 return head->first;
837 else
838 return node->next;
839}
840EXPORT_SYMBOL(seq_hlist_next);
stephen hemminger1cc52322010-02-22 07:57:17 +0000841
842/**
843 * seq_hlist_start_rcu - start an iteration of a hlist protected by RCU
844 * @head: the head of the hlist
845 * @pos: the start position of the sequence
846 *
847 * Called at seq_file->op->start().
848 *
849 * This list-traversal primitive may safely run concurrently with
850 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
851 * as long as the traversal is guarded by rcu_read_lock().
852 */
853struct hlist_node *seq_hlist_start_rcu(struct hlist_head *head,
854 loff_t pos)
855{
856 struct hlist_node *node;
857
858 __hlist_for_each_rcu(node, head)
859 if (pos-- == 0)
860 return node;
861 return NULL;
862}
863EXPORT_SYMBOL(seq_hlist_start_rcu);
864
865/**
866 * seq_hlist_start_head_rcu - start an iteration of a hlist protected by RCU
867 * @head: the head of the hlist
868 * @pos: the start position of the sequence
869 *
870 * Called at seq_file->op->start(). Call this function if you want to
871 * print a header at the top of the output.
872 *
873 * This list-traversal primitive may safely run concurrently with
874 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
875 * as long as the traversal is guarded by rcu_read_lock().
876 */
877struct hlist_node *seq_hlist_start_head_rcu(struct hlist_head *head,
878 loff_t pos)
879{
880 if (!pos)
881 return SEQ_START_TOKEN;
882
883 return seq_hlist_start_rcu(head, pos - 1);
884}
885EXPORT_SYMBOL(seq_hlist_start_head_rcu);
886
887/**
888 * seq_hlist_next_rcu - move to the next position of the hlist protected by RCU
889 * @v: the current iterator
890 * @head: the head of the hlist
Randy Dunlap138860b2010-03-04 09:37:12 -0800891 * @ppos: the current position
stephen hemminger1cc52322010-02-22 07:57:17 +0000892 *
893 * Called at seq_file->op->next().
894 *
895 * This list-traversal primitive may safely run concurrently with
896 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
897 * as long as the traversal is guarded by rcu_read_lock().
898 */
899struct hlist_node *seq_hlist_next_rcu(void *v,
900 struct hlist_head *head,
901 loff_t *ppos)
902{
903 struct hlist_node *node = v;
904
905 ++*ppos;
906 if (v == SEQ_START_TOKEN)
907 return rcu_dereference(head->first);
908 else
909 return rcu_dereference(node->next);
910}
911EXPORT_SYMBOL(seq_hlist_next_rcu);