@@ -498,7 +498,7 @@ int qcow2_snapshot_goto(BlockDriverState *bs, const char *snapshot_id)
* Decrease the refcount referenced by the old one only when the L1
* table is overwritten.
*/
- sn_l1_table = g_try_malloc0(cur_l1_bytes);
+ sn_l1_table = g_try_malloc0(sn_l1_bytes);
if (cur_l1_bytes && sn_l1_table == NULL) {
ret = -ENOMEM;
goto fail;
@@ -523,7 +523,7 @@ int qcow2_snapshot_goto(BlockDriverState *bs, const char *snapshot_id)
}
ret = bdrv_pwrite_sync(bs->file->bs, s->l1_table_offset, sn_l1_table,
- cur_l1_bytes);
+ sn_l1_bytes);
if (ret < 0) {
goto fail;
}
@@ -536,6 +536,15 @@ int qcow2_snapshot_goto(BlockDriverState *bs, const char *snapshot_id)
goto fail;
}
+ uint32_t be_l1_size = cpu_to_be32(sn->l1_size);
+ ret = bdrv_pwrite_sync(bs->file->bs, offsetof(QCowHeader, l1_size),
+ &be_l1_size, sizeof(be_l1_size));
+ if (ret < 0) {
+ goto fail;
+ }
+
+ s->l1_vm_state_index = sn->l1_size;
+
/*
* Decrease refcount of clusters of current L1 table.
*
@@ -553,10 +562,12 @@ int qcow2_snapshot_goto(BlockDriverState *bs, const char *snapshot_id)
* 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.
*/
- for(i = 0;i < s->l1_size; i++) {
+ memset(s->l1_table, 0, s->l1_size*sizeof(uint64_t));
+ for(i = 0;i < sn->l1_size; i++) {
s->l1_table[i] = be64_to_cpu(sn_l1_table[i]);
}
+
if (ret < 0) {
goto fail;
}
@@ -564,6 +575,7 @@ int qcow2_snapshot_goto(BlockDriverState *bs, const char *snapshot_id)
g_free(sn_l1_table);
sn_l1_table = NULL;
+ s->l1_size = sn->l1_size;
/*
* Update QCOW_OFLAG_COPIED in the active L1 table (it may have changed
* when we decreased the refcount of the old snapshot.