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