Blame 1012-scsi-file-posix-add-support-for-persistent-reservati.patch

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