All of lore.kernel.org
 help / color / mirror / Atom feed
From: Christoph Hellwig <hch@lst.de>
To: Al Viro <viro@zeniv.linux.org.uk>
Cc: Greg KH <gregkh@linuxfoundation.org>,
	Linus Torvalds <torvalds@linux-foundation.org>,
	Alexey Dobriyan <adobriyan@gmail.com>,
	linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH 1/6] seq_file: add seq_read_iter
Date: Wed,  4 Nov 2020 09:27:33 +0100	[thread overview]
Message-ID: <20201104082738.1054792-2-hch@lst.de> (raw)
In-Reply-To: <20201104082738.1054792-1-hch@lst.de>

iov_iter based variant for reading a seq_file.  seq_read is
reimplemented on top of the iter variant.

Signed-off-by: Christoph Hellwig <hch@lst.de>
Tested-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
---
 fs/seq_file.c            | 45 ++++++++++++++++++++++++++++------------
 include/linux/seq_file.h |  1 +
 2 files changed, 33 insertions(+), 13 deletions(-)

diff --git a/fs/seq_file.c b/fs/seq_file.c
index 31219c1db17de3..3b20e21604e74a 100644
--- a/fs/seq_file.c
+++ b/fs/seq_file.c
@@ -18,6 +18,7 @@
 #include <linux/mm.h>
 #include <linux/printk.h>
 #include <linux/string_helpers.h>
+#include <linux/uio.h>
 
 #include <linux/uaccess.h>
 #include <asm/page.h>
@@ -146,7 +147,28 @@ static int traverse(struct seq_file *m, loff_t offset)
  */
 ssize_t seq_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
 {
-	struct seq_file *m = file->private_data;
+	struct iovec iov = { .iov_base = buf, .iov_len = size};
+	struct kiocb kiocb;
+	struct iov_iter iter;
+	ssize_t ret;
+
+	init_sync_kiocb(&kiocb, file);
+	iov_iter_init(&iter, READ, &iov, 1, size);
+
+	kiocb.ki_pos = *ppos;
+	ret = seq_read_iter(&kiocb, &iter);
+	*ppos = kiocb.ki_pos;
+	return ret;
+}
+EXPORT_SYMBOL(seq_read);
+
+/*
+ * Ready-made ->f_op->read_iter()
+ */
+ssize_t seq_read_iter(struct kiocb *iocb, struct iov_iter *iter)
+{
+	struct seq_file *m = iocb->ki_filp->private_data;
+	size_t size = iov_iter_count(iter);
 	size_t copied = 0;
 	size_t n;
 	void *p;
@@ -158,14 +180,14 @@ ssize_t seq_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
 	 * if request is to read from zero offset, reset iterator to first
 	 * record as it might have been already advanced by previous requests
 	 */
-	if (*ppos == 0) {
+	if (iocb->ki_pos == 0) {
 		m->index = 0;
 		m->count = 0;
 	}
 
-	/* Don't assume *ppos is where we left it */
-	if (unlikely(*ppos != m->read_pos)) {
-		while ((err = traverse(m, *ppos)) == -EAGAIN)
+	/* Don't assume ki_pos is where we left it */
+	if (unlikely(iocb->ki_pos != m->read_pos)) {
+		while ((err = traverse(m, iocb->ki_pos)) == -EAGAIN)
 			;
 		if (err) {
 			/* With prejudice... */
@@ -174,7 +196,7 @@ ssize_t seq_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
 			m->count = 0;
 			goto Done;
 		} else {
-			m->read_pos = *ppos;
+			m->read_pos = iocb->ki_pos;
 		}
 	}
 
@@ -187,13 +209,11 @@ 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);
-		if (err)
+		if (copy_to_iter(m->buf + m->from, n, iter) != n)
 			goto Efault;
 		m->count -= n;
 		m->from += n;
 		size -= n;
-		buf += n;
 		copied += n;
 		if (!size)
 			goto Done;
@@ -254,8 +274,7 @@ 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);
-	if (err)
+	if (copy_to_iter(m->buf, n, iter) != n)
 		goto Efault;
 	copied += n;
 	m->count -= n;
@@ -264,7 +283,7 @@ ssize_t seq_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
 	if (!copied)
 		copied = err;
 	else {
-		*ppos += copied;
+		iocb->ki_pos += copied;
 		m->read_pos += copied;
 	}
 	mutex_unlock(&m->lock);
@@ -276,7 +295,7 @@ ssize_t seq_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
 	err = -EFAULT;
 	goto Done;
 }
-EXPORT_SYMBOL(seq_read);
+EXPORT_SYMBOL(seq_read_iter);
 
 /**
  *	seq_lseek -	->llseek() method for sequential files.
diff --git a/include/linux/seq_file.h b/include/linux/seq_file.h
index 813614d4b71fbc..b83b3ae3c877f3 100644
--- a/include/linux/seq_file.h
+++ b/include/linux/seq_file.h
@@ -107,6 +107,7 @@ void seq_pad(struct seq_file *m, char c);
 char *mangle_path(char *s, const char *p, const char *esc);
 int seq_open(struct file *, const struct seq_operations *);
 ssize_t seq_read(struct file *, char __user *, size_t, loff_t *);
+ssize_t seq_read_iter(struct kiocb *iocb, struct iov_iter *iter);
 loff_t seq_lseek(struct file *, loff_t, int);
 int seq_release(struct inode *, struct file *);
 int seq_write(struct seq_file *seq, const void *data, size_t len);
-- 
2.28.0


  reply	other threads:[~2020-11-04  8:32 UTC|newest]

Thread overview: 43+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-11-04  8:27 support splice reads on seq_file based procfs files v2 Christoph Hellwig
2020-11-04  8:27 ` Christoph Hellwig [this message]
2020-11-10 21:32   ` [PATCH 1/6] seq_file: add seq_read_iter Al Viro
2020-11-10 21:35     ` Al Viro
2020-11-10 23:20       ` Al Viro
2020-11-11  7:55         ` Christoph Hellwig
2020-11-11 17:54         ` Linus Torvalds
2020-11-11 21:52           ` Al Viro
2020-11-11 22:21             ` Al Viro
2020-11-11 22:27               ` Linus Torvalds
2020-11-11 23:00                 ` Al Viro
2020-11-13 23:54               ` Nathan Chancellor
2020-11-14  1:17                 ` Al Viro
2020-11-14  3:01                   ` Nathan Chancellor
2020-11-14  3:54                     ` Al Viro
2020-11-14  4:14                       ` Nathan Chancellor
2020-11-14  5:50                         ` Al Viro
2020-11-14  6:19                           ` Nathan Chancellor
2020-11-14  7:00                             ` Al Viro
2020-11-14 20:50                               ` Al Viro
2020-11-15 15:53                                 ` Al Viro
2020-11-15 16:56                                   ` Linus Torvalds
2020-11-15 21:41                                   ` Nathan Chancellor
2020-11-15 23:38                                     ` Al Viro
2020-11-15 23:51                                       ` Nathan Chancellor
2020-11-16  0:25                                         ` Al Viro
2020-11-16  0:34                                           ` Nathan Chancellor
2020-11-16  3:29                                             ` Al Viro
2020-11-27 16:29                                               ` Christoph Hellwig
2020-12-08 16:35                                                 ` Christoph Hellwig
2020-12-08 18:34                                                   ` Linus Torvalds
2020-12-08 19:49                                                     ` Al Viro
2020-12-08 20:25                                                       ` Linus Torvalds
2020-12-08 20:53                                                         ` Al Viro
2020-12-08 21:01                                                           ` Linus Torvalds
2020-12-08 19:49                                                     ` Greg KH
2020-11-14 21:44                 ` Al Viro
2020-11-04  8:27 ` [PATCH 2/6] proc: wire up generic_file_splice_read for iter ops Christoph Hellwig
2020-11-04  8:27 ` [PATCH 3/6] proc/cpuinfo: switch to ->read_iter Christoph Hellwig
2020-11-04  8:27 ` [PATCH 4/6] proc/stat: " Christoph Hellwig
2020-11-04  8:27 ` [PATCH 5/6] proc "single files": " Christoph Hellwig
2020-11-04  8:27 ` [PATCH 6/6] proc "seq " Christoph Hellwig
2020-11-04 17:53 ` support splice reads on seq_file based procfs files v2 Linus Torvalds

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20201104082738.1054792-2-hch@lst.de \
    --to=hch@lst.de \
    --cc=adobriyan@gmail.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=torvalds@linux-foundation.org \
    --cc=viro@zeniv.linux.org.uk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.