Blame SOURCES/kvm-scsi-file-posix-add-support-for-persistent-reservati.patch

9bac43
From f9b538c808178d27af2d4726a8f4b36a305b072b Mon Sep 17 00:00:00 2001
9bac43
From: Paolo Bonzini <pbonzini@redhat.com>
9bac43
Date: Sat, 2 Dec 2017 12:19:48 +0100
9bac43
Subject: [PATCH 22/36] scsi, file-posix: add support for persistent
9bac43
 reservation management
9bac43
9bac43
RH-Author: Paolo Bonzini <pbonzini@redhat.com>
9bac43
Message-id: <20171202121953.13317-13-pbonzini@redhat.com>
9bac43
Patchwork-id: 78087
9bac43
O-Subject: [RHEL7.4 qemu-kvm-rhev PATCH 12/17] scsi, file-posix: add support for persistent reservation management
9bac43
Bugzilla: 1464908
9bac43
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
9bac43
RH-Acked-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
9bac43
RH-Acked-by: John Snow <jsnow@redhat.com>
9bac43
9bac43
It is a common requirement for virtual machine to send persistent
9bac43
reservations, but this currently requires either running QEMU with
9bac43
CAP_SYS_RAWIO, or using out-of-tree patches that let an unprivileged
9bac43
QEMU bypass Linux's filter on SG_IO commands.
9bac43
9bac43
As an alternative mechanism, the next patches will introduce a
9bac43
privileged helper to run persistent reservation commands without
9bac43
expanding QEMU's attack surface unnecessarily.
9bac43
9bac43
The helper is invoked through a "pr-manager" QOM object, to which
9bac43
file-posix.c passes SG_IO requests for PERSISTENT RESERVE OUT and
9bac43
PERSISTENT RESERVE IN commands.  For example:
9bac43
9bac43
  $ qemu-system-x86_64
9bac43
      -device virtio-scsi \
9bac43
      -object pr-manager-helper,id=helper0,path=/var/run/qemu-pr-helper.sock
9bac43
      -drive if=none,id=hd,driver=raw,file.filename=/dev/sdb,file.pr-manager=helper0
9bac43
      -device scsi-block,drive=hd
9bac43
9bac43
or:
9bac43
9bac43
  $ qemu-system-x86_64
9bac43
      -device virtio-scsi \
9bac43
      -object pr-manager-helper,id=helper0,path=/var/run/qemu-pr-helper.sock
9bac43
      -blockdev node-name=hd,driver=raw,file.driver=host_device,file.filename=/dev/sdb,file.pr-manager=helper0
9bac43
      -device scsi-block,drive=hd
9bac43
9bac43
Multiple pr-manager implementations are conceivable and possible, though
9bac43
only one is implemented right now.  For example, a pr-manager could:
9bac43
9bac43
- talk directly to the multipath daemon from a privileged QEMU
9bac43
  (i.e. QEMU links to libmpathpersist); this makes reservation work
9bac43
  properly with multipath, but still requires CAP_SYS_RAWIO
9bac43
9bac43
- use the Linux IOC_PR_* ioctls (they require CAP_SYS_ADMIN though)
9bac43
9bac43
- more interestingly, implement reservations directly in QEMU
9bac43
  through file system locks or a shared database (e.g. sqlite)
9bac43
9bac43
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
9bac43
(cherry picked from commit 7c9e527659c67d4d7b41d9504f93d2d7ee482488)
9bac43
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
9bac43
---
9bac43
 Makefile.objs             |   1 +
9bac43
 block/file-posix.c        |  30 +++++++++++++
9bac43
 docs/pr-manager.rst       |  51 ++++++++++++++++++++++
9bac43
 include/scsi/pr-manager.h |  56 ++++++++++++++++++++++++
9bac43
 qapi/block-core.json      |   4 ++
9bac43
 scsi/Makefile.objs        |   2 +
9bac43
 scsi/pr-manager.c         | 109 ++++++++++++++++++++++++++++++++++++++++++++++
9bac43
 scsi/trace-events         |   3 ++
9bac43
 vl.c                      |   3 +-
9bac43
 9 files changed, 258 insertions(+), 1 deletion(-)
9bac43
 create mode 100644 docs/pr-manager.rst
9bac43
 create mode 100644 include/scsi/pr-manager.h
9bac43
 create mode 100644 scsi/pr-manager.c
9bac43
 create mode 100644 scsi/trace-events
9bac43
9bac43
diff --git a/Makefile.objs b/Makefile.objs
9bac43
index f68aa3b..64bebd0 100644
9bac43
--- a/Makefile.objs
9bac43
+++ b/Makefile.objs
9bac43
@@ -168,6 +168,7 @@ trace-events-subdirs += qapi
9bac43
 trace-events-subdirs += accel/tcg
9bac43
 trace-events-subdirs += accel/kvm
9bac43
 trace-events-subdirs += nbd
9bac43
+trace-events-subdirs += scsi
9bac43
 
9bac43
 trace-events-files = $(SRC_PATH)/trace-events $(trace-events-subdirs:%=$(SRC_PATH)/%/trace-events)
9bac43
 
9bac43
diff --git a/block/file-posix.c b/block/file-posix.c
9bac43
index cb3bfce..9cacf06 100644
9bac43
--- a/block/file-posix.c
9bac43
+++ b/block/file-posix.c
9bac43
@@ -34,6 +34,9 @@
9bac43
 #include "qapi/util.h"
9bac43
 #include "qapi/qmp/qstring.h"
9bac43
 
9bac43
+#include "scsi/pr-manager.h"
9bac43
+#include "scsi/constants.h"
9bac43
+
9bac43
 #if defined(__APPLE__) && (__MACH__)
9bac43
 #include <paths.h>
9bac43
 #include <sys/param.h>
9bac43
@@ -156,6 +159,8 @@ typedef struct BDRVRawState {
9bac43
     bool page_cache_inconsistent:1;
9bac43
     bool has_fallocate;
9bac43
     bool needs_alignment;
9bac43
+
9bac43
+    PRManager *pr_mgr;
9bac43
 } BDRVRawState;
9bac43
 
9bac43
 typedef struct BDRVRawReopenState {
9bac43
@@ -403,6 +408,11 @@ static QemuOptsList raw_runtime_opts = {
9bac43
             .type = QEMU_OPT_STRING,
9bac43
             .help = "file locking mode (on/off/auto, default: auto)",
9bac43
         },
9bac43
+        {
9bac43
+            .name = "pr-manager",
9bac43
+            .type = QEMU_OPT_STRING,
9bac43
+            .help = "id of persistent reservation manager object (default: none)",
9bac43
+        },
9bac43
         { /* end of list */ }
9bac43
     },
9bac43
 };
9bac43
@@ -414,6 +424,7 @@ static int raw_open_common(BlockDriverState *bs, QDict *options,
9bac43
     QemuOpts *opts;
9bac43
     Error *local_err = NULL;
9bac43
     const char *filename = NULL;
9bac43
+    const char *str;
9bac43
     BlockdevAioOptions aio, aio_default;
9bac43
     int fd, ret;
9bac43
     struct stat st;
9bac43
@@ -475,6 +486,16 @@ static int raw_open_common(BlockDriverState *bs, QDict *options,
9bac43
         abort();
9bac43
     }
9bac43
 
9bac43
+    str = qemu_opt_get(opts, "pr-manager");
9bac43
+    if (str) {
9bac43
+        s->pr_mgr = pr_manager_lookup(str, &local_err);
9bac43
+        if (local_err) {
9bac43
+            error_propagate(errp, local_err);
9bac43
+            ret = -EINVAL;
9bac43
+            goto fail;
9bac43
+        }
9bac43
+    }
9bac43
+
9bac43
     s->open_flags = open_flags;
9bac43
     raw_parse_flags(bdrv_flags, &s->open_flags);
9bac43
 
9bac43
@@ -2597,6 +2618,15 @@ static BlockAIOCB *hdev_aio_ioctl(BlockDriverState *bs,
9bac43
     if (fd_open(bs) < 0)
9bac43
         return NULL;
9bac43
 
9bac43
+    if (req == SG_IO && s->pr_mgr) {
9bac43
+        struct sg_io_hdr *io_hdr = buf;
9bac43
+        if (io_hdr->cmdp[0] == PERSISTENT_RESERVE_OUT ||
9bac43
+            io_hdr->cmdp[0] == PERSISTENT_RESERVE_IN) {
9bac43
+            return pr_manager_execute(s->pr_mgr, bdrv_get_aio_context(bs),
9bac43
+                                      s->fd, io_hdr, cb, opaque);
9bac43
+        }
9bac43
+    }
9bac43
+
9bac43
     acb = g_new(RawPosixAIOData, 1);
9bac43
     acb->bs = bs;
9bac43
     acb->aio_type = QEMU_AIO_IOCTL;
9bac43
diff --git a/docs/pr-manager.rst b/docs/pr-manager.rst
9bac43
new file mode 100644
9bac43
index 0000000..b6089fb
9bac43
--- /dev/null
9bac43
+++ b/docs/pr-manager.rst
9bac43
@@ -0,0 +1,51 @@
9bac43
+======================================
9bac43
+Persistent reservation managers
9bac43
+======================================
9bac43
+
9bac43
+SCSI persistent Reservations allow restricting access to block devices
9bac43
+to specific initiators in a shared storage setup.  When implementing
9bac43
+clustering of virtual machines, it is a common requirement for virtual
9bac43
+machines to send persistent reservation SCSI commands.  However,
9bac43
+the operating system restricts sending these commands to unprivileged
9bac43
+programs because incorrect usage can disrupt regular operation of the
9bac43
+storage fabric.
9bac43
+
9bac43
+For this reason, QEMU's SCSI passthrough devices, ``scsi-block``
9bac43
+and ``scsi-generic`` (both are only available on Linux) can delegate
9bac43
+implementation of persistent reservations to a separate object,
9bac43
+the "persistent reservation manager".  Only PERSISTENT RESERVE OUT and
9bac43
+PERSISTENT RESERVE IN commands are passed to the persistent reservation
9bac43
+manager object; other commands are processed by QEMU as usual.
9bac43
+
9bac43
+-----------------------------------------
9bac43
+Defining a persistent reservation manager
9bac43
+-----------------------------------------
9bac43
+
9bac43
+A persistent reservation manager is an instance of a subclass of the
9bac43
+"pr-manager" QOM class.
9bac43
+
9bac43
+Right now only one subclass is defined, ``pr-manager-helper``, which
9bac43
+forwards the commands to an external privileged helper program
9bac43
+over Unix sockets.  The helper program only allows sending persistent
9bac43
+reservation commands to devices for which QEMU has a file descriptor,
9bac43
+so that QEMU will not be able to effect persistent reservations
9bac43
+unless it has access to both the socket and the device.
9bac43
+
9bac43
+``pr-manager-helper`` has a single string property, ``path``, which
9bac43
+accepts the path to the helper program's Unix socket.  For example,
9bac43
+the following command line defines a ``pr-manager-helper`` object and
9bac43
+attaches it to a SCSI passthrough device::
9bac43
+
9bac43
+      $ qemu-system-x86_64
9bac43
+          -device virtio-scsi \
9bac43
+          -object pr-manager-helper,id=helper0,path=/var/run/qemu-pr-helper.sock
9bac43
+          -drive if=none,id=hd,driver=raw,file.filename=/dev/sdb,file.pr-manager=helper0
9bac43
+          -device scsi-block,drive=hd
9bac43
+
9bac43
+Alternatively, using ``-blockdev``::
9bac43
+
9bac43
+      $ qemu-system-x86_64
9bac43
+          -device virtio-scsi \
9bac43
+          -object pr-manager-helper,id=helper0,path=/var/run/qemu-pr-helper.sock
9bac43
+          -blockdev node-name=hd,driver=raw,file.driver=host_device,file.filename=/dev/sdb,file.pr-manager=helper0
9bac43
+          -device scsi-block,drive=hd
9bac43
diff --git a/include/scsi/pr-manager.h b/include/scsi/pr-manager.h
9bac43
new file mode 100644
9bac43
index 0000000..b2b37d6
9bac43
--- /dev/null
9bac43
+++ b/include/scsi/pr-manager.h
9bac43
@@ -0,0 +1,56 @@
9bac43
+#ifndef PR_MANAGER_H
9bac43
+#define PR_MANAGER_H
9bac43
+
9bac43
+#include "qom/object.h"
9bac43
+#include "qapi/qmp/qdict.h"
9bac43
+#include "qapi/visitor.h"
9bac43
+#include "qom/object_interfaces.h"
9bac43
+#include "block/aio.h"
9bac43
+
9bac43
+#define TYPE_PR_MANAGER "pr-manager"
9bac43
+
9bac43
+#define PR_MANAGER_CLASS(klass) \
9bac43
+     OBJECT_CLASS_CHECK(PRManagerClass, (klass), TYPE_PR_MANAGER)
9bac43
+#define PR_MANAGER_GET_CLASS(obj) \
9bac43
+     OBJECT_GET_CLASS(PRManagerClass, (obj), TYPE_PR_MANAGER)
9bac43
+#define PR_MANAGER(obj) \
9bac43
+     OBJECT_CHECK(PRManager, (obj), TYPE_PR_MANAGER)
9bac43
+
9bac43
+struct sg_io_hdr;
9bac43
+
9bac43
+typedef struct PRManager {
9bac43
+    /* <private> */
9bac43
+    Object parent;
9bac43
+} PRManager;
9bac43
+
9bac43
+/**
9bac43
+ * PRManagerClass:
9bac43
+ * @parent_class: the base class
9bac43
+ * @run: callback invoked in thread pool context
9bac43
+ */
9bac43
+typedef struct PRManagerClass {
9bac43
+    /* <private> */
9bac43
+    ObjectClass parent_class;
9bac43
+
9bac43
+    /* <public> */
9bac43
+    int (*run)(PRManager *pr_mgr, int fd, struct sg_io_hdr *hdr);
9bac43
+} PRManagerClass;
9bac43
+
9bac43
+BlockAIOCB *pr_manager_execute(PRManager *pr_mgr,
9bac43
+                               AioContext *ctx, int fd,
9bac43
+                               struct sg_io_hdr *hdr,
9bac43
+                               BlockCompletionFunc *complete,
9bac43
+                               void *opaque);
9bac43
+
9bac43
+#ifdef CONFIG_LINUX
9bac43
+PRManager *pr_manager_lookup(const char *id, Error **errp);
9bac43
+#else
9bac43
+static inline PRManager *pr_manager_lookup(const char *id, Error **errp)
9bac43
+{
9bac43
+    /* The classes do not exist at all!  */
9bac43
+    error_setg(errp, "No persistent reservation manager with id '%s'", id);
9bac43
+    return NULL;
9bac43
+}
9bac43
+#endif
9bac43
+
9bac43
+#endif
9bac43
diff --git a/qapi/block-core.json b/qapi/block-core.json
9bac43
index 8f5f105..15fc08f 100644
9bac43
--- a/qapi/block-core.json
9bac43
+++ b/qapi/block-core.json
9bac43
@@ -2191,6 +2191,9 @@
9bac43
 # Driver specific block device options for the file backend.
9bac43
 #
9bac43
 # @filename:    path to the image file
9bac43
+# @pr-manager:  the id for the object that will handle persistent reservations
9bac43
+#               for this device (default: none, forward the commands via SG_IO;
9bac43
+#               since 2.11)
9bac43
 # @aio:         AIO backend (default: threads) (since: 2.8)
9bac43
 # @locking:     whether to enable file locking. If set to 'auto', only enable
9bac43
 #               when Open File Descriptor (OFD) locking API is available
9bac43
@@ -2200,6 +2203,7 @@
9bac43
 ##
9bac43
 { 'struct': 'BlockdevOptionsFile',
9bac43
   'data': { 'filename': 'str',
9bac43
+            '*pr-manager': 'str',
9bac43
             '*locking': 'OnOffAuto',
9bac43
             '*aio': 'BlockdevAioOptions' } }
9bac43
 
9bac43
diff --git a/scsi/Makefile.objs b/scsi/Makefile.objs
9bac43
index 31b82a5..5496d2a 100644
9bac43
--- a/scsi/Makefile.objs
9bac43
+++ b/scsi/Makefile.objs
9bac43
@@ -1 +1,3 @@
9bac43
 block-obj-y += utils.o
9bac43
+
9bac43
+block-obj-$(CONFIG_LINUX) += pr-manager.o
9bac43
diff --git a/scsi/pr-manager.c b/scsi/pr-manager.c
9bac43
new file mode 100644
9bac43
index 0000000..87c45db
9bac43
--- /dev/null
9bac43
+++ b/scsi/pr-manager.c
9bac43
@@ -0,0 +1,109 @@
9bac43
+/*
9bac43
+ * Persistent reservation manager abstract class
9bac43
+ *
9bac43
+ * Copyright (c) 2017 Red Hat, Inc.
9bac43
+ *
9bac43
+ * Author: Paolo Bonzini <pbonzini@redhat.com>
9bac43
+ *
9bac43
+ * This code is licensed under the LGPL.
9bac43
+ *
9bac43
+ */
9bac43
+
9bac43
+#include "qemu/osdep.h"
9bac43
+#include <scsi/sg.h>
9bac43
+
9bac43
+#include "qapi/error.h"
9bac43
+#include "block/aio.h"
9bac43
+#include "block/thread-pool.h"
9bac43
+#include "scsi/pr-manager.h"
9bac43
+#include "trace.h"
9bac43
+
9bac43
+typedef struct PRManagerData {
9bac43
+    PRManager *pr_mgr;
9bac43
+    struct sg_io_hdr *hdr;
9bac43
+    int fd;
9bac43
+} PRManagerData;
9bac43
+
9bac43
+static int pr_manager_worker(void *opaque)
9bac43
+{
9bac43
+    PRManagerData *data = opaque;
9bac43
+    PRManager *pr_mgr = data->pr_mgr;
9bac43
+    PRManagerClass *pr_mgr_class =
9bac43
+        PR_MANAGER_GET_CLASS(pr_mgr);
9bac43
+    struct sg_io_hdr *hdr = data->hdr;
9bac43
+    int fd = data->fd;
9bac43
+    int r;
9bac43
+
9bac43
+    g_free(data);
9bac43
+    trace_pr_manager_run(fd, hdr->cmdp[0], hdr->cmdp[1]);
9bac43
+
9bac43
+    /* The reference was taken in pr_manager_execute.  */
9bac43
+    r = pr_mgr_class->run(pr_mgr, fd, hdr);
9bac43
+    object_unref(OBJECT(pr_mgr));
9bac43
+    return r;
9bac43
+}
9bac43
+
9bac43
+
9bac43
+BlockAIOCB *pr_manager_execute(PRManager *pr_mgr,
9bac43
+                               AioContext *ctx, int fd,
9bac43
+                               struct sg_io_hdr *hdr,
9bac43
+                               BlockCompletionFunc *complete,
9bac43
+                               void *opaque)
9bac43
+{
9bac43
+    PRManagerData *data = g_new(PRManagerData, 1);
9bac43
+    ThreadPool *pool = aio_get_thread_pool(ctx);
9bac43
+
9bac43
+    trace_pr_manager_execute(fd, hdr->cmdp[0], hdr->cmdp[1], opaque);
9bac43
+    data->pr_mgr = pr_mgr;
9bac43
+    data->fd = fd;
9bac43
+    data->hdr = hdr;
9bac43
+
9bac43
+    /* The matching object_unref is in pr_manager_worker.  */
9bac43
+    object_ref(OBJECT(pr_mgr));
9bac43
+    return thread_pool_submit_aio(pool, pr_manager_worker,
9bac43
+                                  data, complete, opaque);
9bac43
+}
9bac43
+
9bac43
+static const TypeInfo pr_manager_info = {
9bac43
+    .parent = TYPE_OBJECT,
9bac43
+    .name = TYPE_PR_MANAGER,
9bac43
+    .class_size = sizeof(PRManagerClass),
9bac43
+    .abstract = true,
9bac43
+    .interfaces = (InterfaceInfo[]) {
9bac43
+        { TYPE_USER_CREATABLE },
9bac43
+        { }
9bac43
+    }
9bac43
+};
9bac43
+
9bac43
+PRManager *pr_manager_lookup(const char *id, Error **errp)
9bac43
+{
9bac43
+    Object *obj;
9bac43
+    PRManager *pr_mgr;
9bac43
+
9bac43
+    obj = object_resolve_path_component(object_get_objects_root(), id);
9bac43
+    if (!obj) {
9bac43
+        error_setg(errp, "No persistent reservation manager with id '%s'", id);
9bac43
+        return NULL;
9bac43
+    }
9bac43
+
9bac43
+    pr_mgr = (PRManager *)
9bac43
+        object_dynamic_cast(obj,
9bac43
+                            TYPE_PR_MANAGER);
9bac43
+    if (!pr_mgr) {
9bac43
+        error_setg(errp,
9bac43
+                   "Object with id '%s' is not a persistent reservation manager",
9bac43
+                   id);
9bac43
+        return NULL;
9bac43
+    }
9bac43
+
9bac43
+    return pr_mgr;
9bac43
+}
9bac43
+
9bac43
+static void
9bac43
+pr_manager_register_types(void)
9bac43
+{
9bac43
+    type_register_static(&pr_manager_info);
9bac43
+}
9bac43
+
9bac43
+
9bac43
+type_init(pr_manager_register_types);
9bac43
diff --git a/scsi/trace-events b/scsi/trace-events
9bac43
new file mode 100644
9bac43
index 0000000..45f5b6e
9bac43
--- /dev/null
9bac43
+++ b/scsi/trace-events
9bac43
@@ -0,0 +1,3 @@
9bac43
+# scsi/pr-manager.c
9bac43
+pr_manager_execute(int fd, int cmd, int sa, void *opaque) "fd=%d cmd=0x%02x service action=0x%02x opaque=%p"
9bac43
+pr_manager_run(int fd, int cmd, int sa) "fd=%d cmd=0x%02x service action=0x%02x"
9bac43
diff --git a/vl.c b/vl.c
9bac43
index 55949e6..bef5ae3 100644
9bac43
--- a/vl.c
9bac43
+++ b/vl.c
9bac43
@@ -2820,7 +2820,8 @@ static int machine_set_property(void *opaque,
9bac43
  */
9bac43
 static bool object_create_initial(const char *type)
9bac43
 {
9bac43
-    if (g_str_equal(type, "rng-egd")) {
9bac43
+    if (g_str_equal(type, "rng-egd") ||
9bac43
+        g_str_has_prefix(type, "pr-manager-")) {
9bac43
         return false;
9bac43
     }
9bac43
 
9bac43
-- 
9bac43
1.8.3.1
9bac43