From patchwork Thu Sep 28 09:27:50 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pavel Butsykin X-Patchwork-Id: 9975581 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 3D524603F2 for ; Thu, 28 Sep 2017 09:33:02 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2EA182953C for ; Thu, 28 Sep 2017 09:33:02 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 235E829540; Thu, 28 Sep 2017 09:33:02 +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 678342953C for ; Thu, 28 Sep 2017 09:33:00 +0000 (UTC) Received: from localhost ([::1]:58161 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dxVBj-0002ft-D3 for patchwork-qemu-devel@patchwork.kernel.org; Thu, 28 Sep 2017 05:32:59 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:34423) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dxV93-0001Qe-2A for qemu-devel@nongnu.org; Thu, 28 Sep 2017 05:30:17 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dxV8x-00005v-52 for qemu-devel@nongnu.org; Thu, 28 Sep 2017 05:30:13 -0400 Received: from mail-eopbgr30098.outbound.protection.outlook.com ([40.107.3.98]:25712 helo=EUR03-AM5-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 1dxV8q-0008LK-Uc; Thu, 28 Sep 2017 05:30:01 -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=+6pdc/cg3hrgw+b72s8NRwkiGFkbQEHqeNnbH1uwkBc=; b=ZoF89OL0xp1pJExKzZL1NXk8UOazasxCzw3xuNqn2w2dffy7+8mBZb9wfk4VS2EF2HKqq3KX5luoLyrwgiZLhCOweqSVVY6BMjLJ6tfLR/l3fB7Iac7lV/DznuID98kb/CqV29RvqYLOwkmWpYJBhYe/eg1eY0PY4/h/IgNVXAs= Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=pbutsykin@virtuozzo.com; Received: from pavelb-Z68P-DS3.sw.ru (195.214.232.6) by VI1PR0802MB2557.eurprd08.prod.outlook.com (2603:10a6:800:ae::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P256) id 15.20.77.7; Thu, 28 Sep 2017 09:29:57 +0000 From: Pavel Butsykin To: qemu-block@nongnu.org, qemu-devel@nongnu.org Date: Thu, 28 Sep 2017 12:27:50 +0300 Message-Id: <20170928092750.24938-3-pbutsykin@virtuozzo.com> X-Mailer: git-send-email 2.14.1 In-Reply-To: <20170928092750.24938-1-pbutsykin@virtuozzo.com> References: <20170928092750.24938-1-pbutsykin@virtuozzo.com> MIME-Version: 1.0 X-Originating-IP: [195.214.232.6] X-ClientProxiedBy: DB6PR1001CA0048.EURPRD10.PROD.OUTLOOK.COM (2603:10a6:4:55::34) To VI1PR0802MB2557.eurprd08.prod.outlook.com (2603:10a6:800:ae::7) X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 8c6b71a4-51c7-4c51-ac43-08d506537cb4 X-Microsoft-Antispam: UriScan:; BCL:0; PCL:0; RULEID:(22001)(2017030254152)(2017052603199)(201703131423075)(201703031133081)(201702281549075); SRVR:VI1PR0802MB2557; X-Microsoft-Exchange-Diagnostics: 1; VI1PR0802MB2557; 3:CokclorACK4TYCVzsVR7Vi+qbZb5PsVCeHozUcXjq3Sep8RiEX9WCzzaqnOchJvMxTh2DD5ff1dVLWMWOSNTz3ONbjIxBIX3MYKayl2eE2U83zT4wsT5jMtOKdhiaE6RPxo8KEpJHranMAo1TTwx4GcpW45yMWNf+9vDNWIH7ttmWwIVEihpGr2N8lQrEiiHokqOBJG7gUKXERXeKHayt4xUi4Wc6BAScS48HXZ26wO6DqCLl69kB0x0WNNmAYBd; 25:eLFaslVH0EnBz4reb05OUX08GfFgYT2jEnchE+N76kmXBHnxCRKRq1KCIq27q1R8nzFg/on5i8+sKXhkMCv8eEgXC3xYsbAbtMleGkodAWUkjIurG/sKi+X4cTYz1Fs+YEqMlt3OIiZz+XkJnMu+ysIVbAZAbeuogcwuhC3L42b56W/Zhp6c1z+Ys2gqCCkiJiPFeEWtaff5pSyv3axDALbf5+BQcNI4lqibhcryZPXnVg0vwveeESS93mCwEjgmuc24C1BhgCAv/Gm6bYzBmR4OKdUyw70iuVuzkrOhkx2Ahc8Bk20znEw+lwrnRP/guY/Yuxka2B8R/xsZxUwpkg==; 31:/3hAKIi7Fi96GAe8qUw4MNzQjs6Zn4E/IkPcWd4qbOmKSrNTUX6/fyxMbv0qVOA6cnS/BP6/+oUJXYmOi5ZU46ZBXd9zpHspVgEjIoebh4fafW+si1+huqttNxmObI2ZAtnHZL0S9M+33iLevwMTftxbCknld/qShbr7mizo7UWHIOXZqOV78uM2bLR2BnRDIShF5JSGqdTq7q57IuGS7xqpS0JpJ7S6Jvp5LQ4DqXU= X-MS-TrafficTypeDiagnostic: VI1PR0802MB2557: X-Microsoft-Exchange-Diagnostics: 1; VI1PR0802MB2557; 20:LUvbud1K3VLXWIRqel/R357GFtsZ8cdhIDnf5tJkGbbKpHzG8jTUZzsoudCS/M3OB6PnT+ANUya232lXgOcfftz+IdVLh2Ge+DRhM2dNKClSf6pO8tHhAYldrTgdoy2e4zhFORmYEKZ2PcwXNSC+Hu5kQAbRZvNFw6eb1HdEVUxP2mKSnFNqEHhUMSrHeddJfXwaLfO3+EZyyrP/PY9FAMTv/lO4VD9ItadWp0tF4qAhliZNQ5cpWwrFbgflPW54vPgBD4+BUA4DYPLrx9sWFyYZAkVgzC2XIX2h+WSPTec1gQEunA/ATfKG+YunLnJdivgmuitTyyiWEtdJn5brxIMVYDiVP7J8GEFfpH+zxVrf/5LIYktS1jAyvPshfrSsTQgwDQWlzLDrguOS3B+l1yElSApNhLCIQfSCh3MyJG0=; 4:w1hpoDzheI2RjokDBBOSlDV+w15mO1AvUdVMnn7pB7/beE7CIdoSpz6sN2w/R98j5ATkZjB1WqRx5G+7n/s2LCQx7Mco0K1NIATzRsnPmMqvw8bhcwWpZUr5BdMfaoGBcl2rqc6hDRTHZyX4VHWoCvcpG0sUBiouQa0Iqent2pNxCjbX3A0+qAW0iYPhwLc9pCafBPMhc4mXY6gDVHy1GrugDbWCcYMw88boHN+Atqf+6qrB5rX4xJRZcTfU5n1N 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)(8121501046)(5005006)(3002001)(93006095)(93001095)(10201501046)(100000703101)(100105400095)(6041248)(20161123558100)(201703131423075)(201702281528075)(201703061421075)(201703061406153)(20161123562025)(20161123555025)(20161123560025)(20161123564025)(6072148)(201708071742011)(100000704101)(100105200095)(100000705101)(100105500095); SRVR:VI1PR0802MB2557; BCL:0; PCL:0; RULEID:(100000800101)(100110000095)(100000801101)(100110300095)(100000802101)(100110100095)(100000803101)(100110400095)(100000804101)(100110200095)(100000805101)(100110500095); SRVR:VI1PR0802MB2557; X-Forefront-PRVS: 0444EB1997 X-Forefront-Antispam-Report: SFV:NSPM; SFS:(10019020)(6009001)(376002)(346002)(199003)(189002)(16526017)(50986999)(478600001)(6486002)(86362001)(50226002)(6506006)(106356001)(316002)(66066001)(105586002)(47776003)(189998001)(5003940100001)(5660300001)(33646002)(7736002)(53936002)(97736004)(16586007)(76176999)(101416001)(305945005)(6666003)(107886003)(69596002)(4326008)(68736007)(2906002)(2950100002)(1076002)(48376002)(50466002)(53416004)(6512007)(8936002)(25786009)(6116002)(3846002)(81166006)(8676002)(81156014)(36756003); DIR:OUT; SFP:1102; SCL:1; SRVR:VI1PR0802MB2557; 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; VI1PR0802MB2557; 23:YnzRgnz32FQZH5dcABeZA3gSB7fpIwzRo72YPAR?= =?us-ascii?Q?KUZ6ZptSoustfrD7rK781o4NvcxOT97OvQ762JWwNE9c+2k6Jbi9tWDMDr9I?= =?us-ascii?Q?UHkSzryMo8kdLE9qzEJt1Fh4KNYFYeSbDQmtb3PJurAZIrJq3ZXAsIXmwsid?= =?us-ascii?Q?hbKYwVrG5QulwnulEpLJxE5aaecvALbuHZmdbxMFkfd+YqdlcEN/38s8GcdA?= =?us-ascii?Q?rofU7tjhF1M6gypFcbL0WwGiRCpUjWPgqqlr5YI5MULhoU1bobA9LmtDCzKT?= =?us-ascii?Q?BARu7PUCt0dHKz9aY6w0IUjFYMpt1i/aGky9vTn++pLNAxSoFnqvvcrSI6w+?= =?us-ascii?Q?yjNqw3oqSnpVYXaXZhMPICM+xgQeLIKxs5pWVZlyJUUWIbX1nV+hjRCNDlR9?= =?us-ascii?Q?P8RLffTpZDZj6ZOxsIr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6MG+rJsJaA37/NnL/1yoc+CroRc3SLenf1XJ5fIcgMVQYcwSFf+mAPImBjb0WpkQ4ACs4H5B57iMVXUYgDlsCr6OaqerAs/gvWon5mmRdIn/oYhrl6aFxB2fGuxtwAVTo5PdmERZj8JnH1ZG7x/KO+fgb/6JWEgRKNY7HxaqYTgVwIdTrAjF7L80KBO1sChw==; 5:mA/STZGHzr5d6CC0u/er/u74pCdsxsHf+MTqxCpnKRjuAyoXrtdLcIVGdiDoaHhMGE58qLIJopPx7uuvhui7/9lE7r2LKAPo8PW5ff7zoW+tMeW8bG/wXz9ni9U7leEPLAr2PNg7U6NyXwXJj3zFrA==; 24:Hbut5SO5HMrDOsJpjOIUQx0ZwUoOEHRqrGa7O/d2gLDDOyq1aLBTbGejAZXdw1wfQp9WOQt/xaY/0H+aONF/HtXKl2fXYObu0VsBpZ/LOMA=; 7:6RsZUiFnvi+5Ew065yfjZDyL2ExfqZHVYD8LSjXWPB8qWaoSFMfHpjM973ehEtJJJsUrT26r/03LlOR7GVogBCa1qdmYYwYfG3+IY7J890Yf/8oxIcVDnnw3dNL5cPUKyr0BuxtYmoU1WYHl2zcq0LATQFEQTeee1dYH/PEAg+4rFQzC7KeOGsFyFXxaYLqA5lm/JTiqKglJWaOYp9bjwRbeb2T7Z+lw3u6V18wixxs= SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-Microsoft-Exchange-Diagnostics: 1; VI1PR0802MB2557; 20:iXKhHFT1FDjNdMOpigoDi3dv5zctnkIUE088ZspOCngehoSjg0uSOKCAwnkPGVQ1CqWFzj8T1GQ0ysWCuxTK5GxCFLJzwDOgYZaPPFFDmLzhiFKnq7tSiIOY020uOy1q6M/3lpQhJE5vykeRedL2ddrnDs3W1JPCRQtfc6GtScE= X-OriginatorOrg: virtuozzo.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 28 Sep 2017 09:29:57.6904 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 0bc7f26d-0264-416e-a6fc-8352af79c58f X-MS-Exchange-Transport-CrossTenantHeadersStamped: VI1PR0802MB2557 X-detected-operating-system: by eggs.gnu.org: Windows 7 or 8 [fuzzy] X-Received-From: 40.107.3.98 Subject: [Qemu-devel] [PATCH v3 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, pbutsykin@virtuozzo.com, mreitz@redhat.com, den@openvz.org, jsnow@redhat.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 | 22 ++++++++++++++++++++++ block/qcow2.c | 23 +++++++++++++++++++++++ block/qcow2.h | 1 + 3 files changed, 46 insertions(+) diff --git a/block/qcow2-refcount.c b/block/qcow2-refcount.c index 88d5a3f1ad..aa3fd6cf17 100644 --- a/block/qcow2-refcount.c +++ b/block/qcow2-refcount.c @@ -3181,3 +3181,25 @@ 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; + + for (i = size_to_clusters(s, size) - 1; i >= 0; i--) { + uint64_t refcount; + 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)); + return ret; + } + if (refcount > 0) { + return i; + } + } + qcow2_signal_corruption(bs, true, -1, -1, + "There are no references in the refcount table."); + return -EIO; +} diff --git a/block/qcow2.c b/block/qcow2.c index 8a4311d338..f08c69ccd9 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 last_cluster, old_file_size; if (prealloc != PREALLOC_MODE_OFF) { error_setg(errp, "Preallocation can't be used for shrinking an image"); @@ -3134,6 +3135,28 @@ 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; + } + last_cluster = qcow2_get_last_cluster(bs, old_file_size); + if (last_cluster < 0) { + error_setg_errno(errp, -last_cluster, + "Failed to find the last cluster"); + return last_cluster; + } + if ((last_cluster + 1) * s->cluster_size < old_file_size) { + ret = bdrv_truncate(bs->file, (last_cluster + 1) * s->cluster_size, + PREALLOC_MODE_OFF, NULL); + if (ret < 0) { + warn_report("Failed to truncate the tail of the image: " + "ret = %d", ret); + ret = 0; + } + } } 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,