Received: from eggs.gnu.org ([2001:4830:134:3::10]:36274) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UygMa-0003ax-8W for qemu-devel@nongnu.org; Mon, 15 Jul 2013 06:50:43 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1UygMV-0002DX-3d for qemu-devel@nongnu.org; Mon, 15 Jul 2013 06:50:39 -0400 Received: from mx.ipv6.kamp.de ([2a02:248:0:51::16]:57612 helo=mx01.kamp.de) by eggs.gnu.org with smtp (Exim 4.71) (envelope-from ) id 1UygMU-0002DB-Pq for qemu-devel@nongnu.org; Mon, 15 Jul 2013 06:50:35 -0400 From: Peter Lieven Date: Mon, 15 Jul 2013 12:49:35 +0200 Message-Id: <1373885375-13601-5-git-send-email-pl@kamp.de> In-Reply-To: <1373885375-13601-1-git-send-email-pl@kamp.de> References: <1373885375-13601-1-git-send-email-pl@kamp.de> Subject: [Qemu-devel] [PATCH 4/4] qemu-img: conditionally discard target on convert List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: kwolf@redhat.com, pbonzini@redhat.com, Peter Lieven , ronniesahlberg@gmail.com, stefanha@redhat.com if a destination has has_zero_init = 0, but it supports discard zeroes use discard to convert the target into an all zero device. Signed-off-by: Peter Lieven --- qemu-img.c | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 55 insertions(+), 1 deletion(-) diff --git a/qemu-img.c b/qemu-img.c index f8c97d3..cbde02f 100644 --- a/qemu-img.c +++ b/qemu-img.c @@ -1342,7 +1342,8 @@ static int img_convert(int argc, char **argv) goto out; } - flags = BDRV_O_RDWR; + flags = BDRV_O_RDWR | BDRV_O_UNMAP; + ret = bdrv_parse_cache_flags(cache, &flags); if (ret < 0) { error_report("Invalid cache option: %s", cache); @@ -1442,8 +1443,61 @@ static int img_convert(int argc, char **argv) /* signal EOF to align */ bdrv_write_compressed(out_bs, 0, NULL, 0); } else { + BlockDriverInfo bdi; + int has_zero_init = bdrv_has_zero_init(out_bs); + /* if the target has no zero init by default check if we + * can discard blocks to zeroize the device */ + if (!has_zero_init && !out_baseimg && + bdrv_get_info(out_bs, &bdi) == 0 && + bdi.discard_zeroes && bdi.max_unmap > 0) { + int64_t target_size = bdrv_getlength(out_bs) / BDRV_SECTOR_SIZE; + int64_t sector_num2 = -1; + int n; + sector_num = 0; + for (;;) { + nb_sectors = target_size - sector_num; + if (nb_sectors <= 0) { + has_zero_init = 1; + break; + } + if (nb_sectors > INT_MAX) { + nb_sectors = INT_MAX; + } + if (!bdrv_is_allocated(out_bs, sector_num, nb_sectors, &n)) { + if (!n) { + /* an error occured, continue with has_zero_init = 0 */ + break; + } + sector_num += n; + continue; + } + if (sector_num == sector_num2) { + /* some drivers allow a discard to silently fail. + * double-check that we do not try to discard the + * same sectors twice. if thats the case + * continue with has_zero_init = 0 */ + break; + } + sector_num2 = sector_num; + while (n > 0) { + nb_sectors = n; + if (nb_sectors > bdi.max_unmap) { + nb_sectors = bdi.max_unmap; + } + ret = bdrv_discard(out_bs, sector_num2, nb_sectors); + if (ret < 0) { + error_report("error while discarding at sector %" PRId64 ": %s", + sector_num, strerror(-ret)); + goto out; + } + sector_num2 += nb_sectors; + n -= nb_sectors;; + } + } + } + sector_num = 0; // total number of sectors converted so far nb_sectors = total_sectors - sector_num; if (nb_sectors != 0) { -- 1.7.9.5