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

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