From patchwork Sat Apr 23 06:26:27 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ren Kimura X-Patchwork-Id: 8917011 Return-Path: X-Original-To: patchwork-qemu-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 392B1BF29F for ; Sat, 23 Apr 2016 06:26:58 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 87AF220225 for ; Sat, 23 Apr 2016 06:26:57 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id D22AD20212 for ; Sat, 23 Apr 2016 06:26:56 +0000 (UTC) Received: from localhost ([::1]:47791 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1atr1r-0006vH-Qn for patchwork-qemu-devel@patchwork.kernel.org; Sat, 23 Apr 2016 02:26:55 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:46863) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1atr1h-0006rk-Ul for qemu-devel@nongnu.org; Sat, 23 Apr 2016 02:26:46 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1atr1h-0003TG-3b for qemu-devel@nongnu.org; Sat, 23 Apr 2016 02:26:45 -0400 Received: from mail-pf0-x241.google.com ([2607:f8b0:400e:c00::241]:36663) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1atr1e-0003Rk-7D; Sat, 23 Apr 2016 02:26:42 -0400 Received: by mail-pf0-x241.google.com with SMTP id p185so11669989pfb.3; Fri, 22 Apr 2016 23:26:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=3tg34p7jOMsSNfTak00ivdmkt9m3do/ROLS6cj6ELbo=; b=pQ9cVWubi03Dep+TG5T/n0TAa0GuLwldLVme5huk8lIxHF+CeopGp72IAYn9cf/JGe GHjDcvvkmzgo6bt/6SJJ6yzRTjKSdMkwkUQ69BUkWFHqXWMEPMr+bkdLyD6CXla9A+GM m5fDLtPEHydjV+CmLkiU3f7NCFGmqQ+q+7MJoddDdHcqvEqKun7DqC7TNi3GcT2eBkNs w36b0ggBuX6GRlYWjxgERFkZvEYeMtGHIAIvgOBJCtplI4oQX9Z2fsfShXxUCGsdk6N0 qTNYpSByvTAvRcRPJT7JPVe4HNo4pEfO7g9nKMkLYiWdsU4cfrcRXLVme24gVVbCOnGQ 4KRQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=3tg34p7jOMsSNfTak00ivdmkt9m3do/ROLS6cj6ELbo=; b=PIK3Fygim1WG5JdTfrmKVTojMNyaIZGprtobqBrnLzCe8MMS+hJcjAoZ6zLuyChIU7 r0Q0+5+PXXjIe69HR8/cIl/4NVztqKZK30Yxmclqe1+8lFM8oY2uNHp81BTwKBWJC785 vfKLmSi8U4E/T55nc9xAhM2QkXlnFz9sL1NXpdaswpogOzayvXhU5Uyz0QZ7KdvmJmWj s0M1y1kv82Kh/GkMgCCKRUIpVBaejJvszCh2105b3oWfJ/jxCCwoBCiihLIfTu7qPAnt ZQ9pMgP6rj07Ynl+V40c8SPhGREcbd8wDdM45IjRWBP088NMxKG9RZ7UF7Caf10i2Gu3 VOZw== X-Gm-Message-State: AOPr4FVklMfWeCNcgMlVh/Nry7l30wXoUX9iebDeIEpiOjT9mgvq3Qdn1vnGfQCwFut+JA== X-Received: by 10.98.0.202 with SMTP id 193mr33554856pfa.120.1461392801705; Fri, 22 Apr 2016 23:26:41 -0700 (PDT) Received: from localhost.localdomain (112-70-24-137f1.hyg1.eonet.ne.jp. [112.70.24.137]) by smtp.googlemail.com with ESMTPSA id qm10sm13621500pac.33.2016.04.22.23.26.40 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 22 Apr 2016 23:26:41 -0700 (PDT) From: Ren Kimura To: mreitz@redhat.com Date: Sat, 23 Apr 2016 15:26:27 +0900 Message-Id: <1461392787-26897-2-git-send-email-rkx1209dev@gmail.com> X-Mailer: git-send-email 2.5.0 In-Reply-To: <1461392787-26897-1-git-send-email-rkx1209dev@gmail.com> References: <1461392787-26897-1-git-send-email-rkx1209dev@gmail.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2607:f8b0:400e:c00::241 Subject: [Qemu-devel] [PATCH v4 1/1] qemu-img: check block status of backing file when converting. X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, qemu-devel@nongnu.org, qemu-block@nongnu.org Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Spam-Status: No, score=-6.8 required=5.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, T_DKIM_INVALID, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP When converting images, check the block status of its backing file chain to avoid needlessly reading zeros. Signed-off-by: Ren Kimura --- qemu-img.c | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/qemu-img.c b/qemu-img.c index 06264d9..b771227 100644 --- a/qemu-img.c +++ b/qemu-img.c @@ -1451,6 +1451,22 @@ static void convert_select_part(ImgConvertState *s, int64_t sector_num) } } +static int64_t get_backing_status(BlockDriverState *bs, + int64_t sector_num, + int nb_sectors, int *pnum) +{ + while (bs->backing) { + int64_t ret; + BlockDriverState *file; + bs = bs->backing->bs; + ret = bdrv_get_block_status(bs, sector_num, nb_sectors, pnum, &file); + if (ret < 0 || ret & (BDRV_BLOCK_DATA | BDRV_BLOCK_ZERO)) { + return ret; + } + } + return -1; +} + static int convert_iteration_sectors(ImgConvertState *s, int64_t sector_num) { int64_t ret; @@ -1477,10 +1493,21 @@ static int convert_iteration_sectors(ImgConvertState *s, int64_t sector_num) } else if (!s->target_has_backing) { /* Without a target backing file we must copy over the contents of * the backing file as well. */ - /* TODO Check block status of the backing file chain to avoid + /* Check block status of the backing file chain to avoid * needlessly reading zeroes and limiting the iteration to the * buffer size */ - s->status = BLK_DATA; + ret = get_backing_status(blk_bs(s->src[s->src_cur]), + sector_num - s->src_cur_offset, + n, &n); + if (ret < 0) { + return ret; + } + + if (ret & BDRV_BLOCK_ZERO) { + s->status = BLK_ZERO; + } else { + s->status = BLK_DATA; + } } else { s->status = BLK_BACKING_FILE; }