From patchwork Thu Apr 6 13:15:36 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: 858585 jemmy X-Patchwork-Id: 9667509 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 7E73A6021C for ; Thu, 6 Apr 2017 13:54:24 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6B5F527F89 for ; Thu, 6 Apr 2017 13:54:24 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 5EC9428113; Thu, 6 Apr 2017 13:54:24 +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 DCF9D27F89 for ; Thu, 6 Apr 2017 13:54:23 +0000 (UTC) Received: from localhost ([::1]:45981 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cw7rj-0001gJ-1Q for patchwork-qemu-devel@patchwork.kernel.org; Thu, 06 Apr 2017 09:54:23 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:52666) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cw7Gl-0003I5-3c for qemu-devel@nongnu.org; Thu, 06 Apr 2017 09:16:15 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cw7Gf-000399-DS for qemu-devel@nongnu.org; Thu, 06 Apr 2017 09:16:11 -0400 Received: from mail-pg0-x242.google.com ([2607:f8b0:400e:c05::242]:33578) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1cw7Gf-00038m-8Q; Thu, 06 Apr 2017 09:16:05 -0400 Received: by mail-pg0-x242.google.com with SMTP id 79so8461330pgf.0; Thu, 06 Apr 2017 06:16:05 -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:in-reply-to:references; bh=oZTW+0JiSeRjS4LsF3WJss7Vj4ad1+CO3mx9SbANm+Y=; b=TIekhfPspsvd6Z9rsDviKldDZq5zn2hhVc3C1nyW3rb8nqXTvu/9s+/u4yORMTsOAM kscBRFZ0HLEYU2chmGnmjMKxY8UQEyYy5Ax2AhWWohOcrhxQniH28pPXMox194RSRZ+b WYfQhfia9RYI/cwLJKpYObGrI12ivXP0OXbSMD8TlwKY57o2kK4W6Y4117Gsz0VoLiVr FQbJAgvlVjJEjv7E6HfPcUvll9RVgm9/e6nHljhrhPM/s7dJBlgQmKjYbfojUwGNqnSL aZsvcv45tRUulp/FbwSU6z/uSP3PqJuMrGjxS5lpeqfPyK0deNstoMb1tj7Ry6CvQrL4 wquQ== 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:in-reply-to :references; bh=oZTW+0JiSeRjS4LsF3WJss7Vj4ad1+CO3mx9SbANm+Y=; b=Uoywl+g8UEYKRpqJ3Mw94kzCNCLJqO7LWMaT6xZXOhIJSiS/aKK9ArR/o7c23EfBa7 a+Yk3A33V5JUs7S9dpUb5pEUpiwOmFAHiUcWurEKFKUqvf2AgKiWdx4GOnJSA9A+PpgQ 0txWxud8e5EXbHPUbSAU/I0C6UwCBFkke3Yp1/PPvkaqadNzPMT46Wb0ySFLQQgFhMEL WrzCu01mBD6Dr3mG4h12flr8pIeddPr6C2q/boolCg/6b0Z0Laxs3FqJ5IbQBH5oyjNh LxjRsV9C4gYOiPmGYCpf1IlKcQeRA34piHiRJ5pttgXMcJjwX7vGU4NHrxbZeSskCdSW 6gcw== X-Gm-Message-State: AFeK/H0us5e7ldQ9TOVIwAfUQpC2WX+CUbhuGx1i+vk+8Wjlu0BJtx2x7NwuD61NJSMPXw== X-Received: by 10.84.238.22 with SMTP id u22mr43669002plk.137.1491484564418; Thu, 06 Apr 2017 06:16:04 -0700 (PDT) Received: from localhost.localdomain ([123.206.110.226]) by smtp.gmail.com with ESMTPSA id n7sm4029624pfn.0.2017.04.06.06.16.01 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 06 Apr 2017 06:16:04 -0700 (PDT) From: jemmy858585@gmail.com X-Google-Original-From: lidongchen@tencent.com To: qemu-devel@nongnu.org Date: Thu, 6 Apr 2017 21:15:36 +0800 Message-Id: <1491484536-22776-3-git-send-email-lidongchen@tencent.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1491484536-22776-1-git-send-email-lidongchen@tencent.com> References: <1491484536-22776-1-git-send-email-lidongchen@tencent.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2607:f8b0:400e:c05::242 Subject: [Qemu-devel] [PATCH 2/2] 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: kwolf@redhat.com, famz@redhat.com, qemu-block@nongnu.org, quintela@redhat.com, dgilbert@redhat.com, mreitz@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 --- migration/block.c | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/migration/block.c b/migration/block.c index 7734ff7..1fce9b9 100644 --- a/migration/block.c +++ b/migration/block.c @@ -885,6 +885,10 @@ static int block_load(QEMUFile *f, void *opaque, int version_id) int64_t total_sectors = 0; int nr_sectors; int ret; + int i; + int cluster_size; + int64_t addr_offset; + uint8_t *buf_offset; do { addr = qemu_get_be64(f); @@ -934,8 +938,34 @@ 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); + + cluster_size = bdrv_get_cluster_size(blk_bs(blk)); + + if (cluster_size > 0) { + for (i = 0; i < BLOCK_SIZE / cluster_size; i++) { + addr_offset = addr * BDRV_SECTOR_SIZE + + i * cluster_size; + buf_offset = buf + i * cluster_size; + + if (buffer_is_zero(buf_offset, cluster_size)) { + ret = blk_pwrite_zeroes(blk, addr_offset, + cluster_size, + BDRV_REQ_MAY_UNMAP); + } else { + ret = blk_pwrite(blk, addr_offset, + buf_offset, 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); }