render / rpms / libvirt

Forked from rpms/libvirt 10 months ago
Clone
7a3408
From f218da31d36f599ecec401773fd675a1ab5e915d Mon Sep 17 00:00:00 2001
7a3408
Message-Id: <f218da31d36f599ecec401773fd675a1ab5e915d@dist-git>
7a3408
From: Peter Krempa <pkrempa@redhat.com>
7a3408
Date: Fri, 7 Aug 2015 13:39:30 +0200
7a3408
Subject: [PATCH] qemu: Fix reporting of physical capacity for block devices
7a3408
7a3408
Qemu reports physical size 0 for block devices. As 15fa84acbb55ebfee6a4
7a3408
changed the behavior of qemuDomainGetBlockInfo to just query the monitor
7a3408
this created a regression since we didn't report the size correctly any
7a3408
more.
7a3408
7a3408
This patch adds code to refresh the physical size of a block device by
7a3408
opening it and seeking to the end and uses it both in
7a3408
qemuDomainGetBlockInfo and also in qemuDomainGetStatsOneBlock that was
7a3408
broken since it was introduced in this respect.
7a3408
7a3408
Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1250982
7a3408
(cherry picked from commit 8dc27259255b79758367789ed272e909bdb56735)
7a3408
7a3408
Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
7a3408
---
7a3408
 src/libvirt_private.syms  |  1 +
7a3408
 src/qemu/qemu_driver.c    | 18 ++++++++++++++++--
7a3408
 src/util/virstoragefile.c | 39 +++++++++++++++++++++++++++++++++++++++
7a3408
 src/util/virstoragefile.h |  2 ++
7a3408
 4 files changed, 58 insertions(+), 2 deletions(-)
7a3408
7a3408
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
7a3408
index ad9ebb1..e5d8437 100644
7a3408
--- a/src/libvirt_private.syms
7a3408
+++ b/src/libvirt_private.syms
7a3408
@@ -2157,6 +2157,7 @@ virStorageSourceParseRBDColonString;
7a3408
 virStorageSourcePoolDefFree;
7a3408
 virStorageSourcePoolModeTypeFromString;
7a3408
 virStorageSourcePoolModeTypeToString;
7a3408
+virStorageSourceUpdateBlockPhysicalSize;
7a3408
 virStorageTypeFromString;
7a3408
 virStorageTypeToString;
7a3408
 
7a3408
diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
7a3408
index 8d569fe..48857ce 100644
7a3408
--- a/src/qemu/qemu_driver.c
7a3408
+++ b/src/qemu/qemu_driver.c
7a3408
@@ -11908,8 +11908,16 @@ qemuDomainGetBlockInfo(virDomainPtr dom,
7a3408
         info->allocation = entry->wr_highest_offset;
7a3408
     }
7a3408
 
7a3408
+    if (entry->physical) {
7a3408
+        info->physical = entry->physical;
7a3408
+    } else {
7a3408
+        if (virStorageSourceUpdateBlockPhysicalSize(disk->src, true) < 0)
7a3408
+            goto endjob;
7a3408
+
7a3408
+        info->physical = disk->src->physical;
7a3408
+    }
7a3408
+
7a3408
     info->capacity = entry->capacity;
7a3408
-    info->physical = entry->physical;
7a3408
 
7a3408
     ret = 0;
7a3408
 
7a3408
@@ -19355,9 +19363,15 @@ qemuDomainGetStatsOneBlock(virQEMUDriverPtr driver,
7a3408
     if (entry->capacity)
7a3408
         QEMU_ADD_BLOCK_PARAM_ULL(record, maxparams, block_idx,
7a3408
                                  "capacity", entry->capacity);
7a3408
-    if (entry->physical)
7a3408
+    if (entry->physical) {
7a3408
         QEMU_ADD_BLOCK_PARAM_ULL(record, maxparams, block_idx,
7a3408
                                  "physical", entry->physical);
7a3408
+    } else {
7a3408
+        if (virStorageSourceUpdateBlockPhysicalSize(src, false) == 0) {
7a3408
+            QEMU_ADD_BLOCK_PARAM_ULL(record, maxparams, block_idx,
7a3408
+                                     "physical", src->physical);
7a3408
+        }
7a3408
+    }
7a3408
 
7a3408
     ret = 0;
7a3408
  cleanup:
7a3408
diff --git a/src/util/virstoragefile.c b/src/util/virstoragefile.c
7a3408
index 6c3017c..2aa1d90 100644
7a3408
--- a/src/util/virstoragefile.c
7a3408
+++ b/src/util/virstoragefile.c
7a3408
@@ -2587,6 +2587,45 @@ virStorageSourceNewFromBacking(virStorageSourcePtr parent)
7a3408
 }
7a3408
 
7a3408
 
7a3408
+/**
7a3408
+ * @src: disk source definiton structure
7a3408
+ * @report: report libvirt errors if set to true
7a3408
+ *
7a3408
+ * Updates src->physical for block devices since qemu doesn't report the current
7a3408
+ * size correctly for them. Returns 0 on success, -1 on error.
7a3408
+ */
7a3408
+int
7a3408
+virStorageSourceUpdateBlockPhysicalSize(virStorageSourcePtr src,
7a3408
+                                        bool report)
7a3408
+{
7a3408
+    int fd = -1;
7a3408
+    off_t end;
7a3408
+    int ret = -1;
7a3408
+
7a3408
+    if (virStorageSourceGetActualType(src) != VIR_STORAGE_TYPE_BLOCK)
7a3408
+        return 0;
7a3408
+
7a3408
+    if ((fd = open(src->path, O_RDONLY)) < 0) {
7a3408
+        if (report)
7a3408
+            virReportSystemError(errno, _("failed to open block device '%s'"),
7a3408
+                                 src->path);
7a3408
+        return -1;
7a3408
+    }
7a3408
+
7a3408
+    if ((end = lseek(fd, 0, SEEK_END)) == (off_t) -1) {
7a3408
+        if (report)
7a3408
+            virReportSystemError(errno,
7a3408
+                                 _("failed to seek to end of '%s'"), src->path);
7a3408
+    } else {
7a3408
+        src->physical = end;
7a3408
+        ret = 0;
7a3408
+    }
7a3408
+
7a3408
+    VIR_FORCE_CLOSE(fd);
7a3408
+    return ret;
7a3408
+}
7a3408
+
7a3408
+
7a3408
 static char *
7a3408
 virStorageFileCanonicalizeFormatPath(char **components,
7a3408
                                      size_t ncomponents,
7a3408
diff --git a/src/util/virstoragefile.h b/src/util/virstoragefile.h
7a3408
index aa17a00..b98fe25 100644
7a3408
--- a/src/util/virstoragefile.h
7a3408
+++ b/src/util/virstoragefile.h
7a3408
@@ -362,6 +362,8 @@ bool virStorageSourceIsLocalStorage(virStorageSourcePtr src);
7a3408
 bool virStorageSourceIsEmpty(virStorageSourcePtr src);
7a3408
 void virStorageSourceFree(virStorageSourcePtr def);
7a3408
 void virStorageSourceBackingStoreClear(virStorageSourcePtr def);
7a3408
+int virStorageSourceUpdateBlockPhysicalSize(virStorageSourcePtr src,
7a3408
+                                            bool report);
7a3408
 virStorageSourcePtr virStorageSourceNewFromBacking(virStorageSourcePtr parent);
7a3408
 virStorageSourcePtr virStorageSourceCopy(const virStorageSource *src,
7a3408
                                          bool backingChain)
7a3408
-- 
7a3408
2.5.0
7a3408