const char *reference,
QDict *options, int flags,
BlockDriverState *parent,
- const BdrvChildRole *child_role,
+ const BdrvChildClass *child_class,
+ BdrvChildRole child_role,
Error **errp);
/* If non-zero, use only whitelisted block drivers */
void bdrv_register(BlockDriver *bdrv)
{
+ assert(bdrv->format_name);
QLIST_INSERT_HEAD(&bdrv_drivers, bdrv, list);
}
CreateCo *cco = opaque;
assert(cco->drv);
- ret = cco->drv->bdrv_co_create_opts(cco->filename, cco->opts, &local_err);
+ ret = cco->drv->bdrv_co_create_opts(cco->drv,
+ cco->filename, cco->opts, &local_err);
error_propagate(&cco->err, local_err);
cco->ret = ret;
}
return ret;
}
-int bdrv_create_file(const char *filename, QemuOpts *opts, Error **errp)
+/**
+ * Helper function for bdrv_create_file_fallback(): Resize @blk to at
+ * least the given @minimum_size.
+ *
+ * On success, return @blk's actual length.
+ * Otherwise, return -errno.
+ */
+static int64_t create_file_fallback_truncate(BlockBackend *blk,
+ int64_t minimum_size, Error **errp)
{
- BlockDriver *drv;
+ Error *local_err = NULL;
+ int64_t size;
+ int ret;
+
+ ret = blk_truncate(blk, minimum_size, false, PREALLOC_MODE_OFF, 0,
+ &local_err);
+ if (ret < 0 && ret != -ENOTSUP) {
+ error_propagate(errp, local_err);
+ return ret;
+ }
+
+ size = blk_getlength(blk);
+ if (size < 0) {
+ error_free(local_err);
+ error_setg_errno(errp, -size,
+ "Failed to inquire the new image file's length");
+ return size;
+ }
+
+ if (size < minimum_size) {
+ /* Need to grow the image, but we failed to do that */
+ error_propagate(errp, local_err);
+ return -ENOTSUP;
+ }
+
+ error_free(local_err);
+ local_err = NULL;
+
+ return size;
+}
+
+/**
+ * Helper function for bdrv_create_file_fallback(): Zero the first
+ * sector to remove any potentially pre-existing image header.
+ */
+static int create_file_fallback_zero_first_sector(BlockBackend *blk,
+ int64_t current_size,
+ Error **errp)
+{
+ int64_t bytes_to_clear;
+ int ret;
+
+ bytes_to_clear = MIN(current_size, BDRV_SECTOR_SIZE);
+ if (bytes_to_clear) {
+ ret = blk_pwrite_zeroes(blk, 0, bytes_to_clear, BDRV_REQ_MAY_UNMAP);
+ if (ret < 0) {
+ error_setg_errno(errp, -ret,
+ "Failed to clear the new image's first sector");
+ return ret;
+ }
+ }
+
+ return 0;
+}
+
+/**
+ * Simple implementation of bdrv_co_create_opts for protocol drivers
+ * which only support creation via opening a file
+ * (usually existing raw storage device)
+ */
+int coroutine_fn bdrv_co_create_opts_simple(BlockDriver *drv,
+ const char *filename,
+ QemuOpts *opts,
+ Error **errp)
+{
+ BlockBackend *blk;
+ QDict *options;
+ int64_t size = 0;
+ char *buf = NULL;
+ PreallocMode prealloc;
Error *local_err = NULL;
int ret;
+ size = qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0);
+ buf = qemu_opt_get_del(opts, BLOCK_OPT_PREALLOC);
+ prealloc = qapi_enum_parse(&PreallocMode_lookup, buf,
+ PREALLOC_MODE_OFF, &local_err);
+ g_free(buf);
+ if (local_err) {
+ error_propagate(errp, local_err);
+ return -EINVAL;
+ }
+
+ if (prealloc != PREALLOC_MODE_OFF) {
+ error_setg(errp, "Unsupported preallocation mode '%s'",
+ PreallocMode_str(prealloc));
+ return -ENOTSUP;
+ }
+
+ options = qdict_new();
+ qdict_put_str(options, "driver", drv->format_name);
+
+ blk = blk_new_open(filename, NULL, options,
+ BDRV_O_RDWR | BDRV_O_RESIZE, errp);
+ if (!blk) {
+ error_prepend(errp, "Protocol driver '%s' does not support image "
+ "creation, and opening the image failed: ",
+ drv->format_name);
+ return -EINVAL;
+ }
+
+ size = create_file_fallback_truncate(blk, size, errp);
+ if (size < 0) {
+ ret = size;
+ goto out;
+ }
+
+ ret = create_file_fallback_zero_first_sector(blk, size, errp);
+ if (ret < 0) {
+ goto out;
+ }
+
+ ret = 0;
+out:
+ blk_unref(blk);
+ return ret;
+}
+
+int bdrv_create_file(const char *filename, QemuOpts *opts, Error **errp)
+{
+ BlockDriver *drv;
+
drv = bdrv_find_protocol(filename, true, errp);
if (drv == NULL) {
return -ENOENT;
}
- ret = bdrv_create(drv, filename, opts, &local_err);
- error_propagate(errp, local_err);
+ return bdrv_create(drv, filename, opts, errp);
+}
+
+int coroutine_fn bdrv_co_delete_file(BlockDriverState *bs, Error **errp)
+{
+ Error *local_err = NULL;
+ int ret;
+
+ assert(bs != NULL);
+
+ if (!bs->drv) {
+ error_setg(errp, "Block node '%s' is not opened", bs->filename);
+ return -ENOMEDIUM;
+ }
+
+ if (!bs->drv->bdrv_co_delete_file) {
+ error_setg(errp, "Driver '%s' does not support image deletion",
+ bs->drv->format_name);
+ return -ENOTSUP;
+ }
+
+ ret = bs->drv->bdrv_co_delete_file(bs, &local_err);
+ if (ret < 0) {
+ error_propagate(errp, local_err);
+ }
+
return ret;
}
bdrv_drained_end_no_poll(bs, drained_end_counter);
}
-static void bdrv_child_cb_attach(BdrvChild *child)
-{
- BlockDriverState *bs = child->opaque;
- bdrv_apply_subtree_drain(child, bs);
-}
-
-static void bdrv_child_cb_detach(BdrvChild *child)
-{
- BlockDriverState *bs = child->opaque;
- bdrv_unapply_subtree_drain(child, bs);
-}
-
static int bdrv_child_cb_inactivate(BdrvChild *child)
{
BlockDriverState *bs = child->opaque;
*child_flags &= ~BDRV_O_NATIVE_AIO;
}
-/*
- * Returns the options and flags that bs->file should get if a protocol driver
- * is expected, based on the given options and flags for the parent BDS
- */
-static void bdrv_inherited_options(int *child_flags, QDict *child_options,
- int parent_flags, QDict *parent_options)
-{
- int flags = parent_flags;
-
- /* Enable protocol handling, disable format probing for bs->file */
- flags |= BDRV_O_PROTOCOL;
-
- /* If the cache mode isn't explicitly set, inherit direct and no-flush from
- * the parent. */
- qdict_copy_default(child_options, parent_options, BDRV_OPT_CACHE_DIRECT);
- qdict_copy_default(child_options, parent_options, BDRV_OPT_CACHE_NO_FLUSH);
- qdict_copy_default(child_options, parent_options, BDRV_OPT_FORCE_SHARE);
-
- /* 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
- * corresponding parent options. */
- qdict_set_default_str(child_options, BDRV_OPT_DISCARD, "unmap");
-
- /* Clear flags that only apply to the top layer */
- flags &= ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING | BDRV_O_COPY_ON_READ |
- BDRV_O_NO_IO);
-
- *child_flags = flags;
-}
-
-const BdrvChildRole child_file = {
- .parent_is_bds = true,
- .get_parent_desc = bdrv_child_get_parent_desc,
- .inherit_options = bdrv_inherited_options,
- .drained_begin = bdrv_child_cb_drained_begin,
- .drained_poll = bdrv_child_cb_drained_poll,
- .drained_end = bdrv_child_cb_drained_end,
- .attach = bdrv_child_cb_attach,
- .detach = bdrv_child_cb_detach,
- .inactivate = bdrv_child_cb_inactivate,
- .can_set_aio_ctx = bdrv_child_cb_can_set_aio_ctx,
- .set_aio_ctx = bdrv_child_cb_set_aio_ctx,
-};
-
-/*
- * Returns the options and flags that bs->file should get if the use of formats
- * (and not only protocols) is permitted for it, based on the given options and
- * flags for the parent BDS
- */
-static void bdrv_inherited_fmt_options(int *child_flags, QDict *child_options,
- int parent_flags, QDict *parent_options)
-{
- child_file.inherit_options(child_flags, child_options,
- parent_flags, parent_options);
-
- *child_flags &= ~(BDRV_O_PROTOCOL | BDRV_O_NO_IO);
-}
-
-const BdrvChildRole child_format = {
- .parent_is_bds = true,
- .get_parent_desc = bdrv_child_get_parent_desc,
- .inherit_options = bdrv_inherited_fmt_options,
- .drained_begin = bdrv_child_cb_drained_begin,
- .drained_poll = bdrv_child_cb_drained_poll,
- .drained_end = bdrv_child_cb_drained_end,
- .attach = bdrv_child_cb_attach,
- .detach = bdrv_child_cb_detach,
- .inactivate = bdrv_child_cb_inactivate,
- .can_set_aio_ctx = bdrv_child_cb_can_set_aio_ctx,
- .set_aio_ctx = bdrv_child_cb_set_aio_ctx,
-};
-
static void bdrv_backing_attach(BdrvChild *c)
{
BlockDriverState *parent = c->opaque;
parent->backing_blocker);
bdrv_op_unblock(backing_hd, BLOCK_OP_TYPE_BACKUP_TARGET,
parent->backing_blocker);
-
- bdrv_child_cb_attach(c);
}
static void bdrv_backing_detach(BdrvChild *c)
bdrv_op_unblock_all(c->bs, parent->backing_blocker);
error_free(parent->backing_blocker);
parent->backing_blocker = NULL;
-
- bdrv_child_cb_detach(c);
-}
-
-/*
- * Returns the options and flags that bs->backing should get, based on the
- * given options and flags for the parent BDS
- */
-static void bdrv_backing_options(int *child_flags, QDict *child_options,
- int parent_flags, QDict *parent_options)
-{
- int flags = parent_flags;
-
- /* The cache mode is inherited unmodified for backing files; except WCE,
- * which is only applied on the top level (BlockBackend) */
- qdict_copy_default(child_options, parent_options, BDRV_OPT_CACHE_DIRECT);
- qdict_copy_default(child_options, parent_options, BDRV_OPT_CACHE_NO_FLUSH);
- qdict_copy_default(child_options, parent_options, BDRV_OPT_FORCE_SHARE);
-
- /* 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 */
- flags &= ~(BDRV_O_SNAPSHOT | BDRV_O_TEMPORARY);
-
- *child_flags = flags;
}
static int bdrv_backing_update_filename(BdrvChild *c, BlockDriverState *base,
return ret;
}
-const BdrvChildRole child_backing = {
+/*
+ * Returns the options and flags that a generic child of a BDS should
+ * get, based on the given options and flags for the parent BDS.
+ */
+static void bdrv_inherited_options(BdrvChildRole role, bool parent_is_format,
+ int *child_flags, QDict *child_options,
+ int parent_flags, QDict *parent_options)
+{
+ int flags = parent_flags;
+
+ /*
+ * First, decide whether to set, clear, or leave BDRV_O_PROTOCOL.
+ * Generally, the question to answer is: Should this child be
+ * format-probed by default?
+ */
+
+ /*
+ * Pure and non-filtered data children of non-format nodes should
+ * be probed by default (even when the node itself has BDRV_O_PROTOCOL
+ * set). This only affects a very limited set of drivers (namely
+ * quorum and blkverify when this comment was written).
+ * Force-clear BDRV_O_PROTOCOL then.
+ */
+ if (!parent_is_format &&
+ (role & BDRV_CHILD_DATA) &&
+ !(role & (BDRV_CHILD_METADATA | BDRV_CHILD_FILTERED)))
+ {
+ flags &= ~BDRV_O_PROTOCOL;
+ }
+
+ /*
+ * All children of format nodes (except for COW children) and all
+ * metadata children in general should never be format-probed.
+ * Force-set BDRV_O_PROTOCOL then.
+ */
+ if ((parent_is_format && !(role & BDRV_CHILD_COW)) ||
+ (role & BDRV_CHILD_METADATA))
+ {
+ flags |= BDRV_O_PROTOCOL;
+ }
+
+ /*
+ * If the cache mode isn't explicitly set, inherit direct and no-flush from
+ * the parent.
+ */
+ qdict_copy_default(child_options, parent_options, BDRV_OPT_CACHE_DIRECT);
+ qdict_copy_default(child_options, parent_options, BDRV_OPT_CACHE_NO_FLUSH);
+ qdict_copy_default(child_options, parent_options, BDRV_OPT_FORCE_SHARE);
+
+ if (role & BDRV_CHILD_COW) {
+ /* backing files are opened read-only by default */
+ qdict_set_default_str(child_options, BDRV_OPT_READ_ONLY, "on");
+ qdict_set_default_str(child_options, BDRV_OPT_AUTO_READ_ONLY, "off");
+ } else {
+ /* 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);
+ }
+
+ /*
+ * bdrv_co_pdiscard() respects unmap policy for the parent, so we
+ * can default to enable it on lower layers regardless of the
+ * parent option.
+ */
+ qdict_set_default_str(child_options, BDRV_OPT_DISCARD, "unmap");
+
+ /* Clear flags that only apply to the top layer */
+ flags &= ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING | BDRV_O_COPY_ON_READ);
+
+ if (role & BDRV_CHILD_METADATA) {
+ flags &= ~BDRV_O_NO_IO;
+ }
+ if (role & BDRV_CHILD_COW) {
+ flags &= ~BDRV_O_TEMPORARY;
+ }
+
+ *child_flags = flags;
+}
+
+static void bdrv_child_cb_attach(BdrvChild *child)
+{
+ BlockDriverState *bs = child->opaque;
+
+ if (child->role & BDRV_CHILD_COW) {
+ bdrv_backing_attach(child);
+ }
+
+ bdrv_apply_subtree_drain(child, bs);
+}
+
+static void bdrv_child_cb_detach(BdrvChild *child)
+{
+ BlockDriverState *bs = child->opaque;
+
+ if (child->role & BDRV_CHILD_COW) {
+ bdrv_backing_detach(child);
+ }
+
+ bdrv_unapply_subtree_drain(child, bs);
+}
+
+static int bdrv_child_cb_update_filename(BdrvChild *c, BlockDriverState *base,
+ const char *filename, Error **errp)
+{
+ if (c->role & BDRV_CHILD_COW) {
+ return bdrv_backing_update_filename(c, base, filename, errp);
+ }
+ return 0;
+}
+
+const BdrvChildClass child_of_bds = {
.parent_is_bds = true,
.get_parent_desc = bdrv_child_get_parent_desc,
- .attach = bdrv_backing_attach,
- .detach = bdrv_backing_detach,
- .inherit_options = bdrv_backing_options,
+ .inherit_options = bdrv_inherited_options,
.drained_begin = bdrv_child_cb_drained_begin,
.drained_poll = bdrv_child_cb_drained_poll,
.drained_end = bdrv_child_cb_drained_end,
+ .attach = bdrv_child_cb_attach,
+ .detach = bdrv_child_cb_detach,
.inactivate = bdrv_child_cb_inactivate,
- .update_filename = bdrv_backing_update_filename,
.can_set_aio_ctx = bdrv_child_cb_can_set_aio_ctx,
.set_aio_ctx = bdrv_child_cb_set_aio_ctx,
+ .update_filename = bdrv_child_cb_update_filename,
};
static int bdrv_open_flags(BlockDriverState *bs, int flags)
},
};
+QemuOptsList bdrv_create_opts_simple = {
+ .name = "simple-create-opts",
+ .head = QTAILQ_HEAD_INITIALIZER(bdrv_create_opts_simple.head),
+ .desc = {
+ {
+ .name = BLOCK_OPT_SIZE,
+ .type = QEMU_OPT_SIZE,
+ .help = "Virtual disk size"
+ },
+ {
+ .name = BLOCK_OPT_PREALLOC,
+ .type = QEMU_OPT_STRING,
+ .help = "Preallocation mode (allowed values: off)"
+ },
+ { /* end of list */ }
+ }
+};
+
/*
* Common part for opening disk images and files
*
bool *tighten_restrictions, Error **errp);
static void bdrv_child_abort_perm_update(BdrvChild *c);
static void bdrv_child_set_perm(BdrvChild *c, uint64_t perm, uint64_t shared);
-static void bdrv_get_cumulative_perm(BlockDriverState *bs, uint64_t *perm,
- uint64_t *shared_perm);
typedef struct BlockReopenQueueEntry {
bool prepared;
}
static void bdrv_child_perm(BlockDriverState *bs, BlockDriverState *child_bs,
- BdrvChild *c, const BdrvChildRole *role,
+ BdrvChild *c, BdrvChildRole role,
BlockReopenQueue *reopen_queue,
uint64_t parent_perm, uint64_t parent_shared,
uint64_t *nperm, uint64_t *nshared)
}
}
-static void bdrv_get_cumulative_perm(BlockDriverState *bs, uint64_t *perm,
- uint64_t *shared_perm)
+void bdrv_get_cumulative_perm(BlockDriverState *bs, uint64_t *perm,
+ uint64_t *shared_perm)
{
BdrvChild *c;
uint64_t cumulative_perms = 0;
static char *bdrv_child_user_desc(BdrvChild *c)
{
- if (c->role->get_parent_desc) {
- return c->role->get_parent_desc(c);
+ if (c->klass->get_parent_desc) {
+ return c->klass->get_parent_desc(c);
}
return g_strdup("another user");
uint64_t perms, shared;
bdrv_get_cumulative_perm(bs, &parent_perms, &parent_shared);
- bdrv_child_perm(bs, c->bs, c, c->role, NULL, parent_perms, parent_shared,
- &perms, &shared);
+ bdrv_child_perm(bs, c->bs, c, c->role, NULL,
+ parent_perms, parent_shared, &perms, &shared);
return bdrv_child_try_set_perm(c, perms, shared, errp);
}
-void bdrv_filter_default_perms(BlockDriverState *bs, BdrvChild *c,
- const BdrvChildRole *role,
- BlockReopenQueue *reopen_queue,
- uint64_t perm, uint64_t shared,
- uint64_t *nperm, uint64_t *nshared)
+/*
+ * Default implementation for .bdrv_child_perm() for block filters:
+ * Forward CONSISTENT_READ, WRITE, WRITE_UNCHANGED, and RESIZE to the
+ * filtered child.
+ */
+static void bdrv_filter_default_perms(BlockDriverState *bs, BdrvChild *c,
+ BdrvChildRole role,
+ BlockReopenQueue *reopen_queue,
+ uint64_t perm, uint64_t shared,
+ uint64_t *nperm, uint64_t *nshared)
{
*nperm = perm & DEFAULT_PERM_PASSTHROUGH;
*nshared = (shared & DEFAULT_PERM_PASSTHROUGH) | DEFAULT_PERM_UNCHANGED;
}
-void bdrv_format_default_perms(BlockDriverState *bs, BdrvChild *c,
- const BdrvChildRole *role,
- BlockReopenQueue *reopen_queue,
- uint64_t perm, uint64_t shared,
- uint64_t *nperm, uint64_t *nshared)
+static void bdrv_default_perms_for_cow(BlockDriverState *bs, BdrvChild *c,
+ BdrvChildRole role,
+ BlockReopenQueue *reopen_queue,
+ uint64_t perm, uint64_t shared,
+ uint64_t *nperm, uint64_t *nshared)
{
- bool backing = (role == &child_backing);
- assert(role == &child_backing || role == &child_file);
+ assert(role & BDRV_CHILD_COW);
+
+ /*
+ * We want consistent read from backing files if the parent needs it.
+ * No other operations are performed on backing files.
+ */
+ perm &= BLK_PERM_CONSISTENT_READ;
- if (!backing) {
- int flags = bdrv_reopen_get_flags(reopen_queue, bs);
+ /*
+ * If the parent can deal with changing data, we're okay with a
+ * writable and resizable backing file.
+ * TODO Require !(perm & BLK_PERM_CONSISTENT_READ), too?
+ */
+ if (shared & BLK_PERM_WRITE) {
+ shared = BLK_PERM_WRITE | BLK_PERM_RESIZE;
+ } else {
+ shared = 0;
+ }
- /* Apart from the modifications below, the same permissions are
- * forwarded and left alone as for filters */
- bdrv_filter_default_perms(bs, c, role, reopen_queue, perm, shared,
- &perm, &shared);
+ shared |= BLK_PERM_CONSISTENT_READ | BLK_PERM_GRAPH_MOD |
+ BLK_PERM_WRITE_UNCHANGED;
+ if (bs->open_flags & BDRV_O_INACTIVE) {
+ shared |= BLK_PERM_WRITE | BLK_PERM_RESIZE;
+ }
+
+ *nperm = perm;
+ *nshared = shared;
+}
+
+static void bdrv_default_perms_for_storage(BlockDriverState *bs, BdrvChild *c,
+ BdrvChildRole role,
+ BlockReopenQueue *reopen_queue,
+ uint64_t perm, uint64_t shared,
+ uint64_t *nperm, uint64_t *nshared)
+{
+ int flags;
+
+ assert(role & (BDRV_CHILD_METADATA | BDRV_CHILD_DATA));
+
+ flags = bdrv_reopen_get_flags(reopen_queue, bs);
+
+ /*
+ * Apart from the modifications below, the same permissions are
+ * forwarded and left alone as for filters
+ */
+ bdrv_filter_default_perms(bs, c, role, reopen_queue,
+ perm, shared, &perm, &shared);
+
+ if (role & BDRV_CHILD_METADATA) {
/* Format drivers may touch metadata even if the guest doesn't write */
if (bdrv_is_writable_after_reopen(bs, reopen_queue)) {
perm |= BLK_PERM_WRITE | BLK_PERM_RESIZE;
}
- /* bs->file always needs to be consistent because of the metadata. We
- * can never allow other users to resize or write to it. */
+ /*
+ * bs->file always needs to be consistent because of the
+ * metadata. We can never allow other users to resize or write
+ * to it.
+ */
if (!(flags & BDRV_O_NO_IO)) {
perm |= BLK_PERM_CONSISTENT_READ;
}
shared &= ~(BLK_PERM_WRITE | BLK_PERM_RESIZE);
- } else {
- /* We want consistent read from backing files if the parent needs it.
- * No other operations are performed on backing files. */
- perm &= BLK_PERM_CONSISTENT_READ;
-
- /* If the parent can deal with changing data, we're okay with a
- * writable and resizable backing file. */
- /* TODO Require !(perm & BLK_PERM_CONSISTENT_READ), too? */
- if (shared & BLK_PERM_WRITE) {
- shared = BLK_PERM_WRITE | BLK_PERM_RESIZE;
- } else {
- shared = 0;
+ }
+
+ if (role & BDRV_CHILD_DATA) {
+ /*
+ * Technically, everything in this block is a subset of the
+ * BDRV_CHILD_METADATA path taken above, and so this could
+ * be an "else if" branch. However, that is not obvious, and
+ * this function is not performance critical, therefore we let
+ * this be an independent "if".
+ */
+
+ /*
+ * We cannot allow other users to resize the file because the
+ * format driver might have some assumptions about the size
+ * (e.g. because it is stored in metadata, or because the file
+ * is split into fixed-size data files).
+ */
+ shared &= ~BLK_PERM_RESIZE;
+
+ /*
+ * WRITE_UNCHANGED often cannot be performed as such on the
+ * data file. For example, the qcow2 driver may still need to
+ * write copied clusters on copy-on-read.
+ */
+ if (perm & BLK_PERM_WRITE_UNCHANGED) {
+ perm |= BLK_PERM_WRITE;
}
- shared |= BLK_PERM_CONSISTENT_READ | BLK_PERM_GRAPH_MOD |
- BLK_PERM_WRITE_UNCHANGED;
+ /*
+ * If the data file is written to, the format driver may
+ * expect to be able to resize it by writing beyond the EOF.
+ */
+ if (perm & BLK_PERM_WRITE) {
+ perm |= BLK_PERM_RESIZE;
+ }
}
if (bs->open_flags & BDRV_O_INACTIVE) {
*nshared = shared;
}
+void bdrv_default_perms(BlockDriverState *bs, BdrvChild *c,
+ BdrvChildRole role, BlockReopenQueue *reopen_queue,
+ uint64_t perm, uint64_t shared,
+ uint64_t *nperm, uint64_t *nshared)
+{
+ if (role & BDRV_CHILD_FILTERED) {
+ assert(!(role & (BDRV_CHILD_DATA | BDRV_CHILD_METADATA |
+ BDRV_CHILD_COW)));
+ bdrv_filter_default_perms(bs, c, role, reopen_queue,
+ perm, shared, nperm, nshared);
+ } else if (role & BDRV_CHILD_COW) {
+ assert(!(role & (BDRV_CHILD_DATA | BDRV_CHILD_METADATA)));
+ bdrv_default_perms_for_cow(bs, c, role, reopen_queue,
+ perm, shared, nperm, nshared);
+ } else if (role & (BDRV_CHILD_METADATA | BDRV_CHILD_DATA)) {
+ bdrv_default_perms_for_storage(bs, c, role, reopen_queue,
+ perm, shared, nperm, nshared);
+ } else {
+ g_assert_not_reached();
+ }
+}
+
uint64_t bdrv_qapi_perm_to_blk_perm(BlockPermission qapi_perm)
{
static const uint64_t permissions[] = {
* If the new child node is drained but the old one was not, flush
* all outstanding requests to the old child node.
*/
- while (drain_saldo > 0 && child->role->drained_begin) {
+ while (drain_saldo > 0 && child->klass->drained_begin) {
bdrv_parent_drained_begin_single(child, true);
drain_saldo--;
}
/* Detach first so that the recursive drain sections coming from @child
* are already gone and we only end the drain sections that came from
* elsewhere. */
- if (child->role->detach) {
- child->role->detach(child);
+ if (child->klass->detach) {
+ child->klass->detach(child);
}
QLIST_REMOVE(child, next_parent);
}
/* Attach only after starting new drained sections, so that recursive
* drain sections coming from @child don't get an extra .drained_begin
* callback. */
- if (child->role->attach) {
- child->role->attach(child);
+ if (child->klass->attach) {
+ child->klass->attach(child);
}
}
* If the old child node was drained but the new one is not, allow
* requests to come in only after the new node has been attached.
*/
- while (drain_saldo < 0 && child->role->drained_end) {
+ while (drain_saldo < 0 && child->klass->drained_end) {
bdrv_parent_drained_end_single(child);
drain_saldo++;
}
*/
BdrvChild *bdrv_root_attach_child(BlockDriverState *child_bs,
const char *child_name,
- const BdrvChildRole *child_role,
+ const BdrvChildClass *child_class,
+ BdrvChildRole child_role,
AioContext *ctx,
uint64_t perm, uint64_t shared_perm,
void *opaque, Error **errp)
*child = (BdrvChild) {
.bs = NULL,
.name = g_strdup(child_name),
+ .klass = child_class,
.role = child_role,
.perm = perm,
.shared_perm = shared_perm,
* try moving the parent into the AioContext of child_bs instead. */
if (bdrv_get_aio_context(child_bs) != ctx) {
ret = bdrv_try_set_aio_context(child_bs, ctx, &local_err);
- if (ret < 0 && child_role->can_set_aio_ctx) {
+ if (ret < 0 && child_class->can_set_aio_ctx) {
GSList *ignore = g_slist_prepend(NULL, child);
ctx = bdrv_get_aio_context(child_bs);
- if (child_role->can_set_aio_ctx(child, ctx, &ignore, NULL)) {
+ if (child_class->can_set_aio_ctx(child, ctx, &ignore, NULL)) {
error_free(local_err);
ret = 0;
g_slist_free(ignore);
ignore = g_slist_prepend(NULL, child);
- child_role->set_aio_ctx(child, ctx, &ignore);
+ child_class->set_aio_ctx(child, ctx, &ignore);
}
g_slist_free(ignore);
}
error_propagate(errp, local_err);
g_free(child);
bdrv_abort_perm_update(child_bs);
+ bdrv_unref(child_bs);
return NULL;
}
}
BdrvChild *bdrv_attach_child(BlockDriverState *parent_bs,
BlockDriverState *child_bs,
const char *child_name,
- const BdrvChildRole *child_role,
+ const BdrvChildClass *child_class,
+ BdrvChildRole child_role,
Error **errp)
{
BdrvChild *child;
bdrv_child_perm(parent_bs, child_bs, NULL, child_role, NULL,
perm, shared_perm, &perm, &shared_perm);
- child = bdrv_root_attach_child(child_bs, child_name, child_role,
- bdrv_get_aio_context(parent_bs),
+ child = bdrv_root_attach_child(child_bs, child_name, child_class,
+ child_role, bdrv_get_aio_context(parent_bs),
perm, shared_perm, parent_bs, errp);
if (child == NULL) {
return NULL;
static void bdrv_detach_child(BdrvChild *child)
{
- if (child->next.le_prev) {
- QLIST_REMOVE(child, next);
- child->next.le_prev = NULL;
- }
+ QLIST_SAFE_REMOVE(child, next);
bdrv_replace_child(child, NULL);
{
BdrvChild *c;
QLIST_FOREACH(c, &bs->parents, next_parent) {
- if (c->role->change_media) {
- c->role->change_media(c, load);
+ if (c->klass->change_media) {
+ c->klass->change_media(c, load);
}
}
}
return child != NULL;
}
+/*
+ * Return the BdrvChildRole for @bs's backing child. bs->backing is
+ * mostly used for COW backing children (role = COW), but also for
+ * filtered children (role = FILTERED | PRIMARY).
+ */
+static BdrvChildRole bdrv_backing_role(BlockDriverState *bs)
+{
+ if (bs->drv && bs->drv->is_filter) {
+ return BDRV_CHILD_FILTERED | BDRV_CHILD_PRIMARY;
+ } else {
+ return BDRV_CHILD_COW;
+ }
+}
+
/*
* 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().
if (bs->backing) {
bdrv_unref_child(bs, bs->backing);
+ bs->backing = NULL;
}
if (!backing_hd) {
- bs->backing = NULL;
goto out;
}
- bs->backing = bdrv_attach_child(bs, backing_hd, "backing", &child_backing,
- errp);
+ bs->backing = bdrv_attach_child(bs, backing_hd, "backing", &child_of_bds,
+ bdrv_backing_role(bs), 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). */
}
backing_hd = bdrv_open_inherit(backing_filename, reference, options, 0, bs,
- &child_backing, errp);
+ &child_of_bds, bdrv_backing_role(bs), errp);
if (!backing_hd) {
bs->open_flags |= BDRV_O_NO_BACKING;
error_prepend(errp, "Could not open backing file: ");
static BlockDriverState *
bdrv_open_child_bs(const char *filename, QDict *options, const char *bdref_key,
- BlockDriverState *parent, const BdrvChildRole *child_role,
- bool allow_none, Error **errp)
+ BlockDriverState *parent, const BdrvChildClass *child_class,
+ BdrvChildRole child_role, bool allow_none, Error **errp)
{
BlockDriverState *bs = NULL;
QDict *image_options;
char *bdref_key_dot;
const char *reference;
- assert(child_role != NULL);
+ assert(child_class != NULL);
bdref_key_dot = g_strdup_printf("%s.", bdref_key);
qdict_extract_subqdict(options, &image_options, bdref_key_dot);
}
bs = bdrv_open_inherit(filename, reference, image_options, 0,
- parent, child_role, errp);
+ parent, child_class, child_role, errp);
if (!bs) {
goto done;
}
BdrvChild *bdrv_open_child(const char *filename,
QDict *options, const char *bdref_key,
BlockDriverState *parent,
- const BdrvChildRole *child_role,
+ const BdrvChildClass *child_class,
+ BdrvChildRole child_role,
bool allow_none, Error **errp)
{
BlockDriverState *bs;
- bs = bdrv_open_child_bs(filename, options, bdref_key, parent, child_role,
- allow_none, errp);
+ bs = bdrv_open_child_bs(filename, options, bdref_key, parent, child_class,
+ child_role, allow_none, errp);
if (bs == NULL) {
return NULL;
}
- return bdrv_attach_child(parent, bs, bdref_key, child_role, errp);
+ return bdrv_attach_child(parent, bs, bdref_key, child_class, child_role,
+ errp);
}
-/* TODO Future callers may need to specify parent/child_role in order for
- * option inheritance to work. Existing callers use it for the root node. */
+/*
+ * TODO Future callers may need to specify parent/child_class in order for
+ * option inheritance to work. Existing callers use it for the root node.
+ */
BlockDriverState *bdrv_open_blockdev_ref(BlockdevRef *ref, Error **errp)
{
BlockDriverState *bs = NULL;
- Error *local_err = NULL;
QObject *obj = NULL;
QDict *qdict = NULL;
const char *reference = NULL;
assert(ref->type == QTYPE_QDICT);
v = qobject_output_visitor_new(&obj);
- visit_type_BlockdevOptions(v, NULL, &options, &local_err);
- if (local_err) {
- error_propagate(errp, local_err);
- goto fail;
- }
+ visit_type_BlockdevOptions(v, NULL, &options, &error_abort);
visit_complete(v, &obj);
qdict = qobject_to(QDict, obj);
}
- bs = bdrv_open_inherit(NULL, reference, qdict, 0, NULL, NULL, errp);
+ bs = bdrv_open_inherit(NULL, reference, qdict, 0, NULL, NULL, 0, errp);
obj = NULL;
-
-fail:
qobject_unref(obj);
visit_free(v);
return bs;
const char *reference,
QDict *options, int flags,
BlockDriverState *parent,
- const BdrvChildRole *child_role,
+ const BdrvChildClass *child_class,
+ BdrvChildRole child_role,
Error **errp)
{
int ret;
QDict *snapshot_options = NULL;
int snapshot_flags = 0;
- assert(!child_role || !flags);
- assert(!child_role == !parent);
+ assert(!child_class || !flags);
+ assert(!child_class == !parent);
if (reference) {
bool options_non_empty = options ? qdict_size(options) : false;
bs->explicit_options = qdict_clone_shallow(options);
- if (child_role) {
+ if (child_class) {
+ bool parent_is_format;
+
+ if (parent->drv) {
+ parent_is_format = parent->drv->is_format;
+ } else {
+ /*
+ * parent->drv is not set yet because this node is opened for
+ * (potential) format probing. That means that @parent is going
+ * to be a format node.
+ */
+ parent_is_format = true;
+ }
+
bs->inherits_from = parent;
- child_role->inherit_options(&flags, options,
- parent->open_flags, parent->options);
+ child_class->inherit_options(child_role, parent_is_format,
+ &flags, options,
+ parent->open_flags, parent->options);
}
ret = bdrv_fill_options(&options, filename, &flags, &local_err);
- if (local_err) {
+ if (ret < 0) {
goto fail;
}
flags, options);
/* Let bdrv_backing_options() override "read-only" */
qdict_del(options, BDRV_OPT_READ_ONLY);
- bdrv_backing_options(&flags, options, flags, options);
+ bdrv_inherited_options(BDRV_CHILD_COW, true,
+ &flags, options, flags, options);
}
bs->open_flags = flags;
BlockDriverState *file_bs;
file_bs = bdrv_open_child_bs(filename, options, "file", bs,
- &child_file, true, &local_err);
+ &child_of_bds, BDRV_CHILD_IMAGE,
+ true, &local_err);
if (local_err) {
goto fail;
}
QDict *options, int flags, Error **errp)
{
return bdrv_open_inherit(filename, reference, options, flags, NULL,
- NULL, errp);
+ NULL, 0, errp);
}
/* Return true if the NULL-terminated @list contains @str */
static BlockReopenQueue *bdrv_reopen_queue_child(BlockReopenQueue *bs_queue,
BlockDriverState *bs,
QDict *options,
- const BdrvChildRole *role,
+ const BdrvChildClass *klass,
+ BdrvChildRole role,
+ bool parent_is_format,
QDict *parent_options,
int parent_flags,
bool keep_old_opts)
/* Inherit from parent node */
if (parent_options) {
flags = 0;
- role->inherit_options(&flags, options, parent_flags, parent_options);
+ klass->inherit_options(role, parent_is_format, &flags, options,
+ parent_flags, parent_options);
} else {
flags = bdrv_get_flags(bs);
}
}
bdrv_reopen_queue_child(bs_queue, child->bs, new_child_options,
- child->role, options, flags, child_keep_old);
+ child->klass, child->role, bs->drv->is_format,
+ options, flags, child_keep_old);
}
return bs_queue;
BlockDriverState *bs,
QDict *options, bool keep_old_opts)
{
- return bdrv_reopen_queue_child(bs_queue, bs, options, NULL, NULL, 0,
- keep_old_opts);
+ return bdrv_reopen_queue_child(bs_queue, bs, options, NULL, 0, false,
+ NULL, 0, keep_old_opts);
}
/*
if (state->replace_backing_bs && state->new_backing_bs) {
uint64_t nperm, nshared;
bdrv_child_perm(state->bs, state->new_backing_bs,
- NULL, &child_backing, bs_queue,
- state->perm, state->shared_perm,
+ NULL, bdrv_backing_role(state->bs),
+ bs_queue, state->perm, state->shared_perm,
&nperm, &nshared);
ret = bdrv_check_update_perm(state->new_backing_bs, NULL,
nperm, nshared, NULL, NULL, errp);
}
}
}
+
+ if (ret == 0) {
+ QTAILQ_FOREACH_REVERSE(bs_entry, bs_queue, entry) {
+ BlockDriverState *bs = bs_entry->state.bs;
+
+ if (bs->drv->bdrv_reopen_commit_post)
+ bs->drv->bdrv_reopen_commit_post(&bs_entry->state);
+ }
+ }
cleanup:
QTAILQ_FOREACH_SAFE(bs_entry, bs_queue, entry, next) {
if (ret) {
*shared = cumulative_shared_perms;
}
+static bool bdrv_reopen_can_attach(BlockDriverState *parent,
+ BdrvChild *child,
+ BlockDriverState *new_child,
+ Error **errp)
+{
+ AioContext *parent_ctx = bdrv_get_aio_context(parent);
+ AioContext *child_ctx = bdrv_get_aio_context(new_child);
+ GSList *ignore;
+ bool ret;
+
+ ignore = g_slist_prepend(NULL, child);
+ ret = bdrv_can_set_aio_context(new_child, parent_ctx, &ignore, NULL);
+ g_slist_free(ignore);
+ if (ret) {
+ return ret;
+ }
+
+ ignore = g_slist_prepend(NULL, child);
+ ret = bdrv_can_set_aio_context(parent, child_ctx, &ignore, errp);
+ g_slist_free(ignore);
+ return ret;
+}
+
/*
* Take a BDRVReopenState and check if the value of 'backing' in the
* reopen_state->options QDict is valid or not.
}
/*
- * TODO: before removing the x- prefix from x-blockdev-reopen we
- * should move the new backing file into the right AioContext
- * instead of returning an error.
+ * Check AioContext compatibility so that the bdrv_set_backing_hd() call in
+ * bdrv_reopen_commit() won't fail.
*/
if (new_backing_bs) {
- if (bdrv_get_aio_context(new_backing_bs) != bdrv_get_aio_context(bs)) {
- error_setg(errp, "Cannot use a new backing file "
- "with a different AioContext");
+ if (!bdrv_reopen_can_attach(bs, bs->backing, new_backing_bs, errp)) {
return -EINVAL;
}
}
GHashTable *found;
bool ret;
- if (c->role->stay_at_node) {
+ if (c->klass->stay_at_node) {
return false;
}
bdrv_ref(from);
assert(qemu_get_current_aio_context() == qemu_get_aio_context());
+ assert(bdrv_get_aio_context(from) == bdrv_get_aio_context(to));
bdrv_drained_begin(from);
/* Put all parents into @list and calculate their cumulative permissions */
}
/* If so, update the backing file path in the image file */
- if (c->role->update_filename) {
- ret = c->role->update_filename(c, base, backing_file_str,
- &local_err);
+ if (c->klass->update_filename) {
+ ret = c->klass->update_filename(c, base, backing_file_str,
+ &local_err);
if (ret < 0) {
bdrv_abort_perm_update(base);
error_report_err(local_err);
}
/* Put this QMP function here so it can access the static graph_bdrv_states. */
-BlockDeviceInfoList *bdrv_named_nodes_list(Error **errp)
+BlockDeviceInfoList *bdrv_named_nodes_list(bool flat,
+ Error **errp)
{
BlockDeviceInfoList *list, *entry;
BlockDriverState *bs;
list = NULL;
QTAILQ_FOREACH(bs, &graph_bdrv_states, node_list) {
- BlockDeviceInfo *info = bdrv_block_device_info(NULL, bs, errp);
+ BlockDeviceInfo *info = bdrv_block_device_info(NULL, bs, flat, errp);
if (!info) {
qapi_free_BlockDeviceInfoList(list);
return NULL;
/* If multiple parents have a name, just pick the first one. */
QLIST_FOREACH(c, &bs->parents, next_parent) {
- if (c->role->get_name) {
- name = c->role->get_name(c);
+ if (c->klass->get_name) {
+ name = c->klass->get_name(c);
if (name && *name) {
return name;
}
return 0;
}
-int bdrv_has_zero_init_truncate(BlockDriverState *bs)
-{
- if (!bs->drv) {
- return 0;
- }
-
- if (bs->backing) {
- /* Depends on the backing image length, but better safe than sorry */
- return 0;
- }
- if (bs->drv->bdrv_has_zero_init_truncate) {
- return bs->drv->bdrv_has_zero_init_truncate(bs);
- }
- if (bs->file && bs->drv->is_filter) {
- return bdrv_has_zero_init_truncate(bs->file->bs);
- }
-
- /* safe default */
- return 0;
-}
-
bool bdrv_unallocated_blocks_are_zero(BlockDriverState *bs)
{
BlockDriverInfo bdi;
}
QLIST_FOREACH(parent, &bs->parents, next_parent) {
- if (parent->role->activate) {
- parent->role->activate(parent, &local_err);
+ if (parent->klass->activate) {
+ parent->klass->activate(parent, &local_err);
if (local_err) {
bs->open_flags |= BDRV_O_INACTIVE;
error_propagate(errp, local_err);
BdrvChild *parent;
QLIST_FOREACH(parent, &bs->parents, next_parent) {
- if (parent->role->parent_is_bds) {
+ if (parent->klass->parent_is_bds) {
BlockDriverState *parent_bs = parent->opaque;
if (!only_active || !(parent_bs->open_flags & BDRV_O_INACTIVE)) {
return true;
}
QLIST_FOREACH(parent, &bs->parents, next_parent) {
- if (parent->role->inactivate) {
- ret = parent->role->inactivate(parent);
+ if (parent->klass->inactivate) {
+ ret = parent->klass->inactivate(parent);
if (ret < 0) {
return ret;
}
if (g_slist_find(*ignore, child)) {
continue;
}
- assert(child->role->set_aio_ctx);
+ assert(child->klass->set_aio_ctx);
*ignore = g_slist_prepend(*ignore, child);
- child->role->set_aio_ctx(child, new_context, ignore);
+ child->klass->set_aio_ctx(child, new_context, ignore);
}
bdrv_detach_aio_context(bs);
}
*ignore = g_slist_prepend(*ignore, c);
- /* A BdrvChildRole that doesn't handle AioContext changes cannot
- * tolerate any AioContext changes */
- if (!c->role->can_set_aio_ctx) {
+ /*
+ * A BdrvChildClass that doesn't handle AioContext changes cannot
+ * tolerate any AioContext changes
+ */
+ if (!c->klass->can_set_aio_ctx) {
char *user = bdrv_child_user_desc(c);
error_setg(errp, "Changing iothreads is not supported by %s", user);
g_free(user);
return false;
}
- if (!c->role->can_set_aio_ctx(c, ctx, ignore, errp)) {
+ if (!c->klass->can_set_aio_ctx(c, ctx, ignore, errp)) {
assert(!errp || *errp);
return false;
}
drv->bdrv_gather_child_options(bs, opts, backing_overridden);
} else {
QLIST_FOREACH(child, &bs->children, next) {
- if (child->role == &child_backing && !backing_overridden) {
+ if (child == bs->backing && !backing_overridden) {
/* We can skip the backing BDS if it has not been overridden */
continue;
}
parent_bs->drv->bdrv_del_child(parent_bs, child, errp);
}
+
+int bdrv_make_empty(BdrvChild *c, Error **errp)
+{
+ BlockDriver *drv = c->bs->drv;
+ int ret;
+
+ assert(c->perm & (BLK_PERM_WRITE | BLK_PERM_WRITE_UNCHANGED));
+
+ if (!drv->bdrv_make_empty) {
+ error_setg(errp, "%s does not support emptying nodes",
+ drv->format_name);
+ return -ENOTSUP;
+ }
+
+ ret = drv->bdrv_make_empty(c->bs);
+ if (ret < 0) {
+ error_setg_errno(errp, -ret, "Failed to empty %s",
+ c->bs->filename);
+ return ret;
+ }
+
+ return 0;
+}