storage: Modify virStorageBackendDiskMakeDataVol logic

Alter the logic such that we only add the volume to the pool once
we've filled in all the information and cause failure to go to a
common error: label. Patches to place the @vol into a few hash tables
will soon "require" that at least the keys (name, target.path, and key)
be populated with valid data.
This commit is contained in:
John Ferlan 2018-01-09 11:31:01 -05:00
parent ec24d2905b
commit 71d80c9726
1 changed files with 25 additions and 15 deletions

View File

@ -61,6 +61,7 @@ virStorageBackendDiskMakeDataVol(virStoragePoolObjPtr pool,
{ {
virStoragePoolDefPtr def = virStoragePoolObjGetDef(pool); virStoragePoolDefPtr def = virStoragePoolObjGetDef(pool);
char *tmp, *devpath, *partname; char *tmp, *devpath, *partname;
bool addVol = false;
/* Prepended path will be same for all partitions, so we can /* Prepended path will be same for all partitions, so we can
* strip the path to form a reasonable pool-unique name * strip the path to form a reasonable pool-unique name
@ -74,18 +75,16 @@ virStorageBackendDiskMakeDataVol(virStoragePoolObjPtr pool,
/* This is typically a reload/restart/refresh path where /* This is typically a reload/restart/refresh path where
* we're discovering the existing partitions for the pool * we're discovering the existing partitions for the pool
*/ */
addVol = true;
if (VIR_ALLOC(vol) < 0) if (VIR_ALLOC(vol) < 0)
return -1; return -1;
if (VIR_STRDUP(vol->name, partname) < 0 || if (VIR_STRDUP(vol->name, partname) < 0)
virStoragePoolObjAddVol(pool, vol) < 0) { goto error;
virStorageVolDefFree(vol);
return -1;
}
} }
if (vol->target.path == NULL) { if (vol->target.path == NULL) {
if (VIR_STRDUP(devpath, groups[0]) < 0) if (VIR_STRDUP(devpath, groups[0]) < 0)
return -1; goto error;
/* Now figure out the stable path /* Now figure out the stable path
* *
@ -96,7 +95,7 @@ virStorageBackendDiskMakeDataVol(virStoragePoolObjPtr pool,
vol->target.path = virStorageBackendStablePath(pool, devpath, true); vol->target.path = virStorageBackendStablePath(pool, devpath, true);
VIR_FREE(devpath); VIR_FREE(devpath);
if (vol->target.path == NULL) if (vol->target.path == NULL)
return -1; goto error;
} }
/* Enforce provided vol->name is the same as what parted created. /* Enforce provided vol->name is the same as what parted created.
@ -129,37 +128,37 @@ virStorageBackendDiskMakeDataVol(virStoragePoolObjPtr pool,
(tmp = strrchr(vol->target.path, 'p'))) (tmp = strrchr(vol->target.path, 'p')))
memmove(tmp, tmp + 1, strlen(tmp)); memmove(tmp, tmp + 1, strlen(tmp));
} }
return -1; goto error;
} }
if (vol->key == NULL) { if (vol->key == NULL) {
/* XXX base off a unique key of the underlying disk */ /* XXX base off a unique key of the underlying disk */
if (VIR_STRDUP(vol->key, vol->target.path) < 0) if (VIR_STRDUP(vol->key, vol->target.path) < 0)
return -1; goto error;
} }
if (vol->source.extents == NULL) { if (vol->source.extents == NULL) {
if (VIR_ALLOC(vol->source.extents) < 0) if (VIR_ALLOC(vol->source.extents) < 0)
return -1; goto error;
vol->source.nextent = 1; vol->source.nextent = 1;
if (virStrToLong_ull(groups[3], NULL, 10, if (virStrToLong_ull(groups[3], NULL, 10,
&vol->source.extents[0].start) < 0) { &vol->source.extents[0].start) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR, virReportError(VIR_ERR_INTERNAL_ERROR,
"%s", _("cannot parse device start location")); "%s", _("cannot parse device start location"));
return -1; goto error;
} }
if (virStrToLong_ull(groups[4], NULL, 10, if (virStrToLong_ull(groups[4], NULL, 10,
&vol->source.extents[0].end) < 0) { &vol->source.extents[0].end) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR, virReportError(VIR_ERR_INTERNAL_ERROR,
"%s", _("cannot parse device end location")); "%s", _("cannot parse device end location"));
return -1; goto error;
} }
if (VIR_STRDUP(vol->source.extents[0].path, if (VIR_STRDUP(vol->source.extents[0].path,
def->source.devices[0].path) < 0) def->source.devices[0].path) < 0)
return -1; goto error;
} }
/* set partition type */ /* set partition type */
@ -190,16 +189,22 @@ virStorageBackendDiskMakeDataVol(virStoragePoolObjPtr pool,
VIR_STORAGE_VOL_OPEN_DEFAULT | VIR_STORAGE_VOL_OPEN_DEFAULT |
VIR_STORAGE_VOL_OPEN_NOERROR, VIR_STORAGE_VOL_OPEN_NOERROR,
0) == -1) 0) == -1)
return -1; goto error;
vol->target.allocation = 0; vol->target.allocation = 0;
vol->target.capacity = vol->target.capacity =
(vol->source.extents[0].end - vol->source.extents[0].start); (vol->source.extents[0].end - vol->source.extents[0].start);
} else { } else {
if (virStorageBackendUpdateVolInfo(vol, false, if (virStorageBackendUpdateVolInfo(vol, false,
VIR_STORAGE_VOL_OPEN_DEFAULT, 0) < 0) VIR_STORAGE_VOL_OPEN_DEFAULT, 0) < 0)
return -1; goto error;
} }
/* Now that we've updated @vol enough, let's add it to the pool
* if it's not already there so that the subsequent pool search
* pool def adjustments will work properly */
if (addVol && virStoragePoolObjAddVol(pool, vol) < 0)
goto error;
/* Find the extended partition and increase the allocation value */ /* Find the extended partition and increase the allocation value */
if (vol->source.partType == VIR_STORAGE_VOL_DISK_TYPE_LOGICAL) { if (vol->source.partType == VIR_STORAGE_VOL_DISK_TYPE_LOGICAL) {
virStorageVolDefPtr voldef; virStorageVolDefPtr voldef;
@ -217,6 +222,11 @@ virStorageBackendDiskMakeDataVol(virStoragePoolObjPtr pool,
def->capacity = vol->source.extents[0].end; def->capacity = vol->source.extents[0].end;
return 0; return 0;
error:
if (addVol)
virStorageVolDefFree(vol);
return -1;
} }
static int static int