[libvirt] [PATCH 6/7] util: Introduce virStorageSourceUpdateCapacity

John Ferlan jferlan at redhat.com
Fri Dec 2 00:39:46 UTC 2016


Instead of having duplicated code in qemuStorageLimitsRefresh and
virStorageBackendUpdateVolTargetInfo to get capacity specific data
about the storage backing source or volume -- create a common API
to handle the details for both.

As a side effect, virStorageFileProbeFormatFromBuf returns to being
a local/static helper to virstoragefile.c

For the QEMU code - if the probe is done, then the format is saved so
as to avoid future such probes.

For the storage backend code, there is no need to deal with the probe
since we cannot call the new API if target->format == NONE.

Signed-off-by: John Ferlan <jferlan at redhat.com>
---
 src/libvirt_private.syms      |  2 +-
 src/qemu/qemu_driver.c        | 29 +++-------------------
 src/storage/storage_backend.c |  8 +-----
 src/util/virstoragefile.c     | 57 ++++++++++++++++++++++++++++++++++++++++++-
 src/util/virstoragefile.h     |  6 ++---
 5 files changed, 65 insertions(+), 37 deletions(-)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 7a78905..4b4a197 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -2417,7 +2417,6 @@ virStorageFileGetSCSIKey;
 virStorageFileIsClusterFS;
 virStorageFileParseChainIndex;
 virStorageFileProbeFormat;
-virStorageFileProbeFormatFromBuf;
 virStorageFileResize;
 virStorageIsFile;
 virStorageNetHostDefClear;
@@ -2443,6 +2442,7 @@ virStorageSourcePoolDefFree;
 virStorageSourcePoolModeTypeFromString;
 virStorageSourcePoolModeTypeToString;
 virStorageSourceUpdateBackingSizes;
+virStorageSourceUpdateCapacity;
 virStorageSourceUpdatePhysicalSize;
 virStorageTypeFromString;
 virStorageTypeToString;
diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index 3d20e30..f508872 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -11657,9 +11657,7 @@ qemuStorageLimitsRefresh(virQEMUDriverPtr driver,
 {
     int ret = -1;
     int fd = -1;
-    virStorageSourcePtr meta = NULL;
     struct stat sb;
-    int format;
     char *buf = NULL;
     ssize_t len;
 
@@ -11681,27 +11679,8 @@ qemuStorageLimitsRefresh(virQEMUDriverPtr driver,
     if (virStorageSourceUpdateBackingSizes(src, fd, &sb) < 0)
         goto cleanup;
 
-    /* Raw files: capacity is physical size.  For all other files: if
-     * the metadata has a capacity, use that, otherwise fall back to
-     * physical size.  */
-    if (!(format = src->format)) {
-        if (!cfg->allowDiskFormatProbing) {
-            virReportError(VIR_ERR_INTERNAL_ERROR,
-                           _("no disk format for %s and probing is disabled"),
-                           src->path);
-            goto cleanup;
-        }
-
-        if ((format = virStorageFileProbeFormatFromBuf(src->path,
-                                                       buf, len)) < 0)
-            goto cleanup;
-    }
-    if (format == VIR_STORAGE_FILE_RAW)
-        src->capacity = src->physical;
-    else if ((meta = virStorageFileGetMetadataFromBuf(src->path, buf,
-                                                      len, format, NULL)))
-        src->capacity = meta->capacity ? meta->capacity : src->physical;
-    else
+    if (virStorageSourceUpdateCapacity(src, buf, len,
+                                       cfg->allowDiskFormatProbing) < 0)
         goto cleanup;
 
     /* If guest is not using raw disk format and is on a host block
@@ -11709,14 +11688,14 @@ qemuStorageLimitsRefresh(virQEMUDriverPtr driver,
      * query the highest allocated extent from QEMU
      */
     if (virStorageSourceGetActualType(src) == VIR_STORAGE_TYPE_BLOCK &&
-        format != VIR_STORAGE_FILE_RAW &&
+        src->format != VIR_STORAGE_FILE_RAW &&
         S_ISBLK(sb.st_mode))
         src->allocation = 0;
 
     ret = 0;
+
  cleanup:
     VIR_FREE(buf);
-    virStorageSourceFree(meta);
     qemuDomainStorageCloseStat(src, &fd);
     return ret;
 }
diff --git a/src/storage/storage_backend.c b/src/storage/storage_backend.c
index 0810ced..a433190 100644
--- a/src/storage/storage_backend.c
+++ b/src/storage/storage_backend.c
@@ -1884,7 +1884,6 @@ virStorageBackendUpdateVolTargetInfo(virStorageSourcePtr target,
 {
     int ret, fd = -1;
     struct stat sb;
-    virStorageSourcePtr meta = NULL;
     char *buf = NULL;
     ssize_t len = VIR_STORAGE_MAX_HEADER;
 
@@ -1929,14 +1928,10 @@ virStorageBackendUpdateVolTargetInfo(virStorageSourcePtr target,
             goto cleanup;
         }
 
-        if (!(meta = virStorageFileGetMetadataFromBuf(target->path, buf, len, target->format,
-                                                      NULL))) {
+        if (virStorageSourceUpdateCapacity(target, buf, len,  false) < 0) {
             ret = -1;
             goto cleanup;
         }
-
-        if (meta->capacity)
-            target->capacity = meta->capacity;
     }
 
     if (withBlockVolFormat) {
@@ -1946,7 +1941,6 @@ virStorageBackendUpdateVolTargetInfo(virStorageSourcePtr target,
     }
 
  cleanup:
-    virStorageSourceFree(meta);
     VIR_FORCE_CLOSE(fd);
     VIR_FREE(buf);
     return ret;
diff --git a/src/util/virstoragefile.c b/src/util/virstoragefile.c
index ab0f9bd..cf62ca9 100644
--- a/src/util/virstoragefile.c
+++ b/src/util/virstoragefile.c
@@ -797,7 +797,7 @@ virStorageIsRelative(const char *backing)
 }
 
 
-int
+static int
 virStorageFileProbeFormatFromBuf(const char *path,
                                  char *buf,
                                  size_t buflen)
@@ -3289,6 +3289,61 @@ virStorageSourceUpdateBackingSizes(virStorageSourcePtr src,
 }
 
 
+/**
+ * @src: disk source definition structure
+ * @buf: buffer to the storage file header
+ * @len: length of the storage file header
+ * @probe: allow probe
+ *
+ * Update the storage @src capacity. This may involve probing the storage
+ * @src in order to "see" if we can recognize what exists.
+ *
+ * Returns 0 on success, -1 on error.
+ */
+int
+virStorageSourceUpdateCapacity(virStorageSourcePtr src,
+                               char *buf,
+                               ssize_t len,
+                               bool probe)
+{
+    int ret = -1;
+    virStorageSourcePtr meta = NULL;
+    int format = src->format;
+
+    /* Raw files: capacity is physical size.  For all other files: if
+     * the metadata has a capacity, use that, otherwise fall back to
+     * physical size.  */
+    if (format == VIR_STORAGE_FILE_NONE) {
+        if (!probe) {
+            virReportError(VIR_ERR_INTERNAL_ERROR,
+                           _("no disk format for %s and probing is disabled"),
+                           src->path);
+            goto cleanup;
+        }
+
+        if ((format = virStorageFileProbeFormatFromBuf(src->path,
+                                                       buf, len)) < 0)
+            goto cleanup;
+
+        src->format = format;
+    }
+
+    if (format == VIR_STORAGE_FILE_RAW)
+        src->capacity = src->physical;
+    else if ((meta = virStorageFileGetMetadataFromBuf(src->path, buf,
+                                                      len, format, NULL)))
+        src->capacity = meta->capacity ? meta->capacity : src->physical;
+    else
+        goto cleanup;
+
+    ret = 0;
+
+ cleanup:
+    virStorageSourceFree(meta);
+    return ret;
+}
+
+
 static char *
 virStorageFileCanonicalizeFormatPath(char **components,
                                      size_t ncomponents,
diff --git a/src/util/virstoragefile.h b/src/util/virstoragefile.h
index b91045b..6d1aac7 100644
--- a/src/util/virstoragefile.h
+++ b/src/util/virstoragefile.h
@@ -284,9 +284,6 @@ struct _virStorageSource {
 # endif
 
 int virStorageFileProbeFormat(const char *path, uid_t uid, gid_t gid);
-int virStorageFileProbeFormatFromBuf(const char *path,
-                                     char *buf,
-                                     size_t buflen);
 
 int virStorageFileGetMetadataInternal(virStorageSourcePtr meta,
                                       char *buf,
@@ -361,6 +358,9 @@ int virStorageSourceUpdatePhysicalSize(virStorageSourcePtr src,
                                        int fd, struct stat const *sb);
 int virStorageSourceUpdateBackingSizes(virStorageSourcePtr src,
                                        int fd, struct stat const *sb);
+int virStorageSourceUpdateCapacity(virStorageSourcePtr src,
+                                   char *buf, ssize_t len,
+                                   bool probe);
 
 virStorageSourcePtr virStorageSourceNewFromBacking(virStorageSourcePtr parent);
 virStorageSourcePtr virStorageSourceCopy(const virStorageSource *src,
-- 
2.7.4




More information about the libvir-list mailing list