Skip to content

Commit

Permalink
seq_file: add seq_set_overflow(), seq_overflow()
Browse files Browse the repository at this point in the history
It is undocumented but a seq_file's overflow state is indicated by
m->count == m->size.  Add seq_set_overflow() and seq_overflow() to
set/check overflow status explicitly.

Based on an idea from Eric Dumazet.

[[email protected]: tweak code comment]
Signed-off-by: KAMEZAWA Hiroyuki <[email protected]>
Cc: Eric Dumazet <[email protected]>
Cc: Al Viro <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Signed-off-by: Linus Torvalds <[email protected]>
  • Loading branch information
hkamezawa authored and torvalds committed Mar 23, 2012
1 parent 1b26c9b commit e075f59
Showing 1 changed file with 26 additions and 10 deletions.
36 changes: 26 additions & 10 deletions fs/seq_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,22 @@
#include <asm/uaccess.h>
#include <asm/page.h>


/*
* seq_files have a buffer which can may overflow. When this happens a larger
* buffer is reallocated and all the data will be printed again.
* The overflow state is true when m->count == m->size.
*/
static bool seq_overflow(struct seq_file *m)
{
return m->count == m->size;
}

static void seq_set_overflow(struct seq_file *m)
{
m->count = m->size;
}

/**
* seq_open - initialize sequential file
* @file: file we initialize
Expand Down Expand Up @@ -92,7 +108,7 @@ static int traverse(struct seq_file *m, loff_t offset)
error = 0;
m->count = 0;
}
if (m->count == m->size)
if (seq_overflow(m))
goto Eoverflow;
if (pos + m->count > offset) {
m->from = offset - pos;
Expand Down Expand Up @@ -234,7 +250,7 @@ ssize_t seq_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
break;
}
err = m->op->show(m, p);
if (m->count == m->size || err) {
if (seq_overflow(m) || err) {
m->count = offs;
if (likely(err <= 0))
break;
Expand Down Expand Up @@ -361,7 +377,7 @@ int seq_escape(struct seq_file *m, const char *s, const char *esc)
*p++ = '0' + (c & 07);
continue;
}
m->count = m->size;
seq_set_overflow(m);
return -1;
}
m->count = p - m->buf;
Expand All @@ -383,7 +399,7 @@ int seq_printf(struct seq_file *m, const char *f, ...)
return 0;
}
}
m->count = m->size;
seq_set_overflow(m);
return -1;
}
EXPORT_SYMBOL(seq_printf);
Expand Down Expand Up @@ -512,7 +528,7 @@ int seq_bitmap(struct seq_file *m, const unsigned long *bits,
return 0;
}
}
m->count = m->size;
seq_set_overflow(m);
return -1;
}
EXPORT_SYMBOL(seq_bitmap);
Expand All @@ -528,7 +544,7 @@ int seq_bitmap_list(struct seq_file *m, const unsigned long *bits,
return 0;
}
}
m->count = m->size;
seq_set_overflow(m);
return -1;
}
EXPORT_SYMBOL(seq_bitmap_list);
Expand Down Expand Up @@ -639,7 +655,7 @@ int seq_puts(struct seq_file *m, const char *s)
m->count += len;
return 0;
}
m->count = m->size;
seq_set_overflow(m);
return -1;
}
EXPORT_SYMBOL(seq_puts);
Expand Down Expand Up @@ -673,7 +689,7 @@ int seq_put_decimal_ull(struct seq_file *m, char delimiter,
m->count += len;
return 0;
overflow:
m->count = m->size;
seq_set_overflow(m);
return -1;
}
EXPORT_SYMBOL(seq_put_decimal_ull);
Expand All @@ -683,7 +699,7 @@ int seq_put_decimal_ll(struct seq_file *m, char delimiter,
{
if (num < 0) {
if (m->count + 3 >= m->size) {
m->count = m->size;
seq_set_overflow(m);
return -1;
}
if (delimiter)
Expand Down Expand Up @@ -711,7 +727,7 @@ int seq_write(struct seq_file *seq, const void *data, size_t len)
seq->count += len;
return 0;
}
seq->count = seq->size;
seq_set_overflow(seq);
return -1;
}
EXPORT_SYMBOL(seq_write);
Expand Down

0 comments on commit e075f59

Please sign in to comment.