[Devel] [PATCH 1/2] ploop: Merge repeating prepare_merge patterns in single function

Kirill Tkhai ktkhai at virtuozzo.com
Fri Mar 29 11:47:08 MSK 2019


Less code ==> better readability.
Also rename prepare_merge methods for better grepping.

Signed-off-by: Kirill Tkhai <ktkhai at virtuozzo.com>
---
 drivers/block/ploop/dev.c        |   23 ++++++++++++++++++++++-
 drivers/block/ploop/fmt_ploop1.c |    7 +------
 drivers/block/ploop/fmt_raw.c    |   14 --------------
 drivers/block/ploop/io_direct.c  |    2 +-
 drivers/block/ploop/io_kaio.c    |    2 +-
 include/linux/ploop/ploop.h      |    4 ++--
 6 files changed, 27 insertions(+), 25 deletions(-)

diff --git a/drivers/block/ploop/dev.c b/drivers/block/ploop/dev.c
index 3ada41f91867..8b74fe03f17f 100644
--- a/drivers/block/ploop/dev.c
+++ b/drivers/block/ploop/dev.c
@@ -3792,6 +3792,27 @@ static void ploop_merge_complete(struct ploop_device * plo,
 	ploop_relax(plo);
 }
 
+static int ploop_prepare_merge(struct ploop_delta *next,
+			       struct ploop_snapdata *sd)
+{
+	int ret;
+
+	if (next->ops->fmt_prepare_merge) {
+		/* Format callback */
+		ret = next->ops->fmt_prepare_merge(next, sd);
+		if (ret)
+			return ret;
+	}
+
+	/* io callback */
+	ret = next->io.ops->io_prepare_merge(&next->io, sd);
+	if (ret)
+		return ret;
+
+	next->flags &= ~PLOOP_FMT_RDONLY;
+	return 0;
+}
+
 static int ploop_merge(struct ploop_device * plo)
 {
 	int err;
@@ -3823,7 +3844,7 @@ static int ploop_merge(struct ploop_device * plo)
 
 	next = list_entry(delta->list.next, struct ploop_delta, list);
 
-	err = next->ops->prepare_merge(next, &sd);
+	err = ploop_prepare_merge(next, &sd);
 	if (err) {
 		printk(KERN_WARNING "prepare_merge for ploop%d failed (%d)\n",
 		       plo->index, err);
diff --git a/drivers/block/ploop/fmt_ploop1.c b/drivers/block/ploop/fmt_ploop1.c
index 6212634a1c0e..4846d96232b8 100644
--- a/drivers/block/ploop/fmt_ploop1.c
+++ b/drivers/block/ploop/fmt_ploop1.c
@@ -516,11 +516,6 @@ ploop1_prepare_merge(struct ploop_delta * delta, struct ploop_snapdata * sd)
 			 (delta->io.plo->cluster_log + 9);
 	delta->io.alloc_head = ph->alloc_head;
 
-	err = delta->io.ops->prepare_merge(&delta->io, sd);
-	if (err)
-		return err;
-
-	delta->flags &= ~PLOOP_FMT_RDONLY;
 	return 0;
 }
 
@@ -783,7 +778,7 @@ static struct ploop_delta_ops ploop1_delta_ops =
 	.sync		=	ploop1_sync,
 	.prepare_snapshot =	ploop1_prepare_snapshot,
 	.complete_snapshot =	ploop1_complete_snapshot,
-	.prepare_merge	=	ploop1_prepare_merge,
+	.fmt_prepare_merge =	ploop1_prepare_merge,
 	.start_merge	=	ploop1_start_merge,
 	.complete_merge =	ploop1_complete_merge,
 	.truncate	=	ploop1_truncate,
diff --git a/drivers/block/ploop/fmt_raw.c b/drivers/block/ploop/fmt_raw.c
index 93164e282955..d88380b27c1d 100644
--- a/drivers/block/ploop/fmt_raw.c
+++ b/drivers/block/ploop/fmt_raw.c
@@ -182,19 +182,6 @@ raw_complete_snapshot(struct ploop_delta * delta, struct ploop_snapdata * sd)
 	return err;
 }
 
-static int
-raw_prepare_merge(struct ploop_delta * delta, struct ploop_snapdata * sd)
-{
-	int err;
-
-	err = delta->io.ops->prepare_merge(&delta->io, sd);
-	if (err)
-		return err;
-
-	delta->flags &= ~PLOOP_FMT_RDONLY;
-	return 0;
-}
-
 static int
 raw_start_merge(struct ploop_delta * delta, struct ploop_snapdata * sd)
 {
@@ -248,7 +235,6 @@ static struct ploop_delta_ops raw_delta_ops =
 	.refresh	=	raw_refresh,
 	.prepare_snapshot =	raw_prepare_snapshot,
 	.complete_snapshot =	raw_complete_snapshot,
-	.prepare_merge	=	raw_prepare_merge,
 	.start_merge	=	raw_start_merge,
 	.prepare_grow	=	raw_prepare_grow,
 };
diff --git a/drivers/block/ploop/io_direct.c b/drivers/block/ploop/io_direct.c
index 1667989c9c3b..9fd2075661ad 100644
--- a/drivers/block/ploop/io_direct.c
+++ b/drivers/block/ploop/io_direct.c
@@ -1932,7 +1932,7 @@ static struct ploop_io_ops ploop_io_ops_direct =
 	.stop		=	dio_stop,
 	.prepare_snapshot =	dio_prepare_snapshot,
 	.complete_snapshot =	dio_complete_snapshot,
-	.prepare_merge  =	dio_prepare_merge,
+	.io_prepare_merge =	dio_prepare_merge,
 	.start_merge	=	dio_start_merge,
 	.truncate	=	dio_truncate,
 
diff --git a/drivers/block/ploop/io_kaio.c b/drivers/block/ploop/io_kaio.c
index b5eb92d9523a..6882f921d069 100644
--- a/drivers/block/ploop/io_kaio.c
+++ b/drivers/block/ploop/io_kaio.c
@@ -1134,7 +1134,7 @@ static struct ploop_io_ops ploop_io_ops_kaio =
 	.stop		=	kaio_stop,
 	.prepare_snapshot =	kaio_prepare_snapshot,
 	.complete_snapshot =	kaio_complete_snapshot,
-	.prepare_merge	=	kaio_prepare_merge,
+	.io_prepare_merge =	kaio_prepare_merge,
 	.start_merge	=	kaio_start_merge,
 	.truncate	=	kaio_truncate,
 
diff --git a/include/linux/ploop/ploop.h b/include/linux/ploop/ploop.h
index c3ced34e8909..7a77e674fe74 100644
--- a/include/linux/ploop/ploop.h
+++ b/include/linux/ploop/ploop.h
@@ -191,7 +191,7 @@ struct ploop_io_ops
 	int	(*stop)(struct ploop_io * io);
 	int	(*prepare_snapshot)(struct ploop_io *, struct ploop_snapdata *);
 	int	(*complete_snapshot)(struct ploop_io *, struct ploop_snapdata *);
-	int	(*prepare_merge)(struct ploop_io *, struct ploop_snapdata *);
+	int	(*io_prepare_merge)(struct ploop_io *, struct ploop_snapdata *);
 	int	(*start_merge)(struct ploop_io *, struct ploop_snapdata *);
 	int	(*truncate)(struct ploop_io *, struct file *, __u32 alloc_head);
 	void	(*queue_settings)(struct ploop_io *, struct request_queue *q);
@@ -281,7 +281,7 @@ struct ploop_delta_ops
 	int		(*sync)(struct ploop_delta *);
 	int		(*prepare_snapshot)(struct ploop_delta *, struct ploop_snapdata *);
 	int		(*complete_snapshot)(struct ploop_delta *, struct ploop_snapdata *);
-	int		(*prepare_merge)(struct ploop_delta *, struct ploop_snapdata *);
+	int		(*fmt_prepare_merge)(struct ploop_delta *, struct ploop_snapdata *);
 	int		(*start_merge)(struct ploop_delta *, struct ploop_snapdata *);
 	int		(*complete_merge)(struct ploop_delta *);
 	int		(*truncate)(struct ploop_delta *, struct file *, __u32 alloc_head);



More information about the Devel mailing list