return 0;
}
-/* TODO Remove (deprecated since 2.11)
- * Block drivers are not supposed to automatically change bs->read_only.
- * Instead, they should just check whether they can provide what the user
- * explicitly requested and error out if read-write is requested, but they can
- * only provide read-only access. */
-int bdrv_set_read_only(BlockDriverState *bs, bool read_only, Error **errp)
+/*
+ * Called by a driver that can only provide a read-only image.
+ *
+ * Returns 0 if the node is already read-only or it could switch the node to
+ * read-only because BDRV_O_AUTO_RDONLY is set.
+ *
+ * Returns -EACCES if the node is read-write and BDRV_O_AUTO_RDONLY is not set
+ * or bdrv_can_set_read_only() forbids making the node read-only. If @errmsg
+ * is not NULL, it is used as the error message for the Error object.
+ */
+int bdrv_apply_auto_read_only(BlockDriverState *bs, const char *errmsg,
+ Error **errp)
{
int ret = 0;
- ret = bdrv_can_set_read_only(bs, read_only, false, errp);
+ if (!(bs->open_flags & BDRV_O_RDWR)) {
+ return 0;
+ }
+ if (!(bs->open_flags & BDRV_O_AUTO_RDONLY)) {
+ goto fail;
+ }
+
+ ret = bdrv_can_set_read_only(bs, true, false, NULL);
if (ret < 0) {
- return ret;
+ goto fail;
}
- bs->read_only = read_only;
+ bs->read_only = true;
+ bs->open_flags &= ~BDRV_O_RDWR;
+
return 0;
+
+fail:
+ error_setg(errp, "%s", errmsg ?: "Image is read-only");
+ return -EACCES;
}
void bdrv_get_full_backing_filename_from_filename(const char *backed,
}
}
+static BlockdevDetectZeroesOptions bdrv_parse_detect_zeroes(QemuOpts *opts,
+ int open_flags,
+ Error **errp)
+{
+ Error *local_err = NULL;
+ char *value = qemu_opt_get_del(opts, "detect-zeroes");
+ BlockdevDetectZeroesOptions detect_zeroes =
+ qapi_enum_parse(&BlockdevDetectZeroesOptions_lookup, value,
+ BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF, &local_err);
+ g_free(value);
+ if (local_err) {
+ error_propagate(errp, local_err);
+ return detect_zeroes;
+ }
+
+ if (detect_zeroes == BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP &&
+ !(open_flags & BDRV_O_UNMAP))
+ {
+ error_setg(errp, "setting detect-zeroes to unmap is not allowed "
+ "without setting discard operation to unmap");
+ }
+
+ return detect_zeroes;
+}
+
/**
* Set open flags for a given discard mode
*
/* Inherit the read-only option from the parent if it's not set */
qdict_copy_default(child_options, parent_options, BDRV_OPT_READ_ONLY);
+ qdict_copy_default(child_options, parent_options, BDRV_OPT_AUTO_READ_ONLY);
/* Our block drivers take care to send flushes and respect unmap policy,
* so we can default to enable both on lower layers regardless of the
/* backing files always opened read-only */
qdict_set_default_str(child_options, BDRV_OPT_READ_ONLY, "on");
+ qdict_set_default_str(child_options, BDRV_OPT_AUTO_READ_ONLY, "off");
flags &= ~BDRV_O_COPY_ON_READ;
/* snapshot=on is handled on the top layer */
const char *filename, Error **errp)
{
BlockDriverState *parent = c->opaque;
- int orig_flags = bdrv_get_flags(parent);
+ bool read_only = bdrv_is_read_only(parent);
int ret;
- if (!(orig_flags & BDRV_O_RDWR)) {
- ret = bdrv_reopen(parent, orig_flags | BDRV_O_RDWR, errp);
+ if (read_only) {
+ ret = bdrv_reopen_set_read_only(parent, false, errp);
if (ret < 0) {
return ret;
}
error_setg_errno(errp, -ret, "Could not update backing file link");
}
- if (!(orig_flags & BDRV_O_RDWR)) {
- bdrv_reopen(parent, orig_flags, NULL);
+ if (read_only) {
+ bdrv_reopen_set_read_only(parent, true, NULL);
}
return ret;
static void update_flags_from_options(int *flags, QemuOpts *opts)
{
- *flags &= ~BDRV_O_CACHE_MASK;
+ *flags &= ~(BDRV_O_CACHE_MASK | BDRV_O_RDWR | BDRV_O_AUTO_RDONLY);
- assert(qemu_opt_find(opts, BDRV_OPT_CACHE_NO_FLUSH));
- if (qemu_opt_get_bool(opts, BDRV_OPT_CACHE_NO_FLUSH, false)) {
+ if (qemu_opt_get_bool_del(opts, BDRV_OPT_CACHE_NO_FLUSH, false)) {
*flags |= BDRV_O_NO_FLUSH;
}
- assert(qemu_opt_find(opts, BDRV_OPT_CACHE_DIRECT));
- if (qemu_opt_get_bool(opts, BDRV_OPT_CACHE_DIRECT, false)) {
+ if (qemu_opt_get_bool_del(opts, BDRV_OPT_CACHE_DIRECT, false)) {
*flags |= BDRV_O_NOCACHE;
}
- *flags &= ~BDRV_O_RDWR;
-
- assert(qemu_opt_find(opts, BDRV_OPT_READ_ONLY));
- if (!qemu_opt_get_bool(opts, BDRV_OPT_READ_ONLY, false)) {
+ if (!qemu_opt_get_bool_del(opts, BDRV_OPT_READ_ONLY, false)) {
*flags |= BDRV_O_RDWR;
}
+ if (qemu_opt_get_bool_del(opts, BDRV_OPT_AUTO_READ_ONLY, false)) {
+ *flags |= BDRV_O_AUTO_RDONLY;
+ }
}
static void update_options_from_flags(QDict *options, int flags)
if (!qdict_haskey(options, BDRV_OPT_READ_ONLY)) {
qdict_put_bool(options, BDRV_OPT_READ_ONLY, !(flags & BDRV_O_RDWR));
}
+ if (!qdict_haskey(options, BDRV_OPT_AUTO_READ_ONLY)) {
+ qdict_put_bool(options, BDRV_OPT_AUTO_READ_ONLY,
+ flags & BDRV_O_AUTO_RDONLY);
+ }
}
static void bdrv_assign_node_name(BlockDriverState *bs,
.type = QEMU_OPT_BOOL,
.help = "Node is opened in read-only mode",
},
+ {
+ .name = BDRV_OPT_AUTO_READ_ONLY,
+ .type = QEMU_OPT_BOOL,
+ .help = "Node can become read-only if opening read-write fails",
+ },
{
.name = "detect-zeroes",
.type = QEMU_OPT_STRING,
.help = "try to optimize zero writes (off, on, unmap)",
},
{
- .name = "discard",
+ .name = BDRV_OPT_DISCARD,
.type = QEMU_OPT_STRING,
.help = "discard operation (ignore/off, unmap/on)",
},
const char *driver_name = NULL;
const char *node_name = NULL;
const char *discard;
- const char *detect_zeroes;
QemuOpts *opts;
BlockDriver *drv;
Error *local_err = NULL;
bs->read_only = !(bs->open_flags & BDRV_O_RDWR);
if (use_bdrv_whitelist && !bdrv_is_whitelisted(drv, bs->read_only)) {
- error_setg(errp,
- !bs->read_only && bdrv_is_whitelisted(drv, true)
- ? "Driver '%s' can only be used for read-only devices"
- : "Driver '%s' is not whitelisted",
- drv->format_name);
- ret = -ENOTSUP;
- goto fail_opts;
+ if (!bs->read_only && bdrv_is_whitelisted(drv, true)) {
+ ret = bdrv_apply_auto_read_only(bs, NULL, NULL);
+ } else {
+ ret = -ENOTSUP;
+ }
+ if (ret < 0) {
+ error_setg(errp,
+ !bs->read_only && bdrv_is_whitelisted(drv, true)
+ ? "Driver '%s' can only be used for read-only devices"
+ : "Driver '%s' is not whitelisted",
+ drv->format_name);
+ goto fail_opts;
+ }
}
/* bdrv_new() and bdrv_close() make it so */
}
}
- discard = qemu_opt_get(opts, "discard");
+ discard = qemu_opt_get(opts, BDRV_OPT_DISCARD);
if (discard != NULL) {
if (bdrv_parse_discard_flags(discard, &bs->open_flags) != 0) {
error_setg(errp, "Invalid discard option");
}
}
- detect_zeroes = qemu_opt_get(opts, "detect-zeroes");
- if (detect_zeroes) {
- BlockdevDetectZeroesOptions value =
- qapi_enum_parse(&BlockdevDetectZeroesOptions_lookup,
- detect_zeroes,
- BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF,
- &local_err);
- if (local_err) {
- error_propagate(errp, local_err);
- ret = -EINVAL;
- goto fail_opts;
- }
-
- if (value == BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP &&
- !(bs->open_flags & BDRV_O_UNMAP))
- {
- error_setg(errp, "setting detect-zeroes to unmap is not allowed "
- "without setting discard operation to unmap");
- ret = -EINVAL;
- goto fail_opts;
- }
-
- bs->detect_zeroes = value;
+ bs->detect_zeroes =
+ bdrv_parse_detect_zeroes(opts, bs->open_flags, &local_err);
+ if (local_err) {
+ error_propagate(errp, local_err);
+ ret = -EINVAL;
+ goto fail_opts;
}
if (filename != NULL) {
}
}
+/* Return true if you can reach parent going through child->inherits_from
+ * recursively. If parent or child are NULL, return false */
+static bool bdrv_inherits_from_recursive(BlockDriverState *child,
+ BlockDriverState *parent)
+{
+ while (child && child != parent) {
+ child = child->inherits_from;
+ }
+
+ return child != NULL;
+}
+
/*
* Sets the backing file 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)
{
+ bool update_inherits_from = bdrv_chain_contains(bs, backing_hd) &&
+ bdrv_inherits_from_recursive(backing_hd, bs);
+
if (backing_hd) {
bdrv_ref(backing_hd);
}
bs->backing = bdrv_attach_child(bs, backing_hd, "backing", &child_backing,
errp);
+ /* 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 (update_inherits_from) {
+ backing_hd->inherits_from = bs;
+ }
if (!bs->backing) {
bdrv_unref(backing_hd);
}
qdict_set_default_str(qdict, BDRV_OPT_CACHE_DIRECT, "off");
qdict_set_default_str(qdict, BDRV_OPT_CACHE_NO_FLUSH, "off");
qdict_set_default_str(qdict, BDRV_OPT_READ_ONLY, "off");
+ qdict_set_default_str(qdict, BDRV_OPT_AUTO_READ_ONLY, "off");
+
}
bs = bdrv_open_inherit(NULL, reference, qdict, 0, NULL, NULL, errp);
static BlockReopenQueue *bdrv_reopen_queue_child(BlockReopenQueue *bs_queue,
BlockDriverState *bs,
QDict *options,
- int flags,
const BdrvChildRole *role,
QDict *parent_options,
int parent_flags)
BlockReopenQueueEntry *bs_entry;
BdrvChild *child;
- QDict *old_options, *explicit_options;
+ QDict *old_options, *explicit_options, *options_copy;
+ int flags;
+ QemuOpts *opts;
/* Make sure that the caller remembered to use a drained section. This is
* important to avoid graph changes between the recursive queuing here and
/*
* Precedence of options:
* 1. Explicitly passed in options (highest)
- * 2. Set in flags (only for top level)
- * 3. Retained from explicitly set options of bs
- * 4. Inherited from parent node
- * 5. Retained from effective options of bs
+ * 2. Retained from explicitly set options of bs
+ * 3. Inherited from parent node
+ * 4. Retained from effective options of bs
*/
- if (!parent_options) {
- /*
- * Any setting represented by flags is always updated. If the
- * corresponding QDict option is set, it takes precedence. Otherwise
- * the flag is translated into a QDict option. The old setting of bs is
- * not considered.
- */
- update_options_from_flags(options, flags);
- }
-
/* Old explicitly set values (don't overwrite by inherited value) */
if (bs_entry) {
old_options = qdict_clone_shallow(bs_entry->state.explicit_options);
/* Inherit from parent node */
if (parent_options) {
- QemuOpts *opts;
- QDict *options_copy;
- assert(!flags);
+ flags = 0;
role->inherit_options(&flags, options, parent_flags, parent_options);
- options_copy = qdict_clone_shallow(options);
- opts = qemu_opts_create(&bdrv_runtime_opts, NULL, 0, &error_abort);
- qemu_opts_absorb_qdict(opts, options_copy, NULL);
- update_flags_from_options(&flags, opts);
- qemu_opts_del(opts);
- qobject_unref(options_copy);
+ } else {
+ flags = bdrv_get_flags(bs);
}
/* Old values are used for options that aren't set yet */
bdrv_join_options(bs, options, old_options);
qobject_unref(old_options);
+ /* We have the final set of options so let's update the flags */
+ options_copy = qdict_clone_shallow(options);
+ opts = qemu_opts_create(&bdrv_runtime_opts, NULL, 0, &error_abort);
+ qemu_opts_absorb_qdict(opts, options_copy, NULL);
+ update_flags_from_options(&flags, opts);
+ qemu_opts_del(opts);
+ qobject_unref(options_copy);
+
/* bdrv_open_inherit() sets and clears some additional flags internally */
flags &= ~BDRV_O_PROTOCOL;
if (flags & BDRV_O_RDWR) {
qdict_extract_subqdict(options, &new_child_options, child_key_dot);
g_free(child_key_dot);
- bdrv_reopen_queue_child(bs_queue, child->bs, new_child_options, 0,
+ bdrv_reopen_queue_child(bs_queue, child->bs, new_child_options,
child->role, options, flags);
}
BlockReopenQueue *bdrv_reopen_queue(BlockReopenQueue *bs_queue,
BlockDriverState *bs,
- QDict *options, int flags)
+ QDict *options)
{
- return bdrv_reopen_queue_child(bs_queue, bs, options, flags,
- NULL, NULL, 0);
+ return bdrv_reopen_queue_child(bs_queue, bs, options, NULL, NULL, 0);
}
/*
return ret;
}
-
-/* Reopen a single BlockDriverState with the specified flags. */
-int bdrv_reopen(BlockDriverState *bs, int bdrv_flags, Error **errp)
+int bdrv_reopen_set_read_only(BlockDriverState *bs, bool read_only,
+ Error **errp)
{
- int ret = -1;
- Error *local_err = NULL;
+ int ret;
BlockReopenQueue *queue;
+ QDict *opts = qdict_new();
- bdrv_subtree_drained_begin(bs);
-
- queue = bdrv_reopen_queue(NULL, bs, NULL, bdrv_flags);
- ret = bdrv_reopen_multiple(bdrv_get_aio_context(bs), queue, &local_err);
- if (local_err != NULL) {
- error_propagate(errp, local_err);
- }
+ qdict_put_bool(opts, BDRV_OPT_READ_ONLY, read_only);
+ bdrv_subtree_drained_begin(bs);
+ queue = bdrv_reopen_queue(NULL, bs, opts);
+ ret = bdrv_reopen_multiple(bdrv_get_aio_context(bs), queue, errp);
bdrv_subtree_drained_end(bs);
return ret;
Error **errp)
{
int ret = -1;
+ int old_flags;
Error *local_err = NULL;
BlockDriver *drv;
QemuOpts *opts;
QDict *orig_reopen_opts;
- const char *value;
+ char *discard = NULL;
bool read_only;
+ bool drv_prepared = false;
assert(reopen_state != NULL);
assert(reopen_state->bs->drv != NULL);
goto error;
}
+ /* This was already called in bdrv_reopen_queue_child() so the flags
+ * are up-to-date. This time we simply want to remove the options from
+ * QemuOpts in order to indicate that they have been processed. */
+ old_flags = reopen_state->flags;
update_flags_from_options(&reopen_state->flags, opts);
+ assert(old_flags == reopen_state->flags);
- /* node-name and driver must be unchanged. Put them back into the QDict, so
- * that they are checked at the end of this function. */
- value = qemu_opt_get(opts, "node-name");
- if (value) {
- qdict_put_str(reopen_state->options, "node-name", value);
+ discard = qemu_opt_get_del(opts, BDRV_OPT_DISCARD);
+ if (discard != NULL) {
+ if (bdrv_parse_discard_flags(discard, &reopen_state->flags) != 0) {
+ error_setg(errp, "Invalid discard option");
+ ret = -EINVAL;
+ goto error;
+ }
}
- value = qemu_opt_get(opts, "driver");
- if (value) {
- qdict_put_str(reopen_state->options, "driver", value);
+ reopen_state->detect_zeroes =
+ bdrv_parse_detect_zeroes(opts, reopen_state->flags, &local_err);
+ if (local_err) {
+ error_propagate(errp, local_err);
+ ret = -EINVAL;
+ goto error;
}
+ /* All other options (including node-name and driver) must be unchanged.
+ * Put them back into the QDict, so that they are checked at the end
+ * of this function. */
+ qemu_opts_to_qdict(opts, reopen_state->options);
+
/* If we are to stay read-only, do not allow permission change
* to r/w. Attempting to set to r/w may fail if either BDRV_O_ALLOW_RDWR is
* not set, or if the BDS still has copy_on_read enabled */
goto error;
}
+ drv_prepared = true;
+
/* Options that are not handled are only okay if they are unchanged
* compared to the old state. It is expected that some options are only
* used for the initial open, but not reopen (e.g. filename) */
QObject *new = entry->value;
QObject *old = qdict_get(reopen_state->bs->options, entry->key);
+ /* Allow child references (child_name=node_name) as long as they
+ * point to the current child (i.e. everything stays the same). */
+ if (qobject_type(new) == QTYPE_QSTRING) {
+ BdrvChild *child;
+ QLIST_FOREACH(child, &reopen_state->bs->children, next) {
+ if (!strcmp(child->name, entry->key)) {
+ break;
+ }
+ }
+
+ if (child) {
+ const char *str = qobject_get_try_str(new);
+ if (!strcmp(child->bs->node_name, str)) {
+ continue; /* Found child with this name, skip option */
+ }
+ }
+ }
+
/*
* TODO: When using -drive to specify blockdev options, all values
* will be strings; however, when using -blockdev, blockdev-add or
reopen_state->options = qobject_ref(orig_reopen_opts);
error:
+ if (ret < 0 && drv_prepared) {
+ /* drv->bdrv_reopen_prepare() has succeeded, so we need to
+ * call drv->bdrv_reopen_abort() before signaling an error
+ * (bdrv_reopen_multiple() will not call bdrv_reopen_abort()
+ * when the respective bdrv_reopen_prepare() has failed) */
+ if (drv->bdrv_reopen_abort) {
+ drv->bdrv_reopen_abort(reopen_state);
+ }
+ }
qemu_opts_del(opts);
qobject_unref(orig_reopen_opts);
+ g_free(discard);
return ret;
}
bs->options = reopen_state->options;
bs->open_flags = reopen_state->flags;
bs->read_only = !(reopen_state->flags & BDRV_O_RDWR);
+ bs->detect_zeroes = reopen_state->detect_zeroes;
/* Remove child references from bs->options and bs->explicit_options.
* Child options were already removed in bdrv_reopen_queue_child() */
{
CheckCo *cco = opaque;
cco->ret = bdrv_co_check(cco->bs, cco->res, cco->fix);
+ aio_wait_kick();
}
int bdrv_check(BlockDriverState *bs,
bdrv_check_co_entry(&cco);
} else {
co = qemu_coroutine_create(bdrv_check_co_entry, &cco);
- qemu_coroutine_enter(co);
+ bdrv_coroutine_enter(bs, co);
BDRV_POLL_WHILE(bs, cco.ret == -EINPROGRESS);
}
int bdrv_drop_intermediate(BlockDriverState *top, BlockDriverState *base,
const char *backing_file_str)
{
+ BlockDriverState *explicit_top = top;
+ bool update_inherits_from;
BdrvChild *c, *next;
Error *local_err = NULL;
int ret = -EIO;
goto exit;
}
+ /* If 'base' recursively inherits from 'top' then we should set
+ * base->inherits_from to top->inherits_from after 'top' and all
+ * other intermediate nodes have been dropped.
+ * If 'top' is an implicit node (e.g. "commit_top") we should skip
+ * it because no one inherits from it. We use explicit_top for that. */
+ while (explicit_top && explicit_top->implicit) {
+ explicit_top = backing_bs(explicit_top);
+ }
+ update_inherits_from = bdrv_inherits_from_recursive(base, explicit_top);
+
/* success - we can delete the intermediate states, and link top->base */
/* TODO Check graph modification op blockers (BLK_PERM_GRAPH_MOD) once
* we've figured out how they should work. */
bdrv_unref(top);
}
+ if (update_inherits_from) {
+ base->inherits_from = explicit_top->inherits_from;
+ }
+
ret = 0;
exit:
bdrv_unref(top);
return list;
}
+#define QAPI_LIST_ADD(list, element) do { \
+ typeof(list) _tmp = g_new(typeof(*(list)), 1); \
+ _tmp->value = (element); \
+ _tmp->next = (list); \
+ (list) = _tmp; \
+} while (0)
+
+typedef struct XDbgBlockGraphConstructor {
+ XDbgBlockGraph *graph;
+ GHashTable *graph_nodes;
+} XDbgBlockGraphConstructor;
+
+static XDbgBlockGraphConstructor *xdbg_graph_new(void)
+{
+ XDbgBlockGraphConstructor *gr = g_new(XDbgBlockGraphConstructor, 1);
+
+ gr->graph = g_new0(XDbgBlockGraph, 1);
+ gr->graph_nodes = g_hash_table_new(NULL, NULL);
+
+ return gr;
+}
+
+static XDbgBlockGraph *xdbg_graph_finalize(XDbgBlockGraphConstructor *gr)
+{
+ XDbgBlockGraph *graph = gr->graph;
+
+ g_hash_table_destroy(gr->graph_nodes);
+ g_free(gr);
+
+ return graph;
+}
+
+static uintptr_t xdbg_graph_node_num(XDbgBlockGraphConstructor *gr, void *node)
+{
+ uintptr_t ret = (uintptr_t)g_hash_table_lookup(gr->graph_nodes, node);
+
+ if (ret != 0) {
+ return ret;
+ }
+
+ /*
+ * Start counting from 1, not 0, because 0 interferes with not-found (NULL)
+ * answer of g_hash_table_lookup.
+ */
+ ret = g_hash_table_size(gr->graph_nodes) + 1;
+ g_hash_table_insert(gr->graph_nodes, node, (void *)ret);
+
+ return ret;
+}
+
+static void xdbg_graph_add_node(XDbgBlockGraphConstructor *gr, void *node,
+ XDbgBlockGraphNodeType type, const char *name)
+{
+ XDbgBlockGraphNode *n;
+
+ n = g_new0(XDbgBlockGraphNode, 1);
+
+ n->id = xdbg_graph_node_num(gr, node);
+ n->type = type;
+ n->name = g_strdup(name);
+
+ QAPI_LIST_ADD(gr->graph->nodes, n);
+}
+
+static void xdbg_graph_add_edge(XDbgBlockGraphConstructor *gr, void *parent,
+ const BdrvChild *child)
+{
+ typedef struct {
+ unsigned int flag;
+ BlockPermission num;
+ } PermissionMap;
+
+ static const PermissionMap permissions[] = {
+ { BLK_PERM_CONSISTENT_READ, BLOCK_PERMISSION_CONSISTENT_READ },
+ { BLK_PERM_WRITE, BLOCK_PERMISSION_WRITE },
+ { BLK_PERM_WRITE_UNCHANGED, BLOCK_PERMISSION_WRITE_UNCHANGED },
+ { BLK_PERM_RESIZE, BLOCK_PERMISSION_RESIZE },
+ { BLK_PERM_GRAPH_MOD, BLOCK_PERMISSION_GRAPH_MOD },
+ { 0, 0 }
+ };
+ const PermissionMap *p;
+ XDbgBlockGraphEdge *edge;
+
+ QEMU_BUILD_BUG_ON(1UL << (ARRAY_SIZE(permissions) - 1) != BLK_PERM_ALL + 1);
+
+ edge = g_new0(XDbgBlockGraphEdge, 1);
+
+ edge->parent = xdbg_graph_node_num(gr, parent);
+ edge->child = xdbg_graph_node_num(gr, child->bs);
+ edge->name = g_strdup(child->name);
+
+ for (p = permissions; p->flag; p++) {
+ if (p->flag & child->perm) {
+ QAPI_LIST_ADD(edge->perm, p->num);
+ }
+ if (p->flag & child->shared_perm) {
+ QAPI_LIST_ADD(edge->shared_perm, p->num);
+ }
+ }
+
+ QAPI_LIST_ADD(gr->graph->edges, edge);
+}
+
+
+XDbgBlockGraph *bdrv_get_xdbg_block_graph(Error **errp)
+{
+ BlockBackend *blk;
+ BlockJob *job;
+ BlockDriverState *bs;
+ BdrvChild *child;
+ XDbgBlockGraphConstructor *gr = xdbg_graph_new();
+
+ for (blk = blk_all_next(NULL); blk; blk = blk_all_next(blk)) {
+ char *allocated_name = NULL;
+ const char *name = blk_name(blk);
+
+ if (!*name) {
+ name = allocated_name = blk_get_attached_dev_id(blk);
+ }
+ xdbg_graph_add_node(gr, blk, X_DBG_BLOCK_GRAPH_NODE_TYPE_BLOCK_BACKEND,
+ name);
+ g_free(allocated_name);
+ if (blk_root(blk)) {
+ xdbg_graph_add_edge(gr, blk, blk_root(blk));
+ }
+ }
+
+ for (job = block_job_next(NULL); job; job = block_job_next(job)) {
+ GSList *el;
+
+ xdbg_graph_add_node(gr, job, X_DBG_BLOCK_GRAPH_NODE_TYPE_BLOCK_JOB,
+ job->job.id);
+ for (el = job->nodes; el; el = el->next) {
+ xdbg_graph_add_edge(gr, job, (BdrvChild *)el->data);
+ }
+ }
+
+ QTAILQ_FOREACH(bs, &graph_bdrv_states, node_list) {
+ xdbg_graph_add_node(gr, bs, X_DBG_BLOCK_GRAPH_NODE_TYPE_BLOCK_DRIVER,
+ bs->node_name);
+ QLIST_FOREACH(child, &bs->children, next) {
+ xdbg_graph_add_edge(gr, bs, child);
+ }
+ }
+
+ return xdbg_graph_finalize(gr);
+}
+
BlockDriverState *bdrv_lookup_bs(const char *device,
const char *node_name,
Error **errp)
return drv->bdrv_get_info(bs, bdi);
}
-ImageInfoSpecific *bdrv_get_specific_info(BlockDriverState *bs)
+ImageInfoSpecific *bdrv_get_specific_info(BlockDriverState *bs,
+ Error **errp)
{
BlockDriver *drv = bs->drv;
if (drv && drv->bdrv_get_specific_info) {
- return drv->bdrv_get_specific_info(bs);
+ return drv->bdrv_get_specific_info(bs, errp);
}
return NULL;
}
uint64_t perm, shared_perm;
Error *local_err = NULL;
int ret;
+ BdrvDirtyBitmap *bm;
if (!bs->drv) {
return;
}
}
+ for (bm = bdrv_dirty_bitmap_next(bs, NULL); bm;
+ bm = bdrv_dirty_bitmap_next(bs, bm))
+ {
+ bdrv_dirty_bitmap_set_migration(bm, false);
+ }
+
ret = refresh_total_sectors(bs, bs->total_sectors);
if (ret < 0) {
bs->open_flags |= BDRV_O_INACTIVE;
if (parent->role->activate) {
parent->role->activate(parent, &local_err);
if (local_err) {
+ bs->open_flags |= BDRV_O_INACTIVE;
error_propagate(errp, local_err);
return;
}
InvalidateCacheCo *ico = opaque;
bdrv_co_invalidate_cache(ico->bs, ico->errp);
ico->done = true;
+ aio_wait_kick();
}
void bdrv_invalidate_cache(BlockDriverState *bs, Error **errp)
bdrv_invalidate_cache_co_entry(&ico);
} else {
co = qemu_coroutine_create(bdrv_invalidate_cache_co_entry, &ico);
- qemu_coroutine_enter(co);
+ bdrv_coroutine_enter(bs, co);
BDRV_POLL_WHILE(bs, !ico.done);
}
}
}
}
-static int bdrv_inactivate_recurse(BlockDriverState *bs,
- bool setting_flag)
+static bool bdrv_has_bds_parent(BlockDriverState *bs, bool only_active)
+{
+ BdrvChild *parent;
+
+ QLIST_FOREACH(parent, &bs->parents, next_parent) {
+ if (parent->role->parent_is_bds) {
+ BlockDriverState *parent_bs = parent->opaque;
+ if (!only_active || !(parent_bs->open_flags & BDRV_O_INACTIVE)) {
+ return true;
+ }
+ }
+ }
+
+ return false;
+}
+
+static int bdrv_inactivate_recurse(BlockDriverState *bs)
{
BdrvChild *child, *parent;
+ uint64_t perm, shared_perm;
int ret;
if (!bs->drv) {
return -ENOMEDIUM;
}
- if (!setting_flag && bs->drv->bdrv_inactivate) {
+ /* Make sure that we don't inactivate a child before its parent.
+ * It will be covered by recursion from the yet active parent. */
+ if (bdrv_has_bds_parent(bs, true)) {
+ return 0;
+ }
+
+ assert(!(bs->open_flags & BDRV_O_INACTIVE));
+
+ /* Inactivate this node */
+ if (bs->drv->bdrv_inactivate) {
ret = bs->drv->bdrv_inactivate(bs);
if (ret < 0) {
return ret;
}
}
- if (setting_flag && !(bs->open_flags & BDRV_O_INACTIVE)) {
- uint64_t perm, shared_perm;
-
- QLIST_FOREACH(parent, &bs->parents, next_parent) {
- if (parent->role->inactivate) {
- ret = parent->role->inactivate(parent);
- if (ret < 0) {
- return ret;
- }
+ QLIST_FOREACH(parent, &bs->parents, next_parent) {
+ if (parent->role->inactivate) {
+ ret = parent->role->inactivate(parent);
+ if (ret < 0) {
+ return ret;
}
}
+ }
- bs->open_flags |= BDRV_O_INACTIVE;
+ bs->open_flags |= BDRV_O_INACTIVE;
+
+ /* Update permissions, they may differ for inactive nodes */
+ bdrv_get_cumulative_perm(bs, &perm, &shared_perm);
+ bdrv_check_perm(bs, NULL, perm, shared_perm, NULL, &error_abort);
+ bdrv_set_perm(bs, perm, shared_perm);
- /* Update permissions, they may differ for inactive nodes */
- bdrv_get_cumulative_perm(bs, &perm, &shared_perm);
- bdrv_check_perm(bs, NULL, perm, shared_perm, NULL, &error_abort);
- bdrv_set_perm(bs, perm, shared_perm);
- }
+ /* Recursively inactivate children */
QLIST_FOREACH(child, &bs->children, next) {
- ret = bdrv_inactivate_recurse(child->bs, setting_flag);
+ ret = bdrv_inactivate_recurse(child->bs);
if (ret < 0) {
return ret;
}
}
- /* At this point persistent bitmaps should be already stored by the format
- * driver */
- bdrv_release_persistent_dirty_bitmaps(bs);
-
return 0;
}
BlockDriverState *bs = NULL;
BdrvNextIterator it;
int ret = 0;
- int pass;
GSList *aio_ctxs = NULL, *ctx;
for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
}
}
- /* We do two passes of inactivation. The first pass calls to drivers'
- * .bdrv_inactivate callbacks recursively so all cache is flushed to disk;
- * the second pass sets the BDRV_O_INACTIVE flag so that no further write
- * is allowed. */
- for (pass = 0; pass < 2; pass++) {
- for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
- ret = bdrv_inactivate_recurse(bs, pass);
- if (ret < 0) {
- bdrv_next_cleanup(&it);
- goto out;
- }
+ for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
+ /* Nodes with BDS parents are covered by recursion from the last
+ * parent that gets inactivated. Don't inactivate them a second
+ * time if that has already happened. */
+ if (bdrv_has_bds_parent(bs, false)) {
+ continue;
+ }
+ ret = bdrv_inactivate_recurse(bs);
+ if (ret < 0) {
+ bdrv_next_cleanup(&it);
+ goto out;
}
}
assert((int) op >= 0 && op < BLOCK_OP_TYPE_MAX);
if (!QLIST_EMPTY(&bs->op_blockers[op])) {
blocker = QLIST_FIRST(&bs->op_blockers[op]);
- error_propagate(errp, error_copy(blocker->reason));
- error_prepend(errp, "Node '%s' is busy: ",
- bdrv_get_device_or_node_name(bs));
+ error_propagate_prepend(errp, error_copy(blocker->reason),
+ "Node '%s' is busy: ",
+ bdrv_get_device_or_node_name(bs));
return true;
}
return false;
if (options) {
qemu_opts_do_parse(opts, options, NULL, &local_err);
if (local_err) {
- error_report_err(local_err);
- local_err = NULL;
- error_setg(errp, "Invalid options for file format '%s'", fmt);
goto out;
}
}