diff mbox series

[for-next,v11,11/13] block: add blk_rq_map_user_io

Message ID 20220929120632.64749-12-anuj20.g@samsung.com (mailing list archive)
State New
Headers show
Series Fixed-buffer for uring-cmd/passthru | expand

Commit Message

Anuj Gupta Sept. 29, 2022, 12:06 p.m. UTC
Create a helper blk_rq_map_user_io for mapping of vectored as well as
non-vectored requests. This will help in saving dupilcation of code at few
places in scsi and nvme.

Signed-off-by: Anuj Gupta <anuj20.g@samsung.com>
Suggested-by: Christoph Hellwig <hch@lst.de>
---
 block/blk-map.c        | 36 ++++++++++++++++++++++++++++++++++++
 include/linux/blk-mq.h |  2 ++
 2 files changed, 38 insertions(+)

Comments

Christoph Hellwig Sept. 29, 2022, 1:28 p.m. UTC | #1
On Thu, Sep 29, 2022 at 05:36:30PM +0530, Anuj Gupta wrote:
> Create a helper blk_rq_map_user_io for mapping of vectored as well as
> non-vectored requests. This will help in saving dupilcation of code at few
> places in scsi and nvme.
> 
> Signed-off-by: Anuj Gupta <anuj20.g@samsung.com>
> Suggested-by: Christoph Hellwig <hch@lst.de>

This looks good:

Reviewed-by: Christoph Hellwig <hch@lst.de>

but it should be early on in this series.  Certainly before the fixebufs
addition, and probably also before the nvme refactoring.
diff mbox series

Patch

diff --git a/block/blk-map.c b/block/blk-map.c
index ffab5d2d8d6d..74ee496c2d3a 100644
--- a/block/blk-map.c
+++ b/block/blk-map.c
@@ -689,6 +689,42 @@  int blk_rq_map_user(struct request_queue *q, struct request *rq,
 }
 EXPORT_SYMBOL(blk_rq_map_user);
 
+int blk_rq_map_user_io(struct request *req, struct rq_map_data *map_data,
+		void __user *ubuf, unsigned long buf_len, gfp_t gfp_mask,
+		bool vec, int iov_count, bool check_iter_count, int rw)
+{
+	int ret;
+
+	if (vec) {
+		struct iovec fast_iov[UIO_FASTIOV];
+		struct iovec *iov = fast_iov;
+		struct iov_iter iter;
+
+		ret = import_iovec(rw, ubuf, iov_count ? iov_count : buf_len,
+				UIO_FASTIOV, &iov, &iter);
+		if (ret < 0)
+			return ret;
+
+		if (iov_count) {
+			/* SG_IO howto says that the shorter of the two wins */
+			iov_iter_truncate(&iter, buf_len);
+			if (check_iter_count && !iov_iter_count(&iter)) {
+				kfree(iov);
+				return -EINVAL;
+			}
+		}
+
+		ret = blk_rq_map_user_iov(req->q, req, map_data, &iter,
+				gfp_mask);
+		kfree(iov);
+	} else if (buf_len) {
+		ret = blk_rq_map_user(req->q, req, map_data, ubuf, buf_len,
+				gfp_mask);
+	}
+	return ret;
+}
+EXPORT_SYMBOL(blk_rq_map_user_io);
+
 /**
  * blk_rq_unmap_user - unmap a request with user data
  * @bio:	       start of bio list
diff --git a/include/linux/blk-mq.h b/include/linux/blk-mq.h
index 50811d0fb143..ba18e9bdb799 100644
--- a/include/linux/blk-mq.h
+++ b/include/linux/blk-mq.h
@@ -985,6 +985,8 @@  struct rq_map_data {
 
 int blk_rq_map_user(struct request_queue *, struct request *,
 		struct rq_map_data *, void __user *, unsigned long, gfp_t);
+int blk_rq_map_user_io(struct request *, struct rq_map_data *,
+		void __user *, unsigned long, gfp_t, bool, int, bool, int);
 int blk_rq_map_user_iov(struct request_queue *, struct request *,
 		struct rq_map_data *, const struct iov_iter *, gfp_t);
 int blk_rq_unmap_user(struct bio *);