We were failing to react to allocation failure when initializing
a snapshot object list. Changing things to store a pointer
instead of a complete object adds one more possible point of
allocation failure, but at the same time, will make it easier to
react to failure now, as well as making it easier for a future
patch to split all virDomainSnapshotPtr handling into a separate
file, as I continue to add even more snapshot code.
Luckily, there was only one client outside of domain_conf.c that
was actually peeking inside the object, and a new wrapper function
was easy.
* src/conf/domain_conf.h (_virDomainObj): Use a pointer.
(virDomainSnapshotObjListInit): Rename.
(virDomainSnapshotObjListFree, virDomainSnapshotForEach): New
declarations.
(_virDomainSnapshotObjList): Move definitions...
* src/conf/domain_conf.c: ...here.
(virDomainSnapshotObjListInit, virDomainSnapshotObjListDeinit):
Rename...
(virDomainSnapshotObjListNew, virDomainSnapshotObjListFree): ...to
these.
(virDomainSnapshotForEach): New function.
(virDomainObjDispose, virDomainListPopulate): Adjust callers.
* src/qemu/qemu_domain.c (qemuDomainSnapshotDiscard)
(qemuDomainSnapshotDiscardAllMetadata): Likewise.
* src/qemu/qemu_migration.c (qemuMigrationIsAllowed): Likewise.
* src/qemu/qemu_driver.c (qemuDomainSnapshotLoad)
(qemuDomainUndefineFlags, qemuDomainSnapshotCreateXML)
(qemuDomainSnapshotListNames, qemuDomainSnapshotNum)
(qemuDomainListAllSnapshots)
(qemuDomainSnapshotListChildrenNames)
(qemuDomainSnapshotNumChildren)
(qemuDomainSnapshotListAllChildren)
(qemuDomainSnapshotLookupByName, qemuDomainSnapshotGetParent)
(qemuDomainSnapshotGetXMLDesc, qemuDomainSnapshotIsCurrent)
(qemuDomainSnapshotHasMetadata, qemuDomainRevertToSnapshot)
(qemuDomainSnapshotDelete): Likewise.
* src/libvirt_private.syms (domain_conf.h): Export new function.
---
src/conf/domain_conf.c | 72 ++++++++++++++++++++++++++++++++---------------
src/conf/domain_conf.h | 14 ++++-----
src/libvirt_private.syms | 1 +
src/qemu/qemu_domain.c | 7 +++--
src/qemu/qemu_driver.c | 50 ++++++++++++++++----------------
src/qemu/qemu_migration.c | 2 +-
6 files changed, 87 insertions(+), 59 deletions(-)
diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index 851284a..0c6671c 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -655,6 +655,15 @@ VIR_ENUM_IMPL(virDomainNumatuneMemPlacementMode,
#define VIR_DOMAIN_XML_WRITE_FLAGS VIR_DOMAIN_XML_SECURE
#define VIR_DOMAIN_XML_READ_FLAGS VIR_DOMAIN_XML_INACTIVE
+struct _virDomainSnapshotObjList {
+ /* name string -> virDomainSnapshotObj mapping
+ * for O(1), lockless lookup-by-name */
+ virHashTable *objs;
+
+ virDomainSnapshotObj metaroot; /* Special parent of all root snapshots */
+};
+
+
static virClassPtr virDomainObjClass;
static void virDomainObjDispose(void *obj);
@@ -1644,8 +1653,6 @@ void virDomainDefFree(virDomainDefPtr def)
VIR_FREE(def);
}
-static void virDomainSnapshotObjListDeinit(virDomainSnapshotObjListPtr snapshots);
-
static void virDomainObjDispose(void *obj)
{
virDomainObjPtr dom = obj;
@@ -1659,7 +1666,7 @@ static void virDomainObjDispose(void *obj)
virMutexDestroy(&dom->lock);
- virDomainSnapshotObjListDeinit(&dom->snapshots);
+ virDomainSnapshotObjListFree(dom->snapshots);
}
@@ -1673,31 +1680,33 @@ virDomainObjPtr virDomainObjNew(virCapsPtr caps)
if (!(domain = virObjectNew(virDomainObjClass)))
return NULL;
- if (caps->privateDataAllocFunc &&
- !(domain->privateData = (caps->privateDataAllocFunc)())) {
- virReportOOMError();
- VIR_FREE(domain);
- return NULL;
- }
- domain->privateDataFreeFunc = caps->privateDataFreeFunc;
-
if (virMutexInit(&domain->lock) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
"%s", _("cannot initialize mutex"));
- if (domain->privateDataFreeFunc)
- (domain->privateDataFreeFunc)(domain->privateData);
VIR_FREE(domain);
return NULL;
}
+ if (caps->privateDataAllocFunc &&
+ !(domain->privateData = (caps->privateDataAllocFunc)())) {
+ virReportOOMError();
+ goto error;
+ }
+ domain->privateDataFreeFunc = caps->privateDataFreeFunc;
+
+ if (!(domain->snapshots = virDomainSnapshotObjListNew()))
+ goto error;
+
virDomainObjLock(domain);
virDomainObjSetState(domain, VIR_DOMAIN_SHUTOFF,
VIR_DOMAIN_SHUTOFF_UNKNOWN);
- virDomainSnapshotObjListInit(&domain->snapshots);
-
VIR_DEBUG("obj=%p", domain);
return domain;
+
+error:
+ virObjectUnref(domain);
+ return NULL;
}
void virDomainObjAssignDef(virDomainObjPtr domain,
@@ -14332,18 +14341,29 @@ virDomainSnapshotObjListDataFree(void *payload,
virDomainSnapshotObjFree(obj);
}
-int virDomainSnapshotObjListInit(virDomainSnapshotObjListPtr snapshots)
+virDomainSnapshotObjListPtr
+virDomainSnapshotObjListNew(void)
{
+ virDomainSnapshotObjListPtr snapshots;
+ if (VIR_ALLOC(snapshots) < 0) {
+ virReportOOMError();
+ return NULL;
+ }
snapshots->objs = virHashCreate(50, virDomainSnapshotObjListDataFree);
- if (!snapshots->objs)
- return -1;
- return 0;
+ if (!snapshots->objs) {
+ VIR_FREE(snapshots);
+ return NULL;
+ }
+ return snapshots;
}
-static void
-virDomainSnapshotObjListDeinit(virDomainSnapshotObjListPtr snapshots)
+void
+virDomainSnapshotObjListFree(virDomainSnapshotObjListPtr snapshots)
{
+ if (!snapshots)
+ return;
virHashFree(snapshots->objs);
+ VIR_FREE(snapshots);
}
struct virDomainSnapshotNameData {
@@ -14464,6 +14484,14 @@ void virDomainSnapshotObjListRemove(virDomainSnapshotObjListPtr
snapshots,
virHashRemoveEntry(snapshots->objs, snapshot->def->name);
}
+int
+virDomainSnapshotForEach(virDomainSnapshotObjListPtr snapshots,
+ virHashIterator iter,
+ void *data)
+{
+ return virHashForEach(snapshots->objs, iter, data);
+}
+
/* Run iter(data) on all direct children of snapshot, while ignoring all
* other entries in snapshots. Return the number of children
* visited. No particular ordering is guaranteed. */
@@ -15385,7 +15413,7 @@ virDomainListPopulate(void *payload,
/* filter by snapshot existence */
if (MATCH(VIR_CONNECT_LIST_DOMAINS_FILTERS_SNAPSHOT)) {
- int nsnap = virDomainSnapshotObjListNum(&vm->snapshots, NULL, 0);
+ int nsnap = virDomainSnapshotObjListNum(vm->snapshots, NULL, 0);
if (!((MATCH(VIR_CONNECT_LIST_DOMAINS_HAS_SNAPSHOT) && nsnap > 0) ||
(MATCH(VIR_CONNECT_LIST_DOMAINS_NO_SNAPSHOT) && nsnap <= 0)))
goto cleanup;
diff --git a/src/conf/domain_conf.h b/src/conf/domain_conf.h
index fd0e89e..78b6bca 100644
--- a/src/conf/domain_conf.h
+++ b/src/conf/domain_conf.h
@@ -1732,13 +1732,9 @@ struct _virDomainSnapshotObj {
typedef struct _virDomainSnapshotObjList virDomainSnapshotObjList;
typedef virDomainSnapshotObjList *virDomainSnapshotObjListPtr;
-struct _virDomainSnapshotObjList {
- /* name string -> virDomainSnapshotObj mapping
- * for O(1), lockless lookup-by-name */
- virHashTable *objs;
- virDomainSnapshotObj metaroot; /* Special parent of all root snapshots */
-};
+virDomainSnapshotObjListPtr virDomainSnapshotObjListNew(void);
+void virDomainSnapshotObjListFree(virDomainSnapshotObjListPtr snapshots);
typedef enum {
VIR_DOMAIN_SNAPSHOT_PARSE_REDEFINE = 1 << 0,
@@ -1761,7 +1757,6 @@ int virDomainSnapshotAlignDisks(virDomainSnapshotDefPtr snapshot,
virDomainSnapshotObjPtr virDomainSnapshotAssignDef(virDomainSnapshotObjListPtr
snapshots,
const virDomainSnapshotDefPtr def);
-int virDomainSnapshotObjListInit(virDomainSnapshotObjListPtr objs);
int virDomainSnapshotObjListGetNames(virDomainSnapshotObjListPtr snapshots,
virDomainSnapshotObjPtr from,
char **const names, int maxnames,
@@ -1773,6 +1768,9 @@ virDomainSnapshotObjPtr virDomainSnapshotFindByName(const
virDomainSnapshotObjLi
const char *name);
void virDomainSnapshotObjListRemove(virDomainSnapshotObjListPtr snapshots,
virDomainSnapshotObjPtr snapshot);
+int virDomainSnapshotForEach(virDomainSnapshotObjListPtr snapshots,
+ virHashIterator iter,
+ void *data);
int virDomainSnapshotForEachChild(virDomainSnapshotObjPtr snapshot,
virHashIterator iter,
void *data);
@@ -1806,7 +1804,7 @@ struct _virDomainObj {
virDomainDefPtr def; /* The current definition */
virDomainDefPtr newDef; /* New definition to activate at shutdown */
- virDomainSnapshotObjList snapshots;
+ virDomainSnapshotObjListPtr snapshots;
virDomainSnapshotObjPtr current_snapshot;
bool hasManagedSave;
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 7539edc..bf37cb3 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -470,6 +470,7 @@ virDomainSnapshotDefFree;
virDomainSnapshotDefParseString;
virDomainSnapshotDropParent;
virDomainSnapshotFindByName;
+virDomainSnapshotForEach;
virDomainSnapshotForEachChild;
virDomainSnapshotForEachDescendant;
virDomainSnapshotObjListGetNames;
diff --git a/src/qemu/qemu_domain.c b/src/qemu/qemu_domain.c
index c47890b..0ae30b7 100644
--- a/src/qemu/qemu_domain.c
+++ b/src/qemu/qemu_domain.c
@@ -1750,7 +1750,7 @@ qemuDomainSnapshotDiscard(struct qemud_driver *driver,
if (snap == vm->current_snapshot) {
if (update_current && snap->def->parent) {
- parentsnap = virDomainSnapshotFindByName(&vm->snapshots,
+ parentsnap = virDomainSnapshotFindByName(vm->snapshots,
snap->def->parent);
if (!parentsnap) {
VIR_WARN("missing parent snapshot matching name '%s'",
@@ -1771,7 +1771,7 @@ qemuDomainSnapshotDiscard(struct qemud_driver *driver,
if (unlink(snapFile) < 0)
VIR_WARN("Failed to unlink %s", snapFile);
- virDomainSnapshotObjListRemove(&vm->snapshots, snap);
+ virDomainSnapshotObjListRemove(vm->snapshots, snap);
ret = 0;
@@ -1808,7 +1808,8 @@ qemuDomainSnapshotDiscardAllMetadata(struct qemud_driver *driver,
rem.vm = vm;
rem.metadata_only = true;
rem.err = 0;
- virHashForEach(vm->snapshots.objs, qemuDomainSnapshotDiscardAll, &rem);
+ virDomainSnapshotForEach(vm->snapshots, qemuDomainSnapshotDiscardAll,
+ &rem);
return rem.err;
}
diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index bd97008..11e043f 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -412,7 +412,7 @@ static void qemuDomainSnapshotLoad(void *payload,
continue;
}
- snap = virDomainSnapshotAssignDef(&vm->snapshots, def);
+ snap = virDomainSnapshotAssignDef(vm->snapshots, def);
if (snap == NULL) {
virDomainSnapshotDefFree(def);
} else if (snap->def->current) {
@@ -431,7 +431,7 @@ static void qemuDomainSnapshotLoad(void *payload,
vm->current_snapshot = NULL;
}
- if (virDomainSnapshotUpdateRelations(&vm->snapshots) < 0)
+ if (virDomainSnapshotUpdateRelations(vm->snapshots) < 0)
VIR_ERROR(_("Snapshots have inconsistent relations for domain %s"),
vm->def->name);
@@ -5176,7 +5176,7 @@ qemuDomainUndefineFlags(virDomainPtr dom,
}
if (!virDomainObjIsActive(vm) &&
- (nsnapshots = virDomainSnapshotObjListNum(&vm->snapshots, NULL, 0))) {
+ (nsnapshots = virDomainSnapshotObjListNum(vm->snapshots, NULL, 0))) {
if (!(flags & VIR_DOMAIN_UNDEFINE_SNAPSHOTS_METADATA)) {
virReportError(VIR_ERR_OPERATION_INVALID,
_("cannot delete inactive domain with %d "
@@ -10583,7 +10583,7 @@ qemuDomainSnapshotCreateXML(virDomainPtr domain,
def->name);
goto cleanup;
}
- other = virDomainSnapshotFindByName(&vm->snapshots, def->parent);
+ other = virDomainSnapshotFindByName(vm->snapshots, def->parent);
if (!other) {
virReportError(VIR_ERR_INVALID_ARG,
_("parent %s for snapshot %s not found"),
@@ -10597,7 +10597,7 @@ qemuDomainSnapshotCreateXML(virDomainPtr domain,
other->def->name, def->name);
goto cleanup;
}
- other = virDomainSnapshotFindByName(&vm->snapshots,
+ other = virDomainSnapshotFindByName(vm->snapshots,
other->def->parent);
if (!other) {
VIR_WARN("snapshots are inconsistent for %s",
@@ -10615,7 +10615,7 @@ qemuDomainSnapshotCreateXML(virDomainPtr domain,
def->name, uuidstr);
goto cleanup;
}
- other = virDomainSnapshotFindByName(&vm->snapshots, def->name);
+ other = virDomainSnapshotFindByName(vm->snapshots, def->name);
if (other) {
if ((other->def->state == VIR_DOMAIN_RUNNING ||
other->def->state == VIR_DOMAIN_PAUSED) !=
@@ -10704,7 +10704,7 @@ qemuDomainSnapshotCreateXML(virDomainPtr domain,
if (snap)
snap->def = def;
- else if (!(snap = virDomainSnapshotAssignDef(&vm->snapshots, def)))
+ else if (!(snap = virDomainSnapshotAssignDef(vm->snapshots, def)))
goto cleanup;
def = NULL;
@@ -10761,7 +10761,7 @@ cleanup:
} else {
if (update_current)
vm->current_snapshot = snap;
- other = virDomainSnapshotFindByName(&vm->snapshots,
+ other = virDomainSnapshotFindByName(vm->snapshots,
snap->def->parent);
snap->parent = other;
other->nchildren++;
@@ -10769,7 +10769,7 @@ cleanup:
other->first_child = snap;
}
} else if (snap) {
- virDomainSnapshotObjListRemove(&vm->snapshots, snap);
+ virDomainSnapshotObjListRemove(vm->snapshots, snap);
}
virDomainObjUnlock(vm);
}
@@ -10800,7 +10800,7 @@ static int qemuDomainSnapshotListNames(virDomainPtr domain, char
**names,
goto cleanup;
}
- n = virDomainSnapshotObjListGetNames(&vm->snapshots, NULL, names, nameslen,
+ n = virDomainSnapshotObjListGetNames(vm->snapshots, NULL, names, nameslen,
flags);
cleanup:
@@ -10830,7 +10830,7 @@ static int qemuDomainSnapshotNum(virDomainPtr domain,
goto cleanup;
}
- n = virDomainSnapshotObjListNum(&vm->snapshots, NULL, flags);
+ n = virDomainSnapshotObjListNum(vm->snapshots, NULL, flags);
cleanup:
if (vm)
@@ -10860,7 +10860,7 @@ qemuDomainListAllSnapshots(virDomainPtr domain,
virDomainSnapshotPtr **snaps,
goto cleanup;
}
- n = virDomainListSnapshots(&vm->snapshots, NULL, domain, snaps, flags);
+ n = virDomainListSnapshots(vm->snapshots, NULL, domain, snaps, flags);
cleanup:
if (vm)
@@ -10893,7 +10893,7 @@ qemuDomainSnapshotListChildrenNames(virDomainSnapshotPtr
snapshot,
goto cleanup;
}
- snap = virDomainSnapshotFindByName(&vm->snapshots, snapshot->name);
+ snap = virDomainSnapshotFindByName(vm->snapshots, snapshot->name);
if (!snap) {
virReportError(VIR_ERR_NO_DOMAIN_SNAPSHOT,
_("no domain snapshot with matching name
'%s'"),
@@ -10901,7 +10901,7 @@ qemuDomainSnapshotListChildrenNames(virDomainSnapshotPtr
snapshot,
goto cleanup;
}
- n = virDomainSnapshotObjListGetNames(&vm->snapshots, snap, names, nameslen,
+ n = virDomainSnapshotObjListGetNames(vm->snapshots, snap, names, nameslen,
flags);
cleanup:
@@ -10933,7 +10933,7 @@ qemuDomainSnapshotNumChildren(virDomainSnapshotPtr snapshot,
goto cleanup;
}
- snap = virDomainSnapshotFindByName(&vm->snapshots, snapshot->name);
+ snap = virDomainSnapshotFindByName(vm->snapshots, snapshot->name);
if (!snap) {
virReportError(VIR_ERR_NO_DOMAIN_SNAPSHOT,
_("no domain snapshot with matching name
'%s'"),
@@ -10941,7 +10941,7 @@ qemuDomainSnapshotNumChildren(virDomainSnapshotPtr snapshot,
goto cleanup;
}
- n = virDomainSnapshotObjListNum(&vm->snapshots, snap, flags);
+ n = virDomainSnapshotObjListNum(vm->snapshots, snap, flags);
cleanup:
if (vm)
@@ -10973,7 +10973,7 @@ qemuDomainSnapshotListAllChildren(virDomainSnapshotPtr snapshot,
goto cleanup;
}
- snap = virDomainSnapshotFindByName(&vm->snapshots, snapshot->name);
+ snap = virDomainSnapshotFindByName(vm->snapshots, snapshot->name);
if (!snap) {
virReportError(VIR_ERR_NO_DOMAIN_SNAPSHOT,
_("no domain snapshot with matching name
'%s'"),
@@ -10981,7 +10981,7 @@ qemuDomainSnapshotListAllChildren(virDomainSnapshotPtr snapshot,
goto cleanup;
}
- n = virDomainListSnapshots(&vm->snapshots, snap, snapshot->domain, snaps,
+ n = virDomainListSnapshots(vm->snapshots, snap, snapshot->domain, snaps,
flags);
cleanup:
@@ -11012,7 +11012,7 @@ static virDomainSnapshotPtr
qemuDomainSnapshotLookupByName(virDomainPtr domain,
goto cleanup;
}
- snap = virDomainSnapshotFindByName(&vm->snapshots, name);
+ snap = virDomainSnapshotFindByName(vm->snapshots, name);
if (!snap) {
virReportError(VIR_ERR_NO_DOMAIN_SNAPSHOT,
_("no snapshot with matching name '%s'"),
name);
@@ -11077,7 +11077,7 @@ qemuDomainSnapshotGetParent(virDomainSnapshotPtr snapshot,
goto cleanup;
}
- snap = virDomainSnapshotFindByName(&vm->snapshots, snapshot->name);
+ snap = virDomainSnapshotFindByName(vm->snapshots, snapshot->name);
if (!snap) {
virReportError(VIR_ERR_NO_DOMAIN_SNAPSHOT,
_("no domain snapshot with matching name
'%s'"),
@@ -11155,7 +11155,7 @@ static char *qemuDomainSnapshotGetXMLDesc(virDomainSnapshotPtr
snapshot,
goto cleanup;
}
- snap = virDomainSnapshotFindByName(&vm->snapshots, snapshot->name);
+ snap = virDomainSnapshotFindByName(vm->snapshots, snapshot->name);
if (!snap) {
virReportError(VIR_ERR_NO_DOMAIN_SNAPSHOT,
_("no domain snapshot with matching name
'%s'"),
@@ -11193,7 +11193,7 @@ qemuDomainSnapshotIsCurrent(virDomainSnapshotPtr snapshot,
goto cleanup;
}
- snap = virDomainSnapshotFindByName(&vm->snapshots, snapshot->name);
+ snap = virDomainSnapshotFindByName(vm->snapshots, snapshot->name);
if (!snap) {
virReportError(VIR_ERR_NO_DOMAIN_SNAPSHOT,
_("no domain snapshot with matching name
'%s'"),
@@ -11233,7 +11233,7 @@ qemuDomainSnapshotHasMetadata(virDomainSnapshotPtr snapshot,
goto cleanup;
}
- snap = virDomainSnapshotFindByName(&vm->snapshots, snapshot->name);
+ snap = virDomainSnapshotFindByName(vm->snapshots, snapshot->name);
if (!snap) {
virReportError(VIR_ERR_NO_DOMAIN_SNAPSHOT,
_("no domain snapshot with matching name
'%s'"),
@@ -11306,7 +11306,7 @@ static int qemuDomainRevertToSnapshot(virDomainSnapshotPtr
snapshot,
goto cleanup;
}
- snap = virDomainSnapshotFindByName(&vm->snapshots, snapshot->name);
+ snap = virDomainSnapshotFindByName(vm->snapshots, snapshot->name);
if (!snap) {
virReportError(VIR_ERR_NO_DOMAIN_SNAPSHOT,
_("no domain snapshot with matching name
'%s'"),
@@ -11674,7 +11674,7 @@ static int qemuDomainSnapshotDelete(virDomainSnapshotPtr
snapshot,
goto cleanup;
}
- snap = virDomainSnapshotFindByName(&vm->snapshots, snapshot->name);
+ snap = virDomainSnapshotFindByName(vm->snapshots, snapshot->name);
if (!snap) {
virReportError(VIR_ERR_NO_DOMAIN_SNAPSHOT,
_("no domain snapshot with matching name
'%s'"),
diff --git a/src/qemu/qemu_migration.c b/src/qemu/qemu_migration.c
index f65c81a..1b21ef6 100644
--- a/src/qemu/qemu_migration.c
+++ b/src/qemu/qemu_migration.c
@@ -807,7 +807,7 @@ qemuMigrationIsAllowed(struct qemud_driver *driver, virDomainObjPtr
vm,
"%s", _("domain is marked for auto
destroy"));
return false;
}
- if ((nsnapshots = virDomainSnapshotObjListNum(&vm->snapshots, NULL,
+ if ((nsnapshots = virDomainSnapshotObjListNum(vm->snapshots, NULL,
0))) {
virReportError(VIR_ERR_OPERATION_INVALID,
_("cannot migrate domain with %d snapshots"),
--
1.7.11.2