Blame SOURCES/kvm-nbd-server-add-nbd_meta_empty_or_pattern-helper.patch

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