2013-05-25 07:09:43 +04:00
|
|
|
/*
|
|
|
|
* Block layer snapshot related functions
|
|
|
|
*
|
|
|
|
* Copyright (c) 2003-2008 Fabrice Bellard
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2016-01-18 21:01:42 +03:00
|
|
|
#include "qemu/osdep.h"
|
2013-05-25 07:09:43 +04:00
|
|
|
#include "block/snapshot.h"
|
|
|
|
#include "block/block_int.h"
|
2018-06-14 22:14:28 +03:00
|
|
|
#include "block/qdict.h"
|
include/qemu/osdep.h: Don't include qapi/error.h
Commit 57cb38b included qapi/error.h into qemu/osdep.h to get the
Error typedef. Since then, we've moved to include qemu/osdep.h
everywhere. Its file comment explains: "To avoid getting into
possible circular include dependencies, this file should not include
any other QEMU headers, with the exceptions of config-host.h,
compiler.h, os-posix.h and os-win32.h, all of which are doing a
similar job to this file and are under similar constraints."
qapi/error.h doesn't do a similar job, and it doesn't adhere to
similar constraints: it includes qapi-types.h. That's in excess of
100KiB of crap most .c files don't actually need.
Add the typedef to qemu/typedefs.h, and include that instead of
qapi/error.h. Include qapi/error.h in .c files that need it and don't
get it now. Include qapi-types.h in qom/object.h for uint16List.
Update scripts/clean-includes accordingly. Update it further to match
reality: replace config.h by config-target.h, add sysemu/os-posix.h,
sysemu/os-win32.h. Update the list of includes in the qemu/osdep.h
comment quoted above similarly.
This reduces the number of objects depending on qapi/error.h from "all
of them" to less than a third. Unfortunately, the number depending on
qapi-types.h shrinks only a little. More work is needed for that one.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
[Fix compilation without the spice devel packages. - Paolo]
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2016-03-14 11:01:28 +03:00
|
|
|
#include "qapi/error.h"
|
2018-02-01 14:18:39 +03:00
|
|
|
#include "qapi/qmp/qdict.h"
|
2015-03-17 19:22:46 +03:00
|
|
|
#include "qapi/qmp/qerror.h"
|
2017-04-05 12:19:09 +03:00
|
|
|
#include "qapi/qmp/qstring.h"
|
2018-02-01 14:18:46 +03:00
|
|
|
#include "qemu/option.h"
|
2019-09-17 13:26:23 +03:00
|
|
|
#include "sysemu/block-backend.h"
|
2013-05-25 07:09:43 +04:00
|
|
|
|
2013-12-04 13:10:55 +04:00
|
|
|
QemuOptsList internal_snapshot_opts = {
|
|
|
|
.name = "snapshot",
|
|
|
|
.head = QTAILQ_HEAD_INITIALIZER(internal_snapshot_opts.head),
|
|
|
|
.desc = {
|
|
|
|
{
|
|
|
|
.name = SNAPSHOT_OPT_ID,
|
|
|
|
.type = QEMU_OPT_STRING,
|
|
|
|
.help = "snapshot id"
|
|
|
|
},{
|
|
|
|
.name = SNAPSHOT_OPT_NAME,
|
|
|
|
.type = QEMU_OPT_STRING,
|
|
|
|
.help = "snapshot name"
|
|
|
|
},{
|
|
|
|
/* end of list */
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2013-05-25 07:09:43 +04:00
|
|
|
int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info,
|
|
|
|
const char *name)
|
|
|
|
{
|
|
|
|
QEMUSnapshotInfo *sn_tab, *sn;
|
|
|
|
int nb_sns, i, ret;
|
|
|
|
|
2022-03-03 18:16:07 +03:00
|
|
|
GLOBAL_STATE_CODE();
|
|
|
|
|
2013-05-25 07:09:43 +04:00
|
|
|
ret = -ENOENT;
|
|
|
|
nb_sns = bdrv_snapshot_list(bs, &sn_tab);
|
|
|
|
if (nb_sns < 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
for (i = 0; i < nb_sns; i++) {
|
|
|
|
sn = &sn_tab[i];
|
block/snapshot.c: eliminate use of ID input in snapshot operations
At this moment, QEMU attempts to create/load/delete snapshots
by using either an ID (id_str) or a name. The problem is that the code
isn't consistent of whether the entered argument is an ID or a name,
causing unexpected behaviors.
For example, when creating snapshots via savevm <arg>, what happens is that
"arg" is treated as both name and id_str. In a guest without snapshots, create
a single snapshot via savevm:
(qemu) savevm 0
(qemu) info snapshots
List of snapshots present on all disks:
ID TAG VM SIZE DATE VM CLOCK
-- 0 741M 2018-07-31 13:39:56 00:41:25.313
A snapshot with name "0" is created. ID is hidden from the user, but the
ID is a non-zero integer that starts at "1". Thus, this snapshot has
id_str=1, TAG="0". Creating a second snapshot with arg = 1, the first one
is deleted:
(qemu) savevm 1
(qemu) info snapshots
List of snapshots present on all disks:
ID TAG VM SIZE DATE VM CLOCK
-- 1 741M 2018-07-31 13:42:14 00:41:55.252
What happened?
- when creating the second snapshot, a verification is done inside
bdrv_all_delete_snapshot to delete any existing snapshots that matches an
string argument. Here, the code calls bdrv_all_delete_snapshot("1", ...);
- bdrv_all_delete_snapshot calls bdrv_snapshot_find(..., "1") for each
BlockDriverState of the guest. And this is where things goes tilting:
bdrv_snapshot_find does a search by both id_str and name. It finds
out that there is a snapshot that has id_str = 1, stores a reference
to the snapshot in the sn_info pointer and then returns match found;
- since a match was found, a call to bdrv_snapshot_delete_by_id_or_name() is
made. This function ignores the pointer written by bdrv_snapshot_find. Instead,
it deletes the snapshot using bdrv_snapshot_delete() calling it first with
id_str = 1. If it fails to delete, then it calls it again with name = 1.
- after all that, QEMU creates the new snapshot, that has id_str = 1 and
name = 1. The user is left wondering that happened with the first snapshot
created. Similar bugs can be triggered when using loadvm and delvm.
Before contemplating discarding the use of ID input in these operations,
I've searched the code of what would be the implications. My findings
are:
- the RBD and Sheepdog drivers don't care. Both uses the 'name' field as
key in their logic, making id_str = name when appropriate.
replay-snapshot.c does not make any special use of id_str;
- qcow2 uses id_str as an unique identifier but it is automatically
calculated, not being influenced by user input. Other than that, there are
no distinguish operations made only with id_str;
- in blockdev.c, the delete operation uses a match of both id_str AND
name. Given that id_str is either a copy of 'name' or auto-generated,
we're fine here.
This gives motivation to not consider ID as a valid user input in HMP
commands - sticking with 'name' input only is more consistent. To
accomplish that, the following changes were made in this patch:
- bdrv_snapshot_find() does not match for id_str anymore, only 'name'. The
function is called in save_snapshot(), load_snapshot(), bdrv_all_delete_snapshot()
and bdrv_all_find_snapshot(). This change makes the search function more
predictable and does not change the behavior of any underlying code that uses
these affected functions, which are related to HMP (which is fine) and the
main loop inside vl.c (which doesn't care about it anyways);
- bdrv_all_delete_snapshot() does not call bdrv_snapshot_delete_by_id_or_name
anymore. Instead, it uses the pointer returned by bdrv_snapshot_find to
erase the snapshot with the exact match of id_str an name. This function
is called in save_snapshot and hmp_delvm, thus this change produces the
intended effect;
- documentation changes to reflect the new behavior. I consider this to
be an API fix instead of an API change - the user was already creating
snapshots using 'name', but now he/she will also enjoy a consistent
behavior.
Ideally we would get rid of the id_str field entirely, but this would have
repercussions on existing snapshots. Another day perhaps.
Signed-off-by: Daniel Henrique Barboza <danielhb413@gmail.com>
Acked-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
2018-11-07 16:09:58 +03:00
|
|
|
if (!strcmp(sn->name, name)) {
|
2013-05-25 07:09:43 +04:00
|
|
|
*sn_info = *sn;
|
|
|
|
ret = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
g_free(sn_tab);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-09-11 10:04:32 +04:00
|
|
|
/**
|
|
|
|
* Look up an internal snapshot by @id and @name.
|
|
|
|
* @bs: block device to search
|
|
|
|
* @id: unique snapshot ID, or NULL
|
|
|
|
* @name: snapshot name, or NULL
|
|
|
|
* @sn_info: location to store information on the snapshot found
|
|
|
|
* @errp: location to store error, will be set only for exception
|
|
|
|
*
|
|
|
|
* This function will traverse snapshot list in @bs to search the matching
|
|
|
|
* one, @id and @name are the matching condition:
|
|
|
|
* If both @id and @name are specified, find the first one with id @id and
|
|
|
|
* name @name.
|
|
|
|
* If only @id is specified, find the first one with id @id.
|
|
|
|
* If only @name is specified, find the first one with name @name.
|
|
|
|
* if none is specified, abort().
|
|
|
|
*
|
|
|
|
* Returns: true when a snapshot is found and @sn_info will be filled, false
|
|
|
|
* when error or not found. If all operation succeed but no matching one is
|
|
|
|
* found, @errp will NOT be set.
|
|
|
|
*/
|
|
|
|
bool bdrv_snapshot_find_by_id_and_name(BlockDriverState *bs,
|
|
|
|
const char *id,
|
|
|
|
const char *name,
|
|
|
|
QEMUSnapshotInfo *sn_info,
|
|
|
|
Error **errp)
|
|
|
|
{
|
|
|
|
QEMUSnapshotInfo *sn_tab, *sn;
|
|
|
|
int nb_sns, i;
|
|
|
|
bool ret = false;
|
|
|
|
|
|
|
|
assert(id || name);
|
2022-03-03 18:16:07 +03:00
|
|
|
GLOBAL_STATE_CODE();
|
2013-09-11 10:04:32 +04:00
|
|
|
|
|
|
|
nb_sns = bdrv_snapshot_list(bs, &sn_tab);
|
|
|
|
if (nb_sns < 0) {
|
|
|
|
error_setg_errno(errp, -nb_sns, "Failed to get a snapshot list");
|
|
|
|
return false;
|
|
|
|
} else if (nb_sns == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (id && name) {
|
|
|
|
for (i = 0; i < nb_sns; i++) {
|
|
|
|
sn = &sn_tab[i];
|
|
|
|
if (!strcmp(sn->id_str, id) && !strcmp(sn->name, name)) {
|
|
|
|
*sn_info = *sn;
|
|
|
|
ret = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (id) {
|
|
|
|
for (i = 0; i < nb_sns; i++) {
|
|
|
|
sn = &sn_tab[i];
|
|
|
|
if (!strcmp(sn->id_str, id)) {
|
|
|
|
*sn_info = *sn;
|
|
|
|
ret = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (name) {
|
|
|
|
for (i = 0; i < nb_sns; i++) {
|
|
|
|
sn = &sn_tab[i];
|
|
|
|
if (!strcmp(sn->name, name)) {
|
|
|
|
*sn_info = *sn;
|
|
|
|
ret = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
g_free(sn_tab);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-06-12 19:21:07 +03:00
|
|
|
/**
|
2022-07-26 23:11:33 +03:00
|
|
|
* Return a pointer to child of given BDS to which we can fall
|
2019-06-12 19:21:07 +03:00
|
|
|
* back if the given BDS does not support snapshots.
|
|
|
|
* Return NULL if there is no BDS to (safely) fall back to.
|
|
|
|
*/
|
2023-09-29 17:51:42 +03:00
|
|
|
static BdrvChild * GRAPH_RDLOCK
|
|
|
|
bdrv_snapshot_fallback_child(BlockDriverState *bs)
|
2019-06-12 19:21:07 +03:00
|
|
|
{
|
2022-07-26 23:11:33 +03:00
|
|
|
BdrvChild *fallback = bdrv_primary_child(bs);
|
|
|
|
BdrvChild *child;
|
2019-06-12 19:21:07 +03:00
|
|
|
|
2023-09-29 17:51:42 +03:00
|
|
|
GLOBAL_STATE_CODE();
|
|
|
|
assert_bdrv_graph_readable();
|
|
|
|
|
2022-07-26 23:11:27 +03:00
|
|
|
/* We allow fallback only to primary child */
|
2022-07-26 23:11:33 +03:00
|
|
|
if (!fallback) {
|
2019-06-12 19:21:07 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check that there are no other children that would need to be
|
|
|
|
* snapshotted. If there are, it is not safe to fall back to
|
2022-07-26 23:11:33 +03:00
|
|
|
* fallback.
|
2019-06-12 19:21:07 +03:00
|
|
|
*/
|
|
|
|
QLIST_FOREACH(child, &bs->children, next) {
|
|
|
|
if (child->role & (BDRV_CHILD_DATA | BDRV_CHILD_METADATA |
|
|
|
|
BDRV_CHILD_FILTERED) &&
|
2022-07-26 23:11:33 +03:00
|
|
|
child != fallback)
|
2019-06-12 19:21:07 +03:00
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return fallback;
|
|
|
|
}
|
|
|
|
|
2023-09-29 17:51:42 +03:00
|
|
|
static BlockDriverState * GRAPH_RDLOCK
|
|
|
|
bdrv_snapshot_fallback(BlockDriverState *bs)
|
2019-06-12 19:21:07 +03:00
|
|
|
{
|
2023-09-29 17:51:42 +03:00
|
|
|
GLOBAL_STATE_CODE();
|
2022-07-26 23:11:33 +03:00
|
|
|
return child_bs(bdrv_snapshot_fallback_child(bs));
|
2019-06-12 19:21:07 +03:00
|
|
|
}
|
|
|
|
|
2013-05-25 07:09:43 +04:00
|
|
|
int bdrv_can_snapshot(BlockDriverState *bs)
|
|
|
|
{
|
|
|
|
BlockDriver *drv = bs->drv;
|
2022-03-03 18:16:07 +03:00
|
|
|
GLOBAL_STATE_CODE();
|
2013-05-25 07:09:43 +04:00
|
|
|
if (!drv || !bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!drv->bdrv_snapshot_create) {
|
2019-06-12 19:21:07 +03:00
|
|
|
BlockDriverState *fallback_bs = bdrv_snapshot_fallback(bs);
|
|
|
|
if (fallback_bs) {
|
|
|
|
return bdrv_can_snapshot(fallback_bs);
|
2013-05-25 07:09:43 +04:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int bdrv_snapshot_create(BlockDriverState *bs,
|
|
|
|
QEMUSnapshotInfo *sn_info)
|
|
|
|
{
|
|
|
|
BlockDriver *drv = bs->drv;
|
2019-06-12 19:21:07 +03:00
|
|
|
BlockDriverState *fallback_bs = bdrv_snapshot_fallback(bs);
|
2022-03-03 18:16:07 +03:00
|
|
|
|
|
|
|
GLOBAL_STATE_CODE();
|
|
|
|
|
2013-05-25 07:09:43 +04:00
|
|
|
if (!drv) {
|
|
|
|
return -ENOMEDIUM;
|
|
|
|
}
|
|
|
|
if (drv->bdrv_snapshot_create) {
|
|
|
|
return drv->bdrv_snapshot_create(bs, sn_info);
|
|
|
|
}
|
2019-06-12 19:21:07 +03:00
|
|
|
if (fallback_bs) {
|
|
|
|
return bdrv_snapshot_create(fallback_bs, sn_info);
|
2013-05-25 07:09:43 +04:00
|
|
|
}
|
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
|
|
|
|
|
|
|
int bdrv_snapshot_goto(BlockDriverState *bs,
|
2017-11-20 17:28:41 +03:00
|
|
|
const char *snapshot_id,
|
|
|
|
Error **errp)
|
2013-05-25 07:09:43 +04:00
|
|
|
{
|
|
|
|
BlockDriver *drv = bs->drv;
|
2022-07-26 23:11:33 +03:00
|
|
|
BdrvChild *fallback;
|
2013-05-25 07:09:43 +04:00
|
|
|
int ret, open_ret;
|
|
|
|
|
2022-03-03 18:16:07 +03:00
|
|
|
GLOBAL_STATE_CODE();
|
|
|
|
|
2013-05-25 07:09:43 +04:00
|
|
|
if (!drv) {
|
2017-11-20 17:28:41 +03:00
|
|
|
error_setg(errp, "Block driver is closed");
|
2013-05-25 07:09:43 +04:00
|
|
|
return -ENOMEDIUM;
|
|
|
|
}
|
2017-10-23 12:29:45 +03:00
|
|
|
|
2017-11-20 17:41:31 +03:00
|
|
|
if (!QLIST_EMPTY(&bs->dirty_bitmaps)) {
|
|
|
|
error_setg(errp, "Device has active dirty bitmaps");
|
|
|
|
return -EBUSY;
|
2017-10-23 12:29:45 +03:00
|
|
|
}
|
|
|
|
|
2013-05-25 07:09:43 +04:00
|
|
|
if (drv->bdrv_snapshot_goto) {
|
2017-11-20 17:28:41 +03:00
|
|
|
ret = drv->bdrv_snapshot_goto(bs, snapshot_id);
|
|
|
|
if (ret < 0) {
|
|
|
|
error_setg_errno(errp, -ret, "Failed to load snapshot");
|
|
|
|
}
|
|
|
|
return ret;
|
2013-05-25 07:09:43 +04:00
|
|
|
}
|
|
|
|
|
2023-09-29 17:51:42 +03:00
|
|
|
bdrv_graph_rdlock_main_loop();
|
2022-07-26 23:11:33 +03:00
|
|
|
fallback = bdrv_snapshot_fallback_child(bs);
|
2023-09-29 17:51:42 +03:00
|
|
|
bdrv_graph_rdunlock_main_loop();
|
|
|
|
|
2022-07-26 23:11:33 +03:00
|
|
|
if (fallback) {
|
2019-06-12 19:21:07 +03:00
|
|
|
QDict *options;
|
2017-04-05 12:19:09 +03:00
|
|
|
QDict *file_options;
|
2017-11-20 17:28:41 +03:00
|
|
|
Error *local_err = NULL;
|
2022-07-26 23:11:33 +03:00
|
|
|
BlockDriverState *fallback_bs = fallback->bs;
|
|
|
|
char *subqdict_prefix = g_strdup_printf("%s.", fallback->name);
|
2019-06-12 19:21:07 +03:00
|
|
|
|
|
|
|
options = qdict_clone_shallow(bs->options);
|
2017-04-05 12:19:09 +03:00
|
|
|
|
|
|
|
/* Prevent it from getting deleted when detached from bs */
|
2019-06-12 19:21:07 +03:00
|
|
|
bdrv_ref(fallback_bs);
|
2017-04-05 12:19:09 +03:00
|
|
|
|
2019-06-12 19:21:07 +03:00
|
|
|
qdict_extract_subqdict(options, &file_options, subqdict_prefix);
|
2018-04-19 18:01:43 +03:00
|
|
|
qobject_unref(file_options);
|
2019-06-12 19:21:07 +03:00
|
|
|
g_free(subqdict_prefix);
|
|
|
|
|
2022-07-26 23:11:33 +03:00
|
|
|
/* Force .bdrv_open() below to re-attach fallback_bs on fallback */
|
|
|
|
qdict_put_str(options, fallback->name,
|
2019-06-12 19:21:07 +03:00
|
|
|
bdrv_get_node_name(fallback_bs));
|
2017-04-05 12:19:09 +03:00
|
|
|
|
2021-05-03 12:54:18 +03:00
|
|
|
/* Now close bs, apply the snapshot on fallback_bs, and re-open bs */
|
2018-08-14 15:43:19 +03:00
|
|
|
if (drv->bdrv_close) {
|
|
|
|
drv->bdrv_close(bs);
|
|
|
|
}
|
2017-04-05 12:19:09 +03:00
|
|
|
|
2021-05-03 12:54:18 +03:00
|
|
|
/* .bdrv_open() will re-attach it */
|
2023-09-11 12:46:19 +03:00
|
|
|
bdrv_graph_wrlock(NULL);
|
2022-07-26 23:11:33 +03:00
|
|
|
bdrv_unref_child(bs, fallback);
|
2023-11-15 20:20:10 +03:00
|
|
|
bdrv_graph_wrunlock(NULL);
|
2019-06-12 19:21:07 +03:00
|
|
|
|
|
|
|
ret = bdrv_snapshot_goto(fallback_bs, snapshot_id, errp);
|
2017-11-20 17:28:41 +03:00
|
|
|
open_ret = drv->bdrv_open(bs, options, bs->open_flags, &local_err);
|
2018-04-19 18:01:43 +03:00
|
|
|
qobject_unref(options);
|
2013-05-25 07:09:43 +04:00
|
|
|
if (open_ret < 0) {
|
2019-06-12 19:21:07 +03:00
|
|
|
bdrv_unref(fallback_bs);
|
2013-05-25 07:09:43 +04:00
|
|
|
bs->drv = NULL;
|
2017-11-20 17:28:41 +03:00
|
|
|
/* A bdrv_snapshot_goto() error takes precedence */
|
|
|
|
error_propagate(errp, local_err);
|
|
|
|
return ret < 0 ? ret : open_ret;
|
2013-05-25 07:09:43 +04:00
|
|
|
}
|
2017-04-05 12:19:09 +03:00
|
|
|
|
2021-05-03 12:54:18 +03:00
|
|
|
/*
|
2022-07-26 23:11:27 +03:00
|
|
|
* fallback was a primary child. It was closed above and set to NULL,
|
|
|
|
* but the .bdrv_open() call has opened it again, because we set the
|
|
|
|
* respective option (with the qdict_put_str() call above).
|
|
|
|
* Assert that .bdrv_open() has attached the right BDS as primary child.
|
2021-05-03 12:54:18 +03:00
|
|
|
*/
|
2023-09-29 17:51:46 +03:00
|
|
|
bdrv_graph_rdlock_main_loop();
|
2022-07-26 23:11:27 +03:00
|
|
|
assert(bdrv_primary_bs(bs) == fallback_bs);
|
2023-09-29 17:51:46 +03:00
|
|
|
bdrv_graph_rdunlock_main_loop();
|
|
|
|
|
2019-06-12 19:21:07 +03:00
|
|
|
bdrv_unref(fallback_bs);
|
2013-05-25 07:09:43 +04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-11-20 17:28:41 +03:00
|
|
|
error_setg(errp, "Block driver does not support snapshots");
|
2013-05-25 07:09:43 +04:00
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
|
|
|
|
snapshot: distinguish id and name in snapshot delete
Snapshot creation actually already distinguish id and name since it take
a structured parameter *sn, but delete can't. Later an accurate delete
is needed in qmp_transaction abort and blockdev-snapshot-delete-sync,
so change its prototype. Also *errp is added to tip error, but return
value is kepted to let caller check what kind of error happens. Existing
caller for it are savevm, delvm and qemu-img, they are not impacted by
introducing a new function bdrv_snapshot_delete_by_id_or_name(), which
check the return value and do the operation again.
Before this patch:
For qcow2, it search id first then name to find the one to delete.
For rbd, it search name.
For sheepdog, it does nothing.
After this patch:
For qcow2, logic is the same by call it twice in caller.
For rbd, it always fails in delete with id, but still search for name
in second try, no change to user.
Some code for *errp is based on Pavel's patch.
Signed-off-by: Wenchao Xia <xiawenc@linux.vnet.ibm.com>
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
2013-09-11 10:04:33 +04:00
|
|
|
/**
|
|
|
|
* Delete an internal snapshot by @snapshot_id and @name.
|
|
|
|
* @bs: block device used in the operation
|
|
|
|
* @snapshot_id: unique snapshot ID, or NULL
|
|
|
|
* @name: snapshot name, or NULL
|
|
|
|
* @errp: location to store error
|
|
|
|
*
|
|
|
|
* If both @snapshot_id and @name are specified, delete the first one with
|
|
|
|
* id @snapshot_id and name @name.
|
|
|
|
* If only @snapshot_id is specified, delete the first one with id
|
|
|
|
* @snapshot_id.
|
|
|
|
* If only @name is specified, delete the first one with name @name.
|
2013-12-04 13:10:54 +04:00
|
|
|
* if none is specified, return -EINVAL.
|
snapshot: distinguish id and name in snapshot delete
Snapshot creation actually already distinguish id and name since it take
a structured parameter *sn, but delete can't. Later an accurate delete
is needed in qmp_transaction abort and blockdev-snapshot-delete-sync,
so change its prototype. Also *errp is added to tip error, but return
value is kepted to let caller check what kind of error happens. Existing
caller for it are savevm, delvm and qemu-img, they are not impacted by
introducing a new function bdrv_snapshot_delete_by_id_or_name(), which
check the return value and do the operation again.
Before this patch:
For qcow2, it search id first then name to find the one to delete.
For rbd, it search name.
For sheepdog, it does nothing.
After this patch:
For qcow2, logic is the same by call it twice in caller.
For rbd, it always fails in delete with id, but still search for name
in second try, no change to user.
Some code for *errp is based on Pavel's patch.
Signed-off-by: Wenchao Xia <xiawenc@linux.vnet.ibm.com>
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
2013-09-11 10:04:33 +04:00
|
|
|
*
|
|
|
|
* Returns: 0 on success, -errno on failure. If @bs is not inserted, return
|
|
|
|
* -ENOMEDIUM. If @snapshot_id and @name are both NULL, return -EINVAL. If @bs
|
|
|
|
* does not support internal snapshot deletion, return -ENOTSUP. If @bs does
|
|
|
|
* not support parameter @snapshot_id or @name, or one of them is not correctly
|
|
|
|
* specified, return -EINVAL. If @bs can't find one matching @id and @name,
|
|
|
|
* return -ENOENT. If @errp != NULL, it will always be filled with error
|
|
|
|
* message on failure.
|
|
|
|
*/
|
|
|
|
int bdrv_snapshot_delete(BlockDriverState *bs,
|
|
|
|
const char *snapshot_id,
|
|
|
|
const char *name,
|
|
|
|
Error **errp)
|
2013-05-25 07:09:43 +04:00
|
|
|
{
|
|
|
|
BlockDriver *drv = bs->drv;
|
2019-06-12 19:21:07 +03:00
|
|
|
BlockDriverState *fallback_bs = bdrv_snapshot_fallback(bs);
|
2015-12-16 21:33:45 +03:00
|
|
|
int ret;
|
|
|
|
|
2022-03-03 18:16:07 +03:00
|
|
|
GLOBAL_STATE_CODE();
|
|
|
|
|
2013-05-25 07:09:43 +04:00
|
|
|
if (!drv) {
|
2015-03-17 13:54:50 +03:00
|
|
|
error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, bdrv_get_device_name(bs));
|
2013-05-25 07:09:43 +04:00
|
|
|
return -ENOMEDIUM;
|
|
|
|
}
|
snapshot: distinguish id and name in snapshot delete
Snapshot creation actually already distinguish id and name since it take
a structured parameter *sn, but delete can't. Later an accurate delete
is needed in qmp_transaction abort and blockdev-snapshot-delete-sync,
so change its prototype. Also *errp is added to tip error, but return
value is kepted to let caller check what kind of error happens. Existing
caller for it are savevm, delvm and qemu-img, they are not impacted by
introducing a new function bdrv_snapshot_delete_by_id_or_name(), which
check the return value and do the operation again.
Before this patch:
For qcow2, it search id first then name to find the one to delete.
For rbd, it search name.
For sheepdog, it does nothing.
After this patch:
For qcow2, logic is the same by call it twice in caller.
For rbd, it always fails in delete with id, but still search for name
in second try, no change to user.
Some code for *errp is based on Pavel's patch.
Signed-off-by: Wenchao Xia <xiawenc@linux.vnet.ibm.com>
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
2013-09-11 10:04:33 +04:00
|
|
|
if (!snapshot_id && !name) {
|
|
|
|
error_setg(errp, "snapshot_id and name are both NULL");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2014-10-21 12:38:01 +04:00
|
|
|
|
|
|
|
/* drain all pending i/o before deleting snapshot */
|
2015-12-16 21:33:45 +03:00
|
|
|
bdrv_drained_begin(bs);
|
2014-10-21 12:38:01 +04:00
|
|
|
|
2013-05-25 07:09:43 +04:00
|
|
|
if (drv->bdrv_snapshot_delete) {
|
2015-12-16 21:33:45 +03:00
|
|
|
ret = drv->bdrv_snapshot_delete(bs, snapshot_id, name, errp);
|
2019-06-12 19:21:07 +03:00
|
|
|
} else if (fallback_bs) {
|
|
|
|
ret = bdrv_snapshot_delete(fallback_bs, snapshot_id, name, errp);
|
2015-12-16 21:33:45 +03:00
|
|
|
} else {
|
|
|
|
error_setg(errp, "Block format '%s' used by device '%s' "
|
|
|
|
"does not support internal snapshot deletion",
|
|
|
|
drv->format_name, bdrv_get_device_name(bs));
|
|
|
|
ret = -ENOTSUP;
|
2013-05-25 07:09:43 +04:00
|
|
|
}
|
2015-12-16 21:33:45 +03:00
|
|
|
|
|
|
|
bdrv_drained_end(bs);
|
|
|
|
return ret;
|
2013-05-25 07:09:43 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int bdrv_snapshot_list(BlockDriverState *bs,
|
|
|
|
QEMUSnapshotInfo **psn_info)
|
|
|
|
{
|
2023-09-29 17:51:42 +03:00
|
|
|
GLOBAL_STATE_CODE();
|
|
|
|
GRAPH_RDLOCK_GUARD_MAINLOOP();
|
|
|
|
|
2013-05-25 07:09:43 +04:00
|
|
|
BlockDriver *drv = bs->drv;
|
2019-06-12 19:21:07 +03:00
|
|
|
BlockDriverState *fallback_bs = bdrv_snapshot_fallback(bs);
|
2022-03-03 18:16:07 +03:00
|
|
|
|
2013-05-25 07:09:43 +04:00
|
|
|
if (!drv) {
|
|
|
|
return -ENOMEDIUM;
|
|
|
|
}
|
|
|
|
if (drv->bdrv_snapshot_list) {
|
|
|
|
return drv->bdrv_snapshot_list(bs, psn_info);
|
|
|
|
}
|
2019-06-12 19:21:07 +03:00
|
|
|
if (fallback_bs) {
|
|
|
|
return bdrv_snapshot_list(fallback_bs, psn_info);
|
2013-05-25 07:09:43 +04:00
|
|
|
}
|
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
|
|
|
|
2013-12-04 13:10:54 +04:00
|
|
|
/**
|
|
|
|
* Temporarily load an internal snapshot by @snapshot_id and @name.
|
|
|
|
* @bs: block device used in the operation
|
|
|
|
* @snapshot_id: unique snapshot ID, or NULL
|
|
|
|
* @name: snapshot name, or NULL
|
|
|
|
* @errp: location to store error
|
|
|
|
*
|
|
|
|
* If both @snapshot_id and @name are specified, load the first one with
|
|
|
|
* id @snapshot_id and name @name.
|
|
|
|
* If only @snapshot_id is specified, load the first one with id
|
|
|
|
* @snapshot_id.
|
|
|
|
* If only @name is specified, load the first one with name @name.
|
|
|
|
* if none is specified, return -EINVAL.
|
|
|
|
*
|
|
|
|
* Returns: 0 on success, -errno on fail. If @bs is not inserted, return
|
|
|
|
* -ENOMEDIUM. If @bs is not readonly, return -EINVAL. If @bs did not support
|
|
|
|
* internal snapshot, return -ENOTSUP. If qemu can't find a matching @id and
|
|
|
|
* @name, return -ENOENT. If @errp != NULL, it will always be filled on
|
|
|
|
* failure.
|
|
|
|
*/
|
2013-05-25 07:09:43 +04:00
|
|
|
int bdrv_snapshot_load_tmp(BlockDriverState *bs,
|
2013-12-04 13:10:54 +04:00
|
|
|
const char *snapshot_id,
|
|
|
|
const char *name,
|
|
|
|
Error **errp)
|
2013-05-25 07:09:43 +04:00
|
|
|
{
|
|
|
|
BlockDriver *drv = bs->drv;
|
2013-12-04 13:10:54 +04:00
|
|
|
|
2022-03-03 18:16:07 +03:00
|
|
|
GLOBAL_STATE_CODE();
|
2023-09-29 17:51:47 +03:00
|
|
|
GRAPH_RDLOCK_GUARD_MAINLOOP();
|
2022-03-03 18:16:07 +03:00
|
|
|
|
2013-05-25 07:09:43 +04:00
|
|
|
if (!drv) {
|
2015-03-17 13:54:50 +03:00
|
|
|
error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, bdrv_get_device_name(bs));
|
2013-05-25 07:09:43 +04:00
|
|
|
return -ENOMEDIUM;
|
|
|
|
}
|
2013-12-04 13:10:54 +04:00
|
|
|
if (!snapshot_id && !name) {
|
|
|
|
error_setg(errp, "snapshot_id and name are both NULL");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2021-05-27 18:40:54 +03:00
|
|
|
if (!bdrv_is_read_only(bs)) {
|
2013-12-04 13:10:54 +04:00
|
|
|
error_setg(errp, "Device is not readonly");
|
2013-05-25 07:09:43 +04:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
if (drv->bdrv_snapshot_load_tmp) {
|
2013-12-04 13:10:54 +04:00
|
|
|
return drv->bdrv_snapshot_load_tmp(bs, snapshot_id, name, errp);
|
2013-05-25 07:09:43 +04:00
|
|
|
}
|
2015-04-08 12:29:19 +03:00
|
|
|
error_setg(errp, "Block format '%s' used by device '%s' "
|
|
|
|
"does not support temporarily loading internal snapshots",
|
|
|
|
drv->format_name, bdrv_get_device_name(bs));
|
2013-05-25 07:09:43 +04:00
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
2013-12-04 13:10:54 +04:00
|
|
|
|
|
|
|
int bdrv_snapshot_load_tmp_by_id_or_name(BlockDriverState *bs,
|
|
|
|
const char *id_or_name,
|
|
|
|
Error **errp)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
Error *local_err = NULL;
|
|
|
|
|
2022-03-03 18:16:07 +03:00
|
|
|
GLOBAL_STATE_CODE();
|
|
|
|
|
2013-12-04 13:10:54 +04:00
|
|
|
ret = bdrv_snapshot_load_tmp(bs, id_or_name, NULL, &local_err);
|
|
|
|
if (ret == -ENOENT || ret == -EINVAL) {
|
|
|
|
error_free(local_err);
|
|
|
|
local_err = NULL;
|
|
|
|
ret = bdrv_snapshot_load_tmp(bs, NULL, id_or_name, &local_err);
|
|
|
|
}
|
|
|
|
|
2016-06-14 00:57:56 +03:00
|
|
|
error_propagate(errp, local_err);
|
2013-12-04 13:10:54 +04:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2015-11-19 09:42:01 +03:00
|
|
|
|
2021-02-04 15:48:26 +03:00
|
|
|
|
2023-09-29 17:51:39 +03:00
|
|
|
static int GRAPH_RDLOCK
|
|
|
|
bdrv_all_get_snapshot_devices(bool has_devices, strList *devices,
|
|
|
|
GList **all_bdrvs, Error **errp)
|
2021-02-04 15:48:26 +03:00
|
|
|
{
|
|
|
|
g_autoptr(GList) bdrvs = NULL;
|
|
|
|
|
|
|
|
if (has_devices) {
|
|
|
|
if (!devices) {
|
|
|
|
error_setg(errp, "At least one device is required for snapshot");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (devices) {
|
|
|
|
BlockDriverState *bs = bdrv_find_node(devices->value);
|
|
|
|
if (!bs) {
|
|
|
|
error_setg(errp, "No block device node '%s'", devices->value);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
bdrvs = g_list_append(bdrvs, bs);
|
|
|
|
devices = devices->next;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
BlockDriverState *bs;
|
|
|
|
BdrvNextIterator it;
|
|
|
|
for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
|
|
|
|
bdrvs = g_list_append(bdrvs, bs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*all_bdrvs = g_steal_pointer(&bdrvs);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-09-29 17:51:39 +03:00
|
|
|
static bool GRAPH_RDLOCK bdrv_all_snapshots_includes_bs(BlockDriverState *bs)
|
2019-09-17 13:26:23 +03:00
|
|
|
{
|
2023-09-29 17:51:42 +03:00
|
|
|
GLOBAL_STATE_CODE();
|
|
|
|
assert_bdrv_graph_readable();
|
|
|
|
|
2019-09-17 13:26:23 +03:00
|
|
|
if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Include all nodes that are either in use by a BlockBackend, or that
|
|
|
|
* aren't attached to any node, but owned by the monitor. */
|
|
|
|
return bdrv_has_blk(bs) || QLIST_EMPTY(&bs->parents);
|
|
|
|
}
|
2015-11-19 09:42:01 +03:00
|
|
|
|
|
|
|
/* Group operations. All block drivers are involved.
|
|
|
|
* These functions will properly handle dataplane (take aio_context_acquire
|
|
|
|
* when appropriate for appropriate block drivers) */
|
|
|
|
|
2021-02-04 15:48:26 +03:00
|
|
|
bool bdrv_all_can_snapshot(bool has_devices, strList *devices,
|
|
|
|
Error **errp)
|
2015-11-19 09:42:01 +03:00
|
|
|
{
|
2021-02-04 15:48:26 +03:00
|
|
|
g_autoptr(GList) bdrvs = NULL;
|
|
|
|
GList *iterbdrvs;
|
2015-11-19 09:42:01 +03:00
|
|
|
|
2022-03-03 18:16:07 +03:00
|
|
|
GLOBAL_STATE_CODE();
|
2023-09-29 17:51:39 +03:00
|
|
|
GRAPH_RDLOCK_GUARD_MAINLOOP();
|
2022-03-03 18:16:07 +03:00
|
|
|
|
2021-02-04 15:48:26 +03:00
|
|
|
if (bdrv_all_get_snapshot_devices(has_devices, devices, &bdrvs, errp) < 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
iterbdrvs = bdrvs;
|
|
|
|
while (iterbdrvs) {
|
|
|
|
BlockDriverState *bs = iterbdrvs->data;
|
2015-11-19 09:42:01 +03:00
|
|
|
AioContext *ctx = bdrv_get_aio_context(bs);
|
2021-02-04 15:48:23 +03:00
|
|
|
bool ok = true;
|
2015-11-19 09:42:01 +03:00
|
|
|
|
|
|
|
aio_context_acquire(ctx);
|
2021-02-04 15:48:26 +03:00
|
|
|
if (devices || bdrv_all_snapshots_includes_bs(bs)) {
|
2015-11-19 09:42:01 +03:00
|
|
|
ok = bdrv_can_snapshot(bs);
|
|
|
|
}
|
|
|
|
aio_context_release(ctx);
|
2016-05-20 19:49:07 +03:00
|
|
|
if (!ok) {
|
2021-02-04 15:48:23 +03:00
|
|
|
error_setg(errp, "Device '%s' is writable but does not support "
|
|
|
|
"snapshots", bdrv_get_device_or_node_name(bs));
|
|
|
|
return false;
|
2016-05-20 19:49:07 +03:00
|
|
|
}
|
2021-02-04 15:48:26 +03:00
|
|
|
|
|
|
|
iterbdrvs = iterbdrvs->next;
|
2015-11-19 09:42:01 +03:00
|
|
|
}
|
|
|
|
|
2021-02-04 15:48:23 +03:00
|
|
|
return true;
|
2015-11-19 09:42:01 +03:00
|
|
|
}
|
2015-11-19 09:42:03 +03:00
|
|
|
|
2021-02-04 15:48:26 +03:00
|
|
|
int bdrv_all_delete_snapshot(const char *name,
|
|
|
|
bool has_devices, strList *devices,
|
|
|
|
Error **errp)
|
2015-11-19 09:42:03 +03:00
|
|
|
{
|
2021-02-04 15:48:26 +03:00
|
|
|
g_autoptr(GList) bdrvs = NULL;
|
|
|
|
GList *iterbdrvs;
|
|
|
|
|
2022-03-03 18:16:07 +03:00
|
|
|
GLOBAL_STATE_CODE();
|
2023-09-29 17:51:39 +03:00
|
|
|
GRAPH_RDLOCK_GUARD_MAINLOOP();
|
2022-03-03 18:16:07 +03:00
|
|
|
|
2021-02-04 15:48:26 +03:00
|
|
|
if (bdrv_all_get_snapshot_devices(has_devices, devices, &bdrvs, errp) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
2015-11-19 09:42:03 +03:00
|
|
|
|
2021-02-04 15:48:26 +03:00
|
|
|
iterbdrvs = bdrvs;
|
|
|
|
while (iterbdrvs) {
|
|
|
|
BlockDriverState *bs = iterbdrvs->data;
|
2015-11-19 09:42:03 +03:00
|
|
|
AioContext *ctx = bdrv_get_aio_context(bs);
|
2021-02-04 15:48:26 +03:00
|
|
|
QEMUSnapshotInfo sn1, *snapshot = &sn1;
|
2021-02-04 15:48:23 +03:00
|
|
|
int ret = 0;
|
2015-11-19 09:42:03 +03:00
|
|
|
|
|
|
|
aio_context_acquire(ctx);
|
2021-02-04 15:48:26 +03:00
|
|
|
if ((devices || bdrv_all_snapshots_includes_bs(bs)) &&
|
2019-09-17 13:26:23 +03:00
|
|
|
bdrv_snapshot_find(bs, snapshot, name) >= 0)
|
|
|
|
{
|
block/snapshot.c: eliminate use of ID input in snapshot operations
At this moment, QEMU attempts to create/load/delete snapshots
by using either an ID (id_str) or a name. The problem is that the code
isn't consistent of whether the entered argument is an ID or a name,
causing unexpected behaviors.
For example, when creating snapshots via savevm <arg>, what happens is that
"arg" is treated as both name and id_str. In a guest without snapshots, create
a single snapshot via savevm:
(qemu) savevm 0
(qemu) info snapshots
List of snapshots present on all disks:
ID TAG VM SIZE DATE VM CLOCK
-- 0 741M 2018-07-31 13:39:56 00:41:25.313
A snapshot with name "0" is created. ID is hidden from the user, but the
ID is a non-zero integer that starts at "1". Thus, this snapshot has
id_str=1, TAG="0". Creating a second snapshot with arg = 1, the first one
is deleted:
(qemu) savevm 1
(qemu) info snapshots
List of snapshots present on all disks:
ID TAG VM SIZE DATE VM CLOCK
-- 1 741M 2018-07-31 13:42:14 00:41:55.252
What happened?
- when creating the second snapshot, a verification is done inside
bdrv_all_delete_snapshot to delete any existing snapshots that matches an
string argument. Here, the code calls bdrv_all_delete_snapshot("1", ...);
- bdrv_all_delete_snapshot calls bdrv_snapshot_find(..., "1") for each
BlockDriverState of the guest. And this is where things goes tilting:
bdrv_snapshot_find does a search by both id_str and name. It finds
out that there is a snapshot that has id_str = 1, stores a reference
to the snapshot in the sn_info pointer and then returns match found;
- since a match was found, a call to bdrv_snapshot_delete_by_id_or_name() is
made. This function ignores the pointer written by bdrv_snapshot_find. Instead,
it deletes the snapshot using bdrv_snapshot_delete() calling it first with
id_str = 1. If it fails to delete, then it calls it again with name = 1.
- after all that, QEMU creates the new snapshot, that has id_str = 1 and
name = 1. The user is left wondering that happened with the first snapshot
created. Similar bugs can be triggered when using loadvm and delvm.
Before contemplating discarding the use of ID input in these operations,
I've searched the code of what would be the implications. My findings
are:
- the RBD and Sheepdog drivers don't care. Both uses the 'name' field as
key in their logic, making id_str = name when appropriate.
replay-snapshot.c does not make any special use of id_str;
- qcow2 uses id_str as an unique identifier but it is automatically
calculated, not being influenced by user input. Other than that, there are
no distinguish operations made only with id_str;
- in blockdev.c, the delete operation uses a match of both id_str AND
name. Given that id_str is either a copy of 'name' or auto-generated,
we're fine here.
This gives motivation to not consider ID as a valid user input in HMP
commands - sticking with 'name' input only is more consistent. To
accomplish that, the following changes were made in this patch:
- bdrv_snapshot_find() does not match for id_str anymore, only 'name'. The
function is called in save_snapshot(), load_snapshot(), bdrv_all_delete_snapshot()
and bdrv_all_find_snapshot(). This change makes the search function more
predictable and does not change the behavior of any underlying code that uses
these affected functions, which are related to HMP (which is fine) and the
main loop inside vl.c (which doesn't care about it anyways);
- bdrv_all_delete_snapshot() does not call bdrv_snapshot_delete_by_id_or_name
anymore. Instead, it uses the pointer returned by bdrv_snapshot_find to
erase the snapshot with the exact match of id_str an name. This function
is called in save_snapshot and hmp_delvm, thus this change produces the
intended effect;
- documentation changes to reflect the new behavior. I consider this to
be an API fix instead of an API change - the user was already creating
snapshots using 'name', but now he/she will also enjoy a consistent
behavior.
Ideally we would get rid of the id_str field entirely, but this would have
repercussions on existing snapshots. Another day perhaps.
Signed-off-by: Daniel Henrique Barboza <danielhb413@gmail.com>
Acked-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
2018-11-07 16:09:58 +03:00
|
|
|
ret = bdrv_snapshot_delete(bs, snapshot->id_str,
|
2019-12-05 20:46:24 +03:00
|
|
|
snapshot->name, errp);
|
2015-11-19 09:42:03 +03:00
|
|
|
}
|
|
|
|
aio_context_release(ctx);
|
2016-05-20 19:49:07 +03:00
|
|
|
if (ret < 0) {
|
2021-02-04 15:48:23 +03:00
|
|
|
error_prepend(errp, "Could not delete snapshot '%s' on '%s': ",
|
|
|
|
name, bdrv_get_device_or_node_name(bs));
|
|
|
|
return -1;
|
2016-05-20 19:49:07 +03:00
|
|
|
}
|
2021-02-04 15:48:26 +03:00
|
|
|
|
|
|
|
iterbdrvs = iterbdrvs->next;
|
2015-11-19 09:42:03 +03:00
|
|
|
}
|
|
|
|
|
2021-02-04 15:48:23 +03:00
|
|
|
return 0;
|
2015-11-19 09:42:03 +03:00
|
|
|
}
|
2015-11-19 09:42:04 +03:00
|
|
|
|
|
|
|
|
2021-02-04 15:48:26 +03:00
|
|
|
int bdrv_all_goto_snapshot(const char *name,
|
|
|
|
bool has_devices, strList *devices,
|
|
|
|
Error **errp)
|
2015-11-19 09:42:04 +03:00
|
|
|
{
|
2021-02-04 15:48:26 +03:00
|
|
|
g_autoptr(GList) bdrvs = NULL;
|
|
|
|
GList *iterbdrvs;
|
2023-09-29 17:51:42 +03:00
|
|
|
int ret;
|
2015-11-19 09:42:04 +03:00
|
|
|
|
2022-03-03 18:16:07 +03:00
|
|
|
GLOBAL_STATE_CODE();
|
|
|
|
|
2023-09-29 17:51:42 +03:00
|
|
|
bdrv_graph_rdlock_main_loop();
|
|
|
|
ret = bdrv_all_get_snapshot_devices(has_devices, devices, &bdrvs, errp);
|
|
|
|
bdrv_graph_rdunlock_main_loop();
|
|
|
|
|
|
|
|
if (ret < 0) {
|
2021-02-04 15:48:26 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
iterbdrvs = bdrvs;
|
|
|
|
while (iterbdrvs) {
|
|
|
|
BlockDriverState *bs = iterbdrvs->data;
|
2015-11-19 09:42:04 +03:00
|
|
|
AioContext *ctx = bdrv_get_aio_context(bs);
|
2023-09-29 17:51:42 +03:00
|
|
|
bool all_snapshots_includes_bs;
|
2015-11-19 09:42:04 +03:00
|
|
|
|
|
|
|
aio_context_acquire(ctx);
|
2023-09-29 17:51:42 +03:00
|
|
|
bdrv_graph_rdlock_main_loop();
|
|
|
|
all_snapshots_includes_bs = bdrv_all_snapshots_includes_bs(bs);
|
|
|
|
bdrv_graph_rdunlock_main_loop();
|
|
|
|
|
2023-10-23 20:50:38 +03:00
|
|
|
ret = (devices || all_snapshots_includes_bs) ?
|
|
|
|
bdrv_snapshot_goto(bs, name, errp) : 0;
|
2015-11-19 09:42:04 +03:00
|
|
|
aio_context_release(ctx);
|
2017-11-20 17:36:48 +03:00
|
|
|
if (ret < 0) {
|
2023-09-29 17:51:47 +03:00
|
|
|
bdrv_graph_rdlock_main_loop();
|
2021-02-04 15:48:23 +03:00
|
|
|
error_prepend(errp, "Could not load snapshot '%s' on '%s': ",
|
|
|
|
name, bdrv_get_device_or_node_name(bs));
|
2023-09-29 17:51:47 +03:00
|
|
|
bdrv_graph_rdunlock_main_loop();
|
2021-02-04 15:48:23 +03:00
|
|
|
return -1;
|
2016-05-20 19:49:07 +03:00
|
|
|
}
|
2021-02-04 15:48:26 +03:00
|
|
|
|
|
|
|
iterbdrvs = iterbdrvs->next;
|
2015-11-19 09:42:04 +03:00
|
|
|
}
|
|
|
|
|
2021-02-04 15:48:23 +03:00
|
|
|
return 0;
|
2015-11-19 09:42:04 +03:00
|
|
|
}
|
2015-11-19 09:42:06 +03:00
|
|
|
|
2021-02-04 15:48:28 +03:00
|
|
|
int bdrv_all_has_snapshot(const char *name,
|
|
|
|
bool has_devices, strList *devices,
|
|
|
|
Error **errp)
|
2015-11-19 09:42:06 +03:00
|
|
|
{
|
2021-02-04 15:48:26 +03:00
|
|
|
g_autoptr(GList) bdrvs = NULL;
|
|
|
|
GList *iterbdrvs;
|
|
|
|
|
2022-03-03 18:16:07 +03:00
|
|
|
GLOBAL_STATE_CODE();
|
2023-09-29 17:51:39 +03:00
|
|
|
GRAPH_RDLOCK_GUARD_MAINLOOP();
|
2022-03-03 18:16:07 +03:00
|
|
|
|
2021-02-04 15:48:26 +03:00
|
|
|
if (bdrv_all_get_snapshot_devices(has_devices, devices, &bdrvs, errp) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
2015-11-19 09:42:06 +03:00
|
|
|
|
2021-02-04 15:48:26 +03:00
|
|
|
iterbdrvs = bdrvs;
|
|
|
|
while (iterbdrvs) {
|
|
|
|
BlockDriverState *bs = iterbdrvs->data;
|
2015-11-19 09:42:06 +03:00
|
|
|
AioContext *ctx = bdrv_get_aio_context(bs);
|
2021-02-04 15:48:26 +03:00
|
|
|
QEMUSnapshotInfo sn;
|
2021-02-04 15:48:23 +03:00
|
|
|
int ret = 0;
|
2015-11-19 09:42:06 +03:00
|
|
|
|
|
|
|
aio_context_acquire(ctx);
|
2021-02-04 15:48:26 +03:00
|
|
|
if (devices || bdrv_all_snapshots_includes_bs(bs)) {
|
2021-02-04 15:48:23 +03:00
|
|
|
ret = bdrv_snapshot_find(bs, &sn, name);
|
2015-11-19 09:42:06 +03:00
|
|
|
}
|
|
|
|
aio_context_release(ctx);
|
2021-02-04 15:48:23 +03:00
|
|
|
if (ret < 0) {
|
2021-02-04 15:48:28 +03:00
|
|
|
if (ret == -ENOENT) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
error_setg_errno(errp, errno,
|
|
|
|
"Could not check snapshot '%s' on '%s'",
|
|
|
|
name, bdrv_get_device_or_node_name(bs));
|
|
|
|
return -1;
|
|
|
|
}
|
2016-05-20 19:49:07 +03:00
|
|
|
}
|
2021-02-04 15:48:26 +03:00
|
|
|
|
|
|
|
iterbdrvs = iterbdrvs->next;
|
2015-11-19 09:42:06 +03:00
|
|
|
}
|
|
|
|
|
2021-02-04 15:48:28 +03:00
|
|
|
return 1;
|
2015-11-19 09:42:06 +03:00
|
|
|
}
|
2015-11-19 09:42:08 +03:00
|
|
|
|
|
|
|
int bdrv_all_create_snapshot(QEMUSnapshotInfo *sn,
|
|
|
|
BlockDriverState *vm_state_bs,
|
|
|
|
uint64_t vm_state_size,
|
2021-02-04 15:48:26 +03:00
|
|
|
bool has_devices, strList *devices,
|
2021-02-04 15:48:23 +03:00
|
|
|
Error **errp)
|
2015-11-19 09:42:08 +03:00
|
|
|
{
|
2021-02-04 15:48:26 +03:00
|
|
|
g_autoptr(GList) bdrvs = NULL;
|
|
|
|
GList *iterbdrvs;
|
2023-09-29 17:51:39 +03:00
|
|
|
|
2022-03-03 18:16:07 +03:00
|
|
|
GLOBAL_STATE_CODE();
|
2023-09-29 17:51:39 +03:00
|
|
|
GRAPH_RDLOCK_GUARD_MAINLOOP();
|
2021-02-04 15:48:26 +03:00
|
|
|
|
|
|
|
if (bdrv_all_get_snapshot_devices(has_devices, devices, &bdrvs, errp) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
2015-11-19 09:42:08 +03:00
|
|
|
|
2021-02-04 15:48:26 +03:00
|
|
|
iterbdrvs = bdrvs;
|
|
|
|
while (iterbdrvs) {
|
|
|
|
BlockDriverState *bs = iterbdrvs->data;
|
2015-11-19 09:42:08 +03:00
|
|
|
AioContext *ctx = bdrv_get_aio_context(bs);
|
2021-02-04 15:48:23 +03:00
|
|
|
int ret = 0;
|
2015-11-19 09:42:08 +03:00
|
|
|
|
|
|
|
aio_context_acquire(ctx);
|
|
|
|
if (bs == vm_state_bs) {
|
|
|
|
sn->vm_state_size = vm_state_size;
|
2021-02-04 15:48:23 +03:00
|
|
|
ret = bdrv_snapshot_create(bs, sn);
|
2021-02-04 15:48:26 +03:00
|
|
|
} else if (devices || bdrv_all_snapshots_includes_bs(bs)) {
|
2015-11-19 09:42:08 +03:00
|
|
|
sn->vm_state_size = 0;
|
2021-02-04 15:48:23 +03:00
|
|
|
ret = bdrv_snapshot_create(bs, sn);
|
2015-11-19 09:42:08 +03:00
|
|
|
}
|
|
|
|
aio_context_release(ctx);
|
2021-02-04 15:48:23 +03:00
|
|
|
if (ret < 0) {
|
|
|
|
error_setg(errp, "Could not create snapshot '%s' on '%s'",
|
|
|
|
sn->name, bdrv_get_device_or_node_name(bs));
|
|
|
|
return -1;
|
2016-05-20 19:49:07 +03:00
|
|
|
}
|
2021-02-04 15:48:26 +03:00
|
|
|
|
|
|
|
iterbdrvs = iterbdrvs->next;
|
2015-11-19 09:42:08 +03:00
|
|
|
}
|
|
|
|
|
2021-02-04 15:48:23 +03:00
|
|
|
return 0;
|
2015-11-19 09:42:08 +03:00
|
|
|
}
|
2015-11-19 09:42:10 +03:00
|
|
|
|
2021-02-04 15:48:27 +03:00
|
|
|
|
|
|
|
BlockDriverState *bdrv_all_find_vmstate_bs(const char *vmstate_bs,
|
|
|
|
bool has_devices, strList *devices,
|
2021-02-04 15:48:26 +03:00
|
|
|
Error **errp)
|
2015-11-19 09:42:10 +03:00
|
|
|
{
|
2021-02-04 15:48:26 +03:00
|
|
|
g_autoptr(GList) bdrvs = NULL;
|
|
|
|
GList *iterbdrvs;
|
2015-11-19 09:42:10 +03:00
|
|
|
|
2022-03-03 18:16:07 +03:00
|
|
|
GLOBAL_STATE_CODE();
|
2023-09-29 17:51:39 +03:00
|
|
|
GRAPH_RDLOCK_GUARD_MAINLOOP();
|
2022-03-03 18:16:07 +03:00
|
|
|
|
2021-02-04 15:48:26 +03:00
|
|
|
if (bdrv_all_get_snapshot_devices(has_devices, devices, &bdrvs, errp) < 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
iterbdrvs = bdrvs;
|
|
|
|
while (iterbdrvs) {
|
|
|
|
BlockDriverState *bs = iterbdrvs->data;
|
2015-11-19 09:42:10 +03:00
|
|
|
AioContext *ctx = bdrv_get_aio_context(bs);
|
2021-02-04 15:48:26 +03:00
|
|
|
bool found = false;
|
2015-11-19 09:42:10 +03:00
|
|
|
|
|
|
|
aio_context_acquire(ctx);
|
2021-02-04 15:48:26 +03:00
|
|
|
found = (devices || bdrv_all_snapshots_includes_bs(bs)) &&
|
|
|
|
bdrv_can_snapshot(bs);
|
2015-11-19 09:42:10 +03:00
|
|
|
aio_context_release(ctx);
|
2016-05-20 19:49:07 +03:00
|
|
|
|
2021-02-04 15:48:27 +03:00
|
|
|
if (vmstate_bs) {
|
|
|
|
if (g_str_equal(vmstate_bs,
|
|
|
|
bdrv_get_node_name(bs))) {
|
|
|
|
if (found) {
|
|
|
|
return bs;
|
|
|
|
} else {
|
|
|
|
error_setg(errp,
|
|
|
|
"vmstate block device '%s' does not support snapshots",
|
|
|
|
vmstate_bs);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (found) {
|
2021-02-04 15:48:26 +03:00
|
|
|
return bs;
|
2016-05-20 19:49:07 +03:00
|
|
|
}
|
2021-02-04 15:48:26 +03:00
|
|
|
|
|
|
|
iterbdrvs = iterbdrvs->next;
|
2015-11-19 09:42:10 +03:00
|
|
|
}
|
2021-02-04 15:48:26 +03:00
|
|
|
|
2021-02-04 15:48:27 +03:00
|
|
|
if (vmstate_bs) {
|
|
|
|
error_setg(errp,
|
|
|
|
"vmstate block device '%s' does not exist", vmstate_bs);
|
|
|
|
} else {
|
|
|
|
error_setg(errp,
|
|
|
|
"no block device can store vmstate for snapshot");
|
|
|
|
}
|
2021-02-04 15:48:26 +03:00
|
|
|
return NULL;
|
2015-11-19 09:42:10 +03:00
|
|
|
}
|