From patchwork Wed Sep 20 13:58:33 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pavel Butsykin X-Patchwork-Id: 9961605 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 0A0C86056A for ; Wed, 20 Sep 2017 15:01:39 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id ED27D29185 for ; Wed, 20 Sep 2017 15:01:38 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id E20C52918E; Wed, 20 Sep 2017 15:01:38 +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=BAD_ENC_HEADER,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 4797D29198 for ; Wed, 20 Sep 2017 15:01:36 +0000 (UTC) Received: from localhost ([::1]:48665 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dugUV-0005EN-Po for patchwork-qemu-devel@patchwork.kernel.org; Wed, 20 Sep 2017 11:00:43 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:51252) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dufWh-000594-LN for qemu-devel@nongnu.org; Wed, 20 Sep 2017 09:58:58 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dufWg-0006xN-MC for qemu-devel@nongnu.org; Wed, 20 Sep 2017 09:58:55 -0400 Received: from mail-db5eur01on0090.outbound.protection.outlook.com ([104.47.2.90]:29273 helo=EUR01-DB5-obe.outbound.protection.outlook.com) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dufWZ-0006uU-6G; Wed, 20 Sep 2017 09:58:47 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=virtuozzo.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version; bh=g+FfzxMgFUiuABl+HO38zfwUzJ4JtG7iuAwtJ2VD1sY=; b=SEu8HMTpEf/eAj05s6TcEJl8gc1nfGeCQV/ITIqH1nhv7CZRn+XlWvZMHEXaOODgLZc0Bo4lb819T+vGqajnoFN2sE/PDGXThSxd7G3F8AuU+hAT1zxndY0FdbouRxLUjuv6Qgq+MothV9cz02PTVoyCORJsk2q05k2MxNsPHvY= Received: from pavelb-Z68P-DS3.sw.ru (195.214.232.6) by HE1PR0802MB2554.eurprd08.prod.outlook.com (2603:10a6:3:e1::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P256) id 15.20.56.11; Wed, 20 Sep 2017 13:58:44 +0000 From: Pavel Butsykin To: qemu-block@nongnu.org, qemu-devel@nongnu.org Date: Wed, 20 Sep 2017 16:58:33 +0300 Message-Id: <20170920135833.20472-3-pbutsykin@virtuozzo.com> X-Mailer: git-send-email 2.14.1 In-Reply-To: <20170920135833.20472-1-pbutsykin@virtuozzo.com> References: <20170920135833.20472-1-pbutsykin@virtuozzo.com> MIME-Version: 1.0 X-Originating-IP: [195.214.232.6] X-ClientProxiedBy: AM5PR0602CA0024.eurprd06.prod.outlook.com (2603:10a6:203:a3::34) To HE1PR0802MB2554.eurprd08.prod.outlook.com (2603:10a6:3:e1::19) X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 3a0d491b-5265-47c0-5fca-08d5002fb567 X-Microsoft-Antispam: UriScan:; BCL:0; PCL:0; RULEID:(300000500095)(300135000095)(300000501095)(300135300095)(22001)(300000502095)(300135100095)(2017030254152)(300000503095)(300135400095)(201703131423075)(201703031133081)(201702281549075)(300000504095)(300135200095)(300000505095)(300135600095)(300000506095)(300135500095); SRVR:HE1PR0802MB2554; X-Microsoft-Exchange-Diagnostics: 1; HE1PR0802MB2554; 3:W7oIfzRurbONOjhplviH8w7qM9CNa5ROLAPT7i3TuzfEiOmznQlPzo+Vg7gfK7Ul6PLSDGyhPq6ldOrEcxd19oskl83ClL9OOS9ZSlgv3e+n+14s5JSIaTUsyWNvNvhBu5z5EGTrINUNxjh3ULKNKdhRSmL1NCgJipm0HmAr5fVSNEW0QzGQembJzRtJqJMOancPGdiqFzqfFRSpw9ErojZ2MZ104AaNygDwCH+zc8gksi1lPW9NA38CRfnJYup0; 25:RhmczFxi3HvQGPmvp9FOZV5i6C5slP/bNS6xz4ZleL5mDmyISR8zXaDMlL+EmqxOK0Ry9q3jNpiKa+Fqo5vy6VaFvVcDcg2u0YLagKwieS3w48X9iP2fwI27zDH/uqZLosXEFR3nLsk5NXXWbQ7OKkkBsvkOIjmcFNdls7tDyqDNVsBkMs4PZjmG2HtX+DsUVilf+lM4s2ZCY/vHSA8t+ooFhn+67h2eJBU8zboWTH4JWJJ1Q/DYVow6WQh4vAyGaYKcfrw1xzGAgW94LT3eQuR85j9l9114z0G8rivO53+1xTZbn+UyLd4GBOskos36PBb4P185EWUyOd4i7JQLtg==; 31:aGRyAxIG0uyt23yYWtHJh78AxOZ9Nsuz4cRD+z9M024W9yNyuiC36w/GcnfaGsY1vYO2JHMiRi7hxwDD3olZTeol6qoGRB+2iajnBdUXXTP43W0MOaXa8rbQRmkcCMK580dkl269IcgOGH9D3eob/zJUYtpgrr3gC5VBte2HHECMyPuxkgT+BAuZfCN9TQ/eOhgW4kPkPZn7k/Pqz2r0L5Q//TQ0CIf4WJTIUr2Cn1Y= X-MS-TrafficTypeDiagnostic: HE1PR0802MB2554: Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=pbutsykin@virtuozzo.com; X-Microsoft-Exchange-Diagnostics: 1; HE1PR0802MB2554; 20:z6DmKrKXPB88PUR6omg1WSCZurHkrCKAa7AxquBr2GJcCttcZihxlanlY80Zs6/5wWsLIKONjdjIKOwNWwKwNxcXyYrJeJ9543oY41DodtDeCk+8FT+EaHmhV0QHnqWMNrZk7MWshtvCKIBfdYuDvN45UVyjVFejuc/wKe5YqP/lc1dkgDHEQv2oq0nuzOnmwF8C8qcyk/uanKcYGQ7jeawpOBkVKDDr/W+CrXH6MYTjnhhmU6ph92ZvvYjx8AxEZELSN+7sKnPU0cadyXdUi1Jgm5QoOvOAAhWNeYtZnOh9J4BF/GL+TaC3pPzKnqIz4NXMrAqEaon6NYMUIWNu+NYeinrUZ4V3yYcUd+8uy9UiFu/Yo2e33bERjvXiq5NclmdRX6btfMSiRpt7j2Qmd7HztzKDuhhvu+J0mVOqbVs=; 4:RDayVjHNJwtJvf2msGh7A1Dw3Wz9yNQR0Vl5uFzpdT87jvq08IXNIZofGqNBlgouEQgFyIu6076i2m722Le9+uPfZT9rZ39UW3q29XbgkD/kHdZ2+7EQN7SlWtdT/NqHCPJcnNdP8gjXBYqom1bK4tynt77VbArQ9P0bafvA0Kx+19JtN4iyZcyxFh+Bm9HP1kFToilqkCckvjWfYBPh4FEJBX5AGJCSjs98J1DIvZMmTSoki/IbfO1Y9HY2GrR3 X-Exchange-Antispam-Report-Test: UriScan:; X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(100000700101)(100105000095)(100000701101)(100105300095)(100000702101)(100105100095)(6040450)(2401047)(5005006)(8121501046)(10201501046)(100000703101)(100105400095)(3002001)(93006095)(93001095)(6041248)(201703131423075)(201702281528075)(201703061421075)(201703061406153)(20161123558100)(20161123555025)(20161123560025)(20161123564025)(20161123562025)(6072148)(201708071742011)(100000704101)(100105200095)(100000705101)(100105500095); SRVR:HE1PR0802MB2554; BCL:0; PCL:0; RULEID:(100000800101)(100110000095)(100000801101)(100110300095)(100000802101)(100110100095)(100000803101)(100110400095)(100000804101)(100110200095)(100000805101)(100110500095); SRVR:HE1PR0802MB2554; X-Forefront-PRVS: 04362AC73B X-Forefront-Antispam-Report: SFV:NSPM; SFS:(10019020)(7370300001)(4630300001)(6009001)(346002)(376002)(199003)(189002)(4326008)(2906002)(5003940100001)(33646002)(81156014)(16526017)(50986999)(8676002)(86362001)(76176999)(81166006)(101416001)(50226002)(97736004)(53936002)(68736007)(189998001)(16586007)(107886003)(6512007)(305945005)(7736002)(316002)(478600001)(25786009)(36756003)(5660300001)(69596002)(47776003)(50466002)(66066001)(48376002)(7350300001)(6116002)(3846002)(105586002)(1076002)(53416004)(2950100002)(6486002)(6666003)(106356001)(6506006); DIR:OUT; SFP:1102; SCL:1; SRVR:HE1PR0802MB2554; H:pavelb-Z68P-DS3.sw.ru; FPR:; SPF:None; PTR:InfoNoRecords; A:1; MX:1; LANG:en; Received-SPF: None (protection.outlook.com: virtuozzo.com does not designate permitted sender hosts) X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1; HE1PR0802MB2554; 23:2oZ9fu1lncYf2h0aUX/MNxd1V10MKEwbTCNCCZN?= =?us-ascii?Q?Jtg+DbO81v7xq7/XRtr9NJnrWMszLF2Hg7Js3/BHEYQQzzXdMxIpyXA2oo9P?= =?us-ascii?Q?u9mTbY0Ba/M1T1ic0pPEwClA1fyK1pK+hKol5flxdbPGyQ56TqIyTN+TLwnK?= =?us-ascii?Q?2dN+YC6pDQtriFPqO0zwidVrIVsHqOojr0FlF7ofokBUsT20kjNXfkB+89b2?= =?us-ascii?Q?IUdqwn1XlLSq2MuXJzomdDo9Y5BKWgg+c6GbrpUnoNl5KLlNNrUEclUmGMex?= =?us-ascii?Q?x8FQXFEg+kR6FRWXW/eNCQl8MvcPfjvMEOqQat3Psv30r+xWdkAdfzQZ5VHy?= =?us-ascii?Q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exchange-Diagnostics: 1; HE1PR0802MB2554; 6:nqCjF8Mm6RSFQPa9MDEF6f5Fdr8idETrhhK63HuMLymGRv7cW7XkhWp2yVgUi0dmvmCAVPTFj/YdQ014rg47m99eEoOl0pUSZQOWPOm/VeFwt0yO++Rr8BLsyOaYInCj1SE+GQcEVW0UbWz4rZBqGViIFan2An+by423uvbPkwpa7b4qipAbekoYoXwO8kFCxTKF+3N3kBpi2VNtKl4Kq8DjetTQcp3o9wcQPWcJpi+qrZjYZXKxrdOA2iPkIrGOfwmK5KG0c9h0DqDms3UJ+T7A239N+tP/Wv+DpY/9BgcKuh9ynaUWLvelaegbkf1FHH0jY+OoZ9hM6j4Ox0Vx4A==; 5:Gj5UVD8Sh2gRAwCbRT02h2SWJZ6Pe6dQ54CozLCKXQaQw0T6KwDAPwYoyYuKNouZEzCbZiVCD/9QwrNd2+UzPquEfikWsZuAYuLRkTkShivUYG5NY5266MtH7PxoFbOnYWgttdVGo8WF7S+k422h6g==; 24:dtaGPZFTrA8aBvBOuaf7UAGuZsa8Rz4frN78j1AV+vR5QouKE5oylOpczG+604jmc110WXYCAsSwFInsKfG8rW+agjrMlZhh+iGgRU2tgmA=; 7:+ZydSyguNE66l+Y1PRaxYa4OJf7J0FmwCP0BqDQELPY2ecYcw3TnowK+E0AB7/7uBlSDXrNILlz5gM1+/iVoSe+sXwbD9C1nDcaUWwVhpFQ4HAu4Y1K0UYCEeEYiNBNSy1u+utsafqVDk4O764o+uIvfOYZuzNuelQ5pv5rzCxEokNmNbIOHO+mEAro+Qy08wH75QsVH7M0UrZw18Z3/8JIKgTHI8GejME97nr0UKko= SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-Microsoft-Exchange-Diagnostics: 1; HE1PR0802MB2554; 20:hR0Lp5kqOE/21N9EapT72BD/IBajcE4D4JatNR13jlcyoX23V3RkhM7pXArN2ShxyqzVGBWz1FKKEmxWk5WMm9vx9YWBsS1aJvcfyjX78oa5ZpH7GVKjJ2cig69VNq4q01+8crM1z2qlaxvOWecJjLn9s7rlb5KBQdjIxDAR+/g= X-OriginatorOrg: virtuozzo.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Sep 2017 13:58:44.2832 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-Transport-CrossTenantHeadersStamped: HE1PR0802MB2554 X-detected-operating-system: by eggs.gnu.org: Windows 7 or 8 [fuzzy] X-Received-From: 104.47.2.90 Subject: [Qemu-devel] [PATCH 2/2] qcow2: truncate the tail of the image file after shrinking the image 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, den@openvz.org, mreitz@redhat.com, pbutsykin@virtuozzo.com Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP Now after shrinking the image, at the end of the image file, there might be a tail that probably will never be used. So we can find the last used cluster and cut the tail. Signed-off-by: Pavel Butsykin Reviewed-by: John Snow --- block/qcow2-refcount.c | 21 +++++++++++++++++++++ block/qcow2.c | 19 +++++++++++++++++++ block/qcow2.h | 1 + 3 files changed, 41 insertions(+) diff --git a/block/qcow2-refcount.c b/block/qcow2-refcount.c index 88d5a3f1ad..5e221a166c 100644 --- a/block/qcow2-refcount.c +++ b/block/qcow2-refcount.c @@ -3181,3 +3181,24 @@ out: g_free(reftable_tmp); return ret; } + +int64_t qcow2_get_last_cluster(BlockDriverState *bs, int64_t size) +{ + BDRVQcow2State *s = bs->opaque; + int64_t i, last_cluster, nb_clusters = size_to_clusters(s, size); + uint64_t refcount; + + for (i = 0, last_cluster = 0; i < nb_clusters; i++) { + int ret = qcow2_get_refcount(bs, i, &refcount); + if (ret < 0) { + fprintf(stderr, "Can't get refcount for cluster %" PRId64 ": %s\n", + i, strerror(-ret)); + continue; + } + + if (refcount > 0) { + last_cluster = i; + } + } + return last_cluster; +} diff --git a/block/qcow2.c b/block/qcow2.c index 8a4311d338..c3b6dd44c4 100644 --- a/block/qcow2.c +++ b/block/qcow2.c @@ -3106,6 +3106,7 @@ static int qcow2_truncate(BlockDriverState *bs, int64_t offset, new_l1_size = size_to_l1(s, offset); if (offset < old_length) { + int64_t image_end_offset, old_file_size; if (prealloc != PREALLOC_MODE_OFF) { error_setg(errp, "Preallocation can't be used for shrinking an image"); @@ -3134,6 +3135,24 @@ static int qcow2_truncate(BlockDriverState *bs, int64_t offset, "Failed to discard unused refblocks"); return ret; } + + old_file_size = bdrv_getlength(bs->file->bs); + if (old_file_size < 0) { + error_setg_errno(errp, -old_file_size, + "Failed to inquire current file length"); + return old_file_size; + } + image_end_offset = (qcow2_get_last_cluster(bs, old_file_size) + 1) * + s->cluster_size; + if (image_end_offset < old_file_size) { + ret = bdrv_truncate(bs->file, image_end_offset, + PREALLOC_MODE_OFF, NULL); + if (ret < 0) { + error_setg_errno(errp, -ret, + "Failed to truncate the tail of the image"); + return ret; + } + } } else { ret = qcow2_grow_l1_table(bs, new_l1_size, true); if (ret < 0) { diff --git a/block/qcow2.h b/block/qcow2.h index 5a289a81e2..782a206ecb 100644 --- a/block/qcow2.h +++ b/block/qcow2.h @@ -597,6 +597,7 @@ int qcow2_change_refcount_order(BlockDriverState *bs, int refcount_order, BlockDriverAmendStatusCB *status_cb, void *cb_opaque, Error **errp); int qcow2_shrink_reftable(BlockDriverState *bs); +int64_t qcow2_get_last_cluster(BlockDriverState *bs, int64_t size); /* qcow2-cluster.c functions */ int qcow2_grow_l1_table(BlockDriverState *bs, uint64_t min_size,