File libxl-add-discard-support.patch of Package libvirt

From d91bb4bd302995d4c680543ad1e1897a389db610 Mon Sep 17 00:00:00 2001
From: Olaf Hering <olaf@aepfle.de>
Date: Thu, 30 Jan 2014 21:25:00 +0100
Subject: libxl: add discard support to libxl_device_disk

Translate libvirt discard settings into libxl-4.5 discard settings.
It makes use of upcoming changes for xen-4.5:
http://lists.xenproject.org/archives/html/xen-devel/2014-01/msg02632.html
It makes use of out-of-tree changes for xen-4.4:
http://lists.xenproject.org/archives/html/xen-devel/2014-01/msg02690.html

Signed-off-by: Olaf Hering <olaf@aepfle.de>
---
 src/libxl/libxl_conf.c |   40 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 40 insertions(+)

Index: libvirt-1.2.5/src/libxl/libxl_conf.c
===================================================================
--- libvirt-1.2.5.orig/src/libxl/libxl_conf.c
+++ libvirt-1.2.5/src/libxl/libxl_conf.c
@@ -769,6 +769,45 @@ libxlMakeDomBuildInfo(virDomainObjPtr vm
     return -1;
 }
 
+static void
+libxlDiskSetDiscard(libxl_device_disk *x_disk, int discard)
+{
+    if (!x_disk->readwrite)
+        return;
+#if defined(LIBXL_HAVE_LIBXL_DEVICE_DISK_DISCARD_ENABLE)
+    switch (discard) {
+    case VIR_DOMAIN_DISK_DISCARD_DEFAULT:
+    case VIR_DOMAIN_DISK_DISCARD_UNMAP:
+        libxl_defbool_set(&x_disk->discard_enable, true);
+        break;
+    default:
+    case VIR_DOMAIN_DISK_DISCARD_IGNORE:
+        libxl_defbool_set(&x_disk->discard_enable, false);
+        break;
+    }
+#elif defined(LIBXL_HAVE_LIBXL_DEVICE_DISK_DISCARD_DISABLE_MAGIC)
+    switch (discard) {
+    case VIR_DOMAIN_DISK_DISCARD_DEFAULT:
+    case VIR_DOMAIN_DISK_DISCARD_UNMAP:
+        break;
+    default:
+    case VIR_DOMAIN_DISK_DISCARD_IGNORE:
+#ifdef LIBXL_HAVE_LIBXL_DEVICE_DISK_DISCARD_DISABLE_MASK
+        x_disk->readwrite = (x_disk->readwrite & LIBXL_HAVE_LIBXL_DEVICE_DISK_DISCARD_DISABLE_MASK) | LIBXL_HAVE_LIBXL_DEVICE_DISK_DISCARD_DISABLE_MAGIC;
+#else
+        x_disk->readwrite = LIBXL_HAVE_LIBXL_DEVICE_DISK_DISCARD_DISABLE_MAGIC;
+#endif
+        break;
+    }
+#else
+    if (discard == VIR_DOMAIN_DISK_DISCARD_DEFAULT)
+        return;
+    virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
+                   _("This version of libxenlight does not support "
+                     "discard= option passing"));
+#endif
+}
+
 int
 libxlMakeDisk(virDomainDiskDefPtr l_disk, libxl_device_disk *x_disk)
 {
@@ -882,6 +921,7 @@ libxlMakeDisk(virDomainDiskDefPtr l_disk
     /* XXX is this right? */
     x_disk->removable = 1;
     x_disk->readwrite = !l_disk->readonly;
+    libxlDiskSetDiscard(x_disk, l_disk->discard);
     x_disk->is_cdrom = l_disk->device == VIR_DOMAIN_DISK_DEVICE_CDROM ? 1 : 0;
     /* An empty CDROM must have the empty format, otherwise libxl fails. */
     if (x_disk->is_cdrom && !x_disk->pdev_path)