[Devel] [PATCH RHEL9 COMMIT] dm-qcow2: Extract similar code from qcow2_get_img_name() and qcow2_get_img_fd()

Konstantin Khorenko khorenko at virtuozzo.com
Tue Dec 7 17:44:57 MSK 2021


The commit is pushed to "branch-rh9-5.14.vz9.1.x-ovz" and will appear at https://src.openvz.org/scm/ovz/vzkernel.git
after rh9-5.14.0-4.vz9.10.33
------>
commit 66084f49879618364cc5de45310939d6f9206a38
Author: Kirill Tkhai <ktkhai at virtuozzo.com>
Date:   Tue Dec 7 17:44:57 2021 +0300

    dm-qcow2: Extract similar code from qcow2_get_img_name() and qcow2_get_img_fd()
    
    ... to separate function.
    
    Feature: dm-qcow2: block device over QCOW2 file driver
    Signed-off-by: Kirill Tkhai <ktkhai at virtuozzo.com>
---
 drivers/md/dm-qcow2-cmd.c | 40 +++++++++++++++++++++-------------------
 1 file changed, 21 insertions(+), 19 deletions(-)

diff --git a/drivers/md/dm-qcow2-cmd.c b/drivers/md/dm-qcow2-cmd.c
index 486d2212652e..2f1df95e219b 100644
--- a/drivers/md/dm-qcow2-cmd.c
+++ b/drivers/md/dm-qcow2-cmd.c
@@ -223,13 +223,10 @@ static int qcow2_merge_backward(struct qcow2_target *tgt)
 	return ret;
 }
 
-static int qcow2_get_img_fd(struct qcow2_target *tgt, u32 img_id,
-			    char *result, unsigned int maxlen)
+static struct qcow2 *qcow2_get_img(struct qcow2_target *tgt, u32 img_id)
 {
 	struct qcow2 *qcow2 = tgt->top;
-	unsigned int sz = 0;
-	struct file *file;
-	int skip, fd;
+	int skip;
 
 	lockdep_assert_held(&tgt->ctl_mutex); /* tgt->top */
 
@@ -239,7 +236,20 @@ static int qcow2_get_img_fd(struct qcow2_target *tgt, u32 img_id,
 		skip--;
 	}
 
-	if (!qcow2 || skip) {
+	if (!qcow2 || skip)
+		return NULL;
+	return qcow2;
+}
+
+static int qcow2_get_img_fd(struct qcow2_target *tgt, u32 img_id,
+			    char *result, unsigned int maxlen)
+{
+	struct qcow2 *qcow2;
+	unsigned int sz = 0;
+	int fd;
+
+	qcow2 = qcow2_get_img(tgt, img_id);
+	if (!qcow2) {
 		result[0] = 0; /* empty output */
 		return 1;
 	}
@@ -254,27 +264,19 @@ static int qcow2_get_img_fd(struct qcow2_target *tgt, u32 img_id,
 		return 0;
 	}
 
-	file = qcow2->file;
-	fd_install(fd, get_file(file));
+	fd_install(fd, get_file(qcow2->file));
 	return 1;
 }
 
 static int qcow2_get_img_name(struct qcow2_target *tgt, u32 img_id,
 			      char *result, unsigned int maxlen)
 {
-	struct qcow2 *qcow2 = tgt->top;
-	int skip, ret;
+	struct qcow2 *qcow2;
 	char *p;
+	int ret;
 
-	lockdep_assert_held(&tgt->ctl_mutex); /* tgt->top */
-
-	skip = tgt->nr_images - 1 - img_id;
-	while (qcow2 && skip > 0) {
-		qcow2 = qcow2->lower;
-		skip--;
-	}
-
-	if (!qcow2 || skip) {
+	qcow2 = qcow2_get_img(tgt, img_id);
+	if (!qcow2) {
 		result[0] = 0; /* empty output */
 		return 1;
 	}


More information about the Devel mailing list