[03/10] ploop: Introduce ploop_submit_alloc() helper

Submitted by Kirill Tkhai on March 26, 2019, 3:18 p.m.

Details

Message ID 155361353234.12708.3365557048103907312.stgit@localhost.localdomain
State New
Series "ploop: Discard with zeroing of ploop1 indexes support"
Headers show

Commit Message

Kirill Tkhai March 26, 2019, 3:18 p.m.
Compress repeating code in single function.
Add (currently unused) case, when desired
block is passed directly.

Signed-off-by: Kirill Tkhai <ktkhai@virtuozzo.com>
---
 drivers/block/ploop/fmt_ploop1.c |    2 +-
 drivers/block/ploop/fmt_raw.c    |    2 +-
 drivers/block/ploop/io.c         |   39 ++++++++++++++++++++++++++++++++++++++
 drivers/block/ploop/io_direct.c  |   14 ++------------
 drivers/block/ploop/io_kaio.c    |   13 +------------
 include/linux/ploop/ploop.h      |    5 +++--
 6 files changed, 47 insertions(+), 28 deletions(-)

Patch hide | download patch | download mbox

diff --git a/drivers/block/ploop/fmt_ploop1.c b/drivers/block/ploop/fmt_ploop1.c
index 382737b4cb6c..eea15aac8790 100644
--- a/drivers/block/ploop/fmt_ploop1.c
+++ b/drivers/block/ploop/fmt_ploop1.c
@@ -235,7 +235,7 @@  ploop1_allocate(struct ploop_delta * delta, struct ploop_request * preq,
 		PLOOP_FAIL_REQUEST(preq, -E2BIG);
 		return;
 	}
-	delta->io.ops->submit_alloc(&delta->io, preq, sbl, size);
+	ploop_submit_alloc(delta, preq, sbl, size, 0);
 }
 
 /* Call this when data write is complete */
diff --git a/drivers/block/ploop/fmt_raw.c b/drivers/block/ploop/fmt_raw.c
index 529ea625e917..93164e282955 100644
--- a/drivers/block/ploop/fmt_raw.c
+++ b/drivers/block/ploop/fmt_raw.c
@@ -98,7 +98,7 @@  static void
 raw_allocate(struct ploop_delta * delta, struct ploop_request * preq,
 		struct bio_list * sbl, unsigned int size)
 {
-	delta->io.ops->submit_alloc(&delta->io, preq, sbl, size);
+	ploop_submit_alloc(delta, preq, sbl, size, 0);
 }
 
 int raw_map_index(struct ploop_delta * delta, unsigned long index, sector_t *sec)
diff --git a/drivers/block/ploop/io.c b/drivers/block/ploop/io.c
index eadcec4e1fcb..a6460c870bf8 100644
--- a/drivers/block/ploop/io.c
+++ b/drivers/block/ploop/io.c
@@ -11,6 +11,7 @@ 
 
 #include <linux/ploop/ploop.h>
 #include <linux/ploop/ploop_if.h>
+#include "ploop_events.h"
 
 /* Generic IO routines. */
 
@@ -148,3 +149,41 @@  void ploop_io_report_fn(struct file * file, char * msg)
 		free_page((unsigned long)path);
 }
 EXPORT_SYMBOL(ploop_io_report_fn);
+
+int ploop_submit_alloc(struct ploop_delta *delta, struct ploop_request *preq,
+		       struct bio_list *sbl, unsigned int size, iblock_t iblk)
+{
+	struct ploop_io *io = &delta->io;
+	bool advanced = false;
+	int ret;
+
+	trace_submit_alloc(preq);
+
+	if (!(io->files.file->f_mode & FMODE_WRITE) ||
+	    (delta->flags & PLOOP_FMT_RDONLY)) {
+		PLOOP_FAIL_REQUEST(preq, -EBADF);
+		return -1;
+	}
+
+	if (!iblk)
+		iblk = io->alloc_head;
+	if (iblk == io->alloc_head) {
+		/*
+		 * Note, even if not zero iblk was passed as parameter,
+		 * alloc_head also must be incremented.
+		 */
+		io->alloc_head++;
+		advanced = true;
+	}
+
+	ret = io->ops->submit_alloc(io, preq, sbl, size, iblk);
+
+	/* FIXME: why original code handles only ENOSPC? */
+	if ((ret == 0 || ret == -ENOSPC) && advanced)
+		io->alloc_head--;
+
+	WARN_ON(iblk > io->alloc_head);
+
+	return ret;
+}
+EXPORT_SYMBOL(ploop_submit_alloc);
diff --git a/drivers/block/ploop/io_direct.c b/drivers/block/ploop/io_direct.c
index b1955d339de5..ad95cce91f6e 100644
--- a/drivers/block/ploop/io_direct.c
+++ b/drivers/block/ploop/io_direct.c
@@ -732,24 +732,14 @@  dio_submit_pad(struct ploop_io *io, struct ploop_request * preq,
 
 static int
 dio_submit_alloc(struct ploop_io *io, struct ploop_request * preq,
-		 struct bio_list * sbl, unsigned int size)
+		 struct bio_list * sbl, unsigned int size, iblock_t iblk)
 {
 	int err;
-	iblock_t iblk = io->alloc_head++;
-
-	trace_submit_alloc(preq);
-
-	if (!(io->files.file->f_mode & FMODE_WRITE)) {
-		PLOOP_FAIL_REQUEST(preq, -EBADF);
-		return -1;
-	}
 
 	err = cached_submit(io, iblk, preq, sbl, size, true);
 	if (err) {
-		if (err == -ENOSPC)
-			io->alloc_head--;
 		PLOOP_FAIL_REQUEST(preq, err);
-		return -1;
+		return err;
 	}
 	preq->eng_state = PLOOP_E_DATA_WBI;
 	return 1;
diff --git a/drivers/block/ploop/io_kaio.c b/drivers/block/ploop/io_kaio.c
index 1e4ae6efbe70..b5eb92d9523a 100644
--- a/drivers/block/ploop/io_kaio.c
+++ b/drivers/block/ploop/io_kaio.c
@@ -548,21 +548,12 @@  static int kaio_fsync_thread(void * data)
 
 static int
 kaio_submit_alloc(struct ploop_io *io, struct ploop_request * preq,
-		 struct bio_list * sbl, unsigned int size)
+		 struct bio_list * sbl, unsigned int size, iblock_t iblk)
 {
-	struct ploop_delta *delta = container_of(io, struct ploop_delta, io);
-	iblock_t iblk;
 	int log = preq->plo->cluster_log + 9;
 	loff_t clu_siz = 1 << log;
 	loff_t end_pos = (loff_t)io->alloc_head << log;
 
-	if (delta->flags & PLOOP_FMT_RDONLY) {
-		PLOOP_FAIL_REQUEST(preq, -EBADF);
-		return -1;
-	}
-
-	iblk = io->alloc_head;
-
 	if (unlikely(preq->req_rw & REQ_FLUSH)) {
 		spin_lock_irq(&io->plo->lock);
 		kaio_queue_fsync_req(preq);
@@ -594,8 +585,6 @@  kaio_submit_alloc(struct ploop_io *io, struct ploop_request * preq,
 		}
 	}
 
-	io->alloc_head++;
-
 	preq->iblock = iblk;
 	preq->eng_state = PLOOP_E_DATA_WBI;
 
diff --git a/include/linux/ploop/ploop.h b/include/linux/ploop/ploop.h
index 599f7dbff7f2..73141c536f65 100644
--- a/include/linux/ploop/ploop.h
+++ b/include/linux/ploop/ploop.h
@@ -160,7 +160,7 @@  struct ploop_io_ops
 			  unsigned long rw,
 			  struct bio_list *sbl, iblock_t iblk, unsigned int size);
 	int	(*submit_alloc)(struct ploop_io *, struct ploop_request *,
-				struct bio_list *sbl, unsigned int size);
+				struct bio_list *, unsigned int, iblock_t);
 	void	(*post_submit)(struct ploop_io *, struct ploop_request *);
 
 	int	(*disable_merge)(struct ploop_io * io, sector_t isector, unsigned int len);
@@ -890,7 +890,8 @@  void ploop_index_wb_complete(struct ploop_request * preq);
 int __init ploop_map_init(void);
 void ploop_map_exit(void);
 void ploop_add_req_to_fsync_queue(struct ploop_request * preq);
-
+int ploop_submit_alloc(struct ploop_delta *delta, struct ploop_request *preq,
+		       struct bio_list *sbl, unsigned int size, iblock_t iblk);
 
 void ploop_quiesce(struct ploop_device * plo);
 void ploop_relax(struct ploop_device * plo);