From nobody Tue Feb 10 04:16:22 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1502961665564456.9719381472264; Thu, 17 Aug 2017 02:21:05 -0700 (PDT) Received: from localhost ([::1]:40550 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1diGzA-0004VT-DD for importer@patchew.org; Thu, 17 Aug 2017 05:21:04 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:45862) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1diGud-0000Hm-2F for qemu-devel@nongnu.org; Thu, 17 Aug 2017 05:16:26 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1diGub-0004fd-GF for qemu-devel@nongnu.org; Thu, 17 Aug 2017 05:16:23 -0400 Received: from mail-db5eur01on0099.outbound.protection.outlook.com ([104.47.2.99]:34144 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 1diGuU-0004EF-Af; Thu, 17 Aug 2017 05:16:14 -0400 Received: from pavelb-Z68P-DS3.sw.ru (195.214.232.6) by HE1PR0802MB2556.eurprd08.prod.outlook.com (2603:10a6:3:e1::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.1341.21; Thu, 17 Aug 2017 09:16:11 +0000 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=LAigXA/yrAMw5OvC8PXoFTLzllXnHPIJ02I9BQX9ryo=; b=OOmDWHgpi9QVltGBd1ZAZYO/P6TS6w3zjRpE/QvfGNNY85x9kPI9PkaKpbrqrn+Rz1+eEuaI6NhMLVQm6S5HTnWS3NyVQCAenYBvTlz1rI7hdDEYlwAVfEDse4vb2aYmHXpGxOP4fQlVRZ/DctH2cZdZHKyXYphSOGAPpi3aq7U= Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=pbutsykin@virtuozzo.com; From: Pavel Butsykin To: qemu-block@nongnu.org, qemu-devel@nongnu.org Date: Thu, 17 Aug 2017 12:15:41 +0300 Message-Id: <20170817091542.9403-4-pbutsykin@virtuozzo.com> X-Mailer: git-send-email 2.14.1 In-Reply-To: <20170817091542.9403-1-pbutsykin@virtuozzo.com> References: <20170817091542.9403-1-pbutsykin@virtuozzo.com> MIME-Version: 1.0 X-Originating-IP: [195.214.232.6] X-ClientProxiedBy: AM5PR0602CA0016.eurprd06.prod.outlook.com (2603:10a6:203:a3::26) To HE1PR0802MB2556.eurprd08.prod.outlook.com (2603:10a6:3:e1::21) X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 9ed32220-7514-434b-b4aa-08d4e5509b11 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:HE1PR0802MB2556; X-Microsoft-Exchange-Diagnostics: 1; HE1PR0802MB2556; 3:kVKBhX3YsB2YLKN2sXez/THQmwhLg/AVWn3vP7jzMD3kDq9QJSwmtg9D9b5DAyFV08OKAqXVm1rhK8DAezf0SKQ8wR7bEjzVsKjRGdIU8CF34rF9Z2o9vlDZlGQAwcHlfU2MMI6RMty+GLUneCdisuz41UJS40bvj0SXdkJBQOV5NHq5QHGUD67dV1ceTZN/3Jplgj6SULIXHVQ9Ske1uHPyi2z8TU5HpET4YOzaHY+o3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Bj6VCEnWdLTNYASooc=; 4:uVykXPQCaEe4x9sxWjRvoiOOkPEvFFnAv8JMlyaolc/QtzaotI9vP2A38YzqI7wO2r5R0t7LSUyUpDaYHklfhyLgVE78JWts5clXu5SSzXj0NhvIJVIhcYj8NH2/ivZ2LjrMn3s8U+1E8Nsg5Sl5MdIMNIkGR5sXvG7lKUtzTD9TPP398GmYAOyj8vToJcTdJDjuYz8NElpqB6WYKhdpEphFdHYSGj4k/Xt9xqzZq+5JdUkmOS2Mp9iywlHpFAqS 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)(601004)(2401047)(8121501046)(5005006)(100000703101)(100105400095)(93006095)(93001095)(10201501046)(3002001)(6041248)(20161123562025)(20161123558100)(20161123560025)(201703131423075)(201702281528075)(201703061421075)(201703061406153)(20161123555025)(20161123564025)(6072148)(201708071742011)(100000704101)(100105200095)(100000705101)(100105500095); SRVR:HE1PR0802MB2556; BCL:0; PCL:0; RULEID:(100000800101)(100110000095)(100000801101)(100110300095)(100000802101)(100110100095)(100000803101)(100110400095)(100000804101)(100110200095)(100000805101)(100110500095); SRVR:HE1PR0802MB2556; X-Forefront-PRVS: 0402872DA1 X-Forefront-Antispam-Report: SFV:NSPM; SFS:(10019020)(7370300001)(4630300001)(6009001)(39830400002)(199003)(189002)(86362001)(105586002)(36756003)(81166006)(50226002)(7736002)(66066001)(189998001)(6116002)(1076002)(97736004)(3846002)(81156014)(8676002)(33646002)(53936002)(107886003)(110136004)(478600001)(6506006)(575784001)(5003940100001)(6486002)(69596002)(48376002)(4326008)(2906002)(50466002)(47776003)(25786009)(6512007)(7350300001)(42186005)(53416004)(6666003)(2950100002)(5660300001)(305945005)(68736007)(50986999)(76176999)(106356001)(101416001); DIR:OUT; SFP:1102; SCL:1; SRVR:HE1PR0802MB2556; H:pavelb-Z68P-DS3.sw.ru; FPR:; SPF:None; PTR:InfoNoRecords; A:1; MX:1; LANG:en; Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: None (protection.outlook.com: virtuozzo.com does not designate permitted sender hosts) X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1; HE1PR0802MB2556; 23:9l1r8WJT3kyUY6Ey83JRc7vNPNtkLdJEMY2kTnL?= =?us-ascii?Q?FzXyLt9AAqAC+67gCoc9vILrvzldWpl2iIKBw44v5WIDu6cWwuDeLEibY/um?= =?us-ascii?Q?EoG98otH0NKzurBHzIpGT3j+u893pVMw95HKw/Isppd0a1mXKimIraGmcAUB?= =?us-ascii?Q?6qmx5ehAPvpCYRAO6jloW5sLZOYueMKxB9hqgcTPHBg2XKN+Ic3jgWSZGblN?= =?us-ascii?Q?wXYjFElX3bslUxToOvtWInYA7Sq02lgJCdK6fviAqAUweMQ3LYspl4rXpCy9?= =?us-ascii?Q?VNg3wP8c8/7r6/mZyB3j3h7oOxfxUnXZxjKul3ljBr0RnvY7e99vwH4kOZwp?= =?us-ascii?Q?fvsxXuGGlsgWKiXDc4erMIRrydiH7TQia7tOS2rzz90Di754ZMBZAlXe9iae?= =?us-ascii?Q?aDh+7ng+RXyc7UqawBny5vCWhM3I1pYDy2UMqeHPMRA1xDq4G1VsgUjeyaJm?= =?us-ascii?Q?pF4y8EpLgNMR+Vpg0+Db9/VTmAZQwy4+eWlOXFrhyKXwfOIDB02Riy7SQLqY?= =?us-ascii?Q?XPhTiHteYCbwQSwnPgO2QSKKsk8BEPa7vqPAG3kY8D3tkO3lzxftCMwFZjeN?= =?us-ascii?Q?Zscl7XGxGkaC1tQz6hYM1fhiodP/cc//Z+fbv2yHZMPnxDYAs4TZ79+D2WdW?= =?us-ascii?Q?g4Jfam9PPk1tQR9bhO1RUUwCQdYqld7rFCcpCXoRSZW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BSrqKMShBxwe99lXZIcP+P70+y5anu5wdf9a9OuIK8fSmqFM8lV5torUnroFt1hmg9A3o6f3Ex58lZBQtYyqwO4vkWij+nAiQ5/YfObHlmhQg1OTP8/J0g==; 24:Pc5n694Y1ETbVhccy3Aqu1GCWkoeWWyjyV9nYCDLK0dNYOL14RZfJU96jRu86ls9SQ92oOSvwFBCE6llRQCB6lJfXSkDWLDS0ktzA/XpQP8=; 7:cA4xquNkkA4I6M+SQEGYKiUR+TgbyWiAMqMYqPENfNdzk69oMEUTgjE0BUK0FOk5/ERSj/A2caj9t0dkQyxifjGK38a0Dtt0sHdq51lDCXm/RVaD/IzBEKJo+wlvAA0fFgKZhnB3AxLhtsORF3sl40mnyDtn6DnnIwx+tghdxRBoj3+yuA53wQgpSYjKgQDxMW1PjFiYfhR/5ommut/MXZI1vTeisFFyZQ0RRPqGWrA= SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-Microsoft-Exchange-Diagnostics: 1; HE1PR0802MB2556; 20:FudCcHw/uwksYqjZhnWCM0w6pw569KYWFZ2saJ/LCVxYaomFXKPtPbhk8d0Og7CCBw31z7b7TsNLJ6ptDIRNeB5IXUtQc1m2TjD9+LvZpLcsQ08J/lddYtsXzDp2N8x1XuaRiEwuy/UrDIVd/+KCxP4MSEwnbPwOlOrOAqdPl2c= X-OriginatorOrg: virtuozzo.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 17 Aug 2017 09:16:11.9262 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-Transport-CrossTenantHeadersStamped: HE1PR0802MB2556 X-detected-operating-system: by eggs.gnu.org: Windows 7 or 8 [fuzzy] X-Received-From: 104.47.2.99 Subject: [Qemu-devel] [PATCH v7 3/4] qcow2: add shrink image support 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, jsnow@redhat.com, armbru@redhat.com, mreitz@redhat.com, den@openvz.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" This patch add shrinking of the image file for qcow2. As a result, this all= ows us to reduce the virtual image size and free up space on the disk without copying the image. Image can be fragmented and shrink is done by punching h= oles in the image file. Signed-off-by: Pavel Butsykin Reviewed-by: Max Reitz --- block/qcow2-cluster.c | 50 +++++++++++++++++++++ block/qcow2-refcount.c | 120 +++++++++++++++++++++++++++++++++++++++++++++= ++++ block/qcow2.c | 43 ++++++++++++++---- block/qcow2.h | 14 ++++++ qapi/block-core.json | 3 +- 5 files changed, 220 insertions(+), 10 deletions(-) diff --git a/block/qcow2-cluster.c b/block/qcow2-cluster.c index f06c08f64c..0c7a9a920c 100644 --- a/block/qcow2-cluster.c +++ b/block/qcow2-cluster.c @@ -32,6 +32,56 @@ #include "qemu/bswap.h" #include "trace.h" =20 +int qcow2_shrink_l1_table(BlockDriverState *bs, uint64_t exact_size) +{ + BDRVQcow2State *s =3D bs->opaque; + int new_l1_size, i, ret; + + if (exact_size >=3D s->l1_size) { + return 0; + } + + new_l1_size =3D exact_size; + +#ifdef DEBUG_ALLOC2 + fprintf(stderr, "shrink l1_table from %d to %d\n", s->l1_size, new_l1_= size); +#endif + + BLKDBG_EVENT(bs->file, BLKDBG_L1_SHRINK_WRITE_TABLE); + ret =3D bdrv_pwrite_zeroes(bs->file, s->l1_table_offset + + new_l1_size * sizeof(uint64_t), + (s->l1_size - new_l1_size) * sizeof(uint64_t)= , 0); + if (ret < 0) { + goto fail; + } + + ret =3D bdrv_flush(bs->file->bs); + if (ret < 0) { + goto fail; + } + + BLKDBG_EVENT(bs->file, BLKDBG_L1_SHRINK_FREE_L2_CLUSTERS); + for (i =3D s->l1_size - 1; i > new_l1_size - 1; i--) { + if ((s->l1_table[i] & L1E_OFFSET_MASK) =3D=3D 0) { + continue; + } + qcow2_free_clusters(bs, s->l1_table[i] & L1E_OFFSET_MASK, + s->cluster_size, QCOW2_DISCARD_ALWAYS); + s->l1_table[i] =3D 0; + } + return 0; + +fail: + /* + * If the write in the l1_table failed the image may contain partially + * overwritten the l1_table. In this case would be better to clear the + * l1_table in memory to avoid possible image corruption. + */ + memset(s->l1_table + exact_size, 0, + (s->l1_size - new_l1_size) * sizeof(uint64_t)); + return ret; +} + int qcow2_grow_l1_table(BlockDriverState *bs, uint64_t min_size, bool exact_size) { diff --git a/block/qcow2-refcount.c b/block/qcow2-refcount.c index 8c17c0e3aa..15af9a795f 100644 --- a/block/qcow2-refcount.c +++ b/block/qcow2-refcount.c @@ -29,6 +29,7 @@ #include "block/qcow2.h" #include "qemu/range.h" #include "qemu/bswap.h" +#include "qemu/cutils.h" =20 static int64_t alloc_clusters_noref(BlockDriverState *bs, uint64_t size); static int QEMU_WARN_UNUSED_RESULT update_refcount(BlockDriverState *bs, @@ -3061,3 +3062,122 @@ done: qemu_vfree(new_refblock); return ret; } + +static int qcow2_discard_refcount_block(BlockDriverState *bs, + uint64_t discard_block_offs) +{ + BDRVQcow2State *s =3D bs->opaque; + uint64_t refblock_offs =3D get_refblock_offset(s, discard_block_offs); + uint64_t cluster_index =3D discard_block_offs >> s->cluster_bits; + uint32_t block_index =3D cluster_index & (s->refcount_block_size - 1); + void *refblock; + int ret; + + assert(discard_block_offs !=3D 0); + + ret =3D qcow2_cache_get(bs, s->refcount_block_cache, refblock_offs, + &refblock); + if (ret < 0) { + return ret; + } + + if (s->get_refcount(refblock, block_index) !=3D 1) { + qcow2_signal_corruption(bs, true, -1, -1, "Invalid refcount:" + " refblock offset %#" PRIx64 + ", reftable index %u" + ", block offset %#" PRIx64 + ", refcount %#" PRIx64, + refblock_offs, + offset_to_reftable_index(s, discard_block_= offs), + discard_block_offs, + s->get_refcount(refblock, block_index)); + qcow2_cache_put(bs, s->refcount_block_cache, &refblock); + return -EINVAL; + } + s->set_refcount(refblock, block_index, 0); + + qcow2_cache_entry_mark_dirty(bs, s->refcount_block_cache, refblock); + + qcow2_cache_put(bs, s->refcount_block_cache, &refblock); + + if (cluster_index < s->free_cluster_index) { + s->free_cluster_index =3D cluster_index; + } + + refblock =3D qcow2_cache_is_table_offset(bs, s->refcount_block_cache, + discard_block_offs); + if (refblock) { + /* discard refblock from the cache if refblock is cached */ + qcow2_cache_discard(bs, s->refcount_block_cache, refblock); + } + update_refcount_discard(bs, discard_block_offs, s->cluster_size); + + return 0; +} + +int qcow2_shrink_reftable(BlockDriverState *bs) +{ + BDRVQcow2State *s =3D bs->opaque; + uint64_t *reftable_tmp =3D + g_malloc(s->refcount_table_size * sizeof(uint64_t)); + int i, ret; + + for (i =3D 0; i < s->refcount_table_size; i++) { + int64_t refblock_offs =3D s->refcount_table[i] & REFT_OFFSET_MASK; + void *refblock; + bool unused_block; + + if (refblock_offs =3D=3D 0) { + reftable_tmp[i] =3D 0; + continue; + } + ret =3D qcow2_cache_get(bs, s->refcount_block_cache, refblock_offs, + &refblock); + if (ret < 0) { + goto out; + } + + /* the refblock has own reference */ + if (i =3D=3D offset_to_reftable_index(s, refblock_offs)) { + uint64_t block_index =3D (refblock_offs >> s->cluster_bits) & + (s->refcount_block_size - 1); + uint64_t refcount =3D s->get_refcount(refblock, block_index); + + s->set_refcount(refblock, block_index, 0); + + unused_block =3D buffer_is_zero(refblock, s->cluster_size); + + s->set_refcount(refblock, block_index, refcount); + } else { + unused_block =3D buffer_is_zero(refblock, s->cluster_size); + } + qcow2_cache_put(bs, s->refcount_block_cache, &refblock); + + reftable_tmp[i] =3D unused_block ? 0 : cpu_to_be64(s->refcount_tab= le[i]); + } + + ret =3D bdrv_pwrite_sync(bs->file, s->refcount_table_offset, reftable_= tmp, + s->refcount_table_size * sizeof(uint64_t)); + /* + * If the write in the reftable failed the image may contain partially + * overwritten the reftable. In this case would be better to clear the + * reftable in memory to avoid possible image corruption. + */ + for (i =3D 0; i < s->refcount_table_size; i++) { + if (s->refcount_table[i] && !reftable_tmp[i]) { + if (ret =3D=3D 0) { + ret =3D qcow2_discard_refcount_block(bs, s->refcount_table= [i] & + REFT_OFFSET_MASK); + } + s->refcount_table[i] =3D 0; + } + } + + if (!s->cache_discards) { + qcow2_process_discards(bs, ret); + } + +out: + g_free(reftable_tmp); + return ret; +} diff --git a/block/qcow2.c b/block/qcow2.c index 40ba26c111..d84caa0694 100644 --- a/block/qcow2.c +++ b/block/qcow2.c @@ -3121,18 +3121,43 @@ static int qcow2_truncate(BlockDriverState *bs, int= 64_t offset, } =20 old_length =3D bs->total_sectors * 512; + new_l1_size =3D size_to_l1(s, offset); =20 - /* shrinking is currently not supported */ if (offset < old_length) { - error_setg(errp, "qcow2 doesn't support shrinking images yet"); - return -ENOTSUP; - } + if (prealloc !=3D PREALLOC_MODE_OFF) { + error_setg(errp, + "Preallocation can't be used for shrinking an image= "); + return -EINVAL; + } =20 - new_l1_size =3D size_to_l1(s, offset); - ret =3D qcow2_grow_l1_table(bs, new_l1_size, true); - if (ret < 0) { - error_setg_errno(errp, -ret, "Failed to grow the L1 table"); - return ret; + ret =3D qcow2_cluster_discard(bs, ROUND_UP(offset, s->cluster_size= ), + old_length - ROUND_UP(offset, + s->cluster_size), + QCOW2_DISCARD_ALWAYS, true); + if (ret < 0) { + error_setg_errno(errp, -ret, "Failed to discard cropped cluste= rs"); + return ret; + } + + ret =3D qcow2_shrink_l1_table(bs, new_l1_size); + if (ret < 0) { + error_setg_errno(errp, -ret, + "Failed to reduce the number of L2 tables"); + return ret; + } + + ret =3D qcow2_shrink_reftable(bs); + if (ret < 0) { + error_setg_errno(errp, -ret, + "Failed to discard unused refblocks"); + return ret; + } + } else { + ret =3D qcow2_grow_l1_table(bs, new_l1_size, true); + if (ret < 0) { + error_setg_errno(errp, -ret, "Failed to grow the L1 table"); + return ret; + } } =20 switch (prealloc) { diff --git a/block/qcow2.h b/block/qcow2.h index 52c374e9ed..5a289a81e2 100644 --- a/block/qcow2.h +++ b/block/qcow2.h @@ -521,6 +521,18 @@ static inline uint64_t refcount_diff(uint64_t r1, uint= 64_t r2) return r1 > r2 ? r1 - r2 : r2 - r1; } =20 +static inline +uint32_t offset_to_reftable_index(BDRVQcow2State *s, uint64_t offset) +{ + return offset >> (s->refcount_block_bits + s->cluster_bits); +} + +static inline uint64_t get_refblock_offset(BDRVQcow2State *s, uint64_t off= set) +{ + uint32_t index =3D offset_to_reftable_index(s, offset); + return s->refcount_table[index] & REFT_OFFSET_MASK; +} + /* qcow2.c functions */ int qcow2_backing_read1(BlockDriverState *bs, QEMUIOVector *qiov, int64_t sector_num, int nb_sectors); @@ -584,10 +596,12 @@ int qcow2_inc_refcounts_imrt(BlockDriverState *bs, Bd= rvCheckResult *res, int qcow2_change_refcount_order(BlockDriverState *bs, int refcount_order, BlockDriverAmendStatusCB *status_cb, void *cb_opaque, Error **errp); +int qcow2_shrink_reftable(BlockDriverState *bs); =20 /* qcow2-cluster.c functions */ int qcow2_grow_l1_table(BlockDriverState *bs, uint64_t min_size, bool exact_size); +int qcow2_shrink_l1_table(BlockDriverState *bs, uint64_t max_size); int qcow2_write_l1_entry(BlockDriverState *bs, int l1_index); int qcow2_decompress_cluster(BlockDriverState *bs, uint64_t cluster_offset= ); int qcow2_encrypt_sectors(BDRVQcow2State *s, int64_t sector_num, diff --git a/qapi/block-core.json b/qapi/block-core.json index 833c602150..d6172bfe15 100644 --- a/qapi/block-core.json +++ b/qapi/block-core.json @@ -2495,7 +2495,8 @@ 'cluster_alloc_bytes', 'cluster_free', 'flush_to_os', 'flush_to_disk', 'pwritev_rmw_head', 'pwritev_rmw_after_head', 'pwritev_rmw_tail', 'pwritev_rmw_after_tail', 'pwritev', - 'pwritev_zero', 'pwritev_done', 'empty_image_prepare' ] } + 'pwritev_zero', 'pwritev_done', 'empty_image_prepare', + 'l1_shrink_write_table', 'l1_shrink_free_l2_clusters' ] } =20 ## # @BlkdebugInjectErrorOptions: --=20 2.14.1