2019-03-15 02:19:18 +00:00
|
|
|
/*
|
|
|
|
* virdomainsnapshotobjlist.c: handle a tree of snapshot objects
|
|
|
|
* (derived from snapshot_conf.c)
|
|
|
|
*
|
|
|
|
* Copyright (C) 2006-2019 Red Hat, Inc.
|
|
|
|
* Copyright (C) 2006-2008 Daniel P. Berrange
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library. If not, see
|
|
|
|
* <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include "internal.h"
|
|
|
|
#include "virdomainsnapshotobjlist.h"
|
|
|
|
#include "snapshot_conf.h"
|
|
|
|
#include "virlog.h"
|
|
|
|
#include "virerror.h"
|
|
|
|
#include "datatypes.h"
|
|
|
|
#include "virstring.h"
|
2019-04-01 14:28:05 +00:00
|
|
|
#include "viralloc.h"
|
2019-03-15 02:19:18 +00:00
|
|
|
|
|
|
|
#define VIR_FROM_THIS VIR_FROM_DOMAIN_SNAPSHOT
|
|
|
|
|
|
|
|
VIR_LOG_INIT("conf.virdomainsnapshotobjlist");
|
|
|
|
|
|
|
|
struct _virDomainSnapshotObjList {
|
2019-03-22 05:46:57 +00:00
|
|
|
virDomainMomentObjListPtr base;
|
2019-03-15 02:19:18 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2019-03-22 05:46:57 +00:00
|
|
|
virDomainMomentObjPtr
|
|
|
|
virDomainSnapshotAssignDef(virDomainSnapshotObjListPtr snapshots,
|
|
|
|
virDomainSnapshotDefPtr def)
|
2019-03-15 02:19:18 +00:00
|
|
|
{
|
2019-05-08 22:10:58 +00:00
|
|
|
return virDomainMomentAssignDef(snapshots->base, &def->parent);
|
2019-03-15 02:19:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-20 02:33:23 +00:00
|
|
|
static bool
|
2019-03-22 04:45:25 +00:00
|
|
|
virDomainSnapshotFilter(virDomainMomentObjPtr obj,
|
2019-03-20 02:33:23 +00:00
|
|
|
unsigned int flags)
|
|
|
|
{
|
|
|
|
virDomainSnapshotDefPtr def = virDomainSnapshotObjGetDef(obj);
|
|
|
|
|
|
|
|
/* Caller has already sanitized flags and performed filtering on
|
|
|
|
* DESCENDANTS and LEAVES. */
|
|
|
|
if (flags & VIR_DOMAIN_SNAPSHOT_FILTERS_STATUS) {
|
|
|
|
if (!(flags & VIR_DOMAIN_SNAPSHOT_LIST_INACTIVE) &&
|
|
|
|
def->state == VIR_DOMAIN_SNAPSHOT_SHUTOFF)
|
|
|
|
return false;
|
|
|
|
if (!(flags & VIR_DOMAIN_SNAPSHOT_LIST_DISK_ONLY) &&
|
|
|
|
def->state == VIR_DOMAIN_SNAPSHOT_DISK_SNAPSHOT)
|
|
|
|
return false;
|
|
|
|
if (!(flags & VIR_DOMAIN_SNAPSHOT_LIST_ACTIVE) &&
|
|
|
|
def->state != VIR_DOMAIN_SNAPSHOT_SHUTOFF &&
|
|
|
|
def->state != VIR_DOMAIN_SNAPSHOT_DISK_SNAPSHOT)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((flags & VIR_DOMAIN_SNAPSHOT_LIST_INTERNAL) &&
|
|
|
|
virDomainSnapshotIsExternal(obj))
|
|
|
|
return false;
|
|
|
|
if ((flags & VIR_DOMAIN_SNAPSHOT_LIST_EXTERNAL) &&
|
|
|
|
!virDomainSnapshotIsExternal(obj))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-15 02:19:18 +00:00
|
|
|
virDomainSnapshotObjListPtr
|
|
|
|
virDomainSnapshotObjListNew(void)
|
|
|
|
{
|
|
|
|
virDomainSnapshotObjListPtr snapshots;
|
2019-03-22 05:46:57 +00:00
|
|
|
|
2020-10-07 19:15:50 +00:00
|
|
|
snapshots = g_new0(virDomainSnapshotObjList, 1);
|
2019-03-22 05:46:57 +00:00
|
|
|
snapshots->base = virDomainMomentObjListNew();
|
|
|
|
if (!snapshots->base) {
|
2019-03-15 02:19:18 +00:00
|
|
|
VIR_FREE(snapshots);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return snapshots;
|
|
|
|
}
|
|
|
|
|
2019-03-22 05:46:57 +00:00
|
|
|
|
2019-03-15 02:19:18 +00:00
|
|
|
void
|
|
|
|
virDomainSnapshotObjListFree(virDomainSnapshotObjListPtr snapshots)
|
|
|
|
{
|
|
|
|
if (!snapshots)
|
|
|
|
return;
|
2019-03-22 05:46:57 +00:00
|
|
|
virDomainMomentObjListFree(snapshots->base);
|
2019-03-15 02:19:18 +00:00
|
|
|
VIR_FREE(snapshots);
|
|
|
|
}
|
|
|
|
|
2019-03-20 02:33:23 +00:00
|
|
|
|
2019-03-15 02:19:18 +00:00
|
|
|
int
|
|
|
|
virDomainSnapshotObjListGetNames(virDomainSnapshotObjListPtr snapshots,
|
2019-03-22 04:45:25 +00:00
|
|
|
virDomainMomentObjPtr from,
|
2019-03-20 02:33:23 +00:00
|
|
|
char **const names,
|
|
|
|
int maxnames,
|
2019-03-15 02:19:18 +00:00
|
|
|
unsigned int flags)
|
|
|
|
{
|
2019-03-22 04:17:01 +00:00
|
|
|
/* Convert public flags into common flags */
|
|
|
|
unsigned int moment_flags = 0;
|
|
|
|
struct { int snap_flag; int moment_flag; } map[] = {
|
|
|
|
{ VIR_DOMAIN_SNAPSHOT_LIST_ROOTS,
|
|
|
|
VIR_DOMAIN_MOMENT_LIST_ROOTS, },
|
|
|
|
{ VIR_DOMAIN_SNAPSHOT_LIST_TOPOLOGICAL,
|
|
|
|
VIR_DOMAIN_MOMENT_LIST_TOPOLOGICAL, },
|
|
|
|
{ VIR_DOMAIN_SNAPSHOT_LIST_LEAVES,
|
|
|
|
VIR_DOMAIN_MOMENT_LIST_LEAVES, },
|
|
|
|
{ VIR_DOMAIN_SNAPSHOT_LIST_NO_LEAVES,
|
|
|
|
VIR_DOMAIN_MOMENT_LIST_NO_LEAVES, },
|
|
|
|
{ VIR_DOMAIN_SNAPSHOT_LIST_METADATA,
|
|
|
|
VIR_DOMAIN_MOMENT_LIST_METADATA, },
|
|
|
|
{ VIR_DOMAIN_SNAPSHOT_LIST_NO_METADATA,
|
|
|
|
VIR_DOMAIN_MOMENT_LIST_NO_METADATA, },
|
|
|
|
};
|
|
|
|
size_t i;
|
|
|
|
|
2019-10-15 11:55:26 +00:00
|
|
|
for (i = 0; i < G_N_ELEMENTS(map); i++) {
|
2019-03-22 04:17:01 +00:00
|
|
|
if (flags & map[i].snap_flag) {
|
|
|
|
flags &= ~map[i].snap_flag;
|
|
|
|
moment_flags |= map[i].moment_flag;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-15 02:19:18 +00:00
|
|
|
/* For ease of coding the visitor, it is easier to zero each group
|
|
|
|
* where all of the bits are set. */
|
2019-03-22 05:46:57 +00:00
|
|
|
if ((flags & VIR_DOMAIN_SNAPSHOT_FILTERS_LEAVES) ==
|
2019-03-15 02:19:18 +00:00
|
|
|
VIR_DOMAIN_SNAPSHOT_FILTERS_LEAVES)
|
2019-03-22 05:46:57 +00:00
|
|
|
flags &= ~VIR_DOMAIN_SNAPSHOT_FILTERS_LEAVES;
|
|
|
|
if ((flags & VIR_DOMAIN_SNAPSHOT_FILTERS_STATUS) ==
|
2019-03-15 02:19:18 +00:00
|
|
|
VIR_DOMAIN_SNAPSHOT_FILTERS_STATUS)
|
2019-03-22 05:46:57 +00:00
|
|
|
flags &= ~VIR_DOMAIN_SNAPSHOT_FILTERS_STATUS;
|
|
|
|
if ((flags & VIR_DOMAIN_SNAPSHOT_FILTERS_LOCATION) ==
|
2019-03-15 02:19:18 +00:00
|
|
|
VIR_DOMAIN_SNAPSHOT_FILTERS_LOCATION)
|
2019-03-22 05:46:57 +00:00
|
|
|
flags &= ~VIR_DOMAIN_SNAPSHOT_FILTERS_LOCATION;
|
|
|
|
return virDomainMomentObjListGetNames(snapshots->base, from, names,
|
2019-03-22 04:17:01 +00:00
|
|
|
maxnames, moment_flags,
|
|
|
|
virDomainSnapshotFilter, flags);
|
2019-03-15 02:19:18 +00:00
|
|
|
}
|
|
|
|
|
2019-03-22 05:46:57 +00:00
|
|
|
|
2019-03-15 02:19:18 +00:00
|
|
|
int
|
|
|
|
virDomainSnapshotObjListNum(virDomainSnapshotObjListPtr snapshots,
|
2019-03-22 04:45:25 +00:00
|
|
|
virDomainMomentObjPtr from,
|
2019-03-15 02:19:18 +00:00
|
|
|
unsigned int flags)
|
|
|
|
{
|
|
|
|
return virDomainSnapshotObjListGetNames(snapshots, from, NULL, 0, flags);
|
|
|
|
}
|
|
|
|
|
2019-03-22 05:46:57 +00:00
|
|
|
|
2019-03-22 04:45:25 +00:00
|
|
|
virDomainMomentObjPtr
|
2019-03-15 02:19:18 +00:00
|
|
|
virDomainSnapshotFindByName(virDomainSnapshotObjListPtr snapshots,
|
|
|
|
const char *name)
|
|
|
|
{
|
2019-03-22 05:46:57 +00:00
|
|
|
return virDomainMomentFindByName(snapshots->base, name);
|
2019-03-17 03:38:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-21 20:00:08 +00:00
|
|
|
/* Return the current snapshot, or NULL */
|
2019-03-22 04:45:25 +00:00
|
|
|
virDomainMomentObjPtr
|
2019-03-21 20:00:08 +00:00
|
|
|
virDomainSnapshotGetCurrent(virDomainSnapshotObjListPtr snapshots)
|
|
|
|
{
|
2019-03-22 05:46:57 +00:00
|
|
|
return virDomainMomentGetCurrent(snapshots->base);
|
2019-03-21 20:00:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Return the current snapshot's name, or NULL */
|
|
|
|
const char *
|
|
|
|
virDomainSnapshotGetCurrentName(virDomainSnapshotObjListPtr snapshots)
|
|
|
|
{
|
2019-03-22 05:46:57 +00:00
|
|
|
return virDomainMomentGetCurrentName(snapshots->base);
|
2019-03-21 20:00:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Update the current snapshot, using NULL if no current remains */
|
|
|
|
void
|
|
|
|
virDomainSnapshotSetCurrent(virDomainSnapshotObjListPtr snapshots,
|
2019-03-22 04:45:25 +00:00
|
|
|
virDomainMomentObjPtr snapshot)
|
2019-03-21 20:00:08 +00:00
|
|
|
{
|
2019-03-22 05:46:57 +00:00
|
|
|
virDomainMomentSetCurrent(snapshots->base, snapshot);
|
2019-03-21 20:00:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Remove snapshot from the list; return true if it was current */
|
|
|
|
bool
|
|
|
|
virDomainSnapshotObjListRemove(virDomainSnapshotObjListPtr snapshots,
|
2019-03-22 04:45:25 +00:00
|
|
|
virDomainMomentObjPtr snapshot)
|
2019-03-21 20:00:08 +00:00
|
|
|
{
|
2019-03-22 05:46:57 +00:00
|
|
|
return virDomainMomentObjListRemove(snapshots->base, snapshot);
|
2019-03-15 02:19:18 +00:00
|
|
|
}
|
|
|
|
|
2019-03-22 05:46:57 +00:00
|
|
|
|
2019-03-17 03:38:33 +00:00
|
|
|
/* Remove all snapshots tracked in the list */
|
|
|
|
void
|
|
|
|
virDomainSnapshotObjListRemoveAll(virDomainSnapshotObjListPtr snapshots)
|
|
|
|
{
|
2019-03-22 05:46:57 +00:00
|
|
|
return virDomainMomentObjListRemoveAll(snapshots->base);
|
2019-03-17 03:38:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-15 02:19:18 +00:00
|
|
|
int
|
|
|
|
virDomainSnapshotForEach(virDomainSnapshotObjListPtr snapshots,
|
|
|
|
virHashIterator iter,
|
|
|
|
void *data)
|
|
|
|
{
|
2019-03-22 05:46:57 +00:00
|
|
|
return virDomainMomentForEach(snapshots->base, iter, data);
|
2019-03-15 02:19:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-07-24 03:26:05 +00:00
|
|
|
/* Populate parent link of a given snapshot. */
|
|
|
|
void
|
|
|
|
virDomainSnapshotLinkParent(virDomainSnapshotObjListPtr snapshots,
|
|
|
|
virDomainMomentObjPtr snap)
|
|
|
|
{
|
|
|
|
return virDomainMomentLinkParent(snapshots->base, snap);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-15 02:19:18 +00:00
|
|
|
/* Populate parent link and child count of all snapshots, with all
|
|
|
|
* assigned defs having relations starting as 0/NULL. Return 0 on
|
|
|
|
* success, -1 if a parent is missing or if a circular relationship
|
|
|
|
* was requested. */
|
|
|
|
int
|
|
|
|
virDomainSnapshotUpdateRelations(virDomainSnapshotObjListPtr snapshots)
|
|
|
|
{
|
2019-03-22 05:46:57 +00:00
|
|
|
return virDomainMomentUpdateRelations(snapshots->base);
|
2019-03-15 02:19:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-07-06 02:07:43 +00:00
|
|
|
int
|
|
|
|
virDomainSnapshotCheckCycles(virDomainSnapshotObjListPtr snapshots,
|
|
|
|
virDomainSnapshotDefPtr def,
|
|
|
|
const char *domname)
|
|
|
|
{
|
|
|
|
return virDomainMomentCheckCycles(snapshots->base, &def->parent, domname);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-15 02:19:18 +00:00
|
|
|
int
|
|
|
|
virDomainListSnapshots(virDomainSnapshotObjListPtr snapshots,
|
2019-03-22 04:45:25 +00:00
|
|
|
virDomainMomentObjPtr from,
|
2019-03-15 02:19:18 +00:00
|
|
|
virDomainPtr dom,
|
|
|
|
virDomainSnapshotPtr **snaps,
|
|
|
|
unsigned int flags)
|
|
|
|
{
|
|
|
|
int count = virDomainSnapshotObjListNum(snapshots, from, flags);
|
|
|
|
virDomainSnapshotPtr *list = NULL;
|
|
|
|
char **names;
|
|
|
|
int ret = -1;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
if (!snaps || count < 0)
|
|
|
|
return count;
|
2020-10-07 19:15:50 +00:00
|
|
|
names = g_new0(char *, count);
|
|
|
|
list = g_new0(virDomainSnapshotPtr, count + 1);
|
2019-03-15 02:19:18 +00:00
|
|
|
|
|
|
|
if (virDomainSnapshotObjListGetNames(snapshots, from, names, count,
|
|
|
|
flags) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
for (i = 0; i < count; i++)
|
|
|
|
if ((list[i] = virGetDomainSnapshot(dom, names[i])) == NULL)
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
ret = count;
|
|
|
|
*snaps = list;
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
for (i = 0; i < count; i++)
|
|
|
|
VIR_FREE(names[i]);
|
|
|
|
VIR_FREE(names);
|
|
|
|
if (ret < 0 && list) {
|
|
|
|
for (i = 0; i < count; i++)
|
|
|
|
virObjectUnref(list[i]);
|
|
|
|
VIR_FREE(list);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|