qcow2: Allow "full" discard
Normally, discarded sectors should read back as zero. However, there are cases in which a sector (or rather cluster) should be discarded as if they were never written in the first place, that is, reading them should fall through to the backing file again. Signed-off-by: Max Reitz <mreitz@redhat.com> Reviewed-by: Eric Blake <eblake@redhat.com> Reviewed-by: Kevin Wolf <kwolf@redhat.com> Message-id: 1414159063-25977-2-git-send-email-mreitz@redhat.com Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
This commit is contained in:
parent
70a5ff6bdd
commit
808c4b6f30
|
@ -1414,7 +1414,7 @@ int qcow2_decompress_cluster(BlockDriverState *bs, uint64_t cluster_offset)
|
||||||
* clusters.
|
* clusters.
|
||||||
*/
|
*/
|
||||||
static int discard_single_l2(BlockDriverState *bs, uint64_t offset,
|
static int discard_single_l2(BlockDriverState *bs, uint64_t offset,
|
||||||
unsigned int nb_clusters, enum qcow2_discard_type type)
|
unsigned int nb_clusters, enum qcow2_discard_type type, bool full_discard)
|
||||||
{
|
{
|
||||||
BDRVQcowState *s = bs->opaque;
|
BDRVQcowState *s = bs->opaque;
|
||||||
uint64_t *l2_table;
|
uint64_t *l2_table;
|
||||||
|
@ -1436,23 +1436,30 @@ static int discard_single_l2(BlockDriverState *bs, uint64_t offset,
|
||||||
old_l2_entry = be64_to_cpu(l2_table[l2_index + i]);
|
old_l2_entry = be64_to_cpu(l2_table[l2_index + i]);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Make sure that a discarded area reads back as zeroes for v3 images
|
* If full_discard is false, make sure that a discarded area reads back
|
||||||
* (we cannot do it for v2 without actually writing a zero-filled
|
* as zeroes for v3 images (we cannot do it for v2 without actually
|
||||||
* buffer). We can skip the operation if the cluster is already marked
|
* writing a zero-filled buffer). We can skip the operation if the
|
||||||
* as zero, or if it's unallocated and we don't have a backing file.
|
* 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
|
* TODO We might want to use bdrv_get_block_status(bs) here, but we're
|
||||||
* holding s->lock, so that doesn't work today.
|
* holding s->lock, so that doesn't work today.
|
||||||
|
*
|
||||||
|
* If full_discard is true, the sector should not read back as zeroes,
|
||||||
|
* but rather fall through to the backing file.
|
||||||
*/
|
*/
|
||||||
switch (qcow2_get_cluster_type(old_l2_entry)) {
|
switch (qcow2_get_cluster_type(old_l2_entry)) {
|
||||||
case QCOW2_CLUSTER_UNALLOCATED:
|
case QCOW2_CLUSTER_UNALLOCATED:
|
||||||
if (!bs->backing_hd) {
|
if (full_discard || !bs->backing_hd) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case QCOW2_CLUSTER_ZERO:
|
case QCOW2_CLUSTER_ZERO:
|
||||||
continue;
|
if (!full_discard) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
case QCOW2_CLUSTER_NORMAL:
|
case QCOW2_CLUSTER_NORMAL:
|
||||||
case QCOW2_CLUSTER_COMPRESSED:
|
case QCOW2_CLUSTER_COMPRESSED:
|
||||||
|
@ -1464,7 +1471,7 @@ static int discard_single_l2(BlockDriverState *bs, uint64_t offset,
|
||||||
|
|
||||||
/* First remove L2 entries */
|
/* First remove L2 entries */
|
||||||
qcow2_cache_entry_mark_dirty(s->l2_table_cache, l2_table);
|
qcow2_cache_entry_mark_dirty(s->l2_table_cache, l2_table);
|
||||||
if (s->qcow_version >= 3) {
|
if (!full_discard && s->qcow_version >= 3) {
|
||||||
l2_table[l2_index + i] = cpu_to_be64(QCOW_OFLAG_ZERO);
|
l2_table[l2_index + i] = cpu_to_be64(QCOW_OFLAG_ZERO);
|
||||||
} else {
|
} else {
|
||||||
l2_table[l2_index + i] = cpu_to_be64(0);
|
l2_table[l2_index + i] = cpu_to_be64(0);
|
||||||
|
@ -1483,7 +1490,7 @@ static int discard_single_l2(BlockDriverState *bs, uint64_t offset,
|
||||||
}
|
}
|
||||||
|
|
||||||
int qcow2_discard_clusters(BlockDriverState *bs, uint64_t offset,
|
int qcow2_discard_clusters(BlockDriverState *bs, uint64_t offset,
|
||||||
int nb_sectors, enum qcow2_discard_type type)
|
int nb_sectors, enum qcow2_discard_type type, bool full_discard)
|
||||||
{
|
{
|
||||||
BDRVQcowState *s = bs->opaque;
|
BDRVQcowState *s = bs->opaque;
|
||||||
uint64_t end_offset;
|
uint64_t end_offset;
|
||||||
|
@ -1506,7 +1513,7 @@ int qcow2_discard_clusters(BlockDriverState *bs, uint64_t offset,
|
||||||
|
|
||||||
/* Each L2 table is handled by its own loop iteration */
|
/* Each L2 table is handled by its own loop iteration */
|
||||||
while (nb_clusters > 0) {
|
while (nb_clusters > 0) {
|
||||||
ret = discard_single_l2(bs, offset, nb_clusters, type);
|
ret = discard_single_l2(bs, offset, nb_clusters, type, full_discard);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
goto fail;
|
goto fail;
|
||||||
}
|
}
|
||||||
|
|
|
@ -441,7 +441,7 @@ int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info)
|
||||||
qcow2_discard_clusters(bs, qcow2_vm_state_offset(s),
|
qcow2_discard_clusters(bs, qcow2_vm_state_offset(s),
|
||||||
align_offset(sn->vm_state_size, s->cluster_size)
|
align_offset(sn->vm_state_size, s->cluster_size)
|
||||||
>> BDRV_SECTOR_BITS,
|
>> BDRV_SECTOR_BITS,
|
||||||
QCOW2_DISCARD_NEVER);
|
QCOW2_DISCARD_NEVER, false);
|
||||||
|
|
||||||
#ifdef DEBUG_ALLOC
|
#ifdef DEBUG_ALLOC
|
||||||
{
|
{
|
||||||
|
|
|
@ -2089,7 +2089,7 @@ static coroutine_fn int qcow2_co_discard(BlockDriverState *bs,
|
||||||
|
|
||||||
qemu_co_mutex_lock(&s->lock);
|
qemu_co_mutex_lock(&s->lock);
|
||||||
ret = qcow2_discard_clusters(bs, sector_num << BDRV_SECTOR_BITS,
|
ret = qcow2_discard_clusters(bs, sector_num << BDRV_SECTOR_BITS,
|
||||||
nb_sectors, QCOW2_DISCARD_REQUEST);
|
nb_sectors, QCOW2_DISCARD_REQUEST, false);
|
||||||
qemu_co_mutex_unlock(&s->lock);
|
qemu_co_mutex_unlock(&s->lock);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
|
@ -534,7 +534,7 @@ uint64_t qcow2_alloc_compressed_cluster_offset(BlockDriverState *bs,
|
||||||
|
|
||||||
int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, QCowL2Meta *m);
|
int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, QCowL2Meta *m);
|
||||||
int qcow2_discard_clusters(BlockDriverState *bs, uint64_t offset,
|
int qcow2_discard_clusters(BlockDriverState *bs, uint64_t offset,
|
||||||
int nb_sectors, enum qcow2_discard_type type);
|
int nb_sectors, enum qcow2_discard_type type, bool full_discard);
|
||||||
int qcow2_zero_clusters(BlockDriverState *bs, uint64_t offset, int nb_sectors);
|
int qcow2_zero_clusters(BlockDriverState *bs, uint64_t offset, int nb_sectors);
|
||||||
|
|
||||||
int qcow2_expand_zero_clusters(BlockDriverState *bs);
|
int qcow2_expand_zero_clusters(BlockDriverState *bs);
|
||||||
|
|
Loading…
Reference in New Issue