qcow2: fix preallocation with metadata on bare block device
PATCH 1/1 qcow2: fix preallocation with metadata on bare block device
Create a qcow2 directly on bare block device with
"-o preallocation=metadata" option. When read this qcow2, it will return dirty data of block device. This patch add QCOW_OFLAG_ZERO for all preallocated l2 entry if the underlying device is a bare block device.
--- block/qcow2-cluster.c | 5 +++-- block/qcow2.c | 19 ++++++++++++++++--- block/qcow2.h | 3 ++- 3 files changed, 21 insertions(+), 6 deletions(-)
block/qcow2-cluster.c +3 -2
@@ -919,7 +919,8 @@ fail:return ret; } -int qcow2_alloc_cluster_link_l2 (BlockDriverState *bs,QCowL2Meta *m) +int qcow2_alloc_cluster_link_l2 (BlockDriverState *bs,QCowL2Meta *m, +uint64_t flags) {BDRVQcow2State *s = bs->opaque;int i, j =0 , l2_index, ret;
@@ -969,7 +970,7 @@ int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, QCowL2Meta *m) } l2_slice[l2_index + i] =cpu_to_be64 ((cluster_offset + - (i << s->cluster_bits)) |QCOW_OFLAG_COPIED ); + (i << s->cluster_bits)) |QCOW_OFLAG_COPIED | flags); }
block/qcow2.c +16 -3
@@ -2044,7 +2044,7 @@ static coroutine_fn int qcow2_co_pwritev(BlockDriverState *bs, uint64_t offset,while (l2meta !=NULL ) {QCowL2Meta *next; - ret =qcow2_alloc_cluster_link_l2 (bs, l2meta); + ret =qcow2_alloc_cluster_link_l2 (bs, l2meta,0 );if (ret <0 ) { goto fail; }
@@ -2534,6 +2534,7 @@ static void coroutine_fn preallocate_co(void *opaque)uint64_t host_offset =0 ;unsigned int cur_bytes;int ret; +struct stat st;QCowL2Meta *meta;qemu_co_mutex_lock (&s->lock);
@@ -2552,7 +2553,19 @@ static void coroutine_fn preallocate_co(void *opaque)while (meta) {QCowL2Meta *next = meta->next; - ret =qcow2_alloc_cluster_link_l2 (bs, meta); +/* Check the underlying device type. +* If the underlying device is a block device, we add +* QCOW_OFLAG_ZERO for all preallocated l2 entry to ignore dirty +* data on block device. +* If the underlying device can't be used with stat(return < 0), +* treat it as a regular file. +*/ +if (stat (bs->filename, &st) <0 || !S_ISBLK (st.st_mode)) { + ret =qcow2_alloc_cluster_link_l2 (bs, meta,0 ); + }else { + ret =qcow2_alloc_cluster_link_l2 (bs, meta,QCOW_OFLAG_ZERO ); + } +if (ret <0 ) {qcow2_free_any_clusters (bs, meta->alloc_offset, meta->nb_clusters,QCOW2_DISCARD_NEVER );
@@ -3458,7 +3471,7 @@ static int qcow2_truncate(BlockDriverState *bs, int64_t offset, };qemu_co_queue_init (&allocation.dependent_requests); - ret =qcow2_alloc_cluster_link_l2 (bs, &allocation); + ret =qcow2_alloc_cluster_link_l2 (bs, &allocation,0 );if (ret <0 ) {error_setg_errno (errp, -ret,"Failed to update L2 tables" ); qcow2_free_clusters(bs, host_offset,
block/qcow2.h +2 -1
@@ -617,7 +617,8 @@ uint64_t qcow2_alloc_compressed_cluster_offset(BlockDriverState *bs,uint64_t offset, int compressed_size); -int qcow2_alloc_cluster_link_l2 (BlockDriverState *bs,QCowL2Meta *m); +int qcow2_alloc_cluster_link_l2 (BlockDriverState *bs,QCowL2Meta *m, +uint64_t flags);int qcow2_cluster_discard (BlockDriverState *bs,uint64_t offset,uint64_t bytes,enum qcow2_discard_type type,bool full_discard);
Signature · 1 line
1.8.3.1
ping for review
Quoted 115 lines · Ivan Ren
Create a qcow2 directly on bare block device with
"-o preallocation=metadata" option. When read this qcow2, it will return dirty data of block device. This patch addQCOW_OFLAG_ZEROfor all preallocated l2 entry if the underlying device is a bare block device.Signed-off-by Ivan Ren <[email protected]>--- block/qcow2-cluster.c | 5 +++-- block/qcow2.c | 19 ++++++++++++++++--- block/qcow2.h | 3 ++- 3 files changed, 21 insertions(+), 6 deletions(-)block/qcow2-cluster.c +3
-2@@ -919,7 +919,8 @@ fail:return ret; } -int qcow2_alloc_cluster_link_l2 (BlockDriverState *bs,QCowL2Meta *m) +int qcow2_alloc_cluster_link_l2 (BlockDriverState *bs,QCowL2Meta *m, +uint64_t flags) {BDRVQcow2State *s = bs->opaque;int i, j =0 , l2_index, ret;@@ -969,7 +970,7 @@ int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, QCowL2Meta *m) } l2_slice[l2_index + i] =cpu_to_be64 ((cluster_offset + - (i << s->cluster_bits)) |QCOW_OFLAG_COPIED ); + (i << s->cluster_bits)) |QCOW_OFLAG_COPIED | flags); }block/qcow2.c +1
-1@@ -2044,7 +2044,7 @@ static coroutine_fn int qcow2_co_pwritev(BlockDriverState *bs, uint64_t offset, while (l2meta != NULL) { QCowL2Meta *next; - ret = qcow2_alloc_cluster_link_l2(bs, l2meta); + ret = qcow2_alloc_cluster_link_l2(bs, l2meta, 0); if (ret < 0) { goto fail;}block/qcow2.c +4
-1@@ -2534,6 +2534,7 @@ static void coroutine_fn preallocate_co(void *opaque)uint64_t host_offset =0 ;unsigned int cur_bytes;int ret; +struct stat st;QCowL2Meta *meta;qemu_co_mutex_lock (&s->lock);@@ -2552,7 +2553,19 @@ static void coroutine_fn preallocate_co(void *opaque)while (meta) {QCowL2Meta *next = meta->next; - ret =qcow2_alloc_cluster_link_l2 (bs, meta); +/* Check the underlying device type. +* If the underlying device is a block device, we add +* QCOW_OFLAG_ZERO for all preallocated l2 entry to ignore dirty
block/qcow2.c +2
-0+* data on block device. +* If the underlying device can't be used with stat(return < 0),
block/qcow2.c +6
-0+* treat it as a regular file. +*/ +if (stat (bs->filename, &st) <0 || !S_ISBLK (st.st_mode)) { + ret =qcow2_alloc_cluster_link_l2 (bs, meta,0 ); + }else { + ret = qcow2_alloc_cluster_link_l2(bs, meta,
QCOW_OFLAG_ZERO);block/qcow2.c +2
-0+ } +if (ret <0 ) { qcow2_free_any_clusters(bs, meta->alloc_offset, meta->nb_clusters,
QCOW2_DISCARD_NEVER);block/qcow2.c +1
-1@@ -3458,7 +3471,7 @@ static int qcow2_truncate(BlockDriverState *bs, int64_t offset, };qemu_co_queue_init (&allocation.dependent_requests); - ret =qcow2_alloc_cluster_link_l2 (bs, &allocation); + ret =qcow2_alloc_cluster_link_l2 (bs, &allocation,0 );if (ret <0 ) { error_setg_errno(errp, -ret, "Failed to update L2tables");
qcow2_free_clusters(bs,host_offset,block/qcow2.h +2
-1@@ -617,7 +617,8 @@ uint64_tqcow2_alloc_compressed_cluster_offset (BlockDriverState *bs, uint64_t offset, int compressed_size); -int qcow2_alloc_cluster_link_l2 (BlockDriverState *bs,QCowL2Meta *m); +int qcow2_alloc_cluster_link_l2 (BlockDriverState *bs,QCowL2Meta *m, +uint64_t flags); int qcow2_cluster_discard(BlockDriverState *bs, uint64_t offset, uint64_t bytes, enum qcow2_discard_type type,bool full_discard);--
1.8.3.1
Quoted 3 lines · Ivan Ren
Create a qcow2 directly on bare block device with
"-o preallocation=metadata" option. When read this qcow2, it will return dirty data of block device.
Yes, reading garbage is expected.
Quoted 3 lines
This patch add
QCOW_OFLAG_ZERO
for all preallocated l2 entry if the underlying device is a bare block device.
This says what the patch does, but not why. What is the actual use case scenario where changing semantics to have the qcow2 overwrite the garbage to read 0 instead of any pre-existing garbage, when dealing with portions of the disk that have not yet been written by the guest? Are you trying to prevent a security leak of previous information that may be resident on the block device?
Quoted 55 lines
Signed-off-by Ivan Ren <[email protected]>--- block/qcow2-cluster.c | 5 +++-- block/qcow2.c | 19 ++++++++++++++++--- block/qcow2.h | 3 ++- 3 files changed, 21 insertions(+), 6 deletions(-)block/qcow2-cluster.c +3
-2@@ -919,7 +919,8 @@ fail:return ret; } -int qcow2_alloc_cluster_link_l2 (BlockDriverState *bs,QCowL2Meta *m) +int qcow2_alloc_cluster_link_l2 (BlockDriverState *bs,QCowL2Meta *m, +uint64_t flags) {BDRVQcow2State *s = bs->opaque;int i, j =0 , l2_index, ret;@@ -969,7 +970,7 @@ int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, QCowL2Meta *m) } l2_slice[l2_index + i] =cpu_to_be64 ((cluster_offset + - (i << s->cluster_bits)) |QCOW_OFLAG_COPIED ); + (i << s->cluster_bits)) |QCOW_OFLAG_COPIED | flags); }block/qcow2.c +2
-1@@ -2044,7 +2044,7 @@ static coroutine_fn int qcow2_co_pwritev(BlockDriverState *bs, uint64_t offset,while (l2meta !=NULL ) {QCowL2Meta *next; - ret =qcow2_alloc_cluster_link_l2 (bs, l2meta); + ret =qcow2_alloc_cluster_link_l2 (bs, l2meta,0 );if (ret <0 ) { goto fail; }@@ -2534,6 +2534,7 @@ static void coroutine_fn preallocate_co(void *opaque)uint64_t host_offset =0 ;unsigned int cur_bytes;int ret; +struct stat st;QCowL2Meta *meta;qemu_co_mutex_lock (&s->lock);@@ -2552,7 +2553,19 @@ static void coroutine_fn preallocate_co(void *opaque)while (meta) {QCowL2Meta *next = meta->next;
block/qcow2.c static void coroutine_fn preallocate_co(void *opaque) · line 2563
Quoted 10 lines
block/qcow2.c +8
-1- ret =qcow2_alloc_cluster_link_l2 (bs, meta); +/* Check the underlying device type. +* If the underlying device is a block device, we add +* QCOW_OFLAG_ZERO for all preallocated l2 entry to ignore dirty +* data on block device. +* If the underlying device can't be used with stat(return < 0), +* treat it as a regular file. +*/ +if (stat (bs->filename, &st) <0 || !S_ISBLK (st.st_mode)) {
Won't work. You cannot guarantee that bs->filename is a local file; it could be a remote protocol, such as NBD or gluster. If you need to know whether bs->filename has a property that it reads as all zeroes when first initialized, we already have BlockDriverInfo::unallocated_blocks_are_zero, which is set to true for regular files and false for block devices.
block/qcow2.c static void coroutine_fn preallocate_co(void *opaque) · line 2566
Quoted 3 lines
block/qcow2.c +3
-0+ ret =qcow2_alloc_cluster_link_l2 (bs, meta,0 ); + }else { + ret =qcow2_alloc_cluster_link_l2 (bs, meta,QCOW_OFLAG_ZERO );
Why would we not want to pass QCOW_OFLAG_ZERO always, rather than special-casing it based on what the underlying protocol might already have in place?
Quoted 29 lines
block/qcow2.c +3
-1+ } +if (ret <0 ) {qcow2_free_any_clusters (bs, meta->alloc_offset, meta->nb_clusters,QCOW2_DISCARD_NEVER );@@ -3458,7 +3471,7 @@ static int qcow2_truncate(BlockDriverState *bs, int64_t offset, };qemu_co_queue_init (&allocation.dependent_requests); - ret =qcow2_alloc_cluster_link_l2 (bs, &allocation); + ret =qcow2_alloc_cluster_link_l2 (bs, &allocation,0 );if (ret <0 ) {error_setg_errno (errp, -ret,"Failed to update L2 tables" ); qcow2_free_clusters(bs, host_offset,block/qcow2.h +2
-1@@ -617,7 +617,8 @@ uint64_t qcow2_alloc_compressed_cluster_offset(BlockDriverState *bs,uint64_t offset, int compressed_size); -int qcow2_alloc_cluster_link_l2 (BlockDriverState *bs,QCowL2Meta *m); +int qcow2_alloc_cluster_link_l2 (BlockDriverState *bs,QCowL2Meta *m, +uint64_t flags);int qcow2_cluster_discard (BlockDriverState *bs,uint64_t offset,uint64_t bytes,enum qcow2_discard_type type,bool full_discard);
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
Quoted 6 lines
This says what the patch does, but not why. What is the actual use case scenario where changing semantics to have the qcow2 overwrite the garbage to read 0 instead of any pre-existing garbage, when dealing with portions of the disk that have not yet been written by the guest? Are you trying to prevent a security leak of previous information that may be resident on the block device?
Yes, any pre-existing data on the device should not be read, otherwise it may lead security leak in cloud environment.
For metadata preallocated qcow2 on bare block device, I think QCOW_OFLAG_ZERO is more suitable to prevent this problem than other(such as do "dd" before create a preallocated qcow2).
Quoted 3 lines
block/qcow2.c static void coroutine_fn preallocate_co(void *opaque)
Quoted 3 lines
block/qcow2.c +3
-0+/* Check the underlying device type. +* If the underlying device is a block device, we add +* QCOW_OFLAG_ZERO for all preallocated l2 entry to ignore
dirty
Quoted 2 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ * data on block device. + * If the underlying device can't be used with stat(return
< 0),
Quoted 10 lines
block/qcow2.c static void coroutine_fn preallocate_co(void *opaque) · line 2563
Quoted 3 lines
block/qcow2.c +3
-0+* treat it as a regular file. +*/ +if (stat (bs->filename, &st) <0 || !S_ISBLK (st.st_mode)) {Won't work. You cannot guarantee that
bs->filenameis a local file; it could be a remote protocol, such as NBD or gluster. If you need to know whetherbs->filenamehas a property that it reads as all zeroes when first initialized, we already haveBlockDriverInfo::unallocated_blocks_are_zero, which is set to true for regular files and false for block devices.
Yes, BlockDriverInfo::unallocated_blocks_are_zero is enough and better to distinguish the device.
Quoted 3 lines
block/qcow2.c static void coroutine_fn preallocate_co(void *opaque)
Quoted 3 lines
block/qcow2.c +3
-0+ ret =qcow2_alloc_cluster_link_l2 (bs, meta,0 ); + }else { + ret = qcow2_alloc_cluster_link_l2(bs, meta,
QCOW_OFLAG_ZERO);
Quoted 4 lines
Why would we not want to pass
QCOW_OFLAG_ZEROalways, rather than special-casing it based on what the underlying protocol might already have in place?
Yea, it sounds good. Always pass QCOW_OFLAG_ZERO in preallocation has no problem and can guarantee no garbage will be read when preallcate metadata for qcow2 on any underlying device.
I will send a v2 patch.
Thanks.
Quoted 146 lines · Eric Blake
Quoted 3 lines · Ivan Ren
Create a qcow2 directly on bare block device with
"-o preallocation=metadata" option. When read this qcow2, it will return dirty data of block device.Yes, reading garbage is expected.
Quoted 3 lines
This patch add
QCOW_OFLAG_ZERO
for all preallocated l2 entry if the underlying device is a bare block device.This says what the patch does, but not why. What is the actual use case scenario where changing semantics to have the qcow2 overwrite the garbage to read 0 instead of any pre-existing garbage, when dealing with portions of the disk that have not yet been written by the guest? Are you trying to prevent a security leak of previous information that may be resident on the block device?
block/qcow2-cluster.c int qcow2_alloc_cluster_link_l2(BlockDriverState
Quoted 23 lines
Signed-off-by Ivan Ren <[email protected]>--- block/qcow2-cluster.c | 5 +++-- block/qcow2.c | 19 ++++++++++++++++--- block/qcow2.h | 3 ++- 3 files changed, 21 insertions(+), 6 deletions(-)block/qcow2-cluster.c +2
-1@@ -919,7 +919,8 @@ fail:return ret; } -int qcow2_alloc_cluster_link_l2 (BlockDriverState *bs,QCowL2Meta *m) +int qcow2_alloc_cluster_link_l2 (BlockDriverState *bs,QCowL2Meta *m, +uint64_t flags) {BDRVQcow2State *s = bs->opaque;int i, j =0 , l2_index, ret;@@ -969,7 +970,7 @@ int qcow2_alloc_cluster_link_l2(BlockDriverState*bs, QCowL2Meta *m)
block/qcow2-cluster.c int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, QCowL2Meta *m)
Quoted 5 lines
block/qcow2-cluster.c +1
-1} l2_slice[l2_index + i] = cpu_to_be64((cluster_offset + - (i << s->cluster_bits)) |QCOW_OFLAG_COPIED ); + (i << s->cluster_bits)) | QCOW_OFLAG_COPIED |flags);
block/qcow2.c static coroutine_fn int
Quoted 8 lines
block/qcow2-cluster.c +0
-0}block/qcow2.c +0
-0@@ -2044,7 +2044,7 @@ static coroutine_fn intqcow2_co_pwritev(BlockDriverState *bs,
uint64_toffset,block/qcow2.c static void coroutine_fn preallocate_co(void
Quoted 9 lines
block/qcow2.c +1
-1while (l2meta !=NULL ) {QCowL2Meta *next; - ret =qcow2_alloc_cluster_link_l2 (bs, l2meta); + ret =qcow2_alloc_cluster_link_l2 (bs, l2meta,0 );if (ret <0 ) { goto fail; }@@ -2534,6 +2534,7 @@ static void coroutine_fn preallocate_co(void*opaque)
block/qcow2.c static void coroutine_fn preallocate_co(void
Quoted 8 lines
block/qcow2.c +1
-0uint64_t host_offset =0 ;unsigned int cur_bytes;int ret; +struct stat st;QCowL2Meta *meta;qemu_co_mutex_lock (&s->lock);@@ -2552,7 +2553,19 @@ static void coroutine_fn preallocate_co(void*opaque)
block/qcow2.c static void coroutine_fn preallocate_co(void *opaque)
Quoted 7 lines
block/qcow2.c +3
-1while (meta) {QCowL2Meta *next = meta->next; - ret =qcow2_alloc_cluster_link_l2 (bs, meta); +/* Check the underlying device type. +* If the underlying device is a block device, we add +* QCOW_OFLAG_ZERO for all preallocated l2 entry to ignore dirty
block/qcow2.c static void coroutine_fn preallocate_co(void · line 2560
Quoted 2 lines
block/qcow2.c +2
-0+* data on block device. +* If the underlying device can't be used with stat(return < 0),
block/qcow2.c static void coroutine_fn preallocate_co(void *opaque) · line 2563
Quoted 3 lines
block/qcow2.c +3
-0+* treat it as a regular file. +*/ +if (stat (bs->filename, &st) <0 || !S_ISBLK (st.st_mode)) {Won't work. You cannot guarantee that
bs->filenameis a local file; it could be a remote protocol, such as NBD or gluster. If you need to know whetherbs->filenamehas a property that it reads as all zeroes when first initialized, we already haveBlockDriverInfo::unallocated_blocks_are_zero, which is set to true for regular files and false for block devices.block/qcow2.c static void coroutine_fn preallocate_co(void *opaque)
Quoted 3 lines
block/qcow2.c +3
-0+ ret =qcow2_alloc_cluster_link_l2 (bs, meta,0 ); + }else { + ret = qcow2_alloc_cluster_link_l2(bs, meta,
QCOW_OFLAG_ZERO);Why would we not want to pass
QCOW_OFLAG_ZEROalways, rather than special-casing it based on what the underlying protocol might already have in place?block/qcow2.c static void coroutine_fn preallocate_co(void *opaque)
Quoted 5 lines
block/qcow2.c +2
-0+ } +if (ret <0 ) { qcow2_free_any_clusters(bs, meta->alloc_offset, meta->nb_clusters,
QCOW2_DISCARD_NEVER);block/qcow2.c static int qcow2_truncate(BlockDriverState *bs,
Quoted 1 line
block/qcow2.c +0
-0@@ -3458,7 +3471,7 @@ static int qcow2_truncate(BlockDriverState *bs,
int64_toffset,block/qcow2.c static int qcow2_truncate(BlockDriverState *bs, int64_t offset,
Quoted 7 lines
block/qcow2.c +1
-1};qemu_co_queue_init (&allocation.dependent_requests); - ret =qcow2_alloc_cluster_link_l2 (bs, &allocation); + ret =qcow2_alloc_cluster_link_l2 (bs, &allocation,0 );if (ret <0 ) { error_setg_errno(errp, -ret, "Failed to update L2tables");
block/qcow2.h uint64_t
Quoted 6 lines
block/qcow2.c +0
-0qcow2_free_clusters(bs, host_offset,block/qcow2.h +0
-0@@ -617,7 +617,8 @@ uint64_tqcow2_alloc_compressed_cluster_offset(BlockDriverState *bs,
block/qcow2.h uint64_t qcow2_alloc_compressed_cluster_offset(BlockDriverState *bs, · line 624
Quoted 10 lines
block/qcow2.h +2
-1uint64_t offset, int compressed_size); -int qcow2_alloc_cluster_link_l2 (BlockDriverState *bs,QCowL2Meta *m); +int qcow2_alloc_cluster_link_l2 (BlockDriverState *bs,QCowL2Meta *m, +uint64_t flags);int qcow2_cluster_discard (BlockDriverState *bs,uint64_t offset,uint64_t bytes,enum qcow2_discard_type type,bool full_discard);--
Eric Blake, Principal Software Engineer
Red Hat, Inc. +1-919-301-3266
Virtualization: qemu.org | libvirt.org