[RFC 2/3] seq_file: add validate() operation to seq_operations

From: Suren Baghdasaryan
Date: Mon Jan 15 2024 - 13:39:22 EST


seq_file outputs data in chunks using seq_file.buf as the intermediate
storage before outputting the generated data for the current chunk. It is
possible for already buffered data to become stale before it gets reported.
In certain situations it is desirable to regenerate that data instead of
reporting the stale one. Provide a validate() operation called before
outputting the buffered data to allow users to validate buffered data.
To indicate valid data, user's validate callback should return 0, to
request regeneration of the stale data it should return -EAGAIN, any
other error will be considered fatal and read operation will be aborted.

Signed-off-by: Suren Baghdasaryan <surenb@xxxxxxxxxx>
---
fs/seq_file.c | 24 +++++++++++++++++++++++-
include/linux/seq_file.h | 1 +
2 files changed, 24 insertions(+), 1 deletion(-)

diff --git a/fs/seq_file.c b/fs/seq_file.c
index f5fdaf3b1572..77833bbe5909 100644
--- a/fs/seq_file.c
+++ b/fs/seq_file.c
@@ -172,6 +172,8 @@ ssize_t seq_read_iter(struct kiocb *iocb, struct iov_iter *iter)
{
struct seq_file *m = iocb->ki_filp->private_data;
size_t copied = 0;
+ loff_t orig_index;
+ size_t orig_count;
size_t n;
void *p;
int err = 0;
@@ -220,6 +222,10 @@ ssize_t seq_read_iter(struct kiocb *iocb, struct iov_iter *iter)
if (m->count) // hadn't managed to copy everything
goto Done;
}
+
+ orig_index = m->index;
+ orig_count = m->count;
+Again:
// get a non-empty record in the buffer
m->from = 0;
p = m->op->start(m, &m->index);
@@ -278,6 +284,22 @@ ssize_t seq_read_iter(struct kiocb *iocb, struct iov_iter *iter)
}
}
m->op->stop(m, p);
+ /* Note: we validate even if err<0 to prevent publishing copied data */
+ if (m->op->validate) {
+ int val_err = m->op->validate(m, p);
+
+ if (val_err) {
+ if (val_err == -EAGAIN) {
+ m->index = orig_index;
+ m->count = orig_count;
+ // data is stale, retry
+ goto Again;
+ }
+ // data is invalid, return the last error
+ err = val_err;
+ goto Done;
+ }
+ }
n = copy_to_iter(m->buf, m->count, iter);
copied += n;
m->count -= n;
@@ -572,7 +594,7 @@ 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_ACCOUNT);
+ struct seq_operations *op = kzalloc(sizeof(*op), GFP_KERNEL_ACCOUNT);
int res = -ENOMEM;

if (op) {
diff --git a/include/linux/seq_file.h b/include/linux/seq_file.h
index 234bcdb1fba4..d0fefac2990f 100644
--- a/include/linux/seq_file.h
+++ b/include/linux/seq_file.h
@@ -34,6 +34,7 @@ struct seq_operations {
void (*stop) (struct seq_file *m, void *v);
void * (*next) (struct seq_file *m, void *v, loff_t *pos);
int (*show) (struct seq_file *m, void *v);
+ int (*validate)(struct seq_file *m, void *v);
};

#define SEQ_SKIP 1
--
2.43.0.381.gb435a96ce8-goog