public inbox for [email protected]
 help / color / mirror / Atom feed
From: Mark Harmstone <[email protected]>
To: <[email protected]>, <[email protected]>
Cc: Mark Harmstone <[email protected]>
Subject: [PATCH 3/6] btrfs: add btrfs_encoded_read_finish
Date: Fri, 23 Aug 2024 17:27:45 +0100	[thread overview]
Message-ID: <[email protected]> (raw)
In-Reply-To: <[email protected]>

Move the end of btrfs_ioctl_encoded_read, responsible for copying to
userspace and cleanup, into its own function.

Signed-off-by: Mark Harmstone <[email protected]>
---
 fs/btrfs/btrfs_inode.h |  1 +
 fs/btrfs/inode.c       | 29 +++++++++++-----------------
 fs/btrfs/ioctl.c       | 43 ++++++++++++++++++++++++++++++------------
 3 files changed, 43 insertions(+), 30 deletions(-)

diff --git a/fs/btrfs/btrfs_inode.h b/fs/btrfs/btrfs_inode.h
index 5cd4308bd337..f4d77c3bb544 100644
--- a/fs/btrfs/btrfs_inode.h
+++ b/fs/btrfs/btrfs_inode.h
@@ -619,6 +619,7 @@ struct btrfs_encoded_read_private {
 	struct iov_iter iter;
 	struct btrfs_ioctl_encoded_io_args args;
 	struct file *file;
+	void __user *copy_out;
 };
 
 ssize_t btrfs_encoded_read(struct btrfs_encoded_read_private *priv);
diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index c1292e58366a..1e53977a4854 100644
--- a/fs/btrfs/inode.c
+++ b/fs/btrfs/inode.c
@@ -9169,13 +9169,13 @@ static ssize_t btrfs_encoded_read_regular(struct btrfs_encoded_read_private *pri
 
 	priv->nr_pages = DIV_ROUND_UP(disk_io_size, PAGE_SIZE);
 	priv->pages = kcalloc(priv->nr_pages, sizeof(struct page *), GFP_NOFS);
-	if (!priv->pages)
+	if (!priv->pages) {
+		priv->nr_pages = 0;
 		return -ENOMEM;
+	}
 	ret = btrfs_alloc_page_array(priv->nr_pages, priv->pages, false);
-	if (ret) {
-		ret = -ENOMEM;
-		goto out;
-		}
+	if (ret)
+		return -ENOMEM;
 
 	_btrfs_encoded_read_regular_fill_pages(inode, start, disk_bytenr,
 					       disk_io_size, priv);
@@ -9185,7 +9185,7 @@ static ssize_t btrfs_encoded_read_regular(struct btrfs_encoded_read_private *pri
 
 	ret = blk_status_to_errno(READ_ONCE(priv->status));
 	if (ret)
-		goto out;
+		return ret;
 
 	unlock_extent(io_tree, start, lockend, &priv->cached_state);
 	btrfs_inode_unlock(inode, BTRFS_ILOCK_SHARED);
@@ -9204,22 +9204,15 @@ static ssize_t btrfs_encoded_read_regular(struct btrfs_encoded_read_private *pri
 				     PAGE_SIZE - page_offset);
 
 		if (copy_page_to_iter(priv->pages[i], page_offset, bytes,
-				      &priv->iter) != bytes) {
-			ret = -EFAULT;
-			goto out;
-		}
+				      &priv->iter) != bytes)
+			return -EFAULT;
+
 		i++;
 		cur += bytes;
 		page_offset = 0;
 	}
-	ret = priv->count;
-out:
-	for (i = 0; i < priv->nr_pages; i++) {
-		if (priv->pages[i])
-			__free_page(priv->pages[i]);
-	}
-	kfree(priv->pages);
-	return ret;
+
+	return priv->count;
 }
 
 ssize_t btrfs_encoded_read(struct btrfs_encoded_read_private *priv)
diff --git a/fs/btrfs/ioctl.c b/fs/btrfs/ioctl.c
index 770bd609f386..3fa661322c26 100644
--- a/fs/btrfs/ioctl.c
+++ b/fs/btrfs/ioctl.c
@@ -4509,6 +4509,34 @@ static int _btrfs_ioctl_send(struct btrfs_inode *inode, void __user *argp, bool
 	return ret;
 }
 
+static ssize_t btrfs_encoded_read_finish(struct btrfs_encoded_read_private *priv,
+					 ssize_t ret)
+{
+	size_t copy_end_kernel = offsetofend(struct btrfs_ioctl_encoded_io_args,
+					     flags);
+	unsigned long i;
+
+	if (ret >= 0) {
+		fsnotify_access(priv->file);
+		if (copy_to_user(priv->copy_out,
+				 (char *)&priv->args + copy_end_kernel,
+				 sizeof(priv->args) - copy_end_kernel))
+			ret = -EFAULT;
+	}
+
+	for (i = 0; i < priv->nr_pages; i++) {
+		if (priv->pages[i])
+			__free_page(priv->pages[i]);
+	}
+	kfree(priv->pages);
+	kfree(priv->iov);
+
+	if (ret > 0)
+		add_rchar(current, ret);
+	inc_syscr(current);
+	return ret;
+}
+
 static int btrfs_ioctl_encoded_read(struct file *file, void __user *argp,
 				    bool compat)
 {
@@ -4573,21 +4601,12 @@ static int btrfs_ioctl_encoded_read(struct file *file, void __user *argp,
 	if (ret < 0)
 		goto out;
 
+	priv.copy_out = argp + copy_end;
+
 	ret = btrfs_encoded_read(&priv);
-	if (ret >= 0) {
-		fsnotify_access(file);
-		if (copy_to_user(argp + copy_end,
-				 (char *)&priv.args + copy_end_kernel,
-				 sizeof(priv.args) - copy_end_kernel))
-			ret = -EFAULT;
-	}
 
 out:
-	kfree(priv.iov);
-	if (ret > 0)
-		add_rchar(current, ret);
-	inc_syscr(current);
-	return ret;
+	return btrfs_encoded_read_finish(&priv, ret);
 }
 
 static int btrfs_ioctl_encoded_write(struct file *file, void __user *argp, bool compat)
-- 
2.44.2


  parent reply	other threads:[~2024-08-23 16:28 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-08-23 16:27 [PATCH 0/6] btrfs: add io_uring for encoded reads Mark Harmstone
2024-08-23 16:27 ` [PATCH 1/6] btrfs: remove iocb from btrfs_encoded_read Mark Harmstone
2024-08-27  1:12   ` David Sterba
2024-08-23 16:27 ` [PATCH 2/6] btrfs: store encoded read state in struct btrfs_encoded_read_private Mark Harmstone
2024-08-26 15:22   ` David Sterba
2024-08-27  1:03   ` David Sterba
2024-09-06 15:19   ` Pavel Begunkov
2024-08-23 16:27 ` Mark Harmstone [this message]
2024-08-23 16:27 ` [PATCH 4/6] btrfs: add btrfs_prepare_encoded_read Mark Harmstone
2024-08-23 16:27 ` [PATCH 5/6] btrfs: move wait out of btrfs_encoded_read Mark Harmstone
2024-09-06 15:11   ` Pavel Begunkov
2024-08-23 16:27 ` [PATCH 6/6] btrfs: add io_uring interface for encoded reads Mark Harmstone
2024-09-06 14:41   ` Pavel Begunkov
2024-09-06 15:33   ` Pavel Begunkov

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 \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox