Blame SOURCES/kvm-scsi-add-persistent-reservation-manager-using-qemu-p.patch

4a2fec
From 65c8aa05dcaaad802c92a55c9352305c3de39de0 Mon Sep 17 00:00:00 2001
4a2fec
From: Paolo Bonzini <pbonzini@redhat.com>
4a2fec
Date: Sat, 2 Dec 2017 12:19:51 +0100
4a2fec
Subject: [PATCH 25/36] scsi: add persistent reservation manager using
4a2fec
 qemu-pr-helper
4a2fec
4a2fec
RH-Author: Paolo Bonzini <pbonzini@redhat.com>
4a2fec
Message-id: <20171202121953.13317-16-pbonzini@redhat.com>
4a2fec
Patchwork-id: 78084
4a2fec
O-Subject: [RHEL7.4 qemu-kvm-rhev PATCH 15/17] scsi: add persistent reservation manager using qemu-pr-helper
4a2fec
Bugzilla: 1464908
4a2fec
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
4a2fec
RH-Acked-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
4a2fec
RH-Acked-by: John Snow <jsnow@redhat.com>
4a2fec
4a2fec
This adds a concrete subclass of pr-manager that talks to qemu-pr-helper.
4a2fec
4a2fec
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
4a2fec
(cherry picked from commit 9bad2a6b9d0aeb2dcf91a07652cc63bbb6e73141)
4a2fec
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
4a2fec
---
4a2fec
 scsi/Makefile.objs       |   2 +-
4a2fec
 scsi/pr-manager-helper.c | 302 +++++++++++++++++++++++++++++++++++++++++++++++
4a2fec
 2 files changed, 303 insertions(+), 1 deletion(-)
4a2fec
 create mode 100644 scsi/pr-manager-helper.c
4a2fec
4a2fec
diff --git a/scsi/Makefile.objs b/scsi/Makefile.objs
4a2fec
index 5496d2a..4d25e47 100644
4a2fec
--- a/scsi/Makefile.objs
4a2fec
+++ b/scsi/Makefile.objs
4a2fec
@@ -1,3 +1,3 @@
4a2fec
 block-obj-y += utils.o
4a2fec
 
4a2fec
-block-obj-$(CONFIG_LINUX) += pr-manager.o
4a2fec
+block-obj-$(CONFIG_LINUX) += pr-manager.o pr-manager-helper.o
4a2fec
diff --git a/scsi/pr-manager-helper.c b/scsi/pr-manager-helper.c
4a2fec
new file mode 100644
4a2fec
index 0000000..82ff6b6
4a2fec
--- /dev/null
4a2fec
+++ b/scsi/pr-manager-helper.c
4a2fec
@@ -0,0 +1,302 @@
4a2fec
+/*
4a2fec
+ * Persistent reservation manager that talks to qemu-pr-helper
4a2fec
+ *
4a2fec
+ * Copyright (c) 2017 Red Hat, Inc.
4a2fec
+ *
4a2fec
+ * Author: Paolo Bonzini <pbonzini@redhat.com>
4a2fec
+ *
4a2fec
+ * This code is licensed under the LGPL v2.1 or later.
4a2fec
+ *
4a2fec
+ */
4a2fec
+
4a2fec
+#include "qemu/osdep.h"
4a2fec
+#include "qapi/error.h"
4a2fec
+#include "scsi/constants.h"
4a2fec
+#include "scsi/pr-manager.h"
4a2fec
+#include "scsi/utils.h"
4a2fec
+#include "io/channel.h"
4a2fec
+#include "io/channel-socket.h"
4a2fec
+#include "pr-helper.h"
4a2fec
+
4a2fec
+#include <scsi/sg.h>
4a2fec
+
4a2fec
+#define PR_MAX_RECONNECT_ATTEMPTS 5
4a2fec
+
4a2fec
+#define TYPE_PR_MANAGER_HELPER "pr-manager-helper"
4a2fec
+
4a2fec
+#define PR_MANAGER_HELPER(obj) \
4a2fec
+     OBJECT_CHECK(PRManagerHelper, (obj), \
4a2fec
+                  TYPE_PR_MANAGER_HELPER)
4a2fec
+
4a2fec
+typedef struct PRManagerHelper {
4a2fec
+    /* <private> */
4a2fec
+    PRManager parent;
4a2fec
+
4a2fec
+    char *path;
4a2fec
+
4a2fec
+    QemuMutex lock;
4a2fec
+    QIOChannel *ioc;
4a2fec
+} PRManagerHelper;
4a2fec
+
4a2fec
+/* Called with lock held.  */
4a2fec
+static int pr_manager_helper_read(PRManagerHelper *pr_mgr,
4a2fec
+                                  void *buf, int sz, Error **errp)
4a2fec
+{
4a2fec
+    ssize_t r = qio_channel_read_all(pr_mgr->ioc, buf, sz, errp);
4a2fec
+
4a2fec
+    if (r < 0) {
4a2fec
+        object_unref(OBJECT(pr_mgr->ioc));
4a2fec
+        pr_mgr->ioc = NULL;
4a2fec
+        return -EINVAL;
4a2fec
+    }
4a2fec
+
4a2fec
+    return 0;
4a2fec
+}
4a2fec
+
4a2fec
+/* Called with lock held.  */
4a2fec
+static int pr_manager_helper_write(PRManagerHelper *pr_mgr,
4a2fec
+                                   int fd,
4a2fec
+                                   const void *buf, int sz, Error **errp)
4a2fec
+{
4a2fec
+    size_t nfds = (fd != -1);
4a2fec
+    while (sz > 0) {
4a2fec
+        struct iovec iov;
4a2fec
+        ssize_t n_written;
4a2fec
+
4a2fec
+        iov.iov_base = (void *)buf;
4a2fec
+        iov.iov_len = sz;
4a2fec
+        n_written = qio_channel_writev_full(QIO_CHANNEL(pr_mgr->ioc), &iov, 1,
4a2fec
+                                            nfds ? &fd : NULL, nfds, errp);
4a2fec
+
4a2fec
+        if (n_written <= 0) {
4a2fec
+            assert(n_written != QIO_CHANNEL_ERR_BLOCK);
4a2fec
+            object_unref(OBJECT(pr_mgr->ioc));
4a2fec
+            return n_written < 0 ? -EINVAL : 0;
4a2fec
+        }
4a2fec
+
4a2fec
+        nfds = 0;
4a2fec
+        buf += n_written;
4a2fec
+        sz -= n_written;
4a2fec
+    }
4a2fec
+
4a2fec
+    return 0;
4a2fec
+}
4a2fec
+
4a2fec
+/* Called with lock held.  */
4a2fec
+static int pr_manager_helper_initialize(PRManagerHelper *pr_mgr,
4a2fec
+                                        Error **errp)
4a2fec
+{
4a2fec
+    char *path = g_strdup(pr_mgr->path);
4a2fec
+    SocketAddress saddr = {
4a2fec
+        .type = SOCKET_ADDRESS_TYPE_UNIX,
4a2fec
+        .u.q_unix.path = path
4a2fec
+    };
4a2fec
+    QIOChannelSocket *sioc = qio_channel_socket_new();
4a2fec
+    Error *local_err = NULL;
4a2fec
+
4a2fec
+    uint32_t flags;
4a2fec
+    int r;
4a2fec
+
4a2fec
+    assert(!pr_mgr->ioc);
4a2fec
+    qio_channel_set_name(QIO_CHANNEL(sioc), "pr-manager-helper");
4a2fec
+    qio_channel_socket_connect_sync(sioc,
4a2fec
+                                    &saddr,
4a2fec
+                                    &local_err);
4a2fec
+    g_free(path);
4a2fec
+    if (local_err) {
4a2fec
+        object_unref(OBJECT(sioc));
4a2fec
+        error_propagate(errp, local_err);
4a2fec
+        return -ENOTCONN;
4a2fec
+    }
4a2fec
+
4a2fec
+    qio_channel_set_delay(QIO_CHANNEL(sioc), false);
4a2fec
+    pr_mgr->ioc = QIO_CHANNEL(sioc);
4a2fec
+
4a2fec
+    /* A simple feature negotation protocol, even though there is
4a2fec
+     * no optional feature right now.
4a2fec
+     */
4a2fec
+    r = pr_manager_helper_read(pr_mgr, &flags, sizeof(flags), errp);
4a2fec
+    if (r < 0) {
4a2fec
+        goto out_close;
4a2fec
+    }
4a2fec
+
4a2fec
+    flags = 0;
4a2fec
+    r = pr_manager_helper_write(pr_mgr, -1, &flags, sizeof(flags), errp);
4a2fec
+    if (r < 0) {
4a2fec
+        goto out_close;
4a2fec
+    }
4a2fec
+
4a2fec
+    return 0;
4a2fec
+
4a2fec
+out_close:
4a2fec
+    object_unref(OBJECT(pr_mgr->ioc));
4a2fec
+    pr_mgr->ioc = NULL;
4a2fec
+    return r;
4a2fec
+}
4a2fec
+
4a2fec
+static int pr_manager_helper_run(PRManager *p,
4a2fec
+                                 int fd, struct sg_io_hdr *io_hdr)
4a2fec
+{
4a2fec
+    PRManagerHelper *pr_mgr = PR_MANAGER_HELPER(p);
4a2fec
+
4a2fec
+    uint32_t len;
4a2fec
+    PRHelperResponse resp;
4a2fec
+    int ret;
4a2fec
+    int expected_dir;
4a2fec
+    int attempts;
4a2fec
+    uint8_t cdb[PR_HELPER_CDB_SIZE] = { 0 };
4a2fec
+
4a2fec
+    if (!io_hdr->cmd_len || io_hdr->cmd_len > PR_HELPER_CDB_SIZE) {
4a2fec
+        return -EINVAL;
4a2fec
+    }
4a2fec
+
4a2fec
+    memcpy(cdb, io_hdr->cmdp, io_hdr->cmd_len);
4a2fec
+    assert(cdb[0] == PERSISTENT_RESERVE_OUT || cdb[0] == PERSISTENT_RESERVE_IN);
4a2fec
+    expected_dir =
4a2fec
+        (cdb[0] == PERSISTENT_RESERVE_OUT ? SG_DXFER_TO_DEV : SG_DXFER_FROM_DEV);
4a2fec
+    if (io_hdr->dxfer_direction != expected_dir) {
4a2fec
+        return -EINVAL;
4a2fec
+    }
4a2fec
+
4a2fec
+    len = scsi_cdb_xfer(cdb);
4a2fec
+    if (io_hdr->dxfer_len < len || len > PR_HELPER_DATA_SIZE) {
4a2fec
+        return -EINVAL;
4a2fec
+    }
4a2fec
+
4a2fec
+    qemu_mutex_lock(&pr_mgr->lock);
4a2fec
+
4a2fec
+    /* Try to reconnect while sending the CDB.  */
4a2fec
+    for (attempts = 0; attempts < PR_MAX_RECONNECT_ATTEMPTS; attempts++) {
4a2fec
+        if (!pr_mgr->ioc) {
4a2fec
+            ret = pr_manager_helper_initialize(pr_mgr, NULL);
4a2fec
+            if (ret < 0) {
4a2fec
+                qemu_mutex_unlock(&pr_mgr->lock);
4a2fec
+                g_usleep(G_USEC_PER_SEC);
4a2fec
+                qemu_mutex_lock(&pr_mgr->lock);
4a2fec
+                continue;
4a2fec
+            }
4a2fec
+        }
4a2fec
+
4a2fec
+        ret = pr_manager_helper_write(pr_mgr, fd, cdb, ARRAY_SIZE(cdb), NULL);
4a2fec
+        if (ret >= 0) {
4a2fec
+            break;
4a2fec
+        }
4a2fec
+    }
4a2fec
+    if (ret < 0) {
4a2fec
+        goto out;
4a2fec
+    }
4a2fec
+
4a2fec
+    /* After sending the CDB, any communications failure causes the
4a2fec
+     * command to fail.  The failure is transient, retrying the command
4a2fec
+     * will invoke pr_manager_helper_initialize again.
4a2fec
+     */
4a2fec
+    if (expected_dir == SG_DXFER_TO_DEV) {
4a2fec
+        io_hdr->resid = io_hdr->dxfer_len - len;
4a2fec
+        ret = pr_manager_helper_write(pr_mgr, -1, io_hdr->dxferp, len, NULL);
4a2fec
+        if (ret < 0) {
4a2fec
+            goto out;
4a2fec
+        }
4a2fec
+    }
4a2fec
+    ret = pr_manager_helper_read(pr_mgr, &resp, sizeof(resp), NULL);
4a2fec
+    if (ret < 0) {
4a2fec
+        goto out;
4a2fec
+    }
4a2fec
+
4a2fec
+    resp.result = be32_to_cpu(resp.result);
4a2fec
+    resp.sz = be32_to_cpu(resp.sz);
4a2fec
+    if (io_hdr->dxfer_direction == SG_DXFER_FROM_DEV) {
4a2fec
+        assert(resp.sz <= io_hdr->dxfer_len);
4a2fec
+        ret = pr_manager_helper_read(pr_mgr, io_hdr->dxferp, resp.sz, NULL);
4a2fec
+        if (ret < 0) {
4a2fec
+            goto out;
4a2fec
+        }
4a2fec
+        io_hdr->resid = io_hdr->dxfer_len - resp.sz;
4a2fec
+    } else {
4a2fec
+        assert(resp.sz == 0);
4a2fec
+    }
4a2fec
+
4a2fec
+    io_hdr->status = resp.result;
4a2fec
+    if (resp.result == CHECK_CONDITION) {
4a2fec
+        io_hdr->driver_status = SG_ERR_DRIVER_SENSE;
4a2fec
+        io_hdr->sb_len_wr = MIN(io_hdr->mx_sb_len, PR_HELPER_SENSE_SIZE);
4a2fec
+        memcpy(io_hdr->sbp, resp.sense, io_hdr->sb_len_wr);
4a2fec
+    }
4a2fec
+
4a2fec
+out:
4a2fec
+    if (ret < 0) {
4a2fec
+        int sense_len = scsi_build_sense(io_hdr->sbp,
4a2fec
+                                         SENSE_CODE(LUN_COMM_FAILURE));
4a2fec
+        io_hdr->driver_status = SG_ERR_DRIVER_SENSE;
4a2fec
+        io_hdr->sb_len_wr = MIN(io_hdr->mx_sb_len, sense_len);
4a2fec
+        io_hdr->status = CHECK_CONDITION;
4a2fec
+    }
4a2fec
+    qemu_mutex_unlock(&pr_mgr->lock);
4a2fec
+    return ret;
4a2fec
+}
4a2fec
+
4a2fec
+static void pr_manager_helper_complete(UserCreatable *uc, Error **errp)
4a2fec
+{
4a2fec
+    PRManagerHelper *pr_mgr = PR_MANAGER_HELPER(uc);
4a2fec
+
4a2fec
+    qemu_mutex_lock(&pr_mgr->lock);
4a2fec
+    pr_manager_helper_initialize(pr_mgr, errp);
4a2fec
+    qemu_mutex_unlock(&pr_mgr->lock);
4a2fec
+}
4a2fec
+
4a2fec
+static char *get_path(Object *obj, Error **errp)
4a2fec
+{
4a2fec
+    PRManagerHelper *pr_mgr = PR_MANAGER_HELPER(obj);
4a2fec
+
4a2fec
+    return g_strdup(pr_mgr->path);
4a2fec
+}
4a2fec
+
4a2fec
+static void set_path(Object *obj, const char *str, Error **errp)
4a2fec
+{
4a2fec
+    PRManagerHelper *pr_mgr = PR_MANAGER_HELPER(obj);
4a2fec
+
4a2fec
+    g_free(pr_mgr->path);
4a2fec
+    pr_mgr->path = g_strdup(str);
4a2fec
+}
4a2fec
+
4a2fec
+static void pr_manager_helper_instance_finalize(Object *obj)
4a2fec
+{
4a2fec
+    PRManagerHelper *pr_mgr = PR_MANAGER_HELPER(obj);
4a2fec
+
4a2fec
+    object_unref(OBJECT(pr_mgr->ioc));
4a2fec
+    qemu_mutex_destroy(&pr_mgr->lock);
4a2fec
+}
4a2fec
+
4a2fec
+static void pr_manager_helper_instance_init(Object *obj)
4a2fec
+{
4a2fec
+    PRManagerHelper *pr_mgr = PR_MANAGER_HELPER(obj);
4a2fec
+
4a2fec
+    qemu_mutex_init(&pr_mgr->lock);
4a2fec
+}
4a2fec
+
4a2fec
+static void pr_manager_helper_class_init(ObjectClass *klass,
4a2fec
+                                         void *class_data G_GNUC_UNUSED)
4a2fec
+{
4a2fec
+    PRManagerClass *prmgr_klass = PR_MANAGER_CLASS(klass);
4a2fec
+    UserCreatableClass *uc_klass = USER_CREATABLE_CLASS(klass);
4a2fec
+
4a2fec
+    object_class_property_add_str(klass, "path", get_path, set_path,
4a2fec
+                                  &error_abort);
4a2fec
+    uc_klass->complete = pr_manager_helper_complete;
4a2fec
+    prmgr_klass->run = pr_manager_helper_run;
4a2fec
+}
4a2fec
+
4a2fec
+static const TypeInfo pr_manager_helper_info = {
4a2fec
+    .parent = TYPE_PR_MANAGER,
4a2fec
+    .name = TYPE_PR_MANAGER_HELPER,
4a2fec
+    .instance_size = sizeof(PRManagerHelper),
4a2fec
+    .instance_init = pr_manager_helper_instance_init,
4a2fec
+    .instance_finalize = pr_manager_helper_instance_finalize,
4a2fec
+    .class_init = pr_manager_helper_class_init,
4a2fec
+};
4a2fec
+
4a2fec
+static void pr_manager_helper_register_types(void)
4a2fec
+{
4a2fec
+    type_register_static(&pr_manager_helper_info);
4a2fec
+}
4a2fec
+
4a2fec
+type_init(pr_manager_helper_register_types);
4a2fec
-- 
4a2fec
1.8.3.1
4a2fec