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 4/6] btrfs: add btrfs_prepare_encoded_read
Date: Fri, 23 Aug 2024 17:27:46 +0100	[thread overview]
Message-ID: <[email protected]> (raw)
In-Reply-To: <[email protected]>

Move the beginning of btrfs_ioctl_encoded_read, responsible for
initialization of priv and validation, into its own function.

Signed-off-by: Mark Harmstone <[email protected]>
---
 fs/btrfs/ioctl.c | 84 ++++++++++++++++++++++++++----------------------
 1 file changed, 46 insertions(+), 38 deletions(-)

diff --git a/fs/btrfs/ioctl.c b/fs/btrfs/ioctl.c
index 3fa661322c26..d2658508e055 100644
--- a/fs/btrfs/ioctl.c
+++ b/fs/btrfs/ioctl.c
@@ -4537,23 +4537,23 @@ static ssize_t btrfs_encoded_read_finish(struct btrfs_encoded_read_private *priv
 	return ret;
 }
 
-static int btrfs_ioctl_encoded_read(struct file *file, void __user *argp,
-				    bool compat)
+static ssize_t btrfs_prepare_encoded_read(struct btrfs_encoded_read_private *priv,
+					  struct file *file, bool compat,
+					  void __user *argp)
 {
 	size_t copy_end_kernel = offsetofend(struct btrfs_ioctl_encoded_io_args,
 					     flags);
 	size_t copy_end;
 	loff_t pos;
 	ssize_t ret;
-	struct btrfs_encoded_read_private priv = {
-		.pending = ATOMIC_INIT(1),
-		.file = file,
-	};
 
-	if (!capable(CAP_SYS_ADMIN)) {
-		ret = -EPERM;
-		goto out;
-	}
+	memset(priv, 0, sizeof(*priv));
+
+	atomic_set(&priv->pending, 1);
+	priv->file = file;
+
+	if (!capable(CAP_SYS_ADMIN))
+		return -EPERM;
 
 	if (compat) {
 #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
@@ -4561,47 +4561,55 @@ static int btrfs_ioctl_encoded_read(struct file *file, void __user *argp,
 
 		copy_end = offsetofend(struct btrfs_ioctl_encoded_io_args_32,
 				       flags);
-		if (copy_from_user(&args32, argp, copy_end)) {
-			ret = -EFAULT;
-			goto out;
-		}
-		priv.args.iov = compat_ptr(args32.iov);
-		priv.args.iovcnt = args32.iovcnt;
-		priv.args.offset = args32.offset;
-		priv.args.flags = args32.flags;
+		if (copy_from_user(&args32, argp, copy_end))
+			return -EFAULT;
+
+		priv->args.iov = compat_ptr(args32.iov);
+		priv->args.iovcnt = args32.iovcnt;
+		priv->args.offset = args32.offset;
+		priv->args.flags = args32.flags;
 #else
 		return -ENOTTY;
 #endif
 	} else {
 		copy_end = copy_end_kernel;
-		if (copy_from_user(&priv.args, argp, copy_end)) {
-			ret = -EFAULT;
-			goto out;
-		}
-	}
-	if (priv.args.flags != 0) {
-		ret = -EINVAL;
-		goto out;
+		if (copy_from_user(&priv->args, argp, copy_end))
+			return -EFAULT;
 	}
 
-	priv.iov = priv.iovstack;
-	ret = import_iovec(ITER_DEST, priv.args.iov, priv.args.iovcnt,
-			   ARRAY_SIZE(priv.iovstack), &priv.iov, &priv.iter);
+	if (priv->args.flags != 0)
+		return -EINVAL;
+
+	priv->iov = priv->iovstack;
+	ret = import_iovec(ITER_DEST, priv->args.iov, priv->args.iovcnt,
+			   ARRAY_SIZE(priv->iovstack), &priv->iov, &priv->iter);
 	if (ret < 0) {
-		priv.iov = NULL;
-		goto out;
+		priv->iov = NULL;
+		return ret;
 	}
 
-	if (iov_iter_count(&priv.iter) == 0) {
-		ret = 0;
-		goto out;
-	}
-	pos = priv.args.offset;
-	ret = rw_verify_area(READ, file, &pos, priv.args.len);
+	pos = priv->args.offset;
+	ret = rw_verify_area(READ, priv->file, &pos, priv->args.len);
 	if (ret < 0)
+		return ret;
+
+	priv->copy_out = argp + copy_end;
+
+	return 0;
+}
+
+static int btrfs_ioctl_encoded_read(struct file *file, void __user *argp,
+				    bool compat)
+{
+	ssize_t ret;
+	struct btrfs_encoded_read_private priv;
+
+	ret = btrfs_prepare_encoded_read(&priv, file, compat, argp);
+	if (ret)
 		goto out;
 
-	priv.copy_out = argp + copy_end;
+	if (iov_iter_count(&priv.iter) == 0)
+		goto out;
 
 	ret = btrfs_encoded_read(&priv);
 
-- 
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 ` [PATCH 3/6] btrfs: add btrfs_encoded_read_finish Mark Harmstone
2024-08-23 16:27 ` Mark Harmstone [this message]
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