2009-05-28 16:07:06 +02:00
|
|
|
/*
|
|
|
|
* Block driver for the QCOW version 2 format
|
|
|
|
*
|
|
|
|
* Copyright (c) 2004-2006 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 19:01:42 +01:00
|
|
|
#include "qemu/osdep.h"
|
qcow2: Allow resize of images with internal snapshots
We originally refused to allow resize of images with internal
snapshots because the v2 image format did not require the tracking of
snapshot size, making it impossible to safely revert to a snapshot
with a different size than the current view of the image. But the
snapshot size tracking was rectified in v3, and our recent fixes to
qemu-img amend (see 0a85af35) guarantee that we always have a valid
snapshot size. Thus, we no longer need to artificially limit image
resizes, but it does become one more thing that would prevent a
downgrade back to v2. And now that we support different-sized
snapshots, it's also easy to fix reverting to a snapshot to apply the
new size.
Upgrade iotest 61 to cover this (we previously had NO coverage of
refusal to resize while snapshots exist). Note that the amend process
can fail but still have effects: in particular, since we break things
into upgrade, resize, downgrade, a failure during resize does not roll
back changes made during upgrade, nor does failure in downgrade roll
back a resize. But this situation is pre-existing even without this
patch; and without journaling, the best we could do is minimize the
chance of partial failure by collecting all changes prior to doing any
writes - which adds a lot of complexity but could still fail with EIO.
On the other hand, we are careful that even if we have partial
modification but then fail, the image is left viable (that is, we are
careful to sequence things so that after each successful cluster
write, there may be transient leaked clusters but no corrupt
metadata). And complicating the code to make it more transaction-like
is not worth the effort: a user can always request multiple 'qemu-img
amend' changing one thing each, if they need finer-grained control
over detecting the first failure than what they get by letting qemu
decide how to sequence multiple changes.
Signed-off-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Max Reitz <mreitz@redhat.com>
Message-Id: <20200428192648.749066-3-eblake@redhat.com>
Signed-off-by: Max Reitz <mreitz@redhat.com>
2020-04-28 21:26:47 +02:00
|
|
|
#include "sysemu/block-backend.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 09:01:28 +01:00
|
|
|
#include "qapi/error.h"
|
2018-05-03 21:50:20 +02:00
|
|
|
#include "qcow2.h"
|
2016-03-15 17:22:36 +01:00
|
|
|
#include "qemu/bswap.h"
|
2015-03-17 18:29:20 +01:00
|
|
|
#include "qemu/error-report.h"
|
2016-03-20 18:16:19 +01:00
|
|
|
#include "qemu/cutils.h"
|
2022-02-26 19:07:23 +01:00
|
|
|
#include "qemu/memalign.h"
|
2009-05-28 16:07:06 +02:00
|
|
|
|
2019-10-11 17:28:10 +02:00
|
|
|
static void qcow2_free_single_snapshot(BlockDriverState *bs, int i)
|
|
|
|
{
|
|
|
|
BDRVQcow2State *s = bs->opaque;
|
|
|
|
|
|
|
|
assert(i >= 0 && i < s->nb_snapshots);
|
|
|
|
g_free(s->snapshots[i].name);
|
|
|
|
g_free(s->snapshots[i].id_str);
|
|
|
|
g_free(s->snapshots[i].unknown_extra_data);
|
|
|
|
memset(&s->snapshots[i], 0, sizeof(s->snapshots[i]));
|
|
|
|
}
|
|
|
|
|
2009-05-28 16:07:07 +02:00
|
|
|
void qcow2_free_snapshots(BlockDriverState *bs)
|
2009-05-28 16:07:06 +02:00
|
|
|
{
|
2015-09-07 17:12:56 +02:00
|
|
|
BDRVQcow2State *s = bs->opaque;
|
2009-05-28 16:07:06 +02:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for(i = 0; i < s->nb_snapshots; i++) {
|
2019-10-11 17:28:10 +02:00
|
|
|
qcow2_free_single_snapshot(bs, i);
|
2009-05-28 16:07:06 +02:00
|
|
|
}
|
2011-08-21 05:09:37 +02:00
|
|
|
g_free(s->snapshots);
|
2009-05-28 16:07:06 +02:00
|
|
|
s->snapshots = NULL;
|
|
|
|
s->nb_snapshots = 0;
|
|
|
|
}
|
|
|
|
|
2019-10-11 17:28:08 +02:00
|
|
|
/*
|
|
|
|
* If @repair is true, try to repair a broken snapshot table instead
|
|
|
|
* of just returning an error:
|
|
|
|
*
|
2019-10-11 17:28:10 +02:00
|
|
|
* - If the snapshot table was too long, set *nb_clusters_reduced to
|
|
|
|
* the number of snapshots removed off the end.
|
|
|
|
* The caller will update the on-disk nb_snapshots accordingly;
|
|
|
|
* this leaks clusters, but is safe.
|
|
|
|
* (The on-disk information must be updated before
|
|
|
|
* qcow2_check_refcounts(), because that function relies on
|
|
|
|
* s->nb_snapshots to reflect the on-disk value.)
|
|
|
|
*
|
2019-10-11 17:28:08 +02:00
|
|
|
* - If there were snapshots with too much extra metadata, increment
|
|
|
|
* *extra_data_dropped for each.
|
|
|
|
* This requires the caller to eventually rewrite the whole snapshot
|
|
|
|
* table, which requires cluster allocation. Therefore, this should
|
|
|
|
* be done only after qcow2_check_refcounts() made sure the refcount
|
|
|
|
* structures are valid.
|
|
|
|
* (In the meantime, the image is still valid because
|
|
|
|
* qcow2_check_refcounts() does not do anything with snapshots'
|
|
|
|
* extra data.)
|
|
|
|
*/
|
2023-03-09 09:44:55 +01:00
|
|
|
static coroutine_fn GRAPH_RDLOCK
|
|
|
|
int qcow2_do_read_snapshots(BlockDriverState *bs, bool repair,
|
|
|
|
int *nb_clusters_reduced,
|
|
|
|
int *extra_data_dropped,
|
|
|
|
Error **errp)
|
2009-05-28 16:07:06 +02:00
|
|
|
{
|
2015-09-07 17:12:56 +02:00
|
|
|
BDRVQcow2State *s = bs->opaque;
|
2009-05-28 16:07:06 +02:00
|
|
|
QCowSnapshotHeader h;
|
2011-11-16 11:35:54 +01:00
|
|
|
QCowSnapshotExtraData extra;
|
2009-05-28 16:07:06 +02:00
|
|
|
QCowSnapshot *sn;
|
|
|
|
int i, id_str_size, name_size;
|
2019-10-11 17:28:10 +02:00
|
|
|
int64_t offset, pre_sn_offset;
|
2019-10-11 17:28:09 +02:00
|
|
|
uint64_t table_length = 0;
|
2011-11-16 11:43:28 +01:00
|
|
|
int ret;
|
2009-05-28 16:07:06 +02:00
|
|
|
|
|
|
|
if (!s->nb_snapshots) {
|
|
|
|
s->snapshots = NULL;
|
|
|
|
s->snapshots_size = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
offset = s->snapshots_offset;
|
block: Use g_new() & friends where that makes obvious sense
g_new(T, n) is neater than g_malloc(sizeof(T) * n). It's also safer,
for two reasons. One, it catches multiplication overflowing size_t.
Two, it returns T * rather than void *, which lets the compiler catch
more type errors.
Patch created with Coccinelle, with two manual changes on top:
* Add const to bdrv_iterate_format() to keep the types straight
* Convert the allocation in bdrv_drop_intermediate(), which Coccinelle
inexplicably misses
Coccinelle semantic patch:
@@
type T;
@@
-g_malloc(sizeof(T))
+g_new(T, 1)
@@
type T;
@@
-g_try_malloc(sizeof(T))
+g_try_new(T, 1)
@@
type T;
@@
-g_malloc0(sizeof(T))
+g_new0(T, 1)
@@
type T;
@@
-g_try_malloc0(sizeof(T))
+g_try_new0(T, 1)
@@
type T;
expression n;
@@
-g_malloc(sizeof(T) * (n))
+g_new(T, n)
@@
type T;
expression n;
@@
-g_try_malloc(sizeof(T) * (n))
+g_try_new(T, n)
@@
type T;
expression n;
@@
-g_malloc0(sizeof(T) * (n))
+g_new0(T, n)
@@
type T;
expression n;
@@
-g_try_malloc0(sizeof(T) * (n))
+g_try_new0(T, n)
@@
type T;
expression p, n;
@@
-g_realloc(p, sizeof(T) * (n))
+g_renew(T, p, n)
@@
type T;
expression p, n;
@@
-g_try_realloc(p, sizeof(T) * (n))
+g_try_renew(T, p, n)
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Max Reitz <mreitz@redhat.com>
Reviewed-by: Jeff Cody <jcody@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
2014-08-19 10:31:08 +02:00
|
|
|
s->snapshots = g_new0(QCowSnapshot, s->nb_snapshots);
|
2011-11-16 11:43:28 +01:00
|
|
|
|
2009-05-28 16:07:06 +02:00
|
|
|
for(i = 0; i < s->nb_snapshots; i++) {
|
2019-10-11 17:28:08 +02:00
|
|
|
bool truncate_unknown_extra_data = false;
|
|
|
|
|
2019-10-11 17:28:10 +02:00
|
|
|
pre_sn_offset = offset;
|
2019-10-11 17:28:09 +02:00
|
|
|
table_length = ROUND_UP(table_length, 8);
|
|
|
|
|
2011-11-16 11:43:28 +01:00
|
|
|
/* Read statically sized part of the snapshot header */
|
2018-02-15 14:10:08 +01:00
|
|
|
offset = ROUND_UP(offset, 8);
|
2023-03-09 09:44:55 +01:00
|
|
|
ret = bdrv_co_pread(bs->file, offset, sizeof(h), &h, 0);
|
2011-11-16 11:43:28 +01:00
|
|
|
if (ret < 0) {
|
2019-10-11 17:28:01 +02:00
|
|
|
error_setg_errno(errp, -ret, "Failed to read snapshot table");
|
2009-05-28 16:07:06 +02:00
|
|
|
goto fail;
|
2011-11-16 11:43:28 +01:00
|
|
|
}
|
|
|
|
|
2009-05-28 16:07:06 +02:00
|
|
|
offset += sizeof(h);
|
|
|
|
sn = s->snapshots + i;
|
|
|
|
sn->l1_table_offset = be64_to_cpu(h.l1_table_offset);
|
|
|
|
sn->l1_size = be32_to_cpu(h.l1_size);
|
|
|
|
sn->vm_state_size = be32_to_cpu(h.vm_state_size);
|
|
|
|
sn->date_sec = be32_to_cpu(h.date_sec);
|
|
|
|
sn->date_nsec = be32_to_cpu(h.date_nsec);
|
|
|
|
sn->vm_clock_nsec = be64_to_cpu(h.vm_clock_nsec);
|
2019-10-11 17:28:02 +02:00
|
|
|
sn->extra_data_size = be32_to_cpu(h.extra_data_size);
|
2009-05-28 16:07:06 +02:00
|
|
|
|
|
|
|
id_str_size = be16_to_cpu(h.id_str_size);
|
|
|
|
name_size = be16_to_cpu(h.name_size);
|
|
|
|
|
2019-10-11 17:28:02 +02:00
|
|
|
if (sn->extra_data_size > QCOW_MAX_SNAPSHOT_EXTRA_DATA) {
|
2019-10-11 17:28:08 +02:00
|
|
|
if (!repair) {
|
|
|
|
ret = -EFBIG;
|
|
|
|
error_setg(errp, "Too much extra metadata in snapshot table "
|
|
|
|
"entry %i", i);
|
|
|
|
error_append_hint(errp, "You can force-remove this extra "
|
|
|
|
"metadata with qemu-img check -r all\n");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(stderr, "Discarding too much extra metadata in snapshot "
|
|
|
|
"table entry %i (%" PRIu32 " > %u)\n",
|
|
|
|
i, sn->extra_data_size, QCOW_MAX_SNAPSHOT_EXTRA_DATA);
|
|
|
|
|
|
|
|
(*extra_data_dropped)++;
|
|
|
|
truncate_unknown_extra_data = true;
|
2019-10-11 17:28:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Read known extra data */
|
2023-03-09 09:44:55 +01:00
|
|
|
ret = bdrv_co_pread(bs->file, offset,
|
|
|
|
MIN(sizeof(extra), sn->extra_data_size), &extra, 0);
|
2011-11-16 11:35:54 +01:00
|
|
|
if (ret < 0) {
|
2019-10-11 17:28:01 +02:00
|
|
|
error_setg_errno(errp, -ret, "Failed to read snapshot table");
|
2011-11-16 11:35:54 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
2019-10-11 17:28:02 +02:00
|
|
|
offset += MIN(sizeof(extra), sn->extra_data_size);
|
2009-05-28 16:07:06 +02:00
|
|
|
|
2019-10-11 17:28:02 +02:00
|
|
|
if (sn->extra_data_size >= endof(QCowSnapshotExtraData,
|
|
|
|
vm_state_size_large)) {
|
2011-11-16 11:35:54 +01:00
|
|
|
sn->vm_state_size = be64_to_cpu(extra.vm_state_size_large);
|
|
|
|
}
|
|
|
|
|
2019-10-11 17:28:02 +02:00
|
|
|
if (sn->extra_data_size >= endof(QCowSnapshotExtraData, disk_size)) {
|
2012-04-11 16:33:50 +02:00
|
|
|
sn->disk_size = be64_to_cpu(extra.disk_size);
|
|
|
|
} else {
|
|
|
|
sn->disk_size = bs->total_sectors * BDRV_SECTOR_SIZE;
|
|
|
|
}
|
|
|
|
|
2020-10-03 19:13:02 +02:00
|
|
|
if (sn->extra_data_size >= endof(QCowSnapshotExtraData, icount)) {
|
|
|
|
sn->icount = be64_to_cpu(extra.icount);
|
|
|
|
} else {
|
|
|
|
sn->icount = -1ULL;
|
|
|
|
}
|
|
|
|
|
2019-10-11 17:28:02 +02:00
|
|
|
if (sn->extra_data_size > sizeof(extra)) {
|
2019-10-11 17:28:08 +02:00
|
|
|
uint64_t extra_data_end;
|
|
|
|
size_t unknown_extra_data_size;
|
|
|
|
|
|
|
|
extra_data_end = offset + sn->extra_data_size - sizeof(extra);
|
2019-10-11 17:28:02 +02:00
|
|
|
|
2019-10-11 17:28:08 +02:00
|
|
|
if (truncate_unknown_extra_data) {
|
|
|
|
sn->extra_data_size = QCOW_MAX_SNAPSHOT_EXTRA_DATA;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Store unknown extra data */
|
|
|
|
unknown_extra_data_size = sn->extra_data_size - sizeof(extra);
|
2019-10-11 17:28:02 +02:00
|
|
|
sn->unknown_extra_data = g_malloc(unknown_extra_data_size);
|
2023-03-09 09:44:55 +01:00
|
|
|
ret = bdrv_co_pread(bs->file, offset, unknown_extra_data_size,
|
|
|
|
sn->unknown_extra_data, 0);
|
2019-10-11 17:28:02 +02:00
|
|
|
if (ret < 0) {
|
2019-10-11 17:28:08 +02:00
|
|
|
error_setg_errno(errp, -ret,
|
|
|
|
"Failed to read snapshot table");
|
2019-10-11 17:28:02 +02:00
|
|
|
goto fail;
|
|
|
|
}
|
2019-10-11 17:28:08 +02:00
|
|
|
offset = extra_data_end;
|
2019-10-11 17:28:02 +02:00
|
|
|
}
|
|
|
|
|
2011-11-16 11:43:28 +01:00
|
|
|
/* Read snapshot ID */
|
2011-08-21 05:09:37 +02:00
|
|
|
sn->id_str = g_malloc(id_str_size + 1);
|
2023-03-09 09:44:55 +01:00
|
|
|
ret = bdrv_co_pread(bs->file, offset, id_str_size, sn->id_str, 0);
|
2011-11-16 11:43:28 +01:00
|
|
|
if (ret < 0) {
|
2019-10-11 17:28:01 +02:00
|
|
|
error_setg_errno(errp, -ret, "Failed to read snapshot table");
|
2009-05-28 16:07:06 +02:00
|
|
|
goto fail;
|
2011-11-16 11:43:28 +01:00
|
|
|
}
|
2009-05-28 16:07:06 +02:00
|
|
|
offset += id_str_size;
|
|
|
|
sn->id_str[id_str_size] = '\0';
|
|
|
|
|
2011-11-16 11:43:28 +01:00
|
|
|
/* Read snapshot name */
|
2011-08-21 05:09:37 +02:00
|
|
|
sn->name = g_malloc(name_size + 1);
|
2023-03-09 09:44:55 +01:00
|
|
|
ret = bdrv_co_pread(bs->file, offset, name_size, sn->name, 0);
|
2011-11-16 11:43:28 +01:00
|
|
|
if (ret < 0) {
|
2019-10-11 17:28:01 +02:00
|
|
|
error_setg_errno(errp, -ret, "Failed to read snapshot table");
|
2009-05-28 16:07:06 +02:00
|
|
|
goto fail;
|
2011-11-16 11:43:28 +01:00
|
|
|
}
|
2009-05-28 16:07:06 +02:00
|
|
|
offset += name_size;
|
|
|
|
sn->name[name_size] = '\0';
|
2014-03-26 13:06:07 +01:00
|
|
|
|
2019-10-11 17:28:09 +02:00
|
|
|
/* Note that the extra data may have been truncated */
|
|
|
|
table_length += sizeof(h) + sn->extra_data_size + id_str_size +
|
|
|
|
name_size;
|
|
|
|
if (!repair) {
|
|
|
|
assert(table_length == offset - s->snapshots_offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (table_length > QCOW_MAX_SNAPSHOTS_SIZE ||
|
|
|
|
offset - s->snapshots_offset > INT_MAX)
|
|
|
|
{
|
2019-10-11 17:28:10 +02:00
|
|
|
if (!repair) {
|
|
|
|
ret = -EFBIG;
|
|
|
|
error_setg(errp, "Snapshot table is too big");
|
|
|
|
error_append_hint(errp, "You can force-remove all %u "
|
|
|
|
"overhanging snapshots with qemu-img check "
|
|
|
|
"-r all\n", s->nb_snapshots - i);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(stderr, "Discarding %u overhanging snapshots (snapshot "
|
|
|
|
"table is too big)\n", s->nb_snapshots - i);
|
|
|
|
|
|
|
|
*nb_clusters_reduced += (s->nb_snapshots - i);
|
|
|
|
|
|
|
|
/* Discard current snapshot also */
|
|
|
|
qcow2_free_single_snapshot(bs, i);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This leaks all the rest of the snapshot table and the
|
|
|
|
* snapshots' clusters, but we run in check -r all mode,
|
|
|
|
* so qcow2_check_refcounts() will take care of it.
|
|
|
|
*/
|
|
|
|
s->nb_snapshots = i;
|
|
|
|
offset = pre_sn_offset;
|
|
|
|
break;
|
2014-03-26 13:06:07 +01:00
|
|
|
}
|
2009-05-28 16:07:06 +02:00
|
|
|
}
|
2011-11-16 11:43:28 +01:00
|
|
|
|
2014-03-26 13:06:07 +01:00
|
|
|
assert(offset - s->snapshots_offset <= INT_MAX);
|
2009-05-28 16:07:06 +02:00
|
|
|
s->snapshots_size = offset - s->snapshots_offset;
|
|
|
|
return 0;
|
2011-11-16 11:43:28 +01:00
|
|
|
|
|
|
|
fail:
|
2009-05-28 16:07:07 +02:00
|
|
|
qcow2_free_snapshots(bs);
|
2011-11-16 11:43:28 +01:00
|
|
|
return ret;
|
2009-05-28 16:07:06 +02:00
|
|
|
}
|
|
|
|
|
2023-03-09 09:44:55 +01:00
|
|
|
int coroutine_fn qcow2_read_snapshots(BlockDriverState *bs, Error **errp)
|
2019-10-11 17:28:08 +02:00
|
|
|
{
|
2019-10-11 17:28:10 +02:00
|
|
|
return qcow2_do_read_snapshots(bs, false, NULL, NULL, errp);
|
2019-10-11 17:28:08 +02:00
|
|
|
}
|
|
|
|
|
2009-05-28 16:07:06 +02:00
|
|
|
/* add at the end of the file a new list of snapshots */
|
2019-10-11 17:28:03 +02:00
|
|
|
int qcow2_write_snapshots(BlockDriverState *bs)
|
2009-05-28 16:07:06 +02:00
|
|
|
{
|
2015-09-07 17:12:56 +02:00
|
|
|
BDRVQcow2State *s = bs->opaque;
|
2009-05-28 16:07:06 +02:00
|
|
|
QCowSnapshot *sn;
|
|
|
|
QCowSnapshotHeader h;
|
2011-11-16 11:35:54 +01:00
|
|
|
QCowSnapshotExtraData extra;
|
2009-05-28 16:07:06 +02:00
|
|
|
int i, name_size, id_str_size, snapshots_size;
|
2011-11-18 18:27:00 +01:00
|
|
|
struct {
|
|
|
|
uint32_t nb_snapshots;
|
|
|
|
uint64_t snapshots_offset;
|
|
|
|
} QEMU_PACKED header_data;
|
2014-03-26 13:06:07 +01:00
|
|
|
int64_t offset, snapshots_offset = 0;
|
2011-11-16 12:00:59 +01:00
|
|
|
int ret;
|
2009-05-28 16:07:06 +02:00
|
|
|
|
|
|
|
/* compute the size of the snapshots */
|
|
|
|
offset = 0;
|
|
|
|
for(i = 0; i < s->nb_snapshots; i++) {
|
|
|
|
sn = s->snapshots + i;
|
2018-02-15 14:10:08 +01:00
|
|
|
offset = ROUND_UP(offset, 8);
|
2009-05-28 16:07:06 +02:00
|
|
|
offset += sizeof(h);
|
2019-10-11 17:28:02 +02:00
|
|
|
offset += MAX(sizeof(extra), sn->extra_data_size);
|
2009-05-28 16:07:06 +02:00
|
|
|
offset += strlen(sn->id_str);
|
|
|
|
offset += strlen(sn->name);
|
2014-03-26 13:06:07 +01:00
|
|
|
|
|
|
|
if (offset > QCOW_MAX_SNAPSHOTS_SIZE) {
|
|
|
|
ret = -EFBIG;
|
|
|
|
goto fail;
|
|
|
|
}
|
2009-05-28 16:07:06 +02:00
|
|
|
}
|
2014-03-26 13:06:07 +01:00
|
|
|
|
|
|
|
assert(offset <= INT_MAX);
|
2009-05-28 16:07:06 +02:00
|
|
|
snapshots_size = offset;
|
|
|
|
|
2011-11-16 12:00:59 +01:00
|
|
|
/* Allocate space for the new snapshot list */
|
2009-05-28 16:07:07 +02:00
|
|
|
snapshots_offset = qcow2_alloc_clusters(bs, snapshots_size);
|
2009-05-28 16:07:06 +02:00
|
|
|
offset = snapshots_offset;
|
2010-01-20 15:04:01 +01:00
|
|
|
if (offset < 0) {
|
2013-10-09 10:51:04 +02:00
|
|
|
ret = offset;
|
|
|
|
goto fail;
|
2010-01-20 15:04:01 +01:00
|
|
|
}
|
2013-03-04 15:02:31 +01:00
|
|
|
ret = bdrv_flush(bs);
|
|
|
|
if (ret < 0) {
|
2013-10-09 10:51:04 +02:00
|
|
|
goto fail;
|
2013-03-04 15:02:31 +01:00
|
|
|
}
|
2009-05-28 16:07:06 +02:00
|
|
|
|
2013-08-30 14:34:26 +02:00
|
|
|
/* The snapshot list position has not yet been updated, so these clusters
|
|
|
|
* must indeed be completely free */
|
2019-01-15 20:39:06 +01:00
|
|
|
ret = qcow2_pre_write_overlap_check(bs, 0, offset, snapshots_size, false);
|
2013-08-30 14:34:26 +02:00
|
|
|
if (ret < 0) {
|
2013-10-09 10:51:04 +02:00
|
|
|
goto fail;
|
2013-08-30 14:34:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-16 12:00:59 +01:00
|
|
|
/* Write all snapshots to the new list */
|
2009-05-28 16:07:06 +02:00
|
|
|
for(i = 0; i < s->nb_snapshots; i++) {
|
|
|
|
sn = s->snapshots + i;
|
|
|
|
memset(&h, 0, sizeof(h));
|
|
|
|
h.l1_table_offset = cpu_to_be64(sn->l1_table_offset);
|
|
|
|
h.l1_size = cpu_to_be32(sn->l1_size);
|
2011-11-16 11:35:54 +01:00
|
|
|
/* If it doesn't fit in 32 bit, older implementations should treat it
|
|
|
|
* as a disk-only snapshot rather than truncate the VM state */
|
|
|
|
if (sn->vm_state_size <= 0xffffffff) {
|
|
|
|
h.vm_state_size = cpu_to_be32(sn->vm_state_size);
|
|
|
|
}
|
2009-05-28 16:07:06 +02:00
|
|
|
h.date_sec = cpu_to_be32(sn->date_sec);
|
|
|
|
h.date_nsec = cpu_to_be32(sn->date_nsec);
|
|
|
|
h.vm_clock_nsec = cpu_to_be64(sn->vm_clock_nsec);
|
2019-10-11 17:28:02 +02:00
|
|
|
h.extra_data_size = cpu_to_be32(MAX(sizeof(extra),
|
|
|
|
sn->extra_data_size));
|
2011-11-16 11:35:54 +01:00
|
|
|
|
|
|
|
memset(&extra, 0, sizeof(extra));
|
|
|
|
extra.vm_state_size_large = cpu_to_be64(sn->vm_state_size);
|
2012-04-11 16:33:50 +02:00
|
|
|
extra.disk_size = cpu_to_be64(sn->disk_size);
|
2020-10-03 19:13:02 +02:00
|
|
|
extra.icount = cpu_to_be64(sn->icount);
|
2009-05-28 16:07:06 +02:00
|
|
|
|
|
|
|
id_str_size = strlen(sn->id_str);
|
|
|
|
name_size = strlen(sn->name);
|
2013-10-09 10:51:06 +02:00
|
|
|
assert(id_str_size <= UINT16_MAX && name_size <= UINT16_MAX);
|
2009-05-28 16:07:06 +02:00
|
|
|
h.id_str_size = cpu_to_be16(id_str_size);
|
|
|
|
h.name_size = cpu_to_be16(name_size);
|
2018-02-15 14:10:08 +01:00
|
|
|
offset = ROUND_UP(offset, 8);
|
2011-11-16 12:00:59 +01:00
|
|
|
|
block: Change bdrv_{pread,pwrite,pwrite_sync}() param order
Swap 'buf' and 'bytes' around for consistency with
bdrv_co_{pread,pwrite}(), and in preparation to implement these
functions using generated_co_wrapper.
Callers were updated using this Coccinelle script:
@@ expression child, offset, buf, bytes, flags; @@
- bdrv_pread(child, offset, buf, bytes, flags)
+ bdrv_pread(child, offset, bytes, buf, flags)
@@ expression child, offset, buf, bytes, flags; @@
- bdrv_pwrite(child, offset, buf, bytes, flags)
+ bdrv_pwrite(child, offset, bytes, buf, flags)
@@ expression child, offset, buf, bytes, flags; @@
- bdrv_pwrite_sync(child, offset, buf, bytes, flags)
+ bdrv_pwrite_sync(child, offset, bytes, buf, flags)
Resulting overly-long lines were then fixed by hand.
Signed-off-by: Alberto Faria <afaria@redhat.com>
Reviewed-by: Paolo Bonzini <pbonzini@redhat.com>
Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@yandex-team.ru>
Message-Id: <20220609152744.3891847-3-afaria@redhat.com>
Reviewed-by: Hanna Reitz <hreitz@redhat.com>
Signed-off-by: Hanna Reitz <hreitz@redhat.com>
2022-06-09 17:27:36 +02:00
|
|
|
ret = bdrv_pwrite(bs->file, offset, sizeof(h), &h, 0);
|
2011-11-16 12:00:59 +01:00
|
|
|
if (ret < 0) {
|
2009-05-28 16:07:06 +02:00
|
|
|
goto fail;
|
2011-11-16 12:00:59 +01:00
|
|
|
}
|
2009-05-28 16:07:06 +02:00
|
|
|
offset += sizeof(h);
|
2011-11-16 12:00:59 +01:00
|
|
|
|
block: Change bdrv_{pread,pwrite,pwrite_sync}() param order
Swap 'buf' and 'bytes' around for consistency with
bdrv_co_{pread,pwrite}(), and in preparation to implement these
functions using generated_co_wrapper.
Callers were updated using this Coccinelle script:
@@ expression child, offset, buf, bytes, flags; @@
- bdrv_pread(child, offset, buf, bytes, flags)
+ bdrv_pread(child, offset, bytes, buf, flags)
@@ expression child, offset, buf, bytes, flags; @@
- bdrv_pwrite(child, offset, buf, bytes, flags)
+ bdrv_pwrite(child, offset, bytes, buf, flags)
@@ expression child, offset, buf, bytes, flags; @@
- bdrv_pwrite_sync(child, offset, buf, bytes, flags)
+ bdrv_pwrite_sync(child, offset, bytes, buf, flags)
Resulting overly-long lines were then fixed by hand.
Signed-off-by: Alberto Faria <afaria@redhat.com>
Reviewed-by: Paolo Bonzini <pbonzini@redhat.com>
Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@yandex-team.ru>
Message-Id: <20220609152744.3891847-3-afaria@redhat.com>
Reviewed-by: Hanna Reitz <hreitz@redhat.com>
Signed-off-by: Hanna Reitz <hreitz@redhat.com>
2022-06-09 17:27:36 +02:00
|
|
|
ret = bdrv_pwrite(bs->file, offset, sizeof(extra), &extra, 0);
|
2011-11-16 11:35:54 +01:00
|
|
|
if (ret < 0) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
offset += sizeof(extra);
|
|
|
|
|
2019-10-11 17:28:02 +02:00
|
|
|
if (sn->extra_data_size > sizeof(extra)) {
|
|
|
|
size_t unknown_extra_data_size =
|
|
|
|
sn->extra_data_size - sizeof(extra);
|
|
|
|
|
|
|
|
/* qcow2_read_snapshots() ensures no unbounded allocation */
|
|
|
|
assert(unknown_extra_data_size <= BDRV_REQUEST_MAX_BYTES);
|
|
|
|
assert(sn->unknown_extra_data);
|
|
|
|
|
block: Change bdrv_{pread,pwrite,pwrite_sync}() param order
Swap 'buf' and 'bytes' around for consistency with
bdrv_co_{pread,pwrite}(), and in preparation to implement these
functions using generated_co_wrapper.
Callers were updated using this Coccinelle script:
@@ expression child, offset, buf, bytes, flags; @@
- bdrv_pread(child, offset, buf, bytes, flags)
+ bdrv_pread(child, offset, bytes, buf, flags)
@@ expression child, offset, buf, bytes, flags; @@
- bdrv_pwrite(child, offset, buf, bytes, flags)
+ bdrv_pwrite(child, offset, bytes, buf, flags)
@@ expression child, offset, buf, bytes, flags; @@
- bdrv_pwrite_sync(child, offset, buf, bytes, flags)
+ bdrv_pwrite_sync(child, offset, bytes, buf, flags)
Resulting overly-long lines were then fixed by hand.
Signed-off-by: Alberto Faria <afaria@redhat.com>
Reviewed-by: Paolo Bonzini <pbonzini@redhat.com>
Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@yandex-team.ru>
Message-Id: <20220609152744.3891847-3-afaria@redhat.com>
Reviewed-by: Hanna Reitz <hreitz@redhat.com>
Signed-off-by: Hanna Reitz <hreitz@redhat.com>
2022-06-09 17:27:36 +02:00
|
|
|
ret = bdrv_pwrite(bs->file, offset, unknown_extra_data_size,
|
|
|
|
sn->unknown_extra_data, 0);
|
2019-10-11 17:28:02 +02:00
|
|
|
if (ret < 0) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
offset += unknown_extra_data_size;
|
|
|
|
}
|
|
|
|
|
block: Change bdrv_{pread,pwrite,pwrite_sync}() param order
Swap 'buf' and 'bytes' around for consistency with
bdrv_co_{pread,pwrite}(), and in preparation to implement these
functions using generated_co_wrapper.
Callers were updated using this Coccinelle script:
@@ expression child, offset, buf, bytes, flags; @@
- bdrv_pread(child, offset, buf, bytes, flags)
+ bdrv_pread(child, offset, bytes, buf, flags)
@@ expression child, offset, buf, bytes, flags; @@
- bdrv_pwrite(child, offset, buf, bytes, flags)
+ bdrv_pwrite(child, offset, bytes, buf, flags)
@@ expression child, offset, buf, bytes, flags; @@
- bdrv_pwrite_sync(child, offset, buf, bytes, flags)
+ bdrv_pwrite_sync(child, offset, bytes, buf, flags)
Resulting overly-long lines were then fixed by hand.
Signed-off-by: Alberto Faria <afaria@redhat.com>
Reviewed-by: Paolo Bonzini <pbonzini@redhat.com>
Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@yandex-team.ru>
Message-Id: <20220609152744.3891847-3-afaria@redhat.com>
Reviewed-by: Hanna Reitz <hreitz@redhat.com>
Signed-off-by: Hanna Reitz <hreitz@redhat.com>
2022-06-09 17:27:36 +02:00
|
|
|
ret = bdrv_pwrite(bs->file, offset, id_str_size, sn->id_str, 0);
|
2011-11-16 12:00:59 +01:00
|
|
|
if (ret < 0) {
|
2009-05-28 16:07:06 +02:00
|
|
|
goto fail;
|
2011-11-16 12:00:59 +01:00
|
|
|
}
|
2009-05-28 16:07:06 +02:00
|
|
|
offset += id_str_size;
|
2011-11-16 12:00:59 +01:00
|
|
|
|
block: Change bdrv_{pread,pwrite,pwrite_sync}() param order
Swap 'buf' and 'bytes' around for consistency with
bdrv_co_{pread,pwrite}(), and in preparation to implement these
functions using generated_co_wrapper.
Callers were updated using this Coccinelle script:
@@ expression child, offset, buf, bytes, flags; @@
- bdrv_pread(child, offset, buf, bytes, flags)
+ bdrv_pread(child, offset, bytes, buf, flags)
@@ expression child, offset, buf, bytes, flags; @@
- bdrv_pwrite(child, offset, buf, bytes, flags)
+ bdrv_pwrite(child, offset, bytes, buf, flags)
@@ expression child, offset, buf, bytes, flags; @@
- bdrv_pwrite_sync(child, offset, buf, bytes, flags)
+ bdrv_pwrite_sync(child, offset, bytes, buf, flags)
Resulting overly-long lines were then fixed by hand.
Signed-off-by: Alberto Faria <afaria@redhat.com>
Reviewed-by: Paolo Bonzini <pbonzini@redhat.com>
Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@yandex-team.ru>
Message-Id: <20220609152744.3891847-3-afaria@redhat.com>
Reviewed-by: Hanna Reitz <hreitz@redhat.com>
Signed-off-by: Hanna Reitz <hreitz@redhat.com>
2022-06-09 17:27:36 +02:00
|
|
|
ret = bdrv_pwrite(bs->file, offset, name_size, sn->name, 0);
|
2011-11-16 12:00:59 +01:00
|
|
|
if (ret < 0) {
|
2009-05-28 16:07:06 +02:00
|
|
|
goto fail;
|
2011-11-16 12:00:59 +01:00
|
|
|
}
|
2009-05-28 16:07:06 +02:00
|
|
|
offset += name_size;
|
|
|
|
}
|
|
|
|
|
2011-11-16 12:00:59 +01:00
|
|
|
/*
|
|
|
|
* Update the header to point to the new snapshot table. This requires the
|
|
|
|
* new table and its refcounts to be stable on disk.
|
|
|
|
*/
|
|
|
|
ret = bdrv_flush(bs);
|
|
|
|
if (ret < 0) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2011-11-18 18:27:00 +01:00
|
|
|
QEMU_BUILD_BUG_ON(offsetof(QCowHeader, snapshots_offset) !=
|
2019-10-11 17:28:00 +02:00
|
|
|
endof(QCowHeader, nb_snapshots));
|
2011-11-18 18:27:00 +01:00
|
|
|
|
|
|
|
header_data.nb_snapshots = cpu_to_be32(s->nb_snapshots);
|
|
|
|
header_data.snapshots_offset = cpu_to_be64(snapshots_offset);
|
2011-11-16 12:00:59 +01:00
|
|
|
|
2016-06-20 20:09:15 +02:00
|
|
|
ret = bdrv_pwrite_sync(bs->file, offsetof(QCowHeader, nb_snapshots),
|
block: Change bdrv_{pread,pwrite,pwrite_sync}() param order
Swap 'buf' and 'bytes' around for consistency with
bdrv_co_{pread,pwrite}(), and in preparation to implement these
functions using generated_co_wrapper.
Callers were updated using this Coccinelle script:
@@ expression child, offset, buf, bytes, flags; @@
- bdrv_pread(child, offset, buf, bytes, flags)
+ bdrv_pread(child, offset, bytes, buf, flags)
@@ expression child, offset, buf, bytes, flags; @@
- bdrv_pwrite(child, offset, buf, bytes, flags)
+ bdrv_pwrite(child, offset, bytes, buf, flags)
@@ expression child, offset, buf, bytes, flags; @@
- bdrv_pwrite_sync(child, offset, buf, bytes, flags)
+ bdrv_pwrite_sync(child, offset, bytes, buf, flags)
Resulting overly-long lines were then fixed by hand.
Signed-off-by: Alberto Faria <afaria@redhat.com>
Reviewed-by: Paolo Bonzini <pbonzini@redhat.com>
Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@yandex-team.ru>
Message-Id: <20220609152744.3891847-3-afaria@redhat.com>
Reviewed-by: Hanna Reitz <hreitz@redhat.com>
Signed-off-by: Hanna Reitz <hreitz@redhat.com>
2022-06-09 17:27:36 +02:00
|
|
|
sizeof(header_data), &header_data, 0);
|
2011-11-16 12:00:59 +01:00
|
|
|
if (ret < 0) {
|
2009-05-28 16:07:06 +02:00
|
|
|
goto fail;
|
2011-11-16 12:00:59 +01:00
|
|
|
}
|
2009-05-28 16:07:06 +02:00
|
|
|
|
|
|
|
/* free the old snapshot table */
|
2013-06-19 13:44:18 +02:00
|
|
|
qcow2_free_clusters(bs, s->snapshots_offset, s->snapshots_size,
|
|
|
|
QCOW2_DISCARD_SNAPSHOT);
|
2009-05-28 16:07:06 +02:00
|
|
|
s->snapshots_offset = snapshots_offset;
|
|
|
|
s->snapshots_size = snapshots_size;
|
|
|
|
return 0;
|
2011-11-16 12:00:59 +01:00
|
|
|
|
|
|
|
fail:
|
2013-10-09 10:51:05 +02:00
|
|
|
if (snapshots_offset > 0) {
|
|
|
|
qcow2_free_clusters(bs, snapshots_offset, snapshots_size,
|
|
|
|
QCOW2_DISCARD_ALWAYS);
|
|
|
|
}
|
2011-11-16 12:00:59 +01:00
|
|
|
return ret;
|
2009-05-28 16:07:06 +02:00
|
|
|
}
|
|
|
|
|
2019-10-11 17:28:06 +02:00
|
|
|
int coroutine_fn qcow2_check_read_snapshot_table(BlockDriverState *bs,
|
|
|
|
BdrvCheckResult *result,
|
|
|
|
BdrvCheckMode fix)
|
|
|
|
{
|
|
|
|
BDRVQcow2State *s = bs->opaque;
|
|
|
|
Error *local_err = NULL;
|
2019-10-11 17:28:10 +02:00
|
|
|
int nb_clusters_reduced = 0;
|
2019-10-11 17:28:08 +02:00
|
|
|
int extra_data_dropped = 0;
|
2019-10-11 17:28:06 +02:00
|
|
|
int ret;
|
|
|
|
struct {
|
|
|
|
uint32_t nb_snapshots;
|
|
|
|
uint64_t snapshots_offset;
|
|
|
|
} QEMU_PACKED snapshot_table_pointer;
|
|
|
|
|
|
|
|
/* qcow2_do_open() discards this information in check mode */
|
2022-10-13 14:37:06 +02:00
|
|
|
ret = bdrv_co_pread(bs->file, offsetof(QCowHeader, nb_snapshots),
|
|
|
|
sizeof(snapshot_table_pointer), &snapshot_table_pointer,
|
|
|
|
0);
|
2019-10-11 17:28:06 +02:00
|
|
|
if (ret < 0) {
|
|
|
|
result->check_errors++;
|
|
|
|
fprintf(stderr, "ERROR failed to read the snapshot table pointer from "
|
|
|
|
"the image header: %s\n", strerror(-ret));
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
s->snapshots_offset = be64_to_cpu(snapshot_table_pointer.snapshots_offset);
|
|
|
|
s->nb_snapshots = be32_to_cpu(snapshot_table_pointer.nb_snapshots);
|
|
|
|
|
2019-10-11 17:28:11 +02:00
|
|
|
if (s->nb_snapshots > QCOW_MAX_SNAPSHOTS && (fix & BDRV_FIX_ERRORS)) {
|
|
|
|
fprintf(stderr, "Discarding %u overhanging snapshots\n",
|
|
|
|
s->nb_snapshots - QCOW_MAX_SNAPSHOTS);
|
|
|
|
|
|
|
|
nb_clusters_reduced += s->nb_snapshots - QCOW_MAX_SNAPSHOTS;
|
|
|
|
s->nb_snapshots = QCOW_MAX_SNAPSHOTS;
|
|
|
|
}
|
|
|
|
|
2019-10-11 17:28:06 +02:00
|
|
|
ret = qcow2_validate_table(bs, s->snapshots_offset, s->nb_snapshots,
|
|
|
|
sizeof(QCowSnapshotHeader),
|
|
|
|
sizeof(QCowSnapshotHeader) * QCOW_MAX_SNAPSHOTS,
|
|
|
|
"snapshot table", &local_err);
|
|
|
|
if (ret < 0) {
|
|
|
|
result->check_errors++;
|
|
|
|
error_reportf_err(local_err, "ERROR ");
|
|
|
|
|
2019-10-11 17:28:11 +02:00
|
|
|
if (s->nb_snapshots > QCOW_MAX_SNAPSHOTS) {
|
|
|
|
fprintf(stderr, "You can force-remove all %u overhanging snapshots "
|
|
|
|
"with qemu-img check -r all\n",
|
|
|
|
s->nb_snapshots - QCOW_MAX_SNAPSHOTS);
|
|
|
|
}
|
|
|
|
|
2019-10-11 17:28:06 +02:00
|
|
|
/* We did not read the snapshot table, so invalidate this information */
|
|
|
|
s->snapshots_offset = 0;
|
|
|
|
s->nb_snapshots = 0;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
qemu_co_mutex_unlock(&s->lock);
|
2019-10-11 17:28:08 +02:00
|
|
|
ret = qcow2_do_read_snapshots(bs, fix & BDRV_FIX_ERRORS,
|
2019-10-11 17:28:10 +02:00
|
|
|
&nb_clusters_reduced, &extra_data_dropped,
|
|
|
|
&local_err);
|
2019-10-11 17:28:06 +02:00
|
|
|
qemu_co_mutex_lock(&s->lock);
|
|
|
|
if (ret < 0) {
|
|
|
|
result->check_errors++;
|
|
|
|
error_reportf_err(local_err,
|
|
|
|
"ERROR failed to read the snapshot table: ");
|
|
|
|
|
|
|
|
/* We did not read the snapshot table, so invalidate this information */
|
|
|
|
s->snapshots_offset = 0;
|
|
|
|
s->nb_snapshots = 0;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2019-10-11 17:28:10 +02:00
|
|
|
result->corruptions += nb_clusters_reduced + extra_data_dropped;
|
|
|
|
|
|
|
|
if (nb_clusters_reduced) {
|
|
|
|
/*
|
|
|
|
* Update image header now, because:
|
|
|
|
* (1) qcow2_check_refcounts() relies on s->nb_snapshots to be
|
|
|
|
* the same as what the image header says,
|
|
|
|
* (2) this leaks clusters, but qcow2_check_refcounts() will
|
|
|
|
* fix that.
|
|
|
|
*/
|
|
|
|
assert(fix & BDRV_FIX_ERRORS);
|
|
|
|
|
|
|
|
snapshot_table_pointer.nb_snapshots = cpu_to_be32(s->nb_snapshots);
|
2022-06-09 17:27:43 +02:00
|
|
|
ret = bdrv_co_pwrite_sync(bs->file, offsetof(QCowHeader, nb_snapshots),
|
|
|
|
sizeof(snapshot_table_pointer.nb_snapshots),
|
|
|
|
&snapshot_table_pointer.nb_snapshots, 0);
|
2019-10-11 17:28:10 +02:00
|
|
|
if (ret < 0) {
|
|
|
|
result->check_errors++;
|
|
|
|
fprintf(stderr, "ERROR failed to update the snapshot count in the "
|
|
|
|
"image header: %s\n", strerror(-ret));
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
result->corruptions_fixed += nb_clusters_reduced;
|
|
|
|
result->corruptions -= nb_clusters_reduced;
|
|
|
|
}
|
2019-10-11 17:28:06 +02:00
|
|
|
|
2019-10-11 17:28:12 +02:00
|
|
|
/*
|
|
|
|
* All of v3 images' snapshot table entries need to have at least
|
|
|
|
* 16 bytes of extra data.
|
|
|
|
*/
|
|
|
|
if (s->qcow_version >= 3) {
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < s->nb_snapshots; i++) {
|
|
|
|
if (s->snapshots[i].extra_data_size <
|
|
|
|
sizeof_field(QCowSnapshotExtraData, vm_state_size_large) +
|
|
|
|
sizeof_field(QCowSnapshotExtraData, disk_size))
|
|
|
|
{
|
|
|
|
result->corruptions++;
|
|
|
|
fprintf(stderr, "%s snapshot table entry %i is incomplete\n",
|
|
|
|
fix & BDRV_FIX_ERRORS ? "Repairing" : "ERROR", i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-11 17:28:06 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-10-11 17:28:07 +02:00
|
|
|
int coroutine_fn qcow2_check_fix_snapshot_table(BlockDriverState *bs,
|
|
|
|
BdrvCheckResult *result,
|
|
|
|
BdrvCheckMode fix)
|
|
|
|
{
|
|
|
|
BDRVQcow2State *s = bs->opaque;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (result->corruptions && (fix & BDRV_FIX_ERRORS)) {
|
|
|
|
qemu_co_mutex_unlock(&s->lock);
|
|
|
|
ret = qcow2_write_snapshots(bs);
|
|
|
|
qemu_co_mutex_lock(&s->lock);
|
|
|
|
if (ret < 0) {
|
|
|
|
result->check_errors++;
|
|
|
|
fprintf(stderr, "ERROR failed to update snapshot table: %s\n",
|
|
|
|
strerror(-ret));
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
result->corruptions_fixed += result->corruptions;
|
|
|
|
result->corruptions = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-05-28 16:07:06 +02:00
|
|
|
static void find_new_snapshot_id(BlockDriverState *bs,
|
|
|
|
char *id_str, int id_str_size)
|
|
|
|
{
|
2015-09-07 17:12:56 +02:00
|
|
|
BDRVQcow2State *s = bs->opaque;
|
2009-05-28 16:07:06 +02:00
|
|
|
QCowSnapshot *sn;
|
2013-10-09 14:42:47 +02:00
|
|
|
int i;
|
|
|
|
unsigned long id, id_max = 0;
|
2009-05-28 16:07:06 +02:00
|
|
|
|
|
|
|
for(i = 0; i < s->nb_snapshots; i++) {
|
|
|
|
sn = s->snapshots + i;
|
|
|
|
id = strtoul(sn->id_str, NULL, 10);
|
|
|
|
if (id > id_max)
|
|
|
|
id_max = id;
|
|
|
|
}
|
2013-10-09 14:42:47 +02:00
|
|
|
snprintf(id_str, id_str_size, "%lu", id_max + 1);
|
2009-05-28 16:07:06 +02:00
|
|
|
}
|
|
|
|
|
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 08:04:33 +02:00
|
|
|
static int find_snapshot_by_id_and_name(BlockDriverState *bs,
|
|
|
|
const char *id,
|
|
|
|
const char *name)
|
2009-05-28 16:07:06 +02:00
|
|
|
{
|
2015-09-07 17:12:56 +02:00
|
|
|
BDRVQcow2State *s = bs->opaque;
|
2009-05-28 16:07:06 +02:00
|
|
|
int i;
|
|
|
|
|
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 08:04:33 +02:00
|
|
|
if (id && name) {
|
|
|
|
for (i = 0; i < s->nb_snapshots; i++) {
|
|
|
|
if (!strcmp(s->snapshots[i].id_str, id) &&
|
|
|
|
!strcmp(s->snapshots[i].name, name)) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (id) {
|
|
|
|
for (i = 0; i < s->nb_snapshots; i++) {
|
|
|
|
if (!strcmp(s->snapshots[i].id_str, id)) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (name) {
|
|
|
|
for (i = 0; i < s->nb_snapshots; i++) {
|
|
|
|
if (!strcmp(s->snapshots[i].name, name)) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
2009-05-28 16:07:06 +02:00
|
|
|
}
|
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 08:04:33 +02:00
|
|
|
|
2009-05-28 16:07:06 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
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 08:04:33 +02:00
|
|
|
static int find_snapshot_by_id_or_name(BlockDriverState *bs,
|
|
|
|
const char *id_or_name)
|
2009-05-28 16:07:06 +02:00
|
|
|
{
|
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 08:04:33 +02:00
|
|
|
int ret;
|
2009-05-28 16:07:06 +02:00
|
|
|
|
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 08:04:33 +02:00
|
|
|
ret = find_snapshot_by_id_and_name(bs, id_or_name, NULL);
|
|
|
|
if (ret >= 0) {
|
2009-05-28 16:07:06 +02:00
|
|
|
return ret;
|
|
|
|
}
|
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 08:04:33 +02:00
|
|
|
return find_snapshot_by_id_and_name(bs, NULL, id_or_name);
|
2009-05-28 16:07:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* if no id is provided, a new one is constructed */
|
2009-05-28 16:07:07 +02:00
|
|
|
int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info)
|
2009-05-28 16:07:06 +02:00
|
|
|
{
|
2015-09-07 17:12:56 +02:00
|
|
|
BDRVQcow2State *s = bs->opaque;
|
2011-11-16 12:43:59 +01:00
|
|
|
QCowSnapshot *new_snapshot_list = NULL;
|
|
|
|
QCowSnapshot *old_snapshot_list = NULL;
|
|
|
|
QCowSnapshot sn1, *sn = &sn1;
|
2009-05-28 16:07:06 +02:00
|
|
|
int i, ret;
|
|
|
|
uint64_t *l1_table = NULL;
|
2010-01-20 15:04:01 +01:00
|
|
|
int64_t l1_table_offset;
|
2009-05-28 16:07:06 +02:00
|
|
|
|
2014-03-26 13:05:45 +01:00
|
|
|
if (s->nb_snapshots >= QCOW_MAX_SNAPSHOTS) {
|
|
|
|
return -EFBIG;
|
|
|
|
}
|
|
|
|
|
2019-02-27 17:25:08 +01:00
|
|
|
if (has_data_file(bs)) {
|
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
|
|
|
|
2009-05-28 16:07:06 +02:00
|
|
|
memset(sn, 0, sizeof(*sn));
|
|
|
|
|
2015-03-12 15:54:42 +01:00
|
|
|
/* Generate an ID */
|
|
|
|
find_new_snapshot_id(bs, sn_info->id_str, sizeof(sn_info->id_str));
|
2009-05-28 16:07:06 +02:00
|
|
|
|
2011-11-16 17:46:29 +01:00
|
|
|
/* Populate sn with passed data */
|
2011-08-21 05:09:37 +02:00
|
|
|
sn->id_str = g_strdup(sn_info->id_str);
|
|
|
|
sn->name = g_strdup(sn_info->name);
|
2011-11-16 17:46:29 +01:00
|
|
|
|
2012-04-11 16:33:50 +02:00
|
|
|
sn->disk_size = bs->total_sectors * BDRV_SECTOR_SIZE;
|
2009-05-28 16:07:06 +02:00
|
|
|
sn->vm_state_size = sn_info->vm_state_size;
|
|
|
|
sn->date_sec = sn_info->date_sec;
|
|
|
|
sn->date_nsec = sn_info->date_nsec;
|
|
|
|
sn->vm_clock_nsec = sn_info->vm_clock_nsec;
|
2020-10-03 19:13:08 +02:00
|
|
|
sn->icount = sn_info->icount;
|
2019-10-11 17:28:02 +02:00
|
|
|
sn->extra_data_size = sizeof(QCowSnapshotExtraData);
|
2009-05-28 16:07:06 +02:00
|
|
|
|
2011-11-16 17:46:29 +01:00
|
|
|
/* Allocate the L1 table of the snapshot and copy the current one there. */
|
2020-08-28 13:08:28 +02:00
|
|
|
l1_table_offset = qcow2_alloc_clusters(bs, s->l1_size * L1E_SIZE);
|
2010-01-20 15:04:01 +01:00
|
|
|
if (l1_table_offset < 0) {
|
2011-11-16 12:43:59 +01:00
|
|
|
ret = l1_table_offset;
|
2010-01-20 15:04:01 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
sn->l1_table_offset = l1_table_offset;
|
2009-05-28 16:07:06 +02:00
|
|
|
sn->l1_size = s->l1_size;
|
|
|
|
|
block: Use g_new() & friends where that makes obvious sense
g_new(T, n) is neater than g_malloc(sizeof(T) * n). It's also safer,
for two reasons. One, it catches multiplication overflowing size_t.
Two, it returns T * rather than void *, which lets the compiler catch
more type errors.
Patch created with Coccinelle, with two manual changes on top:
* Add const to bdrv_iterate_format() to keep the types straight
* Convert the allocation in bdrv_drop_intermediate(), which Coccinelle
inexplicably misses
Coccinelle semantic patch:
@@
type T;
@@
-g_malloc(sizeof(T))
+g_new(T, 1)
@@
type T;
@@
-g_try_malloc(sizeof(T))
+g_try_new(T, 1)
@@
type T;
@@
-g_malloc0(sizeof(T))
+g_new0(T, 1)
@@
type T;
@@
-g_try_malloc0(sizeof(T))
+g_try_new0(T, 1)
@@
type T;
expression n;
@@
-g_malloc(sizeof(T) * (n))
+g_new(T, n)
@@
type T;
expression n;
@@
-g_try_malloc(sizeof(T) * (n))
+g_try_new(T, n)
@@
type T;
expression n;
@@
-g_malloc0(sizeof(T) * (n))
+g_new0(T, n)
@@
type T;
expression n;
@@
-g_try_malloc0(sizeof(T) * (n))
+g_try_new0(T, n)
@@
type T;
expression p, n;
@@
-g_realloc(p, sizeof(T) * (n))
+g_renew(T, p, n)
@@
type T;
expression p, n;
@@
-g_try_realloc(p, sizeof(T) * (n))
+g_try_renew(T, p, n)
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Max Reitz <mreitz@redhat.com>
Reviewed-by: Jeff Cody <jcody@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
2014-08-19 10:31:08 +02:00
|
|
|
l1_table = g_try_new(uint64_t, s->l1_size);
|
2014-05-20 17:12:47 +02:00
|
|
|
if (s->l1_size && l1_table == NULL) {
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2009-05-28 16:07:06 +02:00
|
|
|
for(i = 0; i < s->l1_size; i++) {
|
|
|
|
l1_table[i] = cpu_to_be64(s->l1_table[i]);
|
|
|
|
}
|
2011-11-16 12:43:59 +01:00
|
|
|
|
2013-10-10 11:09:23 +02:00
|
|
|
ret = qcow2_pre_write_overlap_check(bs, 0, sn->l1_table_offset,
|
2020-08-28 13:08:28 +02:00
|
|
|
s->l1_size * L1E_SIZE, false);
|
2013-08-30 14:34:26 +02:00
|
|
|
if (ret < 0) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
block: Change bdrv_{pread,pwrite,pwrite_sync}() param order
Swap 'buf' and 'bytes' around for consistency with
bdrv_co_{pread,pwrite}(), and in preparation to implement these
functions using generated_co_wrapper.
Callers were updated using this Coccinelle script:
@@ expression child, offset, buf, bytes, flags; @@
- bdrv_pread(child, offset, buf, bytes, flags)
+ bdrv_pread(child, offset, bytes, buf, flags)
@@ expression child, offset, buf, bytes, flags; @@
- bdrv_pwrite(child, offset, buf, bytes, flags)
+ bdrv_pwrite(child, offset, bytes, buf, flags)
@@ expression child, offset, buf, bytes, flags; @@
- bdrv_pwrite_sync(child, offset, buf, bytes, flags)
+ bdrv_pwrite_sync(child, offset, bytes, buf, flags)
Resulting overly-long lines were then fixed by hand.
Signed-off-by: Alberto Faria <afaria@redhat.com>
Reviewed-by: Paolo Bonzini <pbonzini@redhat.com>
Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@yandex-team.ru>
Message-Id: <20220609152744.3891847-3-afaria@redhat.com>
Reviewed-by: Hanna Reitz <hreitz@redhat.com>
Signed-off-by: Hanna Reitz <hreitz@redhat.com>
2022-06-09 17:27:36 +02:00
|
|
|
ret = bdrv_pwrite(bs->file, sn->l1_table_offset, s->l1_size * L1E_SIZE,
|
|
|
|
l1_table, 0);
|
2011-11-16 12:43:59 +01:00
|
|
|
if (ret < 0) {
|
2009-05-28 16:07:06 +02:00
|
|
|
goto fail;
|
2011-11-16 12:43:59 +01:00
|
|
|
}
|
|
|
|
|
2011-08-21 05:09:37 +02:00
|
|
|
g_free(l1_table);
|
2009-05-28 16:07:06 +02:00
|
|
|
l1_table = NULL;
|
|
|
|
|
2011-11-16 12:43:59 +01:00
|
|
|
/*
|
|
|
|
* Increase the refcounts of all clusters and make sure everything is
|
|
|
|
* stable on disk before updating the snapshot table to contain a pointer
|
|
|
|
* to the new L1 table.
|
|
|
|
*/
|
|
|
|
ret = qcow2_update_snapshot_refcount(bs, s->l1_table_offset, s->l1_size, 1);
|
|
|
|
if (ret < 0) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Append the new snapshot to the snapshot list */
|
block: Use g_new() & friends where that makes obvious sense
g_new(T, n) is neater than g_malloc(sizeof(T) * n). It's also safer,
for two reasons. One, it catches multiplication overflowing size_t.
Two, it returns T * rather than void *, which lets the compiler catch
more type errors.
Patch created with Coccinelle, with two manual changes on top:
* Add const to bdrv_iterate_format() to keep the types straight
* Convert the allocation in bdrv_drop_intermediate(), which Coccinelle
inexplicably misses
Coccinelle semantic patch:
@@
type T;
@@
-g_malloc(sizeof(T))
+g_new(T, 1)
@@
type T;
@@
-g_try_malloc(sizeof(T))
+g_try_new(T, 1)
@@
type T;
@@
-g_malloc0(sizeof(T))
+g_new0(T, 1)
@@
type T;
@@
-g_try_malloc0(sizeof(T))
+g_try_new0(T, 1)
@@
type T;
expression n;
@@
-g_malloc(sizeof(T) * (n))
+g_new(T, n)
@@
type T;
expression n;
@@
-g_try_malloc(sizeof(T) * (n))
+g_try_new(T, n)
@@
type T;
expression n;
@@
-g_malloc0(sizeof(T) * (n))
+g_new0(T, n)
@@
type T;
expression n;
@@
-g_try_malloc0(sizeof(T) * (n))
+g_try_new0(T, n)
@@
type T;
expression p, n;
@@
-g_realloc(p, sizeof(T) * (n))
+g_renew(T, p, n)
@@
type T;
expression p, n;
@@
-g_try_realloc(p, sizeof(T) * (n))
+g_try_renew(T, p, n)
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Max Reitz <mreitz@redhat.com>
Reviewed-by: Jeff Cody <jcody@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
2014-08-19 10:31:08 +02:00
|
|
|
new_snapshot_list = g_new(QCowSnapshot, s->nb_snapshots + 1);
|
2009-05-28 16:07:06 +02:00
|
|
|
if (s->snapshots) {
|
2011-11-16 12:43:59 +01:00
|
|
|
memcpy(new_snapshot_list, s->snapshots,
|
|
|
|
s->nb_snapshots * sizeof(QCowSnapshot));
|
|
|
|
old_snapshot_list = s->snapshots;
|
2009-05-28 16:07:06 +02:00
|
|
|
}
|
2011-11-16 12:43:59 +01:00
|
|
|
s->snapshots = new_snapshot_list;
|
2009-05-28 16:07:06 +02:00
|
|
|
s->snapshots[s->nb_snapshots++] = *sn;
|
|
|
|
|
2011-11-16 12:43:59 +01:00
|
|
|
ret = qcow2_write_snapshots(bs);
|
|
|
|
if (ret < 0) {
|
|
|
|
g_free(s->snapshots);
|
|
|
|
s->snapshots = old_snapshot_list;
|
2013-10-09 14:42:00 +02:00
|
|
|
s->nb_snapshots--;
|
2009-05-28 16:07:06 +02:00
|
|
|
goto fail;
|
2011-11-16 12:43:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
g_free(old_snapshot_list);
|
|
|
|
|
2013-09-06 12:20:08 +02:00
|
|
|
/* The VM state isn't needed any more in the active L1 table; in fact, it
|
|
|
|
* hurts by causing expensive COW for the next snapshot. */
|
qcow2: Discard/zero clusters by byte count
Passing a byte offset, but sector count, when we ultimately
want to operate on cluster granularity, is madness. Clean up
the external interfaces to take both offset and count as bytes,
while still keeping the assertion added previously that the
caller must align the values to a cluster. Then rename things
to make sure backports don't get confused by changed units:
instead of qcow2_discard_clusters() and qcow2_zero_clusters(),
we now have qcow2_cluster_discard() and qcow2_cluster_zeroize().
The internal functions still operate on clusters at a time, and
return an int for number of cleared clusters; but on an image
with 2M clusters, a single L2 table holds 256k entries that each
represent a 2M cluster, totalling well over INT_MAX bytes if we
ever had a request for that many bytes at once. All our callers
currently limit themselves to 32-bit bytes (and therefore fewer
clusters), but by making this function 64-bit clean, we have one
less place to clean up if we later improve the block layer to
support 64-bit bytes through all operations (with the block layer
auto-fragmenting on behalf of more-limited drivers), rather than
the current state where some interfaces are artificially limited
to INT_MAX at a time.
Signed-off-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Max Reitz <mreitz@redhat.com>
Message-id: 20170507000552.20847-13-eblake@redhat.com
Signed-off-by: Max Reitz <mreitz@redhat.com>
2017-05-07 02:05:52 +02:00
|
|
|
qcow2_cluster_discard(bs, qcow2_vm_state_offset(s),
|
2018-02-15 14:10:08 +01:00
|
|
|
ROUND_UP(sn->vm_state_size, s->cluster_size),
|
qcow2: Discard/zero clusters by byte count
Passing a byte offset, but sector count, when we ultimately
want to operate on cluster granularity, is madness. Clean up
the external interfaces to take both offset and count as bytes,
while still keeping the assertion added previously that the
caller must align the values to a cluster. Then rename things
to make sure backports don't get confused by changed units:
instead of qcow2_discard_clusters() and qcow2_zero_clusters(),
we now have qcow2_cluster_discard() and qcow2_cluster_zeroize().
The internal functions still operate on clusters at a time, and
return an int for number of cleared clusters; but on an image
with 2M clusters, a single L2 table holds 256k entries that each
represent a 2M cluster, totalling well over INT_MAX bytes if we
ever had a request for that many bytes at once. All our callers
currently limit themselves to 32-bit bytes (and therefore fewer
clusters), but by making this function 64-bit clean, we have one
less place to clean up if we later improve the block layer to
support 64-bit bytes through all operations (with the block layer
auto-fragmenting on behalf of more-limited drivers), rather than
the current state where some interfaces are artificially limited
to INT_MAX at a time.
Signed-off-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Max Reitz <mreitz@redhat.com>
Message-id: 20170507000552.20847-13-eblake@redhat.com
Signed-off-by: Max Reitz <mreitz@redhat.com>
2017-05-07 02:05:52 +02:00
|
|
|
QCOW2_DISCARD_NEVER, false);
|
2013-09-06 12:20:08 +02:00
|
|
|
|
2009-05-28 16:07:06 +02:00
|
|
|
#ifdef DEBUG_ALLOC
|
2011-08-04 19:22:10 +02:00
|
|
|
{
|
|
|
|
BdrvCheckResult result = {0};
|
2012-06-15 17:41:07 +02:00
|
|
|
qcow2_check_refcounts(bs, &result, 0);
|
2011-08-04 19:22:10 +02:00
|
|
|
}
|
2009-05-28 16:07:06 +02:00
|
|
|
#endif
|
|
|
|
return 0;
|
2011-11-16 17:46:29 +01:00
|
|
|
|
|
|
|
fail:
|
|
|
|
g_free(sn->id_str);
|
2011-08-21 05:09:37 +02:00
|
|
|
g_free(sn->name);
|
|
|
|
g_free(l1_table);
|
2011-11-16 12:43:59 +01:00
|
|
|
|
|
|
|
return ret;
|
2009-05-28 16:07:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* copy the snapshot 'snapshot_name' into the current disk image */
|
2009-05-28 16:07:07 +02:00
|
|
|
int qcow2_snapshot_goto(BlockDriverState *bs, const char *snapshot_id)
|
2009-05-28 16:07:06 +02:00
|
|
|
{
|
2015-09-07 17:12:56 +02:00
|
|
|
BDRVQcow2State *s = bs->opaque;
|
2009-05-28 16:07:06 +02:00
|
|
|
QCowSnapshot *sn;
|
2018-03-06 17:14:10 +01:00
|
|
|
Error *local_err = NULL;
|
2011-08-05 12:06:11 +02:00
|
|
|
int i, snapshot_index;
|
|
|
|
int cur_l1_bytes, sn_l1_bytes;
|
2011-11-16 15:04:11 +01:00
|
|
|
int ret;
|
2011-11-16 15:20:45 +01:00
|
|
|
uint64_t *sn_l1_table = NULL;
|
2009-05-28 16:07:06 +02:00
|
|
|
|
2019-02-27 17:25:08 +01:00
|
|
|
if (has_data_file(bs)) {
|
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
|
|
|
|
2011-11-16 15:04:11 +01:00
|
|
|
/* Search the snapshot */
|
2009-05-28 16:07:06 +02:00
|
|
|
snapshot_index = find_snapshot_by_id_or_name(bs, snapshot_id);
|
2011-11-16 15:04:11 +01:00
|
|
|
if (snapshot_index < 0) {
|
2009-05-28 16:07:06 +02:00
|
|
|
return -ENOENT;
|
2011-11-16 15:04:11 +01:00
|
|
|
}
|
2009-05-28 16:07:06 +02:00
|
|
|
sn = &s->snapshots[snapshot_index];
|
|
|
|
|
2018-03-06 17:14:10 +01:00
|
|
|
ret = qcow2_validate_table(bs, sn->l1_table_offset, sn->l1_size,
|
2020-08-28 13:08:28 +02:00
|
|
|
L1E_SIZE, QCOW_MAX_L1_SIZE,
|
2018-03-06 17:14:10 +01:00
|
|
|
"Snapshot L1 table", &local_err);
|
|
|
|
if (ret < 0) {
|
|
|
|
error_report_err(local_err);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2012-04-11 16:33:50 +02:00
|
|
|
if (sn->disk_size != bs->total_sectors * BDRV_SECTOR_SIZE) {
|
qcow2: Allow resize of images with internal snapshots
We originally refused to allow resize of images with internal
snapshots because the v2 image format did not require the tracking of
snapshot size, making it impossible to safely revert to a snapshot
with a different size than the current view of the image. But the
snapshot size tracking was rectified in v3, and our recent fixes to
qemu-img amend (see 0a85af35) guarantee that we always have a valid
snapshot size. Thus, we no longer need to artificially limit image
resizes, but it does become one more thing that would prevent a
downgrade back to v2. And now that we support different-sized
snapshots, it's also easy to fix reverting to a snapshot to apply the
new size.
Upgrade iotest 61 to cover this (we previously had NO coverage of
refusal to resize while snapshots exist). Note that the amend process
can fail but still have effects: in particular, since we break things
into upgrade, resize, downgrade, a failure during resize does not roll
back changes made during upgrade, nor does failure in downgrade roll
back a resize. But this situation is pre-existing even without this
patch; and without journaling, the best we could do is minimize the
chance of partial failure by collecting all changes prior to doing any
writes - which adds a lot of complexity but could still fail with EIO.
On the other hand, we are careful that even if we have partial
modification but then fail, the image is left viable (that is, we are
careful to sequence things so that after each successful cluster
write, there may be transient leaked clusters but no corrupt
metadata). And complicating the code to make it more transaction-like
is not worth the effort: a user can always request multiple 'qemu-img
amend' changing one thing each, if they need finer-grained control
over detecting the first failure than what they get by letting qemu
decide how to sequence multiple changes.
Signed-off-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Max Reitz <mreitz@redhat.com>
Message-Id: <20200428192648.749066-3-eblake@redhat.com>
Signed-off-by: Max Reitz <mreitz@redhat.com>
2020-04-28 21:26:47 +02:00
|
|
|
BlockBackend *blk = blk_new_with_bs(bs, BLK_PERM_RESIZE, BLK_PERM_ALL,
|
|
|
|
&local_err);
|
|
|
|
if (!blk) {
|
|
|
|
error_report_err(local_err);
|
|
|
|
ret = -ENOTSUP;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = blk_truncate(blk, sn->disk_size, true, PREALLOC_MODE_OFF, 0,
|
|
|
|
&local_err);
|
|
|
|
blk_unref(blk);
|
|
|
|
if (ret < 0) {
|
|
|
|
error_report_err(local_err);
|
|
|
|
goto fail;
|
|
|
|
}
|
2012-04-11 16:33:50 +02:00
|
|
|
}
|
|
|
|
|
2011-11-16 15:04:11 +01:00
|
|
|
/*
|
|
|
|
* Make sure that the current L1 table is big enough to contain the whole
|
|
|
|
* L1 table of the snapshot. If the snapshot L1 table is smaller, the
|
|
|
|
* current one must be padded with zeros.
|
|
|
|
*/
|
|
|
|
ret = qcow2_grow_l1_table(bs, sn->l1_size, true);
|
|
|
|
if (ret < 0) {
|
2009-05-28 16:07:06 +02:00
|
|
|
goto fail;
|
2011-11-16 15:04:11 +01:00
|
|
|
}
|
2009-05-28 16:07:06 +02:00
|
|
|
|
2020-08-28 13:08:28 +02:00
|
|
|
cur_l1_bytes = s->l1_size * L1E_SIZE;
|
|
|
|
sn_l1_bytes = sn->l1_size * L1E_SIZE;
|
2011-08-05 12:06:11 +02:00
|
|
|
|
2011-11-16 15:04:11 +01:00
|
|
|
/*
|
|
|
|
* Copy the snapshot L1 table to the current L1 table.
|
|
|
|
*
|
|
|
|
* Before overwriting the old current L1 table on disk, make sure to
|
|
|
|
* increase all refcounts for the clusters referenced by the new one.
|
2011-11-16 15:20:45 +01:00
|
|
|
* Decrease the refcount referenced by the old one only when the L1
|
|
|
|
* table is overwritten.
|
2011-11-16 15:04:11 +01:00
|
|
|
*/
|
2014-05-20 17:12:47 +02:00
|
|
|
sn_l1_table = g_try_malloc0(cur_l1_bytes);
|
|
|
|
if (cur_l1_bytes && sn_l1_table == NULL) {
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto fail;
|
|
|
|
}
|
2011-11-16 15:20:45 +01:00
|
|
|
|
block: Change bdrv_{pread,pwrite,pwrite_sync}() param order
Swap 'buf' and 'bytes' around for consistency with
bdrv_co_{pread,pwrite}(), and in preparation to implement these
functions using generated_co_wrapper.
Callers were updated using this Coccinelle script:
@@ expression child, offset, buf, bytes, flags; @@
- bdrv_pread(child, offset, buf, bytes, flags)
+ bdrv_pread(child, offset, bytes, buf, flags)
@@ expression child, offset, buf, bytes, flags; @@
- bdrv_pwrite(child, offset, buf, bytes, flags)
+ bdrv_pwrite(child, offset, bytes, buf, flags)
@@ expression child, offset, buf, bytes, flags; @@
- bdrv_pwrite_sync(child, offset, buf, bytes, flags)
+ bdrv_pwrite_sync(child, offset, bytes, buf, flags)
Resulting overly-long lines were then fixed by hand.
Signed-off-by: Alberto Faria <afaria@redhat.com>
Reviewed-by: Paolo Bonzini <pbonzini@redhat.com>
Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@yandex-team.ru>
Message-Id: <20220609152744.3891847-3-afaria@redhat.com>
Reviewed-by: Hanna Reitz <hreitz@redhat.com>
Signed-off-by: Hanna Reitz <hreitz@redhat.com>
2022-06-09 17:27:36 +02:00
|
|
|
ret = bdrv_pread(bs->file, sn->l1_table_offset, sn_l1_bytes, sn_l1_table,
|
block: Add a 'flags' param to bdrv_{pread,pwrite,pwrite_sync}()
For consistency with other I/O functions, and in preparation to
implement them using generated_co_wrapper.
Callers were updated using this Coccinelle script:
@@ expression child, offset, buf, bytes; @@
- bdrv_pread(child, offset, buf, bytes)
+ bdrv_pread(child, offset, buf, bytes, 0)
@@ expression child, offset, buf, bytes; @@
- bdrv_pwrite(child, offset, buf, bytes)
+ bdrv_pwrite(child, offset, buf, bytes, 0)
@@ expression child, offset, buf, bytes; @@
- bdrv_pwrite_sync(child, offset, buf, bytes)
+ bdrv_pwrite_sync(child, offset, buf, bytes, 0)
Resulting overly-long lines were then fixed by hand.
Signed-off-by: Alberto Faria <afaria@redhat.com>
Reviewed-by: Paolo Bonzini <pbonzini@redhat.com>
Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@yandex-team.ru>
Message-Id: <20220609152744.3891847-2-afaria@redhat.com>
Reviewed-by: Hanna Reitz <hreitz@redhat.com>
Signed-off-by: Hanna Reitz <hreitz@redhat.com>
2022-06-09 17:27:35 +02:00
|
|
|
0);
|
2011-11-16 15:20:45 +01:00
|
|
|
if (ret < 0) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = qcow2_update_snapshot_refcount(bs, sn->l1_table_offset,
|
|
|
|
sn->l1_size, 1);
|
2011-11-16 15:04:11 +01:00
|
|
|
if (ret < 0) {
|
2009-05-28 16:07:06 +02:00
|
|
|
goto fail;
|
2011-11-16 15:04:11 +01:00
|
|
|
}
|
|
|
|
|
2013-10-10 11:09:23 +02:00
|
|
|
ret = qcow2_pre_write_overlap_check(bs, QCOW2_OL_ACTIVE_L1,
|
2019-01-15 20:39:06 +01:00
|
|
|
s->l1_table_offset, cur_l1_bytes,
|
|
|
|
false);
|
2013-08-30 14:34:26 +02:00
|
|
|
if (ret < 0) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
block: Change bdrv_{pread,pwrite,pwrite_sync}() param order
Swap 'buf' and 'bytes' around for consistency with
bdrv_co_{pread,pwrite}(), and in preparation to implement these
functions using generated_co_wrapper.
Callers were updated using this Coccinelle script:
@@ expression child, offset, buf, bytes, flags; @@
- bdrv_pread(child, offset, buf, bytes, flags)
+ bdrv_pread(child, offset, bytes, buf, flags)
@@ expression child, offset, buf, bytes, flags; @@
- bdrv_pwrite(child, offset, buf, bytes, flags)
+ bdrv_pwrite(child, offset, bytes, buf, flags)
@@ expression child, offset, buf, bytes, flags; @@
- bdrv_pwrite_sync(child, offset, buf, bytes, flags)
+ bdrv_pwrite_sync(child, offset, bytes, buf, flags)
Resulting overly-long lines were then fixed by hand.
Signed-off-by: Alberto Faria <afaria@redhat.com>
Reviewed-by: Paolo Bonzini <pbonzini@redhat.com>
Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@yandex-team.ru>
Message-Id: <20220609152744.3891847-3-afaria@redhat.com>
Reviewed-by: Hanna Reitz <hreitz@redhat.com>
Signed-off-by: Hanna Reitz <hreitz@redhat.com>
2022-06-09 17:27:36 +02:00
|
|
|
ret = bdrv_pwrite_sync(bs->file, s->l1_table_offset, cur_l1_bytes,
|
|
|
|
sn_l1_table, 0);
|
2011-11-16 15:04:11 +01:00
|
|
|
if (ret < 0) {
|
2009-05-28 16:07:06 +02:00
|
|
|
goto fail;
|
2011-11-16 15:04:11 +01:00
|
|
|
}
|
|
|
|
|
2011-11-16 15:20:45 +01:00
|
|
|
/*
|
|
|
|
* Decrease refcount of clusters of current L1 table.
|
|
|
|
*
|
|
|
|
* At this point, the in-memory s->l1_table points to the old L1 table,
|
|
|
|
* whereas on disk we already have the new one.
|
|
|
|
*
|
|
|
|
* qcow2_update_snapshot_refcount special cases the current L1 table to use
|
|
|
|
* the in-memory data instead of really using the offset to load a new one,
|
|
|
|
* which is why this works.
|
|
|
|
*/
|
|
|
|
ret = qcow2_update_snapshot_refcount(bs, s->l1_table_offset,
|
|
|
|
s->l1_size, -1);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Now update the in-memory L1 table to be in sync with the on-disk one. We
|
|
|
|
* need to do this even if updating refcounts failed.
|
|
|
|
*/
|
2009-05-28 16:07:06 +02:00
|
|
|
for(i = 0;i < s->l1_size; i++) {
|
2011-11-16 15:20:45 +01:00
|
|
|
s->l1_table[i] = be64_to_cpu(sn_l1_table[i]);
|
2009-05-28 16:07:06 +02:00
|
|
|
}
|
|
|
|
|
2011-11-16 15:20:45 +01:00
|
|
|
if (ret < 0) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_free(sn_l1_table);
|
|
|
|
sn_l1_table = NULL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Update QCOW_OFLAG_COPIED in the active L1 table (it may have changed
|
|
|
|
* when we decreased the refcount of the old snapshot.
|
|
|
|
*/
|
|
|
|
ret = qcow2_update_snapshot_refcount(bs, s->l1_table_offset, s->l1_size, 0);
|
2011-11-16 15:04:11 +01:00
|
|
|
if (ret < 0) {
|
2009-05-28 16:07:06 +02:00
|
|
|
goto fail;
|
2011-11-16 15:04:11 +01:00
|
|
|
}
|
2009-05-28 16:07:06 +02:00
|
|
|
|
|
|
|
#ifdef DEBUG_ALLOC
|
2011-08-04 19:22:10 +02:00
|
|
|
{
|
|
|
|
BdrvCheckResult result = {0};
|
2012-06-15 17:41:07 +02:00
|
|
|
qcow2_check_refcounts(bs, &result, 0);
|
2011-08-04 19:22:10 +02:00
|
|
|
}
|
2009-05-28 16:07:06 +02:00
|
|
|
#endif
|
|
|
|
return 0;
|
2011-11-16 15:04:11 +01:00
|
|
|
|
|
|
|
fail:
|
2011-11-16 15:20:45 +01:00
|
|
|
g_free(sn_l1_table);
|
2011-11-16 15:04:11 +01:00
|
|
|
return ret;
|
2009-05-28 16:07:06 +02:00
|
|
|
}
|
|
|
|
|
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 08:04:33 +02:00
|
|
|
int qcow2_snapshot_delete(BlockDriverState *bs,
|
|
|
|
const char *snapshot_id,
|
|
|
|
const char *name,
|
|
|
|
Error **errp)
|
2009-05-28 16:07:06 +02:00
|
|
|
{
|
2015-09-07 17:12:56 +02:00
|
|
|
BDRVQcow2State *s = bs->opaque;
|
2011-11-16 17:22:10 +01:00
|
|
|
QCowSnapshot sn;
|
2009-05-28 16:07:06 +02:00
|
|
|
int snapshot_index, ret;
|
|
|
|
|
2019-02-27 17:25:08 +01:00
|
|
|
if (has_data_file(bs)) {
|
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
|
|
|
|
2011-11-16 17:22:10 +01:00
|
|
|
/* Search the snapshot */
|
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 08:04:33 +02:00
|
|
|
snapshot_index = find_snapshot_by_id_and_name(bs, snapshot_id, name);
|
2011-11-16 17:22:10 +01:00
|
|
|
if (snapshot_index < 0) {
|
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 08:04:33 +02:00
|
|
|
error_setg(errp, "Can't find the snapshot");
|
2009-05-28 16:07:06 +02:00
|
|
|
return -ENOENT;
|
2011-11-16 17:22:10 +01:00
|
|
|
}
|
|
|
|
sn = s->snapshots[snapshot_index];
|
2009-05-28 16:07:06 +02:00
|
|
|
|
2018-03-06 17:14:11 +01:00
|
|
|
ret = qcow2_validate_table(bs, sn.l1_table_offset, sn.l1_size,
|
2020-08-28 13:08:28 +02:00
|
|
|
L1E_SIZE, QCOW_MAX_L1_SIZE,
|
2018-03-06 17:14:11 +01:00
|
|
|
"Snapshot L1 table", errp);
|
|
|
|
if (ret < 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-11-16 17:22:10 +01:00
|
|
|
/* Remove it from the snapshot list */
|
|
|
|
memmove(s->snapshots + snapshot_index,
|
|
|
|
s->snapshots + snapshot_index + 1,
|
|
|
|
(s->nb_snapshots - snapshot_index - 1) * sizeof(sn));
|
|
|
|
s->nb_snapshots--;
|
|
|
|
ret = qcow2_write_snapshots(bs);
|
|
|
|
if (ret < 0) {
|
2014-02-12 20:46:24 +01:00
|
|
|
error_setg_errno(errp, -ret,
|
|
|
|
"Failed to remove snapshot from snapshot list");
|
2009-05-28 16:07:06 +02:00
|
|
|
return ret;
|
2011-11-16 17:22:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The snapshot is now unused, clean up. If we fail after this point, we
|
|
|
|
* won't recover but just leak clusters.
|
|
|
|
*/
|
2019-10-11 17:28:02 +02:00
|
|
|
g_free(sn.unknown_extra_data);
|
2011-11-16 17:22:10 +01:00
|
|
|
g_free(sn.id_str);
|
|
|
|
g_free(sn.name);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Now decrease the refcounts of clusters referenced by the snapshot and
|
|
|
|
* free the L1 table.
|
|
|
|
*/
|
|
|
|
ret = qcow2_update_snapshot_refcount(bs, sn.l1_table_offset,
|
|
|
|
sn.l1_size, -1);
|
|
|
|
if (ret < 0) {
|
2014-02-12 20:46:24 +01:00
|
|
|
error_setg_errno(errp, -ret, "Failed to free the cluster and L1 table");
|
2009-05-28 16:07:06 +02:00
|
|
|
return ret;
|
2011-11-16 17:22:10 +01:00
|
|
|
}
|
2020-08-28 13:08:28 +02:00
|
|
|
qcow2_free_clusters(bs, sn.l1_table_offset, sn.l1_size * L1E_SIZE,
|
2013-06-19 13:44:18 +02:00
|
|
|
QCOW2_DISCARD_SNAPSHOT);
|
2009-05-28 16:07:06 +02:00
|
|
|
|
2011-11-16 17:22:10 +01:00
|
|
|
/* must update the copied flag on the current cluster offsets */
|
|
|
|
ret = qcow2_update_snapshot_refcount(bs, s->l1_table_offset, s->l1_size, 0);
|
2009-05-28 16:07:06 +02:00
|
|
|
if (ret < 0) {
|
2014-02-12 20:46:24 +01:00
|
|
|
error_setg_errno(errp, -ret,
|
|
|
|
"Failed to update snapshot status in disk");
|
2009-05-28 16:07:06 +02:00
|
|
|
return ret;
|
|
|
|
}
|
2011-11-16 17:22:10 +01:00
|
|
|
|
2009-05-28 16:07:06 +02:00
|
|
|
#ifdef DEBUG_ALLOC
|
2011-08-04 19:22:10 +02:00
|
|
|
{
|
|
|
|
BdrvCheckResult result = {0};
|
2012-06-15 17:41:07 +02:00
|
|
|
qcow2_check_refcounts(bs, &result, 0);
|
2011-08-04 19:22:10 +02:00
|
|
|
}
|
2009-05-28 16:07:06 +02:00
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-05-28 16:07:07 +02:00
|
|
|
int qcow2_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab)
|
2009-05-28 16:07:06 +02:00
|
|
|
{
|
2015-09-07 17:12:56 +02:00
|
|
|
BDRVQcow2State *s = bs->opaque;
|
2009-05-28 16:07:06 +02:00
|
|
|
QEMUSnapshotInfo *sn_tab, *sn_info;
|
|
|
|
QCowSnapshot *sn;
|
|
|
|
int i;
|
|
|
|
|
2019-02-27 17:25:08 +01:00
|
|
|
if (has_data_file(bs)) {
|
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
2009-05-28 16:07:06 +02:00
|
|
|
if (!s->nb_snapshots) {
|
|
|
|
*psn_tab = NULL;
|
|
|
|
return s->nb_snapshots;
|
|
|
|
}
|
|
|
|
|
block: Use g_new() & friends where that makes obvious sense
g_new(T, n) is neater than g_malloc(sizeof(T) * n). It's also safer,
for two reasons. One, it catches multiplication overflowing size_t.
Two, it returns T * rather than void *, which lets the compiler catch
more type errors.
Patch created with Coccinelle, with two manual changes on top:
* Add const to bdrv_iterate_format() to keep the types straight
* Convert the allocation in bdrv_drop_intermediate(), which Coccinelle
inexplicably misses
Coccinelle semantic patch:
@@
type T;
@@
-g_malloc(sizeof(T))
+g_new(T, 1)
@@
type T;
@@
-g_try_malloc(sizeof(T))
+g_try_new(T, 1)
@@
type T;
@@
-g_malloc0(sizeof(T))
+g_new0(T, 1)
@@
type T;
@@
-g_try_malloc0(sizeof(T))
+g_try_new0(T, 1)
@@
type T;
expression n;
@@
-g_malloc(sizeof(T) * (n))
+g_new(T, n)
@@
type T;
expression n;
@@
-g_try_malloc(sizeof(T) * (n))
+g_try_new(T, n)
@@
type T;
expression n;
@@
-g_malloc0(sizeof(T) * (n))
+g_new0(T, n)
@@
type T;
expression n;
@@
-g_try_malloc0(sizeof(T) * (n))
+g_try_new0(T, n)
@@
type T;
expression p, n;
@@
-g_realloc(p, sizeof(T) * (n))
+g_renew(T, p, n)
@@
type T;
expression p, n;
@@
-g_try_realloc(p, sizeof(T) * (n))
+g_try_renew(T, p, n)
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Max Reitz <mreitz@redhat.com>
Reviewed-by: Jeff Cody <jcody@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
2014-08-19 10:31:08 +02:00
|
|
|
sn_tab = g_new0(QEMUSnapshotInfo, s->nb_snapshots);
|
2009-05-28 16:07:06 +02:00
|
|
|
for(i = 0; i < s->nb_snapshots; i++) {
|
|
|
|
sn_info = sn_tab + i;
|
|
|
|
sn = s->snapshots + i;
|
|
|
|
pstrcpy(sn_info->id_str, sizeof(sn_info->id_str),
|
|
|
|
sn->id_str);
|
|
|
|
pstrcpy(sn_info->name, sizeof(sn_info->name),
|
|
|
|
sn->name);
|
|
|
|
sn_info->vm_state_size = sn->vm_state_size;
|
|
|
|
sn_info->date_sec = sn->date_sec;
|
|
|
|
sn_info->date_nsec = sn->date_nsec;
|
|
|
|
sn_info->vm_clock_nsec = sn->vm_clock_nsec;
|
2020-10-03 19:13:08 +02:00
|
|
|
sn_info->icount = sn->icount;
|
2009-05-28 16:07:06 +02:00
|
|
|
}
|
|
|
|
*psn_tab = sn_tab;
|
|
|
|
return s->nb_snapshots;
|
|
|
|
}
|
|
|
|
|
2013-12-04 10:10:54 +01:00
|
|
|
int qcow2_snapshot_load_tmp(BlockDriverState *bs,
|
|
|
|
const char *snapshot_id,
|
|
|
|
const char *name,
|
|
|
|
Error **errp)
|
2010-09-22 04:58:41 +02:00
|
|
|
{
|
2011-11-16 17:30:33 +01:00
|
|
|
int i, snapshot_index;
|
2015-09-07 17:12:56 +02:00
|
|
|
BDRVQcow2State *s = bs->opaque;
|
2010-09-22 04:58:41 +02:00
|
|
|
QCowSnapshot *sn;
|
2011-11-16 17:30:33 +01:00
|
|
|
uint64_t *new_l1_table;
|
|
|
|
int new_l1_bytes;
|
|
|
|
int ret;
|
2010-09-22 04:58:41 +02:00
|
|
|
|
2021-05-27 17:40:54 +02:00
|
|
|
assert(bdrv_is_read_only(bs));
|
2011-11-16 17:30:33 +01:00
|
|
|
|
|
|
|
/* Search the snapshot */
|
2013-12-04 10:10:54 +01:00
|
|
|
snapshot_index = find_snapshot_by_id_and_name(bs, snapshot_id, name);
|
2010-09-22 04:58:41 +02:00
|
|
|
if (snapshot_index < 0) {
|
2013-12-04 10:10:54 +01:00
|
|
|
error_setg(errp,
|
|
|
|
"Can't find snapshot");
|
2010-09-22 04:58:41 +02:00
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
sn = &s->snapshots[snapshot_index];
|
|
|
|
|
2011-11-16 17:30:33 +01:00
|
|
|
/* Allocate and read in the snapshot's L1 table */
|
2018-03-06 17:14:07 +01:00
|
|
|
ret = qcow2_validate_table(bs, sn->l1_table_offset, sn->l1_size,
|
2020-08-28 13:08:28 +02:00
|
|
|
L1E_SIZE, QCOW_MAX_L1_SIZE,
|
2018-03-06 17:14:07 +01:00
|
|
|
"Snapshot L1 table", errp);
|
|
|
|
if (ret < 0) {
|
|
|
|
return ret;
|
2014-03-26 13:06:06 +01:00
|
|
|
}
|
2020-08-28 13:08:28 +02:00
|
|
|
new_l1_bytes = sn->l1_size * L1E_SIZE;
|
2020-01-18 20:09:26 +01:00
|
|
|
new_l1_table = qemu_try_blockalign(bs->file->bs, new_l1_bytes);
|
2014-05-20 17:12:47 +02:00
|
|
|
if (new_l1_table == NULL) {
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
2010-09-22 04:58:41 +02:00
|
|
|
|
block: Change bdrv_{pread,pwrite,pwrite_sync}() param order
Swap 'buf' and 'bytes' around for consistency with
bdrv_co_{pread,pwrite}(), and in preparation to implement these
functions using generated_co_wrapper.
Callers were updated using this Coccinelle script:
@@ expression child, offset, buf, bytes, flags; @@
- bdrv_pread(child, offset, buf, bytes, flags)
+ bdrv_pread(child, offset, bytes, buf, flags)
@@ expression child, offset, buf, bytes, flags; @@
- bdrv_pwrite(child, offset, buf, bytes, flags)
+ bdrv_pwrite(child, offset, bytes, buf, flags)
@@ expression child, offset, buf, bytes, flags; @@
- bdrv_pwrite_sync(child, offset, buf, bytes, flags)
+ bdrv_pwrite_sync(child, offset, bytes, buf, flags)
Resulting overly-long lines were then fixed by hand.
Signed-off-by: Alberto Faria <afaria@redhat.com>
Reviewed-by: Paolo Bonzini <pbonzini@redhat.com>
Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@yandex-team.ru>
Message-Id: <20220609152744.3891847-3-afaria@redhat.com>
Reviewed-by: Hanna Reitz <hreitz@redhat.com>
Signed-off-by: Hanna Reitz <hreitz@redhat.com>
2022-06-09 17:27:36 +02:00
|
|
|
ret = bdrv_pread(bs->file, sn->l1_table_offset, new_l1_bytes,
|
|
|
|
new_l1_table, 0);
|
2011-11-16 17:30:33 +01:00
|
|
|
if (ret < 0) {
|
2013-12-04 10:10:54 +01:00
|
|
|
error_setg(errp, "Failed to read l1 table for snapshot");
|
2014-05-20 17:12:47 +02:00
|
|
|
qemu_vfree(new_l1_table);
|
2011-11-16 17:30:33 +01:00
|
|
|
return ret;
|
2010-09-22 04:58:41 +02:00
|
|
|
}
|
|
|
|
|
2011-11-16 17:30:33 +01:00
|
|
|
/* Switch the L1 table */
|
2014-05-20 17:12:47 +02:00
|
|
|
qemu_vfree(s->l1_table);
|
2011-11-16 17:30:33 +01:00
|
|
|
|
|
|
|
s->l1_size = sn->l1_size;
|
|
|
|
s->l1_table_offset = sn->l1_table_offset;
|
|
|
|
s->l1_table = new_l1_table;
|
|
|
|
|
2010-09-22 04:58:41 +02:00
|
|
|
for(i = 0;i < s->l1_size; i++) {
|
|
|
|
be64_to_cpus(&s->l1_table[i]);
|
|
|
|
}
|
2011-11-16 17:30:33 +01:00
|
|
|
|
2010-09-22 04:58:41 +02:00
|
|
|
return 0;
|
|
|
|
}
|