conf: consolidate disk def allocation

A future patch wants to create disk definitions with non-zero
default contents; to avoid crashes, all callers that allocate
a disk definition should go through a common point.

I found allocation points by looking for any code that increments
ndisks, as well as any matches for ALLOC.*disk.  Most places that
modified ndisks were covered by the parse from XML to domain/device
definition by initial domain creation or device hotplug; I also
hand-checked all drivers that generate a device struct on the
fly during getXMLDesc.

* src/conf/domain_conf.h (virDomainDiskDefNew): New prototype.
* src/conf/domain_conf.c (virDomainDiskDefNew): New function.
(virDomainDiskDefParseXML): Use it.
* src/parallels/parallels_driver.c (parallelsAddHddInfo):
Likewise.
* src/qemu/qemu_command.c (qemuParseCommandLine): Likewise.
* src/vbox/vbox_tmpl.c (vboxDomainGetXMLDesc): Likewise.
* src/vmx/vmx.c (virVMXParseDisk): Likewise.
* src/xenxs/xen_sxpr.c (xenParseSxprDisks, xenParseSxpr):
Likewise.
* src/xenxs/xen_xm.c (xenParseXM): Likewise.
* src/libvirt_private.syms (domain_conf.h): Export it.

Signed-off-by: Eric Blake <eblake@redhat.com>
This commit is contained in:
Eric Blake 2014-05-21 16:50:41 -06:00
parent 00c6327a12
commit bc3f5f190e
9 changed files with 26 additions and 14 deletions

View File

@ -1181,6 +1181,16 @@ void virDomainLeaseDefFree(virDomainLeaseDefPtr def)
} }
virDomainDiskDefPtr
virDomainDiskDefNew(void)
{
virDomainDiskDefPtr ret;
ignore_value(VIR_ALLOC(ret));
return ret;
}
void void
virDomainDiskDefFree(virDomainDiskDefPtr def) virDomainDiskDefFree(virDomainDiskDefPtr def)
{ {
@ -5232,7 +5242,7 @@ virDomainDiskDefParseXML(virDomainXMLOptionPtr xmlopt,
int expected_secret_usage = -1; int expected_secret_usage = -1;
int auth_secret_usage = -1; int auth_secret_usage = -1;
if (VIR_ALLOC(def) < 0) if (!(def = virDomainDiskDefNew()))
return NULL; return NULL;
def->geometry.cylinders = 0; def->geometry.cylinders = 0;

View File

@ -2129,6 +2129,7 @@ void virDomainPanicDefFree(virDomainPanicDefPtr panic);
void virDomainResourceDefFree(virDomainResourceDefPtr resource); void virDomainResourceDefFree(virDomainResourceDefPtr resource);
void virDomainGraphicsDefFree(virDomainGraphicsDefPtr def); void virDomainGraphicsDefFree(virDomainGraphicsDefPtr def);
void virDomainInputDefFree(virDomainInputDefPtr def); void virDomainInputDefFree(virDomainInputDefPtr def);
virDomainDiskDefPtr virDomainDiskDefNew(void);
void virDomainDiskDefFree(virDomainDiskDefPtr def); void virDomainDiskDefFree(virDomainDiskDefPtr def);
void virDomainLeaseDefFree(virDomainLeaseDefPtr def); void virDomainLeaseDefFree(virDomainLeaseDefPtr def);
int virDomainDiskGetType(virDomainDiskDefPtr def); int virDomainDiskGetType(virDomainDiskDefPtr def);

View File

@ -207,6 +207,7 @@ virDomainDiskDefAssignAddress;
virDomainDiskDefForeachPath; virDomainDiskDefForeachPath;
virDomainDiskDefFree; virDomainDiskDefFree;
virDomainDiskDefGetSecurityLabelDef; virDomainDiskDefGetSecurityLabelDef;
virDomainDiskDefNew;
virDomainDiskDeviceTypeToString; virDomainDiskDeviceTypeToString;
virDomainDiskDiscardTypeToString; virDomainDiskDiscardTypeToString;
virDomainDiskErrorPolicyTypeFromString; virDomainDiskErrorPolicyTypeFromString;

View File

@ -407,7 +407,7 @@ parallelsAddHddInfo(virDomainDefPtr def, const char *key, virJSONValuePtr value)
{ {
virDomainDiskDefPtr disk = NULL; virDomainDiskDefPtr disk = NULL;
if (VIR_ALLOC(disk) < 0) if (!(disk = virDomainDiskDefNew()))
goto error; goto error;
if (parallelsGetHddInfo(def, disk, key, value)) if (parallelsGetHddInfo(def, disk, key, value))

View File

@ -10997,7 +10997,7 @@ qemuParseCommandLine(virCapsPtr qemuCaps,
STRPREFIX(arg, "-fd") || STRPREFIX(arg, "-fd") ||
STREQ(arg, "-cdrom")) { STREQ(arg, "-cdrom")) {
WANT_VALUE(); WANT_VALUE();
if (VIR_ALLOC(disk) < 0) if (!(disk = virDomainDiskDefNew()))
goto error; goto error;
if (STRPREFIX(val, "/dev/")) if (STRPREFIX(val, "/dev/"))
@ -11297,7 +11297,7 @@ qemuParseCommandLine(virCapsPtr qemuCaps,
goto error; goto error;
} }
} else if (STRPREFIX(val, "disk:")) { } else if (STRPREFIX(val, "disk:")) {
if (VIR_ALLOC(disk) < 0) if (!(disk = virDomainDiskDefNew()))
goto error; goto error;
if (VIR_STRDUP(disk->src.path, val + strlen("disk:")) < 0) if (VIR_STRDUP(disk->src.path, val + strlen("disk:")) < 0)
goto error; goto error;

View File

@ -2768,7 +2768,7 @@ static char *vboxDomainGetXMLDesc(virDomainPtr dom, unsigned int flags) {
if ((def->ndisks > 0) && (VIR_ALLOC_N(def->disks, def->ndisks) >= 0)) { if ((def->ndisks > 0) && (VIR_ALLOC_N(def->disks, def->ndisks) >= 0)) {
for (i = 0; i < def->ndisks; i++) { for (i = 0; i < def->ndisks; i++) {
if (VIR_ALLOC(def->disks[i]) >= 0) { if ((def->disks[i] = virDomainDiskDefNew())) {
def->disks[i]->device = VIR_DOMAIN_DISK_DEVICE_DISK; def->disks[i]->device = VIR_DOMAIN_DISK_DEVICE_DISK;
def->disks[i]->bus = VIR_DOMAIN_DISK_BUS_IDE; def->disks[i]->bus = VIR_DOMAIN_DISK_BUS_IDE;
virDomainDiskSetType(def->disks[i], virDomainDiskSetType(def->disks[i],
@ -3247,7 +3247,7 @@ static char *vboxDomainGetXMLDesc(virDomainPtr dom, unsigned int flags) {
def->ndisks++; def->ndisks++;
if (VIR_REALLOC_N(def->disks, def->ndisks) >= 0) { if (VIR_REALLOC_N(def->disks, def->ndisks) >= 0) {
if (VIR_ALLOC(def->disks[def->ndisks - 1]) >= 0) { if ((def->disks[def->ndisks - 1] = virDomainDiskDefNew())) {
def->disks[def->ndisks - 1]->device = VIR_DOMAIN_DISK_DEVICE_CDROM; def->disks[def->ndisks - 1]->device = VIR_DOMAIN_DISK_DEVICE_CDROM;
def->disks[def->ndisks - 1]->bus = VIR_DOMAIN_DISK_BUS_IDE; def->disks[def->ndisks - 1]->bus = VIR_DOMAIN_DISK_BUS_IDE;
virDomainDiskSetType(def->disks[def->ndisks - 1], virDomainDiskSetType(def->disks[def->ndisks - 1],
@ -3294,7 +3294,7 @@ static char *vboxDomainGetXMLDesc(virDomainPtr dom, unsigned int flags) {
def->ndisks++; def->ndisks++;
if (VIR_REALLOC_N(def->disks, def->ndisks) >= 0) { if (VIR_REALLOC_N(def->disks, def->ndisks) >= 0) {
if (VIR_ALLOC(def->disks[def->ndisks - 1]) >= 0) { if ((def->disks[def->ndisks - 1] = virDomainDiskDefNew())) {
def->disks[def->ndisks - 1]->device = VIR_DOMAIN_DISK_DEVICE_FLOPPY; def->disks[def->ndisks - 1]->device = VIR_DOMAIN_DISK_DEVICE_FLOPPY;
def->disks[def->ndisks - 1]->bus = VIR_DOMAIN_DISK_BUS_FDC; def->disks[def->ndisks - 1]->bus = VIR_DOMAIN_DISK_BUS_FDC;
virDomainDiskSetType(def->disks[def->ndisks - 1], virDomainDiskSetType(def->disks[def->ndisks - 1],

View File

@ -1998,7 +1998,7 @@ virVMXParseDisk(virVMXContext *ctx, virDomainXMLOptionPtr xmlopt, virConfPtr con
return -1; return -1;
} }
if (VIR_ALLOC(*def) < 0) if (!(*def = virDomainDiskDefNew()))
return -1; return -1;
(*def)->device = device; (*def)->device = device;

View File

@ -370,7 +370,7 @@ xenParseSxprDisks(virDomainDefPtr def,
bootable = sexpr_node(node, "device/tap/bootable"); bootable = sexpr_node(node, "device/tap/bootable");
} }
if (VIR_ALLOC(disk) < 0) if (!(disk = virDomainDiskDefNew()))
goto error; goto error;
if (dst == NULL) { if (dst == NULL) {
@ -1304,7 +1304,7 @@ xenParseSxpr(const struct sexpr *root,
tmp = sexpr_node(root, "domain/image/hvm/cdrom"); tmp = sexpr_node(root, "domain/image/hvm/cdrom");
if ((tmp != NULL) && (tmp[0] != 0)) { if ((tmp != NULL) && (tmp[0] != 0)) {
virDomainDiskDefPtr disk; virDomainDiskDefPtr disk;
if (VIR_ALLOC(disk) < 0) if (!(disk = virDomainDiskDefNew()))
goto error; goto error;
if (virDomainDiskSetSource(disk, tmp) < 0) { if (virDomainDiskSetSource(disk, tmp) < 0) {
virDomainDiskDefFree(disk); virDomainDiskDefFree(disk);
@ -1339,10 +1339,10 @@ xenParseSxpr(const struct sexpr *root,
tmp = sexpr_fmt_node(root, "domain/image/hvm/%s", fds[i]); tmp = sexpr_fmt_node(root, "domain/image/hvm/%s", fds[i]);
if ((tmp != NULL) && (tmp[0] != 0)) { if ((tmp != NULL) && (tmp[0] != 0)) {
virDomainDiskDefPtr disk; virDomainDiskDefPtr disk;
if (VIR_ALLOC(disk) < 0) if (!(disk = virDomainDiskDefNew()))
goto error; goto error;
if (virDomainDiskSetSource(disk, tmp) < 0) { if (virDomainDiskSetSource(disk, tmp) < 0) {
VIR_FREE(disk); virDomainDiskDefFree(disk);
goto error; goto error;
} }
virDomainDiskSetType(disk, VIR_STORAGE_TYPE_FILE); virDomainDiskSetType(disk, VIR_STORAGE_TYPE_FILE);

View File

@ -486,7 +486,7 @@ xenParseXM(virConfPtr conf, int xendConfigVersion,
goto skipdisk; goto skipdisk;
head = list->str; head = list->str;
if (VIR_ALLOC(disk) < 0) if (!(disk = virDomainDiskDefNew()))
goto cleanup; goto cleanup;
/* /*
@ -632,7 +632,7 @@ xenParseXM(virConfPtr conf, int xendConfigVersion,
if (xenXMConfigGetString(conf, "cdrom", &str, NULL) < 0) if (xenXMConfigGetString(conf, "cdrom", &str, NULL) < 0)
goto cleanup; goto cleanup;
if (str) { if (str) {
if (VIR_ALLOC(disk) < 0) if (!(disk = virDomainDiskDefNew()))
goto cleanup; goto cleanup;
virDomainDiskSetType(disk, VIR_STORAGE_TYPE_FILE); virDomainDiskSetType(disk, VIR_STORAGE_TYPE_FILE);