summaryrefslogtreecommitdiff
path: root/block/qcow2-cluster.c
diff options
context:
space:
mode:
authorKevin Wolf <kwolf@redhat.com>2014-02-08 14:38:33 +0100
committerKevin Wolf <kwolf@redhat.com>2014-02-21 21:02:21 +0100
commita71835a0ccff168b19ffc9656fe27988821ec59a (patch)
tree9d328538c78f9be36db90eb4dff9c8d4b3de2e77 /block/qcow2-cluster.c
parent3e890c77cf038d8c2de66ed7996fe77a6f94787c (diff)
downloadqemu-a71835a0ccff168b19ffc9656fe27988821ec59a.tar.gz
qemu-a71835a0ccff168b19ffc9656fe27988821ec59a.tar.bz2
qemu-a71835a0ccff168b19ffc9656fe27988821ec59a.zip
qcow2: Set zero flag for discarded clusters
Instead of making the backing file contents visible again after a discard request, set the zero flag if possible (i.e. on version >= 3). Signed-off-by: Kevin Wolf <kwolf@redhat.com> Reviewed-by: Eric Blake <eblake@redhat.com>
Diffstat (limited to 'block/qcow2-cluster.c')
-rw-r--r--block/qcow2-cluster.c22
1 files changed, 20 insertions, 2 deletions
diff --git a/block/qcow2-cluster.c b/block/qcow2-cluster.c
index c57f39dd2b..36c1bed350 100644
--- a/block/qcow2-cluster.c
+++ b/block/qcow2-cluster.c
@@ -1367,13 +1367,31 @@ static int discard_single_l2(BlockDriverState *bs, uint64_t offset,
uint64_t old_offset;
old_offset = be64_to_cpu(l2_table[l2_index + i]);
- if ((old_offset & L2E_OFFSET_MASK) == 0) {
+
+ /*
+ * Make sure that a discarded area reads back as zeroes for v3 images
+ * (we cannot do it for v2 without actually writing a zero-filled
+ * buffer). We can skip the operation if the cluster is already marked
+ * as zero, or if it's unallocated and we don't have a backing file.
+ *
+ * TODO We might want to use bdrv_get_block_status(bs) here, but we're
+ * holding s->lock, so that doesn't work today.
+ */
+ if (old_offset & QCOW_OFLAG_ZERO) {
+ continue;
+ }
+
+ if ((old_offset & L2E_OFFSET_MASK) == 0 && !bs->backing_hd) {
continue;
}
/* First remove L2 entries */
qcow2_cache_entry_mark_dirty(s->l2_table_cache, l2_table);
- l2_table[l2_index + i] = cpu_to_be64(0);
+ if (s->qcow_version >= 3) {
+ l2_table[l2_index + i] = cpu_to_be64(QCOW_OFLAG_ZERO);
+ } else {
+ l2_table[l2_index + i] = cpu_to_be64(0);
+ }
/* Then decrease the refcount */
qcow2_free_any_clusters(bs, old_offset, 1, type);