From patchwork Sun Apr 9 12:37:40 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: 858585 jemmy X-Patchwork-Id: 9671533 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id DE4BF600CB for ; Sun, 9 Apr 2017 12:38:55 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id BFE0D283F1 for ; Sun, 9 Apr 2017 12:38:55 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id B10BD2846B; Sun, 9 Apr 2017 12:38:55 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, T_DKIM_INVALID autolearn=ham version=3.3.1 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.wl.linuxfoundation.org (Postfix) with ESMTPS id ED0C5283F1 for ; Sun, 9 Apr 2017 12:38:54 +0000 (UTC) Received: from localhost ([::1]:58553 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cxC7H-0004aD-KL for patchwork-qemu-devel@patchwork.kernel.org; Sun, 09 Apr 2017 08:38:51 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:53043) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cxC6V-0004Ze-Fo for qemu-devel@nongnu.org; Sun, 09 Apr 2017 08:38:06 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cxC6S-0006U5-CS for qemu-devel@nongnu.org; Sun, 09 Apr 2017 08:38:03 -0400 Received: from mail-pf0-x243.google.com ([2607:f8b0:400e:c00::243]:34275) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1cxC6S-0006Tt-7U; Sun, 09 Apr 2017 08:38:00 -0400 Received: by mail-pf0-x243.google.com with SMTP id o126so5985746pfb.1; Sun, 09 Apr 2017 05:37:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=DRkasVdrNFOkvLHQWkTewpv2TpwLIJX4WrdKVmqGuGg=; b=FqK6Qmvd8MNDca8aijVSHOhj+9RhsABBvFvvLll4cMXeCM0Us2ofBF99hDD7tnfJiL +9ehMek5iLVU+bzLCYWlXRwC/j1lHa05ouR4u1m2XEceCViMrYWDZU4SwQhs8sxE12Vb pUopIl6BGir6jJa3hrd1SgS80EDfqGYoWirAL2zrTWkgQeSyaaRrrT3dK/GD3E83gIJS gUEdC69wCfyDb7ajaZNzX0HgPDhIyIX5VUmKMric4QhBGngWVzzoRhXlZ9CBbBpVrJ9g HZd8txwvCU6Fj0z8fvEoXQkCY/DQdZxn39W6M+KBTjS+00goM3eeXXz9Thcr5Yq4SZpB zGNA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=DRkasVdrNFOkvLHQWkTewpv2TpwLIJX4WrdKVmqGuGg=; b=MA2FIyKpalDf+A4l3w4dNL4RjGo0/q1iSr7jkB4L4qwGDfUxZHa6m8Twon0BRssRqG gnIm0eaB+OAU54rU5jmxwOB9SzkXDf6vhZUA3php67DmKCSV08GXK5Mk2nhJxMu6+8nr pfG9MD0RlvZ3VNvjuMZjkIfQV/3LCXeLqYtr0vShPsC2hGizHXpf0eAXE2GqQjHRet+h wsMMqmAjvWaJ2jEgx8DMdKGwr8KOjK0zbh7/EeCT2ue0Zuxehc4wwtO43aj76+oI9f45 3lvnfOl+riDbGWQ1AKNnk/ghtTbkvYLfW3341uHC9SiVlZAbMfWbf4dWW5+pInc/uAC0 Y7Iw== X-Gm-Message-State: AFeK/H2oBXOhT0nH0OF9XB6vKWETAwkbwdf77JsoFgtB1tFCnC4ig2TdJ0ZTe8x9ckBQoA== X-Received: by 10.98.148.2 with SMTP id m2mr18449785pfe.194.1491741479091; Sun, 09 Apr 2017 05:37:59 -0700 (PDT) Received: from localhost.localdomain ([119.28.70.25]) by smtp.gmail.com with ESMTPSA id c80sm8818164pfc.38.2017.04.09.05.37.57 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 09 Apr 2017 05:37:58 -0700 (PDT) From: jemmy858585@gmail.com X-Google-Original-From: lidongchen@tencent.com To: qemu-devel@nongnu.org Date: Sun, 9 Apr 2017 20:37:40 +0800 Message-Id: <1491741460-10308-1-git-send-email-lidongchen@tencent.com> X-Mailer: git-send-email 1.8.3.1 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2607:f8b0:400e:c00::243 Subject: [Qemu-devel] [PATCH v3] migration/block: use blk_pwrite_zeroes for each zero cluster 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: famz@redhat.com, qemu-block@nongnu.org, quintela@redhat.com, dgilbert@redhat.com, stefanha@redhat.com, Lidong Chen Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP From: Lidong Chen BLOCK_SIZE is (1 << 20), qcow2 cluster size is 65536 by default, this maybe cause the qcow2 file size is bigger after migration. This patch check each cluster, use blk_pwrite_zeroes for each zero cluster. Signed-off-by: Lidong Chen Reviewed-by: Fam Zheng Reviewed-by: Fam Zheng --- migration/block.c | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/migration/block.c b/migration/block.c index 7734ff7..fe613db 100644 --- a/migration/block.c +++ b/migration/block.c @@ -885,6 +885,8 @@ static int block_load(QEMUFile *f, void *opaque, int version_id) int64_t total_sectors = 0; int nr_sectors; int ret; + BlockDriverInfo bdi; + int cluster_size; do { addr = qemu_get_be64(f); @@ -934,8 +936,40 @@ static int block_load(QEMUFile *f, void *opaque, int version_id) } else { buf = g_malloc(BLOCK_SIZE); qemu_get_buffer(f, buf, BLOCK_SIZE); - ret = blk_pwrite(blk, addr * BDRV_SECTOR_SIZE, buf, - nr_sectors * BDRV_SECTOR_SIZE, 0); + + ret = bdrv_get_info(blk_bs(blk), &bdi); + cluster_size = bdi.cluster_size; + + if (ret == 0 && cluster_size > 0 && + cluster_size <= BLOCK_SIZE && + BLOCK_SIZE % cluster_size == 0) { + int i; + int64_t cur_addr; + uint8_t *cur_buf; + + for (i = 0; i < BLOCK_SIZE / cluster_size; i++) { + cur_addr = addr * BDRV_SECTOR_SIZE + + i * cluster_size; + cur_buf = buf + i * cluster_size; + + if (buffer_is_zero(cur_buf, cluster_size)) { + ret = blk_pwrite_zeroes(blk, cur_addr, + cluster_size, + BDRV_REQ_MAY_UNMAP); + } else { + ret = blk_pwrite(blk, cur_addr, cur_buf, + cluster_size, 0); + } + + if (ret < 0) { + g_free(buf); + return ret; + } + } + } else { + ret = blk_pwrite(blk, addr * BDRV_SECTOR_SIZE, buf, + nr_sectors * BDRV_SECTOR_SIZE, 0); + } g_free(buf); }