From 0080aae5ecdc86e79e2492eed8c946afc588b4b4 Mon Sep 17 00:00:00 2001
From: Rajan Shanmugavelu <rajan.shanmugavelu@oracle.com>
Date: Thu, 8 Dec 2022 15:49:41 -0800
Subject: [PATCH] oracleasm: asm_bio_map_user_iov and asm_bio_unmap update for
 5.15+ kernel

Signed-off-by: Rajan Shanmugavelu <rajan.shanmugavelu@oracle.com>
---
 drivers/block/oracleasm/driver.c | 109 +++++++++++++++++++++++++++++++++++----
 1 file changed, 98 insertions(+), 11 deletions(-)

diff --git a/drivers/block/oracleasm/driver.c b/drivers/block/oracleasm/driver.c
index 1efee330..e75a8e7 100644
--- a/drivers/block/oracleasm/driver.c
+++ b/drivers/block/oracleasm/driver.c
@@ -145,22 +145,10 @@ static inline unsigned int asm_block_size(struct block_device *bdev)
 
 void asm_bio_unmap(struct bio *bio)
 {
-	struct bio_vec *bvec;
-#if LINUX_VERSION_CODE < KERNEL_VERSION(5, 1, 0)
-	int iter_all;
-#else
-	struct bvec_iter_all iter_all;
-#endif
-
-	bio_for_each_segment_all(bvec, bio, iter_all) {
-		if (bio_data_dir(bio) == READ)
-			set_page_dirty_lock(bvec->bv_page);
-
-		put_page(bvec->bv_page);
-	}
-
+	bio_release_pages(bio, bio_data_dir(bio) == READ);
 	bio_put(bio);
 	bio_put(bio);
+	trace_bio(bio, "released");
 }
 
 /*
@@ -1133,6 +1125,101 @@ static void asm_end_bio_io(struct bio *bio)
 	}
 }  /* asm_end_bio_io() */
 
+/*
+ *      asm_bio_map_user_iov - map user iovec into bio
+ *      @q:             the struct request_queue for the bio
+ *      @iter:          iovec iterator
+ *      @gfp_mask:      memory allocation flags
+ *
+ *      Map the user space address into a bio suitable for io to a block
+ *      device. Returns an error pointer in case of error.
+ */
+
+
+static struct bio *asm_bio_map_user_iov(struct request_queue *q,
+                struct iov_iter *iter, gfp_t gfp_mask)
+{
+        struct bio *bio;
+        int ret;
+        int j;
+
+        if (!iov_iter_count(iter))
+                return ERR_PTR(-EINVAL);
+
+        bio = bio_kmalloc(gfp_mask, iov_iter_npages(iter, BIO_MAX_VECS));
+        if (!bio)
+                return ERR_PTR(-ENOMEM);
+
+        while (iov_iter_count(iter)) {
+                struct page **pages;
+                ssize_t bytes;
+                size_t offs, added = 0;
+                int npages;
+
+                bytes = iov_iter_get_pages_alloc(iter, &pages, LONG_MAX, &offs);
+                if (unlikely(bytes <= 0)) {
+                        ret = bytes ? bytes : -EFAULT;
+                        goto out_unmap;
+                }
+
+                npages = DIV_ROUND_UP(offs + bytes, PAGE_SIZE);
+
+                if (unlikely(offs & queue_dma_alignment(q))) {
+                        ret = -EINVAL;
+                        j = 0;
+                } else {
+                        for (j = 0; j < npages; j++) {
+                                struct page *page = pages[j];
+                                unsigned int n = PAGE_SIZE - offs;
+                                /* unsigned short prev_bi_vcnt = bio->bi_vcnt;*/
+                                unsigned short prev_bi_vcnt = bio->bi_vcnt;
+
+                                if (n > bytes)
+                                        n = bytes;
+
+                                if (!bio_add_pc_page(q, bio, page, n, offs)) {
+					/*
+					 * check if vector was merged with previous
+					 * drop page reference if needed
+					 */
+					if (bio->bi_vcnt == prev_bi_vcnt)
+						put_page(page);
+                                        break;
+				}
+
+                                added += n;
+                                bytes -= n;
+                                offs = 0;
+                        }
+                        iov_iter_advance(iter, added);
+                }
+
+                /*
+                 * release the pages we didn't map into the bio, if any
+                 */
+                while (j < npages)
+                        put_page(pages[j++]);
+                kvfree(pages);
+                /* couldn't stuff something into bio? */
+                if (bytes)
+                        break;
+        }
+
+        /*
+         * subtle -- if asm_bio_map_user_iov() ended up bouncing a bio,
+         * it would normally disappear when its bi_end_io is run.
+         * however, we need it for the unmap, so grab an extra
+         * reference to it
+         */
+        bio_get(bio);
+        return bio;
+
+out_unmap:
+        bio_release_pages(bio, false);
+        bio_put(bio);
+        return ERR_PTR(ret);
+}
+
 static int asm_submit_io(struct file *file,
 			 asm_ioc __user *user_iocp,
 			 asm_ioc *ioc)
@@ -1256,7 +1343,7 @@ static int asm_submit_io(struct file *file,
 	iov.iov_base = (void __user *)ioc->buffer_asm_ioc;
 	iov.iov_len = r->r_count;
 	iov_iter_init(&iter, rw, &iov, 1, r->r_count);
-	r->r_bio = bio_map_user_iov(bdev_get_queue(bdev), &iter, GFP_KERNEL);
+	r->r_bio = asm_bio_map_user_iov(bdev_get_queue(bdev), &iter, GFP_KERNEL);
 
 	if (IS_ERR(r->r_bio)) {
 		ret = PTR_ERR(r->r_bio);
-- 
1.8.3.1

