From patchwork Tue May 24 22:25:25 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Blake X-Patchwork-Id: 9134371 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 1FCEC607D8 for ; Tue, 24 May 2016 22:38:34 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 16278281F9 for ; Tue, 24 May 2016 22:38:34 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 0B1992828B; Tue, 24 May 2016 22:38:34 +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 8C4682822B for ; Tue, 24 May 2016 22:38:33 +0000 (UTC) Received: from localhost ([::1]:55775 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b5Ky6-0002pp-Rw for patchwork-qemu-devel@patchwork.kernel.org; Tue, 24 May 2016 18:38:30 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:45536) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b5Klw-0000Hc-5V for qemu-devel@nongnu.org; Tue, 24 May 2016 18:26:00 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1b5Klu-0005c6-1i for qemu-devel@nongnu.org; Tue, 24 May 2016 18:25:55 -0400 Received: from mx1.redhat.com ([209.132.183.28]:40866) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b5Klg-0005UJ-2X; Tue, 24 May 2016 18:25:40 -0400 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (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 87A167F6B1; Tue, 24 May 2016 22:25:39 +0000 (UTC) Received: from red.redhat.com (ovpn-116-141.phx2.redhat.com [10.3.116.141]) by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u4OMPYf5022759; Tue, 24 May 2016 18:25:39 -0400 From: Eric Blake To: qemu-devel@nongnu.org Date: Tue, 24 May 2016 16:25:25 -0600 Message-Id: <1464128732-12667-7-git-send-email-eblake@redhat.com> In-Reply-To: <1464128732-12667-1-git-send-email-eblake@redhat.com> References: <1464128732-12667-1-git-send-email-eblake@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.23 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.25]); Tue, 24 May 2016 22:25:39 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH 06/13] qcow2: Convert to bdrv_co_pwrite_zeroes() 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, qemu-block@nongnu.org, Max Reitz Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP Another step on our continuing quest to switch to byte-based interfaces. There are still opportunities to optimize the qcow2 handling of zero clusters. For example, if the backing file only has non-zero data in the portion about to be overwritten, then we could widen the request and make the entire cluster zero, rather than falling back to -ENOTSUP. But for this patch, intentionally leave the semantics unchanged, even if not optimal. Signed-off-by: Eric Blake Reviewed-by: Kevin Wolf --- block/qcow2.c | 46 +++++++++++++++++++++++++--------------------- 1 file changed, 25 insertions(+), 21 deletions(-) diff --git a/block/qcow2.c b/block/qcow2.c index 978694e..3522fc0 100644 --- a/block/qcow2.c +++ b/block/qcow2.c @@ -2428,43 +2428,47 @@ static bool is_zero_cluster_top_locked(BlockDriverState *bs, int64_t start) return ret == QCOW2_CLUSTER_UNALLOCATED || ret == QCOW2_CLUSTER_ZERO; } -static coroutine_fn int qcow2_co_write_zeroes(BlockDriverState *bs, - int64_t sector_num, int nb_sectors, BdrvRequestFlags flags) +static coroutine_fn int qcow2_co_pwrite_zeroes(BlockDriverState *bs, + int64_t offset, int count, BdrvRequestFlags flags) { int ret; BDRVQcow2State *s = bs->opaque; - int head = sector_num % s->cluster_sectors; - int tail = (sector_num + nb_sectors) % s->cluster_sectors; + int head = offset % s->cluster_size; + int tail = (offset + count) % s->cluster_size; + /* Widen the write to a full cluster, if the cluster already reads + * as zero. */ if (head != 0 || tail != 0) { - int64_t cl_end = -1; + int64_t tail_sector = 0; - sector_num -= head; - nb_sectors += head; - - if (tail != 0) { - nb_sectors += s->cluster_sectors - tail; + offset -= head; + count += head; + if (tail) { + count += s->cluster_size - tail; } - if (!is_zero_cluster(bs, sector_num)) { + if (!is_zero_cluster(bs, offset >> BDRV_SECTOR_BITS)) { return -ENOTSUP; } - if (nb_sectors > s->cluster_sectors) { - /* Technically the request can cover 2 clusters, f.e. 4k write - at s->cluster_sectors - 2k offset. One of these cluster can - be zeroed, one unallocated */ - cl_end = sector_num + nb_sectors - s->cluster_sectors; - if (!is_zero_cluster(bs, cl_end)) { + if (count > s->cluster_size) { + /* Technically the request can cover 2 clusters, f.e. 4k + * write at s->cluster_sectors - 2k offset. One of these + * cluster can be zeroed, one unallocated. Anything larger + * and the front end already split it to alignment + * boundaries. */ + assert(count == 2 * s->cluster_size); + tail_sector = (offset >> BDRV_SECTOR_BITS) + s->cluster_sectors; + if (!is_zero_cluster(bs, tail_sector)) { return -ENOTSUP; } } qemu_co_mutex_lock(&s->lock); /* We can have new write after previous check */ - if (!is_zero_cluster_top_locked(bs, sector_num) || - (cl_end > 0 && !is_zero_cluster_top_locked(bs, cl_end))) { + if (!is_zero_cluster_top_locked(bs, offset >> BDRV_SECTOR_BITS) || + (tail_sector && !is_zero_cluster_top_locked(bs, tail_sector))) { qemu_co_mutex_unlock(&s->lock); return -ENOTSUP; } @@ -2473,7 +2477,7 @@ static coroutine_fn int qcow2_co_write_zeroes(BlockDriverState *bs, } /* Whatever is left can use real zero clusters */ - ret = qcow2_zero_clusters(bs, sector_num << BDRV_SECTOR_BITS, nb_sectors); + ret = qcow2_zero_clusters(bs, offset, count >> BDRV_SECTOR_BITS); qemu_co_mutex_unlock(&s->lock); return ret; @@ -3380,7 +3384,7 @@ BlockDriver bdrv_qcow2 = { .bdrv_co_writev = qcow2_co_writev, .bdrv_co_flush_to_os = qcow2_co_flush_to_os, - .bdrv_co_write_zeroes = qcow2_co_write_zeroes, + .bdrv_co_pwrite_zeroes = qcow2_co_pwrite_zeroes, .bdrv_co_discard = qcow2_co_discard, .bdrv_truncate = qcow2_truncate, .bdrv_write_compressed = qcow2_write_compressed,