From 8f961adcb4fd275c70ebc27dc25fa778eaa6a106 Mon Sep 17 00:00:00 2001 From: Jeffrey Cody Date: Wed, 21 Mar 2012 21:55:18 +0100 Subject: [PATCH 51/55] block: perform zero-detection during copy-on-read RH-Author: Jeffrey Cody Message-id: Patchwork-id: 38901 O-Subject: [RHEL6.3 qemu-kvm PATCH v8 51/54] block: perform zero-detection during copy-on-read Bugzilla: 582475 RH-Acked-by: Paolo Bonzini RH-Acked-by: Marcelo Tosatti RH-Acked-by: Kevin Wolf From: Stefan Hajnoczi Copy-on-Read populates the image file with data read from a backing image. In order to avoid bloating the image file when all zeroes are read we should scan the buffer and perform an optimized zero write operation. Signed-off-by: Stefan Hajnoczi Signed-off-by: Kevin Wolf (cherry picked from commit 79c053bde9fa40595670ae274d047da07f1df88c) Signed-off-by: Stefan Hajnoczi Signed-off-by: Anthony Liguori Signed-off-by: Jeff Cody --- block.c | 14 +++++++++++--- 1 files changed, 11 insertions(+), 3 deletions(-) Signed-off-by: Michal Novotny --- block.c | 14 +++++++++++--- 1 files changed, 11 insertions(+), 3 deletions(-) diff --git a/block.c b/block.c index ca10072..e3c4f64 100644 --- a/block.c +++ b/block.c @@ -1515,6 +1515,7 @@ static int coroutine_fn bdrv_co_do_copy_on_readv(BlockDriverState *bs, */ void *bounce_buffer; + BlockDriver *drv = bs->drv; struct iovec iov; QEMUIOVector bounce_qiov; int64_t cluster_sector_num; @@ -1535,14 +1536,21 @@ static int coroutine_fn bdrv_co_do_copy_on_readv(BlockDriverState *bs, iov.iov_base = bounce_buffer = qemu_blockalign(bs, iov.iov_len); qemu_iovec_init_external(&bounce_qiov, &iov, 1); - ret = bs->drv->bdrv_co_readv(bs, cluster_sector_num, cluster_nb_sectors, - &bounce_qiov); + ret = drv->bdrv_co_readv(bs, cluster_sector_num, cluster_nb_sectors, + &bounce_qiov); if (ret < 0) { goto err; } - ret = bs->drv->bdrv_co_writev(bs, cluster_sector_num, cluster_nb_sectors, + if (drv->bdrv_co_write_zeroes && + buffer_is_zero(bounce_buffer, iov.iov_len)) { + ret = drv->bdrv_co_write_zeroes(bs, cluster_sector_num, + cluster_nb_sectors); + } else { + ret = drv->bdrv_co_writev(bs, cluster_sector_num, cluster_nb_sectors, &bounce_qiov); + } + if (ret < 0) { /* It might be okay to ignore write errors for guest requests. If this * is a deliberate copy-on-read then we don't want to ignore the error. -- 1.7.7.6