]> xenbits.xensource.com Git - libvirt.git/commitdiff
util: Introduce virStorageSourceUpdateCapacity
authorJohn Ferlan <jferlan@redhat.com>
Thu, 1 Dec 2016 21:23:58 +0000 (16:23 -0500)
committerJohn Ferlan <jferlan@redhat.com>
Mon, 12 Dec 2016 21:04:17 +0000 (16:04 -0500)
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@redhat.com>
src/libvirt_private.syms
src/qemu/qemu_driver.c
src/storage/storage_backend.c
src/util/virstoragefile.c
src/util/virstoragefile.h

index 537c70d7f6b3dca2e2abb2baa334fd4659855178..ffbf46c1ac8371a2084f91f40310efb85d9cb5c0 100644 (file)
@@ -2425,7 +2425,6 @@ virStorageFileGetSCSIKey;
 virStorageFileIsClusterFS;
 virStorageFileParseChainIndex;
 virStorageFileProbeFormat;
-virStorageFileProbeFormatFromBuf;
 virStorageFileResize;
 virStorageIsFile;
 virStorageNetHostDefClear;
@@ -2451,6 +2450,7 @@ virStorageSourcePoolDefFree;
 virStorageSourcePoolModeTypeFromString;
 virStorageSourcePoolModeTypeToString;
 virStorageSourceUpdateBackingSizes;
+virStorageSourceUpdateCapacity;
 virStorageSourceUpdatePhysicalSize;
 virStorageTypeFromString;
 virStorageTypeToString;
index 8f3f603f74ce06c6c5504fb264e482bb8a8927a2..15a36fce4aee5f930abb1439cf171aa848ac7d70 100644 (file)
@@ -11667,9 +11667,7 @@ qemuStorageLimitsRefresh(virQEMUDriverPtr driver,
 {
     int ret = -1;
     int fd = -1;
-    virStorageSourcePtr meta = NULL;
     struct stat sb;
-    int format;
     char *buf = NULL;
     ssize_t len;
 
@@ -11691,27 +11689,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
@@ -11719,14 +11698,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;
 }
index 0810ced2e3aa24bd73d46481e6f6a7b226e81448..f2fc0385d88e947c6875eaf3831352f91fd835cf 100644 (file)
@@ -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;
index 181b33f6f273aa0ef336efdeb2d33002f64b7913..f750e121a1abaf715ceb3c569d921c4d26f721e2 100644 (file)
@@ -797,7 +797,7 @@ virStorageIsRelative(const char *backing)
 }
 
 
-int
+static int
 virStorageFileProbeFormatFromBuf(const char *path,
                                  char *buf,
                                  size_t buflen)
@@ -3293,6 +3293,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,
index b91045b3dd06b9cbf7f65509270c653459daa40a..6d1aac78dfb03ac423765414a630d6bbd88b4a5b 100644 (file)
@@ -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,