From patchwork Thu Sep 27 02:54:27 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Blake X-Patchwork-Id: 975527 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=nongnu.org (client-ip=2001:4830:134:3::11; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=redhat.com Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 42LKHJ1sZsz9s3x for ; Thu, 27 Sep 2018 12:57:48 +1000 (AEST) Received: from localhost ([::1]:33507 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1g5MUr-0008PH-UF for incoming@patchwork.ozlabs.org; Wed, 26 Sep 2018 22:57:45 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:58268) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1g5MS6-0006fP-O5 for qemu-devel@nongnu.org; Wed, 26 Sep 2018 22:54:57 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1g5MS3-0008AX-DE for qemu-devel@nongnu.org; Wed, 26 Sep 2018 22:54:54 -0400 Received: from mx1.redhat.com ([209.132.183.28]:35972) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1g5MRy-00088q-Oq; Wed, 26 Sep 2018 22:54:46 -0400 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 16FC53082128; Thu, 27 Sep 2018 02:54:46 +0000 (UTC) Received: from red.redhat.com (ovpn-126-36.rdu2.redhat.com [10.10.126.36]) by smtp.corp.redhat.com (Postfix) with ESMTP id BD7024BE; Thu, 27 Sep 2018 02:54:43 +0000 (UTC) From: Eric Blake To: qemu-devel@nongnu.org Date: Wed, 26 Sep 2018 21:54:27 -0500 Message-Id: <20180927025427.101853-4-eblake@redhat.com> In-Reply-To: <20180927025427.101853-1-eblake@redhat.com> References: <20180927025427.101853-1-eblake@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.42]); Thu, 27 Sep 2018 02:54:46 +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] [PULL 3/3] nbd/server: send more than one extent of base:allocation context 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: Paolo Bonzini , Vladimir Sementsov-Ogievskiy , "open list:Network Block Dev..." Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" From: Vladimir Sementsov-Ogievskiy This is necessary for efficient block-status export, for clients which support it. (qemu is not yet such a client, but could become one.) Signed-off-by: Vladimir Sementsov-Ogievskiy Message-Id: <20180704112302.471456-3-vsementsov@virtuozzo.com> [eblake: grammar tweaks] Signed-off-by: Eric Blake --- nbd/server.c | 87 ++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 64 insertions(+), 23 deletions(-) diff --git a/nbd/server.c b/nbd/server.c index 12f721482dd..c3dd402b45e 100644 --- a/nbd/server.c +++ b/nbd/server.c @@ -1844,37 +1844,68 @@ static int coroutine_fn nbd_co_send_sparse_read(NBDClient *client, return ret; } -static int blockstatus_to_extent_be(BlockDriverState *bs, uint64_t offset, - uint64_t bytes, NBDExtent *extent) +/* + * Populate @extents from block status. Update @bytes to be the actual + * length encoded (which may be smaller than the original), and update + * @nb_extents to the number of extents used. + * + * Returns zero on success and -errno on bdrv_block_status_above failure. + */ +static int blockstatus_to_extents(BlockDriverState *bs, uint64_t offset, + uint64_t *bytes, NBDExtent *extents, + unsigned int *nb_extents) { - uint64_t remaining_bytes = bytes; + uint64_t remaining_bytes = *bytes; + NBDExtent *extent = extents, *extents_end = extents + *nb_extents; + bool first_extent = true; + assert(*nb_extents); while (remaining_bytes) { uint32_t flags; int64_t num; int ret = bdrv_block_status_above(bs, NULL, offset, remaining_bytes, &num, NULL, NULL); + if (ret < 0) { return ret; } flags = (ret & BDRV_BLOCK_ALLOCATED ? 0 : NBD_STATE_HOLE) | (ret & BDRV_BLOCK_ZERO ? NBD_STATE_ZERO : 0); - - if (remaining_bytes == bytes) { - extent->flags = flags; - } - - if (flags != extent->flags) { - break; - } - offset += num; remaining_bytes -= num; + + if (first_extent) { + extent->flags = flags; + extent->length = num; + first_extent = false; + continue; + } + + if (flags == extent->flags) { + /* extend current extent */ + extent->length += num; + } else { + if (extent + 1 == extents_end) { + break; + } + + /* start new extent */ + extent++; + extent->flags = flags; + extent->length = num; + } + } + + extents_end = extent + 1; + + for (extent = extents; extent < extents_end; extent++) { + cpu_to_be32s(&extent->flags); + cpu_to_be32s(&extent->length); } - cpu_to_be32s(&extent->flags); - extent->length = cpu_to_be32(bytes - remaining_bytes); + *bytes -= remaining_bytes; + *nb_extents = extents_end - extents; return 0; } @@ -1910,21 +1941,29 @@ static int nbd_co_send_extents(NBDClient *client, uint64_t handle, /* Get block status from the exported device and send it to the client */ static int nbd_co_send_block_status(NBDClient *client, uint64_t handle, BlockDriverState *bs, uint64_t offset, - uint32_t length, bool last, - uint32_t context_id, Error **errp) + uint32_t length, bool dont_fragment, + bool last, uint32_t context_id, + Error **errp) { int ret; - NBDExtent extent; + unsigned int nb_extents = dont_fragment ? 1 : NBD_MAX_BITMAP_EXTENTS; + NBDExtent *extents = g_new(NBDExtent, nb_extents); + uint64_t final_length = length; - ret = blockstatus_to_extent_be(bs, offset, length, &extent); + ret = blockstatus_to_extents(bs, offset, &final_length, extents, + &nb_extents); if (ret < 0) { + g_free(extents); return nbd_co_send_structured_error( client, handle, -ret, "can't get block status", errp); } - return nbd_co_send_extents(client, handle, &extent, 1, - be32_to_cpu(extent.length), last, - context_id, errp); + ret = nbd_co_send_extents(client, handle, extents, nb_extents, + final_length, last, context_id, errp); + + g_free(extents); + + return ret; } /* @@ -2231,10 +2270,12 @@ static coroutine_fn int nbd_handle_request(NBDClient *client, (client->export_meta.base_allocation || client->export_meta.bitmap)) { + bool dont_fragment = request->flags & NBD_CMD_FLAG_REQ_ONE; + if (client->export_meta.base_allocation) { ret = nbd_co_send_block_status(client, request->handle, blk_bs(exp->blk), request->from, - request->len, + request->len, dont_fragment, !client->export_meta.bitmap, NBD_META_ID_BASE_ALLOCATION, errp); @@ -2247,7 +2288,7 @@ static coroutine_fn int nbd_handle_request(NBDClient *client, ret = nbd_co_send_bitmap(client, request->handle, client->exp->export_bitmap, request->from, request->len, - request->flags & NBD_CMD_FLAG_REQ_ONE, + dont_fragment, true, NBD_META_ID_DIRTY_BITMAP, errp); if (ret < 0) { return ret;