nbd: invert client logic for negotiating protocol version

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 <berrange@redhat.com>
Message-Id: <1455129674-17255-8-git-send-email-berrange@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
This commit is contained in:
Daniel P. Berrange 2016-02-10 18:41:05 +00:00 committed by Paolo Bonzini
parent 1c778ef729
commit f72d705f0d

View File

@ -116,20 +116,11 @@ int nbd_receive_negotiate(QIOChannel *ioc, const char *name, uint32_t *flags,
magic = be64_to_cpu(magic); magic = be64_to_cpu(magic);
TRACE("Magic is 0x%" PRIx64, magic); TRACE("Magic is 0x%" PRIx64, magic);
if (name) { if (magic == NBD_OPTS_MAGIC) {
uint32_t reserved = 0; uint32_t reserved = 0;
uint32_t opt; uint32_t opt;
uint32_t namesize; 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)) { if (read_sync(ioc, &tmp, sizeof(tmp)) != sizeof(tmp)) {
error_setg(errp, "Failed to read server flags"); error_setg(errp, "Failed to read server flags");
goto fail; goto fail;
@ -142,6 +133,10 @@ int nbd_receive_negotiate(QIOChannel *ioc, const char *name, uint32_t *flags,
goto fail; goto fail;
} }
/* write the export name */ /* write the export name */
if (!name) {
error_setg(errp, "Server requires an export name");
goto fail;
}
magic = cpu_to_be64(magic); magic = cpu_to_be64(magic);
if (write_sync(ioc, &magic, sizeof(magic)) != sizeof(magic)) { if (write_sync(ioc, &magic, sizeof(magic)) != sizeof(magic)) {
error_setg(errp, "Failed to send export name magic"); error_setg(errp, "Failed to send export name magic");
@ -162,39 +157,42 @@ int nbd_receive_negotiate(QIOChannel *ioc, const char *name, uint32_t *flags,
error_setg(errp, "Failed to send export name"); error_setg(errp, "Failed to send export name");
goto fail; goto fail;
} }
} else {
TRACE("Checking magic (cli_magic)");
if (magic != NBD_CLIENT_MAGIC) { if (read_sync(ioc, &s, sizeof(s)) != sizeof(s)) {
if (magic == NBD_OPTS_MAGIC) { error_setg(errp, "Failed to read export length");
error_setg(errp, "Server requires an export name");
} else {
error_setg(errp, "Bad magic received");
}
goto fail; goto fail;
} }
} *size = be64_to_cpu(s);
TRACE("Size is %" PRIu64, *size);
if (read_sync(ioc, &s, sizeof(s)) != sizeof(s)) { if (read_sync(ioc, &tmp, sizeof(tmp)) != sizeof(tmp)) {
error_setg(errp, "Failed to read export length"); error_setg(errp, "Failed to read export flags");
goto fail; goto fail;
} }
*size = be64_to_cpu(s); *flags |= be16_to_cpu(tmp);
TRACE("Size is %" PRIu64, *size); } 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)) { if (read_sync(ioc, flags, sizeof(*flags)) != sizeof(*flags)) {
error_setg(errp, "Failed to read export flags"); error_setg(errp, "Failed to read export flags");
goto fail; goto fail;
} }
*flags = be32_to_cpup(flags); *flags = be32_to_cpup(flags);
} else { } else {
if (read_sync(ioc, &tmp, sizeof(tmp)) != sizeof(tmp)) { error_setg(errp, "Bad magic received");
error_setg(errp, "Failed to read export flags"); goto fail;
goto fail;
}
*flags |= be16_to_cpu(tmp);
} }
if (read_sync(ioc, &buf, 124) != 124) { if (read_sync(ioc, &buf, 124) != 124) {
error_setg(errp, "Failed to read reserved block"); error_setg(errp, "Failed to read reserved block");
goto fail; goto fail;