nbd: Simplify meta-context parsing
We had a premature optimization of trying to read as little from the wire as possible while handling NBD_OPT_SET_META_CONTEXT in phases. But in reality, we HAVE to read the entire string from the client before we can get to the next command, and it is easier to just read it all at once than it is to read it in pieces. And once we do that, several functions end up no longer performing I/O, so they can drop length and errp parameters, and just return a bool instead of modifying through a pointer. Our iotests still pass; I also checked that libnbd's testsuite (which covers more corner cases of odd meta context requests) still passes. There are cases where the sequence of trace messages produced differs (for example, when no bitmap is exported, a query for "qemu:" now produces two trace lines instead of one), but trace points are for debug and have no effect on what the client sees. Signed-off-by: Eric Blake <eblake@redhat.com> Message-Id: <20200930121105.667049-4-eblake@redhat.com> Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com> [eblake: enhance commit message] Signed-off-by: Eric Blake <eblake@redhat.com>
This commit is contained in:
parent
d1e2c3e7bd
commit
ebd57062a1
189
nbd/server.c
189
nbd/server.c
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (C) 2016-2018 Red Hat, Inc.
|
||||
* Copyright (C) 2016-2020 Red Hat, Inc.
|
||||
* Copyright (C) 2005 Anthony Liguori <anthony@codemonkey.ws>
|
||||
*
|
||||
* Network Block Device Server Side
|
||||
@ -797,135 +797,95 @@ static int nbd_negotiate_send_meta_context(NBDClient *client,
|
||||
return qio_channel_writev_all(client->ioc, iov, 2, errp) < 0 ? -EIO : 0;
|
||||
}
|
||||
|
||||
/* Read strlen(@pattern) bytes, and set @match to true if they match @pattern.
|
||||
* @match is never set to false.
|
||||
*
|
||||
* Return -errno on I/O error, 0 if option was completely handled by
|
||||
* sending a reply about inconsistent lengths, or 1 on success.
|
||||
*
|
||||
* Note: return code = 1 doesn't mean that we've read exactly @pattern.
|
||||
* It only means that there are no errors.
|
||||
/*
|
||||
* Return true if @query matches @pattern, or if @query is empty when
|
||||
* the @client is performing _LIST_.
|
||||
*/
|
||||
static int nbd_meta_pattern(NBDClient *client, const char *pattern, bool *match,
|
||||
Error **errp)
|
||||
static bool nbd_meta_empty_or_pattern(NBDClient *client, const char *pattern,
|
||||
const char *query)
|
||||
{
|
||||
int ret;
|
||||
char *query;
|
||||
size_t len = strlen(pattern);
|
||||
|
||||
assert(len);
|
||||
|
||||
query = g_malloc(len);
|
||||
ret = nbd_opt_read(client, query, len, true, errp);
|
||||
if (ret <= 0) {
|
||||
g_free(query);
|
||||
return ret;
|
||||
if (!*query) {
|
||||
trace_nbd_negotiate_meta_query_parse("empty");
|
||||
return client->opt == NBD_OPT_LIST_META_CONTEXT;
|
||||
}
|
||||
|
||||
if (strncmp(query, pattern, len) == 0) {
|
||||
if (strcmp(query, pattern) == 0) {
|
||||
trace_nbd_negotiate_meta_query_parse(pattern);
|
||||
*match = true;
|
||||
} else {
|
||||
trace_nbd_negotiate_meta_query_skip("pattern not matched");
|
||||
return true;
|
||||
}
|
||||
g_free(query);
|
||||
|
||||
return 1;
|
||||
trace_nbd_negotiate_meta_query_skip("pattern not matched");
|
||||
return false;
|
||||
}
|
||||
|
||||
/*
|
||||
* Read @len bytes, and set @match to true if they match @pattern, or if @len
|
||||
* is 0 and the client is performing _LIST_. @match is never set to false.
|
||||
*
|
||||
* Return -errno on I/O error, 0 if option was completely handled by
|
||||
* sending a reply about inconsistent lengths, or 1 on success.
|
||||
*
|
||||
* Note: return code = 1 doesn't mean that we've read exactly @pattern.
|
||||
* It only means that there are no errors.
|
||||
* Return true and adjust @str in place if it begins with @prefix.
|
||||
*/
|
||||
static int nbd_meta_empty_or_pattern(NBDClient *client, const char *pattern,
|
||||
uint32_t len, bool *match, Error **errp)
|
||||
static bool nbd_strshift(const char **str, const char *prefix)
|
||||
{
|
||||
if (len == 0) {
|
||||
if (client->opt == NBD_OPT_LIST_META_CONTEXT) {
|
||||
*match = true;
|
||||
}
|
||||
trace_nbd_negotiate_meta_query_parse("empty");
|
||||
return 1;
|
||||
}
|
||||
size_t len = strlen(prefix);
|
||||
|
||||
if (len != strlen(pattern)) {
|
||||
trace_nbd_negotiate_meta_query_skip("different lengths");
|
||||
return nbd_opt_skip(client, len, errp);
|
||||
if (strncmp(*str, prefix, len) == 0) {
|
||||
*str += len;
|
||||
return true;
|
||||
}
|
||||
|
||||
return nbd_meta_pattern(client, pattern, match, errp);
|
||||
return false;
|
||||
}
|
||||
|
||||
/* nbd_meta_base_query
|
||||
*
|
||||
* Handle queries to 'base' namespace. For now, only the base:allocation
|
||||
* context is available. 'len' is the amount of text remaining to be read from
|
||||
* the current name, after the 'base:' portion has been stripped.
|
||||
*
|
||||
* Return -errno on I/O error, 0 if option was completely handled by
|
||||
* sending a reply about inconsistent lengths, or 1 on success.
|
||||
* context is available. Return true if @query has been handled.
|
||||
*/
|
||||
static int nbd_meta_base_query(NBDClient *client, NBDExportMetaContexts *meta,
|
||||
uint32_t len, Error **errp)
|
||||
static bool nbd_meta_base_query(NBDClient *client, NBDExportMetaContexts *meta,
|
||||
const char *query)
|
||||
{
|
||||
return nbd_meta_empty_or_pattern(client, "allocation", len,
|
||||
&meta->base_allocation, errp);
|
||||
if (!nbd_strshift(&query, "base:")) {
|
||||
return false;
|
||||
}
|
||||
trace_nbd_negotiate_meta_query_parse("base:");
|
||||
|
||||
if (nbd_meta_empty_or_pattern(client, "allocation", query)) {
|
||||
meta->base_allocation = true;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/* nbd_meta_bitmap_query
|
||||
/* nbd_meta_qemu_query
|
||||
*
|
||||
* Handle query to 'qemu:' namespace.
|
||||
* @len is the amount of text remaining to be read from the current name, after
|
||||
* the 'qemu:' portion has been stripped.
|
||||
*
|
||||
* Return -errno on I/O error, 0 if option was completely handled by
|
||||
* sending a reply about inconsistent lengths, or 1 on success. */
|
||||
static int nbd_meta_qemu_query(NBDClient *client, NBDExportMetaContexts *meta,
|
||||
uint32_t len, Error **errp)
|
||||
* Handle queries to 'qemu' namespace. For now, only the qemu:dirty-bitmap:
|
||||
* context is available. Return true if @query has been handled.
|
||||
*/
|
||||
static bool nbd_meta_qemu_query(NBDClient *client, NBDExportMetaContexts *meta,
|
||||
const char *query)
|
||||
{
|
||||
bool dirty_bitmap = false;
|
||||
size_t dirty_bitmap_len = strlen("dirty-bitmap:");
|
||||
int ret;
|
||||
|
||||
if (!meta->exp->export_bitmap) {
|
||||
trace_nbd_negotiate_meta_query_skip("no dirty-bitmap exported");
|
||||
return nbd_opt_skip(client, len, errp);
|
||||
if (!nbd_strshift(&query, "qemu:")) {
|
||||
return false;
|
||||
}
|
||||
trace_nbd_negotiate_meta_query_parse("qemu:");
|
||||
|
||||
if (len == 0) {
|
||||
if (!*query) {
|
||||
if (client->opt == NBD_OPT_LIST_META_CONTEXT) {
|
||||
meta->bitmap = true;
|
||||
meta->bitmap = !!meta->exp->export_bitmap;
|
||||
}
|
||||
trace_nbd_negotiate_meta_query_parse("empty");
|
||||
return 1;
|
||||
return true;
|
||||
}
|
||||
|
||||
if (len < dirty_bitmap_len) {
|
||||
trace_nbd_negotiate_meta_query_skip("not dirty-bitmap:");
|
||||
return nbd_opt_skip(client, len, errp);
|
||||
if (nbd_strshift(&query, "dirty-bitmap:")) {
|
||||
trace_nbd_negotiate_meta_query_parse("dirty-bitmap:");
|
||||
if (!meta->exp->export_bitmap) {
|
||||
trace_nbd_negotiate_meta_query_skip("no dirty-bitmap exported");
|
||||
return true;
|
||||
}
|
||||
if (nbd_meta_empty_or_pattern(client,
|
||||
meta->exp->export_bitmap_context +
|
||||
strlen("qemu:dirty-bitmap:"), query)) {
|
||||
meta->bitmap = true;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
len -= dirty_bitmap_len;
|
||||
ret = nbd_meta_pattern(client, "dirty-bitmap:", &dirty_bitmap, errp);
|
||||
if (ret <= 0) {
|
||||
return ret;
|
||||
}
|
||||
if (!dirty_bitmap) {
|
||||
trace_nbd_negotiate_meta_query_skip("not dirty-bitmap:");
|
||||
return nbd_opt_skip(client, len, errp);
|
||||
}
|
||||
|
||||
trace_nbd_negotiate_meta_query_parse("dirty-bitmap:");
|
||||
|
||||
return nbd_meta_empty_or_pattern(
|
||||
client, meta->exp->export_bitmap_context +
|
||||
strlen("qemu:dirty_bitmap:"), len, &meta->bitmap, errp);
|
||||
trace_nbd_negotiate_meta_query_skip("not dirty-bitmap");
|
||||
return true;
|
||||
}
|
||||
|
||||
/* nbd_negotiate_meta_query
|
||||
@ -935,22 +895,13 @@ static int nbd_meta_qemu_query(NBDClient *client, NBDExportMetaContexts *meta,
|
||||
*
|
||||
* The only supported namespaces are 'base' and 'qemu'.
|
||||
*
|
||||
* The function aims not wasting time and memory to read long unknown namespace
|
||||
* names.
|
||||
*
|
||||
* Return -errno on I/O error, 0 if option was completely handled by
|
||||
* sending a reply about inconsistent lengths, or 1 on success. */
|
||||
static int nbd_negotiate_meta_query(NBDClient *client,
|
||||
NBDExportMetaContexts *meta, Error **errp)
|
||||
{
|
||||
/*
|
||||
* Both 'qemu' and 'base' namespaces have length = 5 including a
|
||||
* colon. If another length namespace is later introduced, this
|
||||
* should certainly be refactored.
|
||||
*/
|
||||
int ret;
|
||||
size_t ns_len = 5;
|
||||
char ns[5];
|
||||
g_autofree char *query = NULL;
|
||||
uint32_t len;
|
||||
|
||||
ret = nbd_opt_read(client, &len, sizeof(len), false, errp);
|
||||
@ -963,27 +914,23 @@ static int nbd_negotiate_meta_query(NBDClient *client,
|
||||
trace_nbd_negotiate_meta_query_skip("length too long");
|
||||
return nbd_opt_skip(client, len, errp);
|
||||
}
|
||||
if (len < ns_len) {
|
||||
trace_nbd_negotiate_meta_query_skip("length too short");
|
||||
return nbd_opt_skip(client, len, errp);
|
||||
}
|
||||
|
||||
len -= ns_len;
|
||||
ret = nbd_opt_read(client, ns, ns_len, true, errp);
|
||||
query = g_malloc(len + 1);
|
||||
ret = nbd_opt_read(client, query, len, true, errp);
|
||||
if (ret <= 0) {
|
||||
return ret;
|
||||
}
|
||||
query[len] = '\0';
|
||||
|
||||
if (!strncmp(ns, "base:", ns_len)) {
|
||||
trace_nbd_negotiate_meta_query_parse("base:");
|
||||
return nbd_meta_base_query(client, meta, len, errp);
|
||||
} else if (!strncmp(ns, "qemu:", ns_len)) {
|
||||
trace_nbd_negotiate_meta_query_parse("qemu:");
|
||||
return nbd_meta_qemu_query(client, meta, len, errp);
|
||||
if (nbd_meta_base_query(client, meta, query)) {
|
||||
return 1;
|
||||
}
|
||||
if (nbd_meta_qemu_query(client, meta, query)) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
trace_nbd_negotiate_meta_query_skip("unknown namespace");
|
||||
return nbd_opt_skip(client, len, errp);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* nbd_negotiate_meta_queries
|
||||
|
Loading…
Reference in New Issue
Block a user