Blame SOURCES/kvm-scsi-add-multipath-support-to-qemu-pr-helper.patch

9bac43
From d9ebb2728bc92d055f178f1c45f82979c542a9aa Mon Sep 17 00:00:00 2001
9bac43
From: Paolo Bonzini <pbonzini@redhat.com>
9bac43
Date: Sat, 2 Dec 2017 12:19:50 +0100
9bac43
Subject: [PATCH 24/36] scsi: add multipath support to qemu-pr-helper
9bac43
9bac43
RH-Author: Paolo Bonzini <pbonzini@redhat.com>
9bac43
Message-id: <20171202121953.13317-15-pbonzini@redhat.com>
9bac43
Patchwork-id: 78083
9bac43
O-Subject: [RHEL7.4 qemu-kvm-rhev PATCH 14/17] scsi: add multipath support to qemu-pr-helper
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
Proper support of persistent reservation for multipath devices requires
9bac43
communication with the multipath daemon, so that the reservation is
9bac43
registered and applied when a path comes up.  The device mapper
9bac43
utilities provide a library to do so; this patch makes qemu-pr-helper.c
9bac43
detect multipath devices and, when one is found, delegate the operation
9bac43
to libmpathpersist.
9bac43
9bac43
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
9bac43
(cherry picked from commit fe8fc5ae5c808e037fa4746cbfeb3c07ffe0af81)
9bac43
9bac43
[RHEL: do not use {0} because GCC does not like it; will be changed
9bac43
 upstream, but squash it in here so that all commits compile - Paolo]
9bac43
9bac43
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
9bac43
---
9bac43
 Makefile              |   3 +
9bac43
 configure             |  46 +++++++
9bac43
 docs/pr-manager.rst   |  27 ++++
9bac43
 include/scsi/utils.h  |   4 +
9bac43
 scsi/qemu-pr-helper.c | 346 +++++++++++++++++++++++++++++++++++++++++++++++++-
9bac43
 scsi/utils.c          |  10 ++
9bac43
 6 files changed, 433 insertions(+), 3 deletions(-)
9bac43
9bac43
diff --git a/Makefile b/Makefile
9bac43
index 3e76953..ae48405 100644
9bac43
--- a/Makefile
9bac43
+++ b/Makefile
9bac43
@@ -387,6 +387,9 @@ fsdev/virtfs-proxy-helper$(EXESUF): fsdev/virtfs-proxy-helper.o fsdev/9p-marshal
9bac43
 fsdev/virtfs-proxy-helper$(EXESUF): LIBS += -lcap
9bac43
 
9bac43
 scsi/qemu-pr-helper$(EXESUF): scsi/qemu-pr-helper.o scsi/utils.o $(crypto-obj-y) $(io-obj-y) $(qom-obj-y) $(COMMON_LDADDS)
9bac43
+ifdef CONFIG_MPATH
9bac43
+scsi/qemu-pr-helper$(EXESUF): LIBS += -ludev -lmultipath -lmpathpersist
9bac43
+endif
9bac43
 
9bac43
 qemu-img-cmds.h: $(SRC_PATH)/qemu-img-cmds.hx $(SRC_PATH)/scripts/hxtool
9bac43
 	$(call quiet-command,sh $(SRC_PATH)/scripts/hxtool -h < $< > $@,"GEN","$@")
9bac43
diff --git a/configure b/configure
9bac43
index 2df1b42..97ee507 100755
9bac43
--- a/configure
9bac43
+++ b/configure
9bac43
@@ -291,6 +291,7 @@ pixman=""
9bac43
 sdl=""
9bac43
 sdlabi=""
9bac43
 virtfs=""
9bac43
+mpath=""
9bac43
 vnc="yes"
9bac43
 sparse="no"
9bac43
 vde=""
9bac43
@@ -956,6 +957,10 @@ for opt do
9bac43
   ;;
9bac43
   --enable-virtfs) virtfs="yes"
9bac43
   ;;
9bac43
+  --disable-mpath) mpath="no"
9bac43
+  ;;
9bac43
+  --enable-mpath) mpath="yes"
9bac43
+  ;;
9bac43
   --disable-vnc) vnc="no"
9bac43
   ;;
9bac43
   --enable-vnc) vnc="yes"
9bac43
@@ -1510,6 +1515,7 @@ disabled with --disable-FEATURE, default is enabled if available:
9bac43
   vnc-png         PNG compression for VNC server
9bac43
   cocoa           Cocoa UI (Mac OS X only)
9bac43
   virtfs          VirtFS
9bac43
+  mpath           Multipath persistent reservation passthrough
9bac43
   xen             xen backend driver support
9bac43
   xen-pci-passthrough
9bac43
   brlapi          BrlAPI (Braile)
9bac43
@@ -3358,6 +3364,30 @@ else
9bac43
 fi
9bac43
 
9bac43
 ##########################################
9bac43
+# libmpathpersist probe
9bac43
+
9bac43
+if test "$mpath" != "no" ; then
9bac43
+  cat > $TMPC <
9bac43
+#include <libudev.h>
9bac43
+#include <mpath_persist.h>
9bac43
+unsigned mpath_mx_alloc_len = 1024;
9bac43
+int logsink;
9bac43
+int main(void) {
9bac43
+    struct udev *udev = udev_new();
9bac43
+    mpath_lib_init(udev);
9bac43
+    return 0;
9bac43
+}
9bac43
+EOF
9bac43
+  if compile_prog "" "-ludev -lmultipath -lmpathpersist" ; then
9bac43
+    mpathpersist=yes
9bac43
+  else
9bac43
+    mpathpersist=no
9bac43
+  fi
9bac43
+else
9bac43
+  mpathpersist=no
9bac43
+fi
9bac43
+
9bac43
+##########################################
9bac43
 # libcap probe
9bac43
 
9bac43
 if test "$cap" != "no" ; then
9bac43
@@ -5075,12 +5105,24 @@ if test "$softmmu" = yes ; then
9bac43
       fi
9bac43
       virtfs=no
9bac43
     fi
9bac43
+    if test "$mpath" != no && test "$mpathpersist" = yes ; then
9bac43
+      mpath=yes
9bac43
+    else
9bac43
+      if test "$mpath" = yes; then
9bac43
+        error_exit "Multipath requires libmpathpersist devel"
9bac43
+      fi
9bac43
+      mpath=no
9bac43
+    fi
9bac43
     tools="$tools scsi/qemu-pr-helper\$(EXESUF)"
9bac43
   else
9bac43
     if test "$virtfs" = yes; then
9bac43
       error_exit "VirtFS is supported only on Linux"
9bac43
     fi
9bac43
     virtfs=no
9bac43
+    if test "$mpath" = yes; then
9bac43
+      error_exit "Multipath is supported only on Linux"
9bac43
+    fi
9bac43
+    mpath=no
9bac43
   fi
9bac43
 fi
9bac43
 
9bac43
@@ -5327,6 +5369,7 @@ echo "Audio drivers     $audio_drv_list"
9bac43
 echo "Block whitelist (rw) $block_drv_rw_whitelist"
9bac43
 echo "Block whitelist (ro) $block_drv_ro_whitelist"
9bac43
 echo "VirtFS support    $virtfs"
9bac43
+echo "Multipath support $mpath"
9bac43
 echo "VNC support       $vnc"
9bac43
 if test "$vnc" = "yes" ; then
9bac43
     echo "VNC SASL support  $vnc_sasl"
9bac43
@@ -5777,6 +5820,9 @@ fi
9bac43
 if test "$virtfs" = "yes" ; then
9bac43
   echo "CONFIG_VIRTFS=y" >> $config_host_mak
9bac43
 fi
9bac43
+if test "$mpath" = "yes" ; then
9bac43
+  echo "CONFIG_MPATH=y" >> $config_host_mak
9bac43
+fi
9bac43
 if test "$vhost_scsi" = "yes" ; then
9bac43
   echo "CONFIG_VHOST_SCSI=y" >> $config_host_mak
9bac43
 fi
9bac43
diff --git a/docs/pr-manager.rst b/docs/pr-manager.rst
9bac43
index 7107e59..9b1de19 100644
9bac43
--- a/docs/pr-manager.rst
9bac43
+++ b/docs/pr-manager.rst
9bac43
@@ -60,6 +60,7 @@ system service and supports the following option:
9bac43
 
9bac43
 -d, --daemon              run in the background
9bac43
 -q, --quiet               decrease verbosity
9bac43
+-v, --verbose             increase verbosity
9bac43
 -f, --pidfile=path        PID file when running as a daemon
9bac43
 -k, --socket=path         path to the socket
9bac43
 -T, --trace=trace-opts    tracing options
9bac43
@@ -82,3 +83,29 @@ its operation.  To do this, add the following options:
9bac43
 
9bac43
 -u, --user=user           user to drop privileges to
9bac43
 -g, --group=group         group to drop privileges to
9bac43
+
9bac43
+---------------------------------------------
9bac43
+Multipath devices and persistent reservations
9bac43
+---------------------------------------------
9bac43
+
9bac43
+Proper support of persistent reservation for multipath devices requires
9bac43
+communication with the multipath daemon, so that the reservation is
9bac43
+registered and applied when a path is newly discovered or becomes online
9bac43
+again.  :command:`qemu-pr-helper` can do this if the ``libmpathpersist``
9bac43
+library was available on the system at build time.
9bac43
+
9bac43
+As of August 2017, a reservation key must be specified in ``multipath.conf``
9bac43
+for ``multipathd`` to check for persistent reservation for newly
9bac43
+discovered paths or reinstated paths.  The attribute can be added
9bac43
+to the ``defaults`` section or the ``multipaths`` section; for example::
9bac43
+
9bac43
+    multipaths {
9bac43
+        multipath {
9bac43
+            wwid   XXXXXXXXXXXXXXXX
9bac43
+            alias      yellow
9bac43
+            reservation_key  0x123abc
9bac43
+        }
9bac43
+    }
9bac43
+
9bac43
+Linking :program:`qemu-pr-helper` to ``libmpathpersist`` does not impede
9bac43
+its usage on regular SCSI devices.
9bac43
diff --git a/include/scsi/utils.h b/include/scsi/utils.h
9bac43
index d301b31..00a4bdb 100644
9bac43
--- a/include/scsi/utils.h
9bac43
+++ b/include/scsi/utils.h
9bac43
@@ -72,10 +72,14 @@ extern const struct SCSISense sense_code_IO_ERROR;
9bac43
 extern const struct SCSISense sense_code_I_T_NEXUS_LOSS;
9bac43
 /* Command aborted, Logical Unit failure */
9bac43
 extern const struct SCSISense sense_code_LUN_FAILURE;
9bac43
+/* Command aborted, LUN Communication failure */
9bac43
+extern const struct SCSISense sense_code_LUN_COMM_FAILURE;
9bac43
 /* Command aborted, Overlapped Commands Attempted */
9bac43
 extern const struct SCSISense sense_code_OVERLAPPED_COMMANDS;
9bac43
 /* LUN not ready, Capacity data has changed */
9bac43
 extern const struct SCSISense sense_code_CAPACITY_CHANGED;
9bac43
+/* Unit attention, SCSI bus reset */
9bac43
+extern const struct SCSISense sense_code_SCSI_BUS_RESET;
9bac43
 /* LUN not ready, Medium not present */
9bac43
 extern const struct SCSISense sense_code_UNIT_ATTENTION_NO_MEDIUM;
9bac43
 /* Unit attention, Power on, reset or bus device reset occurred */
9bac43
diff --git a/scsi/qemu-pr-helper.c b/scsi/qemu-pr-helper.c
9bac43
index f46266f..42bc2cf 100644
9bac43
--- a/scsi/qemu-pr-helper.c
9bac43
+++ b/scsi/qemu-pr-helper.c
9bac43
@@ -30,6 +30,12 @@
9bac43
 #include <pwd.h>
9bac43
 #include <grp.h>
9bac43
 
9bac43
+#ifdef CONFIG_MPATH
9bac43
+#include <libudev.h>
9bac43
+#include <mpath_cmd.h>
9bac43
+#include <mpath_persist.h>
9bac43
+#endif
9bac43
+
9bac43
 #include "qapi/error.h"
9bac43
 #include "qemu-common.h"
9bac43
 #include "qemu/cutils.h"
9bac43
@@ -60,6 +66,7 @@ static enum { RUNNING, TERMINATE, TERMINATING } state;
9bac43
 static QIOChannelSocket *server_ioc;
9bac43
 static int server_watch;
9bac43
 static int num_active_sockets = 1;
9bac43
+static int noisy;
9bac43
 static int verbose;
9bac43
 
9bac43
 #ifdef CONFIG_LIBCAP
9bac43
@@ -204,9 +211,316 @@ static int do_sgio(int fd, const uint8_t *cdb, uint8_t *sense,
9bac43
     return r;
9bac43
 }
9bac43
 
9bac43
+/* Device mapper interface */
9bac43
+
9bac43
+#ifdef CONFIG_MPATH
9bac43
+#define CONTROL_PATH "/dev/mapper/control"
9bac43
+
9bac43
+typedef struct DMData {
9bac43
+    struct dm_ioctl dm;
9bac43
+    uint8_t data[1024];
9bac43
+} DMData;
9bac43
+
9bac43
+static int control_fd;
9bac43
+
9bac43
+static void *dm_ioctl(int ioc, struct dm_ioctl *dm)
9bac43
+{
9bac43
+    static DMData d;
9bac43
+    memcpy(&d.dm, dm, sizeof(d.dm));
9bac43
+    QEMU_BUILD_BUG_ON(sizeof(d.data) < sizeof(struct dm_target_spec));
9bac43
+
9bac43
+    d.dm.version[0] = DM_VERSION_MAJOR;
9bac43
+    d.dm.version[1] = 0;
9bac43
+    d.dm.version[2] = 0;
9bac43
+    d.dm.data_size = 1024;
9bac43
+    d.dm.data_start = offsetof(DMData, data);
9bac43
+    if (ioctl(control_fd, ioc, &d) < 0) {
9bac43
+        return NULL;
9bac43
+    }
9bac43
+    memcpy(dm, &d.dm, sizeof(d.dm));
9bac43
+    return &d.data;
9bac43
+}
9bac43
+
9bac43
+static void *dm_dev_ioctl(int fd, int ioc, struct dm_ioctl *dm)
9bac43
+{
9bac43
+    struct stat st;
9bac43
+    int r;
9bac43
+
9bac43
+    r = fstat(fd, &st);
9bac43
+    if (r < 0) {
9bac43
+        perror("fstat");
9bac43
+        exit(1);
9bac43
+    }
9bac43
+
9bac43
+    dm->dev = st.st_rdev;
9bac43
+    return dm_ioctl(ioc, dm);
9bac43
+}
9bac43
+
9bac43
+static void dm_init(void)
9bac43
+{
9bac43
+    control_fd = open(CONTROL_PATH, O_RDWR);
9bac43
+    if (control_fd < 0) {
9bac43
+        perror("Cannot open " CONTROL_PATH);
9bac43
+        exit(1);
9bac43
+    }
9bac43
+    struct dm_ioctl dm = {};
9bac43
+    if (!dm_ioctl(DM_VERSION, &dm)) {
9bac43
+        perror("ioctl");
9bac43
+        exit(1);
9bac43
+    }
9bac43
+    if (dm.version[0] != DM_VERSION_MAJOR) {
9bac43
+        fprintf(stderr, "Unsupported device mapper interface");
9bac43
+        exit(1);
9bac43
+    }
9bac43
+}
9bac43
+
9bac43
+/* Variables required by libmultipath and libmpathpersist.  */
9bac43
+QEMU_BUILD_BUG_ON(PR_HELPER_DATA_SIZE > MPATH_MAX_PARAM_LEN);
9bac43
+unsigned mpath_mx_alloc_len = PR_HELPER_DATA_SIZE;
9bac43
+int logsink;
9bac43
+
9bac43
+static void multipath_pr_init(void)
9bac43
+{
9bac43
+    static struct udev *udev;
9bac43
+
9bac43
+    udev = udev_new();
9bac43
+    mpath_lib_init(udev);
9bac43
+}
9bac43
+
9bac43
+static int is_mpath(int fd)
9bac43
+{
9bac43
+    struct dm_ioctl dm = { .flags = DM_NOFLUSH_FLAG };
9bac43
+    struct dm_target_spec *tgt;
9bac43
+
9bac43
+    tgt = dm_dev_ioctl(fd, DM_TABLE_STATUS, &dm;;
9bac43
+    if (!tgt) {
9bac43
+        if (errno == ENXIO) {
9bac43
+            return 0;
9bac43
+        }
9bac43
+        perror("ioctl");
9bac43
+        exit(EXIT_FAILURE);
9bac43
+    }
9bac43
+    return !strncmp(tgt->target_type, "multipath", DM_MAX_TYPE_NAME);
9bac43
+}
9bac43
+
9bac43
+static int mpath_reconstruct_sense(int fd, int r, uint8_t *sense)
9bac43
+{
9bac43
+    switch (r) {
9bac43
+    case MPATH_PR_SUCCESS:
9bac43
+        return GOOD;
9bac43
+    case MPATH_PR_SENSE_NOT_READY:
9bac43
+    case MPATH_PR_SENSE_MEDIUM_ERROR:
9bac43
+    case MPATH_PR_SENSE_HARDWARE_ERROR:
9bac43
+    case MPATH_PR_SENSE_ABORTED_COMMAND:
9bac43
+        {
9bac43
+            /* libmpathpersist ate the exact sense.  Try to find it by
9bac43
+             * issuing TEST UNIT READY.
9bac43
+             */
9bac43
+            uint8_t cdb[6] = { TEST_UNIT_READY };
9bac43
+            int sz = 0;
9bac43
+            return do_sgio(fd, cdb, sense, NULL, &sz, SG_DXFER_NONE);
9bac43
+        }
9bac43
+
9bac43
+    case MPATH_PR_SENSE_UNIT_ATTENTION:
9bac43
+        /* Congratulations libmpathpersist, you ruined the Unit Attention...
9bac43
+         * Return a heavyweight one.
9bac43
+         */
9bac43
+        scsi_build_sense(sense, SENSE_CODE(SCSI_BUS_RESET));
9bac43
+        return CHECK_CONDITION;
9bac43
+    case MPATH_PR_SENSE_INVALID_OP:
9bac43
+        /* Only one valid sense.  */
9bac43
+        scsi_build_sense(sense, SENSE_CODE(INVALID_OPCODE));
9bac43
+        return CHECK_CONDITION;
9bac43
+    case MPATH_PR_ILLEGAL_REQ:
9bac43
+        /* Guess.  */
9bac43
+        scsi_build_sense(sense, SENSE_CODE(INVALID_PARAM));
9bac43
+        return CHECK_CONDITION;
9bac43
+    case MPATH_PR_NO_SENSE:
9bac43
+        scsi_build_sense(sense, SENSE_CODE(NO_SENSE));
9bac43
+        return CHECK_CONDITION;
9bac43
+
9bac43
+    case MPATH_PR_RESERV_CONFLICT:
9bac43
+        return RESERVATION_CONFLICT;
9bac43
+
9bac43
+    case MPATH_PR_OTHER:
9bac43
+    default:
9bac43
+        scsi_build_sense(sense, SENSE_CODE(LUN_COMM_FAILURE));
9bac43
+        return CHECK_CONDITION;
9bac43
+    }
9bac43
+}
9bac43
+
9bac43
+static int multipath_pr_in(int fd, const uint8_t *cdb, uint8_t *sense,
9bac43
+                           uint8_t *data, int sz)
9bac43
+{
9bac43
+    int rq_servact = cdb[1];
9bac43
+    struct prin_resp resp;
9bac43
+    size_t written;
9bac43
+    int r;
9bac43
+
9bac43
+    switch (rq_servact) {
9bac43
+    case MPATH_PRIN_RKEY_SA:
9bac43
+    case MPATH_PRIN_RRES_SA:
9bac43
+    case MPATH_PRIN_RCAP_SA:
9bac43
+        break;
9bac43
+    case MPATH_PRIN_RFSTAT_SA:
9bac43
+        /* Nobody implements it anyway, so bail out. */
9bac43
+    default:
9bac43
+        /* Cannot parse any other output.  */
9bac43
+        scsi_build_sense(sense, SENSE_CODE(INVALID_FIELD));
9bac43
+        return CHECK_CONDITION;
9bac43
+    }
9bac43
+
9bac43
+    r = mpath_persistent_reserve_in(fd, rq_servact, &resp, noisy, verbose);
9bac43
+    if (r == MPATH_PR_SUCCESS) {
9bac43
+        switch (rq_servact) {
9bac43
+        case MPATH_PRIN_RKEY_SA:
9bac43
+        case MPATH_PRIN_RRES_SA: {
9bac43
+            struct prin_readdescr *out = &resp.prin_descriptor.prin_readkeys;
9bac43
+            assert(sz >= 8);
9bac43
+            written = MIN(out->additional_length + 8, sz);
9bac43
+            stl_be_p(&data[0], out->prgeneration);
9bac43
+            stl_be_p(&data[4], out->additional_length);
9bac43
+            memcpy(&data[8], out->key_list, written - 8);
9bac43
+            break;
9bac43
+        }
9bac43
+        case MPATH_PRIN_RCAP_SA: {
9bac43
+            struct prin_capdescr *out = &resp.prin_descriptor.prin_readcap;
9bac43
+            assert(sz >= 6);
9bac43
+            written = 6;
9bac43
+            stw_be_p(&data[0], out->length);
9bac43
+            data[2] = out->flags[0];
9bac43
+            data[3] = out->flags[1];
9bac43
+            stw_be_p(&data[4], out->pr_type_mask);
9bac43
+            break;
9bac43
+        }
9bac43
+        default:
9bac43
+            scsi_build_sense(sense, SENSE_CODE(INVALID_OPCODE));
9bac43
+            return CHECK_CONDITION;
9bac43
+        }
9bac43
+        assert(written <= sz);
9bac43
+        memset(data + written, 0, sz - written);
9bac43
+    }
9bac43
+
9bac43
+    return mpath_reconstruct_sense(fd, r, sense);
9bac43
+}
9bac43
+
9bac43
+static int multipath_pr_out(int fd, const uint8_t *cdb, uint8_t *sense,
9bac43
+                            const uint8_t *param, int sz)
9bac43
+{
9bac43
+    int rq_servact = cdb[1];
9bac43
+    int rq_scope = cdb[2] >> 4;
9bac43
+    int rq_type = cdb[2] & 0xf;
9bac43
+    struct prout_param_descriptor paramp;
9bac43
+    char transportids[PR_HELPER_DATA_SIZE];
9bac43
+    int r;
9bac43
+
9bac43
+    switch (rq_servact) {
9bac43
+    case MPATH_PROUT_REG_SA:
9bac43
+    case MPATH_PROUT_RES_SA:
9bac43
+    case MPATH_PROUT_REL_SA:
9bac43
+    case MPATH_PROUT_CLEAR_SA:
9bac43
+    case MPATH_PROUT_PREE_SA:
9bac43
+    case MPATH_PROUT_PREE_AB_SA:
9bac43
+    case MPATH_PROUT_REG_IGN_SA:
9bac43
+        break;
9bac43
+    case MPATH_PROUT_REG_MOV_SA:
9bac43
+        /* Not supported by struct prout_param_descriptor.  */
9bac43
+    default:
9bac43
+        /* Cannot parse any other input.  */
9bac43
+        scsi_build_sense(sense, SENSE_CODE(INVALID_FIELD));
9bac43
+        return CHECK_CONDITION;
9bac43
+    }
9bac43
+
9bac43
+    /* Convert input data, especially transport IDs, to the structs
9bac43
+     * used by libmpathpersist (which, of course, will immediately
9bac43
+     * do the opposite).
9bac43
+     */
9bac43
+    memset(&paramp, 0, sizeof(paramp));
9bac43
+    memcpy(&paramp.key, &param[0], 8);
9bac43
+    memcpy(&paramp.sa_key, &param[8], 8);
9bac43
+    paramp.sa_flags = param[10];
9bac43
+    if (sz > PR_OUT_FIXED_PARAM_SIZE) {
9bac43
+        size_t transportid_len;
9bac43
+        int i, j;
9bac43
+        if (sz < PR_OUT_FIXED_PARAM_SIZE + 4) {
9bac43
+            scsi_build_sense(sense, SENSE_CODE(INVALID_PARAM_LEN));
9bac43
+            return CHECK_CONDITION;
9bac43
+        }
9bac43
+        transportid_len = ldl_be_p(&param[24]) + PR_OUT_FIXED_PARAM_SIZE + 4;
9bac43
+        if (transportid_len > sz) {
9bac43
+            scsi_build_sense(sense, SENSE_CODE(INVALID_PARAM));
9bac43
+            return CHECK_CONDITION;
9bac43
+        }
9bac43
+        for (i = PR_OUT_FIXED_PARAM_SIZE + 4, j = 0; i < transportid_len; ) {
9bac43
+            struct transportid *id = (struct transportid *) &transportids[j];
9bac43
+            int len;
9bac43
+
9bac43
+            id->format_code = param[i] & 0xc0;
9bac43
+            id->protocol_id = param[i] & 0x0f;
9bac43
+            switch (param[i] & 0xcf) {
9bac43
+            case 0:
9bac43
+                /* FC transport.  */
9bac43
+                if (i + 24 > transportid_len) {
9bac43
+                    goto illegal_req;
9bac43
+                }
9bac43
+                memcpy(id->n_port_name, &param[i + 8], 8);
9bac43
+                j += offsetof(struct transportid, n_port_name[8]);
9bac43
+                i += 24;
9bac43
+                break;
9bac43
+            case 3:
9bac43
+            case 0x43:
9bac43
+                /* iSCSI transport.  */
9bac43
+                len = lduw_be_p(&param[i + 2]);
9bac43
+                if (len > 252 || (len & 3) || i + len + 4 > transportid_len) {
9bac43
+                    /* For format code 00, the standard says the maximum is 223
9bac43
+                     * plus the NUL terminator.  For format code 01 there is no
9bac43
+                     * maximum length, but libmpathpersist ignores the first
9bac43
+                     * byte of id->iscsi_name so our maximum is 252.
9bac43
+                     */
9bac43
+                    goto illegal_req;
9bac43
+                }
9bac43
+                if (memchr(&param[i + 4], 0, len) == NULL) {
9bac43
+                    goto illegal_req;
9bac43
+                }
9bac43
+                memcpy(id->iscsi_name, &param[i + 2], len + 2);
9bac43
+                j += offsetof(struct transportid, iscsi_name[len + 2]);
9bac43
+                i += len + 4;
9bac43
+                break;
9bac43
+            case 6:
9bac43
+                /* SAS transport.  */
9bac43
+                if (i + 24 > transportid_len) {
9bac43
+                    goto illegal_req;
9bac43
+                }
9bac43
+                memcpy(id->sas_address, &param[i + 4], 8);
9bac43
+                j += offsetof(struct transportid, sas_address[8]);
9bac43
+                i += 24;
9bac43
+                break;
9bac43
+            default:
9bac43
+            illegal_req:
9bac43
+                scsi_build_sense(sense, SENSE_CODE(INVALID_PARAM));
9bac43
+                return CHECK_CONDITION;
9bac43
+            }
9bac43
+
9bac43
+            paramp.trnptid_list[paramp.num_transportid++] = id;
9bac43
+        }
9bac43
+    }
9bac43
+
9bac43
+    r = mpath_persistent_reserve_out(fd, rq_servact, rq_scope, rq_type,
9bac43
+                                     &paramp, noisy, verbose);
9bac43
+    return mpath_reconstruct_sense(fd, r, sense);
9bac43
+}
9bac43
+#endif
9bac43
+
9bac43
 static int do_pr_in(int fd, const uint8_t *cdb, uint8_t *sense,
9bac43
                     uint8_t *data, int *resp_sz)
9bac43
 {
9bac43
+#ifdef CONFIG_MPATH
9bac43
+    if (is_mpath(fd)) {
9bac43
+        /* multipath_pr_in fills the whole input buffer.  */
9bac43
+        return multipath_pr_in(fd, cdb, sense, data, *resp_sz);
9bac43
+    }
9bac43
+#endif
9bac43
+
9bac43
     return do_sgio(fd, cdb, sense, data, resp_sz,
9bac43
                    SG_DXFER_FROM_DEV);
9bac43
 }
9bac43
@@ -214,7 +528,14 @@ static int do_pr_in(int fd, const uint8_t *cdb, uint8_t *sense,
9bac43
 static int do_pr_out(int fd, const uint8_t *cdb, uint8_t *sense,
9bac43
                      const uint8_t *param, int sz)
9bac43
 {
9bac43
-    int resp_sz = sz;
9bac43
+    int resp_sz;
9bac43
+#ifdef CONFIG_MPATH
9bac43
+    if (is_mpath(fd)) {
9bac43
+        return multipath_pr_out(fd, cdb, sense, param, sz);
9bac43
+    }
9bac43
+#endif
9bac43
+
9bac43
+    resp_sz = sz;
9bac43
     return do_sgio(fd, cdb, sense, (uint8_t *)param, &resp_sz,
9bac43
                    SG_DXFER_TO_DEV);
9bac43
 }
9bac43
@@ -525,6 +846,14 @@ static int drop_privileges(void)
9bac43
         return -1;
9bac43
     }
9bac43
 
9bac43
+#ifdef CONFIG_MPATH
9bac43
+    /* For /dev/mapper/control ioctls */
9bac43
+    if (capng_update(CAPNG_ADD, CAPNG_EFFECTIVE | CAPNG_PERMITTED,
9bac43
+                     CAP_SYS_ADMIN) < 0) {
9bac43
+        return -1;
9bac43
+    }
9bac43
+#endif
9bac43
+
9bac43
     /* Change user/group id, retaining the capabilities.  Because file descriptors
9bac43
      * are passed via SCM_RIGHTS, we don't need supplementary groups (and in
9bac43
      * fact the helper can run as "nobody").
9bac43
@@ -541,7 +870,7 @@ static int drop_privileges(void)
9bac43
 
9bac43
 int main(int argc, char **argv)
9bac43
 {
9bac43
-    const char *sopt = "hVk:fdT:u:g:q";
9bac43
+    const char *sopt = "hVk:fdT:u:g:vq";
9bac43
     struct option lopt[] = {
9bac43
         { "help", no_argument, NULL, 'h' },
9bac43
         { "version", no_argument, NULL, 'V' },
9bac43
@@ -551,10 +880,12 @@ int main(int argc, char **argv)
9bac43
         { "trace", required_argument, NULL, 'T' },
9bac43
         { "user", required_argument, NULL, 'u' },
9bac43
         { "group", required_argument, NULL, 'g' },
9bac43
+        { "verbose", no_argument, NULL, 'v' },
9bac43
         { "quiet", no_argument, NULL, 'q' },
9bac43
         { NULL, 0, NULL, 0 }
9bac43
     };
9bac43
     int opt_ind = 0;
9bac43
+    int loglevel = 1;
9bac43
     int quiet = 0;
9bac43
     int ch;
9bac43
     Error *local_err = NULL;
9bac43
@@ -631,6 +962,9 @@ int main(int argc, char **argv)
9bac43
         case 'q':
9bac43
             quiet = 1;
9bac43
             break;
9bac43
+        case 'v':
9bac43
+            ++loglevel;
9bac43
+            break;
9bac43
         case 'T':
9bac43
             g_free(trace_file);
9bac43
             trace_file = trace_opt_parse(optarg);
9bac43
@@ -650,7 +984,8 @@ int main(int argc, char **argv)
9bac43
     }
9bac43
 
9bac43
     /* set verbosity */
9bac43
-    verbose = !quiet;
9bac43
+    noisy = !quiet && (loglevel >= 3);
9bac43
+    verbose = quiet ? 0 : MIN(loglevel, 3);
9bac43
 
9bac43
     if (!trace_init_backends()) {
9bac43
         exit(EXIT_FAILURE);
9bac43
@@ -658,6 +993,11 @@ int main(int argc, char **argv)
9bac43
     trace_init_file(trace_file);
9bac43
     qemu_set_log(LOG_TRACE);
9bac43
 
9bac43
+#ifdef CONFIG_MPATH
9bac43
+    dm_init();
9bac43
+    multipath_pr_init();
9bac43
+#endif
9bac43
+
9bac43
     socket_activation = check_socket_activation();
9bac43
     if (socket_activation == 0) {
9bac43
         SocketAddress saddr;
9bac43
diff --git a/scsi/utils.c b/scsi/utils.c
9bac43
index fab60bd..5684951 100644
9bac43
--- a/scsi/utils.c
9bac43
+++ b/scsi/utils.c
9bac43
@@ -206,6 +206,11 @@ const struct SCSISense sense_code_OVERLAPPED_COMMANDS = {
9bac43
     .key = ABORTED_COMMAND, .asc = 0x4e, .ascq = 0x00
9bac43
 };
9bac43
 
9bac43
+/* Command aborted, LUN Communication Failure */
9bac43
+const struct SCSISense sense_code_LUN_COMM_FAILURE = {
9bac43
+    .key = ABORTED_COMMAND, .asc = 0x08, .ascq = 0x00
9bac43
+};
9bac43
+
9bac43
 /* Unit attention, Capacity data has changed */
9bac43
 const struct SCSISense sense_code_CAPACITY_CHANGED = {
9bac43
     .key = UNIT_ATTENTION, .asc = 0x2a, .ascq = 0x09
9bac43
@@ -216,6 +221,11 @@ const struct SCSISense sense_code_RESET = {
9bac43
     .key = UNIT_ATTENTION, .asc = 0x29, .ascq = 0x00
9bac43
 };
9bac43
 
9bac43
+/* Unit attention, SCSI bus reset */
9bac43
+const struct SCSISense sense_code_SCSI_BUS_RESET = {
9bac43
+    .key = UNIT_ATTENTION, .asc = 0x29, .ascq = 0x02
9bac43
+};
9bac43
+
9bac43
 /* Unit attention, No medium */
9bac43
 const struct SCSISense sense_code_UNIT_ATTENTION_NO_MEDIUM = {
9bac43
     .key = UNIT_ATTENTION, .asc = 0x3a, .ascq = 0x00
9bac43
-- 
9bac43
1.8.3.1
9bac43