From: John Ferlan Date: Thu, 1 Dec 2016 21:23:58 +0000 (-0500) Subject: util: Introduce virStorageSourceUpdateCapacity X-Git-Url: http://xenbits.xensource.com/gitweb?a=commitdiff_plain;h=9d734b60a7f49356abc4e8c402be8e5957af56fc;p=libvirt.git util: Introduce virStorageSourceUpdateCapacity 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 --- diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 537c70d7f6..ffbf46c1ac 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -2425,7 +2425,6 @@ virStorageFileGetSCSIKey; virStorageFileIsClusterFS; virStorageFileParseChainIndex; virStorageFileProbeFormat; -virStorageFileProbeFormatFromBuf; virStorageFileResize; virStorageIsFile; virStorageNetHostDefClear; @@ -2451,6 +2450,7 @@ virStorageSourcePoolDefFree; virStorageSourcePoolModeTypeFromString; virStorageSourcePoolModeTypeToString; virStorageSourceUpdateBackingSizes; +virStorageSourceUpdateCapacity; virStorageSourceUpdatePhysicalSize; virStorageTypeFromString; virStorageTypeToString; diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c index 8f3f603f74..15a36fce4a 100644 --- a/src/qemu/qemu_driver.c +++ b/src/qemu/qemu_driver.c @@ -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; } diff --git a/src/storage/storage_backend.c b/src/storage/storage_backend.c index 0810ced2e3..f2fc0385d8 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 181b33f6f2..f750e121a1 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) @@ -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, diff --git a/src/util/virstoragefile.h b/src/util/virstoragefile.h index b91045b3dd..6d1aac78df 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,