Blame SOURCES/0003-oracleasm-copy-rhel8-s-bio_map_user_iov.patch

fe774a
From 29ee0198fc5acc9647f8d9a97f0e07bb8a278aa7 Mon Sep 17 00:00:00 2001
fe774a
From: Ming Lei <ming.lei@redhat.com>
fe774a
Date: Wed, 16 Sep 2020 11:08:42 +0800
fe774a
Subject: [PATCH 3/3] oracleasm: copy rhel8's bio_map_user_iov
fe774a
fe774a
Signed-off-by: Ming Lei <ming.lei@redhat.com>
fe774a
---
fe774a
 drivers/block/oracleasm/driver.c | 99 +++++++++++++++++++++++++++++++++++++++-
fe774a
 1 file changed, 98 insertions(+), 1 deletion(-)
fe774a
fe774a
diff --git a/drivers/block/oracleasm/driver.c b/drivers/block/oracleasm/driver.c
fe774a
index 756d3f9..c726726 100644
fe774a
--- a/drivers/block/oracleasm/driver.c
fe774a
+++ b/drivers/block/oracleasm/driver.c
fe774a
@@ -1124,6 +1124,103 @@ static void asm_end_bio_io(struct bio *bio)
fe774a
 	}
fe774a
 }  /* asm_end_bio_io() */
fe774a
 
fe774a
+/**
fe774a
+ *	asm_bio_map_user_iov - map user iovec into bio
fe774a
+ *	@q:		the struct request_queue for the bio
fe774a
+ *	@iter:		iovec iterator
fe774a
+ *	@gfp_mask:	memory allocation flags
fe774a
+ *
fe774a
+ *	Map the user space address into a bio suitable for io to a block
fe774a
+ *	device. Returns an error pointer in case of error.
fe774a
+ */
fe774a
+static struct bio *asm_bio_map_user_iov(struct request_queue *q,
fe774a
+			     struct iov_iter *iter,
fe774a
+			     gfp_t gfp_mask)
fe774a
+{
fe774a
+	int j;
fe774a
+	struct bio *bio;
fe774a
+	int ret;
fe774a
+	struct bio_vec *bvec;
fe774a
+
fe774a
+	if (!iov_iter_count(iter))
fe774a
+		return ERR_PTR(-EINVAL);
fe774a
+
fe774a
+	bio = bio_kmalloc(gfp_mask, iov_iter_npages(iter, BIO_MAX_PAGES));
fe774a
+	if (!bio)
fe774a
+		return ERR_PTR(-ENOMEM);
fe774a
+
fe774a
+	while (iov_iter_count(iter)) {
fe774a
+		struct page **pages;
fe774a
+		ssize_t bytes;
fe774a
+		size_t offs, added = 0;
fe774a
+		int npages;
fe774a
+
fe774a
+		bytes = iov_iter_get_pages_alloc(iter, &pages, LONG_MAX, &offs);
fe774a
+		if (unlikely(bytes <= 0)) {
fe774a
+			ret = bytes ? bytes : -EFAULT;
fe774a
+			goto out_unmap;
fe774a
+		}
fe774a
+
fe774a
+		npages = DIV_ROUND_UP(offs + bytes, PAGE_SIZE);
fe774a
+
fe774a
+		if (unlikely(offs & queue_dma_alignment(q))) {
fe774a
+			ret = -EINVAL;
fe774a
+			j = 0;
fe774a
+		} else {
fe774a
+			for (j = 0; j < npages; j++) {
fe774a
+				struct page *page = pages[j];
fe774a
+				unsigned int n = PAGE_SIZE - offs;
fe774a
+				unsigned short prev_bi_vcnt = bio->bi_vcnt;
fe774a
+
fe774a
+				if (n > bytes)
fe774a
+					n = bytes;
fe774a
+
fe774a
+				if (!bio_add_pc_page(q, bio, page, n, offs))
fe774a
+					break;
fe774a
+
fe774a
+				/*
fe774a
+				 * check if vector was merged with previous
fe774a
+				 * drop page reference if needed
fe774a
+				 */
fe774a
+				if (bio->bi_vcnt == prev_bi_vcnt)
fe774a
+					put_page(page);
fe774a
+
fe774a
+				added += n;
fe774a
+				bytes -= n;
fe774a
+				offs = 0;
fe774a
+			}
fe774a
+			iov_iter_advance(iter, added);
fe774a
+		}
fe774a
+		/*
fe774a
+		 * release the pages we didn't map into the bio, if any
fe774a
+		 */
fe774a
+		while (j < npages)
fe774a
+			put_page(pages[j++]);
fe774a
+		kvfree(pages);
fe774a
+		/* couldn't stuff something into bio? */
fe774a
+		if (bytes)
fe774a
+			break;
fe774a
+	}
fe774a
+
fe774a
+	bio_set_flag(bio, BIO_USER_MAPPED);
fe774a
+
fe774a
+	/*
fe774a
+	 * subtle -- if asm_bio_map_user_iov() ended up bouncing a bio,
fe774a
+	 * it would normally disappear when its bi_end_io is run.
fe774a
+	 * however, we need it for the unmap, so grab an extra
fe774a
+	 * reference to it
fe774a
+	 */
fe774a
+	bio_get(bio);
fe774a
+	return bio;
fe774a
+
fe774a
+ out_unmap:
fe774a
+	bio_for_each_segment_all(bvec, bio, j) {
fe774a
+		put_page(bvec->bv_page);
fe774a
+	}
fe774a
+	bio_put(bio);
fe774a
+	return ERR_PTR(ret);
fe774a
+}
fe774a
+
fe774a
 static int asm_submit_io(struct file *file,
fe774a
 			 asm_ioc __user *user_iocp,
fe774a
 			 asm_ioc *ioc)
fe774a
@@ -1247,7 +1344,7 @@ static int asm_submit_io(struct file *file,
fe774a
 	iov.iov_base = (void __user *)ioc->buffer_asm_ioc;
fe774a
 	iov.iov_len = r->r_count;
fe774a
 	iov_iter_init(&iter, rw, &iov, 1, r->r_count);
fe774a
-	r->r_bio = bio_map_user_iov(bdev_get_queue(bdev), &iter, GFP_KERNEL);
fe774a
+	r->r_bio = asm_bio_map_user_iov(bdev_get_queue(bdev), &iter, GFP_KERNEL);
fe774a
 
fe774a
 	if (IS_ERR(r->r_bio)) {
fe774a
 		ret = PTR_ERR(r->r_bio);
fe774a
-- 
fe774a
2.13.6
fe774a