diff options
author | Max Reitz <mreitz@redhat.com> | 2013-09-27 10:21:48 +0200 |
---|---|---|
committer | Kevin Wolf <kwolf@redhat.com> | 2013-09-27 17:22:43 +0200 |
commit | 320c70666687db4dd4df8165f9fe6960de782ca9 (patch) | |
tree | 2e992ef44883af06a0c9ad5da19864cc3a09c9bc | |
parent | be0b742ee320d1139d57062fa18490e7aa485f2a (diff) |
qcow2: Free only newly allocated clusters on error
In expand_zero_clusters_in_l1, a new cluster is only allocated if it was
not already preallocated. On error, such preallocated clusters should
not be freed, but only the newly allocated ones.
Signed-off-by: Max Reitz <mreitz@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
-rw-r--r-- | block/qcow2-cluster.c | 16 |
1 files changed, 10 insertions, 6 deletions
diff --git a/block/qcow2-cluster.c b/block/qcow2-cluster.c index c743db1c01..91d07f2203 100644 --- a/block/qcow2-cluster.c +++ b/block/qcow2-cluster.c @@ -1554,6 +1554,7 @@ static int expand_zero_clusters_in_l1(BlockDriverState *bs, uint64_t *l1_table, uint64_t l2_entry = be64_to_cpu(l2_table[j]); int64_t offset = l2_entry & L2E_OFFSET_MASK, cluster_index; int cluster_type = qcow2_get_cluster_type(l2_entry); + bool preallocated = offset != 0; if (cluster_type == QCOW2_CLUSTER_NORMAL) { cluster_index = offset >> s->cluster_bits; @@ -1579,8 +1580,7 @@ static int expand_zero_clusters_in_l1(BlockDriverState *bs, uint64_t *l1_table, continue; } - if (!offset) { - /* not preallocated */ + if (!preallocated) { if (!bs->backing_hd) { /* not backed; therefore we can simply deallocate the * cluster */ @@ -1599,16 +1599,20 @@ static int expand_zero_clusters_in_l1(BlockDriverState *bs, uint64_t *l1_table, ret = qcow2_pre_write_overlap_check(bs, QCOW2_OL_DEFAULT, offset, s->cluster_size); if (ret < 0) { - qcow2_free_clusters(bs, offset, s->cluster_size, - QCOW2_DISCARD_ALWAYS); + if (!preallocated) { + qcow2_free_clusters(bs, offset, s->cluster_size, + QCOW2_DISCARD_ALWAYS); + } goto fail; } ret = bdrv_write_zeroes(bs->file, offset / BDRV_SECTOR_SIZE, s->cluster_sectors); if (ret < 0) { - qcow2_free_clusters(bs, offset, s->cluster_size, - QCOW2_DISCARD_ALWAYS); + if (!preallocated) { + qcow2_free_clusters(bs, offset, s->cluster_size, + QCOW2_DISCARD_ALWAYS); + } goto fail; } |