Skip to contentSkip to conversation

libqblock-API design

RFC PATCH In review Wenchao Xia 5 participants 9 messages Aug 9, 2012 – Aug 15, 2012

lore
Share & tools
lore
#0 lore raw

RFC PATCH v2 2/3 libqblock-API design

This patch is the API design.

---
 libqblock.c |  670 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 libqblock.h |  447 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 1117 insertions(+), 0 deletions(-)
 create mode 100644 libqblock.c
 create mode 100644 libqblock.h
libqblock.c new file +670 -0
new file mode 100644
index 0000000..52e46dc
@@ -0,0 +1,670 @@
+/*
+ * Copyright IBM Corp. 2012
+ *
+ * Authors:
+ *  Wenchao Xia <[email protected]>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
+ */
+
+#include "libqblock.h"
+
+#include <unistd.h>
+#include "block.h"
+#include "block_int.h"
+
+#define FUNC_FREE free
+#define FUNC_MALLOC malloc
+#define FUNC_CALLOC calloc
+
+#define CLEAN_FREE(p) { \
+        FUNC_FREE(p); \
+        (p) = NULL; \
+}
+
+/* details should be hidden to user */
+struct QBlockState {
+    BlockDriverState *bdrvs;
+    char *filename;
+    const char *err_msg;
+    int err_no;
+};
+
+static void set_err_nomem(struct QBlockState *qbs)
+{
+    qbs->err_msg = NULL;
+    qbs->err_no = 0;
+}
+
+void libqblock_init(void)
+{
+    bdrv_init();
+    return;
+}
+
+int qb_state_new(struct QBlockState **qbs)
+{
+    *qbs = FUNC_CALLOC(1, sizeof(struct QBlockState));
+    if (*qbs == NULL) {
+        return QB_ERR_MEM_ERR;
+    }
+    (*qbs)->bdrvs = bdrv_new("hda");
+    if ((*qbs)->bdrvs == NULL) {
+        CLEAN_FREE(*qbs);
+        return QB_ERR_INTERNAL_ERR;
+    }
+    return 0;
+}
+
+void qb_state_free(struct QBlockState **qbs)
+{
+    CLEAN_FREE((*qbs)->filename);
+    if ((*qbs)->bdrvs == NULL) {
+        bdrv_delete((*qbs)->bdrvs);
+        (*qbs)->bdrvs = NULL;
+    }
+    CLEAN_FREE(*qbs);
+    return;
+}
+
+static const char *fmt2str(enum QBlockFormat fmt)
+{
+    const char *ret = NULL;
+    switch (fmt) {
+    case QB_FMT_COW:
+        ret = "cow";
+        break;
+    case QB_FMT_QED:
+        ret = "qed";
+        break;
+    case QB_FMT_QCOW:
+        ret = "qcow";
+        break;
+    case QB_FMT_QCOW2:
+        ret = "qcow2";
+        break;
+    case QB_FMT_RAW:
+        ret = "raw";
+        break;
+    case QB_FMT_RBD:
+        ret = "rbd";
+        break;
+    case QB_FMT_SHEEPDOG:
+        ret = "sheepdog";
+        break;
+    case QB_FMT_VDI:
+        ret = "vdi";
+        break;
+    case QB_FMT_VMDK:
+        ret = "vmdk";
+        break;
+    case QB_FMT_VPC:
+        ret = "vpc";
+        break;
+    default:
+        break;
+    }
+    return ret;
+}
+
+static enum QBlockFormat str2fmt(const char *fmt)
+{
+    enum QBlockFormat ret = QB_FMT_NONE;
+    if (0 == strcmp(fmt, "cow")) {
+        ret = QB_FMT_COW;
+    } else if (0 == strcmp(fmt, "qed")) {
+        ret = QB_FMT_QED;
+    } else if (0 == strcmp(fmt, "qcow")) {
+        ret = QB_FMT_QCOW;
+    } else if (0 == strcmp(fmt, "qcow2")) {
+        ret = QB_FMT_QCOW2;
+    } else if (0 == strcmp(fmt, "raw")) {
+        ret = QB_FMT_RAW;
+    } else if (0 == strcmp(fmt, "rbd")) {
+        ret = QB_FMT_RBD;
+    } else if (0 == strcmp(fmt, "sheepdog")) {
+        ret = QB_FMT_SHEEPDOG;
+    } else if (0 == strcmp(fmt, "vdi")) {
+        ret = QB_FMT_VDI;
+    } else if (0 == strcmp(fmt, "vmdk")) {
+        ret = QB_FMT_VMDK;
+    } else if (0 == strcmp(fmt, "vpc")) {
+        ret = QB_FMT_VPC;
+    }
+    return ret;
+}
+
+/* copy information and take care the member difference in differect version.
+   Assuming all new member are added in the tail, struct size is the first
+   member, this is old to new version, src have its struct_size set. */
+static void qboo_adjust_o2n(struct QBlockOptionOpen *dest,
+                            struct QBlockOptionOpen *src)
+{
+    /* for simple it does memcpy now, need to take care of embbed structure */
+    memcpy(dest, src, src->struct_size);
+}
+
+int qb_open(struct QBlockState *qbs, struct QBlockOptionOpen *op)
+{
+    int ret = 0, bd_ret;
+    BlockDriverState *bs;
+    struct QBlockOptionOpen qboo;
+    BlockDriver *bd;
+    const char *fmt;
+
+    /* take care of user settings */
+    qboo_adjust_o2n(&qboo, op);
+
+    /* check parameters */
+    if (qboo.o_loc.filename == NULL) {
+        ret = QB_ERR_INVALID_PARAM;
+        qbs->err_msg = "filename was not set.";
+        qbs->err_no = 0;
+        goto out;
+    }
+
+    if (path_has_protocol(qboo.o_loc.filename) > 0) {
+        ret = QB_ERR_INVALID_PARAM;
+        qbs->err_msg = "filename have protocol.";
+        qbs->err_no = 0;
+        goto out;
+    }
+
+    if (qboo.o_loc.protocol >= QB_PROTO_MAX) {
+        ret = QB_ERR_INVALID_PARAM;
+        qbs->err_msg = "protocol was not supported.";
+        qbs->err_no = 0;
+        goto out;
+    }
+
+    bd = NULL;
+    fmt = fmt2str(qboo.o_fmt_type);
+    if (fmt != NULL) {
+        bd = bdrv_find_format(fmt);
+        assert(bd != NULL);
+    }
+
+
+    /* do real openning */
+    bs = qbs->bdrvs;
+    bd_ret = bdrv_open(bs, qboo.o_loc.filename, qboo.o_flag, bd);
+    if (bd_ret < 0) {
+        ret = QB_ERR_INTERNAL_ERR;
+        qbs->err_msg = "failed to open the file.";
+        qbs->err_no = -errno;
+        goto out;
+    }
+
+    if (qbs->filename != NULL) {
+        FUNC_FREE(qbs->filename);
+    }
+    qbs->filename = strdup(qboo.o_loc.filename);
+    if (qbs->filename == NULL) {
+        ret = QB_ERR_INVALID_PARAM;
+        set_err_nomem(qbs);
+        goto out;
+    }
+
+ out:
+    return ret;
+}
+
+void qb_close(struct QBlockState *qbs)
+{
+    BlockDriverState *bs;
+
+    bs = qbs->bdrvs;
+    bdrv_close(bs);
+    return;
+}
+
+/* copy information and take care the member difference in differect version.
+   Assuming all new member are added in the tail, struct size is the first
+   member, this is old to new version, src have its struct_size set. */
+static void qboc_adjust_o2n(struct QBlockOptionCreate *dest,
+                            struct QBlockOptionCreate *src)
+{
+    /* for simple it does memcpy now, need to take care of embbed structure */
+    memcpy(dest, src, src->struct_size);
+}
+
+int qb_create(struct QBlockState *qbs, struct QBlockOptionCreate *op)
+{
+    int ret = 0, bd_ret;
+    BlockDriverState *bs = NULL;
+    BlockDriver *drv = NULL, *backing_drv = NULL;
+    struct QBlockOptionCreate qbco;
+    const char *fmt = NULL, *fmt_backing_str = NULL, *tmp;
+    QEMUOptionParameter *param = NULL, *create_options = NULL;
+    QEMUOptionParameter *backing_fmt, *backing_file, *size;
+    struct QBlockOption_cow *o_cow = NULL;
+    struct QBlockOption_qed *o_qed = NULL;
+    struct QBlockOption_qcow *o_qcow = NULL;
+    struct QBlockOption_qcow2 *o_qcow2 = NULL;
+    struct QBlockOption_raw *o_raw = NULL;
+    struct QBlockOption_rbd *o_rbd = NULL;
+    struct QBlockOption_sheepdog *o_sd = NULL;
+    struct QBlockOption_vdi *o_vdi = NULL;
+    struct QBlockOption_vmdk *o_vmdk = NULL;
+    struct QBlockOption_vpc *o_vpc = NULL;
+    int backing_flag = 0;
+
+    /* take care of user settings */
+    qboc_adjust_o2n(&qbco, op);
+
+    /* check parameters */
+    if (qbco.o_loc.filename == NULL) {
+        ret = QB_ERR_INVALID_PARAM;
+        qbs->err_msg = "filename was not set.";
+        qbs->err_no = 0;
+        goto out;
+    }
+
+    if (path_has_protocol(qbco.o_loc.filename) > 0) {
+        ret = QB_ERR_INVALID_PARAM;
+        qbs->err_msg = "filename have protocol.";
+        qbs->err_no = 0;
+        goto out;
+    }
+
+    if (qbco.o_loc.protocol >= QB_PROTO_MAX) {
+        ret = QB_ERR_INVALID_PARAM;
+        qbs->err_msg = "protocol was not supported.";
+        qbs->err_no = 0;
+        goto out;
+    }
+
+    /* set parameters */
+    fmt = fmt2str(qbco.o_fmt.fmt_type);
+    if (fmt == NULL) {
+        ret = QB_ERR_INVALID_PARAM;
+        qbs->err_msg = "format is not valid.";
+        qbs->err_no = 0;
+        goto out;
+    }
+    drv = bdrv_find_format(fmt);
+    assert(drv != NULL);
+
+    create_options = append_option_parameters(create_options,
+                                              drv->create_options);
+    param = parse_option_parameters("", create_options, param);
+
+    switch (qbco.o_fmt.fmt_type) {
+    case QB_FMT_COW:
+        o_cow = &qbco.o_fmt.fmt_op.o_cow;
+        assert(0 == set_option_parameter_int(param,
+                                BLOCK_OPT_SIZE, o_cow->virt_size));
+        if (o_cow->backing_file) {
+            assert(0 == set_option_parameter(param,
+                                BLOCK_OPT_BACKING_FILE, o_cow->backing_file));
+        }
+        backing_flag = o_cow->backing_flag;
+        break;
+    case QB_FMT_QED:
+        o_qed = &qbco.o_fmt.fmt_op.o_qed;
+        assert(0 == set_option_parameter_int(param,
+                                BLOCK_OPT_SIZE, o_qed->virt_size));
+        if (o_qed->backing_file) {
+            assert(0 == set_option_parameter(param,
+                                BLOCK_OPT_BACKING_FILE, o_qed->backing_file));
+        }
+        fmt_backing_str = fmt2str(o_qed->backing_fmt);
+        if (fmt_backing_str) {
+            assert(0 == set_option_parameter(param,
+                                BLOCK_OPT_BACKING_FMT, fmt_backing_str));
+        }
+        assert(0 == set_option_parameter_int(param,
+                                BLOCK_OPT_CLUSTER_SIZE, o_qed->cluster_size));
+        assert(0 == set_option_parameter_int(param,
+                                BLOCK_OPT_TABLE_SIZE, o_qed->table_size));
+        backing_flag = o_qed->backing_flag;
+        break;
+    case QB_FMT_QCOW:
+        o_qcow = &qbco.o_fmt.fmt_op.o_qcow;
+        assert(0 == set_option_parameter_int(param,
+                                BLOCK_OPT_SIZE, o_qcow->virt_size));
+        if (o_qcow->backing_file) {
+            assert(0 == set_option_parameter(param,
+                                BLOCK_OPT_BACKING_FILE, o_qcow->backing_file));
+        }
+        tmp = o_qcow->encrypt ? "on" : "off";
+        assert(0 == set_option_parameter(param, BLOCK_OPT_ENCRYPT, tmp));
+        backing_flag = o_qcow->backing_flag;
+        break;
+    case QB_FMT_QCOW2:
+        o_qcow2 = &qbco.o_fmt.fmt_op.o_qcow2;
+        assert(0 == set_option_parameter_int(param,
+                              BLOCK_OPT_SIZE, o_qcow2->virt_size));
+        if (o_qcow2->backing_file) {
+            assert(0 == set_option_parameter(param,
+                              BLOCK_OPT_BACKING_FILE, o_qcow2->backing_file));
+        }
+        fmt_backing_str = fmt2str(o_qcow2->backing_fmt);
+        if (fmt_backing_str) {
+            assert(0 == set_option_parameter(param,
+                              BLOCK_OPT_BACKING_FMT, fmt_backing_str));
+        }
+        if (o_qcow2->compat_level) {
+            assert(0 == set_option_parameter(param,
+                              BLOCK_OPT_COMPAT_LEVEL, o_qcow2->compat_level));
+        }
+        tmp = o_qcow2->encrypt ? "on" : "off";
+        assert(0 == set_option_parameter(param, BLOCK_OPT_ENCRYPT, tmp));
+        assert(0 == set_option_parameter_int(param,
+                              BLOCK_OPT_CLUSTER_SIZE, o_qcow2->cluster_size));
+        if (o_qcow2->prealloc_mode) {
+            assert(0 == set_option_parameter(param,
+                              BLOCK_OPT_PREALLOC, o_qcow2->prealloc_mode));
+        }
+        backing_flag = o_qcow2->backing_flag;
+        break;
+    case QB_FMT_RAW:
+        o_raw = &qbco.o_fmt.fmt_op.o_raw;
+        assert(0 == set_option_parameter_int(param,
+                              BLOCK_OPT_SIZE, o_raw->virt_size));
+        break;
+    case QB_FMT_RBD:
+        o_rbd = &qbco.o_fmt.fmt_op.o_rbd;
+        assert(0 == set_option_parameter_int(param,
+                              BLOCK_OPT_SIZE, o_rbd->virt_size));
+        assert(0 == set_option_parameter_int(param,
+                              BLOCK_OPT_CLUSTER_SIZE, o_rbd->cluster_size));
+        break;
+    case QB_FMT_SHEEPDOG:
+        o_sd = &qbco.o_fmt.fmt_op.o_sheepdog;
+        assert(0 == set_option_parameter_int(param,
+                              BLOCK_OPT_SIZE, o_sd->virt_size));
+        if (o_sd->backing_file) {
+            assert(0 == set_option_parameter(param,
+                              BLOCK_OPT_BACKING_FILE, o_sd->backing_file));
+        }
+        if (o_sd->prealloc_mode) {
+            assert(0 == set_option_parameter(param,
+                              BLOCK_OPT_PREALLOC, o_sd->prealloc_mode));
+        }
+        backing_flag = o_sd->backing_flag;
+        break;
+    case QB_FMT_VDI:
+        o_vdi = &qbco.o_fmt.fmt_op.o_vdi;
+        assert(0 == set_option_parameter_int(param,
+                              BLOCK_OPT_SIZE, o_vdi->virt_size));
+        /* following option is not always valid depends on configuration */
+        set_option_parameter_int(param,
+                              BLOCK_OPT_CLUSTER_SIZE, o_vdi->cluster_size);
+        set_option_parameter_int(param, "static", o_vdi->prealloc_mode);
+        break;
+    case QB_FMT_VMDK:
+        o_vmdk = &qbco.o_fmt.fmt_op.o_vmdk;
+        assert(0 == set_option_parameter_int(param,
+                              BLOCK_OPT_SIZE, o_vmdk->virt_size));
+        if (o_vmdk->backing_file) {
+            assert(0 == set_option_parameter(param,
+                              BLOCK_OPT_BACKING_FILE, o_vmdk->backing_file));
+        }
+        tmp = o_vmdk->compat_version6 ? "on" : "off";
+        assert(0 == set_option_parameter(param, BLOCK_OPT_COMPAT6, tmp));
+        if (o_vmdk->subfmt) {
+            assert(0 == set_option_parameter(param,
+                              BLOCK_OPT_SUBFMT, o_vmdk->subfmt));
+        }
+        backing_flag = o_vmdk->backing_flag;
+        break;
+    case QB_FMT_VPC:
+        o_vpc = &qbco.o_fmt.fmt_op.o_vpc;
+        assert(0 == set_option_parameter_int(param,
+                               BLOCK_OPT_SIZE, o_vpc->virt_size));
+        if (o_vpc->subfmt) {
+            assert(0 == set_option_parameter(param,
+                               BLOCK_OPT_SUBFMT, o_vpc->subfmt));
+        }
+        break;
+    default:
+        break;
+    }
+
+    backing_file = get_option_parameter(param, BLOCK_OPT_BACKING_FILE);
+    if (backing_file && backing_file->value.s) {
+        if (!strcmp(op->o_loc.filename, backing_file->value.s)) {
+            ret = QB_ERR_INVALID_PARAM;
+            qbs->err_msg = "backing file is the same with new file.";
+            qbs->err_no = 0;
+            goto out;
+        }
+    }
+
+    backing_fmt = get_option_parameter(param, BLOCK_OPT_BACKING_FMT);
+    if (backing_fmt && backing_fmt->value.s) {
+        backing_drv = bdrv_find_format(backing_fmt->value.s);
+        assert(backing_drv != NULL);
+    }
+
+    size = get_option_parameter(param, BLOCK_OPT_SIZE);
+    if (size && size->value.n <= 0) {
+        if (backing_file && backing_file->value.s) {
+            uint64_t size;
+            char buf[32];
+            int back_flags;
+
+            /* backing files always opened read-only */
+            back_flags =
+                backing_flag &
+                ~(BDRV_O_RDWR | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING);
+
+            bs = bdrv_new("");
+
+            ret = bdrv_open(bs, backing_file->value.s,
+                                back_flags, backing_drv);
+            if (ret < 0) {
+                ret = QB_ERR_INTERNAL_ERR;
+                qbs->err_msg = "failed to open the backing file.";
+                qbs->err_no = -errno;
+                goto out;
+            }
+            bdrv_get_geometry(bs, &size);
+            size *= 512;
+
+            snprintf(buf, sizeof(buf), "%" PRId64, size);
+            set_option_parameter(param, BLOCK_OPT_SIZE, buf);
+        } else {
+            ret = QB_ERR_INVALID_PARAM;
+            qbs->err_msg = "neither size or backing file was not set.";
+            qbs->err_no = 0;
+            goto out;
+        }
+    }
+
+    bd_ret = bdrv_create(drv, op->o_loc.filename, param);
+
+    if (bd_ret < 0) {
+        ret = QB_ERR_INTERNAL_ERR;
+        qbs->err_no = bd_ret;
+        if (bd_ret == -ENOTSUP) {
+            qbs->err_msg = "formatting option not supported.";
+        } else if (bd_ret == -EFBIG) {
+            qbs->err_msg = "The image size is too large.";
+        } else {
+            qbs->err_msg = "Error in creating the image.";
+        }
+        goto out;
+    }
+
+out:
+    free_option_parameters(create_options);
+    free_option_parameters(param);
+
+    if (bs) {
+        bdrv_delete(bs);
+    }
+
+    return ret;
+}
+
+/* ignore case that len is not alligned to 512 now. */
+int qb_read(struct QBlockState *qbs, const void *buf, size_t len,
+                                                      off_t offset)
+{
+    int ret = 0, bd_ret;
+    BlockDriverState *bs;
+
+    bs = qbs->bdrvs;
+
+    assert((len & 0x01ff) == 0);
+    assert((offset & 0x01ff) == 0);
+    bd_ret = bdrv_read(bs, offset >> 9, (uint8_t *)buf, len >> 9);
+    if (bd_ret < 0) {
+        ret = QB_ERR_INTERNAL_ERR;
+        qbs->err_no = bd_ret;
+        if (bd_ret == -EIO) {
+            qbs->err_msg = "Generic I/O error.";
+        } else if (bd_ret == -ENOMEDIUM) {
+            qbs->err_msg = "No meida inserted.";
+        } else if (bd_ret == -EINVAL) {
+            qbs->err_msg = "Sector was not correct.";
+        } else {
+            qbs->err_msg = "Internal error.";
+        }
+    }
+    return ret;
+}
+
+/* ignore case that len is not alligned to 512 now. */
+int qb_write(struct QBlockState *qbs, const void *buf, size_t len,
+                                                       off_t offset)
+{
+    int ret = 0, bd_ret;
+    BlockDriverState *bs;
+
+    bs = qbs->bdrvs;
+
+    assert((len & 0x01ff) == 0);
+    assert((offset & 0x01ff) == 0);
+    bd_ret = bdrv_write(bs, offset >> 9, buf, len >> 9);
+    if (bd_ret < 0) {
+        ret = QB_ERR_INTERNAL_ERR;
+        qbs->err_no = bd_ret;
+        if (bd_ret == -EIO) {
+            qbs->err_msg = "Generic I/O error.";
+        } else if (bd_ret == -ENOMEDIUM) {
+            qbs->err_msg = "No meida inserted.";
+        } else if (bd_ret == -EINVAL) {
+            qbs->err_msg = "Sector was not correct.";
+        } else if (bd_ret == -EACCES) {
+            qbs->err_msg = "Ready only device.";
+        } else {
+            qbs->err_msg = "Internal error.";
+        }
+    }
+    return ret;
+}
+
+int qb_flush(struct QBlockState *qbs)
+{
+    int ret = 0, bd_ret;
+    BlockDriverState *bs;
+
+    bs = qbs->bdrvs;
+    bd_ret = bdrv_flush(bs);
+    if (bd_ret < 0) {
+        ret = QB_ERR_INTERNAL_ERR;
+        qbs->err_no = bd_ret;
+        qbs->err_msg = NULL;
+    }
+    return ret;
+}
+
+int qb_infoimage_get(struct QBlockState *qbs, struct QBlockInfoImage **info)
+{
+    int ret = 0;
+    BlockDriverState *bs;
+    const char *tmp;
+    uint64_t total_sectors;
+    char backing_filename[1024];
+
+    struct QBlockInfoImage *ifimg = FUNC_CALLOC(1,
+                                           sizeof(struct QBlockInfoImage));
+    if (ifimg == NULL) {
+        ret = QB_ERR_MEM_ERR;
+        set_err_nomem(qbs);
+        goto out;
+    }
+
+    bs = qbs->bdrvs;
+    ifimg->filename = strdup(qbs->filename);
+    if (ifimg->filename == NULL) {
+        ret = QB_ERR_MEM_ERR;
+        set_err_nomem(qbs);
+        goto err;
+    }
+    tmp = bdrv_get_format_name(bs);
+    ifimg->format = str2fmt(tmp);
+    /* support only file now */
+    ifimg->protocol = QB_PROTO_FILE;
+
+    bdrv_get_geometry(bs, &total_sectors);
+    ifimg->virt_size = total_sectors * 512;
+    ifimg->allocated_size = bdrv_get_allocated_file_size(bs);
+    ifimg->encrypt = bdrv_is_encrypted(bs);
+    bdrv_get_full_backing_filename(bs, backing_filename,
+                                   sizeof(backing_filename));
+    if (backing_filename[0] != '\0') {
+        ifimg->backing_filename = strdup(backing_filename);
+        if (ifimg->backing_filename == NULL) {
+            ret = QB_ERR_MEM_ERR;
+            set_err_nomem(qbs);
+            goto err;
+        }
+    }
+
+    *info = ifimg;
+ out:
+    return ret;
+ err:
+    qb_infoimage_free(&ifimg);
+    return ret;
+}
+
+void qb_infoimage_free(struct QBlockInfoImage **info)
+{
+    if (*info == NULL) {
+        return;
+    }
+    FUNC_FREE((*info)->filename);
+    FUNC_FREE((*info)->backing_filename);
+    FUNC_FREE(*info);
+    *info = NULL;
+    return;
+}
+
+bool qb_supports_format(enum QBlockFormat fmt)
+{
+    if (fmt < QB_FMT_MAX) {
+        return true;
+    }
+    return false;
+}
+
+bool qb_supports_protocol(enum QBlockProtocol proto)
+{
+    if (proto < QB_PROTO_MAX) {
+        return true;
+    }
+    return false;
+}
+
+const char *qb_error_get_detail(struct QBlockState *qbs, int *err_num)
+{
+    *err_num = qbs->err_no;
+    return qbs->err_msg;
+}
libqblock.h new file +447 -0

447 lines not on this page · raw message

Signature · 1 line
1.7.1
#1 lore raw
Quoted 25 lines · Wenchao Xia

This patch is the API design.

---
 libqblock.c |  670 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 libqblock.h |  447 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 1117 insertions(+), 0 deletions(-)
 create mode 100644 libqblock.c
 create mode 100644 libqblock.h
libqblock.c new file +9 -0
new file mode 100644
index 0000000..52e46dc
@@ -0,0 +1,670 @@
+/*
+ * Copyright IBM Corp. 2012
+ *
+ * Authors:
+ *  Wenchao Xia <[email protected]>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either

libqblock.c line 19

Quoted 10 lines
libqblock.c new file +10 -0
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA

Current address of FSF is:

51 Franklin Street, Fifth Floor
Boston, MA 02110-1301
USA

However, please use the recommended web version like other files.

Quoted 6 lines
libqblock.c new file +6 -0
+ */
+
+#include "libqblock.h"
+
+#include <unistd.h>
+#include "block.h"

libqblock.c line 35

Quoted 10 lines
libqblock.c new file +10 -0
+#include "block_int.h"
+
+#define FUNC_FREE free
+#define FUNC_MALLOC malloc
+#define FUNC_CALLOC calloc
+
+#define CLEAN_FREE(p) { \
+        FUNC_FREE(p); \
+        (p) = NULL; \
+}

I don't think this is enough since block functions may use glib memory allocation. Probably the block files have to be compiled with wrappers for g_malloc and friends.

Quoted 9 lines
libqblock.c new file +9 -0
+
+/* details should be hidden to user */
+struct QBlockState {
+    BlockDriverState *bdrvs;
+    char *filename;
+    const char *err_msg;
+    int err_no;
+};
+

libqblock.c line 54

Quoted 10 lines
libqblock.c new file +10 -0
+static void set_err_nomem(struct QBlockState *qbs)
+{
+    qbs->err_msg = NULL;
+    qbs->err_no = 0;
+}
+
+void libqblock_init(void)
+{
+    bdrv_init();
+    return;

Useless return.

Quoted 15 lines
libqblock.c new file +15 -0
+}
+
+int qb_state_new(struct QBlockState **qbs)
+{
+    *qbs = FUNC_CALLOC(1, sizeof(struct QBlockState));
+    if (*qbs == NULL) {
+        return QB_ERR_MEM_ERR;
+    }
+    (*qbs)->bdrvs = bdrv_new("hda");
+    if ((*qbs)->bdrvs == NULL) {
+        CLEAN_FREE(*qbs);
+        return QB_ERR_INTERNAL_ERR;
+    }
+    return 0;
+}

libqblock.c line 79

Quoted 10 lines
libqblock.c new file +10 -0
+
+void qb_state_free(struct QBlockState **qbs)
+{
+    CLEAN_FREE((*qbs)->filename);
+    if ((*qbs)->bdrvs == NULL) {
+        bdrv_delete((*qbs)->bdrvs);
+        (*qbs)->bdrvs = NULL;
+    }
+    CLEAN_FREE(*qbs);
+    return;

Remove.

Quoted 36 lines
libqblock.c new file +36 -0
+}
+
+static const char *fmt2str(enum QBlockFormat fmt)
+{
+    const char *ret = NULL;
+    switch (fmt) {
+    case QB_FMT_COW:
+        ret = "cow";
+        break;
+    case QB_FMT_QED:
+        ret = "qed";
+        break;
+    case QB_FMT_QCOW:
+        ret = "qcow";
+        break;
+    case QB_FMT_QCOW2:
+        ret = "qcow2";
+        break;
+    case QB_FMT_RAW:
+        ret = "raw";
+        break;
+    case QB_FMT_RBD:
+        ret = "rbd";
+        break;
+    case QB_FMT_SHEEPDOG:
+        ret = "sheepdog";
+        break;
+    case QB_FMT_VDI:
+        ret = "vdi";
+        break;
+    case QB_FMT_VMDK:
+        ret = "vmdk";
+        break;
+    case QB_FMT_VPC:
+        ret = "vpc";
+        break;

libqblock.c line 125

Quoted 10 lines
libqblock.c new file +10 -0
+    default:
+        break;
+    }
+    return ret;
+}
+
+static enum QBlockFormat str2fmt(const char *fmt)
+{
+    enum QBlockFormat ret = QB_FMT_NONE;
+    if (0 == strcmp(fmt, "cow")) {

Please use natural order:
strcmp(fmt, "cow") == 0

Quoted 21 lines
libqblock.c new file +21 -0
+        ret = QB_FMT_COW;
+    } else if (0 == strcmp(fmt, "qed")) {
+        ret = QB_FMT_QED;
+    } else if (0 == strcmp(fmt, "qcow")) {
+        ret = QB_FMT_QCOW;
+    } else if (0 == strcmp(fmt, "qcow2")) {
+        ret = QB_FMT_QCOW2;
+    } else if (0 == strcmp(fmt, "raw")) {
+        ret = QB_FMT_RAW;
+    } else if (0 == strcmp(fmt, "rbd")) {
+        ret = QB_FMT_RBD;
+    } else if (0 == strcmp(fmt, "sheepdog")) {
+        ret = QB_FMT_SHEEPDOG;
+    } else if (0 == strcmp(fmt, "vdi")) {
+        ret = QB_FMT_VDI;
+    } else if (0 == strcmp(fmt, "vmdk")) {
+        ret = QB_FMT_VMDK;
+    } else if (0 == strcmp(fmt, "vpc")) {
+        ret = QB_FMT_VPC;
+    }
+    return ret;

libqblock.c line 156

Quoted 10 lines
libqblock.c new file +10 -0
+}
+
+/* copy information and take care the member difference in differect version.
+   Assuming all new member are added in the tail, struct size is the first
+   member, this is old to new version, src have its struct_size set. */
+static void qboo_adjust_o2n(struct QBlockOptionOpen *dest,
+                            struct QBlockOptionOpen *src)
+{
+    /* for simple it does memcpy now, need to take care of embbed structure */
+    memcpy(dest, src, src->struct_size);

Since later there will be a field by field copy, I'd use:
*dest = *src;

Quoted 14 lines
libqblock.c new file +14 -0
+}
+
+int qb_open(struct QBlockState *qbs, struct QBlockOptionOpen *op)
+{
+    int ret = 0, bd_ret;
+    BlockDriverState *bs;
+    struct QBlockOptionOpen qboo;
+    BlockDriver *bd;
+    const char *fmt;
+
+    /* take care of user settings */
+    qboo_adjust_o2n(&qboo, op);
+
+    /* check parameters */

libqblock.c line 180

Quoted 10 lines
libqblock.c new file +10 -0
+    if (qboo.o_loc.filename == NULL) {
+        ret = QB_ERR_INVALID_PARAM;
+        qbs->err_msg = "filename was not set.";
+        qbs->err_no = 0;
+        goto out;
+    }
+
+    if (path_has_protocol(qboo.o_loc.filename) > 0) {
+        ret = QB_ERR_INVALID_PARAM;
+        qbs->err_msg = "filename have protocol.";

had

Quoted 7 lines
libqblock.c new file +7 -0
+        qbs->err_no = 0;
+        goto out;
+    }
+
+    if (qboo.o_loc.protocol >= QB_PROTO_MAX) {
+        ret = QB_ERR_INVALID_PARAM;
+        qbs->err_msg = "protocol was not supported.";

libqblock.c line 197

Quoted 10 lines
libqblock.c new file +10 -0
+        qbs->err_no = 0;
+        goto out;
+    }
+
+    bd = NULL;
+    fmt = fmt2str(qboo.o_fmt_type);
+    if (fmt != NULL) {
+        bd = bdrv_find_format(fmt);
+        assert(bd != NULL);
+    }

What should happen in the NULL fmt case, goto out?

Quoted 23 lines
libqblock.c new file +23 -0
+
+
+    /* do real openning */
+    bs = qbs->bdrvs;
+    bd_ret = bdrv_open(bs, qboo.o_loc.filename, qboo.o_flag, bd);
+    if (bd_ret < 0) {
+        ret = QB_ERR_INTERNAL_ERR;
+        qbs->err_msg = "failed to open the file.";
+        qbs->err_no = -errno;
+        goto out;
+    }
+
+    if (qbs->filename != NULL) {
+        FUNC_FREE(qbs->filename);
+    }
+    qbs->filename = strdup(qboo.o_loc.filename);
+    if (qbs->filename == NULL) {
+        ret = QB_ERR_INVALID_PARAM;
+        set_err_nomem(qbs);
+        goto out;
+    }
+
+ out:

libqblock.c line 230

Quoted 10 lines
libqblock.c new file +10 -0
+    return ret;
+}
+
+void qb_close(struct QBlockState *qbs)
+{
+    BlockDriverState *bs;
+
+    bs = qbs->bdrvs;
+    bdrv_close(bs);
+    return;

Delete return

Quoted 68 lines
libqblock.c new file +68 -0
+}
+
+/* copy information and take care the member difference in differect version.
+   Assuming all new member are added in the tail, struct size is the first
+   member, this is old to new version, src have its struct_size set. */
+static void qboc_adjust_o2n(struct QBlockOptionCreate *dest,
+                            struct QBlockOptionCreate *src)
+{
+    /* for simple it does memcpy now, need to take care of embbed structure */
+    memcpy(dest, src, src->struct_size);
+}
+
+int qb_create(struct QBlockState *qbs, struct QBlockOptionCreate *op)
+{
+    int ret = 0, bd_ret;
+    BlockDriverState *bs = NULL;
+    BlockDriver *drv = NULL, *backing_drv = NULL;
+    struct QBlockOptionCreate qbco;
+    const char *fmt = NULL, *fmt_backing_str = NULL, *tmp;
+    QEMUOptionParameter *param = NULL, *create_options = NULL;
+    QEMUOptionParameter *backing_fmt, *backing_file, *size;
+    struct QBlockOption_cow *o_cow = NULL;
+    struct QBlockOption_qed *o_qed = NULL;
+    struct QBlockOption_qcow *o_qcow = NULL;
+    struct QBlockOption_qcow2 *o_qcow2 = NULL;
+    struct QBlockOption_raw *o_raw = NULL;
+    struct QBlockOption_rbd *o_rbd = NULL;
+    struct QBlockOption_sheepdog *o_sd = NULL;
+    struct QBlockOption_vdi *o_vdi = NULL;
+    struct QBlockOption_vmdk *o_vmdk = NULL;
+    struct QBlockOption_vpc *o_vpc = NULL;
+    int backing_flag = 0;
+
+    /* take care of user settings */
+    qboc_adjust_o2n(&qbco, op);
+
+    /* check parameters */
+    if (qbco.o_loc.filename == NULL) {
+        ret = QB_ERR_INVALID_PARAM;
+        qbs->err_msg = "filename was not set.";
+        qbs->err_no = 0;
+        goto out;
+    }
+
+    if (path_has_protocol(qbco.o_loc.filename) > 0) {
+        ret = QB_ERR_INVALID_PARAM;
+        qbs->err_msg = "filename have protocol.";
+        qbs->err_no = 0;
+        goto out;
+    }
+
+    if (qbco.o_loc.protocol >= QB_PROTO_MAX) {
+        ret = QB_ERR_INVALID_PARAM;
+        qbs->err_msg = "protocol was not supported.";
+        qbs->err_no = 0;
+        goto out;
+    }
+
+    /* set parameters */
+    fmt = fmt2str(qbco.o_fmt.fmt_type);
+    if (fmt == NULL) {
+        ret = QB_ERR_INVALID_PARAM;
+        qbs->err_msg = "format is not valid.";
+        qbs->err_no = 0;
+        goto out;
+    }
+    drv = bdrv_find_format(fmt);
+    assert(drv != NULL);

libqblock.c line 308

Quoted 10 lines
libqblock.c new file +10 -0
+
+    create_options = append_option_parameters(create_options,
+                                              drv->create_options);
+    param = parse_option_parameters("", create_options, param);
+
+    switch (qbco.o_fmt.fmt_type) {
+    case QB_FMT_COW:
+        o_cow = &qbco.o_fmt.fmt_op.o_cow;
+        assert(0 == set_option_parameter_int(param,
+                                BLOCK_OPT_SIZE, o_cow->virt_size));

expr == 0, also below

Quoted 123 lines
libqblock.c new file +123 -0
+        if (o_cow->backing_file) {
+            assert(0 == set_option_parameter(param,
+                                BLOCK_OPT_BACKING_FILE, o_cow->backing_file));
+        }
+        backing_flag = o_cow->backing_flag;
+        break;
+    case QB_FMT_QED:
+        o_qed = &qbco.o_fmt.fmt_op.o_qed;
+        assert(0 == set_option_parameter_int(param,
+                                BLOCK_OPT_SIZE, o_qed->virt_size));
+        if (o_qed->backing_file) {
+            assert(0 == set_option_parameter(param,
+                                BLOCK_OPT_BACKING_FILE, o_qed->backing_file));
+        }
+        fmt_backing_str = fmt2str(o_qed->backing_fmt);
+        if (fmt_backing_str) {
+            assert(0 == set_option_parameter(param,
+                                BLOCK_OPT_BACKING_FMT, fmt_backing_str));
+        }
+        assert(0 == set_option_parameter_int(param,
+                                BLOCK_OPT_CLUSTER_SIZE, o_qed->cluster_size));
+        assert(0 == set_option_parameter_int(param,
+                                BLOCK_OPT_TABLE_SIZE, o_qed->table_size));
+        backing_flag = o_qed->backing_flag;
+        break;
+    case QB_FMT_QCOW:
+        o_qcow = &qbco.o_fmt.fmt_op.o_qcow;
+        assert(0 == set_option_parameter_int(param,
+                                BLOCK_OPT_SIZE, o_qcow->virt_size));
+        if (o_qcow->backing_file) {
+            assert(0 == set_option_parameter(param,
+                                BLOCK_OPT_BACKING_FILE, o_qcow->backing_file));
+        }
+        tmp = o_qcow->encrypt ? "on" : "off";
+        assert(0 == set_option_parameter(param, BLOCK_OPT_ENCRYPT, tmp));
+        backing_flag = o_qcow->backing_flag;
+        break;
+    case QB_FMT_QCOW2:
+        o_qcow2 = &qbco.o_fmt.fmt_op.o_qcow2;
+        assert(0 == set_option_parameter_int(param,
+                              BLOCK_OPT_SIZE, o_qcow2->virt_size));
+        if (o_qcow2->backing_file) {
+            assert(0 == set_option_parameter(param,
+                              BLOCK_OPT_BACKING_FILE, o_qcow2->backing_file));
+        }
+        fmt_backing_str = fmt2str(o_qcow2->backing_fmt);
+        if (fmt_backing_str) {
+            assert(0 == set_option_parameter(param,
+                              BLOCK_OPT_BACKING_FMT, fmt_backing_str));
+        }
+        if (o_qcow2->compat_level) {
+            assert(0 == set_option_parameter(param,
+                              BLOCK_OPT_COMPAT_LEVEL, o_qcow2->compat_level));
+        }
+        tmp = o_qcow2->encrypt ? "on" : "off";
+        assert(0 == set_option_parameter(param, BLOCK_OPT_ENCRYPT, tmp));
+        assert(0 == set_option_parameter_int(param,
+                              BLOCK_OPT_CLUSTER_SIZE, o_qcow2->cluster_size));
+        if (o_qcow2->prealloc_mode) {
+            assert(0 == set_option_parameter(param,
+                              BLOCK_OPT_PREALLOC, o_qcow2->prealloc_mode));
+        }
+        backing_flag = o_qcow2->backing_flag;
+        break;
+    case QB_FMT_RAW:
+        o_raw = &qbco.o_fmt.fmt_op.o_raw;
+        assert(0 == set_option_parameter_int(param,
+                              BLOCK_OPT_SIZE, o_raw->virt_size));
+        break;
+    case QB_FMT_RBD:
+        o_rbd = &qbco.o_fmt.fmt_op.o_rbd;
+        assert(0 == set_option_parameter_int(param,
+                              BLOCK_OPT_SIZE, o_rbd->virt_size));
+        assert(0 == set_option_parameter_int(param,
+                              BLOCK_OPT_CLUSTER_SIZE, o_rbd->cluster_size));
+        break;
+    case QB_FMT_SHEEPDOG:
+        o_sd = &qbco.o_fmt.fmt_op.o_sheepdog;
+        assert(0 == set_option_parameter_int(param,
+                              BLOCK_OPT_SIZE, o_sd->virt_size));
+        if (o_sd->backing_file) {
+            assert(0 == set_option_parameter(param,
+                              BLOCK_OPT_BACKING_FILE, o_sd->backing_file));
+        }
+        if (o_sd->prealloc_mode) {
+            assert(0 == set_option_parameter(param,
+                              BLOCK_OPT_PREALLOC, o_sd->prealloc_mode));
+        }
+        backing_flag = o_sd->backing_flag;
+        break;
+    case QB_FMT_VDI:
+        o_vdi = &qbco.o_fmt.fmt_op.o_vdi;
+        assert(0 == set_option_parameter_int(param,
+                              BLOCK_OPT_SIZE, o_vdi->virt_size));
+        /* following option is not always valid depends on configuration */
+        set_option_parameter_int(param,
+                              BLOCK_OPT_CLUSTER_SIZE, o_vdi->cluster_size);
+        set_option_parameter_int(param, "static", o_vdi->prealloc_mode);
+        break;
+    case QB_FMT_VMDK:
+        o_vmdk = &qbco.o_fmt.fmt_op.o_vmdk;
+        assert(0 == set_option_parameter_int(param,
+                              BLOCK_OPT_SIZE, o_vmdk->virt_size));
+        if (o_vmdk->backing_file) {
+            assert(0 == set_option_parameter(param,
+                              BLOCK_OPT_BACKING_FILE, o_vmdk->backing_file));
+        }
+        tmp = o_vmdk->compat_version6 ? "on" : "off";
+        assert(0 == set_option_parameter(param, BLOCK_OPT_COMPAT6, tmp));
+        if (o_vmdk->subfmt) {
+            assert(0 == set_option_parameter(param,
+                              BLOCK_OPT_SUBFMT, o_vmdk->subfmt));
+        }
+        backing_flag = o_vmdk->backing_flag;
+        break;
+    case QB_FMT_VPC:
+        o_vpc = &qbco.o_fmt.fmt_op.o_vpc;
+        assert(0 == set_option_parameter_int(param,
+                               BLOCK_OPT_SIZE, o_vpc->virt_size));
+        if (o_vpc->subfmt) {
+            assert(0 == set_option_parameter(param,
+                               BLOCK_OPT_SUBFMT, o_vpc->subfmt));
+        }

libqblock.c line 441

Quoted 10 lines
libqblock.c new file +10 -0
+        break;
+    default:
+        break;
+    }
+
+    backing_file = get_option_parameter(param, BLOCK_OPT_BACKING_FILE);
+    if (backing_file && backing_file->value.s) {
+        if (!strcmp(op->o_loc.filename, backing_file->value.s)) {
+            ret = QB_ERR_INVALID_PARAM;
+            qbs->err_msg = "backing file is the same with new file.";

same as the new file

Quoted 25 lines
libqblock.c new file +25 -0
+            qbs->err_no = 0;
+            goto out;
+        }
+    }
+
+    backing_fmt = get_option_parameter(param, BLOCK_OPT_BACKING_FMT);
+    if (backing_fmt && backing_fmt->value.s) {
+        backing_drv = bdrv_find_format(backing_fmt->value.s);
+        assert(backing_drv != NULL);
+    }
+
+    size = get_option_parameter(param, BLOCK_OPT_SIZE);
+    if (size && size->value.n <= 0) {
+        if (backing_file && backing_file->value.s) {
+            uint64_t size;
+            char buf[32];
+            int back_flags;
+
+            /* backing files always opened read-only */
+            back_flags =
+                backing_flag &
+                ~(BDRV_O_RDWR | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING);
+
+            bs = bdrv_new("");
+

libqblock.c line 476

Quoted 10 lines
libqblock.c new file +10 -0
+            ret = bdrv_open(bs, backing_file->value.s,
+                                back_flags, backing_drv);
+            if (ret < 0) {
+                ret = QB_ERR_INTERNAL_ERR;
+                qbs->err_msg = "failed to open the backing file.";
+                qbs->err_no = -errno;
+                goto out;
+            }
+            bdrv_get_geometry(bs, &size);
+            size *= 512;

Where's the #define?

Quoted 8 lines
libqblock.c new file +8 -0
+
+            snprintf(buf, sizeof(buf), "%" PRId64, size);
+            set_option_parameter(param, BLOCK_OPT_SIZE, buf);
+        } else {
+            ret = QB_ERR_INVALID_PARAM;
+            qbs->err_msg = "neither size or backing file was not set.";
+            qbs->err_no = 0;
+            goto out;

libqblock.c line 494

Quoted 10 lines
libqblock.c new file +10 -0
+        }
+    }
+
+    bd_ret = bdrv_create(drv, op->o_loc.filename, param);
+
+    if (bd_ret < 0) {
+        ret = QB_ERR_INTERNAL_ERR;
+        qbs->err_no = bd_ret;
+        if (bd_ret == -ENOTSUP) {
+            qbs->err_msg = "formatting option not supported.";

s/f/F/

Quoted 10 lines
libqblock.c new file +10 -0
+        } else if (bd_ret == -EFBIG) {
+            qbs->err_msg = "The image size is too large.";
+        } else {
+            qbs->err_msg = "Error in creating the image.";
+        }
+        goto out;
+    }
+
+out:
+    free_option_parameters(create_options);

libqblock.c line 514

Quoted 10 lines
libqblock.c new file +10 -0
+    free_option_parameters(param);
+
+    if (bs) {
+        bdrv_delete(bs);
+    }
+
+    return ret;
+}
+
+/* ignore case that len is not alligned to 512 now. */

aligned

libqblock.c line 524

Quoted 10 lines
libqblock.c new file +10 -0
+int qb_read(struct QBlockState *qbs, const void *buf, size_t len,
+                                                      off_t offset)
+{
+    int ret = 0, bd_ret;
+    BlockDriverState *bs;
+
+    bs = qbs->bdrvs;
+
+    assert((len & 0x01ff) == 0);
+    assert((offset & 0x01ff) == 0);

Also here use the #defined constant.

libqblock.c line 532

Quoted 8 lines
libqblock.c new file +8 -0
+    bd_ret = bdrv_read(bs, offset >> 9, (uint8_t *)buf, len >> 9);
+    if (bd_ret < 0) {
+        ret = QB_ERR_INTERNAL_ERR;
+        qbs->err_no = bd_ret;
+        if (bd_ret == -EIO) {
+            qbs->err_msg = "Generic I/O error.";
+        } else if (bd_ret == -ENOMEDIUM) {
+            qbs->err_msg = "No meida inserted.";

media

Quoted 153 lines
libqblock.c new file +138 -0
+        } else if (bd_ret == -EINVAL) {
+            qbs->err_msg = "Sector was not correct.";
+        } else {
+            qbs->err_msg = "Internal error.";
+        }
+    }
+    return ret;
+}
+
+/* ignore case that len is not alligned to 512 now. */
+int qb_write(struct QBlockState *qbs, const void *buf, size_t len,
+                                                       off_t offset)
+{
+    int ret = 0, bd_ret;
+    BlockDriverState *bs;
+
+    bs = qbs->bdrvs;
+
+    assert((len & 0x01ff) == 0);
+    assert((offset & 0x01ff) == 0);
+    bd_ret = bdrv_write(bs, offset >> 9, buf, len >> 9);
+    if (bd_ret < 0) {
+        ret = QB_ERR_INTERNAL_ERR;
+        qbs->err_no = bd_ret;
+        if (bd_ret == -EIO) {
+            qbs->err_msg = "Generic I/O error.";
+        } else if (bd_ret == -ENOMEDIUM) {
+            qbs->err_msg = "No meida inserted.";
+        } else if (bd_ret == -EINVAL) {
+            qbs->err_msg = "Sector was not correct.";
+        } else if (bd_ret == -EACCES) {
+            qbs->err_msg = "Ready only device.";
+        } else {
+            qbs->err_msg = "Internal error.";
+        }
+    }
+    return ret;
+}
+
+int qb_flush(struct QBlockState *qbs)
+{
+    int ret = 0, bd_ret;
+    BlockDriverState *bs;
+
+    bs = qbs->bdrvs;
+    bd_ret = bdrv_flush(bs);
+    if (bd_ret < 0) {
+        ret = QB_ERR_INTERNAL_ERR;
+        qbs->err_no = bd_ret;
+        qbs->err_msg = NULL;
+    }
+    return ret;
+}
+
+int qb_infoimage_get(struct QBlockState *qbs, struct QBlockInfoImage **info)
+{
+    int ret = 0;
+    BlockDriverState *bs;
+    const char *tmp;
+    uint64_t total_sectors;
+    char backing_filename[1024];
+
+    struct QBlockInfoImage *ifimg = FUNC_CALLOC(1,
+                                           sizeof(struct QBlockInfoImage));
+    if (ifimg == NULL) {
+        ret = QB_ERR_MEM_ERR;
+        set_err_nomem(qbs);
+        goto out;
+    }
+
+    bs = qbs->bdrvs;
+    ifimg->filename = strdup(qbs->filename);
+    if (ifimg->filename == NULL) {
+        ret = QB_ERR_MEM_ERR;
+        set_err_nomem(qbs);
+        goto err;
+    }
+    tmp = bdrv_get_format_name(bs);
+    ifimg->format = str2fmt(tmp);
+    /* support only file now */
+    ifimg->protocol = QB_PROTO_FILE;
+
+    bdrv_get_geometry(bs, &total_sectors);
+    ifimg->virt_size = total_sectors * 512;
+    ifimg->allocated_size = bdrv_get_allocated_file_size(bs);
+    ifimg->encrypt = bdrv_is_encrypted(bs);
+    bdrv_get_full_backing_filename(bs, backing_filename,
+                                   sizeof(backing_filename));
+    if (backing_filename[0] != '\0') {
+        ifimg->backing_filename = strdup(backing_filename);
+        if (ifimg->backing_filename == NULL) {
+            ret = QB_ERR_MEM_ERR;
+            set_err_nomem(qbs);
+            goto err;
+        }
+    }
+
+    *info = ifimg;
+ out:
+    return ret;
+ err:
+    qb_infoimage_free(&ifimg);
+    return ret;
+}
+
+void qb_infoimage_free(struct QBlockInfoImage **info)
+{
+    if (*info == NULL) {
+        return;
+    }
+    FUNC_FREE((*info)->filename);
+    FUNC_FREE((*info)->backing_filename);
+    FUNC_FREE(*info);
+    *info = NULL;
+    return;
+}
+
+bool qb_supports_format(enum QBlockFormat fmt)
+{
+    if (fmt < QB_FMT_MAX) {
+        return true;
+    }
+    return false;
+}
+
+bool qb_supports_protocol(enum QBlockProtocol proto)
+{
+    if (proto < QB_PROTO_MAX) {
+        return true;
+    }
+    return false;
+}
+
+const char *qb_error_get_detail(struct QBlockState *qbs, int *err_num)
+{
+    *err_num = qbs->err_no;
+    return qbs->err_msg;
+}
libqblock.h new file +9 -0
new file mode 100644
index 0000000..d2e9502
@@ -0,0 +1,447 @@
+/*
+ * Copyright IBM Corp. 2012
+ *
+ * Authors:
+ *  Wenchao Xia <[email protected]>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either

libqblock.h line 19

Quoted 10 lines
libqblock.h new file +10 -0
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA

web version

libqblock.h line 29

Quoted 10 lines
libqblock.h new file +10 -0
+ */
+
+#ifndef LIBQBLOCK_H
+#define LIBQBLOCK_H
+
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+
+#define bool _Bool

#include <stdbool.h>

libqblock.h line 39

Quoted 10 lines
libqblock.h new file +10 -0
+
+#define QB_ERR_MEM_ERR (-1)
+#define QB_ERR_INTERNAL_ERR (-2)
+#define QB_ERR_INVALID_PARAM (-3)
+
+/* this library is designed around this core struct. */
+struct QBlockState;
+
+/*
+   libarary init

library

libqblock.h line 41

Quoted 2 lines
libqblock.h new file +2 -0
+   This function get the library ready to use.
+ */

Please use markup for the comments, see for example memory.h.

Quoted 79 lines
libqblock.h new file +79 -0
+void libqblock_init(void);
+
+/*
+   create a new qbs object
+   params:
+     qbs: out, pointer that will receive created obj.
+   return:
+     0 on succeed, negative on failure.
+ */
+int qb_state_new(struct QBlockState **qbs);
+
+/*
+   delete a qbs object
+   params:
+     qbs: in, pointer that will be freed. *qbs will be set to NULL.
+   return:
+     void.
+ */
+void qb_state_free(struct QBlockState **qbs);
+
+
+/* flag used in open and create */
+#define LIBQBLOCK_O_RDWR        0x0002
+/* open the file read only and save writes in a snapshot */
+#define LIBQBLOCK_O_SNAPSHOT    0x0008
+/* do not use the host page cache */
+#define LIBQBLOCK_O_NOCACHE     0x0020
+/* use write-back caching */
+#define LIBQBLOCK_O_CACHE_WB    0x0040
+/* use native AIO instead of the thread pool */
+#define LIBQBLOCK_O_NATIVE_AIO  0x0080
+/* don't open the backing file */
+#define LIBQBLOCK_O_NO_BACKING  0x0100
+/* disable flushing on this disk */
+#define LIBQBLOCK_O_NO_FLUSH    0x0200
+/* copy read backing sectors into image */
+#define LIBQBLOCK_O_COPY_ON_READ 0x0400
+/* consistency hint for incoming migration */
+#define LIBQBLOCK_O_INCOMING    0x0800
+
+#define LIBQBLOCK_O_CACHE_MASK \
+   (LIBQBLOCK_O_NOCACHE | LIBQBLOCK_O_CACHE_WB | LIBQBLOCK_O_NO_FLUSH)
+
+enum QBlockProtocol {
+    QB_PROTO_FILE = 0,
+    QB_PROTO_MAX
+};
+
+enum QBlockFormat {
+    QB_FMT_NONE = 0,
+    QB_FMT_COW,
+    QB_FMT_QED,
+    QB_FMT_QCOW,
+    QB_FMT_QCOW2,
+    QB_FMT_RAW,
+    QB_FMT_RBD,
+    QB_FMT_SHEEPDOG,
+    QB_FMT_VDI,
+    QB_FMT_VMDK,
+    QB_FMT_VPC,
+    QB_FMT_MAX
+};
+
+/* block target location info, it include all information about how to find
+   the image */
+struct QBlockLocInfo {
+    int struct_size;
+    const char *filename;
+    enum QBlockProtocol protocol;
+};
+
+/* how to open the image */
+struct QBlockOptionOpen {
+    int struct_size;
+    struct QBlockLocInfo o_loc; /* how to find */
+    enum QBlockFormat o_fmt_type; /* how to extract */
+    int o_flag; /* how to control */
+};
+

libqblock.h line 130

Quoted 10 lines
libqblock.h new file +10 -0
+/*
+   create a new QBlockOptionOpen structure.
+   params:
+     op: out, pointer that will receive created structure.
+   return:
+     0 on succeed, negative on failure.
+ */
+static inline int qb_oo_new(struct QBlockOptionOpen **op)
+{
+    *op = calloc(1, sizeof(struct QBlockOptionOpen));

calloc() is not wrapped by your macros.

Quoted 8 lines
libqblock.h new file +8 -0
+    if (*op == NULL) {
+        return QB_ERR_MEM_ERR;
+    }
+    (*op)->struct_size = sizeof(struct QBlockOptionOpen);
+    (*op)->o_loc.struct_size = sizeof(struct QBlockLocInfo);
+    return 0;
+}
+

libqblock.h line 148

Quoted 10 lines
libqblock.h new file +10 -0
+/*
+   free QBlockOptionOpen structure.
+   params:
+     op: in, *op will be set as NULL after called.
+   return:
+     void
+ */
+static inline void qb_oo_free(struct QBlockOptionOpen **op)
+{
+    free(*op);

Also free() here.

Quoted 93 lines
libqblock.h new file +93 -0
+    *op = NULL;
+}
+
+/*
+   open a block object.
+   params:
+     qbs: in, pointer to QBlockState.
+     op: in, options for open.
+   return:
+     0 on success, negative on fail.
+ */
+int qb_open(struct QBlockState *qbs, struct QBlockOptionOpen *op);
+
+/*
+   close a block object.
+   params:
+     qbs: in, pointer to QBlockState.
+   return:
+     void.
+ */
+void qb_close(struct QBlockState *qbs);
+
+/* format related options, struct_size must be set. */
+struct QBlockOption_cow {
+    int struct_size;
+    size_t virt_size;
+    const char *backing_file;
+    int backing_flag;
+};
+
+struct QBlockOption_qed {
+    int struct_size;
+    size_t virt_size;
+    const char *backing_file;
+    enum QBlockFormat backing_fmt;
+    int backing_flag;
+    size_t cluster_size; /* unit is bytes */
+    size_t table_size; /* unit is clusters */
+};
+
+struct QBlockOption_qcow {
+    int struct_size;
+    size_t virt_size;
+    const char *backing_file;
+    int backing_flag;
+    bool encrypt;
+};
+
+struct QBlockOption_qcow2 {
+    int struct_size;
+    size_t virt_size;
+    const char *compat_level; /* "Compatibility level (0.10 or 1.1)" */
+    const char *backing_file;
+    enum QBlockFormat backing_fmt;
+    int backing_flag;
+    bool encrypt;
+    size_t cluster_size; /* unit is bytes */
+    const char *prealloc_mode; /* off or metadata */
+};
+
+struct QBlockOption_raw {
+    int struct_size;
+    size_t virt_size;
+};
+
+struct QBlockOption_rbd {
+    int struct_size;
+    size_t virt_size;
+    size_t cluster_size;
+};
+
+struct QBlockOption_sheepdog {
+    int struct_size;
+    size_t virt_size;
+    const char *backing_file;
+    int backing_flag;
+    const char *prealloc_mode; /* off or full */
+};
+
+struct QBlockOption_vdi {
+    int struct_size;
+    size_t virt_size;
+    size_t cluster_size;
+    bool prealloc_mode;
+};
+
+struct QBlockOption_vmdk {
+    int struct_size;
+    size_t virt_size;
+    const char *backing_file;
+    int backing_flag;
+    bool compat_version6;
+    const char *subfmt;

libqblock.h line 251

Quoted 10 lines
libqblock.h new file +10 -0
+    /* vmdk flat extent format, values:
+   "{monolithicSparse (default) | monolithicFlat | twoGbMaxExtentSparse |
+    twoGbMaxExtentFlat | streamOptimized} */
+};
+
+struct QBlockOption_vpc {
+    int struct_size;
+    size_t virt_size;
+    const char *subfmt; /* "{dynamic (default) | fixed} " */
+};

I'd rather have different open functions than variant structures.

Quoted 185 lines
libqblock.h new file +184 -0
+
+union QBlockOption_fmt {
+    struct QBlockOption_cow       o_cow;
+    struct QBlockOption_qed       o_qed;
+    struct QBlockOption_qcow      o_qcow;
+    struct QBlockOption_qcow2     o_qcow2;
+    struct QBlockOption_raw       o_raw;
+    struct QBlockOption_rbd       o_rbd;
+    struct QBlockOption_sheepdog  o_sheepdog;
+    struct QBlockOption_vdi       o_vdi;
+    struct QBlockOption_vmdk      o_vmdk;
+    struct QBlockOption_vpc       o_vpc;
+};
+
+struct QBlockOptionFormat {
+    int struct_size;
+    enum QBlockFormat fmt_type;
+    union QBlockOption_fmt fmt_op;
+};
+
+/* struct_size in o_loc and o_fmt must set. To make this structure extensible,
+   all new member must be added in the tail of each structure. */
+struct QBlockOptionCreate {
+    int struct_size;
+    struct QBlockLocInfo o_loc;
+    struct QBlockOptionFormat o_fmt;
+};
+
+/*
+   create a new QBlockOptionCreate structure.
+   params:
+     op: out, pointer that will receive created structure.
+     fmt: format want to use.
+   return:
+     0 on succeed, negative on failure.
+ */
+static inline int qb_oc_new(struct QBlockOptionCreate **op,
+                            enum QBlockFormat fmt)
+{
+    *op = calloc(1, sizeof(struct QBlockOptionCreate));
+    if (*op == NULL) {
+        return QB_ERR_MEM_ERR;
+    }
+    (*op)->struct_size = sizeof(struct QBlockOptionCreate);
+    (*op)->o_loc.struct_size = sizeof(struct QBlockLocInfo);
+    (*op)->o_fmt.struct_size = sizeof(struct QBlockOptionFormat);
+
+    switch (fmt) {
+    case QB_FMT_COW:
+        (*op)->o_fmt.fmt_op.o_cow.struct_size =
+                                          sizeof(struct QBlockOption_cow);
+        break;
+    case QB_FMT_QED:
+        (*op)->o_fmt.fmt_op.o_qed.struct_size =
+                                          sizeof(struct QBlockOption_qed);
+        break;
+    case QB_FMT_QCOW:
+        (*op)->o_fmt.fmt_op.o_qcow.struct_size =
+                                          sizeof(struct QBlockOption_qcow);
+        break;
+    case QB_FMT_QCOW2:
+        (*op)->o_fmt.fmt_op.o_qcow2.struct_size =
+                                          sizeof(struct QBlockOption_qcow2);
+        break;
+    case QB_FMT_RAW:
+        (*op)->o_fmt.fmt_op.o_raw.struct_size =
+                                          sizeof(struct QBlockOption_raw);
+        break;
+    case QB_FMT_RBD:
+        (*op)->o_fmt.fmt_op.o_rbd.struct_size =
+                                          sizeof(struct QBlockOption_rbd);
+        break;
+    case QB_FMT_SHEEPDOG:
+        (*op)->o_fmt.fmt_op.o_sheepdog.struct_size =
+                                          sizeof(struct QBlockOption_sheepdog);
+        break;
+    case QB_FMT_VDI:
+        (*op)->o_fmt.fmt_op.o_vdi.struct_size =
+                                          sizeof(struct QBlockOption_vdi);
+        break;
+    case QB_FMT_VMDK:
+        (*op)->o_fmt.fmt_op.o_vmdk.struct_size =
+                                          sizeof(struct QBlockOption_vmdk);
+        break;
+    case QB_FMT_VPC:
+        (*op)->o_fmt.fmt_op.o_vpc.struct_size =
+                                          sizeof(struct QBlockOption_vpc);
+        break;
+    default:
+        break;
+    }
+    (*op)->o_fmt.fmt_type = fmt;
+    return 0;
+}
+
+/*
+   free QBlockOptionCreate structure.
+   params:
+     op: in, *op will be set as NULL after called.
+   return:
+     void
+ */
+static inline void qb_oc_free(struct QBlockOptionCreate **op)
+{
+    free(*op);
+    *op = NULL;
+}
+/*
+   create a block file.
+   params:
+     qbs: in, pointer to QBlockState.
+     op: in, create option.
+   return:
+     negative on fail, 0 on success.
+ */
+int qb_create(struct QBlockState *qbs, struct QBlockOptionCreate *op);
+
+
+/* sync access */
+/*
+   block sync read.
+   params:
+     qbs: in, pointer to QBlockState.
+     buf: out, buffer that receive the content.
+     len: in, length to read.
+     offset: in, offset in the block data.
+   return:
+     negative on fail, 0 on success.
+ */
+int qb_read(struct QBlockState *qbs, const void *buf, size_t len,
+                                                      off_t offset);
+
+/*
+   block sync read.
+   params:
+     qbs: in, pointer to QBlockState.
+     buf: in, buffer that would be wrote.
+     len: in, length to write.
+     offset: in, offset in the block data.
+   return:
+     negative on fail, 0 on success.
+ */
+int qb_write(struct QBlockState *qbs, const void *buf, size_t len,
+                                                       off_t offset);
+
+/*
+   block sync flush.
+   params:
+     qbs: in, pointer to QBlockState.
+   return:
+     negative on fail, 0 on success.
+ */
+int qb_flush(struct QBlockState *qbs);
+
+/* information */
+/* image related info, static information, from user perspective. */
+/* now it is a plain structure, wonder if it could be foldered into embbed one
+   to reflect that format related information better. */
+struct QBlockInfoImage {
+    int struct_size;
+    char *filename;
+    enum QBlockProtocol protocol;
+    enum QBlockFormat format;
+    size_t virt_size;
+    /* advance info */
+    size_t allocated_size;
+    bool encrypt;
+    char *backing_filename;
+};
+
+/* image info */
+/*
+   get image info.
+   params:
+     qbs: in, pointer to QBlockState.
+     info, out, pointer that would receive the information.
+   return:
+     negative on fail, 0 on success.
+ */
+int qb_infoimage_get(struct QBlockState *qbs, struct QBlockInfoImage **info);
+
+/*
+   free image info.
+   params:

libqblock.h line 445

Quoted 10 lines
libqblock.h new file +10 -0
+     info, in, pointer, *info would be set to NULL after function called.
+   return:
+     void.
+ */
+void qb_infoimage_free(struct QBlockInfoImage **info);
+
+/* misc */
+bool qb_supports_format(enum QBlockFormat fmt);
+bool qb_supports_protocol(enum QBlockProtocol proto);
+

A comment here stating that the string may not be free()d.

+const char *qb_error_get_detail(struct QBlockState *qbs, int *err_num);

Quoted 6 lines
libqblock.h new file +1 -1
+#endif
--

1.7.1

#2 lore raw
Quoted 39 lines · Blue Swirl
Quoted 25 lines · Wenchao Xia

This patch is the API design.

---
  libqblock.c |  670 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  libqblock.h |  447 +++++++++++++++++++++++++++++++++++++++
  2 files changed, 1117 insertions(+), 0 deletions(-)

create mode 100644 libqblock.c
create mode 100644 libqblock.h

libqblock.c new file +9 -0
new file mode 100644
index 0000000..52e46dc
@@ -0,0 +1,670 @@
+/*
+ * Copyright IBM Corp. 2012
+ *
+ * Authors:
+ *  Wenchao Xia <[email protected]>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either

libqblock.c line 19

Quoted 10 lines
libqblock.c new file +10 -0
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA

Current address of FSF is:

Quoted 6 lines

51 Franklin Street, Fifth Floor
Boston, MA 02110-1301
USA

However, please use the recommended web version like other files.

OK.

Quoted 12 lines
Quoted 12 lines
libqblock.c new file +12 -0
+ */
+
+#include "libqblock.h"
+
+#include <unistd.h>
+#include "block.h"
+#include "block_int.h"
+
+#define FUNC_FREE free
+#define FUNC_MALLOC malloc
+#define FUNC_CALLOC calloc
+
Quoted 9 lines

libqblock.c line 35

Quoted 4 lines
libqblock.c new file +4 -0
+#define CLEAN_FREE(p) { \
+        FUNC_FREE(p); \
+        (p) = NULL; \
+}

I don't think this is enough since block functions may use glib memory allocation. Probably the block files have to be compiled with wrappers for g_malloc and friends.

yes, I plan to patch qemu block code that used glib. I think qemu
block code did not consider OOM, so patches should go there to make it known of that, and then at higher level qemu exit when it found block layer reports OOM.

Quoted 13 lines
Quoted 13 lines
libqblock.c new file +13 -0
+
+/* details should be hidden to user */
+struct QBlockState {
+    BlockDriverState *bdrvs;
+    char *filename;
+    const char *err_msg;
+    int err_no;
+};
+
+static void set_err_nomem(struct QBlockState *qbs)
+{
+    qbs->err_msg = NULL;
+    qbs->err_no = 0;
Quoted 9 lines

libqblock.c line 54

Quoted 6 lines
libqblock.c new file +6 -0
+}
+
+void libqblock_init(void)
+{
+    bdrv_init();
+    return;

Useless return.

done.

Quoted 151 lines
Quoted 15 lines
libqblock.c new file +15 -0
+}
+
+int qb_state_new(struct QBlockState **qbs)
+{
+    *qbs = FUNC_CALLOC(1, sizeof(struct QBlockState));
+    if (*qbs == NULL) {
+        return QB_ERR_MEM_ERR;
+    }
+    (*qbs)->bdrvs = bdrv_new("hda");
+    if ((*qbs)->bdrvs == NULL) {
+        CLEAN_FREE(*qbs);
+        return QB_ERR_INTERNAL_ERR;
+    }
+    return 0;
+}

libqblock.c line 79

Quoted 10 lines
libqblock.c new file +10 -0
+
+void qb_state_free(struct QBlockState **qbs)
+{
+    CLEAN_FREE((*qbs)->filename);
+    if ((*qbs)->bdrvs == NULL) {
+        bdrv_delete((*qbs)->bdrvs);
+        (*qbs)->bdrvs = NULL;
+    }
+    CLEAN_FREE(*qbs);
+    return;

Remove.

Quoted 36 lines
libqblock.c new file +36 -0
+}
+
+static const char *fmt2str(enum QBlockFormat fmt)
+{
+    const char *ret = NULL;
+    switch (fmt) {
+    case QB_FMT_COW:
+        ret = "cow";
+        break;
+    case QB_FMT_QED:
+        ret = "qed";
+        break;
+    case QB_FMT_QCOW:
+        ret = "qcow";
+        break;
+    case QB_FMT_QCOW2:
+        ret = "qcow2";
+        break;
+    case QB_FMT_RAW:
+        ret = "raw";
+        break;
+    case QB_FMT_RBD:
+        ret = "rbd";
+        break;
+    case QB_FMT_SHEEPDOG:
+        ret = "sheepdog";
+        break;
+    case QB_FMT_VDI:
+        ret = "vdi";
+        break;
+    case QB_FMT_VMDK:
+        ret = "vmdk";
+        break;
+    case QB_FMT_VPC:
+        ret = "vpc";
+        break;

libqblock.c line 125

Quoted 10 lines
libqblock.c new file +10 -0
+    default:
+        break;
+    }
+    return ret;
+}
+
+static enum QBlockFormat str2fmt(const char *fmt)
+{
+    enum QBlockFormat ret = QB_FMT_NONE;
+    if (0 == strcmp(fmt, "cow")) {

Please use natural order:
strcmp(fmt, "cow") == 0

Quoted 21 lines
libqblock.c new file +21 -0
+        ret = QB_FMT_COW;
+    } else if (0 == strcmp(fmt, "qed")) {
+        ret = QB_FMT_QED;
+    } else if (0 == strcmp(fmt, "qcow")) {
+        ret = QB_FMT_QCOW;
+    } else if (0 == strcmp(fmt, "qcow2")) {
+        ret = QB_FMT_QCOW2;
+    } else if (0 == strcmp(fmt, "raw")) {
+        ret = QB_FMT_RAW;
+    } else if (0 == strcmp(fmt, "rbd")) {
+        ret = QB_FMT_RBD;
+    } else if (0 == strcmp(fmt, "sheepdog")) {
+        ret = QB_FMT_SHEEPDOG;
+    } else if (0 == strcmp(fmt, "vdi")) {
+        ret = QB_FMT_VDI;
+    } else if (0 == strcmp(fmt, "vmdk")) {
+        ret = QB_FMT_VMDK;
+    } else if (0 == strcmp(fmt, "vpc")) {
+        ret = QB_FMT_VPC;
+    }
+    return ret;

libqblock.c line 156

Quoted 10 lines
libqblock.c new file +10 -0
+}
+
+/* copy information and take care the member difference in differect version.
+   Assuming all new member are added in the tail, struct size is the first
+   member, this is old to new version, src have its struct_size set. */
+static void qboo_adjust_o2n(struct QBlockOptionOpen *dest,
+                            struct QBlockOptionOpen *src)
+{
+    /* for simple it does memcpy now, need to take care of embbed structure */
+    memcpy(dest, src, src->struct_size);

Since later there will be a field by field copy, I'd use:
*dest = *src;

Quoted 14 lines
libqblock.c new file +14 -0
+}
+
+int qb_open(struct QBlockState *qbs, struct QBlockOptionOpen *op)
+{
+    int ret = 0, bd_ret;
+    BlockDriverState *bs;
+    struct QBlockOptionOpen qboo;
+    BlockDriver *bd;
+    const char *fmt;
+
+    /* take care of user settings */
+    qboo_adjust_o2n(&qboo, op);
+
+    /* check parameters */

libqblock.c line 180

Quoted 10 lines
libqblock.c new file +10 -0
+    if (qboo.o_loc.filename == NULL) {
+        ret = QB_ERR_INVALID_PARAM;
+        qbs->err_msg = "filename was not set.";
+        qbs->err_no = 0;
+        goto out;
+    }
+
+    if (path_has_protocol(qboo.o_loc.filename) > 0) {
+        ret = QB_ERR_INVALID_PARAM;
+        qbs->err_msg = "filename have protocol.";

had

Quoted 11 lines
libqblock.c new file +11 -0
+        qbs->err_no = 0;
+        goto out;
+    }
+
+    if (qboo.o_loc.protocol >= QB_PROTO_MAX) {
+        ret = QB_ERR_INVALID_PARAM;
+        qbs->err_msg = "protocol was not supported.";
+        qbs->err_no = 0;
+        goto out;
+    }
+
Quoted 9 lines

libqblock.c line 197

Quoted 6 lines
libqblock.c new file +6 -0
+    bd = NULL;
+    fmt = fmt2str(qboo.o_fmt_type);
+    if (fmt != NULL) {
+        bd = bdrv_find_format(fmt);
+        assert(bd != NULL);
+    }

What should happen in the NULL fmt case, goto out?

This should never happen so I used assert, because fmt was translated in libqblock, it should return a known format string unless there is a bug.

Quoted 551 lines
Quoted 23 lines
libqblock.c new file +23 -0
+
+
+    /* do real openning */
+    bs = qbs->bdrvs;
+    bd_ret = bdrv_open(bs, qboo.o_loc.filename, qboo.o_flag, bd);
+    if (bd_ret < 0) {
+        ret = QB_ERR_INTERNAL_ERR;
+        qbs->err_msg = "failed to open the file.";
+        qbs->err_no = -errno;
+        goto out;
+    }
+
+    if (qbs->filename != NULL) {
+        FUNC_FREE(qbs->filename);
+    }
+    qbs->filename = strdup(qboo.o_loc.filename);
+    if (qbs->filename == NULL) {
+        ret = QB_ERR_INVALID_PARAM;
+        set_err_nomem(qbs);
+        goto out;
+    }
+
+ out:

libqblock.c line 230

Quoted 10 lines
libqblock.c new file +10 -0
+    return ret;
+}
+
+void qb_close(struct QBlockState *qbs)
+{
+    BlockDriverState *bs;
+
+    bs = qbs->bdrvs;
+    bdrv_close(bs);
+    return;

Delete return

Quoted 68 lines
libqblock.c new file +68 -0
+}
+
+/* copy information and take care the member difference in differect version.
+   Assuming all new member are added in the tail, struct size is the first
+   member, this is old to new version, src have its struct_size set. */
+static void qboc_adjust_o2n(struct QBlockOptionCreate *dest,
+                            struct QBlockOptionCreate *src)
+{
+    /* for simple it does memcpy now, need to take care of embbed structure */
+    memcpy(dest, src, src->struct_size);
+}
+
+int qb_create(struct QBlockState *qbs, struct QBlockOptionCreate *op)
+{
+    int ret = 0, bd_ret;
+    BlockDriverState *bs = NULL;
+    BlockDriver *drv = NULL, *backing_drv = NULL;
+    struct QBlockOptionCreate qbco;
+    const char *fmt = NULL, *fmt_backing_str = NULL, *tmp;
+    QEMUOptionParameter *param = NULL, *create_options = NULL;
+    QEMUOptionParameter *backing_fmt, *backing_file, *size;
+    struct QBlockOption_cow *o_cow = NULL;
+    struct QBlockOption_qed *o_qed = NULL;
+    struct QBlockOption_qcow *o_qcow = NULL;
+    struct QBlockOption_qcow2 *o_qcow2 = NULL;
+    struct QBlockOption_raw *o_raw = NULL;
+    struct QBlockOption_rbd *o_rbd = NULL;
+    struct QBlockOption_sheepdog *o_sd = NULL;
+    struct QBlockOption_vdi *o_vdi = NULL;
+    struct QBlockOption_vmdk *o_vmdk = NULL;
+    struct QBlockOption_vpc *o_vpc = NULL;
+    int backing_flag = 0;
+
+    /* take care of user settings */
+    qboc_adjust_o2n(&qbco, op);
+
+    /* check parameters */
+    if (qbco.o_loc.filename == NULL) {
+        ret = QB_ERR_INVALID_PARAM;
+        qbs->err_msg = "filename was not set.";
+        qbs->err_no = 0;
+        goto out;
+    }
+
+    if (path_has_protocol(qbco.o_loc.filename) > 0) {
+        ret = QB_ERR_INVALID_PARAM;
+        qbs->err_msg = "filename have protocol.";
+        qbs->err_no = 0;
+        goto out;
+    }
+
+    if (qbco.o_loc.protocol >= QB_PROTO_MAX) {
+        ret = QB_ERR_INVALID_PARAM;
+        qbs->err_msg = "protocol was not supported.";
+        qbs->err_no = 0;
+        goto out;
+    }
+
+    /* set parameters */
+    fmt = fmt2str(qbco.o_fmt.fmt_type);
+    if (fmt == NULL) {
+        ret = QB_ERR_INVALID_PARAM;
+        qbs->err_msg = "format is not valid.";
+        qbs->err_no = 0;
+        goto out;
+    }
+    drv = bdrv_find_format(fmt);
+    assert(drv != NULL);

libqblock.c line 308

Quoted 10 lines
libqblock.c new file +10 -0
+
+    create_options = append_option_parameters(create_options,
+                                              drv->create_options);
+    param = parse_option_parameters("", create_options, param);
+
+    switch (qbco.o_fmt.fmt_type) {
+    case QB_FMT_COW:
+        o_cow = &qbco.o_fmt.fmt_op.o_cow;
+        assert(0 == set_option_parameter_int(param,
+                                BLOCK_OPT_SIZE, o_cow->virt_size));

expr == 0, also below

Quoted 123 lines
libqblock.c new file +123 -0
+        if (o_cow->backing_file) {
+            assert(0 == set_option_parameter(param,
+                                BLOCK_OPT_BACKING_FILE, o_cow->backing_file));
+        }
+        backing_flag = o_cow->backing_flag;
+        break;
+    case QB_FMT_QED:
+        o_qed = &qbco.o_fmt.fmt_op.o_qed;
+        assert(0 == set_option_parameter_int(param,
+                                BLOCK_OPT_SIZE, o_qed->virt_size));
+        if (o_qed->backing_file) {
+            assert(0 == set_option_parameter(param,
+                                BLOCK_OPT_BACKING_FILE, o_qed->backing_file));
+        }
+        fmt_backing_str = fmt2str(o_qed->backing_fmt);
+        if (fmt_backing_str) {
+            assert(0 == set_option_parameter(param,
+                                BLOCK_OPT_BACKING_FMT, fmt_backing_str));
+        }
+        assert(0 == set_option_parameter_int(param,
+                                BLOCK_OPT_CLUSTER_SIZE, o_qed->cluster_size));
+        assert(0 == set_option_parameter_int(param,
+                                BLOCK_OPT_TABLE_SIZE, o_qed->table_size));
+        backing_flag = o_qed->backing_flag;
+        break;
+    case QB_FMT_QCOW:
+        o_qcow = &qbco.o_fmt.fmt_op.o_qcow;
+        assert(0 == set_option_parameter_int(param,
+                                BLOCK_OPT_SIZE, o_qcow->virt_size));
+        if (o_qcow->backing_file) {
+            assert(0 == set_option_parameter(param,
+                                BLOCK_OPT_BACKING_FILE, o_qcow->backing_file));
+        }
+        tmp = o_qcow->encrypt ? "on" : "off";
+        assert(0 == set_option_parameter(param, BLOCK_OPT_ENCRYPT, tmp));
+        backing_flag = o_qcow->backing_flag;
+        break;
+    case QB_FMT_QCOW2:
+        o_qcow2 = &qbco.o_fmt.fmt_op.o_qcow2;
+        assert(0 == set_option_parameter_int(param,
+                              BLOCK_OPT_SIZE, o_qcow2->virt_size));
+        if (o_qcow2->backing_file) {
+            assert(0 == set_option_parameter(param,
+                              BLOCK_OPT_BACKING_FILE, o_qcow2->backing_file));
+        }
+        fmt_backing_str = fmt2str(o_qcow2->backing_fmt);
+        if (fmt_backing_str) {
+            assert(0 == set_option_parameter(param,
+                              BLOCK_OPT_BACKING_FMT, fmt_backing_str));
+        }
+        if (o_qcow2->compat_level) {
+            assert(0 == set_option_parameter(param,
+                              BLOCK_OPT_COMPAT_LEVEL, o_qcow2->compat_level));
+        }
+        tmp = o_qcow2->encrypt ? "on" : "off";
+        assert(0 == set_option_parameter(param, BLOCK_OPT_ENCRYPT, tmp));
+        assert(0 == set_option_parameter_int(param,
+                              BLOCK_OPT_CLUSTER_SIZE, o_qcow2->cluster_size));
+        if (o_qcow2->prealloc_mode) {
+            assert(0 == set_option_parameter(param,
+                              BLOCK_OPT_PREALLOC, o_qcow2->prealloc_mode));
+        }
+        backing_flag = o_qcow2->backing_flag;
+        break;
+    case QB_FMT_RAW:
+        o_raw = &qbco.o_fmt.fmt_op.o_raw;
+        assert(0 == set_option_parameter_int(param,
+                              BLOCK_OPT_SIZE, o_raw->virt_size));
+        break;
+    case QB_FMT_RBD:
+        o_rbd = &qbco.o_fmt.fmt_op.o_rbd;
+        assert(0 == set_option_parameter_int(param,
+                              BLOCK_OPT_SIZE, o_rbd->virt_size));
+        assert(0 == set_option_parameter_int(param,
+                              BLOCK_OPT_CLUSTER_SIZE, o_rbd->cluster_size));
+        break;
+    case QB_FMT_SHEEPDOG:
+        o_sd = &qbco.o_fmt.fmt_op.o_sheepdog;
+        assert(0 == set_option_parameter_int(param,
+                              BLOCK_OPT_SIZE, o_sd->virt_size));
+        if (o_sd->backing_file) {
+            assert(0 == set_option_parameter(param,
+                              BLOCK_OPT_BACKING_FILE, o_sd->backing_file));
+        }
+        if (o_sd->prealloc_mode) {
+            assert(0 == set_option_parameter(param,
+                              BLOCK_OPT_PREALLOC, o_sd->prealloc_mode));
+        }
+        backing_flag = o_sd->backing_flag;
+        break;
+    case QB_FMT_VDI:
+        o_vdi = &qbco.o_fmt.fmt_op.o_vdi;
+        assert(0 == set_option_parameter_int(param,
+                              BLOCK_OPT_SIZE, o_vdi->virt_size));
+        /* following option is not always valid depends on configuration */
+        set_option_parameter_int(param,
+                              BLOCK_OPT_CLUSTER_SIZE, o_vdi->cluster_size);
+        set_option_parameter_int(param, "static", o_vdi->prealloc_mode);
+        break;
+    case QB_FMT_VMDK:
+        o_vmdk = &qbco.o_fmt.fmt_op.o_vmdk;
+        assert(0 == set_option_parameter_int(param,
+                              BLOCK_OPT_SIZE, o_vmdk->virt_size));
+        if (o_vmdk->backing_file) {
+            assert(0 == set_option_parameter(param,
+                              BLOCK_OPT_BACKING_FILE, o_vmdk->backing_file));
+        }
+        tmp = o_vmdk->compat_version6 ? "on" : "off";
+        assert(0 == set_option_parameter(param, BLOCK_OPT_COMPAT6, tmp));
+        if (o_vmdk->subfmt) {
+            assert(0 == set_option_parameter(param,
+                              BLOCK_OPT_SUBFMT, o_vmdk->subfmt));
+        }
+        backing_flag = o_vmdk->backing_flag;
+        break;
+    case QB_FMT_VPC:
+        o_vpc = &qbco.o_fmt.fmt_op.o_vpc;
+        assert(0 == set_option_parameter_int(param,
+                               BLOCK_OPT_SIZE, o_vpc->virt_size));
+        if (o_vpc->subfmt) {
+            assert(0 == set_option_parameter(param,
+                               BLOCK_OPT_SUBFMT, o_vpc->subfmt));
+        }

libqblock.c line 441

Quoted 10 lines
libqblock.c new file +10 -0
+        break;
+    default:
+        break;
+    }
+
+    backing_file = get_option_parameter(param, BLOCK_OPT_BACKING_FILE);
+    if (backing_file && backing_file->value.s) {
+        if (!strcmp(op->o_loc.filename, backing_file->value.s)) {
+            ret = QB_ERR_INVALID_PARAM;
+            qbs->err_msg = "backing file is the same with new file.";

same as the new file

Quoted 25 lines
libqblock.c new file +25 -0
+            qbs->err_no = 0;
+            goto out;
+        }
+    }
+
+    backing_fmt = get_option_parameter(param, BLOCK_OPT_BACKING_FMT);
+    if (backing_fmt && backing_fmt->value.s) {
+        backing_drv = bdrv_find_format(backing_fmt->value.s);
+        assert(backing_drv != NULL);
+    }
+
+    size = get_option_parameter(param, BLOCK_OPT_SIZE);
+    if (size && size->value.n <= 0) {
+        if (backing_file && backing_file->value.s) {
+            uint64_t size;
+            char buf[32];
+            int back_flags;
+
+            /* backing files always opened read-only */
+            back_flags =
+                backing_flag &
+                ~(BDRV_O_RDWR | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING);
+
+            bs = bdrv_new("");
+

libqblock.c line 476

Quoted 10 lines
libqblock.c new file +10 -0
+            ret = bdrv_open(bs, backing_file->value.s,
+                                back_flags, backing_drv);
+            if (ret < 0) {
+                ret = QB_ERR_INTERNAL_ERR;
+                qbs->err_msg = "failed to open the backing file.";
+                qbs->err_no = -errno;
+                goto out;
+            }
+            bdrv_get_geometry(bs, &size);
+            size *= 512;

Where's the #define?

Quoted 8 lines
libqblock.c new file +8 -0
+
+            snprintf(buf, sizeof(buf), "%" PRId64, size);
+            set_option_parameter(param, BLOCK_OPT_SIZE, buf);
+        } else {
+            ret = QB_ERR_INVALID_PARAM;
+            qbs->err_msg = "neither size or backing file was not set.";
+            qbs->err_no = 0;
+            goto out;

libqblock.c line 494

Quoted 10 lines
libqblock.c new file +10 -0
+        }
+    }
+
+    bd_ret = bdrv_create(drv, op->o_loc.filename, param);
+
+    if (bd_ret < 0) {
+        ret = QB_ERR_INTERNAL_ERR;
+        qbs->err_no = bd_ret;
+        if (bd_ret == -ENOTSUP) {
+            qbs->err_msg = "formatting option not supported.";

s/f/F/

Quoted 10 lines
libqblock.c new file +10 -0
+        } else if (bd_ret == -EFBIG) {
+            qbs->err_msg = "The image size is too large.";
+        } else {
+            qbs->err_msg = "Error in creating the image.";
+        }
+        goto out;
+    }
+
+out:
+    free_option_parameters(create_options);

libqblock.c line 514

Quoted 10 lines
libqblock.c new file +10 -0
+    free_option_parameters(param);
+
+    if (bs) {
+        bdrv_delete(bs);
+    }
+
+    return ret;
+}
+
+/* ignore case that len is not alligned to 512 now. */

aligned

libqblock.c line 524

Quoted 10 lines
libqblock.c new file +10 -0
+int qb_read(struct QBlockState *qbs, const void *buf, size_t len,
+                                                      off_t offset)
+{
+    int ret = 0, bd_ret;
+    BlockDriverState *bs;
+
+    bs = qbs->bdrvs;
+
+    assert((len & 0x01ff) == 0);
+    assert((offset & 0x01ff) == 0);

Also here use the #defined constant.

libqblock.c line 532

Quoted 8 lines
libqblock.c new file +8 -0
+    bd_ret = bdrv_read(bs, offset >> 9, (uint8_t *)buf, len >> 9);
+    if (bd_ret < 0) {
+        ret = QB_ERR_INTERNAL_ERR;
+        qbs->err_no = bd_ret;
+        if (bd_ret == -EIO) {
+            qbs->err_msg = "Generic I/O error.";
+        } else if (bd_ret == -ENOMEDIUM) {
+            qbs->err_msg = "No meida inserted.";

media

Quoted 153 lines
libqblock.c new file +138 -0
+        } else if (bd_ret == -EINVAL) {
+            qbs->err_msg = "Sector was not correct.";
+        } else {
+            qbs->err_msg = "Internal error.";
+        }
+    }
+    return ret;
+}
+
+/* ignore case that len is not alligned to 512 now. */
+int qb_write(struct QBlockState *qbs, const void *buf, size_t len,
+                                                       off_t offset)
+{
+    int ret = 0, bd_ret;
+    BlockDriverState *bs;
+
+    bs = qbs->bdrvs;
+
+    assert((len & 0x01ff) == 0);
+    assert((offset & 0x01ff) == 0);
+    bd_ret = bdrv_write(bs, offset >> 9, buf, len >> 9);
+    if (bd_ret < 0) {
+        ret = QB_ERR_INTERNAL_ERR;
+        qbs->err_no = bd_ret;
+        if (bd_ret == -EIO) {
+            qbs->err_msg = "Generic I/O error.";
+        } else if (bd_ret == -ENOMEDIUM) {
+            qbs->err_msg = "No meida inserted.";
+        } else if (bd_ret == -EINVAL) {
+            qbs->err_msg = "Sector was not correct.";
+        } else if (bd_ret == -EACCES) {
+            qbs->err_msg = "Ready only device.";
+        } else {
+            qbs->err_msg = "Internal error.";
+        }
+    }
+    return ret;
+}
+
+int qb_flush(struct QBlockState *qbs)
+{
+    int ret = 0, bd_ret;
+    BlockDriverState *bs;
+
+    bs = qbs->bdrvs;
+    bd_ret = bdrv_flush(bs);
+    if (bd_ret < 0) {
+        ret = QB_ERR_INTERNAL_ERR;
+        qbs->err_no = bd_ret;
+        qbs->err_msg = NULL;
+    }
+    return ret;
+}
+
+int qb_infoimage_get(struct QBlockState *qbs, struct QBlockInfoImage **info)
+{
+    int ret = 0;
+    BlockDriverState *bs;
+    const char *tmp;
+    uint64_t total_sectors;
+    char backing_filename[1024];
+
+    struct QBlockInfoImage *ifimg = FUNC_CALLOC(1,
+                                           sizeof(struct QBlockInfoImage));
+    if (ifimg == NULL) {
+        ret = QB_ERR_MEM_ERR;
+        set_err_nomem(qbs);
+        goto out;
+    }
+
+    bs = qbs->bdrvs;
+    ifimg->filename = strdup(qbs->filename);
+    if (ifimg->filename == NULL) {
+        ret = QB_ERR_MEM_ERR;
+        set_err_nomem(qbs);
+        goto err;
+    }
+    tmp = bdrv_get_format_name(bs);
+    ifimg->format = str2fmt(tmp);
+    /* support only file now */
+    ifimg->protocol = QB_PROTO_FILE;
+
+    bdrv_get_geometry(bs, &total_sectors);
+    ifimg->virt_size = total_sectors * 512;
+    ifimg->allocated_size = bdrv_get_allocated_file_size(bs);
+    ifimg->encrypt = bdrv_is_encrypted(bs);
+    bdrv_get_full_backing_filename(bs, backing_filename,
+                                   sizeof(backing_filename));
+    if (backing_filename[0] != '\0') {
+        ifimg->backing_filename = strdup(backing_filename);
+        if (ifimg->backing_filename == NULL) {
+            ret = QB_ERR_MEM_ERR;
+            set_err_nomem(qbs);
+            goto err;
+        }
+    }
+
+    *info = ifimg;
+ out:
+    return ret;
+ err:
+    qb_infoimage_free(&ifimg);
+    return ret;
+}
+
+void qb_infoimage_free(struct QBlockInfoImage **info)
+{
+    if (*info == NULL) {
+        return;
+    }
+    FUNC_FREE((*info)->filename);
+    FUNC_FREE((*info)->backing_filename);
+    FUNC_FREE(*info);
+    *info = NULL;
+    return;
+}
+
+bool qb_supports_format(enum QBlockFormat fmt)
+{
+    if (fmt < QB_FMT_MAX) {
+        return true;
+    }
+    return false;
+}
+
+bool qb_supports_protocol(enum QBlockProtocol proto)
+{
+    if (proto < QB_PROTO_MAX) {
+        return true;
+    }
+    return false;
+}
+
+const char *qb_error_get_detail(struct QBlockState *qbs, int *err_num)
+{
+    *err_num = qbs->err_no;
+    return qbs->err_msg;
+}
libqblock.h new file +9 -0
new file mode 100644
index 0000000..d2e9502
@@ -0,0 +1,447 @@
+/*
+ * Copyright IBM Corp. 2012
+ *
+ * Authors:
+ *  Wenchao Xia <[email protected]>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either

libqblock.h line 19

Quoted 10 lines
libqblock.h new file +10 -0
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA

web version

libqblock.h line 29

Quoted 10 lines
libqblock.h new file +10 -0
+ */
+
+#ifndef LIBQBLOCK_H
+#define LIBQBLOCK_H
+
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+
+#define bool _Bool

#include <stdbool.h>

libqblock.h line 39

Quoted 10 lines
libqblock.h new file +10 -0
+
+#define QB_ERR_MEM_ERR (-1)
+#define QB_ERR_INTERNAL_ERR (-2)
+#define QB_ERR_INVALID_PARAM (-3)
+
+/* this library is designed around this core struct. */
+struct QBlockState;
+
+/*
+   libarary init

library

Quoted 5 lines

libqblock.h line 41

Quoted 2 lines
libqblock.h new file +2 -0
+   This function get the library ready to use.
+ */

Please use markup for the comments, see for example memory.h.

OK.

Quoted 83 lines
Quoted 83 lines
libqblock.h new file +83 -0
+void libqblock_init(void);
+
+/*
+   create a new qbs object
+   params:
+     qbs: out, pointer that will receive created obj.
+   return:
+     0 on succeed, negative on failure.
+ */
+int qb_state_new(struct QBlockState **qbs);
+
+/*
+   delete a qbs object
+   params:
+     qbs: in, pointer that will be freed. *qbs will be set to NULL.
+   return:
+     void.
+ */
+void qb_state_free(struct QBlockState **qbs);
+
+
+/* flag used in open and create */
+#define LIBQBLOCK_O_RDWR        0x0002
+/* open the file read only and save writes in a snapshot */
+#define LIBQBLOCK_O_SNAPSHOT    0x0008
+/* do not use the host page cache */
+#define LIBQBLOCK_O_NOCACHE     0x0020
+/* use write-back caching */
+#define LIBQBLOCK_O_CACHE_WB    0x0040
+/* use native AIO instead of the thread pool */
+#define LIBQBLOCK_O_NATIVE_AIO  0x0080
+/* don't open the backing file */
+#define LIBQBLOCK_O_NO_BACKING  0x0100
+/* disable flushing on this disk */
+#define LIBQBLOCK_O_NO_FLUSH    0x0200
+/* copy read backing sectors into image */
+#define LIBQBLOCK_O_COPY_ON_READ 0x0400
+/* consistency hint for incoming migration */
+#define LIBQBLOCK_O_INCOMING    0x0800
+
+#define LIBQBLOCK_O_CACHE_MASK \
+   (LIBQBLOCK_O_NOCACHE | LIBQBLOCK_O_CACHE_WB | LIBQBLOCK_O_NO_FLUSH)
+
+enum QBlockProtocol {
+    QB_PROTO_FILE = 0,
+    QB_PROTO_MAX
+};
+
+enum QBlockFormat {
+    QB_FMT_NONE = 0,
+    QB_FMT_COW,
+    QB_FMT_QED,
+    QB_FMT_QCOW,
+    QB_FMT_QCOW2,
+    QB_FMT_RAW,
+    QB_FMT_RBD,
+    QB_FMT_SHEEPDOG,
+    QB_FMT_VDI,
+    QB_FMT_VMDK,
+    QB_FMT_VPC,
+    QB_FMT_MAX
+};
+
+/* block target location info, it include all information about how to find
+   the image */
+struct QBlockLocInfo {
+    int struct_size;
+    const char *filename;
+    enum QBlockProtocol protocol;
+};
+
+/* how to open the image */
+struct QBlockOptionOpen {
+    int struct_size;
+    struct QBlockLocInfo o_loc; /* how to find */
+    enum QBlockFormat o_fmt_type; /* how to extract */
+    int o_flag; /* how to control */
+};
+
+/*
+   create a new QBlockOptionOpen structure.
+   params:
+     op: out, pointer that will receive created structure.
Quoted 9 lines

libqblock.h line 130

Quoted 6 lines
libqblock.h new file +6 -0
+   return:
+     0 on succeed, negative on failure.
+ */
+static inline int qb_oo_new(struct QBlockOptionOpen **op)
+{
+    *op = calloc(1, sizeof(struct QBlockOptionOpen));

calloc() is not wrapped by your macros.

Macro is defined at .c file, I am not sure if I should bring
it to header file, because user should not care about it.

Quoted 118 lines
Quoted 8 lines
libqblock.h new file +8 -0
+    if (*op == NULL) {
+        return QB_ERR_MEM_ERR;
+    }
+    (*op)->struct_size = sizeof(struct QBlockOptionOpen);
+    (*op)->o_loc.struct_size = sizeof(struct QBlockLocInfo);
+    return 0;
+}
+

libqblock.h line 148

Quoted 10 lines
libqblock.h new file +10 -0
+/*
+   free QBlockOptionOpen structure.
+   params:
+     op: in, *op will be set as NULL after called.
+   return:
+     void
+ */
+static inline void qb_oo_free(struct QBlockOptionOpen **op)
+{
+    free(*op);

Also free() here.

Quoted 97 lines
libqblock.h new file +97 -0
+    *op = NULL;
+}
+
+/*
+   open a block object.
+   params:
+     qbs: in, pointer to QBlockState.
+     op: in, options for open.
+   return:
+     0 on success, negative on fail.
+ */
+int qb_open(struct QBlockState *qbs, struct QBlockOptionOpen *op);
+
+/*
+   close a block object.
+   params:
+     qbs: in, pointer to QBlockState.
+   return:
+     void.
+ */
+void qb_close(struct QBlockState *qbs);
+
+/* format related options, struct_size must be set. */
+struct QBlockOption_cow {
+    int struct_size;
+    size_t virt_size;
+    const char *backing_file;
+    int backing_flag;
+};
+
+struct QBlockOption_qed {
+    int struct_size;
+    size_t virt_size;
+    const char *backing_file;
+    enum QBlockFormat backing_fmt;
+    int backing_flag;
+    size_t cluster_size; /* unit is bytes */
+    size_t table_size; /* unit is clusters */
+};
+
+struct QBlockOption_qcow {
+    int struct_size;
+    size_t virt_size;
+    const char *backing_file;
+    int backing_flag;
+    bool encrypt;
+};
+
+struct QBlockOption_qcow2 {
+    int struct_size;
+    size_t virt_size;
+    const char *compat_level; /* "Compatibility level (0.10 or 1.1)" */
+    const char *backing_file;
+    enum QBlockFormat backing_fmt;
+    int backing_flag;
+    bool encrypt;
+    size_t cluster_size; /* unit is bytes */
+    const char *prealloc_mode; /* off or metadata */
+};
+
+struct QBlockOption_raw {
+    int struct_size;
+    size_t virt_size;
+};
+
+struct QBlockOption_rbd {
+    int struct_size;
+    size_t virt_size;
+    size_t cluster_size;
+};
+
+struct QBlockOption_sheepdog {
+    int struct_size;
+    size_t virt_size;
+    const char *backing_file;
+    int backing_flag;
+    const char *prealloc_mode; /* off or full */
+};
+
+struct QBlockOption_vdi {
+    int struct_size;
+    size_t virt_size;
+    size_t cluster_size;
+    bool prealloc_mode;
+};
+
+struct QBlockOption_vmdk {
+    int struct_size;
+    size_t virt_size;
+    const char *backing_file;
+    int backing_flag;
+    bool compat_version6;
+    const char *subfmt;
+    /* vmdk flat extent format, values:
+   "{monolithicSparse (default) | monolithicFlat | twoGbMaxExtentSparse |
+    twoGbMaxExtentFlat | streamOptimized} */
+};
Quoted 9 lines

libqblock.h line 251

Quoted 6 lines
libqblock.h new file +6 -0
+
+struct QBlockOption_vpc {
+    int struct_size;
+    size_t virt_size;
+    const char *subfmt; /* "{dynamic (default) | fixed} " */
+};

I'd rather have different open functions than variant structures.

It have about 10 formats, API for each one seems not friendly API,
and hard to adding the comibination case of block protocol and formats later.

Quoted 189 lines
Quoted 189 lines
libqblock.h new file +189 -0
+
+union QBlockOption_fmt {
+    struct QBlockOption_cow       o_cow;
+    struct QBlockOption_qed       o_qed;
+    struct QBlockOption_qcow      o_qcow;
+    struct QBlockOption_qcow2     o_qcow2;
+    struct QBlockOption_raw       o_raw;
+    struct QBlockOption_rbd       o_rbd;
+    struct QBlockOption_sheepdog  o_sheepdog;
+    struct QBlockOption_vdi       o_vdi;
+    struct QBlockOption_vmdk      o_vmdk;
+    struct QBlockOption_vpc       o_vpc;
+};
+
+struct QBlockOptionFormat {
+    int struct_size;
+    enum QBlockFormat fmt_type;
+    union QBlockOption_fmt fmt_op;
+};
+
+/* struct_size in o_loc and o_fmt must set. To make this structure extensible,
+   all new member must be added in the tail of each structure. */
+struct QBlockOptionCreate {
+    int struct_size;
+    struct QBlockLocInfo o_loc;
+    struct QBlockOptionFormat o_fmt;
+};
+
+/*
+   create a new QBlockOptionCreate structure.
+   params:
+     op: out, pointer that will receive created structure.
+     fmt: format want to use.
+   return:
+     0 on succeed, negative on failure.
+ */
+static inline int qb_oc_new(struct QBlockOptionCreate **op,
+                            enum QBlockFormat fmt)
+{
+    *op = calloc(1, sizeof(struct QBlockOptionCreate));
+    if (*op == NULL) {
+        return QB_ERR_MEM_ERR;
+    }
+    (*op)->struct_size = sizeof(struct QBlockOptionCreate);
+    (*op)->o_loc.struct_size = sizeof(struct QBlockLocInfo);
+    (*op)->o_fmt.struct_size = sizeof(struct QBlockOptionFormat);
+
+    switch (fmt) {
+    case QB_FMT_COW:
+        (*op)->o_fmt.fmt_op.o_cow.struct_size =
+                                          sizeof(struct QBlockOption_cow);
+        break;
+    case QB_FMT_QED:
+        (*op)->o_fmt.fmt_op.o_qed.struct_size =
+                                          sizeof(struct QBlockOption_qed);
+        break;
+    case QB_FMT_QCOW:
+        (*op)->o_fmt.fmt_op.o_qcow.struct_size =
+                                          sizeof(struct QBlockOption_qcow);
+        break;
+    case QB_FMT_QCOW2:
+        (*op)->o_fmt.fmt_op.o_qcow2.struct_size =
+                                          sizeof(struct QBlockOption_qcow2);
+        break;
+    case QB_FMT_RAW:
+        (*op)->o_fmt.fmt_op.o_raw.struct_size =
+                                          sizeof(struct QBlockOption_raw);
+        break;
+    case QB_FMT_RBD:
+        (*op)->o_fmt.fmt_op.o_rbd.struct_size =
+                                          sizeof(struct QBlockOption_rbd);
+        break;
+    case QB_FMT_SHEEPDOG:
+        (*op)->o_fmt.fmt_op.o_sheepdog.struct_size =
+                                          sizeof(struct QBlockOption_sheepdog);
+        break;
+    case QB_FMT_VDI:
+        (*op)->o_fmt.fmt_op.o_vdi.struct_size =
+                                          sizeof(struct QBlockOption_vdi);
+        break;
+    case QB_FMT_VMDK:
+        (*op)->o_fmt.fmt_op.o_vmdk.struct_size =
+                                          sizeof(struct QBlockOption_vmdk);
+        break;
+    case QB_FMT_VPC:
+        (*op)->o_fmt.fmt_op.o_vpc.struct_size =
+                                          sizeof(struct QBlockOption_vpc);
+        break;
+    default:
+        break;
+    }
+    (*op)->o_fmt.fmt_type = fmt;
+    return 0;
+}
+
+/*
+   free QBlockOptionCreate structure.
+   params:
+     op: in, *op will be set as NULL after called.
+   return:
+     void
+ */
+static inline void qb_oc_free(struct QBlockOptionCreate **op)
+{
+    free(*op);
+    *op = NULL;
+}
+/*
+   create a block file.
+   params:
+     qbs: in, pointer to QBlockState.
+     op: in, create option.
+   return:
+     negative on fail, 0 on success.
+ */
+int qb_create(struct QBlockState *qbs, struct QBlockOptionCreate *op);
+
+
+/* sync access */
+/*
+   block sync read.
+   params:
+     qbs: in, pointer to QBlockState.
+     buf: out, buffer that receive the content.
+     len: in, length to read.
+     offset: in, offset in the block data.
+   return:
+     negative on fail, 0 on success.
+ */
+int qb_read(struct QBlockState *qbs, const void *buf, size_t len,
+                                                      off_t offset);
+
+/*
+   block sync read.
+   params:
+     qbs: in, pointer to QBlockState.
+     buf: in, buffer that would be wrote.
+     len: in, length to write.
+     offset: in, offset in the block data.
+   return:
+     negative on fail, 0 on success.
+ */
+int qb_write(struct QBlockState *qbs, const void *buf, size_t len,
+                                                       off_t offset);
+
+/*
+   block sync flush.
+   params:
+     qbs: in, pointer to QBlockState.
+   return:
+     negative on fail, 0 on success.
+ */
+int qb_flush(struct QBlockState *qbs);
+
+/* information */
+/* image related info, static information, from user perspective. */
+/* now it is a plain structure, wonder if it could be foldered into embbed one
+   to reflect that format related information better. */
+struct QBlockInfoImage {
+    int struct_size;
+    char *filename;
+    enum QBlockProtocol protocol;
+    enum QBlockFormat format;
+    size_t virt_size;
+    /* advance info */
+    size_t allocated_size;
+    bool encrypt;
+    char *backing_filename;
+};
+
+/* image info */
+/*
+   get image info.
+   params:
+     qbs: in, pointer to QBlockState.
+     info, out, pointer that would receive the information.
+   return:
+     negative on fail, 0 on success.
+ */
+int qb_infoimage_get(struct QBlockState *qbs, struct QBlockInfoImage **info);
+
+/*
+   free image info.
+   params:
+     info, in, pointer, *info would be set to NULL after function called.
+   return:
+     void.
+ */
+void qb_infoimage_free(struct QBlockInfoImage **info);
Quoted 9 lines

libqblock.h line 445

Quoted 6 lines
libqblock.h new file +5 -0
+
+/* misc */
+bool qb_supports_format(enum QBlockFormat fmt);
+bool qb_supports_protocol(enum QBlockProtocol proto);
+

A comment here stating that the string may not be free()d.

ok.

Quoted 8 lines

+const char *qb_error_get_detail(struct QBlockState *qbs, int *err_num);

Quoted 6 lines
libqblock.h new file +1 -1
+#endif
--

1.7.1

Signature · 3 lines
Best Regards

Wenchao Xia
#3 lore raw
In reply to Wenchao Xia

Il 09/08/2012 12:12, Wenchao Xia ha scritto:

libqblock.c line 240

Quoted 8 lines
libqblock.c new file +8 -0
+/* copy information and take care the member difference in differect version.
+   Assuming all new member are added in the tail, struct size is the first
+   member, this is old to new version, src have its struct_size set. */
+static void qboc_adjust_o2n(struct QBlockOptionCreate *dest,
+                            struct QBlockOptionCreate *src)
+{
+    /* for simple it does memcpy now, need to take care of embbed structure */
+    memcpy(dest, src, src->struct_size);

You need an assertion that src->struct_size < sizeof(*dest).

However, the structure size perhaps wasn't my brightest idea ever. But still thanks for preparing this prototype! Now that we have a more complete API to discuss, we can iterate to something better.

libqblock.c line 308

Quoted 2 lines
libqblock.c new file +2 -0
+        assert(0 == set_option_parameter_int(param,
+                                BLOCK_OPT_SIZE, o_cow->virt_size));

Assertions should not have side effects.

Quoted 62 lines
libqblock.h new file +56 -0
new file mode 100644
index 0000000..d2e9502
@@ -0,0 +1,447 @@
+/*
+ * Copyright IBM Corp. 2012
+ *
+ * Authors:
+ *  Wenchao Xia <[email protected]>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
+ */
+
+#ifndef LIBQBLOCK_H
+#define LIBQBLOCK_H
+
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+
+#define bool _Bool
+
+#define QB_ERR_MEM_ERR (-1)
+#define QB_ERR_INTERNAL_ERR (-2)
+#define QB_ERR_INVALID_PARAM (-3)
+
+/* this library is designed around this core struct. */
+struct QBlockState;
+
+/*
+   libarary init
+   This function get the library ready to use.
+ */
+void libqblock_init(void);
+
+/*
+   create a new qbs object
+   params:
+     qbs: out, pointer that will receive created obj.
+   return:
+     0 on succeed, negative on failure.
+ */
+int qb_state_new(struct QBlockState **qbs);
+
+/*
+   delete a qbs object
+   params:
+     qbs: in, pointer that will be freed. *qbs will be set to NULL.

libqblock.h line 66

Quoted 10 lines
libqblock.h new file +10 -0
+   return:
+     void.
+ */
+void qb_state_free(struct QBlockState **qbs);
+
+
+/* flag used in open and create */
+#define LIBQBLOCK_O_RDWR        0x0002
+/* open the file read only and save writes in a snapshot */
+#define LIBQBLOCK_O_SNAPSHOT    0x0008

I'd rather avoid exposing this for now.

libqblock.h line 72

Quoted 6 lines
libqblock.h new file +6 -0
+/* do not use the host page cache */
+#define LIBQBLOCK_O_NOCACHE     0x0020
+/* use write-back caching */
+#define LIBQBLOCK_O_CACHE_WB    0x0040
+/* use native AIO instead of the thread pool */
+#define LIBQBLOCK_O_NATIVE_AIO  0x0080

NATIVE_AIO should be an option for the file protocol. But it's mostly for performance and since we only support synchronous I/O we can drop it for now.

libqblock.h line 78

Quoted 6 lines
libqblock.h new file +6 -0
+/* don't open the backing file */
+#define LIBQBLOCK_O_NO_BACKING  0x0100
+/* disable flushing on this disk */
+#define LIBQBLOCK_O_NO_FLUSH    0x0200
+/* copy read backing sectors into image */
+#define LIBQBLOCK_O_COPY_ON_READ 0x0400

I'd rather avoid exposing this for now too.

libqblock.h line 80

Quoted 2 lines
libqblock.h new file +2 -0
+/* consistency hint for incoming migration */
+#define LIBQBLOCK_O_INCOMING    0x0800

This is internal to QEMU.

Please add a mask of valid bits and an assertion that unknown bits are zero.

Quoted 29 lines
libqblock.h new file +29 -0
+
+#define LIBQBLOCK_O_CACHE_MASK \
+   (LIBQBLOCK_O_NOCACHE | LIBQBLOCK_O_CACHE_WB | LIBQBLOCK_O_NO_FLUSH)
+
+enum QBlockProtocol {
+    QB_PROTO_FILE = 0,
+    QB_PROTO_MAX
+};
+
+enum QBlockFormat {
+    QB_FMT_NONE = 0,
+    QB_FMT_COW,
+    QB_FMT_QED,
+    QB_FMT_QCOW,
+    QB_FMT_QCOW2,
+    QB_FMT_RAW,
+    QB_FMT_RBD,
+    QB_FMT_SHEEPDOG,
+    QB_FMT_VDI,
+    QB_FMT_VMDK,
+    QB_FMT_VPC,
+    QB_FMT_MAX
+};
+
+/* block target location info, it include all information about how to find
+   the image */
+struct QBlockLocInfo {
+    int struct_size;
+    const char *filename;

libqblock.h line 119

Quoted 10 lines
libqblock.h new file +10 -0
+    enum QBlockProtocol protocol;
+};
+
+/* how to open the image */
+struct QBlockOptionOpen {
+    int struct_size;
+    struct QBlockLocInfo o_loc; /* how to find */
+    enum QBlockFormat o_fmt_type; /* how to extract */
+    int o_flag; /* how to control */
+};

You are right that the embedded structs are very complicated. I think we need to find the right balance between structs for extensibility and function arguments for ease of use.

For example, we could have

int qb_open(struct QBlockState *qbs,
struct QBlockLocation *loc,
struct QBlockFormatOption *op,
int o_flag);

where:

- QBlockLocation is basically your QBlockLocInfo, but restructured to use unions for the protocol-specific fields.

- QBlockFormatOption is basically your QBlockOptionFormat struct. Thus we can plan for specifying format-specific options at open time rather than just at create time (this can be useful, for example, for things such as lazy_refcounts). Until support is there in the block layer, we can simply check that all format-specific options are zero.

Since both QBlockLocation and QBlockFormatOption are basically a discriminated record (enum + union of structs) we can add a large char member to the union (e.g. char padding[512]) to keep the ABI stable. Users must zero out all fields, and future additions must ensure that the default value is represented by all-zeros.

libqblock.h line 206

Quoted 1 line
libqblock.h new file +1 -0
+    const char *prealloc_mode; /* off or metadata */

Make this an enum.

libqblock.h line 232

Quoted 1 line
libqblock.h new file +1 -0
+    bool prealloc_mode;

Here too.

libqblock.h line 244

Quoted 12 lines
libqblock.h new file +12 -0
+};
+
+struct QBlockOption_vmdk {
+    int struct_size;
+    size_t virt_size;
+    const char *backing_file;
+    int backing_flag;
+    bool compat_version6;
+    const char *subfmt;
+    /* vmdk flat extent format, values:
+   "{monolithicSparse (default) | monolithicFlat | twoGbMaxExtentSparse |
+    twoGbMaxExtentFlat | streamOptimized} */

Here too.

libqblock.h line 410

Quoted 5 lines
libqblock.h new file +5 -0
+/* information */
+/* image related info, static information, from user perspective. */
+/* now it is a plain structure, wonder if it could be foldered into embbed one
+   to reflect that format related information better. */
+struct QBlockInfoImage {

QBlockImageInfo

libqblock.h line 139

Quoted 1 line
libqblock.h new file +1 -0
+    int struct_size;

Here the struct size makes more sense, because it's a returned struct.

libqblock.h line 413

Quoted 2 lines
libqblock.h new file +2 -0
+    char *filename;
+    enum QBlockProtocol protocol;

Can this just be a struct QBlockLocation * (using a pointer avoids problems with embedded structs)?

libqblock.h line 414

Quoted 1 line
libqblock.h new file +1 -0
+    enum QBlockFormat format;

Can this just be a struct QBlockFormatOption * (same as the above)?

Quoted 7 lines
libqblock.h new file +7 -0
+    size_t virt_size;
+    /* advance info */
+    size_t allocated_size;
+    bool encrypt;
+    char *backing_filename;
+};
+

libqblock.h line 431

Quoted 10 lines
libqblock.h new file +10 -0
+/* image info */
+/*
+   get image info.
+   params:
+     qbs: in, pointer to QBlockState.
+     info, out, pointer that would receive the information.
+   return:
+     negative on fail, 0 on success.
+ */
+int qb_infoimage_get(struct QBlockState *qbs, struct QBlockInfoImage **info);

qb_get_image_info

libqblock.h line 440

Quoted 9 lines
libqblock.h new file +9 -0
+
+/*
+   free image info.
+   params:
+     info, in, pointer, *info would be set to NULL after function called.
+   return:
+     void.
+ */
+void qb_infoimage_free(struct QBlockInfoImage **info);

qb_free_image_info

Paolo

Quoted 8 lines
libqblock.h new file +7 -0
+
+/* misc */
+bool qb_supports_format(enum QBlockFormat fmt);
+bool qb_supports_protocol(enum QBlockProtocol proto);
+
+const char *qb_error_get_detail(struct QBlockState *qbs, int *err_num);
+#endif
#4 lore raw
Quoted 17 lines · Paolo Bonzini

Il 09/08/2012 12:12, Wenchao Xia ha scritto:

libqblock.c line 240

Quoted 8 lines
libqblock.c new file +8 -0
+/* copy information and take care the member difference in differect version.
+   Assuming all new member are added in the tail, struct size is the first
+   member, this is old to new version, src have its struct_size set. */
+static void qboc_adjust_o2n(struct QBlockOptionCreate *dest,
+                            struct QBlockOptionCreate *src)
+{
+    /* for simple it does memcpy now, need to take care of embbed structure */
+    memcpy(dest, src, src->struct_size);

You need an assertion that src->struct_size < sizeof(*dest).

However, the structure size perhaps wasn't my brightest idea ever. But still thanks for preparing this prototype! Now that we have a more complete API to discuss, we can iterate to something better.

Quoted 5 lines

libqblock.c line 308

Quoted 2 lines
libqblock.c new file +2 -0
+        assert(0 == set_option_parameter_int(param,
+                                BLOCK_OPT_SIZE, o_cow->virt_size));

Assertions should not have side effects.

what side effects would this line have?

Quoted 97 lines
Quoted 63 lines
libqblock.h new file +56 -0
new file mode 100644
index 0000000..d2e9502
@@ -0,0 +1,447 @@
+/*
+ * Copyright IBM Corp. 2012
+ *
+ * Authors:
+ *  Wenchao Xia <[email protected]>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
+ */
+
+#ifndef LIBQBLOCK_H
+#define LIBQBLOCK_H
+
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+
+#define bool _Bool
+
+#define QB_ERR_MEM_ERR (-1)
+#define QB_ERR_INTERNAL_ERR (-2)
+#define QB_ERR_INVALID_PARAM (-3)
+
+/* this library is designed around this core struct. */
+struct QBlockState;
+
+/*
+   libarary init
+   This function get the library ready to use.
+ */
+void libqblock_init(void);
+
+/*
+   create a new qbs object
+   params:
+     qbs: out, pointer that will receive created obj.
+   return:
+     0 on succeed, negative on failure.
+ */
+int qb_state_new(struct QBlockState **qbs);
+
+/*
+   delete a qbs object
+   params:
+     qbs: in, pointer that will be freed. *qbs will be set to NULL.

libqblock.h line 66

Quoted 10 lines
libqblock.h new file +10 -0
+   return:
+     void.
+ */
+void qb_state_free(struct QBlockState **qbs);
+
+
+/* flag used in open and create */
+#define LIBQBLOCK_O_RDWR        0x0002
+/* open the file read only and save writes in a snapshot */
+#define LIBQBLOCK_O_SNAPSHOT    0x0008

I'd rather avoid exposing this for now.

libqblock.h line 72

Quoted 6 lines
libqblock.h new file +6 -0
+/* do not use the host page cache */
+#define LIBQBLOCK_O_NOCACHE     0x0020
+/* use write-back caching */
+#define LIBQBLOCK_O_CACHE_WB    0x0040
+/* use native AIO instead of the thread pool */
+#define LIBQBLOCK_O_NATIVE_AIO  0x0080

NATIVE_AIO should be an option for the file protocol. But it's mostly for performance and since we only support synchronous I/O we can drop it for now.

libqblock.h line 78

Quoted 6 lines
libqblock.h new file +6 -0
+/* don't open the backing file */
+#define LIBQBLOCK_O_NO_BACKING  0x0100
+/* disable flushing on this disk */
+#define LIBQBLOCK_O_NO_FLUSH    0x0200
+/* copy read backing sectors into image */
+#define LIBQBLOCK_O_COPY_ON_READ 0x0400

I'd rather avoid exposing this for now too.

Quoted 7 lines

libqblock.h line 80

Quoted 2 lines
libqblock.h new file +2 -0
+/* consistency hint for incoming migration */
+#define LIBQBLOCK_O_INCOMING    0x0800

This is internal to QEMU.

Please add a mask of valid bits and an assertion that unknown bits are zero.

OK to hide this flag now. Do you think this flag belongs to the
control flag or I should sort them to anther option? In long term the library should provide full capablities as qemu block layer have so qemu can try migrate to it, so now I need to make sure the API designed
have left room for all features qemu have support.

Quoted 106 lines
Quoted 29 lines
libqblock.h new file +29 -0
+
+#define LIBQBLOCK_O_CACHE_MASK \
+   (LIBQBLOCK_O_NOCACHE | LIBQBLOCK_O_CACHE_WB | LIBQBLOCK_O_NO_FLUSH)
+
+enum QBlockProtocol {
+    QB_PROTO_FILE = 0,
+    QB_PROTO_MAX
+};
+
+enum QBlockFormat {
+    QB_FMT_NONE = 0,
+    QB_FMT_COW,
+    QB_FMT_QED,
+    QB_FMT_QCOW,
+    QB_FMT_QCOW2,
+    QB_FMT_RAW,
+    QB_FMT_RBD,
+    QB_FMT_SHEEPDOG,
+    QB_FMT_VDI,
+    QB_FMT_VMDK,
+    QB_FMT_VPC,
+    QB_FMT_MAX
+};
+
+/* block target location info, it include all information about how to find
+   the image */
+struct QBlockLocInfo {
+    int struct_size;
+    const char *filename;

libqblock.h line 119

Quoted 10 lines
libqblock.h new file +10 -0
+    enum QBlockProtocol protocol;
+};
+
+/* how to open the image */
+struct QBlockOptionOpen {
+    int struct_size;
+    struct QBlockLocInfo o_loc; /* how to find */
+    enum QBlockFormat o_fmt_type; /* how to extract */
+    int o_flag; /* how to control */
+};

You are right that the embedded structs are very complicated. I think we need to find the right balance between structs for extensibility and function arguments for ease of use.

For example, we could have

int qb_open(struct QBlockState *qbs,
struct QBlockLocation *loc,
struct QBlockFormatOption *op,
int o_flag);

where:

- QBlockLocation is basically your QBlockLocInfo, but restructured to use unions for the protocol-specific fields.

- QBlockFormatOption is basically your QBlockOptionFormat struct. Thus we can plan for specifying format-specific options at open time rather than just at create time (this can be useful, for example, for things such as lazy_refcounts). Until support is there in the block layer, we can simply check that all format-specific options are zero.

Since both QBlockLocation and QBlockFormatOption are basically a discriminated record (enum + union of structs) we can add a large char member to the union (e.g. char padding[512]) to keep the ABI stable. Users must zero out all fields, and future additions must ensure that the default value is represented by all-zeros.

libqblock.h line 206

Quoted 1 line
libqblock.h new file +1 -0
+    const char *prealloc_mode; /* off or metadata */

Make this an enum.

libqblock.h line 232

Quoted 1 line
libqblock.h new file +1 -0
+    bool prealloc_mode;

Here too.

libqblock.h line 244

Quoted 12 lines
libqblock.h new file +12 -0
+};
+
+struct QBlockOption_vmdk {
+    int struct_size;
+    size_t virt_size;
+    const char *backing_file;
+    int backing_flag;
+    bool compat_version6;
+    const char *subfmt;
+    /* vmdk flat extent format, values:
+   "{monolithicSparse (default) | monolithicFlat | twoGbMaxExtentSparse |
+    twoGbMaxExtentFlat | streamOptimized} */

Here too.

libqblock.h line 410

Quoted 5 lines
libqblock.h new file +5 -0
+/* information */
+/* image related info, static information, from user perspective. */
+/* now it is a plain structure, wonder if it could be foldered into embbed one
+   to reflect that format related information better. */
+struct QBlockInfoImage {

QBlockImageInfo

libqblock.h line 139

Quoted 1 line
libqblock.h new file +1 -0
+    int struct_size;

Here the struct size makes more sense, because it's a returned struct.

libqblock.h line 413

Quoted 2 lines
libqblock.h new file +2 -0
+    char *filename;
+    enum QBlockProtocol protocol;
Quoted 7 lines

Can this just be a struct QBlockLocation * (using a pointer avoids problems with embedded structs)?

libqblock.h line 414

Quoted 1 line
libqblock.h new file +1 -0
+    enum QBlockFormat format;

Can this just be a struct QBlockFormatOption * (same as the above)?

Yes, I tried it before but found that structure alignment is hard to
solve, but as you suggest, I think plain structure plus pointer could make it simpler. Blue Swirl suggest using a API to unify property setting and solve version difference, not using structure, as:
qb_set_property(s, "filename", "c:\\\\autoexec.bat");
What do you think of that way?

Quoted 11 lines
Quoted 11 lines
libqblock.h new file +11 -0
+    size_t virt_size;
+    /* advance info */
+    size_t allocated_size;
+    bool encrypt;
+    char *backing_filename;
+};
+
+/* image info */
+/*
+   get image info.
+   params:
Quoted 9 lines

libqblock.h line 431

Quoted 6 lines
libqblock.h new file +6 -0
+     qbs: in, pointer to QBlockState.
+     info, out, pointer that would receive the information.
+   return:
+     negative on fail, 0 on success.
+ */
+int qb_infoimage_get(struct QBlockState *qbs, struct QBlockInfoImage **info);

qb_get_image_info

OK.

Quoted 24 lines

libqblock.h line 440

Quoted 9 lines
libqblock.h new file +9 -0
+
+/*
+   free image info.
+   params:
+     info, in, pointer, *info would be set to NULL after function called.
+   return:
+     void.
+ */
+void qb_infoimage_free(struct QBlockInfoImage **info);

qb_free_image_info

Paolo

Quoted 8 lines
libqblock.h new file +7 -0
+
+/* misc */
+bool qb_supports_format(enum QBlockFormat fmt);
+bool qb_supports_protocol(enum QBlockProtocol proto);
+
+const char *qb_error_get_detail(struct QBlockState *qbs, int *err_num);
+#endif
Signature · 3 lines
Best Regards

Wenchao Xia
#5 lore raw

Am 13.08.2012 12:27, schrieb Wenchao Xia:

Quoted 8 lines
Quoted 6 lines · Paolo Bonzini

Il 09/08/2012 12:12, Wenchao Xia ha scritto:

libqblock.c line 308

Quoted 2 lines
libqblock.c new file +2 -0
+        assert(0 == set_option_parameter_int(param,
+                                BLOCK_OPT_SIZE, o_cow->virt_size));

Assertions should not have side effects.

what side effects would this line have?

It sets some BLOCK_OPT_SIZE parameter. :)

What Paolo probably meant is to extract that part from the assert():
ret = set_option_...(...);
assert(ret == 0);
eliminating the kraxelism at the same time. ;)
Then you can more safely do, e.g., #define assert(x) do {} while (0)

Regards,
Andreas

Signature · 2 lines
SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
GF: Jeff Hawn, Jennifer Guild, Felix Imendörffer; HRB 16746 AG Nürnberg
#6 lore raw
In reply to Wenchao Xia

Am 09.08.2012 12:12, schrieb Wenchao Xia:

Quoted 9 lines

This patch is the API design.

---
 libqblock.c |  670 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 libqblock.h |  447 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 1117 insertions(+), 0 deletions(-)
 create mode 100644 libqblock.c
 create mode 100644 libqblock.h

Ignoring the implementation for now as the design should be visible in the header file.

Quoted 25 lines
libqblock.h new file +19 -0
new file mode 100644
index 0000000..d2e9502
@@ -0,0 +1,447 @@
+/*
+ * Copyright IBM Corp. 2012
+ *
+ * Authors:
+ *  Wenchao Xia <[email protected]>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA

libqblock.h line 29

Quoted 10 lines
libqblock.h new file +10 -0
+ */
+
+#ifndef LIBQBLOCK_H
+#define LIBQBLOCK_H
+
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+
+#define bool _Bool

Why not use stdbool.h?

libqblock.h line 33

Quoted 4 lines
libqblock.h new file +4 -0
+
+#define QB_ERR_MEM_ERR (-1)
+#define QB_ERR_INTERNAL_ERR (-2)
+#define QB_ERR_INVALID_PARAM (-3)

qemu uses errno values internally, I think they would make sense in the library interface as well.

Quoted 17 lines
libqblock.h new file +17 -0
+
+/* this library is designed around this core struct. */
+struct QBlockState;
+
+/*
+   libarary init
+   This function get the library ready to use.
+ */
+void libqblock_init(void);
+
+/*
+   create a new qbs object
+   params:
+     qbs: out, pointer that will receive created obj.
+   return:
+     0 on succeed, negative on failure.
+ */

libqblock.h line 60

Quoted 10 lines
libqblock.h new file +10 -0
+int qb_state_new(struct QBlockState **qbs);
+
+/*
+   delete a qbs object
+   params:
+     qbs: in, pointer that will be freed. *qbs will be set to NULL.
+   return:
+     void.
+ */
+void qb_state_free(struct QBlockState **qbs);

What happens if the qbs is open? Will it be flushed and closed? If so, can it fail and we need to allow an error return?

Quoted 32 lines
libqblock.h new file +32 -0
+
+/* flag used in open and create */
+#define LIBQBLOCK_O_RDWR        0x0002
+/* open the file read only and save writes in a snapshot */
+#define LIBQBLOCK_O_SNAPSHOT    0x0008
+/* do not use the host page cache */
+#define LIBQBLOCK_O_NOCACHE     0x0020
+/* use write-back caching */
+#define LIBQBLOCK_O_CACHE_WB    0x0040
+/* use native AIO instead of the thread pool */
+#define LIBQBLOCK_O_NATIVE_AIO  0x0080
+/* don't open the backing file */
+#define LIBQBLOCK_O_NO_BACKING  0x0100
+/* disable flushing on this disk */
+#define LIBQBLOCK_O_NO_FLUSH    0x0200
+/* copy read backing sectors into image */
+#define LIBQBLOCK_O_COPY_ON_READ 0x0400
+/* consistency hint for incoming migration */
+#define LIBQBLOCK_O_INCOMING    0x0800
+
+#define LIBQBLOCK_O_CACHE_MASK \
+   (LIBQBLOCK_O_NOCACHE | LIBQBLOCK_O_CACHE_WB | LIBQBLOCK_O_NO_FLUSH)
+
+enum QBlockProtocol {
+    QB_PROTO_FILE = 0,
+    QB_PROTO_MAX
+};
+
+enum QBlockFormat {
+    QB_FMT_NONE = 0,
+    QB_FMT_COW,
+    QB_FMT_QED,

libqblock.h line 103

Quoted 10 lines
libqblock.h new file +10 -0
+    QB_FMT_QCOW,
+    QB_FMT_QCOW2,
+    QB_FMT_RAW,
+    QB_FMT_RBD,
+    QB_FMT_SHEEPDOG,
+    QB_FMT_VDI,
+    QB_FMT_VMDK,
+    QB_FMT_VPC,
+    QB_FMT_MAX
+};

Not sure if this is a good idea with respect to extensibility. Today you only need to create a new block/foo.c and add it to the Makefile in order to add a new format and protocol. It would be better if the library could make use of it without changing these enums, e.g. by referring to formats by string (possibly getting a struct referring to a format by something like qblk_get_format("raw"))

libqblock.h line 111

Quoted 8 lines
libqblock.h new file +8 -0
+
+/* block target location info, it include all information about how to find
+   the image */
+struct QBlockLocInfo {
+    int struct_size;
+    const char *filename;
+    enum QBlockProtocol protocol;
+};

This is a relatively simplistic view of the world. It works okay for local image files (which is probably the most common use case for the library), but you get into trouble as soon as you want to build some less trivial structures, like something including blkdebug.

Eventually, this leads straight into -blockdev discussions, so maybe we should indeed go with a very simplistic approach for the start, but always be aware that a more general solution is to be expected and should be possible to fit in naturally.

Quoted 39 lines
libqblock.h new file +39 -0
+
+/* how to open the image */
+struct QBlockOptionOpen {
+    int struct_size;
+    struct QBlockLocInfo o_loc; /* how to find */
+    enum QBlockFormat o_fmt_type; /* how to extract */
+    int o_flag; /* how to control */
+};
+
+/*
+   create a new QBlockOptionOpen structure.
+   params:
+     op: out, pointer that will receive created structure.
+   return:
+     0 on succeed, negative on failure.
+ */
+static inline int qb_oo_new(struct QBlockOptionOpen **op)
+{
+    *op = calloc(1, sizeof(struct QBlockOptionOpen));
+    if (*op == NULL) {
+        return QB_ERR_MEM_ERR;
+    }
+    (*op)->struct_size = sizeof(struct QBlockOptionOpen);
+    (*op)->o_loc.struct_size = sizeof(struct QBlockLocInfo);
+    return 0;
+}
+
+/*
+   free QBlockOptionOpen structure.
+   params:
+     op: in, *op will be set as NULL after called.
+   return:
+     void
+ */
+static inline void qb_oo_free(struct QBlockOptionOpen **op)
+{
+    free(*op);
+    *op = NULL;
+}

libqblock.h line 160

Quoted 10 lines
libqblock.h new file +10 -0
+
+/*
+   open a block object.
+   params:
+     qbs: in, pointer to QBlockState.
+     op: in, options for open.
+   return:
+     0 on success, negative on fail.
+ */
+int qb_open(struct QBlockState *qbs, struct QBlockOptionOpen *op);

No further explanation on what negative values there are allowed? In practice I guess -errno, but will it be officially undefined?

Will there be a function that allows getting an error message if it has failed?

libqblock.h line 169

Quoted 9 lines
libqblock.h new file +9 -0
+
+/*
+   close a block object.
+   params:
+     qbs: in, pointer to QBlockState.
+   return:
+     void.
+ */
+void qb_close(struct QBlockState *qbs);

Does it imply a flush? Do we need an error return then?

Quoted 72 lines
libqblock.h new file +72 -0
+
+/* format related options, struct_size must be set. */
+struct QBlockOption_cow {
+    int struct_size;
+    size_t virt_size;
+    const char *backing_file;
+    int backing_flag;
+};
+
+struct QBlockOption_qed {
+    int struct_size;
+    size_t virt_size;
+    const char *backing_file;
+    enum QBlockFormat backing_fmt;
+    int backing_flag;
+    size_t cluster_size; /* unit is bytes */
+    size_t table_size; /* unit is clusters */
+};
+
+struct QBlockOption_qcow {
+    int struct_size;
+    size_t virt_size;
+    const char *backing_file;
+    int backing_flag;
+    bool encrypt;
+};
+
+struct QBlockOption_qcow2 {
+    int struct_size;
+    size_t virt_size;
+    const char *compat_level; /* "Compatibility level (0.10 or 1.1)" */
+    const char *backing_file;
+    enum QBlockFormat backing_fmt;
+    int backing_flag;
+    bool encrypt;
+    size_t cluster_size; /* unit is bytes */
+    const char *prealloc_mode; /* off or metadata */
+};
+
+struct QBlockOption_raw {
+    int struct_size;
+    size_t virt_size;
+};
+
+struct QBlockOption_rbd {
+    int struct_size;
+    size_t virt_size;
+    size_t cluster_size;
+};
+
+struct QBlockOption_sheepdog {
+    int struct_size;
+    size_t virt_size;
+    const char *backing_file;
+    int backing_flag;
+    const char *prealloc_mode; /* off or full */
+};
+
+struct QBlockOption_vdi {
+    int struct_size;
+    size_t virt_size;
+    size_t cluster_size;
+    bool prealloc_mode;
+};
+
+struct QBlockOption_vmdk {
+    int struct_size;
+    size_t virt_size;
+    const char *backing_file;
+    int backing_flag;
+    bool compat_version6;
+    const char *subfmt;

libqblock.h line 251

Quoted 10 lines
libqblock.h new file +10 -0
+    /* vmdk flat extent format, values:
+   "{monolithicSparse (default) | monolithicFlat | twoGbMaxExtentSparse |
+    twoGbMaxExtentFlat | streamOptimized} */
+};
+
+struct QBlockOption_vpc {
+    int struct_size;
+    size_t virt_size;
+    const char *subfmt; /* "{dynamic (default) | fixed} " */
+};

Ouch. :-)

But maybe it's the best we can do...

Quoted 14 lines
libqblock.h new file +14 -0
+
+union QBlockOption_fmt {
+    struct QBlockOption_cow       o_cow;
+    struct QBlockOption_qed       o_qed;
+    struct QBlockOption_qcow      o_qcow;
+    struct QBlockOption_qcow2     o_qcow2;
+    struct QBlockOption_raw       o_raw;
+    struct QBlockOption_rbd       o_rbd;
+    struct QBlockOption_sheepdog  o_sheepdog;
+    struct QBlockOption_vdi       o_vdi;
+    struct QBlockOption_vmdk      o_vmdk;
+    struct QBlockOption_vpc       o_vpc;
+};
+

libqblock.h line 275

Quoted 10 lines
libqblock.h new file +10 -0
+struct QBlockOptionFormat {
+    int struct_size;
+    enum QBlockFormat fmt_type;
+    union QBlockOption_fmt fmt_op;
+};
+
+/* struct_size in o_loc and o_fmt must set. To make this structure extensible,
+   all new member must be added in the tail of each structure. */
+struct QBlockOptionCreate {
+    int struct_size;

size_t?

Quoted 135 lines
libqblock.h new file +135 -0
+    struct QBlockLocInfo o_loc;
+    struct QBlockOptionFormat o_fmt;
+};
+
+/*
+   create a new QBlockOptionCreate structure.
+   params:
+     op: out, pointer that will receive created structure.
+     fmt: format want to use.
+   return:
+     0 on succeed, negative on failure.
+ */
+static inline int qb_oc_new(struct QBlockOptionCreate **op,
+                            enum QBlockFormat fmt)
+{
+    *op = calloc(1, sizeof(struct QBlockOptionCreate));
+    if (*op == NULL) {
+        return QB_ERR_MEM_ERR;
+    }
+    (*op)->struct_size = sizeof(struct QBlockOptionCreate);
+    (*op)->o_loc.struct_size = sizeof(struct QBlockLocInfo);
+    (*op)->o_fmt.struct_size = sizeof(struct QBlockOptionFormat);
+
+    switch (fmt) {
+    case QB_FMT_COW:
+        (*op)->o_fmt.fmt_op.o_cow.struct_size =
+                                          sizeof(struct QBlockOption_cow);
+        break;
+    case QB_FMT_QED:
+        (*op)->o_fmt.fmt_op.o_qed.struct_size =
+                                          sizeof(struct QBlockOption_qed);
+        break;
+    case QB_FMT_QCOW:
+        (*op)->o_fmt.fmt_op.o_qcow.struct_size =
+                                          sizeof(struct QBlockOption_qcow);
+        break;
+    case QB_FMT_QCOW2:
+        (*op)->o_fmt.fmt_op.o_qcow2.struct_size =
+                                          sizeof(struct QBlockOption_qcow2);
+        break;
+    case QB_FMT_RAW:
+        (*op)->o_fmt.fmt_op.o_raw.struct_size =
+                                          sizeof(struct QBlockOption_raw);
+        break;
+    case QB_FMT_RBD:
+        (*op)->o_fmt.fmt_op.o_rbd.struct_size =
+                                          sizeof(struct QBlockOption_rbd);
+        break;
+    case QB_FMT_SHEEPDOG:
+        (*op)->o_fmt.fmt_op.o_sheepdog.struct_size =
+                                          sizeof(struct QBlockOption_sheepdog);
+        break;
+    case QB_FMT_VDI:
+        (*op)->o_fmt.fmt_op.o_vdi.struct_size =
+                                          sizeof(struct QBlockOption_vdi);
+        break;
+    case QB_FMT_VMDK:
+        (*op)->o_fmt.fmt_op.o_vmdk.struct_size =
+                                          sizeof(struct QBlockOption_vmdk);
+        break;
+    case QB_FMT_VPC:
+        (*op)->o_fmt.fmt_op.o_vpc.struct_size =
+                                          sizeof(struct QBlockOption_vpc);
+        break;
+    default:
+        break;
+    }
+    (*op)->o_fmt.fmt_type = fmt;
+    return 0;
+}
+
+/*
+   free QBlockOptionCreate structure.
+   params:
+     op: in, *op will be set as NULL after called.
+   return:
+     void
+ */
+static inline void qb_oc_free(struct QBlockOptionCreate **op)
+{
+    free(*op);
+    *op = NULL;
+}
+/*
+   create a block file.
+   params:
+     qbs: in, pointer to QBlockState.
+     op: in, create option.
+   return:
+     negative on fail, 0 on success.
+ */
+int qb_create(struct QBlockState *qbs, struct QBlockOptionCreate *op);
+
+
+/* sync access */
+/*
+   block sync read.
+   params:
+     qbs: in, pointer to QBlockState.
+     buf: out, buffer that receive the content.
+     len: in, length to read.
+     offset: in, offset in the block data.
+   return:
+     negative on fail, 0 on success.
+ */
+int qb_read(struct QBlockState *qbs, const void *buf, size_t len,
+                                                      off_t offset);
+
+/*
+   block sync read.
+   params:
+     qbs: in, pointer to QBlockState.
+     buf: in, buffer that would be wrote.
+     len: in, length to write.
+     offset: in, offset in the block data.
+   return:
+     negative on fail, 0 on success.
+ */
+int qb_write(struct QBlockState *qbs, const void *buf, size_t len,
+                                                       off_t offset);
+
+/*
+   block sync flush.
+   params:
+     qbs: in, pointer to QBlockState.
+   return:
+     negative on fail, 0 on success.
+ */
+int qb_flush(struct QBlockState *qbs);
+
+/* information */
+/* image related info, static information, from user perspective. */
+/* now it is a plain structure, wonder if it could be foldered into embbed one
+   to reflect that format related information better. */
+struct QBlockInfoImage {

libqblock.h line 420

Quoted 10 lines
libqblock.h new file +10 -0
+    int struct_size;
+    char *filename;
+    enum QBlockProtocol protocol;
+    enum QBlockFormat format;
+    size_t virt_size;
+    /* advance info */
+    size_t allocated_size;
+    bool encrypt;
+    char *backing_filename;
+};

What about things like cluster size, dirty state etc. that only some image formats have?

Quoted 16 lines
libqblock.h new file +16 -0
+
+/* image info */
+/*
+   get image info.
+   params:
+     qbs: in, pointer to QBlockState.
+     info, out, pointer that would receive the information.
+   return:
+     negative on fail, 0 on success.
+ */
+int qb_infoimage_get(struct QBlockState *qbs, struct QBlockInfoImage **info);
+
+/*
+   free image info.
+   params:
+     info, in, pointer, *info would be set to NULL after function called.

libqblock.h line 446

Quoted 10 lines
libqblock.h new file +10 -0
+   return:
+     void.
+ */
+void qb_infoimage_free(struct QBlockInfoImage **info);
+
+/* misc */
+bool qb_supports_format(enum QBlockFormat fmt);
+bool qb_supports_protocol(enum QBlockProtocol proto);
+
+const char *qb_error_get_detail(struct QBlockState *qbs, int *err_num);

This needs a more detailed description. I also think that you'll want to add a const char** parameter for human readable error messages (as printed with qerror_report or error_set in qemu).

Kevin

#7 lore raw
Quoted 43 lines · Kevin Wolf

Am 09.08.2012 12:12, schrieb Wenchao Xia:

Quoted 9 lines

This patch is the API design.

---
  libqblock.c |  670 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  libqblock.h |  447 +++++++++++++++++++++++++++++++++++++++
  2 files changed, 1117 insertions(+), 0 deletions(-)

create mode 100644 libqblock.c
create mode 100644 libqblock.h

Ignoring the implementation for now as the design should be visible in the header file.

Quoted 29 lines
libqblock.h new file +23 -0
new file mode 100644
index 0000000..d2e9502
@@ -0,0 +1,447 @@
+/*
+ * Copyright IBM Corp. 2012
+ *
+ * Authors:
+ *  Wenchao Xia <[email protected]>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
+ */
+
+#ifndef LIBQBLOCK_H
+#define LIBQBLOCK_H
Quoted 9 lines

libqblock.h line 29

Quoted 6 lines
libqblock.h new file +6 -0
+
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+
+#define bool _Bool

Why not use stdbool.h?

forgot to use that, will change it, thanks.

Quoted 30 lines

libqblock.h line 33

Quoted 4 lines
libqblock.h new file +4 -0
+
+#define QB_ERR_MEM_ERR (-1)
+#define QB_ERR_INTERNAL_ERR (-2)
+#define QB_ERR_INVALID_PARAM (-3)

qemu uses errno values internally, I think they would make sense in the library interface as well.

Quoted 22 lines
libqblock.h new file +22 -0
+
+/* this library is designed around this core struct. */
+struct QBlockState;
+
+/*
+   libarary init
+   This function get the library ready to use.
+ */
+void libqblock_init(void);
+
+/*
+   create a new qbs object
+   params:
+     qbs: out, pointer that will receive created obj.
+   return:
+     0 on succeed, negative on failure.
+ */
+int qb_state_new(struct QBlockState **qbs);
+
+/*
+   delete a qbs object
+   params:
Quoted 9 lines

libqblock.h line 60

Quoted 5 lines
libqblock.h new file +5 -0
+     qbs: in, pointer that will be freed. *qbs will be set to NULL.
+   return:
+     void.
+ */
+void qb_state_free(struct QBlockState **qbs);

What happens if the qbs is open? Will it be flushed and closed? If so, can it fail and we need to allow an error return?

user should call qb_close() if he have called qb_open(), other wise
unexpected thing happens such as this file is not flushed. I need document this in the comments.

Quoted 41 lines
Quoted 41 lines
libqblock.h new file +41 -0
+
+/* flag used in open and create */
+#define LIBQBLOCK_O_RDWR        0x0002
+/* open the file read only and save writes in a snapshot */
+#define LIBQBLOCK_O_SNAPSHOT    0x0008
+/* do not use the host page cache */
+#define LIBQBLOCK_O_NOCACHE     0x0020
+/* use write-back caching */
+#define LIBQBLOCK_O_CACHE_WB    0x0040
+/* use native AIO instead of the thread pool */
+#define LIBQBLOCK_O_NATIVE_AIO  0x0080
+/* don't open the backing file */
+#define LIBQBLOCK_O_NO_BACKING  0x0100
+/* disable flushing on this disk */
+#define LIBQBLOCK_O_NO_FLUSH    0x0200
+/* copy read backing sectors into image */
+#define LIBQBLOCK_O_COPY_ON_READ 0x0400
+/* consistency hint for incoming migration */
+#define LIBQBLOCK_O_INCOMING    0x0800
+
+#define LIBQBLOCK_O_CACHE_MASK \
+   (LIBQBLOCK_O_NOCACHE | LIBQBLOCK_O_CACHE_WB | LIBQBLOCK_O_NO_FLUSH)
+
+enum QBlockProtocol {
+    QB_PROTO_FILE = 0,
+    QB_PROTO_MAX
+};
+
+enum QBlockFormat {
+    QB_FMT_NONE = 0,
+    QB_FMT_COW,
+    QB_FMT_QED,
+    QB_FMT_QCOW,
+    QB_FMT_QCOW2,
+    QB_FMT_RAW,
+    QB_FMT_RBD,
+    QB_FMT_SHEEPDOG,
+    QB_FMT_VDI,
+    QB_FMT_VMDK,
+    QB_FMT_VPC,
+    QB_FMT_MAX
Quoted 9 lines

libqblock.h line 102

Quoted 1 line
libqblock.h new file +1 -0
+};

Not sure if this is a good idea with respect to extensibility. Today you only need to create a new block/foo.c and add it to the Makefile in order to add a new format and protocol. It would be better if the library could make use of it without changing these enums, e.g. by referring to formats by string (possibly getting a struct referring to a format by something like qblk_get_format("raw"))

qblk_get_format("raw") seems a good way solve the option difference,
maybe user call it as:
void *op = qblk_get_format_option("raw");
struct QBlockOption_raw *o_raw = (struct QBlockOption_raw *)op; o_raw.virt_size = 16 * 1024;

I think this is good, with the cost of user have to deal string
instead of simpler enum integer.

Quoted 14 lines

libqblock.h line 111

Quoted 8 lines
libqblock.h new file +8 -0
+
+/* block target location info, it include all information about how to find
+   the image */
+struct QBlockLocInfo {
+    int struct_size;
+    const char *filename;
+    enum QBlockProtocol protocol;
+};

This is a relatively simplistic view of the world. It works okay for local image files (which is probably the most common use case for the library), but you get into trouble as soon as you want to build some less trivial structures, like something including blkdebug.

Quoted 5 lines

Eventually, this leads straight into -blockdev discussions, so maybe we should indeed go with a very simplistic approach for the start, but always be aware that a more general solution is to be expected and should be possible to fit in naturally.

if blkdebug is used for debug block I/O, maybe we can sort it into
another protocol.

Quoted 53 lines
Quoted 39 lines
libqblock.h new file +39 -0
+
+/* how to open the image */
+struct QBlockOptionOpen {
+    int struct_size;
+    struct QBlockLocInfo o_loc; /* how to find */
+    enum QBlockFormat o_fmt_type; /* how to extract */
+    int o_flag; /* how to control */
+};
+
+/*
+   create a new QBlockOptionOpen structure.
+   params:
+     op: out, pointer that will receive created structure.
+   return:
+     0 on succeed, negative on failure.
+ */
+static inline int qb_oo_new(struct QBlockOptionOpen **op)
+{
+    *op = calloc(1, sizeof(struct QBlockOptionOpen));
+    if (*op == NULL) {
+        return QB_ERR_MEM_ERR;
+    }
+    (*op)->struct_size = sizeof(struct QBlockOptionOpen);
+    (*op)->o_loc.struct_size = sizeof(struct QBlockLocInfo);
+    return 0;
+}
+
+/*
+   free QBlockOptionOpen structure.
+   params:
+     op: in, *op will be set as NULL after called.
+   return:
+     void
+ */
+static inline void qb_oo_free(struct QBlockOptionOpen **op)
+{
+    free(*op);
+    *op = NULL;
+}

libqblock.h line 160

Quoted 10 lines
libqblock.h new file +10 -0
+
+/*
+   open a block object.
+   params:
+     qbs: in, pointer to QBlockState.
+     op: in, options for open.
+   return:
+     0 on success, negative on fail.
+ */
+int qb_open(struct QBlockState *qbs, struct QBlockOptionOpen *op);

No further explanation on what negative values there are allowed? In practice I guess -errno, but will it be officially undefined?

Quoted 3 lines

Will there be a function that allows getting an error message if it has failed?

Yes, there is a API doint that.

Quoted 12 lines

libqblock.h line 169

Quoted 9 lines
libqblock.h new file +9 -0
+
+/*
+   close a block object.
+   params:
+     qbs: in, pointer to QBlockState.
+   return:
+     void.
+ */
+void qb_close(struct QBlockState *qbs);

Does it imply a flush? Do we need an error return then?

No flush now, will flush and document it next time.

Quoted 85 lines
Quoted 72 lines
libqblock.h new file +72 -0
+
+/* format related options, struct_size must be set. */
+struct QBlockOption_cow {
+    int struct_size;
+    size_t virt_size;
+    const char *backing_file;
+    int backing_flag;
+};
+
+struct QBlockOption_qed {
+    int struct_size;
+    size_t virt_size;
+    const char *backing_file;
+    enum QBlockFormat backing_fmt;
+    int backing_flag;
+    size_t cluster_size; /* unit is bytes */
+    size_t table_size; /* unit is clusters */
+};
+
+struct QBlockOption_qcow {
+    int struct_size;
+    size_t virt_size;
+    const char *backing_file;
+    int backing_flag;
+    bool encrypt;
+};
+
+struct QBlockOption_qcow2 {
+    int struct_size;
+    size_t virt_size;
+    const char *compat_level; /* "Compatibility level (0.10 or 1.1)" */
+    const char *backing_file;
+    enum QBlockFormat backing_fmt;
+    int backing_flag;
+    bool encrypt;
+    size_t cluster_size; /* unit is bytes */
+    const char *prealloc_mode; /* off or metadata */
+};
+
+struct QBlockOption_raw {
+    int struct_size;
+    size_t virt_size;
+};
+
+struct QBlockOption_rbd {
+    int struct_size;
+    size_t virt_size;
+    size_t cluster_size;
+};
+
+struct QBlockOption_sheepdog {
+    int struct_size;
+    size_t virt_size;
+    const char *backing_file;
+    int backing_flag;
+    const char *prealloc_mode; /* off or full */
+};
+
+struct QBlockOption_vdi {
+    int struct_size;
+    size_t virt_size;
+    size_t cluster_size;
+    bool prealloc_mode;
+};
+
+struct QBlockOption_vmdk {
+    int struct_size;
+    size_t virt_size;
+    const char *backing_file;
+    int backing_flag;
+    bool compat_version6;
+    const char *subfmt;

libqblock.h line 251

Quoted 10 lines
libqblock.h new file +10 -0
+    /* vmdk flat extent format, values:
+   "{monolithicSparse (default) | monolithicFlat | twoGbMaxExtentSparse |
+    twoGbMaxExtentFlat | streamOptimized} */
+};
+
+struct QBlockOption_vpc {
+    int struct_size;
+    size_t virt_size;
+    const char *subfmt; /* "{dynamic (default) | fixed} " */
+};

Ouch. :-)

Quoted 2 lines

But maybe it's the best we can do...

This structure brought format specified options to the surface,
user could understand them easier and simply give values to the members, so this is a friendly API. But with a union implement became hard, I think the way your suggested as qblk_format_option_get("raw")
could make thing easier.

Quoted 18 lines
Quoted 18 lines
libqblock.h new file +18 -0
+
+union QBlockOption_fmt {
+    struct QBlockOption_cow       o_cow;
+    struct QBlockOption_qed       o_qed;
+    struct QBlockOption_qcow      o_qcow;
+    struct QBlockOption_qcow2     o_qcow2;
+    struct QBlockOption_raw       o_raw;
+    struct QBlockOption_rbd       o_rbd;
+    struct QBlockOption_sheepdog  o_sheepdog;
+    struct QBlockOption_vdi       o_vdi;
+    struct QBlockOption_vmdk      o_vmdk;
+    struct QBlockOption_vpc       o_vpc;
+};
+
+struct QBlockOptionFormat {
+    int struct_size;
+    enum QBlockFormat fmt_type;
+    union QBlockOption_fmt fmt_op;
Quoted 9 lines

libqblock.h line 275

Quoted 6 lines
libqblock.h new file +6 -0
+};
+
+/* struct_size in o_loc and o_fmt must set. To make this structure extensible,
+   all new member must be added in the tail of each structure. */
+struct QBlockOptionCreate {
+    int struct_size;

size_t?

struct_size will not be big, should I also use size_t?

Quoted 63 lines
Quoted 63 lines
libqblock.h new file +63 -0
+    struct QBlockLocInfo o_loc;
+    struct QBlockOptionFormat o_fmt;
+};
+
+/*
+   create a new QBlockOptionCreate structure.
+   params:
+     op: out, pointer that will receive created structure.
+     fmt: format want to use.
+   return:
+     0 on succeed, negative on failure.
+ */
+static inline int qb_oc_new(struct QBlockOptionCreate **op,
+                            enum QBlockFormat fmt)
+{
+    *op = calloc(1, sizeof(struct QBlockOptionCreate));
+    if (*op == NULL) {
+        return QB_ERR_MEM_ERR;
+    }
+    (*op)->struct_size = sizeof(struct QBlockOptionCreate);
+    (*op)->o_loc.struct_size = sizeof(struct QBlockLocInfo);
+    (*op)->o_fmt.struct_size = sizeof(struct QBlockOptionFormat);
+
+    switch (fmt) {
+    case QB_FMT_COW:
+        (*op)->o_fmt.fmt_op.o_cow.struct_size =
+                                          sizeof(struct QBlockOption_cow);
+        break;
+    case QB_FMT_QED:
+        (*op)->o_fmt.fmt_op.o_qed.struct_size =
+                                          sizeof(struct QBlockOption_qed);
+        break;
+    case QB_FMT_QCOW:
+        (*op)->o_fmt.fmt_op.o_qcow.struct_size =
+                                          sizeof(struct QBlockOption_qcow);
+        break;
+    case QB_FMT_QCOW2:
+        (*op)->o_fmt.fmt_op.o_qcow2.struct_size =
+                                          sizeof(struct QBlockOption_qcow2);
+        break;
+    case QB_FMT_RAW:
+        (*op)->o_fmt.fmt_op.o_raw.struct_size =
+                                          sizeof(struct QBlockOption_raw);
+        break;
+    case QB_FMT_RBD:
+        (*op)->o_fmt.fmt_op.o_rbd.struct_size =
+                                          sizeof(struct QBlockOption_rbd);
+        break;
+    case QB_FMT_SHEEPDOG:
+        (*op)->o_fmt.fmt_op.o_sheepdog.struct_size =
+                                          sizeof(struct QBlockOption_sheepdog);
+        break;
+    case QB_FMT_VDI:
+        (*op)->o_fmt.fmt_op.o_vdi.struct_size =
+                                          sizeof(struct QBlockOption_vdi);
+        break;
+    case QB_FMT_VMDK:
+        (*op)->o_fmt.fmt_op.o_vmdk.struct_size =
+                                          sizeof(struct QBlockOption_vmdk);
+        break;
+    case QB_FMT_VPC:
+        (*op)->o_fmt.fmt_op.o_vpc.struct_size =
+                                          sizeof(struct QBlockOption_vpc);
Quoted 8 lines

libqblock.h line 346

Quoted 8 lines
libqblock.h new file +8 -0
+        break;
+    default:
+        break;
+    }
+    (*op)->o_fmt.fmt_type = fmt;
+    return 0;
+}
+

This is what make implement hard, with format union, in order to
form a friendly API.

Quoted 69 lines
Quoted 69 lines
libqblock.h new file +69 -0
+/*
+   free QBlockOptionCreate structure.
+   params:
+     op: in, *op will be set as NULL after called.
+   return:
+     void
+ */
+static inline void qb_oc_free(struct QBlockOptionCreate **op)
+{
+    free(*op);
+    *op = NULL;
+}
+/*
+   create a block file.
+   params:
+     qbs: in, pointer to QBlockState.
+     op: in, create option.
+   return:
+     negative on fail, 0 on success.
+ */
+int qb_create(struct QBlockState *qbs, struct QBlockOptionCreate *op);
+
+
+/* sync access */
+/*
+   block sync read.
+   params:
+     qbs: in, pointer to QBlockState.
+     buf: out, buffer that receive the content.
+     len: in, length to read.
+     offset: in, offset in the block data.
+   return:
+     negative on fail, 0 on success.
+ */
+int qb_read(struct QBlockState *qbs, const void *buf, size_t len,
+                                                      off_t offset);
+
+/*
+   block sync read.
+   params:
+     qbs: in, pointer to QBlockState.
+     buf: in, buffer that would be wrote.
+     len: in, length to write.
+     offset: in, offset in the block data.
+   return:
+     negative on fail, 0 on success.
+ */
+int qb_write(struct QBlockState *qbs, const void *buf, size_t len,
+                                                       off_t offset);
+
+/*
+   block sync flush.
+   params:
+     qbs: in, pointer to QBlockState.
+   return:
+     negative on fail, 0 on success.
+ */
+int qb_flush(struct QBlockState *qbs);
+
+/* information */
+/* image related info, static information, from user perspective. */
+/* now it is a plain structure, wonder if it could be foldered into embbed one
+   to reflect that format related information better. */
+struct QBlockInfoImage {
+    int struct_size;
+    char *filename;
+    enum QBlockProtocol protocol;
+    enum QBlockFormat format;
+    size_t virt_size;
Quoted 9 lines

libqblock.h line 420

Quoted 5 lines
libqblock.h new file +5 -0
+    /* advance info */
+    size_t allocated_size;
+    bool encrypt;
+    char *backing_filename;
+};

What about things like cluster size, dirty state etc. that only some image formats have?

hard to resolve, enum plus sub format structure seems reasonable. My
plan is using embbed structure:
struct QBlockInfoImage {
int struct_size;
struct QBlockInfoLoc *i_loc;
struct QBlockInfoFormat *i_fmt;
};
with format string user got, user do a cast to specified format info.

Quoted 22 lines
Quoted 22 lines
libqblock.h new file +22 -0
+
+/* image info */
+/*
+   get image info.
+   params:
+     qbs: in, pointer to QBlockState.
+     info, out, pointer that would receive the information.
+   return:
+     negative on fail, 0 on success.
+ */
+int qb_infoimage_get(struct QBlockState *qbs, struct QBlockInfoImage **info);
+
+/*
+   free image info.
+   params:
+     info, in, pointer, *info would be set to NULL after function called.
+   return:
+     void.
+ */
+void qb_infoimage_free(struct QBlockInfoImage **info);
+
+/* misc */
Quoted 9 lines

libqblock.h line 446

Quoted 4 lines
libqblock.h new file +4 -0
+bool qb_supports_format(enum QBlockFormat fmt);
+bool qb_supports_protocol(enum QBlockProtocol proto);
+
+const char *qb_error_get_detail(struct QBlockState *qbs, int *err_num);

This needs a more detailed description. I also think that you'll want to add a const char** parameter for human readable error messages (as printed with qerror_report or error_set in qemu).

This API try to return human readable strings. Will add document.

Quoted 2 lines

Kevin

Signature · 3 lines
Best Regards

Wenchao Xia
#8 lore raw

[Qemu-devel] [RFC] libqblock-API v2, re-designed

In reply to Kevin Wolf

Hi,
according to the comments, the header files were adjusted as
following:
1 info are sorted into two kinds of structure: location and format,
which are both union + structure type.
2 removed size_struct. Although it is suggested to use pointer to
make embbed struct easier, but with union it requires memory allocated at create time, for eg:

struct QBlockOption_fmt_qed {
    size_t virt_size;
    struct QBlockOptionLoc *backing_loc;
};
union QBlockOption_fmt {
    struct QBlockOption_fmt_cow       o_cow;
    struct QBlockOption_fmt_qed       o_qed;
};

when user create union QBlockOption_fmt, he must set format type
first so we can malloc that memory regions to the pointer. This also bring risk that at free time the format type is also need to be set correctly to free the correct pointer. So this version using reserved space and the structures are created with xx_new functions internally.
3 Balanced the parameters extension and folder depth, More than one
parameters were used in APIs.
4 advanced information that only some format have, will get -1 to
indicate it do not exist.

/*
* QEMU block layer library
*
* Copyright IBM, Corp. 2012
*
* Authors:
* Wenchao Xia <[email protected]>
*
* This work is licensed under the terms of the GNU LGPL, version 2 or
later.
* See the COPYING.LIB file in the top-level directory.
*
*/

#ifndef LIBQBLOCK_H
#define LIBQBLOCK_H

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdbool.h>

#define QB_ERR_MEM_ERR (-1)
#define QB_ERR_INTERNAL_ERR (-2)
#define QB_ERR_INVALID_PARAM (-3)

/* this library is designed around this core struct. */ struct QBlockState;

/**
* libqblock_init: Initialize the library
*/
void libqblock_init(void);

/**
* qb_state_new: allocate a new QBloctState struct *
* Following qblock action were based on this struct *
* Returns 0 if succeed, negative value on fail. *
* @qbs: used to receive the created struct. */
int qb_state_new(struct QBlockState **qbs);

/**
* qb_state_free: free a QBloctState struct
*
* if it is opened, a qb_close must be called before free. *
* @qbs: pointer to the struct's pointer.
*/
void qb_state_free(struct QBlockState **qbs);

/* flag used in open and create */
#define LIBQBLOCK_O_RDWR 0x0002
/* do not use the host page cache */
#define LIBQBLOCK_O_NOCACHE 0x0020
/* use write-back caching */
#define LIBQBLOCK_O_CACHE_WB 0x0040
/* don't open the backing file */
#define LIBQBLOCK_O_NO_BACKING 0x0100
/* disable flushing on this disk */
#define LIBQBLOCK_O_NO_FLUSH 0x0200

#define LIBQBLOCK_O_CACHE_MASK \
(LIBQBLOCK_O_NOCACHE | LIBQBLOCK_O_CACHE_WB | LIBQBLOCK_O_NO_FLUSH)

#define LIBQBLOCK_O_VALID_MASK \
(LIBQBLOCK_O_RDWR | LIBQBLOCK_O_NOCACHE | LIBQBLOCK_O_CACHE_WB | \
LIBQBLOCK_O_NO_BACKING | LIBQBLOCK_O_NO_FLUSH)

enum QBlockProtocol {
    QB_PROTO_FILE = 0,
    QB_PROTO_MAX
};
enum QBlockFormat {
    QB_FMT_NONE = 0,
    QB_FMT_COW,
    QB_FMT_QED,
    QB_FMT_QCOW,
    QB_FMT_QCOW2,
    QB_FMT_RAW,
    QB_FMT_RBD,
    QB_FMT_SHEEPDOG,
    QB_FMT_VDI,
    QB_FMT_VMDK,
    QB_FMT_VPC,
    QB_FMT_MAX
};
struct QBlockOption_prot_file {
    char *filename;
};
union QBlockOption_prot {
    struct QBlockOption_prot_file       o_file;
};

/**
* struct QBlockOptionLoc: contains information about how to find the image *
* @prot_type: protocol type, now only support FILE.
* @prot_op: protocol related options.
*/

struct QBlockOptionLoc {
    enum QBlockProtocol prot_type;
    union QBlockOption_prot prot_op;
    uint8 reserved[512];
};

/**
* qb_ol_new: create a new struct QBlockOptionLoc. *
* return 0 on success, negative on fail. *
* @op: pointer to receive the new created one. */
int qb_ol_new(struct QBlockOptionLoc **op);

/**
* qb_ol_free: free a struct QBlockOptionLoc.
*
* @op: pointer to the object, *op would be set to NULL. */
void qb_ol_free(struct QBlockOptionLoc **op);

/* format related options */

struct QBlockOption_fmt_cow {
    size_t virt_size;
    struct QBlockOptionLoc backing_loc;
};
struct QBlockOption_fmt_qed {
    size_t virt_size;
    struct QBlockOptionLoc backing_loc;
    enum QBlockFormat backing_fmt;
    size_t cluster_size; /* unit is bytes */
    size_t table_size; /* unit is clusters */
};
struct QBlockOption_fmt_qcow {
    size_t virt_size;
    struct QBlockOptionLoc backing_loc;
    bool encrypt;
};

/* "Compatibility level (0.10 or 1.1)" */

enum QBlockOption_fmt_qcow2_cpt {
    QBO_FMT_QCOW2_CPT_None = 0,
    QBO_FMT_QCOW2_CPT_V010,
    QBO_FMT_QCOW2_CPT_V011,
};

/* off or metadata */

enum QBlockOption_fmt_qcow2_prealloc {
    QBO_FMT_QCOW2_PREALLOC_OFF = 0,
    QBO_FMT_QCOW2_PREALLOC_METADATA,
};
struct QBlockOption_fmt_qcow2 {
    size_t virt_size;
    struct QBlockOptionLoc backing_loc;
    enum QBlockFormat backing_fmt;
    bool encrypt;
    size_t cluster_size; /* unit is bytes */
    enum QBlockOption_fmt_qcow2_cpt cpt_lv;
    enum QBlockOption_fmt_qcow2_prealloc pre_mode;
};
struct QBlockOption_fmt_raw {
    size_t virt_size;
};
struct QBlockOption_fmt_rbd {
    size_t virt_size;
    size_t cluster_size;
};

/* off or full */
enum QBlockOption_fmt_sheepdog_prealloc {
QBO_FMT_SD_PREALLOC_OFF = 0,
QBO_FMT_SD_PREALLOC_FULL,
};

struct QBlockOption_fmt_sheepdog {
    size_t virt_size;
    struct QBlockOptionLoc backing_loc;
    enum QBlockOption_fmt_sheepdog_prealloc pre_mode;
};

enum QBlockOption_fmt_vdi_prealloc {
QBO_FMT_VDI_PREALLOC_FALSE = 0,
QBO_FMT_VDI_PREALLOC_TRUE,
};

struct QBlockOption_fmt_vdi {
    size_t virt_size;
    size_t cluster_size;
    enum QBlockOption_fmt_vdi_prealloc pre_mode;
};

/* whether compact to vmdk verion 6 */

enum QBlockOption_fmt_vmdk_cpt {
    QBO_FMT_VMDK_CPT_NONE = 0,
    QBO_FMT_VMDK_CPT_VMDKV6,
};

/* vmdk flat extent format, values:
"{monolithicSparse (default) | monolithicFlat | twoGbMaxExtentSparse | twoGbMaxExtentFlat | streamOptimized} */
enum QBlockOption_fmt_vmdk_subfmt {
QBO_FMT_VMDK_SUBFMT_MONOLITHIC_SPARSE = 0, QBO_FMT_VMDK_SUBFMT_MONOLITHIC_FLAT, QBO_FMT_VMDK_SUBFMT_TWOGBMAX_EXTENT_SPARSE, QBO_FMT_VMDK_SUBFMT_TWOGBMAX_EXTENT_FLAT, QBO_FMT_VMDK_SUBFMT_STREAM_OPTIMIZED,
};

struct QBlockOption_fmt_vmdk {
    size_t virt_size;
    struct QBlockOptionLoc backing_loc;
    enum QBlockOption_fmt_vmdk_cpt cpt_lv;
    enum QBlockOption_fmt_vmdk_subfmt subfmt;
};

/* "{dynamic (default) | fixed} " */
enum QBlockOption_fmt_vpc_subfmt {
QBO_FMT_VPC_SUBFMT_DYNAMIC = 0,
QBO_FMT_VPC_SUBFMT_FIXED,
};

struct QBlockOption_fmt_vpc {
    size_t virt_size;
    enum QBlockOption_fmt_vpc_subfmt subfmt;
};
union QBlockOption_fmt {
    struct QBlockOption_fmt_cow       o_cow;
    struct QBlockOption_fmt_qed       o_qed;
    struct QBlockOption_fmt_qcow      o_qcow;
    struct QBlockOption_fmt_qcow2     o_qcow2;
    struct QBlockOption_fmt_raw       o_raw;
    struct QBlockOption_fmt_rbd       o_rbd;
    struct QBlockOption_fmt_sheepdog  o_sheepdog;
    struct QBlockOption_fmt_vdi       o_vdi;
    struct QBlockOption_fmt_vmdk      o_vmdk;
    struct QBlockOption_fmt_vpc       o_vpc;
};
struct QBlockOptionFormat {
    enum QBlockFormat fmt_type;
    union QBlockOption_fmt fmt_op;
    uint8 reserved[512];
};

/**
* qb_of_new: create a new QBlockOptionFormat structure. *
* return 0 on success, negative on fail.
*
* @op: pointer that will receive created struct. */
int qb_of_new(struct QBlockOptionFormat **op);

/**
* qb_of_free: free QBlockOptionFormat structure. *
* @op: pointer to the struct, *op would be set to NULL. */
void qb_of_free(struct QBlockOptionFormat **op);

/**
* qb_open: open a block object.
*
* return 0 on success, negative on fail.
*
* @qbs: pointer to struct QBlockState.
* @loc: location options for open, how to find the image.
* @fmt: format options, how to extract the data, only valid member now is

     fmt->fmt_type.
 * @flag: behavior control flags.
 */
int qb_open(struct QBlockState *qbs, struct QBlockOptionLoc *loc,
                                 struct QBlockOptionFormat *fmt, int flag);

/**
* qb_close: close a block object.
*
* qb_flush is automaticlly done inside.
*
* @qbs: pointer to struct QBlockState.
*/
void qb_close(struct QBlockState *qbs);

/**
* qb_create: create a block image or object.
*
* return negative on fail, 0 on success.
*
* @qbs: pointer to struct QBlockState.
* @loc: location options for open, how to find the image.
* @fmt: format options, how to extract the data.
* @flag: behavior control flags.
*/
int qb_create(struct QBlockState *qbs, struct QBlockOptionLoc *loc,
struct QBlockOptionFormat *fmt, int
flag);

/* sync access */
/**
* qb_read: block sync read.
*
* return negative on fail, 0 on success.
*
* @qbs: pointer to struct QBlockState.
* @buf: buffer that receive the content.
* @len: length to read.
* @offset: offset in the block data.
*/
int qb_read(struct QBlockState *qbs, const void *buf, size_t len,

                                                      off_t offset);

/**
* qb_write: block sync write.
*
* return negative on fail, 0 on success.
*
* @qbs: pointer to struct QBlockState.
* @buf: buffer that receive the content.
* @len: length to write.
* @offset: offset in the block data.
*/
int qb_write(struct QBlockState *qbs, const void *buf, size_t len,

                                                       off_t offset);

/**
* qb_flush: block sync flush.
*
* return negative on fail, 0 on success.
*
* @qbs: pointer to struct QBlockState.
*/
int qb_flush(struct QBlockState *qbs);

/* image information */
/**
* QBlockInfoImage: information about the block image. *
* @i_loc: location info.
* @i_fmt: format info.
* @i_virt_size: virtual size.
* @i_alloc_size: allocated size, -1 if not available.
* @i_dirty: dirty status, -1 if not available. */

struct QBlockInfoImage {
    struct QBlockOptionLoc i_loc;
    struct QBlockOptionFormat i_fmt;
    size_t i_virt_size;
    /* advance info */
    size_t i_allocated_size;
    int i_dirty;
};

/**
* qb_get_image_info: get image info.
*
* return negative on fail, 0 on success.
*
* @qbs: pointer to struct QBlockState.
* @info: pointer that would receive the information.
*/
int qb_get_image_info(struct QBlockState *qbs, struct QBlockInfoImage **info);

/**
* qb_free_image_info: free image info.
*
* @info: pointer to the information struct. */
void qb_free_image_info(struct QBlockInfoImage **info);

/* misc */
bool qb_supports_format(enum QBlockFormat fmt); bool qb_supports_protocol(enum QBlockProtocol proto);

/**
* qb_error_get_detail: get erro details.
*
* return a human readable string.
*
* @qbs: pointer to struct QBlockState.
* @err_ret: negative value last qblock API returned.
* @err_num: pointer that will receive errno, it is valid only when
err_ret is
* QB_ERR_INTERNAL_ERR.
*/
const char *qb_error_get_detail(struct QBlockState *qbs, int err_ret,

                                                         int *err_num);

#endif