From patchwork Mon Jul 15 10:49:35 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Lieven X-Patchwork-Id: 258994 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id A6F822C00E7 for ; Mon, 15 Jul 2013 20:52:22 +1000 (EST) Received: from localhost ([::1]:56283 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UygOC-0005p1-Jg for incoming@patchwork.ozlabs.org; Mon, 15 Jul 2013 06:52:20 -0400 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 Received: (qmail 21523 invoked by uid 89); 15 Jul 2013 10:50:33 -0000 Received: from [82.141.1.145] by client-16-kamp (envelope-from , uid 89) with qmail-scanner-2.01 (clamdscan: 0.97.8/17510. hbedv: 8.2.12.78/7.11.90.136. spamassassin: 3.3.1. Clear:RC:1(82.141.1.145):SA:0(-1.2/5.0):. Processed in 10.361193 secs); 15 Jul 2013 10:50:33 -0000 Received: from ns.kamp-intra.net (HELO dns.kamp-intra.net) ([82.141.1.145]) by mx01.kamp.de with SMTP; 15 Jul 2013 10:50:23 -0000 X-GL_Whitelist: yes Received: from lieven-pc.kamp-intra.net (lieven-pc.kamp-intra.net [172.21.12.60]) by dns.kamp-intra.net (Postfix) with ESMTP id 318B8206AB; Mon, 15 Jul 2013 12:49:43 +0200 (CEST) Received: by lieven-pc.kamp-intra.net (Postfix, from userid 1000) id 75E335FA15; Mon, 15 Jul 2013 12:49:43 +0200 (CEST) From: Peter Lieven To: qemu-devel@nongnu.org Date: Mon, 15 Jul 2013 12:49:35 +0200 Message-Id: <1373885375-13601-5-git-send-email-pl@kamp.de> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: <1373885375-13601-1-git-send-email-pl@kamp.de> References: <1373885375-13601-1-git-send-email-pl@kamp.de> X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-Received-From: 2a02:248:0:51::16 Cc: kwolf@redhat.com, pbonzini@redhat.com, Peter Lieven , ronniesahlberg@gmail.com, stefanha@redhat.com Subject: [Qemu-devel] [PATCH 4/4] qemu-img: conditionally discard target on convert X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org 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) {