From nobody Tue Apr 8 23:36:19 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1598345359; cv=none; d=zohomail.com; s=zohoarc; b=NFDtCMDO4E3lVNLlxiFNE0akBrwUvVtk5qcn2Szga5Y3ayidVvRwuYt/Smd58xynXkS77JIi6R6J1miOLkmysQ5ri5tcGxT5x+n396QyBYAAtz5hIXI2WMfHTqJG4SuxEnOBWoYa7q6VDNprFUNyP8LWkbMEm7O8YJdDcBHtLTY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1598345359; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=UG6SkOayHtZafH5MCyV+bvGceoz5k9ttBs5EYi+2QfQ=; b=mOumEnbmi6mZXCFWihGMckyeLSvdX0VMCFkSe7bDZs05z6Dg8iXvmA6HNupzLgzXXc8DAaUJuWgmqMUhLFEZitdmUCWLGZpVimroDl+VxjHx2nlf67t/AzqED85FpQORDkKg/pzyG+8Oto8mqdFtZ5qPwQW+FOPh04XNLGVu8Ho= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1598345359100714.5619371224647; Tue, 25 Aug 2020 01:49:19 -0700 (PDT) Received: from localhost ([::1]:57714 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kAUdp-00023S-JW for importer@patchew.org; Tue, 25 Aug 2020 04:49:17 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:60780) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kAUPX-0005Bv-7s for qemu-devel@nongnu.org; Tue, 25 Aug 2020 04:34:31 -0400 Received: from us-smtp-delivery-1.mimecast.com ([207.211.31.120]:60879 helo=us-smtp-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1kAUPP-0000hn-6i for qemu-devel@nongnu.org; Tue, 25 Aug 2020 04:34:30 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-177--ywQron9OE2-V0-eHlvsTQ-1; Tue, 25 Aug 2020 04:34:19 -0400 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 1DB731074641; Tue, 25 Aug 2020 08:34:18 +0000 (UTC) Received: from localhost (ovpn-113-72.ams2.redhat.com [10.36.113.72]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 865E61A92A; Tue, 25 Aug 2020 08:34:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1598344461; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=UG6SkOayHtZafH5MCyV+bvGceoz5k9ttBs5EYi+2QfQ=; b=X68WhzXZXtYhqVzZAiOaMPGscDQZdlE4JRdVmnVxrW6ojY/siDpG/2X4ni8q25hUoeNhdG iVCyd/yIZzL0WL3th0+8SdQkcQwqod83rO5hujVKzxO4tfMm1F10/YW/mdaUptEOKt/W2D 1IgXWF3QCqd8/4ssot/wGGWD+RnyNPk= X-MC-Unique: -ywQron9OE2-V0-eHlvsTQ-1 From: Max Reitz To: qemu-block@nongnu.org Subject: [PULL 28/34] qcow2: Add subcluster support to qcow2_co_pwrite_zeroes() Date: Tue, 25 Aug 2020 10:33:05 +0200 Message-Id: <20200825083311.1098442-29-mreitz@redhat.com> In-Reply-To: <20200825083311.1098442-1-mreitz@redhat.com> References: <20200825083311.1098442-1-mreitz@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=mreitz@redhat.com X-Mimecast-Spam-Score: 0.001 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=207.211.31.120; envelope-from=mreitz@redhat.com; helo=us-smtp-1.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/08/25 01:37:44 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -30 X-Spam_score: -3.1 X-Spam_bar: --- X-Spam_report: (-3.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.956, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , Peter Maydell , qemu-devel@nongnu.org, Max Reitz Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" From: Alberto Garcia This works now at the subcluster level and pwrite_zeroes_alignment is updated accordingly. qcow2_cluster_zeroize() is turned into qcow2_subcluster_zeroize() with the following changes: - The request can now be subcluster-aligned. - The cluster-aligned body of the request is still zeroized using zero_in_l2_slice() as before. - The subcluster-aligned head and tail of the request are zeroized with the new zero_l2_subclusters() function. There is just one thing to take into account for a possible future improvement: compressed clusters cannot be partially zeroized so zero_l2_subclusters() on the head or the tail can return -ENOTSUP. This makes the caller repeat the *complete* request and write actual zeroes to disk. This is sub-optimal because 1) if the head area was compressed we would still be able to use the fast path for the body and possibly the tail. 2) if the tail area was compressed we are writing zeroes to the head and the body areas, which are already zeroized. Signed-off-by: Alberto Garcia Reviewed-by: Max Reitz Message-Id: <17e05e2ee7e12f10dcf012da81e83ebe27eb3bef.1594396418.git.berto@= igalia.com> Signed-off-by: Max Reitz --- block/qcow2.h | 4 +-- block/qcow2-cluster.c | 81 +++++++++++++++++++++++++++++++++++++++---- block/qcow2.c | 33 +++++++++--------- 3 files changed, 94 insertions(+), 24 deletions(-) diff --git a/block/qcow2.h b/block/qcow2.h index 4fad40b96b..4ef4ae4ab0 100644 --- a/block/qcow2.h +++ b/block/qcow2.h @@ -898,8 +898,8 @@ void qcow2_alloc_cluster_abort(BlockDriverState *bs, QC= owL2Meta *m); int qcow2_cluster_discard(BlockDriverState *bs, uint64_t offset, uint64_t bytes, enum qcow2_discard_type type, bool full_discard); -int qcow2_cluster_zeroize(BlockDriverState *bs, uint64_t offset, - uint64_t bytes, int flags); +int qcow2_subcluster_zeroize(BlockDriverState *bs, uint64_t offset, + uint64_t bytes, int flags); =20 int qcow2_expand_zero_clusters(BlockDriverState *bs, BlockDriverAmendStatusCB *status_cb, diff --git a/block/qcow2-cluster.c b/block/qcow2-cluster.c index 1e84bd8e2e..9d349d61c6 100644 --- a/block/qcow2-cluster.c +++ b/block/qcow2-cluster.c @@ -2016,12 +2016,59 @@ static int zero_in_l2_slice(BlockDriverState *bs, u= int64_t offset, return nb_clusters; } =20 -int qcow2_cluster_zeroize(BlockDriverState *bs, uint64_t offset, - uint64_t bytes, int flags) +static int zero_l2_subclusters(BlockDriverState *bs, uint64_t offset, + unsigned nb_subclusters) +{ + BDRVQcow2State *s =3D bs->opaque; + uint64_t *l2_slice; + uint64_t old_l2_bitmap, l2_bitmap; + int l2_index, ret, sc =3D offset_to_sc_index(s, offset); + + /* For full clusters use zero_in_l2_slice() instead */ + assert(nb_subclusters > 0 && nb_subclusters < s->subclusters_per_clust= er); + assert(sc + nb_subclusters <=3D s->subclusters_per_cluster); + assert(offset_into_subcluster(s, offset) =3D=3D 0); + + ret =3D get_cluster_table(bs, offset, &l2_slice, &l2_index); + if (ret < 0) { + return ret; + } + + switch (qcow2_get_cluster_type(bs, get_l2_entry(s, l2_slice, l2_index)= )) { + case QCOW2_CLUSTER_COMPRESSED: + ret =3D -ENOTSUP; /* We cannot partially zeroize compressed cluste= rs */ + goto out; + case QCOW2_CLUSTER_NORMAL: + case QCOW2_CLUSTER_UNALLOCATED: + break; + default: + g_assert_not_reached(); + } + + old_l2_bitmap =3D l2_bitmap =3D get_l2_bitmap(s, l2_slice, l2_index); + + l2_bitmap |=3D QCOW_OFLAG_SUB_ZERO_RANGE(sc, sc + nb_subclusters); + l2_bitmap &=3D ~QCOW_OFLAG_SUB_ALLOC_RANGE(sc, sc + nb_subclusters); + + if (old_l2_bitmap !=3D l2_bitmap) { + set_l2_bitmap(s, l2_slice, l2_index, l2_bitmap); + qcow2_cache_entry_mark_dirty(s->l2_table_cache, l2_slice); + } + + ret =3D 0; +out: + qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice); + + return ret; +} + +int qcow2_subcluster_zeroize(BlockDriverState *bs, uint64_t offset, + uint64_t bytes, int flags) { BDRVQcow2State *s =3D bs->opaque; uint64_t end_offset =3D offset + bytes; uint64_t nb_clusters; + unsigned head, tail; int64_t cleared; int ret; =20 @@ -2036,8 +2083,8 @@ int qcow2_cluster_zeroize(BlockDriverState *bs, uint6= 4_t offset, } =20 /* Caller must pass aligned values, except at image end */ - assert(QEMU_IS_ALIGNED(offset, s->cluster_size)); - assert(QEMU_IS_ALIGNED(end_offset, s->cluster_size) || + assert(offset_into_subcluster(s, offset) =3D=3D 0); + assert(offset_into_subcluster(s, end_offset) =3D=3D 0 || end_offset >=3D bs->total_sectors << BDRV_SECTOR_BITS); =20 /* @@ -2052,11 +2099,26 @@ int qcow2_cluster_zeroize(BlockDriverState *bs, uin= t64_t offset, return -ENOTSUP; } =20 - /* Each L2 slice is handled by its own loop iteration */ - nb_clusters =3D size_to_clusters(s, bytes); + head =3D MIN(end_offset, ROUND_UP(offset, s->cluster_size)) - offset; + offset +=3D head; + + tail =3D (end_offset >=3D bs->total_sectors << BDRV_SECTOR_BITS) ? 0 : + end_offset - MAX(offset, start_of_cluster(s, end_offset)); + end_offset -=3D tail; =20 s->cache_discards =3D true; =20 + if (head) { + ret =3D zero_l2_subclusters(bs, offset - head, + size_to_subclusters(s, head)); + if (ret < 0) { + goto fail; + } + } + + /* Each L2 slice is handled by its own loop iteration */ + nb_clusters =3D size_to_clusters(s, end_offset - offset); + while (nb_clusters > 0) { cleared =3D zero_in_l2_slice(bs, offset, nb_clusters, flags); if (cleared < 0) { @@ -2068,6 +2130,13 @@ int qcow2_cluster_zeroize(BlockDriverState *bs, uint= 64_t offset, offset +=3D (cleared * s->cluster_size); } =20 + if (tail) { + ret =3D zero_l2_subclusters(bs, end_offset, size_to_subclusters(s,= tail)); + if (ret < 0) { + goto fail; + } + } + ret =3D 0; fail: s->cache_discards =3D false; diff --git a/block/qcow2.c b/block/qcow2.c index 9990535c46..0cf0b0a9fb 100644 --- a/block/qcow2.c +++ b/block/qcow2.c @@ -1913,7 +1913,7 @@ static void qcow2_refresh_limits(BlockDriverState *bs= , Error **errp) /* Encryption works on a sector granularity */ bs->bl.request_alignment =3D qcrypto_block_get_sector_size(s->cryp= to); } - bs->bl.pwrite_zeroes_alignment =3D s->cluster_size; + bs->bl.pwrite_zeroes_alignment =3D s->subcluster_size; bs->bl.pdiscard_alignment =3D s->cluster_size; } =20 @@ -3833,8 +3833,9 @@ static coroutine_fn int qcow2_co_pwrite_zeroes(BlockD= riverState *bs, int ret; BDRVQcow2State *s =3D bs->opaque; =20 - uint32_t head =3D offset % s->cluster_size; - uint32_t tail =3D (offset + bytes) % s->cluster_size; + uint32_t head =3D offset_into_subcluster(s, offset); + uint32_t tail =3D ROUND_UP(offset + bytes, s->subcluster_size) - + (offset + bytes); =20 trace_qcow2_pwrite_zeroes_start_req(qemu_coroutine_self(), offset, byt= es); if (offset + bytes =3D=3D bs->total_sectors * BDRV_SECTOR_SIZE) { @@ -3846,20 +3847,19 @@ static coroutine_fn int qcow2_co_pwrite_zeroes(Bloc= kDriverState *bs, unsigned int nr; QCow2SubclusterType type; =20 - assert(head + bytes <=3D s->cluster_size); + assert(head + bytes + tail <=3D s->subcluster_size); =20 /* check whether remainder of cluster already reads as zero */ if (!(is_zero(bs, offset - head, head) && - is_zero(bs, offset + bytes, - tail ? s->cluster_size - tail : 0))) { + is_zero(bs, offset + bytes, tail))) { return -ENOTSUP; } =20 qemu_co_mutex_lock(&s->lock); /* We can have new write after previous check */ - offset =3D QEMU_ALIGN_DOWN(offset, s->cluster_size); - bytes =3D s->cluster_size; - nr =3D s->cluster_size; + offset -=3D head; + bytes =3D s->subcluster_size; + nr =3D s->subcluster_size; ret =3D qcow2_get_host_offset(bs, offset, &nr, &off, &type); if (ret < 0 || (type !=3D QCOW2_SUBCLUSTER_UNALLOCATED_PLAIN && @@ -3875,8 +3875,8 @@ static coroutine_fn int qcow2_co_pwrite_zeroes(BlockD= riverState *bs, =20 trace_qcow2_pwrite_zeroes(qemu_coroutine_self(), offset, bytes); =20 - /* Whatever is left can use real zero clusters */ - ret =3D qcow2_cluster_zeroize(bs, offset, bytes, flags); + /* Whatever is left can use real zero subclusters */ + ret =3D qcow2_subcluster_zeroize(bs, offset, bytes, flags); qemu_co_mutex_unlock(&s->lock); =20 return ret; @@ -4357,15 +4357,16 @@ static int coroutine_fn qcow2_co_truncate(BlockDriv= erState *bs, int64_t offset, } =20 if ((flags & BDRV_REQ_ZERO_WRITE) && offset > old_length) { - uint64_t zero_start =3D QEMU_ALIGN_UP(old_length, s->cluster_size); + uint64_t zero_start =3D QEMU_ALIGN_UP(old_length, s->subcluster_si= ze); =20 /* - * Use zero clusters as much as we can. qcow2_cluster_zeroize() - * requires a cluster-aligned start. The end may be unaligned if i= t is - * at the end of the image (which it is here). + * Use zero clusters as much as we can. qcow2_subcluster_zeroize() + * requires a subcluster-aligned start. The end may be unaligned if + * it is at the end of the image (which it is here). */ if (offset > zero_start) { - ret =3D qcow2_cluster_zeroize(bs, zero_start, offset - zero_st= art, 0); + ret =3D qcow2_subcluster_zeroize(bs, zero_start, offset - zero= _start, + 0); if (ret < 0) { error_setg_errno(errp, -ret, "Failed to zero out new clust= ers"); goto fail; --=20 2.26.2