Skip to contentSkip to conversation

Support Archipelago as a QEMU block backend

PATCH Superseded Chrysostomos Nanakos 3 participants 8 messages Jun 19, 2014 – Jun 23, 2014 Block layer

v4 of 8 Superseded Latest is v8 →

lore
Share & tools
lore
#0 lore raw

v4 cover Support Archipelago as a QEMU block backend

v4:
- Move Archipelago QMP support from qapi-schema.json file to qapi/block-core.json. Fixe various typographic errors, thanks to Kevin Wolf and Eric Blake.
- Use new .create_opts format, define new QemuOptsList structure and refactor qemu_archipelago_create function.

v3:
- Break down initial patch from one to three. First patch implements Archipelago QEMU block backend with read/write functionality. Second patch implements .bdrv_create() and adds support for creating Archipelago images. Third patch adds QMP support.
- Remove global variable g_xseg_init, make xseg_initialize(), xseg_join() and xseg_leave() reentrant and thread-safe.
- Introduce new enum BlockdevOptionsArchipelago for the QMP support.

v2:
- Implement .bdrv_parse_filename() function to convert the shortuct version with a single string to the individual options.
- Remove global variables and move relevant fields to ArchipelagoAIOCB struct.
- Remove ArchipelagoConf struct and use the relevant fields as individual arguments.
- Remove ArchipelagoCB struct and use ArchipelagoAIOCB instead.
- Remove ArchipelagoThread struct and move relevant fields to ArchipelagoAIOCB instead. Now an I/O thread is spawned for per-device to handle all async I/O requests.
- Remove double data copy, use qemu_iovec_from_buf() and copy data directly to the destination buffer.
- Remove archipelago_aio_bh_cb() function, a full request is completed in qemu_archipelago_complete_aio() instead.
- Resolve proposed changes from Kevin Wolf and miscellaneous style issues.

Chrysostomos Nanakos (3):
block: Support Archipelago as a QEMU block backend block/archipelago: Add support for creating images
QMP: Add support for Archipelago

 MAINTAINERS          |    6 +
 block/Makefile.objs  |    2 +
 block/archipelago.c  | 1182 ++++++++++++++++++++++++++++++++++++++++++++++++++
 configure            |   40 ++
 qapi/block-core.json |   33 +-
 5 files changed, 1260 insertions(+), 3 deletions(-)
 create mode 100644 block/archipelago.c
Signature · 1 line
1.7.10.4
#1 lore raw

PATCH v4 1/3 block: Support Archipelago as a QEMU block backend

VM Image on Archipelago volume is specified like this:

file=archipelago:<volumename>[/mport=<mapperd_port>[:vport=<vlmcd_port>]]

or

file.driver=archipelago,file.volume=<volumename>[,file.mport=<mapperd_port>[, file.vport=<vlmcd_port>]]

'archipelago' is the protocol.

'mport' is the port number on which mapperd is listening. This is optional and if not specified, QEMU will make Archipelago to use the default port.

'vport' is the port number on which vlmcd is listening. This is optional and if not specified, QEMU will make Archipelago to use the default port.

Examples:

file=archipelago:my_vm_volume file=archipelago:my_vm_volume/mport=123 file=archipelago:my_vm_volume/mport=123:vport=1234

or

file.driver=archipelago,file.volume=my_vm_volume file.driver=archipelago,file.volume=my_vm_volume,file.mport=123 file.driver=archipelago,file.volume=my_vm_volume,file.mport=123, file.vport=1234

---
 MAINTAINERS         |    6 +
 block/Makefile.objs |    2 +
 block/archipelago.c | 1046 +++++++++++++++++++++++++++++++++++++++++++++++++++
 configure           |   40 ++
 4 files changed, 1094 insertions(+)
 create mode 100644 block/archipelago.c
MAINTAINERS +6 -0
index d1a3405..db3c16e 100644
@@ -994,3 +994,9 @@ SSH
 M: Richard W.M. Jones <[email protected]>
 S: Supported
 F: block/ssh.c
+
+ARCHIPELAGO
+M: Chrysostomos Nanakos <[email protected]>
+M: Chrysostomos Nanakos <[email protected]>
+S: Maintained
+F: block/archipelago.c
block/Makefile.objs +2 -0
index fd88c03..858d2b3 100644
@@ -17,6 +17,7 @@ block-obj-$(CONFIG_LIBNFS) += nfs.o
 block-obj-$(CONFIG_CURL) += curl.o
 block-obj-$(CONFIG_RBD) += rbd.o
 block-obj-$(CONFIG_GLUSTERFS) += gluster.o
+block-obj-$(CONFIG_ARCHIPELAGO) += archipelago.o
 block-obj-$(CONFIG_LIBSSH2) += ssh.o
 endif
 
@@ -35,5 +36,6 @@ gluster.o-cflags   := $(GLUSTERFS_CFLAGS)
 gluster.o-libs     := $(GLUSTERFS_LIBS)
 ssh.o-cflags       := $(LIBSSH2_CFLAGS)
 ssh.o-libs         := $(LIBSSH2_LIBS)
+archipelago.o-libs := $(ARCHIPELAGO_LIBS)
 qcow.o-libs        := -lz
 linux-aio.o-libs   := -laio
block/archipelago.c new file +1046 -0

1,046 lines not on this page · raw message

configure +40 -0
index 27d84d9..c0bd2b3 100755
@@ -326,6 +326,7 @@ seccomp=""
 glusterfs=""
 glusterfs_discard="no"
 glusterfs_zerofill="no"
+archipelago=""
 virtio_blk_data_plane=""
 gtk=""
 gtkabi=""
@@ -1086,6 +1087,10 @@ for opt do
   ;;
   --enable-glusterfs) glusterfs="yes"
   ;;
+  --disable-archipelago) archipelago="no"
+  ;;
+  --enable-archipelago) archipelago="yes"
+  ;;
   --disable-virtio-blk-data-plane) virtio_blk_data_plane="no"
   ;;
   --enable-virtio-blk-data-plane) virtio_blk_data_plane="yes"
@@ -1375,6 +1380,8 @@ Advanced options (experts only):
   --enable-coroutine-pool  enable coroutine freelist (better performance)
   --enable-glusterfs       enable GlusterFS backend
   --disable-glusterfs      disable GlusterFS backend
+  --enable-archipelago     enable Archipelago backend
+  --disable-archipelago    disable Archipelago backend
   --enable-gcov            enable test coverage analysis with gcov
   --gcov=GCOV              use specified gcov [$gcov_tool]
   --enable-tpm             enable TPM support
@@ -3041,6 +3048,33 @@ EOF
   fi
 fi
 
+
+##########################################
+# archipelago probe
+if test "$archipelago" != "no" ; then
+    cat > $TMPC <<EOF
+#include <stdio.h>
+#include <xseg/xseg.h>
+#include <xseg/protocol.h>
+int main(void) {
+    xseg_initialize();
+    return 0;
+}
+EOF
+    archipelago_libs=-lxseg
+    if compile_prog "" "$archipelago_libs"; then
+        archipelago="yes"
+        libs_tools="$archipelago_libs $libs_tools"
+        libs_softmmu="$archipelago_libs $libs_softmmu"
+    else
+      if test "$archipelago" = "yes" ; then
+        feature_not_found "Archipelago backend support" "Install libxseg devel"
+      fi
+      archipelago="no"
+    fi
+fi
+
+
 ##########################################
 # glusterfs probe
 if test "$glusterfs" != "no" ; then
@@ -4230,6 +4264,7 @@ echo "seccomp support   $seccomp"
 echo "coroutine backend $coroutine"
 echo "coroutine pool    $coroutine_pool"
 echo "GlusterFS support $glusterfs"
+echo "Archipelago support $archipelago"
 echo "virtio-blk-data-plane $virtio_blk_data_plane"
 echo "gcov              $gcov_tool"
 echo "gcov enabled      $gcov"
@@ -4664,6 +4699,11 @@ if test "$glusterfs_zerofill" = "yes" ; then
   echo "CONFIG_GLUSTERFS_ZEROFILL=y" >> $config_host_mak
 fi
 
+if test "$archipelago" = "yes" ; then
+  echo "CONFIG_ARCHIPELAGO=m" >> $config_host_mak
+  echo "ARCHIPELAGO_LIBS=$archipelago_libs" >> $config_host_mak
+fi
+
 if test "$libssh2" = "yes" ; then
   echo "CONFIG_LIBSSH2=m" >> $config_host_mak
   echo "LIBSSH2_CFLAGS=$libssh2_cflags" >> $config_host_mak
Signature · 1 line
1.7.10.4
#2 lore raw

Re: [Qemu-devel] [PATCH v4 1/3] block: Support Archipelago as a QEMU block backend

block/archipelago.c line 80

Quoted 3 lines · Chrysostomos Nanakos
block/archipelago.c new file +3 -0
+typedef struct BDRVArchipelagoState {
+    int fds[2];
+    int qemu_aio_count;

This field is never used. It's increment and decremented but nothing ever checks the value. It can be dropped.

Quoted 40 lines
block/archipelago.c new file +40 -0
+    int event_reader_pos;
+    ArchipelagoAIOCB *event_acb;
+    const char *volname;
+    uint64_t size;
+    /* Archipelago specific */
+    struct xseg *xseg;
+    struct xseg_port *port;
+    xport srcport;
+    xport sport;
+    xport mportno;
+    xport vportno;
+    QemuMutex archip_mutex;
+    QemuCond archip_cond;
+    bool is_signaled;
+    /* Request handler specific */
+    QemuThread request_th;
+    QemuCond request_cond;
+    QemuMutex request_mutex;
+    bool th_is_signaled;
+    bool stopping;
+} BDRVArchipelagoState;
+
+typedef struct ArchipelagoSegmentedRequest {
+    size_t count;
+    size_t total;
+    int ref;
+    int failed;
+} ArchipelagoSegmentedRequest;
+
+typedef struct AIORequestData {
+    const char *volname;
+    off_t offset;
+    size_t size;
+    uint64_t bufidx;
+    int ret;
+    int op;
+    ArchipelagoAIOCB *aio_cb;
+    ArchipelagoSegmentedRequest *segreq;
+} AIORequestData;
+

block/archipelago.c line 130

Quoted 10 lines
block/archipelago.c new file +10 -0
+
+static int qemu_archipelago_signal_pipe(ArchipelagoAIOCB *aio_cb);
+
+static void init_local_signal(struct xseg *xseg, xport sport, xport srcport)
+{
+    if (xseg && (sport != srcport)) {
+        xseg_init_local_signal(xseg, srcport);
+        sport = srcport;
+    }
+}

QEMU should clean up by calling xseg_quit_local_signal().

Quoted 29 lines
block/archipelago.c new file +29 -0
+
+static void archipelago_finish_aiocb(AIORequestData *reqdata)
+{
+    int ret;
+    ret = qemu_archipelago_signal_pipe(reqdata->aio_cb);
+    if (ret < 0) {
+        error_report("archipelago_finish_aiocb(): failed writing"
+                     " aio_cb->s->fds");
+    }
+    g_free(reqdata);
+}
+
+static int wait_reply(struct xseg *xseg, xport srcport, struct xseg_port *port,
+                      struct xseg_request *expected_req)
+{
+    struct xseg_request *req;
+    xseg_prepare_wait(xseg, srcport);
+    void *psd = xseg_get_signal_desc(xseg, port);
+    while (1) {
+        req = xseg_receive(xseg, srcport, 0);
+        if (req) {
+            if (req != expected_req) {
+                archipelagolog("Unknown received request\n");
+                xseg_put_request(xseg, req, srcport);
+            } else if (!(req->state & XS_SERVED)) {
+                archipelagolog("Failed req\n");
+                return -1;
+            } else {
+                break;

block/archipelago.c line 169

Quoted 10 lines
block/archipelago.c new file +10 -0
+            }
+        }
+        xseg_wait_signal(xseg, psd, 100000UL);
+    }
+    xseg_cancel_wait(xseg, srcport);
+    return 0;
+}
+
+static void xseg_request_handler(void *state)
+{

This thread is only necessary because you're not integrating xseg into the QEMU event loop. If you got the pipe fds from xseg and used aio_set_fd_handler() you could eliminate this thread. The advantage is that you can skip the archipelago_finish_aiocb() and get slightly better performance due to one less context switch between threads.

Quoted 24 lines
block/archipelago.c new file +24 -0
+    BDRVArchipelagoState *s = (BDRVArchipelagoState *) state;
+    void *psd = xseg_get_signal_desc(s->xseg, s->port);
+    qemu_mutex_lock(&s->request_mutex);
+
+    while (!s->stopping) {
+        struct xseg_request *req;
+        char *data;
+        xseg_prepare_wait(s->xseg, s->srcport);
+        req = xseg_receive(s->xseg, s->srcport, 0);
+        if (req) {
+            AIORequestData *reqdata;
+            ArchipelagoSegmentedRequest *segreq;
+            xseg_get_req_data(s->xseg, req, (void **)&reqdata);
+
+            if (!(req->state & XS_SERVED)) {
+                    segreq = reqdata->segreq;
+                    __sync_bool_compare_and_swap(&segreq->failed, 0, 1);
+            }
+
+            switch (reqdata->op) {
+            case ARCHIP_OP_READ:
+                    data = xseg_get_data(s->xseg, req);
+                    segreq = reqdata->segreq;
+                    segreq->count += req->serviced;

block/archipelago.c line 203

Quoted 10 lines
block/archipelago.c new file +10 -0
+
+                    qemu_iovec_from_buf(reqdata->aio_cb->qiov, reqdata->bufidx,
+                            data,
+                            req->serviced);
+
+                    xseg_put_request(s->xseg, req, s->srcport);
+
+                    __sync_add_and_fetch(&segreq->ref, -1);
+
+                    if (segreq->ref == 0) {

Not sure about the value of __sync_add_and_fetch() since the if statement fetches segreq->ref again. But I'm not reviewing the details of the shared memory accesses. I'm assuming this stuff is correct, secure, etc.

block/archipelago.c line 207

Quoted 4 lines
block/archipelago.c new file +4 -0
+                        if (!segreq->failed) {
+                            reqdata->aio_cb->ret = segreq->count;
+                            archipelago_finish_aiocb(reqdata);
+                        }

What does segreq->failed mean? We should always finish the I/O request, otherwise the upper layers will run out of resources as we leak failed requests.

Quoted 8 lines
block/archipelago.c new file +8 -0
+static void parse_filename_opts(const char *filename, Error **errp,
+                                char **volume, xport *mport, xport *vport)
+{
+    const char *start;
+    char *tokens[3], *ds;
+    int idx;
+    xport lmport = NoPort, lvport = NoPort;
+

block/archipelago.c line 359

Quoted 10 lines
block/archipelago.c new file +10 -0
+    strstart(filename, "archipelago:", &start);
+
+    ds = g_strdup(start);
+    tokens[0] = strtok(ds, "/");
+    tokens[1] = strtok(NULL, ":");
+    tokens[2] = strtok(NULL, "\0");
+
+    if (!strlen(tokens[0])) {
+        error_setg(errp, "volume name must be specified first");
+        return;

ds is leaked.

Quoted 7 lines
block/archipelago.c new file +7 -0
+    }
+
+    for (idx = 1; idx < 3; idx++) {
+        if (tokens[idx] != NULL) {
+            if (strstart(tokens[idx], "mport=", NULL)) {
+                xseg_find_port(tokens[idx], "mport=", &lmport);
+            }

block/archipelago.c line 376

Quoted 10 lines
block/archipelago.c new file +10 -0
+            if (strstart(tokens[idx], "vport=", NULL)) {
+                xseg_find_port(tokens[idx], "vport=", &lvport);
+            }
+        }
+    }
+
+    if ((lmport == (xport) -2) || (lvport == (xport) -2)) {
+        error_setg(errp, "Usage: file=archipelago:"
+                   "<volumename>[/mport=<mapperd_port>"
+                   "[:vport=<vlmcd_port>]]");

ds is leaked.

Quoted 87 lines
block/archipelago.c new file +87 -0
+        return;
+    }
+    *volume = g_strdup(tokens[0]);
+    *mport = lmport;
+    *vport = lvport;
+    g_free(ds);
+}
+
+static void archipelago_parse_filename(const char *filename, QDict *options,
+                                       Error **errp)
+{
+    const char *start;
+    char *volume = NULL;
+    xport mport = NoPort, vport = NoPort;
+
+    if (qdict_haskey(options, ARCHIPELAGO_OPT_VOLUME)
+            || qdict_haskey(options, ARCHIPELAGO_OPT_MPORT)
+            || qdict_haskey(options, ARCHIPELAGO_OPT_VPORT)) {
+        error_setg(errp, "volume/mport/vport and a file name may not be "
+                         "specified at the same time");
+        return;
+    }
+
+    if (!strstart(filename, "archipelago:", &start)) {
+        error_setg(errp, "File name must start with 'archipelago:'");
+        return;
+    }
+
+    if (!strlen(start) || strstart(start, "/", NULL)) {
+        error_setg(errp, "volume name must be specified");
+        return;
+    }
+
+    parse_filename_opts(filename, errp, &volume, &mport, &vport);
+
+    if (volume) {
+        qdict_put(options, ARCHIPELAGO_OPT_VOLUME, qstring_from_str(volume));
+        g_free(volume);
+    }
+    if (mport != NoPort) {
+        qdict_put(options, ARCHIPELAGO_OPT_MPORT, qint_from_int(mport));
+    }
+    if (vport != NoPort) {
+        qdict_put(options, ARCHIPELAGO_OPT_VPORT, qint_from_int(vport));
+    }
+}
+
+static QemuOptsList archipelago_runtime_opts = {
+    .name = "archipelago",
+    .head = QTAILQ_HEAD_INITIALIZER(archipelago_runtime_opts.head),
+    .desc = {
+        {
+            .name = ARCHIPELAGO_OPT_VOLUME,
+            .type = QEMU_OPT_STRING,
+            .help = "Name of the volume image",
+        },
+        {
+            .name = ARCHIPELAGO_OPT_MPORT,
+            .type = QEMU_OPT_NUMBER,
+            .help = "Archipelago mapperd port number"
+        },
+        {
+            .name = ARCHIPELAGO_OPT_VPORT,
+            .type = QEMU_OPT_NUMBER,
+            .help = "Archipelago vlmcd port number"
+
+        },
+        { /* end of list */ }
+    },
+};
+
+static int qemu_archipelago_open(BlockDriverState *bs,
+                                 QDict *options,
+                                 int bdrv_flags,
+                                 Error **errp)
+{
+    int ret = 0;
+    const char *volume;
+    QemuOpts *opts;
+    Error *local_err = NULL;
+    BDRVArchipelagoState *s = bs->opaque;
+
+    opts = qemu_opts_create(&archipelago_runtime_opts, NULL, 0, &error_abort);
+    qemu_opts_absorb_qdict(opts, options, &local_err);
+    if (local_err) {
+        error_propagate(errp, local_err);
+        qemu_opts_del(opts);

block/archipelago.c line 473

Quoted 10 lines
block/archipelago.c new file +10 -0
+        return -EINVAL;
+    }
+
+    s->mportno = qemu_opt_get_number(opts, ARCHIPELAGO_OPT_MPORT, 1001);
+    s->vportno = qemu_opt_get_number(opts, ARCHIPELAGO_OPT_VPORT, 501);
+
+    volume = qemu_opt_get(opts, ARCHIPELAGO_OPT_VOLUME);
+    if (volume == NULL) {
+        error_setg(errp, "archipelago block driver requires an 'volume'"
+                   " options");

"archipelago block driver requires the 'volume' option"

Quoted 1 line

+ error_propagate(errp, local_err);

This line is unnecessary since the error message was already put into errp.

Quoted 8 lines
block/archipelago.c new file +8 -0
+        qemu_opts_del(opts);
+        return -EINVAL;
+    }
+    s->volname = g_strdup(volume);
+
+    /* Initialize XSEG, join shared memory segment */
+    ret = qemu_archipelago_init(s);
+    if (ret < 0) {

block/archipelago.c line 492

Quoted 10 lines
block/archipelago.c new file +10 -0
+        error_setg(errp, "cannot initialize XSEG and join shared "
+                   "memory segment");
+        goto err_exit;
+    }
+
+    s->event_reader_pos = 0;
+    ret = qemu_pipe(s->fds);
+    if (ret < 0) {
+        error_setg(errp, "cannot create pipe");
+        goto err_exit;

Do we need to xseg_leave() to avoid leaking xseg refcounts, leaving memory mapped, and memory leaks?

Quoted 4 lines
block/archipelago.c new file +4 -0
+    }
+
+    fcntl(s->fds[ARCHIP_FD_READ], F_SETFL, O_NONBLOCK);
+    fcntl(s->fds[ARCHIP_FD_WRITE], F_SETFL, O_NONBLOCK);

block/archipelago.c line 506

Quoted 10 lines
block/archipelago.c new file +10 -0
+    qemu_aio_set_fd_handler(s->fds[ARCHIP_FD_READ],
+                            qemu_archipelago_aio_event_reader, NULL,
+                            s);
+
+    qemu_opts_del(opts);
+    return 0;
+
+err_exit:
+    qemu_opts_del(opts);
+    return ret;

s->volname is leaked

Quoted 13 lines
block/archipelago.c new file +13 -0
+}
+
+static void qemu_archipelago_close(BlockDriverState *bs)
+{
+    int r, targetlen;
+    char *target;
+    struct xseg_request *req;
+    BDRVArchipelagoState *s = bs->opaque;
+
+    qemu_aio_set_fd_handler(s->fds[ARCHIP_FD_READ], NULL, NULL, NULL);
+    close(s->fds[0]);
+    close(s->fds[1]);
+

block/archipelago.c line 529

Quoted 10 lines
block/archipelago.c new file +10 -0
+    s->stopping = true;
+
+    qemu_mutex_lock(&s->request_mutex);
+    while (!s->th_is_signaled) {
+        qemu_cond_wait(&s->request_cond,
+                       &s->request_mutex);
+    }
+    qemu_mutex_unlock(&s->request_mutex);
+    qemu_cond_destroy(&s->request_cond);
+    qemu_mutex_destroy(&s->request_mutex);

It's not safe to qemu_mutex_destroy() because the other thread may still be inside qemu_mutex_unlock(&s->request_mutex) and may still access s->request_mutex memory.

Use qemu_thread_join() before destroying request_cond and request_mutex. That way you can be sure there is no race condition.

(I recently did the same thing and Paolo Bonzini pointed out the bug. After checking the glibc implementation I was convinced that it's not safe.)

Quoted 9 lines
block/archipelago.c new file +9 -0
+
+    qemu_cond_destroy(&s->archip_cond);
+    qemu_mutex_destroy(&s->archip_mutex);
+
+    targetlen = strlen(s->volname);
+    req = xseg_get_request(s->xseg, s->srcport, s->vportno, X_ALLOC);
+    if (!req) {
+        archipelagolog("Cannot get XSEG request\n");
+        goto err_exit;

block/archipelago.c line 548

Quoted 10 lines
block/archipelago.c new file +10 -0
+    }
+    r = xseg_prep_request(s->xseg, req, targetlen, 0);
+    if (r < 0) {
+        xseg_put_request(s->xseg, req, s->srcport);
+        archipelagolog("Cannot prepare XSEG close request\n");
+        goto err_exit;
+    }
+
+    target = xseg_get_target(s->xseg, req);
+    strncpy(target, s->volname, targetlen);

Using strncpy() hints that target is a string when in fact it's not. I think memcpy() would be clearer here since you don't want a '\0' byte at the end of the string.

Or maybe I'm wrong and there is some guarantee that there will be a '\0' byte after target?

Quoted 15 lines
block/archipelago.c new file +15 -0
+    req->size = req->datalen;
+    req->offset = 0;
+    req->op = X_CLOSE;
+
+    xport p = xseg_submit(s->xseg, req, s->srcport, X_ALLOC);
+    if (p == NoPort) {
+        xseg_put_request(s->xseg, req, s->srcport);
+        archipelagolog("Cannot submit XSEG close request\n");
+        goto err_exit;
+    }
+
+    xseg_signal(s->xseg, p);
+    r = wait_reply(s->xseg, s->srcport, s->port, req);
+    if (r < 0) {
+        archipelagolog("wait_reply() error\n");

block/archipelago.c line 573

Quoted 10 lines
block/archipelago.c new file +10 -0
+    }
+    if (!(req->state & XS_SERVED)) {
+        archipelagolog("Could no close map for volume '%s'\n", s->volname);
+    }
+
+    xseg_put_request(s->xseg, req, s->srcport);
+
+err_exit:
+    xseg_leave_dynport(s->xseg, s->port);
+    xseg_leave(s->xseg);

s->volname is leaked.

Quoted 32 lines
block/archipelago.c new file +32 -0
+}
+
+static void qemu_archipelago_aio_cancel(BlockDriverAIOCB *blockacb)
+{
+    ArchipelagoAIOCB *aio_cb = (ArchipelagoAIOCB *) blockacb;
+    aio_cb->cancelled = true;
+    while (aio_cb->status == -EINPROGRESS) {
+        qemu_aio_wait();
+    }
+    qemu_aio_release(aio_cb);
+}
+
+static const AIOCBInfo archipelago_aiocb_info = {
+    .aiocb_size = sizeof(ArchipelagoAIOCB),
+    .cancel = qemu_archipelago_aio_cancel,
+};
+
+static int qemu_archipelago_signal_pipe(ArchipelagoAIOCB *aio_cb)
+{
+    int ret = 0;
+    while (1) {
+        fd_set wfd;
+        int fd = aio_cb->s->fds[1];
+
+        ret = write(fd, (void *)&aio_cb, sizeof(aio_cb));
+        if (ret > 0) {
+            break;
+        }
+        if (errno == EINTR) {
+            continue;
+        }
+        if (errno != EAGAIN) {

block/archipelago.c line 615

Quoted 10 lines
block/archipelago.c new file +10 -0
+            break;
+        }
+        FD_ZERO(&wfd);
+        FD_SET(fd, &wfd);
+        do {
+            ret = select(fd + 1, NULL, &wfd, NULL, NULL);
+        } while (ret < 0 && errno == EINTR);
+    }
+    return ret;
+}

A newer signalling approach is available and will let you drop the pipe code. QEMUBH is a "bottom half" or deferred function call that can be scheduled in an event loop. Scheduling the the QEMUBH is thread-safe so you can perform it from any thread.

See block/gluster.c:gluster_finish_aiocb() for an example using QEMUBH.

block/archipelago.c line 953

Quoted 11 lines
block/archipelago.c new file +11 -0
+static int64_t archipelago_volume_info(BDRVArchipelagoState *s)
+{
+    uint64_t size;
+    int ret, targetlen;
+    struct xseg_request *req;
+    struct xseg_reply_info *xinfo;
+    AIORequestData *reqdata = g_malloc(sizeof(AIORequestData));
+
+    if (!reqdata) {
+        archipelagolog("Cannot allocate reqdata\n");
+        return -1;

g_malloc() never returns NULL, this if statement can be dropped.

#3 lore raw

Re: [Qemu-devel] [PATCH v4 1/3] block: Support Archipelago as a QEMU block backend

Quoted 134 lines · Stefan Hajnoczi

block/archipelago.c line 80

Quoted 3 lines · Chrysostomos Nanakos
block/archipelago.c new file +3 -0
+typedef struct BDRVArchipelagoState {
+    int fds[2];
+    int qemu_aio_count;

This field is never used. It's increment and decremented but nothing ever checks the value. It can be dropped.

Quoted 40 lines
block/archipelago.c new file +40 -0
+    int event_reader_pos;
+    ArchipelagoAIOCB *event_acb;
+    const char *volname;
+    uint64_t size;
+    /* Archipelago specific */
+    struct xseg *xseg;
+    struct xseg_port *port;
+    xport srcport;
+    xport sport;
+    xport mportno;
+    xport vportno;
+    QemuMutex archip_mutex;
+    QemuCond archip_cond;
+    bool is_signaled;
+    /* Request handler specific */
+    QemuThread request_th;
+    QemuCond request_cond;
+    QemuMutex request_mutex;
+    bool th_is_signaled;
+    bool stopping;
+} BDRVArchipelagoState;
+
+typedef struct ArchipelagoSegmentedRequest {
+    size_t count;
+    size_t total;
+    int ref;
+    int failed;
+} ArchipelagoSegmentedRequest;
+
+typedef struct AIORequestData {
+    const char *volname;
+    off_t offset;
+    size_t size;
+    uint64_t bufidx;
+    int ret;
+    int op;
+    ArchipelagoAIOCB *aio_cb;
+    ArchipelagoSegmentedRequest *segreq;
+} AIORequestData;
+

block/archipelago.c line 130

Quoted 10 lines
block/archipelago.c new file +10 -0
+
+static int qemu_archipelago_signal_pipe(ArchipelagoAIOCB *aio_cb);
+
+static void init_local_signal(struct xseg *xseg, xport sport, xport srcport)
+{
+    if (xseg && (sport != srcport)) {
+        xseg_init_local_signal(xseg, srcport);
+        sport = srcport;
+    }
+}

QEMU should clean up by calling xseg_quit_local_signal().

Quoted 29 lines
block/archipelago.c new file +29 -0
+
+static void archipelago_finish_aiocb(AIORequestData *reqdata)
+{
+    int ret;
+    ret = qemu_archipelago_signal_pipe(reqdata->aio_cb);
+    if (ret < 0) {
+        error_report("archipelago_finish_aiocb(): failed writing"
+                     " aio_cb->s->fds");
+    }
+    g_free(reqdata);
+}
+
+static int wait_reply(struct xseg *xseg, xport srcport, struct xseg_port *port,
+                      struct xseg_request *expected_req)
+{
+    struct xseg_request *req;
+    xseg_prepare_wait(xseg, srcport);
+    void *psd = xseg_get_signal_desc(xseg, port);
+    while (1) {
+        req = xseg_receive(xseg, srcport, 0);
+        if (req) {
+            if (req != expected_req) {
+                archipelagolog("Unknown received request\n");
+                xseg_put_request(xseg, req, srcport);
+            } else if (!(req->state & XS_SERVED)) {
+                archipelagolog("Failed req\n");
+                return -1;
+            } else {
+                break;

block/archipelago.c line 169

Quoted 10 lines
block/archipelago.c new file +10 -0
+            }
+        }
+        xseg_wait_signal(xseg, psd, 100000UL);
+    }
+    xseg_cancel_wait(xseg, srcport);
+    return 0;
+}
+
+static void xseg_request_handler(void *state)
+{

This thread is only necessary because you're not integrating xseg into the QEMU event loop. If you got the pipe fds from xseg and used aio_set_fd_handler() you could eliminate this thread. The advantage is that you can skip the archipelago_finish_aiocb() and get slightly better performance due to one less context switch between threads.

Quoted 30 lines
block/archipelago.c new file +30 -0
+    BDRVArchipelagoState *s = (BDRVArchipelagoState *) state;
+    void *psd = xseg_get_signal_desc(s->xseg, s->port);
+    qemu_mutex_lock(&s->request_mutex);
+
+    while (!s->stopping) {
+        struct xseg_request *req;
+        char *data;
+        xseg_prepare_wait(s->xseg, s->srcport);
+        req = xseg_receive(s->xseg, s->srcport, 0);
+        if (req) {
+            AIORequestData *reqdata;
+            ArchipelagoSegmentedRequest *segreq;
+            xseg_get_req_data(s->xseg, req, (void **)&reqdata);
+
+            if (!(req->state & XS_SERVED)) {
+                    segreq = reqdata->segreq;
+                    __sync_bool_compare_and_swap(&segreq->failed, 0, 1);
+            }
+
+            switch (reqdata->op) {
+            case ARCHIP_OP_READ:
+                    data = xseg_get_data(s->xseg, req);
+                    segreq = reqdata->segreq;
+                    segreq->count += req->serviced;
+
+                    qemu_iovec_from_buf(reqdata->aio_cb->qiov, reqdata->bufidx,
+                            data,
+                            req->serviced);
+
+                    xseg_put_request(s->xseg, req, s->srcport);
Quoted 8 lines

block/archipelago.c line 203

Quoted 4 lines
block/archipelago.c new file +4 -0
+
+                    __sync_add_and_fetch(&segreq->ref, -1);
+
+                    if (segreq->ref == 0) {

Not sure about the value of __sync_add_and_fetch() since the if statement fetches segreq->ref again. But I'm not reviewing the details of the shared memory accesses. I'm assuming this stuff is correct, secure, etc.

Yes you are right, IMHO a better and safer approach is:

if ((__sync_add_and_fetch(&segreq->ref, -1)) == 0) {
...

Quoted 8 lines

block/archipelago.c line 207

Quoted 5 lines
block/archipelago.c new file +4 -0
+                        if (!segreq->failed) {
+                            reqdata->aio_cb->ret = segreq->count;
+                            archipelago_finish_aiocb(reqdata);
+                        }

What does segreq->failed mean? We should always finish the I/O request, otherwise the upper layers will run out of resources as we leak failed requests.

Yes you are right.
If a request fails while submitting it to Archipelago archipelago_aio_segmented_rw()
will return -EIO to qemu_archipelago_aio_rw() which will return NULL to .bdrv_aio_readv/_write(). Now if all requests to Archipelago have succeeded in submission and one or all of them haven't been serviced (partial read/write) from Archipelago, archipelago_finish_aiocb() will fail the request. The last one wasn't implemented in this patch, v5 series has the appropriate changes.

Is this a proper and accepted approach along with the removal of the pipe code and the introduction of the QEMU "bottom-half" scheduled in archipelago_finish_aiocb()?

Quoted 155 lines
Quoted 9 lines
block/archipelago.c new file +8 -0
+static void parse_filename_opts(const char *filename, Error **errp,
+                                char **volume, xport *mport, xport *vport)
+{
+    const char *start;
+    char *tokens[3], *ds;
+    int idx;
+    xport lmport = NoPort, lvport = NoPort;
+

block/archipelago.c line 359

Quoted 10 lines
block/archipelago.c new file +10 -0
+    strstart(filename, "archipelago:", &start);
+
+    ds = g_strdup(start);
+    tokens[0] = strtok(ds, "/");
+    tokens[1] = strtok(NULL, ":");
+    tokens[2] = strtok(NULL, "\0");
+
+    if (!strlen(tokens[0])) {
+        error_setg(errp, "volume name must be specified first");
+        return;

ds is leaked.

Quoted 7 lines
block/archipelago.c new file +7 -0
+    }
+
+    for (idx = 1; idx < 3; idx++) {
+        if (tokens[idx] != NULL) {
+            if (strstart(tokens[idx], "mport=", NULL)) {
+                xseg_find_port(tokens[idx], "mport=", &lmport);
+            }

block/archipelago.c line 376

Quoted 10 lines
block/archipelago.c new file +10 -0
+            if (strstart(tokens[idx], "vport=", NULL)) {
+                xseg_find_port(tokens[idx], "vport=", &lvport);
+            }
+        }
+    }
+
+    if ((lmport == (xport) -2) || (lvport == (xport) -2)) {
+        error_setg(errp, "Usage: file=archipelago:"
+                   "<volumename>[/mport=<mapperd_port>"
+                   "[:vport=<vlmcd_port>]]");

ds is leaked.

Quoted 87 lines
block/archipelago.c new file +87 -0
+        return;
+    }
+    *volume = g_strdup(tokens[0]);
+    *mport = lmport;
+    *vport = lvport;
+    g_free(ds);
+}
+
+static void archipelago_parse_filename(const char *filename, QDict *options,
+                                       Error **errp)
+{
+    const char *start;
+    char *volume = NULL;
+    xport mport = NoPort, vport = NoPort;
+
+    if (qdict_haskey(options, ARCHIPELAGO_OPT_VOLUME)
+            || qdict_haskey(options, ARCHIPELAGO_OPT_MPORT)
+            || qdict_haskey(options, ARCHIPELAGO_OPT_VPORT)) {
+        error_setg(errp, "volume/mport/vport and a file name may not be "
+                         "specified at the same time");
+        return;
+    }
+
+    if (!strstart(filename, "archipelago:", &start)) {
+        error_setg(errp, "File name must start with 'archipelago:'");
+        return;
+    }
+
+    if (!strlen(start) || strstart(start, "/", NULL)) {
+        error_setg(errp, "volume name must be specified");
+        return;
+    }
+
+    parse_filename_opts(filename, errp, &volume, &mport, &vport);
+
+    if (volume) {
+        qdict_put(options, ARCHIPELAGO_OPT_VOLUME, qstring_from_str(volume));
+        g_free(volume);
+    }
+    if (mport != NoPort) {
+        qdict_put(options, ARCHIPELAGO_OPT_MPORT, qint_from_int(mport));
+    }
+    if (vport != NoPort) {
+        qdict_put(options, ARCHIPELAGO_OPT_VPORT, qint_from_int(vport));
+    }
+}
+
+static QemuOptsList archipelago_runtime_opts = {
+    .name = "archipelago",
+    .head = QTAILQ_HEAD_INITIALIZER(archipelago_runtime_opts.head),
+    .desc = {
+        {
+            .name = ARCHIPELAGO_OPT_VOLUME,
+            .type = QEMU_OPT_STRING,
+            .help = "Name of the volume image",
+        },
+        {
+            .name = ARCHIPELAGO_OPT_MPORT,
+            .type = QEMU_OPT_NUMBER,
+            .help = "Archipelago mapperd port number"
+        },
+        {
+            .name = ARCHIPELAGO_OPT_VPORT,
+            .type = QEMU_OPT_NUMBER,
+            .help = "Archipelago vlmcd port number"
+
+        },
+        { /* end of list */ }
+    },
+};
+
+static int qemu_archipelago_open(BlockDriverState *bs,
+                                 QDict *options,
+                                 int bdrv_flags,
+                                 Error **errp)
+{
+    int ret = 0;
+    const char *volume;
+    QemuOpts *opts;
+    Error *local_err = NULL;
+    BDRVArchipelagoState *s = bs->opaque;
+
+    opts = qemu_opts_create(&archipelago_runtime_opts, NULL, 0, &error_abort);
+    qemu_opts_absorb_qdict(opts, options, &local_err);
+    if (local_err) {
+        error_propagate(errp, local_err);
+        qemu_opts_del(opts);

block/archipelago.c line 473

Quoted 10 lines
block/archipelago.c new file +10 -0
+        return -EINVAL;
+    }
+
+    s->mportno = qemu_opt_get_number(opts, ARCHIPELAGO_OPT_MPORT, 1001);
+    s->vportno = qemu_opt_get_number(opts, ARCHIPELAGO_OPT_VPORT, 501);
+
+    volume = qemu_opt_get(opts, ARCHIPELAGO_OPT_VOLUME);
+    if (volume == NULL) {
+        error_setg(errp, "archipelago block driver requires an 'volume'"
+                   " options");

"archipelago block driver requires the 'volume' option"

Quoted 1 line

+ error_propagate(errp, local_err);

This line is unnecessary since the error message was already put into errp.

Quoted 12 lines
block/archipelago.c new file +12 -0
+        qemu_opts_del(opts);
+        return -EINVAL;
+    }
+    s->volname = g_strdup(volume);
+
+    /* Initialize XSEG, join shared memory segment */
+    ret = qemu_archipelago_init(s);
+    if (ret < 0) {
+        error_setg(errp, "cannot initialize XSEG and join shared "
+                   "memory segment");
+        goto err_exit;
+    }
Quoted 8 lines

block/archipelago.c line 492

Quoted 6 lines
block/archipelago.c new file +6 -0
+
+    s->event_reader_pos = 0;
+    ret = qemu_pipe(s->fds);
+    if (ret < 0) {
+        error_setg(errp, "cannot create pipe");
+        goto err_exit;

Do we need to xseg_leave() to avoid leaking xseg refcounts, leaving memory mapped, and memory leaks?

Removed qemu_pipe() call so we do not need to call xseg_leave() anymore.

Quoted 44 lines
Quoted 5 lines
block/archipelago.c new file +4 -0
+    }
+
+    fcntl(s->fds[ARCHIP_FD_READ], F_SETFL, O_NONBLOCK);
+    fcntl(s->fds[ARCHIP_FD_WRITE], F_SETFL, O_NONBLOCK);

block/archipelago.c line 506

Quoted 10 lines
block/archipelago.c new file +10 -0
+    qemu_aio_set_fd_handler(s->fds[ARCHIP_FD_READ],
+                            qemu_archipelago_aio_event_reader, NULL,
+                            s);
+
+    qemu_opts_del(opts);
+    return 0;
+
+err_exit:
+    qemu_opts_del(opts);
+    return ret;

s->volname is leaked

Quoted 13 lines
block/archipelago.c new file +13 -0
+}
+
+static void qemu_archipelago_close(BlockDriverState *bs)
+{
+    int r, targetlen;
+    char *target;
+    struct xseg_request *req;
+    BDRVArchipelagoState *s = bs->opaque;
+
+    qemu_aio_set_fd_handler(s->fds[ARCHIP_FD_READ], NULL, NULL, NULL);
+    close(s->fds[0]);
+    close(s->fds[1]);
+

block/archipelago.c line 529

Quoted 10 lines
block/archipelago.c new file +10 -0
+    s->stopping = true;
+
+    qemu_mutex_lock(&s->request_mutex);
+    while (!s->th_is_signaled) {
+        qemu_cond_wait(&s->request_cond,
+                       &s->request_mutex);
+    }
+    qemu_mutex_unlock(&s->request_mutex);
+    qemu_cond_destroy(&s->request_cond);
+    qemu_mutex_destroy(&s->request_mutex);

It's not safe to qemu_mutex_destroy() because the other thread may still be inside qemu_mutex_unlock(&s->request_mutex) and may still access s->request_mutex memory.

Quoted 6 lines

Use qemu_thread_join() before destroying request_cond and request_mutex. That way you can be sure there is no race condition.

(I recently did the same thing and Paolo Bonzini pointed out the bug. After checking the glibc implementation I was convinced that it's not safe.)

Got it! Paolo was absolutely right! Thanks for sharing!

Quoted 23 lines
Quoted 9 lines
block/archipelago.c new file +9 -0
+
+    qemu_cond_destroy(&s->archip_cond);
+    qemu_mutex_destroy(&s->archip_mutex);
+
+    targetlen = strlen(s->volname);
+    req = xseg_get_request(s->xseg, s->srcport, s->vportno, X_ALLOC);
+    if (!req) {
+        archipelagolog("Cannot get XSEG request\n");
+        goto err_exit;

block/archipelago.c line 548

Quoted 10 lines
block/archipelago.c new file +10 -0
+    }
+    r = xseg_prep_request(s->xseg, req, targetlen, 0);
+    if (r < 0) {
+        xseg_put_request(s->xseg, req, s->srcport);
+        archipelagolog("Cannot prepare XSEG close request\n");
+        goto err_exit;
+    }
+
+    target = xseg_get_target(s->xseg, req);
+    strncpy(target, s->volname, targetlen);

Using strncpy() hints that target is a string when in fact it's not. I think memcpy() would be clearer here since you don't want a '\0' byte at the end of the string.

Quoted 2 lines

Or maybe I'm wrong and there is some guarantee that there will be a '\0' byte after target?

No you are not wrong, memcpy() is clearer, fixed for v5 series.

Quoted 89 lines
Quoted 15 lines
block/archipelago.c new file +15 -0
+    req->size = req->datalen;
+    req->offset = 0;
+    req->op = X_CLOSE;
+
+    xport p = xseg_submit(s->xseg, req, s->srcport, X_ALLOC);
+    if (p == NoPort) {
+        xseg_put_request(s->xseg, req, s->srcport);
+        archipelagolog("Cannot submit XSEG close request\n");
+        goto err_exit;
+    }
+
+    xseg_signal(s->xseg, p);
+    r = wait_reply(s->xseg, s->srcport, s->port, req);
+    if (r < 0) {
+        archipelagolog("wait_reply() error\n");

block/archipelago.c line 573

Quoted 10 lines
block/archipelago.c new file +10 -0
+    }
+    if (!(req->state & XS_SERVED)) {
+        archipelagolog("Could no close map for volume '%s'\n", s->volname);
+    }
+
+    xseg_put_request(s->xseg, req, s->srcport);
+
+err_exit:
+    xseg_leave_dynport(s->xseg, s->port);
+    xseg_leave(s->xseg);

s->volname is leaked.

Quoted 32 lines
block/archipelago.c new file +32 -0
+}
+
+static void qemu_archipelago_aio_cancel(BlockDriverAIOCB *blockacb)
+{
+    ArchipelagoAIOCB *aio_cb = (ArchipelagoAIOCB *) blockacb;
+    aio_cb->cancelled = true;
+    while (aio_cb->status == -EINPROGRESS) {
+        qemu_aio_wait();
+    }
+    qemu_aio_release(aio_cb);
+}
+
+static const AIOCBInfo archipelago_aiocb_info = {
+    .aiocb_size = sizeof(ArchipelagoAIOCB),
+    .cancel = qemu_archipelago_aio_cancel,
+};
+
+static int qemu_archipelago_signal_pipe(ArchipelagoAIOCB *aio_cb)
+{
+    int ret = 0;
+    while (1) {
+        fd_set wfd;
+        int fd = aio_cb->s->fds[1];
+
+        ret = write(fd, (void *)&aio_cb, sizeof(aio_cb));
+        if (ret > 0) {
+            break;
+        }
+        if (errno == EINTR) {
+            continue;
+        }
+        if (errno != EAGAIN) {

block/archipelago.c line 615

Quoted 10 lines
block/archipelago.c new file +10 -0
+            break;
+        }
+        FD_ZERO(&wfd);
+        FD_SET(fd, &wfd);
+        do {
+            ret = select(fd + 1, NULL, &wfd, NULL, NULL);
+        } while (ret < 0 && errno == EINTR);
+    }
+    return ret;
+}

A newer signalling approach is available and will let you drop the pipe code. QEMUBH is a "bottom half" or deferred function call that can be scheduled in an event loop. Scheduling the the QEMUBH is thread-safe so you can perform it from any thread.

See block/gluster.c:gluster_finish_aiocb() for an example using QEMUBH.

block/archipelago.c line 953

Quoted 11 lines
block/archipelago.c new file +11 -0
+static int64_t archipelago_volume_info(BDRVArchipelagoState *s)
+{
+    uint64_t size;
+    int ret, targetlen;
+    struct xseg_request *req;
+    struct xseg_reply_info *xinfo;
+    AIORequestData *reqdata = g_malloc(sizeof(AIORequestData));
+
+    if (!reqdata) {
+        archipelagolog("Cannot allocate reqdata\n");
+        return -1;

g_malloc() never returns NULL, this if statement can be dropped.

#4 lore raw

Re: [Qemu-devel] [PATCH v4 1/3] block: Support Archipelago as a QEMU block backend

Quoted 21 lines · Chrysostomos Nanakos
Quoted 9 lines · Stefan Hajnoczi

block/archipelago.c line 207

Quoted 4 lines · Chrysostomos Nanakos
block/archipelago.c new file +4 -0
+                        if (!segreq->failed) {
+                            reqdata->aio_cb->ret = segreq->count;
+                            archipelago_finish_aiocb(reqdata);
+                        }

What does segreq->failed mean? We should always finish the I/O request, otherwise the upper layers will run out of resources as we leak failed requests.

Yes you are right.
If a request fails while submitting it to Archipelago archipelago_aio_segmented_rw()
will return -EIO to qemu_archipelago_aio_rw() which will return NULL to .bdrv_aio_readv/_write(). Now if all requests to Archipelago have succeeded in submission and one or all of them haven't been serviced (partial read/write) from Archipelago, archipelago_finish_aiocb() will fail the request. The last one wasn't implemented in this patch, v5 series has the appropriate changes.

Quoted 3 lines

Is this a proper and accepted approach along with the removal of the pipe code and the introduction of the QEMU "bottom-half" scheduled in archipelago_finish_aiocb()?

Sounds good.

Please also take a look at
http://wiki.qemu.org/Documentation/QemuIoTests

This is the test suite we use for QEMU block drivers. It includes many basic I/O tests and also some specialized tests for advanced features like snapshotting.

For an example of how to add support, take a look at the commit which added NFS support:
http://git.qemu.org/?p=qemu.git;a=commitdiff;h=170632dbc9f75217861dd8bf2e6da3c269a1ba18

Please do something similar so you can run the test suite like this:

cd tests/qemu-iotests
./check -archipelago

Stefan

#5 lore raw

PATCH v4 2/3 block/archipelago: Add support for creating images

In reply to Chrysostomos Nanakos

qemu-img archipelago:<volumename>[/mport=<mapperd_port>[:vport=<vlmcd_port>]] [size]

---
 block/archipelago.c |  137 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 137 insertions(+)
block/archipelago.c +137 -0
index 3da7a1c..4553b2d 100644
@@ -32,6 +32,7 @@
  * or implied, of GRNET S.A.
  */
 
+#include "qemu-common.h"
 #include "block/block_int.h"
 #include "qemu/error-report.h"
 #include "qemu/thread.h"
@@ -573,6 +574,127 @@ err_exit:
     xseg_leave(s->xseg);
 }
 
+static int qemu_archipelago_create_volume(Error **errp, const char *volname,
+                                          uint64_t size, xport mportno,
+                                          xport vportno)
+{
+    int ret, targetlen;
+    struct xseg *xseg = NULL;
+    struct xseg_request *req;
+    struct xseg_request_clone *xclone;
+    struct xseg_port *port;
+    xport srcport = NoPort, sport = NoPort;
+    char *target;
+
+    /* Try default values if none has been set */
+    if (mportno == (xport) -1) {
+        mportno = 1001;
+    }
+
+    if (vportno == (xport) -1) {
+        vportno = 501;
+    }
+
+    if (xseg_initialize()) {
+        error_setg(errp, "Cannot initialize XSEG");
+        return -1;
+    }
+
+    xseg = xseg_join((char *)"posix", (char *)"archipelago",
+                     (char *)"posixfd", NULL);
+
+    if (!xseg) {
+        error_setg(errp, "Cannot join XSEG shared memory segment");
+        return -1;
+    }
+
+    port = xseg_bind_dynport(xseg);
+    srcport = port->portno;
+    init_local_signal(xseg, sport, srcport);
+
+    req = xseg_get_request(xseg, srcport, mportno, X_ALLOC);
+    if (!req) {
+        error_setg(errp, "Cannot get XSEG request");
+        return -1;
+    }
+
+    targetlen = strlen(volname);
+    ret = xseg_prep_request(xseg, req, targetlen,
+                            sizeof(struct xseg_request_clone));
+    if (ret < 0) {
+        error_setg(errp, "Cannot prepare XSEG request");
+        goto err_exit;
+    }
+
+    target = xseg_get_target(xseg, req);
+    if (!target) {
+        error_setg(errp, "Cannot get XSEG target.\n");
+        goto err_exit;
+    }
+    strncpy(target, volname, targetlen);
+    xclone = (struct xseg_request_clone *) xseg_get_data(xseg, req);
+    memset(xclone->target, 0 , XSEG_MAX_TARGETLEN);
+    xclone->targetlen = 0;
+    xclone->size = size * BDRV_SECTOR_SIZE;
+    req->offset = 0;
+    req->size = req->datalen;
+    req->op = X_CLONE;
+
+    xport p = xseg_submit(xseg, req, srcport, X_ALLOC);
+    if (p == NoPort) {
+        error_setg(errp, "Could not submit XSEG request");
+        goto err_exit;
+    }
+    xseg_signal(xseg, p);
+
+    ret = wait_reply(xseg, srcport, port, req);
+    if (ret < 0) {
+        error_setg(errp, "wait_reply() error.");
+    }
+
+    xseg_put_request(xseg, req, srcport);
+    xseg_leave_dynport(xseg, port);
+    xseg_leave(xseg);
+    return ret;
+
+err_exit:
+    xseg_put_request(xseg, req, srcport);
+    xseg_leave_dynport(xseg, port);
+    xseg_leave(xseg);
+    return -1;
+}
+
+static int qemu_archipelago_create(const char *filename,
+                                   QemuOpts *options,
+                                   Error **errp)
+{
+    int ret = 0;
+    uint64_t total_size = 0;
+    char *volname = NULL;
+    const char *start;
+    xport mport = NoPort, vport = NoPort;
+
+    if (!strstart(filename, "archipelago:", &start)) {
+        error_setg(errp, "File name must start with 'archipelago:'");
+        return -1;
+    }
+
+    if (!strlen(start) || strstart(start, "/", NULL)) {
+        error_setg(errp, "volume name must be specified");
+        return -1;
+    }
+
+    parse_filename_opts(filename, errp, &volname, &mport, &vport);
+    total_size = qemu_opt_get_size_del(options, BLOCK_OPT_SIZE, 0) / \
+                 BDRV_SECTOR_SIZE;
+
+    /* Create an Archipelago volume */
+    ret = qemu_archipelago_create_volume(errp, volname, total_size, mport,
+                                         vport);
+    g_free(volname);
+    return ret;
+}
+
 static void qemu_archipelago_aio_cancel(BlockDriverAIOCB *blockacb)
 {
     ArchipelagoAIOCB *aio_cb = (ArchipelagoAIOCB *) blockacb;
@@ -1017,6 +1139,19 @@ static int64_t qemu_archipelago_getlength(BlockDriverState *bs)
     return ret;
 }
 
+static QemuOptsList qemu_archipelago_create_opts = {
+    .name = "archipelago-create-opts",
+    .head = QTAILQ_HEAD_INITIALIZER(qemu_archipelago_create_opts.head),
+    .desc = {
+        {
+            .name = BLOCK_OPT_SIZE,
+            .type = QEMU_OPT_SIZE,
+            .help = "Virtual disk size"
+        },
+        { /* end of list */ }
+    }
+};
+
 static BlockDriverAIOCB *qemu_archipelago_aio_flush(BlockDriverState *bs,
         BlockDriverCompletionFunc *cb, void *opaque)
 {
@@ -1031,11 +1166,13 @@ static BlockDriver bdrv_archipelago = {
     .bdrv_parse_filename = archipelago_parse_filename,
     .bdrv_file_open      = qemu_archipelago_open,
     .bdrv_close          = qemu_archipelago_close,
+    .bdrv_create         = qemu_archipelago_create,
     .bdrv_getlength      = qemu_archipelago_getlength,
     .bdrv_aio_readv      = qemu_archipelago_aio_readv,
     .bdrv_aio_writev     = qemu_archipelago_aio_writev,
     .bdrv_aio_flush      = qemu_archipelago_aio_flush,
     .bdrv_has_zero_init  = bdrv_has_zero_init_1,
+    .create_opts         = &qemu_archipelago_create_opts,
 };
 
 static void bdrv_archipelago_init(void)
Signature · 1 line
1.7.10.4
#6 lore raw

Re: [Qemu-devel] [PATCH v4 2/3] block/archipelago: Add support for creating images

Quoted 32 lines · Chrysostomos Nanakos
block/archipelago.c +32 -0
+static int qemu_archipelago_create_volume(Error **errp, const char *volname,
+                                          uint64_t size, xport mportno,
+                                          xport vportno)
+{
+    int ret, targetlen;
+    struct xseg *xseg = NULL;
+    struct xseg_request *req;
+    struct xseg_request_clone *xclone;
+    struct xseg_port *port;
+    xport srcport = NoPort, sport = NoPort;
+    char *target;
+
+    /* Try default values if none has been set */
+    if (mportno == (xport) -1) {
+        mportno = 1001;
+    }
+
+    if (vportno == (xport) -1) {
+        vportno = 501;
+    }
+
+    if (xseg_initialize()) {
+        error_setg(errp, "Cannot initialize XSEG");
+        return -1;
+    }
+
+    xseg = xseg_join((char *)"posix", (char *)"archipelago",
+                     (char *)"posixfd", NULL);
+
+    if (!xseg) {
+        error_setg(errp, "Cannot join XSEG shared memory segment");
+        return -1;

block/archipelago.c err_exit: · line 618

Quoted 10 lines
block/archipelago.c +10 -0
+    }
+
+    port = xseg_bind_dynport(xseg);
+    srcport = port->portno;
+    init_local_signal(xseg, sport, srcport);
+
+    req = xseg_get_request(xseg, srcport, mportno, X_ALLOC);
+    if (!req) {
+        error_setg(errp, "Cannot get XSEG request");
+        return -1;

xseg_leave() is missing.

#7 lore raw

PATCH v4 3/3 QMP: Add support for Archipelago

In reply to Chrysostomos Nanakos

Introduce new enum BlockdevOptionsArchipelago.

@volume: #Name of the Archipelago volume image

@mport: #'mport' is the port number on which mapperd is
listening. This is optional and if not specified, QEMU will make Archipelago to use the default port.

@vport: #'vport' is the port number on which vlmcd is
listening. This is optional and if not specified, QEMU will make Archipelago to use the default port.

---
 qapi/block-core.json |   33 ++++++++++++++++++++++++++++++---
 1 file changed, 30 insertions(+), 3 deletions(-)
qapi/block-core.json +30 -3
index 7215e48..a745de0 100644
@@ -190,8 +190,8 @@
 # @ro: true if the backing device was open read-only
 #
 # @drv: the name of the block format used to open the backing device. As of
-#       0.14.0 this can be: 'blkdebug', 'bochs', 'cloop', 'cow', 'dmg',
-#       'file', 'file', 'ftp', 'ftps', 'host_cdrom', 'host_device',
+#       0.14.0 this can be: 'archipelago', 'blkdebug', 'bochs', 'cloop', 'cow',
+#       'dmg', 'file', 'file', 'ftp', 'ftps', 'host_cdrom', 'host_device',
 #       'host_floppy', 'http', 'https', 'nbd', 'parallels', 'qcow',
 #       'qcow2', 'raw', 'tftp', 'vdi', 'vmdk', 'vpc', 'vvfat'
 #
@@ -1077,7 +1077,7 @@
 # Since: 2.0
 ##
 { 'enum': 'BlockdevDriver',
-  'data': [ 'file', 'host_device', 'host_cdrom', 'host_floppy',
+  'data': [ 'archipelago', 'file', 'host_device', 'host_cdrom', 'host_floppy',
             'http', 'https', 'ftp', 'ftps', 'tftp', 'vvfat', 'blkdebug',
             'blkverify', 'bochs', 'cloop', 'cow', 'dmg', 'parallels', 'qcow',
             'qcow2', 'qed', 'raw', 'vdi', 'vhdx', 'vmdk', 'vpc', 'quorum' ] }
@@ -1207,6 +1207,32 @@
             '*pass-discard-snapshot': 'bool',
             '*pass-discard-other': 'bool' } }
 
+
+##
+# @BlockdevOptionsArchipelago
+#
+# Driver specific block device options for Archipelago.
+#
+# @volume:              Name of the Archipelago volume image
+#
+#
+# @mport:               #optional The port number on which mapperd is
+#                       listening. This is optional
+#                       and if not specified, QEMU will make Archipelago
+#                       use the default port.
+#
+# @vport:               #optional The port number on which vlmcd is
+#                       listening. This is optional
+#                       and if not specified, QEMU will make Archipelago
+#                       use the default port.
+# Since: 2.1
+##
+{ 'type': 'BlockdevOptionsArchipelago',
+  'data': { 'volume': 'str',
+            '*mport': 'int',
+            '*vport': 'int' } }
+
+
 ##
 # @BlkdebugEvent
 #
@@ -1347,6 +1373,7 @@
   'base': 'BlockdevOptionsBase',
   'discriminator': 'driver',
   'data': {
+      'archipelago':'BlockdevOptionsArchipelago',
       'file':       'BlockdevOptionsFile',
       'host_device':'BlockdevOptionsFile',
       'host_cdrom': 'BlockdevOptionsFile',
Signature · 1 line
1.7.10.4