[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PULL 24/48] qcow2: Zero-initialise first cluster for new i
From: |
Stefan Hajnoczi |
Subject: |
[Qemu-devel] [PULL 24/48] qcow2: Zero-initialise first cluster for new images |
Date: |
Fri, 6 Dec 2013 17:36:23 +0100 |
From: Kevin Wolf <address@hidden>
Strictly speaking, this is only required for has_zero_init() == false,
but it's easy enough to just do a cluster-aligned write that is padded
with zeros after the header.
This fixes that after 'qemu-img create' header extensions are attempted
to be parsed that are really just random leftover data.
Cc: address@hidden
Signed-off-by: Kevin Wolf <address@hidden>
Reviewed-by: Fam Zheng <address@hidden>
Reviewed-by: Paolo Bonzini <address@hidden>
Signed-off-by: Stefan Hajnoczi <address@hidden>
---
block/qcow2.c | 36 ++++++++++++++++++++----------------
1 file changed, 20 insertions(+), 16 deletions(-)
diff --git a/block/qcow2.c b/block/qcow2.c
index 19e0374..f29aa88 100644
--- a/block/qcow2.c
+++ b/block/qcow2.c
@@ -1472,7 +1472,7 @@ static int qcow2_create2(const char *filename, int64_t
total_size,
* size for any qcow2 image.
*/
BlockDriverState* bs;
- QCowHeader header;
+ QCowHeader *header;
uint8_t* refcount_table;
Error *local_err = NULL;
int ret;
@@ -1490,30 +1490,34 @@ static int qcow2_create2(const char *filename, int64_t
total_size,
}
/* Write the header */
- memset(&header, 0, sizeof(header));
- header.magic = cpu_to_be32(QCOW_MAGIC);
- header.version = cpu_to_be32(version);
- header.cluster_bits = cpu_to_be32(cluster_bits);
- header.size = cpu_to_be64(0);
- header.l1_table_offset = cpu_to_be64(0);
- header.l1_size = cpu_to_be32(0);
- header.refcount_table_offset = cpu_to_be64(cluster_size);
- header.refcount_table_clusters = cpu_to_be32(1);
- header.refcount_order = cpu_to_be32(3 + REFCOUNT_SHIFT);
- header.header_length = cpu_to_be32(sizeof(header));
+ QEMU_BUILD_BUG_ON((1 << MIN_CLUSTER_BITS) < sizeof(*header));
+ header = g_malloc0(cluster_size);
+ *header = (QCowHeader) {
+ .magic = cpu_to_be32(QCOW_MAGIC),
+ .version = cpu_to_be32(version),
+ .cluster_bits = cpu_to_be32(cluster_bits),
+ .size = cpu_to_be64(0),
+ .l1_table_offset = cpu_to_be64(0),
+ .l1_size = cpu_to_be32(0),
+ .refcount_table_offset = cpu_to_be64(cluster_size),
+ .refcount_table_clusters = cpu_to_be32(1),
+ .refcount_order = cpu_to_be32(3 + REFCOUNT_SHIFT),
+ .header_length = cpu_to_be32(sizeof(*header)),
+ };
if (flags & BLOCK_FLAG_ENCRYPT) {
- header.crypt_method = cpu_to_be32(QCOW_CRYPT_AES);
+ header->crypt_method = cpu_to_be32(QCOW_CRYPT_AES);
} else {
- header.crypt_method = cpu_to_be32(QCOW_CRYPT_NONE);
+ header->crypt_method = cpu_to_be32(QCOW_CRYPT_NONE);
}
if (flags & BLOCK_FLAG_LAZY_REFCOUNTS) {
- header.compatible_features |=
+ header->compatible_features |=
cpu_to_be64(QCOW2_COMPAT_LAZY_REFCOUNTS);
}
- ret = bdrv_pwrite(bs, 0, &header, sizeof(header));
+ ret = bdrv_pwrite(bs, 0, header, cluster_size);
+ g_free(header);
if (ret < 0) {
error_setg_errno(errp, -ret, "Could not write qcow2 header");
goto out;
--
1.8.4.2
- [Qemu-devel] [PULL 13/48] block/iscsi: remove .bdrv_has_zero_init, (continued)
- [Qemu-devel] [PULL 13/48] block/iscsi: remove .bdrv_has_zero_init, Stefan Hajnoczi, 2013/12/06
- [Qemu-devel] [PULL 14/48] block/iscsi: updated copyright, Stefan Hajnoczi, 2013/12/06
- [Qemu-devel] [PULL 16/48] raw-posix: implement write_zeroes with MAY_UNMAP for files, Stefan Hajnoczi, 2013/12/06
- [Qemu-devel] [PULL 17/48] raw-posix: implement write_zeroes with MAY_UNMAP for block devices, Stefan Hajnoczi, 2013/12/06
- [Qemu-devel] [PULL 18/48] raw-posix: add support for write_zeroes on XFS and block devices, Stefan Hajnoczi, 2013/12/06
- [Qemu-devel] [PULL 19/48] qemu-iotests: 033 is fast, Stefan Hajnoczi, 2013/12/06
- [Qemu-devel] [PULL 20/48] scsi-disk: catch write protection errors in UNMAP, Stefan Hajnoczi, 2013/12/06
- [Qemu-devel] [PULL 21/48] scsi-disk: reject ANCHOR=1 for UNMAP and WRITE SAME commands, Stefan Hajnoczi, 2013/12/06
- [Qemu-devel] [PULL 22/48] scsi-disk: correctly implement WRITE SAME, Stefan Hajnoczi, 2013/12/06
- [Qemu-devel] [PULL 23/48] block: Close backing file early in bdrv_img_create, Stefan Hajnoczi, 2013/12/06
- [Qemu-devel] [PULL 24/48] qcow2: Zero-initialise first cluster for new images,
Stefan Hajnoczi <=
- [Qemu-devel] [PULL 25/48] qemu-iotests: Add "-c <cache-mode>" option, Stefan Hajnoczi, 2013/12/06
- [Qemu-devel] [PULL 26/48] qemu-iotests: Honour cache mode in iotests.py, Stefan Hajnoczi, 2013/12/06
- [Qemu-devel] [PULL 27/48] qemu-iotests: Add _default_cache_mode and _supported_cache_modes, Stefan Hajnoczi, 2013/12/06
- [Qemu-devel] [PULL 28/48] qemu-iotests: Change default cache mode to "writeback", Stefan Hajnoczi, 2013/12/06
- [Qemu-devel] [PULL 29/48] qemu-iotests: Clean up spaces in usage output, Stefan Hajnoczi, 2013/12/06
- [Qemu-devel] [PULL 30/48] qemu-iotests: Split qcow2 only cases in 048, Stefan Hajnoczi, 2013/12/06
- [Qemu-devel] [PULL 32/48] qemu-nbd: support internal snapshot export, Stefan Hajnoczi, 2013/12/06
- [Qemu-devel] [PULL 33/48] qemu-iotests: add 058 internal snapshot export with qemu-nbd case, Stefan Hajnoczi, 2013/12/06
- [Qemu-devel] [PULL 34/48] qemu-img: add -l for snapshot in convert, Stefan Hajnoczi, 2013/12/06
- [Qemu-devel] [PULL 35/48] qemu-iotests: add test for snapshot in qemu-img convert, Stefan Hajnoczi, 2013/12/06