From a83bb0dc19d7c92c200b9a234e120d16878eac19 Mon Sep 17 00:00:00 2001
From: AlexChen <alex.chen@huawei.com>
Date: Tue, 19 Oct 2021 22:41:24 +0800
Subject: [PATCH] hotpatch: introduce hotpatch async job flag

Signed-off-by: Hao Wang <wanghao232@huawei.com>
Signed-off-by: Bihong Yu <yubihong@huawei.com>
Signed-off-by: AlexChen <alex.chen@huawei.com>
---
 include/libvirt/libvirt-domain.h |  1 +
 src/qemu/qemu_domain.c           |  3 +++
 src/qemu/qemu_domain.h           |  1 +
 src/qemu/qemu_driver.c           | 13 +++++++++++++
 src/qemu/qemu_migration.c        |  2 ++
 src/qemu/qemu_process.c          |  1 +
 tools/virsh-domain.c             |  1 +
 7 files changed, 22 insertions(+)

diff --git a/include/libvirt/libvirt-domain.h b/include/libvirt/libvirt-domain.h
index f91061724b..2d6432cab2 100644
--- a/include/libvirt/libvirt-domain.h
+++ b/include/libvirt/libvirt-domain.h
@@ -3295,6 +3295,7 @@ typedef enum {
     VIR_DOMAIN_JOB_OPERATION_SNAPSHOT_REVERT = 7,
     VIR_DOMAIN_JOB_OPERATION_DUMP = 8,
     VIR_DOMAIN_JOB_OPERATION_BACKUP = 9,
+    VIR_DOMAIN_JOB_OPERATION_HOTPATCH = 10,
 
 # ifdef VIR_ENUM_SENTINELS
     VIR_DOMAIN_JOB_OPERATION_LAST
diff --git a/src/qemu/qemu_domain.c b/src/qemu/qemu_domain.c
index 5d35d49638..2351cac120 100644
--- a/src/qemu/qemu_domain.c
+++ b/src/qemu/qemu_domain.c
@@ -111,6 +111,7 @@ VIR_ENUM_IMPL(qemuDomainAsyncJob,
               "snapshot",
               "start",
               "backup",
+              "hotpatch",
 );
 
 VIR_ENUM_IMPL(qemuDomainNamespace,
@@ -217,6 +218,7 @@ qemuDomainAsyncJobPhaseToString(qemuDomainAsyncJob job,
     case QEMU_ASYNC_JOB_START:
     case QEMU_ASYNC_JOB_NONE:
     case QEMU_ASYNC_JOB_BACKUP:
+    case QEMU_ASYNC_JOB_HOTPATCH:
         G_GNUC_FALLTHROUGH;
     case QEMU_ASYNC_JOB_LAST:
         break;
@@ -243,6 +245,7 @@ qemuDomainAsyncJobPhaseFromString(qemuDomainAsyncJob job,
     case QEMU_ASYNC_JOB_START:
     case QEMU_ASYNC_JOB_NONE:
     case QEMU_ASYNC_JOB_BACKUP:
+    case QEMU_ASYNC_JOB_HOTPATCH:
         G_GNUC_FALLTHROUGH;
     case QEMU_ASYNC_JOB_LAST:
         break;
diff --git a/src/qemu/qemu_domain.h b/src/qemu/qemu_domain.h
index cf19f4d101..678ddab624 100644
--- a/src/qemu/qemu_domain.h
+++ b/src/qemu/qemu_domain.h
@@ -107,6 +107,7 @@ typedef enum {
     QEMU_ASYNC_JOB_SNAPSHOT,
     QEMU_ASYNC_JOB_START,
     QEMU_ASYNC_JOB_BACKUP,
+    QEMU_ASYNC_JOB_HOTPATCH,
 
     QEMU_ASYNC_JOB_LAST
 } qemuDomainAsyncJob;
diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index f6d99957a5..d4c5f073bb 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -13866,6 +13866,9 @@ static int qemuDomainAbortJob(virDomainPtr dom)
         ret = 0;
         break;
 
+    case QEMU_ASYNC_JOB_HOTPATCH:
+        break;
+
     case QEMU_ASYNC_JOB_LAST:
     default:
         virReportEnumRangeError(qemuDomainAsyncJob, priv->job.asyncJob);
@@ -23180,6 +23183,7 @@ qemuDomainHotpatchManage(virDomainPtr domain,
                          unsigned int flags)
 {
     virDomainObjPtr vm;
+    virQEMUDriverPtr driver = domain->conn->privateData;
     char *ret = NULL;
     size_t len;
 
@@ -23188,6 +23192,12 @@ qemuDomainHotpatchManage(virDomainPtr domain,
     if (!(vm = qemuDomainObjFromDomain(domain)))
         goto cleanup;
 
+    if (qemuDomainObjBeginAsyncJob(driver, vm, QEMU_ASYNC_JOB_HOTPATCH,
+                                   VIR_DOMAIN_JOB_OPERATION_HOTPATCH, 0) < 0)
+        goto cleanup;
+
+    qemuDomainObjSetAsyncJobMask(vm, QEMU_JOB_DEFAULT_MASK);
+
     switch (action) {
     case VIR_DOMAIN_HOTPATCH_APPLY:
         ret = qemuDomainHotpatchApply(vm, patch);
@@ -23214,6 +23224,9 @@ qemuDomainHotpatchManage(virDomainPtr domain,
     if (len > 0)
         ret[len - 1] = '\0';
 
+ endjob:
+    qemuDomainObjEndAsyncJob(driver, vm);
+
  cleanup:
     virDomainObjEndAPI(&vm);
     return ret;
diff --git a/src/qemu/qemu_migration.c b/src/qemu/qemu_migration.c
index 3f4627bd39..1665071eb3 100644
--- a/src/qemu/qemu_migration.c
+++ b/src/qemu/qemu_migration.c
@@ -1532,6 +1532,8 @@ qemuMigrationJobName(virDomainObjPtr vm)
         return _("start job");
     case QEMU_ASYNC_JOB_BACKUP:
         return _("backup job");
+    case QEMU_ASYNC_JOB_HOTPATCH:
+        return _("hotpatch job");
     case QEMU_ASYNC_JOB_LAST:
     default:
         return _("job");
diff --git a/src/qemu/qemu_process.c b/src/qemu/qemu_process.c
index 9cf7242f31..818a72d8f9 100644
--- a/src/qemu/qemu_process.c
+++ b/src/qemu/qemu_process.c
@@ -3646,6 +3646,7 @@ qemuProcessRecoverJob(virQEMUDriverPtr driver,
         priv->job.current->started = now;
         break;
 
+    case QEMU_ASYNC_JOB_HOTPATCH:
     case QEMU_ASYNC_JOB_NONE:
     case QEMU_ASYNC_JOB_LAST:
         break;
diff --git a/tools/virsh-domain.c b/tools/virsh-domain.c
index 65d5c831ec..f643bd403e 100644
--- a/tools/virsh-domain.c
+++ b/tools/virsh-domain.c
@@ -6167,6 +6167,7 @@ VIR_ENUM_IMPL(virshDomainJobOperation,
               N_("Snapshot revert"),
               N_("Dump"),
               N_("Backup"),
+              N_("Hotpatch"),
 );
 
 static const char *
-- 
2.27.0

