mvadkert / rpms / qemu

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