From patchwork Sun Apr 17 04:30:33 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ren Kimura X-Patchwork-Id: 8862391 Return-Path: X-Original-To: patchwork-qemu-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id E15DA9F36E for ; Sun, 17 Apr 2016 04:30:56 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 3DE2B20154 for ; Sun, 17 Apr 2016 04:30:56 +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 8F7FA20149 for ; Sun, 17 Apr 2016 04:30:55 +0000 (UTC) Received: from localhost ([::1]:39185 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1areMI-0004Hs-4R for patchwork-qemu-devel@patchwork.kernel.org; Sun, 17 Apr 2016 00:30:54 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:53317) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1areMA-0004DU-Cv for qemu-devel@nongnu.org; Sun, 17 Apr 2016 00:30:47 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1areM9-0004KW-GQ for qemu-devel@nongnu.org; Sun, 17 Apr 2016 00:30:46 -0400 Received: from mail-pa0-x244.google.com ([2607:f8b0:400e:c03::244]:34324) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1areM5-0004KB-9F; Sun, 17 Apr 2016 00:30:41 -0400 Received: by mail-pa0-x244.google.com with SMTP id hb4so13134425pac.1; Sat, 16 Apr 2016 21:30:41 -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; bh=KsERbItJC5m3CKBVecTbCkgzJjCyoLEcEP4CP+ItOz8=; b=A0YkcN9BICG0Y6OQHj5wesb9nlwP9KdOqfQVBbimzrsiN2YvxfoBzaYzAzAnyq3gw2 outmzLk2CrjsOxwCnyokbW3rJLIPnNHJgDv9ML1Y9odxpqNJ6ocxq4olR9AMgHOoD30m bRtaJqJpEBLdBPAC6MO9/uOKgJIbybo1+meqfwa2FQXLI1AqmzIPbCltqmYdfMKYdX0l qMtTIwPZIcxcR06V6NVIL/3iME1iLRd2arOR8hDQuyLEVo8Gr/kS9yKhF8ioMt0FHzS/ erN5/EvbUcMifBdXvV8xpPkRQmbHCtlw3CJMjXJZdXJuWKWc034UwJArX/XXLLT5/IgM Dyzw== 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; bh=KsERbItJC5m3CKBVecTbCkgzJjCyoLEcEP4CP+ItOz8=; b=HsxIte6vlXhcAPocmcWEshyEnwjTx4YhdF9pS3QEviQAH6UxJtbM8XEdEizeGkmuYM da2NVtB3AgqqiQEa+Djgp3IBRiZbSsY/u27eBdRSyGi35J4ZXGIsl02bGlhYmXBnhwVF AG4MQmabr6sM30qe7uqrWqUsZ6L7rhHbRZFrCIcm5Kc8pcuWaUkvPGP6p3uayiHt0djq ckJLCuEokh/EX9wTfTH/jLqDbSE7oRRGGb5TscHgu9mkJ3wPWWNw7RtLAX9458MA82kR 3JXHYHtMy+iuqYkIPiWI3R6EW1Ehd0JBGFkkfcxH+duYkDLmPTomoYUTGbpxJZ9+UUO5 PxdQ== X-Gm-Message-State: AOPr4FW+A/vQhjdOlLEwRpk5+4BeJzkw072W5V2YCL6OrMwpXxq7ob3epv+kIYkc02/KTw== X-Received: by 10.66.232.226 with SMTP id tr2mr40829316pac.44.1460867440511; Sat, 16 Apr 2016 21:30:40 -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 tc8sm74748418pac.29.2016.04.16.21.30.38 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sat, 16 Apr 2016 21:30:40 -0700 (PDT) From: Ren Kimura To: mreitz@redhat.com Date: Sun, 17 Apr 2016 13:30:33 +0900 Message-Id: <1460867433-13251-1-git-send-email-rkx1209dev@gmail.com> X-Mailer: git-send-email 2.5.0 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2607:f8b0:400e:c03::244 Subject: [Qemu-devel] [PATCH v3] 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 | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/qemu-img.c b/qemu-img.c index 06264d9..ad1df5f 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) +{ + int64_t ret; + BlockDriverState *file; + ret = bdrv_get_block_status(bs, sector_num, nb_sectors, pnum, &file); + if (!(ret & (BDRV_BLOCK_DATA | BDRV_BLOCK_ZERO))) { + if (bs->backing) { + ret = get_backing_status(bs->backing->bs, sector_num, + nb_sectors, pnum); + } + } + return ret; +} + static int convert_iteration_sectors(ImgConvertState *s, int64_t sector_num) { int64_t ret; @@ -1477,10 +1493,17 @@ 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 & BDRV_BLOCK_ZERO) { + s->status = BLK_ZERO; + } else { + s->status = BLK_DATA; + } } else { s->status = BLK_BACKING_FILE; }