From: Eric Blake <eblake@redhat.com>
To: qemu-devel@nongnu.org
Cc: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>,
Paolo Bonzini <pbonzini@redhat.com>,
"open list:Network Block Dev..." <qemu-block@nongnu.org>
Subject: [Qemu-devel] [PULL 4/7] nbd/server: add nbd_meta_empty_or_pattern helper
Date: Wed, 20 Jun 2018 22:19:54 -0500 [thread overview]
Message-ID: <20180621031957.134718-5-eblake@redhat.com> (raw)
In-Reply-To: <20180621031957.134718-1-eblake@redhat.com>
From: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
Add nbd_meta_pattern() and nbd_meta_empty_or_pattern() helpers for
metadata query parsing. nbd_meta_pattern() will be reused for the
"qemu" namespace in following patches.
Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
Message-Id: <20180609151758.17343-4-vsementsov@virtuozzo.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
[eblake: comment tweaks]
Signed-off-by: Eric Blake <eblake@redhat.com>
---
nbd/server.c | 101 ++++++++++++++++++++++++++++++++++++++++-------------------
1 file changed, 68 insertions(+), 33 deletions(-)
diff --git a/nbd/server.c b/nbd/server.c
index bbdc3c01b9f..cea5192addb 100644
--- a/nbd/server.c
+++ b/nbd/server.c
@@ -733,6 +733,71 @@ 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.
+ */
+static int nbd_meta_pattern(NBDClient *client, const char *pattern, bool *match,
+ Error **errp)
+{
+ int ret;
+ char *query;
+ size_t len = strlen(pattern);
+
+ assert(len);
+
+ query = g_malloc(len);
+ ret = nbd_opt_read(client, query, len, errp);
+ if (ret <= 0) {
+ g_free(query);
+ return ret;
+ }
+
+ if (strncmp(query, pattern, len) == 0) {
+ trace_nbd_negotiate_meta_query_parse(pattern);
+ *match = true;
+ } else {
+ trace_nbd_negotiate_meta_query_skip("pattern not matched");
+ }
+ g_free(query);
+
+ return 1;
+}
+
+/*
+ * 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.
+ */
+static int nbd_meta_empty_or_pattern(NBDClient *client, const char *pattern,
+ uint32_t len, bool *match, Error **errp)
+{
+ if (len == 0) {
+ if (client->opt == NBD_OPT_LIST_META_CONTEXT) {
+ *match = true;
+ }
+ trace_nbd_negotiate_meta_query_parse("empty");
+ return 1;
+ }
+
+ if (len != strlen(pattern)) {
+ trace_nbd_negotiate_meta_query_skip("different lengths");
+ return nbd_opt_skip(client, len, errp);
+ }
+
+ return nbd_meta_pattern(client, pattern, match, errp);
+}
+
/* nbd_meta_base_query
*
* Handle queries to 'base' namespace. For now, only the base:allocation
@@ -741,43 +806,12 @@ static int nbd_negotiate_send_meta_context(NBDClient *client,
*
* 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 parsed the "base:allocation"
- * namespace. It only means that there are no errors.
*/
static int nbd_meta_base_query(NBDClient *client, NBDExportMetaContexts *meta,
uint32_t len, Error **errp)
{
- int ret;
- char query[sizeof("allocation") - 1];
- size_t alen = strlen("allocation");
-
- if (len == 0) {
- if (client->opt == NBD_OPT_LIST_META_CONTEXT) {
- meta->base_allocation = true;
- }
- trace_nbd_negotiate_meta_query_parse("base:");
- return 1;
- }
-
- if (len != alen) {
- trace_nbd_negotiate_meta_query_skip("not base:allocation");
- return nbd_opt_skip(client, len, errp);
- }
-
- ret = nbd_opt_read(client, query, len, errp);
- if (ret <= 0) {
- return ret;
- }
-
- if (strncmp(query, "allocation", alen) == 0) {
- trace_nbd_negotiate_meta_query_parse("base:allocation");
- meta->base_allocation = true;
- } else {
- trace_nbd_negotiate_meta_query_skip("not base:allocation");
- }
-
- return 1;
+ return nbd_meta_empty_or_pattern(client, "allocation", len,
+ &meta->base_allocation, errp);
}
/* nbd_negotiate_meta_query
@@ -823,6 +857,7 @@ static int nbd_negotiate_meta_query(NBDClient *client,
return nbd_opt_skip(client, len, errp);
}
+ trace_nbd_negotiate_meta_query_parse("base:");
return nbd_meta_base_query(client, meta, len, errp);
}
--
2.14.4
next prev parent reply other threads:[~2018-06-21 3:20 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-06-21 3:19 [Qemu-devel] [PULL 0/7] bitmap export over NBD Eric Blake
2018-06-21 3:19 ` [Qemu-devel] [PULL 1/7] tests: Simplify .gitignore Eric Blake
2018-06-21 3:19 ` [Qemu-devel] [PULL 2/7] nbd/server: fix trace Eric Blake
2018-06-21 3:19 ` [Qemu-devel] [PULL 3/7] nbd/server: refactor NBDExportMetaContexts Eric Blake
2018-06-21 3:19 ` Eric Blake [this message]
2018-06-21 3:19 ` [Qemu-devel] [PULL 5/7] nbd/server: implement dirty bitmap export Eric Blake
2018-06-21 10:17 ` Vladimir Sementsov-Ogievskiy
2018-06-21 11:47 ` Eric Blake
2018-06-21 3:19 ` [Qemu-devel] [PULL 6/7] qapi: new qmp command nbd-server-add-bitmap Eric Blake
2018-06-21 3:19 ` [Qemu-devel] [PULL 7/7] docs/interop: add nbd.txt Eric Blake
2018-06-21 5:34 ` [Qemu-devel] [PULL 0/7] bitmap export over NBD no-reply
2018-06-21 11:40 ` Eric Blake
2018-06-21 6:09 ` no-reply
2018-06-21 9:03 ` Peter Maydell
2018-06-21 11:52 ` Eric Blake
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20180621031957.134718-5-eblake@redhat.com \
--to=eblake@redhat.com \
--cc=pbonzini@redhat.com \
--cc=qemu-block@nongnu.org \
--cc=qemu-devel@nongnu.org \
--cc=vsementsov@virtuozzo.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).