Skip to content
This repository has been archived by the owner on Aug 5, 2022. It is now read-only.

Commit

Permalink
blockdev: Split monitor reference from BB creation
Browse files Browse the repository at this point in the history
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: Max Reitz <mreitz@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
  • Loading branch information
XanClic authored and kevmw committed Mar 17, 2016
1 parent e5e7855 commit efaa7c4
Show file tree
Hide file tree
Showing 18 changed files with 68 additions and 76 deletions.
26 changes: 7 additions & 19 deletions block/block-backend.c
Expand Up @@ -80,41 +80,32 @@ 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;

blk = g_new0(BlockBackend, 1);
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;
}

/*
* 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;
}
Expand All @@ -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;
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion block/parallels.c
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion block/qcow.c
Expand Up @@ -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) {
Expand Down
6 changes: 3 additions & 3 deletions block/qcow2.c
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion block/qed.c
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions block/sheepdog.c
Expand Up @@ -1646,7 +1646,7 @@ static int sd_prealloc(const char *filename, Error **errp)
void *buf = NULL;
int ret;

blk = blk_new_open("image-prealloc", filename, NULL, NULL,
blk = blk_new_open(filename, NULL, NULL,
BDRV_O_RDWR | BDRV_O_CACHE_WB | BDRV_O_PROTOCOL,
errp);
if (blk == NULL) {
Expand Down Expand Up @@ -1843,7 +1843,7 @@ static int sd_create(const char *filename, QemuOpts *opts,
goto out;
}

blk = blk_new_open("backing", backing_file, NULL, NULL,
blk = blk_new_open(backing_file, NULL, NULL,
BDRV_O_PROTOCOL | BDRV_O_CACHE_WB, errp);
if (blk == NULL) {
ret = -EIO;
Expand Down
2 changes: 1 addition & 1 deletion block/vdi.c
Expand Up @@ -768,7 +768,7 @@ static int vdi_create(const char *filename, QemuOpts *opts, Error **errp)
goto exit;
}

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) {
Expand Down
2 changes: 1 addition & 1 deletion block/vhdx.c
Expand Up @@ -1838,7 +1838,7 @@ static int vhdx_create(const char *filename, QemuOpts *opts, Error **errp)
goto exit;
}

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) {
Expand Down
6 changes: 3 additions & 3 deletions block/vmdk.c
Expand Up @@ -1661,7 +1661,7 @@ static int vmdk_create_extent(const char *filename, int64_t filesize,
goto exit;
}

blk = blk_new_open("extent", 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) {
Expand Down Expand Up @@ -1946,7 +1946,7 @@ static int vmdk_create(const char *filename, QemuOpts *opts, Error **errp)
goto exit;
}

blk = blk_new_open("backing", full_backing, NULL, NULL,
blk = blk_new_open(full_backing, NULL, NULL,
BDRV_O_NO_BACKING | BDRV_O_CACHE_WB, errp);
g_free(full_backing);
if (blk == NULL) {
Expand Down Expand Up @@ -2018,7 +2018,7 @@ static int vmdk_create(const char *filename, QemuOpts *opts, Error **errp)
}
}

new_blk = blk_new_open("descriptor", filename, NULL, NULL,
new_blk = blk_new_open(filename, NULL, NULL,
BDRV_O_RDWR | BDRV_O_CACHE_WB | BDRV_O_PROTOCOL,
&local_err);
if (new_blk == NULL) {
Expand Down
2 changes: 1 addition & 1 deletion block/vpc.c
Expand Up @@ -888,7 +888,7 @@ static int vpc_create(const char *filename, QemuOpts *opts, Error **errp)
goto out;
}

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) {
Expand Down
17 changes: 14 additions & 3 deletions blockdev.c
Expand Up @@ -147,6 +147,7 @@ void blockdev_auto_del(BlockBackend *blk)
DriveInfo *dinfo = blk_legacy_dinfo(blk);

if (dinfo && dinfo->auto_del) {
monitor_remove_blk(blk);
blk_unref(blk);
}
}
Expand Down Expand Up @@ -561,7 +562,7 @@ static BlockBackend *blockdev_init(const char *file, QDict *bs_opts,
if ((!file || !*file) && !qdict_size(bs_opts)) {
BlockBackendRootState *blk_rs;

blk = blk_new(qemu_opts_id(opts), errp);
blk = blk_new(errp);
if (!blk) {
goto early_err;
}
Expand Down Expand Up @@ -597,8 +598,7 @@ static BlockBackend *blockdev_init(const char *file, QDict *bs_opts,
bdrv_flags |= BDRV_O_INACTIVE;
}

blk = blk_new_open(qemu_opts_id(opts), file, NULL, bs_opts, bdrv_flags,
errp);
blk = blk_new_open(file, NULL, bs_opts, bdrv_flags, errp);
if (!blk) {
goto err_no_bs_opts;
}
Expand Down Expand Up @@ -630,6 +630,12 @@ static BlockBackend *blockdev_init(const char *file, QDict *bs_opts,

blk_set_on_error(blk, on_read_error, on_write_error);

if (!monitor_add_blk(blk, qemu_opts_id(opts), errp)) {
blk_unref(blk);
blk = NULL;
goto err_no_bs_opts;
}

err_no_bs_opts:
qemu_opts_del(opts);
QDECREF(interval_dict);
Expand Down Expand Up @@ -2859,6 +2865,8 @@ void hmp_drive_del(Monitor *mon, const QDict *qdict)
blk_remove_bs(blk);
}

monitor_remove_blk(blk);

/* if we have a device attached to this BlockDriverState
* then we need to make the drive anonymous until the device
* can be removed. If this is a drive with no device backing
Expand Down Expand Up @@ -3976,6 +3984,7 @@ void qmp_blockdev_add(BlockdevOptions *options, Error **errp)

if (bs && bdrv_key_required(bs)) {
if (blk) {
monitor_remove_blk(blk);
blk_unref(blk);
} else {
QTAILQ_REMOVE(&monitor_bdrv_states, bs, monitor_list);
Expand Down Expand Up @@ -4005,6 +4014,7 @@ void qmp_x_blockdev_del(bool has_id, const char *id,
}

if (has_id) {
/* blk_by_name() never returns a BB that is not owned by the monitor */
blk = blk_by_name(id);
if (!blk) {
error_setg(errp, "Cannot find block backend %s", id);
Expand Down Expand Up @@ -4052,6 +4062,7 @@ void qmp_x_blockdev_del(bool has_id, const char *id,
}

if (blk) {
monitor_remove_blk(blk);
blk_unref(blk);
} else {
QTAILQ_REMOVE(&monitor_bdrv_states, bs, monitor_list);
Expand Down
4 changes: 3 additions & 1 deletion device-hotplug.c
Expand Up @@ -84,6 +84,8 @@ void hmp_drive_add(Monitor *mon, const QDict *qdict)

err:
if (dinfo) {
blk_unref(blk_by_legacy_dinfo(dinfo));
BlockBackend *blk = blk_by_legacy_dinfo(dinfo);
monitor_remove_blk(blk);
blk_unref(blk);
}
}
2 changes: 1 addition & 1 deletion hw/block/xen_disk.c
Expand Up @@ -917,7 +917,7 @@ static int blk_connect(struct XenDevice *xendev)

/* setup via xenbus -> create new block driver instance */
xen_be_printf(&blkdev->xendev, 2, "create new bdrv (xenbus setup)\n");
blkdev->blk = blk_new_open(blkdev->dev, blkdev->filename, NULL, options,
blkdev->blk = blk_new_open(blkdev->filename, NULL, options,
qflags, &local_err);
if (!blkdev->blk) {
xen_be_printf(&blkdev->xendev, 0, "error: %s\n",
Expand Down
9 changes: 4 additions & 5 deletions include/sysemu/block-backend.h
Expand Up @@ -59,11 +59,10 @@ typedef struct BlockDevOps {
void (*resize_cb)(void *opaque);
} BlockDevOps;

BlockBackend *blk_new(const char *name, Error **errp);
BlockBackend *blk_new_with_bs(const char *name, Error **errp);
BlockBackend *blk_new_open(const char *name, const char *filename,
const char *reference, QDict *options, int flags,
Error **errp);
BlockBackend *blk_new(Error **errp);
BlockBackend *blk_new_with_bs(Error **errp);
BlockBackend *blk_new_open(const char *filename, const char *reference,
QDict *options, int flags, Error **errp);
int blk_get_refcnt(BlockBackend *blk);
void blk_ref(BlockBackend *blk);
void blk_unref(BlockBackend *blk);
Expand Down

0 comments on commit efaa7c4

Please sign in to comment.