From patchwork Thu Apr 13 02:34:28 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: 858585 jemmy X-Patchwork-Id: 9678625 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 C3EDE60383 for ; Thu, 13 Apr 2017 02:35:53 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A7FA02863B for ; Thu, 13 Apr 2017 02:35:53 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 99C4228648; Thu, 13 Apr 2017 02:35:53 +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 F3BE72863B for ; Thu, 13 Apr 2017 02:35:52 +0000 (UTC) Received: from localhost ([::1]:47084 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cyUbt-0004j0-Uz for patchwork-qemu-devel@patchwork.kernel.org; Wed, 12 Apr 2017 22:35:49 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:41696) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cyUbH-0004it-1V for qemu-devel@nongnu.org; Wed, 12 Apr 2017 22:35:12 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cyUbD-0002BF-TN for qemu-devel@nongnu.org; Wed, 12 Apr 2017 22:35:11 -0400 Received: from mail-pg0-x244.google.com ([2607:f8b0:400e:c05::244]:36860) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1cyUbD-000283-N8; Wed, 12 Apr 2017 22:35:07 -0400 Received: by mail-pg0-x244.google.com with SMTP id 34so5612769pgx.3; Wed, 12 Apr 2017 19:35:06 -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=4rTvyQzn1PE8XTcnK9GDpOrQr/29ywsY3NL4K+0brxY=; b=lj4MTZH/MHciphErpIiAIBBCKnAxGHvhULuHy8/6KD35HXW0iqHQ7+ldV02XbAIYLH 6k9DkFCB3yhp0dNEZnLZrtksk8YAn/iBQnpveXRm8NmBLJ7OW2AV2k/aZLnFt8Rm05GQ 7jZPO0cm0hHy9R3njwJNzVdy/iq6z+aRp/xjekrhrooXRUwHKr/ly+hwK4EkJUmVtfCM m/lYAlytXuILz8Fp3ZykvPKpJoFaC5VBMrcr6zJzcs8GDbbO5t2sqI9B4R9SWJtJ+2G/ TIs8PgYllsPjlai1GOJPGCTUBpnasM/WyBQnH1FfPrvpTmFRW7y45MphIob9m3X2P6+y B3DQ== 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=4rTvyQzn1PE8XTcnK9GDpOrQr/29ywsY3NL4K+0brxY=; b=RqzdiGrF2oVoxGqRSsK0UH4lB/pgRDr7W2DIa3gx3ErhlWsr6ABbAe2jKWBKowagEl Ix//ffhQxoCP27uhf4OeUBSPj2fprhEk+Z+UBm0DfePUcp3qITwG967CkMNc7IT6tWEM qxIWOduUNksLRjhanUav/Mny8NDTy1wvo8JxuMTmQADFWLUdMsLugv39Hg54mAEFzI4a 4NTXgIfzLY/Wa7OG182QkO5JwYfjchALVA2GrkuykCdTYQR8MBrX9qiRsCdtaiwaFeBv UCVqcsqcK9ALLe9PqNBpKpqoU7pZz27zDNd/iyoEAOK8bJ3vnIWSHpGrNRt7LNFAHhu9 RlaQ== X-Gm-Message-State: AN3rC/4nquigckO6BiEKmUCfTyObuuUmjNq35xSOIB5zkYLfejkkaJ7u kuC3JFs8cpdGqg== X-Received: by 10.99.131.195 with SMTP id h186mr853924pge.57.1492050905466; Wed, 12 Apr 2017 19:35:05 -0700 (PDT) Received: from localhost.localdomain ([119.28.70.25]) by smtp.gmail.com with ESMTPSA id p2sm3167857pfj.93.2017.04.12.19.35.03 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 12 Apr 2017 19:35:04 -0700 (PDT) From: jemmy858585@gmail.com X-Google-Original-From: lidongchen@tencent.com To: qemu-devel@nongnu.org Date: Thu, 13 Apr 2017 10:34:28 +0800 Message-Id: <1492050868-16200-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:c05::244 Subject: [Qemu-devel] [PATCH v6] 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 may cause the qcow2 file size to be bigger after migration. This patch checks each cluster, using blk_pwrite_zeroes for each zero cluster. Reviewed-by: Stefan Hajnoczi Signed-off-by: Lidong Chen --- v6 changelog: Fix up some grammar in the comment. --- migration/block.c | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/migration/block.c b/migration/block.c index 7734ff7..41c7a55 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); @@ -919,6 +921,15 @@ static int block_load(QEMUFile *f, void *opaque, int version_id) error_report_err(local_err); return -EINVAL; } + + ret = bdrv_get_info(blk_bs(blk), &bdi); + if (ret == 0 && bdi.cluster_size > 0 && + bdi.cluster_size <= BLOCK_SIZE && + BLOCK_SIZE % bdi.cluster_size == 0) { + cluster_size = bdi.cluster_size; + } else { + cluster_size = BLOCK_SIZE; + } } if (total_sectors - addr < BDRV_SECTORS_PER_DIRTY_CHUNK) { @@ -932,10 +943,30 @@ static int block_load(QEMUFile *f, void *opaque, int version_id) nr_sectors * BDRV_SECTOR_SIZE, BDRV_REQ_MAY_UNMAP); } else { + int i; + int64_t cur_addr; + uint8_t *cur_buf; + 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); + 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 ((!block_mig_state.zero_blocks || + cluster_size < BLOCK_SIZE) && + 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) { + break; + } + } g_free(buf); }