26ba25
From de65ad93b1b0a38c8a3f26eb1bc4090eb19db00f Mon Sep 17 00:00:00 2001
26ba25
From: John Snow <jsnow@redhat.com>
26ba25
Date: Wed, 18 Jul 2018 22:55:02 +0200
26ba25
Subject: [PATCH 244/268] nbd/server: add nbd_meta_empty_or_pattern helper
26ba25
26ba25
RH-Author: John Snow <jsnow@redhat.com>
26ba25
Message-id: <20180718225511.14878-27-jsnow@redhat.com>
26ba25
Patchwork-id: 81398
26ba25
O-Subject: [RHEL-7.6 qemu-kvm-rhev PATCH 26/35] nbd/server: add nbd_meta_empty_or_pattern helper
26ba25
Bugzilla: 1207657
26ba25
RH-Acked-by: Eric Blake <eblake@redhat.com>
26ba25
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
26ba25
RH-Acked-by: Fam Zheng <famz@redhat.com>
26ba25
26ba25
From: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
26ba25
26ba25
Add nbd_meta_pattern() and nbd_meta_empty_or_pattern() helpers for
26ba25
metadata query parsing. nbd_meta_pattern() will be reused for the
26ba25
"qemu" namespace in following patches.
26ba25
26ba25
Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
26ba25
Message-Id: <20180609151758.17343-4-vsementsov@virtuozzo.com>
26ba25
Reviewed-by: Eric Blake <eblake@redhat.com>
26ba25
[eblake: comment tweaks]
26ba25
Signed-off-by: Eric Blake <eblake@redhat.com>
26ba25
(cherry picked from commit b0769d8f8df0b51881f1f15c9e29722cf6191a43)
26ba25
Signed-off-by: John Snow <jsnow@redhat.com>
26ba25
26ba25
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
26ba25
---
26ba25
 nbd/server.c | 89 ++++++++++++++++++++++++++++++++++++++++++------------------
26ba25
 1 file changed, 62 insertions(+), 27 deletions(-)
26ba25
26ba25
diff --git a/nbd/server.c b/nbd/server.c
26ba25
index 26cc41a..9171cd4 100644
26ba25
--- a/nbd/server.c
26ba25
+++ b/nbd/server.c
26ba25
@@ -733,53 +733,87 @@ static int nbd_negotiate_send_meta_context(NBDClient *client,
26ba25
     return qio_channel_writev_all(client->ioc, iov, 2, errp) < 0 ? -EIO : 0;
26ba25
 }
26ba25
 
26ba25
-/* nbd_meta_base_query
26ba25
- *
26ba25
- * Handle queries to 'base' namespace. For now, only the base:allocation
26ba25
- * context is available.  'len' is the amount of text remaining to be read from
26ba25
- * the current name, after the 'base:' portion has been stripped.
26ba25
+/* Read strlen(@pattern) bytes, and set @match to true if they match @pattern.
26ba25
+ * @match is never set to false.
26ba25
  *
26ba25
  * Return -errno on I/O error, 0 if option was completely handled by
26ba25
  * sending a reply about inconsistent lengths, or 1 on success.
26ba25
  *
26ba25
- * Note: return code = 1 doesn't mean that we've parsed the "base:allocation"
26ba25
- * namespace. It only means that there are no errors.
26ba25
+ * Note: return code = 1 doesn't mean that we've read exactly @pattern.
26ba25
+ * It only means that there are no errors.
26ba25
  */
26ba25
-static int nbd_meta_base_query(NBDClient *client, NBDExportMetaContexts *meta,
26ba25
-                               uint32_t len, Error **errp)
26ba25
+static int nbd_meta_pattern(NBDClient *client, const char *pattern, bool *match,
26ba25
+                            Error **errp)
26ba25
 {
26ba25
     int ret;
26ba25
-    char query[sizeof("allocation") - 1];
26ba25
-    size_t alen = strlen("allocation");
26ba25
-
26ba25
-    if (len == 0) {
26ba25
-        if (client->opt == NBD_OPT_LIST_META_CONTEXT) {
26ba25
-            meta->base_allocation = true;
26ba25
-        }
26ba25
-        trace_nbd_negotiate_meta_query_parse("base:");
26ba25
-        return 1;
26ba25
-    }
26ba25
+    char *query;
26ba25
+    size_t len = strlen(pattern);
26ba25
 
26ba25
-    if (len != alen) {
26ba25
-        trace_nbd_negotiate_meta_query_skip("not base:allocation");
26ba25
-        return nbd_opt_skip(client, len, errp);
26ba25
-    }
26ba25
+    assert(len);
26ba25
 
26ba25
+    query = g_malloc(len);
26ba25
     ret = nbd_opt_read(client, query, len, errp);
26ba25
     if (ret <= 0) {
26ba25
+        g_free(query);
26ba25
         return ret;
26ba25
     }
26ba25
 
26ba25
-    if (strncmp(query, "allocation", alen) == 0) {
26ba25
-        trace_nbd_negotiate_meta_query_parse("base:allocation");
26ba25
-        meta->base_allocation = true;
26ba25
+    if (strncmp(query, pattern, len) == 0) {
26ba25
+        trace_nbd_negotiate_meta_query_parse(pattern);
26ba25
+        *match = true;
26ba25
     } else {
26ba25
-        trace_nbd_negotiate_meta_query_skip("not base:allocation");
26ba25
+        trace_nbd_negotiate_meta_query_skip("pattern not matched");
26ba25
     }
26ba25
+    g_free(query);
26ba25
 
26ba25
     return 1;
26ba25
 }
26ba25
 
26ba25
+/*
26ba25
+ * Read @len bytes, and set @match to true if they match @pattern, or if @len
26ba25
+ * is 0 and the client is performing _LIST_. @match is never set to false.
26ba25
+ *
26ba25
+ * Return -errno on I/O error, 0 if option was completely handled by
26ba25
+ * sending a reply about inconsistent lengths, or 1 on success.
26ba25
+ *
26ba25
+ * Note: return code = 1 doesn't mean that we've read exactly @pattern.
26ba25
+ * It only means that there are no errors.
26ba25
+ */
26ba25
+static int nbd_meta_empty_or_pattern(NBDClient *client, const char *pattern,
26ba25
+                                     uint32_t len, bool *match, Error **errp)
26ba25
+{
26ba25
+    if (len == 0) {
26ba25
+        if (client->opt == NBD_OPT_LIST_META_CONTEXT) {
26ba25
+            *match = true;
26ba25
+        }
26ba25
+        trace_nbd_negotiate_meta_query_parse("empty");
26ba25
+        return 1;
26ba25
+    }
26ba25
+
26ba25
+    if (len != strlen(pattern)) {
26ba25
+        trace_nbd_negotiate_meta_query_skip("different lengths");
26ba25
+        return nbd_opt_skip(client, len, errp);
26ba25
+    }
26ba25
+
26ba25
+    return nbd_meta_pattern(client, pattern, match, errp);
26ba25
+}
26ba25
+
26ba25
+/* nbd_meta_base_query
26ba25
+ *
26ba25
+ * Handle queries to 'base' namespace. For now, only the base:allocation
26ba25
+ * context is available.  'len' is the amount of text remaining to be read from
26ba25
+ * the current name, after the 'base:' portion has been stripped.
26ba25
+ *
26ba25
+ * Return -errno on I/O error, 0 if option was completely handled by
26ba25
+ * sending a reply about inconsistent lengths, or 1 on success.
26ba25
+ */
26ba25
+static int nbd_meta_base_query(NBDClient *client, NBDExportMetaContexts *meta,
26ba25
+                               uint32_t len, Error **errp)
26ba25
+{
26ba25
+    return nbd_meta_empty_or_pattern(client, "allocation", len,
26ba25
+                                     &meta->base_allocation, errp);
26ba25
+}
26ba25
+
26ba25
 /* nbd_negotiate_meta_query
26ba25
  *
26ba25
  * Parse namespace name and call corresponding function to parse body of the
26ba25
@@ -823,6 +857,7 @@ static int nbd_negotiate_meta_query(NBDClient *client,
26ba25
         return nbd_opt_skip(client, len, errp);
26ba25
     }
26ba25
 
26ba25
+    trace_nbd_negotiate_meta_query_parse("base:");
26ba25
     return nbd_meta_base_query(client, meta, len, errp);
26ba25
 }
26ba25
 
26ba25
-- 
26ba25
1.8.3.1
26ba25