From patchwork Thu Jan 21 16:37:18 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Daniel_P=2E_Berrang=C3=A9?= X-Patchwork-Id: 571237 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 04D1D1402D9 for ; Fri, 22 Jan 2016 03:38:28 +1100 (AEDT) Received: from localhost ([::1]:48630 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aMIFe-0005vz-2Y for incoming@patchwork.ozlabs.org; Thu, 21 Jan 2016 11:38:26 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:41467) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aMIF1-0004h5-Ku for qemu-devel@nongnu.org; Thu, 21 Jan 2016 11:37:48 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aMIF0-0004BW-Mt for qemu-devel@nongnu.org; Thu, 21 Jan 2016 11:37:47 -0500 Received: from mx1.redhat.com ([209.132.183.28]:60732) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aMIEw-00048g-NR; Thu, 21 Jan 2016 11:37:42 -0500 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by mx1.redhat.com (Postfix) with ESMTPS id 58CADC0ABF40; Thu, 21 Jan 2016 16:37:42 +0000 (UTC) Received: from t530wlan.home.berrange.com.com (vpn1-6-170.ams2.redhat.com [10.36.6.170]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u0LGbXn2001020; Thu, 21 Jan 2016 11:37:41 -0500 From: "Daniel P. Berrange" To: qemu-devel@nongnu.org Date: Thu, 21 Jan 2016 16:37:18 +0000 Message-Id: <1453394247-2267-6-git-send-email-berrange@redhat.com> In-Reply-To: <1453394247-2267-1-git-send-email-berrange@redhat.com> References: <1453394247-2267-1-git-send-email-berrange@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.22 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: Paolo Bonzini , qemu-block@nongnu.org Subject: [Qemu-devel] [PATCH v4 05/14] nbd: invert client logic for negotiating protocol version X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org The nbd_receive_negotiate() method takes different code paths based on whether 'name == NULL', and then checks the expected protocol version in each branch. This patch inverts the logic, so that it takes different code paths based on what protocol version it receives and then checks if name is NULL or not as needed. This facilitates later code which allows the client to be capable of using the new style protocol regardless of whether an export name is listed or not. Signed-off-by: Daniel P. Berrange --- nbd/client.c | 60 +++++++++++++++++++++++++++++------------------------------- 1 file changed, 29 insertions(+), 31 deletions(-) diff --git a/nbd/client.c b/nbd/client.c index 07e1fed..65c8a27 100644 --- a/nbd/client.c +++ b/nbd/client.c @@ -115,20 +115,11 @@ int nbd_receive_negotiate(QIOChannel *ioc, const char *name, uint32_t *flags, magic = be64_to_cpu(magic); TRACE("Magic is 0x%" PRIx64, magic); - if (name) { + if (magic == NBD_OPTS_MAGIC) { uint32_t reserved = 0; uint32_t opt; uint32_t namesize; - TRACE("Checking magic (opts_magic)"); - if (magic != NBD_OPTS_MAGIC) { - if (magic == NBD_CLIENT_MAGIC) { - error_setg(errp, "Server does not support export names"); - } else { - error_setg(errp, "Bad magic received"); - } - goto fail; - } if (read_sync(ioc, &tmp, sizeof(tmp)) != sizeof(tmp)) { error_setg(errp, "Failed to read server flags"); goto fail; @@ -141,6 +132,10 @@ int nbd_receive_negotiate(QIOChannel *ioc, const char *name, uint32_t *flags, goto fail; } /* write the export name */ + if (!name) { + error_setg(errp, "Server requires an export name"); + goto fail; + } magic = cpu_to_be64(magic); if (write_sync(ioc, &magic, sizeof(magic)) != sizeof(magic)) { error_setg(errp, "Failed to send export name magic"); @@ -161,39 +156,42 @@ int nbd_receive_negotiate(QIOChannel *ioc, const char *name, uint32_t *flags, error_setg(errp, "Failed to send export name"); goto fail; } - } else { - TRACE("Checking magic (cli_magic)"); - if (magic != NBD_CLIENT_MAGIC) { - if (magic == NBD_OPTS_MAGIC) { - error_setg(errp, "Server requires an export name"); - } else { - error_setg(errp, "Bad magic received"); - } + if (read_sync(ioc, &s, sizeof(s)) != sizeof(s)) { + error_setg(errp, "Failed to read export length"); goto fail; } - } + *size = be64_to_cpu(s); + TRACE("Size is %" PRIu64, *size); - if (read_sync(ioc, &s, sizeof(s)) != sizeof(s)) { - error_setg(errp, "Failed to read export length"); - goto fail; - } - *size = be64_to_cpu(s); - TRACE("Size is %" PRIu64, *size); + if (read_sync(ioc, &tmp, sizeof(tmp)) != sizeof(tmp)) { + error_setg(errp, "Failed to read export flags"); + goto fail; + } + *flags |= be16_to_cpu(tmp); + } else if (magic == NBD_CLIENT_MAGIC) { + if (name) { + error_setg(errp, "Server does not support export names"); + goto fail; + } + + if (read_sync(ioc, &s, sizeof(s)) != sizeof(s)) { + error_setg(errp, "Failed to read export length"); + goto fail; + } + *size = be64_to_cpu(s); + TRACE("Size is %" PRIu64, *size); - if (!name) { if (read_sync(ioc, flags, sizeof(*flags)) != sizeof(*flags)) { error_setg(errp, "Failed to read export flags"); goto fail; } *flags = be32_to_cpup(flags); } else { - if (read_sync(ioc, &tmp, sizeof(tmp)) != sizeof(tmp)) { - error_setg(errp, "Failed to read export flags"); - goto fail; - } - *flags |= be16_to_cpu(tmp); + error_setg(errp, "Bad magic received"); + goto fail; } + if (read_sync(ioc, &buf, 124) != 124) { error_setg(errp, "Failed to read reserved block"); goto fail;