Another step on our continuing quest to switch to byte-based interfaces. There are still opportunities to optimize the qcow2 handling of zero clusters. For example, if the backing file only has non-zero data in the portion about to be overwritten, then we could widen the request and make the entire cluster zero, rather than falling back to -ENOTSUP. But for this patch, intentionally leave the semantics unchanged, even if not optimal.
Signed-off-by: Eric Blake <ebl...@redhat.com> --- block/qcow2.c | 46 +++++++++++++++++++++++++--------------------- 1 file changed, 25 insertions(+), 21 deletions(-) diff --git a/block/qcow2.c b/block/qcow2.c index 978694e..3522fc0 100644 --- a/block/qcow2.c +++ b/block/qcow2.c @@ -2428,43 +2428,47 @@ static bool is_zero_cluster_top_locked(BlockDriverState *bs, int64_t start) return ret == QCOW2_CLUSTER_UNALLOCATED || ret == QCOW2_CLUSTER_ZERO; } -static coroutine_fn int qcow2_co_write_zeroes(BlockDriverState *bs, - int64_t sector_num, int nb_sectors, BdrvRequestFlags flags) +static coroutine_fn int qcow2_co_pwrite_zeroes(BlockDriverState *bs, + int64_t offset, int count, BdrvRequestFlags flags) { int ret; BDRVQcow2State *s = bs->opaque; - int head = sector_num % s->cluster_sectors; - int tail = (sector_num + nb_sectors) % s->cluster_sectors; + int head = offset % s->cluster_size; + int tail = (offset + count) % s->cluster_size; + /* Widen the write to a full cluster, if the cluster already reads + * as zero. */ if (head != 0 || tail != 0) { - int64_t cl_end = -1; + int64_t tail_sector = 0; - sector_num -= head; - nb_sectors += head; - - if (tail != 0) { - nb_sectors += s->cluster_sectors - tail; + offset -= head; + count += head; + if (tail) { + count += s->cluster_size - tail; } - if (!is_zero_cluster(bs, sector_num)) { + if (!is_zero_cluster(bs, offset >> BDRV_SECTOR_BITS)) { return -ENOTSUP; } - if (nb_sectors > s->cluster_sectors) { - /* Technically the request can cover 2 clusters, f.e. 4k write - at s->cluster_sectors - 2k offset. One of these cluster can - be zeroed, one unallocated */ - cl_end = sector_num + nb_sectors - s->cluster_sectors; - if (!is_zero_cluster(bs, cl_end)) { + if (count > s->cluster_size) { + /* Technically the request can cover 2 clusters, f.e. 4k + * write at s->cluster_sectors - 2k offset. One of these + * cluster can be zeroed, one unallocated. Anything larger + * and the front end already split it to alignment + * boundaries. */ + assert(count == 2 * s->cluster_size); + tail_sector = (offset >> BDRV_SECTOR_BITS) + s->cluster_sectors; + if (!is_zero_cluster(bs, tail_sector)) { return -ENOTSUP; } } qemu_co_mutex_lock(&s->lock); /* We can have new write after previous check */ - if (!is_zero_cluster_top_locked(bs, sector_num) || - (cl_end > 0 && !is_zero_cluster_top_locked(bs, cl_end))) { + if (!is_zero_cluster_top_locked(bs, offset >> BDRV_SECTOR_BITS) || + (tail_sector && !is_zero_cluster_top_locked(bs, tail_sector))) { qemu_co_mutex_unlock(&s->lock); return -ENOTSUP; } @@ -2473,7 +2477,7 @@ static coroutine_fn int qcow2_co_write_zeroes(BlockDriverState *bs, } /* Whatever is left can use real zero clusters */ - ret = qcow2_zero_clusters(bs, sector_num << BDRV_SECTOR_BITS, nb_sectors); + ret = qcow2_zero_clusters(bs, offset, count >> BDRV_SECTOR_BITS); qemu_co_mutex_unlock(&s->lock); return ret; @@ -3380,7 +3384,7 @@ BlockDriver bdrv_qcow2 = { .bdrv_co_writev = qcow2_co_writev, .bdrv_co_flush_to_os = qcow2_co_flush_to_os, - .bdrv_co_write_zeroes = qcow2_co_write_zeroes, + .bdrv_co_pwrite_zeroes = qcow2_co_pwrite_zeroes, .bdrv_co_discard = qcow2_co_discard, .bdrv_truncate = qcow2_truncate, .bdrv_write_compressed = qcow2_write_compressed, -- 2.5.5