blob: 74717b43b160c92b4680d2e0591a3a50893708f0 [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>
12
13#include <asm/uaccess.h>
14#include <asm/page.h>
15
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -070016
17/*
18 * seq_files have a buffer which can may overflow. When this happens a larger
19 * buffer is reallocated and all the data will be printed again.
20 * The overflow state is true when m->count == m->size.
21 */
22static bool seq_overflow(struct seq_file *m)
23{
24 return m->count == m->size;
25}
26
27static void seq_set_overflow(struct seq_file *m)
28{
29 m->count = m->size;
30}
31
Linus Torvalds1da177e2005-04-16 15:20:36 -070032/**
33 * seq_open - initialize sequential file
34 * @file: file we initialize
35 * @op: method table describing the sequence
36 *
37 * seq_open() sets @file, associating it with a sequence described
38 * by @op. @op->start() sets the iterator up and returns the first
39 * element of sequence. @op->stop() shuts it down. @op->next()
40 * returns the next element of sequence. @op->show() prints element
41 * into the buffer. In case of error ->start() and ->next() return
42 * ERR_PTR(error). In the end of sequence they return %NULL. ->show()
43 * returns 0 in case of success and negative number in case of error.
Al Viro521b5d02008-03-28 00:46:41 -040044 * Returning SEQ_SKIP means "discard this element and move on".
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 */
Helge Deller15ad7cd2006-12-06 20:40:36 -080046int seq_open(struct file *file, const struct seq_operations *op)
Linus Torvalds1da177e2005-04-16 15:20:36 -070047{
Al Viro1abe77b2005-11-07 17:15:34 -050048 struct seq_file *p = file->private_data;
49
50 if (!p) {
51 p = kmalloc(sizeof(*p), GFP_KERNEL);
52 if (!p)
53 return -ENOMEM;
54 file->private_data = p;
55 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 memset(p, 0, sizeof(*p));
Ingo Molnar0ac17592006-03-23 03:00:37 -080057 mutex_init(&p->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 p->op = op;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60 /*
61 * Wrappers around seq_open(e.g. swaps_open) need to be
62 * aware of this. If they set f_version themselves, they
63 * should call seq_open first and then set f_version.
64 */
65 file->f_version = 0;
66
Eric Biederman8f19d472009-02-18 14:48:16 -080067 /*
68 * seq_files support lseek() and pread(). They do not implement
69 * write() at all, but we clear FMODE_PWRITE here for historical
70 * reasons.
71 *
72 * If a client of seq_files a) implements file.write() and b) wishes to
73 * support pwrite() then that client will need to implement its own
74 * file.open() which calls seq_open() and then sets FMODE_PWRITE.
75 */
76 file->f_mode &= ~FMODE_PWRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 return 0;
78}
79EXPORT_SYMBOL(seq_open);
80
Eric Biederman33da8892009-02-04 15:12:25 -080081static int traverse(struct seq_file *m, loff_t offset)
82{
83 loff_t pos = 0, index;
84 int error = 0;
85 void *p;
86
87 m->version = 0;
88 index = 0;
89 m->count = m->from = 0;
90 if (!offset) {
91 m->index = index;
92 return 0;
93 }
94 if (!m->buf) {
95 m->buf = kmalloc(m->size = PAGE_SIZE, GFP_KERNEL);
96 if (!m->buf)
97 return -ENOMEM;
98 }
99 p = m->op->start(m, &index);
100 while (p) {
101 error = PTR_ERR(p);
102 if (IS_ERR(p))
103 break;
104 error = m->op->show(m, p);
105 if (error < 0)
106 break;
107 if (unlikely(error)) {
108 error = 0;
109 m->count = 0;
110 }
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -0700111 if (seq_overflow(m))
Eric Biederman33da8892009-02-04 15:12:25 -0800112 goto Eoverflow;
113 if (pos + m->count > offset) {
114 m->from = offset - pos;
115 m->count -= m->from;
116 m->index = index;
117 break;
118 }
119 pos += m->count;
120 m->count = 0;
121 if (pos == offset) {
122 index++;
123 m->index = index;
124 break;
125 }
126 p = m->op->next(m, p, &index);
127 }
128 m->op->stop(m, p);
Alexey Dobriyanf01d1d52009-02-06 00:30:05 +0300129 m->index = index;
Eric Biederman33da8892009-02-04 15:12:25 -0800130 return error;
131
132Eoverflow:
133 m->op->stop(m, p);
134 kfree(m->buf);
135 m->buf = kmalloc(m->size <<= 1, GFP_KERNEL);
136 return !m->buf ? -ENOMEM : -EAGAIN;
137}
138
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139/**
140 * seq_read - ->read() method for sequential files.
Martin Waitz67be2dd2005-05-01 08:59:26 -0700141 * @file: the file to read from
142 * @buf: the buffer to read to
143 * @size: the maximum number of bytes to read
144 * @ppos: the current position in the file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 *
146 * Ready-made ->f_op->read()
147 */
148ssize_t seq_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
149{
Joe Perches8209e2f2010-09-04 18:52:49 -0700150 struct seq_file *m = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 size_t copied = 0;
152 loff_t pos;
153 size_t n;
154 void *p;
155 int err = 0;
156
Ingo Molnar0ac17592006-03-23 03:00:37 -0800157 mutex_lock(&m->lock);
Eric Biederman8f19d472009-02-18 14:48:16 -0800158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 /*
160 * seq_file->op->..m_start/m_stop/m_next may do special actions
161 * or optimisations based on the file->f_version, so we want to
162 * pass the file->f_version to those methods.
163 *
164 * seq_file->version is just copy of f_version, and seq_file
165 * methods can treat it simply as file version.
166 * It is copied in first and copied out after all operations.
167 * It is convenient to have it as part of structure to avoid the
168 * need of passing another argument to all the seq_file methods.
169 */
170 m->version = file->f_version;
Earl Chew7904ac82012-03-21 16:33:43 -0700171
172 /* Don't assume *ppos is where we left it */
173 if (unlikely(*ppos != m->read_pos)) {
174 while ((err = traverse(m, *ppos)) == -EAGAIN)
175 ;
176 if (err) {
177 /* With prejudice... */
178 m->read_pos = 0;
179 m->version = 0;
180 m->index = 0;
181 m->count = 0;
182 goto Done;
183 } else {
184 m->read_pos = *ppos;
185 }
186 }
187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 /* grab buffer if we didn't have one */
189 if (!m->buf) {
190 m->buf = kmalloc(m->size = PAGE_SIZE, GFP_KERNEL);
191 if (!m->buf)
192 goto Enomem;
193 }
194 /* if not empty - flush it first */
195 if (m->count) {
196 n = min(m->count, size);
197 err = copy_to_user(buf, m->buf + m->from, n);
198 if (err)
199 goto Efault;
200 m->count -= n;
201 m->from += n;
202 size -= n;
203 buf += n;
204 copied += n;
205 if (!m->count)
206 m->index++;
207 if (!size)
208 goto Done;
209 }
210 /* we need at least one record in buffer */
Al Viro4cdfe842008-08-24 07:45:33 -0400211 pos = m->index;
212 p = m->op->start(m, &pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 while (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 err = PTR_ERR(p);
215 if (!p || IS_ERR(p))
216 break;
217 err = m->op->show(m, p);
Al Viro521b5d02008-03-28 00:46:41 -0400218 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 break;
Al Viro521b5d02008-03-28 00:46:41 -0400220 if (unlikely(err))
221 m->count = 0;
Al Viro4cdfe842008-08-24 07:45:33 -0400222 if (unlikely(!m->count)) {
223 p = m->op->next(m, p, &pos);
224 m->index = pos;
225 continue;
226 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 if (m->count < m->size)
228 goto Fill;
229 m->op->stop(m, p);
230 kfree(m->buf);
231 m->buf = kmalloc(m->size <<= 1, GFP_KERNEL);
232 if (!m->buf)
233 goto Enomem;
234 m->count = 0;
235 m->version = 0;
Al Viro4cdfe842008-08-24 07:45:33 -0400236 pos = m->index;
237 p = m->op->start(m, &pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 }
239 m->op->stop(m, p);
240 m->count = 0;
241 goto Done;
242Fill:
243 /* they want more? let's try to get some more */
244 while (m->count < size) {
245 size_t offs = m->count;
246 loff_t next = pos;
247 p = m->op->next(m, p, &next);
248 if (!p || IS_ERR(p)) {
249 err = PTR_ERR(p);
250 break;
251 }
252 err = m->op->show(m, p);
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -0700253 if (seq_overflow(m) || err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 m->count = offs;
Al Viro521b5d02008-03-28 00:46:41 -0400255 if (likely(err <= 0))
256 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 }
258 pos = next;
259 }
260 m->op->stop(m, p);
261 n = min(m->count, size);
262 err = copy_to_user(buf, m->buf, n);
263 if (err)
264 goto Efault;
265 copied += n;
266 m->count -= n;
267 if (m->count)
268 m->from = n;
269 else
270 pos++;
271 m->index = pos;
272Done:
273 if (!copied)
274 copied = err;
Eric Biederman8f19d472009-02-18 14:48:16 -0800275 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 *ppos += copied;
Eric Biederman8f19d472009-02-18 14:48:16 -0800277 m->read_pos += copied;
278 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 file->f_version = m->version;
Ingo Molnar0ac17592006-03-23 03:00:37 -0800280 mutex_unlock(&m->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 return copied;
282Enomem:
283 err = -ENOMEM;
284 goto Done;
285Efault:
286 err = -EFAULT;
287 goto Done;
288}
289EXPORT_SYMBOL(seq_read);
290
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291/**
292 * seq_lseek - ->llseek() method for sequential files.
Martin Waitz67be2dd2005-05-01 08:59:26 -0700293 * @file: the file in question
294 * @offset: new position
295 * @origin: 0 for absolute, 1 for relative position
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 *
297 * Ready-made ->f_op->llseek()
298 */
299loff_t seq_lseek(struct file *file, loff_t offset, int origin)
300{
Joe Perches8209e2f2010-09-04 18:52:49 -0700301 struct seq_file *m = file->private_data;
David Sterba16abef02008-04-22 15:09:22 +0200302 loff_t retval = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
Ingo Molnar0ac17592006-03-23 03:00:37 -0800304 mutex_lock(&m->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 m->version = file->f_version;
306 switch (origin) {
307 case 1:
308 offset += file->f_pos;
309 case 0:
310 if (offset < 0)
311 break;
312 retval = offset;
Eric Biederman8f19d472009-02-18 14:48:16 -0800313 if (offset != m->read_pos) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 while ((retval=traverse(m, offset)) == -EAGAIN)
315 ;
316 if (retval) {
317 /* with extreme prejudice... */
318 file->f_pos = 0;
Eric Biederman8f19d472009-02-18 14:48:16 -0800319 m->read_pos = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 m->version = 0;
321 m->index = 0;
322 m->count = 0;
323 } else {
Eric Biederman8f19d472009-02-18 14:48:16 -0800324 m->read_pos = offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 retval = file->f_pos = offset;
326 }
327 }
328 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 file->f_version = m->version;
Alexey Dobriyan00c57462007-07-15 23:40:22 -0700330 mutex_unlock(&m->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 return retval;
332}
333EXPORT_SYMBOL(seq_lseek);
334
335/**
336 * seq_release - free the structures associated with sequential file.
337 * @file: file in question
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800338 * @inode: file->f_path.dentry->d_inode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 *
340 * Frees the structures associated with sequential file; can be used
341 * as ->f_op->release() if you don't have private data to destroy.
342 */
343int seq_release(struct inode *inode, struct file *file)
344{
Joe Perches8209e2f2010-09-04 18:52:49 -0700345 struct seq_file *m = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 kfree(m->buf);
347 kfree(m);
348 return 0;
349}
350EXPORT_SYMBOL(seq_release);
351
352/**
353 * seq_escape - print string into buffer, escaping some characters
354 * @m: target buffer
355 * @s: string
356 * @esc: set of characters that need escaping
357 *
358 * Puts string into buffer, replacing each occurrence of character from
359 * @esc with usual octal escape. Returns 0 in case of success, -1 - in
360 * case of overflow.
361 */
362int seq_escape(struct seq_file *m, const char *s, const char *esc)
363{
364 char *end = m->buf + m->size;
365 char *p;
366 char c;
367
368 for (p = m->buf + m->count; (c = *s) != '\0' && p < end; s++) {
369 if (!strchr(esc, c)) {
370 *p++ = c;
371 continue;
372 }
373 if (p + 3 < end) {
374 *p++ = '\\';
375 *p++ = '0' + ((c & 0300) >> 6);
376 *p++ = '0' + ((c & 070) >> 3);
377 *p++ = '0' + (c & 07);
378 continue;
379 }
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -0700380 seq_set_overflow(m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 return -1;
382 }
383 m->count = p - m->buf;
384 return 0;
385}
386EXPORT_SYMBOL(seq_escape);
387
388int seq_printf(struct seq_file *m, const char *f, ...)
389{
390 va_list args;
391 int len;
392
393 if (m->count < m->size) {
394 va_start(args, f);
395 len = vsnprintf(m->buf + m->count, m->size - m->count, f, args);
396 va_end(args);
397 if (m->count + len < m->size) {
398 m->count += len;
399 return 0;
400 }
401 }
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -0700402 seq_set_overflow(m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 return -1;
404}
405EXPORT_SYMBOL(seq_printf);
406
Török Edwin74e2f332008-11-22 13:28:48 +0200407/**
Török Edwin958086d2008-11-23 23:24:53 +0200408 * mangle_path - mangle and copy path to buffer beginning
409 * @s: buffer start
410 * @p: beginning of path in above buffer
411 * @esc: set of characters that need escaping
Török Edwin74e2f332008-11-22 13:28:48 +0200412 *
413 * Copy the path from @p to @s, replacing each occurrence of character from
414 * @esc with usual octal escape.
415 * Returns pointer past last written character in @s, or NULL in case of
416 * failure.
417 */
Al Viro8c9379e2011-12-08 20:18:57 -0500418char *mangle_path(char *s, const char *p, const char *esc)
Ram Pai6092d042008-03-27 13:06:20 +0100419{
420 while (s <= p) {
421 char c = *p++;
422 if (!c) {
423 return s;
424 } else if (!strchr(esc, c)) {
425 *s++ = c;
426 } else if (s + 4 > p) {
427 break;
428 } else {
429 *s++ = '\\';
430 *s++ = '0' + ((c & 0300) >> 6);
431 *s++ = '0' + ((c & 070) >> 3);
432 *s++ = '0' + (c & 07);
433 }
434 }
435 return NULL;
436}
Ingo Molnar604094f2008-11-28 18:03:22 +0100437EXPORT_SYMBOL(mangle_path);
Ram Pai6092d042008-03-27 13:06:20 +0100438
Arjan van de Ven52afeef2008-12-01 14:35:00 -0800439/**
440 * seq_path - seq_file interface to print a pathname
441 * @m: the seq_file handle
442 * @path: the struct path to print
443 * @esc: set of characters to escape in the output
444 *
445 * return the absolute path of 'path', as represented by the
446 * dentry / mnt pair in the path parameter.
Ram Pai6092d042008-03-27 13:06:20 +0100447 */
Al Viro8c9379e2011-12-08 20:18:57 -0500448int seq_path(struct seq_file *m, const struct path *path, const char *esc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449{
Miklos Szeredif8439802009-09-21 14:48:36 +0200450 char *buf;
451 size_t size = seq_get_buf(m, &buf);
452 int res = -1;
453
454 if (size) {
455 char *p = d_path(path, buf, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 if (!IS_ERR(p)) {
Miklos Szeredif8439802009-09-21 14:48:36 +0200457 char *end = mangle_path(buf, p, esc);
458 if (end)
459 res = end - buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 }
461 }
Miklos Szeredif8439802009-09-21 14:48:36 +0200462 seq_commit(m, res);
463
464 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465}
466EXPORT_SYMBOL(seq_path);
467
Ram Pai6092d042008-03-27 13:06:20 +0100468/*
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100469 * Same as seq_path, but relative to supplied root.
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100470 */
Al Viro8c9379e2011-12-08 20:18:57 -0500471int seq_path_root(struct seq_file *m, const struct path *path,
472 const struct path *root, const char *esc)
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100473{
Miklos Szeredif8439802009-09-21 14:48:36 +0200474 char *buf;
475 size_t size = seq_get_buf(m, &buf);
476 int res = -ENAMETOOLONG;
477
478 if (size) {
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100479 char *p;
480
Miklos Szeredif8439802009-09-21 14:48:36 +0200481 p = __d_path(path, root, buf, size);
Al Viro02125a82011-12-05 08:43:34 -0500482 if (!p)
483 return SEQ_SKIP;
Miklos Szeredif8439802009-09-21 14:48:36 +0200484 res = PTR_ERR(p);
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100485 if (!IS_ERR(p)) {
Miklos Szeredif8439802009-09-21 14:48:36 +0200486 char *end = mangle_path(buf, p, esc);
487 if (end)
488 res = end - buf;
489 else
490 res = -ENAMETOOLONG;
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100491 }
492 }
Miklos Szeredif8439802009-09-21 14:48:36 +0200493 seq_commit(m, res);
494
Al Viro02125a82011-12-05 08:43:34 -0500495 return res < 0 && res != -ENAMETOOLONG ? res : 0;
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100496}
497
498/*
Ram Pai6092d042008-03-27 13:06:20 +0100499 * returns the path of the 'dentry' from the root of its filesystem.
500 */
Al Viro8c9379e2011-12-08 20:18:57 -0500501int seq_dentry(struct seq_file *m, struct dentry *dentry, const char *esc)
Ram Pai6092d042008-03-27 13:06:20 +0100502{
Miklos Szeredif8439802009-09-21 14:48:36 +0200503 char *buf;
504 size_t size = seq_get_buf(m, &buf);
505 int res = -1;
506
507 if (size) {
508 char *p = dentry_path(dentry, buf, size);
Ram Pai6092d042008-03-27 13:06:20 +0100509 if (!IS_ERR(p)) {
Miklos Szeredif8439802009-09-21 14:48:36 +0200510 char *end = mangle_path(buf, p, esc);
511 if (end)
512 res = end - buf;
Ram Pai6092d042008-03-27 13:06:20 +0100513 }
514 }
Miklos Szeredif8439802009-09-21 14:48:36 +0200515 seq_commit(m, res);
516
517 return res;
Ram Pai6092d042008-03-27 13:06:20 +0100518}
519
Rusty Russellcb78a0c2008-12-30 09:05:14 +1030520int seq_bitmap(struct seq_file *m, const unsigned long *bits,
521 unsigned int nr_bits)
Alexey Dobriyan50ac2d62008-08-12 15:09:02 -0700522{
Lai Jiangshan85dd0302008-10-18 20:28:18 -0700523 if (m->count < m->size) {
524 int len = bitmap_scnprintf(m->buf + m->count,
525 m->size - m->count, bits, nr_bits);
526 if (m->count + len < m->size) {
527 m->count += len;
528 return 0;
529 }
Alexey Dobriyan50ac2d62008-08-12 15:09:02 -0700530 }
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -0700531 seq_set_overflow(m);
Alexey Dobriyan50ac2d62008-08-12 15:09:02 -0700532 return -1;
533}
Lai Jiangshan85dd0302008-10-18 20:28:18 -0700534EXPORT_SYMBOL(seq_bitmap);
Alexey Dobriyan50ac2d62008-08-12 15:09:02 -0700535
Rusty Russellaf76aba2009-03-30 22:05:11 -0600536int seq_bitmap_list(struct seq_file *m, const unsigned long *bits,
Lai Jiangshan3eda2012008-10-18 20:28:19 -0700537 unsigned int nr_bits)
538{
539 if (m->count < m->size) {
540 int len = bitmap_scnlistprintf(m->buf + m->count,
541 m->size - m->count, bits, nr_bits);
542 if (m->count + len < m->size) {
543 m->count += len;
544 return 0;
545 }
546 }
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -0700547 seq_set_overflow(m);
Lai Jiangshan3eda2012008-10-18 20:28:19 -0700548 return -1;
549}
550EXPORT_SYMBOL(seq_bitmap_list);
551
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552static void *single_start(struct seq_file *p, loff_t *pos)
553{
554 return NULL + (*pos == 0);
555}
556
557static void *single_next(struct seq_file *p, void *v, loff_t *pos)
558{
559 ++*pos;
560 return NULL;
561}
562
563static void single_stop(struct seq_file *p, void *v)
564{
565}
566
567int single_open(struct file *file, int (*show)(struct seq_file *, void *),
568 void *data)
569{
570 struct seq_operations *op = kmalloc(sizeof(*op), GFP_KERNEL);
571 int res = -ENOMEM;
572
573 if (op) {
574 op->start = single_start;
575 op->next = single_next;
576 op->stop = single_stop;
577 op->show = show;
578 res = seq_open(file, op);
579 if (!res)
580 ((struct seq_file *)file->private_data)->private = data;
581 else
582 kfree(op);
583 }
584 return res;
585}
586EXPORT_SYMBOL(single_open);
587
588int single_release(struct inode *inode, struct file *file)
589{
Helge Deller15ad7cd2006-12-06 20:40:36 -0800590 const struct seq_operations *op = ((struct seq_file *)file->private_data)->op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 int res = seq_release(inode, file);
592 kfree(op);
593 return res;
594}
595EXPORT_SYMBOL(single_release);
596
597int seq_release_private(struct inode *inode, struct file *file)
598{
599 struct seq_file *seq = file->private_data;
600
601 kfree(seq->private);
602 seq->private = NULL;
603 return seq_release(inode, file);
604}
605EXPORT_SYMBOL(seq_release_private);
606
Pavel Emelyanov39699032007-10-10 02:28:42 -0700607void *__seq_open_private(struct file *f, const struct seq_operations *ops,
608 int psize)
609{
610 int rc;
611 void *private;
612 struct seq_file *seq;
613
614 private = kzalloc(psize, GFP_KERNEL);
615 if (private == NULL)
616 goto out;
617
618 rc = seq_open(f, ops);
619 if (rc < 0)
620 goto out_free;
621
622 seq = f->private_data;
623 seq->private = private;
624 return private;
625
626out_free:
627 kfree(private);
628out:
629 return NULL;
630}
631EXPORT_SYMBOL(__seq_open_private);
632
633int seq_open_private(struct file *filp, const struct seq_operations *ops,
634 int psize)
635{
636 return __seq_open_private(filp, ops, psize) ? 0 : -ENOMEM;
637}
638EXPORT_SYMBOL(seq_open_private);
639
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640int seq_putc(struct seq_file *m, char c)
641{
642 if (m->count < m->size) {
643 m->buf[m->count++] = c;
644 return 0;
645 }
646 return -1;
647}
648EXPORT_SYMBOL(seq_putc);
649
650int seq_puts(struct seq_file *m, const char *s)
651{
652 int len = strlen(s);
653 if (m->count + len < m->size) {
654 memcpy(m->buf + m->count, s, len);
655 m->count += len;
656 return 0;
657 }
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -0700658 seq_set_overflow(m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 return -1;
660}
661EXPORT_SYMBOL(seq_puts);
Pavel Emelianovbcf67e12007-07-10 17:22:26 -0700662
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700663/*
664 * A helper routine for putting decimal numbers without rich format of printf().
665 * only 'unsigned long long' is supported.
666 * This routine will put one byte delimiter + number into seq_file.
667 * This routine is very quick when you show lots of numbers.
668 * In usual cases, it will be better to use seq_printf(). It's easier to read.
669 */
670int seq_put_decimal_ull(struct seq_file *m, char delimiter,
671 unsigned long long num)
672{
673 int len;
674
675 if (m->count + 2 >= m->size) /* we'll write 2 bytes at least */
676 goto overflow;
677
KAMEZAWA Hiroyukibda7bad2012-03-23 15:02:54 -0700678 if (delimiter)
679 m->buf[m->count++] = delimiter;
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700680
681 if (num < 10) {
682 m->buf[m->count++] = num + '0';
683 return 0;
684 }
685
686 len = num_to_str(m->buf + m->count, m->size - m->count, num);
687 if (!len)
688 goto overflow;
689 m->count += len;
690 return 0;
691overflow:
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -0700692 seq_set_overflow(m);
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700693 return -1;
694}
695EXPORT_SYMBOL(seq_put_decimal_ull);
696
KAMEZAWA Hiroyukibda7bad2012-03-23 15:02:54 -0700697int seq_put_decimal_ll(struct seq_file *m, char delimiter,
698 long long num)
699{
700 if (num < 0) {
701 if (m->count + 3 >= m->size) {
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -0700702 seq_set_overflow(m);
KAMEZAWA Hiroyukibda7bad2012-03-23 15:02:54 -0700703 return -1;
704 }
705 if (delimiter)
706 m->buf[m->count++] = delimiter;
707 num = -num;
708 delimiter = '-';
709 }
710 return seq_put_decimal_ull(m, delimiter, num);
711
712}
713EXPORT_SYMBOL(seq_put_decimal_ll);
714
Peter Oberparleiter0b923602009-06-17 16:28:05 -0700715/**
716 * seq_write - write arbitrary data to buffer
717 * @seq: seq_file identifying the buffer to which data should be written
718 * @data: data address
719 * @len: number of bytes
720 *
721 * Return 0 on success, non-zero otherwise.
722 */
723int seq_write(struct seq_file *seq, const void *data, size_t len)
724{
725 if (seq->count + len < seq->size) {
726 memcpy(seq->buf + seq->count, data, len);
727 seq->count += len;
728 return 0;
729 }
KAMEZAWA Hiroyukie075f592012-03-23 15:02:55 -0700730 seq_set_overflow(seq);
Peter Oberparleiter0b923602009-06-17 16:28:05 -0700731 return -1;
732}
733EXPORT_SYMBOL(seq_write);
734
Tetsuo Handae7704c22013-11-14 14:31:56 -0800735/**
736 * seq_pad - write padding spaces to buffer
737 * @m: seq_file identifying the buffer to which data should be written
738 * @c: the byte to append after padding if non-zero
739 */
740void seq_pad(struct seq_file *m, char c)
741{
742 int size = m->pad_until - m->count;
743 if (size > 0)
744 seq_printf(m, "%*s", size, "");
745 if (c)
746 seq_putc(m, c);
747}
748EXPORT_SYMBOL(seq_pad);
749
Pavel Emelianovbcf67e12007-07-10 17:22:26 -0700750struct list_head *seq_list_start(struct list_head *head, loff_t pos)
751{
752 struct list_head *lh;
753
754 list_for_each(lh, head)
755 if (pos-- == 0)
756 return lh;
757
758 return NULL;
759}
Pavel Emelianovbcf67e12007-07-10 17:22:26 -0700760EXPORT_SYMBOL(seq_list_start);
761
762struct list_head *seq_list_start_head(struct list_head *head, loff_t pos)
763{
764 if (!pos)
765 return head;
766
767 return seq_list_start(head, pos - 1);
768}
Pavel Emelianovbcf67e12007-07-10 17:22:26 -0700769EXPORT_SYMBOL(seq_list_start_head);
770
771struct list_head *seq_list_next(void *v, struct list_head *head, loff_t *ppos)
772{
773 struct list_head *lh;
774
775 lh = ((struct list_head *)v)->next;
776 ++*ppos;
777 return lh == head ? NULL : lh;
778}
Pavel Emelianovbcf67e12007-07-10 17:22:26 -0700779EXPORT_SYMBOL(seq_list_next);
Li Zefan66655de2010-02-08 23:18:22 +0000780
781/**
782 * seq_hlist_start - start an iteration of a hlist
783 * @head: the head of the hlist
784 * @pos: the start position of the sequence
785 *
786 * Called at seq_file->op->start().
787 */
788struct hlist_node *seq_hlist_start(struct hlist_head *head, loff_t pos)
789{
790 struct hlist_node *node;
791
792 hlist_for_each(node, head)
793 if (pos-- == 0)
794 return node;
795 return NULL;
796}
797EXPORT_SYMBOL(seq_hlist_start);
798
799/**
800 * seq_hlist_start_head - start an iteration of a hlist
801 * @head: the head of the hlist
802 * @pos: the start position of the sequence
803 *
804 * Called at seq_file->op->start(). Call this function if you want to
805 * print a header at the top of the output.
806 */
807struct hlist_node *seq_hlist_start_head(struct hlist_head *head, loff_t pos)
808{
809 if (!pos)
810 return SEQ_START_TOKEN;
811
812 return seq_hlist_start(head, pos - 1);
813}
814EXPORT_SYMBOL(seq_hlist_start_head);
815
816/**
817 * seq_hlist_next - move to the next position of the hlist
818 * @v: the current iterator
819 * @head: the head of the hlist
Randy Dunlap138860b2010-03-04 09:37:12 -0800820 * @ppos: the current position
Li Zefan66655de2010-02-08 23:18:22 +0000821 *
822 * Called at seq_file->op->next().
823 */
824struct hlist_node *seq_hlist_next(void *v, struct hlist_head *head,
825 loff_t *ppos)
826{
827 struct hlist_node *node = v;
828
829 ++*ppos;
830 if (v == SEQ_START_TOKEN)
831 return head->first;
832 else
833 return node->next;
834}
835EXPORT_SYMBOL(seq_hlist_next);
stephen hemminger1cc52322010-02-22 07:57:17 +0000836
837/**
838 * seq_hlist_start_rcu - start an iteration of a hlist protected by RCU
839 * @head: the head of the hlist
840 * @pos: the start position of the sequence
841 *
842 * Called at seq_file->op->start().
843 *
844 * This list-traversal primitive may safely run concurrently with
845 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
846 * as long as the traversal is guarded by rcu_read_lock().
847 */
848struct hlist_node *seq_hlist_start_rcu(struct hlist_head *head,
849 loff_t pos)
850{
851 struct hlist_node *node;
852
853 __hlist_for_each_rcu(node, head)
854 if (pos-- == 0)
855 return node;
856 return NULL;
857}
858EXPORT_SYMBOL(seq_hlist_start_rcu);
859
860/**
861 * seq_hlist_start_head_rcu - start an iteration of a hlist protected by RCU
862 * @head: the head of the hlist
863 * @pos: the start position of the sequence
864 *
865 * Called at seq_file->op->start(). Call this function if you want to
866 * print a header at the top of the output.
867 *
868 * This list-traversal primitive may safely run concurrently with
869 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
870 * as long as the traversal is guarded by rcu_read_lock().
871 */
872struct hlist_node *seq_hlist_start_head_rcu(struct hlist_head *head,
873 loff_t pos)
874{
875 if (!pos)
876 return SEQ_START_TOKEN;
877
878 return seq_hlist_start_rcu(head, pos - 1);
879}
880EXPORT_SYMBOL(seq_hlist_start_head_rcu);
881
882/**
883 * seq_hlist_next_rcu - move to the next position of the hlist protected by RCU
884 * @v: the current iterator
885 * @head: the head of the hlist
Randy Dunlap138860b2010-03-04 09:37:12 -0800886 * @ppos: the current position
stephen hemminger1cc52322010-02-22 07:57:17 +0000887 *
888 * Called at seq_file->op->next().
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_next_rcu(void *v,
895 struct hlist_head *head,
896 loff_t *ppos)
897{
898 struct hlist_node *node = v;
899
900 ++*ppos;
901 if (v == SEQ_START_TOKEN)
902 return rcu_dereference(head->first);
903 else
904 return rcu_dereference(node->next);
905}
906EXPORT_SYMBOL(seq_hlist_next_rcu);