2005-04-17 06:20:36 +08:00
|
|
|
/*
|
|
|
|
* linux/fs/seq_file.c
|
|
|
|
*
|
|
|
|
* helper functions for making synthetic files from sequences of records.
|
|
|
|
* initial implementation -- AV, Oct 2001.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/fs.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/seq_file.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
|
|
|
|
#include <asm/uaccess.h>
|
|
|
|
#include <asm/page.h>
|
|
|
|
|
|
|
|
/**
|
|
|
|
* seq_open - initialize sequential file
|
|
|
|
* @file: file we initialize
|
|
|
|
* @op: method table describing the sequence
|
|
|
|
*
|
|
|
|
* seq_open() sets @file, associating it with a sequence described
|
|
|
|
* by @op. @op->start() sets the iterator up and returns the first
|
|
|
|
* element of sequence. @op->stop() shuts it down. @op->next()
|
|
|
|
* returns the next element of sequence. @op->show() prints element
|
|
|
|
* into the buffer. In case of error ->start() and ->next() return
|
|
|
|
* ERR_PTR(error). In the end of sequence they return %NULL. ->show()
|
|
|
|
* returns 0 in case of success and negative number in case of error.
|
2008-03-28 12:46:41 +08:00
|
|
|
* Returning SEQ_SKIP means "discard this element and move on".
|
2005-04-17 06:20:36 +08:00
|
|
|
*/
|
2006-12-07 12:40:36 +08:00
|
|
|
int seq_open(struct file *file, const struct seq_operations *op)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2005-11-08 06:15:34 +08:00
|
|
|
struct seq_file *p = file->private_data;
|
|
|
|
|
|
|
|
if (!p) {
|
|
|
|
p = kmalloc(sizeof(*p), GFP_KERNEL);
|
|
|
|
if (!p)
|
|
|
|
return -ENOMEM;
|
|
|
|
file->private_data = p;
|
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
memset(p, 0, sizeof(*p));
|
2006-03-23 19:00:37 +08:00
|
|
|
mutex_init(&p->lock);
|
2005-04-17 06:20:36 +08:00
|
|
|
p->op = op;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Wrappers around seq_open(e.g. swaps_open) need to be
|
|
|
|
* aware of this. If they set f_version themselves, they
|
|
|
|
* should call seq_open first and then set f_version.
|
|
|
|
*/
|
|
|
|
file->f_version = 0;
|
|
|
|
|
|
|
|
/* SEQ files support lseek, but not pread/pwrite */
|
|
|
|
file->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(seq_open);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* seq_read - ->read() method for sequential files.
|
2005-05-01 23:59:26 +08:00
|
|
|
* @file: the file to read from
|
|
|
|
* @buf: the buffer to read to
|
|
|
|
* @size: the maximum number of bytes to read
|
|
|
|
* @ppos: the current position in the file
|
2005-04-17 06:20:36 +08:00
|
|
|
*
|
|
|
|
* Ready-made ->f_op->read()
|
|
|
|
*/
|
|
|
|
ssize_t seq_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
|
|
|
|
{
|
|
|
|
struct seq_file *m = (struct seq_file *)file->private_data;
|
|
|
|
size_t copied = 0;
|
|
|
|
loff_t pos;
|
|
|
|
size_t n;
|
|
|
|
void *p;
|
|
|
|
int err = 0;
|
|
|
|
|
2006-03-23 19:00:37 +08:00
|
|
|
mutex_lock(&m->lock);
|
2005-04-17 06:20:36 +08:00
|
|
|
/*
|
|
|
|
* seq_file->op->..m_start/m_stop/m_next may do special actions
|
|
|
|
* or optimisations based on the file->f_version, so we want to
|
|
|
|
* pass the file->f_version to those methods.
|
|
|
|
*
|
|
|
|
* seq_file->version is just copy of f_version, and seq_file
|
|
|
|
* methods can treat it simply as file version.
|
|
|
|
* It is copied in first and copied out after all operations.
|
|
|
|
* It is convenient to have it as part of structure to avoid the
|
|
|
|
* need of passing another argument to all the seq_file methods.
|
|
|
|
*/
|
|
|
|
m->version = file->f_version;
|
|
|
|
/* grab buffer if we didn't have one */
|
|
|
|
if (!m->buf) {
|
|
|
|
m->buf = kmalloc(m->size = PAGE_SIZE, GFP_KERNEL);
|
|
|
|
if (!m->buf)
|
|
|
|
goto Enomem;
|
|
|
|
}
|
|
|
|
/* if not empty - flush it first */
|
|
|
|
if (m->count) {
|
|
|
|
n = min(m->count, size);
|
|
|
|
err = copy_to_user(buf, m->buf + m->from, n);
|
|
|
|
if (err)
|
|
|
|
goto Efault;
|
|
|
|
m->count -= n;
|
|
|
|
m->from += n;
|
|
|
|
size -= n;
|
|
|
|
buf += n;
|
|
|
|
copied += n;
|
|
|
|
if (!m->count)
|
|
|
|
m->index++;
|
|
|
|
if (!size)
|
|
|
|
goto Done;
|
|
|
|
}
|
|
|
|
/* we need at least one record in buffer */
|
|
|
|
while (1) {
|
|
|
|
pos = m->index;
|
|
|
|
p = m->op->start(m, &pos);
|
|
|
|
err = PTR_ERR(p);
|
|
|
|
if (!p || IS_ERR(p))
|
|
|
|
break;
|
|
|
|
err = m->op->show(m, p);
|
2008-03-28 12:46:41 +08:00
|
|
|
if (err < 0)
|
2005-04-17 06:20:36 +08:00
|
|
|
break;
|
2008-03-28 12:46:41 +08:00
|
|
|
if (unlikely(err))
|
|
|
|
m->count = 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
if (m->count < m->size)
|
|
|
|
goto Fill;
|
|
|
|
m->op->stop(m, p);
|
|
|
|
kfree(m->buf);
|
|
|
|
m->buf = kmalloc(m->size <<= 1, GFP_KERNEL);
|
|
|
|
if (!m->buf)
|
|
|
|
goto Enomem;
|
|
|
|
m->count = 0;
|
|
|
|
m->version = 0;
|
|
|
|
}
|
|
|
|
m->op->stop(m, p);
|
|
|
|
m->count = 0;
|
|
|
|
goto Done;
|
|
|
|
Fill:
|
|
|
|
/* they want more? let's try to get some more */
|
|
|
|
while (m->count < size) {
|
|
|
|
size_t offs = m->count;
|
|
|
|
loff_t next = pos;
|
|
|
|
p = m->op->next(m, p, &next);
|
|
|
|
if (!p || IS_ERR(p)) {
|
|
|
|
err = PTR_ERR(p);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
err = m->op->show(m, p);
|
2008-03-28 12:46:41 +08:00
|
|
|
if (m->count == m->size || err) {
|
2005-04-17 06:20:36 +08:00
|
|
|
m->count = offs;
|
2008-03-28 12:46:41 +08:00
|
|
|
if (likely(err <= 0))
|
|
|
|
break;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
pos = next;
|
|
|
|
}
|
|
|
|
m->op->stop(m, p);
|
|
|
|
n = min(m->count, size);
|
|
|
|
err = copy_to_user(buf, m->buf, n);
|
|
|
|
if (err)
|
|
|
|
goto Efault;
|
|
|
|
copied += n;
|
|
|
|
m->count -= n;
|
|
|
|
if (m->count)
|
|
|
|
m->from = n;
|
|
|
|
else
|
|
|
|
pos++;
|
|
|
|
m->index = pos;
|
|
|
|
Done:
|
|
|
|
if (!copied)
|
|
|
|
copied = err;
|
|
|
|
else
|
|
|
|
*ppos += copied;
|
|
|
|
file->f_version = m->version;
|
2006-03-23 19:00:37 +08:00
|
|
|
mutex_unlock(&m->lock);
|
2005-04-17 06:20:36 +08:00
|
|
|
return copied;
|
|
|
|
Enomem:
|
|
|
|
err = -ENOMEM;
|
|
|
|
goto Done;
|
|
|
|
Efault:
|
|
|
|
err = -EFAULT;
|
|
|
|
goto Done;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(seq_read);
|
|
|
|
|
|
|
|
static int traverse(struct seq_file *m, loff_t offset)
|
|
|
|
{
|
seq_file: more atomicity in traverse()
Original problem: in some circumstances seq_file interface can present
infinite proc file to the following script when normally said proc file is
finite:
while read line; do
[do something with $line]
done </proc/$FILE
bash, to implement such loop does essentially
read(0, buf, 128);
[find \n]
lseek(0, -difference, SEEK_CUR);
Consider, proc file prints list of objects each of them consists of many
lines, each line is shorter than 128 bytes.
Two objects in list, with ->index'es being 0 and 1. Current one is 1, as
bash prints second object line by line.
Imagine first object being removed right before lseek().
traverse() will be called, because there is negative offset.
traverse() will reset ->index to 0 (!).
traverse() will call ->next() and get NULL in any usual iterate-over-list
code using list_for_each_entry_continue() and such. There is one object in
list now after all...
traverse() will return 0, lseek() will update file position and pretend
everything is OK.
So, what we have now: ->f_pos points to place where second object will be
printed, but ->index is 0. seq_read() instead of returning EOF, will start
printing first line of first object every time it's called, until enough
objects are added to ->f_pos return in bounds.
Fix is to update ->index only after we're sure we saw enough objects down
the road.
Signed-off-by: Alexey Dobriyan <adobriyan@sw.ru>
Cc: Al Viro <viro@zeniv.linux.org.uk>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2007-07-16 14:40:39 +08:00
|
|
|
loff_t pos = 0, index;
|
2005-04-17 06:20:36 +08:00
|
|
|
int error = 0;
|
|
|
|
void *p;
|
|
|
|
|
|
|
|
m->version = 0;
|
seq_file: more atomicity in traverse()
Original problem: in some circumstances seq_file interface can present
infinite proc file to the following script when normally said proc file is
finite:
while read line; do
[do something with $line]
done </proc/$FILE
bash, to implement such loop does essentially
read(0, buf, 128);
[find \n]
lseek(0, -difference, SEEK_CUR);
Consider, proc file prints list of objects each of them consists of many
lines, each line is shorter than 128 bytes.
Two objects in list, with ->index'es being 0 and 1. Current one is 1, as
bash prints second object line by line.
Imagine first object being removed right before lseek().
traverse() will be called, because there is negative offset.
traverse() will reset ->index to 0 (!).
traverse() will call ->next() and get NULL in any usual iterate-over-list
code using list_for_each_entry_continue() and such. There is one object in
list now after all...
traverse() will return 0, lseek() will update file position and pretend
everything is OK.
So, what we have now: ->f_pos points to place where second object will be
printed, but ->index is 0. seq_read() instead of returning EOF, will start
printing first line of first object every time it's called, until enough
objects are added to ->f_pos return in bounds.
Fix is to update ->index only after we're sure we saw enough objects down
the road.
Signed-off-by: Alexey Dobriyan <adobriyan@sw.ru>
Cc: Al Viro <viro@zeniv.linux.org.uk>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2007-07-16 14:40:39 +08:00
|
|
|
index = 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
m->count = m->from = 0;
|
seq_file: more atomicity in traverse()
Original problem: in some circumstances seq_file interface can present
infinite proc file to the following script when normally said proc file is
finite:
while read line; do
[do something with $line]
done </proc/$FILE
bash, to implement such loop does essentially
read(0, buf, 128);
[find \n]
lseek(0, -difference, SEEK_CUR);
Consider, proc file prints list of objects each of them consists of many
lines, each line is shorter than 128 bytes.
Two objects in list, with ->index'es being 0 and 1. Current one is 1, as
bash prints second object line by line.
Imagine first object being removed right before lseek().
traverse() will be called, because there is negative offset.
traverse() will reset ->index to 0 (!).
traverse() will call ->next() and get NULL in any usual iterate-over-list
code using list_for_each_entry_continue() and such. There is one object in
list now after all...
traverse() will return 0, lseek() will update file position and pretend
everything is OK.
So, what we have now: ->f_pos points to place where second object will be
printed, but ->index is 0. seq_read() instead of returning EOF, will start
printing first line of first object every time it's called, until enough
objects are added to ->f_pos return in bounds.
Fix is to update ->index only after we're sure we saw enough objects down
the road.
Signed-off-by: Alexey Dobriyan <adobriyan@sw.ru>
Cc: Al Viro <viro@zeniv.linux.org.uk>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2007-07-16 14:40:39 +08:00
|
|
|
if (!offset) {
|
|
|
|
m->index = index;
|
2005-04-17 06:20:36 +08:00
|
|
|
return 0;
|
seq_file: more atomicity in traverse()
Original problem: in some circumstances seq_file interface can present
infinite proc file to the following script when normally said proc file is
finite:
while read line; do
[do something with $line]
done </proc/$FILE
bash, to implement such loop does essentially
read(0, buf, 128);
[find \n]
lseek(0, -difference, SEEK_CUR);
Consider, proc file prints list of objects each of them consists of many
lines, each line is shorter than 128 bytes.
Two objects in list, with ->index'es being 0 and 1. Current one is 1, as
bash prints second object line by line.
Imagine first object being removed right before lseek().
traverse() will be called, because there is negative offset.
traverse() will reset ->index to 0 (!).
traverse() will call ->next() and get NULL in any usual iterate-over-list
code using list_for_each_entry_continue() and such. There is one object in
list now after all...
traverse() will return 0, lseek() will update file position and pretend
everything is OK.
So, what we have now: ->f_pos points to place where second object will be
printed, but ->index is 0. seq_read() instead of returning EOF, will start
printing first line of first object every time it's called, until enough
objects are added to ->f_pos return in bounds.
Fix is to update ->index only after we're sure we saw enough objects down
the road.
Signed-off-by: Alexey Dobriyan <adobriyan@sw.ru>
Cc: Al Viro <viro@zeniv.linux.org.uk>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2007-07-16 14:40:39 +08:00
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
if (!m->buf) {
|
|
|
|
m->buf = kmalloc(m->size = PAGE_SIZE, GFP_KERNEL);
|
|
|
|
if (!m->buf)
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
seq_file: more atomicity in traverse()
Original problem: in some circumstances seq_file interface can present
infinite proc file to the following script when normally said proc file is
finite:
while read line; do
[do something with $line]
done </proc/$FILE
bash, to implement such loop does essentially
read(0, buf, 128);
[find \n]
lseek(0, -difference, SEEK_CUR);
Consider, proc file prints list of objects each of them consists of many
lines, each line is shorter than 128 bytes.
Two objects in list, with ->index'es being 0 and 1. Current one is 1, as
bash prints second object line by line.
Imagine first object being removed right before lseek().
traverse() will be called, because there is negative offset.
traverse() will reset ->index to 0 (!).
traverse() will call ->next() and get NULL in any usual iterate-over-list
code using list_for_each_entry_continue() and such. There is one object in
list now after all...
traverse() will return 0, lseek() will update file position and pretend
everything is OK.
So, what we have now: ->f_pos points to place where second object will be
printed, but ->index is 0. seq_read() instead of returning EOF, will start
printing first line of first object every time it's called, until enough
objects are added to ->f_pos return in bounds.
Fix is to update ->index only after we're sure we saw enough objects down
the road.
Signed-off-by: Alexey Dobriyan <adobriyan@sw.ru>
Cc: Al Viro <viro@zeniv.linux.org.uk>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2007-07-16 14:40:39 +08:00
|
|
|
p = m->op->start(m, &index);
|
2005-04-17 06:20:36 +08:00
|
|
|
while (p) {
|
|
|
|
error = PTR_ERR(p);
|
|
|
|
if (IS_ERR(p))
|
|
|
|
break;
|
|
|
|
error = m->op->show(m, p);
|
2008-03-28 12:46:41 +08:00
|
|
|
if (error < 0)
|
2005-04-17 06:20:36 +08:00
|
|
|
break;
|
2008-03-28 12:46:41 +08:00
|
|
|
if (unlikely(error)) {
|
|
|
|
error = 0;
|
|
|
|
m->count = 0;
|
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
if (m->count == m->size)
|
|
|
|
goto Eoverflow;
|
|
|
|
if (pos + m->count > offset) {
|
|
|
|
m->from = offset - pos;
|
|
|
|
m->count -= m->from;
|
seq_file: more atomicity in traverse()
Original problem: in some circumstances seq_file interface can present
infinite proc file to the following script when normally said proc file is
finite:
while read line; do
[do something with $line]
done </proc/$FILE
bash, to implement such loop does essentially
read(0, buf, 128);
[find \n]
lseek(0, -difference, SEEK_CUR);
Consider, proc file prints list of objects each of them consists of many
lines, each line is shorter than 128 bytes.
Two objects in list, with ->index'es being 0 and 1. Current one is 1, as
bash prints second object line by line.
Imagine first object being removed right before lseek().
traverse() will be called, because there is negative offset.
traverse() will reset ->index to 0 (!).
traverse() will call ->next() and get NULL in any usual iterate-over-list
code using list_for_each_entry_continue() and such. There is one object in
list now after all...
traverse() will return 0, lseek() will update file position and pretend
everything is OK.
So, what we have now: ->f_pos points to place where second object will be
printed, but ->index is 0. seq_read() instead of returning EOF, will start
printing first line of first object every time it's called, until enough
objects are added to ->f_pos return in bounds.
Fix is to update ->index only after we're sure we saw enough objects down
the road.
Signed-off-by: Alexey Dobriyan <adobriyan@sw.ru>
Cc: Al Viro <viro@zeniv.linux.org.uk>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2007-07-16 14:40:39 +08:00
|
|
|
m->index = index;
|
2005-04-17 06:20:36 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
pos += m->count;
|
|
|
|
m->count = 0;
|
|
|
|
if (pos == offset) {
|
seq_file: more atomicity in traverse()
Original problem: in some circumstances seq_file interface can present
infinite proc file to the following script when normally said proc file is
finite:
while read line; do
[do something with $line]
done </proc/$FILE
bash, to implement such loop does essentially
read(0, buf, 128);
[find \n]
lseek(0, -difference, SEEK_CUR);
Consider, proc file prints list of objects each of them consists of many
lines, each line is shorter than 128 bytes.
Two objects in list, with ->index'es being 0 and 1. Current one is 1, as
bash prints second object line by line.
Imagine first object being removed right before lseek().
traverse() will be called, because there is negative offset.
traverse() will reset ->index to 0 (!).
traverse() will call ->next() and get NULL in any usual iterate-over-list
code using list_for_each_entry_continue() and such. There is one object in
list now after all...
traverse() will return 0, lseek() will update file position and pretend
everything is OK.
So, what we have now: ->f_pos points to place where second object will be
printed, but ->index is 0. seq_read() instead of returning EOF, will start
printing first line of first object every time it's called, until enough
objects are added to ->f_pos return in bounds.
Fix is to update ->index only after we're sure we saw enough objects down
the road.
Signed-off-by: Alexey Dobriyan <adobriyan@sw.ru>
Cc: Al Viro <viro@zeniv.linux.org.uk>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2007-07-16 14:40:39 +08:00
|
|
|
index++;
|
|
|
|
m->index = index;
|
2005-04-17 06:20:36 +08:00
|
|
|
break;
|
|
|
|
}
|
seq_file: more atomicity in traverse()
Original problem: in some circumstances seq_file interface can present
infinite proc file to the following script when normally said proc file is
finite:
while read line; do
[do something with $line]
done </proc/$FILE
bash, to implement such loop does essentially
read(0, buf, 128);
[find \n]
lseek(0, -difference, SEEK_CUR);
Consider, proc file prints list of objects each of them consists of many
lines, each line is shorter than 128 bytes.
Two objects in list, with ->index'es being 0 and 1. Current one is 1, as
bash prints second object line by line.
Imagine first object being removed right before lseek().
traverse() will be called, because there is negative offset.
traverse() will reset ->index to 0 (!).
traverse() will call ->next() and get NULL in any usual iterate-over-list
code using list_for_each_entry_continue() and such. There is one object in
list now after all...
traverse() will return 0, lseek() will update file position and pretend
everything is OK.
So, what we have now: ->f_pos points to place where second object will be
printed, but ->index is 0. seq_read() instead of returning EOF, will start
printing first line of first object every time it's called, until enough
objects are added to ->f_pos return in bounds.
Fix is to update ->index only after we're sure we saw enough objects down
the road.
Signed-off-by: Alexey Dobriyan <adobriyan@sw.ru>
Cc: Al Viro <viro@zeniv.linux.org.uk>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2007-07-16 14:40:39 +08:00
|
|
|
p = m->op->next(m, p, &index);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
m->op->stop(m, p);
|
|
|
|
return error;
|
|
|
|
|
|
|
|
Eoverflow:
|
|
|
|
m->op->stop(m, p);
|
|
|
|
kfree(m->buf);
|
|
|
|
m->buf = kmalloc(m->size <<= 1, GFP_KERNEL);
|
|
|
|
return !m->buf ? -ENOMEM : -EAGAIN;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* seq_lseek - ->llseek() method for sequential files.
|
2005-05-01 23:59:26 +08:00
|
|
|
* @file: the file in question
|
|
|
|
* @offset: new position
|
|
|
|
* @origin: 0 for absolute, 1 for relative position
|
2005-04-17 06:20:36 +08:00
|
|
|
*
|
|
|
|
* Ready-made ->f_op->llseek()
|
|
|
|
*/
|
|
|
|
loff_t seq_lseek(struct file *file, loff_t offset, int origin)
|
|
|
|
{
|
|
|
|
struct seq_file *m = (struct seq_file *)file->private_data;
|
2008-04-22 21:09:22 +08:00
|
|
|
loff_t retval = -EINVAL;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2006-03-23 19:00:37 +08:00
|
|
|
mutex_lock(&m->lock);
|
2005-04-17 06:20:36 +08:00
|
|
|
m->version = file->f_version;
|
|
|
|
switch (origin) {
|
|
|
|
case 1:
|
|
|
|
offset += file->f_pos;
|
|
|
|
case 0:
|
|
|
|
if (offset < 0)
|
|
|
|
break;
|
|
|
|
retval = offset;
|
|
|
|
if (offset != file->f_pos) {
|
|
|
|
while ((retval=traverse(m, offset)) == -EAGAIN)
|
|
|
|
;
|
|
|
|
if (retval) {
|
|
|
|
/* with extreme prejudice... */
|
|
|
|
file->f_pos = 0;
|
|
|
|
m->version = 0;
|
|
|
|
m->index = 0;
|
|
|
|
m->count = 0;
|
|
|
|
} else {
|
|
|
|
retval = file->f_pos = offset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
file->f_version = m->version;
|
2007-07-16 14:40:22 +08:00
|
|
|
mutex_unlock(&m->lock);
|
2005-04-17 06:20:36 +08:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(seq_lseek);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* seq_release - free the structures associated with sequential file.
|
|
|
|
* @file: file in question
|
2006-12-08 18:36:35 +08:00
|
|
|
* @inode: file->f_path.dentry->d_inode
|
2005-04-17 06:20:36 +08:00
|
|
|
*
|
|
|
|
* Frees the structures associated with sequential file; can be used
|
|
|
|
* as ->f_op->release() if you don't have private data to destroy.
|
|
|
|
*/
|
|
|
|
int seq_release(struct inode *inode, struct file *file)
|
|
|
|
{
|
|
|
|
struct seq_file *m = (struct seq_file *)file->private_data;
|
|
|
|
kfree(m->buf);
|
|
|
|
kfree(m);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(seq_release);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* seq_escape - print string into buffer, escaping some characters
|
|
|
|
* @m: target buffer
|
|
|
|
* @s: string
|
|
|
|
* @esc: set of characters that need escaping
|
|
|
|
*
|
|
|
|
* Puts string into buffer, replacing each occurrence of character from
|
|
|
|
* @esc with usual octal escape. Returns 0 in case of success, -1 - in
|
|
|
|
* case of overflow.
|
|
|
|
*/
|
|
|
|
int seq_escape(struct seq_file *m, const char *s, const char *esc)
|
|
|
|
{
|
|
|
|
char *end = m->buf + m->size;
|
|
|
|
char *p;
|
|
|
|
char c;
|
|
|
|
|
|
|
|
for (p = m->buf + m->count; (c = *s) != '\0' && p < end; s++) {
|
|
|
|
if (!strchr(esc, c)) {
|
|
|
|
*p++ = c;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (p + 3 < end) {
|
|
|
|
*p++ = '\\';
|
|
|
|
*p++ = '0' + ((c & 0300) >> 6);
|
|
|
|
*p++ = '0' + ((c & 070) >> 3);
|
|
|
|
*p++ = '0' + (c & 07);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
m->count = m->size;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
m->count = p - m->buf;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(seq_escape);
|
|
|
|
|
|
|
|
int seq_printf(struct seq_file *m, const char *f, ...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
if (m->count < m->size) {
|
|
|
|
va_start(args, f);
|
|
|
|
len = vsnprintf(m->buf + m->count, m->size - m->count, f, args);
|
|
|
|
va_end(args);
|
|
|
|
if (m->count + len < m->size) {
|
|
|
|
m->count += len;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m->count = m->size;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(seq_printf);
|
|
|
|
|
2008-02-15 11:38:43 +08:00
|
|
|
int seq_path(struct seq_file *m, struct path *path, char *esc)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
if (m->count < m->size) {
|
|
|
|
char *s = m->buf + m->count;
|
2008-02-15 11:38:44 +08:00
|
|
|
char *p = d_path(path, s, m->size - m->count);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (!IS_ERR(p)) {
|
|
|
|
while (s <= p) {
|
|
|
|
char c = *p++;
|
|
|
|
if (!c) {
|
|
|
|
p = m->buf + m->count;
|
|
|
|
m->count = s - m->buf;
|
|
|
|
return s - p;
|
|
|
|
} else if (!strchr(esc, c)) {
|
|
|
|
*s++ = c;
|
|
|
|
} else if (s + 4 > p) {
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
*s++ = '\\';
|
|
|
|
*s++ = '0' + ((c & 0300) >> 6);
|
|
|
|
*s++ = '0' + ((c & 070) >> 3);
|
|
|
|
*s++ = '0' + (c & 07);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m->count = m->size;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(seq_path);
|
|
|
|
|
|
|
|
static void *single_start(struct seq_file *p, loff_t *pos)
|
|
|
|
{
|
|
|
|
return NULL + (*pos == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *single_next(struct seq_file *p, void *v, loff_t *pos)
|
|
|
|
{
|
|
|
|
++*pos;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void single_stop(struct seq_file *p, void *v)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
int single_open(struct file *file, int (*show)(struct seq_file *, void *),
|
|
|
|
void *data)
|
|
|
|
{
|
|
|
|
struct seq_operations *op = kmalloc(sizeof(*op), GFP_KERNEL);
|
|
|
|
int res = -ENOMEM;
|
|
|
|
|
|
|
|
if (op) {
|
|
|
|
op->start = single_start;
|
|
|
|
op->next = single_next;
|
|
|
|
op->stop = single_stop;
|
|
|
|
op->show = show;
|
|
|
|
res = seq_open(file, op);
|
|
|
|
if (!res)
|
|
|
|
((struct seq_file *)file->private_data)->private = data;
|
|
|
|
else
|
|
|
|
kfree(op);
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(single_open);
|
|
|
|
|
|
|
|
int single_release(struct inode *inode, struct file *file)
|
|
|
|
{
|
2006-12-07 12:40:36 +08:00
|
|
|
const struct seq_operations *op = ((struct seq_file *)file->private_data)->op;
|
2005-04-17 06:20:36 +08:00
|
|
|
int res = seq_release(inode, file);
|
|
|
|
kfree(op);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(single_release);
|
|
|
|
|
|
|
|
int seq_release_private(struct inode *inode, struct file *file)
|
|
|
|
{
|
|
|
|
struct seq_file *seq = file->private_data;
|
|
|
|
|
|
|
|
kfree(seq->private);
|
|
|
|
seq->private = NULL;
|
|
|
|
return seq_release(inode, file);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(seq_release_private);
|
|
|
|
|
2007-10-10 17:28:42 +08:00
|
|
|
void *__seq_open_private(struct file *f, const struct seq_operations *ops,
|
|
|
|
int psize)
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
void *private;
|
|
|
|
struct seq_file *seq;
|
|
|
|
|
|
|
|
private = kzalloc(psize, GFP_KERNEL);
|
|
|
|
if (private == NULL)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
rc = seq_open(f, ops);
|
|
|
|
if (rc < 0)
|
|
|
|
goto out_free;
|
|
|
|
|
|
|
|
seq = f->private_data;
|
|
|
|
seq->private = private;
|
|
|
|
return private;
|
|
|
|
|
|
|
|
out_free:
|
|
|
|
kfree(private);
|
|
|
|
out:
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(__seq_open_private);
|
|
|
|
|
|
|
|
int seq_open_private(struct file *filp, const struct seq_operations *ops,
|
|
|
|
int psize)
|
|
|
|
{
|
|
|
|
return __seq_open_private(filp, ops, psize) ? 0 : -ENOMEM;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(seq_open_private);
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
int seq_putc(struct seq_file *m, char c)
|
|
|
|
{
|
|
|
|
if (m->count < m->size) {
|
|
|
|
m->buf[m->count++] = c;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(seq_putc);
|
|
|
|
|
|
|
|
int seq_puts(struct seq_file *m, const char *s)
|
|
|
|
{
|
|
|
|
int len = strlen(s);
|
|
|
|
if (m->count + len < m->size) {
|
|
|
|
memcpy(m->buf + m->count, s, len);
|
|
|
|
m->count += len;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
m->count = m->size;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(seq_puts);
|
2007-07-11 08:22:26 +08:00
|
|
|
|
|
|
|
struct list_head *seq_list_start(struct list_head *head, loff_t pos)
|
|
|
|
{
|
|
|
|
struct list_head *lh;
|
|
|
|
|
|
|
|
list_for_each(lh, head)
|
|
|
|
if (pos-- == 0)
|
|
|
|
return lh;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
EXPORT_SYMBOL(seq_list_start);
|
|
|
|
|
|
|
|
struct list_head *seq_list_start_head(struct list_head *head, loff_t pos)
|
|
|
|
{
|
|
|
|
if (!pos)
|
|
|
|
return head;
|
|
|
|
|
|
|
|
return seq_list_start(head, pos - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
EXPORT_SYMBOL(seq_list_start_head);
|
|
|
|
|
|
|
|
struct list_head *seq_list_next(void *v, struct list_head *head, loff_t *ppos)
|
|
|
|
{
|
|
|
|
struct list_head *lh;
|
|
|
|
|
|
|
|
lh = ((struct list_head *)v)->next;
|
|
|
|
++*ppos;
|
|
|
|
return lh == head ? NULL : lh;
|
|
|
|
}
|
|
|
|
|
|
|
|
EXPORT_SYMBOL(seq_list_next);
|