Commit efaa7c4e authored by Max Reitz's avatar Max Reitz Committed by Kevin Wolf
Browse files

blockdev: Split monitor reference from BB creation



Before this patch, blk_new() automatically assigned a name to the new
BlockBackend and considered it referenced by the monitor. This patch
removes the implicit monitor_add_blk() call from blk_new() (and
consequently the monitor_remove_blk() call from blk_delete(), too) and
thus blk_new() (and related functions) no longer take a BB name
argument.

In fact, there is only a single point where blk_new()/blk_new_open() is
called and the new BB is monitor-owned, and that is in blockdev_init().
Besides thus relieving us from having to invent names for all of the BBs
we use in qemu-img, this fixes a bug where qemu cannot create a new
image if there already is a monitor-owned BB named "image".

If a BB and its BDS tree are created in a single operation, as of this
patch the BDS tree will be created before the BB is given a name
(whereas it was the other way around before). This results in minor
change to the output of iotest 087, whose reference output is amended
accordingly.

Signed-off-by: default avatarMax Reitz <mreitz@redhat.com>
Signed-off-by: default avatarKevin Wolf <kwolf@redhat.com>
parent e5e78550
Loading
Loading
Loading
Loading
+7 −19
Original line number Diff line number Diff line
@@ -80,13 +80,11 @@ static QTAILQ_HEAD(, BlockBackend) monitor_block_backends =
    QTAILQ_HEAD_INITIALIZER(monitor_block_backends);

/*
 * Create a new BlockBackend with @name, with a reference count of one.
 * @name must not be null or empty.
 * Fail if a BlockBackend with this name already exists.
 * Create a new BlockBackend with a reference count of one.
 * Store an error through @errp on failure, unless it's null.
 * Return the new BlockBackend on success, null on failure.
 */
BlockBackend *blk_new(const char *name, Error **errp)
BlockBackend *blk_new(Error **errp)
{
    BlockBackend *blk;

@@ -94,14 +92,7 @@ BlockBackend *blk_new(const char *name, Error **errp)
    blk->refcnt = 1;
    notifier_list_init(&blk->remove_bs_notifiers);
    notifier_list_init(&blk->insert_bs_notifiers);

    QTAILQ_INSERT_TAIL(&block_backends, blk, link);

    if (!monitor_add_blk(blk, name, errp)) {
        blk_unref(blk);
        return NULL;
    }

    return blk;
}

@@ -109,12 +100,12 @@ BlockBackend *blk_new(const char *name, Error **errp)
 * Create a new BlockBackend with a new BlockDriverState attached.
 * Otherwise just like blk_new(), which see.
 */
BlockBackend *blk_new_with_bs(const char *name, Error **errp)
BlockBackend *blk_new_with_bs(Error **errp)
{
    BlockBackend *blk;
    BlockDriverState *bs;

    blk = blk_new(name, errp);
    blk = blk_new(errp);
    if (!blk) {
        return NULL;
    }
@@ -137,14 +128,13 @@ BlockBackend *blk_new_with_bs(const char *name, Error **errp)
 * though, so callers of this function have to be able to specify @filename and
 * @flags.
 */
BlockBackend *blk_new_open(const char *name, const char *filename,
                           const char *reference, QDict *options, int flags,
                           Error **errp)
BlockBackend *blk_new_open(const char *filename, const char *reference,
                           QDict *options, int flags, Error **errp)
{
    BlockBackend *blk;
    int ret;

    blk = blk_new_with_bs(name, errp);
    blk = blk_new_with_bs(errp);
    if (!blk) {
        QDECREF(options);
        return NULL;
@@ -161,8 +151,6 @@ BlockBackend *blk_new_open(const char *name, const char *filename,

static void blk_delete(BlockBackend *blk)
{
    monitor_remove_blk(blk);

    assert(!blk->refcnt);
    assert(!blk->name);
    assert(!blk->dev);
+1 −1
Original line number Diff line number Diff line
@@ -478,7 +478,7 @@ static int parallels_create(const char *filename, QemuOpts *opts, Error **errp)
        return ret;
    }

    file = blk_new_open("image", filename, NULL, NULL,
    file = blk_new_open(filename, NULL, NULL,
                        BDRV_O_RDWR | BDRV_O_CACHE_WB | BDRV_O_PROTOCOL,
                        &local_err);
    if (file == NULL) {
+1 −1
Original line number Diff line number Diff line
@@ -793,7 +793,7 @@ static int qcow_create(const char *filename, QemuOpts *opts, Error **errp)
        goto cleanup;
    }

    qcow_blk = blk_new_open("image", filename, NULL, NULL,
    qcow_blk = blk_new_open(filename, NULL, NULL,
                            BDRV_O_RDWR | BDRV_O_CACHE_WB | BDRV_O_PROTOCOL,
                            &local_err);
    if (qcow_blk == NULL) {
+3 −3
Original line number Diff line number Diff line
@@ -2159,7 +2159,7 @@ static int qcow2_create2(const char *filename, int64_t total_size,
        return ret;
    }

    blk = blk_new_open("image", filename, NULL, NULL,
    blk = blk_new_open(filename, NULL, NULL,
                       BDRV_O_RDWR | BDRV_O_CACHE_WB | BDRV_O_PROTOCOL,
                       &local_err);
    if (blk == NULL) {
@@ -2224,7 +2224,7 @@ static int qcow2_create2(const char *filename, int64_t total_size,
     */
    options = qdict_new();
    qdict_put(options, "driver", qstring_from_str("qcow2"));
    blk = blk_new_open("image-qcow2", filename, NULL, options,
    blk = blk_new_open(filename, NULL, options,
                       BDRV_O_RDWR | BDRV_O_CACHE_WB | BDRV_O_NO_FLUSH,
                       &local_err);
    if (blk == NULL) {
@@ -2286,7 +2286,7 @@ static int qcow2_create2(const char *filename, int64_t total_size,
    /* Reopen the image without BDRV_O_NO_FLUSH to flush it before returning */
    options = qdict_new();
    qdict_put(options, "driver", qstring_from_str("qcow2"));
    blk = blk_new_open("image-flush", filename, NULL, options,
    blk = blk_new_open(filename, NULL, options,
                       BDRV_O_RDWR | BDRV_O_CACHE_WB | BDRV_O_NO_BACKING,
                       &local_err);
    if (blk == NULL) {
+1 −1
Original line number Diff line number Diff line
@@ -574,7 +574,7 @@ static int qed_create(const char *filename, uint32_t cluster_size,
        return ret;
    }

    blk = blk_new_open("image", filename, NULL, NULL,
    blk = blk_new_open(filename, NULL, NULL,
                       BDRV_O_RDWR | BDRV_O_CACHE_WB | BDRV_O_PROTOCOL,
                       &local_err);
    if (blk == NULL) {
Loading