int bdrv_create_file(const char *filename, QemuOpts *opts, Error **errp)
{
+ QemuOpts *protocol_opts;
BlockDriver *drv;
+ QDict *qdict;
+ int ret;
drv = bdrv_find_protocol(filename, true, errp);
if (drv == NULL) {
return -ENOENT;
}
- return bdrv_create(drv, filename, opts, errp);
+ if (!drv->create_opts) {
+ error_setg(errp, "Driver '%s' does not support image creation",
+ drv->format_name);
+ return -ENOTSUP;
+ }
+
+ /*
+ * 'opts' contains a QemuOptsList with a combination of format and protocol
+ * default values.
+ *
+ * The format properly removes its options, but the default values remain
+ * in 'opts->list'. So if the protocol has options with the same name
+ * (e.g. rbd has 'cluster_size' as qcow2), it will see the default values
+ * of the format, since for overlapping options, the format wins.
+ *
+ * To avoid this issue, lets convert QemuOpts to QDict, in this way we take
+ * only the set options, and then convert it back to QemuOpts, using the
+ * create_opts of the protocol. So the new QemuOpts, will contain only the
+ * protocol defaults.
+ */
+ qdict = qemu_opts_to_qdict(opts, NULL);
+ protocol_opts = qemu_opts_from_qdict(drv->create_opts, qdict, errp);
+ if (protocol_opts == NULL) {
+ ret = -EINVAL;
+ goto out;
+ }
+
+ ret = bdrv_create(drv, filename, protocol_opts, errp);
+out:
+ qemu_opts_del(protocol_opts);
+ qobject_unref(qdict);
+ return ret;
}
int coroutine_fn bdrv_co_delete_file(BlockDriverState *bs, Error **errp)
return ret;
}
+void coroutine_fn bdrv_co_delete_file_noerr(BlockDriverState *bs)
+{
+ Error *local_err = NULL;
+ int ret;
+
+ if (!bs) {
+ return;
+ }
+
+ ret = bdrv_co_delete_file(bs, &local_err);
+ /*
+ * ENOTSUP will happen if the block driver doesn't support
+ * the 'bdrv_co_delete_file' interface. This is a predictable
+ * scenario and shouldn't be reported back to the user.
+ */
+ if (ret == -ENOTSUP) {
+ error_free(local_err);
+ } else if (ret < 0) {
+ error_report_err(local_err);
+ }
+}
+
/**
* Try to get @bs's logical and physical block size.
* On success, store them in @bsz struct and return 0.
* Check for empty string or invalid characters, but not if it is
* generated (generated names use characters not available to the user)
*/
- error_setg(errp, "Invalid node name");
+ error_setg(errp, "Invalid node-name: '%s'", node_name);
return;
}
/* takes care of avoiding duplicates node names */
if (bdrv_find_node(node_name)) {
- error_setg(errp, "Duplicate node name");
+ error_setg(errp, "Duplicate nodes with node-name='%s'", node_name);
goto out;
}
* Sets the bs->backing link of a BDS. A new reference is created; callers
* which don't need their own reference any more must call bdrv_unref().
*/
-void bdrv_set_backing_hd(BlockDriverState *bs, BlockDriverState *backing_hd,
- Error **errp)
+int bdrv_set_backing_hd(BlockDriverState *bs, BlockDriverState *backing_hd,
+ Error **errp)
{
+ int ret = 0;
bool update_inherits_from = bdrv_chain_contains(bs, backing_hd) &&
bdrv_inherits_from_recursive(backing_hd, bs);
if (bdrv_is_backing_chain_frozen(bs, child_bs(bs->backing), errp)) {
- return;
+ return -EPERM;
}
if (backing_hd) {
bs->backing = bdrv_attach_child(bs, backing_hd, "backing", &child_of_bds,
bdrv_backing_role(bs), errp);
+ if (!bs->backing) {
+ ret = -EPERM;
+ goto out;
+ }
+
/* If backing_hd was already part of bs's backing chain, and
* inherits_from pointed recursively to bs then let's update it to
* point directly to bs (else it will become NULL). */
- if (bs->backing && update_inherits_from) {
+ if (update_inherits_from) {
backing_hd->inherits_from = bs;
}
out:
bdrv_refresh_limits(bs, NULL);
+
+ return ret;
}
/*
/* Hook up the backing file link; drop our reference, bs owns the
* backing_hd reference now */
- bdrv_set_backing_hd(bs, backing_hd, &local_err);
+ ret = bdrv_set_backing_hd(bs, backing_hd, errp);
bdrv_unref(backing_hd);
- if (local_err) {
- error_propagate(errp, local_err);
- ret = -EINVAL;
+ if (ret < 0) {
goto free_exit;
}
int64_t total_size;
QemuOpts *opts = NULL;
BlockDriverState *bs_snapshot = NULL;
- Error *local_err = NULL;
int ret;
/* if snapshot, we create a temporary backing file and open it
* order to be able to return one, we have to increase
* bs_snapshot's refcount here */
bdrv_ref(bs_snapshot);
- bdrv_append(bs_snapshot, bs, &local_err);
- if (local_err) {
- error_propagate(errp, local_err);
+ ret = bdrv_append(bs_snapshot, bs, errp);
+ if (ret < 0) {
bs_snapshot = NULL;
goto out;
}
void bdrv_close_all(void)
{
assert(job_next(NULL) == NULL);
- blk_exp_close_all();
/* Drop references from requests still in flight, such as canceled block
* jobs whose AIO context has not been polled yet */
* With auto_skip=false the error is returned if from has a parent which should
* not be updated.
*/
-static void bdrv_replace_node_common(BlockDriverState *from,
- BlockDriverState *to,
- bool auto_skip, Error **errp)
+static int bdrv_replace_node_common(BlockDriverState *from,
+ BlockDriverState *to,
+ bool auto_skip, Error **errp)
{
BdrvChild *c, *next;
GSList *list = NULL, *p;
if (auto_skip) {
continue;
}
+ ret = -EINVAL;
error_setg(errp, "Should not change '%s' link to '%s'",
c->name, from->node_name);
goto out;
}
if (c->frozen) {
+ ret = -EPERM;
error_setg(errp, "Cannot change '%s' link to '%s'",
c->name, from->node_name);
goto out;
bdrv_set_perm(to);
+ ret = 0;
+
out:
g_slist_free(list);
bdrv_drained_end(from);
bdrv_unref(from);
+
+ return ret;
}
-void bdrv_replace_node(BlockDriverState *from, BlockDriverState *to,
- Error **errp)
+int bdrv_replace_node(BlockDriverState *from, BlockDriverState *to,
+ Error **errp)
{
return bdrv_replace_node_common(from, to, true, errp);
}
* parents of bs_top after bdrv_append() returns. If the caller needs to keep a
* reference of its own, it must call bdrv_ref().
*/
-void bdrv_append(BlockDriverState *bs_new, BlockDriverState *bs_top,
- Error **errp)
+int bdrv_append(BlockDriverState *bs_new, BlockDriverState *bs_top,
+ Error **errp)
{
- Error *local_err = NULL;
-
- bdrv_set_backing_hd(bs_new, bs_top, &local_err);
- if (local_err) {
- error_propagate(errp, local_err);
+ int ret = bdrv_set_backing_hd(bs_new, bs_top, errp);
+ if (ret < 0) {
goto out;
}
- bdrv_replace_node(bs_top, bs_new, &local_err);
- if (local_err) {
- error_propagate(errp, local_err);
+ ret = bdrv_replace_node(bs_top, bs_new, errp);
+ if (ret < 0) {
bdrv_set_backing_hd(bs_new, NULL, &error_abort);
goto out;
}
- /* bs_new is now referenced by its new parents, we don't need the
- * additional reference any more. */
+ ret = 0;
+
out:
+ /*
+ * bs_new is now referenced by its new parents, we don't need the
+ * additional reference any more.
+ */
bdrv_unref(bs_new);
+
+ return ret;
}
static void bdrv_delete(BlockDriverState *bs)
g_free(bs);
}
+BlockDriverState *bdrv_insert_node(BlockDriverState *bs, QDict *node_options,
+ int flags, Error **errp)
+{
+ BlockDriverState *new_node_bs;
+ Error *local_err = NULL;
+
+ new_node_bs = bdrv_open(NULL, NULL, node_options, flags, errp);
+ if (new_node_bs == NULL) {
+ error_prepend(errp, "Could not create node: ");
+ return NULL;
+ }
+
+ bdrv_drained_begin(bs);
+ bdrv_replace_node(bs, new_node_bs, &local_err);
+ bdrv_drained_end(bs);
+
+ if (local_err) {
+ bdrv_unref(new_node_bs);
+ error_propagate(errp, local_err);
+ return NULL;
+ }
+
+ return new_node_bs;
+}
+
/*
* Run consistency checks on an image
*
}
}
- error_setg(errp, "Cannot find device=%s nor node_name=%s",
+ error_setg(errp, "Cannot find device=\'%s\' nor node-name=\'%s\'",
device ? device : "",
node_name ? node_name : "");
return NULL;
AioContext *new_context, GSList **ignore)
{
AioContext *old_context = bdrv_get_aio_context(bs);
- BdrvChild *child;
+ GSList *children_to_process = NULL;
+ GSList *parents_to_process = NULL;
+ GSList *entry;
+ BdrvChild *child, *parent;
g_assert(qemu_get_current_aio_context() == qemu_get_aio_context());
continue;
}
*ignore = g_slist_prepend(*ignore, child);
- bdrv_set_aio_context_ignore(child->bs, new_context, ignore);
+ children_to_process = g_slist_prepend(children_to_process, child);
}
- QLIST_FOREACH(child, &bs->parents, next_parent) {
- if (g_slist_find(*ignore, child)) {
+
+ QLIST_FOREACH(parent, &bs->parents, next_parent) {
+ if (g_slist_find(*ignore, parent)) {
continue;
}
- assert(child->klass->set_aio_ctx);
- *ignore = g_slist_prepend(*ignore, child);
- child->klass->set_aio_ctx(child, new_context, ignore);
+ *ignore = g_slist_prepend(*ignore, parent);
+ parents_to_process = g_slist_prepend(parents_to_process, parent);
+ }
+
+ for (entry = children_to_process;
+ entry != NULL;
+ entry = g_slist_next(entry)) {
+ child = entry->data;
+ bdrv_set_aio_context_ignore(child->bs, new_context, ignore);
+ }
+ g_slist_free(children_to_process);
+
+ for (entry = parents_to_process;
+ entry != NULL;
+ entry = g_slist_next(entry)) {
+ parent = entry->data;
+ assert(parent->klass->set_aio_ctx);
+ parent->klass->set_aio_ctx(parent, new_context, ignore);
}
+ g_slist_free(parents_to_process);
bdrv_detach_aio_context(bs);
AioContext *aio_context;
if (!to_replace_bs) {
- error_setg(errp, "Node name '%s' not found", node_name);
+ error_setg(errp, "Failed to find node with node-name='%s'", node_name);
return NULL;
}