From: yuxiating <yuxiating(a)huawei.com>
DISCARD and WRITE_ZEROES features for machine type >= 4.0 is enabled by default
since 5c81161f8041("virtio-blk: add "discard" and "write-zeroes"
properties).
Virtio_blk kernel driver has a bug that causes memory corruption in
virtblk_setup_discard_write_zeroes();
af822aa68fbd ("block: virtio_blk: fix handling single range discard request")
has fix it.
However, some operating systems are not fixed and need to disabled on the
QEMU side.
Signed-off-by: yuxiating <yuxiating(a)huawei.com>
---
docs/formatdomain.rst | 3 +++
docs/schemas/domaincommon.rng | 8 ++++++++
src/conf/domain_conf.c | 8 ++++++++
src/conf/domain_conf.h | 1 +
src/conf/domain_validate.c | 6 ++++++
5 files changed, 26 insertions(+)
diff --git a/docs/formatdomain.rst b/docs/formatdomain.rst
index 479a3acfbb..e579b9f4dc 100644
--- a/docs/formatdomain.rst
+++ b/docs/formatdomain.rst
@@ -3087,6 +3087,9 @@ paravirtualized driver is specified via the ``disk`` element.
virtio-blk. ( :since:`Since 3.9.0` )
- For virtio disks, `Virtio-specific options <#elementsVirtio>`__ can also
be set. ( :since:`Since 3.5.0` )
+ - The optional ``discard_enable`` attribute controls whether to enable the
+ DISCRAD feature of virtio-blk.The value can be either "on" or
"off".
+ :since:`Since 7.7.0 (QEMU and KVM only)`
- The optional ``metadata_cache`` subelement controls aspects related to the
format specific caching of storage image metadata. Note that this setting
applies only on the top level image; the identically named subelement of
diff --git a/docs/schemas/domaincommon.rng b/docs/schemas/domaincommon.rng
index 11fa24f398..72576cac01 100644
--- a/docs/schemas/domaincommon.rng
+++ b/docs/schemas/domaincommon.rng
@@ -2363,6 +2363,9 @@
<ref name="positiveInteger"/>
</attribute>
</optional>
+ <optional>
+ <ref name="discard_enable"/>
+ </optional>
<ref name="virtioOptions"/>
<optional>
<element name="metadata_cache">
@@ -2468,6 +2471,11 @@
</choice>
</attribute>
</define>
+ <define name="discard_enable">
+ <attribute name='discard_enable'>
+ <ref name="virOnOff"/>
+ </attribute>
+ </define>
<define name="controller">
<element name="controller">
<optional>
diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index 6127513117..304015f42e 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -8930,6 +8930,10 @@ virDomainDiskDefDriverParseXML(virDomainDiskDef *def,
if (virXMLPropUInt(cur, "queues", 10, VIR_XML_PROP_NONE,
&def->queues) < 0)
return -1;
+ if (virXMLPropEnum(cur, "discard_enable", virTristateSwitchTypeFromString,
+ VIR_XML_PROP_NONZERO, &def->discard_enable) < 0)
+ return -1;
+
return 0;
}
@@ -23416,6 +23420,10 @@ virDomainDiskDefFormatDriver(virBuffer *buf,
if (disk->queues)
virBufferAsprintf(&attrBuf, " queues='%u'",
disk->queues);
+ if (disk->discard_enable)
+ virBufferAsprintf(&attrBuf, " discard_enable='%s'",
+ virTristateSwitchTypeToString(disk->discard_enable));
+
virDomainVirtioOptionsFormat(&attrBuf, disk->virtio);
if (disk->src->metadataCacheMaxSize > 0) {
diff --git a/src/conf/domain_conf.h b/src/conf/domain_conf.h
index c7e6df7981..56cb6e1244 100644
--- a/src/conf/domain_conf.h
+++ b/src/conf/domain_conf.h
@@ -589,6 +589,7 @@ struct _virDomainDiskDef {
bool diskElementAuth;
bool diskElementEnc;
+ virTristateSwitch discard_enable;
};
diff --git a/src/conf/domain_validate.c b/src/conf/domain_validate.c
index 60f7ccdddd..6eb346916a 100644
--- a/src/conf/domain_validate.c
+++ b/src/conf/domain_validate.c
@@ -612,6 +612,12 @@ virDomainDiskDefValidate(const virDomainDef *def,
return -1;
}
+ if (disk->discard_enable) {
+ virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
+ _("discard_enable attribute in disk driver element is
only supported by virtio-blk"));
+ return -1;
+ }
+
if (disk->event_idx != VIR_TRISTATE_SWITCH_ABSENT) {
virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
_("disk event_idx mode supported only for virtio
bus"));
--
2.27.0