From patchwork Sat Dec 3 18:19:14 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Blake X-Patchwork-Id: 9459739 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 DF9276074E for ; Sat, 3 Dec 2016 18:19:52 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C346E283E7 for ; Sat, 3 Dec 2016 18:19:52 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id B60352840C; Sat, 3 Dec 2016 18:19:52 +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.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI 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 0D710283E7 for ; Sat, 3 Dec 2016 18:19:51 +0000 (UTC) Received: from localhost ([::1]:52058 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cDEub-00080o-Ck for patchwork-qemu-devel@patchwork.kernel.org; Sat, 03 Dec 2016 13:19:49 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:34705) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cDEuK-0007yx-Do for qemu-devel@nongnu.org; Sat, 03 Dec 2016 13:19:33 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cDEuJ-0003fV-C8 for qemu-devel@nongnu.org; Sat, 03 Dec 2016 13:19:32 -0500 Received: from mx1.redhat.com ([209.132.183.28]:36750) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1cDEuE-0003bC-HE; Sat, 03 Dec 2016 13:19:26 -0500 Received: from int-mx14.intmail.prod.int.phx2.redhat.com (int-mx14.intmail.prod.int.phx2.redhat.com [10.5.11.27]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 0E1FF1555C; Sat, 3 Dec 2016 18:19:25 +0000 (UTC) Received: from red.redhat.com ([10.10.66.15]) by int-mx14.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id uB3IJN7c001764; Sat, 3 Dec 2016 13:19:24 -0500 From: Eric Blake To: qemu-devel@nongnu.org Date: Sat, 3 Dec 2016 12:19:14 -0600 Message-Id: <20161203181914.7467-1-eblake@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.27 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.29]); Sat, 03 Dec 2016 18:19:25 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH] qcow2: Discard/zero clusters by byte count 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 , "open list:qcow2" , Max Reitz Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP Passing a byte offset, but sector count, when we ultimately want to operate on cluster granularity, is madness. Clean up the interfaces to take byte offset and count. Rename qcow2_discard_clusters() and qcow2_zero_clusters() to the shorter qcow2_discard() and qcow2_zero() to make sure backports don't get confused by changed units. Signed-off-by: Eric Blake --- 2.9 material, depends on 'Don't strand clusters near 2G intervals during commit' block/qcow2.h | 8 ++++---- block/qcow2-cluster.c | 20 +++++++++++--------- block/qcow2-snapshot.c | 7 +++---- block/qcow2.c | 22 ++++++++++------------ 4 files changed, 28 insertions(+), 29 deletions(-) diff --git a/block/qcow2.h b/block/qcow2.h index 1823414..a0d169b 100644 --- a/block/qcow2.h +++ b/block/qcow2.h @@ -543,10 +543,10 @@ uint64_t qcow2_alloc_compressed_cluster_offset(BlockDriverState *bs, int compressed_size); int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, QCowL2Meta *m); -int qcow2_discard_clusters(BlockDriverState *bs, uint64_t offset, - int nb_sectors, enum qcow2_discard_type type, bool full_discard); -int qcow2_zero_clusters(BlockDriverState *bs, uint64_t offset, int nb_sectors, - int flags); +int qcow2_discard(BlockDriverState *bs, uint64_t offset, uint64_t count, + enum qcow2_discard_type type, bool full_discard); +int qcow2_zero(BlockDriverState *bs, uint64_t offset, uint64_t count, + int flags); int qcow2_expand_zero_clusters(BlockDriverState *bs, BlockDriverAmendStatusCB *status_cb, diff --git a/block/qcow2-cluster.c b/block/qcow2-cluster.c index 928c1e2..3ee0815 100644 --- a/block/qcow2-cluster.c +++ b/block/qcow2-cluster.c @@ -1511,19 +1511,17 @@ static int discard_single_l2(BlockDriverState *bs, uint64_t offset, return nb_clusters; } -int qcow2_discard_clusters(BlockDriverState *bs, uint64_t offset, - int nb_sectors, enum qcow2_discard_type type, bool full_discard) +int qcow2_discard(BlockDriverState *bs, uint64_t offset, uint64_t count, + enum qcow2_discard_type type, bool full_discard) { BDRVQcow2State *s = bs->opaque; uint64_t end_offset; uint64_t nb_clusters; int ret; - end_offset = offset + (nb_sectors << BDRV_SECTOR_BITS); - - /* Round start up and end down */ + /* Round start up and end down to cluster boundary */ + end_offset = start_of_cluster(s, offset + count); offset = align_offset(offset, s->cluster_size); - end_offset = start_of_cluster(s, end_offset); if (offset > end_offset) { return 0; @@ -1595,20 +1593,24 @@ static int zero_single_l2(BlockDriverState *bs, uint64_t offset, return nb_clusters; } -int qcow2_zero_clusters(BlockDriverState *bs, uint64_t offset, int nb_sectors, - int flags) +int qcow2_zero(BlockDriverState *bs, uint64_t offset, uint64_t count, + int flags) { BDRVQcow2State *s = bs->opaque; uint64_t nb_clusters; int ret; + /* Block layer guarantees cluster alignment */ + assert(QEMU_IS_ALIGNED(offset, s->cluster_size)); + assert(QEMU_IS_ALIGNED(count, s->cluster_size)); + /* The zero flag is only supported by version 3 and newer */ if (s->qcow_version < 3) { return -ENOTSUP; } /* Each L2 table is handled by its own loop iteration */ - nb_clusters = size_to_clusters(s, nb_sectors << BDRV_SECTOR_BITS); + nb_clusters = size_to_clusters(s, count); s->cache_discards = true; diff --git a/block/qcow2-snapshot.c b/block/qcow2-snapshot.c index 0324243..fd088e5 100644 --- a/block/qcow2-snapshot.c +++ b/block/qcow2-snapshot.c @@ -440,10 +440,9 @@ int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info) /* The VM state isn't needed any more in the active L1 table; in fact, it * hurts by causing expensive COW for the next snapshot. */ - qcow2_discard_clusters(bs, qcow2_vm_state_offset(s), - align_offset(sn->vm_state_size, s->cluster_size) - >> BDRV_SECTOR_BITS, - QCOW2_DISCARD_NEVER, false); + qcow2_discard(bs, qcow2_vm_state_offset(s), + align_offset(sn->vm_state_size, s->cluster_size), + QCOW2_DISCARD_NEVER, false); #ifdef DEBUG_ALLOC { diff --git a/block/qcow2.c b/block/qcow2.c index 369b542..601831f 100644 --- a/block/qcow2.c +++ b/block/qcow2.c @@ -2487,7 +2487,7 @@ static coroutine_fn int qcow2_co_pwrite_zeroes(BlockDriverState *bs, trace_qcow2_pwrite_zeroes(qemu_coroutine_self(), offset, count); /* Whatever is left can use real zero clusters */ - ret = qcow2_zero_clusters(bs, offset, count >> BDRV_SECTOR_BITS, flags); + ret = qcow2_zero(bs, offset, count, flags); qemu_co_mutex_unlock(&s->lock); return ret; @@ -2505,8 +2505,7 @@ static coroutine_fn int qcow2_co_pdiscard(BlockDriverState *bs, } qemu_co_mutex_lock(&s->lock); - ret = qcow2_discard_clusters(bs, offset, count >> BDRV_SECTOR_BITS, - QCOW2_DISCARD_REQUEST, false); + ret = qcow2_discard(bs, offset, count, QCOW2_DISCARD_REQUEST, false); qemu_co_mutex_unlock(&s->lock); return ret; } @@ -2807,9 +2806,8 @@ fail: static int qcow2_make_empty(BlockDriverState *bs) { BDRVQcow2State *s = bs->opaque; - uint64_t start_sector; - int sector_step = QEMU_ALIGN_DOWN(INT_MAX / BDRV_SECTOR_SIZE, - s->cluster_size); + uint64_t offset; + int step = QEMU_ALIGN_DOWN(INT_MAX, s->cluster_size); int l1_clusters, ret = 0; l1_clusters = DIV_ROUND_UP(s->l1_size, s->cluster_size / sizeof(uint64_t)); @@ -2826,18 +2824,18 @@ static int qcow2_make_empty(BlockDriverState *bs) /* This fallback code simply discards every active cluster; this is slow, * but works in all cases */ - for (start_sector = 0; start_sector < bs->total_sectors; - start_sector += sector_step) + for (offset = 0; offset < bs->total_sectors * BDRV_SECTOR_SIZE; + offset += step) { /* As this function is generally used after committing an external * snapshot, QCOW2_DISCARD_SNAPSHOT seems appropriate. Also, the * default action for this kind of discard is to pass the discard, * which will ideally result in an actually smaller image file, as * is probably desired. */ - ret = qcow2_discard_clusters(bs, start_sector * BDRV_SECTOR_SIZE, - MIN(sector_step, - bs->total_sectors - start_sector), - QCOW2_DISCARD_SNAPSHOT, true); + ret = qcow2_discard(bs, offset, + MIN(step, + bs->total_sectors * BDRV_SECTOR_SIZE - offset), + QCOW2_DISCARD_SNAPSHOT, true); if (ret < 0) { break; }