]> xenbits.xensource.com Git - libvirt.git/commitdiff
util: Introduce virStorageSourceUpdateBackingSizes
authorJohn Ferlan <jferlan@redhat.com>
Thu, 1 Dec 2016 21:39:36 +0000 (16:39 -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
virStorageBackendUpdateVolTargetInfoFD to fill in the storage backing
source or volume allocation, capacity, and physical values - create a
common API that will handle the details for both.

The common API will fill in "default" capacity values as well - although
those more than likely will be overridden by subsequent code. Having just
one place to make the determination of what the values should be will
make things be more consistent.

For the QEMU code - the data filled in will be for inactive domains
for the GetBlockInfo and DomainGetStatsOneBlock API's. For the storage
backend code - the data will be filled in during the volume updates.

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 7d74ec5fb46fd82985f896dc83bd0e875fec8d0c..537c70d7f6b3dca2e2abb2baa334fd4659855178 100644 (file)
@@ -2450,6 +2450,7 @@ virStorageSourceParseRBDColonString;
 virStorageSourcePoolDefFree;
 virStorageSourcePoolModeTypeFromString;
 virStorageSourcePoolModeTypeToString;
+virStorageSourceUpdateBackingSizes;
 virStorageSourceUpdatePhysicalSize;
 virStorageTypeFromString;
 virStorageTypeToString;
index 3157723359d321a4ee7ca2be7963d2b463dacf84..8f3f603f74ce06c6c5504fb264e482bb8a8927a2 100644 (file)
@@ -11667,7 +11667,6 @@ qemuStorageLimitsRefresh(virQEMUDriverPtr driver,
 {
     int ret = -1;
     int fd = -1;
-    off_t end;
     virStorageSourcePtr meta = NULL;
     struct stat sb;
     int format;
@@ -11689,34 +11688,8 @@ qemuStorageLimitsRefresh(virQEMUDriverPtr driver,
             goto cleanup;
     }
 
-    /* Get info for normal formats */
-    if (S_ISREG(sb.st_mode) || fd == -1) {
-#ifndef WIN32
-        src->allocation = (unsigned long long)sb.st_blocks *
-            (unsigned long long)DEV_BSIZE;
-#else
-        src->allocation = sb.st_size;
-#endif
-        /* Allocation tracks when the file is sparse, physical is the
-         * last offset of the file. */
-        src->physical = sb.st_size;
-    } else {
-        /* NB. Because we configure with AC_SYS_LARGEFILE, off_t
-         * should be 64 bits on all platforms.  For block devices, we
-         * have to seek (safe even if someone else is writing) to
-         * determine physical size, and assume that allocation is the
-         * same as physical (but can refine that assumption later if
-         * qemu is still running).
-         */
-        end = lseek(fd, 0, SEEK_END);
-        if (end == (off_t)-1) {
-            virReportSystemError(errno,
-                                 _("failed to seek to end of %s"), src->path);
-            goto cleanup;
-        }
-        src->physical = end;
-        src->allocation = end;
-    }
+    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
index d4334dca223c1eca1eeb0e7856ccd3dc740393c5..0810ced2e3aa24bd73d46481e6f6a7b226e81448 100644 (file)
@@ -2004,37 +2004,8 @@ virStorageBackendUpdateVolTargetInfoFD(virStorageSourcePtr target,
     security_context_t filecon = NULL;
 #endif
 
-    if (S_ISREG(sb->st_mode)) {
-#ifndef WIN32
-        target->allocation = (unsigned long long)sb->st_blocks *
-            (unsigned long long)DEV_BSIZE;
-#else
-        target->allocation = sb->st_size;
-#endif
-        /* Regular files may be sparse, so logical size (capacity) is not same
-         * as actual allocation above
-         */
-        target->capacity = sb->st_size;
-    } else if (S_ISDIR(sb->st_mode)) {
-        target->allocation = 0;
-        target->capacity = 0;
-    } else if (fd >= 0) {
-        off_t end;
-        /* XXX this is POSIX compliant, but doesn't work for CHAR files,
-         * only BLOCK. There is a Linux specific ioctl() for getting
-         * size of both CHAR / BLOCK devices we should check for in
-         * configure
-         */
-        end = lseek(fd, 0, SEEK_END);
-        if (end == (off_t)-1) {
-            virReportSystemError(errno,
-                                 _("cannot seek to end of file '%s'"),
-                                 target->path);
-            return -1;
-        }
-        target->allocation = end;
-        target->capacity = end;
-    }
+    if (virStorageSourceUpdateBackingSizes(target, fd, sb) < 0)
+        return -1;
 
     if (!target->perms && VIR_ALLOC(target->perms) < 0)
         return -1;
index 47b6444abfc8f78f52f39592934ca08c8ced8e25..181b33f6f273aa0ef336efdeb2d33002f64b7913 100644 (file)
@@ -3228,6 +3228,71 @@ virStorageSourceUpdatePhysicalSize(virStorageSourcePtr src,
 }
 
 
+/**
+ * @src: disk source definition structure
+ * @fd: file descriptor
+ * @sb: stat buffer
+ *
+ * Update the capacity, allocation, physical values for the storage @src
+ * Shared between the domain storage source for an inactive domain and the
+ * voldef source target as the result is not affected by the 'type' field.
+ *
+ * Returns 0 on success, -1 on error.
+ */
+int
+virStorageSourceUpdateBackingSizes(virStorageSourcePtr src,
+                                   int fd,
+                                   struct stat const *sb)
+{
+    /* Get info for normal formats */
+    if (S_ISREG(sb->st_mode) || fd == -1) {
+#ifndef WIN32
+        src->allocation = (unsigned long long)sb->st_blocks *
+            (unsigned long long)DEV_BSIZE;
+#else
+        src->allocation = sb->st_size;
+#endif
+        /* Regular files may be sparse, so logical size (capacity) is not same
+         * as actual allocation above
+         */
+        src->capacity = sb->st_size;
+
+        /* Allocation tracks when the file is sparse, physical is the
+         * last offset of the file. */
+        src->physical = sb->st_size;
+    } else if (S_ISDIR(sb->st_mode)) {
+        src->allocation = 0;
+        src->capacity = 0;
+        src->physical = 0;
+    } else if (fd >= 0) {
+        off_t end;
+
+        /* XXX this is POSIX compliant, but doesn't work for CHAR files,
+         * only BLOCK. There is a Linux specific ioctl() for getting
+         * size of both CHAR / BLOCK devices we should check for in
+         * configure
+         *
+         * NB. Because we configure with AC_SYS_LARGEFILE, off_t
+         * should be 64 bits on all platforms.  For block devices, we
+         * have to seek (safe even if someone else is writing) to
+         * determine physical size, and assume that allocation is the
+         * same as physical (but can refine that assumption later if
+         * qemu is still running).
+         */
+        if ((end = lseek(fd, 0, SEEK_END)) == (off_t)-1) {
+            virReportSystemError(errno,
+                                 _("failed to seek to end of %s"), src->path);
+            return -1;
+        }
+        src->physical = end;
+        src->allocation = end;
+        src->capacity = end;
+    }
+
+    return 0;
+}
+
+
 static char *
 virStorageFileCanonicalizeFormatPath(char **components,
                                      size_t ncomponents,
index e38d01fd709b5e7875e739053a75900fbfa99423..b91045b3dd06b9cbf7f65509270c653459daa40a 100644 (file)
@@ -359,6 +359,9 @@ void virStorageSourceFree(virStorageSourcePtr def);
 void virStorageSourceBackingStoreClear(virStorageSourcePtr def);
 int virStorageSourceUpdatePhysicalSize(virStorageSourcePtr src,
                                        int fd, struct stat const *sb);
+int virStorageSourceUpdateBackingSizes(virStorageSourcePtr src,
+                                       int fd, struct stat const *sb);
+
 virStorageSourcePtr virStorageSourceNewFromBacking(virStorageSourcePtr parent);
 virStorageSourcePtr virStorageSourceCopy(const virStorageSource *src,
                                          bool backingChain)