qcow2: Repair snapshot table with too many entries

The user cannot choose which snapshots are removed.  This is fine
because we have chosen the maximum snapshot table size to be so large
(65536 entries) that it cannot be reasonably reached.  If the snapshot
table exceeds this size, the image has probably been corrupted in some
way; in this case, it is most important to just make the image usable
such that the user can copy off at least the active layer.
(Also note that the snapshots will be removed only with "-r all", so a
plain "check" or "check -r leaks" will not delete any data.)

Signed-off-by: Max Reitz <mreitz@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Message-id: 20191011152814.14791-14-mreitz@redhat.com
Signed-off-by: Max Reitz <mreitz@redhat.com>
This commit is contained in:
Max Reitz 2019-10-11 17:28:11 +02:00
parent 099febf3ac
commit d2b1d1ec73

View file

@ -444,6 +444,14 @@ int coroutine_fn qcow2_check_read_snapshot_table(BlockDriverState *bs,
s->snapshots_offset = be64_to_cpu(snapshot_table_pointer.snapshots_offset);
s->nb_snapshots = be32_to_cpu(snapshot_table_pointer.nb_snapshots);
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;
}
ret = qcow2_validate_table(bs, s->snapshots_offset, s->nb_snapshots,
sizeof(QCowSnapshotHeader),
sizeof(QCowSnapshotHeader) * QCOW_MAX_SNAPSHOTS,
@ -452,6 +460,12 @@ int coroutine_fn qcow2_check_read_snapshot_table(BlockDriverState *bs,
result->check_errors++;
error_reportf_err(local_err, "ERROR ");
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);
}
/* We did not read the snapshot table, so invalidate this information */
s->snapshots_offset = 0;
s->nb_snapshots = 0;