blob: 5267098532bfc8a3a6c25026119a14c144d49f5c [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>
9#include <linux/module.h>
10#include <linux/seq_file.h>
11#include <linux/slab.h>
12
13#include <asm/uaccess.h>
14#include <asm/page.h>
15
16/**
17 * seq_open - initialize sequential file
18 * @file: file we initialize
19 * @op: method table describing the sequence
20 *
21 * seq_open() sets @file, associating it with a sequence described
22 * by @op. @op->start() sets the iterator up and returns the first
23 * element of sequence. @op->stop() shuts it down. @op->next()
24 * returns the next element of sequence. @op->show() prints element
25 * into the buffer. In case of error ->start() and ->next() return
26 * ERR_PTR(error). In the end of sequence they return %NULL. ->show()
27 * returns 0 in case of success and negative number in case of error.
Al Viro521b5d02008-03-28 00:46:41 -040028 * Returning SEQ_SKIP means "discard this element and move on".
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 */
Helge Deller15ad7cd2006-12-06 20:40:36 -080030int seq_open(struct file *file, const struct seq_operations *op)
Linus Torvalds1da177e2005-04-16 15:20:36 -070031{
Al Viro1abe77b2005-11-07 17:15:34 -050032 struct seq_file *p = file->private_data;
33
34 if (!p) {
35 p = kmalloc(sizeof(*p), GFP_KERNEL);
36 if (!p)
37 return -ENOMEM;
38 file->private_data = p;
39 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 memset(p, 0, sizeof(*p));
Ingo Molnar0ac17592006-03-23 03:00:37 -080041 mutex_init(&p->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 p->op = op;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44 /*
45 * Wrappers around seq_open(e.g. swaps_open) need to be
46 * aware of this. If they set f_version themselves, they
47 * should call seq_open first and then set f_version.
48 */
49 file->f_version = 0;
50
51 /* SEQ files support lseek, but not pread/pwrite */
52 file->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
53 return 0;
54}
55EXPORT_SYMBOL(seq_open);
56
Eric Biederman33da8892009-02-04 15:12:25 -080057static int traverse(struct seq_file *m, loff_t offset)
58{
59 loff_t pos = 0, index;
60 int error = 0;
61 void *p;
62
63 m->version = 0;
64 index = 0;
65 m->count = m->from = 0;
66 if (!offset) {
67 m->index = index;
68 return 0;
69 }
70 if (!m->buf) {
71 m->buf = kmalloc(m->size = PAGE_SIZE, GFP_KERNEL);
72 if (!m->buf)
73 return -ENOMEM;
74 }
75 p = m->op->start(m, &index);
76 while (p) {
77 error = PTR_ERR(p);
78 if (IS_ERR(p))
79 break;
80 error = m->op->show(m, p);
81 if (error < 0)
82 break;
83 if (unlikely(error)) {
84 error = 0;
85 m->count = 0;
86 }
87 if (m->count == m->size)
88 goto Eoverflow;
89 if (pos + m->count > offset) {
90 m->from = offset - pos;
91 m->count -= m->from;
92 m->index = index;
93 break;
94 }
95 pos += m->count;
96 m->count = 0;
97 if (pos == offset) {
98 index++;
99 m->index = index;
100 break;
101 }
102 p = m->op->next(m, p, &index);
103 }
104 m->op->stop(m, p);
Alexey Dobriyanf01d1d52009-02-06 00:30:05 +0300105 m->index = index;
Eric Biederman33da8892009-02-04 15:12:25 -0800106 return error;
107
108Eoverflow:
109 m->op->stop(m, p);
110 kfree(m->buf);
111 m->buf = kmalloc(m->size <<= 1, GFP_KERNEL);
112 return !m->buf ? -ENOMEM : -EAGAIN;
113}
114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115/**
116 * seq_read - ->read() method for sequential files.
Martin Waitz67be2dd2005-05-01 08:59:26 -0700117 * @file: the file to read from
118 * @buf: the buffer to read to
119 * @size: the maximum number of bytes to read
120 * @ppos: the current position in the file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 *
122 * Ready-made ->f_op->read()
123 */
124ssize_t seq_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
125{
126 struct seq_file *m = (struct seq_file *)file->private_data;
127 size_t copied = 0;
128 loff_t pos;
129 size_t n;
130 void *p;
131 int err = 0;
132
Ingo Molnar0ac17592006-03-23 03:00:37 -0800133 mutex_lock(&m->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 /*
135 * seq_file->op->..m_start/m_stop/m_next may do special actions
136 * or optimisations based on the file->f_version, so we want to
137 * pass the file->f_version to those methods.
138 *
139 * seq_file->version is just copy of f_version, and seq_file
140 * methods can treat it simply as file version.
141 * It is copied in first and copied out after all operations.
142 * It is convenient to have it as part of structure to avoid the
143 * need of passing another argument to all the seq_file methods.
144 */
145 m->version = file->f_version;
146 /* grab buffer if we didn't have one */
147 if (!m->buf) {
148 m->buf = kmalloc(m->size = PAGE_SIZE, GFP_KERNEL);
149 if (!m->buf)
150 goto Enomem;
151 }
152 /* if not empty - flush it first */
153 if (m->count) {
154 n = min(m->count, size);
155 err = copy_to_user(buf, m->buf + m->from, n);
156 if (err)
157 goto Efault;
158 m->count -= n;
159 m->from += n;
160 size -= n;
161 buf += n;
162 copied += n;
163 if (!m->count)
164 m->index++;
165 if (!size)
166 goto Done;
167 }
168 /* we need at least one record in buffer */
Al Viro4cdfe842008-08-24 07:45:33 -0400169 pos = m->index;
170 p = m->op->start(m, &pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 while (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 err = PTR_ERR(p);
173 if (!p || IS_ERR(p))
174 break;
175 err = m->op->show(m, p);
Al Viro521b5d02008-03-28 00:46:41 -0400176 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 break;
Al Viro521b5d02008-03-28 00:46:41 -0400178 if (unlikely(err))
179 m->count = 0;
Al Viro4cdfe842008-08-24 07:45:33 -0400180 if (unlikely(!m->count)) {
181 p = m->op->next(m, p, &pos);
182 m->index = pos;
183 continue;
184 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 if (m->count < m->size)
186 goto Fill;
187 m->op->stop(m, p);
188 kfree(m->buf);
189 m->buf = kmalloc(m->size <<= 1, GFP_KERNEL);
190 if (!m->buf)
191 goto Enomem;
192 m->count = 0;
193 m->version = 0;
Al Viro4cdfe842008-08-24 07:45:33 -0400194 pos = m->index;
195 p = m->op->start(m, &pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 }
197 m->op->stop(m, p);
198 m->count = 0;
199 goto Done;
200Fill:
201 /* they want more? let's try to get some more */
202 while (m->count < size) {
203 size_t offs = m->count;
204 loff_t next = pos;
205 p = m->op->next(m, p, &next);
206 if (!p || IS_ERR(p)) {
207 err = PTR_ERR(p);
208 break;
209 }
210 err = m->op->show(m, p);
Al Viro521b5d02008-03-28 00:46:41 -0400211 if (m->count == m->size || err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 m->count = offs;
Al Viro521b5d02008-03-28 00:46:41 -0400213 if (likely(err <= 0))
214 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 }
216 pos = next;
217 }
218 m->op->stop(m, p);
219 n = min(m->count, size);
220 err = copy_to_user(buf, m->buf, n);
221 if (err)
222 goto Efault;
223 copied += n;
224 m->count -= n;
225 if (m->count)
226 m->from = n;
227 else
228 pos++;
229 m->index = pos;
230Done:
231 if (!copied)
232 copied = err;
233 else
234 *ppos += copied;
235 file->f_version = m->version;
Ingo Molnar0ac17592006-03-23 03:00:37 -0800236 mutex_unlock(&m->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 return copied;
238Enomem:
239 err = -ENOMEM;
240 goto Done;
241Efault:
242 err = -EFAULT;
243 goto Done;
244}
245EXPORT_SYMBOL(seq_read);
246
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247/**
248 * seq_lseek - ->llseek() method for sequential files.
Martin Waitz67be2dd2005-05-01 08:59:26 -0700249 * @file: the file in question
250 * @offset: new position
251 * @origin: 0 for absolute, 1 for relative position
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 *
253 * Ready-made ->f_op->llseek()
254 */
255loff_t seq_lseek(struct file *file, loff_t offset, int origin)
256{
257 struct seq_file *m = (struct seq_file *)file->private_data;
David Sterba16abef02008-04-22 15:09:22 +0200258 loff_t retval = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
Ingo Molnar0ac17592006-03-23 03:00:37 -0800260 mutex_lock(&m->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 m->version = file->f_version;
262 switch (origin) {
263 case 1:
264 offset += file->f_pos;
265 case 0:
266 if (offset < 0)
267 break;
268 retval = offset;
269 if (offset != file->f_pos) {
270 while ((retval=traverse(m, offset)) == -EAGAIN)
271 ;
272 if (retval) {
273 /* with extreme prejudice... */
274 file->f_pos = 0;
275 m->version = 0;
276 m->index = 0;
277 m->count = 0;
278 } else {
279 retval = file->f_pos = offset;
280 }
281 }
282 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 file->f_version = m->version;
Alexey Dobriyan00c57462007-07-15 23:40:22 -0700284 mutex_unlock(&m->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 return retval;
286}
287EXPORT_SYMBOL(seq_lseek);
288
289/**
290 * seq_release - free the structures associated with sequential file.
291 * @file: file in question
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800292 * @inode: file->f_path.dentry->d_inode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 *
294 * Frees the structures associated with sequential file; can be used
295 * as ->f_op->release() if you don't have private data to destroy.
296 */
297int seq_release(struct inode *inode, struct file *file)
298{
299 struct seq_file *m = (struct seq_file *)file->private_data;
300 kfree(m->buf);
301 kfree(m);
302 return 0;
303}
304EXPORT_SYMBOL(seq_release);
305
306/**
307 * seq_escape - print string into buffer, escaping some characters
308 * @m: target buffer
309 * @s: string
310 * @esc: set of characters that need escaping
311 *
312 * Puts string into buffer, replacing each occurrence of character from
313 * @esc with usual octal escape. Returns 0 in case of success, -1 - in
314 * case of overflow.
315 */
316int seq_escape(struct seq_file *m, const char *s, const char *esc)
317{
318 char *end = m->buf + m->size;
319 char *p;
320 char c;
321
322 for (p = m->buf + m->count; (c = *s) != '\0' && p < end; s++) {
323 if (!strchr(esc, c)) {
324 *p++ = c;
325 continue;
326 }
327 if (p + 3 < end) {
328 *p++ = '\\';
329 *p++ = '0' + ((c & 0300) >> 6);
330 *p++ = '0' + ((c & 070) >> 3);
331 *p++ = '0' + (c & 07);
332 continue;
333 }
334 m->count = m->size;
335 return -1;
336 }
337 m->count = p - m->buf;
338 return 0;
339}
340EXPORT_SYMBOL(seq_escape);
341
342int seq_printf(struct seq_file *m, const char *f, ...)
343{
344 va_list args;
345 int len;
346
347 if (m->count < m->size) {
348 va_start(args, f);
349 len = vsnprintf(m->buf + m->count, m->size - m->count, f, args);
350 va_end(args);
351 if (m->count + len < m->size) {
352 m->count += len;
353 return 0;
354 }
355 }
356 m->count = m->size;
357 return -1;
358}
359EXPORT_SYMBOL(seq_printf);
360
Török Edwin74e2f332008-11-22 13:28:48 +0200361/**
Török Edwin958086d2008-11-23 23:24:53 +0200362 * mangle_path - mangle and copy path to buffer beginning
363 * @s: buffer start
364 * @p: beginning of path in above buffer
365 * @esc: set of characters that need escaping
Török Edwin74e2f332008-11-22 13:28:48 +0200366 *
367 * Copy the path from @p to @s, replacing each occurrence of character from
368 * @esc with usual octal escape.
369 * Returns pointer past last written character in @s, or NULL in case of
370 * failure.
371 */
372char *mangle_path(char *s, char *p, char *esc)
Ram Pai6092d042008-03-27 13:06:20 +0100373{
374 while (s <= p) {
375 char c = *p++;
376 if (!c) {
377 return s;
378 } else if (!strchr(esc, c)) {
379 *s++ = c;
380 } else if (s + 4 > p) {
381 break;
382 } else {
383 *s++ = '\\';
384 *s++ = '0' + ((c & 0300) >> 6);
385 *s++ = '0' + ((c & 070) >> 3);
386 *s++ = '0' + (c & 07);
387 }
388 }
389 return NULL;
390}
Ingo Molnar604094f2008-11-28 18:03:22 +0100391EXPORT_SYMBOL(mangle_path);
Ram Pai6092d042008-03-27 13:06:20 +0100392
Arjan van de Ven52afeef2008-12-01 14:35:00 -0800393/**
394 * seq_path - seq_file interface to print a pathname
395 * @m: the seq_file handle
396 * @path: the struct path to print
397 * @esc: set of characters to escape in the output
398 *
399 * return the absolute path of 'path', as represented by the
400 * dentry / mnt pair in the path parameter.
Ram Pai6092d042008-03-27 13:06:20 +0100401 */
Jan Blunckc32c2f62008-02-14 19:38:43 -0800402int seq_path(struct seq_file *m, struct path *path, char *esc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403{
404 if (m->count < m->size) {
405 char *s = m->buf + m->count;
Jan Blunckcf28b482008-02-14 19:38:44 -0800406 char *p = d_path(path, s, m->size - m->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 if (!IS_ERR(p)) {
Ram Pai6092d042008-03-27 13:06:20 +0100408 s = mangle_path(s, p, esc);
409 if (s) {
410 p = m->buf + m->count;
411 m->count = s - m->buf;
412 return s - p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 }
414 }
415 }
416 m->count = m->size;
417 return -1;
418}
419EXPORT_SYMBOL(seq_path);
420
Ram Pai6092d042008-03-27 13:06:20 +0100421/*
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +0100422 * Same as seq_path, but relative to supplied root.
423 *
424 * root may be changed, see __d_path().
425 */
426int seq_path_root(struct seq_file *m, struct path *path, struct path *root,
427 char *esc)
428{
429 int err = -ENAMETOOLONG;
430 if (m->count < m->size) {
431 char *s = m->buf + m->count;
432 char *p;
433
434 spin_lock(&dcache_lock);
435 p = __d_path(path, root, s, m->size - m->count);
436 spin_unlock(&dcache_lock);
437 err = PTR_ERR(p);
438 if (!IS_ERR(p)) {
439 s = mangle_path(s, p, esc);
440 if (s) {
441 p = m->buf + m->count;
442 m->count = s - m->buf;
443 return 0;
444 }
445 }
446 }
447 m->count = m->size;
448 return err;
449}
450
451/*
Ram Pai6092d042008-03-27 13:06:20 +0100452 * returns the path of the 'dentry' from the root of its filesystem.
453 */
454int seq_dentry(struct seq_file *m, struct dentry *dentry, char *esc)
455{
456 if (m->count < m->size) {
457 char *s = m->buf + m->count;
458 char *p = dentry_path(dentry, s, m->size - m->count);
459 if (!IS_ERR(p)) {
460 s = mangle_path(s, p, esc);
461 if (s) {
462 p = m->buf + m->count;
463 m->count = s - m->buf;
464 return s - p;
465 }
466 }
467 }
468 m->count = m->size;
469 return -1;
470}
471
Rusty Russellcb78a0c2008-12-30 09:05:14 +1030472int seq_bitmap(struct seq_file *m, const unsigned long *bits,
473 unsigned int nr_bits)
Alexey Dobriyan50ac2d62008-08-12 15:09:02 -0700474{
Lai Jiangshan85dd0302008-10-18 20:28:18 -0700475 if (m->count < m->size) {
476 int len = bitmap_scnprintf(m->buf + m->count,
477 m->size - m->count, bits, nr_bits);
478 if (m->count + len < m->size) {
479 m->count += len;
480 return 0;
481 }
Alexey Dobriyan50ac2d62008-08-12 15:09:02 -0700482 }
483 m->count = m->size;
484 return -1;
485}
Lai Jiangshan85dd0302008-10-18 20:28:18 -0700486EXPORT_SYMBOL(seq_bitmap);
Alexey Dobriyan50ac2d62008-08-12 15:09:02 -0700487
Lai Jiangshan3eda2012008-10-18 20:28:19 -0700488int seq_bitmap_list(struct seq_file *m, unsigned long *bits,
489 unsigned int nr_bits)
490{
491 if (m->count < m->size) {
492 int len = bitmap_scnlistprintf(m->buf + m->count,
493 m->size - m->count, bits, nr_bits);
494 if (m->count + len < m->size) {
495 m->count += len;
496 return 0;
497 }
498 }
499 m->count = m->size;
500 return -1;
501}
502EXPORT_SYMBOL(seq_bitmap_list);
503
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504static void *single_start(struct seq_file *p, loff_t *pos)
505{
506 return NULL + (*pos == 0);
507}
508
509static void *single_next(struct seq_file *p, void *v, loff_t *pos)
510{
511 ++*pos;
512 return NULL;
513}
514
515static void single_stop(struct seq_file *p, void *v)
516{
517}
518
519int single_open(struct file *file, int (*show)(struct seq_file *, void *),
520 void *data)
521{
522 struct seq_operations *op = kmalloc(sizeof(*op), GFP_KERNEL);
523 int res = -ENOMEM;
524
525 if (op) {
526 op->start = single_start;
527 op->next = single_next;
528 op->stop = single_stop;
529 op->show = show;
530 res = seq_open(file, op);
531 if (!res)
532 ((struct seq_file *)file->private_data)->private = data;
533 else
534 kfree(op);
535 }
536 return res;
537}
538EXPORT_SYMBOL(single_open);
539
540int single_release(struct inode *inode, struct file *file)
541{
Helge Deller15ad7cd2006-12-06 20:40:36 -0800542 const struct seq_operations *op = ((struct seq_file *)file->private_data)->op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 int res = seq_release(inode, file);
544 kfree(op);
545 return res;
546}
547EXPORT_SYMBOL(single_release);
548
549int seq_release_private(struct inode *inode, struct file *file)
550{
551 struct seq_file *seq = file->private_data;
552
553 kfree(seq->private);
554 seq->private = NULL;
555 return seq_release(inode, file);
556}
557EXPORT_SYMBOL(seq_release_private);
558
Pavel Emelyanov39699032007-10-10 02:28:42 -0700559void *__seq_open_private(struct file *f, const struct seq_operations *ops,
560 int psize)
561{
562 int rc;
563 void *private;
564 struct seq_file *seq;
565
566 private = kzalloc(psize, GFP_KERNEL);
567 if (private == NULL)
568 goto out;
569
570 rc = seq_open(f, ops);
571 if (rc < 0)
572 goto out_free;
573
574 seq = f->private_data;
575 seq->private = private;
576 return private;
577
578out_free:
579 kfree(private);
580out:
581 return NULL;
582}
583EXPORT_SYMBOL(__seq_open_private);
584
585int seq_open_private(struct file *filp, const struct seq_operations *ops,
586 int psize)
587{
588 return __seq_open_private(filp, ops, psize) ? 0 : -ENOMEM;
589}
590EXPORT_SYMBOL(seq_open_private);
591
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592int seq_putc(struct seq_file *m, char c)
593{
594 if (m->count < m->size) {
595 m->buf[m->count++] = c;
596 return 0;
597 }
598 return -1;
599}
600EXPORT_SYMBOL(seq_putc);
601
602int seq_puts(struct seq_file *m, const char *s)
603{
604 int len = strlen(s);
605 if (m->count + len < m->size) {
606 memcpy(m->buf + m->count, s, len);
607 m->count += len;
608 return 0;
609 }
610 m->count = m->size;
611 return -1;
612}
613EXPORT_SYMBOL(seq_puts);
Pavel Emelianovbcf67e12007-07-10 17:22:26 -0700614
615struct list_head *seq_list_start(struct list_head *head, loff_t pos)
616{
617 struct list_head *lh;
618
619 list_for_each(lh, head)
620 if (pos-- == 0)
621 return lh;
622
623 return NULL;
624}
625
626EXPORT_SYMBOL(seq_list_start);
627
628struct list_head *seq_list_start_head(struct list_head *head, loff_t pos)
629{
630 if (!pos)
631 return head;
632
633 return seq_list_start(head, pos - 1);
634}
635
636EXPORT_SYMBOL(seq_list_start_head);
637
638struct list_head *seq_list_next(void *v, struct list_head *head, loff_t *ppos)
639{
640 struct list_head *lh;
641
642 lh = ((struct list_head *)v)->next;
643 ++*ppos;
644 return lh == head ? NULL : lh;
645}
646
647EXPORT_SYMBOL(seq_list_next);