qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Fam Zheng <famz@redhat.com>
To: Paolo Bonzini <pbonzini@redhat.com>
Cc: qemu-devel@nongnu.org, qemu-block@nongnu.org
Subject: Re: [Qemu-devel] [PATCH 10/10] scsi: add persistent reservation manager using qemu-pr-helper
Date: Wed, 23 Aug 2017 12:49:26 +0800	[thread overview]
Message-ID: <20170823044926.GB21343@lemon> (raw)
In-Reply-To: <20170822131832.20191-11-pbonzini@redhat.com>

On Tue, 08/22 15:18, Paolo Bonzini wrote:
> This adds a concrete subclass of pr-manager that talks to qemu-pr-helper.
> 
> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
> ---
>  scsi/Makefile.objs       |   2 +-
>  scsi/pr-manager-helper.c | 288 +++++++++++++++++++++++++++++++++++++++++++++++
>  2 files changed, 289 insertions(+), 1 deletion(-)
>  create mode 100644 scsi/pr-manager-helper.c
> 
> diff --git a/scsi/Makefile.objs b/scsi/Makefile.objs
> index 5496d2ae6a..4d25e476cf 100644
> --- a/scsi/Makefile.objs
> +++ b/scsi/Makefile.objs
> @@ -1,3 +1,3 @@
>  block-obj-y += utils.o
>  
> -block-obj-$(CONFIG_LINUX) += pr-manager.o
> +block-obj-$(CONFIG_LINUX) += pr-manager.o pr-manager-helper.o
> diff --git a/scsi/pr-manager-helper.c b/scsi/pr-manager-helper.c
> new file mode 100644
> index 0000000000..c9d9606696
> --- /dev/null
> +++ b/scsi/pr-manager-helper.c
> @@ -0,0 +1,288 @@
> +/*
> + * Persistent reservation manager that talks to qemu-mpath-helper

s/qemu-mpath-helper/qemu-pr-helper/

> + *
> + * Copyright (c) 2017 Red Hat, Inc.

Since I'm commenting on a header, just BTW I was educated that (c) stands for
"copyright" so this is actually a ubiquitous redundancy. :)

> + *
> + * Author: Paolo Bonzini <pbonzini@redhat.com>
> + *
> + * This code is licensed under the LGPL.

Interesting, I think the version of the license is required?

> + *
> + */
> +
> +#include "qemu/osdep.h"
> +#include "qapi/error.h"
> +#include "scsi/constants.h"
> +#include "scsi/pr-manager.h"
> +#include "scsi/utils.h"
> +#include "io/channel.h"
> +#include "io/channel-socket.h"
> +#include "pr-helper.h"
> +
> +#include <scsi/sg.h>
> +
> +#define PR_MAX_RECONNECT_ATTEMPTS 5
> +
> +#define TYPE_PR_MANAGER_HELPER "pr-manager-helper"
> +
> +#define PR_MANAGER_HELPER(obj) \
> +     INTERFACE_CHECK(PRManagerHelper, (obj), \
> +                     TYPE_PR_MANAGER_HELPER)
> +
> +typedef struct PRManagerHelper {
> +    /* <private> */
> +    PRManager parent;
> +
> +    char *path;
> +
> +    QemuMutex lock;
> +    QIOChannel *ioc;
> +} PRManagerHelper;
> +
> +/* Called with lock held.  */
> +static int pr_manager_helper_read(PRManagerHelper *pr_mgr,
> +                                  void *buf, int sz, Error **errp)
> +{
> +    ssize_t r = qio_channel_read_all(pr_mgr->ioc, buf, sz, errp);
> +
> +    if (r < 0) {
> +        object_unref(OBJECT(pr_mgr->ioc));
> +        pr_mgr->ioc = NULL;
> +        return r;
> +    }
> +
> +    return r < 0 ? r : 0;
> +}
> +
> +/* Called with lock held.  */
> +static int pr_manager_helper_write(PRManagerHelper *pr_mgr,
> +                                   int fd,
> +                                   const void *buf, int sz, Error **errp)
> +{
> +    size_t nfds = (fd != -1);
> +    while (sz > 0) {
> +        struct iovec iov;
> +        ssize_t n_written;
> +
> +        iov.iov_base = (void *)buf;
> +        iov.iov_len = sz;
> +        n_written = qio_channel_writev_full(QIO_CHANNEL(pr_mgr->ioc), &iov, 1,
> +                                            nfds ? &fd : NULL, nfds, errp);
> +
> +        if (n_written <= 0) {
> +            assert(n_written != QIO_CHANNEL_ERR_BLOCK);
> +            object_unref(OBJECT(pr_mgr->ioc));
> +            pr_mgr->ioc = NULL;
> +            return n_written;
> +        }
> +
> +        nfds = 0;
> +        buf += n_written;
> +        sz -= n_written;
> +    }
> +
> +    return 0;
> +}
> +
> +/* Called with lock held.  */
> +static int pr_manager_helper_initialize(PRManagerHelper *pr_mgr,
> +                                        Error **errp)
> +{
> +    uint32_t flags;
> +
> +    SocketAddress saddr = {
> +        .type = SOCKET_ADDRESS_TYPE_UNIX,
> +        .u.q_unix.path = g_strdup(pr_mgr->path)

Missing g_free()?

> +    };
> +    QIOChannelSocket *sioc = qio_channel_socket_new();
> +    Error *local_err = NULL;
> +
> +    qio_channel_set_name(QIO_CHANNEL(sioc), "pr-manager-helper");
> +    qio_channel_socket_connect_sync(sioc,
> +                                    &saddr,
> +                                    &local_err);
> +    if (local_err) {
> +        object_unref(OBJECT(sioc));
> +        error_propagate(errp, local_err);
> +        return -ENOTCONN;
> +    }
> +
> +    qio_channel_set_delay(QIO_CHANNEL(sioc), false);
> +    pr_mgr->ioc = QIO_CHANNEL(sioc);
> +
> +    /* A simple feature negotation protocol, even though there is
> +     * no optional feature right now.
> +     */
> +    if (pr_manager_helper_read(pr_mgr, &flags, sizeof(flags), errp) < 0) {

Not returning the return value of pr_manager_helper_read()?.

> +        return -EINVAL;
> +    }
> +
> +    flags = 0;
> +    if (pr_manager_helper_write(pr_mgr, -1, &flags, sizeof(flags), errp) < 0) {
> +        return -EINVAL;

Same here.

> +    }
> +
> +    return 0;
> +}
> +
> +static int pr_manager_helper_run(PRManager *p,
> +                                 int fd, struct sg_io_hdr *io_hdr)
> +{
> +    PRManagerHelper *pr_mgr = PR_MANAGER_HELPER(p);
> +
> +    uint32_t len;
> +    PRHelperResponse resp;
> +    int ret;
> +    int expected_dir;
> +    int attempts;
> +    uint8_t cdb[PR_HELPER_CDB_SIZE] = { 0 };
> +
> +    if (!io_hdr->cmd_len || io_hdr->cmd_len > PR_HELPER_CDB_SIZE) {
> +        return -EINVAL;
> +    }
> +
> +    memcpy(cdb, io_hdr->cmdp, io_hdr->cmd_len);
> +    assert(cdb[0] == PERSISTENT_RESERVE_OUT || cdb[0] == PERSISTENT_RESERVE_IN);
> +    expected_dir =
> +        (cdb[0] == PERSISTENT_RESERVE_OUT ? SG_DXFER_TO_DEV : SG_DXFER_FROM_DEV);
> +    if (io_hdr->dxfer_direction != expected_dir) {
> +        return -EINVAL;
> +    }
> +
> +    len = scsi_cdb_xfer(cdb);
> +    if (io_hdr->dxfer_len < len || len > PR_HELPER_DATA_SIZE) {
> +        return -EINVAL;
> +    }
> +
> +    ret = 0;
> +    qemu_mutex_lock(&pr_mgr->lock);
> +
> +    /* Try to reconnect while sending the CDB.  */
> +    for (attempts = 0; attempts < PR_MAX_RECONNECT_ATTEMPTS; attempts++) {
> +        if (!pr_mgr->ioc) {
> +            if (pr_manager_helper_initialize(pr_mgr, NULL) < 0) {
> +                qemu_mutex_unlock(&pr_mgr->lock);
> +                g_usleep(G_USEC_PER_SEC);
> +                qemu_mutex_lock(&pr_mgr->lock);
> +            }
> +        }
> +
> +        if (pr_mgr->ioc) {
> +            if (pr_manager_helper_write(pr_mgr, fd, cdb,
> +                                        ARRAY_SIZE(cdb), NULL) >= 0) {
> +                break;
> +            }
> +        }
> +    }
> +    if (attempts == PR_MAX_RECONNECT_ATTEMPTS) {
> +        ret = -EINVAL;
> +        goto out;
> +    }
> +
> +    /* After sending the CDB, any communications failure causes the
> +     * command to fail.  The failure is transient, retrying the command
> +     * will invoke pr_manager_helper_initialize again.
> +     */
> +    if (expected_dir == SG_DXFER_TO_DEV) {
> +        if (pr_manager_helper_write(pr_mgr, -1, io_hdr->dxferp,
> +                                    len, NULL) < 0) {
> +            ret = -EINVAL;
> +            goto out;
> +        }
> +    }
> +    if (pr_manager_helper_read(pr_mgr, &resp, sizeof(resp), NULL) < 0) {
> +        ret = -EINVAL;
> +        goto out;
> +    }
> +    if (expected_dir == SG_DXFER_FROM_DEV && resp.result == 0) {
> +        if (pr_manager_helper_read(pr_mgr, io_hdr->dxferp, len, NULL) < 0) {
> +            ret = -EINVAL;
> +            goto out;
> +        }
> +    }

Same for these three errors, too.

> +
> +    io_hdr->status = resp.result;
> +    if (resp.result == CHECK_CONDITION) {
> +        io_hdr->driver_status = SG_ERR_DRIVER_SENSE;
> +        io_hdr->sb_len_wr = MIN(io_hdr->mx_sb_len, PR_HELPER_SENSE_SIZE);
> +        memcpy(io_hdr->sbp, resp.sense, io_hdr->sb_len_wr);
> +    }
> +
> +out:
> +    if (ret < 0) {
> +        int sense_len = scsi_build_sense(io_hdr->sbp,
> +                                         SENSE_CODE(LUN_COMM_FAILURE));
> +        io_hdr->driver_status = SG_ERR_DRIVER_SENSE;
> +        io_hdr->sb_len_wr = MIN(io_hdr->mx_sb_len, sense_len);
> +        io_hdr->status = CHECK_CONDITION;
> +    }
> +    qemu_mutex_unlock(&pr_mgr->lock);
> +    return ret;
> +}

Fam

  reply	other threads:[~2017-08-23  4:49 UTC|newest]

Thread overview: 48+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-08-22 13:18 [Qemu-devel] [RFC PATCH 00/10] scsi, block: introduce persistent reservation managers Paolo Bonzini
2017-08-22 13:18 ` [Qemu-devel] [PATCH 01/10] scsi: rename scsi_convert_sense Paolo Bonzini
2017-08-22 13:38   ` Philippe Mathieu-Daudé
2017-08-22 13:18 ` [Qemu-devel] [PATCH 02/10] scsi: move non-emulation specific code to scsi/ Paolo Bonzini
2017-08-22 13:34   ` Philippe Mathieu-Daudé
2017-08-22 13:18 ` [Qemu-devel] [PATCH 03/10] scsi: introduce scsi_build_sense Paolo Bonzini
2017-08-22 13:35   ` Philippe Mathieu-Daudé
2017-08-30 13:39   ` Stefan Hajnoczi
2017-08-22 13:18 ` [Qemu-devel] [PATCH 04/10] scsi: introduce sg_io_sense_from_errno Paolo Bonzini
2017-08-22 13:45   ` Philippe Mathieu-Daudé
2017-08-22 13:53     ` Paolo Bonzini
2017-08-30 13:41   ` Stefan Hajnoczi
2017-08-22 13:18 ` [Qemu-devel] [PATCH 05/10] scsi: move block/scsi.h to include/scsi/constants.h Paolo Bonzini
2017-08-22 13:37   ` Philippe Mathieu-Daudé
2017-08-30 13:41   ` Stefan Hajnoczi
2017-08-22 13:18 ` [Qemu-devel] [PATCH 06/10] scsi, file-posix: add support for persistent reservation management Paolo Bonzini
2017-08-23  4:13   ` Fam Zheng
2017-08-23  6:56     ` Paolo Bonzini
2017-08-24 15:37   ` Eric Blake
2017-08-24 15:47     ` Paolo Bonzini
2017-08-30 12:59   ` Daniel P. Berrange
2017-08-30 14:26   ` [Qemu-devel] [Qemu-block] " Stefan Hajnoczi
2017-08-22 13:18 ` [Qemu-devel] [PATCH 07/10] io: add qio_channel_read/write_all Paolo Bonzini
2017-08-23  5:08   ` Fam Zheng
2017-08-23  6:54     ` Paolo Bonzini
2017-08-30 12:52   ` Daniel P. Berrange
2017-08-30 14:33   ` [Qemu-devel] [Qemu-block] " Stefan Hajnoczi
2017-08-22 13:18 ` [Qemu-devel] [PATCH 08/10] scsi: build qemu-pr-helper Paolo Bonzini
2017-08-22 14:34   ` Marc-André Lureau
2017-08-22 16:04     ` Paolo Bonzini
2017-08-24 15:45   ` Eric Blake
2017-08-30 15:44   ` [Qemu-devel] [Qemu-block] " Stefan Hajnoczi
2017-08-30 16:06   ` Stefan Hajnoczi
2017-08-22 13:18 ` [Qemu-devel] [PATCH 09/10] scsi: add multipath support to qemu-pr-helper Paolo Bonzini
2017-08-23  5:01   ` Fam Zheng
2017-08-23  6:50     ` Paolo Bonzini
2017-08-30 16:06   ` Stefan Hajnoczi
2017-08-30 16:37   ` Stefan Hajnoczi
2017-09-11  9:14     ` [Qemu-devel] [Qemu-block] " Paolo Bonzini
2017-08-22 13:18 ` [Qemu-devel] [PATCH 10/10] scsi: add persistent reservation manager using qemu-pr-helper Paolo Bonzini
2017-08-23  4:49   ` Fam Zheng [this message]
2017-08-23  6:55     ` Paolo Bonzini
2017-08-23  7:48     ` Paolo Bonzini
2017-08-30 16:58   ` [Qemu-devel] [Qemu-block] " Stefan Hajnoczi
2017-08-22 13:48 ` [Qemu-devel] [RFC PATCH 00/10] scsi, block: introduce persistent reservation managers no-reply
2017-08-22 13:50 ` no-reply
2017-08-22 13:50 ` no-reply
2017-08-22 13:51 ` no-reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170823044926.GB21343@lemon \
    --to=famz@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).