[Devel] [PATCH rh7 3/5] ploop: Extrack extent-related logic from dio_sync_io()

Kirill Tkhai ktkhai at virtuozzo.com
Wed Jul 3 19:52:58 MSK 2019


This patch moves the code related to extents to separate
function.

Since we don't know the number of bios, which will be
submitted, we set counter into INT_MAX, and then decrement
it in required number.

Signed-off-by: Kirill Tkhai <ktkhai at virtuozzo.com>
---
 drivers/block/ploop/io_direct.c |   58 ++++++++++++++++++++++++++-------------
 1 file changed, 38 insertions(+), 20 deletions(-)

diff --git a/drivers/block/ploop/io_direct.c b/drivers/block/ploop/io_direct.c
index 754f35923950..8ade457bc01a 100644
--- a/drivers/block/ploop/io_direct.c
+++ b/drivers/block/ploop/io_direct.c
@@ -1064,26 +1064,19 @@ DEFINE_BIO_CB(dio_endio_sync)
 END_BIO_CB(dio_endio_sync)
 
 static int
-dio_sync_io(struct ploop_io * io, int rw, struct page * page,
-	    unsigned int len, unsigned int off, sector_t sec)
+dio_make_io(struct ploop_io *io, int rw, struct page *page,
+	    unsigned int len, unsigned int off, sector_t sec,
+	    bio_end_io_t *bi_end_io, void *bi_private)
 {
 	struct bio_list bl = BIO_EMPTY_LIST;
 	struct bio * bio;
-	struct dio_comp comp;
 	struct extent_map * em;
+	int nr_bios = 0, err;
 	sector_t nsec;
-	int err;
-
-	BUG_ON(len & 511);
-	BUG_ON(off & 511);
 
 	bio = NULL;
 	em = NULL;
 
-	init_completion(&comp.comp);
-	atomic_set(&comp.count, 1);
-	comp.error = 0;
-
 	while (len > 0) {
 		int copy;
 
@@ -1129,18 +1122,13 @@ dio_sync_io(struct ploop_io * io, int rw, struct page * page,
 		bl.head = b->bi_next;
 
 		b->bi_next = NULL;
-		b->bi_end_io = dio_endio_sync;
-		b->bi_private = &comp;
-		atomic_inc(&comp.count);
+		b->bi_end_io = bi_end_io;
+		b->bi_private = bi_private;
+		nr_bios++;
 		submit_bio(rw, b);
 	}
 
-	if (atomic_dec_and_test(&comp.count))
-		complete(&comp.comp);
-
-	wait_for_completion(&comp.comp);
-
-	return comp.error;
+	return nr_bios;
 
 
 enomem:
@@ -1159,6 +1147,36 @@ dio_sync_io(struct ploop_io * io, int rw, struct page * page,
 	return err;
 }
 
+static int
+dio_sync_io(struct ploop_io *io, int rw, struct page *page,
+	    unsigned int len, unsigned int off, sector_t sec)
+{
+	struct dio_comp comp;
+	int nr_bios;
+
+	BUG_ON((len & 511) || (off & 511));
+
+	/*
+	 * We set initial count to INT_MAX, so dio_endio_sync()
+	 * never makes it 0 before we've done atomic_sub_and_test().
+	 */
+	atomic_set(&comp.count, INT_MAX);
+	init_completion(&comp.comp);
+	comp.error = 0;
+
+	nr_bios = dio_make_io(io, rw, page, len, off, sec,
+			      dio_endio_sync, &comp);
+	if (nr_bios < 0)
+		return nr_bios;
+
+	if (atomic_sub_and_test(INT_MAX - nr_bios, &comp.count))
+		complete(&comp.comp);
+
+	wait_for_completion(&comp.comp);
+
+	return comp.error;
+}
+
 static int
 dio_sync_read(struct ploop_io * io, struct page * page, unsigned int len,
 	      unsigned int off, sector_t pos)



More information about the Devel mailing list