From patchwork Wed Jun 1 10:24:22 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: zhangzhiming X-Patchwork-Id: 9147085 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 D2CC860777 for ; Wed, 1 Jun 2016 10:25:26 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C20161FF65 for ; Wed, 1 Jun 2016 10:25:26 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id B677326785; Wed, 1 Jun 2016 10:25:26 +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_SIGNED, 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 7B9241FF65 for ; Wed, 1 Jun 2016 10:25:23 +0000 (UTC) Received: from localhost ([::1]:41586 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b83L0-0004qm-KY for patchwork-qemu-devel@patchwork.kernel.org; Wed, 01 Jun 2016 06:25:22 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:42163) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b83KW-0004mx-14 for qemu-devel@nongnu.org; Wed, 01 Jun 2016 06:24:55 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1b83KS-00015x-7b for qemu-devel@nongnu.org; Wed, 01 Jun 2016 06:24:51 -0400 Received: from mx-fe5-210.meituan.com ([103.37.138.210]:39969 helo=mx02.meituan.com) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b83KI-0000yI-FC; Wed, 01 Jun 2016 06:24:39 -0400 Received: from localhost (localhost [127.0.0.1]) by it-mx02.lf.sankuai.com (Postfix) with ESMTP id 684A929786CD; Wed, 1 Jun 2016 18:24:30 +0800 (CST) Authentication-Results: it-mx02.lf.sankuai.com (amavisd-new); dkim=pass (1024-bit key) header.d=meituan.com Received: from mx02.meituan.com ([127.0.0.1]) by localhost (it-mx02.lf.sankuai.com [127.0.0.1]) (amavisd-new, port 10032) with ESMTP id YF9ksQCsqpGb; Wed, 1 Jun 2016 18:24:30 +0800 (CST) Received: from localhost (localhost [127.0.0.1]) by it-mx02.lf.sankuai.com (Postfix) with ESMTP id 3942529786C9; Wed, 1 Jun 2016 18:24:30 +0800 (CST) DKIM-Filter: OpenDKIM Filter v2.9.2 it-mx02.lf.sankuai.com 3942529786C9 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=meituan.com; s=20130113; t=1464776670; bh=4FgrgibZCtOHMXbI3mjYsoyZTwcKOCFlCNpJjpu87dY=; h=Content-Type:Mime-Version:Subject:From:Date:Message-Id:To; b=bXFnJk7GxST4OvxYOSmklqomllWqSEajvXKD0GEBqYwRz/xpSktVkSh54ih3LuRmp fgKZyljESSienS1+NX9HUN6Xtw2+Ut/4sjb/lU2JP3X2yppjbsXYknpwrrOIXYBJ5F 9z5F32xEkJZTd9DKUJN0YufSRlxWaZTprwcur46M= X-Virus-Scanned: amavisd-new at it-mx02.lf.sankuai.com Received: from mx02.meituan.com ([127.0.0.1]) by localhost (it-mx02.lf.sankuai.com [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id CyjXx9g0X9JW; Wed, 1 Jun 2016 18:24:30 +0800 (CST) Received: from [172.18.178.40] (unknown [103.37.140.15]) by it-mx02.lf.sankuai.com (Postfix) with ESMTPSA id 36FE729786CD; Wed, 1 Jun 2016 18:24:22 +0800 (CST) Mime-Version: 1.0 (Mac OS X Mail 8.2 \(2070.6\)) From: zhangzhiming In-Reply-To: <467DE7EB-A927-4100-96FB-736444263B06@meituan.com> Date: Wed, 1 Jun 2016 18:24:22 +0800 Message-Id: <6DA33516-D7FB-44C2-A4B8-01974A3E3B77@meituan.com> References: <8E87729A-E996-4D12-B940-87B77C1D22EC@meituan.com> <574DC0CB.7020707@redhat.com> <467DE7EB-A927-4100-96FB-736444263B06@meituan.com> To: Eric Blake X-Mailer: Apple Mail (2.2070.6) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [generic] X-Received-From: 103.37.138.210 X-Content-Filtered-By: Mailman/MimeDel 2.1.21 Subject: Re: [Qemu-devel] [Qemu-block] [PATCH] qcow2 resize with snapshot 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: Kevin Wolf , lihuiba , qemu-devel@nongnu.org, qemu-block@nongnu.org, Max Reitz Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP hi, here are all changes of my code. thanks for the review! zhangzhiming zhangzhiming02@meituan.com Signed-off-by: zhangzhiming --- block.c | 19 +++++++++++++++++++ block/qcow2-cluster.c | 29 +++++++++++++++++++++++++++++ block/qcow2-snapshot.c | 33 +++++++++++++++++++++++---------- block/qcow2.c | 29 ++++++++++++++++++++--------- block/qcow2.h | 1 + include/block/snapshot.h | 1 + 6 files changed, 93 insertions(+), 19 deletions(-) diff --git a/block.c b/block.c index 18a497f..b6f2004 100644 --- a/block.c +++ b/block.c @@ -2631,6 +2631,25 @@ int bdrv_truncate(BlockDriverState *bs, int64_t offset) return ret; } +int bdrv_apply_snapshot(BlockDriverState *bs, const char *snapshot_id, + uint64_t snapshot_size) +{ + int ret = bdrv_snapshot_goto(bs, snapshot_id); + if (ret < 0) { + return ret; + } + + ret = refresh_total_sectors(bs, snapshot_size >> BDRV_SECTOR_BITS); + if (ret < 0) { + return ret; + } + bdrv_dirty_bitmap_truncate(bs); + if (bs->blk) { + blk_dev_resize_cb(bs->blk); + } + return ret; +} + /** * Length of a allocated file in bytes. Sparse files are counted by actual * allocated space. Return < 0 if error or unknown. diff --git a/block/qcow2-cluster.c b/block/qcow2-cluster.c index 31ecc10..f921fd8 100644 --- a/block/qcow2-cluster.c +++ b/block/qcow2-cluster.c @@ -31,6 +31,35 @@ #include "block/qcow2.h" #include "trace.h" +int shrink_l1_table(BlockDriverState *bs, int64_t new_l1_size) +{ + BDRVQcow2State *s = bs->opaque; + int64_t old_l1_size = s->l1_size; + s->l1_size = new_l1_size; + int ret = qcow2_update_snapshot_refcount(bs, s->l1_table_offset, + s->l1_size, 1); + if (ret < 0) { + return ret; + } + + s->l1_size = old_l1_size; + ret = qcow2_update_snapshot_refcount(bs, s->l1_table_offset, + s->l1_size, -1); + if (ret < 0) { + return ret; + } + s->l1_size = new_l1_size; + + uint32_t be_l1_size = cpu_to_be32(s->l1_size); + ret = bdrv_pwrite_sync(bs->file->bs, offsetof(QCowHeader, l1_size), + &be_l1_size, sizeof(be_l1_size)); + if (ret < 0) { + return ret; + } + + return 0; +} + int qcow2_grow_l1_table(BlockDriverState *bs, uint64_t min_size, bool exact_size) { diff --git a/block/qcow2-snapshot.c b/block/qcow2-snapshot.c index 5f4a17e..1ed0e18 100644 --- a/block/qcow2-snapshot.c +++ b/block/qcow2-snapshot.c @@ -477,13 +477,6 @@ int qcow2_snapshot_goto(BlockDriverState *bs, const char *snapshot_id) } sn = &s->snapshots[snapshot_index]; - if (sn->disk_size != bs->total_sectors * BDRV_SECTOR_SIZE) { - error_report("qcow2: Loading snapshots with different disk " - "size is not implemented"); - ret = -ENOTSUP; - goto fail; - } - /* * Make sure that the current L1 table is big enough to contain the whole * L1 table of the snapshot. If the snapshot L1 table is smaller, the @@ -505,7 +498,7 @@ int qcow2_snapshot_goto(BlockDriverState *bs, const char *snapshot_id) * Decrease the refcount referenced by the old one only when the L1 * table is overwritten. */ - sn_l1_table = g_try_malloc0(cur_l1_bytes); + sn_l1_table = g_try_malloc0(sn_l1_bytes); if (cur_l1_bytes && sn_l1_table == NULL) { ret = -ENOMEM; goto fail; @@ -530,11 +523,28 @@ int qcow2_snapshot_goto(BlockDriverState *bs, const char *snapshot_id) } ret = bdrv_pwrite_sync(bs->file->bs, s->l1_table_offset, sn_l1_table, - cur_l1_bytes); + sn_l1_bytes); + if (ret < 0) { + goto fail; + } + + /* write updated header.size */ + uint64_t be_disk_size = cpu_to_be64(sn->disk_size); + ret = bdrv_pwrite_sync(bs->file->bs, offsetof(QCowHeader, size), + &be_disk_size, sizeof(uint64_t)); if (ret < 0) { goto fail; } + uint32_t be_l1_size = cpu_to_be32(sn->l1_size); + ret = bdrv_pwrite_sync(bs->file->bs, offsetof(QCowHeader, l1_size), + &be_l1_size, sizeof(be_l1_size)); + if (ret < 0) { + goto fail; + } + + s->l1_vm_state_index = sn->l1_size; + /* * Decrease refcount of clusters of current L1 table. * @@ -552,7 +562,8 @@ int qcow2_snapshot_goto(BlockDriverState *bs, const char *snapshot_id) * Now update the in-memory L1 table to be in sync with the on-disk one. We * need to do this even if updating refcounts failed. */ - for(i = 0;i < s->l1_size; i++) { + memset(s->l1_table, 0, s->l1_size * sizeof(uint64_t)); + for (i = 0; i < sn->l1_size; i++) { s->l1_table[i] = be64_to_cpu(sn_l1_table[i]); } @@ -563,6 +574,7 @@ int qcow2_snapshot_goto(BlockDriverState *bs, const char *snapshot_id) g_free(sn_l1_table); sn_l1_table = NULL; + s->l1_size = sn->l1_size; /* * Update QCOW_OFLAG_COPIED in the active L1 table (it may have changed * when we decreased the refcount of the old snapshot. @@ -675,6 +687,7 @@ int qcow2_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab) sn_info->date_sec = sn->date_sec; sn_info->date_nsec = sn->date_nsec; sn_info->vm_clock_nsec = sn->vm_clock_nsec; + sn_info->disk_size = sn->disk_size; } *psn_tab = sn_tab; return s->nb_snapshots; diff --git a/block/qcow2.c b/block/qcow2.c index 62febfc..58b53e1 100644 --- a/block/qcow2.c +++ b/block/qcow2.c @@ -2501,22 +2501,33 @@ static int qcow2_truncate(BlockDriverState *bs, int64_t offset) return -EINVAL; } - /* cannot proceed if image has snapshots */ - if (s->nb_snapshots) { - error_report("Can't resize an image which has snapshots"); + bool v3_truncate = (s->qcow_version == 3); + + /* cannot proceed if image has snapshots and qcow_version is not 3 */ + if (!v3_truncate && s->nb_snapshots) { + error_report("Can't resize an image which has snapshots and " + "qcow_version is not 3"); return -ENOTSUP; } - /* shrinking is currently not supported */ - if (offset < bs->total_sectors * 512) { - error_report("qcow2 doesn't support shrinking images yet"); + /* shrinking is supported from version 3 */ + if (!v3_truncate && offset < bs->total_sectors * 512) { + error_report("qcow2 doesn't support shrinking images yet while" + " qcow_version is not 3"); return -ENOTSUP; } new_l1_size = size_to_l1(s, offset); - ret = qcow2_grow_l1_table(bs, new_l1_size, true); - if (ret < 0) { - return ret; + if (offset < bs->total_sectors * 512) { + ret = shrink_l1_table(bs, new_l1_size); + if (ret < 0) { + return ret; + } + } else { + ret = qcow2_grow_l1_table(bs, new_l1_size, true); + if (ret < 0) { + return ret; + } } /* write updated header.size */ diff --git a/block/qcow2.h b/block/qcow2.h index a063a3c..dab9e48 100644 --- a/block/qcow2.h +++ b/block/qcow2.h @@ -534,6 +534,7 @@ int qcow2_change_refcount_order(BlockDriverState *bs, int refcount_order, void *cb_opaque, Error **errp); /* qcow2-cluster.c functions */ +int shrink_l1_table(BlockDriverState *bs, int64_t new_l1_size); int qcow2_grow_l1_table(BlockDriverState *bs, uint64_t min_size, bool exact_size); int qcow2_write_l1_entry(BlockDriverState *bs, int l1_index); diff --git a/include/block/snapshot.h b/include/block/snapshot.h index e5c0553..7279c12 100644 --- a/include/block/snapshot.h +++ b/include/block/snapshot.h @@ -44,6 +44,7 @@ typedef struct QEMUSnapshotInfo { uint32_t date_sec; /* UTC date of the snapshot */ uint32_t date_nsec; uint64_t vm_clock_nsec; /* VM clock relative to boot */ + uint64_t disk_size; } QEMUSnapshotInfo; int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info,