[PATCH] seq_file: move seq_read() flushing into a function

From: Vito Caputo
Date: Tue Oct 08 2019 - 22:02:33 EST


Consolidate some duplicated bookkeeping from seq_read() into a
function.

Signed-off-by: Vito Caputo <vcaputo@xxxxxxxxxxx>
---
fs/seq_file.c | 34 +++++++++++++++++++++-------------
1 file changed, 21 insertions(+), 13 deletions(-)

diff --git a/fs/seq_file.c b/fs/seq_file.c
index 1600034a929b..e5d2bccf5ac4 100644
--- a/fs/seq_file.c
+++ b/fs/seq_file.c
@@ -141,6 +141,25 @@ static int traverse(struct seq_file *m, loff_t offset)
return !m->buf ? -ENOMEM : -EAGAIN;
}

+static int flush(struct seq_file *m, char __user **buf, size_t *size, size_t *copied)
+{
+ int err = 0;
+ size_t n;
+
+ if (!m->count)
+ return err;
+ n = min(m->count, *size);
+ err = copy_to_user(*buf, m->buf + m->from, n);
+ if (err)
+ return err;
+ m->count -= n;
+ m->from += n;
+ *size -= n;
+ *buf += n;
+ *copied += n;
+ return err;
+}
+
/**
* seq_read - ->read() method for sequential files.
* @file: the file to read from
@@ -154,7 +173,6 @@ ssize_t seq_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
{
struct seq_file *m = file->private_data;
size_t copied = 0;
- size_t n;
void *p;
int err = 0;

@@ -207,15 +225,9 @@ ssize_t seq_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
}
/* if not empty - flush it first */
if (m->count) {
- n = min(m->count, size);
- err = copy_to_user(buf, m->buf + m->from, n);
+ err = flush(m, &buf, &size, &copied);
if (err)
goto Efault;
- m->count -= n;
- m->from += n;
- size -= n;
- buf += n;
- copied += n;
if (!size)
goto Done;
}
@@ -273,13 +285,9 @@ ssize_t seq_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
}
}
m->op->stop(m, p);
- n = min(m->count, size);
- err = copy_to_user(buf, m->buf, n);
+ err = flush(m, &buf, &size, &copied);
if (err)
goto Efault;
- copied += n;
- m->count -= n;
- m->from = n;
Done:
if (!copied)
copied = err;
--
2.11.0