From: Martin Kletzander <mklet...@redhat.com>

That allows for disabling discard in a way that some guest
OSes (e.g. Windows) understand and do not try to trim the disk.

Resolves: https://issues.redhat.com/browse/RHEL-72006
Signed-off-by: Martin Kletzander <mklet...@redhat.com>
---
Out of all the approaches for allowing previously default value 100% of them are
ugly.  This one is maybe slightly more error-prone.

 src/conf/domain_conf.c     | 12 ++++++++----
 src/conf/domain_conf.h     |  1 +
 src/conf/domain_validate.c |  2 +-
 src/qemu/qemu_command.c    |  7 ++++++-
 tests/qemuxmlconftest.c    |  1 +
 5 files changed, 17 insertions(+), 6 deletions(-)

diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index bfc62b62705f..ba0d4a7b128c 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -8554,6 +8554,8 @@ virDomainDiskDefParseXML(virDomainXMLOption *xmlopt,
     }
 
     if ((blockioNode = virXPathNode("./blockio", ctxt))) {
+        int tmp = 0;
+
         if (virXMLPropUInt(blockioNode, "logical_block_size", 10, 
VIR_XML_PROP_NONE,
                            &def->blockio.logical_block_size) < 0)
             return NULL;
@@ -8562,9 +8564,11 @@ virDomainDiskDefParseXML(virDomainXMLOption *xmlopt,
                            &def->blockio.physical_block_size) < 0)
             return NULL;
 
-        if (virXMLPropUInt(blockioNode, "discard_granularity", 10, 
VIR_XML_PROP_NONE,
-                           &def->blockio.discard_granularity) < 0)
+        if ((tmp = virXMLPropUInt(blockioNode, "discard_granularity", 10, 
VIR_XML_PROP_NONE,
+                                  &def->blockio.discard_granularity)) < 0)
             return NULL;
+        if (tmp > 0)
+            def->blockio.discard_granularity_specified = true;
     }
 
     if ((driverNode = virXPathNode("./driver", ctxt))) {
@@ -23102,7 +23106,7 @@ virDomainDiskBlockIoDefFormat(virBuffer *buf,
 {
     if (def->blockio.logical_block_size > 0 ||
         def->blockio.physical_block_size > 0 ||
-        def->blockio.discard_granularity > 0) {
+        def->blockio.discard_granularity_specified) {
         virBufferAddLit(buf, "<blockio");
         if (def->blockio.logical_block_size > 0) {
             virBufferAsprintf(buf,
@@ -23114,7 +23118,7 @@ virDomainDiskBlockIoDefFormat(virBuffer *buf,
                               " physical_block_size='%u'",
                               def->blockio.physical_block_size);
         }
-        if (def->blockio.discard_granularity > 0) {
+        if (def->blockio.discard_granularity_specified) {
             virBufferAsprintf(buf,
                               " discard_granularity='%u'",
                               def->blockio.discard_granularity);
diff --git a/src/conf/domain_conf.h b/src/conf/domain_conf.h
index 6997cf7c09be..6008ec66d3fe 100644
--- a/src/conf/domain_conf.h
+++ b/src/conf/domain_conf.h
@@ -560,6 +560,7 @@ struct _virDomainDiskDef {
         unsigned int logical_block_size;
         unsigned int physical_block_size;
         unsigned int discard_granularity;
+        bool discard_granularity_specified;
     } blockio;
 
     virDomainBlockIoTuneInfo blkdeviotune;
diff --git a/src/conf/domain_validate.c b/src/conf/domain_validate.c
index b28af7fa5618..8f7259a0e1ed 100644
--- a/src/conf/domain_validate.c
+++ b/src/conf/domain_validate.c
@@ -462,7 +462,7 @@ virDomainDiskVhostUserValidate(const virDomainDiskDef *disk)
 
     if (disk->blockio.logical_block_size > 0 ||
         disk->blockio.physical_block_size > 0 ||
-        disk->blockio.discard_granularity > 0) {
+        disk->blockio.discard_granularity_specified) {
         virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                        _("blockio is not supported with vhostuser disk"));
         return -1;
diff --git a/src/qemu/qemu_command.c b/src/qemu/qemu_command.c
index 7658cc4d395a..fc6ce4dd9143 100644
--- a/src/qemu/qemu_command.c
+++ b/src/qemu/qemu_command.c
@@ -1899,7 +1899,6 @@ qemuBuildDiskDeviceProps(const virDomainDef *def,
                               "S:loadparm", bootLoadparm,
                               "p:logical_block_size", logical_block_size,
                               "p:physical_block_size", physical_block_size,
-                              "p:discard_granularity", discard_granularity,
                               "A:wwn", &wwn,
                               "p:rotation_rate", disk->rotation_rate,
                               "S:vendor", disk->vendor,
@@ -1917,6 +1916,12 @@ qemuBuildDiskDeviceProps(const virDomainDef *def,
                               NULL) < 0)
         return NULL;
 
+    if (disk->blockio.discard_granularity_specified &&
+        virJSONValueObjectAdd(&props,
+                              "u:discard_granularity", discard_granularity,
+                              NULL) < 0)
+        return NULL;
+
     return g_steal_pointer(&props);
 }
 
diff --git a/tests/qemuxmlconftest.c b/tests/qemuxmlconftest.c
index 9fba98429019..ae8efd58f8ac 100644
--- a/tests/qemuxmlconftest.c
+++ b/tests/qemuxmlconftest.c
@@ -1694,6 +1694,7 @@ mymain(void)
     DO_TEST_CAPS_LATEST("disk-ide-wwn");
     DO_TEST_CAPS_LATEST("disk-geometry");
     DO_TEST_CAPS_LATEST("disk-blockio");
+    DO_TEST_CAPS_LATEST("disk-blockio-no-discard");
 
     driver.config->storageUseNbdkit = 1;
     DO_TEST_CAPS_LATEST_NBDKIT("disk-cdrom-network-nbdkit", 
QEMU_NBDKIT_CAPS_PLUGIN_CURL);
-- 
2.50.1

Reply via email to