qemu-img: use blk_co_pwrite_zeroes for zero sectors when compressed
When the buffer is zero, blk_co_pwrite_zeroes is more effective than blk_co_pwritev with BDRV_REQ_WRITE_COMPRESSED. This patch can reduce the time for converting qcow2 images with lots of zero data. Signed-off-by: Lidong Chen <lidongchen@tencent.com> Message-id: 1493261907-18734-1-git-send-email-lidongchen@tencent.com Signed-off-by: Max Reitz <mreitz@redhat.com>
This commit is contained in:
parent
9f1b92add2
commit
db933fbe06
44
qemu-img.c
44
qemu-img.c
|
@ -1649,6 +1649,8 @@ static int coroutine_fn convert_co_write(ImgConvertState *s, int64_t sector_num,
|
||||||
|
|
||||||
while (nb_sectors > 0) {
|
while (nb_sectors > 0) {
|
||||||
int n = nb_sectors;
|
int n = nb_sectors;
|
||||||
|
BdrvRequestFlags flags = s->compressed ? BDRV_REQ_WRITE_COMPRESSED : 0;
|
||||||
|
|
||||||
switch (status) {
|
switch (status) {
|
||||||
case BLK_BACKING_FILE:
|
case BLK_BACKING_FILE:
|
||||||
/* If we have a backing file, leave clusters unallocated that are
|
/* If we have a backing file, leave clusters unallocated that are
|
||||||
|
@ -1658,43 +1660,24 @@ static int coroutine_fn convert_co_write(ImgConvertState *s, int64_t sector_num,
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case BLK_DATA:
|
case BLK_DATA:
|
||||||
/* We must always write compressed clusters as a whole, so don't
|
/* If we're told to keep the target fully allocated (-S 0) or there
|
||||||
* try to find zeroed parts in the buffer. We can only save the
|
* is real non-zero data, we must write it. Otherwise we can treat
|
||||||
* write if the buffer is completely zeroed and we're allowed to
|
* it as zero sectors.
|
||||||
* keep the target sparse. */
|
* Compressed clusters need to be written as a whole, so in that
|
||||||
if (s->compressed) {
|
* case we can only save the write if the buffer is completely
|
||||||
if (s->has_zero_init && s->min_sparse &&
|
* zeroed. */
|
||||||
buffer_is_zero(buf, n * BDRV_SECTOR_SIZE))
|
|
||||||
{
|
|
||||||
assert(!s->target_has_backing);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
iov.iov_base = buf;
|
|
||||||
iov.iov_len = n << BDRV_SECTOR_BITS;
|
|
||||||
qemu_iovec_init_external(&qiov, &iov, 1);
|
|
||||||
|
|
||||||
ret = blk_co_pwritev(s->target, sector_num << BDRV_SECTOR_BITS,
|
|
||||||
n << BDRV_SECTOR_BITS, &qiov,
|
|
||||||
BDRV_REQ_WRITE_COMPRESSED);
|
|
||||||
if (ret < 0) {
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* If there is real non-zero data or we're told to keep the target
|
|
||||||
* fully allocated (-S 0), we must write it. Otherwise we can treat
|
|
||||||
* it as zero sectors. */
|
|
||||||
if (!s->min_sparse ||
|
if (!s->min_sparse ||
|
||||||
is_allocated_sectors_min(buf, n, &n, s->min_sparse))
|
(!s->compressed &&
|
||||||
|
is_allocated_sectors_min(buf, n, &n, s->min_sparse)) ||
|
||||||
|
(s->compressed &&
|
||||||
|
!buffer_is_zero(buf, n * BDRV_SECTOR_SIZE)))
|
||||||
{
|
{
|
||||||
iov.iov_base = buf;
|
iov.iov_base = buf;
|
||||||
iov.iov_len = n << BDRV_SECTOR_BITS;
|
iov.iov_len = n << BDRV_SECTOR_BITS;
|
||||||
qemu_iovec_init_external(&qiov, &iov, 1);
|
qemu_iovec_init_external(&qiov, &iov, 1);
|
||||||
|
|
||||||
ret = blk_co_pwritev(s->target, sector_num << BDRV_SECTOR_BITS,
|
ret = blk_co_pwritev(s->target, sector_num << BDRV_SECTOR_BITS,
|
||||||
n << BDRV_SECTOR_BITS, &qiov, 0);
|
n << BDRV_SECTOR_BITS, &qiov, flags);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
@ -1704,6 +1687,7 @@ static int coroutine_fn convert_co_write(ImgConvertState *s, int64_t sector_num,
|
||||||
|
|
||||||
case BLK_ZERO:
|
case BLK_ZERO:
|
||||||
if (s->has_zero_init) {
|
if (s->has_zero_init) {
|
||||||
|
assert(!s->target_has_backing);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
ret = blk_co_pwrite_zeroes(s->target,
|
ret = blk_co_pwrite_zeroes(s->target,
|
||||||
|
|
Loading…
Reference in New Issue