aboutsummaryrefslogtreecommitdiff
path: root/block/qcow2.c
diff options
context:
space:
mode:
authorStefan Hajnoczi <stefanha@redhat.com>2013-04-15 17:17:31 +0200
committerKevin Wolf <kwolf@redhat.com>2013-04-22 10:27:58 +0200
commitf4d38bef7cc79018e2aa789b0e4c23c3a8cdfca5 (patch)
treea83c3003f7b316626becabac10cfce1390def876 /block/qcow2.c
parent68c0aa6e02f79f8825c0c5dc4c7ed25d524aaa8b (diff)
qcow2: allow sub-cluster compressed write to last cluster
Compression in qcow2 requires image length to be a multiple of the cluster size. Lift this requirement by zero-padding the final cluster when necessary. The virtual disk size is still not cluster-aligned, so the guest cannot access the zero sectors. Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com> Signed-off-by: Kevin Wolf <kwolf@redhat.com>
Diffstat (limited to 'block/qcow2.c')
-rw-r--r--block/qcow2.c17
1 files changed, 15 insertions, 2 deletions
diff --git a/block/qcow2.c b/block/qcow2.c
index e8934de185..2e346d8c42 100644
--- a/block/qcow2.c
+++ b/block/qcow2.c
@@ -1537,8 +1537,21 @@ static int qcow2_write_compressed(BlockDriverState *bs, int64_t sector_num,
return 0;
}
- if (nb_sectors != s->cluster_sectors)
- return -EINVAL;
+ if (nb_sectors != s->cluster_sectors) {
+ ret = -EINVAL;
+
+ /* Zero-pad last write if image size is not cluster aligned */
+ if (sector_num + nb_sectors == bs->total_sectors &&
+ nb_sectors < s->cluster_sectors) {
+ uint8_t *pad_buf = qemu_blockalign(bs, s->cluster_size);
+ memset(pad_buf, 0, s->cluster_size);
+ memcpy(pad_buf, buf, nb_sectors * BDRV_SECTOR_SIZE);
+ ret = qcow2_write_compressed(bs, sector_num,
+ pad_buf, s->cluster_sectors);
+ qemu_vfree(pad_buf);
+ }
+ return ret;
+ }
out_buf = g_malloc(s->cluster_size + (s->cluster_size / 1000) + 128);