[PATCH 09/20] ublk: split out ublk_user_copy() helper

Caleb Sander Mateos
2025-12-17T05:35:13Z

ublk_ch_read_iter() and ublk_ch_write_iter() are nearly identical except
for the iter direction. Split out a helper function ublk_user_copy() to
reduce the code duplication as these functions are about to get larger.

Signed-off-by: Caleb Sander Mateos <csander@purestorage.com>
---
drivers/block/ublk_drv.c | 26 ++++++++++----------------
1 file changed, 10 insertions(+), 16 deletions(-)

diff --git a/drivers/block/ublk_drv.c b/drivers/block/ublk_drv.c
index 0499add560b5..e39ed8a0f0ae 100644
--- a/drivers/block/ublk_drv.c
+++ b/drivers/block/ublk_drv.c
@@ -2657,42 +2657,36 @@ static struct request *ublk_check_and_get_req(struct kiocb *iocb,
fail:
ublk_put_req_ref(*io, req);
return ERR_PTR(-EACCES);
}

-static ssize_t ublk_ch_read_iter(struct kiocb *iocb, struct iov_iter *to)
+static ssize_t
+ublk_user_copy(struct kiocb *iocb, struct iov_iter *iter, int dir)
{
struct request *req;
struct ublk_io *io;
size_t buf_off;
size_t ret;

- req = ublk_check_and_get_req(iocb, to, &buf_off, ITER_DEST, &io);
+ req = ublk_check_and_get_req(iocb, iter, &buf_off, dir, &io);
if (IS_ERR(req))
return PTR_ERR(req);

- ret = ublk_copy_user_pages(req, buf_off, to, ITER_DEST);
+ ret = ublk_copy_user_pages(req, buf_off, iter, dir);
ublk_put_req_ref(io, req);

return ret;
}

-static ssize_t ublk_ch_write_iter(struct kiocb *iocb, struct iov_iter *from)
+static ssize_t ublk_ch_read_iter(struct kiocb *iocb, struct iov_iter *to)
{
- struct request *req;
- struct ublk_io *io;
- size_t buf_off;
- size_t ret;
-
- req = ublk_check_and_get_req(iocb, from, &buf_off, ITER_SOURCE, &io);
- if (IS_ERR(req))
- return PTR_ERR(req);
-
- ret = ublk_copy_user_pages(req, buf_off, from, ITER_SOURCE);
- ublk_put_req_ref(io, req);
+ return ublk_user_copy(iocb, to, ITER_DEST);
+}

- return ret;
+static ssize_t ublk_ch_write_iter(struct kiocb *iocb, struct iov_iter *from)
+{
+ return ublk_user_copy(iocb, from, ITER_SOURCE);
}

static const struct file_operations ublk_ch_fops = {
.owner = THIS_MODULE,
.open = ublk_ch_open,
--
2.45.2