cryptospore / rpms / qemu-kvm

Forked from rpms/qemu-kvm 2 years ago
Clone

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

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